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 const uint32_t domain
=
152 (target_bo
->flags
& EXEC_OBJECT_WRITE
) ? I915_GEM_DOMAIN_RENDER
: 0;
154 VkResult result
= anv_reloc_list_grow(list
, alloc
, 1);
155 if (result
!= VK_SUCCESS
)
158 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
159 index
= list
->num_relocs
++;
160 list
->reloc_bos
[index
] = target_bo
;
161 entry
= &list
->relocs
[index
];
162 entry
->target_handle
= target_bo
->gem_handle
;
163 entry
->delta
= delta
;
164 entry
->offset
= offset
;
165 entry
->presumed_offset
= target_bo
->offset
;
166 entry
->read_domains
= domain
;
167 entry
->write_domain
= domain
;
168 VG(VALGRIND_CHECK_MEM_IS_DEFINED(entry
, sizeof(*entry
)));
174 anv_reloc_list_append(struct anv_reloc_list
*list
,
175 const VkAllocationCallbacks
*alloc
,
176 struct anv_reloc_list
*other
, uint32_t offset
)
178 VkResult result
= anv_reloc_list_grow(list
, alloc
, other
->num_relocs
);
179 if (result
!= VK_SUCCESS
)
182 memcpy(&list
->relocs
[list
->num_relocs
], &other
->relocs
[0],
183 other
->num_relocs
* sizeof(other
->relocs
[0]));
184 memcpy(&list
->reloc_bos
[list
->num_relocs
], &other
->reloc_bos
[0],
185 other
->num_relocs
* sizeof(other
->reloc_bos
[0]));
187 for (uint32_t i
= 0; i
< other
->num_relocs
; i
++)
188 list
->relocs
[i
+ list
->num_relocs
].offset
+= offset
;
190 list
->num_relocs
+= other
->num_relocs
;
194 /*-----------------------------------------------------------------------*
195 * Functions related to anv_batch
196 *-----------------------------------------------------------------------*/
199 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
201 if (batch
->next
+ num_dwords
* 4 > batch
->end
) {
202 VkResult result
= batch
->extend_cb(batch
, batch
->user_data
);
203 if (result
!= VK_SUCCESS
) {
204 anv_batch_set_error(batch
, result
);
209 void *p
= batch
->next
;
211 batch
->next
+= num_dwords
* 4;
212 assert(batch
->next
<= batch
->end
);
218 anv_batch_emit_reloc(struct anv_batch
*batch
,
219 void *location
, struct anv_bo
*bo
, uint32_t delta
)
221 VkResult result
= anv_reloc_list_add(batch
->relocs
, batch
->alloc
,
222 location
- batch
->start
, bo
, delta
);
223 if (result
!= VK_SUCCESS
) {
224 anv_batch_set_error(batch
, result
);
228 return bo
->offset
+ delta
;
232 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
234 uint32_t size
, offset
;
236 size
= other
->next
- other
->start
;
237 assert(size
% 4 == 0);
239 if (batch
->next
+ size
> batch
->end
) {
240 VkResult result
= batch
->extend_cb(batch
, batch
->user_data
);
241 if (result
!= VK_SUCCESS
) {
242 anv_batch_set_error(batch
, result
);
247 assert(batch
->next
+ size
<= batch
->end
);
249 VG(VALGRIND_CHECK_MEM_IS_DEFINED(other
->start
, size
));
250 memcpy(batch
->next
, other
->start
, size
);
252 offset
= batch
->next
- batch
->start
;
253 VkResult result
= anv_reloc_list_append(batch
->relocs
, batch
->alloc
,
254 other
->relocs
, offset
);
255 if (result
!= VK_SUCCESS
) {
256 anv_batch_set_error(batch
, result
);
263 /*-----------------------------------------------------------------------*
264 * Functions related to anv_batch_bo
265 *-----------------------------------------------------------------------*/
268 anv_batch_bo_create(struct anv_cmd_buffer
*cmd_buffer
,
269 struct anv_batch_bo
**bbo_out
)
273 struct anv_batch_bo
*bbo
= vk_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
274 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
276 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
278 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
,
279 ANV_CMD_BUFFER_BATCH_SIZE
);
280 if (result
!= VK_SUCCESS
)
283 result
= anv_reloc_list_init(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
284 if (result
!= VK_SUCCESS
)
292 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
294 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
300 anv_batch_bo_clone(struct anv_cmd_buffer
*cmd_buffer
,
301 const struct anv_batch_bo
*other_bbo
,
302 struct anv_batch_bo
**bbo_out
)
306 struct anv_batch_bo
*bbo
= vk_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
307 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
309 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
311 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
,
313 if (result
!= VK_SUCCESS
)
316 result
= anv_reloc_list_init_clone(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
,
318 if (result
!= VK_SUCCESS
)
321 bbo
->length
= other_bbo
->length
;
322 memcpy(bbo
->bo
.map
, other_bbo
->bo
.map
, other_bbo
->length
);
329 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
331 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
337 anv_batch_bo_start(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
338 size_t batch_padding
)
340 batch
->next
= batch
->start
= bbo
->bo
.map
;
341 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
342 batch
->relocs
= &bbo
->relocs
;
343 bbo
->relocs
.num_relocs
= 0;
347 anv_batch_bo_continue(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
348 size_t batch_padding
)
350 batch
->start
= bbo
->bo
.map
;
351 batch
->next
= bbo
->bo
.map
+ bbo
->length
;
352 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
353 batch
->relocs
= &bbo
->relocs
;
357 anv_batch_bo_finish(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
)
359 assert(batch
->start
== bbo
->bo
.map
);
360 bbo
->length
= batch
->next
- batch
->start
;
361 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch
->start
, bbo
->length
));
365 anv_batch_bo_grow(struct anv_cmd_buffer
*cmd_buffer
, struct anv_batch_bo
*bbo
,
366 struct anv_batch
*batch
, size_t aditional
,
367 size_t batch_padding
)
369 assert(batch
->start
== bbo
->bo
.map
);
370 bbo
->length
= batch
->next
- batch
->start
;
372 size_t new_size
= bbo
->bo
.size
;
373 while (new_size
<= bbo
->length
+ aditional
+ batch_padding
)
376 if (new_size
== bbo
->bo
.size
)
379 struct anv_bo new_bo
;
380 VkResult result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
,
382 if (result
!= VK_SUCCESS
)
385 memcpy(new_bo
.map
, bbo
->bo
.map
, bbo
->length
);
387 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
390 anv_batch_bo_continue(bbo
, batch
, batch_padding
);
396 anv_batch_bo_destroy(struct anv_batch_bo
*bbo
,
397 struct anv_cmd_buffer
*cmd_buffer
)
399 anv_reloc_list_finish(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
400 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
401 vk_free(&cmd_buffer
->pool
->alloc
, bbo
);
405 anv_batch_bo_list_clone(const struct list_head
*list
,
406 struct anv_cmd_buffer
*cmd_buffer
,
407 struct list_head
*new_list
)
409 VkResult result
= VK_SUCCESS
;
411 list_inithead(new_list
);
413 struct anv_batch_bo
*prev_bbo
= NULL
;
414 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
415 struct anv_batch_bo
*new_bbo
= NULL
;
416 result
= anv_batch_bo_clone(cmd_buffer
, bbo
, &new_bbo
);
417 if (result
!= VK_SUCCESS
)
419 list_addtail(&new_bbo
->link
, new_list
);
422 /* As we clone this list of batch_bo's, they chain one to the
423 * other using MI_BATCH_BUFFER_START commands. We need to fix up
424 * those relocations as we go. Fortunately, this is pretty easy
425 * as it will always be the last relocation in the list.
427 uint32_t last_idx
= prev_bbo
->relocs
.num_relocs
- 1;
428 assert(prev_bbo
->relocs
.reloc_bos
[last_idx
] == &bbo
->bo
);
429 prev_bbo
->relocs
.reloc_bos
[last_idx
] = &new_bbo
->bo
;
435 if (result
!= VK_SUCCESS
) {
436 list_for_each_entry_safe(struct anv_batch_bo
, bbo
, new_list
, link
)
437 anv_batch_bo_destroy(bbo
, cmd_buffer
);
443 /*-----------------------------------------------------------------------*
444 * Functions related to anv_batch_bo
445 *-----------------------------------------------------------------------*/
447 static inline struct anv_batch_bo
*
448 anv_cmd_buffer_current_batch_bo(struct anv_cmd_buffer
*cmd_buffer
)
450 return LIST_ENTRY(struct anv_batch_bo
, cmd_buffer
->batch_bos
.prev
, link
);
454 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer
*cmd_buffer
)
456 struct anv_state
*bt_block
= u_vector_head(&cmd_buffer
->bt_block_states
);
457 return (struct anv_address
) {
458 .bo
= &cmd_buffer
->device
->surface_state_pool
.block_pool
.bo
,
459 .offset
= bt_block
->offset
,
464 emit_batch_buffer_start(struct anv_cmd_buffer
*cmd_buffer
,
465 struct anv_bo
*bo
, uint32_t offset
)
467 /* In gen8+ the address field grew to two dwords to accomodate 48 bit
468 * offsets. The high 16 bits are in the last dword, so we can use the gen8
469 * version in either case, as long as we set the instruction length in the
470 * header accordingly. This means that we always emit three dwords here
471 * and all the padding and adjustment we do in this file works for all
475 #define GEN7_MI_BATCH_BUFFER_START_length 2
476 #define GEN7_MI_BATCH_BUFFER_START_length_bias 2
478 const uint32_t gen7_length
=
479 GEN7_MI_BATCH_BUFFER_START_length
- GEN7_MI_BATCH_BUFFER_START_length_bias
;
480 const uint32_t gen8_length
=
481 GEN8_MI_BATCH_BUFFER_START_length
- GEN8_MI_BATCH_BUFFER_START_length_bias
;
483 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_BATCH_BUFFER_START
, bbs
) {
484 bbs
.DWordLength
= cmd_buffer
->device
->info
.gen
< 8 ?
485 gen7_length
: gen8_length
;
486 bbs
._2ndLevelBatchBuffer
= _1stlevelbatch
;
487 bbs
.AddressSpaceIndicator
= ASI_PPGTT
;
488 bbs
.BatchBufferStartAddress
= (struct anv_address
) { bo
, offset
};
493 cmd_buffer_chain_to_batch_bo(struct anv_cmd_buffer
*cmd_buffer
,
494 struct anv_batch_bo
*bbo
)
496 struct anv_batch
*batch
= &cmd_buffer
->batch
;
497 struct anv_batch_bo
*current_bbo
=
498 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
500 /* We set the end of the batch a little short so we would be sure we
501 * have room for the chaining command. Since we're about to emit the
502 * chaining command, let's set it back where it should go.
504 batch
->end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
505 assert(batch
->end
== current_bbo
->bo
.map
+ current_bbo
->bo
.size
);
507 emit_batch_buffer_start(cmd_buffer
, &bbo
->bo
, 0);
509 anv_batch_bo_finish(current_bbo
, batch
);
513 anv_cmd_buffer_chain_batch(struct anv_batch
*batch
, void *_data
)
515 struct anv_cmd_buffer
*cmd_buffer
= _data
;
516 struct anv_batch_bo
*new_bbo
;
518 VkResult result
= anv_batch_bo_create(cmd_buffer
, &new_bbo
);
519 if (result
!= VK_SUCCESS
)
522 struct anv_batch_bo
**seen_bbo
= u_vector_add(&cmd_buffer
->seen_bbos
);
523 if (seen_bbo
== NULL
) {
524 anv_batch_bo_destroy(new_bbo
, cmd_buffer
);
525 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
529 cmd_buffer_chain_to_batch_bo(cmd_buffer
, new_bbo
);
531 list_addtail(&new_bbo
->link
, &cmd_buffer
->batch_bos
);
533 anv_batch_bo_start(new_bbo
, batch
, GEN8_MI_BATCH_BUFFER_START_length
* 4);
539 anv_cmd_buffer_grow_batch(struct anv_batch
*batch
, void *_data
)
541 struct anv_cmd_buffer
*cmd_buffer
= _data
;
542 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
544 anv_batch_bo_grow(cmd_buffer
, bbo
, &cmd_buffer
->batch
, 4096,
545 GEN8_MI_BATCH_BUFFER_START_length
* 4);
550 /** Allocate a binding table
552 * This function allocates a binding table. This is a bit more complicated
553 * than one would think due to a combination of Vulkan driver design and some
554 * unfortunate hardware restrictions.
556 * The 3DSTATE_BINDING_TABLE_POINTERS_* packets only have a 16-bit field for
557 * the binding table pointer which means that all binding tables need to live
558 * in the bottom 64k of surface state base address. The way the GL driver has
559 * classically dealt with this restriction is to emit all surface states
560 * on-the-fly into the batch and have a batch buffer smaller than 64k. This
561 * isn't really an option in Vulkan for a couple of reasons:
563 * 1) In Vulkan, we have growing (or chaining) batches so surface states have
564 * to live in their own buffer and we have to be able to re-emit
565 * STATE_BASE_ADDRESS as needed which requires a full pipeline stall. In
566 * order to avoid emitting STATE_BASE_ADDRESS any more often than needed
567 * (it's not that hard to hit 64k of just binding tables), we allocate
568 * surface state objects up-front when VkImageView is created. In order
569 * for this to work, surface state objects need to be allocated from a
572 * 2) We tried to design the surface state system in such a way that it's
573 * already ready for bindless texturing. The way bindless texturing works
574 * on our hardware is that you have a big pool of surface state objects
575 * (with its own state base address) and the bindless handles are simply
576 * offsets into that pool. With the architecture we chose, we already
577 * have that pool and it's exactly the same pool that we use for regular
578 * surface states so we should already be ready for bindless.
580 * 3) For render targets, we need to be able to fill out the surface states
581 * later in vkBeginRenderPass so that we can assign clear colors
582 * correctly. One way to do this would be to just create the surface
583 * state data and then repeatedly copy it into the surface state BO every
584 * time we have to re-emit STATE_BASE_ADDRESS. While this works, it's
585 * rather annoying and just being able to allocate them up-front and
586 * re-use them for the entire render pass.
588 * While none of these are technically blockers for emitting state on the fly
589 * like we do in GL, the ability to have a single surface state pool is
590 * simplifies things greatly. Unfortunately, it comes at a cost...
592 * Because of the 64k limitation of 3DSTATE_BINDING_TABLE_POINTERS_*, we can't
593 * place the binding tables just anywhere in surface state base address.
594 * Because 64k isn't a whole lot of space, we can't simply restrict the
595 * surface state buffer to 64k, we have to be more clever. The solution we've
596 * chosen is to have a block pool with a maximum size of 2G that starts at
597 * zero and grows in both directions. All surface states are allocated from
598 * the top of the pool (positive offsets) and we allocate blocks (< 64k) of
599 * binding tables from the bottom of the pool (negative offsets). Every time
600 * we allocate a new binding table block, we set surface state base address to
601 * point to the bottom of the binding table block. This way all of the
602 * binding tables in the block are in the bottom 64k of surface state base
603 * address. When we fill out the binding table, we add the distance between
604 * the bottom of our binding table block and zero of the block pool to the
605 * surface state offsets so that they are correct relative to out new surface
606 * state base address at the bottom of the binding table block.
608 * \see adjust_relocations_from_block_pool()
609 * \see adjust_relocations_too_block_pool()
611 * \param[in] entries The number of surface state entries the binding
612 * table should be able to hold.
614 * \param[out] state_offset The offset surface surface state base address
615 * where the surface states live. This must be
616 * added to the surface state offset when it is
617 * written into the binding table entry.
619 * \return An anv_state representing the binding table
622 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
623 uint32_t entries
, uint32_t *state_offset
)
625 struct anv_state_pool
*state_pool
= &cmd_buffer
->device
->surface_state_pool
;
626 struct anv_state
*bt_block
= u_vector_head(&cmd_buffer
->bt_block_states
);
627 struct anv_state state
;
629 state
.alloc_size
= align_u32(entries
* 4, 32);
631 if (cmd_buffer
->bt_next
+ state
.alloc_size
> state_pool
->block_size
)
632 return (struct anv_state
) { 0 };
634 state
.offset
= cmd_buffer
->bt_next
;
635 state
.map
= state_pool
->block_pool
.map
+ bt_block
->offset
+ state
.offset
;
637 cmd_buffer
->bt_next
+= state
.alloc_size
;
639 assert(bt_block
->offset
< 0);
640 *state_offset
= -bt_block
->offset
;
646 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
)
648 struct isl_device
*isl_dev
= &cmd_buffer
->device
->isl_dev
;
649 return anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
,
650 isl_dev
->ss
.size
, isl_dev
->ss
.align
);
654 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer
*cmd_buffer
,
655 uint32_t size
, uint32_t alignment
)
657 return anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
662 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer
*cmd_buffer
)
664 struct anv_state_pool
*state_pool
= &cmd_buffer
->device
->surface_state_pool
;
666 struct anv_state
*bt_block
= u_vector_add(&cmd_buffer
->bt_block_states
);
667 if (bt_block
== NULL
) {
668 anv_batch_set_error(&cmd_buffer
->batch
, VK_ERROR_OUT_OF_HOST_MEMORY
);
669 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
672 *bt_block
= anv_state_pool_alloc_back(state_pool
);
673 cmd_buffer
->bt_next
= 0;
679 anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
681 struct anv_batch_bo
*batch_bo
;
684 list_inithead(&cmd_buffer
->batch_bos
);
686 result
= anv_batch_bo_create(cmd_buffer
, &batch_bo
);
687 if (result
!= VK_SUCCESS
)
690 list_addtail(&batch_bo
->link
, &cmd_buffer
->batch_bos
);
692 cmd_buffer
->batch
.alloc
= &cmd_buffer
->pool
->alloc
;
693 cmd_buffer
->batch
.user_data
= cmd_buffer
;
695 if (cmd_buffer
->device
->can_chain_batches
) {
696 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_chain_batch
;
698 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_grow_batch
;
701 anv_batch_bo_start(batch_bo
, &cmd_buffer
->batch
,
702 GEN8_MI_BATCH_BUFFER_START_length
* 4);
704 int success
= u_vector_init(&cmd_buffer
->seen_bbos
,
705 sizeof(struct anv_bo
*),
706 8 * sizeof(struct anv_bo
*));
710 *(struct anv_batch_bo
**)u_vector_add(&cmd_buffer
->seen_bbos
) = batch_bo
;
712 success
= u_vector_init(&cmd_buffer
->bt_block_states
,
713 sizeof(struct anv_state
),
714 8 * sizeof(struct anv_state
));
718 result
= anv_reloc_list_init(&cmd_buffer
->surface_relocs
,
719 &cmd_buffer
->pool
->alloc
);
720 if (result
!= VK_SUCCESS
)
722 cmd_buffer
->last_ss_pool_center
= 0;
724 result
= anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
725 if (result
!= VK_SUCCESS
)
731 u_vector_finish(&cmd_buffer
->bt_block_states
);
733 u_vector_finish(&cmd_buffer
->seen_bbos
);
735 anv_batch_bo_destroy(batch_bo
, cmd_buffer
);
741 anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
743 struct anv_state
*bt_block
;
744 u_vector_foreach(bt_block
, &cmd_buffer
->bt_block_states
)
745 anv_state_pool_free(&cmd_buffer
->device
->surface_state_pool
, *bt_block
);
746 u_vector_finish(&cmd_buffer
->bt_block_states
);
748 anv_reloc_list_finish(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
);
750 u_vector_finish(&cmd_buffer
->seen_bbos
);
752 /* Destroy all of the batch buffers */
753 list_for_each_entry_safe(struct anv_batch_bo
, bbo
,
754 &cmd_buffer
->batch_bos
, link
) {
755 anv_batch_bo_destroy(bbo
, cmd_buffer
);
760 anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
762 /* Delete all but the first batch bo */
763 assert(!list_empty(&cmd_buffer
->batch_bos
));
764 while (cmd_buffer
->batch_bos
.next
!= cmd_buffer
->batch_bos
.prev
) {
765 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
766 list_del(&bbo
->link
);
767 anv_batch_bo_destroy(bbo
, cmd_buffer
);
769 assert(!list_empty(&cmd_buffer
->batch_bos
));
771 anv_batch_bo_start(anv_cmd_buffer_current_batch_bo(cmd_buffer
),
773 GEN8_MI_BATCH_BUFFER_START_length
* 4);
775 while (u_vector_length(&cmd_buffer
->bt_block_states
) > 1) {
776 struct anv_state
*bt_block
= u_vector_remove(&cmd_buffer
->bt_block_states
);
777 anv_state_pool_free(&cmd_buffer
->device
->surface_state_pool
, *bt_block
);
779 assert(u_vector_length(&cmd_buffer
->bt_block_states
) == 1);
780 cmd_buffer
->bt_next
= 0;
782 cmd_buffer
->surface_relocs
.num_relocs
= 0;
783 cmd_buffer
->last_ss_pool_center
= 0;
785 /* Reset the list of seen buffers */
786 cmd_buffer
->seen_bbos
.head
= 0;
787 cmd_buffer
->seen_bbos
.tail
= 0;
789 *(struct anv_batch_bo
**)u_vector_add(&cmd_buffer
->seen_bbos
) =
790 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
794 anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer
*cmd_buffer
)
796 struct anv_batch_bo
*batch_bo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
798 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
) {
799 /* When we start a batch buffer, we subtract a certain amount of
800 * padding from the end to ensure that we always have room to emit a
801 * BATCH_BUFFER_START to chain to the next BO. We need to remove
802 * that padding before we end the batch; otherwise, we may end up
803 * with our BATCH_BUFFER_END in another BO.
805 cmd_buffer
->batch
.end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
806 assert(cmd_buffer
->batch
.end
== batch_bo
->bo
.map
+ batch_bo
->bo
.size
);
808 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_BATCH_BUFFER_END
, bbe
);
810 /* Round batch up to an even number of dwords. */
811 if ((cmd_buffer
->batch
.next
- cmd_buffer
->batch
.start
) & 4)
812 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_NOOP
, noop
);
814 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_PRIMARY
;
817 anv_batch_bo_finish(batch_bo
, &cmd_buffer
->batch
);
819 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
) {
820 /* If this is a secondary command buffer, we need to determine the
821 * mode in which it will be executed with vkExecuteCommands. We
822 * determine this statically here so that this stays in sync with the
823 * actual ExecuteCommands implementation.
825 if (!cmd_buffer
->device
->can_chain_batches
) {
826 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
;
827 } else if ((cmd_buffer
->batch_bos
.next
== cmd_buffer
->batch_bos
.prev
) &&
828 (batch_bo
->length
< ANV_CMD_BUFFER_BATCH_SIZE
/ 2)) {
829 /* If the secondary has exactly one batch buffer in its list *and*
830 * that batch buffer is less than half of the maximum size, we're
831 * probably better of simply copying it into our batch.
833 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_EMIT
;
834 } else if (!(cmd_buffer
->usage_flags
&
835 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT
)) {
836 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_CHAIN
;
838 /* When we chain, we need to add an MI_BATCH_BUFFER_START command
839 * with its relocation. In order to handle this we'll increment here
840 * so we can unconditionally decrement right before adding the
841 * MI_BATCH_BUFFER_START command.
843 batch_bo
->relocs
.num_relocs
++;
844 cmd_buffer
->batch
.next
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
846 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
;
851 static inline VkResult
852 anv_cmd_buffer_add_seen_bbos(struct anv_cmd_buffer
*cmd_buffer
,
853 struct list_head
*list
)
855 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
856 struct anv_batch_bo
**bbo_ptr
= u_vector_add(&cmd_buffer
->seen_bbos
);
858 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
867 anv_cmd_buffer_add_secondary(struct anv_cmd_buffer
*primary
,
868 struct anv_cmd_buffer
*secondary
)
870 switch (secondary
->exec_mode
) {
871 case ANV_CMD_BUFFER_EXEC_MODE_EMIT
:
872 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
874 case ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
: {
875 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(primary
);
876 unsigned length
= secondary
->batch
.end
- secondary
->batch
.start
;
877 anv_batch_bo_grow(primary
, bbo
, &primary
->batch
, length
,
878 GEN8_MI_BATCH_BUFFER_START_length
* 4);
879 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
882 case ANV_CMD_BUFFER_EXEC_MODE_CHAIN
: {
883 struct anv_batch_bo
*first_bbo
=
884 list_first_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
885 struct anv_batch_bo
*last_bbo
=
886 list_last_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
888 emit_batch_buffer_start(primary
, &first_bbo
->bo
, 0);
890 struct anv_batch_bo
*this_bbo
= anv_cmd_buffer_current_batch_bo(primary
);
891 assert(primary
->batch
.start
== this_bbo
->bo
.map
);
892 uint32_t offset
= primary
->batch
.next
- primary
->batch
.start
;
893 const uint32_t inst_size
= GEN8_MI_BATCH_BUFFER_START_length
* 4;
895 /* Roll back the previous MI_BATCH_BUFFER_START and its relocation so we
896 * can emit a new command and relocation for the current splice. In
897 * order to handle the initial-use case, we incremented next and
898 * num_relocs in end_batch_buffer() so we can alyways just subtract
901 last_bbo
->relocs
.num_relocs
--;
902 secondary
->batch
.next
-= inst_size
;
903 emit_batch_buffer_start(secondary
, &this_bbo
->bo
, offset
);
904 anv_cmd_buffer_add_seen_bbos(primary
, &secondary
->batch_bos
);
906 /* After patching up the secondary buffer, we need to clflush the
907 * modified instruction in case we're on a !llc platform. We use a
908 * little loop to handle the case where the instruction crosses a cache
911 if (!primary
->device
->info
.has_llc
) {
912 void *inst
= secondary
->batch
.next
- inst_size
;
913 void *p
= (void *) (((uintptr_t) inst
) & ~CACHELINE_MASK
);
914 __builtin_ia32_mfence();
915 while (p
< secondary
->batch
.next
) {
916 __builtin_ia32_clflush(p
);
922 case ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
: {
923 struct list_head copy_list
;
924 VkResult result
= anv_batch_bo_list_clone(&secondary
->batch_bos
,
927 if (result
!= VK_SUCCESS
)
930 anv_cmd_buffer_add_seen_bbos(primary
, ©_list
);
932 struct anv_batch_bo
*first_bbo
=
933 list_first_entry(©_list
, struct anv_batch_bo
, link
);
934 struct anv_batch_bo
*last_bbo
=
935 list_last_entry(©_list
, struct anv_batch_bo
, link
);
937 cmd_buffer_chain_to_batch_bo(primary
, first_bbo
);
939 list_splicetail(©_list
, &primary
->batch_bos
);
941 anv_batch_bo_continue(last_bbo
, &primary
->batch
,
942 GEN8_MI_BATCH_BUFFER_START_length
* 4);
946 assert(!"Invalid execution mode");
949 anv_reloc_list_append(&primary
->surface_relocs
, &primary
->pool
->alloc
,
950 &secondary
->surface_relocs
, 0);
954 struct drm_i915_gem_execbuffer2 execbuf
;
956 struct drm_i915_gem_exec_object2
* objects
;
958 struct anv_bo
** bos
;
960 /* Allocated length of the 'objects' and 'bos' arrays */
961 uint32_t array_length
;
965 anv_execbuf_init(struct anv_execbuf
*exec
)
967 memset(exec
, 0, sizeof(*exec
));
971 anv_execbuf_finish(struct anv_execbuf
*exec
,
972 const VkAllocationCallbacks
*alloc
)
974 vk_free(alloc
, exec
->objects
);
975 vk_free(alloc
, exec
->bos
);
979 anv_execbuf_add_bo(struct anv_execbuf
*exec
,
981 struct anv_reloc_list
*relocs
,
982 uint32_t extra_flags
,
983 const VkAllocationCallbacks
*alloc
)
985 struct drm_i915_gem_exec_object2
*obj
= NULL
;
987 if (bo
->index
< exec
->bo_count
&& exec
->bos
[bo
->index
] == bo
)
988 obj
= &exec
->objects
[bo
->index
];
991 /* We've never seen this one before. Add it to the list and assign
992 * an id that we can use later.
994 if (exec
->bo_count
>= exec
->array_length
) {
995 uint32_t new_len
= exec
->objects
? exec
->array_length
* 2 : 64;
997 struct drm_i915_gem_exec_object2
*new_objects
=
998 vk_alloc(alloc
, new_len
* sizeof(*new_objects
),
999 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1000 if (new_objects
== NULL
)
1001 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1003 struct anv_bo
**new_bos
=
1004 vk_alloc(alloc
, new_len
* sizeof(*new_bos
),
1005 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1006 if (new_bos
== NULL
) {
1007 vk_free(alloc
, new_objects
);
1008 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1011 if (exec
->objects
) {
1012 memcpy(new_objects
, exec
->objects
,
1013 exec
->bo_count
* sizeof(*new_objects
));
1014 memcpy(new_bos
, exec
->bos
,
1015 exec
->bo_count
* sizeof(*new_bos
));
1018 vk_free(alloc
, exec
->objects
);
1019 vk_free(alloc
, exec
->bos
);
1021 exec
->objects
= new_objects
;
1022 exec
->bos
= new_bos
;
1023 exec
->array_length
= new_len
;
1026 assert(exec
->bo_count
< exec
->array_length
);
1028 bo
->index
= exec
->bo_count
++;
1029 obj
= &exec
->objects
[bo
->index
];
1030 exec
->bos
[bo
->index
] = bo
;
1032 obj
->handle
= bo
->gem_handle
;
1033 obj
->relocation_count
= 0;
1034 obj
->relocs_ptr
= 0;
1036 obj
->offset
= bo
->offset
;
1037 obj
->flags
= bo
->flags
| extra_flags
;
1042 if (relocs
!= NULL
&& obj
->relocation_count
== 0) {
1043 /* This is the first time we've ever seen a list of relocations for
1044 * this BO. Go ahead and set the relocations and then walk the list
1045 * of relocations and add them all.
1047 obj
->relocation_count
= relocs
->num_relocs
;
1048 obj
->relocs_ptr
= (uintptr_t) relocs
->relocs
;
1050 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1053 /* A quick sanity check on relocations */
1054 assert(relocs
->relocs
[i
].offset
< bo
->size
);
1055 result
= anv_execbuf_add_bo(exec
, relocs
->reloc_bos
[i
], NULL
,
1056 extra_flags
, alloc
);
1058 if (result
!= VK_SUCCESS
)
1067 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
1068 struct anv_reloc_list
*list
)
1070 for (size_t i
= 0; i
< list
->num_relocs
; i
++)
1071 list
->relocs
[i
].target_handle
= list
->reloc_bos
[i
]->index
;
1075 write_reloc(const struct anv_device
*device
, void *p
, uint64_t v
, bool flush
)
1077 unsigned reloc_size
= 0;
1078 if (device
->info
.gen
>= 8) {
1079 /* From the Broadwell PRM Vol. 2a, MI_LOAD_REGISTER_MEM::MemoryAddress:
1081 * "This field specifies the address of the memory location where the
1082 * register value specified in the DWord above will read from. The
1083 * address specifies the DWord location of the data. Range =
1084 * GraphicsVirtualAddress[63:2] for a DWord register GraphicsAddress
1085 * [63:48] are ignored by the HW and assumed to be in correct
1086 * canonical form [63:48] == [47]."
1088 const int shift
= 63 - 47;
1089 reloc_size
= sizeof(uint64_t);
1090 *(uint64_t *)p
= (((int64_t)v
) << shift
) >> shift
;
1092 reloc_size
= sizeof(uint32_t);
1096 if (flush
&& !device
->info
.has_llc
)
1097 anv_flush_range(p
, reloc_size
);
1101 adjust_relocations_from_state_pool(struct anv_state_pool
*pool
,
1102 struct anv_reloc_list
*relocs
,
1103 uint32_t last_pool_center_bo_offset
)
1105 assert(last_pool_center_bo_offset
<= pool
->block_pool
.center_bo_offset
);
1106 uint32_t delta
= pool
->block_pool
.center_bo_offset
- last_pool_center_bo_offset
;
1108 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1109 /* All of the relocations from this block pool to other BO's should
1110 * have been emitted relative to the surface block pool center. We
1111 * need to add the center offset to make them relative to the
1112 * beginning of the actual GEM bo.
1114 relocs
->relocs
[i
].offset
+= delta
;
1119 adjust_relocations_to_state_pool(struct anv_state_pool
*pool
,
1120 struct anv_bo
*from_bo
,
1121 struct anv_reloc_list
*relocs
,
1122 uint32_t last_pool_center_bo_offset
)
1124 assert(last_pool_center_bo_offset
<= pool
->block_pool
.center_bo_offset
);
1125 uint32_t delta
= pool
->block_pool
.center_bo_offset
- last_pool_center_bo_offset
;
1127 /* When we initially emit relocations into a block pool, we don't
1128 * actually know what the final center_bo_offset will be so we just emit
1129 * it as if center_bo_offset == 0. Now that we know what the center
1130 * offset is, we need to walk the list of relocations and adjust any
1131 * relocations that point to the pool bo with the correct offset.
1133 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1134 if (relocs
->reloc_bos
[i
] == &pool
->block_pool
.bo
) {
1135 /* Adjust the delta value in the relocation to correctly
1136 * correspond to the new delta. Initially, this value may have
1137 * been negative (if treated as unsigned), but we trust in
1138 * uint32_t roll-over to fix that for us at this point.
1140 relocs
->relocs
[i
].delta
+= delta
;
1142 /* Since the delta has changed, we need to update the actual
1143 * relocated value with the new presumed value. This function
1144 * should only be called on batch buffers, so we know it isn't in
1145 * use by the GPU at the moment.
1147 assert(relocs
->relocs
[i
].offset
< from_bo
->size
);
1148 write_reloc(pool
->block_pool
.device
,
1149 from_bo
->map
+ relocs
->relocs
[i
].offset
,
1150 relocs
->relocs
[i
].presumed_offset
+
1151 relocs
->relocs
[i
].delta
, false);
1157 anv_reloc_list_apply(struct anv_device
*device
,
1158 struct anv_reloc_list
*list
,
1160 bool always_relocate
)
1162 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
1163 struct anv_bo
*target_bo
= list
->reloc_bos
[i
];
1164 if (list
->relocs
[i
].presumed_offset
== target_bo
->offset
&&
1168 void *p
= bo
->map
+ list
->relocs
[i
].offset
;
1169 write_reloc(device
, p
, target_bo
->offset
+ list
->relocs
[i
].delta
, true);
1170 list
->relocs
[i
].presumed_offset
= target_bo
->offset
;
1175 * This function applies the relocation for a command buffer and writes the
1176 * actual addresses into the buffers as per what we were told by the kernel on
1177 * the previous execbuf2 call. This should be safe to do because, for each
1178 * relocated address, we have two cases:
1180 * 1) The target BO is inactive (as seen by the kernel). In this case, it is
1181 * not in use by the GPU so updating the address is 100% ok. It won't be
1182 * in-use by the GPU (from our context) again until the next execbuf2
1183 * happens. If the kernel decides to move it in the next execbuf2, it
1184 * will have to do the relocations itself, but that's ok because it should
1185 * have all of the information needed to do so.
1187 * 2) The target BO is active (as seen by the kernel). In this case, it
1188 * hasn't moved since the last execbuffer2 call because GTT shuffling
1189 * *only* happens when the BO is idle. (From our perspective, it only
1190 * happens inside the execbuffer2 ioctl, but the shuffling may be
1191 * triggered by another ioctl, with full-ppgtt this is limited to only
1192 * execbuffer2 ioctls on the same context, or memory pressure.) Since the
1193 * target BO hasn't moved, our anv_bo::offset exactly matches the BO's GTT
1194 * address and the relocated value we are writing into the BO will be the
1195 * same as the value that is already there.
1197 * There is also a possibility that the target BO is active but the exact
1198 * RENDER_SURFACE_STATE object we are writing the relocation into isn't in
1199 * use. In this case, the address currently in the RENDER_SURFACE_STATE
1200 * may be stale but it's still safe to write the relocation because that
1201 * particular RENDER_SURFACE_STATE object isn't in-use by the GPU and
1202 * won't be until the next execbuf2 call.
1204 * By doing relocations on the CPU, we can tell the kernel that it doesn't
1205 * need to bother. We want to do this because the surface state buffer is
1206 * used by every command buffer so, if the kernel does the relocations, it
1207 * will always be busy and the kernel will always stall. This is also
1208 * probably the fastest mechanism for doing relocations since the kernel would
1209 * have to make a full copy of all the relocations lists.
1212 relocate_cmd_buffer(struct anv_cmd_buffer
*cmd_buffer
,
1213 struct anv_execbuf
*exec
)
1215 static int userspace_relocs
= -1;
1216 if (userspace_relocs
< 0)
1217 userspace_relocs
= env_var_as_boolean("ANV_USERSPACE_RELOCS", true);
1218 if (!userspace_relocs
)
1221 /* First, we have to check to see whether or not we can even do the
1222 * relocation. New buffers which have never been submitted to the kernel
1223 * don't have a valid offset so we need to let the kernel do relocations so
1224 * that we can get offsets for them. On future execbuf2 calls, those
1225 * buffers will have offsets and we will be able to skip relocating.
1226 * Invalid offsets are indicated by anv_bo::offset == (uint64_t)-1.
1228 for (uint32_t i
= 0; i
< exec
->bo_count
; i
++) {
1229 if (exec
->bos
[i
]->offset
== (uint64_t)-1)
1233 /* Since surface states are shared between command buffers and we don't
1234 * know what order they will be submitted to the kernel, we don't know
1235 * what address is actually written in the surface state object at any
1236 * given time. The only option is to always relocate them.
1238 anv_reloc_list_apply(cmd_buffer
->device
, &cmd_buffer
->surface_relocs
,
1239 &cmd_buffer
->device
->surface_state_pool
.block_pool
.bo
,
1240 true /* always relocate surface states */);
1242 /* Since we own all of the batch buffers, we know what values are stored
1243 * in the relocated addresses and only have to update them if the offsets
1246 struct anv_batch_bo
**bbo
;
1247 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1248 anv_reloc_list_apply(cmd_buffer
->device
,
1249 &(*bbo
)->relocs
, &(*bbo
)->bo
, false);
1252 for (uint32_t i
= 0; i
< exec
->bo_count
; i
++)
1253 exec
->objects
[i
].offset
= exec
->bos
[i
]->offset
;
1259 setup_execbuf_for_cmd_buffer(struct anv_execbuf
*execbuf
,
1260 struct anv_cmd_buffer
*cmd_buffer
)
1262 struct anv_batch
*batch
= &cmd_buffer
->batch
;
1263 struct anv_state_pool
*ss_pool
=
1264 &cmd_buffer
->device
->surface_state_pool
;
1266 adjust_relocations_from_state_pool(ss_pool
, &cmd_buffer
->surface_relocs
,
1267 cmd_buffer
->last_ss_pool_center
);
1268 VkResult result
= anv_execbuf_add_bo(execbuf
, &ss_pool
->block_pool
.bo
,
1269 &cmd_buffer
->surface_relocs
, 0,
1270 &cmd_buffer
->device
->alloc
);
1271 if (result
!= VK_SUCCESS
)
1274 /* First, we walk over all of the bos we've seen and add them and their
1275 * relocations to the validate list.
1277 struct anv_batch_bo
**bbo
;
1278 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1279 adjust_relocations_to_state_pool(ss_pool
, &(*bbo
)->bo
, &(*bbo
)->relocs
,
1280 cmd_buffer
->last_ss_pool_center
);
1282 result
= anv_execbuf_add_bo(execbuf
, &(*bbo
)->bo
, &(*bbo
)->relocs
, 0,
1283 &cmd_buffer
->device
->alloc
);
1284 if (result
!= VK_SUCCESS
)
1288 /* Now that we've adjusted all of the surface state relocations, we need to
1289 * record the surface state pool center so future executions of the command
1290 * buffer can adjust correctly.
1292 cmd_buffer
->last_ss_pool_center
= ss_pool
->block_pool
.center_bo_offset
;
1294 struct anv_batch_bo
*first_batch_bo
=
1295 list_first_entry(&cmd_buffer
->batch_bos
, struct anv_batch_bo
, link
);
1297 /* The kernel requires that the last entry in the validation list be the
1298 * batch buffer to execute. We can simply swap the element
1299 * corresponding to the first batch_bo in the chain with the last
1300 * element in the list.
1302 if (first_batch_bo
->bo
.index
!= execbuf
->bo_count
- 1) {
1303 uint32_t idx
= first_batch_bo
->bo
.index
;
1304 uint32_t last_idx
= execbuf
->bo_count
- 1;
1306 struct drm_i915_gem_exec_object2 tmp_obj
= execbuf
->objects
[idx
];
1307 assert(execbuf
->bos
[idx
] == &first_batch_bo
->bo
);
1309 execbuf
->objects
[idx
] = execbuf
->objects
[last_idx
];
1310 execbuf
->bos
[idx
] = execbuf
->bos
[last_idx
];
1311 execbuf
->bos
[idx
]->index
= idx
;
1313 execbuf
->objects
[last_idx
] = tmp_obj
;
1314 execbuf
->bos
[last_idx
] = &first_batch_bo
->bo
;
1315 first_batch_bo
->bo
.index
= last_idx
;
1318 /* Now we go through and fixup all of the relocation lists to point to
1319 * the correct indices in the object array. We have to do this after we
1320 * reorder the list above as some of the indices may have changed.
1322 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
)
1323 anv_cmd_buffer_process_relocs(cmd_buffer
, &(*bbo
)->relocs
);
1325 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
1327 if (!cmd_buffer
->device
->info
.has_llc
) {
1328 __builtin_ia32_mfence();
1329 u_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1330 for (uint32_t i
= 0; i
< (*bbo
)->length
; i
+= CACHELINE_SIZE
)
1331 __builtin_ia32_clflush((*bbo
)->bo
.map
+ i
);
1335 execbuf
->execbuf
= (struct drm_i915_gem_execbuffer2
) {
1336 .buffers_ptr
= (uintptr_t) execbuf
->objects
,
1337 .buffer_count
= execbuf
->bo_count
,
1338 .batch_start_offset
= 0,
1339 .batch_len
= batch
->next
- batch
->start
,
1344 .flags
= I915_EXEC_HANDLE_LUT
| I915_EXEC_RENDER
|
1345 I915_EXEC_CONSTANTS_REL_GENERAL
,
1346 .rsvd1
= cmd_buffer
->device
->context_id
,
1350 if (relocate_cmd_buffer(cmd_buffer
, execbuf
)) {
1351 /* If we were able to successfully relocate everything, tell the kernel
1352 * that it can skip doing relocations. The requirement for using
1355 * 1) The addresses written in the objects must match the corresponding
1356 * reloc.presumed_offset which in turn must match the corresponding
1357 * execobject.offset.
1359 * 2) To avoid stalling, execobject.offset should match the current
1360 * address of that object within the active context.
1362 * In order to satisfy all of the invariants that make userspace
1363 * relocations to be safe (see relocate_cmd_buffer()), we need to
1364 * further ensure that the addresses we use match those used by the
1365 * kernel for the most recent execbuf2.
1367 * The kernel may still choose to do relocations anyway if something has
1368 * moved in the GTT. In this case, the relocation list still needs to be
1369 * valid. All relocations on the batch buffers are already valid and
1370 * kept up-to-date. For surface state relocations, by applying the
1371 * relocations in relocate_cmd_buffer, we ensured that the address in
1372 * the RENDER_SURFACE_STATE matches presumed_offset, so it should be
1373 * safe for the kernel to relocate them as needed.
1375 execbuf
->execbuf
.flags
|= I915_EXEC_NO_RELOC
;
1377 /* In the case where we fall back to doing kernel relocations, we need
1378 * to ensure that the relocation list is valid. All relocations on the
1379 * batch buffers are already valid and kept up-to-date. Since surface
1380 * states are shared between command buffers and we don't know what
1381 * order they will be submitted to the kernel, we don't know what
1382 * address is actually written in the surface state object at any given
1383 * time. The only option is to set a bogus presumed offset and let the
1384 * kernel relocate them.
1386 for (size_t i
= 0; i
< cmd_buffer
->surface_relocs
.num_relocs
; i
++)
1387 cmd_buffer
->surface_relocs
.relocs
[i
].presumed_offset
= -1;
1394 anv_cmd_buffer_execbuf(struct anv_device
*device
,
1395 struct anv_cmd_buffer
*cmd_buffer
,
1396 const VkSemaphore
*in_semaphores
,
1397 uint32_t num_in_semaphores
,
1398 const VkSemaphore
*out_semaphores
,
1399 uint32_t num_out_semaphores
)
1401 struct anv_execbuf execbuf
;
1402 anv_execbuf_init(&execbuf
);
1404 VkResult result
= VK_SUCCESS
;
1405 for (uint32_t i
= 0; i
< num_in_semaphores
; i
++) {
1406 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, in_semaphores
[i
]);
1407 assert(semaphore
->temporary
.type
== ANV_SEMAPHORE_TYPE_NONE
);
1408 struct anv_semaphore_impl
*impl
= &semaphore
->permanent
;
1410 switch (impl
->type
) {
1411 case ANV_SEMAPHORE_TYPE_BO
:
1412 result
= anv_execbuf_add_bo(&execbuf
, impl
->bo
, NULL
,
1414 if (result
!= VK_SUCCESS
)
1422 for (uint32_t i
= 0; i
< num_out_semaphores
; i
++) {
1423 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, out_semaphores
[i
]);
1424 assert(semaphore
->temporary
.type
== ANV_SEMAPHORE_TYPE_NONE
);
1425 struct anv_semaphore_impl
*impl
= &semaphore
->permanent
;
1427 switch (impl
->type
) {
1428 case ANV_SEMAPHORE_TYPE_BO
:
1429 result
= anv_execbuf_add_bo(&execbuf
, impl
->bo
, NULL
,
1430 EXEC_OBJECT_WRITE
, &device
->alloc
);
1431 if (result
!= VK_SUCCESS
)
1439 result
= setup_execbuf_for_cmd_buffer(&execbuf
, cmd_buffer
);
1440 if (result
!= VK_SUCCESS
)
1443 result
= anv_device_execbuf(device
, &execbuf
.execbuf
, execbuf
.bos
);
1445 anv_execbuf_finish(&execbuf
, &device
->alloc
);