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(struct anv_reloc_list
*list
,
51 const VkAllocationCallbacks
*alloc
)
53 memset(list
, 0, sizeof(*list
));
58 anv_reloc_list_init_clone(struct anv_reloc_list
*list
,
59 const VkAllocationCallbacks
*alloc
,
60 const struct anv_reloc_list
*other_list
)
62 list
->num_relocs
= other_list
->num_relocs
;
63 list
->array_length
= other_list
->array_length
;
65 if (list
->num_relocs
> 0) {
67 vk_alloc(alloc
, list
->array_length
* sizeof(*list
->relocs
), 8,
68 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
69 if (list
->relocs
== NULL
)
70 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
73 vk_alloc(alloc
, list
->array_length
* sizeof(*list
->reloc_bos
), 8,
74 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
75 if (list
->reloc_bos
== NULL
) {
76 vk_free(alloc
, list
->relocs
);
77 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
80 memcpy(list
->relocs
, other_list
->relocs
,
81 list
->array_length
* sizeof(*list
->relocs
));
82 memcpy(list
->reloc_bos
, other_list
->reloc_bos
,
83 list
->array_length
* sizeof(*list
->reloc_bos
));
86 list
->reloc_bos
= NULL
;
89 list
->dep_words
= other_list
->dep_words
;
91 if (list
->dep_words
> 0) {
93 vk_alloc(alloc
, list
->dep_words
* sizeof(BITSET_WORD
), 8,
94 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
95 memcpy(list
->deps
, other_list
->deps
,
96 list
->dep_words
* sizeof(BITSET_WORD
));
105 anv_reloc_list_finish(struct anv_reloc_list
*list
,
106 const VkAllocationCallbacks
*alloc
)
108 vk_free(alloc
, list
->relocs
);
109 vk_free(alloc
, list
->reloc_bos
);
110 vk_free(alloc
, list
->deps
);
114 anv_reloc_list_grow(struct anv_reloc_list
*list
,
115 const VkAllocationCallbacks
*alloc
,
116 size_t num_additional_relocs
)
118 if (list
->num_relocs
+ num_additional_relocs
<= list
->array_length
)
121 size_t new_length
= MAX2(16, list
->array_length
* 2);
122 while (new_length
< list
->num_relocs
+ num_additional_relocs
)
125 struct drm_i915_gem_relocation_entry
*new_relocs
=
126 vk_realloc(alloc
, list
->relocs
,
127 new_length
* sizeof(*list
->relocs
), 8,
128 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
129 if (new_relocs
== NULL
)
130 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
131 list
->relocs
= new_relocs
;
133 struct anv_bo
**new_reloc_bos
=
134 vk_realloc(alloc
, list
->reloc_bos
,
135 new_length
* sizeof(*list
->reloc_bos
), 8,
136 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
137 if (new_reloc_bos
== NULL
)
138 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
139 list
->reloc_bos
= new_reloc_bos
;
141 list
->array_length
= new_length
;
147 anv_reloc_list_grow_deps(struct anv_reloc_list
*list
,
148 const VkAllocationCallbacks
*alloc
,
149 uint32_t min_num_words
)
151 if (min_num_words
<= list
->dep_words
)
154 uint32_t new_length
= MAX2(32, list
->dep_words
* 2);
155 while (new_length
< min_num_words
)
158 BITSET_WORD
*new_deps
=
159 vk_realloc(alloc
, list
->deps
, new_length
* sizeof(BITSET_WORD
), 8,
160 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
161 if (new_deps
== NULL
)
162 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
163 list
->deps
= new_deps
;
165 /* Zero out the new data */
166 memset(list
->deps
+ list
->dep_words
, 0,
167 (new_length
- list
->dep_words
) * sizeof(BITSET_WORD
));
168 list
->dep_words
= new_length
;
173 #define READ_ONCE(x) (*(volatile __typeof__(x) *)&(x))
176 anv_reloc_list_add(struct anv_reloc_list
*list
,
177 const VkAllocationCallbacks
*alloc
,
178 uint32_t offset
, struct anv_bo
*target_bo
, uint32_t delta
,
179 uint64_t *address_u64_out
)
181 struct drm_i915_gem_relocation_entry
*entry
;
184 struct anv_bo
*unwrapped_target_bo
= anv_bo_unwrap(target_bo
);
185 uint64_t target_bo_offset
= READ_ONCE(unwrapped_target_bo
->offset
);
187 *address_u64_out
= target_bo_offset
+ delta
;
189 if (unwrapped_target_bo
->flags
& EXEC_OBJECT_PINNED
) {
190 assert(!target_bo
->is_wrapper
);
191 uint32_t idx
= unwrapped_target_bo
->gem_handle
;
192 anv_reloc_list_grow_deps(list
, alloc
, (idx
/ BITSET_WORDBITS
) + 1);
193 BITSET_SET(list
->deps
, unwrapped_target_bo
->gem_handle
);
197 VkResult result
= anv_reloc_list_grow(list
, alloc
, 1);
198 if (result
!= VK_SUCCESS
)
201 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
202 index
= list
->num_relocs
++;
203 list
->reloc_bos
[index
] = target_bo
;
204 entry
= &list
->relocs
[index
];
205 entry
->target_handle
= -1; /* See also anv_cmd_buffer_process_relocs() */
206 entry
->delta
= delta
;
207 entry
->offset
= offset
;
208 entry
->presumed_offset
= target_bo_offset
;
209 entry
->read_domains
= 0;
210 entry
->write_domain
= 0;
211 VG(VALGRIND_CHECK_MEM_IS_DEFINED(entry
, sizeof(*entry
)));
217 anv_reloc_list_clear(struct anv_reloc_list
*list
)
219 list
->num_relocs
= 0;
220 if (list
->dep_words
> 0)
221 memset(list
->deps
, 0, list
->dep_words
* sizeof(BITSET_WORD
));
225 anv_reloc_list_append(struct anv_reloc_list
*list
,
226 const VkAllocationCallbacks
*alloc
,
227 struct anv_reloc_list
*other
, uint32_t offset
)
229 VkResult result
= anv_reloc_list_grow(list
, alloc
, other
->num_relocs
);
230 if (result
!= VK_SUCCESS
)
233 if (other
->num_relocs
> 0) {
234 memcpy(&list
->relocs
[list
->num_relocs
], &other
->relocs
[0],
235 other
->num_relocs
* sizeof(other
->relocs
[0]));
236 memcpy(&list
->reloc_bos
[list
->num_relocs
], &other
->reloc_bos
[0],
237 other
->num_relocs
* sizeof(other
->reloc_bos
[0]));
239 for (uint32_t i
= 0; i
< other
->num_relocs
; i
++)
240 list
->relocs
[i
+ list
->num_relocs
].offset
+= offset
;
242 list
->num_relocs
+= other
->num_relocs
;
245 anv_reloc_list_grow_deps(list
, alloc
, other
->dep_words
);
246 for (uint32_t w
= 0; w
< other
->dep_words
; w
++)
247 list
->deps
[w
] |= other
->deps
[w
];
252 /*-----------------------------------------------------------------------*
253 * Functions related to anv_batch
254 *-----------------------------------------------------------------------*/
257 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
259 if (batch
->next
+ num_dwords
* 4 > batch
->end
) {
260 VkResult result
= batch
->extend_cb(batch
, batch
->user_data
);
261 if (result
!= VK_SUCCESS
) {
262 anv_batch_set_error(batch
, result
);
267 void *p
= batch
->next
;
269 batch
->next
+= num_dwords
* 4;
270 assert(batch
->next
<= batch
->end
);
276 anv_batch_emit_reloc(struct anv_batch
*batch
,
277 void *location
, struct anv_bo
*bo
, uint32_t delta
)
279 uint64_t address_u64
= 0;
280 VkResult result
= anv_reloc_list_add(batch
->relocs
, batch
->alloc
,
281 location
- batch
->start
, bo
, delta
,
283 if (result
!= VK_SUCCESS
) {
284 anv_batch_set_error(batch
, result
);
292 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
294 uint32_t size
, offset
;
296 size
= other
->next
- other
->start
;
297 assert(size
% 4 == 0);
299 if (batch
->next
+ size
> batch
->end
) {
300 VkResult result
= batch
->extend_cb(batch
, batch
->user_data
);
301 if (result
!= VK_SUCCESS
) {
302 anv_batch_set_error(batch
, result
);
307 assert(batch
->next
+ size
<= batch
->end
);
309 VG(VALGRIND_CHECK_MEM_IS_DEFINED(other
->start
, size
));
310 memcpy(batch
->next
, other
->start
, size
);
312 offset
= batch
->next
- batch
->start
;
313 VkResult result
= anv_reloc_list_append(batch
->relocs
, batch
->alloc
,
314 other
->relocs
, offset
);
315 if (result
!= VK_SUCCESS
) {
316 anv_batch_set_error(batch
, result
);
323 /*-----------------------------------------------------------------------*
324 * Functions related to anv_batch_bo
325 *-----------------------------------------------------------------------*/
328 anv_batch_bo_create(struct anv_cmd_buffer
*cmd_buffer
,
329 struct anv_batch_bo
**bbo_out
)
333 struct anv_batch_bo
*bbo
= vk_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
334 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
336 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
338 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
,
339 ANV_CMD_BUFFER_BATCH_SIZE
, &bbo
->bo
);
340 if (result
!= VK_SUCCESS
)
343 result
= anv_reloc_list_init(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
344 if (result
!= VK_SUCCESS
)
352 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, bbo
->bo
);
354 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
360 anv_batch_bo_clone(struct anv_cmd_buffer
*cmd_buffer
,
361 const struct anv_batch_bo
*other_bbo
,
362 struct anv_batch_bo
**bbo_out
)
366 struct anv_batch_bo
*bbo
= vk_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
367 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
369 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
371 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
,
372 other_bbo
->bo
->size
, &bbo
->bo
);
373 if (result
!= VK_SUCCESS
)
376 result
= anv_reloc_list_init_clone(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
,
378 if (result
!= VK_SUCCESS
)
381 bbo
->length
= other_bbo
->length
;
382 memcpy(bbo
->bo
->map
, other_bbo
->bo
->map
, other_bbo
->length
);
388 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, bbo
->bo
);
390 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
396 anv_batch_bo_start(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
397 size_t batch_padding
)
399 batch
->next
= batch
->start
= bbo
->bo
->map
;
400 batch
->end
= bbo
->bo
->map
+ bbo
->bo
->size
- batch_padding
;
401 batch
->relocs
= &bbo
->relocs
;
402 anv_reloc_list_clear(&bbo
->relocs
);
406 anv_batch_bo_continue(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
407 size_t batch_padding
)
409 batch
->start
= bbo
->bo
->map
;
410 batch
->next
= bbo
->bo
->map
+ bbo
->length
;
411 batch
->end
= bbo
->bo
->map
+ bbo
->bo
->size
- batch_padding
;
412 batch
->relocs
= &bbo
->relocs
;
416 anv_batch_bo_finish(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
)
418 assert(batch
->start
== bbo
->bo
->map
);
419 bbo
->length
= batch
->next
- batch
->start
;
420 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch
->start
, bbo
->length
));
424 anv_batch_bo_grow(struct anv_cmd_buffer
*cmd_buffer
, struct anv_batch_bo
*bbo
,
425 struct anv_batch
*batch
, size_t aditional
,
426 size_t batch_padding
)
428 assert(batch
->start
== bbo
->bo
->map
);
429 bbo
->length
= batch
->next
- batch
->start
;
431 size_t new_size
= bbo
->bo
->size
;
432 while (new_size
<= bbo
->length
+ aditional
+ batch_padding
)
435 if (new_size
== bbo
->bo
->size
)
438 struct anv_bo
*new_bo
;
439 VkResult result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
,
441 if (result
!= VK_SUCCESS
)
444 memcpy(new_bo
->map
, bbo
->bo
->map
, bbo
->length
);
446 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, bbo
->bo
);
449 anv_batch_bo_continue(bbo
, batch
, batch_padding
);
455 anv_batch_bo_link(struct anv_cmd_buffer
*cmd_buffer
,
456 struct anv_batch_bo
*prev_bbo
,
457 struct anv_batch_bo
*next_bbo
,
458 uint32_t next_bbo_offset
)
460 const uint32_t bb_start_offset
=
461 prev_bbo
->length
- GEN8_MI_BATCH_BUFFER_START_length
* 4;
462 ASSERTED
const uint32_t *bb_start
= prev_bbo
->bo
->map
+ bb_start_offset
;
464 /* Make sure we're looking at a MI_BATCH_BUFFER_START */
465 assert(((*bb_start
>> 29) & 0x07) == 0);
466 assert(((*bb_start
>> 23) & 0x3f) == 49);
468 if (cmd_buffer
->device
->physical
->use_softpin
) {
469 assert(prev_bbo
->bo
->flags
& EXEC_OBJECT_PINNED
);
470 assert(next_bbo
->bo
->flags
& EXEC_OBJECT_PINNED
);
472 write_reloc(cmd_buffer
->device
,
473 prev_bbo
->bo
->map
+ bb_start_offset
+ 4,
474 next_bbo
->bo
->offset
+ next_bbo_offset
, true);
476 uint32_t reloc_idx
= prev_bbo
->relocs
.num_relocs
- 1;
477 assert(prev_bbo
->relocs
.relocs
[reloc_idx
].offset
== bb_start_offset
+ 4);
479 prev_bbo
->relocs
.reloc_bos
[reloc_idx
] = next_bbo
->bo
;
480 prev_bbo
->relocs
.relocs
[reloc_idx
].delta
= next_bbo_offset
;
482 /* Use a bogus presumed offset to force a relocation */
483 prev_bbo
->relocs
.relocs
[reloc_idx
].presumed_offset
= -1;
488 anv_batch_bo_destroy(struct anv_batch_bo
*bbo
,
489 struct anv_cmd_buffer
*cmd_buffer
)
491 anv_reloc_list_finish(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
492 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, bbo
->bo
);
493 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
497 anv_batch_bo_list_clone(const struct list_head
*list
,
498 struct anv_cmd_buffer
*cmd_buffer
,
499 struct list_head
*new_list
)
501 VkResult result
= VK_SUCCESS
;
503 list_inithead(new_list
);
505 struct anv_batch_bo
*prev_bbo
= NULL
;
506 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
507 struct anv_batch_bo
*new_bbo
= NULL
;
508 result
= anv_batch_bo_clone(cmd_buffer
, bbo
, &new_bbo
);
509 if (result
!= VK_SUCCESS
)
511 list_addtail(&new_bbo
->link
, new_list
);
514 anv_batch_bo_link(cmd_buffer
, prev_bbo
, new_bbo
, 0);
519 if (result
!= VK_SUCCESS
) {
520 list_for_each_entry_safe(struct anv_batch_bo
, bbo
, new_list
, link
) {
521 list_del(&bbo
->link
);
522 anv_batch_bo_destroy(bbo
, cmd_buffer
);
529 /*-----------------------------------------------------------------------*
530 * Functions related to anv_batch_bo
531 *-----------------------------------------------------------------------*/
533 static struct anv_batch_bo
*
534 anv_cmd_buffer_current_batch_bo(struct anv_cmd_buffer
*cmd_buffer
)
536 return LIST_ENTRY(struct anv_batch_bo
, cmd_buffer
->batch_bos
.prev
, link
);
540 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer
*cmd_buffer
)
542 struct anv_state
*bt_block
= u_vector_head(&cmd_buffer
->bt_block_states
);
543 return (struct anv_address
) {
544 .bo
= anv_binding_table_pool(cmd_buffer
->device
)->block_pool
.bo
,
545 .offset
= bt_block
->offset
,
550 emit_batch_buffer_start(struct anv_cmd_buffer
*cmd_buffer
,
551 struct anv_bo
*bo
, uint32_t offset
)
553 /* In gen8+ the address field grew to two dwords to accomodate 48 bit
554 * offsets. The high 16 bits are in the last dword, so we can use the gen8
555 * version in either case, as long as we set the instruction length in the
556 * header accordingly. This means that we always emit three dwords here
557 * and all the padding and adjustment we do in this file works for all
561 #define GEN7_MI_BATCH_BUFFER_START_length 2
562 #define GEN7_MI_BATCH_BUFFER_START_length_bias 2
564 const uint32_t gen7_length
=
565 GEN7_MI_BATCH_BUFFER_START_length
- GEN7_MI_BATCH_BUFFER_START_length_bias
;
566 const uint32_t gen8_length
=
567 GEN8_MI_BATCH_BUFFER_START_length
- GEN8_MI_BATCH_BUFFER_START_length_bias
;
569 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_BATCH_BUFFER_START
, bbs
) {
570 bbs
.DWordLength
= cmd_buffer
->device
->info
.gen
< 8 ?
571 gen7_length
: gen8_length
;
572 bbs
.SecondLevelBatchBuffer
= Firstlevelbatch
;
573 bbs
.AddressSpaceIndicator
= ASI_PPGTT
;
574 bbs
.BatchBufferStartAddress
= (struct anv_address
) { bo
, offset
};
579 cmd_buffer_chain_to_batch_bo(struct anv_cmd_buffer
*cmd_buffer
,
580 struct anv_batch_bo
*bbo
)
582 struct anv_batch
*batch
= &cmd_buffer
->batch
;
583 struct anv_batch_bo
*current_bbo
=
584 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
586 /* We set the end of the batch a little short so we would be sure we
587 * have room for the chaining command. Since we're about to emit the
588 * chaining command, let's set it back where it should go.
590 batch
->end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
591 assert(batch
->end
== current_bbo
->bo
->map
+ current_bbo
->bo
->size
);
593 emit_batch_buffer_start(cmd_buffer
, bbo
->bo
, 0);
595 anv_batch_bo_finish(current_bbo
, batch
);
599 anv_cmd_buffer_chain_batch(struct anv_batch
*batch
, void *_data
)
601 struct anv_cmd_buffer
*cmd_buffer
= _data
;
602 struct anv_batch_bo
*new_bbo
;
604 VkResult result
= anv_batch_bo_create(cmd_buffer
, &new_bbo
);
605 if (result
!= VK_SUCCESS
)
608 struct anv_batch_bo
**seen_bbo
= u_vector_add(&cmd_buffer
->seen_bbos
);
609 if (seen_bbo
== NULL
) {
610 anv_batch_bo_destroy(new_bbo
, cmd_buffer
);
611 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
615 cmd_buffer_chain_to_batch_bo(cmd_buffer
, new_bbo
);
617 list_addtail(&new_bbo
->link
, &cmd_buffer
->batch_bos
);
619 anv_batch_bo_start(new_bbo
, batch
, GEN8_MI_BATCH_BUFFER_START_length
* 4);
625 anv_cmd_buffer_grow_batch(struct anv_batch
*batch
, void *_data
)
627 struct anv_cmd_buffer
*cmd_buffer
= _data
;
628 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
630 anv_batch_bo_grow(cmd_buffer
, bbo
, &cmd_buffer
->batch
, 4096,
631 GEN8_MI_BATCH_BUFFER_START_length
* 4);
636 /** Allocate a binding table
638 * This function allocates a binding table. This is a bit more complicated
639 * than one would think due to a combination of Vulkan driver design and some
640 * unfortunate hardware restrictions.
642 * The 3DSTATE_BINDING_TABLE_POINTERS_* packets only have a 16-bit field for
643 * the binding table pointer which means that all binding tables need to live
644 * in the bottom 64k of surface state base address. The way the GL driver has
645 * classically dealt with this restriction is to emit all surface states
646 * on-the-fly into the batch and have a batch buffer smaller than 64k. This
647 * isn't really an option in Vulkan for a couple of reasons:
649 * 1) In Vulkan, we have growing (or chaining) batches so surface states have
650 * to live in their own buffer and we have to be able to re-emit
651 * STATE_BASE_ADDRESS as needed which requires a full pipeline stall. In
652 * order to avoid emitting STATE_BASE_ADDRESS any more often than needed
653 * (it's not that hard to hit 64k of just binding tables), we allocate
654 * surface state objects up-front when VkImageView is created. In order
655 * for this to work, surface state objects need to be allocated from a
658 * 2) We tried to design the surface state system in such a way that it's
659 * already ready for bindless texturing. The way bindless texturing works
660 * on our hardware is that you have a big pool of surface state objects
661 * (with its own state base address) and the bindless handles are simply
662 * offsets into that pool. With the architecture we chose, we already
663 * have that pool and it's exactly the same pool that we use for regular
664 * surface states so we should already be ready for bindless.
666 * 3) For render targets, we need to be able to fill out the surface states
667 * later in vkBeginRenderPass so that we can assign clear colors
668 * correctly. One way to do this would be to just create the surface
669 * state data and then repeatedly copy it into the surface state BO every
670 * time we have to re-emit STATE_BASE_ADDRESS. While this works, it's
671 * rather annoying and just being able to allocate them up-front and
672 * re-use them for the entire render pass.
674 * While none of these are technically blockers for emitting state on the fly
675 * like we do in GL, the ability to have a single surface state pool is
676 * simplifies things greatly. Unfortunately, it comes at a cost...
678 * Because of the 64k limitation of 3DSTATE_BINDING_TABLE_POINTERS_*, we can't
679 * place the binding tables just anywhere in surface state base address.
680 * Because 64k isn't a whole lot of space, we can't simply restrict the
681 * surface state buffer to 64k, we have to be more clever. The solution we've
682 * chosen is to have a block pool with a maximum size of 2G that starts at
683 * zero and grows in both directions. All surface states are allocated from
684 * the top of the pool (positive offsets) and we allocate blocks (< 64k) of
685 * binding tables from the bottom of the pool (negative offsets). Every time
686 * we allocate a new binding table block, we set surface state base address to
687 * point to the bottom of the binding table block. This way all of the
688 * binding tables in the block are in the bottom 64k of surface state base
689 * address. When we fill out the binding table, we add the distance between
690 * the bottom of our binding table block and zero of the block pool to the
691 * surface state offsets so that they are correct relative to out new surface
692 * state base address at the bottom of the binding table block.
694 * \see adjust_relocations_from_block_pool()
695 * \see adjust_relocations_too_block_pool()
697 * \param[in] entries The number of surface state entries the binding
698 * table should be able to hold.
700 * \param[out] state_offset The offset surface surface state base address
701 * where the surface states live. This must be
702 * added to the surface state offset when it is
703 * written into the binding table entry.
705 * \return An anv_state representing the binding table
708 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
709 uint32_t entries
, uint32_t *state_offset
)
711 struct anv_device
*device
= cmd_buffer
->device
;
712 struct anv_state
*bt_block
= u_vector_head(&cmd_buffer
->bt_block_states
);
714 uint32_t bt_size
= align_u32(entries
* 4, 32);
716 struct anv_state state
= cmd_buffer
->bt_next
;
717 if (bt_size
> state
.alloc_size
)
718 return (struct anv_state
) { 0 };
720 state
.alloc_size
= bt_size
;
721 cmd_buffer
->bt_next
.offset
+= bt_size
;
722 cmd_buffer
->bt_next
.map
+= bt_size
;
723 cmd_buffer
->bt_next
.alloc_size
-= bt_size
;
725 if (device
->physical
->use_softpin
) {
726 assert(bt_block
->offset
>= 0);
727 *state_offset
= device
->surface_state_pool
.block_pool
.start_address
-
728 device
->binding_table_pool
.block_pool
.start_address
- bt_block
->offset
;
730 assert(bt_block
->offset
< 0);
731 *state_offset
= -bt_block
->offset
;
738 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
)
740 struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
741 return anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
742 isl_dev
->ss
.size
, isl_dev
->ss
.align
);
746 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer
*cmd_buffer
,
747 uint32_t size
, uint32_t alignment
)
749 return anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
754 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer
*cmd_buffer
)
756 struct anv_state
*bt_block
= u_vector_add(&cmd_buffer
->bt_block_states
);
757 if (bt_block
== NULL
) {
758 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
759 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
762 *bt_block
= anv_binding_table_pool_alloc(cmd_buffer
->device
);
764 /* The bt_next state is a rolling state (we update it as we suballocate
765 * from it) which is relative to the start of the binding table block.
767 cmd_buffer
->bt_next
= *bt_block
;
768 cmd_buffer
->bt_next
.offset
= 0;
774 anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
776 struct anv_batch_bo
*batch_bo
;
779 list_inithead(&cmd_buffer
->batch_bos
);
781 result
= anv_batch_bo_create(cmd_buffer
, &batch_bo
);
782 if (result
!= VK_SUCCESS
)
785 list_addtail(&batch_bo
->link
, &cmd_buffer
->batch_bos
);
787 cmd_buffer
->batch
.alloc
= &cmd_buffer
->pool
->alloc
;
788 cmd_buffer
->batch
.user_data
= cmd_buffer
;
790 if (cmd_buffer
->device
->can_chain_batches
) {
791 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_chain_batch
;
793 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_grow_batch
;
796 anv_batch_bo_start(batch_bo
, &cmd_buffer
->batch
,
797 GEN8_MI_BATCH_BUFFER_START_length
* 4);
799 int success
= u_vector_init(&cmd_buffer
->seen_bbos
,
800 sizeof(struct anv_bo
*),
801 8 * sizeof(struct anv_bo
*));
805 *(struct anv_batch_bo
**)u_vector_add(&cmd_buffer
->seen_bbos
) = batch_bo
;
807 /* u_vector requires power-of-two size elements */
808 unsigned pow2_state_size
= util_next_power_of_two(sizeof(struct anv_state
));
809 success
= u_vector_init(&cmd_buffer
->bt_block_states
,
810 pow2_state_size
, 8 * pow2_state_size
);
814 result
= anv_reloc_list_init(&cmd_buffer
->surface_relocs
,
815 &cmd_buffer
->pool
->alloc
);
816 if (result
!= VK_SUCCESS
)
818 cmd_buffer
->last_ss_pool_center
= 0;
820 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
821 if (result
!= VK_SUCCESS
)
827 u_vector_finish(&cmd_buffer
->bt_block_states
);
829 u_vector_finish(&cmd_buffer
->seen_bbos
);
831 anv_batch_bo_destroy(batch_bo
, cmd_buffer
);
837 anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
839 struct anv_state
*bt_block
;
840 u_vector_foreach(bt_block
, &cmd_buffer
->bt_block_states
)
841 anv_binding_table_pool_free(cmd_buffer
->device
, *bt_block
);
842 u_vector_finish(&cmd_buffer
->bt_block_states
);
844 anv_reloc_list_finish(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
);
846 u_vector_finish(&cmd_buffer
->seen_bbos
);
848 /* Destroy all of the batch buffers */
849 list_for_each_entry_safe(struct anv_batch_bo
, bbo
,
850 &cmd_buffer
->batch_bos
, link
) {
851 list_del(&bbo
->link
);
852 anv_batch_bo_destroy(bbo
, cmd_buffer
);
857 anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
859 /* Delete all but the first batch bo */
860 assert(!list_is_empty(&cmd_buffer
->batch_bos
));
861 while (cmd_buffer
->batch_bos
.next
!= cmd_buffer
->batch_bos
.prev
) {
862 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
863 list_del(&bbo
->link
);
864 anv_batch_bo_destroy(bbo
, cmd_buffer
);
866 assert(!list_is_empty(&cmd_buffer
->batch_bos
));
868 anv_batch_bo_start(anv_cmd_buffer_current_batch_bo(cmd_buffer
),
870 GEN8_MI_BATCH_BUFFER_START_length
* 4);
872 while (u_vector_length(&cmd_buffer
->bt_block_states
) > 1) {
873 struct anv_state
*bt_block
= u_vector_remove(&cmd_buffer
->bt_block_states
);
874 anv_binding_table_pool_free(cmd_buffer
->device
, *bt_block
);
876 assert(u_vector_length(&cmd_buffer
->bt_block_states
) == 1);
877 cmd_buffer
->bt_next
= *(struct anv_state
*)u_vector_head(&cmd_buffer
->bt_block_states
);
878 cmd_buffer
->bt_next
.offset
= 0;
880 anv_reloc_list_clear(&cmd_buffer
->surface_relocs
);
881 cmd_buffer
->last_ss_pool_center
= 0;
883 /* Reset the list of seen buffers */
884 cmd_buffer
->seen_bbos
.head
= 0;
885 cmd_buffer
->seen_bbos
.tail
= 0;
887 *(struct anv_batch_bo
**)u_vector_add(&cmd_buffer
->seen_bbos
) =
888 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
892 anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer
*cmd_buffer
)
894 struct anv_batch_bo
*batch_bo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
896 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
) {
897 /* When we start a batch buffer, we subtract a certain amount of
898 * padding from the end to ensure that we always have room to emit a
899 * BATCH_BUFFER_START to chain to the next BO. We need to remove
900 * that padding before we end the batch; otherwise, we may end up
901 * with our BATCH_BUFFER_END in another BO.
903 cmd_buffer
->batch
.end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
904 assert(cmd_buffer
->batch
.end
== batch_bo
->bo
->map
+ batch_bo
->bo
->size
);
906 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_BATCH_BUFFER_END
, bbe
);
908 /* Round batch up to an even number of dwords. */
909 if ((cmd_buffer
->batch
.next
- cmd_buffer
->batch
.start
) & 4)
910 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_NOOP
, noop
);
912 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_PRIMARY
;
914 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
);
915 /* If this is a secondary command buffer, we need to determine the
916 * mode in which it will be executed with vkExecuteCommands. We
917 * determine this statically here so that this stays in sync with the
918 * actual ExecuteCommands implementation.
920 const uint32_t length
= cmd_buffer
->batch
.next
- cmd_buffer
->batch
.start
;
921 if (!cmd_buffer
->device
->can_chain_batches
) {
922 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
;
923 } else if ((cmd_buffer
->batch_bos
.next
== cmd_buffer
->batch_bos
.prev
) &&
924 (length
< ANV_CMD_BUFFER_BATCH_SIZE
/ 2)) {
925 /* If the secondary has exactly one batch buffer in its list *and*
926 * that batch buffer is less than half of the maximum size, we're
927 * probably better of simply copying it into our batch.
929 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_EMIT
;
930 } else if (!(cmd_buffer
->usage_flags
&
931 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT
)) {
932 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_CHAIN
;
934 /* In order to chain, we need this command buffer to contain an
935 * MI_BATCH_BUFFER_START which will jump back to the calling batch.
936 * It doesn't matter where it points now so long as has a valid
937 * relocation. We'll adjust it later as part of the chaining
940 * We set the end of the batch a little short so we would be sure we
941 * have room for the chaining command. Since we're about to emit the
942 * chaining command, let's set it back where it should go.
944 cmd_buffer
->batch
.end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
945 assert(cmd_buffer
->batch
.start
== batch_bo
->bo
->map
);
946 assert(cmd_buffer
->batch
.end
== batch_bo
->bo
->map
+ batch_bo
->bo
->size
);
948 emit_batch_buffer_start(cmd_buffer
, batch_bo
->bo
, 0);
949 assert(cmd_buffer
->batch
.start
== batch_bo
->bo
->map
);
951 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
;
955 anv_batch_bo_finish(batch_bo
, &cmd_buffer
->batch
);
959 anv_cmd_buffer_add_seen_bbos(struct anv_cmd_buffer
*cmd_buffer
,
960 struct list_head
*list
)
962 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
963 struct anv_batch_bo
**bbo_ptr
= u_vector_add(&cmd_buffer
->seen_bbos
);
965 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
974 anv_cmd_buffer_add_secondary(struct anv_cmd_buffer
*primary
,
975 struct anv_cmd_buffer
*secondary
)
977 switch (secondary
->exec_mode
) {
978 case ANV_CMD_BUFFER_EXEC_MODE_EMIT
:
979 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
981 case ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
: {
982 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(primary
);
983 unsigned length
= secondary
->batch
.end
- secondary
->batch
.start
;
984 anv_batch_bo_grow(primary
, bbo
, &primary
->batch
, length
,
985 GEN8_MI_BATCH_BUFFER_START_length
* 4);
986 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
989 case ANV_CMD_BUFFER_EXEC_MODE_CHAIN
: {
990 struct anv_batch_bo
*first_bbo
=
991 list_first_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
992 struct anv_batch_bo
*last_bbo
=
993 list_last_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
995 emit_batch_buffer_start(primary
, first_bbo
->bo
, 0);
997 struct anv_batch_bo
*this_bbo
= anv_cmd_buffer_current_batch_bo(primary
);
998 assert(primary
->batch
.start
== this_bbo
->bo
->map
);
999 uint32_t offset
= primary
->batch
.next
- primary
->batch
.start
;
1001 /* Make the tail of the secondary point back to right after the
1002 * MI_BATCH_BUFFER_START in the primary batch.
1004 anv_batch_bo_link(primary
, last_bbo
, this_bbo
, offset
);
1006 anv_cmd_buffer_add_seen_bbos(primary
, &secondary
->batch_bos
);
1009 case ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
: {
1010 struct list_head copy_list
;
1011 VkResult result
= anv_batch_bo_list_clone(&secondary
->batch_bos
,
1014 if (result
!= VK_SUCCESS
)
1017 anv_cmd_buffer_add_seen_bbos(primary
, ©_list
);
1019 struct anv_batch_bo
*first_bbo
=
1020 list_first_entry(©_list
, struct anv_batch_bo
, link
);
1021 struct anv_batch_bo
*last_bbo
=
1022 list_last_entry(©_list
, struct anv_batch_bo
, link
);
1024 cmd_buffer_chain_to_batch_bo(primary
, first_bbo
);
1026 list_splicetail(©_list
, &primary
->batch_bos
);
1028 anv_batch_bo_continue(last_bbo
, &primary
->batch
,
1029 GEN8_MI_BATCH_BUFFER_START_length
* 4);
1033 assert(!"Invalid execution mode");
1036 anv_reloc_list_append(&primary
->surface_relocs
, &primary
->pool
->alloc
,
1037 &secondary
->surface_relocs
, 0);
1040 struct anv_execbuf
{
1041 struct drm_i915_gem_execbuffer2 execbuf
;
1043 struct drm_i915_gem_exec_object2
* objects
;
1045 struct anv_bo
** bos
;
1047 /* Allocated length of the 'objects' and 'bos' arrays */
1048 uint32_t array_length
;
1052 const VkAllocationCallbacks
* alloc
;
1053 VkSystemAllocationScope alloc_scope
;
1057 anv_execbuf_init(struct anv_execbuf
*exec
)
1059 memset(exec
, 0, sizeof(*exec
));
1063 anv_execbuf_finish(struct anv_execbuf
*exec
)
1065 vk_free(exec
->alloc
, exec
->objects
);
1066 vk_free(exec
->alloc
, exec
->bos
);
1070 anv_execbuf_add_bo_bitset(struct anv_device
*device
,
1071 struct anv_execbuf
*exec
,
1074 uint32_t extra_flags
);
1077 anv_execbuf_add_bo(struct anv_device
*device
,
1078 struct anv_execbuf
*exec
,
1080 struct anv_reloc_list
*relocs
,
1081 uint32_t extra_flags
)
1083 struct drm_i915_gem_exec_object2
*obj
= NULL
;
1085 bo
= anv_bo_unwrap(bo
);
1087 if (bo
->index
< exec
->bo_count
&& exec
->bos
[bo
->index
] == bo
)
1088 obj
= &exec
->objects
[bo
->index
];
1091 /* We've never seen this one before. Add it to the list and assign
1092 * an id that we can use later.
1094 if (exec
->bo_count
>= exec
->array_length
) {
1095 uint32_t new_len
= exec
->objects
? exec
->array_length
* 2 : 64;
1097 struct drm_i915_gem_exec_object2
*new_objects
=
1098 vk_alloc(exec
->alloc
, new_len
* sizeof(*new_objects
), 8, exec
->alloc_scope
);
1099 if (new_objects
== NULL
)
1100 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1102 struct anv_bo
**new_bos
=
1103 vk_alloc(exec
->alloc
, new_len
* sizeof(*new_bos
), 8, exec
->alloc_scope
);
1104 if (new_bos
== NULL
) {
1105 vk_free(exec
->alloc
, new_objects
);
1106 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1109 if (exec
->objects
) {
1110 memcpy(new_objects
, exec
->objects
,
1111 exec
->bo_count
* sizeof(*new_objects
));
1112 memcpy(new_bos
, exec
->bos
,
1113 exec
->bo_count
* sizeof(*new_bos
));
1116 vk_free(exec
->alloc
, exec
->objects
);
1117 vk_free(exec
->alloc
, exec
->bos
);
1119 exec
->objects
= new_objects
;
1120 exec
->bos
= new_bos
;
1121 exec
->array_length
= new_len
;
1124 assert(exec
->bo_count
< exec
->array_length
);
1126 bo
->index
= exec
->bo_count
++;
1127 obj
= &exec
->objects
[bo
->index
];
1128 exec
->bos
[bo
->index
] = bo
;
1130 obj
->handle
= bo
->gem_handle
;
1131 obj
->relocation_count
= 0;
1132 obj
->relocs_ptr
= 0;
1134 obj
->offset
= bo
->offset
;
1135 obj
->flags
= bo
->flags
| extra_flags
;
1140 if (extra_flags
& EXEC_OBJECT_WRITE
) {
1141 obj
->flags
|= EXEC_OBJECT_WRITE
;
1142 obj
->flags
&= ~EXEC_OBJECT_ASYNC
;
1145 if (relocs
!= NULL
) {
1146 assert(obj
->relocation_count
== 0);
1148 if (relocs
->num_relocs
> 0) {
1149 /* This is the first time we've ever seen a list of relocations for
1150 * this BO. Go ahead and set the relocations and then walk the list
1151 * of relocations and add them all.
1153 exec
->has_relocs
= true;
1154 obj
->relocation_count
= relocs
->num_relocs
;
1155 obj
->relocs_ptr
= (uintptr_t) relocs
->relocs
;
1157 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1160 /* A quick sanity check on relocations */
1161 assert(relocs
->relocs
[i
].offset
< bo
->size
);
1162 result
= anv_execbuf_add_bo(device
, exec
, relocs
->reloc_bos
[i
],
1164 if (result
!= VK_SUCCESS
)
1169 return anv_execbuf_add_bo_bitset(device
, exec
, relocs
->dep_words
,
1170 relocs
->deps
, extra_flags
);
1176 /* Add BO dependencies to execbuf */
1178 anv_execbuf_add_bo_bitset(struct anv_device
*device
,
1179 struct anv_execbuf
*exec
,
1182 uint32_t extra_flags
)
1184 for (uint32_t w
= 0; w
< dep_words
; w
++) {
1185 BITSET_WORD mask
= deps
[w
];
1187 int i
= u_bit_scan(&mask
);
1188 uint32_t gem_handle
= w
* BITSET_WORDBITS
+ i
;
1189 struct anv_bo
*bo
= anv_device_lookup_bo(device
, gem_handle
);
1190 assert(bo
->refcount
> 0);
1192 anv_execbuf_add_bo(device
, exec
, bo
, NULL
, extra_flags
);
1193 if (result
!= VK_SUCCESS
)
1202 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
1203 struct anv_reloc_list
*list
)
1205 for (size_t i
= 0; i
< list
->num_relocs
; i
++)
1206 list
->relocs
[i
].target_handle
= anv_bo_unwrap(list
->reloc_bos
[i
])->index
;
1210 adjust_relocations_from_state_pool(struct anv_state_pool
*pool
,
1211 struct anv_reloc_list
*relocs
,
1212 uint32_t last_pool_center_bo_offset
)
1214 assert(last_pool_center_bo_offset
<= pool
->block_pool
.center_bo_offset
);
1215 uint32_t delta
= pool
->block_pool
.center_bo_offset
- last_pool_center_bo_offset
;
1217 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1218 /* All of the relocations from this block pool to other BO's should
1219 * have been emitted relative to the surface block pool center. We
1220 * need to add the center offset to make them relative to the
1221 * beginning of the actual GEM bo.
1223 relocs
->relocs
[i
].offset
+= delta
;
1228 adjust_relocations_to_state_pool(struct anv_state_pool
*pool
,
1229 struct anv_bo
*from_bo
,
1230 struct anv_reloc_list
*relocs
,
1231 uint32_t last_pool_center_bo_offset
)
1233 assert(!from_bo
->is_wrapper
);
1234 assert(last_pool_center_bo_offset
<= pool
->block_pool
.center_bo_offset
);
1235 uint32_t delta
= pool
->block_pool
.center_bo_offset
- last_pool_center_bo_offset
;
1237 /* When we initially emit relocations into a block pool, we don't
1238 * actually know what the final center_bo_offset will be so we just emit
1239 * it as if center_bo_offset == 0. Now that we know what the center
1240 * offset is, we need to walk the list of relocations and adjust any
1241 * relocations that point to the pool bo with the correct offset.
1243 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1244 if (relocs
->reloc_bos
[i
] == pool
->block_pool
.bo
) {
1245 /* Adjust the delta value in the relocation to correctly
1246 * correspond to the new delta. Initially, this value may have
1247 * been negative (if treated as unsigned), but we trust in
1248 * uint32_t roll-over to fix that for us at this point.
1250 relocs
->relocs
[i
].delta
+= delta
;
1252 /* Since the delta has changed, we need to update the actual
1253 * relocated value with the new presumed value. This function
1254 * should only be called on batch buffers, so we know it isn't in
1255 * use by the GPU at the moment.
1257 assert(relocs
->relocs
[i
].offset
< from_bo
->size
);
1258 write_reloc(pool
->block_pool
.device
,
1259 from_bo
->map
+ relocs
->relocs
[i
].offset
,
1260 relocs
->relocs
[i
].presumed_offset
+
1261 relocs
->relocs
[i
].delta
, false);
1267 anv_reloc_list_apply(struct anv_device
*device
,
1268 struct anv_reloc_list
*list
,
1270 bool always_relocate
)
1272 bo
= anv_bo_unwrap(bo
);
1274 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
1275 struct anv_bo
*target_bo
= anv_bo_unwrap(list
->reloc_bos
[i
]);
1276 if (list
->relocs
[i
].presumed_offset
== target_bo
->offset
&&
1280 void *p
= bo
->map
+ list
->relocs
[i
].offset
;
1281 write_reloc(device
, p
, target_bo
->offset
+ list
->relocs
[i
].delta
, true);
1282 list
->relocs
[i
].presumed_offset
= target_bo
->offset
;
1287 * This function applies the relocation for a command buffer and writes the
1288 * actual addresses into the buffers as per what we were told by the kernel on
1289 * the previous execbuf2 call. This should be safe to do because, for each
1290 * relocated address, we have two cases:
1292 * 1) The target BO is inactive (as seen by the kernel). In this case, it is
1293 * not in use by the GPU so updating the address is 100% ok. It won't be
1294 * in-use by the GPU (from our context) again until the next execbuf2
1295 * happens. If the kernel decides to move it in the next execbuf2, it
1296 * will have to do the relocations itself, but that's ok because it should
1297 * have all of the information needed to do so.
1299 * 2) The target BO is active (as seen by the kernel). In this case, it
1300 * hasn't moved since the last execbuffer2 call because GTT shuffling
1301 * *only* happens when the BO is idle. (From our perspective, it only
1302 * happens inside the execbuffer2 ioctl, but the shuffling may be
1303 * triggered by another ioctl, with full-ppgtt this is limited to only
1304 * execbuffer2 ioctls on the same context, or memory pressure.) Since the
1305 * target BO hasn't moved, our anv_bo::offset exactly matches the BO's GTT
1306 * address and the relocated value we are writing into the BO will be the
1307 * same as the value that is already there.
1309 * There is also a possibility that the target BO is active but the exact
1310 * RENDER_SURFACE_STATE object we are writing the relocation into isn't in
1311 * use. In this case, the address currently in the RENDER_SURFACE_STATE
1312 * may be stale but it's still safe to write the relocation because that
1313 * particular RENDER_SURFACE_STATE object isn't in-use by the GPU and
1314 * won't be until the next execbuf2 call.
1316 * By doing relocations on the CPU, we can tell the kernel that it doesn't
1317 * need to bother. We want to do this because the surface state buffer is
1318 * used by every command buffer so, if the kernel does the relocations, it
1319 * will always be busy and the kernel will always stall. This is also
1320 * probably the fastest mechanism for doing relocations since the kernel would
1321 * have to make a full copy of all the relocations lists.
1324 relocate_cmd_buffer(struct anv_cmd_buffer
*cmd_buffer
,
1325 struct anv_execbuf
*exec
)
1327 if (!exec
->has_relocs
)
1330 static int userspace_relocs
= -1;
1331 if (userspace_relocs
< 0)
1332 userspace_relocs
= env_var_as_boolean("ANV_USERSPACE_RELOCS", true);
1333 if (!userspace_relocs
)
1336 /* First, we have to check to see whether or not we can even do the
1337 * relocation. New buffers which have never been submitted to the kernel
1338 * don't have a valid offset so we need to let the kernel do relocations so
1339 * that we can get offsets for them. On future execbuf2 calls, those
1340 * buffers will have offsets and we will be able to skip relocating.
1341 * Invalid offsets are indicated by anv_bo::offset == (uint64_t)-1.
1343 for (uint32_t i
= 0; i
< exec
->bo_count
; i
++) {
1344 assert(!exec
->bos
[i
]->is_wrapper
);
1345 if (exec
->bos
[i
]->offset
== (uint64_t)-1)
1349 /* Since surface states are shared between command buffers and we don't
1350 * know what order they will be submitted to the kernel, we don't know
1351 * what address is actually written in the surface state object at any
1352 * given time. The only option is to always relocate them.
1354 struct anv_bo
*surface_state_bo
=
1355 anv_bo_unwrap(cmd_buffer
->device
->surface_state_pool
.block_pool
.bo
);
1356 anv_reloc_list_apply(cmd_buffer
->device
, &cmd_buffer
->surface_relocs
,
1358 true /* always relocate surface states */);
1360 /* Since we own all of the batch buffers, we know what values are stored
1361 * in the relocated addresses and only have to update them if the offsets
1364 struct anv_batch_bo
**bbo
;
1365 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1366 anv_reloc_list_apply(cmd_buffer
->device
,
1367 &(*bbo
)->relocs
, (*bbo
)->bo
, false);
1370 for (uint32_t i
= 0; i
< exec
->bo_count
; i
++)
1371 exec
->objects
[i
].offset
= exec
->bos
[i
]->offset
;
1377 setup_execbuf_for_cmd_buffer(struct anv_execbuf
*execbuf
,
1378 struct anv_cmd_buffer
*cmd_buffer
)
1380 struct anv_batch
*batch
= &cmd_buffer
->batch
;
1381 struct anv_state_pool
*ss_pool
=
1382 &cmd_buffer
->device
->surface_state_pool
;
1384 adjust_relocations_from_state_pool(ss_pool
, &cmd_buffer
->surface_relocs
,
1385 cmd_buffer
->last_ss_pool_center
);
1387 if (cmd_buffer
->device
->physical
->use_softpin
) {
1388 anv_block_pool_foreach_bo(bo
, &ss_pool
->block_pool
) {
1389 result
= anv_execbuf_add_bo(cmd_buffer
->device
, execbuf
,
1391 if (result
!= VK_SUCCESS
)
1394 /* Add surface dependencies (BOs) to the execbuf */
1395 anv_execbuf_add_bo_bitset(cmd_buffer
->device
, execbuf
,
1396 cmd_buffer
->surface_relocs
.dep_words
,
1397 cmd_buffer
->surface_relocs
.deps
, 0);
1399 /* Add the BOs for all memory objects */
1400 list_for_each_entry(struct anv_device_memory
, mem
,
1401 &cmd_buffer
->device
->memory_objects
, link
) {
1402 result
= anv_execbuf_add_bo(cmd_buffer
->device
, execbuf
,
1404 if (result
!= VK_SUCCESS
)
1408 struct anv_block_pool
*pool
;
1409 pool
= &cmd_buffer
->device
->dynamic_state_pool
.block_pool
;
1410 anv_block_pool_foreach_bo(bo
, pool
) {
1411 result
= anv_execbuf_add_bo(cmd_buffer
->device
, execbuf
,
1413 if (result
!= VK_SUCCESS
)
1417 pool
= &cmd_buffer
->device
->instruction_state_pool
.block_pool
;
1418 anv_block_pool_foreach_bo(bo
, pool
) {
1419 result
= anv_execbuf_add_bo(cmd_buffer
->device
, execbuf
,
1421 if (result
!= VK_SUCCESS
)
1425 pool
= &cmd_buffer
->device
->binding_table_pool
.block_pool
;
1426 anv_block_pool_foreach_bo(bo
, pool
) {
1427 result
= anv_execbuf_add_bo(cmd_buffer
->device
, execbuf
,
1429 if (result
!= VK_SUCCESS
)
1433 /* Since we aren't in the softpin case, all of our STATE_BASE_ADDRESS BOs
1434 * will get added automatically by processing relocations on the batch
1435 * buffer. We have to add the surface state BO manually because it has
1436 * relocations of its own that we need to be sure are processsed.
1438 result
= anv_execbuf_add_bo(cmd_buffer
->device
, execbuf
,
1439 ss_pool
->block_pool
.bo
,
1440 &cmd_buffer
->surface_relocs
, 0);
1441 if (result
!= VK_SUCCESS
)
1445 /* First, we walk over all of the bos we've seen and add them and their
1446 * relocations to the validate list.
1448 struct anv_batch_bo
**bbo
;
1449 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1450 adjust_relocations_to_state_pool(ss_pool
, (*bbo
)->bo
, &(*bbo
)->relocs
,
1451 cmd_buffer
->last_ss_pool_center
);
1453 result
= anv_execbuf_add_bo(cmd_buffer
->device
, execbuf
,
1454 (*bbo
)->bo
, &(*bbo
)->relocs
, 0);
1455 if (result
!= VK_SUCCESS
)
1459 /* Now that we've adjusted all of the surface state relocations, we need to
1460 * record the surface state pool center so future executions of the command
1461 * buffer can adjust correctly.
1463 cmd_buffer
->last_ss_pool_center
= ss_pool
->block_pool
.center_bo_offset
;
1465 struct anv_batch_bo
*first_batch_bo
=
1466 list_first_entry(&cmd_buffer
->batch_bos
, struct anv_batch_bo
, link
);
1468 /* The kernel requires that the last entry in the validation list be the
1469 * batch buffer to execute. We can simply swap the element
1470 * corresponding to the first batch_bo in the chain with the last
1471 * element in the list.
1473 if (first_batch_bo
->bo
->index
!= execbuf
->bo_count
- 1) {
1474 uint32_t idx
= first_batch_bo
->bo
->index
;
1475 uint32_t last_idx
= execbuf
->bo_count
- 1;
1477 struct drm_i915_gem_exec_object2 tmp_obj
= execbuf
->objects
[idx
];
1478 assert(execbuf
->bos
[idx
] == first_batch_bo
->bo
);
1480 execbuf
->objects
[idx
] = execbuf
->objects
[last_idx
];
1481 execbuf
->bos
[idx
] = execbuf
->bos
[last_idx
];
1482 execbuf
->bos
[idx
]->index
= idx
;
1484 execbuf
->objects
[last_idx
] = tmp_obj
;
1485 execbuf
->bos
[last_idx
] = first_batch_bo
->bo
;
1486 first_batch_bo
->bo
->index
= last_idx
;
1489 /* If we are pinning our BOs, we shouldn't have to relocate anything */
1490 if (cmd_buffer
->device
->physical
->use_softpin
)
1491 assert(!execbuf
->has_relocs
);
1493 /* Now we go through and fixup all of the relocation lists to point to
1494 * the correct indices in the object array. We have to do this after we
1495 * reorder the list above as some of the indices may have changed.
1497 if (execbuf
->has_relocs
) {
1498 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
)
1499 anv_cmd_buffer_process_relocs(cmd_buffer
, &(*bbo
)->relocs
);
1501 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
1504 if (!cmd_buffer
->device
->info
.has_llc
) {
1505 __builtin_ia32_mfence();
1506 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1507 for (uint32_t i
= 0; i
< (*bbo
)->length
; i
+= CACHELINE_SIZE
)
1508 __builtin_ia32_clflush((*bbo
)->bo
->map
+ i
);
1512 execbuf
->execbuf
= (struct drm_i915_gem_execbuffer2
) {
1513 .buffers_ptr
= (uintptr_t) execbuf
->objects
,
1514 .buffer_count
= execbuf
->bo_count
,
1515 .batch_start_offset
= 0,
1516 .batch_len
= batch
->next
- batch
->start
,
1521 .flags
= I915_EXEC_HANDLE_LUT
| I915_EXEC_RENDER
,
1522 .rsvd1
= cmd_buffer
->device
->context_id
,
1526 if (relocate_cmd_buffer(cmd_buffer
, execbuf
)) {
1527 /* If we were able to successfully relocate everything, tell the kernel
1528 * that it can skip doing relocations. The requirement for using
1531 * 1) The addresses written in the objects must match the corresponding
1532 * reloc.presumed_offset which in turn must match the corresponding
1533 * execobject.offset.
1535 * 2) To avoid stalling, execobject.offset should match the current
1536 * address of that object within the active context.
1538 * In order to satisfy all of the invariants that make userspace
1539 * relocations to be safe (see relocate_cmd_buffer()), we need to
1540 * further ensure that the addresses we use match those used by the
1541 * kernel for the most recent execbuf2.
1543 * The kernel may still choose to do relocations anyway if something has
1544 * moved in the GTT. In this case, the relocation list still needs to be
1545 * valid. All relocations on the batch buffers are already valid and
1546 * kept up-to-date. For surface state relocations, by applying the
1547 * relocations in relocate_cmd_buffer, we ensured that the address in
1548 * the RENDER_SURFACE_STATE matches presumed_offset, so it should be
1549 * safe for the kernel to relocate them as needed.
1551 execbuf
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
1553 /* In the case where we fall back to doing kernel relocations, we need
1554 * to ensure that the relocation list is valid. All relocations on the
1555 * batch buffers are already valid and kept up-to-date. Since surface
1556 * states are shared between command buffers and we don't know what
1557 * order they will be submitted to the kernel, we don't know what
1558 * address is actually written in the surface state object at any given
1559 * time. The only option is to set a bogus presumed offset and let the
1560 * kernel relocate them.
1562 for (size_t i
= 0; i
< cmd_buffer
->surface_relocs
.num_relocs
; i
++)
1563 cmd_buffer
->surface_relocs
.relocs
[i
].presumed_offset
= -1;
1570 setup_empty_execbuf(struct anv_execbuf
*execbuf
, struct anv_device
*device
)
1572 VkResult result
= anv_execbuf_add_bo(device
, execbuf
,
1573 device
->trivial_batch_bo
,
1575 if (result
!= VK_SUCCESS
)
1578 execbuf
->execbuf
= (struct drm_i915_gem_execbuffer2
) {
1579 .buffers_ptr
= (uintptr_t) execbuf
->objects
,
1580 .buffer_count
= execbuf
->bo_count
,
1581 .batch_start_offset
= 0,
1582 .batch_len
= 8, /* GEN7_MI_BATCH_BUFFER_END and NOOP */
1583 .flags
= I915_EXEC_HANDLE_LUT
| I915_EXEC_RENDER
,
1584 .rsvd1
= device
->context_id
,
1591 /* We lock around execbuf for three main reasons:
1593 * 1) When a block pool is resized, we create a new gem handle with a
1594 * different size and, in the case of surface states, possibly a different
1595 * center offset but we re-use the same anv_bo struct when we do so. If
1596 * this happens in the middle of setting up an execbuf, we could end up
1597 * with our list of BOs out of sync with our list of gem handles.
1599 * 2) The algorithm we use for building the list of unique buffers isn't
1600 * thread-safe. While the client is supposed to syncronize around
1601 * QueueSubmit, this would be extremely difficult to debug if it ever came
1602 * up in the wild due to a broken app. It's better to play it safe and
1603 * just lock around QueueSubmit.
1605 * 3) The anv_cmd_buffer_execbuf function may perform relocations in
1606 * userspace. Due to the fact that the surface state buffer is shared
1607 * between batches, we can't afford to have that happen from multiple
1608 * threads at the same time. Even though the user is supposed to ensure
1609 * this doesn't happen, we play it safe as in (2) above.
1611 * Since the only other things that ever take the device lock such as block
1612 * pool resize only rarely happen, this will almost never be contended so
1613 * taking a lock isn't really an expensive operation in this case.
1616 anv_queue_execbuf_locked(struct anv_queue
*queue
,
1617 struct anv_queue_submit
*submit
)
1619 struct anv_device
*device
= queue
->device
;
1620 struct anv_execbuf execbuf
;
1621 anv_execbuf_init(&execbuf
);
1622 execbuf
.alloc
= submit
->alloc
;
1623 execbuf
.alloc_scope
= submit
->alloc_scope
;
1627 for (uint32_t i
= 0; i
< submit
->fence_bo_count
; i
++) {
1629 struct anv_bo
*bo
= anv_unpack_ptr(submit
->fence_bos
[i
], 1, &signaled
);
1631 result
= anv_execbuf_add_bo(device
, &execbuf
, bo
, NULL
,
1632 signaled
? EXEC_OBJECT_WRITE
: 0);
1633 if (result
!= VK_SUCCESS
)
1637 if (submit
->cmd_buffer
) {
1638 result
= setup_execbuf_for_cmd_buffer(&execbuf
, submit
->cmd_buffer
);
1639 } else if (submit
->simple_bo
) {
1640 result
= anv_execbuf_add_bo(device
, &execbuf
, submit
->simple_bo
, NULL
, 0);
1641 if (result
!= VK_SUCCESS
)
1644 execbuf
.execbuf
= (struct drm_i915_gem_execbuffer2
) {
1645 .buffers_ptr
= (uintptr_t) execbuf
.objects
,
1646 .buffer_count
= execbuf
.bo_count
,
1647 .batch_start_offset
= 0,
1648 .batch_len
= submit
->simple_bo_size
,
1649 .flags
= I915_EXEC_HANDLE_LUT
| I915_EXEC_RENDER
,
1650 .rsvd1
= device
->context_id
,
1654 result
= setup_empty_execbuf(&execbuf
, queue
->device
);
1657 if (result
!= VK_SUCCESS
)
1660 if (unlikely(INTEL_DEBUG
& DEBUG_BATCH
)) {
1661 if (submit
->cmd_buffer
) {
1662 struct anv_batch_bo
**bo
= u_vector_tail(&submit
->cmd_buffer
->seen_bbos
);
1664 device
->cmd_buffer_being_decoded
= submit
->cmd_buffer
;
1665 gen_print_batch(&device
->decoder_ctx
, (*bo
)->bo
->map
,
1666 (*bo
)->bo
->size
, (*bo
)->bo
->offset
, false);
1667 device
->cmd_buffer_being_decoded
= NULL
;
1668 } else if (submit
->simple_bo
) {
1669 gen_print_batch(&device
->decoder_ctx
, submit
->simple_bo
->map
,
1670 submit
->simple_bo
->size
, submit
->simple_bo
->offset
, false);
1672 gen_print_batch(&device
->decoder_ctx
,
1673 device
->trivial_batch_bo
->map
,
1674 device
->trivial_batch_bo
->size
,
1675 device
->trivial_batch_bo
->offset
, false);
1679 if (submit
->fence_count
> 0) {
1680 assert(device
->physical
->has_syncobj
);
1681 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_ARRAY
;
1682 execbuf
.execbuf
.num_cliprects
= submit
->fence_count
;
1683 execbuf
.execbuf
.cliprects_ptr
= (uintptr_t)submit
->fences
;
1686 if (submit
->in_fence
!= -1) {
1687 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_IN
;
1688 execbuf
.execbuf
.rsvd2
|= (uint32_t)submit
->in_fence
;
1691 if (submit
->need_out_fence
)
1692 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_OUT
;
1694 int ret
= queue
->device
->no_hw
? 0 :
1695 anv_gem_execbuffer(queue
->device
, &execbuf
.execbuf
);
1697 result
= anv_queue_set_lost(queue
, "execbuf2 failed: %m");
1699 struct drm_i915_gem_exec_object2
*objects
= execbuf
.objects
;
1700 for (uint32_t k
= 0; k
< execbuf
.bo_count
; k
++) {
1701 if (execbuf
.bos
[k
]->flags
& EXEC_OBJECT_PINNED
)
1702 assert(execbuf
.bos
[k
]->offset
== objects
[k
].offset
);
1703 execbuf
.bos
[k
]->offset
= objects
[k
].offset
;
1706 if (result
== VK_SUCCESS
&& submit
->need_out_fence
)
1707 submit
->out_fence
= execbuf
.execbuf
.rsvd2
>> 32;
1710 pthread_cond_broadcast(&device
->queue_submit
);
1712 anv_execbuf_finish(&execbuf
);