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/gen7_pack.h"
33 #include "genxml/gen8_pack.h"
35 /** \file anv_batch_chain.c
37 * This file contains functions related to anv_cmd_buffer as a data
38 * structure. This involves everything required to create and destroy
39 * the actual batch buffers as well as link them together and handle
40 * relocations and surface state. It specifically does *not* contain any
41 * handling of actual vkCmd calls beyond vkCmdExecuteCommands.
44 /*-----------------------------------------------------------------------*
45 * Functions related to anv_reloc_list
46 *-----------------------------------------------------------------------*/
49 anv_reloc_list_init_clone(struct anv_reloc_list
*list
,
50 const VkAllocationCallbacks
*alloc
,
51 const struct anv_reloc_list
*other_list
)
54 list
->num_relocs
= other_list
->num_relocs
;
55 list
->array_length
= other_list
->array_length
;
58 list
->array_length
= 256;
62 anv_alloc(alloc
, list
->array_length
* sizeof(*list
->relocs
), 8,
63 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
65 if (list
->relocs
== NULL
)
66 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
69 anv_alloc(alloc
, list
->array_length
* sizeof(*list
->reloc_bos
), 8,
70 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
72 if (list
->reloc_bos
== NULL
) {
73 anv_free(alloc
, list
->relocs
);
74 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
78 memcpy(list
->relocs
, other_list
->relocs
,
79 list
->array_length
* sizeof(*list
->relocs
));
80 memcpy(list
->reloc_bos
, other_list
->reloc_bos
,
81 list
->array_length
* sizeof(*list
->reloc_bos
));
88 anv_reloc_list_init(struct anv_reloc_list
*list
,
89 const VkAllocationCallbacks
*alloc
)
91 return anv_reloc_list_init_clone(list
, alloc
, NULL
);
95 anv_reloc_list_finish(struct anv_reloc_list
*list
,
96 const VkAllocationCallbacks
*alloc
)
98 anv_free(alloc
, list
->relocs
);
99 anv_free(alloc
, list
->reloc_bos
);
103 anv_reloc_list_grow(struct anv_reloc_list
*list
,
104 const VkAllocationCallbacks
*alloc
,
105 size_t num_additional_relocs
)
107 if (list
->num_relocs
+ num_additional_relocs
<= list
->array_length
)
110 size_t new_length
= list
->array_length
* 2;
111 while (new_length
< list
->num_relocs
+ num_additional_relocs
)
114 struct drm_i915_gem_relocation_entry
*new_relocs
=
115 anv_alloc(alloc
, new_length
* sizeof(*list
->relocs
), 8,
116 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
117 if (new_relocs
== NULL
)
118 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
120 struct anv_bo
**new_reloc_bos
=
121 anv_alloc(alloc
, new_length
* sizeof(*list
->reloc_bos
), 8,
122 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
123 if (new_reloc_bos
== NULL
) {
124 anv_free(alloc
, new_relocs
);
125 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
128 memcpy(new_relocs
, list
->relocs
, list
->num_relocs
* sizeof(*list
->relocs
));
129 memcpy(new_reloc_bos
, list
->reloc_bos
,
130 list
->num_relocs
* sizeof(*list
->reloc_bos
));
132 anv_free(alloc
, list
->relocs
);
133 anv_free(alloc
, list
->reloc_bos
);
135 list
->array_length
= new_length
;
136 list
->relocs
= new_relocs
;
137 list
->reloc_bos
= new_reloc_bos
;
143 anv_reloc_list_add(struct anv_reloc_list
*list
,
144 const VkAllocationCallbacks
*alloc
,
145 uint32_t offset
, struct anv_bo
*target_bo
, uint32_t delta
)
147 struct drm_i915_gem_relocation_entry
*entry
;
150 const uint32_t domain
=
151 target_bo
->is_winsys_bo
? I915_GEM_DOMAIN_RENDER
: 0;
153 anv_reloc_list_grow(list
, alloc
, 1);
154 /* TODO: Handle failure */
156 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
157 index
= list
->num_relocs
++;
158 list
->reloc_bos
[index
] = target_bo
;
159 entry
= &list
->relocs
[index
];
160 entry
->target_handle
= target_bo
->gem_handle
;
161 entry
->delta
= delta
;
162 entry
->offset
= offset
;
163 entry
->presumed_offset
= target_bo
->offset
;
164 entry
->read_domains
= domain
;
165 entry
->write_domain
= domain
;
166 VG(VALGRIND_CHECK_MEM_IS_DEFINED(entry
, sizeof(*entry
)));
168 return target_bo
->offset
+ delta
;
172 anv_reloc_list_append(struct anv_reloc_list
*list
,
173 const VkAllocationCallbacks
*alloc
,
174 struct anv_reloc_list
*other
, uint32_t offset
)
176 anv_reloc_list_grow(list
, alloc
, other
->num_relocs
);
177 /* TODO: Handle failure */
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
;
190 /*-----------------------------------------------------------------------*
191 * Functions related to anv_batch
192 *-----------------------------------------------------------------------*/
195 anv_batch_emit_dwords(struct anv_batch
*batch
, int num_dwords
)
197 if (batch
->next
+ num_dwords
* 4 > batch
->end
)
198 batch
->extend_cb(batch
, batch
->user_data
);
200 void *p
= batch
->next
;
202 batch
->next
+= num_dwords
* 4;
203 assert(batch
->next
<= batch
->end
);
209 anv_batch_emit_reloc(struct anv_batch
*batch
,
210 void *location
, struct anv_bo
*bo
, uint32_t delta
)
212 return anv_reloc_list_add(batch
->relocs
, batch
->alloc
,
213 location
- batch
->start
, bo
, delta
);
217 anv_batch_emit_batch(struct anv_batch
*batch
, struct anv_batch
*other
)
219 uint32_t size
, offset
;
221 size
= other
->next
- other
->start
;
222 assert(size
% 4 == 0);
224 if (batch
->next
+ size
> batch
->end
)
225 batch
->extend_cb(batch
, batch
->user_data
);
227 assert(batch
->next
+ size
<= batch
->end
);
229 VG(VALGRIND_CHECK_MEM_IS_DEFINED(other
->start
, size
));
230 memcpy(batch
->next
, other
->start
, size
);
232 offset
= batch
->next
- batch
->start
;
233 anv_reloc_list_append(batch
->relocs
, batch
->alloc
,
234 other
->relocs
, offset
);
239 /*-----------------------------------------------------------------------*
240 * Functions related to anv_batch_bo
241 *-----------------------------------------------------------------------*/
244 anv_batch_bo_create(struct anv_cmd_buffer
*cmd_buffer
,
245 struct anv_batch_bo
**bbo_out
)
249 struct anv_batch_bo
*bbo
= anv_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
250 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
252 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
254 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
,
255 ANV_CMD_BUFFER_BATCH_SIZE
);
256 if (result
!= VK_SUCCESS
)
259 result
= anv_reloc_list_init(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
260 if (result
!= VK_SUCCESS
)
268 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
270 anv_free(&cmd_buffer
->pool
->alloc
, bbo
);
276 anv_batch_bo_clone(struct anv_cmd_buffer
*cmd_buffer
,
277 const struct anv_batch_bo
*other_bbo
,
278 struct anv_batch_bo
**bbo_out
)
282 struct anv_batch_bo
*bbo
= anv_alloc(&cmd_buffer
->pool
->alloc
, sizeof(*bbo
),
283 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
285 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
287 result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
,
289 if (result
!= VK_SUCCESS
)
292 result
= anv_reloc_list_init_clone(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
,
294 if (result
!= VK_SUCCESS
)
297 bbo
->length
= other_bbo
->length
;
298 memcpy(bbo
->bo
.map
, other_bbo
->bo
.map
, other_bbo
->length
);
300 bbo
->last_ss_pool_bo_offset
= other_bbo
->last_ss_pool_bo_offset
;
307 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
309 anv_free(&cmd_buffer
->pool
->alloc
, bbo
);
315 anv_batch_bo_start(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
316 size_t batch_padding
)
318 batch
->next
= batch
->start
= bbo
->bo
.map
;
319 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
320 batch
->relocs
= &bbo
->relocs
;
321 bbo
->last_ss_pool_bo_offset
= 0;
322 bbo
->relocs
.num_relocs
= 0;
326 anv_batch_bo_continue(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
,
327 size_t batch_padding
)
329 batch
->start
= bbo
->bo
.map
;
330 batch
->next
= bbo
->bo
.map
+ bbo
->length
;
331 batch
->end
= bbo
->bo
.map
+ bbo
->bo
.size
- batch_padding
;
332 batch
->relocs
= &bbo
->relocs
;
336 anv_batch_bo_finish(struct anv_batch_bo
*bbo
, struct anv_batch
*batch
)
338 assert(batch
->start
== bbo
->bo
.map
);
339 bbo
->length
= batch
->next
- batch
->start
;
340 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch
->start
, bbo
->length
));
344 anv_batch_bo_grow(struct anv_cmd_buffer
*cmd_buffer
, struct anv_batch_bo
*bbo
,
345 struct anv_batch
*batch
, size_t aditional
,
346 size_t batch_padding
)
348 assert(batch
->start
== bbo
->bo
.map
);
349 bbo
->length
= batch
->next
- batch
->start
;
351 size_t new_size
= bbo
->bo
.size
;
352 while (new_size
<= bbo
->length
+ aditional
+ batch_padding
)
355 if (new_size
== bbo
->bo
.size
)
358 struct anv_bo new_bo
;
359 VkResult result
= anv_bo_pool_alloc(&cmd_buffer
->device
->batch_bo_pool
,
361 if (result
!= VK_SUCCESS
)
364 memcpy(new_bo
.map
, bbo
->bo
.map
, bbo
->length
);
366 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
369 anv_batch_bo_continue(bbo
, batch
, batch_padding
);
375 anv_batch_bo_destroy(struct anv_batch_bo
*bbo
,
376 struct anv_cmd_buffer
*cmd_buffer
)
378 anv_reloc_list_finish(&bbo
->relocs
, &cmd_buffer
->pool
->alloc
);
379 anv_bo_pool_free(&cmd_buffer
->device
->batch_bo_pool
, &bbo
->bo
);
380 anv_free(&cmd_buffer
->pool
->alloc
, bbo
);
384 anv_batch_bo_list_clone(const struct list_head
*list
,
385 struct anv_cmd_buffer
*cmd_buffer
,
386 struct list_head
*new_list
)
388 VkResult result
= VK_SUCCESS
;
390 list_inithead(new_list
);
392 struct anv_batch_bo
*prev_bbo
= NULL
;
393 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
394 struct anv_batch_bo
*new_bbo
= NULL
;
395 result
= anv_batch_bo_clone(cmd_buffer
, bbo
, &new_bbo
);
396 if (result
!= VK_SUCCESS
)
398 list_addtail(&new_bbo
->link
, new_list
);
401 /* As we clone this list of batch_bo's, they chain one to the
402 * other using MI_BATCH_BUFFER_START commands. We need to fix up
403 * those relocations as we go. Fortunately, this is pretty easy
404 * as it will always be the last relocation in the list.
406 uint32_t last_idx
= prev_bbo
->relocs
.num_relocs
- 1;
407 assert(prev_bbo
->relocs
.reloc_bos
[last_idx
] == &bbo
->bo
);
408 prev_bbo
->relocs
.reloc_bos
[last_idx
] = &new_bbo
->bo
;
414 if (result
!= VK_SUCCESS
) {
415 list_for_each_entry_safe(struct anv_batch_bo
, bbo
, new_list
, link
)
416 anv_batch_bo_destroy(bbo
, cmd_buffer
);
422 /*-----------------------------------------------------------------------*
423 * Functions related to anv_batch_bo
424 *-----------------------------------------------------------------------*/
426 static inline struct anv_batch_bo
*
427 anv_cmd_buffer_current_batch_bo(struct anv_cmd_buffer
*cmd_buffer
)
429 return LIST_ENTRY(struct anv_batch_bo
, cmd_buffer
->batch_bos
.prev
, link
);
433 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer
*cmd_buffer
)
435 return (struct anv_address
) {
436 .bo
= &cmd_buffer
->device
->surface_state_block_pool
.bo
,
437 .offset
= *(int32_t *)anv_vector_head(&cmd_buffer
->bt_blocks
),
442 emit_batch_buffer_start(struct anv_cmd_buffer
*cmd_buffer
,
443 struct anv_bo
*bo
, uint32_t offset
)
445 /* In gen8+ the address field grew to two dwords to accomodate 48 bit
446 * offsets. The high 16 bits are in the last dword, so we can use the gen8
447 * version in either case, as long as we set the instruction length in the
448 * header accordingly. This means that we always emit three dwords here
449 * and all the padding and adjustment we do in this file works for all
453 const uint32_t gen7_length
=
454 GEN7_MI_BATCH_BUFFER_START_length
- GEN7_MI_BATCH_BUFFER_START_length_bias
;
455 const uint32_t gen8_length
=
456 GEN8_MI_BATCH_BUFFER_START_length
- GEN8_MI_BATCH_BUFFER_START_length_bias
;
458 anv_batch_emit(&cmd_buffer
->batch
, GEN8_MI_BATCH_BUFFER_START
, bbs
) {
459 bbs
.DWordLength
= cmd_buffer
->device
->info
.gen
< 8 ?
460 gen7_length
: gen8_length
;
461 bbs
._2ndLevelBatchBuffer
= _1stlevelbatch
;
462 bbs
.AddressSpaceIndicator
= ASI_PPGTT
;
463 bbs
.BatchBufferStartAddress
= (struct anv_address
) { bo
, offset
};
468 cmd_buffer_chain_to_batch_bo(struct anv_cmd_buffer
*cmd_buffer
,
469 struct anv_batch_bo
*bbo
)
471 struct anv_batch
*batch
= &cmd_buffer
->batch
;
472 struct anv_batch_bo
*current_bbo
=
473 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
475 /* We set the end of the batch a little short so we would be sure we
476 * have room for the chaining command. Since we're about to emit the
477 * chaining command, let's set it back where it should go.
479 batch
->end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
480 assert(batch
->end
== current_bbo
->bo
.map
+ current_bbo
->bo
.size
);
482 emit_batch_buffer_start(cmd_buffer
, &bbo
->bo
, 0);
484 anv_batch_bo_finish(current_bbo
, batch
);
488 anv_cmd_buffer_chain_batch(struct anv_batch
*batch
, void *_data
)
490 struct anv_cmd_buffer
*cmd_buffer
= _data
;
491 struct anv_batch_bo
*new_bbo
;
493 VkResult result
= anv_batch_bo_create(cmd_buffer
, &new_bbo
);
494 if (result
!= VK_SUCCESS
)
497 struct anv_batch_bo
**seen_bbo
= anv_vector_add(&cmd_buffer
->seen_bbos
);
498 if (seen_bbo
== NULL
) {
499 anv_batch_bo_destroy(new_bbo
, cmd_buffer
);
500 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
504 cmd_buffer_chain_to_batch_bo(cmd_buffer
, new_bbo
);
506 list_addtail(&new_bbo
->link
, &cmd_buffer
->batch_bos
);
508 anv_batch_bo_start(new_bbo
, batch
, GEN8_MI_BATCH_BUFFER_START_length
* 4);
514 anv_cmd_buffer_grow_batch(struct anv_batch
*batch
, void *_data
)
516 struct anv_cmd_buffer
*cmd_buffer
= _data
;
517 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
519 anv_batch_bo_grow(cmd_buffer
, bbo
, &cmd_buffer
->batch
, 4096,
520 GEN8_MI_BATCH_BUFFER_START_length
* 4);
526 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer
*cmd_buffer
,
527 uint32_t entries
, uint32_t *state_offset
)
529 struct anv_block_pool
*block_pool
=
530 &cmd_buffer
->device
->surface_state_block_pool
;
531 int32_t *bt_block
= anv_vector_head(&cmd_buffer
->bt_blocks
);
532 struct anv_state state
;
534 state
.alloc_size
= align_u32(entries
* 4, 32);
536 if (cmd_buffer
->bt_next
+ state
.alloc_size
> block_pool
->block_size
)
537 return (struct anv_state
) { 0 };
539 state
.offset
= cmd_buffer
->bt_next
;
540 state
.map
= block_pool
->map
+ *bt_block
+ state
.offset
;
542 cmd_buffer
->bt_next
+= state
.alloc_size
;
544 assert(*bt_block
< 0);
545 *state_offset
= -(*bt_block
);
551 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer
*cmd_buffer
)
553 return anv_state_stream_alloc(&cmd_buffer
->surface_state_stream
, 64, 64);
557 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer
*cmd_buffer
,
558 uint32_t size
, uint32_t alignment
)
560 return anv_state_stream_alloc(&cmd_buffer
->dynamic_state_stream
,
565 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer
*cmd_buffer
)
567 struct anv_block_pool
*block_pool
=
568 &cmd_buffer
->device
->surface_state_block_pool
;
570 int32_t *offset
= anv_vector_add(&cmd_buffer
->bt_blocks
);
572 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
574 *offset
= anv_block_pool_alloc_back(block_pool
);
575 cmd_buffer
->bt_next
= 0;
581 anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
583 struct anv_batch_bo
*batch_bo
;
586 list_inithead(&cmd_buffer
->batch_bos
);
588 result
= anv_batch_bo_create(cmd_buffer
, &batch_bo
);
589 if (result
!= VK_SUCCESS
)
592 list_addtail(&batch_bo
->link
, &cmd_buffer
->batch_bos
);
594 cmd_buffer
->batch
.alloc
= &cmd_buffer
->pool
->alloc
;
595 cmd_buffer
->batch
.user_data
= cmd_buffer
;
597 if (cmd_buffer
->device
->can_chain_batches
) {
598 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_chain_batch
;
600 cmd_buffer
->batch
.extend_cb
= anv_cmd_buffer_grow_batch
;
603 anv_batch_bo_start(batch_bo
, &cmd_buffer
->batch
,
604 GEN8_MI_BATCH_BUFFER_START_length
* 4);
606 int success
= anv_vector_init(&cmd_buffer
->seen_bbos
,
607 sizeof(struct anv_bo
*),
608 8 * sizeof(struct anv_bo
*));
612 *(struct anv_batch_bo
**)anv_vector_add(&cmd_buffer
->seen_bbos
) = batch_bo
;
614 success
= anv_vector_init(&cmd_buffer
->bt_blocks
, sizeof(int32_t),
615 8 * sizeof(int32_t));
619 result
= anv_reloc_list_init(&cmd_buffer
->surface_relocs
,
620 &cmd_buffer
->pool
->alloc
);
621 if (result
!= VK_SUCCESS
)
624 anv_cmd_buffer_new_binding_table_block(cmd_buffer
);
626 cmd_buffer
->execbuf2
.objects
= NULL
;
627 cmd_buffer
->execbuf2
.bos
= NULL
;
628 cmd_buffer
->execbuf2
.array_length
= 0;
633 anv_vector_finish(&cmd_buffer
->bt_blocks
);
635 anv_vector_finish(&cmd_buffer
->seen_bbos
);
637 anv_batch_bo_destroy(batch_bo
, cmd_buffer
);
643 anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
646 anv_vector_foreach(bt_block
, &cmd_buffer
->bt_blocks
) {
647 anv_block_pool_free(&cmd_buffer
->device
->surface_state_block_pool
,
650 anv_vector_finish(&cmd_buffer
->bt_blocks
);
652 anv_reloc_list_finish(&cmd_buffer
->surface_relocs
, &cmd_buffer
->pool
->alloc
);
654 anv_vector_finish(&cmd_buffer
->seen_bbos
);
656 /* Destroy all of the batch buffers */
657 list_for_each_entry_safe(struct anv_batch_bo
, bbo
,
658 &cmd_buffer
->batch_bos
, link
) {
659 anv_batch_bo_destroy(bbo
, cmd_buffer
);
662 anv_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
->execbuf2
.objects
);
663 anv_free(&cmd_buffer
->pool
->alloc
, cmd_buffer
->execbuf2
.bos
);
667 anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer
*cmd_buffer
)
669 /* Delete all but the first batch bo */
670 assert(!list_empty(&cmd_buffer
->batch_bos
));
671 while (cmd_buffer
->batch_bos
.next
!= cmd_buffer
->batch_bos
.prev
) {
672 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
673 list_del(&bbo
->link
);
674 anv_batch_bo_destroy(bbo
, cmd_buffer
);
676 assert(!list_empty(&cmd_buffer
->batch_bos
));
678 anv_batch_bo_start(anv_cmd_buffer_current_batch_bo(cmd_buffer
),
680 GEN8_MI_BATCH_BUFFER_START_length
* 4);
682 while (anv_vector_length(&cmd_buffer
->bt_blocks
) > 1) {
683 int32_t *bt_block
= anv_vector_remove(&cmd_buffer
->bt_blocks
);
684 anv_block_pool_free(&cmd_buffer
->device
->surface_state_block_pool
,
687 assert(anv_vector_length(&cmd_buffer
->bt_blocks
) == 1);
688 cmd_buffer
->bt_next
= 0;
690 cmd_buffer
->surface_relocs
.num_relocs
= 0;
692 /* Reset the list of seen buffers */
693 cmd_buffer
->seen_bbos
.head
= 0;
694 cmd_buffer
->seen_bbos
.tail
= 0;
696 *(struct anv_batch_bo
**)anv_vector_add(&cmd_buffer
->seen_bbos
) =
697 anv_cmd_buffer_current_batch_bo(cmd_buffer
);
701 anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer
*cmd_buffer
)
703 struct anv_batch_bo
*batch_bo
= anv_cmd_buffer_current_batch_bo(cmd_buffer
);
705 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
) {
706 /* When we start a batch buffer, we subtract a certain amount of
707 * padding from the end to ensure that we always have room to emit a
708 * BATCH_BUFFER_START to chain to the next BO. We need to remove
709 * that padding before we end the batch; otherwise, we may end up
710 * with our BATCH_BUFFER_END in another BO.
712 cmd_buffer
->batch
.end
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
713 assert(cmd_buffer
->batch
.end
== batch_bo
->bo
.map
+ batch_bo
->bo
.size
);
715 anv_batch_emit(&cmd_buffer
->batch
, GEN7_MI_BATCH_BUFFER_END
, bbe
);
717 /* Round batch up to an even number of dwords. */
718 if ((cmd_buffer
->batch
.next
- cmd_buffer
->batch
.start
) & 4)
719 anv_batch_emit(&cmd_buffer
->batch
, GEN7_MI_NOOP
, noop
);
721 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_PRIMARY
;
724 anv_batch_bo_finish(batch_bo
, &cmd_buffer
->batch
);
726 if (cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_SECONDARY
) {
727 /* If this is a secondary command buffer, we need to determine the
728 * mode in which it will be executed with vkExecuteCommands. We
729 * determine this statically here so that this stays in sync with the
730 * actual ExecuteCommands implementation.
732 if (!cmd_buffer
->device
->can_chain_batches
) {
733 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
;
734 } else if ((cmd_buffer
->batch_bos
.next
== cmd_buffer
->batch_bos
.prev
) &&
735 (batch_bo
->length
< ANV_CMD_BUFFER_BATCH_SIZE
/ 2)) {
736 /* If the secondary has exactly one batch buffer in its list *and*
737 * that batch buffer is less than half of the maximum size, we're
738 * probably better of simply copying it into our batch.
740 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_EMIT
;
741 } else if (!(cmd_buffer
->usage_flags
&
742 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT
)) {
743 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_CHAIN
;
745 /* When we chain, we need to add an MI_BATCH_BUFFER_START command
746 * with its relocation. In order to handle this we'll increment here
747 * so we can unconditionally decrement right before adding the
748 * MI_BATCH_BUFFER_START command.
750 batch_bo
->relocs
.num_relocs
++;
751 cmd_buffer
->batch
.next
+= GEN8_MI_BATCH_BUFFER_START_length
* 4;
753 cmd_buffer
->exec_mode
= ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
;
758 static inline VkResult
759 anv_cmd_buffer_add_seen_bbos(struct anv_cmd_buffer
*cmd_buffer
,
760 struct list_head
*list
)
762 list_for_each_entry(struct anv_batch_bo
, bbo
, list
, link
) {
763 struct anv_batch_bo
**bbo_ptr
= anv_vector_add(&cmd_buffer
->seen_bbos
);
765 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
774 anv_cmd_buffer_add_secondary(struct anv_cmd_buffer
*primary
,
775 struct anv_cmd_buffer
*secondary
)
777 switch (secondary
->exec_mode
) {
778 case ANV_CMD_BUFFER_EXEC_MODE_EMIT
:
779 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
780 anv_cmd_buffer_emit_state_base_address(primary
);
782 case ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT
: {
783 struct anv_batch_bo
*bbo
= anv_cmd_buffer_current_batch_bo(primary
);
784 unsigned length
= secondary
->batch
.end
- secondary
->batch
.start
;
785 anv_batch_bo_grow(primary
, bbo
, &primary
->batch
, length
,
786 GEN8_MI_BATCH_BUFFER_START_length
* 4);
787 anv_batch_emit_batch(&primary
->batch
, &secondary
->batch
);
788 anv_cmd_buffer_emit_state_base_address(primary
);
791 case ANV_CMD_BUFFER_EXEC_MODE_CHAIN
: {
792 struct anv_batch_bo
*first_bbo
=
793 list_first_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
794 struct anv_batch_bo
*last_bbo
=
795 list_last_entry(&secondary
->batch_bos
, struct anv_batch_bo
, link
);
797 emit_batch_buffer_start(primary
, &first_bbo
->bo
, 0);
799 struct anv_batch_bo
*this_bbo
= anv_cmd_buffer_current_batch_bo(primary
);
800 assert(primary
->batch
.start
== this_bbo
->bo
.map
);
801 uint32_t offset
= primary
->batch
.next
- primary
->batch
.start
;
802 const uint32_t inst_size
= GEN8_MI_BATCH_BUFFER_START_length
* 4;
804 /* Roll back the previous MI_BATCH_BUFFER_START and its relocation so we
805 * can emit a new command and relocation for the current splice. In
806 * order to handle the initial-use case, we incremented next and
807 * num_relocs in end_batch_buffer() so we can alyways just subtract
810 last_bbo
->relocs
.num_relocs
--;
811 secondary
->batch
.next
-= inst_size
;
812 emit_batch_buffer_start(secondary
, &this_bbo
->bo
, offset
);
813 anv_cmd_buffer_add_seen_bbos(primary
, &secondary
->batch_bos
);
815 /* After patching up the secondary buffer, we need to clflush the
816 * modified instruction in case we're on a !llc platform. We use a
817 * little loop to handle the case where the instruction crosses a cache
820 if (!primary
->device
->info
.has_llc
) {
821 void *inst
= secondary
->batch
.next
- inst_size
;
822 void *p
= (void *) (((uintptr_t) inst
) & ~CACHELINE_MASK
);
823 __builtin_ia32_mfence();
824 while (p
< secondary
->batch
.next
) {
825 __builtin_ia32_clflush(p
);
830 anv_cmd_buffer_emit_state_base_address(primary
);
833 case ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN
: {
834 struct list_head copy_list
;
835 VkResult result
= anv_batch_bo_list_clone(&secondary
->batch_bos
,
838 if (result
!= VK_SUCCESS
)
841 anv_cmd_buffer_add_seen_bbos(primary
, ©_list
);
843 struct anv_batch_bo
*first_bbo
=
844 list_first_entry(©_list
, struct anv_batch_bo
, link
);
845 struct anv_batch_bo
*last_bbo
=
846 list_last_entry(©_list
, struct anv_batch_bo
, link
);
848 cmd_buffer_chain_to_batch_bo(primary
, first_bbo
);
850 list_splicetail(©_list
, &primary
->batch_bos
);
852 anv_batch_bo_continue(last_bbo
, &primary
->batch
,
853 GEN8_MI_BATCH_BUFFER_START_length
* 4);
855 anv_cmd_buffer_emit_state_base_address(primary
);
859 assert(!"Invalid execution mode");
862 anv_reloc_list_append(&primary
->surface_relocs
, &primary
->pool
->alloc
,
863 &secondary
->surface_relocs
, 0);
867 anv_cmd_buffer_add_bo(struct anv_cmd_buffer
*cmd_buffer
,
869 struct anv_reloc_list
*relocs
)
871 struct drm_i915_gem_exec_object2
*obj
= NULL
;
873 if (bo
->index
< cmd_buffer
->execbuf2
.bo_count
&&
874 cmd_buffer
->execbuf2
.bos
[bo
->index
] == bo
)
875 obj
= &cmd_buffer
->execbuf2
.objects
[bo
->index
];
878 /* We've never seen this one before. Add it to the list and assign
879 * an id that we can use later.
881 if (cmd_buffer
->execbuf2
.bo_count
>= cmd_buffer
->execbuf2
.array_length
) {
882 uint32_t new_len
= cmd_buffer
->execbuf2
.objects
?
883 cmd_buffer
->execbuf2
.array_length
* 2 : 64;
885 struct drm_i915_gem_exec_object2
*new_objects
=
886 anv_alloc(&cmd_buffer
->pool
->alloc
, new_len
* sizeof(*new_objects
),
887 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
888 if (new_objects
== NULL
)
889 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
891 struct anv_bo
**new_bos
=
892 anv_alloc(&cmd_buffer
->pool
->alloc
, new_len
* sizeof(*new_bos
),
893 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
894 if (new_bos
== NULL
) {
895 anv_free(&cmd_buffer
->pool
->alloc
, new_objects
);
896 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
899 if (cmd_buffer
->execbuf2
.objects
) {
900 memcpy(new_objects
, cmd_buffer
->execbuf2
.objects
,
901 cmd_buffer
->execbuf2
.bo_count
* sizeof(*new_objects
));
902 memcpy(new_bos
, cmd_buffer
->execbuf2
.bos
,
903 cmd_buffer
->execbuf2
.bo_count
* sizeof(*new_bos
));
906 cmd_buffer
->execbuf2
.objects
= new_objects
;
907 cmd_buffer
->execbuf2
.bos
= new_bos
;
908 cmd_buffer
->execbuf2
.array_length
= new_len
;
911 assert(cmd_buffer
->execbuf2
.bo_count
< cmd_buffer
->execbuf2
.array_length
);
913 bo
->index
= cmd_buffer
->execbuf2
.bo_count
++;
914 obj
= &cmd_buffer
->execbuf2
.objects
[bo
->index
];
915 cmd_buffer
->execbuf2
.bos
[bo
->index
] = bo
;
917 obj
->handle
= bo
->gem_handle
;
918 obj
->relocation_count
= 0;
921 obj
->offset
= bo
->offset
;
922 obj
->flags
= bo
->is_winsys_bo
? EXEC_OBJECT_WRITE
: 0;
927 if (relocs
!= NULL
&& obj
->relocation_count
== 0) {
928 /* This is the first time we've ever seen a list of relocations for
929 * this BO. Go ahead and set the relocations and then walk the list
930 * of relocations and add them all.
932 obj
->relocation_count
= relocs
->num_relocs
;
933 obj
->relocs_ptr
= (uintptr_t) relocs
->relocs
;
935 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
936 /* A quick sanity check on relocations */
937 assert(relocs
->relocs
[i
].offset
< bo
->size
);
938 anv_cmd_buffer_add_bo(cmd_buffer
, relocs
->reloc_bos
[i
], NULL
);
946 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer
*cmd_buffer
,
947 struct anv_reloc_list
*list
)
951 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
952 * struct drm_i915_gem_exec_object2 against the bos current offset and if
953 * all bos haven't moved it will skip relocation processing alltogether.
954 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
955 * value of offset so we can set it either way. For that to work we need
956 * to make sure all relocs use the same presumed offset.
959 for (size_t i
= 0; i
< list
->num_relocs
; i
++) {
960 bo
= list
->reloc_bos
[i
];
961 if (bo
->offset
!= list
->relocs
[i
].presumed_offset
)
962 cmd_buffer
->execbuf2
.need_reloc
= true;
964 list
->relocs
[i
].target_handle
= bo
->index
;
969 read_reloc(const struct anv_device
*device
, const void *p
)
971 if (device
->info
.gen
>= 8)
972 return *(uint64_t *)p
;
974 return *(uint32_t *)p
;
978 write_reloc(const struct anv_device
*device
, void *p
, uint64_t v
)
980 if (device
->info
.gen
>= 8)
987 adjust_relocations_from_block_pool(struct anv_block_pool
*pool
,
988 struct anv_reloc_list
*relocs
)
990 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
991 /* In general, we don't know how stale the relocated value is. It
992 * may have been used last time or it may not. Since we don't want
993 * to stomp it while the GPU may be accessing it, we haven't updated
994 * it anywhere else in the code. Instead, we just set the presumed
995 * offset to what it is now based on the delta and the data in the
996 * block pool. Then the kernel will update it for us if needed.
998 assert(relocs
->relocs
[i
].offset
< pool
->state
.end
);
999 const void *p
= pool
->map
+ relocs
->relocs
[i
].offset
;
1001 /* We're reading back the relocated value from potentially incoherent
1002 * memory here. However, any change to the value will be from the kernel
1003 * writing out relocations, which will keep the CPU cache up to date.
1005 relocs
->relocs
[i
].presumed_offset
=
1006 read_reloc(pool
->device
, p
) - relocs
->relocs
[i
].delta
;
1008 /* All of the relocations from this block pool to other BO's should
1009 * have been emitted relative to the surface block pool center. We
1010 * need to add the center offset to make them relative to the
1011 * beginning of the actual GEM bo.
1013 relocs
->relocs
[i
].offset
+= pool
->center_bo_offset
;
1018 adjust_relocations_to_block_pool(struct anv_block_pool
*pool
,
1019 struct anv_bo
*from_bo
,
1020 struct anv_reloc_list
*relocs
,
1021 uint32_t *last_pool_center_bo_offset
)
1023 assert(*last_pool_center_bo_offset
<= pool
->center_bo_offset
);
1024 uint32_t delta
= pool
->center_bo_offset
- *last_pool_center_bo_offset
;
1026 /* When we initially emit relocations into a block pool, we don't
1027 * actually know what the final center_bo_offset will be so we just emit
1028 * it as if center_bo_offset == 0. Now that we know what the center
1029 * offset is, we need to walk the list of relocations and adjust any
1030 * relocations that point to the pool bo with the correct offset.
1032 for (size_t i
= 0; i
< relocs
->num_relocs
; i
++) {
1033 if (relocs
->reloc_bos
[i
] == &pool
->bo
) {
1034 /* Adjust the delta value in the relocation to correctly
1035 * correspond to the new delta. Initially, this value may have
1036 * been negative (if treated as unsigned), but we trust in
1037 * uint32_t roll-over to fix that for us at this point.
1039 relocs
->relocs
[i
].delta
+= delta
;
1041 /* Since the delta has changed, we need to update the actual
1042 * relocated value with the new presumed value. This function
1043 * should only be called on batch buffers, so we know it isn't in
1044 * use by the GPU at the moment.
1046 assert(relocs
->relocs
[i
].offset
< from_bo
->size
);
1047 write_reloc(pool
->device
, from_bo
->map
+ relocs
->relocs
[i
].offset
,
1048 relocs
->relocs
[i
].presumed_offset
+
1049 relocs
->relocs
[i
].delta
);
1053 *last_pool_center_bo_offset
= pool
->center_bo_offset
;
1057 anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer
*cmd_buffer
)
1059 struct anv_batch
*batch
= &cmd_buffer
->batch
;
1060 struct anv_block_pool
*ss_pool
=
1061 &cmd_buffer
->device
->surface_state_block_pool
;
1063 cmd_buffer
->execbuf2
.bo_count
= 0;
1064 cmd_buffer
->execbuf2
.need_reloc
= false;
1066 adjust_relocations_from_block_pool(ss_pool
, &cmd_buffer
->surface_relocs
);
1067 anv_cmd_buffer_add_bo(cmd_buffer
, &ss_pool
->bo
, &cmd_buffer
->surface_relocs
);
1069 /* First, we walk over all of the bos we've seen and add them and their
1070 * relocations to the validate list.
1072 struct anv_batch_bo
**bbo
;
1073 anv_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1074 adjust_relocations_to_block_pool(ss_pool
, &(*bbo
)->bo
, &(*bbo
)->relocs
,
1075 &(*bbo
)->last_ss_pool_bo_offset
);
1077 anv_cmd_buffer_add_bo(cmd_buffer
, &(*bbo
)->bo
, &(*bbo
)->relocs
);
1080 struct anv_batch_bo
*first_batch_bo
=
1081 list_first_entry(&cmd_buffer
->batch_bos
, struct anv_batch_bo
, link
);
1083 /* The kernel requires that the last entry in the validation list be the
1084 * batch buffer to execute. We can simply swap the element
1085 * corresponding to the first batch_bo in the chain with the last
1086 * element in the list.
1088 if (first_batch_bo
->bo
.index
!= cmd_buffer
->execbuf2
.bo_count
- 1) {
1089 uint32_t idx
= first_batch_bo
->bo
.index
;
1090 uint32_t last_idx
= cmd_buffer
->execbuf2
.bo_count
- 1;
1092 struct drm_i915_gem_exec_object2 tmp_obj
=
1093 cmd_buffer
->execbuf2
.objects
[idx
];
1094 assert(cmd_buffer
->execbuf2
.bos
[idx
] == &first_batch_bo
->bo
);
1096 cmd_buffer
->execbuf2
.objects
[idx
] = cmd_buffer
->execbuf2
.objects
[last_idx
];
1097 cmd_buffer
->execbuf2
.bos
[idx
] = cmd_buffer
->execbuf2
.bos
[last_idx
];
1098 cmd_buffer
->execbuf2
.bos
[idx
]->index
= idx
;
1100 cmd_buffer
->execbuf2
.objects
[last_idx
] = tmp_obj
;
1101 cmd_buffer
->execbuf2
.bos
[last_idx
] = &first_batch_bo
->bo
;
1102 first_batch_bo
->bo
.index
= last_idx
;
1105 /* Now we go through and fixup all of the relocation lists to point to
1106 * the correct indices in the object array. We have to do this after we
1107 * reorder the list above as some of the indices may have changed.
1109 anv_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
)
1110 anv_cmd_buffer_process_relocs(cmd_buffer
, &(*bbo
)->relocs
);
1112 anv_cmd_buffer_process_relocs(cmd_buffer
, &cmd_buffer
->surface_relocs
);
1114 if (!cmd_buffer
->device
->info
.has_llc
) {
1115 __builtin_ia32_mfence();
1116 anv_vector_foreach(bbo
, &cmd_buffer
->seen_bbos
) {
1117 for (uint32_t i
= 0; i
< (*bbo
)->length
; i
+= CACHELINE_SIZE
)
1118 __builtin_ia32_clflush((*bbo
)->bo
.map
+ i
);
1122 cmd_buffer
->execbuf2
.execbuf
= (struct drm_i915_gem_execbuffer2
) {
1123 .buffers_ptr
= (uintptr_t) cmd_buffer
->execbuf2
.objects
,
1124 .buffer_count
= cmd_buffer
->execbuf2
.bo_count
,
1125 .batch_start_offset
= 0,
1126 .batch_len
= batch
->next
- batch
->start
,
1131 .flags
= I915_EXEC_HANDLE_LUT
| I915_EXEC_RENDER
|
1132 I915_EXEC_CONSTANTS_REL_GENERAL
,
1133 .rsvd1
= cmd_buffer
->device
->context_id
,
1137 if (!cmd_buffer
->execbuf2
.need_reloc
)
1138 cmd_buffer
->execbuf2
.execbuf
.flags
|= I915_EXEC_NO_RELOC
;