Skip to content
Merged
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
17 changes: 17 additions & 0 deletions src/asahi/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ if with_tools.contains('drm-shim') and with_asahi_vk and with_tests
suite : ['asahi'],
)

# A sparse image bind as a queue's first submission, with no command buffer.
test(
'hk-g17p-sparse-bind-first-submit',
prog_python,
args : [
files('../gallium/drivers/asahi/tests/run-shim.py'),
libasahi_noop_drm_shim,
hk_vulkan_sparse_bind_first_submit,
],
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
29 changes: 23 additions & 6 deletions src/asahi/vulkan/hk_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,7 @@ hk_QueueBindSparse(VkQueue _queue, uint32_t bindInfoCount,
static bool
hk_sibling_eligible(struct hk_device *dev, const struct hk_queue *queue,
const struct vk_queue_submit *submit,
const struct hk_sync_resolved *resolved,
const uint64_t base[HK_SYNC_NUM_SQ], unsigned command_count,
bool kernel_cumulative)
{
Expand Down Expand Up @@ -1506,9 +1507,10 @@ hk_sibling_eligible(struct hk_device *dev, const struct hk_queue *queue,
return false;

/* A zero barrier names every command of earlier ioctls. */
const struct hk_sync_resolved *r = resolved++;
for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) {
if ((cs->sync.prior[sq] && cs->sync.prior[sq] <= base[sq]) ||
cs->sync.barrier[sq] == 0)
if ((r->prior[sq] && r->prior[sq] <= base[sq]) ||
r->barrier[sq] == 0)
return false;
}
}
Expand Down Expand Up @@ -1594,11 +1596,21 @@ queue_submit(struct hk_device *dev, struct hk_queue *queue,
if (queue->sync.exact_prior &&
!hk_sync_resolve_submit(
queue, (struct hk_cmd_buffer *const *)submit->command_buffers,
submit->command_buffer_count, false, false, 0))
submit->command_buffer_count, false, false, 0, NULL))
return vk_device_set_lost(&dev->vk, "Asahi command ordinal overflow");
return queue_submit_empty(dev, queue, submit);
}

/* Resolved sync fields, one entry per stream occurrence: a
* simultaneous-use command buffer may occur more than once. A sparse bind
* that only flushes the sparse table has none, and resizing a never
* allocated array to zero returns NULL. */
const unsigned resolved_count = command_count - flush_sstt;
if (resolved_count &&
!util_dynarray_resize(&queue->sync_resolved, struct hk_sync_resolved,
resolved_count))
return vk_error(queue, VK_ERROR_OUT_OF_HOST_MEMORY);

bool replaced;
VkResult queue_result = hk_queue_ensure_drm(
dev, queue, hk_submit_needs_render_rq(submit), &replaced);
Expand Down Expand Up @@ -1803,13 +1815,15 @@ queue_submit(struct hk_device *dev, struct hk_queue *queue,
* (hk_drm_serialize_prefix), and keeps no ordinals, so it skips this. */
const bool resolve = !queue->sync.serialize || queue->sync.exact_prior ||
queue->sync.stage_overlap;
const struct hk_sync_resolved *resolved =
resolve ? queue->sync_resolved.data : NULL;
if (resolve && !hk_sync_resolve_submit(queue,
(struct hk_cmd_buffer *const *)submit->command_buffers,
submit->command_buffer_count, flush_sstt,
wait_count > 0 &&
!(dev->dev.params.features &
DRM_ASAHI_FEATURE_PER_COMMAND_INPUT_SYNCS),
completed)) {
completed, queue->sync_resolved.data)) {
util_dynarray_fini(&payload);
if (unlikely(anatomy))
hk_frame_anatomy_end_submit(anatomy, dev, queue, VK_ERROR_DEVICE_LOST);
Expand All @@ -1825,8 +1839,8 @@ queue_submit(struct hk_device *dev, struct hk_queue *queue,
const bool sib_remap = queue->sib.count > 1 && queue->sib.used;
bool sib_earlier = false;
if (queue->sib.count > 1 && !replaced && !flush_sstt && !unlikely(anatomy) &&
hk_sibling_eligible(dev, queue, submit, prior_base, command_count,
kernel_cumulative) &&
hk_sibling_eligible(dev, queue, submit, resolved, prior_base,
command_count, kernel_cumulative) &&
hk_sibling_deep(dev, queue, first_chunk_point - 1)) {
sib_target = queue->sib.next;
queue->sib.next = (queue->sib.next + 1) % queue->sib.count;
Expand Down Expand Up @@ -1864,6 +1878,8 @@ queue_submit(struct hk_device *dev, struct hk_queue *queue,
}

list_for_each_entry(struct hk_cs, cs, &cmdbuf->control_streams, node) {
if (resolved)
hk_sync_load_resolved(cs, resolved++);
struct drm_asahi_cmd_header header =
agx_cmd_header(cs->type == HK_CS_CDM, cs->sync.barrier[HK_SYNC_VDM],
cs->sync.barrier[HK_SYNC_CDM]);
Expand Down Expand Up @@ -2651,5 +2667,6 @@ hk_queue_finish(struct hk_device *dev, struct hk_queue *queue)
agx_destroy_command_queue(&dev->dev, queue->sib.id[i]);
}
agx_bo_unreference(&dev->dev, queue->g17p_sstt_flush_bo);
util_dynarray_fini(&queue->sync_resolved);
vk_free(&dev->vk.alloc, queue);
}
4 changes: 4 additions & 0 deletions src/asahi/vulkan/hk_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ struct hk_queue {
/* Kinds of streams this queue has ever run, see hk_sync.h. */
struct hk_sync_queue sync;

/* Scratch for queue_submit: the resolved sync fields of each stream
* occurrence of the submission being built (hk_sync_resolve_submit()). */
struct util_dynarray sync_resolved;

/* Compute kick (hk_compute_kick.c). watched is set at device creation
* for a queue whose priority class has a kick queue. The rest is
* protected by the kicker's lock. */
Expand Down
128 changes: 70 additions & 58 deletions src/asahi/vulkan/hk_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -1779,11 +1779,21 @@ resolve_one(const struct submit_state *ss, unsigned sq,
}
}

void
hk_sync_load_resolved(struct hk_cs *cs, const struct hk_sync_resolved *r)
{
memcpy(cs->sync.barrier, r->barrier, sizeof(r->barrier));
memcpy(cs->sync.barrier_ta, r->barrier_ta, sizeof(r->barrier_ta));
memcpy(cs->sync.prior, r->prior, sizeof(r->prior));
memcpy(cs->sync.prior_ta, r->prior_ta, sizeof(r->prior_ta));
cs->sync.ta_split = r->ta_split;
}

bool
hk_sync_resolve_submit(struct hk_queue *queue,
struct hk_cmd_buffer *const *cmdbufs, unsigned count,
bool first_cdm_is_flush, bool chain_first,
uint64_t completed)
uint64_t completed, struct hk_sync_resolved *out)
{
/* Validate all ordinal arithmetic before mutating command or queue state.
* Historical ordinals use zero for absence, so wrapping them is invalid.
Expand Down Expand Up @@ -1857,6 +1867,32 @@ hk_sync_resolve_submit(struct hk_queue *queue,
}
}

/* Legacy kernels attach input syncs to the first hardware command only;
* the caller requests that chain when needed. A sparse flush must still
* precede everything. Every
* command that does not already follow the first one transitively gets
* an explicit wait on it. Waits are prefixes, so following any command
* that follows the first is enough. */
/* The first command is the first stream of the first command buffer that
* emits any: command buffers without streams (barrier-only or empty) send
* nothing to the kernel. Without any command there is nothing to chain. */
int first_sq = first_cdm_is_flush ? HK_SYNC_CDM : -1;
for (unsigned i = 0; first_sq < 0 && i < count; ++i) {
if (!list_is_empty(&cmdbufs[i]->control_streams))
first_sq = hk_sync_sq(list_first_entry(&cmdbufs[i]->control_streams,
struct hk_cs, node));
}
const bool chain = (chain_first || first_cdm_is_flush) && first_sq >= 0;

/* covered[sq]: the lowest index on sq of a command that follows the
* first command, 0 if none yet. A wait on prefix b of subqueue d
* follows the first command when covered[d] is within that prefix. */
uint32_t covered[HK_SYNC_NUM_SQ] = {0, first_cdm_is_flush ? 1 : 0};
bool seen_first = first_cdm_is_flush;

/* A command buffer may occur more than once: everything below that reads
* or writes a stream's index and resolved fields finishes with one
* occurrence, and saves it to out, before the next one starts. */
for (unsigned i = 0; i < count; ++i) {
struct hk_cmd_buffer *cmd = cmdbufs[i];

Expand Down Expand Up @@ -1969,36 +2005,9 @@ hk_sync_resolve_submit(struct hk_queue *queue,
ss.last[k][sq] = value_max(ss.last[k][sq], last->sync.index);
}
}
}

/* Legacy kernels attach input syncs to the first hardware command only;
* the caller requests that chain when needed. A sparse flush must still
* precede everything. Every
* command that does not already follow the first one transitively gets
* an explicit wait on it. Waits are prefixes, so following any command
* that follows the first is enough. */
/* The first command is the first stream of the first command buffer that
* emits any: command buffers without streams (barrier-only or empty) send
* nothing to the kernel. Without any command there is nothing to chain. */
int first_sq = first_cdm_is_flush ? HK_SYNC_CDM : -1;
for (unsigned i = 0; first_sq < 0 && i < count; ++i) {
if (!list_is_empty(&cmdbufs[i]->control_streams))
first_sq = hk_sync_sq(list_first_entry(&cmdbufs[i]->control_streams,
struct hk_cs, node));
}

if ((chain_first || first_cdm_is_flush) && first_sq >= 0) {
/* covered[sq]: the lowest index on sq of a command that follows the
* first command, 0 if none yet. A wait on prefix b of subqueue d
* follows the first command when covered[d] is within that prefix. */
uint32_t covered[HK_SYNC_NUM_SQ] = {0, 0};
if (first_cdm_is_flush)
covered[HK_SYNC_CDM] = 1;

bool seen_first = first_cdm_is_flush;
for (unsigned i = 0; i < count; ++i) {
list_for_each_entry(struct hk_cs, cs, &cmdbufs[i]->control_streams,
node) {
list_for_each_entry(struct hk_cs, cs, &cmd->control_streams, node) {
if (chain) {
const unsigned sq = hk_sync_sq(cs);
bool covers = false;

Expand All @@ -2023,41 +2032,44 @@ hk_sync_resolve_submit(struct hk_queue *queue,
if (covers && covered[sq] == 0)
covered[sq] = cs->sync.index;
}
}
}

/* A render's vertex half may wait for less than its fragment half only
* when the kernel orders the halves separately and nothing above raised
* the whole render's fields afterwards (cross-command-buffer tile hazards
* and the first-command chain only touch barrier[]): recompute, and keep
* the vertex half at least as strict as before. */
for (unsigned i = 0; i < count; ++i) {
list_for_each_entry(struct hk_cs, cs, &cmdbufs[i]->control_streams,
node) {
/* A render's vertex half may wait for less than its fragment half
* only when the kernel orders the halves separately and nothing
* above raised the whole render's fields afterwards
* (cross-command-buffer tile hazards and the first-command chain
* only touch barrier[]): recompute, and keep the vertex half at
* least as strict as before. */
cs->sync.ta_split = false;
if (cs->type == HK_CS_CDM)
continue;
if (chain_first || first_cdm_is_flush) {
if (cs->type != HK_CS_CDM && (chain_first || first_cdm_is_flush)) {
memcpy(cs->sync.barrier_ta, cs->sync.barrier,
sizeof(cs->sync.barrier_ta));
memcpy(cs->sync.prior_ta, cs->sync.prior,
sizeof(cs->sync.prior_ta));
continue;
}
/* M5: tiling whose pre-rasterization programs keep state in
* thread-private storage corrupts that state when it overlaps an
* earlier render's fragment work (Counter-Strike 2: one-frame
* garbage geometry and blocks, only for such renders). Such a
* render's vertex half keeps the whole render-subqueue prefix;
* its compute prefix may still be narrower. */
if (cs->m5_vertex_tps) {
cs->sync.barrier_ta[HK_SYNC_VDM] = cs->sync.barrier[HK_SYNC_VDM];
cs->sync.prior_ta[HK_SYNC_VDM] = cs->sync.prior[HK_SYNC_VDM];
} else if (cs->type != HK_CS_CDM) {
/* M5: tiling whose pre-rasterization programs keep state in
* thread-private storage corrupts that state when it overlaps an
* earlier render's fragment work (Counter-Strike 2: one-frame
* garbage geometry and blocks, only for such renders). Such a
* render's vertex half keeps the whole render-subqueue prefix;
* its compute prefix may still be narrower. */
if (cs->m5_vertex_tps) {
cs->sync.barrier_ta[HK_SYNC_VDM] = cs->sync.barrier[HK_SYNC_VDM];
cs->sync.prior_ta[HK_SYNC_VDM] = cs->sync.prior[HK_SYNC_VDM];
}
for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) {
cs->sync.ta_split |=
cs->sync.barrier_ta[sq] != cs->sync.barrier[sq] ||
cs->sync.prior_ta[sq] != cs->sync.prior[sq];
}
}
for (unsigned sq = 0; sq < HK_SYNC_NUM_SQ; ++sq) {
cs->sync.ta_split |=
cs->sync.barrier_ta[sq] != cs->sync.barrier[sq] ||
cs->sync.prior_ta[sq] != cs->sync.prior[sq];

if (out) {
struct hk_sync_resolved *r = out++;
memcpy(r->barrier, cs->sync.barrier, sizeof(r->barrier));
memcpy(r->barrier_ta, cs->sync.barrier_ta, sizeof(r->barrier_ta));
memcpy(r->prior, cs->sync.prior, sizeof(r->prior));
memcpy(r->prior_ta, cs->sync.prior_ta, sizeof(r->prior_ta));
r->ta_split = cs->sync.ta_split;
}
}
}
Expand Down
24 changes: 22 additions & 2 deletions src/asahi/vulkan/hk_sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ struct hk_sync_stream {
struct hk_cs *prev_mem;

/* Set at submission: 1-based position among the commands of this
* subqueue in the ioctl being built. */
* subqueue in the ioctl being built. Like the resolved fields below, it
* describes one occurrence of the stream: a simultaneous-use command
* buffer may occur more than once in a submission
* (hk_sync_resolve_submit()). */
uint32_t index;

/* Resolved barrier fields for the ioctl being built. */
Expand Down Expand Up @@ -259,6 +262,15 @@ struct hk_sync_stream {
uint32_t vtx_images_epoch;
};

/* The resolved fields of one occurrence of a stream in a submission. */
struct hk_sync_resolved {
uint16_t barrier[HK_SYNC_NUM_SQ];
uint16_t barrier_ta[HK_SYNC_NUM_SQ];
uint64_t prior[HK_SYNC_NUM_SQ];
uint64_t prior_ta[HK_SYNC_NUM_SQ];
bool ta_split;
};

/* Per-command-buffer state. */
struct hk_sync_state {
uint32_t next_seq;
Expand Down Expand Up @@ -517,8 +529,16 @@ void hk_sync_resolve_end(struct hk_cmd_buffer *cmd);
* command must run after; chain_first says the commands must transitively
* follow the first hardware command (the kernel attaches the ioctl's input
* syncs only to it).
*
* A command buffer may occur more than once in cmdbufs, and its streams then
* resolve differently at each occurrence. Unless out is NULL, it receives
* one entry per stream occurrence in submission order; the caller loads each
* into its stream (hk_sync_load_resolved()) before emitting it. The streams
* keep the fields of their last occurrence.
*/
bool hk_sync_resolve_submit(struct hk_queue *queue,
struct hk_cmd_buffer *const *cmdbufs,
unsigned count, bool first_cdm_is_flush,
bool chain_first, uint64_t completed);
bool chain_first, uint64_t completed,
struct hk_sync_resolved *out);
void hk_sync_load_resolved(struct hk_cs *cs, const struct hk_sync_resolved *r);
9 changes: 9 additions & 0 deletions src/asahi/vulkan/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,15 @@ if with_tests
gnu_symbol_visibility : 'hidden',
)

hk_vulkan_sparse_bind_first_submit = executable(
'hk-vulkan-sparse-bind-first-submit',
files('tests/hk_vulkan_sparse_bind_first_submit.c'),
include_directories : [inc_include],
dependencies : [cc.find_library('vulkan')],
c_args : [c_msvc_compat_args],
gnu_symbol_visibility : 'hidden',
)

hk_vulkan_explicit_plane_layout = executable(
'hk-vulkan-explicit-plane-layout',
files('tests/hk_vulkan_explicit_plane_layout.c'),
Expand Down
Loading