2 * Copyright © 2015 Intel Corporation
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:
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
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
25 * This file implements VkQueue, VkFence, and VkSemaphore
32 #include "anv_private.h"
35 #include "genxml/gen7_pack.h"
37 uint64_t anv_gettime_ns(void)
39 struct timespec current
;
40 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
41 return (uint64_t)current
.tv_sec
* NSEC_PER_SEC
+ current
.tv_nsec
;
44 uint64_t anv_get_absolute_timeout(uint64_t timeout
)
48 uint64_t current_time
= anv_gettime_ns();
49 uint64_t max_timeout
= (uint64_t) INT64_MAX
- current_time
;
51 timeout
= MIN2(max_timeout
, timeout
);
53 return (current_time
+ timeout
);
56 static int64_t anv_get_relative_timeout(uint64_t abs_timeout
)
58 uint64_t now
= anv_gettime_ns();
60 /* We don't want negative timeouts.
62 * DRM_IOCTL_I915_GEM_WAIT uses a signed 64 bit timeout and is
63 * supposed to block indefinitely timeouts < 0. Unfortunately,
64 * this was broken for a couple of kernel releases. Since there's
65 * no way to know whether or not the kernel we're using is one of
66 * the broken ones, the best we can do is to clamp the timeout to
67 * INT64_MAX. This limits the maximum timeout from 584 years to
68 * 292 years - likely not a big deal.
70 if (abs_timeout
< now
)
73 uint64_t rel_timeout
= abs_timeout
- now
;
74 if (rel_timeout
> (uint64_t) INT64_MAX
)
75 rel_timeout
= INT64_MAX
;
80 static struct anv_semaphore
*anv_semaphore_ref(struct anv_semaphore
*semaphore
);
81 static void anv_semaphore_unref(struct anv_device
*device
, struct anv_semaphore
*semaphore
);
82 static void anv_semaphore_impl_cleanup(struct anv_device
*device
,
83 struct anv_semaphore_impl
*impl
);
86 anv_queue_submit_free(struct anv_device
*device
,
87 struct anv_queue_submit
*submit
)
89 const VkAllocationCallbacks
*alloc
= submit
->alloc
;
91 for (uint32_t i
= 0; i
< submit
->temporary_semaphore_count
; i
++)
92 anv_semaphore_impl_cleanup(device
, &submit
->temporary_semaphores
[i
]);
93 for (uint32_t i
= 0; i
< submit
->sync_fd_semaphore_count
; i
++)
94 anv_semaphore_unref(device
, submit
->sync_fd_semaphores
[i
]);
95 /* Execbuf does not consume the in_fence. It's our job to close it. */
96 if (submit
->in_fence
!= -1)
97 close(submit
->in_fence
);
98 if (submit
->out_fence
!= -1)
99 close(submit
->out_fence
);
100 vk_free(alloc
, submit
->fences
);
101 vk_free(alloc
, submit
->temporary_semaphores
);
102 vk_free(alloc
, submit
->wait_timelines
);
103 vk_free(alloc
, submit
->wait_timeline_values
);
104 vk_free(alloc
, submit
->signal_timelines
);
105 vk_free(alloc
, submit
->signal_timeline_values
);
106 vk_free(alloc
, submit
->fence_bos
);
107 vk_free(alloc
, submit
);
111 anv_queue_submit_ready_locked(struct anv_queue_submit
*submit
)
113 for (uint32_t i
= 0; i
< submit
->wait_timeline_count
; i
++) {
114 if (submit
->wait_timeline_values
[i
] > submit
->wait_timelines
[i
]->highest_pending
)
122 anv_timeline_init(struct anv_device
*device
,
123 struct anv_timeline
*timeline
,
124 uint64_t initial_value
)
126 timeline
->highest_past
=
127 timeline
->highest_pending
= initial_value
;
128 list_inithead(&timeline
->points
);
129 list_inithead(&timeline
->free_points
);
135 anv_timeline_finish(struct anv_device
*device
,
136 struct anv_timeline
*timeline
)
138 list_for_each_entry_safe(struct anv_timeline_point
, point
,
139 &timeline
->free_points
, link
) {
140 list_del(&point
->link
);
141 anv_device_release_bo(device
, point
->bo
);
142 vk_free(&device
->alloc
, point
);
144 list_for_each_entry_safe(struct anv_timeline_point
, point
,
145 &timeline
->points
, link
) {
146 list_del(&point
->link
);
147 anv_device_release_bo(device
, point
->bo
);
148 vk_free(&device
->alloc
, point
);
153 anv_timeline_add_point_locked(struct anv_device
*device
,
154 struct anv_timeline
*timeline
,
156 struct anv_timeline_point
**point
)
158 VkResult result
= VK_SUCCESS
;
160 if (list_is_empty(&timeline
->free_points
)) {
162 vk_zalloc(&device
->alloc
, sizeof(**point
),
163 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
165 result
= vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
166 if (result
== VK_SUCCESS
) {
167 result
= anv_device_alloc_bo(device
, 4096,
168 ANV_BO_ALLOC_EXTERNAL
|
169 ANV_BO_ALLOC_IMPLICIT_SYNC
,
170 0 /* explicit_address */,
172 if (result
!= VK_SUCCESS
)
173 vk_free(&device
->alloc
, *point
);
176 *point
= list_first_entry(&timeline
->free_points
,
177 struct anv_timeline_point
, link
);
178 list_del(&(*point
)->link
);
181 if (result
== VK_SUCCESS
) {
182 (*point
)->serial
= value
;
183 list_addtail(&(*point
)->link
, &timeline
->points
);
190 anv_timeline_gc_locked(struct anv_device
*device
,
191 struct anv_timeline
*timeline
)
193 list_for_each_entry_safe(struct anv_timeline_point
, point
,
194 &timeline
->points
, link
) {
195 /* timeline->higest_pending is only incremented once submission has
196 * happened. If this point has a greater serial, it means the point
197 * hasn't been submitted yet.
199 if (point
->serial
> timeline
->highest_pending
)
202 /* If someone is waiting on this time point, consider it busy and don't
203 * try to recycle it. There's a slim possibility that it's no longer
204 * busy by the time we look at it but we would be recycling it out from
205 * under a waiter and that can lead to weird races.
207 * We walk the list in-order so if this time point is still busy so is
208 * every following time point
210 assert(point
->waiting
>= 0);
214 /* Garbage collect any signaled point. */
215 VkResult result
= anv_device_bo_busy(device
, point
->bo
);
216 if (result
== VK_NOT_READY
) {
217 /* We walk the list in-order so if this time point is still busy so
218 * is every following time point
221 } else if (result
!= VK_SUCCESS
) {
225 assert(timeline
->highest_past
< point
->serial
);
226 timeline
->highest_past
= point
->serial
;
228 list_del(&point
->link
);
229 list_add(&point
->link
, &timeline
->free_points
);
235 static VkResult
anv_queue_submit_add_fence_bo(struct anv_queue_submit
*submit
,
240 anv_queue_submit_timeline_locked(struct anv_queue
*queue
,
241 struct anv_queue_submit
*submit
)
245 for (uint32_t i
= 0; i
< submit
->wait_timeline_count
; i
++) {
246 struct anv_timeline
*timeline
= submit
->wait_timelines
[i
];
247 uint64_t wait_value
= submit
->wait_timeline_values
[i
];
249 if (timeline
->highest_past
>= wait_value
)
252 list_for_each_entry(struct anv_timeline_point
, point
, &timeline
->points
, link
) {
253 if (point
->serial
< wait_value
)
255 result
= anv_queue_submit_add_fence_bo(submit
, point
->bo
, false);
256 if (result
!= VK_SUCCESS
)
261 for (uint32_t i
= 0; i
< submit
->signal_timeline_count
; i
++) {
262 struct anv_timeline
*timeline
= submit
->signal_timelines
[i
];
263 uint64_t signal_value
= submit
->signal_timeline_values
[i
];
264 struct anv_timeline_point
*point
;
266 result
= anv_timeline_add_point_locked(queue
->device
, timeline
,
267 signal_value
, &point
);
268 if (result
!= VK_SUCCESS
)
271 result
= anv_queue_submit_add_fence_bo(submit
, point
->bo
, true);
272 if (result
!= VK_SUCCESS
)
276 result
= anv_queue_execbuf_locked(queue
, submit
);
278 if (result
== VK_SUCCESS
) {
279 /* Update the pending values in the timeline objects. */
280 for (uint32_t i
= 0; i
< submit
->signal_timeline_count
; i
++) {
281 struct anv_timeline
*timeline
= submit
->signal_timelines
[i
];
282 uint64_t signal_value
= submit
->signal_timeline_values
[i
];
284 assert(signal_value
> timeline
->highest_pending
);
285 timeline
->highest_pending
= signal_value
;
288 /* Update signaled semaphores backed by syncfd. */
289 for (uint32_t i
= 0; i
< submit
->sync_fd_semaphore_count
; i
++) {
290 struct anv_semaphore
*semaphore
= submit
->sync_fd_semaphores
[i
];
291 /* Out fences can't have temporary state because that would imply
292 * that we imported a sync file and are trying to signal it.
294 assert(semaphore
->temporary
.type
== ANV_SEMAPHORE_TYPE_NONE
);
295 struct anv_semaphore_impl
*impl
= &semaphore
->permanent
;
297 assert(impl
->type
== ANV_SEMAPHORE_TYPE_SYNC_FILE
);
298 impl
->fd
= dup(submit
->out_fence
);
301 /* Unblock any waiter by signaling the points, the application will get
302 * a device lost error code.
304 for (uint32_t i
= 0; i
< submit
->signal_timeline_count
; i
++) {
305 struct anv_timeline
*timeline
= submit
->signal_timelines
[i
];
306 uint64_t signal_value
= submit
->signal_timeline_values
[i
];
308 assert(signal_value
> timeline
->highest_pending
);
309 timeline
->highest_past
= timeline
->highest_pending
= signal_value
;
317 anv_queue_submit_deferred_locked(struct anv_queue
*queue
, uint32_t *advance
)
319 VkResult result
= VK_SUCCESS
;
321 /* Go through all the queued submissions and submit then until we find one
322 * that's waiting on a point that hasn't materialized yet.
324 list_for_each_entry_safe(struct anv_queue_submit
, submit
,
325 &queue
->queued_submits
, link
) {
326 if (!anv_queue_submit_ready_locked(submit
))
330 list_del(&submit
->link
);
332 result
= anv_queue_submit_timeline_locked(queue
, submit
);
334 anv_queue_submit_free(queue
->device
, submit
);
336 if (result
!= VK_SUCCESS
)
344 anv_device_submit_deferred_locked(struct anv_device
*device
)
346 uint32_t advance
= 0;
347 return anv_queue_submit_deferred_locked(&device
->queue
, &advance
);
351 _anv_queue_submit(struct anv_queue
*queue
, struct anv_queue_submit
**_submit
,
354 struct anv_queue_submit
*submit
= *_submit
;
356 /* Wait before signal behavior means we might keep alive the
357 * anv_queue_submit object a bit longer, so transfer the ownership to the
362 pthread_mutex_lock(&queue
->device
->mutex
);
363 list_addtail(&submit
->link
, &queue
->queued_submits
);
364 VkResult result
= anv_device_submit_deferred_locked(queue
->device
);
366 while (result
== VK_SUCCESS
&& !list_is_empty(&queue
->queued_submits
)) {
367 int ret
= pthread_cond_wait(&queue
->device
->queue_submit
,
368 &queue
->device
->mutex
);
370 result
= anv_device_set_lost(queue
->device
, "wait timeout");
374 result
= anv_device_submit_deferred_locked(queue
->device
);
377 pthread_mutex_unlock(&queue
->device
->mutex
);
382 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
384 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
385 queue
->device
= device
;
388 list_inithead(&queue
->queued_submits
);
394 anv_queue_finish(struct anv_queue
*queue
)
399 anv_queue_submit_add_fence_bo(struct anv_queue_submit
*submit
,
403 if (submit
->fence_bo_count
>= submit
->fence_bo_array_length
) {
404 uint32_t new_len
= MAX2(submit
->fence_bo_array_length
* 2, 64);
407 vk_realloc(submit
->alloc
,
408 submit
->fence_bos
, new_len
* sizeof(*submit
->fence_bos
),
409 8, submit
->alloc_scope
);
410 if (submit
->fence_bos
== NULL
)
411 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
413 submit
->fence_bo_array_length
= new_len
;
416 /* Take advantage that anv_bo are allocated at 8 byte alignement so we can
417 * use the lowest bit to store whether this is a BO we need to signal.
419 submit
->fence_bos
[submit
->fence_bo_count
++] = anv_pack_ptr(bo
, 1, signal
);
425 anv_queue_submit_add_syncobj(struct anv_queue_submit
* submit
,
426 struct anv_device
*device
,
427 uint32_t handle
, uint32_t flags
)
431 if (submit
->fence_count
>= submit
->fence_array_length
) {
432 uint32_t new_len
= MAX2(submit
->fence_array_length
* 2, 64);
435 vk_realloc(submit
->alloc
,
436 submit
->fences
, new_len
* sizeof(*submit
->fences
),
437 8, submit
->alloc_scope
);
438 if (submit
->fences
== NULL
)
439 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
441 submit
->fence_array_length
= new_len
;
444 submit
->fences
[submit
->fence_count
++] = (struct drm_i915_gem_exec_fence
) {
453 anv_queue_submit_add_sync_fd_fence(struct anv_queue_submit
*submit
,
454 struct anv_semaphore
*semaphore
)
456 if (submit
->sync_fd_semaphore_count
>= submit
->sync_fd_semaphore_array_length
) {
457 uint32_t new_len
= MAX2(submit
->sync_fd_semaphore_array_length
* 2, 64);
458 struct anv_semaphore
**new_semaphores
=
459 vk_realloc(submit
->alloc
, submit
->sync_fd_semaphores
,
460 new_len
* sizeof(*submit
->sync_fd_semaphores
), 8,
461 submit
->alloc_scope
);
462 if (new_semaphores
== NULL
)
463 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
465 submit
->sync_fd_semaphores
= new_semaphores
;
468 submit
->sync_fd_semaphores
[submit
->sync_fd_semaphore_count
++] =
469 anv_semaphore_ref(semaphore
);
470 submit
->need_out_fence
= true;
476 anv_queue_submit_add_timeline_wait(struct anv_queue_submit
* submit
,
477 struct anv_device
*device
,
478 struct anv_timeline
*timeline
,
481 if (submit
->wait_timeline_count
>= submit
->wait_timeline_array_length
) {
482 uint32_t new_len
= MAX2(submit
->wait_timeline_array_length
* 2, 64);
484 submit
->wait_timelines
=
485 vk_realloc(submit
->alloc
,
486 submit
->wait_timelines
, new_len
* sizeof(*submit
->wait_timelines
),
487 8, submit
->alloc_scope
);
488 if (submit
->wait_timelines
== NULL
)
489 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
491 submit
->wait_timeline_values
=
492 vk_realloc(submit
->alloc
,
493 submit
->wait_timeline_values
, new_len
* sizeof(*submit
->wait_timeline_values
),
494 8, submit
->alloc_scope
);
495 if (submit
->wait_timeline_values
== NULL
)
496 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
498 submit
->wait_timeline_array_length
= new_len
;
501 submit
->wait_timelines
[submit
->wait_timeline_count
] = timeline
;
502 submit
->wait_timeline_values
[submit
->wait_timeline_count
] = value
;
504 submit
->wait_timeline_count
++;
510 anv_queue_submit_add_timeline_signal(struct anv_queue_submit
* submit
,
511 struct anv_device
*device
,
512 struct anv_timeline
*timeline
,
515 assert(timeline
->highest_pending
< value
);
517 if (submit
->signal_timeline_count
>= submit
->signal_timeline_array_length
) {
518 uint32_t new_len
= MAX2(submit
->signal_timeline_array_length
* 2, 64);
520 submit
->signal_timelines
=
521 vk_realloc(submit
->alloc
,
522 submit
->signal_timelines
, new_len
* sizeof(*submit
->signal_timelines
),
523 8, submit
->alloc_scope
);
524 if (submit
->signal_timelines
== NULL
)
525 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
527 submit
->signal_timeline_values
=
528 vk_realloc(submit
->alloc
,
529 submit
->signal_timeline_values
, new_len
* sizeof(*submit
->signal_timeline_values
),
530 8, submit
->alloc_scope
);
531 if (submit
->signal_timeline_values
== NULL
)
532 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
534 submit
->signal_timeline_array_length
= new_len
;
537 submit
->signal_timelines
[submit
->signal_timeline_count
] = timeline
;
538 submit
->signal_timeline_values
[submit
->signal_timeline_count
] = value
;
540 submit
->signal_timeline_count
++;
545 static struct anv_queue_submit
*
546 anv_queue_submit_alloc(struct anv_device
*device
)
548 const VkAllocationCallbacks
*alloc
= &device
->alloc
;
549 VkSystemAllocationScope alloc_scope
= VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
;
551 struct anv_queue_submit
*submit
= vk_zalloc(alloc
, sizeof(*submit
), 8, alloc_scope
);
555 submit
->alloc
= alloc
;
556 submit
->alloc_scope
= alloc_scope
;
557 submit
->in_fence
= -1;
558 submit
->out_fence
= -1;
564 anv_queue_submit_simple_batch(struct anv_queue
*queue
,
565 struct anv_batch
*batch
)
567 struct anv_device
*device
= queue
->device
;
568 struct anv_queue_submit
*submit
= anv_queue_submit_alloc(device
);
570 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
572 bool has_syncobj_wait
= device
->physical
->has_syncobj_wait
;
575 struct anv_bo
*batch_bo
, *sync_bo
;
577 if (has_syncobj_wait
) {
578 syncobj
= anv_gem_syncobj_create(device
, 0);
580 result
= vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
581 goto err_free_submit
;
584 result
= anv_queue_submit_add_syncobj(submit
, device
, syncobj
,
585 I915_EXEC_FENCE_SIGNAL
);
587 result
= anv_device_alloc_bo(device
, 4096,
588 ANV_BO_ALLOC_EXTERNAL
|
589 ANV_BO_ALLOC_IMPLICIT_SYNC
,
590 0 /* explicit_address */,
592 if (result
!= VK_SUCCESS
)
593 goto err_free_submit
;
595 result
= anv_queue_submit_add_fence_bo(submit
, sync_bo
, true /* signal */);
598 if (result
!= VK_SUCCESS
)
599 goto err_destroy_sync_primitive
;
602 uint32_t size
= align_u32(batch
->next
- batch
->start
, 8);
603 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, size
, &batch_bo
);
604 if (result
!= VK_SUCCESS
)
605 goto err_destroy_sync_primitive
;
607 memcpy(batch_bo
->map
, batch
->start
, size
);
608 if (!device
->info
.has_llc
)
609 gen_flush_range(batch_bo
->map
, size
);
611 submit
->simple_bo
= batch_bo
;
612 submit
->simple_bo_size
= size
;
615 result
= _anv_queue_submit(queue
, &submit
, true);
617 if (result
== VK_SUCCESS
) {
618 if (has_syncobj_wait
) {
619 if (anv_gem_syncobj_wait(device
, &syncobj
, 1,
620 anv_get_absolute_timeout(INT64_MAX
), true))
621 result
= anv_device_set_lost(device
, "anv_gem_syncobj_wait failed: %m");
622 anv_gem_syncobj_destroy(device
, syncobj
);
624 result
= anv_device_wait(device
, sync_bo
,
625 anv_get_relative_timeout(INT64_MAX
));
626 anv_device_release_bo(device
, sync_bo
);
631 anv_bo_pool_free(&device
->batch_bo_pool
, batch_bo
);
634 anv_queue_submit_free(device
, submit
);
638 err_destroy_sync_primitive
:
639 if (has_syncobj_wait
)
640 anv_gem_syncobj_destroy(device
, syncobj
);
642 anv_device_release_bo(device
, sync_bo
);
645 anv_queue_submit_free(device
, submit
);
650 /* Transfer ownership of temporary semaphores from the VkSemaphore object to
651 * the anv_queue_submit object. Those temporary semaphores are then freed in
652 * anv_queue_submit_free() once the driver is finished with them.
655 maybe_transfer_temporary_semaphore(struct anv_queue_submit
*submit
,
656 struct anv_semaphore
*semaphore
,
657 struct anv_semaphore_impl
**out_impl
)
659 struct anv_semaphore_impl
*impl
= &semaphore
->temporary
;
661 if (impl
->type
== ANV_SEMAPHORE_TYPE_NONE
) {
662 *out_impl
= &semaphore
->permanent
;
666 /* BO backed timeline semaphores cannot be temporary. */
667 assert(impl
->type
!= ANV_SEMAPHORE_TYPE_TIMELINE
);
670 * There is a requirement to reset semaphore to their permanent state after
671 * submission. From the Vulkan 1.0.53 spec:
673 * "If the import is temporary, the implementation must restore the
674 * semaphore to its prior permanent state after submitting the next
675 * semaphore wait operation."
677 * In the case we defer the actual submission to a thread because of the
678 * wait-before-submit behavior required for timeline semaphores, we need to
679 * make copies of the temporary syncobj to ensure they stay alive until we
680 * do the actual execbuffer ioctl.
682 if (submit
->temporary_semaphore_count
>= submit
->temporary_semaphore_array_length
) {
683 uint32_t new_len
= MAX2(submit
->temporary_semaphore_array_length
* 2, 8);
684 /* Make sure that if the realloc fails, we still have the old semaphore
685 * array around to properly clean things up on failure.
687 struct anv_semaphore_impl
*new_array
=
688 vk_realloc(submit
->alloc
,
689 submit
->temporary_semaphores
,
690 new_len
* sizeof(*submit
->temporary_semaphores
),
691 8, submit
->alloc_scope
);
692 if (new_array
== NULL
)
693 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
695 submit
->temporary_semaphores
= new_array
;
696 submit
->temporary_semaphore_array_length
= new_len
;
699 /* Copy anv_semaphore_impl into anv_queue_submit. */
700 submit
->temporary_semaphores
[submit
->temporary_semaphore_count
++] = *impl
;
701 *out_impl
= &submit
->temporary_semaphores
[submit
->temporary_semaphore_count
- 1];
703 /* Clear the incoming semaphore */
704 impl
->type
= ANV_SEMAPHORE_TYPE_NONE
;
710 anv_queue_submit(struct anv_queue
*queue
,
711 struct anv_cmd_buffer
*cmd_buffer
,
712 const VkSemaphore
*in_semaphores
,
713 const uint64_t *in_values
,
714 uint32_t num_in_semaphores
,
715 const VkSemaphore
*out_semaphores
,
716 const uint64_t *out_values
,
717 uint32_t num_out_semaphores
,
718 struct anv_bo
*wsi_signal_bo
,
721 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
722 struct anv_device
*device
= queue
->device
;
723 UNUSED
struct anv_physical_device
*pdevice
= device
->physical
;
724 struct anv_queue_submit
*submit
= anv_queue_submit_alloc(device
);
726 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
728 submit
->cmd_buffer
= cmd_buffer
;
730 VkResult result
= VK_SUCCESS
;
732 for (uint32_t i
= 0; i
< num_in_semaphores
; i
++) {
733 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, in_semaphores
[i
]);
734 struct anv_semaphore_impl
*impl
;
736 result
= maybe_transfer_temporary_semaphore(submit
, semaphore
, &impl
);
737 if (result
!= VK_SUCCESS
)
740 switch (impl
->type
) {
741 case ANV_SEMAPHORE_TYPE_BO
:
742 assert(!pdevice
->has_syncobj
);
743 result
= anv_queue_submit_add_fence_bo(submit
, impl
->bo
, false /* signal */);
744 if (result
!= VK_SUCCESS
)
748 case ANV_SEMAPHORE_TYPE_WSI_BO
:
749 /* When using a window-system buffer as a semaphore, always enable
750 * EXEC_OBJECT_WRITE. This gives us a WaR hazard with the display or
751 * compositor's read of the buffer and enforces that we don't start
752 * rendering until they are finished. This is exactly the
753 * synchronization we want with vkAcquireNextImage.
755 result
= anv_queue_submit_add_fence_bo(submit
, impl
->bo
, true /* signal */);
756 if (result
!= VK_SUCCESS
)
760 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
761 assert(!pdevice
->has_syncobj
);
762 if (submit
->in_fence
== -1) {
763 submit
->in_fence
= impl
->fd
;
764 if (submit
->in_fence
== -1) {
765 result
= vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
770 int merge
= anv_gem_sync_file_merge(device
, submit
->in_fence
, impl
->fd
);
772 result
= vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
776 close(submit
->in_fence
);
778 submit
->in_fence
= merge
;
782 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
: {
783 result
= anv_queue_submit_add_syncobj(submit
, device
,
785 I915_EXEC_FENCE_WAIT
);
786 if (result
!= VK_SUCCESS
)
791 case ANV_SEMAPHORE_TYPE_TIMELINE
:
792 result
= anv_queue_submit_add_timeline_wait(submit
, device
,
794 in_values
? in_values
[i
] : 0);
795 if (result
!= VK_SUCCESS
)
804 for (uint32_t i
= 0; i
< num_out_semaphores
; i
++) {
805 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, out_semaphores
[i
]);
807 /* Under most circumstances, out fences won't be temporary. However,
808 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
810 * "If the import is temporary, the implementation must restore the
811 * semaphore to its prior permanent state after submitting the next
812 * semaphore wait operation."
814 * The spec says nothing whatsoever about signal operations on
815 * temporarily imported semaphores so it appears they are allowed.
816 * There are also CTS tests that require this to work.
818 struct anv_semaphore_impl
*impl
=
819 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
820 &semaphore
->temporary
: &semaphore
->permanent
;
822 switch (impl
->type
) {
823 case ANV_SEMAPHORE_TYPE_BO
:
824 assert(!pdevice
->has_syncobj
);
825 result
= anv_queue_submit_add_fence_bo(submit
, impl
->bo
, true /* signal */);
826 if (result
!= VK_SUCCESS
)
830 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
831 assert(!pdevice
->has_syncobj
);
832 result
= anv_queue_submit_add_sync_fd_fence(submit
, semaphore
);
833 if (result
!= VK_SUCCESS
)
837 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
: {
838 result
= anv_queue_submit_add_syncobj(submit
, device
, impl
->syncobj
,
839 I915_EXEC_FENCE_SIGNAL
);
840 if (result
!= VK_SUCCESS
)
845 case ANV_SEMAPHORE_TYPE_TIMELINE
:
846 result
= anv_queue_submit_add_timeline_signal(submit
, device
,
848 out_values
? out_values
[i
] : 0);
849 if (result
!= VK_SUCCESS
)
859 result
= anv_queue_submit_add_fence_bo(submit
, wsi_signal_bo
, true /* signal */);
860 if (result
!= VK_SUCCESS
)
865 /* Under most circumstances, out fences won't be temporary. However,
866 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
868 * "If the import is temporary, the implementation must restore the
869 * semaphore to its prior permanent state after submitting the next
870 * semaphore wait operation."
872 * The spec says nothing whatsoever about signal operations on
873 * temporarily imported semaphores so it appears they are allowed.
874 * There are also CTS tests that require this to work.
876 struct anv_fence_impl
*impl
=
877 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
878 &fence
->temporary
: &fence
->permanent
;
880 switch (impl
->type
) {
881 case ANV_FENCE_TYPE_BO
:
882 result
= anv_queue_submit_add_fence_bo(submit
, impl
->bo
.bo
, true /* signal */);
883 if (result
!= VK_SUCCESS
)
887 case ANV_FENCE_TYPE_SYNCOBJ
: {
889 * For the same reason we reset the signaled binary syncobj above,
890 * also reset the fence's syncobj so that they don't contain a
891 * signaled dma-fence.
893 result
= anv_queue_submit_add_syncobj(submit
, device
, impl
->syncobj
,
894 I915_EXEC_FENCE_SIGNAL
);
895 if (result
!= VK_SUCCESS
)
901 unreachable("Invalid fence type");
905 result
= _anv_queue_submit(queue
, &submit
, false);
906 if (result
!= VK_SUCCESS
)
909 if (fence
&& fence
->permanent
.type
== ANV_FENCE_TYPE_BO
) {
910 /* If we have permanent BO fence, the only type of temporary possible
911 * would be BO_WSI (because BO fences are not shareable). The Vulkan spec
912 * also requires that the fence passed to vkQueueSubmit() be :
915 * * not be associated with any other queue command that has not yet
916 * completed execution on that queue
918 * So the only acceptable type for the temporary is NONE.
920 assert(fence
->temporary
.type
== ANV_FENCE_TYPE_NONE
);
922 /* Once the execbuf has returned, we need to set the fence state to
923 * SUBMITTED. We can't do this before calling execbuf because
924 * anv_GetFenceStatus does take the global device lock before checking
927 * We set the fence state to SUBMITTED regardless of whether or not the
928 * execbuf succeeds because we need to ensure that vkWaitForFences() and
929 * vkGetFenceStatus() return a valid result (VK_ERROR_DEVICE_LOST or
930 * VK_SUCCESS) in a finite amount of time even if execbuf fails.
932 fence
->permanent
.bo
.state
= ANV_BO_FENCE_STATE_SUBMITTED
;
937 anv_queue_submit_free(device
, submit
);
942 VkResult
anv_QueueSubmit(
944 uint32_t submitCount
,
945 const VkSubmitInfo
* pSubmits
,
948 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
950 /* Query for device status prior to submitting. Technically, we don't need
951 * to do this. However, if we have a client that's submitting piles of
952 * garbage, we would rather break as early as possible to keep the GPU
953 * hanging contained. If we don't check here, we'll either be waiting for
954 * the kernel to kick us or we'll have to wait until the client waits on a
955 * fence before we actually know whether or not we've hung.
957 VkResult result
= anv_device_query_status(queue
->device
);
958 if (result
!= VK_SUCCESS
)
961 if (fence
&& submitCount
== 0) {
962 /* If we don't have any command buffers, we need to submit a dummy
963 * batch to give GEM something to wait on. We could, potentially,
964 * come up with something more efficient but this shouldn't be a
967 result
= anv_queue_submit(queue
, NULL
, NULL
, NULL
, 0, NULL
, NULL
, 0,
972 for (uint32_t i
= 0; i
< submitCount
; i
++) {
973 /* Fence for this submit. NULL for all but the last one */
974 VkFence submit_fence
= (i
== submitCount
- 1) ? fence
: VK_NULL_HANDLE
;
976 const struct wsi_memory_signal_submit_info
*mem_signal_info
=
977 vk_find_struct_const(pSubmits
[i
].pNext
,
978 WSI_MEMORY_SIGNAL_SUBMIT_INFO_MESA
);
979 struct anv_bo
*wsi_signal_bo
=
980 mem_signal_info
&& mem_signal_info
->memory
!= VK_NULL_HANDLE
?
981 anv_device_memory_from_handle(mem_signal_info
->memory
)->bo
: NULL
;
983 const VkTimelineSemaphoreSubmitInfoKHR
*timeline_info
=
984 vk_find_struct_const(pSubmits
[i
].pNext
,
985 TIMELINE_SEMAPHORE_SUBMIT_INFO_KHR
);
986 const uint64_t *wait_values
=
987 timeline_info
&& timeline_info
->waitSemaphoreValueCount
?
988 timeline_info
->pWaitSemaphoreValues
: NULL
;
989 const uint64_t *signal_values
=
990 timeline_info
&& timeline_info
->signalSemaphoreValueCount
?
991 timeline_info
->pSignalSemaphoreValues
: NULL
;
993 if (pSubmits
[i
].commandBufferCount
== 0) {
994 /* If we don't have any command buffers, we need to submit a dummy
995 * batch to give GEM something to wait on. We could, potentially,
996 * come up with something more efficient but this shouldn't be a
999 result
= anv_queue_submit(queue
, NULL
,
1000 pSubmits
[i
].pWaitSemaphores
,
1002 pSubmits
[i
].waitSemaphoreCount
,
1003 pSubmits
[i
].pSignalSemaphores
,
1005 pSubmits
[i
].signalSemaphoreCount
,
1008 if (result
!= VK_SUCCESS
)
1014 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
1015 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
1016 pSubmits
[i
].pCommandBuffers
[j
]);
1017 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
1018 assert(!anv_batch_has_error(&cmd_buffer
->batch
));
1020 /* Fence for this execbuf. NULL for all but the last one */
1021 VkFence execbuf_fence
=
1022 (j
== pSubmits
[i
].commandBufferCount
- 1) ?
1023 submit_fence
: VK_NULL_HANDLE
;
1025 const VkSemaphore
*in_semaphores
= NULL
, *out_semaphores
= NULL
;
1026 const uint64_t *in_values
= NULL
, *out_values
= NULL
;
1027 uint32_t num_in_semaphores
= 0, num_out_semaphores
= 0;
1029 /* Only the first batch gets the in semaphores */
1030 in_semaphores
= pSubmits
[i
].pWaitSemaphores
;
1031 in_values
= wait_values
;
1032 num_in_semaphores
= pSubmits
[i
].waitSemaphoreCount
;
1035 if (j
== pSubmits
[i
].commandBufferCount
- 1) {
1036 /* Only the last batch gets the out semaphores */
1037 out_semaphores
= pSubmits
[i
].pSignalSemaphores
;
1038 out_values
= signal_values
;
1039 num_out_semaphores
= pSubmits
[i
].signalSemaphoreCount
;
1042 result
= anv_queue_submit(queue
, cmd_buffer
,
1043 in_semaphores
, in_values
, num_in_semaphores
,
1044 out_semaphores
, out_values
, num_out_semaphores
,
1045 wsi_signal_bo
, execbuf_fence
);
1046 if (result
!= VK_SUCCESS
)
1052 if (result
!= VK_SUCCESS
&& result
!= VK_ERROR_DEVICE_LOST
) {
1053 /* In the case that something has gone wrong we may end up with an
1054 * inconsistent state from which it may not be trivial to recover.
1055 * For example, we might have computed address relocations and
1056 * any future attempt to re-submit this job will need to know about
1057 * this and avoid computing relocation addresses again.
1059 * To avoid this sort of issues, we assume that if something was
1060 * wrong during submission we must already be in a really bad situation
1061 * anyway (such us being out of memory) and return
1062 * VK_ERROR_DEVICE_LOST to ensure that clients do not attempt to
1063 * submit the same job again to this device.
1065 * We skip doing this on VK_ERROR_DEVICE_LOST because
1066 * anv_device_set_lost() would have been called already by a callee of
1067 * anv_queue_submit().
1069 result
= anv_device_set_lost(queue
->device
, "vkQueueSubmit() failed");
1075 VkResult
anv_QueueWaitIdle(
1078 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1080 if (anv_device_is_lost(queue
->device
))
1081 return VK_ERROR_DEVICE_LOST
;
1083 return anv_queue_submit_simple_batch(queue
, NULL
);
1086 VkResult
anv_CreateFence(
1088 const VkFenceCreateInfo
* pCreateInfo
,
1089 const VkAllocationCallbacks
* pAllocator
,
1092 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1093 struct anv_fence
*fence
;
1095 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1097 fence
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*fence
), 8,
1098 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1100 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1102 if (device
->physical
->has_syncobj_wait
) {
1103 fence
->permanent
.type
= ANV_FENCE_TYPE_SYNCOBJ
;
1105 uint32_t create_flags
= 0;
1106 if (pCreateInfo
->flags
& VK_FENCE_CREATE_SIGNALED_BIT
)
1107 create_flags
|= DRM_SYNCOBJ_CREATE_SIGNALED
;
1109 fence
->permanent
.syncobj
= anv_gem_syncobj_create(device
, create_flags
);
1110 if (!fence
->permanent
.syncobj
)
1111 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1113 fence
->permanent
.type
= ANV_FENCE_TYPE_BO
;
1115 VkResult result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, 4096,
1116 &fence
->permanent
.bo
.bo
);
1117 if (result
!= VK_SUCCESS
)
1120 if (pCreateInfo
->flags
& VK_FENCE_CREATE_SIGNALED_BIT
) {
1121 fence
->permanent
.bo
.state
= ANV_BO_FENCE_STATE_SIGNALED
;
1123 fence
->permanent
.bo
.state
= ANV_BO_FENCE_STATE_RESET
;
1127 *pFence
= anv_fence_to_handle(fence
);
1133 anv_fence_impl_cleanup(struct anv_device
*device
,
1134 struct anv_fence_impl
*impl
)
1136 switch (impl
->type
) {
1137 case ANV_FENCE_TYPE_NONE
:
1138 /* Dummy. Nothing to do */
1141 case ANV_FENCE_TYPE_BO
:
1142 anv_bo_pool_free(&device
->batch_bo_pool
, impl
->bo
.bo
);
1145 case ANV_FENCE_TYPE_WSI_BO
:
1146 anv_device_release_bo(device
, impl
->bo
.bo
);
1149 case ANV_FENCE_TYPE_SYNCOBJ
:
1150 anv_gem_syncobj_destroy(device
, impl
->syncobj
);
1153 case ANV_FENCE_TYPE_WSI
:
1154 impl
->fence_wsi
->destroy(impl
->fence_wsi
);
1158 unreachable("Invalid fence type");
1161 impl
->type
= ANV_FENCE_TYPE_NONE
;
1165 anv_fence_reset_temporary(struct anv_device
*device
,
1166 struct anv_fence
*fence
)
1168 if (fence
->temporary
.type
== ANV_FENCE_TYPE_NONE
)
1171 anv_fence_impl_cleanup(device
, &fence
->temporary
);
1174 void anv_DestroyFence(
1177 const VkAllocationCallbacks
* pAllocator
)
1179 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1180 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1185 anv_fence_impl_cleanup(device
, &fence
->temporary
);
1186 anv_fence_impl_cleanup(device
, &fence
->permanent
);
1188 vk_free2(&device
->alloc
, pAllocator
, fence
);
1191 VkResult
anv_ResetFences(
1193 uint32_t fenceCount
,
1194 const VkFence
* pFences
)
1196 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1198 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1199 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1201 /* From the Vulkan 1.0.53 spec:
1203 * "If any member of pFences currently has its payload imported with
1204 * temporary permanence, that fence’s prior permanent payload is
1205 * first restored. The remaining operations described therefore
1206 * operate on the restored payload.
1208 anv_fence_reset_temporary(device
, fence
);
1210 struct anv_fence_impl
*impl
= &fence
->permanent
;
1212 switch (impl
->type
) {
1213 case ANV_FENCE_TYPE_BO
:
1214 impl
->bo
.state
= ANV_BO_FENCE_STATE_RESET
;
1217 case ANV_FENCE_TYPE_SYNCOBJ
:
1218 anv_gem_syncobj_reset(device
, impl
->syncobj
);
1222 unreachable("Invalid fence type");
1229 VkResult
anv_GetFenceStatus(
1233 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1234 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1236 if (anv_device_is_lost(device
))
1237 return VK_ERROR_DEVICE_LOST
;
1239 struct anv_fence_impl
*impl
=
1240 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1241 &fence
->temporary
: &fence
->permanent
;
1243 switch (impl
->type
) {
1244 case ANV_FENCE_TYPE_BO
:
1245 case ANV_FENCE_TYPE_WSI_BO
:
1246 switch (impl
->bo
.state
) {
1247 case ANV_BO_FENCE_STATE_RESET
:
1248 /* If it hasn't even been sent off to the GPU yet, it's not ready */
1249 return VK_NOT_READY
;
1251 case ANV_BO_FENCE_STATE_SIGNALED
:
1252 /* It's been signaled, return success */
1255 case ANV_BO_FENCE_STATE_SUBMITTED
: {
1256 VkResult result
= anv_device_bo_busy(device
, impl
->bo
.bo
);
1257 if (result
== VK_SUCCESS
) {
1258 impl
->bo
.state
= ANV_BO_FENCE_STATE_SIGNALED
;
1265 unreachable("Invalid fence status");
1268 case ANV_FENCE_TYPE_SYNCOBJ
: {
1269 int ret
= anv_gem_syncobj_wait(device
, &impl
->syncobj
, 1, 0, true);
1271 if (errno
== ETIME
) {
1272 return VK_NOT_READY
;
1274 /* We don't know the real error. */
1275 return anv_device_set_lost(device
, "drm_syncobj_wait failed: %m");
1283 unreachable("Invalid fence type");
1288 anv_wait_for_syncobj_fences(struct anv_device
*device
,
1289 uint32_t fenceCount
,
1290 const VkFence
*pFences
,
1292 uint64_t abs_timeout_ns
)
1294 uint32_t *syncobjs
= vk_zalloc(&device
->alloc
,
1295 sizeof(*syncobjs
) * fenceCount
, 8,
1296 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1298 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1300 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1301 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1302 assert(fence
->permanent
.type
== ANV_FENCE_TYPE_SYNCOBJ
);
1304 struct anv_fence_impl
*impl
=
1305 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1306 &fence
->temporary
: &fence
->permanent
;
1308 assert(impl
->type
== ANV_FENCE_TYPE_SYNCOBJ
);
1309 syncobjs
[i
] = impl
->syncobj
;
1312 /* The gem_syncobj_wait ioctl may return early due to an inherent
1313 * limitation in the way it computes timeouts. Loop until we've actually
1314 * passed the timeout.
1318 ret
= anv_gem_syncobj_wait(device
, syncobjs
, fenceCount
,
1319 abs_timeout_ns
, waitAll
);
1320 } while (ret
== -1 && errno
== ETIME
&& anv_gettime_ns() < abs_timeout_ns
);
1322 vk_free(&device
->alloc
, syncobjs
);
1325 if (errno
== ETIME
) {
1328 /* We don't know the real error. */
1329 return anv_device_set_lost(device
, "drm_syncobj_wait failed: %m");
1337 anv_wait_for_bo_fences(struct anv_device
*device
,
1338 uint32_t fenceCount
,
1339 const VkFence
*pFences
,
1341 uint64_t abs_timeout_ns
)
1343 VkResult result
= VK_SUCCESS
;
1344 uint32_t pending_fences
= fenceCount
;
1345 while (pending_fences
) {
1347 bool signaled_fences
= false;
1348 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1349 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1351 struct anv_fence_impl
*impl
=
1352 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1353 &fence
->temporary
: &fence
->permanent
;
1354 assert(impl
->type
== ANV_FENCE_TYPE_BO
||
1355 impl
->type
== ANV_FENCE_TYPE_WSI_BO
);
1357 switch (impl
->bo
.state
) {
1358 case ANV_BO_FENCE_STATE_RESET
:
1359 /* This fence hasn't been submitted yet, we'll catch it the next
1360 * time around. Yes, this may mean we dead-loop but, short of
1361 * lots of locking and a condition variable, there's not much that
1362 * we can do about that.
1367 case ANV_BO_FENCE_STATE_SIGNALED
:
1368 /* This fence is not pending. If waitAll isn't set, we can return
1369 * early. Otherwise, we have to keep going.
1372 result
= VK_SUCCESS
;
1377 case ANV_BO_FENCE_STATE_SUBMITTED
:
1378 /* These are the fences we really care about. Go ahead and wait
1379 * on it until we hit a timeout.
1381 result
= anv_device_wait(device
, impl
->bo
.bo
,
1382 anv_get_relative_timeout(abs_timeout_ns
));
1385 impl
->bo
.state
= ANV_BO_FENCE_STATE_SIGNALED
;
1386 signaled_fences
= true;
1400 if (pending_fences
&& !signaled_fences
) {
1401 /* If we've hit this then someone decided to vkWaitForFences before
1402 * they've actually submitted any of them to a queue. This is a
1403 * fairly pessimal case, so it's ok to lock here and use a standard
1404 * pthreads condition variable.
1406 pthread_mutex_lock(&device
->mutex
);
1408 /* It's possible that some of the fences have changed state since the
1409 * last time we checked. Now that we have the lock, check for
1410 * pending fences again and don't wait if it's changed.
1412 uint32_t now_pending_fences
= 0;
1413 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1414 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1415 if (fence
->permanent
.bo
.state
== ANV_BO_FENCE_STATE_RESET
)
1416 now_pending_fences
++;
1418 assert(now_pending_fences
<= pending_fences
);
1420 if (now_pending_fences
== pending_fences
) {
1421 struct timespec abstime
= {
1422 .tv_sec
= abs_timeout_ns
/ NSEC_PER_SEC
,
1423 .tv_nsec
= abs_timeout_ns
% NSEC_PER_SEC
,
1427 ret
= pthread_cond_timedwait(&device
->queue_submit
,
1428 &device
->mutex
, &abstime
);
1429 assert(ret
!= EINVAL
);
1430 if (anv_gettime_ns() >= abs_timeout_ns
) {
1431 pthread_mutex_unlock(&device
->mutex
);
1432 result
= VK_TIMEOUT
;
1437 pthread_mutex_unlock(&device
->mutex
);
1442 if (anv_device_is_lost(device
))
1443 return VK_ERROR_DEVICE_LOST
;
1449 anv_wait_for_wsi_fence(struct anv_device
*device
,
1450 struct anv_fence_impl
*impl
,
1451 uint64_t abs_timeout
)
1453 return impl
->fence_wsi
->wait(impl
->fence_wsi
, abs_timeout
);
1457 anv_wait_for_fences(struct anv_device
*device
,
1458 uint32_t fenceCount
,
1459 const VkFence
*pFences
,
1461 uint64_t abs_timeout
)
1463 VkResult result
= VK_SUCCESS
;
1465 if (fenceCount
<= 1 || waitAll
) {
1466 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1467 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1468 struct anv_fence_impl
*impl
=
1469 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1470 &fence
->temporary
: &fence
->permanent
;
1472 switch (impl
->type
) {
1473 case ANV_FENCE_TYPE_BO
:
1474 case ANV_FENCE_TYPE_WSI_BO
:
1475 result
= anv_wait_for_bo_fences(device
, 1, &pFences
[i
],
1478 case ANV_FENCE_TYPE_SYNCOBJ
:
1479 result
= anv_wait_for_syncobj_fences(device
, 1, &pFences
[i
],
1482 case ANV_FENCE_TYPE_WSI
:
1483 result
= anv_wait_for_wsi_fence(device
, impl
, abs_timeout
);
1485 case ANV_FENCE_TYPE_NONE
:
1486 result
= VK_SUCCESS
;
1489 if (result
!= VK_SUCCESS
)
1494 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1495 if (anv_wait_for_fences(device
, 1, &pFences
[i
], true, 0) == VK_SUCCESS
)
1498 } while (anv_gettime_ns() < abs_timeout
);
1499 result
= VK_TIMEOUT
;
1504 static bool anv_all_fences_syncobj(uint32_t fenceCount
, const VkFence
*pFences
)
1506 for (uint32_t i
= 0; i
< fenceCount
; ++i
) {
1507 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1508 struct anv_fence_impl
*impl
=
1509 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1510 &fence
->temporary
: &fence
->permanent
;
1511 if (impl
->type
!= ANV_FENCE_TYPE_SYNCOBJ
)
1517 static bool anv_all_fences_bo(uint32_t fenceCount
, const VkFence
*pFences
)
1519 for (uint32_t i
= 0; i
< fenceCount
; ++i
) {
1520 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1521 struct anv_fence_impl
*impl
=
1522 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1523 &fence
->temporary
: &fence
->permanent
;
1524 if (impl
->type
!= ANV_FENCE_TYPE_BO
&&
1525 impl
->type
!= ANV_FENCE_TYPE_WSI_BO
)
1531 VkResult
anv_WaitForFences(
1533 uint32_t fenceCount
,
1534 const VkFence
* pFences
,
1538 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1540 if (anv_device_is_lost(device
))
1541 return VK_ERROR_DEVICE_LOST
;
1543 uint64_t abs_timeout
= anv_get_absolute_timeout(timeout
);
1544 if (anv_all_fences_syncobj(fenceCount
, pFences
)) {
1545 return anv_wait_for_syncobj_fences(device
, fenceCount
, pFences
,
1546 waitAll
, abs_timeout
);
1547 } else if (anv_all_fences_bo(fenceCount
, pFences
)) {
1548 return anv_wait_for_bo_fences(device
, fenceCount
, pFences
,
1549 waitAll
, abs_timeout
);
1551 return anv_wait_for_fences(device
, fenceCount
, pFences
,
1552 waitAll
, abs_timeout
);
1556 void anv_GetPhysicalDeviceExternalFenceProperties(
1557 VkPhysicalDevice physicalDevice
,
1558 const VkPhysicalDeviceExternalFenceInfo
* pExternalFenceInfo
,
1559 VkExternalFenceProperties
* pExternalFenceProperties
)
1561 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1563 switch (pExternalFenceInfo
->handleType
) {
1564 case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT
:
1565 case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
:
1566 if (device
->has_syncobj_wait
) {
1567 pExternalFenceProperties
->exportFromImportedHandleTypes
=
1568 VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT
|
1569 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
;
1570 pExternalFenceProperties
->compatibleHandleTypes
=
1571 VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT
|
1572 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
;
1573 pExternalFenceProperties
->externalFenceFeatures
=
1574 VK_EXTERNAL_FENCE_FEATURE_EXPORTABLE_BIT
|
1575 VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT
;
1584 pExternalFenceProperties
->exportFromImportedHandleTypes
= 0;
1585 pExternalFenceProperties
->compatibleHandleTypes
= 0;
1586 pExternalFenceProperties
->externalFenceFeatures
= 0;
1589 VkResult
anv_ImportFenceFdKHR(
1591 const VkImportFenceFdInfoKHR
* pImportFenceFdInfo
)
1593 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1594 ANV_FROM_HANDLE(anv_fence
, fence
, pImportFenceFdInfo
->fence
);
1595 int fd
= pImportFenceFdInfo
->fd
;
1597 assert(pImportFenceFdInfo
->sType
==
1598 VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR
);
1600 struct anv_fence_impl new_impl
= {
1601 .type
= ANV_FENCE_TYPE_NONE
,
1604 switch (pImportFenceFdInfo
->handleType
) {
1605 case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT
:
1606 new_impl
.type
= ANV_FENCE_TYPE_SYNCOBJ
;
1608 new_impl
.syncobj
= anv_gem_syncobj_fd_to_handle(device
, fd
);
1609 if (!new_impl
.syncobj
)
1610 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1614 case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
:
1615 /* Sync files are a bit tricky. Because we want to continue using the
1616 * syncobj implementation of WaitForFences, we don't use the sync file
1617 * directly but instead import it into a syncobj.
1619 new_impl
.type
= ANV_FENCE_TYPE_SYNCOBJ
;
1621 new_impl
.syncobj
= anv_gem_syncobj_create(device
, 0);
1622 if (!new_impl
.syncobj
)
1623 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1625 if (anv_gem_syncobj_import_sync_file(device
, new_impl
.syncobj
, fd
)) {
1626 anv_gem_syncobj_destroy(device
, new_impl
.syncobj
);
1627 return vk_errorf(device
, NULL
, VK_ERROR_INVALID_EXTERNAL_HANDLE
,
1628 "syncobj sync file import failed: %m");
1633 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1636 /* From the Vulkan 1.0.53 spec:
1638 * "Importing a fence payload from a file descriptor transfers
1639 * ownership of the file descriptor from the application to the
1640 * Vulkan implementation. The application must not perform any
1641 * operations on the file descriptor after a successful import."
1643 * If the import fails, we leave the file descriptor open.
1647 if (pImportFenceFdInfo
->flags
& VK_FENCE_IMPORT_TEMPORARY_BIT
) {
1648 anv_fence_impl_cleanup(device
, &fence
->temporary
);
1649 fence
->temporary
= new_impl
;
1651 anv_fence_impl_cleanup(device
, &fence
->permanent
);
1652 fence
->permanent
= new_impl
;
1658 VkResult
anv_GetFenceFdKHR(
1660 const VkFenceGetFdInfoKHR
* pGetFdInfo
,
1663 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1664 ANV_FROM_HANDLE(anv_fence
, fence
, pGetFdInfo
->fence
);
1666 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_GET_FD_INFO_KHR
);
1668 struct anv_fence_impl
*impl
=
1669 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1670 &fence
->temporary
: &fence
->permanent
;
1672 assert(impl
->type
== ANV_FENCE_TYPE_SYNCOBJ
);
1673 switch (pGetFdInfo
->handleType
) {
1674 case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT
: {
1675 int fd
= anv_gem_syncobj_handle_to_fd(device
, impl
->syncobj
);
1677 return vk_error(VK_ERROR_TOO_MANY_OBJECTS
);
1683 case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
: {
1684 int fd
= anv_gem_syncobj_export_sync_file(device
, impl
->syncobj
);
1686 return vk_error(VK_ERROR_TOO_MANY_OBJECTS
);
1693 unreachable("Invalid fence export handle type");
1696 /* From the Vulkan 1.0.53 spec:
1698 * "Export operations have the same transference as the specified handle
1699 * type’s import operations. [...] If the fence was using a
1700 * temporarily imported payload, the fence’s prior permanent payload
1703 if (impl
== &fence
->temporary
)
1704 anv_fence_impl_cleanup(device
, impl
);
1709 // Queue semaphore functions
1711 static VkSemaphoreTypeKHR
1712 get_semaphore_type(const void *pNext
, uint64_t *initial_value
)
1714 const VkSemaphoreTypeCreateInfoKHR
*type_info
=
1715 vk_find_struct_const(pNext
, SEMAPHORE_TYPE_CREATE_INFO_KHR
);
1718 return VK_SEMAPHORE_TYPE_BINARY_KHR
;
1721 *initial_value
= type_info
->initialValue
;
1722 return type_info
->semaphoreType
;
1726 binary_semaphore_create(struct anv_device
*device
,
1727 struct anv_semaphore_impl
*impl
,
1730 if (device
->physical
->has_syncobj
) {
1731 impl
->type
= ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
;
1732 impl
->syncobj
= anv_gem_syncobj_create(device
, 0);
1734 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1737 impl
->type
= ANV_SEMAPHORE_TYPE_BO
;
1739 anv_device_alloc_bo(device
, 4096,
1740 ANV_BO_ALLOC_EXTERNAL
|
1741 ANV_BO_ALLOC_IMPLICIT_SYNC
,
1742 0 /* explicit_address */,
1744 /* If we're going to use this as a fence, we need to *not* have the
1745 * EXEC_OBJECT_ASYNC bit set.
1747 assert(!(impl
->bo
->flags
& EXEC_OBJECT_ASYNC
));
1753 timeline_semaphore_create(struct anv_device
*device
,
1754 struct anv_semaphore_impl
*impl
,
1755 uint64_t initial_value
)
1757 impl
->type
= ANV_SEMAPHORE_TYPE_TIMELINE
;
1758 anv_timeline_init(device
, &impl
->timeline
, initial_value
);
1762 VkResult
anv_CreateSemaphore(
1764 const VkSemaphoreCreateInfo
* pCreateInfo
,
1765 const VkAllocationCallbacks
* pAllocator
,
1766 VkSemaphore
* pSemaphore
)
1768 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1769 struct anv_semaphore
*semaphore
;
1771 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO
);
1773 uint64_t timeline_value
= 0;
1774 VkSemaphoreTypeKHR sem_type
= get_semaphore_type(pCreateInfo
->pNext
, &timeline_value
);
1776 semaphore
= vk_alloc(&device
->alloc
, sizeof(*semaphore
), 8,
1777 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1778 if (semaphore
== NULL
)
1779 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1781 p_atomic_set(&semaphore
->refcount
, 1);
1783 const VkExportSemaphoreCreateInfo
*export
=
1784 vk_find_struct_const(pCreateInfo
->pNext
, EXPORT_SEMAPHORE_CREATE_INFO
);
1785 VkExternalSemaphoreHandleTypeFlags handleTypes
=
1786 export
? export
->handleTypes
: 0;
1789 if (handleTypes
== 0) {
1790 if (sem_type
== VK_SEMAPHORE_TYPE_BINARY_KHR
)
1791 result
= binary_semaphore_create(device
, &semaphore
->permanent
, false);
1793 result
= timeline_semaphore_create(device
, &semaphore
->permanent
, timeline_value
);
1794 if (result
!= VK_SUCCESS
) {
1795 vk_free2(&device
->alloc
, pAllocator
, semaphore
);
1798 } else if (handleTypes
& VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
) {
1799 assert(handleTypes
== VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
);
1800 assert(sem_type
== VK_SEMAPHORE_TYPE_BINARY_KHR
);
1801 result
= binary_semaphore_create(device
, &semaphore
->permanent
, true);
1802 if (result
!= VK_SUCCESS
) {
1803 vk_free2(&device
->alloc
, pAllocator
, semaphore
);
1806 } else if (handleTypes
& VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
) {
1807 assert(handleTypes
== VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
);
1808 assert(sem_type
== VK_SEMAPHORE_TYPE_BINARY_KHR
);
1809 if (device
->physical
->has_syncobj
) {
1810 semaphore
->permanent
.type
= ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
;
1811 semaphore
->permanent
.syncobj
= anv_gem_syncobj_create(device
, 0);
1812 if (!semaphore
->permanent
.syncobj
) {
1813 vk_free2(&device
->alloc
, pAllocator
, semaphore
);
1814 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1817 semaphore
->permanent
.type
= ANV_SEMAPHORE_TYPE_SYNC_FILE
;
1818 semaphore
->permanent
.fd
= -1;
1821 assert(!"Unknown handle type");
1822 vk_free2(&device
->alloc
, pAllocator
, semaphore
);
1823 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1826 semaphore
->temporary
.type
= ANV_SEMAPHORE_TYPE_NONE
;
1828 *pSemaphore
= anv_semaphore_to_handle(semaphore
);
1834 anv_semaphore_impl_cleanup(struct anv_device
*device
,
1835 struct anv_semaphore_impl
*impl
)
1837 switch (impl
->type
) {
1838 case ANV_SEMAPHORE_TYPE_NONE
:
1839 case ANV_SEMAPHORE_TYPE_DUMMY
:
1840 /* Dummy. Nothing to do */
1843 case ANV_SEMAPHORE_TYPE_BO
:
1844 case ANV_SEMAPHORE_TYPE_WSI_BO
:
1845 anv_device_release_bo(device
, impl
->bo
);
1848 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
1853 case ANV_SEMAPHORE_TYPE_TIMELINE
:
1854 anv_timeline_finish(device
, &impl
->timeline
);
1857 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
:
1858 anv_gem_syncobj_destroy(device
, impl
->syncobj
);
1862 unreachable("Invalid semaphore type");
1865 impl
->type
= ANV_SEMAPHORE_TYPE_NONE
;
1869 anv_semaphore_reset_temporary(struct anv_device
*device
,
1870 struct anv_semaphore
*semaphore
)
1872 if (semaphore
->temporary
.type
== ANV_SEMAPHORE_TYPE_NONE
)
1875 anv_semaphore_impl_cleanup(device
, &semaphore
->temporary
);
1878 static struct anv_semaphore
*
1879 anv_semaphore_ref(struct anv_semaphore
*semaphore
)
1881 assert(semaphore
->refcount
);
1882 p_atomic_inc(&semaphore
->refcount
);
1887 anv_semaphore_unref(struct anv_device
*device
, struct anv_semaphore
*semaphore
)
1889 if (!p_atomic_dec_zero(&semaphore
->refcount
))
1892 anv_semaphore_impl_cleanup(device
, &semaphore
->temporary
);
1893 anv_semaphore_impl_cleanup(device
, &semaphore
->permanent
);
1894 vk_free(&device
->alloc
, semaphore
);
1897 void anv_DestroySemaphore(
1899 VkSemaphore _semaphore
,
1900 const VkAllocationCallbacks
* pAllocator
)
1902 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1903 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, _semaphore
);
1905 if (semaphore
== NULL
)
1908 anv_semaphore_unref(device
, semaphore
);
1911 void anv_GetPhysicalDeviceExternalSemaphoreProperties(
1912 VkPhysicalDevice physicalDevice
,
1913 const VkPhysicalDeviceExternalSemaphoreInfo
* pExternalSemaphoreInfo
,
1914 VkExternalSemaphoreProperties
* pExternalSemaphoreProperties
)
1916 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1918 VkSemaphoreTypeKHR sem_type
=
1919 get_semaphore_type(pExternalSemaphoreInfo
->pNext
, NULL
);
1921 switch (pExternalSemaphoreInfo
->handleType
) {
1922 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
:
1923 /* Timeline semaphores are not exportable. */
1924 if (sem_type
== VK_SEMAPHORE_TYPE_TIMELINE_KHR
)
1926 pExternalSemaphoreProperties
->exportFromImportedHandleTypes
=
1927 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
;
1928 pExternalSemaphoreProperties
->compatibleHandleTypes
=
1929 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
;
1930 pExternalSemaphoreProperties
->externalSemaphoreFeatures
=
1931 VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT
|
1932 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT
;
1935 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
:
1936 if (sem_type
== VK_SEMAPHORE_TYPE_TIMELINE_KHR
)
1938 if (!device
->has_exec_fence
)
1940 pExternalSemaphoreProperties
->exportFromImportedHandleTypes
=
1941 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
;
1942 pExternalSemaphoreProperties
->compatibleHandleTypes
=
1943 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
;
1944 pExternalSemaphoreProperties
->externalSemaphoreFeatures
=
1945 VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT
|
1946 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT
;
1953 pExternalSemaphoreProperties
->exportFromImportedHandleTypes
= 0;
1954 pExternalSemaphoreProperties
->compatibleHandleTypes
= 0;
1955 pExternalSemaphoreProperties
->externalSemaphoreFeatures
= 0;
1958 VkResult
anv_ImportSemaphoreFdKHR(
1960 const VkImportSemaphoreFdInfoKHR
* pImportSemaphoreFdInfo
)
1962 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1963 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, pImportSemaphoreFdInfo
->semaphore
);
1964 int fd
= pImportSemaphoreFdInfo
->fd
;
1966 struct anv_semaphore_impl new_impl
= {
1967 .type
= ANV_SEMAPHORE_TYPE_NONE
,
1970 switch (pImportSemaphoreFdInfo
->handleType
) {
1971 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
:
1972 if (device
->physical
->has_syncobj
) {
1973 new_impl
.type
= ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
;
1975 new_impl
.syncobj
= anv_gem_syncobj_fd_to_handle(device
, fd
);
1976 if (!new_impl
.syncobj
)
1977 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1979 new_impl
.type
= ANV_SEMAPHORE_TYPE_BO
;
1981 VkResult result
= anv_device_import_bo(device
, fd
,
1982 ANV_BO_ALLOC_EXTERNAL
|
1983 ANV_BO_ALLOC_IMPLICIT_SYNC
,
1984 0 /* client_address */,
1986 if (result
!= VK_SUCCESS
)
1989 if (new_impl
.bo
->size
< 4096) {
1990 anv_device_release_bo(device
, new_impl
.bo
);
1991 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1994 /* If we're going to use this as a fence, we need to *not* have the
1995 * EXEC_OBJECT_ASYNC bit set.
1997 assert(!(new_impl
.bo
->flags
& EXEC_OBJECT_ASYNC
));
2000 /* From the Vulkan spec:
2002 * "Importing semaphore state from a file descriptor transfers
2003 * ownership of the file descriptor from the application to the
2004 * Vulkan implementation. The application must not perform any
2005 * operations on the file descriptor after a successful import."
2007 * If the import fails, we leave the file descriptor open.
2012 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
:
2013 if (device
->physical
->has_syncobj
) {
2014 new_impl
= (struct anv_semaphore_impl
) {
2015 .type
= ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
,
2016 .syncobj
= anv_gem_syncobj_create(device
, 0),
2018 if (!new_impl
.syncobj
)
2019 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2020 if (anv_gem_syncobj_import_sync_file(device
, new_impl
.syncobj
, fd
)) {
2021 anv_gem_syncobj_destroy(device
, new_impl
.syncobj
);
2022 return vk_errorf(device
, NULL
, VK_ERROR_INVALID_EXTERNAL_HANDLE
,
2023 "syncobj sync file import failed: %m");
2025 /* Ownership of the FD is transfered to Anv. Since we don't need it
2026 * anymore because the associated fence has been put into a syncobj,
2027 * we must close the FD.
2031 new_impl
= (struct anv_semaphore_impl
) {
2032 .type
= ANV_SEMAPHORE_TYPE_SYNC_FILE
,
2039 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2042 if (pImportSemaphoreFdInfo
->flags
& VK_SEMAPHORE_IMPORT_TEMPORARY_BIT
) {
2043 anv_semaphore_impl_cleanup(device
, &semaphore
->temporary
);
2044 semaphore
->temporary
= new_impl
;
2046 anv_semaphore_impl_cleanup(device
, &semaphore
->permanent
);
2047 semaphore
->permanent
= new_impl
;
2053 VkResult
anv_GetSemaphoreFdKHR(
2055 const VkSemaphoreGetFdInfoKHR
* pGetFdInfo
,
2058 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2059 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, pGetFdInfo
->semaphore
);
2063 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR
);
2065 struct anv_semaphore_impl
*impl
=
2066 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
2067 &semaphore
->temporary
: &semaphore
->permanent
;
2069 switch (impl
->type
) {
2070 case ANV_SEMAPHORE_TYPE_BO
:
2071 result
= anv_device_export_bo(device
, impl
->bo
, pFd
);
2072 if (result
!= VK_SUCCESS
)
2076 case ANV_SEMAPHORE_TYPE_SYNC_FILE
: {
2077 /* There's a potential race here with vkQueueSubmit if you are trying
2078 * to export a semaphore Fd while the queue submit is still happening.
2079 * This can happen if we see all dependencies get resolved via timeline
2080 * semaphore waits completing before the execbuf completes and we
2081 * process the resulting out fence. To work around this, take a lock
2082 * around grabbing the fd.
2084 pthread_mutex_lock(&device
->mutex
);
2086 /* From the Vulkan 1.0.53 spec:
2088 * "...exporting a semaphore payload to a handle with copy
2089 * transference has the same side effects on the source
2090 * semaphore’s payload as executing a semaphore wait operation."
2092 * In other words, it may still be a SYNC_FD semaphore, but it's now
2093 * considered to have been waited on and no longer has a sync file
2099 pthread_mutex_unlock(&device
->mutex
);
2101 /* There are two reasons why this could happen:
2103 * 1) The user is trying to export without submitting something that
2104 * signals the semaphore. If this is the case, it's their bug so
2105 * what we return here doesn't matter.
2107 * 2) The kernel didn't give us a file descriptor. The most likely
2108 * reason for this is running out of file descriptors.
2111 return vk_error(VK_ERROR_TOO_MANY_OBJECTS
);
2117 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
:
2118 if (pGetFdInfo
->handleType
== VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
)
2119 fd
= anv_gem_syncobj_export_sync_file(device
, impl
->syncobj
);
2121 assert(pGetFdInfo
->handleType
== VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
);
2122 fd
= anv_gem_syncobj_handle_to_fd(device
, impl
->syncobj
);
2125 return vk_error(VK_ERROR_TOO_MANY_OBJECTS
);
2130 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2133 /* From the Vulkan 1.0.53 spec:
2135 * "Export operations have the same transference as the specified handle
2136 * type’s import operations. [...] If the semaphore was using a
2137 * temporarily imported payload, the semaphore’s prior permanent payload
2140 if (impl
== &semaphore
->temporary
)
2141 anv_semaphore_impl_cleanup(device
, impl
);
2146 VkResult
anv_GetSemaphoreCounterValue(
2148 VkSemaphore _semaphore
,
2151 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2152 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, _semaphore
);
2154 struct anv_semaphore_impl
*impl
=
2155 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
2156 &semaphore
->temporary
: &semaphore
->permanent
;
2158 switch (impl
->type
) {
2159 case ANV_SEMAPHORE_TYPE_TIMELINE
: {
2160 pthread_mutex_lock(&device
->mutex
);
2161 *pValue
= impl
->timeline
.highest_past
;
2162 pthread_mutex_unlock(&device
->mutex
);
2167 unreachable("Invalid semaphore type");
2172 anv_timeline_wait_locked(struct anv_device
*device
,
2173 struct anv_timeline
*timeline
,
2174 uint64_t serial
, uint64_t abs_timeout_ns
)
2176 /* Wait on the queue_submit condition variable until the timeline has a
2177 * time point pending that's at least as high as serial.
2179 while (timeline
->highest_pending
< serial
) {
2180 struct timespec abstime
= {
2181 .tv_sec
= abs_timeout_ns
/ NSEC_PER_SEC
,
2182 .tv_nsec
= abs_timeout_ns
% NSEC_PER_SEC
,
2185 int ret
= pthread_cond_timedwait(&device
->queue_submit
,
2186 &device
->mutex
, &abstime
);
2187 assert(ret
!= EINVAL
);
2188 if (anv_gettime_ns() >= abs_timeout_ns
&&
2189 timeline
->highest_pending
< serial
)
2194 VkResult result
= anv_timeline_gc_locked(device
, timeline
);
2195 if (result
!= VK_SUCCESS
)
2198 if (timeline
->highest_past
>= serial
)
2201 /* If we got here, our earliest time point has a busy BO */
2202 struct anv_timeline_point
*point
=
2203 list_first_entry(&timeline
->points
,
2204 struct anv_timeline_point
, link
);
2206 /* Drop the lock while we wait. */
2208 pthread_mutex_unlock(&device
->mutex
);
2210 result
= anv_device_wait(device
, point
->bo
,
2211 anv_get_relative_timeout(abs_timeout_ns
));
2213 /* Pick the mutex back up */
2214 pthread_mutex_lock(&device
->mutex
);
2217 /* This covers both VK_TIMEOUT and VK_ERROR_DEVICE_LOST */
2218 if (result
!= VK_SUCCESS
)
2224 anv_timelines_wait(struct anv_device
*device
,
2225 struct anv_timeline
**timelines
,
2226 const uint64_t *serials
,
2227 uint32_t n_timelines
,
2229 uint64_t abs_timeout_ns
)
2231 if (!wait_all
&& n_timelines
> 1) {
2234 pthread_mutex_lock(&device
->mutex
);
2235 for (uint32_t i
= 0; i
< n_timelines
; i
++) {
2237 anv_timeline_wait_locked(device
, timelines
[i
], serials
[i
], 0);
2238 if (result
!= VK_TIMEOUT
)
2242 if (result
!= VK_TIMEOUT
||
2243 anv_gettime_ns() >= abs_timeout_ns
) {
2244 pthread_mutex_unlock(&device
->mutex
);
2248 /* If none of them are ready do a short wait so we don't completely
2249 * spin while holding the lock. The 10us is completely arbitrary.
2251 uint64_t abs_short_wait_ns
=
2252 anv_get_absolute_timeout(
2253 MIN2((anv_gettime_ns() - abs_timeout_ns
) / 10, 10 * 1000));
2254 struct timespec abstime
= {
2255 .tv_sec
= abs_short_wait_ns
/ NSEC_PER_SEC
,
2256 .tv_nsec
= abs_short_wait_ns
% NSEC_PER_SEC
,
2259 ret
= pthread_cond_timedwait(&device
->queue_submit
,
2260 &device
->mutex
, &abstime
);
2261 assert(ret
!= EINVAL
);
2264 VkResult result
= VK_SUCCESS
;
2265 pthread_mutex_lock(&device
->mutex
);
2266 for (uint32_t i
= 0; i
< n_timelines
; i
++) {
2268 anv_timeline_wait_locked(device
, timelines
[i
],
2269 serials
[i
], abs_timeout_ns
);
2270 if (result
!= VK_SUCCESS
)
2273 pthread_mutex_unlock(&device
->mutex
);
2278 VkResult
anv_WaitSemaphores(
2280 const VkSemaphoreWaitInfoKHR
* pWaitInfo
,
2283 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2285 struct anv_timeline
**timelines
=
2286 vk_alloc(&device
->alloc
,
2287 pWaitInfo
->semaphoreCount
* sizeof(*timelines
),
2288 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
2290 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2292 uint64_t *values
= vk_alloc(&device
->alloc
,
2293 pWaitInfo
->semaphoreCount
* sizeof(*values
),
2294 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
2296 vk_free(&device
->alloc
, timelines
);
2297 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2300 uint32_t handle_count
= 0;
2301 for (uint32_t i
= 0; i
< pWaitInfo
->semaphoreCount
; i
++) {
2302 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, pWaitInfo
->pSemaphores
[i
]);
2303 struct anv_semaphore_impl
*impl
=
2304 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
2305 &semaphore
->temporary
: &semaphore
->permanent
;
2307 assert(impl
->type
== ANV_SEMAPHORE_TYPE_TIMELINE
);
2309 if (pWaitInfo
->pValues
[i
] == 0)
2312 timelines
[handle_count
] = &impl
->timeline
;
2313 values
[handle_count
] = pWaitInfo
->pValues
[i
];
2317 VkResult result
= VK_SUCCESS
;
2318 if (handle_count
> 0) {
2319 result
= anv_timelines_wait(device
, timelines
, values
, handle_count
,
2320 !(pWaitInfo
->flags
& VK_SEMAPHORE_WAIT_ANY_BIT_KHR
),
2324 vk_free(&device
->alloc
, timelines
);
2325 vk_free(&device
->alloc
, values
);
2330 VkResult
anv_SignalSemaphore(
2332 const VkSemaphoreSignalInfoKHR
* pSignalInfo
)
2334 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2335 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, pSignalInfo
->semaphore
);
2337 struct anv_semaphore_impl
*impl
=
2338 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
2339 &semaphore
->temporary
: &semaphore
->permanent
;
2341 switch (impl
->type
) {
2342 case ANV_SEMAPHORE_TYPE_TIMELINE
: {
2343 pthread_mutex_lock(&device
->mutex
);
2345 VkResult result
= anv_timeline_gc_locked(device
, &impl
->timeline
);
2347 assert(pSignalInfo
->value
> impl
->timeline
.highest_pending
);
2349 impl
->timeline
.highest_pending
= impl
->timeline
.highest_past
= pSignalInfo
->value
;
2351 if (result
== VK_SUCCESS
)
2352 result
= anv_device_submit_deferred_locked(device
);
2354 pthread_cond_broadcast(&device
->queue_submit
);
2355 pthread_mutex_unlock(&device
->mutex
);
2360 unreachable("Invalid semaphore type");