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