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
99 changes: 99 additions & 0 deletions drivers/gpu/drm/apple/afk-parser-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,104 @@ static void afk_compact_service_call_report_test(struct kunit *test)
KUNIT_EXPECT_EQ(test, cap.pa, 0x3400);
}

/*
* Replies to firmware calls wait on the endpoint's transmit queue. While it
* makes no progress, further calls are taken only as long as their replies
* fit the room of one message.
*/
struct compact_reply_gate {
struct work_struct work;
struct completion open;
};

static void compact_reply_gate_fn(struct work_struct *work)
{
struct compact_reply_gate *gate =
container_of(work, struct compact_reply_gate, work);

wait_for_completion(&gate->open);
}

static int compact_call_count_cb(struct apple_epic_service *service, u16 group,
u32 command, const void *data, size_t size,
void *reply, size_t room)
{
unsigned int *count = service->cookie;

(*count)++;
return 0;
}

static const struct apple_epic_service_ops compact_call_count_ops = {
.name = "compact-call-test",
.call_v2 = compact_call_count_cb,
};

static void afk_compact_call_reply_budget_test(struct kunit *test)
{
/* Four calls of this size take all the room; the timestamp and sub-header precede. */
const size_t payload_size = AFK_COMPACT_MESSAGE_MAX / 4;
const size_t wire_size = 16 + payload_size;
struct apple_dcp_afkep *ep = test->priv;
struct compact_reply_gate gate;
struct epic_compact_sub_hdr *sub;
struct epic_service_call *call;
unsigned int handled = 0, i;
u8 *wire;

wire = kunit_kzalloc(test, wire_size, GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, wire);
sub = (void *)(wire + 8);
sub->category = 1;
sub->type = EPIC_SUBTYPE_STD_SERVICE;
call = (void *)((u8 *)(sub + 1) + sizeof(struct epic_compact_desc));
call->command = cpu_to_le32(4);
call->data_len = cpu_to_le32(payload_size - sizeof(struct epic_compact_desc) -
sizeof(*call));
call->magic = cpu_to_le32(EPIC_SERVICE_CALL_MAGIC);

ep->services[0].ops = &compact_call_count_ops;
ep->services[0].cookie = &handled;
ep->tx_wq = alloc_ordered_workqueue("afk-parser-test-tx", 0);
KUNIT_ASSERT_NOT_NULL(test, ep->tx_wq);

/* The queue is ordered: nothing behind the gate runs until it opens. */
INIT_WORK_ONSTACK(&gate.work, compact_reply_gate_fn);
init_completion(&gate.open);
queue_work(ep->tx_wq, &gate.work);

/* A call left without a reply must hold off a coordinated sleep. */
ep->dcp->v27_sleep_transport = true;
for (i = 0; i < 4; i++)
afk_recv_compact_message(ep, 1, wire, wire_size);
KUNIT_EXPECT_FALSE(test, ep->sleep_poisoned);
afk_recv_compact_message(ep, 1, wire, wire_size);
KUNIT_EXPECT_EQ(test, handled, 4U);
KUNIT_EXPECT_EQ(test, atomic_read(&ep->sleep_jobs), 4);
KUNIT_EXPECT_TRUE(test, ep->sleep_poisoned);

/* A small call no longer fits either. */
call->data_len = cpu_to_le32(0);
afk_recv_compact_message(ep, 1, wire,
16 + sizeof(struct epic_compact_desc) + sizeof(*call));
KUNIT_EXPECT_EQ(test, handled, 4U);
KUNIT_EXPECT_EQ(test, atomic_read(&ep->sleep_jobs), 4);

/* The replies fail without a transmit ring, which frees their room. */
complete(&gate.open);
flush_workqueue(ep->tx_wq);
KUNIT_EXPECT_EQ(test, atomic_read(&ep->sleep_jobs), 0);

afk_recv_compact_message(ep, 1, wire,
16 + sizeof(struct epic_compact_desc) + sizeof(*call));
KUNIT_EXPECT_EQ(test, handled, 5U);

destroy_workqueue(ep->tx_wq);
ep->tx_wq = NULL;
destroy_work_on_stack(&gate.work);
KUNIT_EXPECT_EQ(test, atomic_read(&ep->sleep_jobs), 0);
}

/*
* Slot reuse for re-announced compact services. The firmware re-announces a
* service under a fresh interface on every DP relink without closing the old
Expand Down Expand Up @@ -341,6 +439,7 @@ static void afk_compact_cut_before_wildcard_test(struct kunit *test)
static struct kunit_case afk_parser_cases[] = {
KUNIT_CASE(afk_serialized_report_test),
KUNIT_CASE(afk_compact_service_call_report_test),
KUNIT_CASE(afk_compact_call_reply_budget_test),
KUNIT_CASE(afk_compact_header_test),
KUNIT_CASE(afk_short_epic_test),
KUNIT_CASE(afk_short_reply_test),
Expand Down
56 changes: 51 additions & 5 deletions drivers/gpu/drm/apple/afk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,43 @@ struct afk_compact_reply {
u8 data[];
};

/*
* Replies wait on the transmit queue for ring space that only the firmware
* frees, while the receive worker keeps taking calls, so what is queued
* needs a limit of its own. One message is at most AFK_COMPACT_MESSAGE_MAX
* bytes and the receive side assembles one at a time; the queued replies
* together get the same room. A reply is never larger than its call, so
* any single call fits when nothing else is queued, and the calls the
* firmware makes are a small fraction of that even in a burst.
*/
static bool afk_compact_reply_reserve(struct apple_dcp_afkep *ep, size_t size)
{
guard(spinlock_irqsave)(&ep->lock);
if (size > AFK_COMPACT_MESSAGE_MAX ||
ep->compact_reply_bytes > AFK_COMPACT_MESSAGE_MAX - size)
return false;
ep->compact_reply_bytes += size;
return true;
}

static void afk_compact_reply_release(struct apple_dcp_afkep *ep, size_t size)
{
guard(spinlock_irqsave)(&ep->lock);
ep->compact_reply_bytes -= size;
}

/*
* A call that gets no reply leaves the firmware waiting on it, so a
* coordinated sleep must not run against it afterwards.
*/
static void afk_compact_call_unanswered(struct apple_dcp_afkep *ep)
{
if (!ep->dcp->v27_sleep_transport)
return;
guard(spinlock_irqsave)(&ep->lock);
ep->sleep_poisoned = true;
}

static void afk_compact_reply_worker(struct work_struct *work)
{
struct afk_compact_reply *r = container_of(work, struct afk_compact_reply, work);
Expand All @@ -1127,10 +1164,9 @@ static void afk_compact_reply_worker(struct work_struct *work)
dev_err(r->ep->dcp->dev,
"AFK[ep:%02x]: failed to reply to compact service call: %d\n",
r->ep->endpoint, ret);
if (ret && r->ep->dcp->v27_sleep_transport) {
guard(spinlock_irqsave)(&r->ep->lock);
r->ep->sleep_poisoned = true;
}
if (ret)
afk_compact_call_unanswered(r->ep);
afk_compact_reply_release(r->ep, r->size);
atomic_dec_return_release(&r->ep->sleep_jobs);
kvfree(r);
}
Expand Down Expand Up @@ -1167,9 +1203,19 @@ static void afk_recv_handle_compact_call(struct apple_dcp_afkep *ep,
group = le16_to_cpu(call->group);
command = le32_to_cpu(call->command);

if (!afk_compact_reply_reserve(ep, payload_size)) {
dev_warn_ratelimited(ep->dcp->dev,
"AFK[ep:%02x]: too many replies queued, dropping compact service %u call %u:%u\n",
ep->endpoint, channel, group, command);
afk_compact_call_unanswered(ep);
return;
}
r = kvzalloc(struct_size(r, data, payload_size), GFP_KERNEL);
if (!r)
if (!r) {
afk_compact_reply_release(ep, payload_size);
afk_compact_call_unanswered(ep);
return;
}
INIT_WORK(&r->work, afk_compact_reply_worker);
r->ep = ep;
r->channel = channel;
Expand Down
2 changes: 2 additions & 0 deletions drivers/gpu/drm/apple/afk.h
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,8 @@ struct apple_dcp_afkep {
bool compact_tx_desync;
/* Replies to firmware calls, see afk_compact_reply_worker(). */
struct workqueue_struct *tx_wq;
/* Payload bytes of the replies queued there and not yet sent (lock). */
size_t compact_reply_bytes;
/* Accepted RX and queued replies, until each worker has returned. */
atomic_t sleep_jobs;
struct mutex compact_tx_lock;
Expand Down
Loading