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
)
353 struct anv_queue_submit
*submit
= *_submit
;
355 /* Wait before signal behavior means we might keep alive the
356 * anv_queue_submit object a bit longer, so transfer the ownership to the
361 pthread_mutex_lock(&queue
->device
->mutex
);
362 list_addtail(&submit
->link
, &queue
->queued_submits
);
363 VkResult result
= anv_device_submit_deferred_locked(queue
->device
);
364 pthread_mutex_unlock(&queue
->device
->mutex
);
369 anv_queue_init(struct anv_device
*device
, struct anv_queue
*queue
)
371 queue
->_loader_data
.loaderMagic
= ICD_LOADER_MAGIC
;
372 queue
->device
= device
;
375 list_inithead(&queue
->queued_submits
);
381 anv_queue_finish(struct anv_queue
*queue
)
386 anv_queue_submit_add_fence_bo(struct anv_queue_submit
*submit
,
390 if (submit
->fence_bo_count
>= submit
->fence_bo_array_length
) {
391 uint32_t new_len
= MAX2(submit
->fence_bo_array_length
* 2, 64);
394 vk_realloc(submit
->alloc
,
395 submit
->fence_bos
, new_len
* sizeof(*submit
->fence_bos
),
396 8, submit
->alloc_scope
);
397 if (submit
->fence_bos
== NULL
)
398 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
400 submit
->fence_bo_array_length
= new_len
;
403 /* Take advantage that anv_bo are allocated at 8 byte alignement so we can
404 * use the lowest bit to store whether this is a BO we need to signal.
406 submit
->fence_bos
[submit
->fence_bo_count
++] = anv_pack_ptr(bo
, 1, signal
);
412 anv_queue_submit_add_syncobj(struct anv_queue_submit
* submit
,
413 struct anv_device
*device
,
414 uint32_t handle
, uint32_t flags
)
418 if (submit
->fence_count
>= submit
->fence_array_length
) {
419 uint32_t new_len
= MAX2(submit
->fence_array_length
* 2, 64);
422 vk_realloc(submit
->alloc
,
423 submit
->fences
, new_len
* sizeof(*submit
->fences
),
424 8, submit
->alloc_scope
);
425 if (submit
->fences
== NULL
)
426 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
428 submit
->fence_array_length
= new_len
;
431 submit
->fences
[submit
->fence_count
++] = (struct drm_i915_gem_exec_fence
) {
440 anv_queue_submit_add_sync_fd_fence(struct anv_queue_submit
*submit
,
441 struct anv_semaphore
*semaphore
)
443 if (submit
->sync_fd_semaphore_count
>= submit
->sync_fd_semaphore_array_length
) {
444 uint32_t new_len
= MAX2(submit
->sync_fd_semaphore_array_length
* 2, 64);
445 struct anv_semaphore
**new_semaphores
=
446 vk_realloc(submit
->alloc
, submit
->sync_fd_semaphores
,
447 new_len
* sizeof(*submit
->sync_fd_semaphores
), 8,
448 submit
->alloc_scope
);
449 if (new_semaphores
== NULL
)
450 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
452 submit
->sync_fd_semaphores
= new_semaphores
;
455 submit
->sync_fd_semaphores
[submit
->sync_fd_semaphore_count
++] =
456 anv_semaphore_ref(semaphore
);
457 submit
->need_out_fence
= true;
463 anv_queue_submit_add_timeline_wait(struct anv_queue_submit
* submit
,
464 struct anv_device
*device
,
465 struct anv_timeline
*timeline
,
468 if (submit
->wait_timeline_count
>= submit
->wait_timeline_array_length
) {
469 uint32_t new_len
= MAX2(submit
->wait_timeline_array_length
* 2, 64);
471 submit
->wait_timelines
=
472 vk_realloc(submit
->alloc
,
473 submit
->wait_timelines
, new_len
* sizeof(*submit
->wait_timelines
),
474 8, submit
->alloc_scope
);
475 if (submit
->wait_timelines
== NULL
)
476 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
478 submit
->wait_timeline_values
=
479 vk_realloc(submit
->alloc
,
480 submit
->wait_timeline_values
, new_len
* sizeof(*submit
->wait_timeline_values
),
481 8, submit
->alloc_scope
);
482 if (submit
->wait_timeline_values
== NULL
)
483 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
485 submit
->wait_timeline_array_length
= new_len
;
488 submit
->wait_timelines
[submit
->wait_timeline_count
] = timeline
;
489 submit
->wait_timeline_values
[submit
->wait_timeline_count
] = value
;
491 submit
->wait_timeline_count
++;
497 anv_queue_submit_add_timeline_signal(struct anv_queue_submit
* submit
,
498 struct anv_device
*device
,
499 struct anv_timeline
*timeline
,
502 assert(timeline
->highest_pending
< value
);
504 if (submit
->signal_timeline_count
>= submit
->signal_timeline_array_length
) {
505 uint32_t new_len
= MAX2(submit
->signal_timeline_array_length
* 2, 64);
507 submit
->signal_timelines
=
508 vk_realloc(submit
->alloc
,
509 submit
->signal_timelines
, new_len
* sizeof(*submit
->signal_timelines
),
510 8, submit
->alloc_scope
);
511 if (submit
->signal_timelines
== NULL
)
512 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
514 submit
->signal_timeline_values
=
515 vk_realloc(submit
->alloc
,
516 submit
->signal_timeline_values
, new_len
* sizeof(*submit
->signal_timeline_values
),
517 8, submit
->alloc_scope
);
518 if (submit
->signal_timeline_values
== NULL
)
519 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
521 submit
->signal_timeline_array_length
= new_len
;
524 submit
->signal_timelines
[submit
->signal_timeline_count
] = timeline
;
525 submit
->signal_timeline_values
[submit
->signal_timeline_count
] = value
;
527 submit
->signal_timeline_count
++;
532 static struct anv_queue_submit
*
533 anv_queue_submit_alloc(struct anv_device
*device
)
535 const VkAllocationCallbacks
*alloc
= &device
->alloc
;
536 VkSystemAllocationScope alloc_scope
= VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
;
538 struct anv_queue_submit
*submit
= vk_zalloc(alloc
, sizeof(*submit
), 8, alloc_scope
);
542 submit
->alloc
= alloc
;
543 submit
->alloc_scope
= alloc_scope
;
544 submit
->in_fence
= -1;
545 submit
->out_fence
= -1;
551 anv_queue_submit_simple_batch(struct anv_queue
*queue
,
552 struct anv_batch
*batch
)
554 struct anv_device
*device
= queue
->device
;
555 struct anv_queue_submit
*submit
= anv_queue_submit_alloc(device
);
557 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
559 bool has_syncobj_wait
= device
->instance
->physicalDevice
.has_syncobj_wait
;
562 struct anv_bo
*batch_bo
, *sync_bo
;
564 if (has_syncobj_wait
) {
565 syncobj
= anv_gem_syncobj_create(device
, 0);
567 result
= vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY
);
568 goto err_free_submit
;
571 result
= anv_queue_submit_add_syncobj(submit
, device
, syncobj
,
572 I915_EXEC_FENCE_SIGNAL
);
574 result
= anv_device_alloc_bo(device
, 4096,
575 ANV_BO_ALLOC_EXTERNAL
|
576 ANV_BO_ALLOC_IMPLICIT_SYNC
,
577 0 /* explicit_address */,
579 if (result
!= VK_SUCCESS
)
580 goto err_free_submit
;
582 result
= anv_queue_submit_add_fence_bo(submit
, sync_bo
, true /* signal */);
585 if (result
!= VK_SUCCESS
)
586 goto err_destroy_sync_primitive
;
589 uint32_t size
= align_u32(batch
->next
- batch
->start
, 8);
590 result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, size
, &batch_bo
);
591 if (result
!= VK_SUCCESS
)
592 goto err_destroy_sync_primitive
;
594 memcpy(batch_bo
->map
, batch
->start
, size
);
595 if (!device
->info
.has_llc
)
596 gen_flush_range(batch_bo
->map
, size
);
598 submit
->simple_bo
= batch_bo
;
599 submit
->simple_bo_size
= size
;
602 result
= _anv_queue_submit(queue
, &submit
);
604 if (result
== VK_SUCCESS
) {
605 if (has_syncobj_wait
) {
606 if (anv_gem_syncobj_wait(device
, &syncobj
, 1,
607 anv_get_absolute_timeout(INT64_MAX
), true))
608 result
= anv_device_set_lost(device
, "anv_gem_syncobj_wait failed: %m");
609 anv_gem_syncobj_destroy(device
, syncobj
);
611 result
= anv_device_wait(device
, sync_bo
,
612 anv_get_relative_timeout(INT64_MAX
));
613 anv_device_release_bo(device
, sync_bo
);
618 anv_bo_pool_free(&device
->batch_bo_pool
, batch_bo
);
621 anv_queue_submit_free(device
, submit
);
625 err_destroy_sync_primitive
:
626 if (has_syncobj_wait
)
627 anv_gem_syncobj_destroy(device
, syncobj
);
629 anv_device_release_bo(device
, sync_bo
);
632 anv_queue_submit_free(device
, submit
);
637 /* Transfer ownership of temporary semaphores from the VkSemaphore object to
638 * the anv_queue_submit object. Those temporary semaphores are then freed in
639 * anv_queue_submit_free() once the driver is finished with them.
642 maybe_transfer_temporary_semaphore(struct anv_queue_submit
*submit
,
643 struct anv_semaphore
*semaphore
,
644 struct anv_semaphore_impl
**out_impl
)
646 struct anv_semaphore_impl
*impl
= &semaphore
->temporary
;
648 if (impl
->type
== ANV_SEMAPHORE_TYPE_NONE
) {
649 *out_impl
= &semaphore
->permanent
;
653 /* BO backed timeline semaphores cannot be temporary. */
654 assert(impl
->type
!= ANV_SEMAPHORE_TYPE_TIMELINE
);
657 * There is a requirement to reset semaphore to their permanent state after
658 * submission. From the Vulkan 1.0.53 spec:
660 * "If the import is temporary, the implementation must restore the
661 * semaphore to its prior permanent state after submitting the next
662 * semaphore wait operation."
664 * In the case we defer the actual submission to a thread because of the
665 * wait-before-submit behavior required for timeline semaphores, we need to
666 * make copies of the temporary syncobj to ensure they stay alive until we
667 * do the actual execbuffer ioctl.
669 if (submit
->temporary_semaphore_count
>= submit
->temporary_semaphore_array_length
) {
670 uint32_t new_len
= MAX2(submit
->temporary_semaphore_array_length
* 2, 8);
671 /* Make sure that if the realloc fails, we still have the old semaphore
672 * array around to properly clean things up on failure.
674 struct anv_semaphore_impl
*new_array
=
675 vk_realloc(submit
->alloc
,
676 submit
->temporary_semaphores
,
677 new_len
* sizeof(*submit
->temporary_semaphores
),
678 8, submit
->alloc_scope
);
679 if (new_array
== NULL
)
680 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
682 submit
->temporary_semaphores
= new_array
;
683 submit
->temporary_semaphore_array_length
= new_len
;
686 /* Copy anv_semaphore_impl into anv_queue_submit. */
687 submit
->temporary_semaphores
[submit
->temporary_semaphore_count
++] = *impl
;
688 *out_impl
= &submit
->temporary_semaphores
[submit
->temporary_semaphore_count
- 1];
690 /* Clear the incoming semaphore */
691 impl
->type
= ANV_SEMAPHORE_TYPE_NONE
;
697 anv_queue_submit(struct anv_queue
*queue
,
698 struct anv_cmd_buffer
*cmd_buffer
,
699 const VkSemaphore
*in_semaphores
,
700 const uint64_t *in_values
,
701 uint32_t num_in_semaphores
,
702 const VkSemaphore
*out_semaphores
,
703 const uint64_t *out_values
,
704 uint32_t num_out_semaphores
,
707 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
708 struct anv_device
*device
= queue
->device
;
709 UNUSED
struct anv_physical_device
*pdevice
= &device
->instance
->physicalDevice
;
710 struct anv_queue_submit
*submit
= anv_queue_submit_alloc(device
);
712 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
714 submit
->cmd_buffer
= cmd_buffer
;
716 VkResult result
= VK_SUCCESS
;
718 for (uint32_t i
= 0; i
< num_in_semaphores
; i
++) {
719 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, in_semaphores
[i
]);
720 struct anv_semaphore_impl
*impl
;
722 result
= maybe_transfer_temporary_semaphore(submit
, semaphore
, &impl
);
723 if (result
!= VK_SUCCESS
)
726 switch (impl
->type
) {
727 case ANV_SEMAPHORE_TYPE_BO
:
728 assert(!pdevice
->has_syncobj
);
729 result
= anv_queue_submit_add_fence_bo(submit
, impl
->bo
, false /* signal */);
730 if (result
!= VK_SUCCESS
)
734 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
735 assert(!pdevice
->has_syncobj
);
736 if (submit
->in_fence
== -1) {
737 submit
->in_fence
= impl
->fd
;
738 if (submit
->in_fence
== -1) {
739 result
= vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
744 int merge
= anv_gem_sync_file_merge(device
, submit
->in_fence
, impl
->fd
);
746 result
= vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
750 close(submit
->in_fence
);
752 submit
->in_fence
= merge
;
756 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
: {
757 result
= anv_queue_submit_add_syncobj(submit
, device
,
759 I915_EXEC_FENCE_WAIT
);
760 if (result
!= VK_SUCCESS
)
765 case ANV_SEMAPHORE_TYPE_TIMELINE
:
766 result
= anv_queue_submit_add_timeline_wait(submit
, device
,
768 in_values
? in_values
[i
] : 0);
769 if (result
!= VK_SUCCESS
)
778 for (uint32_t i
= 0; i
< num_out_semaphores
; i
++) {
779 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, out_semaphores
[i
]);
781 /* Under most circumstances, out fences won't be temporary. However,
782 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
784 * "If the import is temporary, the implementation must restore the
785 * semaphore to its prior permanent state after submitting the next
786 * semaphore wait operation."
788 * The spec says nothing whatsoever about signal operations on
789 * temporarily imported semaphores so it appears they are allowed.
790 * There are also CTS tests that require this to work.
792 struct anv_semaphore_impl
*impl
=
793 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
794 &semaphore
->temporary
: &semaphore
->permanent
;
796 switch (impl
->type
) {
797 case ANV_SEMAPHORE_TYPE_BO
:
798 assert(!pdevice
->has_syncobj
);
799 result
= anv_queue_submit_add_fence_bo(submit
, impl
->bo
, true /* signal */);
800 if (result
!= VK_SUCCESS
)
804 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
805 assert(!pdevice
->has_syncobj
);
806 result
= anv_queue_submit_add_sync_fd_fence(submit
, semaphore
);
807 if (result
!= VK_SUCCESS
)
811 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
: {
812 result
= anv_queue_submit_add_syncobj(submit
, device
, impl
->syncobj
,
813 I915_EXEC_FENCE_SIGNAL
);
814 if (result
!= VK_SUCCESS
)
819 case ANV_SEMAPHORE_TYPE_TIMELINE
:
820 result
= anv_queue_submit_add_timeline_signal(submit
, device
,
822 out_values
? out_values
[i
] : 0);
823 if (result
!= VK_SUCCESS
)
833 /* Under most circumstances, out fences won't be temporary. However,
834 * the spec does allow it for opaque_fd. From the Vulkan 1.0.53 spec:
836 * "If the import is temporary, the implementation must restore the
837 * semaphore to its prior permanent state after submitting the next
838 * semaphore wait operation."
840 * The spec says nothing whatsoever about signal operations on
841 * temporarily imported semaphores so it appears they are allowed.
842 * There are also CTS tests that require this to work.
844 struct anv_fence_impl
*impl
=
845 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
846 &fence
->temporary
: &fence
->permanent
;
848 switch (impl
->type
) {
849 case ANV_FENCE_TYPE_BO
:
850 result
= anv_queue_submit_add_fence_bo(submit
, impl
->bo
.bo
, true /* signal */);
851 if (result
!= VK_SUCCESS
)
855 case ANV_FENCE_TYPE_SYNCOBJ
: {
857 * For the same reason we reset the signaled binary syncobj above,
858 * also reset the fence's syncobj so that they don't contain a
859 * signaled dma-fence.
861 result
= anv_queue_submit_add_syncobj(submit
, device
, impl
->syncobj
,
862 I915_EXEC_FENCE_SIGNAL
);
863 if (result
!= VK_SUCCESS
)
869 unreachable("Invalid fence type");
873 result
= _anv_queue_submit(queue
, &submit
);
874 if (result
!= VK_SUCCESS
)
877 if (fence
&& fence
->permanent
.type
== ANV_FENCE_TYPE_BO
) {
878 /* BO fences can't be shared, so they can't be temporary. */
879 assert(fence
->temporary
.type
== ANV_FENCE_TYPE_NONE
);
881 /* Once the execbuf has returned, we need to set the fence state to
882 * SUBMITTED. We can't do this before calling execbuf because
883 * anv_GetFenceStatus does take the global device lock before checking
886 * We set the fence state to SUBMITTED regardless of whether or not the
887 * execbuf succeeds because we need to ensure that vkWaitForFences() and
888 * vkGetFenceStatus() return a valid result (VK_ERROR_DEVICE_LOST or
889 * VK_SUCCESS) in a finite amount of time even if execbuf fails.
891 fence
->permanent
.bo
.state
= ANV_BO_FENCE_STATE_SUBMITTED
;
896 anv_queue_submit_free(device
, submit
);
901 VkResult
anv_QueueSubmit(
903 uint32_t submitCount
,
904 const VkSubmitInfo
* pSubmits
,
907 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
909 /* Query for device status prior to submitting. Technically, we don't need
910 * to do this. However, if we have a client that's submitting piles of
911 * garbage, we would rather break as early as possible to keep the GPU
912 * hanging contained. If we don't check here, we'll either be waiting for
913 * the kernel to kick us or we'll have to wait until the client waits on a
914 * fence before we actually know whether or not we've hung.
916 VkResult result
= anv_device_query_status(queue
->device
);
917 if (result
!= VK_SUCCESS
)
920 if (fence
&& submitCount
== 0) {
921 /* If we don't have any command buffers, we need to submit a dummy
922 * batch to give GEM something to wait on. We could, potentially,
923 * come up with something more efficient but this shouldn't be a
926 result
= anv_queue_submit(queue
, NULL
, NULL
, NULL
, 0, NULL
, NULL
, 0, fence
);
930 for (uint32_t i
= 0; i
< submitCount
; i
++) {
931 /* Fence for this submit. NULL for all but the last one */
932 VkFence submit_fence
= (i
== submitCount
- 1) ? fence
: VK_NULL_HANDLE
;
934 const VkTimelineSemaphoreSubmitInfoKHR
*timeline_info
=
935 vk_find_struct_const(pSubmits
[i
].pNext
,
936 TIMELINE_SEMAPHORE_SUBMIT_INFO_KHR
);
937 const uint64_t *wait_values
=
938 timeline_info
&& timeline_info
->waitSemaphoreValueCount
?
939 timeline_info
->pWaitSemaphoreValues
: NULL
;
940 const uint64_t *signal_values
=
941 timeline_info
&& timeline_info
->signalSemaphoreValueCount
?
942 timeline_info
->pSignalSemaphoreValues
: NULL
;
944 if (pSubmits
[i
].commandBufferCount
== 0) {
945 /* If we don't have any command buffers, we need to submit a dummy
946 * batch to give GEM something to wait on. We could, potentially,
947 * come up with something more efficient but this shouldn't be a
950 result
= anv_queue_submit(queue
, NULL
,
951 pSubmits
[i
].pWaitSemaphores
,
953 pSubmits
[i
].waitSemaphoreCount
,
954 pSubmits
[i
].pSignalSemaphores
,
956 pSubmits
[i
].signalSemaphoreCount
,
958 if (result
!= VK_SUCCESS
)
964 for (uint32_t j
= 0; j
< pSubmits
[i
].commandBufferCount
; j
++) {
965 ANV_FROM_HANDLE(anv_cmd_buffer
, cmd_buffer
,
966 pSubmits
[i
].pCommandBuffers
[j
]);
967 assert(cmd_buffer
->level
== VK_COMMAND_BUFFER_LEVEL_PRIMARY
);
968 assert(!anv_batch_has_error(&cmd_buffer
->batch
));
970 /* Fence for this execbuf. NULL for all but the last one */
971 VkFence execbuf_fence
=
972 (j
== pSubmits
[i
].commandBufferCount
- 1) ?
973 submit_fence
: VK_NULL_HANDLE
;
975 const VkSemaphore
*in_semaphores
= NULL
, *out_semaphores
= NULL
;
976 const uint64_t *in_values
= NULL
, *out_values
= NULL
;
977 uint32_t num_in_semaphores
= 0, num_out_semaphores
= 0;
979 /* Only the first batch gets the in semaphores */
980 in_semaphores
= pSubmits
[i
].pWaitSemaphores
;
981 in_values
= wait_values
;
982 num_in_semaphores
= pSubmits
[i
].waitSemaphoreCount
;
985 if (j
== pSubmits
[i
].commandBufferCount
- 1) {
986 /* Only the last batch gets the out semaphores */
987 out_semaphores
= pSubmits
[i
].pSignalSemaphores
;
988 out_values
= signal_values
;
989 num_out_semaphores
= pSubmits
[i
].signalSemaphoreCount
;
992 result
= anv_queue_submit(queue
, cmd_buffer
,
993 in_semaphores
, in_values
, num_in_semaphores
,
994 out_semaphores
, out_values
, num_out_semaphores
,
996 if (result
!= VK_SUCCESS
)
1002 if (result
!= VK_SUCCESS
&& result
!= VK_ERROR_DEVICE_LOST
) {
1003 /* In the case that something has gone wrong we may end up with an
1004 * inconsistent state from which it may not be trivial to recover.
1005 * For example, we might have computed address relocations and
1006 * any future attempt to re-submit this job will need to know about
1007 * this and avoid computing relocation addresses again.
1009 * To avoid this sort of issues, we assume that if something was
1010 * wrong during submission we must already be in a really bad situation
1011 * anyway (such us being out of memory) and return
1012 * VK_ERROR_DEVICE_LOST to ensure that clients do not attempt to
1013 * submit the same job again to this device.
1015 * We skip doing this on VK_ERROR_DEVICE_LOST because
1016 * anv_device_set_lost() would have been called already by a callee of
1017 * anv_queue_submit().
1019 result
= anv_device_set_lost(queue
->device
, "vkQueueSubmit() failed");
1025 VkResult
anv_QueueWaitIdle(
1028 ANV_FROM_HANDLE(anv_queue
, queue
, _queue
);
1030 if (anv_device_is_lost(queue
->device
))
1031 return VK_ERROR_DEVICE_LOST
;
1033 return anv_queue_submit_simple_batch(queue
, NULL
);
1036 VkResult
anv_CreateFence(
1038 const VkFenceCreateInfo
* pCreateInfo
,
1039 const VkAllocationCallbacks
* pAllocator
,
1042 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1043 struct anv_fence
*fence
;
1045 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_CREATE_INFO
);
1047 fence
= vk_zalloc2(&device
->alloc
, pAllocator
, sizeof(*fence
), 8,
1048 VK_SYSTEM_ALLOCATION_SCOPE_OBJECT
);
1050 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1052 if (device
->instance
->physicalDevice
.has_syncobj_wait
) {
1053 fence
->permanent
.type
= ANV_FENCE_TYPE_SYNCOBJ
;
1055 uint32_t create_flags
= 0;
1056 if (pCreateInfo
->flags
& VK_FENCE_CREATE_SIGNALED_BIT
)
1057 create_flags
|= DRM_SYNCOBJ_CREATE_SIGNALED
;
1059 fence
->permanent
.syncobj
= anv_gem_syncobj_create(device
, create_flags
);
1060 if (!fence
->permanent
.syncobj
)
1061 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1063 fence
->permanent
.type
= ANV_FENCE_TYPE_BO
;
1065 VkResult result
= anv_bo_pool_alloc(&device
->batch_bo_pool
, 4096,
1066 &fence
->permanent
.bo
.bo
);
1067 if (result
!= VK_SUCCESS
)
1070 if (pCreateInfo
->flags
& VK_FENCE_CREATE_SIGNALED_BIT
) {
1071 fence
->permanent
.bo
.state
= ANV_BO_FENCE_STATE_SIGNALED
;
1073 fence
->permanent
.bo
.state
= ANV_BO_FENCE_STATE_RESET
;
1077 *pFence
= anv_fence_to_handle(fence
);
1083 anv_fence_impl_cleanup(struct anv_device
*device
,
1084 struct anv_fence_impl
*impl
)
1086 switch (impl
->type
) {
1087 case ANV_FENCE_TYPE_NONE
:
1088 /* Dummy. Nothing to do */
1091 case ANV_FENCE_TYPE_BO
:
1092 anv_bo_pool_free(&device
->batch_bo_pool
, impl
->bo
.bo
);
1095 case ANV_FENCE_TYPE_SYNCOBJ
:
1096 anv_gem_syncobj_destroy(device
, impl
->syncobj
);
1099 case ANV_FENCE_TYPE_WSI
:
1100 impl
->fence_wsi
->destroy(impl
->fence_wsi
);
1104 unreachable("Invalid fence type");
1107 impl
->type
= ANV_FENCE_TYPE_NONE
;
1110 void anv_DestroyFence(
1113 const VkAllocationCallbacks
* pAllocator
)
1115 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1116 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1121 anv_fence_impl_cleanup(device
, &fence
->temporary
);
1122 anv_fence_impl_cleanup(device
, &fence
->permanent
);
1124 vk_free2(&device
->alloc
, pAllocator
, fence
);
1127 VkResult
anv_ResetFences(
1129 uint32_t fenceCount
,
1130 const VkFence
* pFences
)
1132 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1134 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1135 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1137 /* From the Vulkan 1.0.53 spec:
1139 * "If any member of pFences currently has its payload imported with
1140 * temporary permanence, that fence’s prior permanent payload is
1141 * first restored. The remaining operations described therefore
1142 * operate on the restored payload.
1144 if (fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
)
1145 anv_fence_impl_cleanup(device
, &fence
->temporary
);
1147 struct anv_fence_impl
*impl
= &fence
->permanent
;
1149 switch (impl
->type
) {
1150 case ANV_FENCE_TYPE_BO
:
1151 impl
->bo
.state
= ANV_BO_FENCE_STATE_RESET
;
1154 case ANV_FENCE_TYPE_SYNCOBJ
:
1155 anv_gem_syncobj_reset(device
, impl
->syncobj
);
1159 unreachable("Invalid fence type");
1166 VkResult
anv_GetFenceStatus(
1170 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1171 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1173 if (anv_device_is_lost(device
))
1174 return VK_ERROR_DEVICE_LOST
;
1176 struct anv_fence_impl
*impl
=
1177 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1178 &fence
->temporary
: &fence
->permanent
;
1180 switch (impl
->type
) {
1181 case ANV_FENCE_TYPE_BO
:
1182 /* BO fences don't support import/export */
1183 assert(fence
->temporary
.type
== ANV_FENCE_TYPE_NONE
);
1184 switch (impl
->bo
.state
) {
1185 case ANV_BO_FENCE_STATE_RESET
:
1186 /* If it hasn't even been sent off to the GPU yet, it's not ready */
1187 return VK_NOT_READY
;
1189 case ANV_BO_FENCE_STATE_SIGNALED
:
1190 /* It's been signaled, return success */
1193 case ANV_BO_FENCE_STATE_SUBMITTED
: {
1194 VkResult result
= anv_device_bo_busy(device
, impl
->bo
.bo
);
1195 if (result
== VK_SUCCESS
) {
1196 impl
->bo
.state
= ANV_BO_FENCE_STATE_SIGNALED
;
1203 unreachable("Invalid fence status");
1206 case ANV_FENCE_TYPE_SYNCOBJ
: {
1207 int ret
= anv_gem_syncobj_wait(device
, &impl
->syncobj
, 1, 0, true);
1209 if (errno
== ETIME
) {
1210 return VK_NOT_READY
;
1212 /* We don't know the real error. */
1213 return anv_device_set_lost(device
, "drm_syncobj_wait failed: %m");
1221 unreachable("Invalid fence type");
1226 anv_wait_for_syncobj_fences(struct anv_device
*device
,
1227 uint32_t fenceCount
,
1228 const VkFence
*pFences
,
1230 uint64_t abs_timeout_ns
)
1232 uint32_t *syncobjs
= vk_zalloc(&device
->alloc
,
1233 sizeof(*syncobjs
) * fenceCount
, 8,
1234 VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
1236 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1238 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1239 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1240 assert(fence
->permanent
.type
== ANV_FENCE_TYPE_SYNCOBJ
);
1242 struct anv_fence_impl
*impl
=
1243 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1244 &fence
->temporary
: &fence
->permanent
;
1246 assert(impl
->type
== ANV_FENCE_TYPE_SYNCOBJ
);
1247 syncobjs
[i
] = impl
->syncobj
;
1250 /* The gem_syncobj_wait ioctl may return early due to an inherent
1251 * limitation in the way it computes timeouts. Loop until we've actually
1252 * passed the timeout.
1256 ret
= anv_gem_syncobj_wait(device
, syncobjs
, fenceCount
,
1257 abs_timeout_ns
, waitAll
);
1258 } while (ret
== -1 && errno
== ETIME
&& anv_gettime_ns() < abs_timeout_ns
);
1260 vk_free(&device
->alloc
, syncobjs
);
1263 if (errno
== ETIME
) {
1266 /* We don't know the real error. */
1267 return anv_device_set_lost(device
, "drm_syncobj_wait failed: %m");
1275 anv_wait_for_bo_fences(struct anv_device
*device
,
1276 uint32_t fenceCount
,
1277 const VkFence
*pFences
,
1279 uint64_t abs_timeout_ns
)
1281 VkResult result
= VK_SUCCESS
;
1282 uint32_t pending_fences
= fenceCount
;
1283 while (pending_fences
) {
1285 bool signaled_fences
= false;
1286 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1287 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1289 /* This function assumes that all fences are BO fences and that they
1290 * have no temporary state. Since BO fences will never be exported,
1291 * this should be a safe assumption.
1293 assert(fence
->permanent
.type
== ANV_FENCE_TYPE_BO
);
1294 assert(fence
->temporary
.type
== ANV_FENCE_TYPE_NONE
);
1295 struct anv_fence_impl
*impl
= &fence
->permanent
;
1297 switch (impl
->bo
.state
) {
1298 case ANV_BO_FENCE_STATE_RESET
:
1299 /* This fence hasn't been submitted yet, we'll catch it the next
1300 * time around. Yes, this may mean we dead-loop but, short of
1301 * lots of locking and a condition variable, there's not much that
1302 * we can do about that.
1307 case ANV_BO_FENCE_STATE_SIGNALED
:
1308 /* This fence is not pending. If waitAll isn't set, we can return
1309 * early. Otherwise, we have to keep going.
1312 result
= VK_SUCCESS
;
1317 case ANV_BO_FENCE_STATE_SUBMITTED
:
1318 /* These are the fences we really care about. Go ahead and wait
1319 * on it until we hit a timeout.
1321 result
= anv_device_wait(device
, impl
->bo
.bo
,
1322 anv_get_relative_timeout(abs_timeout_ns
));
1325 impl
->bo
.state
= ANV_BO_FENCE_STATE_SIGNALED
;
1326 signaled_fences
= true;
1340 if (pending_fences
&& !signaled_fences
) {
1341 /* If we've hit this then someone decided to vkWaitForFences before
1342 * they've actually submitted any of them to a queue. This is a
1343 * fairly pessimal case, so it's ok to lock here and use a standard
1344 * pthreads condition variable.
1346 pthread_mutex_lock(&device
->mutex
);
1348 /* It's possible that some of the fences have changed state since the
1349 * last time we checked. Now that we have the lock, check for
1350 * pending fences again and don't wait if it's changed.
1352 uint32_t now_pending_fences
= 0;
1353 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1354 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1355 if (fence
->permanent
.bo
.state
== ANV_BO_FENCE_STATE_RESET
)
1356 now_pending_fences
++;
1358 assert(now_pending_fences
<= pending_fences
);
1360 if (now_pending_fences
== pending_fences
) {
1361 struct timespec abstime
= {
1362 .tv_sec
= abs_timeout_ns
/ NSEC_PER_SEC
,
1363 .tv_nsec
= abs_timeout_ns
% NSEC_PER_SEC
,
1367 ret
= pthread_cond_timedwait(&device
->queue_submit
,
1368 &device
->mutex
, &abstime
);
1369 assert(ret
!= EINVAL
);
1370 if (anv_gettime_ns() >= abs_timeout_ns
) {
1371 pthread_mutex_unlock(&device
->mutex
);
1372 result
= VK_TIMEOUT
;
1377 pthread_mutex_unlock(&device
->mutex
);
1382 if (anv_device_is_lost(device
))
1383 return VK_ERROR_DEVICE_LOST
;
1389 anv_wait_for_wsi_fence(struct anv_device
*device
,
1390 const VkFence _fence
,
1391 uint64_t abs_timeout
)
1393 ANV_FROM_HANDLE(anv_fence
, fence
, _fence
);
1394 struct anv_fence_impl
*impl
= &fence
->permanent
;
1396 return impl
->fence_wsi
->wait(impl
->fence_wsi
, abs_timeout
);
1400 anv_wait_for_fences(struct anv_device
*device
,
1401 uint32_t fenceCount
,
1402 const VkFence
*pFences
,
1404 uint64_t abs_timeout
)
1406 VkResult result
= VK_SUCCESS
;
1408 if (fenceCount
<= 1 || waitAll
) {
1409 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1410 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1411 switch (fence
->permanent
.type
) {
1412 case ANV_FENCE_TYPE_BO
:
1413 result
= anv_wait_for_bo_fences(device
, 1, &pFences
[i
],
1416 case ANV_FENCE_TYPE_SYNCOBJ
:
1417 result
= anv_wait_for_syncobj_fences(device
, 1, &pFences
[i
],
1420 case ANV_FENCE_TYPE_WSI
:
1421 result
= anv_wait_for_wsi_fence(device
, pFences
[i
], abs_timeout
);
1423 case ANV_FENCE_TYPE_NONE
:
1424 result
= VK_SUCCESS
;
1427 if (result
!= VK_SUCCESS
)
1432 for (uint32_t i
= 0; i
< fenceCount
; i
++) {
1433 if (anv_wait_for_fences(device
, 1, &pFences
[i
], true, 0) == VK_SUCCESS
)
1436 } while (anv_gettime_ns() < abs_timeout
);
1437 result
= VK_TIMEOUT
;
1442 static bool anv_all_fences_syncobj(uint32_t fenceCount
, const VkFence
*pFences
)
1444 for (uint32_t i
= 0; i
< fenceCount
; ++i
) {
1445 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1446 if (fence
->permanent
.type
!= ANV_FENCE_TYPE_SYNCOBJ
)
1452 static bool anv_all_fences_bo(uint32_t fenceCount
, const VkFence
*pFences
)
1454 for (uint32_t i
= 0; i
< fenceCount
; ++i
) {
1455 ANV_FROM_HANDLE(anv_fence
, fence
, pFences
[i
]);
1456 if (fence
->permanent
.type
!= ANV_FENCE_TYPE_BO
)
1462 VkResult
anv_WaitForFences(
1464 uint32_t fenceCount
,
1465 const VkFence
* pFences
,
1469 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1471 if (anv_device_is_lost(device
))
1472 return VK_ERROR_DEVICE_LOST
;
1474 uint64_t abs_timeout
= anv_get_absolute_timeout(timeout
);
1475 if (anv_all_fences_syncobj(fenceCount
, pFences
)) {
1476 return anv_wait_for_syncobj_fences(device
, fenceCount
, pFences
,
1477 waitAll
, abs_timeout
);
1478 } else if (anv_all_fences_bo(fenceCount
, pFences
)) {
1479 return anv_wait_for_bo_fences(device
, fenceCount
, pFences
,
1480 waitAll
, abs_timeout
);
1482 return anv_wait_for_fences(device
, fenceCount
, pFences
,
1483 waitAll
, abs_timeout
);
1487 void anv_GetPhysicalDeviceExternalFenceProperties(
1488 VkPhysicalDevice physicalDevice
,
1489 const VkPhysicalDeviceExternalFenceInfo
* pExternalFenceInfo
,
1490 VkExternalFenceProperties
* pExternalFenceProperties
)
1492 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1494 switch (pExternalFenceInfo
->handleType
) {
1495 case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT
:
1496 case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
:
1497 if (device
->has_syncobj_wait
) {
1498 pExternalFenceProperties
->exportFromImportedHandleTypes
=
1499 VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT
|
1500 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
;
1501 pExternalFenceProperties
->compatibleHandleTypes
=
1502 VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT
|
1503 VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
;
1504 pExternalFenceProperties
->externalFenceFeatures
=
1505 VK_EXTERNAL_FENCE_FEATURE_EXPORTABLE_BIT
|
1506 VK_EXTERNAL_FENCE_FEATURE_IMPORTABLE_BIT
;
1515 pExternalFenceProperties
->exportFromImportedHandleTypes
= 0;
1516 pExternalFenceProperties
->compatibleHandleTypes
= 0;
1517 pExternalFenceProperties
->externalFenceFeatures
= 0;
1520 VkResult
anv_ImportFenceFdKHR(
1522 const VkImportFenceFdInfoKHR
* pImportFenceFdInfo
)
1524 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1525 ANV_FROM_HANDLE(anv_fence
, fence
, pImportFenceFdInfo
->fence
);
1526 int fd
= pImportFenceFdInfo
->fd
;
1528 assert(pImportFenceFdInfo
->sType
==
1529 VK_STRUCTURE_TYPE_IMPORT_FENCE_FD_INFO_KHR
);
1531 struct anv_fence_impl new_impl
= {
1532 .type
= ANV_FENCE_TYPE_NONE
,
1535 switch (pImportFenceFdInfo
->handleType
) {
1536 case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT
:
1537 new_impl
.type
= ANV_FENCE_TYPE_SYNCOBJ
;
1539 new_impl
.syncobj
= anv_gem_syncobj_fd_to_handle(device
, fd
);
1540 if (!new_impl
.syncobj
)
1541 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1545 case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
:
1546 /* Sync files are a bit tricky. Because we want to continue using the
1547 * syncobj implementation of WaitForFences, we don't use the sync file
1548 * directly but instead import it into a syncobj.
1550 new_impl
.type
= ANV_FENCE_TYPE_SYNCOBJ
;
1552 new_impl
.syncobj
= anv_gem_syncobj_create(device
, 0);
1553 if (!new_impl
.syncobj
)
1554 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1556 if (anv_gem_syncobj_import_sync_file(device
, new_impl
.syncobj
, fd
)) {
1557 anv_gem_syncobj_destroy(device
, new_impl
.syncobj
);
1558 return vk_errorf(device
->instance
, NULL
,
1559 VK_ERROR_INVALID_EXTERNAL_HANDLE
,
1560 "syncobj sync file import failed: %m");
1565 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1568 /* From the Vulkan 1.0.53 spec:
1570 * "Importing a fence payload from a file descriptor transfers
1571 * ownership of the file descriptor from the application to the
1572 * Vulkan implementation. The application must not perform any
1573 * operations on the file descriptor after a successful import."
1575 * If the import fails, we leave the file descriptor open.
1579 if (pImportFenceFdInfo
->flags
& VK_FENCE_IMPORT_TEMPORARY_BIT
) {
1580 anv_fence_impl_cleanup(device
, &fence
->temporary
);
1581 fence
->temporary
= new_impl
;
1583 anv_fence_impl_cleanup(device
, &fence
->permanent
);
1584 fence
->permanent
= new_impl
;
1590 VkResult
anv_GetFenceFdKHR(
1592 const VkFenceGetFdInfoKHR
* pGetFdInfo
,
1595 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1596 ANV_FROM_HANDLE(anv_fence
, fence
, pGetFdInfo
->fence
);
1598 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_FENCE_GET_FD_INFO_KHR
);
1600 struct anv_fence_impl
*impl
=
1601 fence
->temporary
.type
!= ANV_FENCE_TYPE_NONE
?
1602 &fence
->temporary
: &fence
->permanent
;
1604 assert(impl
->type
== ANV_FENCE_TYPE_SYNCOBJ
);
1605 switch (pGetFdInfo
->handleType
) {
1606 case VK_EXTERNAL_FENCE_HANDLE_TYPE_OPAQUE_FD_BIT
: {
1607 int fd
= anv_gem_syncobj_handle_to_fd(device
, impl
->syncobj
);
1609 return vk_error(VK_ERROR_TOO_MANY_OBJECTS
);
1615 case VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT
: {
1616 int fd
= anv_gem_syncobj_export_sync_file(device
, impl
->syncobj
);
1618 return vk_error(VK_ERROR_TOO_MANY_OBJECTS
);
1625 unreachable("Invalid fence export handle type");
1628 /* From the Vulkan 1.0.53 spec:
1630 * "Export operations have the same transference as the specified handle
1631 * type’s import operations. [...] If the fence was using a
1632 * temporarily imported payload, the fence’s prior permanent payload
1635 if (impl
== &fence
->temporary
)
1636 anv_fence_impl_cleanup(device
, impl
);
1641 // Queue semaphore functions
1643 static VkSemaphoreTypeKHR
1644 get_semaphore_type(const void *pNext
, uint64_t *initial_value
)
1646 const VkSemaphoreTypeCreateInfoKHR
*type_info
=
1647 vk_find_struct_const(pNext
, SEMAPHORE_TYPE_CREATE_INFO_KHR
);
1650 return VK_SEMAPHORE_TYPE_BINARY_KHR
;
1653 *initial_value
= type_info
->initialValue
;
1654 return type_info
->semaphoreType
;
1658 binary_semaphore_create(struct anv_device
*device
,
1659 struct anv_semaphore_impl
*impl
,
1662 if (device
->instance
->physicalDevice
.has_syncobj
) {
1663 impl
->type
= ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
;
1664 impl
->syncobj
= anv_gem_syncobj_create(device
, 0);
1666 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1669 impl
->type
= ANV_SEMAPHORE_TYPE_BO
;
1671 anv_device_alloc_bo(device
, 4096,
1672 ANV_BO_ALLOC_EXTERNAL
|
1673 ANV_BO_ALLOC_IMPLICIT_SYNC
,
1674 0 /* explicit_address */,
1676 /* If we're going to use this as a fence, we need to *not* have the
1677 * EXEC_OBJECT_ASYNC bit set.
1679 assert(!(impl
->bo
->flags
& EXEC_OBJECT_ASYNC
));
1685 timeline_semaphore_create(struct anv_device
*device
,
1686 struct anv_semaphore_impl
*impl
,
1687 uint64_t initial_value
)
1689 impl
->type
= ANV_SEMAPHORE_TYPE_TIMELINE
;
1690 anv_timeline_init(device
, &impl
->timeline
, initial_value
);
1694 VkResult
anv_CreateSemaphore(
1696 const VkSemaphoreCreateInfo
* pCreateInfo
,
1697 const VkAllocationCallbacks
* pAllocator
,
1698 VkSemaphore
* pSemaphore
)
1700 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1701 struct anv_semaphore
*semaphore
;
1703 assert(pCreateInfo
->sType
== VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO
);
1705 uint64_t timeline_value
= 0;
1706 VkSemaphoreTypeKHR sem_type
= get_semaphore_type(pCreateInfo
->pNext
, &timeline_value
);
1708 semaphore
= vk_alloc(&device
->alloc
, sizeof(*semaphore
), 8,
1709 VK_SYSTEM_ALLOCATION_SCOPE_DEVICE
);
1710 if (semaphore
== NULL
)
1711 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1713 p_atomic_set(&semaphore
->refcount
, 1);
1715 const VkExportSemaphoreCreateInfo
*export
=
1716 vk_find_struct_const(pCreateInfo
->pNext
, EXPORT_SEMAPHORE_CREATE_INFO
);
1717 VkExternalSemaphoreHandleTypeFlags handleTypes
=
1718 export
? export
->handleTypes
: 0;
1721 if (handleTypes
== 0) {
1722 if (sem_type
== VK_SEMAPHORE_TYPE_BINARY_KHR
)
1723 result
= binary_semaphore_create(device
, &semaphore
->permanent
, false);
1725 result
= timeline_semaphore_create(device
, &semaphore
->permanent
, timeline_value
);
1726 if (result
!= VK_SUCCESS
) {
1727 vk_free2(&device
->alloc
, pAllocator
, semaphore
);
1730 } else if (handleTypes
& VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
) {
1731 assert(handleTypes
== VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
);
1732 assert(sem_type
== VK_SEMAPHORE_TYPE_BINARY_KHR
);
1733 result
= binary_semaphore_create(device
, &semaphore
->permanent
, true);
1734 if (result
!= VK_SUCCESS
) {
1735 vk_free2(&device
->alloc
, pAllocator
, semaphore
);
1738 } else if (handleTypes
& VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
) {
1739 assert(handleTypes
== VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
);
1740 assert(sem_type
== VK_SEMAPHORE_TYPE_BINARY_KHR
);
1741 if (device
->instance
->physicalDevice
.has_syncobj
) {
1742 semaphore
->permanent
.type
= ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
;
1743 semaphore
->permanent
.syncobj
= anv_gem_syncobj_create(device
, 0);
1744 if (!semaphore
->permanent
.syncobj
) {
1745 vk_free2(&device
->alloc
, pAllocator
, semaphore
);
1746 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1749 semaphore
->permanent
.type
= ANV_SEMAPHORE_TYPE_SYNC_FILE
;
1750 semaphore
->permanent
.fd
= -1;
1753 assert(!"Unknown handle type");
1754 vk_free2(&device
->alloc
, pAllocator
, semaphore
);
1755 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1758 semaphore
->temporary
.type
= ANV_SEMAPHORE_TYPE_NONE
;
1760 *pSemaphore
= anv_semaphore_to_handle(semaphore
);
1766 anv_semaphore_impl_cleanup(struct anv_device
*device
,
1767 struct anv_semaphore_impl
*impl
)
1769 switch (impl
->type
) {
1770 case ANV_SEMAPHORE_TYPE_NONE
:
1771 case ANV_SEMAPHORE_TYPE_DUMMY
:
1772 /* Dummy. Nothing to do */
1775 case ANV_SEMAPHORE_TYPE_BO
:
1776 anv_device_release_bo(device
, impl
->bo
);
1779 case ANV_SEMAPHORE_TYPE_SYNC_FILE
:
1783 case ANV_SEMAPHORE_TYPE_TIMELINE
:
1784 anv_timeline_finish(device
, &impl
->timeline
);
1787 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
:
1788 anv_gem_syncobj_destroy(device
, impl
->syncobj
);
1792 unreachable("Invalid semaphore type");
1795 impl
->type
= ANV_SEMAPHORE_TYPE_NONE
;
1799 anv_semaphore_reset_temporary(struct anv_device
*device
,
1800 struct anv_semaphore
*semaphore
)
1802 if (semaphore
->temporary
.type
== ANV_SEMAPHORE_TYPE_NONE
)
1805 anv_semaphore_impl_cleanup(device
, &semaphore
->temporary
);
1808 static struct anv_semaphore
*
1809 anv_semaphore_ref(struct anv_semaphore
*semaphore
)
1811 assert(semaphore
->refcount
);
1812 p_atomic_inc(&semaphore
->refcount
);
1817 anv_semaphore_unref(struct anv_device
*device
, struct anv_semaphore
*semaphore
)
1819 if (!p_atomic_dec_zero(&semaphore
->refcount
))
1822 anv_semaphore_impl_cleanup(device
, &semaphore
->temporary
);
1823 anv_semaphore_impl_cleanup(device
, &semaphore
->permanent
);
1824 vk_free(&device
->alloc
, semaphore
);
1827 void anv_DestroySemaphore(
1829 VkSemaphore _semaphore
,
1830 const VkAllocationCallbacks
* pAllocator
)
1832 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1833 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, _semaphore
);
1835 if (semaphore
== NULL
)
1838 anv_semaphore_unref(device
, semaphore
);
1841 void anv_GetPhysicalDeviceExternalSemaphoreProperties(
1842 VkPhysicalDevice physicalDevice
,
1843 const VkPhysicalDeviceExternalSemaphoreInfo
* pExternalSemaphoreInfo
,
1844 VkExternalSemaphoreProperties
* pExternalSemaphoreProperties
)
1846 ANV_FROM_HANDLE(anv_physical_device
, device
, physicalDevice
);
1848 VkSemaphoreTypeKHR sem_type
=
1849 get_semaphore_type(pExternalSemaphoreInfo
->pNext
, NULL
);
1851 switch (pExternalSemaphoreInfo
->handleType
) {
1852 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
:
1853 /* Timeline semaphores are not exportable. */
1854 if (sem_type
== VK_SEMAPHORE_TYPE_TIMELINE_KHR
)
1856 pExternalSemaphoreProperties
->exportFromImportedHandleTypes
=
1857 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
;
1858 pExternalSemaphoreProperties
->compatibleHandleTypes
=
1859 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
;
1860 pExternalSemaphoreProperties
->externalSemaphoreFeatures
=
1861 VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT
|
1862 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT
;
1865 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
:
1866 if (sem_type
== VK_SEMAPHORE_TYPE_TIMELINE_KHR
)
1868 if (!device
->has_exec_fence
)
1870 pExternalSemaphoreProperties
->exportFromImportedHandleTypes
=
1871 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
;
1872 pExternalSemaphoreProperties
->compatibleHandleTypes
=
1873 VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
;
1874 pExternalSemaphoreProperties
->externalSemaphoreFeatures
=
1875 VK_EXTERNAL_SEMAPHORE_FEATURE_EXPORTABLE_BIT
|
1876 VK_EXTERNAL_SEMAPHORE_FEATURE_IMPORTABLE_BIT
;
1883 pExternalSemaphoreProperties
->exportFromImportedHandleTypes
= 0;
1884 pExternalSemaphoreProperties
->compatibleHandleTypes
= 0;
1885 pExternalSemaphoreProperties
->externalSemaphoreFeatures
= 0;
1888 VkResult
anv_ImportSemaphoreFdKHR(
1890 const VkImportSemaphoreFdInfoKHR
* pImportSemaphoreFdInfo
)
1892 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1893 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, pImportSemaphoreFdInfo
->semaphore
);
1894 int fd
= pImportSemaphoreFdInfo
->fd
;
1896 struct anv_semaphore_impl new_impl
= {
1897 .type
= ANV_SEMAPHORE_TYPE_NONE
,
1900 switch (pImportSemaphoreFdInfo
->handleType
) {
1901 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
:
1902 if (device
->instance
->physicalDevice
.has_syncobj
) {
1903 new_impl
.type
= ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
;
1905 new_impl
.syncobj
= anv_gem_syncobj_fd_to_handle(device
, fd
);
1906 if (!new_impl
.syncobj
)
1907 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1909 new_impl
.type
= ANV_SEMAPHORE_TYPE_BO
;
1911 VkResult result
= anv_device_import_bo(device
, fd
,
1912 ANV_BO_ALLOC_EXTERNAL
|
1913 ANV_BO_ALLOC_IMPLICIT_SYNC
,
1914 0 /* client_address */,
1916 if (result
!= VK_SUCCESS
)
1919 if (new_impl
.bo
->size
< 4096) {
1920 anv_device_release_bo(device
, new_impl
.bo
);
1921 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1924 /* If we're going to use this as a fence, we need to *not* have the
1925 * EXEC_OBJECT_ASYNC bit set.
1927 assert(!(new_impl
.bo
->flags
& EXEC_OBJECT_ASYNC
));
1930 /* From the Vulkan spec:
1932 * "Importing semaphore state from a file descriptor transfers
1933 * ownership of the file descriptor from the application to the
1934 * Vulkan implementation. The application must not perform any
1935 * operations on the file descriptor after a successful import."
1937 * If the import fails, we leave the file descriptor open.
1942 case VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
:
1943 if (device
->instance
->physicalDevice
.has_syncobj
) {
1944 new_impl
= (struct anv_semaphore_impl
) {
1945 .type
= ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
,
1946 .syncobj
= anv_gem_syncobj_create(device
, 0),
1948 if (!new_impl
.syncobj
)
1949 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
1950 if (anv_gem_syncobj_import_sync_file(device
, new_impl
.syncobj
, fd
)) {
1951 anv_gem_syncobj_destroy(device
, new_impl
.syncobj
);
1952 return vk_errorf(device
->instance
, NULL
,
1953 VK_ERROR_INVALID_EXTERNAL_HANDLE
,
1954 "syncobj sync file import failed: %m");
1956 /* Ownership of the FD is transfered to Anv. Since we don't need it
1957 * anymore because the associated fence has been put into a syncobj,
1958 * we must close the FD.
1962 new_impl
= (struct anv_semaphore_impl
) {
1963 .type
= ANV_SEMAPHORE_TYPE_SYNC_FILE
,
1970 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
1973 if (pImportSemaphoreFdInfo
->flags
& VK_SEMAPHORE_IMPORT_TEMPORARY_BIT
) {
1974 anv_semaphore_impl_cleanup(device
, &semaphore
->temporary
);
1975 semaphore
->temporary
= new_impl
;
1977 anv_semaphore_impl_cleanup(device
, &semaphore
->permanent
);
1978 semaphore
->permanent
= new_impl
;
1984 VkResult
anv_GetSemaphoreFdKHR(
1986 const VkSemaphoreGetFdInfoKHR
* pGetFdInfo
,
1989 ANV_FROM_HANDLE(anv_device
, device
, _device
);
1990 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, pGetFdInfo
->semaphore
);
1994 assert(pGetFdInfo
->sType
== VK_STRUCTURE_TYPE_SEMAPHORE_GET_FD_INFO_KHR
);
1996 struct anv_semaphore_impl
*impl
=
1997 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
1998 &semaphore
->temporary
: &semaphore
->permanent
;
2000 switch (impl
->type
) {
2001 case ANV_SEMAPHORE_TYPE_BO
:
2002 result
= anv_device_export_bo(device
, impl
->bo
, pFd
);
2003 if (result
!= VK_SUCCESS
)
2007 case ANV_SEMAPHORE_TYPE_SYNC_FILE
: {
2008 /* There's a potential race here with vkQueueSubmit if you are trying
2009 * to export a semaphore Fd while the queue submit is still happening.
2010 * This can happen if we see all dependencies get resolved via timeline
2011 * semaphore waits completing before the execbuf completes and we
2012 * process the resulting out fence. To work around this, take a lock
2013 * around grabbing the fd.
2015 pthread_mutex_lock(&device
->mutex
);
2017 /* From the Vulkan 1.0.53 spec:
2019 * "...exporting a semaphore payload to a handle with copy
2020 * transference has the same side effects on the source
2021 * semaphore’s payload as executing a semaphore wait operation."
2023 * In other words, it may still be a SYNC_FD semaphore, but it's now
2024 * considered to have been waited on and no longer has a sync file
2030 pthread_mutex_unlock(&device
->mutex
);
2032 /* There are two reasons why this could happen:
2034 * 1) The user is trying to export without submitting something that
2035 * signals the semaphore. If this is the case, it's their bug so
2036 * what we return here doesn't matter.
2038 * 2) The kernel didn't give us a file descriptor. The most likely
2039 * reason for this is running out of file descriptors.
2042 return vk_error(VK_ERROR_TOO_MANY_OBJECTS
);
2048 case ANV_SEMAPHORE_TYPE_DRM_SYNCOBJ
:
2049 if (pGetFdInfo
->handleType
== VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_SYNC_FD_BIT
)
2050 fd
= anv_gem_syncobj_export_sync_file(device
, impl
->syncobj
);
2052 assert(pGetFdInfo
->handleType
== VK_EXTERNAL_SEMAPHORE_HANDLE_TYPE_OPAQUE_FD_BIT
);
2053 fd
= anv_gem_syncobj_handle_to_fd(device
, impl
->syncobj
);
2056 return vk_error(VK_ERROR_TOO_MANY_OBJECTS
);
2061 return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE
);
2064 /* From the Vulkan 1.0.53 spec:
2066 * "Export operations have the same transference as the specified handle
2067 * type’s import operations. [...] If the semaphore was using a
2068 * temporarily imported payload, the semaphore’s prior permanent payload
2071 if (impl
== &semaphore
->temporary
)
2072 anv_semaphore_impl_cleanup(device
, impl
);
2077 VkResult
anv_GetSemaphoreCounterValueKHR(
2079 VkSemaphore _semaphore
,
2082 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2083 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, _semaphore
);
2085 struct anv_semaphore_impl
*impl
=
2086 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
2087 &semaphore
->temporary
: &semaphore
->permanent
;
2089 switch (impl
->type
) {
2090 case ANV_SEMAPHORE_TYPE_TIMELINE
: {
2091 pthread_mutex_lock(&device
->mutex
);
2092 *pValue
= impl
->timeline
.highest_past
;
2093 pthread_mutex_unlock(&device
->mutex
);
2098 unreachable("Invalid semaphore type");
2103 anv_timeline_wait_locked(struct anv_device
*device
,
2104 struct anv_timeline
*timeline
,
2105 uint64_t serial
, uint64_t abs_timeout_ns
)
2107 /* Wait on the queue_submit condition variable until the timeline has a
2108 * time point pending that's at least as high as serial.
2110 while (timeline
->highest_pending
< serial
) {
2111 struct timespec abstime
= {
2112 .tv_sec
= abs_timeout_ns
/ NSEC_PER_SEC
,
2113 .tv_nsec
= abs_timeout_ns
% NSEC_PER_SEC
,
2116 int ret
= pthread_cond_timedwait(&device
->queue_submit
,
2117 &device
->mutex
, &abstime
);
2118 assert(ret
!= EINVAL
);
2119 if (anv_gettime_ns() >= abs_timeout_ns
&&
2120 timeline
->highest_pending
< serial
)
2125 VkResult result
= anv_timeline_gc_locked(device
, timeline
);
2126 if (result
!= VK_SUCCESS
)
2129 if (timeline
->highest_past
>= serial
)
2132 /* If we got here, our earliest time point has a busy BO */
2133 struct anv_timeline_point
*point
=
2134 list_first_entry(&timeline
->points
,
2135 struct anv_timeline_point
, link
);
2137 /* Drop the lock while we wait. */
2139 pthread_mutex_unlock(&device
->mutex
);
2141 result
= anv_device_wait(device
, point
->bo
,
2142 anv_get_relative_timeout(abs_timeout_ns
));
2144 /* Pick the mutex back up */
2145 pthread_mutex_lock(&device
->mutex
);
2148 /* This covers both VK_TIMEOUT and VK_ERROR_DEVICE_LOST */
2149 if (result
!= VK_SUCCESS
)
2155 anv_timelines_wait(struct anv_device
*device
,
2156 struct anv_timeline
**timelines
,
2157 const uint64_t *serials
,
2158 uint32_t n_timelines
,
2160 uint64_t abs_timeout_ns
)
2162 if (!wait_all
&& n_timelines
> 1) {
2165 pthread_mutex_lock(&device
->mutex
);
2166 for (uint32_t i
= 0; i
< n_timelines
; i
++) {
2168 anv_timeline_wait_locked(device
, timelines
[i
], serials
[i
], 0);
2169 if (result
!= VK_TIMEOUT
)
2173 if (result
!= VK_TIMEOUT
||
2174 anv_gettime_ns() >= abs_timeout_ns
) {
2175 pthread_mutex_unlock(&device
->mutex
);
2179 /* If none of them are ready do a short wait so we don't completely
2180 * spin while holding the lock. The 10us is completely arbitrary.
2182 uint64_t abs_short_wait_ns
=
2183 anv_get_absolute_timeout(
2184 MIN2((anv_gettime_ns() - abs_timeout_ns
) / 10, 10 * 1000));
2185 struct timespec abstime
= {
2186 .tv_sec
= abs_short_wait_ns
/ NSEC_PER_SEC
,
2187 .tv_nsec
= abs_short_wait_ns
% NSEC_PER_SEC
,
2190 ret
= pthread_cond_timedwait(&device
->queue_submit
,
2191 &device
->mutex
, &abstime
);
2192 assert(ret
!= EINVAL
);
2195 VkResult result
= VK_SUCCESS
;
2196 pthread_mutex_lock(&device
->mutex
);
2197 for (uint32_t i
= 0; i
< n_timelines
; i
++) {
2199 anv_timeline_wait_locked(device
, timelines
[i
],
2200 serials
[i
], abs_timeout_ns
);
2201 if (result
!= VK_SUCCESS
)
2204 pthread_mutex_unlock(&device
->mutex
);
2209 VkResult
anv_WaitSemaphoresKHR(
2211 const VkSemaphoreWaitInfoKHR
* pWaitInfo
,
2214 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2216 struct anv_timeline
**timelines
=
2217 vk_alloc(&device
->alloc
,
2218 pWaitInfo
->semaphoreCount
* sizeof(*timelines
),
2219 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
2221 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2223 uint64_t *values
= vk_alloc(&device
->alloc
,
2224 pWaitInfo
->semaphoreCount
* sizeof(*values
),
2225 8, VK_SYSTEM_ALLOCATION_SCOPE_COMMAND
);
2227 vk_free(&device
->alloc
, timelines
);
2228 return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY
);
2231 uint32_t handle_count
= 0;
2232 for (uint32_t i
= 0; i
< pWaitInfo
->semaphoreCount
; i
++) {
2233 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, pWaitInfo
->pSemaphores
[i
]);
2234 struct anv_semaphore_impl
*impl
=
2235 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
2236 &semaphore
->temporary
: &semaphore
->permanent
;
2238 assert(impl
->type
== ANV_SEMAPHORE_TYPE_TIMELINE
);
2240 if (pWaitInfo
->pValues
[i
] == 0)
2243 timelines
[handle_count
] = &impl
->timeline
;
2244 values
[handle_count
] = pWaitInfo
->pValues
[i
];
2248 VkResult result
= VK_SUCCESS
;
2249 if (handle_count
> 0) {
2250 result
= anv_timelines_wait(device
, timelines
, values
, handle_count
,
2251 !(pWaitInfo
->flags
& VK_SEMAPHORE_WAIT_ANY_BIT_KHR
),
2255 vk_free(&device
->alloc
, timelines
);
2256 vk_free(&device
->alloc
, values
);
2261 VkResult
anv_SignalSemaphoreKHR(
2263 const VkSemaphoreSignalInfoKHR
* pSignalInfo
)
2265 ANV_FROM_HANDLE(anv_device
, device
, _device
);
2266 ANV_FROM_HANDLE(anv_semaphore
, semaphore
, pSignalInfo
->semaphore
);
2268 struct anv_semaphore_impl
*impl
=
2269 semaphore
->temporary
.type
!= ANV_SEMAPHORE_TYPE_NONE
?
2270 &semaphore
->temporary
: &semaphore
->permanent
;
2272 switch (impl
->type
) {
2273 case ANV_SEMAPHORE_TYPE_TIMELINE
: {
2274 pthread_mutex_lock(&device
->mutex
);
2276 VkResult result
= anv_timeline_gc_locked(device
, &impl
->timeline
);
2278 assert(pSignalInfo
->value
> impl
->timeline
.highest_pending
);
2280 impl
->timeline
.highest_pending
= impl
->timeline
.highest_past
= pSignalInfo
->value
;
2282 if (result
== VK_SUCCESS
)
2283 result
= anv_device_submit_deferred_locked(device
);
2285 pthread_cond_broadcast(&device
->queue_submit
);
2286 pthread_mutex_unlock(&device
->mutex
);
2291 unreachable("Invalid semaphore type");