9e01a275a1f40a600c75740c2e1f492ed2286547
[mesa.git] / src / intel / vulkan / anv_batch_chain.c
1 /*
2 * Copyright © 2015 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include <assert.h>
25 #include <stdbool.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "anv_private.h"
31
32 #include "genxml/gen8_pack.h"
33
34 #include "util/debug.h"
35
36 /** \file anv_batch_chain.c
37 *
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.
43 */
44
45 /*-----------------------------------------------------------------------*
46 * Functions related to anv_reloc_list
47 *-----------------------------------------------------------------------*/
48
49 static VkResult
50 anv_reloc_list_init_clone(struct anv_reloc_list *list,
51 const VkAllocationCallbacks *alloc,
52 const struct anv_reloc_list *other_list)
53 {
54 if (other_list) {
55 list->num_relocs = other_list->num_relocs;
56 list->array_length = other_list->array_length;
57 } else {
58 list->num_relocs = 0;
59 list->array_length = 256;
60 }
61
62 list->relocs =
63 vk_alloc(alloc, list->array_length * sizeof(*list->relocs), 8,
64 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
65
66 if (list->relocs == NULL)
67 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
68
69 list->reloc_bos =
70 vk_alloc(alloc, list->array_length * sizeof(*list->reloc_bos), 8,
71 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
72
73 if (list->reloc_bos == NULL) {
74 vk_free(alloc, list->relocs);
75 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
76 }
77
78 if (other_list) {
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));
83 }
84
85 return VK_SUCCESS;
86 }
87
88 VkResult
89 anv_reloc_list_init(struct anv_reloc_list *list,
90 const VkAllocationCallbacks *alloc)
91 {
92 return anv_reloc_list_init_clone(list, alloc, NULL);
93 }
94
95 void
96 anv_reloc_list_finish(struct anv_reloc_list *list,
97 const VkAllocationCallbacks *alloc)
98 {
99 vk_free(alloc, list->relocs);
100 vk_free(alloc, list->reloc_bos);
101 }
102
103 static VkResult
104 anv_reloc_list_grow(struct anv_reloc_list *list,
105 const VkAllocationCallbacks *alloc,
106 size_t num_additional_relocs)
107 {
108 if (list->num_relocs + num_additional_relocs <= list->array_length)
109 return VK_SUCCESS;
110
111 size_t new_length = list->array_length * 2;
112 while (new_length < list->num_relocs + num_additional_relocs)
113 new_length *= 2;
114
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);
120
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);
127 }
128
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));
132
133 vk_free(alloc, list->relocs);
134 vk_free(alloc, list->reloc_bos);
135
136 list->array_length = new_length;
137 list->relocs = new_relocs;
138 list->reloc_bos = new_reloc_bos;
139
140 return VK_SUCCESS;
141 }
142
143 VkResult
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)
147 {
148 struct drm_i915_gem_relocation_entry *entry;
149 int index;
150
151 VkResult result = anv_reloc_list_grow(list, alloc, 1);
152 if (result != VK_SUCCESS)
153 return result;
154
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)));
166
167 return VK_SUCCESS;
168 }
169
170 static VkResult
171 anv_reloc_list_append(struct anv_reloc_list *list,
172 const VkAllocationCallbacks *alloc,
173 struct anv_reloc_list *other, uint32_t offset)
174 {
175 VkResult result = anv_reloc_list_grow(list, alloc, other->num_relocs);
176 if (result != VK_SUCCESS)
177 return result;
178
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]));
183
184 for (uint32_t i = 0; i < other->num_relocs; i++)
185 list->relocs[i + list->num_relocs].offset += offset;
186
187 list->num_relocs += other->num_relocs;
188 return VK_SUCCESS;
189 }
190
191 /*-----------------------------------------------------------------------*
192 * Functions related to anv_batch
193 *-----------------------------------------------------------------------*/
194
195 void *
196 anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords)
197 {
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);
202 return NULL;
203 }
204 }
205
206 void *p = batch->next;
207
208 batch->next += num_dwords * 4;
209 assert(batch->next <= batch->end);
210
211 return p;
212 }
213
214 uint64_t
215 anv_batch_emit_reloc(struct anv_batch *batch,
216 void *location, struct anv_bo *bo, uint32_t delta)
217 {
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);
222 return 0;
223 }
224
225 return bo->offset + delta;
226 }
227
228 void
229 anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other)
230 {
231 uint32_t size, offset;
232
233 size = other->next - other->start;
234 assert(size % 4 == 0);
235
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);
240 return;
241 }
242 }
243
244 assert(batch->next + size <= batch->end);
245
246 VG(VALGRIND_CHECK_MEM_IS_DEFINED(other->start, size));
247 memcpy(batch->next, other->start, size);
248
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);
254 return;
255 }
256
257 batch->next += size;
258 }
259
260 /*-----------------------------------------------------------------------*
261 * Functions related to anv_batch_bo
262 *-----------------------------------------------------------------------*/
263
264 static VkResult
265 anv_batch_bo_create(struct anv_cmd_buffer *cmd_buffer,
266 struct anv_batch_bo **bbo_out)
267 {
268 VkResult result;
269
270 struct anv_batch_bo *bbo = vk_alloc(&cmd_buffer->pool->alloc, sizeof(*bbo),
271 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
272 if (bbo == NULL)
273 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
274
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)
278 goto fail_alloc;
279
280 result = anv_reloc_list_init(&bbo->relocs, &cmd_buffer->pool->alloc);
281 if (result != VK_SUCCESS)
282 goto fail_bo_alloc;
283
284 *bbo_out = bbo;
285
286 return VK_SUCCESS;
287
288 fail_bo_alloc:
289 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
290 fail_alloc:
291 vk_free(&cmd_buffer->pool->alloc, bbo);
292
293 return result;
294 }
295
296 static VkResult
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)
300 {
301 VkResult result;
302
303 struct anv_batch_bo *bbo = vk_alloc(&cmd_buffer->pool->alloc, sizeof(*bbo),
304 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
305 if (bbo == NULL)
306 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
307
308 result = anv_bo_pool_alloc(&cmd_buffer->device->batch_bo_pool, &bbo->bo,
309 other_bbo->bo.size);
310 if (result != VK_SUCCESS)
311 goto fail_alloc;
312
313 result = anv_reloc_list_init_clone(&bbo->relocs, &cmd_buffer->pool->alloc,
314 &other_bbo->relocs);
315 if (result != VK_SUCCESS)
316 goto fail_bo_alloc;
317
318 bbo->length = other_bbo->length;
319 memcpy(bbo->bo.map, other_bbo->bo.map, other_bbo->length);
320
321 *bbo_out = bbo;
322
323 return VK_SUCCESS;
324
325 fail_bo_alloc:
326 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
327 fail_alloc:
328 vk_free(&cmd_buffer->pool->alloc, bbo);
329
330 return result;
331 }
332
333 static void
334 anv_batch_bo_start(struct anv_batch_bo *bbo, struct anv_batch *batch,
335 size_t batch_padding)
336 {
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;
341 }
342
343 static void
344 anv_batch_bo_continue(struct anv_batch_bo *bbo, struct anv_batch *batch,
345 size_t batch_padding)
346 {
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;
351 }
352
353 static void
354 anv_batch_bo_finish(struct anv_batch_bo *bbo, struct anv_batch *batch)
355 {
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));
359 }
360
361 static VkResult
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)
365 {
366 assert(batch->start == bbo->bo.map);
367 bbo->length = batch->next - batch->start;
368
369 size_t new_size = bbo->bo.size;
370 while (new_size <= bbo->length + aditional + batch_padding)
371 new_size *= 2;
372
373 if (new_size == bbo->bo.size)
374 return VK_SUCCESS;
375
376 struct anv_bo new_bo;
377 VkResult result = anv_bo_pool_alloc(&cmd_buffer->device->batch_bo_pool,
378 &new_bo, new_size);
379 if (result != VK_SUCCESS)
380 return result;
381
382 memcpy(new_bo.map, bbo->bo.map, bbo->length);
383
384 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
385
386 bbo->bo = new_bo;
387 anv_batch_bo_continue(bbo, batch, batch_padding);
388
389 return VK_SUCCESS;
390 }
391
392 static void
393 anv_batch_bo_destroy(struct anv_batch_bo *bbo,
394 struct anv_cmd_buffer *cmd_buffer)
395 {
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);
399 }
400
401 static VkResult
402 anv_batch_bo_list_clone(const struct list_head *list,
403 struct anv_cmd_buffer *cmd_buffer,
404 struct list_head *new_list)
405 {
406 VkResult result = VK_SUCCESS;
407
408 list_inithead(new_list);
409
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)
415 break;
416 list_addtail(&new_bbo->link, new_list);
417
418 if (prev_bbo) {
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.
423 */
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;
427 }
428
429 prev_bbo = new_bbo;
430 }
431
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);
435 }
436
437 return result;
438 }
439
440 /*-----------------------------------------------------------------------*
441 * Functions related to anv_batch_bo
442 *-----------------------------------------------------------------------*/
443
444 static struct anv_batch_bo *
445 anv_cmd_buffer_current_batch_bo(struct anv_cmd_buffer *cmd_buffer)
446 {
447 return LIST_ENTRY(struct anv_batch_bo, cmd_buffer->batch_bos.prev, link);
448 }
449
450 struct anv_address
451 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer *cmd_buffer)
452 {
453 struct anv_state *bt_block = u_vector_head(&cmd_buffer->bt_block_states);
454 return (struct anv_address) {
455 .bo = &anv_binding_table_pool(cmd_buffer->device)->block_pool.bo,
456 .offset = bt_block->offset,
457 };
458 }
459
460 static void
461 emit_batch_buffer_start(struct anv_cmd_buffer *cmd_buffer,
462 struct anv_bo *bo, uint32_t offset)
463 {
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
469 * gens.
470 */
471
472 #define GEN7_MI_BATCH_BUFFER_START_length 2
473 #define GEN7_MI_BATCH_BUFFER_START_length_bias 2
474
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;
479
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 };
486 }
487 }
488
489 static void
490 cmd_buffer_chain_to_batch_bo(struct anv_cmd_buffer *cmd_buffer,
491 struct anv_batch_bo *bbo)
492 {
493 struct anv_batch *batch = &cmd_buffer->batch;
494 struct anv_batch_bo *current_bbo =
495 anv_cmd_buffer_current_batch_bo(cmd_buffer);
496
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.
500 */
501 batch->end += GEN8_MI_BATCH_BUFFER_START_length * 4;
502 assert(batch->end == current_bbo->bo.map + current_bbo->bo.size);
503
504 emit_batch_buffer_start(cmd_buffer, &bbo->bo, 0);
505
506 anv_batch_bo_finish(current_bbo, batch);
507 }
508
509 static VkResult
510 anv_cmd_buffer_chain_batch(struct anv_batch *batch, void *_data)
511 {
512 struct anv_cmd_buffer *cmd_buffer = _data;
513 struct anv_batch_bo *new_bbo;
514
515 VkResult result = anv_batch_bo_create(cmd_buffer, &new_bbo);
516 if (result != VK_SUCCESS)
517 return result;
518
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);
523 }
524 *seen_bbo = new_bbo;
525
526 cmd_buffer_chain_to_batch_bo(cmd_buffer, new_bbo);
527
528 list_addtail(&new_bbo->link, &cmd_buffer->batch_bos);
529
530 anv_batch_bo_start(new_bbo, batch, GEN8_MI_BATCH_BUFFER_START_length * 4);
531
532 return VK_SUCCESS;
533 }
534
535 static VkResult
536 anv_cmd_buffer_grow_batch(struct anv_batch *batch, void *_data)
537 {
538 struct anv_cmd_buffer *cmd_buffer = _data;
539 struct anv_batch_bo *bbo = anv_cmd_buffer_current_batch_bo(cmd_buffer);
540
541 anv_batch_bo_grow(cmd_buffer, bbo, &cmd_buffer->batch, 4096,
542 GEN8_MI_BATCH_BUFFER_START_length * 4);
543
544 return VK_SUCCESS;
545 }
546
547 /** Allocate a binding table
548 *
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.
552 *
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:
559 *
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
567 * global buffer.
568 *
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.
576 *
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.
584 *
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...
588 *
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.
604 *
605 * \see adjust_relocations_from_block_pool()
606 * \see adjust_relocations_too_block_pool()
607 *
608 * \param[in] entries The number of surface state entries the binding
609 * table should be able to hold.
610 *
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.
615 *
616 * \return An anv_state representing the binding table
617 */
618 struct anv_state
619 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
620 uint32_t entries, uint32_t *state_offset)
621 {
622 struct anv_device *device = cmd_buffer->device;
623 struct anv_state_pool *state_pool = &device->surface_state_pool;
624 struct anv_state *bt_block = u_vector_head(&cmd_buffer->bt_block_states);
625 struct anv_state state;
626
627 state.alloc_size = align_u32(entries * 4, 32);
628
629 if (cmd_buffer->bt_next + state.alloc_size > state_pool->block_size)
630 return (struct anv_state) { 0 };
631
632 state.offset = cmd_buffer->bt_next;
633 state.map = anv_binding_table_pool(device)->block_pool.map +
634 bt_block->offset + state.offset;
635
636 cmd_buffer->bt_next += state.alloc_size;
637
638 if (device->instance->physicalDevice.use_softpin) {
639 assert(bt_block->offset >= 0);
640 *state_offset = device->surface_state_pool.block_pool.start_address -
641 device->binding_table_pool.block_pool.start_address - bt_block->offset;
642 } else {
643 assert(bt_block->offset < 0);
644 *state_offset = -bt_block->offset;
645 }
646
647 return state;
648 }
649
650 struct anv_state
651 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer)
652 {
653 struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
654 return anv_state_stream_alloc(&cmd_buffer->surface_state_stream,
655 isl_dev->ss.size, isl_dev->ss.align);
656 }
657
658 struct anv_state
659 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
660 uint32_t size, uint32_t alignment)
661 {
662 return anv_state_stream_alloc(&cmd_buffer->dynamic_state_stream,
663 size, alignment);
664 }
665
666 VkResult
667 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer)
668 {
669 struct anv_state *bt_block = u_vector_add(&cmd_buffer->bt_block_states);
670 if (bt_block == NULL) {
671 anv_batch_set_error(&cmd_buffer->batch, VK_ERROR_OUT_OF_HOST_MEMORY);
672 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
673 }
674
675 *bt_block = anv_binding_table_pool_alloc(cmd_buffer->device);
676 cmd_buffer->bt_next = 0;
677
678 return VK_SUCCESS;
679 }
680
681 VkResult
682 anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
683 {
684 struct anv_batch_bo *batch_bo;
685 VkResult result;
686
687 list_inithead(&cmd_buffer->batch_bos);
688
689 result = anv_batch_bo_create(cmd_buffer, &batch_bo);
690 if (result != VK_SUCCESS)
691 return result;
692
693 list_addtail(&batch_bo->link, &cmd_buffer->batch_bos);
694
695 cmd_buffer->batch.alloc = &cmd_buffer->pool->alloc;
696 cmd_buffer->batch.user_data = cmd_buffer;
697
698 if (cmd_buffer->device->can_chain_batches) {
699 cmd_buffer->batch.extend_cb = anv_cmd_buffer_chain_batch;
700 } else {
701 cmd_buffer->batch.extend_cb = anv_cmd_buffer_grow_batch;
702 }
703
704 anv_batch_bo_start(batch_bo, &cmd_buffer->batch,
705 GEN8_MI_BATCH_BUFFER_START_length * 4);
706
707 int success = u_vector_init(&cmd_buffer->seen_bbos,
708 sizeof(struct anv_bo *),
709 8 * sizeof(struct anv_bo *));
710 if (!success)
711 goto fail_batch_bo;
712
713 *(struct anv_batch_bo **)u_vector_add(&cmd_buffer->seen_bbos) = batch_bo;
714
715 /* u_vector requires power-of-two size elements */
716 unsigned pow2_state_size = util_next_power_of_two(sizeof(struct anv_state));
717 success = u_vector_init(&cmd_buffer->bt_block_states,
718 pow2_state_size, 8 * pow2_state_size);
719 if (!success)
720 goto fail_seen_bbos;
721
722 result = anv_reloc_list_init(&cmd_buffer->surface_relocs,
723 &cmd_buffer->pool->alloc);
724 if (result != VK_SUCCESS)
725 goto fail_bt_blocks;
726 cmd_buffer->last_ss_pool_center = 0;
727
728 result = anv_cmd_buffer_new_binding_table_block(cmd_buffer);
729 if (result != VK_SUCCESS)
730 goto fail_bt_blocks;
731
732 return VK_SUCCESS;
733
734 fail_bt_blocks:
735 u_vector_finish(&cmd_buffer->bt_block_states);
736 fail_seen_bbos:
737 u_vector_finish(&cmd_buffer->seen_bbos);
738 fail_batch_bo:
739 anv_batch_bo_destroy(batch_bo, cmd_buffer);
740
741 return result;
742 }
743
744 void
745 anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
746 {
747 struct anv_state *bt_block;
748 u_vector_foreach(bt_block, &cmd_buffer->bt_block_states)
749 anv_binding_table_pool_free(cmd_buffer->device, *bt_block);
750 u_vector_finish(&cmd_buffer->bt_block_states);
751
752 anv_reloc_list_finish(&cmd_buffer->surface_relocs, &cmd_buffer->pool->alloc);
753
754 u_vector_finish(&cmd_buffer->seen_bbos);
755
756 /* Destroy all of the batch buffers */
757 list_for_each_entry_safe(struct anv_batch_bo, bbo,
758 &cmd_buffer->batch_bos, link) {
759 anv_batch_bo_destroy(bbo, cmd_buffer);
760 }
761 }
762
763 void
764 anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
765 {
766 /* Delete all but the first batch bo */
767 assert(!list_empty(&cmd_buffer->batch_bos));
768 while (cmd_buffer->batch_bos.next != cmd_buffer->batch_bos.prev) {
769 struct anv_batch_bo *bbo = anv_cmd_buffer_current_batch_bo(cmd_buffer);
770 list_del(&bbo->link);
771 anv_batch_bo_destroy(bbo, cmd_buffer);
772 }
773 assert(!list_empty(&cmd_buffer->batch_bos));
774
775 anv_batch_bo_start(anv_cmd_buffer_current_batch_bo(cmd_buffer),
776 &cmd_buffer->batch,
777 GEN8_MI_BATCH_BUFFER_START_length * 4);
778
779 while (u_vector_length(&cmd_buffer->bt_block_states) > 1) {
780 struct anv_state *bt_block = u_vector_remove(&cmd_buffer->bt_block_states);
781 anv_binding_table_pool_free(cmd_buffer->device, *bt_block);
782 }
783 assert(u_vector_length(&cmd_buffer->bt_block_states) == 1);
784 cmd_buffer->bt_next = 0;
785
786 cmd_buffer->surface_relocs.num_relocs = 0;
787 cmd_buffer->last_ss_pool_center = 0;
788
789 /* Reset the list of seen buffers */
790 cmd_buffer->seen_bbos.head = 0;
791 cmd_buffer->seen_bbos.tail = 0;
792
793 *(struct anv_batch_bo **)u_vector_add(&cmd_buffer->seen_bbos) =
794 anv_cmd_buffer_current_batch_bo(cmd_buffer);
795 }
796
797 void
798 anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer)
799 {
800 struct anv_batch_bo *batch_bo = anv_cmd_buffer_current_batch_bo(cmd_buffer);
801
802 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
803 /* When we start a batch buffer, we subtract a certain amount of
804 * padding from the end to ensure that we always have room to emit a
805 * BATCH_BUFFER_START to chain to the next BO. We need to remove
806 * that padding before we end the batch; otherwise, we may end up
807 * with our BATCH_BUFFER_END in another BO.
808 */
809 cmd_buffer->batch.end += GEN8_MI_BATCH_BUFFER_START_length * 4;
810 assert(cmd_buffer->batch.end == batch_bo->bo.map + batch_bo->bo.size);
811
812 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_BATCH_BUFFER_END, bbe);
813
814 /* Round batch up to an even number of dwords. */
815 if ((cmd_buffer->batch.next - cmd_buffer->batch.start) & 4)
816 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_NOOP, noop);
817
818 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_PRIMARY;
819 }
820
821 anv_batch_bo_finish(batch_bo, &cmd_buffer->batch);
822
823 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
824 /* If this is a secondary command buffer, we need to determine the
825 * mode in which it will be executed with vkExecuteCommands. We
826 * determine this statically here so that this stays in sync with the
827 * actual ExecuteCommands implementation.
828 */
829 if (!cmd_buffer->device->can_chain_batches) {
830 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT;
831 } else if ((cmd_buffer->batch_bos.next == cmd_buffer->batch_bos.prev) &&
832 (batch_bo->length < ANV_CMD_BUFFER_BATCH_SIZE / 2)) {
833 /* If the secondary has exactly one batch buffer in its list *and*
834 * that batch buffer is less than half of the maximum size, we're
835 * probably better of simply copying it into our batch.
836 */
837 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_EMIT;
838 } else if (!(cmd_buffer->usage_flags &
839 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
840 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_CHAIN;
841
842 /* When we chain, we need to add an MI_BATCH_BUFFER_START command
843 * with its relocation. In order to handle this we'll increment here
844 * so we can unconditionally decrement right before adding the
845 * MI_BATCH_BUFFER_START command.
846 */
847 batch_bo->relocs.num_relocs++;
848 cmd_buffer->batch.next += GEN8_MI_BATCH_BUFFER_START_length * 4;
849 } else {
850 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN;
851 }
852 }
853 }
854
855 static VkResult
856 anv_cmd_buffer_add_seen_bbos(struct anv_cmd_buffer *cmd_buffer,
857 struct list_head *list)
858 {
859 list_for_each_entry(struct anv_batch_bo, bbo, list, link) {
860 struct anv_batch_bo **bbo_ptr = u_vector_add(&cmd_buffer->seen_bbos);
861 if (bbo_ptr == NULL)
862 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
863
864 *bbo_ptr = bbo;
865 }
866
867 return VK_SUCCESS;
868 }
869
870 void
871 anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
872 struct anv_cmd_buffer *secondary)
873 {
874 switch (secondary->exec_mode) {
875 case ANV_CMD_BUFFER_EXEC_MODE_EMIT:
876 anv_batch_emit_batch(&primary->batch, &secondary->batch);
877 break;
878 case ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT: {
879 struct anv_batch_bo *bbo = anv_cmd_buffer_current_batch_bo(primary);
880 unsigned length = secondary->batch.end - secondary->batch.start;
881 anv_batch_bo_grow(primary, bbo, &primary->batch, length,
882 GEN8_MI_BATCH_BUFFER_START_length * 4);
883 anv_batch_emit_batch(&primary->batch, &secondary->batch);
884 break;
885 }
886 case ANV_CMD_BUFFER_EXEC_MODE_CHAIN: {
887 struct anv_batch_bo *first_bbo =
888 list_first_entry(&secondary->batch_bos, struct anv_batch_bo, link);
889 struct anv_batch_bo *last_bbo =
890 list_last_entry(&secondary->batch_bos, struct anv_batch_bo, link);
891
892 emit_batch_buffer_start(primary, &first_bbo->bo, 0);
893
894 struct anv_batch_bo *this_bbo = anv_cmd_buffer_current_batch_bo(primary);
895 assert(primary->batch.start == this_bbo->bo.map);
896 uint32_t offset = primary->batch.next - primary->batch.start;
897 const uint32_t inst_size = GEN8_MI_BATCH_BUFFER_START_length * 4;
898
899 /* Roll back the previous MI_BATCH_BUFFER_START and its relocation so we
900 * can emit a new command and relocation for the current splice. In
901 * order to handle the initial-use case, we incremented next and
902 * num_relocs in end_batch_buffer() so we can alyways just subtract
903 * here.
904 */
905 last_bbo->relocs.num_relocs--;
906 secondary->batch.next -= inst_size;
907 emit_batch_buffer_start(secondary, &this_bbo->bo, offset);
908 anv_cmd_buffer_add_seen_bbos(primary, &secondary->batch_bos);
909
910 /* After patching up the secondary buffer, we need to clflush the
911 * modified instruction in case we're on a !llc platform. We use a
912 * little loop to handle the case where the instruction crosses a cache
913 * line boundary.
914 */
915 if (!primary->device->info.has_llc) {
916 void *inst = secondary->batch.next - inst_size;
917 void *p = (void *) (((uintptr_t) inst) & ~CACHELINE_MASK);
918 __builtin_ia32_mfence();
919 while (p < secondary->batch.next) {
920 __builtin_ia32_clflush(p);
921 p += CACHELINE_SIZE;
922 }
923 }
924 break;
925 }
926 case ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN: {
927 struct list_head copy_list;
928 VkResult result = anv_batch_bo_list_clone(&secondary->batch_bos,
929 secondary,
930 &copy_list);
931 if (result != VK_SUCCESS)
932 return; /* FIXME */
933
934 anv_cmd_buffer_add_seen_bbos(primary, &copy_list);
935
936 struct anv_batch_bo *first_bbo =
937 list_first_entry(&copy_list, struct anv_batch_bo, link);
938 struct anv_batch_bo *last_bbo =
939 list_last_entry(&copy_list, struct anv_batch_bo, link);
940
941 cmd_buffer_chain_to_batch_bo(primary, first_bbo);
942
943 list_splicetail(&copy_list, &primary->batch_bos);
944
945 anv_batch_bo_continue(last_bbo, &primary->batch,
946 GEN8_MI_BATCH_BUFFER_START_length * 4);
947 break;
948 }
949 default:
950 assert(!"Invalid execution mode");
951 }
952
953 anv_reloc_list_append(&primary->surface_relocs, &primary->pool->alloc,
954 &secondary->surface_relocs, 0);
955 }
956
957 struct anv_execbuf {
958 struct drm_i915_gem_execbuffer2 execbuf;
959
960 struct drm_i915_gem_exec_object2 * objects;
961 uint32_t bo_count;
962 struct anv_bo ** bos;
963
964 /* Allocated length of the 'objects' and 'bos' arrays */
965 uint32_t array_length;
966
967 uint32_t fence_count;
968 uint32_t fence_array_length;
969 struct drm_i915_gem_exec_fence * fences;
970 struct anv_syncobj ** syncobjs;
971 };
972
973 static void
974 anv_execbuf_init(struct anv_execbuf *exec)
975 {
976 memset(exec, 0, sizeof(*exec));
977 }
978
979 static void
980 anv_execbuf_finish(struct anv_execbuf *exec,
981 const VkAllocationCallbacks *alloc)
982 {
983 vk_free(alloc, exec->objects);
984 vk_free(alloc, exec->bos);
985 vk_free(alloc, exec->fences);
986 vk_free(alloc, exec->syncobjs);
987 }
988
989 static VkResult
990 anv_execbuf_add_bo(struct anv_execbuf *exec,
991 struct anv_bo *bo,
992 struct anv_reloc_list *relocs,
993 uint32_t extra_flags,
994 const VkAllocationCallbacks *alloc)
995 {
996 struct drm_i915_gem_exec_object2 *obj = NULL;
997
998 if (bo->index < exec->bo_count && exec->bos[bo->index] == bo)
999 obj = &exec->objects[bo->index];
1000
1001 if (obj == NULL) {
1002 /* We've never seen this one before. Add it to the list and assign
1003 * an id that we can use later.
1004 */
1005 if (exec->bo_count >= exec->array_length) {
1006 uint32_t new_len = exec->objects ? exec->array_length * 2 : 64;
1007
1008 struct drm_i915_gem_exec_object2 *new_objects =
1009 vk_alloc(alloc, new_len * sizeof(*new_objects),
1010 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
1011 if (new_objects == NULL)
1012 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1013
1014 struct anv_bo **new_bos =
1015 vk_alloc(alloc, new_len * sizeof(*new_bos),
1016 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
1017 if (new_bos == NULL) {
1018 vk_free(alloc, new_objects);
1019 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1020 }
1021
1022 if (exec->objects) {
1023 memcpy(new_objects, exec->objects,
1024 exec->bo_count * sizeof(*new_objects));
1025 memcpy(new_bos, exec->bos,
1026 exec->bo_count * sizeof(*new_bos));
1027 }
1028
1029 vk_free(alloc, exec->objects);
1030 vk_free(alloc, exec->bos);
1031
1032 exec->objects = new_objects;
1033 exec->bos = new_bos;
1034 exec->array_length = new_len;
1035 }
1036
1037 assert(exec->bo_count < exec->array_length);
1038
1039 bo->index = exec->bo_count++;
1040 obj = &exec->objects[bo->index];
1041 exec->bos[bo->index] = bo;
1042
1043 obj->handle = bo->gem_handle;
1044 obj->relocation_count = 0;
1045 obj->relocs_ptr = 0;
1046 obj->alignment = 0;
1047 obj->offset = bo->offset;
1048 obj->flags = bo->flags | extra_flags;
1049 obj->rsvd1 = 0;
1050 obj->rsvd2 = 0;
1051 }
1052
1053 if (relocs != NULL && obj->relocation_count == 0) {
1054 /* This is the first time we've ever seen a list of relocations for
1055 * this BO. Go ahead and set the relocations and then walk the list
1056 * of relocations and add them all.
1057 */
1058 obj->relocation_count = relocs->num_relocs;
1059 obj->relocs_ptr = (uintptr_t) relocs->relocs;
1060
1061 for (size_t i = 0; i < relocs->num_relocs; i++) {
1062 VkResult result;
1063
1064 /* A quick sanity check on relocations */
1065 assert(relocs->relocs[i].offset < bo->size);
1066 result = anv_execbuf_add_bo(exec, relocs->reloc_bos[i], NULL,
1067 extra_flags, alloc);
1068
1069 if (result != VK_SUCCESS)
1070 return result;
1071 }
1072 }
1073
1074 return VK_SUCCESS;
1075 }
1076
1077 static VkResult
1078 anv_execbuf_add_syncobj(struct anv_execbuf *exec,
1079 uint32_t handle, uint32_t flags,
1080 const VkAllocationCallbacks *alloc)
1081 {
1082 assert(flags != 0);
1083
1084 if (exec->fence_count >= exec->fence_array_length) {
1085 uint32_t new_len = MAX2(exec->fence_array_length * 2, 64);
1086
1087 exec->fences = vk_realloc(alloc, exec->fences,
1088 new_len * sizeof(*exec->fences),
1089 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
1090 if (exec->fences == NULL)
1091 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
1092
1093 exec->fence_array_length = new_len;
1094 }
1095
1096 exec->fences[exec->fence_count] = (struct drm_i915_gem_exec_fence) {
1097 .handle = handle,
1098 .flags = flags,
1099 };
1100
1101 exec->fence_count++;
1102
1103 return VK_SUCCESS;
1104 }
1105
1106 static void
1107 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer *cmd_buffer,
1108 struct anv_reloc_list *list)
1109 {
1110 for (size_t i = 0; i < list->num_relocs; i++)
1111 list->relocs[i].target_handle = list->reloc_bos[i]->index;
1112 }
1113
1114 static void
1115 adjust_relocations_from_state_pool(struct anv_state_pool *pool,
1116 struct anv_reloc_list *relocs,
1117 uint32_t last_pool_center_bo_offset)
1118 {
1119 assert(last_pool_center_bo_offset <= pool->block_pool.center_bo_offset);
1120 uint32_t delta = pool->block_pool.center_bo_offset - last_pool_center_bo_offset;
1121
1122 for (size_t i = 0; i < relocs->num_relocs; i++) {
1123 /* All of the relocations from this block pool to other BO's should
1124 * have been emitted relative to the surface block pool center. We
1125 * need to add the center offset to make them relative to the
1126 * beginning of the actual GEM bo.
1127 */
1128 relocs->relocs[i].offset += delta;
1129 }
1130 }
1131
1132 static void
1133 adjust_relocations_to_state_pool(struct anv_state_pool *pool,
1134 struct anv_bo *from_bo,
1135 struct anv_reloc_list *relocs,
1136 uint32_t last_pool_center_bo_offset)
1137 {
1138 assert(last_pool_center_bo_offset <= pool->block_pool.center_bo_offset);
1139 uint32_t delta = pool->block_pool.center_bo_offset - last_pool_center_bo_offset;
1140
1141 /* When we initially emit relocations into a block pool, we don't
1142 * actually know what the final center_bo_offset will be so we just emit
1143 * it as if center_bo_offset == 0. Now that we know what the center
1144 * offset is, we need to walk the list of relocations and adjust any
1145 * relocations that point to the pool bo with the correct offset.
1146 */
1147 for (size_t i = 0; i < relocs->num_relocs; i++) {
1148 if (relocs->reloc_bos[i] == &pool->block_pool.bo) {
1149 /* Adjust the delta value in the relocation to correctly
1150 * correspond to the new delta. Initially, this value may have
1151 * been negative (if treated as unsigned), but we trust in
1152 * uint32_t roll-over to fix that for us at this point.
1153 */
1154 relocs->relocs[i].delta += delta;
1155
1156 /* Since the delta has changed, we need to update the actual
1157 * relocated value with the new presumed value. This function
1158 * should only be called on batch buffers, so we know it isn't in
1159 * use by the GPU at the moment.
1160 */
1161 assert(relocs->relocs[i].offset < from_bo->size);
1162 write_reloc(pool->block_pool.device,
1163 from_bo->map + relocs->relocs[i].offset,
1164 relocs->relocs[i].presumed_offset +
1165 relocs->relocs[i].delta, false);
1166 }
1167 }
1168 }
1169
1170 static void
1171 anv_reloc_list_apply(struct anv_device *device,
1172 struct anv_reloc_list *list,
1173 struct anv_bo *bo,
1174 bool always_relocate)
1175 {
1176 for (size_t i = 0; i < list->num_relocs; i++) {
1177 struct anv_bo *target_bo = list->reloc_bos[i];
1178 if (list->relocs[i].presumed_offset == target_bo->offset &&
1179 !always_relocate)
1180 continue;
1181
1182 void *p = bo->map + list->relocs[i].offset;
1183 write_reloc(device, p, target_bo->offset + list->relocs[i].delta, true);
1184 list->relocs[i].presumed_offset = target_bo->offset;
1185 }
1186 }
1187
1188 /**
1189 * This function applies the relocation for a command buffer and writes the
1190 * actual addresses into the buffers as per what we were told by the kernel on
1191 * the previous execbuf2 call. This should be safe to do because, for each
1192 * relocated address, we have two cases:
1193 *
1194 * 1) The target BO is inactive (as seen by the kernel). In this case, it is
1195 * not in use by the GPU so updating the address is 100% ok. It won't be
1196 * in-use by the GPU (from our context) again until the next execbuf2
1197 * happens. If the kernel decides to move it in the next execbuf2, it
1198 * will have to do the relocations itself, but that's ok because it should
1199 * have all of the information needed to do so.
1200 *
1201 * 2) The target BO is active (as seen by the kernel). In this case, it
1202 * hasn't moved since the last execbuffer2 call because GTT shuffling
1203 * *only* happens when the BO is idle. (From our perspective, it only
1204 * happens inside the execbuffer2 ioctl, but the shuffling may be
1205 * triggered by another ioctl, with full-ppgtt this is limited to only
1206 * execbuffer2 ioctls on the same context, or memory pressure.) Since the
1207 * target BO hasn't moved, our anv_bo::offset exactly matches the BO's GTT
1208 * address and the relocated value we are writing into the BO will be the
1209 * same as the value that is already there.
1210 *
1211 * There is also a possibility that the target BO is active but the exact
1212 * RENDER_SURFACE_STATE object we are writing the relocation into isn't in
1213 * use. In this case, the address currently in the RENDER_SURFACE_STATE
1214 * may be stale but it's still safe to write the relocation because that
1215 * particular RENDER_SURFACE_STATE object isn't in-use by the GPU and
1216 * won't be until the next execbuf2 call.
1217 *
1218 * By doing relocations on the CPU, we can tell the kernel that it doesn't
1219 * need to bother. We want to do this because the surface state buffer is
1220 * used by every command buffer so, if the kernel does the relocations, it
1221 * will always be busy and the kernel will always stall. This is also
1222 * probably the fastest mechanism for doing relocations since the kernel would
1223 * have to make a full copy of all the relocations lists.
1224 */
1225 static bool
1226 relocate_cmd_buffer(struct anv_cmd_buffer *cmd_buffer,
1227 struct anv_execbuf *exec)
1228 {
1229 static int userspace_relocs = -1;
1230 if (userspace_relocs < 0)
1231 userspace_relocs = env_var_as_boolean("ANV_USERSPACE_RELOCS", true);
1232 if (!userspace_relocs)
1233 return false;
1234
1235 /* First, we have to check to see whether or not we can even do the
1236 * relocation. New buffers which have never been submitted to the kernel
1237 * don't have a valid offset so we need to let the kernel do relocations so
1238 * that we can get offsets for them. On future execbuf2 calls, those
1239 * buffers will have offsets and we will be able to skip relocating.
1240 * Invalid offsets are indicated by anv_bo::offset == (uint64_t)-1.
1241 */
1242 for (uint32_t i = 0; i < exec->bo_count; i++) {
1243 if (exec->bos[i]->offset == (uint64_t)-1)
1244 return false;
1245 }
1246
1247 /* Since surface states are shared between command buffers and we don't
1248 * know what order they will be submitted to the kernel, we don't know
1249 * what address is actually written in the surface state object at any
1250 * given time. The only option is to always relocate them.
1251 */
1252 anv_reloc_list_apply(cmd_buffer->device, &cmd_buffer->surface_relocs,
1253 &cmd_buffer->device->surface_state_pool.block_pool.bo,
1254 true /* always relocate surface states */);
1255
1256 /* Since we own all of the batch buffers, we know what values are stored
1257 * in the relocated addresses and only have to update them if the offsets
1258 * have changed.
1259 */
1260 struct anv_batch_bo **bbo;
1261 u_vector_foreach(bbo, &cmd_buffer->seen_bbos) {
1262 anv_reloc_list_apply(cmd_buffer->device,
1263 &(*bbo)->relocs, &(*bbo)->bo, false);
1264 }
1265
1266 for (uint32_t i = 0; i < exec->bo_count; i++)
1267 exec->objects[i].offset = exec->bos[i]->offset;
1268
1269 return true;
1270 }
1271
1272 static VkResult
1273 setup_execbuf_for_cmd_buffer(struct anv_execbuf *execbuf,
1274 struct anv_cmd_buffer *cmd_buffer)
1275 {
1276 struct anv_batch *batch = &cmd_buffer->batch;
1277 struct anv_state_pool *ss_pool =
1278 &cmd_buffer->device->surface_state_pool;
1279
1280 adjust_relocations_from_state_pool(ss_pool, &cmd_buffer->surface_relocs,
1281 cmd_buffer->last_ss_pool_center);
1282 VkResult result = anv_execbuf_add_bo(execbuf, &ss_pool->block_pool.bo,
1283 &cmd_buffer->surface_relocs, 0,
1284 &cmd_buffer->device->alloc);
1285 if (result != VK_SUCCESS)
1286 return result;
1287
1288 /* First, we walk over all of the bos we've seen and add them and their
1289 * relocations to the validate list.
1290 */
1291 struct anv_batch_bo **bbo;
1292 u_vector_foreach(bbo, &cmd_buffer->seen_bbos) {
1293 adjust_relocations_to_state_pool(ss_pool, &(*bbo)->bo, &(*bbo)->relocs,
1294 cmd_buffer->last_ss_pool_center);
1295
1296 result = anv_execbuf_add_bo(execbuf, &(*bbo)->bo, &(*bbo)->relocs, 0,
1297 &cmd_buffer->device->alloc);
1298 if (result != VK_SUCCESS)
1299 return result;
1300 }
1301
1302 /* Now that we've adjusted all of the surface state relocations, we need to
1303 * record the surface state pool center so future executions of the command
1304 * buffer can adjust correctly.
1305 */
1306 cmd_buffer->last_ss_pool_center = ss_pool->block_pool.center_bo_offset;
1307
1308 struct anv_batch_bo *first_batch_bo =
1309 list_first_entry(&cmd_buffer->batch_bos, struct anv_batch_bo, link);
1310
1311 /* The kernel requires that the last entry in the validation list be the
1312 * batch buffer to execute. We can simply swap the element
1313 * corresponding to the first batch_bo in the chain with the last
1314 * element in the list.
1315 */
1316 if (first_batch_bo->bo.index != execbuf->bo_count - 1) {
1317 uint32_t idx = first_batch_bo->bo.index;
1318 uint32_t last_idx = execbuf->bo_count - 1;
1319
1320 struct drm_i915_gem_exec_object2 tmp_obj = execbuf->objects[idx];
1321 assert(execbuf->bos[idx] == &first_batch_bo->bo);
1322
1323 execbuf->objects[idx] = execbuf->objects[last_idx];
1324 execbuf->bos[idx] = execbuf->bos[last_idx];
1325 execbuf->bos[idx]->index = idx;
1326
1327 execbuf->objects[last_idx] = tmp_obj;
1328 execbuf->bos[last_idx] = &first_batch_bo->bo;
1329 first_batch_bo->bo.index = last_idx;
1330 }
1331
1332 /* Now we go through and fixup all of the relocation lists to point to
1333 * the correct indices in the object array. We have to do this after we
1334 * reorder the list above as some of the indices may have changed.
1335 */
1336 u_vector_foreach(bbo, &cmd_buffer->seen_bbos)
1337 anv_cmd_buffer_process_relocs(cmd_buffer, &(*bbo)->relocs);
1338
1339 anv_cmd_buffer_process_relocs(cmd_buffer, &cmd_buffer->surface_relocs);
1340
1341 if (!cmd_buffer->device->info.has_llc) {
1342 __builtin_ia32_mfence();
1343 u_vector_foreach(bbo, &cmd_buffer->seen_bbos) {
1344 for (uint32_t i = 0; i < (*bbo)->length; i += CACHELINE_SIZE)
1345 __builtin_ia32_clflush((*bbo)->bo.map + i);
1346 }
1347 }
1348
1349 execbuf->execbuf = (struct drm_i915_gem_execbuffer2) {
1350 .buffers_ptr = (uintptr_t) execbuf->objects,
1351 .buffer_count = execbuf->bo_count,
1352 .batch_start_offset = 0,
1353 .batch_len = batch->next - batch->start,
1354 .cliprects_ptr = 0,
1355 .num_cliprects = 0,
1356 .DR1 = 0,
1357 .DR4 = 0,
1358 .flags = I915_EXEC_HANDLE_LUT | I915_EXEC_RENDER,
1359 .rsvd1 = cmd_buffer->device->context_id,
1360 .rsvd2 = 0,
1361 };
1362
1363 if (relocate_cmd_buffer(cmd_buffer, execbuf)) {
1364 /* If we were able to successfully relocate everything, tell the kernel
1365 * that it can skip doing relocations. The requirement for using
1366 * NO_RELOC is:
1367 *
1368 * 1) The addresses written in the objects must match the corresponding
1369 * reloc.presumed_offset which in turn must match the corresponding
1370 * execobject.offset.
1371 *
1372 * 2) To avoid stalling, execobject.offset should match the current
1373 * address of that object within the active context.
1374 *
1375 * In order to satisfy all of the invariants that make userspace
1376 * relocations to be safe (see relocate_cmd_buffer()), we need to
1377 * further ensure that the addresses we use match those used by the
1378 * kernel for the most recent execbuf2.
1379 *
1380 * The kernel may still choose to do relocations anyway if something has
1381 * moved in the GTT. In this case, the relocation list still needs to be
1382 * valid. All relocations on the batch buffers are already valid and
1383 * kept up-to-date. For surface state relocations, by applying the
1384 * relocations in relocate_cmd_buffer, we ensured that the address in
1385 * the RENDER_SURFACE_STATE matches presumed_offset, so it should be
1386 * safe for the kernel to relocate them as needed.
1387 */
1388 execbuf->execbuf.flags |= I915_EXEC_NO_RELOC;
1389 } else {
1390 /* In the case where we fall back to doing kernel relocations, we need
1391 * to ensure that the relocation list is valid. All relocations on the
1392 * batch buffers are already valid and kept up-to-date. Since surface
1393 * states are shared between command buffers and we don't know what
1394 * order they will be submitted to the kernel, we don't know what
1395 * address is actually written in the surface state object at any given
1396 * time. The only option is to set a bogus presumed offset and let the
1397 * kernel relocate them.
1398 */
1399 for (size_t i = 0; i < cmd_buffer->surface_relocs.num_relocs; i++)
1400 cmd_buffer->surface_relocs.relocs[i].presumed_offset = -1;
1401 }
1402
1403 return VK_SUCCESS;
1404 }
1405
1406 static VkResult
1407 setup_empty_execbuf(struct anv_execbuf *execbuf, struct anv_device *device)
1408 {
1409 VkResult result = anv_execbuf_add_bo(execbuf, &device->trivial_batch_bo,
1410 NULL, 0, &device->alloc);
1411 if (result != VK_SUCCESS)
1412 return result;
1413
1414 execbuf->execbuf = (struct drm_i915_gem_execbuffer2) {
1415 .buffers_ptr = (uintptr_t) execbuf->objects,
1416 .buffer_count = execbuf->bo_count,
1417 .batch_start_offset = 0,
1418 .batch_len = 8, /* GEN7_MI_BATCH_BUFFER_END and NOOP */
1419 .flags = I915_EXEC_HANDLE_LUT | I915_EXEC_RENDER,
1420 .rsvd1 = device->context_id,
1421 .rsvd2 = 0,
1422 };
1423
1424 return VK_SUCCESS;
1425 }
1426
1427 VkResult
1428 anv_cmd_buffer_execbuf(struct anv_device *device,
1429 struct anv_cmd_buffer *cmd_buffer,
1430 const VkSemaphore *in_semaphores,
1431 uint32_t num_in_semaphores,
1432 const VkSemaphore *out_semaphores,
1433 uint32_t num_out_semaphores,
1434 VkFence _fence)
1435 {
1436 ANV_FROM_HANDLE(anv_fence, fence, _fence);
1437
1438 struct anv_execbuf execbuf;
1439 anv_execbuf_init(&execbuf);
1440
1441 int in_fence = -1;
1442 VkResult result = VK_SUCCESS;
1443 for (uint32_t i = 0; i < num_in_semaphores; i++) {
1444 ANV_FROM_HANDLE(anv_semaphore, semaphore, in_semaphores[i]);
1445 struct anv_semaphore_impl *impl =
1446 semaphore->temporary.type != ANV_SEMAPHORE_TYPE_NONE ?
1447 &semaphore->temporary : &semaphore->permanent;
1448
1449 switch (impl->type) {
1450 case ANV_SEMAPHORE_TYPE_BO:
1451 result = anv_execbuf_add_bo(&execbuf, impl->bo, NULL,
1452 0, &device->alloc);
1453 if (result != VK_SUCCESS)
1454 return result;
1455 break;
1456
1457 case ANV_SEMAPHORE_TYPE_SYNC_FILE:
1458 if (in_fence == -1) {
1459 in_fence = impl->fd;
1460 } else {
1461 int merge = anv_gem_sync_file_merge(device, in_fence, impl->fd);
1462 if (merge == -1)
1463 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE);
1464
1465 close(impl->fd);
1466 close(in_fence);
1467 in_fence = merge;
1468 }
1469
1470 impl->fd = -1;
1471 break;
1472
1473 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ:
1474 result = anv_execbuf_add_syncobj(&execbuf, impl->syncobj,
1475 I915_EXEC_FENCE_WAIT,
1476 &device->alloc);
1477 if (result != VK_SUCCESS)
1478 return result;
1479 break;
1480
1481 default:
1482 break;
1483 }
1484 }
1485
1486 bool need_out_fence = false;
1487 for (uint32_t i = 0; i < num_out_semaphores; i++) {
1488 ANV_FROM_HANDLE(anv_semaphore, semaphore, out_semaphores[i]);
1489
1490 /* Under most circumstances, out fences won't be temporary. However,
1491 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
1492 *
1493 * "If the import is temporary, the implementation must restore the
1494 * semaphore to its prior permanent state after submitting the next
1495 * semaphore wait operation."
1496 *
1497 * The spec says nothing whatsoever about signal operations on
1498 * temporarily imported semaphores so it appears they are allowed.
1499 * There are also CTS tests that require this to work.
1500 */
1501 struct anv_semaphore_impl *impl =
1502 semaphore->temporary.type != ANV_SEMAPHORE_TYPE_NONE ?
1503 &semaphore->temporary : &semaphore->permanent;
1504
1505 switch (impl->type) {
1506 case ANV_SEMAPHORE_TYPE_BO:
1507 result = anv_execbuf_add_bo(&execbuf, impl->bo, NULL,
1508 EXEC_OBJECT_WRITE, &device->alloc);
1509 if (result != VK_SUCCESS)
1510 return result;
1511 break;
1512
1513 case ANV_SEMAPHORE_TYPE_SYNC_FILE:
1514 need_out_fence = true;
1515 break;
1516
1517 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ:
1518 result = anv_execbuf_add_syncobj(&execbuf, impl->syncobj,
1519 I915_EXEC_FENCE_SIGNAL,
1520 &device->alloc);
1521 if (result != VK_SUCCESS)
1522 return result;
1523 break;
1524
1525 default:
1526 break;
1527 }
1528 }
1529
1530 if (fence) {
1531 /* Under most circumstances, out fences won't be temporary. However,
1532 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
1533 *
1534 * "If the import is temporary, the implementation must restore the
1535 * semaphore to its prior permanent state after submitting the next
1536 * semaphore wait operation."
1537 *
1538 * The spec says nothing whatsoever about signal operations on
1539 * temporarily imported semaphores so it appears they are allowed.
1540 * There are also CTS tests that require this to work.
1541 */
1542 struct anv_fence_impl *impl =
1543 fence->temporary.type != ANV_FENCE_TYPE_NONE ?
1544 &fence->temporary : &fence->permanent;
1545
1546 switch (impl->type) {
1547 case ANV_FENCE_TYPE_BO:
1548 result = anv_execbuf_add_bo(&execbuf, &impl->bo.bo, NULL,
1549 EXEC_OBJECT_WRITE, &device->alloc);
1550 if (result != VK_SUCCESS)
1551 return result;
1552 break;
1553
1554 case ANV_FENCE_TYPE_SYNCOBJ:
1555 result = anv_execbuf_add_syncobj(&execbuf, impl->syncobj,
1556 I915_EXEC_FENCE_SIGNAL,
1557 &device->alloc);
1558 if (result != VK_SUCCESS)
1559 return result;
1560 break;
1561
1562 default:
1563 unreachable("Invalid fence type");
1564 }
1565 }
1566
1567 if (cmd_buffer)
1568 result = setup_execbuf_for_cmd_buffer(&execbuf, cmd_buffer);
1569 else
1570 result = setup_empty_execbuf(&execbuf, device);
1571
1572 if (result != VK_SUCCESS)
1573 return result;
1574
1575 if (execbuf.fence_count > 0) {
1576 assert(device->instance->physicalDevice.has_syncobj);
1577 execbuf.execbuf.flags |= I915_EXEC_FENCE_ARRAY;
1578 execbuf.execbuf.num_cliprects = execbuf.fence_count;
1579 execbuf.execbuf.cliprects_ptr = (uintptr_t) execbuf.fences;
1580 }
1581
1582 if (in_fence != -1) {
1583 execbuf.execbuf.flags |= I915_EXEC_FENCE_IN;
1584 execbuf.execbuf.rsvd2 |= (uint32_t)in_fence;
1585 }
1586
1587 if (need_out_fence)
1588 execbuf.execbuf.flags |= I915_EXEC_FENCE_OUT;
1589
1590 result = anv_device_execbuf(device, &execbuf.execbuf, execbuf.bos);
1591
1592 /* Execbuf does not consume the in_fence. It's our job to close it. */
1593 if (in_fence != -1)
1594 close(in_fence);
1595
1596 for (uint32_t i = 0; i < num_in_semaphores; i++) {
1597 ANV_FROM_HANDLE(anv_semaphore, semaphore, in_semaphores[i]);
1598 /* From the Vulkan 1.0.53 spec:
1599 *
1600 * "If the import is temporary, the implementation must restore the
1601 * semaphore to its prior permanent state after submitting the next
1602 * semaphore wait operation."
1603 *
1604 * This has to happen after the execbuf in case we close any syncobjs in
1605 * the process.
1606 */
1607 anv_semaphore_reset_temporary(device, semaphore);
1608 }
1609
1610 if (fence && fence->permanent.type == ANV_FENCE_TYPE_BO) {
1611 /* BO fences can't be shared, so they can't be temporary. */
1612 assert(fence->temporary.type == ANV_FENCE_TYPE_NONE);
1613
1614 /* Once the execbuf has returned, we need to set the fence state to
1615 * SUBMITTED. We can't do this before calling execbuf because
1616 * anv_GetFenceStatus does take the global device lock before checking
1617 * fence->state.
1618 *
1619 * We set the fence state to SUBMITTED regardless of whether or not the
1620 * execbuf succeeds because we need to ensure that vkWaitForFences() and
1621 * vkGetFenceStatus() return a valid result (VK_ERROR_DEVICE_LOST or
1622 * VK_SUCCESS) in a finite amount of time even if execbuf fails.
1623 */
1624 fence->permanent.bo.state = ANV_BO_FENCE_STATE_SUBMITTED;
1625 }
1626
1627 if (result == VK_SUCCESS && need_out_fence) {
1628 int out_fence = execbuf.execbuf.rsvd2 >> 32;
1629 for (uint32_t i = 0; i < num_out_semaphores; i++) {
1630 ANV_FROM_HANDLE(anv_semaphore, semaphore, out_semaphores[i]);
1631 /* Out fences can't have temporary state because that would imply
1632 * that we imported a sync file and are trying to signal it.
1633 */
1634 assert(semaphore->temporary.type == ANV_SEMAPHORE_TYPE_NONE);
1635 struct anv_semaphore_impl *impl = &semaphore->permanent;
1636
1637 if (impl->type == ANV_SEMAPHORE_TYPE_SYNC_FILE) {
1638 assert(impl->fd == -1);
1639 impl->fd = dup(out_fence);
1640 }
1641 }
1642 close(out_fence);
1643 }
1644
1645 anv_execbuf_finish(&execbuf, &device->alloc);
1646
1647 return result;
1648 }