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