Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/asahi/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,24 @@ if with_tools.contains('drm-shim') and with_asahi_vk and with_tests
timeout : 120,
)

# A host-only vkCmdWaitEvents2 starts its poll in an ioctl sent after
# vkSetEvent; a device-set event's wait stays in the one ioctl.
test(
'hk-g17p-host-event-wait',
prog_python,
args : [
files('../gallium/drivers/asahi/tests/run-shim.py'),
libasahi_noop_drm_shim,
hk_vulkan_host_event_wait,
],
env : [
'ASAHI_SHIM_CHIP=g17p',
'VK_DRIVER_FILES=' + _dev_icd.full_path(),
],
depends : [_dev_icd, libvulkan_asahi],
suite : ['asahi'],
)

# Explicit DRM plane layouts: placement, requirements, subresource layouts
# and validation, without executing on the GPU.
foreach chip : ['g13g', 'g17p']
Expand Down
3 changes: 2 additions & 1 deletion src/asahi/vulkan/hk_cmd_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ hk_reset_cmd_buffer(struct vk_command_buffer *vk_cmd_buffer,
cmd->geom_index_count = 0;
cmd->geom_instance_count = 0;
cmd->uses_heap = false;
cmd->host_waits = 0;
hk_sync_reset(cmd);
if (cmd->m5_pushed_images)
_mesa_set_clear(cmd->m5_pushed_images, NULL);
Expand Down Expand Up @@ -785,7 +786,7 @@ merge_control_streams(struct hk_cmd_buffer *cmd)
!last->timestamp.end.handle &&
!(last->timestamp.start.handle && cs->g17p_timestamp_query) &&
!(last->timestamp.start.handle && cs->timestamp.start.handle) &&
!hk_sync_merge_delays(cmd, last, cs)) {
!cs->host_wait && !hk_sync_merge_delays(cmd, last, cs)) {

hk_sync_merge(cmd, last, cs);
hk_cs_merge_cdm(last, cs);
Expand Down
8 changes: 8 additions & 0 deletions src/asahi/vulkan/hk_cmd_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,11 @@ struct hk_cs {
bool gen1_native_vertex_rq;
bool m5_matrix_ops;

/* The host-set event a host-only vkCmdWaitEvents2 polls for in this
* stream: the stream starts a command of its own (merge_control_streams),
* which submission sends after vkSetEvent. */
struct hk_event *host_wait;

/* Threads of the application's direct dispatches in this stream, and
* whether it holds an indirect one (hk_queue::sib only takes streams with
* enough work to overlap). */
Expand Down Expand Up @@ -1857,6 +1862,9 @@ struct hk_cmd_buffer {
/* Does the command buffer use the geometry heap? */
bool uses_heap;

/* Streams with hk_cs::host_wait. */
uint32_t host_waits;

/* Owned BOs and retained references needed by recorded commands. */
struct util_dynarray large_bos;
/* Direct-mapped temporal cache avoids rescanning large_bos for the linked
Expand Down
10 changes: 10 additions & 0 deletions src/asahi/vulkan/hk_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,8 @@ hk_CreateDevice(VkPhysicalDevice physicalDevice,

*pDevice = hk_device_to_handle(dev);

mtx_init(&dev->host_event.lock, mtx_plain);
cnd_init(&dev->host_event.set);
simple_mtx_init(&dev->cmd_pending_lock, mtx_plain);
list_inithead(&dev->cmd_pending_queues);
list_inithead(&dev->cmd_pending_failed);
Expand Down Expand Up @@ -1315,6 +1317,12 @@ hk_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator)
/* Its thread and kicks use the queues and the device's kernels. */
hk_compute_kick_stop(dev);

/* A submission still waiting for a vkSetEvent fails instead. */
mtx_lock(&dev->host_event.lock);
dev->host_event.stop = true;
cnd_broadcast(&dev->host_event.set);
mtx_unlock(&dev->host_event.lock);

util_dynarray_fini(&dev->external_bos.counts);
util_dynarray_fini(&dev->external_bos.list);
u_rwlock_destroy(&dev->external_bos.lock);
Expand Down Expand Up @@ -1352,6 +1360,8 @@ hk_DestroyDevice(VkDevice _device, const VkAllocationCallbacks *pAllocator)
* a new recording during teardown. Kernel job pins retain failed work. */
hk_cmd_pending_finish(dev);
simple_mtx_destroy(&dev->cmd_pending_lock);
cnd_destroy(&dev->host_event.set);
mtx_destroy(&dev->host_event.lock);
hk_device_finish_vdm_slabs(dev);
simple_mtx_destroy(&dev->vdm.lock);

Expand Down
9 changes: 9 additions & 0 deletions src/asahi/vulkan/hk_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,15 @@ struct hk_device {
uint32_t next_size[HK_CMD_SLAB_COUNT];
} vdm;

/* A host vkSetEvent wakes a submission waiting for it on a submit
* thread (hk_event_wait_host_set()); stop ends those waits when the
* device is destroyed, whose queues join their submit threads. */
struct {
mtx_t lock;
cnd_t set;
bool stop;
} host_event;

bool cmd_pending_ready;
simple_mtx_t cmd_pending_lock;
/* Queues with pending submissions (hk_queue::cmd_link). */
Expand Down
63 changes: 63 additions & 0 deletions src/asahi/vulkan/hk_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,46 @@ hk_GetEventStatus(VkDevice device, VkEvent _event)
VKAPI_ATTR VkResult VKAPI_CALL
hk_SetEvent(VkDevice device, VkEvent _event)
{
VK_FROM_HANDLE(hk_device, dev, device);
VK_FROM_HANDLE(hk_event, event, _event);

mtx_lock(&dev->host_event.lock);
*event->status = VK_EVENT_SET;
event->host_sets++;
cnd_broadcast(&dev->host_event.set);
mtx_unlock(&dev->host_event.lock);

return VK_SUCCESS;
}

/* Whether the event is still reset. *sets is the count of host sets so far,
* for hk_event_wait_host_set(). */
bool
hk_event_host_unset(struct hk_device *dev, struct hk_event *event,
uint32_t *sets)
{
mtx_lock(&dev->host_event.lock);
*sets = event->host_sets;
const bool unset = *event->status != VK_EVENT_SET;
mtx_unlock(&dev->host_event.lock);
return unset;
}

/* Until the host sets the event after the sets counted. False if the device
* is being destroyed first. */
bool
hk_event_wait_host_set(struct hk_device *dev, struct hk_event *event,
uint32_t sets)
{
mtx_lock(&dev->host_event.lock);
while (!dev->host_event.stop && event->host_sets == sets &&
*event->status != VK_EVENT_SET)
cnd_wait(&dev->host_event.set, &dev->host_event.lock);
const bool set = !dev->host_event.stop;
mtx_unlock(&dev->host_event.lock);
return set;
}

VKAPI_ATTR VkResult VKAPI_CALL
hk_ResetEvent(VkDevice device, VkEvent _event)
{
Expand Down Expand Up @@ -163,6 +196,24 @@ hk_event_wait_polls(const struct hk_device *dev)
return dev->dev.chip == AGX_CHIP_G17P;
}

/* Whether the first synchronization scope is host operations alone. Only
* vkSetEvent sets the event then: a wait on a host-set event names only the
* host stage (VUID-vkCmdWaitEvents2-pEvents-03839), and vkCmdSetEvent2,
* whose scope the wait repeats, cannot name it. A legacy vkCmdWaitEvents
* mask that adds device stages for other events is not host-only. */
static bool
hk_dependency_host_only(const VkDependencyInfo *info)
{
VkPipelineStageFlags2 src = 0;
for (uint32_t i = 0; i < info->memoryBarrierCount; ++i)
src |= info->pMemoryBarriers[i].srcStageMask;
for (uint32_t i = 0; i < info->bufferMemoryBarrierCount; ++i)
src |= info->pBufferMemoryBarriers[i].srcStageMask;
for (uint32_t i = 0; i < info->imageMemoryBarrierCount; ++i)
src |= info->pImageMemoryBarriers[i].srcStageMask;
return src == VK_PIPELINE_STAGE_2_HOST_BIT;
}

VKAPI_ATTR void VKAPI_CALL
hk_CmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount,
const VkEvent *pEvents,
Expand Down Expand Up @@ -192,6 +243,14 @@ hk_CmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount,
hk_sync_src_dep(cmd, src_kinds, cmd->current_cs.cs, dep);

if (hk_event_wait_polls(dev)) {
/* A host-only wait polls in a stream of its own: the GPU does not
* see a host write made while a command runs, so submission holds
* that stream's command back until vkSetEvent
* (queue_submit_looped()). */
const bool host = hk_dependency_host_only(&pDependencyInfos[i]);
if (host)
hk_cmd_buffer_end_compute(cmd);

cmd->sync.kind_override = HK_SYNC_ALL;
struct hk_cs *cs = hk_sync_get_cs_after(cmd, dep);
if (cs)
Expand All @@ -203,6 +262,10 @@ hk_CmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount,
/* Later work of the destination kinds runs after the poll. The
* poll stream is ended so it cannot absorb that work. */
cs = cmd->current_cs.cs ? cmd->current_cs.cs : cs;
if (host && cs) {
cs->host_wait = event;
cmd->host_waits++;
}
hk_sync_after_stream(cmd, cs, dst_kinds);
hk_cmd_buffer_end_compute(cmd);
}
Expand Down
10 changes: 10 additions & 0 deletions src/asahi/vulkan/hk_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ struct hk_event {

uint64_t addr;
VkResult *status;

/* Host sets, under hk_device::host_event.lock. */
uint32_t host_sets;
};

struct hk_device;

bool hk_event_host_unset(struct hk_device *dev, struct hk_event *event,
uint32_t *sets);
bool hk_event_wait_host_set(struct hk_device *dev, struct hk_event *event,
uint32_t sets);

VK_DEFINE_NONDISP_HANDLE_CASTS(hk_event, base, VkEvent, VK_OBJECT_TYPE_EVENT)
Loading