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
109 changes: 101 additions & 8 deletions drivers/gpu/drm/apple/afk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1755,7 +1755,14 @@ static int afk_send_compact_command(struct apple_epic_service *service, u8 type,
static int afk_send_epic_token(struct apple_dcp_afkep *ep, u32 channel, u16 tag,
enum epic_type etype, enum epic_category ecat, u8 stype,
const void *payload, size_t payload_len, u64 token,
bool *published);
u64 *published);

/* The TX write pointer has moved from @old to @new, which is another place in the ring. */
static void afk_tx_published_locked(struct apple_dcp_afkep *ep, u32 old, u32 new)
{
lockdep_assert_held(&ep->lock);
ep->tx_published += new > old ? new - old : ep->txbfr.bufsz - old + new;
}

/* Caller has closed host publication and settled ordinary commands first. */
int afk_v27_sleep_check(struct apple_dcp_afkep *ep)
Expand Down Expand Up @@ -1882,6 +1889,7 @@ int afk_v27_sleep_control(struct apple_dcp_afkep *ep, bool open)
ep->sleep_waiting = true;
dma_wmb();
WRITE_ONCE(*ep->txbfr.wptr, cpu_to_le32(next));
afk_tx_published_locked(ep, old, next);
dma_wmb();
/* Return the transport's send error; a published entry still poisons it. */
ret = apple_rtkit_send_message(ep->dcp->rtk, ep->endpoint,
Expand Down Expand Up @@ -3271,9 +3279,9 @@ int afk_receive_message(struct apple_dcp_afkep *ep, u64 message)
static int afk_send_epic_token(struct apple_dcp_afkep *ep, u32 channel, u16 tag,
enum epic_type etype, enum epic_category ecat, u8 stype,
const void *payload, size_t payload_len, u64 token,
bool *published)
u64 *published)
{
u32 rptr, wptr;
u32 rptr, wptr, old;
struct afk_qe *hdr, *hdr2;
struct epic_hdr *ehdr;
struct epic_sub_hdr *eshdr;
Expand All @@ -3286,7 +3294,7 @@ static int afk_send_epic_token(struct apple_dcp_afkep *ep, u32 channel, u16 tag,
bool panel_resume;

if (published)
*published = false;
*published = 0;

/* External H17P services use AFKV2; the internal profile has no EPIC sender. */
if (ep->dcp->fw_compat == DCP_FIRMWARE_H17P && !ep->wire_v2) {
Expand Down Expand Up @@ -3367,6 +3375,7 @@ static int afk_send_epic_token(struct apple_dcp_afkep *ep, u32 channel, u16 tag,
dma_rmb();
rptr = le32_to_cpu(*ep->txbfr.rptr);
wptr = le32_to_cpu(*ep->txbfr.wptr);
old = wptr;
if (rptr >= ep->txbfr.bufsz || wptr >= ep->txbfr.bufsz ||
!IS_ALIGNED(rptr, ep->txbfr.stride) ||
!IS_ALIGNED(wptr, ep->txbfr.stride)) {
Expand Down Expand Up @@ -3536,8 +3545,9 @@ static int afk_send_epic_token(struct apple_dcp_afkep *ep, u32 channel, u16 tag,
if (!ret) {
dma_wmb();
*ep->txbfr.wptr = cpu_to_le32(wptr);
afk_tx_published_locked(ep, old, wptr);
if (published)
*published = true;
*published = ep->tx_published;
if (!afk_send_may_sleep(ep))
ret = afk_send_raw_locked(ep, FIELD_PREP(RBEP_TYPE, RBEP_SEND) |
FIELD_PREP(SEND_WPTR, wptr));
Expand Down Expand Up @@ -3565,7 +3575,7 @@ static int afk_send_epic_token(struct apple_dcp_afkep *ep, u32 channel, u16 tag,
static int afk_send_epic_published(struct apple_dcp_afkep *ep, u32 channel, u16 tag,
enum epic_type etype, enum epic_category ecat, u8 stype,
const void *payload, size_t payload_len,
bool *published)
u64 *published)
{
return afk_send_epic_token(ep, channel, tag, etype, ecat, stype,
payload, payload_len, 0, published);
Expand Down Expand Up @@ -3614,6 +3624,85 @@ static u16 afk_command_tag(struct apple_epic_service *service, u8 slot)
return tag;
}

/**
* afk_service_call_ring_unread - ring again for a call the firmware has not read
* @service: the service the call is for
* @group: the call's group
* @command: the call's command
* @data: the call's input
* @data_len: the length of @data
*
* When the command queued last on @service is this call, with the same input,
* it has gone unanswered and the firmware has not read its entry in the TX ring,
* only ring the doorbell again and return true: the call itself waits. A copy would
* queue behind it to the same effect, and keep a command slot of its own until
* answered, so a caller that repeats a call to firmware that has stopped reading
* takes every slot, and the service then refuses all of its calls with -ENOSPC.
*
* A call the sender would refuse gets its error from there. Compact EPIC keeps
* its timed-out commands apart, and is not covered.
*/
bool afk_service_call_ring_unread(struct apple_epic_service *service, u16 group,
u32 command, const void *data, size_t data_len)
{
const struct epic_service_call *call;
const struct epic_cmd_info *cmd;
struct apple_dcp_afkep *ep;
u32 rptr, wptr = 0, unread;
unsigned long flags;
bool waits;
u64 end;

if (!service || !service->ep || READ_ONCE(service->ep->compact))
return false;
ep = service->ep;

spin_lock_irqsave(&service->lock, flags);
cmd = &service->cmds[service->last_slot];
call = cmd->txbuf;
/* A set slot bit keeps the buffers: the reply clears it before freeing them. */
waits = service->enabled && !service->torndown &&
test_bit(service->last_slot, service->cmd_map) &&
!cmd->done && !cmd->completion &&
cmd->type == EPIC_SUBTYPE_STD_SERVICE &&
cmd->txlen >= sizeof(*call) + data_len &&
le32_to_cpu(call->magic) == EPIC_SERVICE_CALL_MAGIC &&
le16_to_cpu(call->group) == group &&
le32_to_cpu(call->command) == command &&
!memcmp(call + 1, data, data_len);
/* Its sender writes this without the lock until it gives its slot up. */
end = waits ? cmd->tx_end : 0;
spin_unlock_irqrestore(&service->lock, flags);
if (!waits)
return false;

mutex_lock(&ep->tx_lock);
spin_lock_irqsave(&ep->lock, flags);
waits = !READ_ONCE(ep->dcp->quiescing) && !afk_tx_admit_locked(ep) &&
READ_ONCE(ep->dcp->sleep_v27_closed) == DCP_V27_TX_LIVE &&
ep->txbfr.ready;
if (waits) {
dma_rmb();
rptr = le32_to_cpu(READ_ONCE(*ep->txbfr.rptr));
wptr = le32_to_cpu(READ_ONCE(*ep->txbfr.wptr));
unread = wptr >= rptr ? wptr - rptr : ep->txbfr.bufsz - rptr + wptr;
/* Fewer bytes follow its entry than the firmware has not read: it's unread. */
waits = ep->tx_published - end < unread;
}
if (waits)
ep->tx_active++;
spin_unlock_irqrestore(&ep->lock, flags);
if (waits) {
afk_send(ep, FIELD_PREP(RBEP_TYPE, RBEP_SEND) |
FIELD_PREP(SEND_WPTR, wptr));
spin_lock_irqsave(&ep->lock, flags);
ep->tx_active--;
spin_unlock_irqrestore(&ep->lock, flags);
}
mutex_unlock(&ep->tx_lock);
return waits;
}

static int afk_send_command_common(struct apple_epic_service *service, u8 type,
const void *payload, size_t payload_len,
void *output, size_t output_len, u32 *retcode,
Expand Down Expand Up @@ -3664,7 +3753,7 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type,
void *rxbuf, *txbuf;
dma_addr_t rxbuf_dma, txbuf_dma;
unsigned long flags;
bool published = false;
u64 published = 0;
int ret, idx;
u16 tag;
struct apple_dcp_afkep *ep;
Expand Down Expand Up @@ -3813,7 +3902,9 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type,
service->cmds[idx].txlen = payload_len;
service->cmds[idx].free_on_ack = false;
service->cmds[idx].done = false;
service->cmds[idx].tx_end = 0;
service->cmds[idx].completion = &completion;
service->last_slot = idx;
init_completion(&completion);
spin_unlock_irqrestore(&service->lock, flags);

Expand All @@ -3836,6 +3927,8 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type,
}
kfree(inline_command);
inline_command = NULL;
/* Read only once this call has given its slot up, under the lock. */
service->cmds[idx].tx_end = published;
if (ret) {
if (!published)
goto err_free_cmd;
Expand Down Expand Up @@ -3877,7 +3970,7 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type,
"AFK[ep:%02x]: %s (chan:%u) command type 0x%x tag 0x%04x timed out after %u ms\n",
ep->endpoint, service->ops->name, service->channel,
type, tag, timeout_ms);
if (ep->dcp->external && ep->txbfr.ready && ep->rxbfr.ready) {
if (ep->txbfr.ready && ep->rxbfr.ready) {
/* Diagnostic snapshot of shared RAM, not coprocessor MMIO.
* Unequal TX pointers mean firmware has not drained the queue.
*/
Expand Down
12 changes: 12 additions & 0 deletions drivers/gpu/drm/apple/afk.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ struct epic_cmd_info {
size_t reply_len;
bool reply_len_valid;

/* Where its TX ring entry ends, as tx_published; 0 before it is published. */
u64 tx_end;

u32 retcode;
bool done;
bool free_on_ack;
Expand All @@ -57,6 +60,11 @@ struct apple_epic_service {
DECLARE_BITMAP(cmd_map, MAX_PENDING_CMDS);
/* Legacy EPIC sequence; AFKV2 history belongs to the interface. */
u8 cmd_tag;
/*
* Slot of the command queued last (lock). It names none once that slot
* is free, so none of an earlier instance either: reuse needs all free.
*/
u8 last_slot;
spinlock_t lock;
/* Pins held by owners and synchronous callers; protected by lock. */
unsigned int users;
Expand Down Expand Up @@ -596,6 +604,8 @@ struct apple_dcp_afkep {
/* Serializes TX ring writes and mailbox doorbells outside ep->lock. */
struct mutex tx_lock;
u16 qe_seq;
/* lock: bytes the host has advanced the TX write pointer by, ever. */
u64 tx_published;
bool stopping; /* lock: no new receive work after shutdown. */
bool tx_paused; /* lock: no new host commands while stopping/stopped. */
bool rx_paused; /* lock: defer incoming RX words while stopping/stopped. */
Expand Down Expand Up @@ -742,6 +752,8 @@ int afk_service_call_timeout_len(struct apple_epic_service *service, u16 group,
size_t data_pad, void *output, size_t output_len,
size_t output_pad, unsigned int timeout_ms,
size_t *received);
bool afk_service_call_ring_unread(struct apple_epic_service *service, u16 group,
u32 command, const void *data, size_t data_len);

/* Strict variants return only firmware-reported bytes; legacy callers unchanged. */
int afk_send_command_with_reply_len(struct apple_epic_service *service, u8 type,
Expand Down
23 changes: 22 additions & 1 deletion drivers/gpu/drm/apple/dptxep.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ struct dcpdptx_connection_cmd {
__le32 target;
} __attribute__((packed));

/* The port's HPD level: group and command of the call, and its input. */
#define DCPDPTX_HOTPLUG_GROUP 8
#define DCPDPTX_HOTPLUG_CMD 8

struct dcpdptx_hotplug_cmd {
u8 _pad0[16];
__le32 unk;
Expand Down Expand Up @@ -254,7 +258,24 @@ int dptxport_set_hpd_timeout(struct apple_epic_service *service, bool hpd,
if (hpd)
cmd.unk = cpu_to_le32(1);

ret = afk_service_call_timeout_len(service, 8, 8, &cmd, sizeof(cmd), 12,
/*
* The same HPD level went unanswered and still waits in the firmware's
* queue, with nothing of the port behind it. A copy would change nothing,
* and keep a command slot until answered. A port whose Type-C SAFE state
* is retried (dcp_typec_route_prepare()) repeats its HPD low, and would
* take every slot: the port then refuses all its calls with -ENOSPC, the
* HPD of the next display among them.
*/
if (afk_service_call_ring_unread(service, DCPDPTX_HOTPLUG_GROUP,
DCPDPTX_HOTPLUG_CMD, &cmd, sizeof(cmd))) {
dev_warn_ratelimited(service->ep->dcp->dev,
"set_hpd(%d): the same request is still unread, only rang again\n",
hpd);
return -ETIMEDOUT;
}

ret = afk_service_call_timeout_len(service, DCPDPTX_HOTPLUG_GROUP,
DCPDPTX_HOTPLUG_CMD, &cmd, sizeof(cmd), 12,
&resp, sizeof(resp), 12, timeout_ms,
&received);
if (ret)
Expand Down
32 changes: 29 additions & 3 deletions tools/testing/selftests/drivers/gpu/apple-afkv2/extract-hpd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# SPDX-License-Identifier: GPL-2.0-only OR MIT
"""Exercise the production DPTX HPD request with mocked service-call replies.

Covers the legacy, AFK-v2 and compact EPIC replies and the 14.7 AV client close.
Covers the legacy, AFK-v2 and compact EPIC replies, the 14.7 AV client close,
and a request that is not sent again while the same one still waits unread.
"""
import pathlib
import re
Expand All @@ -27,6 +28,7 @@ def function(source, name):
dptx, re.M | re.S)
if not command:
raise RuntimeError("production HPD request layout not found")
call_ids = re.findall(r"^#define DCPDPTX_HOTPLUG_\w+\s.*$", dptx, re.M)
prefix = r'''
#undef NDEBUG
#include <assert.h>
Expand All @@ -48,6 +50,7 @@ def function(source, name):
#define EPIC_SERVICE_CALL_MAGIC 0x69706378
#define EPIC_SUBTYPE_STD_SERVICE 0xc0
#define dev_warn(...) do {} while (0)
#define dev_warn_ratelimited(...) do {} while (0)
#define dev_dbg(...) do {} while (0)
#define READ_ONCE(x) (x)
static u32 get_unaligned_le32(const void *p)
Expand Down Expand Up @@ -95,6 +98,17 @@ def function(source, name):
assert(dcp == expected_dcp && service != NULL);
cec_closes++;
}
/* The HPD request queued last on the port still waits unread. */
static bool unread;
static unsigned int sends, unread_asked;
bool afk_service_call_ring_unread(struct apple_epic_service *service, u16 group,
u32 command, const void *data, size_t data_len)
{
(void)service; (void)data;
assert(group == 8 && command == 8 && data_len == 20);
unread_asked++;
return unread;
}
static int afk_send_command_timeout_len(struct apple_epic_service *s, u8 type,
const void *payload, size_t payload_len, void *output, size_t output_len,
u32 *retcode, unsigned int timeout_ms, size_t *received)
Expand All @@ -109,6 +123,7 @@ def function(source, name):
assert(call->group == 8 && call->command == 8);
closes_at_send = v14_closes;
cec_closes_at_send = cec_closes;
sends++;
if (wire_v2 && !received && arrived < output_len - sizeof(*call))
return -EMSGSIZE;
memcpy(&echo, body + 16, sizeof(echo));
Expand Down Expand Up @@ -206,14 +221,25 @@ def function(source, name):
}
ep.compact = false; compact_status = 0;
}
/* A copy of an unread request is not sent, and fails as the request did. */
for (int hpd = 0; hpd < 2; hpd++) {
unsigned int before = sends, asked = unread_asked;
int ret;

unread = true;
ret = dptxport_set_hpd_timeout(&service, hpd, 1000);
unread = false;
expect(ret == -ETIMEDOUT && sends == before && unread_asked == asked + 1,
"copy of an unread request not sent", hpd, ret);
}
printf("%d HPD reply checks failed\n", failures);
if (failures)
return 1;
puts("PASS: extracted production DPTX HPD request: short, oversized, wrong-echo and complete replies, legacy, AFK-v2 and compact status, AV client close on HPD low");
puts("PASS: extracted production DPTX HPD request: short, oversized, wrong-echo and complete replies, legacy, AFK-v2 and compact status, AV client close on HPD low, no copy of an unread request");
return 0;
}
'''
unit = (prefix + command.group(0) + "\n" + function(afk, "afk_service_call_timeout_len") + "\n" +
unit = (prefix + "\n".join(call_ids) + "\n" + command.group(0) + "\n" + function(afk, "afk_service_call_timeout_len") + "\n" +
function(afk, "afk_service_call_timeout") + "\n" +
function(dptx, "dptxport_set_hpd_timeout") + suffix)
pathlib.Path(sys.argv[3]).write_text(unit)
Loading
Loading