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
);
79 memcpy(list
->relocs
, other_list
->relocs
,
80 list
->array_length
* sizeof(*list
->relocs
));
81 memcpy(list
->reloc_bos
, other_list
->reloc_bos
,
82 list
->array_length
* sizeof(*list
->reloc_bos
));
89 anv_reloc_list_init(struct anv_reloc_list
*list
,
90 const VkAllocationCallbacks
*alloc
)
92 return anv_reloc_list_init_clone(list
, alloc
, NULL
);
96 anv_reloc_list_finish(struct anv_reloc_list
*list
,
97 const VkAllocationCallbacks
*alloc
)
99 vk_free(alloc
, list
->relocs
);
100 vk_free(alloc
, list
->reloc_bos
);
104 anv_reloc_list_grow(struct anv_reloc_list
*list
,
105 const VkAllocationCallbacks
*alloc
,
106 size_t num_additional_relocs
)
108 if (list
->num_relocs
+ num_additional_relocs
<= list
->array_length
)
111 size_t new_length
= list
->array_length
* 2;
112 while (new_length
< list
->num_relocs
+ num_additional_relocs
)
115 struct drm_i915_gem_relocation_entry
*new_relocs
=
116 vk_alloc(alloc
, new_length
* sizeof(*list
->relocs
), 8,
117 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
118 if (new_relocs
== NULL
)
119 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
121 struct anv_bo
**new_reloc_bos
=
122 vk_alloc(alloc
, new_length
* sizeof(*list
->reloc_bos
), 8,
123 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
124 if (new_reloc_bos
== NULL
) {
125 vk_free(alloc
, new_relocs
);
126 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
129 memcpy(new_relocs
, list
->relocs
, list
->num_relocs
* sizeof(*list
->relocs
));
130 memcpy(new_reloc_bos
, list
->reloc_bos
,
131 list
->num_relocs
* sizeof(*list
->reloc_bos
));
133 vk_free(alloc
, list
->relocs
);
134 vk_free(alloc
, list
->reloc_bos
);
136 list
->array_length
= new_length
;
137 list
->relocs
= new_relocs
;
138 list
->reloc_bos
= new_reloc_bos
;
144 anv_reloc_list_add(struct anv_reloc_list
*list
,
145 const VkAllocationCallbacks
*alloc
,
146 uint32_t offset
, struct anv_bo
*target_bo
, uint32_t delta
)
148 struct drm_i915_gem_relocation_entry
*entry
;
151 VkResult result
= anv_reloc_list_grow(list
, alloc
, 1);
152 if (result
!= VK_SUCCESS
)
155 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
156 index
= list
->num_relocs
++;
157 list
->reloc_bos
[index
] = target_bo
;
158 entry
= &list
->relocs
[index
];
159 entry
->target_handle
= target_bo
->gem_handle
;
160 entry
->delta
= delta
;
161 entry
->offset
= offset
;
162 entry
->presumed_offset
= target_bo
->offset
;
163 entry
->read_domains
= 0;
164 entry
->write_domain
= 0;
165 VG(VALGRIND_CHECK_MEM_IS_DEFINED(entry
, sizeof(*entry
)));
171 anv_reloc_list_append(struct anv_reloc_list
*list
,
172 const VkAllocationCallbacks
*alloc
,
173 struct anv_reloc_list
*other
, uint32_t offset
)
175 VkResult result
= anv_reloc_list_grow(list
, alloc
, other
->num_relocs
);
176 if (result
!= VK_SUCCESS
)
179 memcpy(&list
->relocs
[list
->num_relocs
], &other
->relocs
[0],
180 other
->num_relocs
* sizeof(other
->relocs
[0]));
181 memcpy(&list
->reloc_bos
[list
->num_relocs
], &other
->reloc_bos
[0],
182 other
->num_relocs
* sizeof(other
->reloc_bos
[0]));
184 for (uint32_t i
= 0; i
< other
->num_relocs
; i
++)
185 list
->relocs
[i
+ list
->num_relocs
].offset
+= offset
;
187 list
->num_relocs
+= other
->num_relocs
;
191 /*-----------------------------------------------------------------------*
192 * Functions related to anv_batch
193 *-----------------------------------------------------------------------*/
196 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
198 if (batch
->next
+ num_dwords
* 4 > batch
->end
) {
199 VkResult result
= batch
->extend_cb(batch
, batch
->user_data
);
200 if (result
!= VK_SUCCESS
) {
201 anv_batch_set_error(batch
, result
);
206 void *p
= batch
->next
;
208 batch
->next
+= num_dwords
* 4;
209 assert(batch
->next
<= batch
->end
);
215 anv_batch_emit_reloc(struct anv_batch
*batch
,
216 void *location
, struct anv_bo
*bo
, uint32_t delta
)
218 VkResult result
= anv_reloc_list_add(batch
->relocs
, batch
->alloc
,
219 location
- batch
->start
, bo
, delta
);
220 if (result
!= VK_SUCCESS
) {
221 anv_batch_set_error(batch
, result
);
225 return bo
->offset
+ delta
;
229 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
231 uint32_t size
, offset
;
233 size
= other
->next
- other
->start
;
234 assert(size
% 4 == 0);
236 if (batch
->next
+ size
> batch
->end
) {
237 VkResult result
= batch
->extend_cb(batch
, batch
->user_data
);
238 if (result
!= VK_SUCCESS
) {
239 anv_batch_set_error(batch
, result
);
244 assert(batch
->next
+ size
<= batch
->end
);
246 VG(VALGRIND_CHECK_MEM_IS_DEFINED(other
->start
, size
));
247 memcpy(batch
->next
, other
->start
, size
);
249 offset
= batch
->next
- batch
->start
;
250 VkResult result
= anv_reloc_list_append(batch
->relocs
, batch
->alloc
,
251 other
->relocs
, offset
);
252 if (result
!= VK_SUCCESS
) {
253 anv_batch_set_error(batch
, result
);
260 /*-----------------------------------------------------------------------*
261 * Functions related to anv_batch_bo
262 *-----------------------------------------------------------------------*/
265 anv_batch_bo_create(struct anv_cmd_buffer
*cmd_buffer
,
266 struct anv_batch_bo
**bbo_out
)
270 struct anv_batch_bo
*bbo
= vk_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
271 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
273 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
275 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
,
276 ANV_CMD_BUFFER_BATCH_SIZE
);
277 if (result
!= VK_SUCCESS
)
280 result
= anv_reloc_list_init(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
281 if (result
!= VK_SUCCESS
)
289 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
291 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
297 anv_batch_bo_clone(struct anv_cmd_buffer
*cmd_buffer
,
298 const struct anv_batch_bo
*other_bbo
,
299 struct anv_batch_bo
**bbo_out
)
303 struct anv_batch_bo
*bbo
= vk_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
304 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
306 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
308 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
,
310 if (result
!= VK_SUCCESS
)
313 result
= anv_reloc_list_init_clone(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
,
315 if (result
!= VK_SUCCESS
)
318 bbo
->length
= other_bbo
->length
;
319 memcpy(bbo
->bo
.map
, other_bbo
->bo
.map
, other_bbo
->length
);
326 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
328 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
334 anv_batch_bo_start(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
335 size_t batch_padding
)
337 batch
->next
= batch
->start
= bbo
->bo
.map
;
338 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
339 batch
->relocs
= &bbo
->relocs
;
340 bbo
->relocs
.num_relocs
= 0;
344 anv_batch_bo_continue(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
345 size_t batch_padding
)
347 batch
->start
= bbo
->bo
.map
;
348 batch
->next
= bbo
->bo
.map
+ bbo
->length
;
349 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
350 batch
->relocs
= &bbo
->relocs
;
354 anv_batch_bo_finish(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
)
356 assert(batch
->start
== bbo
->bo
.map
);
357 bbo
->length
= batch
->next
- batch
->start
;
358 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch
->start
, bbo
->length
));
362 anv_batch_bo_grow(struct anv_cmd_buffer
*cmd_buffer
, struct anv_batch_bo
*bbo
,
363 struct anv_batch
*batch
, size_t aditional
,
364 size_t batch_padding
)
366 assert(batch
->start
== bbo
->bo
.map
);
367 bbo
->length
= batch
->next
- batch
->start
;
369 size_t new_size
= bbo
->bo
.size
;
370 while (new_size
<= bbo
->length
+ aditional
+ batch_padding
)
373 if (new_size
== bbo
->bo
.size
)
376 struct anv_bo new_bo
;
377 VkResult result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
,
379 if (result
!= VK_SUCCESS
)
382 memcpy(new_bo
.map
, bbo
->bo
.map
, bbo
->length
);
384 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
387 anv_batch_bo_continue(bbo
, batch
, batch_padding
);
393 anv_batch_bo_destroy(struct anv_batch_bo
*bbo
,
394 struct anv_cmd_buffer
*cmd_buffer
)
396 anv_reloc_list_finish(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
397 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
398 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
402 anv_batch_bo_list_clone(const struct list_head
*list
,
403 struct anv_cmd_buffer
*cmd_buffer
,
404 struct list_head
*new_list
)
406 VkResult result
= VK_SUCCESS
;
408 list_inithead(new_list
);
410 struct anv_batch_bo
*prev_bbo
= NULL
;
411 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
412 struct anv_batch_bo
*new_bbo
= NULL
;
413 result
= anv_batch_bo_clone(cmd_buffer
, bbo
, &new_bbo
);
414 if (result
!= VK_SUCCESS
)
416 list_addtail(&new_bbo
->link
, new_list
);
419 /* As we clone this list of batch_bo's, they chain one to the
420 * other using MI_BATCH_BUFFER_START commands. We need to fix up
421 * those relocations as we go. Fortunately, this is pretty easy
422 * as it will always be the last relocation in the list.
424 uint32_t last_idx
= prev_bbo
->relocs
.num_relocs
- 1;
425 assert(prev_bbo
->relocs
.reloc_bos
[last_idx
] == &bbo
->bo
);
426 prev_bbo
->relocs
.reloc_bos
[last_idx
] = &new_bbo
->bo
;
432 if (result
!= VK_SUCCESS
) {
433 list_for_each_entry_safe(struct anv_batch_bo
, bbo
, new_list
, link
)
434 anv_batch_bo_destroy(bbo
, cmd_buffer
);
440 /*-----------------------------------------------------------------------*
441 * Functions related to anv_batch_bo
442 *-----------------------------------------------------------------------*/
444 static inline struct anv_batch_bo
*
445 anv_cmd_buffer_current_batch_bo(struct anv_cmd_buffer
*cmd_buffer
)
447 return LIST_ENTRY(struct anv_batch_bo
, cmd_buffer
->batch_bos
.prev
, link
);
451 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer
*cmd_buffer
)
453 struct anv_state
*bt_block
= u_vector_head(&cmd_buffer
->bt_block_states
);
454 return (struct anv_address
) {
455 .bo
= &cmd_buffer
->device
->surface_state_pool
.block_pool
.bo
,
456 .offset
= bt_block
->offset
,
461 emit_batch_buffer_start(struct anv_cmd_buffer
*cmd_buffer
,
462 struct anv_bo
*bo
, uint32_t offset
)
464 /* In gen8+ the address field grew to two dwords to accomodate 48 bit
465 * offsets. The high 16 bits are in the last dword, so we can use the gen8
466 * version in either case, as long as we set the instruction length in the
467 * header accordingly. This means that we always emit three dwords here
468 * and all the padding and adjustment we do in this file works for all
472 #define GEN7_MI_BATCH_BUFFER_START_length 2
473 #define GEN7_MI_BATCH_BUFFER_START_length_bias 2
475 const uint32_t gen7_length
=
476 GEN7_MI_BATCH_BUFFER_START_length
- GEN7_MI_BATCH_BUFFER_START_length_bias
;
477 const uint32_t gen8_length
=
478 GEN8_MI_BATCH_BUFFER_START_length
- GEN8_MI_BATCH_BUFFER_START_length_bias
;
480 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_BATCH_BUFFER_START
, bbs
) {
481 bbs
.DWordLength
= cmd_buffer
->device
->info
.gen
< 8 ?
482 gen7_length
: gen8_length
;
483 bbs
._2ndLevelBatchBuffer
= _1stlevelbatch
;
484 bbs
.AddressSpaceIndicator
= ASI_PPGTT
;
485 bbs
.BatchBufferStartAddress
= (struct anv_address
) { bo
, offset
};
490 cmd_buffer_chain_to_batch_bo(struct anv_cmd_buffer
*cmd_buffer
,
491 struct anv_batch_bo
*bbo
)
493 struct anv_batch
*batch
= &cmd_buffer
->batch
;
494 struct anv_batch_bo
*current_bbo
=
495 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
497 /* We set the end of the batch a little short so we would be sure we
498 * have room for the chaining command. Since we're about to emit the
499 * chaining command, let's set it back where it should go.
501 batch
->end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
502 assert(batch
->end
== current_bbo
->bo
.map
+ current_bbo
->bo
.size
);
504 emit_batch_buffer_start(cmd_buffer
, &bbo
->bo
, 0);
506 anv_batch_bo_finish(current_bbo
, batch
);
510 anv_cmd_buffer_chain_batch(struct anv_batch
*batch
, void *_data
)
512 struct anv_cmd_buffer
*cmd_buffer
= _data
;
513 struct anv_batch_bo
*new_bbo
;
515 VkResult result
= anv_batch_bo_create(cmd_buffer
, &new_bbo
);
516 if (result
!= VK_SUCCESS
)
519 struct anv_batch_bo
**seen_bbo
= u_vector_add(&cmd_buffer
->seen_bbos
);
520 if (seen_bbo
== NULL
) {
521 anv_batch_bo_destroy(new_bbo
, cmd_buffer
);
522 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
526 cmd_buffer_chain_to_batch_bo(cmd_buffer
, new_bbo
);
528 list_addtail(&new_bbo
->link
, &cmd_buffer
->batch_bos
);
530 anv_batch_bo_start(new_bbo
, batch
, GEN8_MI_BATCH_BUFFER_START_length
* 4);
536 anv_cmd_buffer_grow_batch(struct anv_batch
*batch
, void *_data
)
538 struct anv_cmd_buffer
*cmd_buffer
= _data
;
539 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
541 anv_batch_bo_grow(cmd_buffer
, bbo
, &cmd_buffer
->batch
, 4096,
542 GEN8_MI_BATCH_BUFFER_START_length
* 4);
547 /** Allocate a binding table
549 * This function allocates a binding table. This is a bit more complicated
550 * than one would think due to a combination of Vulkan driver design and some
551 * unfortunate hardware restrictions.
553 * The 3DSTATE_BINDING_TABLE_POINTERS_* packets only have a 16-bit field for
554 * the binding table pointer which means that all binding tables need to live
555 * in the bottom 64k of surface state base address. The way the GL driver has
556 * classically dealt with this restriction is to emit all surface states
557 * on-the-fly into the batch and have a batch buffer smaller than 64k. This
558 * isn't really an option in Vulkan for a couple of reasons:
560 * 1) In Vulkan, we have growing (or chaining) batches so surface states have
561 * to live in their own buffer and we have to be able to re-emit
562 * STATE_BASE_ADDRESS as needed which requires a full pipeline stall. In
563 * order to avoid emitting STATE_BASE_ADDRESS any more often than needed
564 * (it's not that hard to hit 64k of just binding tables), we allocate
565 * surface state objects up-front when VkImageView is created. In order
566 * for this to work, surface state objects need to be allocated from a
569 * 2) We tried to design the surface state system in such a way that it's
570 * already ready for bindless texturing. The way bindless texturing works
571 * on our hardware is that you have a big pool of surface state objects
572 * (with its own state base address) and the bindless handles are simply
573 * offsets into that pool. With the architecture we chose, we already
574 * have that pool and it's exactly the same pool that we use for regular
575 * surface states so we should already be ready for bindless.
577 * 3) For render targets, we need to be able to fill out the surface states
578 * later in vkBeginRenderPass so that we can assign clear colors
579 * correctly. One way to do this would be to just create the surface
580 * state data and then repeatedly copy it into the surface state BO every
581 * time we have to re-emit STATE_BASE_ADDRESS. While this works, it's
582 * rather annoying and just being able to allocate them up-front and
583 * re-use them for the entire render pass.
585 * While none of these are technically blockers for emitting state on the fly
586 * like we do in GL, the ability to have a single surface state pool is
587 * simplifies things greatly. Unfortunately, it comes at a cost...
589 * Because of the 64k limitation of 3DSTATE_BINDING_TABLE_POINTERS_*, we can't
590 * place the binding tables just anywhere in surface state base address.
591 * Because 64k isn't a whole lot of space, we can't simply restrict the
592 * surface state buffer to 64k, we have to be more clever. The solution we've
593 * chosen is to have a block pool with a maximum size of 2G that starts at
594 * zero and grows in both directions. All surface states are allocated from
595 * the top of the pool (positive offsets) and we allocate blocks (< 64k) of
596 * binding tables from the bottom of the pool (negative offsets). Every time
597 * we allocate a new binding table block, we set surface state base address to
598 * point to the bottom of the binding table block. This way all of the
599 * binding tables in the block are in the bottom 64k of surface state base
600 * address. When we fill out the binding table, we add the distance between
601 * the bottom of our binding table block and zero of the block pool to the
602 * surface state offsets so that they are correct relative to out new surface
603 * state base address at the bottom of the binding table block.
605 * \see adjust_relocations_from_block_pool()
606 * \see adjust_relocations_too_block_pool()
608 * \param[in] entries The number of surface state entries the binding
609 * table should be able to hold.
611 * \param[out] state_offset The offset surface surface state base address
612 * where the surface states live. This must be
613 * added to the surface state offset when it is
614 * written into the binding table entry.
616 * \return An anv_state representing the binding table
619 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
620 uint32_t entries
, uint32_t *state_offset
)
622 struct anv_state_pool
*state_pool
= &cmd_buffer
->device
->surface_state_pool
;
623 struct anv_state
*bt_block
= u_vector_head(&cmd_buffer
->bt_block_states
);
624 struct anv_state state
;
626 state
.alloc_size
= align_u32(entries
* 4, 32);
628 if (cmd_buffer
->bt_next
+ state
.alloc_size
> state_pool
->block_size
)
629 return (struct anv_state
) { 0 };
631 state
.offset
= cmd_buffer
->bt_next
;
632 state
.map
= state_pool
->block_pool
.map
+ bt_block
->offset
+ state
.offset
;
634 cmd_buffer
->bt_next
+= state
.alloc_size
;
636 assert(bt_block
->offset
< 0);
637 *state_offset
= -bt_block
->offset
;
643 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
)
645 struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
646 return anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
647 isl_dev
->ss
.size
, isl_dev
->ss
.align
);
651 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer
*cmd_buffer
,
652 uint32_t size
, uint32_t alignment
)
654 return anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
659 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer
*cmd_buffer
)
661 struct anv_state_pool
*state_pool
= &cmd_buffer
->device
->surface_state_pool
;
663 struct anv_state
*bt_block
= u_vector_add(&cmd_buffer
->bt_block_states
);
664 if (bt_block
== NULL
) {
665 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
666 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
669 *bt_block
= anv_state_pool_alloc_back(state_pool
);
670 cmd_buffer
->bt_next
= 0;
676 anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
678 struct anv_batch_bo
*batch_bo
;
681 list_inithead(&cmd_buffer
->batch_bos
);
683 result
= anv_batch_bo_create(cmd_buffer
, &batch_bo
);
684 if (result
!= VK_SUCCESS
)
687 list_addtail(&batch_bo
->link
, &cmd_buffer
->batch_bos
);
689 cmd_buffer
->batch
.alloc
= &cmd_buffer
->pool
->alloc
;
690 cmd_buffer
->batch
.user_data
= cmd_buffer
;
692 if (cmd_buffer
->device
->can_chain_batches
) {
693 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_chain_batch
;
695 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_grow_batch
;
698 anv_batch_bo_start(batch_bo
, &cmd_buffer
->batch
,
699 GEN8_MI_BATCH_BUFFER_START_length
* 4);
701 int success
= u_vector_init(&cmd_buffer
->seen_bbos
,
702 sizeof(struct anv_bo
*),
703 8 * sizeof(struct anv_bo
*));
707 *(struct anv_batch_bo
**)u_vector_add(&cmd_buffer
->seen_bbos
) = batch_bo
;
709 /* u_vector requires power-of-two size elements */
710 unsigned pow2_state_size
= util_next_power_of_two(sizeof(struct anv_state
));
711 success
= u_vector_init(&cmd_buffer
->bt_block_states
,
712 pow2_state_size
, 8 * pow2_state_size
);
716 result
= anv_reloc_list_init(&cmd_buffer
->surface_relocs
,
717 &cmd_buffer
->pool
->alloc
);
718 if (result
!= VK_SUCCESS
)
720 cmd_buffer
->last_ss_pool_center
= 0;
722 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
723 if (result
!= VK_SUCCESS
)
729 u_vector_finish(&cmd_buffer
->bt_block_states
);
731 u_vector_finish(&cmd_buffer
->seen_bbos
);
733 anv_batch_bo_destroy(batch_bo
, cmd_buffer
);
739 anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
741 struct anv_state
*bt_block
;
742 u_vector_foreach(bt_block
, &cmd_buffer
->bt_block_states
)
743 anv_state_pool_free(&cmd_buffer
->device
->surface_state_pool
, *bt_block
);
744 u_vector_finish(&cmd_buffer
->bt_block_states
);
746 anv_reloc_list_finish(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
);
748 u_vector_finish(&cmd_buffer
->seen_bbos
);
750 /* Destroy all of the batch buffers */
751 list_for_each_entry_safe(struct anv_batch_bo
, bbo
,
752 &cmd_buffer
->batch_bos
, link
) {
753 anv_batch_bo_destroy(bbo
, cmd_buffer
);
758 anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
760 /* Delete all but the first batch bo */
761 assert(!list_empty(&cmd_buffer
->batch_bos
));
762 while (cmd_buffer
->batch_bos
.next
!= cmd_buffer
->batch_bos
.prev
) {
763 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
764 list_del(&bbo
->link
);
765 anv_batch_bo_destroy(bbo
, cmd_buffer
);
767 assert(!list_empty(&cmd_buffer
->batch_bos
));
769 anv_batch_bo_start(anv_cmd_buffer_current_batch_bo(cmd_buffer
),
771 GEN8_MI_BATCH_BUFFER_START_length
* 4);
773 while (u_vector_length(&cmd_buffer
->bt_block_states
) > 1) {
774 struct anv_state
*bt_block
= u_vector_remove(&cmd_buffer
->bt_block_states
);
775 anv_state_pool_free(&cmd_buffer
->device
->surface_state_pool
, *bt_block
);
777 assert(u_vector_length(&cmd_buffer
->bt_block_states
) == 1);
778 cmd_buffer
->bt_next
= 0;
780 cmd_buffer
->surface_relocs
.num_relocs
= 0;
781 cmd_buffer
->last_ss_pool_center
= 0;
783 /* Reset the list of seen buffers */
784 cmd_buffer
->seen_bbos
.head
= 0;
785 cmd_buffer
->seen_bbos
.tail
= 0;
787 *(struct anv_batch_bo
**)u_vector_add(&cmd_buffer
->seen_bbos
) =
788 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
792 anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer
*cmd_buffer
)
794 struct anv_batch_bo
*batch_bo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
796 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
) {
797 /* When we start a batch buffer, we subtract a certain amount of
798 * padding from the end to ensure that we always have room to emit a
799 * BATCH_BUFFER_START to chain to the next BO. We need to remove
800 * that padding before we end the batch; otherwise, we may end up
801 * with our BATCH_BUFFER_END in another BO.
803 cmd_buffer
->batch
.end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
804 assert(cmd_buffer
->batch
.end
== batch_bo
->bo
.map
+ batch_bo
->bo
.size
);
806 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_BATCH_BUFFER_END
, bbe
);
808 /* Round batch up to an even number of dwords. */
809 if ((cmd_buffer
->batch
.next
- cmd_buffer
->batch
.start
) & 4)
810 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_NOOP
, noop
);
812 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_PRIMARY
;
815 anv_batch_bo_finish(batch_bo
, &cmd_buffer
->batch
);
817 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
) {
818 /* If this is a secondary command buffer, we need to determine the
819 * mode in which it will be executed with vkExecuteCommands. We
820 * determine this statically here so that this stays in sync with the
821 * actual ExecuteCommands implementation.
823 if (!cmd_buffer
->device
->can_chain_batches
) {
824 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
;
825 } else if ((cmd_buffer
->batch_bos
.next
== cmd_buffer
->batch_bos
.prev
) &&
826 (batch_bo
->length
< ANV_CMD_BUFFER_BATCH_SIZE
/ 2)) {
827 /* If the secondary has exactly one batch buffer in its list *and*
828 * that batch buffer is less than half of the maximum size, we're
829 * probably better of simply copying it into our batch.
831 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_EMIT
;
832 } else if (!(cmd_buffer
->usage_flags
&
833 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT
)) {
834 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_CHAIN
;
836 /* When we chain, we need to add an MI_BATCH_BUFFER_START command
837 * with its relocation. In order to handle this we'll increment here
838 * so we can unconditionally decrement right before adding the
839 * MI_BATCH_BUFFER_START command.
841 batch_bo
->relocs
.num_relocs
++;
842 cmd_buffer
->batch
.next
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
844 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
;
849 static inline VkResult
850 anv_cmd_buffer_add_seen_bbos(struct anv_cmd_buffer
*cmd_buffer
,
851 struct list_head
*list
)
853 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
854 struct anv_batch_bo
**bbo_ptr
= u_vector_add(&cmd_buffer
->seen_bbos
);
856 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
865 anv_cmd_buffer_add_secondary(struct anv_cmd_buffer
*primary
,
866 struct anv_cmd_buffer
*secondary
)
868 switch (secondary
->exec_mode
) {
869 case ANV_CMD_BUFFER_EXEC_MODE_EMIT
:
870 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
872 case ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
: {
873 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(primary
);
874 unsigned length
= secondary
->batch
.end
- secondary
->batch
.start
;
875 anv_batch_bo_grow(primary
, bbo
, &primary
->batch
, length
,
876 GEN8_MI_BATCH_BUFFER_START_length
* 4);
877 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
880 case ANV_CMD_BUFFER_EXEC_MODE_CHAIN
: {
881 struct anv_batch_bo
*first_bbo
=
882 list_first_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
883 struct anv_batch_bo
*last_bbo
=
884 list_last_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
886 emit_batch_buffer_start(primary
, &first_bbo
->bo
, 0);
888 struct anv_batch_bo
*this_bbo
= anv_cmd_buffer_current_batch_bo(primary
);
889 assert(primary
->batch
.start
== this_bbo
->bo
.map
);
890 uint32_t offset
= primary
->batch
.next
- primary
->batch
.start
;
891 const uint32_t inst_size
= GEN8_MI_BATCH_BUFFER_START_length
* 4;
893 /* Roll back the previous MI_BATCH_BUFFER_START and its relocation so we
894 * can emit a new command and relocation for the current splice. In
895 * order to handle the initial-use case, we incremented next and
896 * num_relocs in end_batch_buffer() so we can alyways just subtract
899 last_bbo
->relocs
.num_relocs
--;
900 secondary
->batch
.next
-= inst_size
;
901 emit_batch_buffer_start(secondary
, &this_bbo
->bo
, offset
);
902 anv_cmd_buffer_add_seen_bbos(primary
, &secondary
->batch_bos
);
904 /* After patching up the secondary buffer, we need to clflush the
905 * modified instruction in case we're on a !llc platform. We use a
906 * little loop to handle the case where the instruction crosses a cache
909 if (!primary
->device
->info
.has_llc
) {
910 void *inst
= secondary
->batch
.next
- inst_size
;
911 void *p
= (void *) (((uintptr_t) inst
) & ~CACHELINE_MASK
);
912 __builtin_ia32_mfence();
913 while (p
< secondary
->batch
.next
) {
914 __builtin_ia32_clflush(p
);
920 case ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
: {
921 struct list_head copy_list
;
922 VkResult result
= anv_batch_bo_list_clone(&secondary
->batch_bos
,
925 if (result
!= VK_SUCCESS
)
928 anv_cmd_buffer_add_seen_bbos(primary
, ©_list
);
930 struct anv_batch_bo
*first_bbo
=
931 list_first_entry(©_list
, struct anv_batch_bo
, link
);
932 struct anv_batch_bo
*last_bbo
=
933 list_last_entry(©_list
, struct anv_batch_bo
, link
);
935 cmd_buffer_chain_to_batch_bo(primary
, first_bbo
);
937 list_splicetail(©_list
, &primary
->batch_bos
);
939 anv_batch_bo_continue(last_bbo
, &primary
->batch
,
940 GEN8_MI_BATCH_BUFFER_START_length
* 4);
944 assert(!"Invalid execution mode");
947 anv_reloc_list_append(&primary
->surface_relocs
, &primary
->pool
->alloc
,
948 &secondary
->surface_relocs
, 0);
952 struct drm_i915_gem_execbuffer2 execbuf
;
954 struct drm_i915_gem_exec_object2
* objects
;
956 struct anv_bo
** bos
;
958 /* Allocated length of the 'objects' and 'bos' arrays */
959 uint32_t array_length
;
961 uint32_t fence_count
;
962 uint32_t fence_array_length
;
963 struct drm_i915_gem_exec_fence
* fences
;
964 struct anv_syncobj
** syncobjs
;
968 anv_execbuf_init(struct anv_execbuf
*exec
)
970 memset(exec
, 0, sizeof(*exec
));
974 anv_execbuf_finish(struct anv_execbuf
*exec
,
975 const VkAllocationCallbacks
*alloc
)
977 vk_free(alloc
, exec
->objects
);
978 vk_free(alloc
, exec
->bos
);
979 vk_free(alloc
, exec
->fences
);
980 vk_free(alloc
, exec
->syncobjs
);
984 anv_execbuf_add_bo(struct anv_execbuf
*exec
,
986 struct anv_reloc_list
*relocs
,
987 uint32_t extra_flags
,
988 const VkAllocationCallbacks
*alloc
)
990 struct drm_i915_gem_exec_object2
*obj
= NULL
;
992 if (bo
->index
< exec
->bo_count
&& exec
->bos
[bo
->index
] == bo
)
993 obj
= &exec
->objects
[bo
->index
];
996 /* We've never seen this one before. Add it to the list and assign
997 * an id that we can use later.
999 if (exec
->bo_count
>= exec
->array_length
) {
1000 uint32_t new_len
= exec
->objects
? exec
->array_length
* 2 : 64;
1002 struct drm_i915_gem_exec_object2
*new_objects
=
1003 vk_alloc(alloc
, new_len
* sizeof(*new_objects
),
1004 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1005 if (new_objects
== NULL
)
1006 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1008 struct anv_bo
**new_bos
=
1009 vk_alloc(alloc
, new_len
* sizeof(*new_bos
),
1010 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1011 if (new_bos
== NULL
) {
1012 vk_free(alloc
, new_objects
);
1013 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1016 if (exec
->objects
) {
1017 memcpy(new_objects
, exec
->objects
,
1018 exec
->bo_count
* sizeof(*new_objects
));
1019 memcpy(new_bos
, exec
->bos
,
1020 exec
->bo_count
* sizeof(*new_bos
));
1023 vk_free(alloc
, exec
->objects
);
1024 vk_free(alloc
, exec
->bos
);
1026 exec
->objects
= new_objects
;
1027 exec
->bos
= new_bos
;
1028 exec
->array_length
= new_len
;
1031 assert(exec
->bo_count
< exec
->array_length
);
1033 bo
->index
= exec
->bo_count
++;
1034 obj
= &exec
->objects
[bo
->index
];
1035 exec
->bos
[bo
->index
] = bo
;
1037 obj
->handle
= bo
->gem_handle
;
1038 obj
->relocation_count
= 0;
1039 obj
->relocs_ptr
= 0;
1041 obj
->offset
= bo
->offset
;
1042 obj
->flags
= bo
->flags
| extra_flags
;
1047 if (relocs
!= NULL
&& obj
->relocation_count
== 0) {
1048 /* This is the first time we've ever seen a list of relocations for
1049 * this BO. Go ahead and set the relocations and then walk the list
1050 * of relocations and add them all.
1052 obj
->relocation_count
= relocs
->num_relocs
;
1053 obj
->relocs_ptr
= (uintptr_t) relocs
->relocs
;
1055 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1058 /* A quick sanity check on relocations */
1059 assert(relocs
->relocs
[i
].offset
< bo
->size
);
1060 result
= anv_execbuf_add_bo(exec
, relocs
->reloc_bos
[i
], NULL
,
1061 extra_flags
, alloc
);
1063 if (result
!= VK_SUCCESS
)
1072 anv_execbuf_add_syncobj(struct anv_execbuf
*exec
,
1073 uint32_t handle
, uint32_t flags
,
1074 const VkAllocationCallbacks
*alloc
)
1078 if (exec
->fence_count
>= exec
->fence_array_length
) {
1079 uint32_t new_len
= MAX2(exec
->fence_array_length
* 2, 64);
1081 exec
->fences
= vk_realloc(alloc
, exec
->fences
,
1082 new_len
* sizeof(*exec
->fences
),
1083 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1084 if (exec
->fences
== NULL
)
1085 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1087 exec
->fence_array_length
= new_len
;
1090 exec
->fences
[exec
->fence_count
] = (struct drm_i915_gem_exec_fence
) {
1095 exec
->fence_count
++;
1101 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
1102 struct anv_reloc_list
*list
)
1104 for (size_t i
= 0; i
< list
->num_relocs
; i
++)
1105 list
->relocs
[i
].target_handle
= list
->reloc_bos
[i
]->index
;
1109 write_reloc(const struct anv_device
*device
, void *p
, uint64_t v
, bool flush
)
1111 unsigned reloc_size
= 0;
1112 if (device
->info
.gen
>= 8) {
1113 /* From the Broadwell PRM Vol. 2a, MI_LOAD_REGISTER_MEM::MemoryAddress:
1115 * "This field specifies the address of the memory location where the
1116 * register value specified in the DWord above will read from. The
1117 * address specifies the DWord location of the data. Range =
1118 * GraphicsVirtualAddress[63:2] for a DWord register GraphicsAddress
1119 * [63:48] are ignored by the HW and assumed to be in correct
1120 * canonical form [63:48] == [47]."
1122 const int shift
= 63 - 47;
1123 reloc_size
= sizeof(uint64_t);
1124 *(uint64_t *)p
= (((int64_t)v
) << shift
) >> shift
;
1126 reloc_size
= sizeof(uint32_t);
1130 if (flush
&& !device
->info
.has_llc
)
1131 gen_flush_range(p
, reloc_size
);
1135 adjust_relocations_from_state_pool(struct anv_state_pool
*pool
,
1136 struct anv_reloc_list
*relocs
,
1137 uint32_t last_pool_center_bo_offset
)
1139 assert(last_pool_center_bo_offset
<= pool
->block_pool
.center_bo_offset
);
1140 uint32_t delta
= pool
->block_pool
.center_bo_offset
- last_pool_center_bo_offset
;
1142 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1143 /* All of the relocations from this block pool to other BO's should
1144 * have been emitted relative to the surface block pool center. We
1145 * need to add the center offset to make them relative to the
1146 * beginning of the actual GEM bo.
1148 relocs
->relocs
[i
].offset
+= delta
;
1153 adjust_relocations_to_state_pool(struct anv_state_pool
*pool
,
1154 struct anv_bo
*from_bo
,
1155 struct anv_reloc_list
*relocs
,
1156 uint32_t last_pool_center_bo_offset
)
1158 assert(last_pool_center_bo_offset
<= pool
->block_pool
.center_bo_offset
);
1159 uint32_t delta
= pool
->block_pool
.center_bo_offset
- last_pool_center_bo_offset
;
1161 /* When we initially emit relocations into a block pool, we don't
1162 * actually know what the final center_bo_offset will be so we just emit
1163 * it as if center_bo_offset == 0. Now that we know what the center
1164 * offset is, we need to walk the list of relocations and adjust any
1165 * relocations that point to the pool bo with the correct offset.
1167 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1168 if (relocs
->reloc_bos
[i
] == &pool
->block_pool
.bo
) {
1169 /* Adjust the delta value in the relocation to correctly
1170 * correspond to the new delta. Initially, this value may have
1171 * been negative (if treated as unsigned), but we trust in
1172 * uint32_t roll-over to fix that for us at this point.
1174 relocs
->relocs
[i
].delta
+= delta
;
1176 /* Since the delta has changed, we need to update the actual
1177 * relocated value with the new presumed value. This function
1178 * should only be called on batch buffers, so we know it isn't in
1179 * use by the GPU at the moment.
1181 assert(relocs
->relocs
[i
].offset
< from_bo
->size
);
1182 write_reloc(pool
->block_pool
.device
,
1183 from_bo
->map
+ relocs
->relocs
[i
].offset
,
1184 relocs
->relocs
[i
].presumed_offset
+
1185 relocs
->relocs
[i
].delta
, false);
1191 anv_reloc_list_apply(struct anv_device
*device
,
1192 struct anv_reloc_list
*list
,
1194 bool always_relocate
)
1196 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
1197 struct anv_bo
*target_bo
= list
->reloc_bos
[i
];
1198 if (list
->relocs
[i
].presumed_offset
== target_bo
->offset
&&
1202 void *p
= bo
->map
+ list
->relocs
[i
].offset
;
1203 write_reloc(device
, p
, target_bo
->offset
+ list
->relocs
[i
].delta
, true);
1204 list
->relocs
[i
].presumed_offset
= target_bo
->offset
;
1209 * This function applies the relocation for a command buffer and writes the
1210 * actual addresses into the buffers as per what we were told by the kernel on
1211 * the previous execbuf2 call. This should be safe to do because, for each
1212 * relocated address, we have two cases:
1214 * 1) The target BO is inactive (as seen by the kernel). In this case, it is
1215 * not in use by the GPU so updating the address is 100% ok. It won't be
1216 * in-use by the GPU (from our context) again until the next execbuf2
1217 * happens. If the kernel decides to move it in the next execbuf2, it
1218 * will have to do the relocations itself, but that's ok because it should
1219 * have all of the information needed to do so.
1221 * 2) The target BO is active (as seen by the kernel). In this case, it
1222 * hasn't moved since the last execbuffer2 call because GTT shuffling
1223 * *only* happens when the BO is idle. (From our perspective, it only
1224 * happens inside the execbuffer2 ioctl, but the shuffling may be
1225 * triggered by another ioctl, with full-ppgtt this is limited to only
1226 * execbuffer2 ioctls on the same context, or memory pressure.) Since the
1227 * target BO hasn't moved, our anv_bo::offset exactly matches the BO's GTT
1228 * address and the relocated value we are writing into the BO will be the
1229 * same as the value that is already there.
1231 * There is also a possibility that the target BO is active but the exact
1232 * RENDER_SURFACE_STATE object we are writing the relocation into isn't in
1233 * use. In this case, the address currently in the RENDER_SURFACE_STATE
1234 * may be stale but it's still safe to write the relocation because that
1235 * particular RENDER_SURFACE_STATE object isn't in-use by the GPU and
1236 * won't be until the next execbuf2 call.
1238 * By doing relocations on the CPU, we can tell the kernel that it doesn't
1239 * need to bother. We want to do this because the surface state buffer is
1240 * used by every command buffer so, if the kernel does the relocations, it
1241 * will always be busy and the kernel will always stall. This is also
1242 * probably the fastest mechanism for doing relocations since the kernel would
1243 * have to make a full copy of all the relocations lists.
1246 relocate_cmd_buffer(struct anv_cmd_buffer
*cmd_buffer
,
1247 struct anv_execbuf
*exec
)
1249 static int userspace_relocs
= -1;
1250 if (userspace_relocs
< 0)
1251 userspace_relocs
= env_var_as_boolean("ANV_USERSPACE_RELOCS", true);
1252 if (!userspace_relocs
)
1255 /* First, we have to check to see whether or not we can even do the
1256 * relocation. New buffers which have never been submitted to the kernel
1257 * don't have a valid offset so we need to let the kernel do relocations so
1258 * that we can get offsets for them. On future execbuf2 calls, those
1259 * buffers will have offsets and we will be able to skip relocating.
1260 * Invalid offsets are indicated by anv_bo::offset == (uint64_t)-1.
1262 for (uint32_t i
= 0; i
< exec
->bo_count
; i
++) {
1263 if (exec
->bos
[i
]->offset
== (uint64_t)-1)
1267 /* Since surface states are shared between command buffers and we don't
1268 * know what order they will be submitted to the kernel, we don't know
1269 * what address is actually written in the surface state object at any
1270 * given time. The only option is to always relocate them.
1272 anv_reloc_list_apply(cmd_buffer
->device
, &cmd_buffer
->surface_relocs
,
1273 &cmd_buffer
->device
->surface_state_pool
.block_pool
.bo
,
1274 true /* always relocate surface states */);
1276 /* Since we own all of the batch buffers, we know what values are stored
1277 * in the relocated addresses and only have to update them if the offsets
1280 struct anv_batch_bo
**bbo
;
1281 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1282 anv_reloc_list_apply(cmd_buffer
->device
,
1283 &(*bbo
)->relocs
, &(*bbo
)->bo
, false);
1286 for (uint32_t i
= 0; i
< exec
->bo_count
; i
++)
1287 exec
->objects
[i
].offset
= exec
->bos
[i
]->offset
;
1293 setup_execbuf_for_cmd_buffer(struct anv_execbuf
*execbuf
,
1294 struct anv_cmd_buffer
*cmd_buffer
)
1296 struct anv_batch
*batch
= &cmd_buffer
->batch
;
1297 struct anv_state_pool
*ss_pool
=
1298 &cmd_buffer
->device
->surface_state_pool
;
1300 adjust_relocations_from_state_pool(ss_pool
, &cmd_buffer
->surface_relocs
,
1301 cmd_buffer
->last_ss_pool_center
);
1302 VkResult result
= anv_execbuf_add_bo(execbuf
, &ss_pool
->block_pool
.bo
,
1303 &cmd_buffer
->surface_relocs
, 0,
1304 &cmd_buffer
->device
->alloc
);
1305 if (result
!= VK_SUCCESS
)
1308 /* First, we walk over all of the bos we've seen and add them and their
1309 * relocations to the validate list.
1311 struct anv_batch_bo
**bbo
;
1312 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1313 adjust_relocations_to_state_pool(ss_pool
, &(*bbo
)->bo
, &(*bbo
)->relocs
,
1314 cmd_buffer
->last_ss_pool_center
);
1316 result
= anv_execbuf_add_bo(execbuf
, &(*bbo
)->bo
, &(*bbo
)->relocs
, 0,
1317 &cmd_buffer
->device
->alloc
);
1318 if (result
!= VK_SUCCESS
)
1322 /* Now that we've adjusted all of the surface state relocations, we need to
1323 * record the surface state pool center so future executions of the command
1324 * buffer can adjust correctly.
1326 cmd_buffer
->last_ss_pool_center
= ss_pool
->block_pool
.center_bo_offset
;
1328 struct anv_batch_bo
*first_batch_bo
=
1329 list_first_entry(&cmd_buffer
->batch_bos
, struct anv_batch_bo
, link
);
1331 /* The kernel requires that the last entry in the validation list be the
1332 * batch buffer to execute. We can simply swap the element
1333 * corresponding to the first batch_bo in the chain with the last
1334 * element in the list.
1336 if (first_batch_bo
->bo
.index
!= execbuf
->bo_count
- 1) {
1337 uint32_t idx
= first_batch_bo
->bo
.index
;
1338 uint32_t last_idx
= execbuf
->bo_count
- 1;
1340 struct drm_i915_gem_exec_object2 tmp_obj
= execbuf
->objects
[idx
];
1341 assert(execbuf
->bos
[idx
] == &first_batch_bo
->bo
);
1343 execbuf
->objects
[idx
] = execbuf
->objects
[last_idx
];
1344 execbuf
->bos
[idx
] = execbuf
->bos
[last_idx
];
1345 execbuf
->bos
[idx
]->index
= idx
;
1347 execbuf
->objects
[last_idx
] = tmp_obj
;
1348 execbuf
->bos
[last_idx
] = &first_batch_bo
->bo
;
1349 first_batch_bo
->bo
.index
= last_idx
;
1352 /* Now we go through and fixup all of the relocation lists to point to
1353 * the correct indices in the object array. We have to do this after we
1354 * reorder the list above as some of the indices may have changed.
1356 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
)
1357 anv_cmd_buffer_process_relocs(cmd_buffer
, &(*bbo
)->relocs
);
1359 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
1361 if (!cmd_buffer
->device
->info
.has_llc
) {
1362 __builtin_ia32_mfence();
1363 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1364 for (uint32_t i
= 0; i
< (*bbo
)->length
; i
+= CACHELINE_SIZE
)
1365 __builtin_ia32_clflush((*bbo
)->bo
.map
+ i
);
1369 execbuf
->execbuf
= (struct drm_i915_gem_execbuffer2
) {
1370 .buffers_ptr
= (uintptr_t) execbuf
->objects
,
1371 .buffer_count
= execbuf
->bo_count
,
1372 .batch_start_offset
= 0,
1373 .batch_len
= batch
->next
- batch
->start
,
1378 .flags
= I915_EXEC_HANDLE_LUT
| I915_EXEC_RENDER
|
1379 I915_EXEC_CONSTANTS_REL_GENERAL
,
1380 .rsvd1
= cmd_buffer
->device
->context_id
,
1384 if (relocate_cmd_buffer(cmd_buffer
, execbuf
)) {
1385 /* If we were able to successfully relocate everything, tell the kernel
1386 * that it can skip doing relocations. The requirement for using
1389 * 1) The addresses written in the objects must match the corresponding
1390 * reloc.presumed_offset which in turn must match the corresponding
1391 * execobject.offset.
1393 * 2) To avoid stalling, execobject.offset should match the current
1394 * address of that object within the active context.
1396 * In order to satisfy all of the invariants that make userspace
1397 * relocations to be safe (see relocate_cmd_buffer()), we need to
1398 * further ensure that the addresses we use match those used by the
1399 * kernel for the most recent execbuf2.
1401 * The kernel may still choose to do relocations anyway if something has
1402 * moved in the GTT. In this case, the relocation list still needs to be
1403 * valid. All relocations on the batch buffers are already valid and
1404 * kept up-to-date. For surface state relocations, by applying the
1405 * relocations in relocate_cmd_buffer, we ensured that the address in
1406 * the RENDER_SURFACE_STATE matches presumed_offset, so it should be
1407 * safe for the kernel to relocate them as needed.
1409 execbuf
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
1411 /* In the case where we fall back to doing kernel relocations, we need
1412 * to ensure that the relocation list is valid. All relocations on the
1413 * batch buffers are already valid and kept up-to-date. Since surface
1414 * states are shared between command buffers and we don't know what
1415 * order they will be submitted to the kernel, we don't know what
1416 * address is actually written in the surface state object at any given
1417 * time. The only option is to set a bogus presumed offset and let the
1418 * kernel relocate them.
1420 for (size_t i
= 0; i
< cmd_buffer
->surface_relocs
.num_relocs
; i
++)
1421 cmd_buffer
->surface_relocs
.relocs
[i
].presumed_offset
= -1;
1428 setup_empty_execbuf(struct anv_execbuf
*execbuf
, struct anv_device
*device
)
1430 VkResult result
= anv_execbuf_add_bo(execbuf
, &device
->trivial_batch_bo
,
1431 NULL
, 0, &device
->alloc
);
1432 if (result
!= VK_SUCCESS
)
1435 execbuf
->execbuf
= (struct drm_i915_gem_execbuffer2
) {
1436 .buffers_ptr
= (uintptr_t) execbuf
->objects
,
1437 .buffer_count
= execbuf
->bo_count
,
1438 .batch_start_offset
= 0,
1439 .batch_len
= 8, /* GEN7_MI_BATCH_BUFFER_END and NOOP */
1440 .flags
= I915_EXEC_HANDLE_LUT
| I915_EXEC_RENDER
,
1441 .rsvd1
= device
->context_id
,
1449 anv_cmd_buffer_execbuf(struct anv_device
*device
,
1450 struct anv_cmd_buffer
*cmd_buffer
,
1451 const VkSemaphore
*in_semaphores
,
1452 uint32_t num_in_semaphores
,
1453 const VkSemaphore
*out_semaphores
,
1454 uint32_t num_out_semaphores
,
1457 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1459 struct anv_execbuf execbuf
;
1460 anv_execbuf_init(&execbuf
);
1463 VkResult result
= VK_SUCCESS
;
1464 for (uint32_t i
= 0; i
< num_in_semaphores
; i
++) {
1465 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, in_semaphores
[i
]);
1466 struct anv_semaphore_impl
*impl
=
1467 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
1468 &semaphore
->temporary
: &semaphore
->permanent
;
1470 switch (impl
->type
) {
1471 case ANV_SEMAPHORE_TYPE_BO
:
1472 result
= anv_execbuf_add_bo(&execbuf
, impl
->bo
, NULL
,
1474 if (result
!= VK_SUCCESS
)
1478 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
1479 if (in_fence
== -1) {
1480 in_fence
= impl
->fd
;
1482 int merge
= anv_gem_sync_file_merge(device
, in_fence
, impl
->fd
);
1484 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR
);
1494 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
:
1495 result
= anv_execbuf_add_syncobj(&execbuf
, impl
->syncobj
,
1496 I915_EXEC_FENCE_WAIT
,
1498 if (result
!= VK_SUCCESS
)
1507 bool need_out_fence
= false;
1508 for (uint32_t i
= 0; i
< num_out_semaphores
; i
++) {
1509 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, out_semaphores
[i
]);
1511 /* Under most circumstances, out fences won't be temporary. However,
1512 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
1514 * "If the import is temporary, the implementation must restore the
1515 * semaphore to its prior permanent state after submitting the next
1516 * semaphore wait operation."
1518 * The spec says nothing whatsoever about signal operations on
1519 * temporarily imported semaphores so it appears they are allowed.
1520 * There are also CTS tests that require this to work.
1522 struct anv_semaphore_impl
*impl
=
1523 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
1524 &semaphore
->temporary
: &semaphore
->permanent
;
1526 switch (impl
->type
) {
1527 case ANV_SEMAPHORE_TYPE_BO
:
1528 result
= anv_execbuf_add_bo(&execbuf
, impl
->bo
, NULL
,
1529 EXEC_OBJECT_WRITE
, &device
->alloc
);
1530 if (result
!= VK_SUCCESS
)
1534 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
1535 need_out_fence
= true;
1538 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
:
1539 result
= anv_execbuf_add_syncobj(&execbuf
, impl
->syncobj
,
1540 I915_EXEC_FENCE_SIGNAL
,
1542 if (result
!= VK_SUCCESS
)
1552 assert(fence
->temporary
.type
== ANV_FENCE_TYPE_NONE
);
1553 struct anv_fence_impl
*impl
= &fence
->permanent
;
1555 switch (impl
->type
) {
1556 case ANV_FENCE_TYPE_BO
:
1557 result
= anv_execbuf_add_bo(&execbuf
, &impl
->bo
.bo
, NULL
,
1558 EXEC_OBJECT_WRITE
, &device
->alloc
);
1559 if (result
!= VK_SUCCESS
)
1564 unreachable("Invalid fence type");
1569 result
= setup_execbuf_for_cmd_buffer(&execbuf
, cmd_buffer
);
1571 result
= setup_empty_execbuf(&execbuf
, device
);
1573 if (result
!= VK_SUCCESS
)
1576 if (execbuf
.fence_count
> 0) {
1577 assert(device
->instance
->physicalDevice
.has_syncobj
);
1578 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_ARRAY
;
1579 execbuf
.execbuf
.num_cliprects
= execbuf
.fence_count
;
1580 execbuf
.execbuf
.cliprects_ptr
= (uintptr_t) execbuf
.fences
;
1583 if (in_fence
!= -1) {
1584 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_IN
;
1585 execbuf
.execbuf
.rsvd2
|= (uint32_t)in_fence
;
1589 execbuf
.execbuf
.flags
|= I915_EXEC_FENCE_OUT
;
1591 result
= anv_device_execbuf(device
, &execbuf
.execbuf
, execbuf
.bos
);
1593 /* Execbuf does not consume the in_fence. It's our job to close it. */
1597 for (uint32_t i
= 0; i
< num_in_semaphores
; i
++) {
1598 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, in_semaphores
[i
]);
1599 /* From the Vulkan 1.0.53 spec:
1601 * "If the import is temporary, the implementation must restore the
1602 * semaphore to its prior permanent state after submitting the next
1603 * semaphore wait operation."
1605 * This has to happen after the execbuf in case we close any syncobjs in
1608 anv_semaphore_reset_temporary(device
, semaphore
);
1611 if (fence
&& fence
->permanent
.type
== ANV_FENCE_TYPE_BO
) {
1612 /* Once the execbuf has returned, we need to set the fence state to
1613 * SUBMITTED. We can't do this before calling execbuf because
1614 * anv_GetFenceStatus does take the global device lock before checking
1617 * We set the fence state to SUBMITTED regardless of whether or not the
1618 * execbuf succeeds because we need to ensure that vkWaitForFences() and
1619 * vkGetFenceStatus() return a valid result (VK_ERROR_DEVICE_LOST or
1620 * VK_SUCCESS) in a finite amount of time even if execbuf fails.
1622 fence
->permanent
.bo
.state
= ANV_BO_FENCE_STATE_SUBMITTED
;
1625 if (result
== VK_SUCCESS
&& need_out_fence
) {
1626 int out_fence
= execbuf
.execbuf
.rsvd2
>> 32;
1627 for (uint32_t i
= 0; i
< num_out_semaphores
; i
++) {
1628 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, out_semaphores
[i
]);
1629 /* Out fences can't have temporary state because that would imply
1630 * that we imported a sync file and are trying to signal it.
1632 assert(semaphore
->temporary
.type
== ANV_SEMAPHORE_TYPE_NONE
);
1633 struct anv_semaphore_impl
*impl
= &semaphore
->permanent
;
1635 if (impl
->type
== ANV_SEMAPHORE_TYPE_SYNC_FILE
) {
1636 assert(impl
->fd
== -1);
1637 impl
->fd
= dup(out_fence
);
1643 anv_execbuf_finish(&execbuf
, &device
->alloc
);