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
83 changes: 80 additions & 3 deletions src/asahi/vulkan/hk_compute_kick.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ struct hk_kick_class {
/* Timeline semaphore that the nth kick signals to n. */
VkSemaphore done;
uint64_t sent;
bool counted;
};

struct hk_compute_kick {
Expand Down Expand Up @@ -256,6 +257,7 @@ hk_compute_kick_thread(void *data)
struct hk_compute_kick *k = data;
const int fd = k->dev->dev.fd;

unsigned pending_kicks = 0;
u_thread_setname("hk-compute-kick");

mtx_lock(&k->lock);
Expand All @@ -271,17 +273,40 @@ hk_compute_kick_thread(void *data)
int64_t deadline = INT64_MAX;
unsigned count = 0;
unsigned due = 0;
bool packed_atomics_active = false;

for (unsigned i = 0; i < k->watch_count; i++) {
struct hk_queue *queue = k->watch[i];
if (!queue->kick.pending)
if (queue->kick.packed_atomics_inflight)
packed_atomics_active = true;

if (!queue->kick.pending && !queue->kick.packed_atomics_point)
continue;

uint64_t value = 0;
if (drmSyncobjQuery(fd, &queue->drm.syncobj, &value, 1) ||
value >= queue->kick.pending) {
if (drmSyncobjQuery(fd, &queue->drm.syncobj, &value, 1)) {
queue->kick.pending = 0;
queue->kick.due_ns = 0;
queue->kick.packed_atomics_point = 0;
continue;
}

if (queue->kick.packed_atomics_point) {
if (value >= queue->kick.packed_atomics_point) {
queue->kick.packed_atomics_point = 0;
} else {
packed_atomics_active = true;
}
}

if (!queue->kick.pending || value >= queue->kick.pending) {
queue->kick.pending = 0;
queue->kick.due_ns = 0;
if (queue->kick.packed_atomics_point) {
k->handles[count] = queue->drm.syncobj;
k->points[count] = queue->kick.packed_atomics_point;
count++;
}
continue;
}

Expand All @@ -302,12 +327,25 @@ hk_compute_kick_thread(void *data)
}

if (!count) {
pending_kicks = 0;
k->idle = true;
u_cnd_monotonic_wait(&k->wake, &k->lock);
k->idle = false;
continue;
}

/* Close admission under the same lock as pack_atomics_begin. Existing
* packets can retire, but a later submission cannot extend their point.
*/
u_foreach_bit(class, due) {
if (!k->class[class].counted && k->class[class].queue &&
!k->class[class].failed) {
k->class[class].counted = true;
p_atomic_inc(&k->dev->compute_kick_queues);
}
}
due = hk_compute_kick_take_due(&pending_kicks, due, packed_atomics_active);

mtx_unlock(&k->lock);

u_foreach_bit(class, due)
Expand Down Expand Up @@ -360,6 +398,41 @@ hk_compute_kick_note(struct hk_device *dev, struct hk_queue *queue)
mtx_unlock(&k->lock);
}

bool
hk_compute_kick_pack_atomics_begin(struct hk_device *dev,
struct hk_queue *queue)
{
struct hk_compute_kick *k = dev->compute_kick;
if (!k || !queue->kick.watched)
return false;

mtx_lock(&k->lock);
if (k->stop || p_atomic_read(&dev->compute_kick_queues) != 0) {
mtx_unlock(&k->lock);
return false;
}
queue->kick.packed_atomics_inflight++;
mtx_unlock(&k->lock);
return true;
}

void
hk_compute_kick_pack_atomics_end(struct hk_device *dev,
struct hk_queue *queue,
uint64_t timeline_point)
{
struct hk_compute_kick *k = dev->compute_kick;
if (!k || !queue->kick.watched)
return;

mtx_lock(&k->lock);
assert(queue->kick.packed_atomics_inflight > 0);
queue->kick.packed_atomics_inflight--;
if (timeline_point > queue->kick.packed_atomics_point)
queue->kick.packed_atomics_point = timeline_point;
mtx_unlock(&k->lock);
}

static void
hk_compute_kick_free_mem(struct hk_device *dev, struct hk_compute_kick *k)
{
Expand Down Expand Up @@ -441,7 +514,11 @@ hk_compute_kick_init(struct hk_device *dev, unsigned period_ms)
queue->kick.watched = c->queue != NULL;
}

/* Leave compute_kick_queues at 0 until a class actually sends its first
* kick: dormant kick queues allocate no kernel queue and must not block
* single-queue global-atomic CDM packing. */
dev->compute_kick = k;
dev->compute_kick_queues = 0;
}

void
Expand Down
27 changes: 27 additions & 0 deletions src/asahi/vulkan/hk_compute_kick.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@

#pragma once

#include <stdbool.h>
#include <stdint.h>

/* Remember each due class until already accepted packed atomics retire. */
static inline unsigned
hk_compute_kick_take_due(unsigned *pending, unsigned due, bool packed_active)
{
*pending |= due;
if (packed_active)
return 0;

unsigned ready = *pending;
*pending = 0;
return ready;
}

struct hk_device;
struct hk_queue;

Expand All @@ -24,3 +40,14 @@ void hk_compute_kick_finish(struct hk_device *dev);
/* Called from the queue's submission callback after compute work was
* submitted to a watched queue, with its timeline value already advanced. */
void hk_compute_kick_note(struct hk_device *dev, struct hk_queue *queue);

/* Guard a single-queue packed global-atomic submission against concurrent
* kick-queue activation. Returns true when the queue is watched, no kick
* queue has been activated, and the caller has registered an in-flight
* packed-atomics submission (which must be paired with
* hk_compute_kick_pack_atomics_end()). */
bool hk_compute_kick_pack_atomics_begin(struct hk_device *dev,
struct hk_queue *queue);
void hk_compute_kick_pack_atomics_end(struct hk_device *dev,
struct hk_queue *queue,
uint64_t timeline_point);
4 changes: 3 additions & 1 deletion src/asahi/vulkan/hk_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,10 @@ struct hk_device {
* HK_FRAME_ANATOMY environment variable is set. */
struct hk_frame_anatomy *frame_anatomy;
/* Compute kick (hk_compute_kick.h); NULL unless G17C with
* hk_compute_kick_ms set. */
* hk_compute_kick_ms set. compute_kick_queues counts priority classes whose
* kick queue has been activated (0 while dormant). */
struct hk_compute_kick *compute_kick;
uint32_t compute_kick_queues;

struct hk_descriptor_table occlusion_queries;
struct hk_sampler_heap samplers;
Expand Down
Loading