anv: Set correct write domain on window system BOs
[mesa.git] / src / 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 "gen7_pack.h"
33 #include "gen8_pack.h"
34
35 /** \file anv_batch_chain.c
36 *
37 * This file contains functions related to anv_cmd_buffer as a data
38 * structure. This involves everything required to create and destroy
39 * the actual batch buffers as well as link them together and handle
40 * relocations and surface state. It specifically does *not* contain any
41 * handling of actual vkCmd calls beyond vkCmdExecuteCommands.
42 */
43
44 /*-----------------------------------------------------------------------*
45 * Functions related to anv_reloc_list
46 *-----------------------------------------------------------------------*/
47
48 static VkResult
49 anv_reloc_list_init_clone(struct anv_reloc_list *list,
50 const VkAllocationCallbacks *alloc,
51 const struct anv_reloc_list *other_list)
52 {
53 if (other_list) {
54 list->num_relocs = other_list->num_relocs;
55 list->array_length = other_list->array_length;
56 } else {
57 list->num_relocs = 0;
58 list->array_length = 256;
59 }
60
61 list->relocs =
62 anv_alloc(alloc, list->array_length * sizeof(*list->relocs), 8,
63 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
64
65 if (list->relocs == NULL)
66 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
67
68 list->reloc_bos =
69 anv_alloc(alloc, list->array_length * sizeof(*list->reloc_bos), 8,
70 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
71
72 if (list->reloc_bos == NULL) {
73 anv_free(alloc, list->relocs);
74 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
75 }
76
77 if (other_list) {
78 memcpy(list->relocs, other_list->relocs,
79 list->array_length * sizeof(*list->relocs));
80 memcpy(list->reloc_bos, other_list->reloc_bos,
81 list->array_length * sizeof(*list->reloc_bos));
82 }
83
84 return VK_SUCCESS;
85 }
86
87 VkResult
88 anv_reloc_list_init(struct anv_reloc_list *list,
89 const VkAllocationCallbacks *alloc)
90 {
91 return anv_reloc_list_init_clone(list, alloc, NULL);
92 }
93
94 void
95 anv_reloc_list_finish(struct anv_reloc_list *list,
96 const VkAllocationCallbacks *alloc)
97 {
98 anv_free(alloc, list->relocs);
99 anv_free(alloc, list->reloc_bos);
100 }
101
102 static VkResult
103 anv_reloc_list_grow(struct anv_reloc_list *list,
104 const VkAllocationCallbacks *alloc,
105 size_t num_additional_relocs)
106 {
107 if (list->num_relocs + num_additional_relocs <= list->array_length)
108 return VK_SUCCESS;
109
110 size_t new_length = list->array_length * 2;
111 while (new_length < list->num_relocs + num_additional_relocs)
112 new_length *= 2;
113
114 struct drm_i915_gem_relocation_entry *new_relocs =
115 anv_alloc(alloc, new_length * sizeof(*list->relocs), 8,
116 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
117 if (new_relocs == NULL)
118 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
119
120 struct anv_bo **new_reloc_bos =
121 anv_alloc(alloc, new_length * sizeof(*list->reloc_bos), 8,
122 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
123 if (new_relocs == NULL) {
124 anv_free(alloc, new_relocs);
125 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
126 }
127
128 memcpy(new_relocs, list->relocs, list->num_relocs * sizeof(*list->relocs));
129 memcpy(new_reloc_bos, list->reloc_bos,
130 list->num_relocs * sizeof(*list->reloc_bos));
131
132 anv_free(alloc, list->relocs);
133 anv_free(alloc, list->reloc_bos);
134
135 list->array_length = new_length;
136 list->relocs = new_relocs;
137 list->reloc_bos = new_reloc_bos;
138
139 return VK_SUCCESS;
140 }
141
142 uint64_t
143 anv_reloc_list_add(struct anv_reloc_list *list,
144 const VkAllocationCallbacks *alloc,
145 uint32_t offset, struct anv_bo *target_bo, uint32_t delta)
146 {
147 struct drm_i915_gem_relocation_entry *entry;
148 int index;
149
150 const uint32_t domain =
151 target_bo->is_winsys_bo ? I915_GEM_DOMAIN_RENDER : 0;
152
153 anv_reloc_list_grow(list, alloc, 1);
154 /* TODO: Handle failure */
155
156 /* XXX: Can we use I915_EXEC_HANDLE_LUT? */
157 index = list->num_relocs++;
158 list->reloc_bos[index] = target_bo;
159 entry = &list->relocs[index];
160 entry->target_handle = target_bo->gem_handle;
161 entry->delta = delta;
162 entry->offset = offset;
163 entry->presumed_offset = target_bo->offset;
164 entry->read_domains = domain;
165 entry->write_domain = domain;
166 VG(VALGRIND_CHECK_MEM_IS_DEFINED(entry, sizeof(*entry)));
167
168 return target_bo->offset + delta;
169 }
170
171 static void
172 anv_reloc_list_append(struct anv_reloc_list *list,
173 const VkAllocationCallbacks *alloc,
174 struct anv_reloc_list *other, uint32_t offset)
175 {
176 anv_reloc_list_grow(list, alloc, other->num_relocs);
177 /* TODO: Handle failure */
178
179 memcpy(&list->relocs[list->num_relocs], &other->relocs[0],
180 other->num_relocs * sizeof(other->relocs[0]));
181 memcpy(&list->reloc_bos[list->num_relocs], &other->reloc_bos[0],
182 other->num_relocs * sizeof(other->reloc_bos[0]));
183
184 for (uint32_t i = 0; i < other->num_relocs; i++)
185 list->relocs[i + list->num_relocs].offset += offset;
186
187 list->num_relocs += other->num_relocs;
188 }
189
190 /*-----------------------------------------------------------------------*
191 * Functions related to anv_batch
192 *-----------------------------------------------------------------------*/
193
194 void *
195 anv_batch_emit_dwords(struct anv_batch *batch, int num_dwords)
196 {
197 if (batch->next + num_dwords * 4 > batch->end)
198 batch->extend_cb(batch, batch->user_data);
199
200 void *p = batch->next;
201
202 batch->next += num_dwords * 4;
203 assert(batch->next <= batch->end);
204
205 return p;
206 }
207
208 uint64_t
209 anv_batch_emit_reloc(struct anv_batch *batch,
210 void *location, struct anv_bo *bo, uint32_t delta)
211 {
212 return anv_reloc_list_add(batch->relocs, batch->alloc,
213 location - batch->start, bo, delta);
214 }
215
216 void
217 anv_batch_emit_batch(struct anv_batch *batch, struct anv_batch *other)
218 {
219 uint32_t size, offset;
220
221 size = other->next - other->start;
222 assert(size % 4 == 0);
223
224 if (batch->next + size > batch->end)
225 batch->extend_cb(batch, batch->user_data);
226
227 assert(batch->next + size <= batch->end);
228
229 VG(VALGRIND_CHECK_MEM_IS_DEFINED(other->start, size));
230 memcpy(batch->next, other->start, size);
231
232 offset = batch->next - batch->start;
233 anv_reloc_list_append(batch->relocs, batch->alloc,
234 other->relocs, offset);
235
236 batch->next += size;
237 }
238
239 /*-----------------------------------------------------------------------*
240 * Functions related to anv_batch_bo
241 *-----------------------------------------------------------------------*/
242
243 static VkResult
244 anv_batch_bo_create(struct anv_cmd_buffer *cmd_buffer,
245 struct anv_batch_bo **bbo_out)
246 {
247 VkResult result;
248
249 struct anv_batch_bo *bbo = anv_alloc(&cmd_buffer->pool->alloc, sizeof(*bbo),
250 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
251 if (bbo == NULL)
252 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
253
254 result = anv_bo_pool_alloc(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
255 if (result != VK_SUCCESS)
256 goto fail_alloc;
257
258 result = anv_reloc_list_init(&bbo->relocs, &cmd_buffer->pool->alloc);
259 if (result != VK_SUCCESS)
260 goto fail_bo_alloc;
261
262 *bbo_out = bbo;
263
264 return VK_SUCCESS;
265
266 fail_bo_alloc:
267 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
268 fail_alloc:
269 anv_free(&cmd_buffer->pool->alloc, bbo);
270
271 return result;
272 }
273
274 static VkResult
275 anv_batch_bo_clone(struct anv_cmd_buffer *cmd_buffer,
276 const struct anv_batch_bo *other_bbo,
277 struct anv_batch_bo **bbo_out)
278 {
279 VkResult result;
280
281 struct anv_batch_bo *bbo = anv_alloc(&cmd_buffer->pool->alloc, sizeof(*bbo),
282 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
283 if (bbo == NULL)
284 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
285
286 result = anv_bo_pool_alloc(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
287 if (result != VK_SUCCESS)
288 goto fail_alloc;
289
290 result = anv_reloc_list_init_clone(&bbo->relocs, &cmd_buffer->pool->alloc,
291 &other_bbo->relocs);
292 if (result != VK_SUCCESS)
293 goto fail_bo_alloc;
294
295 bbo->length = other_bbo->length;
296 memcpy(bbo->bo.map, other_bbo->bo.map, other_bbo->length);
297
298 bbo->last_ss_pool_bo_offset = other_bbo->last_ss_pool_bo_offset;
299
300 *bbo_out = bbo;
301
302 return VK_SUCCESS;
303
304 fail_bo_alloc:
305 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
306 fail_alloc:
307 anv_free(&cmd_buffer->pool->alloc, bbo);
308
309 return result;
310 }
311
312 static void
313 anv_batch_bo_start(struct anv_batch_bo *bbo, struct anv_batch *batch,
314 size_t batch_padding)
315 {
316 batch->next = batch->start = bbo->bo.map;
317 batch->end = bbo->bo.map + bbo->bo.size - batch_padding;
318 batch->relocs = &bbo->relocs;
319 bbo->last_ss_pool_bo_offset = 0;
320 bbo->relocs.num_relocs = 0;
321 }
322
323 static void
324 anv_batch_bo_continue(struct anv_batch_bo *bbo, struct anv_batch *batch,
325 size_t batch_padding)
326 {
327 batch->start = bbo->bo.map;
328 batch->next = bbo->bo.map + bbo->length;
329 batch->end = bbo->bo.map + bbo->bo.size - batch_padding;
330 batch->relocs = &bbo->relocs;
331 }
332
333 static void
334 anv_batch_bo_finish(struct anv_batch_bo *bbo, struct anv_batch *batch)
335 {
336 assert(batch->start == bbo->bo.map);
337 bbo->length = batch->next - batch->start;
338 VG(VALGRIND_CHECK_MEM_IS_DEFINED(batch->start, bbo->length));
339 }
340
341 static void
342 anv_batch_bo_destroy(struct anv_batch_bo *bbo,
343 struct anv_cmd_buffer *cmd_buffer)
344 {
345 anv_reloc_list_finish(&bbo->relocs, &cmd_buffer->pool->alloc);
346 anv_bo_pool_free(&cmd_buffer->device->batch_bo_pool, &bbo->bo);
347 anv_free(&cmd_buffer->pool->alloc, bbo);
348 }
349
350 static VkResult
351 anv_batch_bo_list_clone(const struct list_head *list,
352 struct anv_cmd_buffer *cmd_buffer,
353 struct list_head *new_list)
354 {
355 VkResult result = VK_SUCCESS;
356
357 list_inithead(new_list);
358
359 struct anv_batch_bo *prev_bbo = NULL;
360 list_for_each_entry(struct anv_batch_bo, bbo, list, link) {
361 struct anv_batch_bo *new_bbo = NULL;
362 result = anv_batch_bo_clone(cmd_buffer, bbo, &new_bbo);
363 if (result != VK_SUCCESS)
364 break;
365 list_addtail(&new_bbo->link, new_list);
366
367 if (prev_bbo) {
368 /* As we clone this list of batch_bo's, they chain one to the
369 * other using MI_BATCH_BUFFER_START commands. We need to fix up
370 * those relocations as we go. Fortunately, this is pretty easy
371 * as it will always be the last relocation in the list.
372 */
373 uint32_t last_idx = prev_bbo->relocs.num_relocs - 1;
374 assert(prev_bbo->relocs.reloc_bos[last_idx] == &bbo->bo);
375 prev_bbo->relocs.reloc_bos[last_idx] = &new_bbo->bo;
376 }
377
378 prev_bbo = new_bbo;
379 }
380
381 if (result != VK_SUCCESS) {
382 list_for_each_entry_safe(struct anv_batch_bo, bbo, new_list, link)
383 anv_batch_bo_destroy(bbo, cmd_buffer);
384 }
385
386 return result;
387 }
388
389 /*-----------------------------------------------------------------------*
390 * Functions related to anv_batch_bo
391 *-----------------------------------------------------------------------*/
392
393 static inline struct anv_batch_bo *
394 anv_cmd_buffer_current_batch_bo(struct anv_cmd_buffer *cmd_buffer)
395 {
396 return LIST_ENTRY(struct anv_batch_bo, cmd_buffer->batch_bos.prev, link);
397 }
398
399 struct anv_address
400 anv_cmd_buffer_surface_base_address(struct anv_cmd_buffer *cmd_buffer)
401 {
402 return (struct anv_address) {
403 .bo = &cmd_buffer->device->surface_state_block_pool.bo,
404 .offset = *(int32_t *)anv_vector_head(&cmd_buffer->bt_blocks),
405 };
406 }
407
408 static void
409 emit_batch_buffer_start(struct anv_cmd_buffer *cmd_buffer,
410 struct anv_bo *bo, uint32_t offset)
411 {
412 /* In gen8+ the address field grew to two dwords to accomodate 48 bit
413 * offsets. The high 16 bits are in the last dword, so we can use the gen8
414 * version in either case, as long as we set the instruction length in the
415 * header accordingly. This means that we always emit three dwords here
416 * and all the padding and adjustment we do in this file works for all
417 * gens.
418 */
419
420 const uint32_t gen7_length =
421 GEN7_MI_BATCH_BUFFER_START_length - GEN7_MI_BATCH_BUFFER_START_length_bias;
422 const uint32_t gen8_length =
423 GEN8_MI_BATCH_BUFFER_START_length - GEN8_MI_BATCH_BUFFER_START_length_bias;
424
425 anv_batch_emit(&cmd_buffer->batch, GEN8_MI_BATCH_BUFFER_START,
426 .DWordLength = cmd_buffer->device->info.gen < 8 ?
427 gen7_length : gen8_length,
428 ._2ndLevelBatchBuffer = _1stlevelbatch,
429 .AddressSpaceIndicator = ASI_PPGTT,
430 .BatchBufferStartAddress = { bo, offset });
431 }
432
433 static void
434 cmd_buffer_chain_to_batch_bo(struct anv_cmd_buffer *cmd_buffer,
435 struct anv_batch_bo *bbo)
436 {
437 struct anv_batch *batch = &cmd_buffer->batch;
438 struct anv_batch_bo *current_bbo =
439 anv_cmd_buffer_current_batch_bo(cmd_buffer);
440
441 /* We set the end of the batch a little short so we would be sure we
442 * have room for the chaining command. Since we're about to emit the
443 * chaining command, let's set it back where it should go.
444 */
445 batch->end += GEN8_MI_BATCH_BUFFER_START_length * 4;
446 assert(batch->end == current_bbo->bo.map + current_bbo->bo.size);
447
448 emit_batch_buffer_start(cmd_buffer, &bbo->bo, 0);
449
450 anv_batch_bo_finish(current_bbo, batch);
451 }
452
453 static VkResult
454 anv_cmd_buffer_chain_batch(struct anv_batch *batch, void *_data)
455 {
456 struct anv_cmd_buffer *cmd_buffer = _data;
457 struct anv_batch_bo *new_bbo;
458
459 VkResult result = anv_batch_bo_create(cmd_buffer, &new_bbo);
460 if (result != VK_SUCCESS)
461 return result;
462
463 struct anv_batch_bo **seen_bbo = anv_vector_add(&cmd_buffer->seen_bbos);
464 if (seen_bbo == NULL) {
465 anv_batch_bo_destroy(new_bbo, cmd_buffer);
466 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
467 }
468 *seen_bbo = new_bbo;
469
470 cmd_buffer_chain_to_batch_bo(cmd_buffer, new_bbo);
471
472 list_addtail(&new_bbo->link, &cmd_buffer->batch_bos);
473
474 anv_batch_bo_start(new_bbo, batch, GEN8_MI_BATCH_BUFFER_START_length * 4);
475
476 return VK_SUCCESS;
477 }
478
479 struct anv_state
480 anv_cmd_buffer_alloc_binding_table(struct anv_cmd_buffer *cmd_buffer,
481 uint32_t entries, uint32_t *state_offset)
482 {
483 struct anv_block_pool *block_pool =
484 &cmd_buffer->device->surface_state_block_pool;
485 int32_t *bt_block = anv_vector_head(&cmd_buffer->bt_blocks);
486 struct anv_state state;
487
488 state.alloc_size = align_u32(entries * 4, 32);
489
490 if (cmd_buffer->bt_next + state.alloc_size > block_pool->block_size)
491 return (struct anv_state) { 0 };
492
493 state.offset = cmd_buffer->bt_next;
494 state.map = block_pool->map + *bt_block + state.offset;
495
496 cmd_buffer->bt_next += state.alloc_size;
497
498 assert(*bt_block < 0);
499 *state_offset = -(*bt_block);
500
501 return state;
502 }
503
504 struct anv_state
505 anv_cmd_buffer_alloc_surface_state(struct anv_cmd_buffer *cmd_buffer)
506 {
507 return anv_state_stream_alloc(&cmd_buffer->surface_state_stream, 64, 64);
508 }
509
510 struct anv_state
511 anv_cmd_buffer_alloc_dynamic_state(struct anv_cmd_buffer *cmd_buffer,
512 uint32_t size, uint32_t alignment)
513 {
514 return anv_state_stream_alloc(&cmd_buffer->dynamic_state_stream,
515 size, alignment);
516 }
517
518 VkResult
519 anv_cmd_buffer_new_binding_table_block(struct anv_cmd_buffer *cmd_buffer)
520 {
521 struct anv_block_pool *block_pool =
522 &cmd_buffer->device->surface_state_block_pool;
523
524 int32_t *offset = anv_vector_add(&cmd_buffer->bt_blocks);
525 if (offset == NULL)
526 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
527
528 *offset = anv_block_pool_alloc_back(block_pool);
529 cmd_buffer->bt_next = 0;
530
531 return VK_SUCCESS;
532 }
533
534 VkResult
535 anv_cmd_buffer_init_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
536 {
537 struct anv_batch_bo *batch_bo;
538 VkResult result;
539
540 list_inithead(&cmd_buffer->batch_bos);
541
542 result = anv_batch_bo_create(cmd_buffer, &batch_bo);
543 if (result != VK_SUCCESS)
544 return result;
545
546 list_addtail(&batch_bo->link, &cmd_buffer->batch_bos);
547
548 cmd_buffer->batch.alloc = &cmd_buffer->pool->alloc;
549 cmd_buffer->batch.extend_cb = anv_cmd_buffer_chain_batch;
550 cmd_buffer->batch.user_data = cmd_buffer;
551
552 anv_batch_bo_start(batch_bo, &cmd_buffer->batch,
553 GEN8_MI_BATCH_BUFFER_START_length * 4);
554
555 int success = anv_vector_init(&cmd_buffer->seen_bbos,
556 sizeof(struct anv_bo *),
557 8 * sizeof(struct anv_bo *));
558 if (!success)
559 goto fail_batch_bo;
560
561 *(struct anv_batch_bo **)anv_vector_add(&cmd_buffer->seen_bbos) = batch_bo;
562
563 success = anv_vector_init(&cmd_buffer->bt_blocks, sizeof(int32_t),
564 8 * sizeof(int32_t));
565 if (!success)
566 goto fail_seen_bbos;
567
568 result = anv_reloc_list_init(&cmd_buffer->surface_relocs,
569 &cmd_buffer->pool->alloc);
570 if (result != VK_SUCCESS)
571 goto fail_bt_blocks;
572
573 anv_cmd_buffer_new_binding_table_block(cmd_buffer);
574
575 cmd_buffer->execbuf2.objects = NULL;
576 cmd_buffer->execbuf2.bos = NULL;
577 cmd_buffer->execbuf2.array_length = 0;
578
579 return VK_SUCCESS;
580
581 fail_bt_blocks:
582 anv_vector_finish(&cmd_buffer->bt_blocks);
583 fail_seen_bbos:
584 anv_vector_finish(&cmd_buffer->seen_bbos);
585 fail_batch_bo:
586 anv_batch_bo_destroy(batch_bo, cmd_buffer);
587
588 return result;
589 }
590
591 void
592 anv_cmd_buffer_fini_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
593 {
594 int32_t *bt_block;
595 anv_vector_foreach(bt_block, &cmd_buffer->bt_blocks) {
596 anv_block_pool_free(&cmd_buffer->device->surface_state_block_pool,
597 *bt_block);
598 }
599 anv_vector_finish(&cmd_buffer->bt_blocks);
600
601 anv_reloc_list_finish(&cmd_buffer->surface_relocs, &cmd_buffer->pool->alloc);
602
603 anv_vector_finish(&cmd_buffer->seen_bbos);
604
605 /* Destroy all of the batch buffers */
606 list_for_each_entry_safe(struct anv_batch_bo, bbo,
607 &cmd_buffer->batch_bos, link) {
608 anv_batch_bo_destroy(bbo, cmd_buffer);
609 }
610
611 anv_free(&cmd_buffer->pool->alloc, cmd_buffer->execbuf2.objects);
612 anv_free(&cmd_buffer->pool->alloc, cmd_buffer->execbuf2.bos);
613 }
614
615 void
616 anv_cmd_buffer_reset_batch_bo_chain(struct anv_cmd_buffer *cmd_buffer)
617 {
618 /* Delete all but the first batch bo */
619 assert(!list_empty(&cmd_buffer->batch_bos));
620 while (cmd_buffer->batch_bos.next != cmd_buffer->batch_bos.prev) {
621 struct anv_batch_bo *bbo = anv_cmd_buffer_current_batch_bo(cmd_buffer);
622 list_del(&bbo->link);
623 anv_batch_bo_destroy(bbo, cmd_buffer);
624 }
625 assert(!list_empty(&cmd_buffer->batch_bos));
626
627 anv_batch_bo_start(anv_cmd_buffer_current_batch_bo(cmd_buffer),
628 &cmd_buffer->batch,
629 GEN8_MI_BATCH_BUFFER_START_length * 4);
630
631 while (anv_vector_length(&cmd_buffer->bt_blocks) > 1) {
632 int32_t *bt_block = anv_vector_remove(&cmd_buffer->bt_blocks);
633 anv_block_pool_free(&cmd_buffer->device->surface_state_block_pool,
634 *bt_block);
635 }
636 assert(anv_vector_length(&cmd_buffer->bt_blocks) == 1);
637 cmd_buffer->bt_next = 0;
638
639 cmd_buffer->surface_relocs.num_relocs = 0;
640
641 /* Reset the list of seen buffers */
642 cmd_buffer->seen_bbos.head = 0;
643 cmd_buffer->seen_bbos.tail = 0;
644
645 *(struct anv_batch_bo **)anv_vector_add(&cmd_buffer->seen_bbos) =
646 anv_cmd_buffer_current_batch_bo(cmd_buffer);
647 }
648
649 void
650 anv_cmd_buffer_end_batch_buffer(struct anv_cmd_buffer *cmd_buffer)
651 {
652 struct anv_batch_bo *batch_bo = anv_cmd_buffer_current_batch_bo(cmd_buffer);
653
654 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_PRIMARY) {
655 /* When we start a batch buffer, we subtract a certain amount of
656 * padding from the end to ensure that we always have room to emit a
657 * BATCH_BUFFER_START to chain to the next BO. We need to remove
658 * that padding before we end the batch; otherwise, we may end up
659 * with our BATCH_BUFFER_END in another BO.
660 */
661 cmd_buffer->batch.end += GEN8_MI_BATCH_BUFFER_START_length * 4;
662 assert(cmd_buffer->batch.end == batch_bo->bo.map + batch_bo->bo.size);
663
664 anv_batch_emit(&cmd_buffer->batch, GEN7_MI_BATCH_BUFFER_END);
665
666 /* Round batch up to an even number of dwords. */
667 if ((cmd_buffer->batch.next - cmd_buffer->batch.start) & 4)
668 anv_batch_emit(&cmd_buffer->batch, GEN7_MI_NOOP);
669
670 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_PRIMARY;
671 }
672
673 anv_batch_bo_finish(batch_bo, &cmd_buffer->batch);
674
675 if (cmd_buffer->level == VK_COMMAND_BUFFER_LEVEL_SECONDARY) {
676 /* If this is a secondary command buffer, we need to determine the
677 * mode in which it will be executed with vkExecuteCommands. We
678 * determine this statically here so that this stays in sync with the
679 * actual ExecuteCommands implementation.
680 */
681 if ((cmd_buffer->batch_bos.next == cmd_buffer->batch_bos.prev) &&
682 (batch_bo->length < ANV_CMD_BUFFER_BATCH_SIZE / 2)) {
683 /* If the secondary has exactly one batch buffer in its list *and*
684 * that batch buffer is less than half of the maximum size, we're
685 * probably better of simply copying it into our batch.
686 */
687 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_EMIT;
688 } else if (!(cmd_buffer->usage_flags &
689 VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT)) {
690 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_CHAIN;
691
692 /* When we chain, we need to add an MI_BATCH_BUFFER_START command
693 * with its relocation. In order to handle this we'll increment here
694 * so we can unconditionally decrement right before adding the
695 * MI_BATCH_BUFFER_START command.
696 */
697 batch_bo->relocs.num_relocs++;
698 cmd_buffer->batch.next += GEN8_MI_BATCH_BUFFER_START_length * 4;
699 } else {
700 cmd_buffer->exec_mode = ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN;
701 }
702 }
703 }
704
705 static inline VkResult
706 anv_cmd_buffer_add_seen_bbos(struct anv_cmd_buffer *cmd_buffer,
707 struct list_head *list)
708 {
709 list_for_each_entry(struct anv_batch_bo, bbo, list, link) {
710 struct anv_batch_bo **bbo_ptr = anv_vector_add(&cmd_buffer->seen_bbos);
711 if (bbo_ptr == NULL)
712 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
713
714 *bbo_ptr = bbo;
715 }
716
717 return VK_SUCCESS;
718 }
719
720 void
721 anv_cmd_buffer_add_secondary(struct anv_cmd_buffer *primary,
722 struct anv_cmd_buffer *secondary)
723 {
724 switch (secondary->exec_mode) {
725 case ANV_CMD_BUFFER_EXEC_MODE_EMIT:
726 anv_batch_emit_batch(&primary->batch, &secondary->batch);
727 anv_cmd_buffer_emit_state_base_address(primary);
728 break;
729 case ANV_CMD_BUFFER_EXEC_MODE_CHAIN: {
730 struct anv_batch_bo *first_bbo =
731 list_first_entry(&secondary->batch_bos, struct anv_batch_bo, link);
732 struct anv_batch_bo *last_bbo =
733 list_last_entry(&secondary->batch_bos, struct anv_batch_bo, link);
734
735 emit_batch_buffer_start(primary, &first_bbo->bo, 0);
736
737 struct anv_batch_bo *this_bbo = anv_cmd_buffer_current_batch_bo(primary);
738 assert(primary->batch.start == this_bbo->bo.map);
739 uint32_t offset = primary->batch.next - primary->batch.start;
740 const uint32_t inst_size = GEN8_MI_BATCH_BUFFER_START_length * 4;
741
742 /* Roll back the previous MI_BATCH_BUFFER_START and its relocation so we
743 * can emit a new command and relocation for the current splice. In
744 * order to handle the initial-use case, we incremented next and
745 * num_relocs in end_batch_buffer() so we can alyways just subtract
746 * here.
747 */
748 last_bbo->relocs.num_relocs--;
749 secondary->batch.next -= inst_size;
750 emit_batch_buffer_start(secondary, &this_bbo->bo, offset);
751 anv_cmd_buffer_add_seen_bbos(primary, &secondary->batch_bos);
752
753 /* After patching up the secondary buffer, we need to clflush the
754 * modified instruction in case we're on a !llc platform. We use a
755 * little loop to handle the case where the instruction crosses a cache
756 * line boundary.
757 */
758 if (!primary->device->info.has_llc) {
759 void *inst = secondary->batch.next - inst_size;
760 void *p = (void *) (((uintptr_t) inst) & ~CACHELINE_MASK);
761 __builtin_ia32_mfence();
762 while (p < secondary->batch.next) {
763 __builtin_ia32_clflush(p);
764 p += CACHELINE_SIZE;
765 }
766 }
767
768 anv_cmd_buffer_emit_state_base_address(primary);
769 break;
770 }
771 case ANV_CMD_BUFFER_EXEC_MODE_COPY_AND_CHAIN: {
772 struct list_head copy_list;
773 VkResult result = anv_batch_bo_list_clone(&secondary->batch_bos,
774 secondary,
775 &copy_list);
776 if (result != VK_SUCCESS)
777 return; /* FIXME */
778
779 anv_cmd_buffer_add_seen_bbos(primary, &copy_list);
780
781 struct anv_batch_bo *first_bbo =
782 list_first_entry(&copy_list, struct anv_batch_bo, link);
783 struct anv_batch_bo *last_bbo =
784 list_last_entry(&copy_list, struct anv_batch_bo, link);
785
786 cmd_buffer_chain_to_batch_bo(primary, first_bbo);
787
788 list_splicetail(&copy_list, &primary->batch_bos);
789
790 anv_batch_bo_continue(last_bbo, &primary->batch,
791 GEN8_MI_BATCH_BUFFER_START_length * 4);
792
793 anv_cmd_buffer_emit_state_base_address(primary);
794 break;
795 }
796 default:
797 assert(!"Invalid execution mode");
798 }
799
800 anv_reloc_list_append(&primary->surface_relocs, &primary->pool->alloc,
801 &secondary->surface_relocs, 0);
802 }
803
804 static VkResult
805 anv_cmd_buffer_add_bo(struct anv_cmd_buffer *cmd_buffer,
806 struct anv_bo *bo,
807 struct anv_reloc_list *relocs)
808 {
809 struct drm_i915_gem_exec_object2 *obj = NULL;
810
811 if (bo->index < cmd_buffer->execbuf2.bo_count &&
812 cmd_buffer->execbuf2.bos[bo->index] == bo)
813 obj = &cmd_buffer->execbuf2.objects[bo->index];
814
815 if (obj == NULL) {
816 /* We've never seen this one before. Add it to the list and assign
817 * an id that we can use later.
818 */
819 if (cmd_buffer->execbuf2.bo_count >= cmd_buffer->execbuf2.array_length) {
820 uint32_t new_len = cmd_buffer->execbuf2.objects ?
821 cmd_buffer->execbuf2.array_length * 2 : 64;
822
823 struct drm_i915_gem_exec_object2 *new_objects =
824 anv_alloc(&cmd_buffer->pool->alloc, new_len * sizeof(*new_objects),
825 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
826 if (new_objects == NULL)
827 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
828
829 struct anv_bo **new_bos =
830 anv_alloc(&cmd_buffer->pool->alloc, new_len * sizeof(*new_bos),
831 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
832 if (new_objects == NULL) {
833 anv_free(&cmd_buffer->pool->alloc, new_objects);
834 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
835 }
836
837 if (cmd_buffer->execbuf2.objects) {
838 memcpy(new_objects, cmd_buffer->execbuf2.objects,
839 cmd_buffer->execbuf2.bo_count * sizeof(*new_objects));
840 memcpy(new_bos, cmd_buffer->execbuf2.bos,
841 cmd_buffer->execbuf2.bo_count * sizeof(*new_bos));
842 }
843
844 cmd_buffer->execbuf2.objects = new_objects;
845 cmd_buffer->execbuf2.bos = new_bos;
846 cmd_buffer->execbuf2.array_length = new_len;
847 }
848
849 assert(cmd_buffer->execbuf2.bo_count < cmd_buffer->execbuf2.array_length);
850
851 bo->index = cmd_buffer->execbuf2.bo_count++;
852 obj = &cmd_buffer->execbuf2.objects[bo->index];
853 cmd_buffer->execbuf2.bos[bo->index] = bo;
854
855 obj->handle = bo->gem_handle;
856 obj->relocation_count = 0;
857 obj->relocs_ptr = 0;
858 obj->alignment = 0;
859 obj->offset = bo->offset;
860 obj->flags = bo->is_winsys_bo ? EXEC_OBJECT_WRITE : 0;
861 obj->rsvd1 = 0;
862 obj->rsvd2 = 0;
863 }
864
865 if (relocs != NULL && obj->relocation_count == 0) {
866 /* This is the first time we've ever seen a list of relocations for
867 * this BO. Go ahead and set the relocations and then walk the list
868 * of relocations and add them all.
869 */
870 obj->relocation_count = relocs->num_relocs;
871 obj->relocs_ptr = (uintptr_t) relocs->relocs;
872
873 for (size_t i = 0; i < relocs->num_relocs; i++) {
874 /* A quick sanity check on relocations */
875 assert(relocs->relocs[i].offset < bo->size);
876 anv_cmd_buffer_add_bo(cmd_buffer, relocs->reloc_bos[i], NULL);
877 }
878 }
879
880 return VK_SUCCESS;
881 }
882
883 static void
884 anv_cmd_buffer_process_relocs(struct anv_cmd_buffer *cmd_buffer,
885 struct anv_reloc_list *list)
886 {
887 struct anv_bo *bo;
888
889 /* If the kernel supports I915_EXEC_NO_RELOC, it will compare offset in
890 * struct drm_i915_gem_exec_object2 against the bos current offset and if
891 * all bos haven't moved it will skip relocation processing alltogether.
892 * If I915_EXEC_NO_RELOC is not supported, the kernel ignores the incoming
893 * value of offset so we can set it either way. For that to work we need
894 * to make sure all relocs use the same presumed offset.
895 */
896
897 for (size_t i = 0; i < list->num_relocs; i++) {
898 bo = list->reloc_bos[i];
899 if (bo->offset != list->relocs[i].presumed_offset)
900 cmd_buffer->execbuf2.need_reloc = true;
901
902 list->relocs[i].target_handle = bo->index;
903 }
904 }
905
906 static uint64_t
907 read_reloc(const struct anv_device *device, const void *p)
908 {
909 if (device->info.gen >= 8)
910 return *(uint64_t *)p;
911 else
912 return *(uint32_t *)p;
913 }
914
915 static void
916 write_reloc(const struct anv_device *device, void *p, uint64_t v)
917 {
918 if (device->info.gen >= 8)
919 *(uint64_t *)p = v;
920 else
921 *(uint32_t *)p = v;
922 }
923
924 static void
925 adjust_relocations_from_block_pool(struct anv_block_pool *pool,
926 struct anv_reloc_list *relocs)
927 {
928 for (size_t i = 0; i < relocs->num_relocs; i++) {
929 /* In general, we don't know how stale the relocated value is. It
930 * may have been used last time or it may not. Since we don't want
931 * to stomp it while the GPU may be accessing it, we haven't updated
932 * it anywhere else in the code. Instead, we just set the presumed
933 * offset to what it is now based on the delta and the data in the
934 * block pool. Then the kernel will update it for us if needed.
935 */
936 assert(relocs->relocs[i].offset < pool->state.end);
937 const void *p = pool->map + relocs->relocs[i].offset;
938
939 /* We're reading back the relocated value from potentially incoherent
940 * memory here. However, any change to the value will be from the kernel
941 * writing out relocations, which will keep the CPU cache up to date.
942 */
943 relocs->relocs[i].presumed_offset =
944 read_reloc(pool->device, p) - relocs->relocs[i].delta;
945
946 /* All of the relocations from this block pool to other BO's should
947 * have been emitted relative to the surface block pool center. We
948 * need to add the center offset to make them relative to the
949 * beginning of the actual GEM bo.
950 */
951 relocs->relocs[i].offset += pool->center_bo_offset;
952 }
953 }
954
955 static void
956 adjust_relocations_to_block_pool(struct anv_block_pool *pool,
957 struct anv_bo *from_bo,
958 struct anv_reloc_list *relocs,
959 uint32_t *last_pool_center_bo_offset)
960 {
961 assert(*last_pool_center_bo_offset <= pool->center_bo_offset);
962 uint32_t delta = pool->center_bo_offset - *last_pool_center_bo_offset;
963
964 /* When we initially emit relocations into a block pool, we don't
965 * actually know what the final center_bo_offset will be so we just emit
966 * it as if center_bo_offset == 0. Now that we know what the center
967 * offset is, we need to walk the list of relocations and adjust any
968 * relocations that point to the pool bo with the correct offset.
969 */
970 for (size_t i = 0; i < relocs->num_relocs; i++) {
971 if (relocs->reloc_bos[i] == &pool->bo) {
972 /* Adjust the delta value in the relocation to correctly
973 * correspond to the new delta. Initially, this value may have
974 * been negative (if treated as unsigned), but we trust in
975 * uint32_t roll-over to fix that for us at this point.
976 */
977 relocs->relocs[i].delta += delta;
978
979 /* Since the delta has changed, we need to update the actual
980 * relocated value with the new presumed value. This function
981 * should only be called on batch buffers, so we know it isn't in
982 * use by the GPU at the moment.
983 */
984 assert(relocs->relocs[i].offset < from_bo->size);
985 write_reloc(pool->device, from_bo->map + relocs->relocs[i].offset,
986 relocs->relocs[i].presumed_offset +
987 relocs->relocs[i].delta);
988 }
989 }
990
991 *last_pool_center_bo_offset = pool->center_bo_offset;
992 }
993
994 void
995 anv_cmd_buffer_prepare_execbuf(struct anv_cmd_buffer *cmd_buffer)
996 {
997 struct anv_batch *batch = &cmd_buffer->batch;
998 struct anv_block_pool *ss_pool =
999 &cmd_buffer->device->surface_state_block_pool;
1000
1001 cmd_buffer->execbuf2.bo_count = 0;
1002 cmd_buffer->execbuf2.need_reloc = false;
1003
1004 adjust_relocations_from_block_pool(ss_pool, &cmd_buffer->surface_relocs);
1005 anv_cmd_buffer_add_bo(cmd_buffer, &ss_pool->bo, &cmd_buffer->surface_relocs);
1006
1007 /* First, we walk over all of the bos we've seen and add them and their
1008 * relocations to the validate list.
1009 */
1010 struct anv_batch_bo **bbo;
1011 anv_vector_foreach(bbo, &cmd_buffer->seen_bbos) {
1012 adjust_relocations_to_block_pool(ss_pool, &(*bbo)->bo, &(*bbo)->relocs,
1013 &(*bbo)->last_ss_pool_bo_offset);
1014
1015 anv_cmd_buffer_add_bo(cmd_buffer, &(*bbo)->bo, &(*bbo)->relocs);
1016 }
1017
1018 struct anv_batch_bo *first_batch_bo =
1019 list_first_entry(&cmd_buffer->batch_bos, struct anv_batch_bo, link);
1020
1021 /* The kernel requires that the last entry in the validation list be the
1022 * batch buffer to execute. We can simply swap the element
1023 * corresponding to the first batch_bo in the chain with the last
1024 * element in the list.
1025 */
1026 if (first_batch_bo->bo.index != cmd_buffer->execbuf2.bo_count - 1) {
1027 uint32_t idx = first_batch_bo->bo.index;
1028 uint32_t last_idx = cmd_buffer->execbuf2.bo_count - 1;
1029
1030 struct drm_i915_gem_exec_object2 tmp_obj =
1031 cmd_buffer->execbuf2.objects[idx];
1032 assert(cmd_buffer->execbuf2.bos[idx] == &first_batch_bo->bo);
1033
1034 cmd_buffer->execbuf2.objects[idx] = cmd_buffer->execbuf2.objects[last_idx];
1035 cmd_buffer->execbuf2.bos[idx] = cmd_buffer->execbuf2.bos[last_idx];
1036 cmd_buffer->execbuf2.bos[idx]->index = idx;
1037
1038 cmd_buffer->execbuf2.objects[last_idx] = tmp_obj;
1039 cmd_buffer->execbuf2.bos[last_idx] = &first_batch_bo->bo;
1040 first_batch_bo->bo.index = last_idx;
1041 }
1042
1043 /* Now we go through and fixup all of the relocation lists to point to
1044 * the correct indices in the object array. We have to do this after we
1045 * reorder the list above as some of the indices may have changed.
1046 */
1047 anv_vector_foreach(bbo, &cmd_buffer->seen_bbos)
1048 anv_cmd_buffer_process_relocs(cmd_buffer, &(*bbo)->relocs);
1049
1050 anv_cmd_buffer_process_relocs(cmd_buffer, &cmd_buffer->surface_relocs);
1051
1052 if (!cmd_buffer->device->info.has_llc) {
1053 __builtin_ia32_mfence();
1054 anv_vector_foreach(bbo, &cmd_buffer->seen_bbos) {
1055 for (uint32_t i = 0; i < (*bbo)->length; i += CACHELINE_SIZE)
1056 __builtin_ia32_clflush((*bbo)->bo.map + i);
1057 }
1058 }
1059
1060 cmd_buffer->execbuf2.execbuf = (struct drm_i915_gem_execbuffer2) {
1061 .buffers_ptr = (uintptr_t) cmd_buffer->execbuf2.objects,
1062 .buffer_count = cmd_buffer->execbuf2.bo_count,
1063 .batch_start_offset = 0,
1064 .batch_len = batch->next - batch->start,
1065 .cliprects_ptr = 0,
1066 .num_cliprects = 0,
1067 .DR1 = 0,
1068 .DR4 = 0,
1069 .flags = I915_EXEC_HANDLE_LUT | I915_EXEC_RENDER |
1070 I915_EXEC_CONSTANTS_REL_GENERAL,
1071 .rsvd1 = cmd_buffer->device->context_id,
1072 .rsvd2 = 0,
1073 };
1074
1075 if (!cmd_buffer->execbuf2.need_reloc)
1076 cmd_buffer->execbuf2.execbuf.flags |= I915_EXEC_NO_RELOC;
1077 }