anv: handle allocation failure in anv_batch_emit_dwords()
[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 uint64_t
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 const uint32_t domain =
152 target_bo->is_winsys_bo ? I915_GEM_DOMAIN_RENDER : 0;
153
154 anv_reloc_list_grow(list, alloc, 1);
155 /* TODO: Handle failure */
156
157 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
158 index = list->num_relocs++;
159 list->reloc_bos[index] = target_bo;
160 entry = &list->relocs[index];
161 entry->target_handle = target_bo->gem_handle;
162 entry->delta = delta;
163 entry->offset = offset;
164 entry->presumed_offset = target_bo->offset;
165 entry->read_domains = domain;
166 entry->write_domain = domain;
167 VG(VALGRIND_CHECK_MEM_IS_DEFINED(entry, sizeof(*entry)));
168
169 return target_bo->offset + delta;
170 }
171
172 static void
173 anv_reloc_list_append(struct anv_reloc_list *list,
174 const VkAllocationCallbacks *alloc,
175 struct anv_reloc_list *other, uint32_t offset)
176 {
177 anv_reloc_list_grow(list, alloc, other->num_relocs);
178 /* TODO: Handle failure */
179
180 memcpy(&list->relocs[list->num_relocs], &other->relocs[0],
181 other->num_relocs * sizeof(other->relocs[0]));
182 memcpy(&list->reloc_bos[list->num_relocs], &other->reloc_bos[0],
183 other->num_relocs * sizeof(other->reloc_bos[0]));
184
185 for (uint32_t i = 0; i < other->num_relocs; i++)
186 list->relocs[i + list->num_relocs].offset += offset;
187
188 list->num_relocs += other->num_relocs;
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 return anv_reloc_list_add(batch->relocs, batch->alloc,
219 location - batch->start, bo, delta);
220 }
221
222 void
223 anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other)
224 {
225 uint32_t size, offset;
226
227 size = other->next - other->start;
228 assert(size % 4 == 0);
229
230 if (batch->next + size > batch->end) {
231 VkResult result = batch->extend_cb(batch, batch->user_data);
232 if (result != VK_SUCCESS) {
233 anv_batch_set_error(batch, result);
234 return;
235 }
236 }
237
238 assert(batch->next + size <= batch->end);
239
240 VG(VALGRIND_CHECK_MEM_IS_DEFINED(other->start, size));
241 memcpy(batch->next, other->start, size);
242
243 offset = batch->next - batch->start;
244 anv_reloc_list_append(batch->relocs, batch->alloc,
245 other->relocs, offset);
246
247 batch->next += size;
248 }
249
250 /*-----------------------------------------------------------------------*
251 * Functions related to anv_batch_bo
252 *-----------------------------------------------------------------------*/
253
254 static VkResult
255 anv_batch_bo_create(struct anv_cmd_buffer *cmd_buffer,
256 struct anv_batch_bo **bbo_out)
257 {
258 VkResult result;
259
260 struct anv_batch_bo *bbo = vk_alloc(&cmd_buffer->pool->alloc, sizeof(*bbo),
261 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
262 if (bbo == NULL)
263 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
264
265 result = anv_bo_pool_alloc(&cmd_buffer->device->batch_bo_pool, &bbo->bo,
266 ANV_CMD_BUFFER_BATCH_SIZE);
267 if (result != VK_SUCCESS)
268 goto fail_alloc;
269
270 result = anv_reloc_list_init(&bbo->relocs, &cmd_buffer->pool->alloc);
271 if (result != VK_SUCCESS)
272 goto fail_bo_alloc;
273
274 *bbo_out = bbo;
275
276 return VK_SUCCESS;
277
278 fail_bo_alloc:
279 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
280 fail_alloc:
281 vk_free(&cmd_buffer->pool->alloc, bbo);
282
283 return result;
284 }
285
286 static VkResult
287 anv_batch_bo_clone(struct anv_cmd_buffer *cmd_buffer,
288 const struct anv_batch_bo *other_bbo,
289 struct anv_batch_bo **bbo_out)
290 {
291 VkResult result;
292
293 struct anv_batch_bo *bbo = vk_alloc(&cmd_buffer->pool->alloc, sizeof(*bbo),
294 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
295 if (bbo == NULL)
296 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
297
298 result = anv_bo_pool_alloc(&cmd_buffer->device->batch_bo_pool, &bbo->bo,
299 other_bbo->bo.size);
300 if (result != VK_SUCCESS)
301 goto fail_alloc;
302
303 result = anv_reloc_list_init_clone(&bbo->relocs, &cmd_buffer->pool->alloc,
304 &other_bbo->relocs);
305 if (result != VK_SUCCESS)
306 goto fail_bo_alloc;
307
308 bbo->length = other_bbo->length;
309 memcpy(bbo->bo.map, other_bbo->bo.map, other_bbo->length);
310
311 *bbo_out = bbo;
312
313 return VK_SUCCESS;
314
315 fail_bo_alloc:
316 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
317 fail_alloc:
318 vk_free(&cmd_buffer->pool->alloc, bbo);
319
320 return result;
321 }
322
323 static void
324 anv_batch_bo_start(struct anv_batch_bo *bbo, struct anv_batch *batch,
325 size_t batch_padding)
326 {
327 batch->next = batch->start = bbo->bo.map;
328 batch->end = bbo->bo.map + bbo->bo.size - batch_padding;
329 batch->relocs = &bbo->relocs;
330 bbo->relocs.num_relocs = 0;
331 }
332
333 static void
334 anv_batch_bo_continue(struct anv_batch_bo *bbo, struct anv_batch *batch,
335 size_t batch_padding)
336 {
337 batch->start = bbo->bo.map;
338 batch->next = bbo->bo.map + bbo->length;
339 batch->end = bbo->bo.map + bbo->bo.size - batch_padding;
340 batch->relocs = &bbo->relocs;
341 }
342
343 static void
344 anv_batch_bo_finish(struct anv_batch_bo *bbo, struct anv_batch *batch)
345 {
346 assert(batch->start == bbo->bo.map);
347 bbo->length = batch->next - batch->start;
348 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch->start, bbo->length));
349 }
350
351 static VkResult
352 anv_batch_bo_grow(struct anv_cmd_buffer *cmd_buffer, struct anv_batch_bo *bbo,
353 struct anv_batch *batch, size_t aditional,
354 size_t batch_padding)
355 {
356 assert(batch->start == bbo->bo.map);
357 bbo->length = batch->next - batch->start;
358
359 size_t new_size = bbo->bo.size;
360 while (new_size <= bbo->length + aditional + batch_padding)
361 new_size *= 2;
362
363 if (new_size == bbo->bo.size)
364 return VK_SUCCESS;
365
366 struct anv_bo new_bo;
367 VkResult result = anv_bo_pool_alloc(&cmd_buffer->device->batch_bo_pool,
368 &new_bo, new_size);
369 if (result != VK_SUCCESS)
370 return result;
371
372 memcpy(new_bo.map, bbo->bo.map, bbo->length);
373
374 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
375
376 bbo->bo = new_bo;
377 anv_batch_bo_continue(bbo, batch, batch_padding);
378
379 return VK_SUCCESS;
380 }
381
382 static void
383 anv_batch_bo_destroy(struct anv_batch_bo *bbo,
384 struct anv_cmd_buffer *cmd_buffer)
385 {
386 anv_reloc_list_finish(&bbo->relocs, &cmd_buffer->pool->alloc);
387 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
388 vk_free(&cmd_buffer->pool->alloc, bbo);
389 }
390
391 static VkResult
392 anv_batch_bo_list_clone(const struct list_head *list,
393 struct anv_cmd_buffer *cmd_buffer,
394 struct list_head *new_list)
395 {
396 VkResult result = VK_SUCCESS;
397
398 list_inithead(new_list);
399
400 struct anv_batch_bo *prev_bbo = NULL;
401 list_for_each_entry(struct anv_batch_bo, bbo, list, link) {
402 struct anv_batch_bo *new_bbo = NULL;
403 result = anv_batch_bo_clone(cmd_buffer, bbo, &new_bbo);
404 if (result != VK_SUCCESS)
405 break;
406 list_addtail(&new_bbo->link, new_list);
407
408 if (prev_bbo) {
409 /* As we clone this list of batch_bo's, they chain one to the
410 * other using MI_BATCH_BUFFER_START commands. We need to fix up
411 * those relocations as we go. Fortunately, this is pretty easy
412 * as it will always be the last relocation in the list.
413 */
414 uint32_t last_idx = prev_bbo->relocs.num_relocs - 1;
415 assert(prev_bbo->relocs.reloc_bos[last_idx] == &bbo->bo);
416 prev_bbo->relocs.reloc_bos[last_idx] = &new_bbo->bo;
417 }
418
419 prev_bbo = new_bbo;
420 }
421
422 if (result != VK_SUCCESS) {
423 list_for_each_entry_safe(struct anv_batch_bo, bbo, new_list, link)
424 anv_batch_bo_destroy(bbo, cmd_buffer);
425 }
426
427 return result;
428 }
429
430 /*-----------------------------------------------------------------------*
431 * Functions related to anv_batch_bo
432 *-----------------------------------------------------------------------*/
433
434 static inline struct anv_batch_bo *
435 anv_cmd_buffer_current_batch_bo(struct anv_cmd_buffer *cmd_buffer)
436 {
437 return LIST_ENTRY(struct anv_batch_bo, cmd_buffer->batch_bos.prev, link);
438 }
439
440 struct anv_address
441 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer *cmd_buffer)
442 {
443 return (struct anv_address) {
444 .bo = &cmd_buffer->device->surface_state_block_pool.bo,
445 .offset = *(int32_t *)u_vector_head(&cmd_buffer->bt_blocks),
446 };
447 }
448
449 static void
450 emit_batch_buffer_start(struct anv_cmd_buffer *cmd_buffer,
451 struct anv_bo *bo, uint32_t offset)
452 {
453 /* In gen8+ the address field grew to two dwords to accomodate 48 bit
454 * offsets. The high 16 bits are in the last dword, so we can use the gen8
455 * version in either case, as long as we set the instruction length in the
456 * header accordingly. This means that we always emit three dwords here
457 * and all the padding and adjustment we do in this file works for all
458 * gens.
459 */
460
461 #define GEN7_MI_BATCH_BUFFER_START_length 2
462 #define GEN7_MI_BATCH_BUFFER_START_length_bias 2
463
464 const uint32_t gen7_length =
465 GEN7_MI_BATCH_BUFFER_START_length - GEN7_MI_BATCH_BUFFER_START_length_bias;
466 const uint32_t gen8_length =
467 GEN8_MI_BATCH_BUFFER_START_length - GEN8_MI_BATCH_BUFFER_START_length_bias;
468
469 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_BATCH_BUFFER_START, bbs) {
470 bbs.DWordLength = cmd_buffer->device->info.gen < 8 ?
471 gen7_length : gen8_length;
472 bbs._2ndLevelBatchBuffer = _1stlevelbatch;
473 bbs.AddressSpaceIndicator = ASI_PPGTT;
474 bbs.BatchBufferStartAddress = (struct anv_address) { bo, offset };
475 }
476 }
477
478 static void
479 cmd_buffer_chain_to_batch_bo(struct anv_cmd_buffer *cmd_buffer,
480 struct anv_batch_bo *bbo)
481 {
482 struct anv_batch *batch = &cmd_buffer->batch;
483 struct anv_batch_bo *current_bbo =
484 anv_cmd_buffer_current_batch_bo(cmd_buffer);
485
486 /* We set the end of the batch a little short so we would be sure we
487 * have room for the chaining command. Since we're about to emit the
488 * chaining command, let's set it back where it should go.
489 */
490 batch->end += GEN8_MI_BATCH_BUFFER_START_length * 4;
491 assert(batch->end == current_bbo->bo.map + current_bbo->bo.size);
492
493 emit_batch_buffer_start(cmd_buffer, &bbo->bo, 0);
494
495 anv_batch_bo_finish(current_bbo, batch);
496 }
497
498 static VkResult
499 anv_cmd_buffer_chain_batch(struct anv_batch *batch, void *_data)
500 {
501 struct anv_cmd_buffer *cmd_buffer = _data;
502 struct anv_batch_bo *new_bbo;
503
504 VkResult result = anv_batch_bo_create(cmd_buffer, &new_bbo);
505 if (result != VK_SUCCESS)
506 return result;
507
508 struct anv_batch_bo **seen_bbo = u_vector_add(&cmd_buffer->seen_bbos);
509 if (seen_bbo == NULL) {
510 anv_batch_bo_destroy(new_bbo, cmd_buffer);
511 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
512 }
513 *seen_bbo = new_bbo;
514
515 cmd_buffer_chain_to_batch_bo(cmd_buffer, new_bbo);
516
517 list_addtail(&new_bbo->link, &cmd_buffer->batch_bos);
518
519 anv_batch_bo_start(new_bbo, batch, GEN8_MI_BATCH_BUFFER_START_length * 4);
520
521 return VK_SUCCESS;
522 }
523
524 static VkResult
525 anv_cmd_buffer_grow_batch(struct anv_batch *batch, void *_data)
526 {
527 struct anv_cmd_buffer *cmd_buffer = _data;
528 struct anv_batch_bo *bbo = anv_cmd_buffer_current_batch_bo(cmd_buffer);
529
530 anv_batch_bo_grow(cmd_buffer, bbo, &cmd_buffer->batch, 4096,
531 GEN8_MI_BATCH_BUFFER_START_length * 4);
532
533 return VK_SUCCESS;
534 }
535
536 /** Allocate a binding table
537 *
538 * This function allocates a binding table. This is a bit more complicated
539 * than one would think due to a combination of Vulkan driver design and some
540 * unfortunate hardware restrictions.
541 *
542 * The 3DSTATE_BINDING_TABLE_POINTERS_* packets only have a 16-bit field for
543 * the binding table pointer which means that all binding tables need to live
544 * in the bottom 64k of surface state base address. The way the GL driver has
545 * classically dealt with this restriction is to emit all surface states
546 * on-the-fly into the batch and have a batch buffer smaller than 64k. This
547 * isn't really an option in Vulkan for a couple of reasons:
548 *
549 * 1) In Vulkan, we have growing (or chaining) batches so surface states have
550 * to live in their own buffer and we have to be able to re-emit
551 * STATE_BASE_ADDRESS as needed which requires a full pipeline stall. In
552 * order to avoid emitting STATE_BASE_ADDRESS any more often than needed
553 * (it's not that hard to hit 64k of just binding tables), we allocate
554 * surface state objects up-front when VkImageView is created. In order
555 * for this to work, surface state objects need to be allocated from a
556 * global buffer.
557 *
558 * 2) We tried to design the surface state system in such a way that it's
559 * already ready for bindless texturing. The way bindless texturing works
560 * on our hardware is that you have a big pool of surface state objects
561 * (with its own state base address) and the bindless handles are simply
562 * offsets into that pool. With the architecture we chose, we already
563 * have that pool and it's exactly the same pool that we use for regular
564 * surface states so we should already be ready for bindless.
565 *
566 * 3) For render targets, we need to be able to fill out the surface states
567 * later in vkBeginRenderPass so that we can assign clear colors
568 * correctly. One way to do this would be to just create the surface
569 * state data and then repeatedly copy it into the surface state BO every
570 * time we have to re-emit STATE_BASE_ADDRESS. While this works, it's
571 * rather annoying and just being able to allocate them up-front and
572 * re-use them for the entire render pass.
573 *
574 * While none of these are technically blockers for emitting state on the fly
575 * like we do in GL, the ability to have a single surface state pool is
576 * simplifies things greatly. Unfortunately, it comes at a cost...
577 *
578 * Because of the 64k limitation of 3DSTATE_BINDING_TABLE_POINTERS_*, we can't
579 * place the binding tables just anywhere in surface state base address.
580 * Because 64k isn't a whole lot of space, we can't simply restrict the
581 * surface state buffer to 64k, we have to be more clever. The solution we've
582 * chosen is to have a block pool with a maximum size of 2G that starts at
583 * zero and grows in both directions. All surface states are allocated from
584 * the top of the pool (positive offsets) and we allocate blocks (< 64k) of
585 * binding tables from the bottom of the pool (negative offsets). Every time
586 * we allocate a new binding table block, we set surface state base address to
587 * point to the bottom of the binding table block. This way all of the
588 * binding tables in the block are in the bottom 64k of surface state base
589 * address. When we fill out the binding table, we add the distance between
590 * the bottom of our binding table block and zero of the block pool to the
591 * surface state offsets so that they are correct relative to out new surface
592 * state base address at the bottom of the binding table block.
593 *
594 * \see adjust_relocations_from_block_pool()
595 * \see adjust_relocations_too_block_pool()
596 *
597 * \param[in] entries The number of surface state entries the binding
598 * table should be able to hold.
599 *
600 * \param[out] state_offset The offset surface surface state base address
601 * where the surface states live. This must be
602 * added to the surface state offset when it is
603 * written into the binding table entry.
604 *
605 * \return An anv_state representing the binding table
606 */
607 struct anv_state
608 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
609 uint32_t entries, uint32_t *state_offset)
610 {
611 struct anv_block_pool *block_pool =
612 &cmd_buffer->device->surface_state_block_pool;
613 int32_t *bt_block = u_vector_head(&cmd_buffer->bt_blocks);
614 struct anv_state state;
615
616 state.alloc_size = align_u32(entries * 4, 32);
617
618 if (cmd_buffer->bt_next + state.alloc_size > block_pool->block_size)
619 return (struct anv_state) { 0 };
620
621 state.offset = cmd_buffer->bt_next;
622 state.map = block_pool->map + *bt_block + state.offset;
623
624 cmd_buffer->bt_next += state.alloc_size;
625
626 assert(*bt_block < 0);
627 *state_offset = -(*bt_block);
628
629 return state;
630 }
631
632 struct anv_state
633 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer)
634 {
635 struct isl_device *isl_dev = &cmd_buffer->device->isl_dev;
636 return anv_state_stream_alloc(&cmd_buffer->surface_state_stream,
637 isl_dev->ss.size, isl_dev->ss.align);
638 }
639
640 struct anv_state
641 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
642 uint32_t size, uint32_t alignment)
643 {
644 return anv_state_stream_alloc(&cmd_buffer->dynamic_state_stream,
645 size, alignment);
646 }
647
648 VkResult
649 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer)
650 {
651 struct anv_block_pool *block_pool =
652 &cmd_buffer->device->surface_state_block_pool;
653
654 int32_t *offset = u_vector_add(&cmd_buffer->bt_blocks);
655 if (offset == NULL)
656 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
657
658 *offset = anv_block_pool_alloc_back(block_pool);
659 cmd_buffer->bt_next = 0;
660
661 return VK_SUCCESS;
662 }
663
664 VkResult
665 anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
666 {
667 struct anv_batch_bo *batch_bo;
668 VkResult result;
669
670 list_inithead(&cmd_buffer->batch_bos);
671
672 result = anv_batch_bo_create(cmd_buffer, &batch_bo);
673 if (result != VK_SUCCESS)
674 return result;
675
676 list_addtail(&batch_bo->link, &cmd_buffer->batch_bos);
677
678 cmd_buffer->batch.alloc = &cmd_buffer->pool->alloc;
679 cmd_buffer->batch.user_data = cmd_buffer;
680
681 if (cmd_buffer->device->can_chain_batches) {
682 cmd_buffer->batch.extend_cb = anv_cmd_buffer_chain_batch;
683 } else {
684 cmd_buffer->batch.extend_cb = anv_cmd_buffer_grow_batch;
685 }
686
687 anv_batch_bo_start(batch_bo, &cmd_buffer->batch,
688 GEN8_MI_BATCH_BUFFER_START_length * 4);
689
690 int success = u_vector_init(&cmd_buffer->seen_bbos,
691 sizeof(struct anv_bo *),
692 8 * sizeof(struct anv_bo *));
693 if (!success)
694 goto fail_batch_bo;
695
696 *(struct anv_batch_bo **)u_vector_add(&cmd_buffer->seen_bbos) = batch_bo;
697
698 success = u_vector_init(&cmd_buffer->bt_blocks, sizeof(int32_t),
699 8 * sizeof(int32_t));
700 if (!success)
701 goto fail_seen_bbos;
702
703 result = anv_reloc_list_init(&cmd_buffer->surface_relocs,
704 &cmd_buffer->pool->alloc);
705 if (result != VK_SUCCESS)
706 goto fail_bt_blocks;
707 cmd_buffer->last_ss_pool_center = 0;
708
709 anv_cmd_buffer_new_binding_table_block(cmd_buffer);
710
711 return VK_SUCCESS;
712
713 fail_bt_blocks:
714 u_vector_finish(&cmd_buffer->bt_blocks);
715 fail_seen_bbos:
716 u_vector_finish(&cmd_buffer->seen_bbos);
717 fail_batch_bo:
718 anv_batch_bo_destroy(batch_bo, cmd_buffer);
719
720 return result;
721 }
722
723 void
724 anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
725 {
726 int32_t *bt_block;
727 u_vector_foreach(bt_block, &cmd_buffer->bt_blocks) {
728 anv_block_pool_free(&cmd_buffer->device->surface_state_block_pool,
729 *bt_block);
730 }
731 u_vector_finish(&cmd_buffer->bt_blocks);
732
733 anv_reloc_list_finish(&cmd_buffer->surface_relocs, &cmd_buffer->pool->alloc);
734
735 u_vector_finish(&cmd_buffer->seen_bbos);
736
737 /* Destroy all of the batch buffers */
738 list_for_each_entry_safe(struct anv_batch_bo, bbo,
739 &cmd_buffer->batch_bos, link) {
740 anv_batch_bo_destroy(bbo, cmd_buffer);
741 }
742 }
743
744 void
745 anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
746 {
747 /* Delete all but the first batch bo */
748 assert(!list_empty(&cmd_buffer->batch_bos));
749 while (cmd_buffer->batch_bos.next != cmd_buffer->batch_bos.prev) {
750 struct anv_batch_bo *bbo = anv_cmd_buffer_current_batch_bo(cmd_buffer);
751 list_del(&bbo->link);
752 anv_batch_bo_destroy(bbo, cmd_buffer);
753 }
754 assert(!list_empty(&cmd_buffer->batch_bos));
755
756 anv_batch_bo_start(anv_cmd_buffer_current_batch_bo(cmd_buffer),
757 &cmd_buffer->batch,
758 GEN8_MI_BATCH_BUFFER_START_length * 4);
759
760 while (u_vector_length(&cmd_buffer->bt_blocks) > 1) {
761 int32_t *bt_block = u_vector_remove(&cmd_buffer->bt_blocks);
762 anv_block_pool_free(&cmd_buffer->device->surface_state_block_pool,
763 *bt_block);
764 }
765 assert(u_vector_length(&cmd_buffer->bt_blocks) == 1);
766 cmd_buffer->bt_next = 0;
767
768 cmd_buffer->surface_relocs.num_relocs = 0;
769 cmd_buffer->last_ss_pool_center = 0;
770
771 /* Reset the list of seen buffers */
772 cmd_buffer->seen_bbos.head = 0;
773 cmd_buffer->seen_bbos.tail = 0;
774
775 *(struct anv_batch_bo **)u_vector_add(&cmd_buffer->seen_bbos) =
776 anv_cmd_buffer_current_batch_bo(cmd_buffer);
777 }
778
779 void
780 anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer)
781 {
782 struct anv_batch_bo *batch_bo = anv_cmd_buffer_current_batch_bo(cmd_buffer);
783
784 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
785 /* When we start a batch buffer, we subtract a certain amount of
786 * padding from the end to ensure that we always have room to emit a
787 * BATCH_BUFFER_START to chain to the next BO. We need to remove
788 * that padding before we end the batch; otherwise, we may end up
789 * with our BATCH_BUFFER_END in another BO.
790 */
791 cmd_buffer->batch.end += GEN8_MI_BATCH_BUFFER_START_length * 4;
792 assert(cmd_buffer->batch.end == batch_bo->bo.map + batch_bo->bo.size);
793
794 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_BATCH_BUFFER_END, bbe);
795
796 /* Round batch up to an even number of dwords. */
797 if ((cmd_buffer->batch.next - cmd_buffer->batch.start) & 4)
798 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_NOOP, noop);
799
800 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_PRIMARY;
801 }
802
803 anv_batch_bo_finish(batch_bo, &cmd_buffer->batch);
804
805 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
806 /* If this is a secondary command buffer, we need to determine the
807 * mode in which it will be executed with vkExecuteCommands. We
808 * determine this statically here so that this stays in sync with the
809 * actual ExecuteCommands implementation.
810 */
811 if (!cmd_buffer->device->can_chain_batches) {
812 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT;
813 } else if ((cmd_buffer->batch_bos.next == cmd_buffer->batch_bos.prev) &&
814 (batch_bo->length < ANV_CMD_BUFFER_BATCH_SIZE / 2)) {
815 /* If the secondary has exactly one batch buffer in its list *and*
816 * that batch buffer is less than half of the maximum size, we're
817 * probably better of simply copying it into our batch.
818 */
819 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_EMIT;
820 } else if (!(cmd_buffer->usage_flags &
821 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
822 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_CHAIN;
823
824 /* When we chain, we need to add an MI_BATCH_BUFFER_START command
825 * with its relocation. In order to handle this we'll increment here
826 * so we can unconditionally decrement right before adding the
827 * MI_BATCH_BUFFER_START command.
828 */
829 batch_bo->relocs.num_relocs++;
830 cmd_buffer->batch.next += GEN8_MI_BATCH_BUFFER_START_length * 4;
831 } else {
832 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN;
833 }
834 }
835 }
836
837 static inline VkResult
838 anv_cmd_buffer_add_seen_bbos(struct anv_cmd_buffer *cmd_buffer,
839 struct list_head *list)
840 {
841 list_for_each_entry(struct anv_batch_bo, bbo, list, link) {
842 struct anv_batch_bo **bbo_ptr = u_vector_add(&cmd_buffer->seen_bbos);
843 if (bbo_ptr == NULL)
844 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
845
846 *bbo_ptr = bbo;
847 }
848
849 return VK_SUCCESS;
850 }
851
852 void
853 anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
854 struct anv_cmd_buffer *secondary)
855 {
856 switch (secondary->exec_mode) {
857 case ANV_CMD_BUFFER_EXEC_MODE_EMIT:
858 anv_batch_emit_batch(&primary->batch, &secondary->batch);
859 break;
860 case ANV_CMD_BUFFER_EXEC_MODE_GROW_AND_EMIT: {
861 struct anv_batch_bo *bbo = anv_cmd_buffer_current_batch_bo(primary);
862 unsigned length = secondary->batch.end - secondary->batch.start;
863 anv_batch_bo_grow(primary, bbo, &primary->batch, length,
864 GEN8_MI_BATCH_BUFFER_START_length * 4);
865 anv_batch_emit_batch(&primary->batch, &secondary->batch);
866 break;
867 }
868 case ANV_CMD_BUFFER_EXEC_MODE_CHAIN: {
869 struct anv_batch_bo *first_bbo =
870 list_first_entry(&secondary->batch_bos, struct anv_batch_bo, link);
871 struct anv_batch_bo *last_bbo =
872 list_last_entry(&secondary->batch_bos, struct anv_batch_bo, link);
873
874 emit_batch_buffer_start(primary, &first_bbo->bo, 0);
875
876 struct anv_batch_bo *this_bbo = anv_cmd_buffer_current_batch_bo(primary);
877 assert(primary->batch.start == this_bbo->bo.map);
878 uint32_t offset = primary->batch.next - primary->batch.start;
879 const uint32_t inst_size = GEN8_MI_BATCH_BUFFER_START_length * 4;
880
881 /* Roll back the previous MI_BATCH_BUFFER_START and its relocation so we
882 * can emit a new command and relocation for the current splice. In
883 * order to handle the initial-use case, we incremented next and
884 * num_relocs in end_batch_buffer() so we can alyways just subtract
885 * here.
886 */
887 last_bbo->relocs.num_relocs--;
888 secondary->batch.next -= inst_size;
889 emit_batch_buffer_start(secondary, &this_bbo->bo, offset);
890 anv_cmd_buffer_add_seen_bbos(primary, &secondary->batch_bos);
891
892 /* After patching up the secondary buffer, we need to clflush the
893 * modified instruction in case we're on a !llc platform. We use a
894 * little loop to handle the case where the instruction crosses a cache
895 * line boundary.
896 */
897 if (!primary->device->info.has_llc) {
898 void *inst = secondary->batch.next - inst_size;
899 void *p = (void *) (((uintptr_t) inst) & ~CACHELINE_MASK);
900 __builtin_ia32_mfence();
901 while (p < secondary->batch.next) {
902 __builtin_ia32_clflush(p);
903 p += CACHELINE_SIZE;
904 }
905 }
906 break;
907 }
908 case ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN: {
909 struct list_head copy_list;
910 VkResult result = anv_batch_bo_list_clone(&secondary->batch_bos,
911 secondary,
912 &copy_list);
913 if (result != VK_SUCCESS)
914 return; /* FIXME */
915
916 anv_cmd_buffer_add_seen_bbos(primary, &copy_list);
917
918 struct anv_batch_bo *first_bbo =
919 list_first_entry(&copy_list, struct anv_batch_bo, link);
920 struct anv_batch_bo *last_bbo =
921 list_last_entry(&copy_list, struct anv_batch_bo, link);
922
923 cmd_buffer_chain_to_batch_bo(primary, first_bbo);
924
925 list_splicetail(&copy_list, &primary->batch_bos);
926
927 anv_batch_bo_continue(last_bbo, &primary->batch,
928 GEN8_MI_BATCH_BUFFER_START_length * 4);
929 break;
930 }
931 default:
932 assert(!"Invalid execution mode");
933 }
934
935 anv_reloc_list_append(&primary->surface_relocs, &primary->pool->alloc,
936 &secondary->surface_relocs, 0);
937 }
938
939 struct anv_execbuf {
940 struct drm_i915_gem_execbuffer2 execbuf;
941
942 struct drm_i915_gem_exec_object2 * objects;
943 uint32_t bo_count;
944 struct anv_bo ** bos;
945
946 /* Allocated length of the 'objects' and 'bos' arrays */
947 uint32_t array_length;
948 };
949
950 static void
951 anv_execbuf_init(struct anv_execbuf *exec)
952 {
953 memset(exec, 0, sizeof(*exec));
954 }
955
956 static void
957 anv_execbuf_finish(struct anv_execbuf *exec,
958 const VkAllocationCallbacks *alloc)
959 {
960 vk_free(alloc, exec->objects);
961 vk_free(alloc, exec->bos);
962 }
963
964 static VkResult
965 anv_execbuf_add_bo(struct anv_execbuf *exec,
966 struct anv_bo *bo,
967 struct anv_reloc_list *relocs,
968 const VkAllocationCallbacks *alloc)
969 {
970 struct drm_i915_gem_exec_object2 *obj = NULL;
971
972 if (bo->index < exec->bo_count && exec->bos[bo->index] == bo)
973 obj = &exec->objects[bo->index];
974
975 if (obj == NULL) {
976 /* We've never seen this one before. Add it to the list and assign
977 * an id that we can use later.
978 */
979 if (exec->bo_count >= exec->array_length) {
980 uint32_t new_len = exec->objects ? exec->array_length * 2 : 64;
981
982 struct drm_i915_gem_exec_object2 *new_objects =
983 vk_alloc(alloc, new_len * sizeof(*new_objects),
984 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
985 if (new_objects == NULL)
986 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
987
988 struct anv_bo **new_bos =
989 vk_alloc(alloc, new_len * sizeof(*new_bos),
990 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND);
991 if (new_bos == NULL) {
992 vk_free(alloc, new_objects);
993 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
994 }
995
996 if (exec->objects) {
997 memcpy(new_objects, exec->objects,
998 exec->bo_count * sizeof(*new_objects));
999 memcpy(new_bos, exec->bos,
1000 exec->bo_count * sizeof(*new_bos));
1001 }
1002
1003 vk_free(alloc, exec->objects);
1004 vk_free(alloc, exec->bos);
1005
1006 exec->objects = new_objects;
1007 exec->bos = new_bos;
1008 exec->array_length = new_len;
1009 }
1010
1011 assert(exec->bo_count < exec->array_length);
1012
1013 bo->index = exec->bo_count++;
1014 obj = &exec->objects[bo->index];
1015 exec->bos[bo->index] = bo;
1016
1017 obj->handle = bo->gem_handle;
1018 obj->relocation_count = 0;
1019 obj->relocs_ptr = 0;
1020 obj->alignment = 0;
1021 obj->offset = bo->offset;
1022 obj->flags = bo->is_winsys_bo ? EXEC_OBJECT_WRITE : 0;
1023 obj->rsvd1 = 0;
1024 obj->rsvd2 = 0;
1025 }
1026
1027 if (relocs != NULL && obj->relocation_count == 0) {
1028 /* This is the first time we've ever seen a list of relocations for
1029 * this BO. Go ahead and set the relocations and then walk the list
1030 * of relocations and add them all.
1031 */
1032 obj->relocation_count = relocs->num_relocs;
1033 obj->relocs_ptr = (uintptr_t) relocs->relocs;
1034
1035 for (size_t i = 0; i < relocs->num_relocs; i++) {
1036 /* A quick sanity check on relocations */
1037 assert(relocs->relocs[i].offset < bo->size);
1038 anv_execbuf_add_bo(exec, relocs->reloc_bos[i], NULL, alloc);
1039 }
1040 }
1041
1042 return VK_SUCCESS;
1043 }
1044
1045 static void
1046 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer *cmd_buffer,
1047 struct anv_reloc_list *list)
1048 {
1049 for (size_t i = 0; i < list->num_relocs; i++)
1050 list->relocs[i].target_handle = list->reloc_bos[i]->index;
1051 }
1052
1053 static void
1054 write_reloc(const struct anv_device *device, void *p, uint64_t v, bool flush)
1055 {
1056 unsigned reloc_size = 0;
1057 if (device->info.gen >= 8) {
1058 /* From the Broadwell PRM Vol. 2a, MI_LOAD_REGISTER_MEM::MemoryAddress:
1059 *
1060 * "This field specifies the address of the memory location where the
1061 * register value specified in the DWord above will read from. The
1062 * address specifies the DWord location of the data. Range =
1063 * GraphicsVirtualAddress[63:2] for a DWord register GraphicsAddress
1064 * [63:48] are ignored by the HW and assumed to be in correct
1065 * canonical form [63:48] == [47]."
1066 */
1067 const int shift = 63 - 47;
1068 reloc_size = sizeof(uint64_t);
1069 *(uint64_t *)p = (((int64_t)v) << shift) >> shift;
1070 } else {
1071 reloc_size = sizeof(uint32_t);
1072 *(uint32_t *)p = v;
1073 }
1074
1075 if (flush && !device->info.has_llc)
1076 anv_flush_range(p, reloc_size);
1077 }
1078
1079 static void
1080 adjust_relocations_from_state_pool(struct anv_block_pool *pool,
1081 struct anv_reloc_list *relocs,
1082 uint32_t last_pool_center_bo_offset)
1083 {
1084 assert(last_pool_center_bo_offset <= pool->center_bo_offset);
1085 uint32_t delta = pool->center_bo_offset - last_pool_center_bo_offset;
1086
1087 for (size_t i = 0; i < relocs->num_relocs; i++) {
1088 /* All of the relocations from this block pool to other BO's should
1089 * have been emitted relative to the surface block pool center. We
1090 * need to add the center offset to make them relative to the
1091 * beginning of the actual GEM bo.
1092 */
1093 relocs->relocs[i].offset += delta;
1094 }
1095 }
1096
1097 static void
1098 adjust_relocations_to_state_pool(struct anv_block_pool *pool,
1099 struct anv_bo *from_bo,
1100 struct anv_reloc_list *relocs,
1101 uint32_t last_pool_center_bo_offset)
1102 {
1103 assert(last_pool_center_bo_offset <= pool->center_bo_offset);
1104 uint32_t delta = pool->center_bo_offset - last_pool_center_bo_offset;
1105
1106 /* When we initially emit relocations into a block pool, we don't
1107 * actually know what the final center_bo_offset will be so we just emit
1108 * it as if center_bo_offset == 0. Now that we know what the center
1109 * offset is, we need to walk the list of relocations and adjust any
1110 * relocations that point to the pool bo with the correct offset.
1111 */
1112 for (size_t i = 0; i < relocs->num_relocs; i++) {
1113 if (relocs->reloc_bos[i] == &pool->bo) {
1114 /* Adjust the delta value in the relocation to correctly
1115 * correspond to the new delta. Initially, this value may have
1116 * been negative (if treated as unsigned), but we trust in
1117 * uint32_t roll-over to fix that for us at this point.
1118 */
1119 relocs->relocs[i].delta += delta;
1120
1121 /* Since the delta has changed, we need to update the actual
1122 * relocated value with the new presumed value. This function
1123 * should only be called on batch buffers, so we know it isn't in
1124 * use by the GPU at the moment.
1125 */
1126 assert(relocs->relocs[i].offset < from_bo->size);
1127 write_reloc(pool->device, from_bo->map + relocs->relocs[i].offset,
1128 relocs->relocs[i].presumed_offset +
1129 relocs->relocs[i].delta, false);
1130 }
1131 }
1132 }
1133
1134 static void
1135 anv_reloc_list_apply(struct anv_device *device,
1136 struct anv_reloc_list *list,
1137 struct anv_bo *bo,
1138 bool always_relocate)
1139 {
1140 for (size_t i = 0; i < list->num_relocs; i++) {
1141 struct anv_bo *target_bo = list->reloc_bos[i];
1142 if (list->relocs[i].presumed_offset == target_bo->offset &&
1143 !always_relocate)
1144 continue;
1145
1146 void *p = bo->map + list->relocs[i].offset;
1147 write_reloc(device, p, target_bo->offset + list->relocs[i].delta, true);
1148 list->relocs[i].presumed_offset = target_bo->offset;
1149 }
1150 }
1151
1152 /**
1153 * This function applies the relocation for a command buffer and writes the
1154 * actual addresses into the buffers as per what we were told by the kernel on
1155 * the previous execbuf2 call. This should be safe to do because, for each
1156 * relocated address, we have two cases:
1157 *
1158 * 1) The target BO is inactive (as seen by the kernel). In this case, it is
1159 * not in use by the GPU so updating the address is 100% ok. It won't be
1160 * in-use by the GPU (from our context) again until the next execbuf2
1161 * happens. If the kernel decides to move it in the next execbuf2, it
1162 * will have to do the relocations itself, but that's ok because it should
1163 * have all of the information needed to do so.
1164 *
1165 * 2) The target BO is active (as seen by the kernel). In this case, it
1166 * hasn't moved since the last execbuffer2 call because GTT shuffling
1167 * *only* happens when the BO is idle. (From our perspective, it only
1168 * happens inside the execbuffer2 ioctl, but the shuffling may be
1169 * triggered by another ioctl, with full-ppgtt this is limited to only
1170 * execbuffer2 ioctls on the same context, or memory pressure.) Since the
1171 * target BO hasn't moved, our anv_bo::offset exactly matches the BO's GTT
1172 * address and the relocated value we are writing into the BO will be the
1173 * same as the value that is already there.
1174 *
1175 * There is also a possibility that the target BO is active but the exact
1176 * RENDER_SURFACE_STATE object we are writing the relocation into isn't in
1177 * use. In this case, the address currently in the RENDER_SURFACE_STATE
1178 * may be stale but it's still safe to write the relocation because that
1179 * particular RENDER_SURFACE_STATE object isn't in-use by the GPU and
1180 * won't be until the next execbuf2 call.
1181 *
1182 * By doing relocations on the CPU, we can tell the kernel that it doesn't
1183 * need to bother. We want to do this because the surface state buffer is
1184 * used by every command buffer so, if the kernel does the relocations, it
1185 * will always be busy and the kernel will always stall. This is also
1186 * probably the fastest mechanism for doing relocations since the kernel would
1187 * have to make a full copy of all the relocations lists.
1188 */
1189 static bool
1190 relocate_cmd_buffer(struct anv_cmd_buffer *cmd_buffer,
1191 struct anv_execbuf *exec)
1192 {
1193 static int userspace_relocs = -1;
1194 if (userspace_relocs < 0)
1195 userspace_relocs = env_var_as_boolean("ANV_USERSPACE_RELOCS", true);
1196 if (!userspace_relocs)
1197 return false;
1198
1199 /* First, we have to check to see whether or not we can even do the
1200 * relocation. New buffers which have never been submitted to the kernel
1201 * don't have a valid offset so we need to let the kernel do relocations so
1202 * that we can get offsets for them. On future execbuf2 calls, those
1203 * buffers will have offsets and we will be able to skip relocating.
1204 * Invalid offsets are indicated by anv_bo::offset == (uint64_t)-1.
1205 */
1206 for (uint32_t i = 0; i < exec->bo_count; i++) {
1207 if (exec->bos[i]->offset == (uint64_t)-1)
1208 return false;
1209 }
1210
1211 /* Since surface states are shared between command buffers and we don't
1212 * know what order they will be submitted to the kernel, we don't know
1213 * what address is actually written in the surface state object at any
1214 * given time. The only option is to always relocate them.
1215 */
1216 anv_reloc_list_apply(cmd_buffer->device, &cmd_buffer->surface_relocs,
1217 &cmd_buffer->device->surface_state_block_pool.bo,
1218 true /* always relocate surface states */);
1219
1220 /* Since we own all of the batch buffers, we know what values are stored
1221 * in the relocated addresses and only have to update them if the offsets
1222 * have changed.
1223 */
1224 struct anv_batch_bo **bbo;
1225 u_vector_foreach(bbo, &cmd_buffer->seen_bbos) {
1226 anv_reloc_list_apply(cmd_buffer->device,
1227 &(*bbo)->relocs, &(*bbo)->bo, false);
1228 }
1229
1230 for (uint32_t i = 0; i < exec->bo_count; i++)
1231 exec->objects[i].offset = exec->bos[i]->offset;
1232
1233 return true;
1234 }
1235
1236 VkResult
1237 anv_cmd_buffer_execbuf(struct anv_device *device,
1238 struct anv_cmd_buffer *cmd_buffer)
1239 {
1240 struct anv_batch *batch = &cmd_buffer->batch;
1241 struct anv_block_pool *ss_pool =
1242 &cmd_buffer->device->surface_state_block_pool;
1243
1244 struct anv_execbuf execbuf;
1245 anv_execbuf_init(&execbuf);
1246
1247 adjust_relocations_from_state_pool(ss_pool, &cmd_buffer->surface_relocs,
1248 cmd_buffer->last_ss_pool_center);
1249 anv_execbuf_add_bo(&execbuf, &ss_pool->bo, &cmd_buffer->surface_relocs,
1250 &cmd_buffer->pool->alloc);
1251
1252 /* First, we walk over all of the bos we've seen and add them and their
1253 * relocations to the validate list.
1254 */
1255 struct anv_batch_bo **bbo;
1256 u_vector_foreach(bbo, &cmd_buffer->seen_bbos) {
1257 adjust_relocations_to_state_pool(ss_pool, &(*bbo)->bo, &(*bbo)->relocs,
1258 cmd_buffer->last_ss_pool_center);
1259
1260 anv_execbuf_add_bo(&execbuf, &(*bbo)->bo, &(*bbo)->relocs,
1261 &cmd_buffer->pool->alloc);
1262 }
1263
1264 /* Now that we've adjusted all of the surface state relocations, we need to
1265 * record the surface state pool center so future executions of the command
1266 * buffer can adjust correctly.
1267 */
1268 cmd_buffer->last_ss_pool_center = ss_pool->center_bo_offset;
1269
1270 struct anv_batch_bo *first_batch_bo =
1271 list_first_entry(&cmd_buffer->batch_bos, struct anv_batch_bo, link);
1272
1273 /* The kernel requires that the last entry in the validation list be the
1274 * batch buffer to execute. We can simply swap the element
1275 * corresponding to the first batch_bo in the chain with the last
1276 * element in the list.
1277 */
1278 if (first_batch_bo->bo.index != execbuf.bo_count - 1) {
1279 uint32_t idx = first_batch_bo->bo.index;
1280 uint32_t last_idx = execbuf.bo_count - 1;
1281
1282 struct drm_i915_gem_exec_object2 tmp_obj = execbuf.objects[idx];
1283 assert(execbuf.bos[idx] == &first_batch_bo->bo);
1284
1285 execbuf.objects[idx] = execbuf.objects[last_idx];
1286 execbuf.bos[idx] = execbuf.bos[last_idx];
1287 execbuf.bos[idx]->index = idx;
1288
1289 execbuf.objects[last_idx] = tmp_obj;
1290 execbuf.bos[last_idx] = &first_batch_bo->bo;
1291 first_batch_bo->bo.index = last_idx;
1292 }
1293
1294 /* Now we go through and fixup all of the relocation lists to point to
1295 * the correct indices in the object array. We have to do this after we
1296 * reorder the list above as some of the indices may have changed.
1297 */
1298 u_vector_foreach(bbo, &cmd_buffer->seen_bbos)
1299 anv_cmd_buffer_process_relocs(cmd_buffer, &(*bbo)->relocs);
1300
1301 anv_cmd_buffer_process_relocs(cmd_buffer, &cmd_buffer->surface_relocs);
1302
1303 if (!cmd_buffer->device->info.has_llc) {
1304 __builtin_ia32_mfence();
1305 u_vector_foreach(bbo, &cmd_buffer->seen_bbos) {
1306 for (uint32_t i = 0; i < (*bbo)->length; i += CACHELINE_SIZE)
1307 __builtin_ia32_clflush((*bbo)->bo.map + i);
1308 }
1309 }
1310
1311 execbuf.execbuf = (struct drm_i915_gem_execbuffer2) {
1312 .buffers_ptr = (uintptr_t) execbuf.objects,
1313 .buffer_count = execbuf.bo_count,
1314 .batch_start_offset = 0,
1315 .batch_len = batch->next - batch->start,
1316 .cliprects_ptr = 0,
1317 .num_cliprects = 0,
1318 .DR1 = 0,
1319 .DR4 = 0,
1320 .flags = I915_EXEC_HANDLE_LUT | I915_EXEC_RENDER |
1321 I915_EXEC_CONSTANTS_REL_GENERAL,
1322 .rsvd1 = cmd_buffer->device->context_id,
1323 .rsvd2 = 0,
1324 };
1325
1326 if (relocate_cmd_buffer(cmd_buffer, &execbuf)) {
1327 /* If we were able to successfully relocate everything, tell the kernel
1328 * that it can skip doing relocations. The requirement for using
1329 * NO_RELOC is:
1330 *
1331 * 1) The addresses written in the objects must match the corresponding
1332 * reloc.presumed_offset which in turn must match the corresponding
1333 * execobject.offset.
1334 *
1335 * 2) To avoid stalling, execobject.offset should match the current
1336 * address of that object within the active context.
1337 *
1338 * In order to satisfy all of the invariants that make userspace
1339 * relocations to be safe (see relocate_cmd_buffer()), we need to
1340 * further ensure that the addresses we use match those used by the
1341 * kernel for the most recent execbuf2.
1342 *
1343 * The kernel may still choose to do relocations anyway if something has
1344 * moved in the GTT. In this case, the relocation list still needs to be
1345 * valid. All relocations on the batch buffers are already valid and
1346 * kept up-to-date. For surface state relocations, by applying the
1347 * relocations in relocate_cmd_buffer, we ensured that the address in
1348 * the RENDER_SURFACE_STATE matches presumed_offset, so it should be
1349 * safe for the kernel to relocate them as needed.
1350 */
1351 execbuf.execbuf.flags |= I915_EXEC_NO_RELOC;
1352 } else {
1353 /* In the case where we fall back to doing kernel relocations, we need
1354 * to ensure that the relocation list is valid. All relocations on the
1355 * batch buffers are already valid and kept up-to-date. Since surface
1356 * states are shared between command buffers and we don't know what
1357 * order they will be submitted to the kernel, we don't know what
1358 * address is actually written in the surface state object at any given
1359 * time. The only option is to set a bogus presumed offset and let the
1360 * kernel relocate them.
1361 */
1362 for (size_t i = 0; i < cmd_buffer->surface_relocs.num_relocs; i++)
1363 cmd_buffer->surface_relocs.relocs[i].presumed_offset = -1;
1364 }
1365
1366 VkResult result = anv_device_execbuf(device, &execbuf.execbuf, execbuf.bos);
1367
1368 anv_execbuf_finish(&execbuf, &cmd_buffer->pool->alloc);
1369
1370 return result;
1371 }