diff --git a/src/asahi/vulkan/hk_cmd_pool.c b/src/asahi/vulkan/hk_cmd_pool.c index 00b614d82613..1b2cdac42c34 100644 --- a/src/asahi/vulkan/hk_cmd_pool.c +++ b/src/asahi/vulkan/hk_cmd_pool.c @@ -803,23 +803,54 @@ hk_cmd_pending_poll(struct hk_device *dev, struct hk_cmd_pending *p, bool wait) /* A queue's submissions are in timeline order, and a reached timeline point * means every timeline signal up to it has happened. Check the queue's * submissions oldest first and stop at the first one past the reached point. - * The point only skips the wait: each submission's own syncobjs are still - * read before its memory is released. Fences on the timeline can signal out - * of order, so an unfinished submission can hold back finished ones behind - * it; once it has done so for a while, a few of them are also polled on each - * reap. Finished work behind several long-running submissions stays pinned - * until those finish. Called with cmd_pending_lock held. + * When the kernel's VM status mirror is mapped and clean (0), a single-chunk + * submission whose point is reached completed without error and can be reaped + * without per-syncobj ExportSyncFile + SYNC_IOC_FILE_INFO + close syscalls. + * Multi-chunk submissions, lost devices, and non-zero mirror words fall back + * to per-syncobj polling. Fences on the timeline can signal out of order, so + * an unfinished submission can hold back finished ones behind it; once it has + * done so for a while, a few of them are also polled on each reap. Finished + * work behind several long-running submissions stays pinned until those + * finish. Called with cmd_pending_lock held. */ static void hk_cmd_pending_reap_queue(struct hk_device *dev, struct hk_queue *queue, - struct list_head *ready) + struct list_head *ready, + const struct hk_queue *cached_queue, + bool *cached_valid, uint64_t *cached_done) { - uint64_t reached = 0; - if (drmSyncobjQuery(dev->dev.fd, &queue->drm.syncobj, &reached, 1)) - reached = 0; - struct hk_cmd_pending *oldest = list_first_entry(&queue->cmd_pending, struct hk_cmd_pending, link); + struct hk_cmd_pending *newest = + list_last_entry(&queue->cmd_pending, struct hk_cmd_pending, link); + + uint64_t reached = p_atomic_read(&queue->drm.signalled_value); + if (cached_queue == queue && cached_valid && *cached_valid) { + reached = MAX2(reached, *cached_done); + } else if (reached < newest->point) { + uint64_t queried = 0; + if (drmSyncobjQuery(dev->dev.fd, &queue->drm.syncobj, &queried, 1) == 0) { + hk_queue_note_signalled(queue, queried); + reached = MAX2(reached, queried); + if (cached_queue == queue && cached_valid && cached_done) { + *cached_done = reached; + *cached_valid = true; + } + } + } + + /* The kernel stores any sticky VM error into vm_status_mirror before + * signalling that job's fence (hk_init_vm_status_mirror()). An acquire load + * after observing the reached timeline point guarantees every single-chunk + * submission up to `reached` completed without error, avoiding three + * syscalls (ExportSyncFile + SYNC_IOC_FILE_INFO + close) per retired + * submission. Any non-zero error word, lost device, or multi-chunk + * submission falls back to per-syncobj polling. */ + const bool vm_clean = + dev->vm_status_mirror.word != NULL && + !vk_device_is_lost_no_report(&dev->vk) && + __atomic_load_n(dev->vm_status_mirror.word, __ATOMIC_ACQUIRE) == 0; + if (oldest->point <= reached) queue->cmd_stalled = 0; else if (queue->cmd_stalled < HK_CMD_STALL_REAPS) @@ -834,7 +865,12 @@ hk_cmd_pending_reap_queue(struct hk_device *dev, struct hk_queue *queue, if (!signalled && !polls) break; - switch (hk_cmd_pending_poll(dev, p, !signalled)) { + const enum hk_cmd_pending_state state = + (signalled && vm_clean && p->chunks == 1 && p->attempted == 1) + ? HK_PENDING_SUCCEEDED + : hk_cmd_pending_poll(dev, p, !signalled); + + switch (state) { case HK_PENDING_BUSY: if (!signalled) polls--; @@ -860,7 +896,9 @@ hk_cmd_pending_reap_queue(struct hk_device *dev, struct hk_queue *queue, } void -hk_cmd_pending_reap(struct hk_device *dev) +hk_cmd_pending_reap_cached(struct hk_device *dev, + const struct hk_queue *cached_queue, + bool *cached_valid, uint64_t *cached_done) { /* Meta command pools can be destroyed during device-init failure. Skip the * device lock when no driver-owned pending submissions are active. */ @@ -873,7 +911,8 @@ hk_cmd_pending_reap(struct hk_device *dev) simple_mtx_lock(&dev->cmd_pending_lock); list_for_each_entry_safe(struct hk_queue, queue, &dev->cmd_pending_queues, cmd_link) - hk_cmd_pending_reap_queue(dev, queue, &ready); + hk_cmd_pending_reap_queue(dev, queue, &ready, cached_queue, cached_valid, + cached_done); simple_mtx_unlock(&dev->cmd_pending_lock); /* Releasing a span takes vdm.lock; never do it under the pending lock. */ @@ -883,13 +922,22 @@ hk_cmd_pending_reap(struct hk_device *dev) } } +void +hk_cmd_pending_reap(struct hk_device *dev) +{ + hk_cmd_pending_reap_cached(dev, NULL, NULL, NULL); +} + VkResult hk_cmd_pending_begin(struct hk_device *dev, unsigned chunks, struct agx_bo *const *extra_bos, unsigned extra_bo_count, + struct hk_queue *reap_queue, bool *reap_done_valid, + uint64_t *reap_done, bool already_reaped, struct hk_cmd_pending **out) { *out = NULL; - hk_cmd_pending_reap(dev); + if (!already_reaped) + hk_cmd_pending_reap_cached(dev, reap_queue, reap_done_valid, reap_done); size_t count = extra_bo_count; if (count > (SIZE_MAX - sizeof(struct hk_cmd_pending)) / diff --git a/src/asahi/vulkan/hk_cmd_pool.h b/src/asahi/vulkan/hk_cmd_pool.h index 710d6296e0d3..cd630a3fe0ac 100644 --- a/src/asahi/vulkan/hk_cmd_pool.h +++ b/src/asahi/vulkan/hk_cmd_pool.h @@ -209,14 +209,20 @@ struct hk_cmd_pending; struct hk_queue; void hk_cmd_pending_init(struct hk_device *dev); VkResult hk_cmd_pending_begin(struct hk_device *dev, unsigned chunks, - struct agx_bo *const *extra_bos, - unsigned extra_bo_count, - struct hk_cmd_pending **out); + struct agx_bo *const *extra_bos, + unsigned extra_bo_count, + struct hk_queue *reap_queue, + bool *reap_done_valid, uint64_t *reap_done, + bool already_reaped, + struct hk_cmd_pending **out); uint32_t hk_cmd_pending_signal(struct hk_cmd_pending *pending); void hk_cmd_pending_end(struct hk_device *dev, struct hk_cmd_pending *pending, struct hk_queue *queue, VkResult result); void hk_cmd_pending_retain_lost(struct hk_device *dev, struct hk_cmd_buffer *cmd); +void hk_cmd_pending_reap_cached(struct hk_device *dev, + const struct hk_queue *cached_queue, + bool *cached_valid, uint64_t *cached_done); void hk_cmd_pending_reap(struct hk_device *dev); void hk_cmd_pending_finish_queue(struct hk_device *dev, struct hk_queue *queue); void hk_cmd_pending_finish(struct hk_device *dev); diff --git a/src/asahi/vulkan/hk_queue.c b/src/asahi/vulkan/hk_queue.c index 79273595eb06..50526f7a4fc5 100644 --- a/src/asahi/vulkan/hk_queue.c +++ b/src/asahi/vulkan/hk_queue.c @@ -1709,51 +1709,78 @@ hk_sibling_eligible(struct hk_device *dev, const struct hk_queue *queue, * (a depth-2 compute stream ran 19% slower with siblings on the M4 Pro). * With work queued behind them, the overlap fills the launch and drain gaps * between kicks instead. */ -/* Query the queue timeline's signalled point, short-circuiting through the - * cached monotonic lower bound whenever it already covers `target`. */ +/* Per-submission cache of queue->drm.syncobj's observed timeline point so a + * single submission queries the queue timeline at most once across packing, + * pending-reap, timestamp/wait hazards, and sibling eligibility checks. */ +struct hk_queue_done_cache { + bool valid; + bool reaped; + uint64_t point; +}; + static bool -hk_queue_query_progress(struct hk_device *dev, struct hk_queue *queue, - uint64_t target, uint64_t *done_out) +hk_queue_query_done(struct hk_device *dev, struct hk_queue *queue, + struct hk_queue_done_cache *cache, uint64_t *done_out) { - uint64_t done = p_atomic_read(&queue->drm.signalled_value); - if (done >= target) { - *done_out = done; + if (cache && cache->valid) { + *done_out = MAX2(cache->point, p_atomic_read(&queue->drm.signalled_value)); return true; } + uint64_t done = 0; if (drmSyncobjQuery(dev->dev.fd, &queue->drm.syncobj, &done, 1) != 0) return false; hk_queue_note_signalled(queue, done); + if (cache) { + cache->point = done; + cache->valid = true; + } *done_out = done; return true; } +/* Query the queue timeline's signalled point, short-circuiting through the + * cached monotonic lower bound whenever it already covers `target`. */ +static bool +hk_queue_query_progress(struct hk_device *dev, struct hk_queue *queue, + uint64_t target, struct hk_queue_done_cache *cache, + uint64_t *done_out) +{ + uint64_t done = p_atomic_read(&queue->drm.signalled_value); + if (done >= target) { + *done_out = done; + return true; + } + + return hk_queue_query_done(dev, queue, cache, done_out); +} + static bool hk_sibling_deep(struct hk_device *dev, struct hk_queue *queue, - uint64_t submitted_point) + uint64_t submitted_point, struct hk_queue_done_cache *cache) { uint64_t done = p_atomic_read(&queue->drm.signalled_value); if (submitted_point <= done || submitted_point - done < HK_G17P_SIBLING_MIN_IN_FLIGHT) return false; - if (drmSyncobjQuery(dev->dev.fd, &queue->drm.syncobj, &done, 1)) + if (!hk_queue_query_done(dev, queue, cache, &done)) return false; - hk_queue_note_signalled(queue, done); return submitted_point > done && submitted_point - done >= HK_G17P_SIBLING_MIN_IN_FLIGHT; } /* Whether any sibling kernel queue may still be executing work. */ static bool -hk_sibling_in_flight(struct hk_device *dev, struct hk_queue *queue) +hk_sibling_in_flight(struct hk_device *dev, struct hk_queue *queue, + struct hk_queue_done_cache *cache) { if (queue->sib.count <= 1 || !queue->sib.used || !queue->sib.last_point) return false; uint64_t done = 0; - if (!hk_queue_query_progress(dev, queue, queue->sib.last_point, &done)) + if (!hk_queue_query_progress(dev, queue, queue->sib.last_point, cache, &done)) return true; return done < queue->sib.last_point; } @@ -1762,14 +1789,15 @@ hk_sibling_in_flight(struct hk_device *dev, struct hk_queue *queue) * the primary kernel queue; sibling routing stays off until it completes so * no second kernel queue can run beside a packed atomic chain. */ static bool -hk_sibling_packed_atomic_busy(struct hk_device *dev, struct hk_queue *queue) +hk_sibling_packed_atomic_busy(struct hk_device *dev, struct hk_queue *queue, + struct hk_queue_done_cache *cache) { if (!queue->sib.packed_atomic_point) return false; uint64_t done = 0; if (!hk_queue_query_progress(dev, queue, queue->sib.packed_atomic_point, - &done)) + cache, &done)) return true; if (done >= queue->sib.packed_atomic_point) { queue->sib.packed_atomic_point = 0; @@ -1881,8 +1909,13 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, struct vk_queue_submit *submit, const struct vk_queue_submit *original, struct agx_bo *const *extra_bos, - unsigned extra_bo_count, bool *untouched) + unsigned extra_bo_count, + struct hk_queue_done_cache *done_cache, + bool *untouched) { + struct hk_queue_done_cache local_cache = {0}; + if (!done_cache) + done_cache = &local_cache; *untouched = false; bool flush_sstt = false; const bool bound = submit->buffer_bind_count || submit->image_bind_count || @@ -1985,8 +2018,8 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, check_point = MAX2(check_point, queue->sib.last_point); if (check_point > 0) { uint64_t done = 0; - const bool ok = - hk_queue_query_progress(dev, queue, check_point, &done); + const bool ok = hk_queue_query_progress(dev, queue, check_point, + done_cache, &done); prior_busy = !ok || done < check_point; if (ok) { if (queue->drm.last_sq_point[other_sq] > 0 && @@ -2056,8 +2089,10 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, struct hk_cmd_pending *pending = NULL; if (extra_bo_count && !dev->dev.is_virtio) { unsigned chunks = 1 + (command_count - 1) / max_commands_per_submit(dev); - VkResult pin_result = - hk_cmd_pending_begin(dev, chunks, extra_bos, extra_bo_count, &pending); + VkResult pin_result = hk_cmd_pending_begin( + dev, chunks, extra_bos, extra_bo_count, queue, &done_cache->valid, + &done_cache->point, done_cache->reaped, &pending); + done_cache->reaped = true; if (pin_result != VK_SUCCESS) { *untouched = !bound; return vk_error(queue, pin_result); @@ -2127,7 +2162,7 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, uint64_t done = 0; const bool queried = hk_queue_query_progress(dev, queue, queue->drm.empty_wait_point, - &done); + done_cache, &done); if (hk_drm_empty_wait_pending(queue->drm.empty_wait_point, queried, done)) { hk_drm_wait_own_timeline(syncs, &wait_count, 0, &own_wait, @@ -2267,10 +2302,8 @@ queue_submit_recordings(struct hk_device *dev, struct hk_queue *queue, submit->command_buffer_count) || hk_sync_submit_has_unretired_priors(queue)) { uint64_t point = 0; - if (drmSyncobjQuery(dev->dev.fd, &queue->drm.syncobj, &point, 1) == 0) { - hk_queue_note_signalled(queue, point); + if (hk_queue_query_done(dev, queue, done_cache, &point)) completed = MAX2(completed, point); - } } /* Every command's barrier fields come from recorded dependencies. Legacy @@ -2324,12 +2357,12 @@ queue_submit_recordings(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 && !prepend_cdm && !unlikely(anatomy) && - !extra_bo_count && !hk_sibling_packed_atomic_busy(dev, queue) && + !extra_bo_count && !hk_sibling_packed_atomic_busy(dev, queue, done_cache) && !(host_flush && hk_host_flush_names_earlier(flush_barrier, flush_prior, prior_base)) && hk_sibling_eligible(dev, queue, submit, resolved, prior_base, command_count, kernel_cumulative) && - hk_sibling_deep(dev, queue, first_chunk_point - 1)) { + hk_sibling_deep(dev, queue, first_chunk_point - 1, done_cache)) { sib_target = queue->sib.next; queue->sib.next = (queue->sib.next + 1) % queue->sib.count; if (sib_target && queue->sib.id[sib_target] == UINT32_MAX) { @@ -3096,8 +3129,9 @@ DEBUG_GET_ONCE_OPTION(hk_pack_atomics_opt, "HK_PACK_ATOMICS", NULL) DEBUG_GET_ONCE_BOOL_OPTION(hk_pack_gemm_dw_opt, "HK_PACK_GEMM_DW", true) DEBUG_GET_ONCE_OPTION(m5_pack_atomics_opt, "ASAHI_M5_PACK_ATOMICS", NULL) -bool -hk_pack_atomics_allowed(struct hk_device *dev, struct hk_queue *queue) +static bool +hk_pack_atomics_allowed_cached(struct hk_device *dev, struct hk_queue *queue, + struct hk_queue_done_cache *done_cache) { const char *opt = debug_get_option_hk_pack_atomics_opt(); if (!opt) @@ -3115,7 +3149,7 @@ hk_pack_atomics_allowed(struct hk_device *dev, struct hk_queue *queue) const bool default_on = dev->dev.chip == AGX_CHIP_G17P; if (!hk_pack_atomics_choice(default_on, opt, 1)) return false; - if (queue->kick.watched || hk_sibling_in_flight(dev, queue)) + if (queue->kick.watched || hk_sibling_in_flight(dev, queue, done_cache)) return false; vk_foreach_queue(iter, &dev->vk) { @@ -3128,6 +3162,12 @@ hk_pack_atomics_allowed(struct hk_device *dev, struct hk_queue *queue) return true; } +bool +hk_pack_atomics_allowed(struct hk_device *dev, struct hk_queue *queue) +{ + return hk_pack_atomics_allowed_cached(dev, queue, NULL); +} + /* A submission-owned replacement for a run of command buffers. The * existing resolver assigns ordinals to it, so later submissions name its * actual kernel command. */ @@ -5645,7 +5685,7 @@ compute_pair_emitted_footprint(const struct vk_queue_submit *submit, static bool hk_pack_runs(struct hk_device *dev, struct hk_queue *queue, const struct vk_queue_submit *submit, bool submit_is_original, - struct hk_packed_runs *out) + struct hk_packed_runs *out, struct hk_queue_done_cache *done_cache) { memset(out, 0, sizeof(*out)); const unsigned n = submit->command_buffer_count; @@ -5662,7 +5702,7 @@ hk_pack_runs(struct hk_device *dev, struct hk_queue *queue, const bool visible_completion = hk_pack_boundary(dev) == HK_PACK_BOUNDARY_VISIBLE_COMPLETION; - const bool atomics = hk_pack_atomics_allowed(dev, queue); + const bool atomics = hk_pack_atomics_allowed_cached(dev, queue, done_cache); const struct hk_pack_limits limits = { .max_attachments = hk_pack_attachment_limit(dev), .overhead = @@ -6733,8 +6773,13 @@ hk_pack_runs(struct hk_device *dev, struct hk_queue *queue, /* Reap already-retired submissions before allocating packet BOs so the * previous iteration's packed/Ioto/root BOs can hit the BO cache directly. */ - if (!dev->dev.is_virtio) - hk_cmd_pending_reap(dev); + if (!dev->dev.is_virtio) { + hk_cmd_pending_reap_cached(dev, queue, + done_cache ? &done_cache->valid : NULL, + done_cache ? &done_cache->point : NULL); + if (done_cache) + done_cache->reaped = true; + } const bool cached = hk_g17p_cached_cmd_memory(&dev->dev); out->bo = agx_bo_create(&dev->dev, total, 0, @@ -8394,7 +8439,9 @@ queue_submit_segment(struct hk_device *dev, struct hk_queue *queue, submit->image_bind_count || submit->image_opaque_bind_count || !hk_pack_runs_enabled(dev)) return queue_submit_recordings(dev, queue, submit, original, NULL, 0, - untouched); + NULL, untouched); + + struct hk_queue_done_cache done_cache = {0}; /* Packing copies the command buffers' streams: no patch site may be * written meanwhile (host_alias.patch_lock). The copies are the only CPU @@ -8404,14 +8451,14 @@ queue_submit_segment(struct hk_device *dev, struct hk_queue *queue, u_rwlock_rdlock(&dev->host_alias.patch_lock); struct hk_packed_runs runs; const bool have_runs = - hk_pack_runs(dev, queue, submit, submit == original, &runs); + hk_pack_runs(dev, queue, submit, submit == original, &runs, &done_cache); if (unlikely(alias_dynamic)) u_rwlock_rdunlock(&dev->host_alias.patch_lock); const uint64_t prev_pt = queue->drm.timeline_value; if (!have_runs) { VkResult result = queue_submit_recordings(dev, queue, submit, original, - NULL, 0, untouched); + NULL, 0, &done_cache, untouched); if (result == VK_SUCCESS && runs.loop_count >= 2 && runs.cb_hash != 0 && queue->drm.timeline_value > prev_pt) hk_pack_obs_arm(queue, runs.cb_hash, submit->command_buffer_count, @@ -8426,7 +8473,7 @@ queue_submit_segment(struct hk_device *dev, struct hk_queue *queue, util_dynarray_begin(runs.extra_bos), util_dynarray_num_elements( runs.extra_bos, struct agx_bo *), - untouched); + &done_cache, untouched); if (result == VK_SUCCESS && runs.loop_count >= 2 && runs.cb_hash != 0 && queue->drm.timeline_value > prev_pt) hk_pack_obs_arm(queue, runs.cb_hash, submit->command_buffer_count, diff --git a/src/asahi/vulkan/tests/test-hk-cmd-pending.c b/src/asahi/vulkan/tests/test-hk-cmd-pending.c index 61a0d402d7a3..d9d77e796036 100644 --- a/src/asahi/vulkan/tests/test-hk-cmd-pending.c +++ b/src/asahi/vulkan/tests/test-hk-cmd-pending.c @@ -198,12 +198,140 @@ test_lost_quarantine(enum agx_chip chip, bool vm_status_loss) simple_mtx_destroy(&dev.dev.bo_map_lock); } +static void +test_vm_status_mirror_fast_reap(void) +{ + /* Case 1: clean vm_status_mirror + reached signalled_value reaps a + * single-chunk pending submit with zero DRM ioctls (fd == -1). */ + { + struct hk_device dev; + struct hk_queue queue; + init_test_device(&dev, AGX_CHIP_G17S); + memset(&queue, 0, sizeof(queue)); + list_inithead(&queue.cmd_pending); + + int32_t vm_ok = 0; + dev.vm_status_mirror.word = &vm_ok; + dev.cmd_syncobjs[0] = 0xdead; + dev.cmd_syncobj_count = 1; + + struct agx_bo extra = { .refcnt = 2 }; + struct agx_bo *extras[1] = { &extra }; + struct hk_cmd_pending *p = NULL; + assert(hk_cmd_pending_begin(&dev, 1, extras, 1, &queue, NULL, NULL, + false, &p) == VK_SUCCESS); + assert(p != NULL); + assert(hk_cmd_pending_signal(p) == 0xdead); + queue.drm.timeline_value = 5; + hk_cmd_pending_end(&dev, p, &queue, VK_SUCCESS); + assert(p_atomic_read(&dev.cmd_pending_active) == 1); + assert(p_atomic_read(&extra.refcnt) == 3); + + hk_queue_note_signalled(&queue, 5); + hk_cmd_pending_reap(&dev); + + assert(list_is_empty(&queue.cmd_pending)); + assert(p_atomic_read(&dev.cmd_pending_active) == 0); + assert(list_is_empty(&dev.cmd_pending_failed)); + assert(dev.cmd_syncobj_count == 1 && dev.cmd_syncobjs[0] == 0xdead); + assert(p_atomic_read(&extra.refcnt) == 2); + assert(!(extra.flags & AGX_BO_NO_CACHE)); + + dev.cmd_syncobj_count = 0; + hk_cmd_pending_finish(&dev); + simple_mtx_destroy(&dev.cmd_pending_lock); + simple_mtx_destroy(&dev.dev.bo_map_lock); + } + + /* Case 2: cached timeline point reaps without touching drmSyncobjQuery. */ + { + struct hk_device dev; + struct hk_queue queue; + init_test_device(&dev, AGX_CHIP_G17S); + memset(&queue, 0, sizeof(queue)); + list_inithead(&queue.cmd_pending); + + int32_t vm_ok = 0; + dev.vm_status_mirror.word = &vm_ok; + dev.cmd_syncobjs[0] = 0xbeef; + dev.cmd_syncobj_count = 1; + + struct agx_bo extra = { .refcnt = 2 }; + struct agx_bo *extras[1] = { &extra }; + struct hk_cmd_pending *p = NULL; + assert(hk_cmd_pending_begin(&dev, 1, extras, 1, &queue, NULL, NULL, + false, &p) == VK_SUCCESS); + assert(hk_cmd_pending_signal(p) == 0xbeef); + queue.drm.timeline_value = 7; + hk_cmd_pending_end(&dev, p, &queue, VK_SUCCESS); + + bool cached_valid = true; + uint64_t cached_done = 7; + hk_cmd_pending_reap_cached(&dev, &queue, &cached_valid, &cached_done); + + assert(list_is_empty(&queue.cmd_pending)); + assert(p_atomic_read(&dev.cmd_pending_active) == 0); + assert(dev.cmd_syncobj_count == 1 && dev.cmd_syncobjs[0] == 0xbeef); + assert(p_atomic_read(&extra.refcnt) == 2); + + dev.cmd_syncobj_count = 0; + hk_cmd_pending_finish(&dev); + simple_mtx_destroy(&dev.cmd_pending_lock); + simple_mtx_destroy(&dev.dev.bo_map_lock); + } + + /* Case 3: non-zero vm_status_mirror, lost device, or multi-chunk submit + * must all fall back to per-syncobj polling (never fast-reap on fd == -1). */ + for (unsigned mode = 0; mode < 3; ++mode) { + struct hk_device dev; + struct hk_queue queue; + init_test_device(&dev, AGX_CHIP_G17S); + memset(&queue, 0, sizeof(queue)); + list_inithead(&queue.cmd_pending); + + int32_t vm_word = (mode == 0) ? -EIO : 0; + dev.vm_status_mirror.word = &vm_word; + if (mode == 1) + p_atomic_set(&dev.vk._lost.lost, 1); + + const unsigned chunks = (mode == 2) ? 2 : 1; + for (unsigned i = 0; i < chunks; ++i) + dev.cmd_syncobjs[i] = 0x100 + i; + dev.cmd_syncobj_count = chunks; + + struct agx_bo extra = { .refcnt = 2 }; + struct agx_bo *extras[1] = { &extra }; + struct hk_cmd_pending *p = NULL; + assert(hk_cmd_pending_begin(&dev, chunks, extras, 1, &queue, NULL, NULL, + false, &p) == VK_SUCCESS); + for (unsigned i = 0; i < chunks; ++i) + (void)hk_cmd_pending_signal(p); + queue.drm.timeline_value = 9; + hk_cmd_pending_end(&dev, p, &queue, VK_SUCCESS); + + hk_queue_note_signalled(&queue, 9); + hk_cmd_pending_reap(&dev); + + assert(!list_is_empty(&queue.cmd_pending)); + assert(p_atomic_read(&dev.cmd_pending_active) == 1); + assert(p_atomic_read(&extra.refcnt) == 3); + + hk_cmd_pending_finish_queue(&dev, &queue); + hk_cmd_pending_finish(&dev); + assert(p_atomic_read(&extra.refcnt) == 2); + assert(extra.flags & AGX_BO_NO_CACHE); + simple_mtx_destroy(&dev.cmd_pending_lock); + simple_mtx_destroy(&dev.dev.bo_map_lock); + } +} + int main(void) { test_healthy_reuse(); test_lost_quarantine(AGX_CHIP_G17S, false); test_lost_quarantine(AGX_CHIP_G16G, true); + test_vm_status_mirror_fast_reap(); puts("hk_cmd_pending: ok"); return 0; }