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_set_create(NULL
, _mesa_hash_pointer
,
79 _mesa_key_pointer_equal
);
82 vk_free(alloc
, list
->relocs
);
83 vk_free(alloc
, list
->reloc_bos
);
84 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
88 memcpy(list
->relocs
, other_list
->relocs
,
89 list
->array_length
* sizeof(*list
->relocs
));
90 memcpy(list
->reloc_bos
, other_list
->reloc_bos
,
91 list
->array_length
* sizeof(*list
->reloc_bos
));
92 struct set_entry
*entry
;
93 set_foreach(other_list
->deps
, entry
) {
94 _mesa_set_add_pre_hashed(list
->deps
, entry
->hash
, entry
->key
);
102 anv_reloc_list_init(struct anv_reloc_list
*list
,
103 const VkAllocationCallbacks
*alloc
)
105 return anv_reloc_list_init_clone(list
, alloc
, NULL
);
109 anv_reloc_list_finish(struct anv_reloc_list
*list
,
110 const VkAllocationCallbacks
*alloc
)
112 vk_free(alloc
, list
->relocs
);
113 vk_free(alloc
, list
->reloc_bos
);
114 _mesa_set_destroy(list
->deps
, NULL
);
118 anv_reloc_list_grow(struct anv_reloc_list
*list
,
119 const VkAllocationCallbacks
*alloc
,
120 size_t num_additional_relocs
)
122 if (list
->num_relocs
+ num_additional_relocs
<= list
->array_length
)
125 size_t new_length
= list
->array_length
* 2;
126 while (new_length
< list
->num_relocs
+ num_additional_relocs
)
129 struct drm_i915_gem_relocation_entry
*new_relocs
=
130 vk_alloc(alloc
, new_length
* sizeof(*list
->relocs
), 8,
131 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
132 if (new_relocs
== NULL
)
133 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
135 struct anv_bo
**new_reloc_bos
=
136 vk_alloc(alloc
, new_length
* sizeof(*list
->reloc_bos
), 8,
137 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
138 if (new_reloc_bos
== NULL
) {
139 vk_free(alloc
, new_relocs
);
140 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
143 memcpy(new_relocs
, list
->relocs
, list
->num_relocs
* sizeof(*list
->relocs
));
144 memcpy(new_reloc_bos
, list
->reloc_bos
,
145 list
->num_relocs
* sizeof(*list
->reloc_bos
));
147 vk_free(alloc
, list
->relocs
);
148 vk_free(alloc
, list
->reloc_bos
);
150 list
->array_length
= new_length
;
151 list
->relocs
= new_relocs
;
152 list
->reloc_bos
= new_reloc_bos
;
158 anv_reloc_list_add(struct anv_reloc_list
*list
,
159 const VkAllocationCallbacks
*alloc
,
160 uint32_t offset
, struct anv_bo
*target_bo
, uint32_t delta
)
162 struct drm_i915_gem_relocation_entry
*entry
;
165 if (target_bo
->flags
& EXEC_OBJECT_PINNED
) {
166 _mesa_set_add(list
->deps
, target_bo
);
170 VkResult result
= anv_reloc_list_grow(list
, alloc
, 1);
171 if (result
!= VK_SUCCESS
)
174 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
175 index
= list
->num_relocs
++;
176 list
->reloc_bos
[index
] = target_bo
;
177 entry
= &list
->relocs
[index
];
178 entry
->target_handle
= target_bo
->gem_handle
;
179 entry
->delta
= delta
;
180 entry
->offset
= offset
;
181 entry
->presumed_offset
= target_bo
->offset
;
182 entry
->read_domains
= 0;
183 entry
->write_domain
= 0;
184 VG(VALGRIND_CHECK_MEM_IS_DEFINED(entry
, sizeof(*entry
)));
190 anv_reloc_list_append(struct anv_reloc_list
*list
,
191 const VkAllocationCallbacks
*alloc
,
192 struct anv_reloc_list
*other
, uint32_t offset
)
194 VkResult result
= anv_reloc_list_grow(list
, alloc
, other
->num_relocs
);
195 if (result
!= VK_SUCCESS
)
198 memcpy(&list
->relocs
[list
->num_relocs
], &other
->relocs
[0],
199 other
->num_relocs
* sizeof(other
->relocs
[0]));
200 memcpy(&list
->reloc_bos
[list
->num_relocs
], &other
->reloc_bos
[0],
201 other
->num_relocs
* sizeof(other
->reloc_bos
[0]));
203 for (uint32_t i
= 0; i
< other
->num_relocs
; i
++)
204 list
->relocs
[i
+ list
->num_relocs
].offset
+= offset
;
206 list
->num_relocs
+= other
->num_relocs
;
208 struct set_entry
*entry
;
209 set_foreach(other
->deps
, entry
) {
210 _mesa_set_add_pre_hashed(list
->deps
, entry
->hash
, entry
->key
);
216 /*-----------------------------------------------------------------------*
217 * Functions related to anv_batch
218 *-----------------------------------------------------------------------*/
221 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
223 if (batch
->next
+ num_dwords
* 4 > batch
->end
) {
224 VkResult result
= batch
->extend_cb(batch
, batch
->user_data
);
225 if (result
!= VK_SUCCESS
) {
226 anv_batch_set_error(batch
, result
);
231 void *p
= batch
->next
;
233 batch
->next
+= num_dwords
* 4;
234 assert(batch
->next
<= batch
->end
);
240 anv_batch_emit_reloc(struct anv_batch
*batch
,
241 void *location
, struct anv_bo
*bo
, uint32_t delta
)
243 VkResult result
= anv_reloc_list_add(batch
->relocs
, batch
->alloc
,
244 location
- batch
->start
, bo
, delta
);
245 if (result
!= VK_SUCCESS
) {
246 anv_batch_set_error(batch
, result
);
250 return bo
->offset
+ delta
;
254 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
256 uint32_t size
, offset
;
258 size
= other
->next
- other
->start
;
259 assert(size
% 4 == 0);
261 if (batch
->next
+ size
> batch
->end
) {
262 VkResult result
= batch
->extend_cb(batch
, batch
->user_data
);
263 if (result
!= VK_SUCCESS
) {
264 anv_batch_set_error(batch
, result
);
269 assert(batch
->next
+ size
<= batch
->end
);
271 VG(VALGRIND_CHECK_MEM_IS_DEFINED(other
->start
, size
));
272 memcpy(batch
->next
, other
->start
, size
);
274 offset
= batch
->next
- batch
->start
;
275 VkResult result
= anv_reloc_list_append(batch
->relocs
, batch
->alloc
,
276 other
->relocs
, offset
);
277 if (result
!= VK_SUCCESS
) {
278 anv_batch_set_error(batch
, result
);
285 /*-----------------------------------------------------------------------*
286 * Functions related to anv_batch_bo
287 *-----------------------------------------------------------------------*/
290 anv_batch_bo_create(struct anv_cmd_buffer
*cmd_buffer
,
291 struct anv_batch_bo
**bbo_out
)
295 struct anv_batch_bo
*bbo
= vk_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
296 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
298 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
300 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
,
301 ANV_CMD_BUFFER_BATCH_SIZE
);
302 if (result
!= VK_SUCCESS
)
305 result
= anv_reloc_list_init(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
306 if (result
!= VK_SUCCESS
)
314 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
316 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
322 anv_batch_bo_clone(struct anv_cmd_buffer
*cmd_buffer
,
323 const struct anv_batch_bo
*other_bbo
,
324 struct anv_batch_bo
**bbo_out
)
328 struct anv_batch_bo
*bbo
= vk_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
329 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
331 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
333 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
,
335 if (result
!= VK_SUCCESS
)
338 result
= anv_reloc_list_init_clone(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
,
340 if (result
!= VK_SUCCESS
)
343 bbo
->length
= other_bbo
->length
;
344 memcpy(bbo
->bo
.map
, other_bbo
->bo
.map
, other_bbo
->length
);
351 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
353 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
359 anv_batch_bo_start(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
360 size_t batch_padding
)
362 batch
->next
= batch
->start
= bbo
->bo
.map
;
363 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
364 batch
->relocs
= &bbo
->relocs
;
365 bbo
->relocs
.num_relocs
= 0;
366 _mesa_set_clear(bbo
->relocs
.deps
, NULL
);
370 anv_batch_bo_continue(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
371 size_t batch_padding
)
373 batch
->start
= bbo
->bo
.map
;
374 batch
->next
= bbo
->bo
.map
+ bbo
->length
;
375 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
376 batch
->relocs
= &bbo
->relocs
;
380 anv_batch_bo_finish(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
)
382 assert(batch
->start
== bbo
->bo
.map
);
383 bbo
->length
= batch
->next
- batch
->start
;
384 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch
->start
, bbo
->length
));
388 anv_batch_bo_grow(struct anv_cmd_buffer
*cmd_buffer
, struct anv_batch_bo
*bbo
,
389 struct anv_batch
*batch
, size_t aditional
,
390 size_t batch_padding
)
392 assert(batch
->start
== bbo
->bo
.map
);
393 bbo
->length
= batch
->next
- batch
->start
;
395 size_t new_size
= bbo
->bo
.size
;
396 while (new_size
<= bbo
->length
+ aditional
+ batch_padding
)
399 if (new_size
== bbo
->bo
.size
)
402 struct anv_bo new_bo
;
403 VkResult result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
,
405 if (result
!= VK_SUCCESS
)
408 memcpy(new_bo
.map
, bbo
->bo
.map
, bbo
->length
);
410 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
413 anv_batch_bo_continue(bbo
, batch
, batch_padding
);
419 anv_batch_bo_link(struct anv_cmd_buffer
*cmd_buffer
,
420 struct anv_batch_bo
*prev_bbo
,
421 struct anv_batch_bo
*next_bbo
,
422 uint32_t next_bbo_offset
)
424 MAYBE_UNUSED
const uint32_t bb_start_offset
=
425 prev_bbo
->length
- GEN8_MI_BATCH_BUFFER_START_length
* 4;
426 MAYBE_UNUSED
const uint32_t *bb_start
= prev_bbo
->bo
.map
+ bb_start_offset
;
428 /* Make sure we're looking at a MI_BATCH_BUFFER_START */
429 assert(((*bb_start
>> 29) & 0x07) == 0);
430 assert(((*bb_start
>> 23) & 0x3f) == 49);
432 uint32_t reloc_idx
= prev_bbo
->relocs
.num_relocs
- 1;
433 assert(prev_bbo
->relocs
.relocs
[reloc_idx
].offset
== bb_start_offset
+ 4);
435 prev_bbo
->relocs
.reloc_bos
[reloc_idx
] = &next_bbo
->bo
;
436 prev_bbo
->relocs
.relocs
[reloc_idx
].delta
= next_bbo_offset
;
438 /* Use a bogus presumed offset to force a relocation */
439 prev_bbo
->relocs
.relocs
[reloc_idx
].presumed_offset
= -1;
443 anv_batch_bo_destroy(struct anv_batch_bo
*bbo
,
444 struct anv_cmd_buffer
*cmd_buffer
)
446 anv_reloc_list_finish(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
447 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
448 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
452 anv_batch_bo_list_clone(const struct list_head
*list
,
453 struct anv_cmd_buffer
*cmd_buffer
,
454 struct list_head
*new_list
)
456 VkResult result
= VK_SUCCESS
;
458 list_inithead(new_list
);
460 struct anv_batch_bo
*prev_bbo
= NULL
;
461 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
462 struct anv_batch_bo
*new_bbo
= NULL
;
463 result
= anv_batch_bo_clone(cmd_buffer
, bbo
, &new_bbo
);
464 if (result
!= VK_SUCCESS
)
466 list_addtail(&new_bbo
->link
, new_list
);
469 anv_batch_bo_link(cmd_buffer
, prev_bbo
, new_bbo
, 0);
474 if (result
!= VK_SUCCESS
) {
475 list_for_each_entry_safe(struct anv_batch_bo
, bbo
, new_list
, link
)
476 anv_batch_bo_destroy(bbo
, cmd_buffer
);
482 /*-----------------------------------------------------------------------*
483 * Functions related to anv_batch_bo
484 *-----------------------------------------------------------------------*/
486 static struct anv_batch_bo
*
487 anv_cmd_buffer_current_batch_bo(struct anv_cmd_buffer
*cmd_buffer
)
489 return LIST_ENTRY(struct anv_batch_bo
, cmd_buffer
->batch_bos
.prev
, link
);
493 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer
*cmd_buffer
)
495 struct anv_state
*bt_block
= u_vector_head(&cmd_buffer
->bt_block_states
);
496 return (struct anv_address
) {
497 .bo
= &anv_binding_table_pool(cmd_buffer
->device
)->block_pool
.bo
,
498 .offset
= bt_block
->offset
,
503 emit_batch_buffer_start(struct anv_cmd_buffer
*cmd_buffer
,
504 struct anv_bo
*bo
, uint32_t offset
)
506 /* In gen8+ the address field grew to two dwords to accomodate 48 bit
507 * offsets. The high 16 bits are in the last dword, so we can use the gen8
508 * version in either case, as long as we set the instruction length in the
509 * header accordingly. This means that we always emit three dwords here
510 * and all the padding and adjustment we do in this file works for all
514 #define GEN7_MI_BATCH_BUFFER_START_length 2
515 #define GEN7_MI_BATCH_BUFFER_START_length_bias 2
517 const uint32_t gen7_length
=
518 GEN7_MI_BATCH_BUFFER_START_length
- GEN7_MI_BATCH_BUFFER_START_length_bias
;
519 const uint32_t gen8_length
=
520 GEN8_MI_BATCH_BUFFER_START_length
- GEN8_MI_BATCH_BUFFER_START_length_bias
;
522 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_BATCH_BUFFER_START
, bbs
) {
523 bbs
.DWordLength
= cmd_buffer
->device
->info
.gen
< 8 ?
524 gen7_length
: gen8_length
;
525 bbs
._2ndLevelBatchBuffer
= _1stlevelbatch
;
526 bbs
.AddressSpaceIndicator
= ASI_PPGTT
;
527 bbs
.BatchBufferStartAddress
= (struct anv_address
) { bo
, offset
};
532 cmd_buffer_chain_to_batch_bo(struct anv_cmd_buffer
*cmd_buffer
,
533 struct anv_batch_bo
*bbo
)
535 struct anv_batch
*batch
= &cmd_buffer
->batch
;
536 struct anv_batch_bo
*current_bbo
=
537 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
539 /* We set the end of the batch a little short so we would be sure we
540 * have room for the chaining command. Since we're about to emit the
541 * chaining command, let's set it back where it should go.
543 batch
->end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
544 assert(batch
->end
== current_bbo
->bo
.map
+ current_bbo
->bo
.size
);
546 emit_batch_buffer_start(cmd_buffer
, &bbo
->bo
, 0);
548 anv_batch_bo_finish(current_bbo
, batch
);
552 anv_cmd_buffer_chain_batch(struct anv_batch
*batch
, void *_data
)
554 struct anv_cmd_buffer
*cmd_buffer
= _data
;
555 struct anv_batch_bo
*new_bbo
;
557 VkResult result
= anv_batch_bo_create(cmd_buffer
, &new_bbo
);
558 if (result
!= VK_SUCCESS
)
561 struct anv_batch_bo
**seen_bbo
= u_vector_add(&cmd_buffer
->seen_bbos
);
562 if (seen_bbo
== NULL
) {
563 anv_batch_bo_destroy(new_bbo
, cmd_buffer
);
564 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
568 cmd_buffer_chain_to_batch_bo(cmd_buffer
, new_bbo
);
570 list_addtail(&new_bbo
->link
, &cmd_buffer
->batch_bos
);
572 anv_batch_bo_start(new_bbo
, batch
, GEN8_MI_BATCH_BUFFER_START_length
* 4);
578 anv_cmd_buffer_grow_batch(struct anv_batch
*batch
, void *_data
)
580 struct anv_cmd_buffer
*cmd_buffer
= _data
;
581 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
583 anv_batch_bo_grow(cmd_buffer
, bbo
, &cmd_buffer
->batch
, 4096,
584 GEN8_MI_BATCH_BUFFER_START_length
* 4);
589 /** Allocate a binding table
591 * This function allocates a binding table. This is a bit more complicated
592 * than one would think due to a combination of Vulkan driver design and some
593 * unfortunate hardware restrictions.
595 * The 3DSTATE_BINDING_TABLE_POINTERS_* packets only have a 16-bit field for
596 * the binding table pointer which means that all binding tables need to live
597 * in the bottom 64k of surface state base address. The way the GL driver has
598 * classically dealt with this restriction is to emit all surface states
599 * on-the-fly into the batch and have a batch buffer smaller than 64k. This
600 * isn't really an option in Vulkan for a couple of reasons:
602 * 1) In Vulkan, we have growing (or chaining) batches so surface states have
603 * to live in their own buffer and we have to be able to re-emit
604 * STATE_BASE_ADDRESS as needed which requires a full pipeline stall. In
605 * order to avoid emitting STATE_BASE_ADDRESS any more often than needed
606 * (it's not that hard to hit 64k of just binding tables), we allocate
607 * surface state objects up-front when VkImageView is created. In order
608 * for this to work, surface state objects need to be allocated from a
611 * 2) We tried to design the surface state system in such a way that it's
612 * already ready for bindless texturing. The way bindless texturing works
613 * on our hardware is that you have a big pool of surface state objects
614 * (with its own state base address) and the bindless handles are simply
615 * offsets into that pool. With the architecture we chose, we already
616 * have that pool and it's exactly the same pool that we use for regular
617 * surface states so we should already be ready for bindless.
619 * 3) For render targets, we need to be able to fill out the surface states
620 * later in vkBeginRenderPass so that we can assign clear colors
621 * correctly. One way to do this would be to just create the surface
622 * state data and then repeatedly copy it into the surface state BO every
623 * time we have to re-emit STATE_BASE_ADDRESS. While this works, it's
624 * rather annoying and just being able to allocate them up-front and
625 * re-use them for the entire render pass.
627 * While none of these are technically blockers for emitting state on the fly
628 * like we do in GL, the ability to have a single surface state pool is
629 * simplifies things greatly. Unfortunately, it comes at a cost...
631 * Because of the 64k limitation of 3DSTATE_BINDING_TABLE_POINTERS_*, we can't
632 * place the binding tables just anywhere in surface state base address.
633 * Because 64k isn't a whole lot of space, we can't simply restrict the
634 * surface state buffer to 64k, we have to be more clever. The solution we've
635 * chosen is to have a block pool with a maximum size of 2G that starts at
636 * zero and grows in both directions. All surface states are allocated from
637 * the top of the pool (positive offsets) and we allocate blocks (< 64k) of
638 * binding tables from the bottom of the pool (negative offsets). Every time
639 * we allocate a new binding table block, we set surface state base address to
640 * point to the bottom of the binding table block. This way all of the
641 * binding tables in the block are in the bottom 64k of surface state base
642 * address. When we fill out the binding table, we add the distance between
643 * the bottom of our binding table block and zero of the block pool to the
644 * surface state offsets so that they are correct relative to out new surface
645 * state base address at the bottom of the binding table block.
647 * \see adjust_relocations_from_block_pool()
648 * \see adjust_relocations_too_block_pool()
650 * \param[in] entries The number of surface state entries the binding
651 * table should be able to hold.
653 * \param[out] state_offset The offset surface surface state base address
654 * where the surface states live. This must be
655 * added to the surface state offset when it is
656 * written into the binding table entry.
658 * \return An anv_state representing the binding table
661 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
662 uint32_t entries
, uint32_t *state_offset
)
664 struct anv_device
*device
= cmd_buffer
->device
;
665 struct anv_state_pool
*state_pool
= &device
->surface_state_pool
;
666 struct anv_state
*bt_block
= u_vector_head(&cmd_buffer
->bt_block_states
);
667 struct anv_state state
;
669 state
.alloc_size
= align_u32(entries
* 4, 32);
671 if (cmd_buffer
->bt_next
+ state
.alloc_size
> state_pool
->block_size
)
672 return (struct anv_state
) { 0 };
674 state
.offset
= cmd_buffer
->bt_next
;
675 state
.map
= anv_binding_table_pool(device
)->block_pool
.map
+
676 bt_block
->offset
+ state
.offset
;
678 cmd_buffer
->bt_next
+= state
.alloc_size
;
680 if (device
->instance
->physicalDevice
.use_softpin
) {
681 assert(bt_block
->offset
>= 0);
682 *state_offset
= device
->surface_state_pool
.block_pool
.start_address
-
683 device
->binding_table_pool
.block_pool
.start_address
- bt_block
->offset
;
685 assert(bt_block
->offset
< 0);
686 *state_offset
= -bt_block
->offset
;
693 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
)
695 struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
696 return anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
697 isl_dev
->ss
.size
, isl_dev
->ss
.align
);
701 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer
*cmd_buffer
,
702 uint32_t size
, uint32_t alignment
)
704 return anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
709 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer
*cmd_buffer
)
711 struct anv_state
*bt_block
= u_vector_add(&cmd_buffer
->bt_block_states
);
712 if (bt_block
== NULL
) {
713 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
714 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
717 *bt_block
= anv_binding_table_pool_alloc(cmd_buffer
->device
);
718 cmd_buffer
->bt_next
= 0;
724 anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
726 struct anv_batch_bo
*batch_bo
;
729 list_inithead(&cmd_buffer
->batch_bos
);
731 result
= anv_batch_bo_create(cmd_buffer
, &batch_bo
);
732 if (result
!= VK_SUCCESS
)
735 list_addtail(&batch_bo
->link
, &cmd_buffer
->batch_bos
);
737 cmd_buffer
->batch
.alloc
= &cmd_buffer
->pool
->alloc
;
738 cmd_buffer
->batch
.user_data
= cmd_buffer
;
740 if (cmd_buffer
->device
->can_chain_batches
) {
741 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_chain_batch
;
743 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_grow_batch
;
746 anv_batch_bo_start(batch_bo
, &cmd_buffer
->batch
,
747 GEN8_MI_BATCH_BUFFER_START_length
* 4);
749 int success
= u_vector_init(&cmd_buffer
->seen_bbos
,
750 sizeof(struct anv_bo
*),
751 8 * sizeof(struct anv_bo
*));
755 *(struct anv_batch_bo
**)u_vector_add(&cmd_buffer
->seen_bbos
) = batch_bo
;
757 /* u_vector requires power-of-two size elements */
758 unsigned pow2_state_size
= util_next_power_of_two(sizeof(struct anv_state
));
759 success
= u_vector_init(&cmd_buffer
->bt_block_states
,
760 pow2_state_size
, 8 * pow2_state_size
);
764 result
= anv_reloc_list_init(&cmd_buffer
->surface_relocs
,
765 &cmd_buffer
->pool
->alloc
);
766 if (result
!= VK_SUCCESS
)
768 cmd_buffer
->last_ss_pool_center
= 0;
770 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
771 if (result
!= VK_SUCCESS
)
777 u_vector_finish(&cmd_buffer
->bt_block_states
);
779 u_vector_finish(&cmd_buffer
->seen_bbos
);
781 anv_batch_bo_destroy(batch_bo
, cmd_buffer
);
787 anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
789 struct anv_state
*bt_block
;
790 u_vector_foreach(bt_block
, &cmd_buffer
->bt_block_states
)
791 anv_binding_table_pool_free(cmd_buffer
->device
, *bt_block
);
792 u_vector_finish(&cmd_buffer
->bt_block_states
);
794 anv_reloc_list_finish(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
);
796 u_vector_finish(&cmd_buffer
->seen_bbos
);
798 /* Destroy all of the batch buffers */
799 list_for_each_entry_safe(struct anv_batch_bo
, bbo
,
800 &cmd_buffer
->batch_bos
, link
) {
801 anv_batch_bo_destroy(bbo
, cmd_buffer
);
806 anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
808 /* Delete all but the first batch bo */
809 assert(!list_empty(&cmd_buffer
->batch_bos
));
810 while (cmd_buffer
->batch_bos
.next
!= cmd_buffer
->batch_bos
.prev
) {
811 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
812 list_del(&bbo
->link
);
813 anv_batch_bo_destroy(bbo
, cmd_buffer
);
815 assert(!list_empty(&cmd_buffer
->batch_bos
));
817 anv_batch_bo_start(anv_cmd_buffer_current_batch_bo(cmd_buffer
),
819 GEN8_MI_BATCH_BUFFER_START_length
* 4);
821 while (u_vector_length(&cmd_buffer
->bt_block_states
) > 1) {
822 struct anv_state
*bt_block
= u_vector_remove(&cmd_buffer
->bt_block_states
);
823 anv_binding_table_pool_free(cmd_buffer
->device
, *bt_block
);
825 assert(u_vector_length(&cmd_buffer
->bt_block_states
) == 1);
826 cmd_buffer
->bt_next
= 0;
828 cmd_buffer
->surface_relocs
.num_relocs
= 0;
829 _mesa_set_clear(cmd_buffer
->surface_relocs
.deps
, NULL
);
830 cmd_buffer
->last_ss_pool_center
= 0;
832 /* Reset the list of seen buffers */
833 cmd_buffer
->seen_bbos
.head
= 0;
834 cmd_buffer
->seen_bbos
.tail
= 0;
836 *(struct anv_batch_bo
**)u_vector_add(&cmd_buffer
->seen_bbos
) =
837 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
841 anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer
*cmd_buffer
)
843 struct anv_batch_bo
*batch_bo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
845 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
) {
846 /* When we start a batch buffer, we subtract a certain amount of
847 * padding from the end to ensure that we always have room to emit a
848 * BATCH_BUFFER_START to chain to the next BO. We need to remove
849 * that padding before we end the batch; otherwise, we may end up
850 * with our BATCH_BUFFER_END in another BO.
852 cmd_buffer
->batch
.end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
853 assert(cmd_buffer
->batch
.end
== batch_bo
->bo
.map
+ batch_bo
->bo
.size
);
855 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_BATCH_BUFFER_END
, bbe
);
857 /* Round batch up to an even number of dwords. */
858 if ((cmd_buffer
->batch
.next
- cmd_buffer
->batch
.start
) & 4)
859 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_NOOP
, noop
);
861 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_PRIMARY
;
863 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
);
864 /* If this is a secondary command buffer, we need to determine the
865 * mode in which it will be executed with vkExecuteCommands. We
866 * determine this statically here so that this stays in sync with the
867 * actual ExecuteCommands implementation.
869 const uint32_t length
= cmd_buffer
->batch
.next
- cmd_buffer
->batch
.start
;
870 if (!cmd_buffer
->device
->can_chain_batches
) {
871 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
;
872 } else if ((cmd_buffer
->batch_bos
.next
== cmd_buffer
->batch_bos
.prev
) &&
873 (length
< ANV_CMD_BUFFER_BATCH_SIZE
/ 2)) {
874 /* If the secondary has exactly one batch buffer in its list *and*
875 * that batch buffer is less than half of the maximum size, we're
876 * probably better of simply copying it into our batch.
878 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_EMIT
;
879 } else if (!(cmd_buffer
->usage_flags
&
880 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT
)) {
881 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_CHAIN
;
883 /* In order to chain, we need this command buffer to contain an
884 * MI_BATCH_BUFFER_START which will jump back to the calling batch.
885 * It doesn't matter where it points now so long as has a valid
886 * relocation. We'll adjust it later as part of the chaining
889 emit_batch_buffer_start(cmd_buffer
, &batch_bo
->bo
, 0);
891 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
;
895 anv_batch_bo_finish(batch_bo
, &cmd_buffer
->batch
);
899 anv_cmd_buffer_add_seen_bbos(struct anv_cmd_buffer
*cmd_buffer
,
900 struct list_head
*list
)
902 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
903 struct anv_batch_bo
**bbo_ptr
= u_vector_add(&cmd_buffer
->seen_bbos
);
905 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
914 anv_cmd_buffer_add_secondary(struct anv_cmd_buffer
*primary
,
915 struct anv_cmd_buffer
*secondary
)
917 switch (secondary
->exec_mode
) {
918 case ANV_CMD_BUFFER_EXEC_MODE_EMIT
:
919 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
921 case ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
: {
922 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(primary
);
923 unsigned length
= secondary
->batch
.end
- secondary
->batch
.start
;
924 anv_batch_bo_grow(primary
, bbo
, &primary
->batch
, length
,
925 GEN8_MI_BATCH_BUFFER_START_length
* 4);
926 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
929 case ANV_CMD_BUFFER_EXEC_MODE_CHAIN
: {
930 struct anv_batch_bo
*first_bbo
=
931 list_first_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
932 struct anv_batch_bo
*last_bbo
=
933 list_last_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
935 emit_batch_buffer_start(primary
, &first_bbo
->bo
, 0);
937 struct anv_batch_bo
*this_bbo
= anv_cmd_buffer_current_batch_bo(primary
);
938 assert(primary
->batch
.start
== this_bbo
->bo
.map
);
939 uint32_t offset
= primary
->batch
.next
- primary
->batch
.start
;
941 /* Make the tail of the secondary point back to right after the
942 * MI_BATCH_BUFFER_START in the primary batch.
944 anv_batch_bo_link(primary
, last_bbo
, this_bbo
, offset
);
946 anv_cmd_buffer_add_seen_bbos(primary
, &secondary
->batch_bos
);
949 case ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
: {
950 struct list_head copy_list
;
951 VkResult result
= anv_batch_bo_list_clone(&secondary
->batch_bos
,
954 if (result
!= VK_SUCCESS
)
957 anv_cmd_buffer_add_seen_bbos(primary
, ©_list
);
959 struct anv_batch_bo
*first_bbo
=
960 list_first_entry(©_list
, struct anv_batch_bo
, link
);
961 struct anv_batch_bo
*last_bbo
=
962 list_last_entry(©_list
, struct anv_batch_bo
, link
);
964 cmd_buffer_chain_to_batch_bo(primary
, first_bbo
);
966 list_splicetail(©_list
, &primary
->batch_bos
);
968 anv_batch_bo_continue(last_bbo
, &primary
->batch
,
969 GEN8_MI_BATCH_BUFFER_START_length
* 4);
973 assert(!"Invalid execution mode");
976 anv_reloc_list_append(&primary
->surface_relocs
, &primary
->pool
->alloc
,
977 &secondary
->surface_relocs
, 0);
981 struct drm_i915_gem_execbuffer2 execbuf
;
983 struct drm_i915_gem_exec_object2
* objects
;
985 struct anv_bo
** bos
;
987 /* Allocated length of the 'objects' and 'bos' arrays */
988 uint32_t array_length
;
990 uint32_t fence_count
;
991 uint32_t fence_array_length
;
992 struct drm_i915_gem_exec_fence
* fences
;
993 struct anv_syncobj
** syncobjs
;
997 anv_execbuf_init(struct anv_execbuf
*exec
)
999 memset(exec
, 0, sizeof(*exec
));
1003 anv_execbuf_finish(struct anv_execbuf
*exec
,
1004 const VkAllocationCallbacks
*alloc
)
1006 vk_free(alloc
, exec
->objects
);
1007 vk_free(alloc
, exec
->bos
);
1008 vk_free(alloc
, exec
->fences
);
1009 vk_free(alloc
, exec
->syncobjs
);
1013 _compare_bo_handles(const void *_bo1
, const void *_bo2
)
1015 struct anv_bo
* const *bo1
= _bo1
;
1016 struct anv_bo
* const *bo2
= _bo2
;
1018 return (*bo1
)->gem_handle
- (*bo2
)->gem_handle
;
1022 anv_execbuf_add_bo(struct anv_execbuf
*exec
,
1024 struct anv_reloc_list
*relocs
,
1025 uint32_t extra_flags
,
1026 const VkAllocationCallbacks
*alloc
)
1028 struct drm_i915_gem_exec_object2
*obj
= NULL
;
1030 if (bo
->index
< exec
->bo_count
&& exec
->bos
[bo
->index
] == bo
)
1031 obj
= &exec
->objects
[bo
->index
];
1034 /* We've never seen this one before. Add it to the list and assign
1035 * an id that we can use later.
1037 if (exec
->bo_count
>= exec
->array_length
) {
1038 uint32_t new_len
= exec
->objects
? exec
->array_length
* 2 : 64;
1040 struct drm_i915_gem_exec_object2
*new_objects
=
1041 vk_alloc(alloc
, new_len
* sizeof(*new_objects
),
1042 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1043 if (new_objects
== NULL
)
1044 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1046 struct anv_bo
**new_bos
=
1047 vk_alloc(alloc
, new_len
* sizeof(*new_bos
),
1048 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1049 if (new_bos
== NULL
) {
1050 vk_free(alloc
, new_objects
);
1051 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1054 if (exec
->objects
) {
1055 memcpy(new_objects
, exec
->objects
,
1056 exec
->bo_count
* sizeof(*new_objects
));
1057 memcpy(new_bos
, exec
->bos
,
1058 exec
->bo_count
* sizeof(*new_bos
));
1061 vk_free(alloc
, exec
->objects
);
1062 vk_free(alloc
, exec
->bos
);
1064 exec
->objects
= new_objects
;
1065 exec
->bos
= new_bos
;
1066 exec
->array_length
= new_len
;
1069 assert(exec
->bo_count
< exec
->array_length
);
1071 bo
->index
= exec
->bo_count
++;
1072 obj
= &exec
->objects
[bo
->index
];
1073 exec
->bos
[bo
->index
] = bo
;
1075 obj
->handle
= bo
->gem_handle
;
1076 obj
->relocation_count
= 0;
1077 obj
->relocs_ptr
= 0;
1079 obj
->offset
= bo
->offset
;
1080 obj
->flags
= bo
->flags
| extra_flags
;
1085 if (relocs
!= NULL
&& obj
->relocation_count
== 0) {
1086 /* This is the first time we've ever seen a list of relocations for
1087 * this BO. Go ahead and set the relocations and then walk the list
1088 * of relocations and add them all.
1090 obj
->relocation_count
= relocs
->num_relocs
;
1091 obj
->relocs_ptr
= (uintptr_t) relocs
->relocs
;
1093 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1096 /* A quick sanity check on relocations */
1097 assert(relocs
->relocs
[i
].offset
< bo
->size
);
1098 result
= anv_execbuf_add_bo(exec
, relocs
->reloc_bos
[i
], NULL
,
1099 extra_flags
, alloc
);
1101 if (result
!= VK_SUCCESS
)
1105 const uint32_t entries
= relocs
->deps
->entries
;
1106 struct anv_bo
**bos
=
1107 vk_alloc(alloc
, entries
* sizeof(*bos
),
1108 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1110 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1112 struct set_entry
*entry
;
1113 struct anv_bo
**bo
= bos
;
1114 set_foreach(relocs
->deps
, entry
) {
1115 *bo
++ = (void *)entry
->key
;
1118 qsort(bos
, entries
, sizeof(struct anv_bo
*), _compare_bo_handles
);
1120 VkResult result
= VK_SUCCESS
;
1121 for (bo
= bos
; bo
< bos
+ entries
; bo
++) {
1122 result
= anv_execbuf_add_bo(exec
, *bo
, NULL
, extra_flags
, alloc
);
1123 if (result
!= VK_SUCCESS
)
1127 vk_free(alloc
, bos
);
1129 if (result
!= VK_SUCCESS
)
1137 anv_execbuf_add_syncobj(struct anv_execbuf
*exec
,
1138 uint32_t handle
, uint32_t flags
,
1139 const VkAllocationCallbacks
*alloc
)
1143 if (exec
->fence_count
>= exec
->fence_array_length
) {
1144 uint32_t new_len
= MAX2(exec
->fence_array_length
* 2, 64);
1146 exec
->fences
= vk_realloc(alloc
, exec
->fences
,
1147 new_len
* sizeof(*exec
->fences
),
1148 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1149 if (exec
->fences
== NULL
)
1150 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1152 exec
->fence_array_length
= new_len
;
1155 exec
->fences
[exec
->fence_count
] = (struct drm_i915_gem_exec_fence
) {
1160 exec
->fence_count
++;
1166 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
1167 struct anv_reloc_list
*list
)
1169 for (size_t i
= 0; i
< list
->num_relocs
; i
++)
1170 list
->relocs
[i
].target_handle
= list
->reloc_bos
[i
]->index
;
1174 adjust_relocations_from_state_pool(struct anv_state_pool
*pool
,
1175 struct anv_reloc_list
*relocs
,
1176 uint32_t last_pool_center_bo_offset
)
1178 assert(last_pool_center_bo_offset
<= pool
->block_pool
.center_bo_offset
);
1179 uint32_t delta
= pool
->block_pool
.center_bo_offset
- last_pool_center_bo_offset
;
1181 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1182 /* All of the relocations from this block pool to other BO's should
1183 * have been emitted relative to the surface block pool center. We
1184 * need to add the center offset to make them relative to the
1185 * beginning of the actual GEM bo.
1187 relocs
->relocs
[i
].offset
+= delta
;
1192 adjust_relocations_to_state_pool(struct anv_state_pool
*pool
,
1193 struct anv_bo
*from_bo
,
1194 struct anv_reloc_list
*relocs
,
1195 uint32_t last_pool_center_bo_offset
)
1197 assert(last_pool_center_bo_offset
<= pool
->block_pool
.center_bo_offset
);
1198 uint32_t delta
= pool
->block_pool
.center_bo_offset
- last_pool_center_bo_offset
;
1200 /* When we initially emit relocations into a block pool, we don't
1201 * actually know what the final center_bo_offset will be so we just emit
1202 * it as if center_bo_offset == 0. Now that we know what the center
1203 * offset is, we need to walk the list of relocations and adjust any
1204 * relocations that point to the pool bo with the correct offset.
1206 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1207 if (relocs
->reloc_bos
[i
] == &pool
->block_pool
.bo
) {
1208 /* Adjust the delta value in the relocation to correctly
1209 * correspond to the new delta. Initially, this value may have
1210 * been negative (if treated as unsigned), but we trust in
1211 * uint32_t roll-over to fix that for us at this point.
1213 relocs
->relocs
[i
].delta
+= delta
;
1215 /* Since the delta has changed, we need to update the actual
1216 * relocated value with the new presumed value. This function
1217 * should only be called on batch buffers, so we know it isn't in
1218 * use by the GPU at the moment.
1220 assert(relocs
->relocs
[i
].offset
< from_bo
->size
);
1221 write_reloc(pool
->block_pool
.device
,
1222 from_bo
->map
+ relocs
->relocs
[i
].offset
,
1223 relocs
->relocs
[i
].presumed_offset
+
1224 relocs
->relocs
[i
].delta
, false);
1230 anv_reloc_list_apply(struct anv_device
*device
,
1231 struct anv_reloc_list
*list
,
1233 bool always_relocate
)
1235 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
1236 struct anv_bo
*target_bo
= list
->reloc_bos
[i
];
1237 if (list
->relocs
[i
].presumed_offset
== target_bo
->offset
&&
1241 void *p
= bo
->map
+ list
->relocs
[i
].offset
;
1242 write_reloc(device
, p
, target_bo
->offset
+ list
->relocs
[i
].delta
, true);
1243 list
->relocs
[i
].presumed_offset
= target_bo
->offset
;
1248 * This function applies the relocation for a command buffer and writes the
1249 * actual addresses into the buffers as per what we were told by the kernel on
1250 * the previous execbuf2 call. This should be safe to do because, for each
1251 * relocated address, we have two cases:
1253 * 1) The target BO is inactive (as seen by the kernel). In this case, it is
1254 * not in use by the GPU so updating the address is 100% ok. It won't be
1255 * in-use by the GPU (from our context) again until the next execbuf2
1256 * happens. If the kernel decides to move it in the next execbuf2, it
1257 * will have to do the relocations itself, but that's ok because it should
1258 * have all of the information needed to do so.
1260 * 2) The target BO is active (as seen by the kernel). In this case, it
1261 * hasn't moved since the last execbuffer2 call because GTT shuffling
1262 * *only* happens when the BO is idle. (From our perspective, it only
1263 * happens inside the execbuffer2 ioctl, but the shuffling may be
1264 * triggered by another ioctl, with full-ppgtt this is limited to only
1265 * execbuffer2 ioctls on the same context, or memory pressure.) Since the
1266 * target BO hasn't moved, our anv_bo::offset exactly matches the BO's GTT
1267 * address and the relocated value we are writing into the BO will be the
1268 * same as the value that is already there.
1270 * There is also a possibility that the target BO is active but the exact
1271 * RENDER_SURFACE_STATE object we are writing the relocation into isn't in
1272 * use. In this case, the address currently in the RENDER_SURFACE_STATE
1273 * may be stale but it's still safe to write the relocation because that
1274 * particular RENDER_SURFACE_STATE object isn't in-use by the GPU and
1275 * won't be until the next execbuf2 call.
1277 * By doing relocations on the CPU, we can tell the kernel that it doesn't
1278 * need to bother. We want to do this because the surface state buffer is
1279 * used by every command buffer so, if the kernel does the relocations, it
1280 * will always be busy and the kernel will always stall. This is also
1281 * probably the fastest mechanism for doing relocations since the kernel would
1282 * have to make a full copy of all the relocations lists.
1285 relocate_cmd_buffer(struct anv_cmd_buffer
*cmd_buffer
,
1286 struct anv_execbuf
*exec
)
1288 static int userspace_relocs
= -1;
1289 if (userspace_relocs
< 0)
1290 userspace_relocs
= env_var_as_boolean("ANV_USERSPACE_RELOCS", true);
1291 if (!userspace_relocs
)
1294 /* First, we have to check to see whether or not we can even do the
1295 * relocation. New buffers which have never been submitted to the kernel
1296 * don't have a valid offset so we need to let the kernel do relocations so
1297 * that we can get offsets for them. On future execbuf2 calls, those
1298 * buffers will have offsets and we will be able to skip relocating.
1299 * Invalid offsets are indicated by anv_bo::offset == (uint64_t)-1.
1301 for (uint32_t i
= 0; i
< exec
->bo_count
; i
++) {
1302 if (exec
->bos
[i
]->offset
== (uint64_t)-1)
1306 /* Since surface states are shared between command buffers and we don't
1307 * know what order they will be submitted to the kernel, we don't know
1308 * what address is actually written in the surface state object at any
1309 * given time. The only option is to always relocate them.
1311 anv_reloc_list_apply(cmd_buffer
->device
, &cmd_buffer
->surface_relocs
,
1312 &cmd_buffer
->device
->surface_state_pool
.block_pool
.bo
,
1313 true /* always relocate surface states */);
1315 /* Since we own all of the batch buffers, we know what values are stored
1316 * in the relocated addresses and only have to update them if the offsets
1319 struct anv_batch_bo
**bbo
;
1320 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1321 anv_reloc_list_apply(cmd_buffer
->device
,
1322 &(*bbo
)->relocs
, &(*bbo
)->bo
, false);
1325 for (uint32_t i
= 0; i
< exec
->bo_count
; i
++)
1326 exec
->objects
[i
].offset
= exec
->bos
[i
]->offset
;
1332 setup_execbuf_for_cmd_buffer(struct anv_execbuf
*execbuf
,
1333 struct anv_cmd_buffer
*cmd_buffer
)
1335 struct anv_batch
*batch
= &cmd_buffer
->batch
;
1336 struct anv_state_pool
*ss_pool
=
1337 &cmd_buffer
->device
->surface_state_pool
;
1339 adjust_relocations_from_state_pool(ss_pool
, &cmd_buffer
->surface_relocs
,
1340 cmd_buffer
->last_ss_pool_center
);
1341 VkResult result
= anv_execbuf_add_bo(execbuf
, &ss_pool
->block_pool
.bo
,
1342 &cmd_buffer
->surface_relocs
, 0,
1343 &cmd_buffer
->device
->alloc
);
1344 if (result
!= VK_SUCCESS
)
1347 /* First, we walk over all of the bos we've seen and add them and their
1348 * relocations to the validate list.
1350 struct anv_batch_bo
**bbo
;
1351 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1352 adjust_relocations_to_state_pool(ss_pool
, &(*bbo
)->bo
, &(*bbo
)->relocs
,
1353 cmd_buffer
->last_ss_pool_center
);
1355 result
= anv_execbuf_add_bo(execbuf
, &(*bbo
)->bo
, &(*bbo
)->relocs
, 0,
1356 &cmd_buffer
->device
->alloc
);
1357 if (result
!= VK_SUCCESS
)
1361 /* Now that we've adjusted all of the surface state relocations, we need to
1362 * record the surface state pool center so future executions of the command
1363 * buffer can adjust correctly.
1365 cmd_buffer
->last_ss_pool_center
= ss_pool
->block_pool
.center_bo_offset
;
1367 struct anv_batch_bo
*first_batch_bo
=
1368 list_first_entry(&cmd_buffer
->batch_bos
, struct anv_batch_bo
, link
);
1370 /* The kernel requires that the last entry in the validation list be the
1371 * batch buffer to execute. We can simply swap the element
1372 * corresponding to the first batch_bo in the chain with the last
1373 * element in the list.
1375 if (first_batch_bo
->bo
.index
!= execbuf
->bo_count
- 1) {
1376 uint32_t idx
= first_batch_bo
->bo
.index
;
1377 uint32_t last_idx
= execbuf
->bo_count
- 1;
1379 struct drm_i915_gem_exec_object2 tmp_obj
= execbuf
->objects
[idx
];
1380 assert(execbuf
->bos
[idx
] == &first_batch_bo
->bo
);
1382 execbuf
->objects
[idx
] = execbuf
->objects
[last_idx
];
1383 execbuf
->bos
[idx
] = execbuf
->bos
[last_idx
];
1384 execbuf
->bos
[idx
]->index
= idx
;
1386 execbuf
->objects
[last_idx
] = tmp_obj
;
1387 execbuf
->bos
[last_idx
] = &first_batch_bo
->bo
;
1388 first_batch_bo
->bo
.index
= last_idx
;
1391 /* Now we go through and fixup all of the relocation lists to point to
1392 * the correct indices in the object array. We have to do this after we
1393 * reorder the list above as some of the indices may have changed.
1395 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
)
1396 anv_cmd_buffer_process_relocs(cmd_buffer
, &(*bbo
)->relocs
);
1398 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
1400 if (!cmd_buffer
->device
->info
.has_llc
) {
1401 __builtin_ia32_mfence();
1402 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1403 for (uint32_t i
= 0; i
< (*bbo
)->length
; i
+= CACHELINE_SIZE
)
1404 __builtin_ia32_clflush((*bbo
)->bo
.map
+ i
);
1408 execbuf
->execbuf
= (struct drm_i915_gem_execbuffer2
) {
1409 .buffers_ptr
= (uintptr_t) execbuf
->objects
,
1410 .buffer_count
= execbuf
->bo_count
,
1411 .batch_start_offset
= 0,
1412 .batch_len
= batch
->next
- batch
->start
,
1417 .flags
= I915_EXEC_HANDLE_LUT
| I915_EXEC_RENDER
,
1418 .rsvd1
= cmd_buffer
->device
->context_id
,
1422 if (relocate_cmd_buffer(cmd_buffer
, execbuf
)) {
1423 /* If we were able to successfully relocate everything, tell the kernel
1424 * that it can skip doing relocations. The requirement for using
1427 * 1) The addresses written in the objects must match the corresponding
1428 * reloc.presumed_offset which in turn must match the corresponding
1429 * execobject.offset.
1431 * 2) To avoid stalling, execobject.offset should match the current
1432 * address of that object within the active context.
1434 * In order to satisfy all of the invariants that make userspace
1435 * relocations to be safe (see relocate_cmd_buffer()), we need to
1436 * further ensure that the addresses we use match those used by the
1437 * kernel for the most recent execbuf2.
1439 * The kernel may still choose to do relocations anyway if something has
1440 * moved in the GTT. In this case, the relocation list still needs to be
1441 * valid. All relocations on the batch buffers are already valid and
1442 * kept up-to-date. For surface state relocations, by applying the
1443 * relocations in relocate_cmd_buffer, we ensured that the address in
1444 * the RENDER_SURFACE_STATE matches presumed_offset, so it should be
1445 * safe for the kernel to relocate them as needed.
1447 execbuf
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
1449 /* In the case where we fall back to doing kernel relocations, we need
1450 * to ensure that the relocation list is valid. All relocations on the
1451 * batch buffers are already valid and kept up-to-date. Since surface
1452 * states are shared between command buffers and we don't know what
1453 * order they will be submitted to the kernel, we don't know what
1454 * address is actually written in the surface state object at any given
1455 * time. The only option is to set a bogus presumed offset and let the
1456 * kernel relocate them.
1458 for (size_t i
= 0; i
< cmd_buffer
->surface_relocs
.num_relocs
; i
++)
1459 cmd_buffer
->surface_relocs
.relocs
[i
].presumed_offset
= -1;
1466 setup_empty_execbuf(struct anv_execbuf
*execbuf
, struct anv_device
*device
)
1468 VkResult result
= anv_execbuf_add_bo(execbuf
, &device
->trivial_batch_bo
,
1469 NULL
, 0, &device
->alloc
);
1470 if (result
!= VK_SUCCESS
)
1473 execbuf
->execbuf
= (struct drm_i915_gem_execbuffer2
) {
1474 .buffers_ptr
= (uintptr_t) execbuf
->objects
,
1475 .buffer_count
= execbuf
->bo_count
,
1476 .batch_start_offset
= 0,
1477 .batch_len
= 8, /* GEN7_MI_BATCH_BUFFER_END and NOOP */
1478 .flags
= I915_EXEC_HANDLE_LUT
| I915_EXEC_RENDER
,
1479 .rsvd1
= device
->context_id
,
1487 anv_cmd_buffer_execbuf(struct anv_device
*device
,
1488 struct anv_cmd_buffer
*cmd_buffer
,
1489 const VkSemaphore
*in_semaphores
,
1490 uint32_t num_in_semaphores
,
1491 const VkSemaphore
*out_semaphores
,
1492 uint32_t num_out_semaphores
,
1495 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1497 struct anv_execbuf execbuf
;
1498 anv_execbuf_init(&execbuf
);
1501 VkResult result
= VK_SUCCESS
;
1502 for (uint32_t i
= 0; i
< num_in_semaphores
; i
++) {
1503 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, in_semaphores
[i
]);
1504 struct anv_semaphore_impl
*impl
=
1505 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
1506 &semaphore
->temporary
: &semaphore
->permanent
;
1508 switch (impl
->type
) {
1509 case ANV_SEMAPHORE_TYPE_BO
:
1510 result
= anv_execbuf_add_bo(&execbuf
, impl
->bo
, NULL
,
1512 if (result
!= VK_SUCCESS
)
1516 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
1517 if (in_fence
== -1) {
1518 in_fence
= impl
->fd
;
1520 int merge
= anv_gem_sync_file_merge(device
, in_fence
, impl
->fd
);
1522 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1532 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
:
1533 result
= anv_execbuf_add_syncobj(&execbuf
, impl
->syncobj
,
1534 I915_EXEC_FENCE_WAIT
,
1536 if (result
!= VK_SUCCESS
)
1545 bool need_out_fence
= false;
1546 for (uint32_t i
= 0; i
< num_out_semaphores
; i
++) {
1547 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, out_semaphores
[i
]);
1549 /* Under most circumstances, out fences won't be temporary. However,
1550 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
1552 * "If the import is temporary, the implementation must restore the
1553 * semaphore to its prior permanent state after submitting the next
1554 * semaphore wait operation."
1556 * The spec says nothing whatsoever about signal operations on
1557 * temporarily imported semaphores so it appears they are allowed.
1558 * There are also CTS tests that require this to work.
1560 struct anv_semaphore_impl
*impl
=
1561 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
1562 &semaphore
->temporary
: &semaphore
->permanent
;
1564 switch (impl
->type
) {
1565 case ANV_SEMAPHORE_TYPE_BO
:
1566 result
= anv_execbuf_add_bo(&execbuf
, impl
->bo
, NULL
,
1567 EXEC_OBJECT_WRITE
, &device
->alloc
);
1568 if (result
!= VK_SUCCESS
)
1572 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
1573 need_out_fence
= true;
1576 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
:
1577 result
= anv_execbuf_add_syncobj(&execbuf
, impl
->syncobj
,
1578 I915_EXEC_FENCE_SIGNAL
,
1580 if (result
!= VK_SUCCESS
)
1590 /* Under most circumstances, out fences won't be temporary. However,
1591 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
1593 * "If the import is temporary, the implementation must restore the
1594 * semaphore to its prior permanent state after submitting the next
1595 * semaphore wait operation."
1597 * The spec says nothing whatsoever about signal operations on
1598 * temporarily imported semaphores so it appears they are allowed.
1599 * There are also CTS tests that require this to work.
1601 struct anv_fence_impl
*impl
=
1602 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1603 &fence
->temporary
: &fence
->permanent
;
1605 switch (impl
->type
) {
1606 case ANV_FENCE_TYPE_BO
:
1607 result
= anv_execbuf_add_bo(&execbuf
, &impl
->bo
.bo
, NULL
,
1608 EXEC_OBJECT_WRITE
, &device
->alloc
);
1609 if (result
!= VK_SUCCESS
)
1613 case ANV_FENCE_TYPE_SYNCOBJ
:
1614 result
= anv_execbuf_add_syncobj(&execbuf
, impl
->syncobj
,
1615 I915_EXEC_FENCE_SIGNAL
,
1617 if (result
!= VK_SUCCESS
)
1622 unreachable("Invalid fence type");
1627 result
= setup_execbuf_for_cmd_buffer(&execbuf
, cmd_buffer
);
1629 result
= setup_empty_execbuf(&execbuf
, device
);
1631 if (result
!= VK_SUCCESS
)
1634 if (execbuf
.fence_count
> 0) {
1635 assert(device
->instance
->physicalDevice
.has_syncobj
);
1636 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_ARRAY
;
1637 execbuf
.execbuf
.num_cliprects
= execbuf
.fence_count
;
1638 execbuf
.execbuf
.cliprects_ptr
= (uintptr_t) execbuf
.fences
;
1641 if (in_fence
!= -1) {
1642 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_IN
;
1643 execbuf
.execbuf
.rsvd2
|= (uint32_t)in_fence
;
1647 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_OUT
;
1649 result
= anv_device_execbuf(device
, &execbuf
.execbuf
, execbuf
.bos
);
1651 /* Execbuf does not consume the in_fence. It's our job to close it. */
1655 for (uint32_t i
= 0; i
< num_in_semaphores
; i
++) {
1656 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, in_semaphores
[i
]);
1657 /* From the Vulkan 1.0.53 spec:
1659 * "If the import is temporary, the implementation must restore the
1660 * semaphore to its prior permanent state after submitting the next
1661 * semaphore wait operation."
1663 * This has to happen after the execbuf in case we close any syncobjs in
1666 anv_semaphore_reset_temporary(device
, semaphore
);
1669 if (fence
&& fence
->permanent
.type
== ANV_FENCE_TYPE_BO
) {
1670 /* BO fences can't be shared, so they can't be temporary. */
1671 assert(fence
->temporary
.type
== ANV_FENCE_TYPE_NONE
);
1673 /* Once the execbuf has returned, we need to set the fence state to
1674 * SUBMITTED. We can't do this before calling execbuf because
1675 * anv_GetFenceStatus does take the global device lock before checking
1678 * We set the fence state to SUBMITTED regardless of whether or not the
1679 * execbuf succeeds because we need to ensure that vkWaitForFences() and
1680 * vkGetFenceStatus() return a valid result (VK_ERROR_DEVICE_LOST or
1681 * VK_SUCCESS) in a finite amount of time even if execbuf fails.
1683 fence
->permanent
.bo
.state
= ANV_BO_FENCE_STATE_SUBMITTED
;
1686 if (result
== VK_SUCCESS
&& need_out_fence
) {
1687 int out_fence
= execbuf
.execbuf
.rsvd2
>> 32;
1688 for (uint32_t i
= 0; i
< num_out_semaphores
; i
++) {
1689 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, out_semaphores
[i
]);
1690 /* Out fences can't have temporary state because that would imply
1691 * that we imported a sync file and are trying to signal it.
1693 assert(semaphore
->temporary
.type
== ANV_SEMAPHORE_TYPE_NONE
);
1694 struct anv_semaphore_impl
*impl
= &semaphore
->permanent
;
1696 if (impl
->type
== ANV_SEMAPHORE_TYPE_SYNC_FILE
) {
1697 assert(impl
->fd
== -1);
1698 impl
->fd
= dup(out_fence
);
1704 anv_execbuf_finish(&execbuf
, &device
->alloc
);