From 0b35d733b33db2eba45c15dcc098a77bb7362665 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Fri, 27 Nov 2026 01:05:25 -0500 Subject: [PATCH 1/8] drm/apple: afk: report a stalled endpoint and its undelivered doorbells On a J316c (M1 Max, external display processor on firmware 13.5) the DPTX port stopped answering after an s2idle: an HPD low timed out, and every later call on the port timed out or failed with -ENOSPC. The log held only the timeouts. What the host saw of the endpoint at that point tells a firmware that stopped reading its queue from one that was never told about the command, but it was printed only for the T6030 external processor. A doorbell the mailbox failed to deliver was not reported at all, except on firmware 14.7 and native26. On the first timeout of a stall (until the service's next reply), report once: - the service's command slots, and how many hold timed-out commands; - the TX ring pointers and how many bytes the firmware has not read yet, and the RX ring pointers; - how many doorbells in a row the mailbox did not deliver, and why; - whether RTKit runs or has crashed. Say when a timed-out command is answered after all, and how long after it was queued: that tells a slow firmware from a stopped one. Say once, until the next reply, that a service has no free command slot left, rather than only the caller's -ENOSPC. dcp_send_message() now returns the mailbox result and warns, rate limited, on every firmware. A lost doorbell is only counted: its entry stays in the ring, and the next doorbell carries a later write pointer that covers it. Nothing here reads the coprocessor: the rings are host memory. Signed-off-by: DjDeveloperr --- drivers/gpu/drm/apple/afk-stall-test.c | 147 ++++++++++++++++++ drivers/gpu/drm/apple/afk.c | 125 ++++++++++++--- drivers/gpu/drm/apple/afk.h | 17 ++ drivers/gpu/drm/apple/dcp.c | 14 +- drivers/gpu/drm/apple/dcp.h | 2 +- .../drivers/gpu/apple-afkv2/extract-send.py | 9 +- .../selftests/drivers/gpu/apple-dcp/edid.py | 7 +- .../drivers/gpu/apple-dcp/recovery.py | 3 + 8 files changed, 296 insertions(+), 28 deletions(-) create mode 100644 drivers/gpu/drm/apple/afk-stall-test.c diff --git a/drivers/gpu/drm/apple/afk-stall-test.c b/drivers/gpu/drm/apple/afk-stall-test.c new file mode 100644 index 0000000000000..14ec8158776e9 --- /dev/null +++ b/drivers/gpu/drm/apple/afk-stall-test.c @@ -0,0 +1,147 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Included by afk.c: a legacy EPIC endpoint whose firmware stops answering, + * through the real send, timeout and reply paths. The rings are host memory + * and the doorbell is a stub, so nothing reads the queued commands. + */ +#include +#include +#include +#include + +#include "dptxep.h" + +#define AFK_STALL_STRIDE 64 +/* The ring size firmware 13.5 gives its DPTX endpoint: 16 KiB less three strides. */ +#define AFK_STALL_RING (SZ_16K - 3 * AFK_STALL_STRIDE) +/* Timeouts of a single jiffy: nothing answers in time anyway. */ +#define AFK_STALL_TIMEOUT_MS 1 + +struct afk_stall_test { + struct apple_dcp_afkep ep; + struct apple_dcp dcp; + unsigned int doorbells; + int doorbell_ret; +}; + +static const struct apple_epic_service_ops afk_stall_ops = { + .name = "dcpdptx-port-epic", +}; + +static int afk_stall_doorbell(struct apple_dcp_afkep *ep, u64 message) +{ + struct afk_stall_test *t = container_of(ep, struct afk_stall_test, ep); + + t->doorbells++; + return t->doorbell_ret; +} + +static int afk_stall_ring(struct kunit *test, struct afk_ringbuffer *bfr) +{ + u8 *hdr = kunit_kzalloc(test, 3 * AFK_STALL_STRIDE + AFK_STALL_RING, GFP_KERNEL); + + if (!hdr) + return -ENOMEM; + bfr->rptr = (__le32 *)(hdr + AFK_STALL_STRIDE); + bfr->wptr = (__le32 *)(hdr + 2 * AFK_STALL_STRIDE); + bfr->buf = hdr + 3 * AFK_STALL_STRIDE; + bfr->bufsz = AFK_STALL_RING; + bfr->stride = AFK_STALL_STRIDE; + bfr->ready = true; + return 0; +} + +static int afk_stall_test_init(struct kunit *test) +{ + struct apple_epic_service *service; + struct afk_stall_test *t; + struct device *dev; + int ret; + + t = kunit_kzalloc(test, sizeof(*t), GFP_KERNEL); + if (!t) + return -ENOMEM; + dev = kunit_device_register(test, "afk-stall"); + if (IS_ERR(dev)) + return PTR_ERR(dev); + /* The command buffers are coherent DMA, as on the display processor. */ + dev->coherent_dma_mask = DMA_BIT_MASK(64); + dev->dma_mask = &dev->coherent_dma_mask; + t->dcp.dev = dev; + t->dcp.fw_compat = DCP_FIRMWARE_V_13_5; + afk_ep_init_state(&t->ep); + t->ep.dcp = &t->dcp; + t->ep.endpoint = 0x2a; + ret = afk_stall_ring(test, &t->ep.txbfr) ?: afk_stall_ring(test, &t->ep.rxbfr); + if (ret) + return ret; + t->ep.num_channels = 1; + service = &t->ep.services[0]; + service->ep = &t->ep; + service->ops = &afk_stall_ops; + service->channel = 1; + service->enabled = true; + test->priv = t; + kunit_activate_static_stub(test, afk_send, afk_stall_doorbell); + return 0; +} + +static unsigned int afk_stall_held(struct apple_epic_service *service) +{ + return bitmap_weight(service->cmd_map, MAX_PENDING_CMDS); +} + +static u32 afk_stall_unread(struct apple_dcp_afkep *ep) +{ + return le32_to_cpu(*ep->txbfr.wptr) - le32_to_cpu(*ep->txbfr.rptr); +} + +/* The firmware answers every command still waiting: the test frees their buffers. */ +static void afk_stall_answer_all(struct afk_stall_test *t) +{ + struct apple_epic_service *service = &t->ep.services[0]; + struct epic_cmd reply = {}; + unsigned int i; + + for_each_set_bit(i, service->cmd_map, MAX_PENDING_CMDS) + afk_recv_handle_reply(&t->ep, service->channel, service->cmds[i].tag, + &reply, sizeof(reply)); +} + +/* + * A doorbell the mailbox did not deliver leaves its entry in the ring for the + * next one: the command keeps its slot until answered, and the loss is noted. + */ +static void afk_stall_lost_doorbell_test(struct kunit *test) +{ + struct afk_stall_test *t = test->priv; + struct apple_epic_service *service = &t->ep.services[0]; + + t->doorbell_ret = -ETIMEDOUT; + KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, true, AFK_STALL_TIMEOUT_MS), + -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, t->ep.doorbells_lost, 1U); + KUNIT_EXPECT_EQ(test, t->ep.doorbell_error, -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, afk_stall_held(service), 1U); + KUNIT_EXPECT_GT(test, afk_stall_unread(&t->ep), 0U); + + t->doorbell_ret = 0; + KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), + -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, t->ep.doorbells_lost, 0U); + KUNIT_EXPECT_EQ(test, afk_stall_held(service), 2U); + afk_stall_answer_all(t); +} + +static struct kunit_case afk_stall_cases[] = { + KUNIT_CASE(afk_stall_lost_doorbell_test), + {} +}; + +static struct kunit_suite afk_stall_suite = { + .name = "apple-afk-stall", + .init = afk_stall_test_init, + .test_cases = afk_stall_cases, +}; + +kunit_test_suite(afk_stall_suite); diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 10e3586487a9d..334de4fffaac4 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -21,6 +21,7 @@ #include #include +#include #include #include "afk.h" @@ -95,9 +96,10 @@ static unsigned int afk_service_timeout_ms(struct apple_epic_service *service) return service ? afk_timeout_ms(service->ep) : MSEC_PER_SEC; } -static void afk_send(struct apple_dcp_afkep *ep, u64 message) +static int afk_send(struct apple_dcp_afkep *ep, u64 message) { - dcp_send_message(ep->dcp, ep->endpoint, message); + KUNIT_STATIC_STUB_REDIRECT(afk_send, ep, message); + return dcp_send_message(ep->dcp, ep->endpoint, message); } static void afk_release_context(void *data) @@ -2091,9 +2093,10 @@ static void afk_recv_handle_reply(struct apple_dcp_afkep *ep, u32 channel, { struct epic_cmd *cmd = payload; struct apple_epic_service *service; - unsigned long flags; + unsigned long flags, waited; u8 idx = tag & (ep->wire_v2 ? 0xf : 0xff); void *rxbuf, *txbuf; + bool late; dma_addr_t rxbuf_dma, txbuf_dma; size_t rxlen, txlen; @@ -2139,6 +2142,11 @@ static void afk_recv_handle_reply(struct apple_dcp_afkep *ep, u32 channel, service->cmds[idx].retcode = le32_to_cpu(cmd->retcode); service->cmds[idx].reply_len = le32_to_cpu(cmd->rxlen); service->cmds[idx].reply_len_valid = true; + /* The firmware answers again: a stall from now on is a new one. */ + service->stall_reported = false; + service->full_reported = false; + late = !service->cmds[idx].completion; + waited = jiffies - service->cmds[idx].queued; if (service->cmds[idx].free_on_ack) { /* defer freeing until we're no longer in atomic context */ rxbuf = service->cmds[idx].rxbuf; @@ -2157,6 +2165,13 @@ static void afk_recv_handle_reply(struct apple_dcp_afkep *ep, u32 channel, spin_unlock_irqrestore(&service->lock, flags); + /* Whether the firmware was only slow, or had stopped: say when it answered. */ + if (late) + dev_info_ratelimited(ep->dcp->dev, + "AFK[ep:%02x]: chan %u tag 0x%04x answered after its timeout, %u ms after it was queued (retcode %#x)\n", + ep->endpoint, channel, tag, jiffies_to_msecs(waited), + le32_to_cpu(cmd->retcode)); + if (rxbuf && rxlen) dma_free_coherent(ep->dcp->dev, rxlen, rxbuf, rxbuf_dma); if (txbuf && txlen) @@ -3257,8 +3272,19 @@ static int afk_send_epic_token(struct apple_dcp_afkep *ep, u32 channel, u16 tag, spin_unlock_irqrestore(&ep->lock, flags); /* Keep tx_lock held across the doorbell so ring writes stay ordered. */ - afk_send(ep, FIELD_PREP(RBEP_TYPE, RBEP_SEND) | - FIELD_PREP(SEND_WPTR, wptr)); + ret = afk_send(ep, FIELD_PREP(RBEP_TYPE, RBEP_SEND) | + FIELD_PREP(SEND_WPTR, wptr)); + /* + * The entry stays in the ring whatever became of its doorbell: a later + * doorbell carries a later write pointer, and the firmware reads up to + * that. A lost one is only noted, for afk_report_stall(). + */ + if (ret) { + WRITE_ONCE(ep->doorbells_lost, ep->doorbells_lost + 1); + WRITE_ONCE(ep->doorbell_error, ret); + } else { + WRITE_ONCE(ep->doorbells_lost, 0); + } mutex_unlock(&ep->tx_lock); return 0; @@ -3311,6 +3337,67 @@ static u16 afk_command_tag(struct apple_epic_service *service, u8 slot) return tag; } +/* + * A command that timed out keeps its slot, and its buffers, for the reply the + * firmware may still send: the firmware writes the reply into the command's + * buffer and names the slot in it, so both stay the command's until then + * (afk_recv_handle_reply()). The number of such commands on @service. + */ +static unsigned int afk_service_unanswered_locked(struct apple_epic_service *service) +{ + unsigned int i, unanswered = 0; + + lockdep_assert_held(&service->lock); + for_each_set_bit(i, service->cmd_map, MAX_PENDING_CMDS) + if (!service->cmds[i].done && !service->cmds[i].completion) + unanswered++; + return unanswered; +} + +/* + * A command of @service, @tag, timed out unanswered. The first one since the + * service's last reply reports what the host sees of the endpoint, once: its + * slots, the rings in shared memory and the doorbells. Nothing here reads the + * coprocessor. + */ +static void afk_report_stall(struct apple_epic_service *service, u16 tag) +{ + struct apple_dcp_afkep *ep = service->ep; + struct apple_rtkit *rtk = ep->dcp->rtk; + unsigned int held, unanswered; + u32 tx_r, tx_w, rx_r, rx_w; + unsigned long flags; + bool first; + + spin_lock_irqsave(&service->lock, flags); + first = !service->stall_reported; + service->stall_reported = true; + held = bitmap_weight(service->cmd_map, MAX_PENDING_CMDS); + unanswered = afk_service_unanswered_locked(service); + spin_unlock_irqrestore(&service->lock, flags); + if (!first || !ep->txbfr.ready || !ep->rxbfr.ready) + return; + + dma_rmb(); + tx_r = le32_to_cpu(READ_ONCE(*ep->txbfr.rptr)); + tx_w = le32_to_cpu(READ_ONCE(*ep->txbfr.wptr)); + rx_r = le32_to_cpu(READ_ONCE(*ep->rxbfr.rptr)); + rx_w = le32_to_cpu(READ_ONCE(*ep->rxbfr.wptr)); + /* + * Unread TX bytes are what the firmware has not taken from the ring; + * equal RX pointers, that the host has read all it was sent. + */ + dev_warn(ep->dcp->dev, + "AFK[ep:%02x]: %s (chan:%u) stalled at tag 0x%04x: %u of %u command slots taken, %u by timed-out commands; TX r/w %#x/%#x (%u of %zu bytes unread), RX r/w %#x/%#x; %u doorbell(s) undelivered (%d); running=%d crashed=%d\n", + ep->endpoint, service->ops->name, service->channel, tag, held, + MAX_PENDING_CMDS, unanswered, tx_r, tx_w, + tx_w >= tx_r ? tx_w - tx_r : (u32)ep->txbfr.bufsz - tx_r + tx_w, + ep->txbfr.bufsz, rx_r, rx_w, + READ_ONCE(ep->doorbells_lost), READ_ONCE(ep->doorbell_error), + !IS_ERR_OR_NULL(rtk) && apple_rtkit_is_running(rtk), + !IS_ERR_OR_NULL(rtk) && apple_rtkit_is_crashed(rtk)); +} + 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, @@ -3481,6 +3568,13 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type, } idx = bitmap_find_free_region(service->cmd_map, MAX_PENDING_CMDS, 0); if (idx < 0) { + /* Once until the next reply: every call is refused until then. */ + if (!service->full_reported) + dev_warn(ep->dcp->dev, + "AFK[ep:%02x]: %s (chan:%u) has no free command slot: %u of %u held by timed-out commands still unanswered\n", + ep->endpoint, service->ops->name, service->channel, + afk_service_unanswered_locked(service), MAX_PENDING_CMDS); + service->full_reported = true; ret = -ENOSPC; goto err_unlock; } @@ -3501,6 +3595,7 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type, service->cmds[idx].free_on_ack = false; service->cmds[idx].done = false; service->cmds[idx].completion = &completion; + service->cmds[idx].queued = jiffies; init_completion(&completion); spin_unlock_irqrestore(&service->lock, flags); @@ -3542,21 +3637,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) { - /* Diagnostic snapshot of shared RAM, not coprocessor MMIO. - * Unequal TX pointers mean firmware has not drained the queue. - */ - dma_rmb(); - dev_warn(ep->dcp->dev, - "AFK timeout channel=%u tag=%#x TX r/w=%#x/%#x RX r/w=%#x/%#x running=%d crashed=%d\n", - service->channel, tag, - le32_to_cpu(READ_ONCE(*ep->txbfr.rptr)), - le32_to_cpu(READ_ONCE(*ep->txbfr.wptr)), - le32_to_cpu(READ_ONCE(*ep->rxbfr.rptr)), - le32_to_cpu(READ_ONCE(*ep->rxbfr.wptr)), - apple_rtkit_is_running(ep->dcp->rtk), - apple_rtkit_is_crashed(ep->dcp->rtk)); - } + afk_report_stall(service, tag); afk_service_put(service); return -ETIMEDOUT; } @@ -3964,6 +4045,10 @@ static void afk_remove_service_debugfs(struct apple_epic_service *srv) #include "afk-parser-test.c" #endif +#if IS_ENABLED(CONFIG_DRM_APPLE_DISPLAY_KUNIT_TEST) +#include "afk-stall-test.c" +#endif + /* Called after RX and the ordered endpoint worker have been joined. */ bool afk_host_idle(struct apple_dcp_afkep *ep) { diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index df2c292a5e03e..0eff376e09492 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -47,6 +47,8 @@ struct epic_cmd_info { bool done; bool free_on_ack; struct completion *completion; + /* When the command was queued, in jiffies: for the report of a late reply. */ + unsigned long queued; }; struct apple_epic_service { @@ -57,6 +59,14 @@ struct apple_epic_service { DECLARE_BITMAP(cmd_map, MAX_PENDING_CMDS); /* Legacy EPIC sequence; AFKV2 history belongs to the interface. */ u8 cmd_tag; + /* + * A command that timed out unanswered was reported with the state of + * its endpoint, and so was a call refused for want of a free slot. + * The next reply on the service ends both; only a reply frees the slot + * of a timed-out command (lock). + */ + bool stall_reported; + bool full_reported; spinlock_t lock; /* Pins held by owners and synchronous callers; protected by lock. */ unsigned int users; @@ -587,6 +597,13 @@ struct apple_dcp_afkep { /* Serializes TX ring writes and mailbox doorbells outside ep->lock. */ struct mutex tx_lock; u16 qe_seq; + /* + * For the report of a command that goes unanswered: the doorbells not + * delivered since the last one that was, and the error of the last + * (tx_lock). + */ + unsigned int doorbells_lost; + int doorbell_error; bool stopping; /* lock: no new receive work after shutdown. */ /* Compact EPIC, detected from the first compact message (desktop 26.x). */ diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 9c794ebdb06e7..9da429b121516 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -2329,7 +2329,7 @@ static const struct apple_rtkit_ops external_h17p_rtkit_ops = { .shmem_destroy = dcp_rtk_shmem_destroy, }; -void dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message) +int dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message) { bool native_14_7 = dcp->fw_compat == DCP_FIRMWARE_V_14_7; int ret; @@ -2338,7 +2338,7 @@ void dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message) if (READ_ONCE(dcp->quiescing) || (dcp_is_ext26(dcp) && READ_ONCE(dcp->ext26_blocked))) - return; + return -ESHUTDOWN; trace_dcp_send_msg(dcp, endpoint, message); /* * The adopted T6030 14.7 and native26 (ext26) sessions share this @@ -2352,10 +2352,16 @@ void dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message) if (ret && dcp_is_ext26(dcp)) { WRITE_ONCE(dcp->crashed, true); dev_err(dcp->dev, "native26 mailbox send failed ep=%#x: %d\n", endpoint, ret); - } - if (ret && native_14_7) + } else if (ret) { + /* + * Nothing retries the message: an AFK doorbell lost here leaves + * its command unread until a later doorbell, which a stalled + * mailbox never delivers either. + */ dev_warn_ratelimited(dcp->dev, "DCP send ep %02x failed: %d\n", endpoint, ret); + } + return ret; } int dcp_crtc_atomic_check(struct drm_crtc *crtc, struct drm_atomic_commit *commit) diff --git a/drivers/gpu/drm/apple/dcp.h b/drivers/gpu/drm/apple/dcp.h index 4ea6ebc535f5f..a216ee6a7fe1a 100644 --- a/drivers/gpu/drm/apple/dcp.h +++ b/drivers/gpu/drm/apple/dcp.h @@ -157,7 +157,7 @@ void dcp_panel_read_dimensions(struct apple_dcp *dcp, struct device_node *panel_ void dcp_update_connector_dimensions(struct apple_dcp *dcp, struct drm_connector *connector); /* Must be called from sleepable context; may sleep on 14.7/ext26 mailboxes. */ -void dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message); +int dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message); int dcp_dptx_connect_oob(struct platform_device *pdev, u32 port); bool dcp_t6030_ext_native(const struct device_node *np); diff --git a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py index 65e51df3bfddc..33d764940e6a8 100644 --- a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py +++ b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py @@ -54,6 +54,9 @@ def function(pattern, what): #define dma_rmb() do {} while(0) #define dev_warn(...) do {} while(0) #define dev_err(...) do {} while(0) +static void dev_info_ratelimited(const void *dev,const char *fmt,...) { (void)dev;(void)fmt; } +#define jiffies 0UL +#define jiffies_to_msecs(j) ((unsigned int)(j)) #define complete(p) (*(p)=1) #define EPIC_TYPE_COMMAND 3 #define EPIC_CAT_COMMAND 0x30 @@ -67,7 +70,7 @@ def function(pattern, what): u16 tag; u8 type; size_t reply_length; void *rxbuf,*txbuf; dma_addr_t rxbuf_dma,txbuf_dma; size_t rxlen,txlen; u32 retcode; size_t reply_len; bool reply_len_valid; - bool done,free_on_ack; int *completion; + bool done,free_on_ack; int *completion; unsigned long queued; }; struct dcp { void *dev; bool quiescing,external,crashed,ext26; void *rtk; }; static bool dcp_is_ext26(const struct dcp *dcp) { return dcp->ext26; } @@ -78,7 +81,11 @@ def function(pattern, what): struct apple_epic_service { struct apple_dcp_afkep *ep; struct ops *ops; unsigned int channel; unsigned long cmd_map[1]; int lock; unsigned int users; bool enabled,torndown; u8 cmd_tag; struct cmd_info cmds[16]; + bool stall_reported,full_reported; }; +/* The stall report reads the rings and the mailbox state; nothing here checks its text. */ +static void afk_report_stall(struct apple_epic_service *s,u16 tag) { (void)s;(void)tag; } +__attribute__((unused)) static unsigned int afk_service_unanswered_locked(struct apple_epic_service *s) { (void)s; return 0; } static struct apple_epic_service *active; static unsigned int frees, sends; static int wire_fail, timeout, reply_status; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py index 021f328f0ea61..1aa540c7eb85e 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py @@ -46,6 +46,9 @@ def function(file,signature): #define dev_warn(...) ((void)0) #define dev_err(...) do { diagnostic_errors++; } while (0) #define dev_dbg(dev,fmt,...) do { if(!strcmp(fmt,"DCPAVSERV: legacy EDID service published\n")) legacy_publishes++; } while (0) +#define jiffies 0UL +#define jiffies_to_msecs(j) ((unsigned int)(j)) +static void dev_info_ratelimited(const void *dev,const char *fmt,...) { (void)dev;(void)fmt; } #define dev_info(...) do { diagnostic_information++; } while (0) static unsigned diagnostic_errors,diagnostic_information,legacy_publishes; @@ -86,10 +89,10 @@ def function(file,signature): void afk_service_retry(struct apple_dcp_afkep *ep); /* open_later: a firmware 27 panel service that sends its own OPEN; none is here. */ struct apple_epic_service_ops { char name[32];bool reusable,optional,open_later,singleton;void (*init)(struct apple_epic_service *,const char *,const char *,s64);int (*prepare)(struct apple_epic_service *,const char *,const char *,s64);int (*revalidate)(struct apple_epic_service *,const char *,const char *,s64);void (*init_props)(struct apple_epic_service *,s64,const void *,size_t);void (*teardown)(struct apple_epic_service *); }; -struct epic_cmd_info { u16 tag;u8 type;size_t reply_length,reply_len;bool reply_len_valid;void *rxbuf,*txbuf;dma_addr_t rxbuf_dma,txbuf_dma;size_t rxlen,txlen;u32 retcode;bool done,free_on_ack;int *completion; }; +struct epic_cmd_info { u16 tag;u8 type;size_t reply_length,reply_len;bool reply_len_valid;void *rxbuf,*txbuf;dma_addr_t rxbuf_dma,txbuf_dma;size_t rxlen,txlen;u32 retcode;bool done,free_on_ack;int *completion;unsigned long queued; }; struct apple_epic_service { struct apple_dcp_afkep *ep;const struct apple_epic_service_ops *ops; - bool lock,enabled,torndown;unsigned int users;u32 channel;u8 cmd_tag; + bool lock,enabled,torndown,stall_reported,full_reported;unsigned int users;u32 channel;u8 cmd_tag; unsigned long cmd_map[1];struct epic_cmd_info cmds[MAX_PENDING_CMDS]; struct { u64 generation,epoch; } admission;void *cookie; bool pending_open;const char *pending_name,*pending_class;s64 pending_unit; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py b/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py index 67e9c39e82448..a3c49069688a3 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py @@ -45,6 +45,9 @@ def extract(text,signature): #define kmalloc(n,g) malloc(n) #define EPIC_TYPE_COMMAND 3 #define EPIC_CAT_COMMAND 3 + /* The stall report reads the rings and the mailbox state; nothing here checks its text. */ + static void afk_report_stall(struct apple_epic_service *s,u16 tag) { (void)s;(void)tag; } + __attribute__((unused)) static unsigned int afk_service_unanswered_locked(struct apple_epic_service *s) { (void)s;return 0; } static int bitmap_find_free_region(unsigned long *p,int n,int order) { (void)order;for(int i=0;i Date: Fri, 27 Nov 2026 01:05:33 -0500 Subject: [PATCH 2/8] drm/apple: dptxep: don't queue an HPD request behind its unanswered copy A command that times out keeps its slot and its buffers until the firmware answers it: the reply names the slot and lands in the command's buffer, so neither can be reused before then (afk_recv_handle_reply()). That keeps late replies safe, but a caller that repeats a call the firmware does not answer takes one more of the service's 16 slots each time. The Type-C SAFE transition does that with a DPTX port's HPD low. dcp_typec_route_prepare() lowers HPD through dcp_dptx_disconnect_oob() and fails the transition when the HPD low times out. The port stays safe_pending, the port controller retries the transition, and every retry and every later cable event lowers HPD again. On a J316c whose external display processor had stopped reading its queue (firmware 13.5, after an s2idle), all 16 slots of the port ended up held by unanswered HPD lows, with 2048 unread bytes in its TX ring. From then on the port refused every call with -ENOSPC until a reboot. A copy queued behind an identical request that is still unanswered does nothing: no other call was queued on the service in between, so the firmware takes it right after the first, with the same input. Add afk_service_call_unanswered(), which tells whether the call queued last on a service is this one, with the same input, and timed out unanswered. dptxport_set_hpd_timeout() then returns -ETIMEDOUT at once, as the first request did, without sending a copy. The other HPD level, and any other call, is sent as before, and so is the same request once the firmware has answered. The slots of commands that did time out are not reclaimed, so late replies stay safe. This does not bring back a port whose firmware has stopped; it keeps the stall from taking every slot of the port. Signed-off-by: DjDeveloperr --- drivers/gpu/drm/apple/afk-stall-test.c | 97 +++++++++++++++++++ drivers/gpu/drm/apple/afk.c | 51 ++++++++++ drivers/gpu/drm/apple/afk.h | 10 ++ drivers/gpu/drm/apple/dptxep.c | 24 ++++- .../drivers/gpu/apple-afkv2/extract-hpd.py | 32 +++++- .../drivers/gpu/apple-afkv2/extract-send.py | 2 +- .../selftests/drivers/gpu/apple-dcp/edid.py | 1 + 7 files changed, 212 insertions(+), 5 deletions(-) diff --git a/drivers/gpu/drm/apple/afk-stall-test.c b/drivers/gpu/drm/apple/afk-stall-test.c index 14ec8158776e9..9729559ca28cd 100644 --- a/drivers/gpu/drm/apple/afk-stall-test.c +++ b/drivers/gpu/drm/apple/afk-stall-test.c @@ -108,6 +108,100 @@ static void afk_stall_answer_all(struct afk_stall_test *t) &reply, sizeof(reply)); } +/* + * The SAFE state of a port whose firmware stopped answering is retried, and + * each retry lowers HPD again: one request waits in the queue, however often. + */ +static void afk_stall_hpd_low_repeats_test(struct kunit *test) +{ + struct afk_stall_test *t = test->priv; + struct apple_epic_service *service = &t->ep.services[0]; + u32 unread; + int i; + + KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), + -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, t->doorbells, 1U); + KUNIT_EXPECT_EQ(test, afk_stall_held(service), 1U); + unread = afk_stall_unread(&t->ep); + KUNIT_EXPECT_GT(test, unread, 0U); + + /* More retries than there are slots: nothing more is queued. */ + for (i = 0; i < 2 * MAX_PENDING_CMDS; i++) + KUNIT_EXPECT_EQ(test, + dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), + -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, t->doorbells, 1U); + KUNIT_EXPECT_EQ(test, afk_stall_held(service), 1U); + KUNIT_EXPECT_EQ(test, afk_stall_unread(&t->ep), unread); + + /* The next display's HPD still gets a slot, and its unplug another. */ + KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, true, AFK_STALL_TIMEOUT_MS), + -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), + -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, t->doorbells, 3U); + KUNIT_EXPECT_EQ(test, afk_stall_held(service), 3U); + KUNIT_EXPECT_EQ(test, afk_stall_unread(&t->ep), 3 * unread); + + afk_stall_answer_all(t); + KUNIT_EXPECT_EQ(test, afk_stall_held(service), 0U); +} + +/* A late answer frees its slot, and the same request is then sent again. */ +static void afk_stall_answered_resends_test(struct kunit *test) +{ + struct afk_stall_test *t = test->priv; + struct apple_epic_service *service = &t->ep.services[0]; + struct epic_cmd reply = {}; + + KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), + -ETIMEDOUT); + KUNIT_ASSERT_EQ(test, afk_stall_held(service), 1U); + KUNIT_EXPECT_TRUE(test, service->stall_reported); + afk_recv_handle_reply(&t->ep, service->channel, service->last_tag, &reply, + sizeof(reply)); + KUNIT_EXPECT_EQ(test, afk_stall_held(service), 0U); + KUNIT_EXPECT_FALSE(test, service->stall_reported); + + KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), + -ETIMEDOUT); + KUNIT_EXPECT_EQ(test, t->doorbells, 2U); + KUNIT_EXPECT_EQ(test, afk_stall_held(service), 1U); + afk_stall_answer_all(t); +} + +/* Only the call queued last, with the same input, and only once it timed out. */ +static void afk_stall_unanswered_match_test(struct kunit *test) +{ + struct afk_stall_test *t = test->priv; + struct apple_epic_service *service = &t->ep.services[0]; + u8 data[20] = {}, other[20] = { [16] = 1 }; + + KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 8, 8, data, sizeof(data))); + KUNIT_EXPECT_EQ(test, afk_service_call_timeout(service, 8, 8, data, sizeof(data), 12, + NULL, 0, 12, AFK_STALL_TIMEOUT_MS), + -ETIMEDOUT); + KUNIT_EXPECT_TRUE(test, afk_service_call_unanswered(service, 8, 8, data, sizeof(data))); + KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 8, 8, other, sizeof(other))); + KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 8, 7, data, sizeof(data))); + KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 0, 8, data, sizeof(data))); + + /* Another call queued behind it: the copy would no longer be redundant. */ + KUNIT_EXPECT_EQ(test, afk_service_call_timeout(service, 0, 6, NULL, 0, 16, NULL, 0, 16, + AFK_STALL_TIMEOUT_MS), + -ETIMEDOUT); + KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 8, 8, data, sizeof(data))); + KUNIT_EXPECT_TRUE(test, afk_service_call_unanswered(service, 0, 6, NULL, 0)); + + /* Compact EPIC keeps its timed-out commands elsewhere. */ + t->ep.compact = true; + KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 0, 6, NULL, 0)); + t->ep.compact = false; + afk_stall_answer_all(t); + KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 0, 6, NULL, 0)); +} + /* * A doorbell the mailbox did not deliver leaves its entry in the ring for the * next one: the command keeps its slot until answered, and the loss is noted. @@ -134,6 +228,9 @@ static void afk_stall_lost_doorbell_test(struct kunit *test) } static struct kunit_case afk_stall_cases[] = { + KUNIT_CASE(afk_stall_hpd_low_repeats_test), + KUNIT_CASE(afk_stall_answered_resends_test), + KUNIT_CASE(afk_stall_unanswered_match_test), KUNIT_CASE(afk_stall_lost_doorbell_test), {} }; diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 334de4fffaac4..c359a60cdd708 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -3398,6 +3398,54 @@ static void afk_report_stall(struct apple_epic_service *service, u16 tag) !IS_ERR_OR_NULL(rtk) && apple_rtkit_is_crashed(rtk)); } +/** + * afk_service_call_unanswered - whether a standard call is already queued unanswered + * @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 + * + * True when the command queued last on @service is this call, with the same + * input, and it timed out with its answer still to come. The call then still + * waits in the firmware's queue with nothing queued behind it, and a copy + * would only queue behind it, to the same effect. The copy would also keep a + * command slot of its own until the firmware answers it (see + * afk_service_unanswered_locked()), so a caller that repeats such a call to + * firmware that has stopped answering takes every slot, and the service then + * refuses all of its calls with -ENOSPC. + * + * Compact EPIC keeps its timed-out commands apart (compact_keep_orphans), and + * is not covered. + */ +bool afk_service_call_unanswered(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; + unsigned long flags; + bool queued; + + if (!service || !service->ep || READ_ONCE(service->ep->compact)) + return false; + + 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. */ + queued = service->last_valid && test_bit(service->last_slot, service->cmd_map) && + cmd->tag == service->last_tag && !cmd->done && !cmd->completion && + cmd->type == EPIC_SUBTYPE_STD_SERVICE && call && + 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 && + (!data_len || !memcmp(call + 1, data, data_len)); + spin_unlock_irqrestore(&service->lock, flags); + return queued; +} +EXPORT_SYMBOL_IF_KUNIT(afk_service_call_unanswered); + 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, @@ -3596,6 +3644,9 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type, service->cmds[idx].done = false; service->cmds[idx].completion = &completion; service->cmds[idx].queued = jiffies; + service->last_slot = idx; + service->last_tag = tag; + service->last_valid = true; init_completion(&completion); spin_unlock_irqrestore(&service->lock, flags); diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index 0eff376e09492..aa64a12c5e5e1 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -59,6 +59,14 @@ struct apple_epic_service { DECLARE_BITMAP(cmd_map, MAX_PENDING_CMDS); /* Legacy EPIC sequence; AFKV2 history belongs to the interface. */ u8 cmd_tag; + /* + * The command queued last, see afk_service_call_unanswered(). A reused + * service has an empty cmd_map, so this names no command of the new + * instance (lock). + */ + u16 last_tag; + u8 last_slot; + bool last_valid; /* * A command that timed out unanswered was reported with the state of * its endpoint, and so was a call refused for want of a free slot. @@ -727,6 +735,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_unanswered(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, diff --git a/drivers/gpu/drm/apple/dptxep.c b/drivers/gpu/drm/apple/dptxep.c index 513514cd2cfb1..a300f17553a0d 100644 --- a/drivers/gpu/drm/apple/dptxep.c +++ b/drivers/gpu/drm/apple/dptxep.c @@ -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; @@ -254,7 +258,25 @@ 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, queued last on the port, timed out and is still + * unanswered: the firmware takes none of the port's calls for now. A + * copy would queue behind it to the same effect, 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 otherwise + * take every slot: the port then refuses all its calls with -ENOSPC, + * the HPD of the next display among them. + */ + if (afk_service_call_unanswered(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 unanswered, not sent 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) diff --git a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-hpd.py b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-hpd.py index ea3915f0de720..7b5e5981f296f 100644 --- a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-hpd.py +++ b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-hpd.py @@ -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 its timed-out copy is unanswered. """ import pathlib import re @@ -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 @@ -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) @@ -95,6 +98,17 @@ def function(source, name): assert(dcp == expected_dcp && service != NULL); cec_closes++; } +/* The HPD request queued last on the port timed out and is still unanswered. */ +static bool unanswered; +static unsigned int sends, unanswered_asked; +bool afk_service_call_unanswered(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); + unanswered_asked++; + return unanswered; +} 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) @@ -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)); @@ -206,14 +221,25 @@ def function(source, name): } ep.compact = false; compact_status = 0; } + /* A copy of an unanswered request is not sent, and fails as the request did. */ + for (int hpd = 0; hpd < 2; hpd++) { + unsigned int before = sends, asked = unanswered_asked; + int ret; + + unanswered = true; + ret = dptxport_set_hpd_timeout(&service, hpd, 1000); + unanswered = false; + expect(ret == -ETIMEDOUT && sends == before && unanswered_asked == asked + 1, + "copy of an unanswered 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 unanswered 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) diff --git a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py index 33d764940e6a8..fc6686dc0c9f2 100644 --- a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py +++ b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py @@ -81,7 +81,7 @@ def function(pattern, what): struct apple_epic_service { struct apple_dcp_afkep *ep; struct ops *ops; unsigned int channel; unsigned long cmd_map[1]; int lock; unsigned int users; bool enabled,torndown; u8 cmd_tag; struct cmd_info cmds[16]; - bool stall_reported,full_reported; + u16 last_tag; u8 last_slot; bool last_valid,stall_reported,full_reported; }; /* The stall report reads the rings and the mailbox state; nothing here checks its text. */ static void afk_report_stall(struct apple_epic_service *s,u16 tag) { (void)s;(void)tag; } diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py index 1aa540c7eb85e..f5570f913469e 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py @@ -93,6 +93,7 @@ def function(file,signature): struct apple_epic_service { struct apple_dcp_afkep *ep;const struct apple_epic_service_ops *ops; bool lock,enabled,torndown,stall_reported,full_reported;unsigned int users;u32 channel;u8 cmd_tag; + u16 last_tag;u8 last_slot;bool last_valid; unsigned long cmd_map[1];struct epic_cmd_info cmds[MAX_PENDING_CMDS]; struct { u64 generation,epoch; } admission;void *cookie; bool pending_open;const char *pending_name,*pending_class;s64 pending_unit; From 09938fa533928c16eb9cba786dc2f9637a181a12 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 18:36:00 -0500 Subject: [PATCH 3/8] drm/apple: afk: drop the stall report and the doorbell count Main now returns the error of an undelivered doorbell from afk_send_ret() to the caller, keeps the slot and buffers of the published command itself, and logs the failed call. A command that times out has had its doorbell delivered. That replaces what this branch did with an int afk_send(), a dcp_send_message() that returns the mailbox result, and a count of undelivered doorbells. The rest of the report (slots, late replies, the first stall only) is not needed for the fix. Keep only the HPD check. The apple-afk-stall KUnit suite stubbed the second afk_send() and goes with it. The host tests run the real sender instead. Signed-off-by: DjDeveloperr --- drivers/gpu/drm/apple/afk-stall-test.c | 244 ------------------ drivers/gpu/drm/apple/afk.c | 133 ++-------- drivers/gpu/drm/apple/afk.h | 17 -- drivers/gpu/drm/apple/dcp.c | 14 +- drivers/gpu/drm/apple/dcp.h | 2 +- .../drivers/gpu/apple-afkv2/extract-send.py | 10 +- .../selftests/drivers/gpu/apple-dcp/edid.py | 7 +- .../drivers/gpu/apple-dcp/recovery.py | 3 - 8 files changed, 32 insertions(+), 398 deletions(-) delete mode 100644 drivers/gpu/drm/apple/afk-stall-test.c diff --git a/drivers/gpu/drm/apple/afk-stall-test.c b/drivers/gpu/drm/apple/afk-stall-test.c deleted file mode 100644 index 9729559ca28cd..0000000000000 --- a/drivers/gpu/drm/apple/afk-stall-test.c +++ /dev/null @@ -1,244 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-only OR MIT -/* - * Included by afk.c: a legacy EPIC endpoint whose firmware stops answering, - * through the real send, timeout and reply paths. The rings are host memory - * and the doorbell is a stub, so nothing reads the queued commands. - */ -#include -#include -#include -#include - -#include "dptxep.h" - -#define AFK_STALL_STRIDE 64 -/* The ring size firmware 13.5 gives its DPTX endpoint: 16 KiB less three strides. */ -#define AFK_STALL_RING (SZ_16K - 3 * AFK_STALL_STRIDE) -/* Timeouts of a single jiffy: nothing answers in time anyway. */ -#define AFK_STALL_TIMEOUT_MS 1 - -struct afk_stall_test { - struct apple_dcp_afkep ep; - struct apple_dcp dcp; - unsigned int doorbells; - int doorbell_ret; -}; - -static const struct apple_epic_service_ops afk_stall_ops = { - .name = "dcpdptx-port-epic", -}; - -static int afk_stall_doorbell(struct apple_dcp_afkep *ep, u64 message) -{ - struct afk_stall_test *t = container_of(ep, struct afk_stall_test, ep); - - t->doorbells++; - return t->doorbell_ret; -} - -static int afk_stall_ring(struct kunit *test, struct afk_ringbuffer *bfr) -{ - u8 *hdr = kunit_kzalloc(test, 3 * AFK_STALL_STRIDE + AFK_STALL_RING, GFP_KERNEL); - - if (!hdr) - return -ENOMEM; - bfr->rptr = (__le32 *)(hdr + AFK_STALL_STRIDE); - bfr->wptr = (__le32 *)(hdr + 2 * AFK_STALL_STRIDE); - bfr->buf = hdr + 3 * AFK_STALL_STRIDE; - bfr->bufsz = AFK_STALL_RING; - bfr->stride = AFK_STALL_STRIDE; - bfr->ready = true; - return 0; -} - -static int afk_stall_test_init(struct kunit *test) -{ - struct apple_epic_service *service; - struct afk_stall_test *t; - struct device *dev; - int ret; - - t = kunit_kzalloc(test, sizeof(*t), GFP_KERNEL); - if (!t) - return -ENOMEM; - dev = kunit_device_register(test, "afk-stall"); - if (IS_ERR(dev)) - return PTR_ERR(dev); - /* The command buffers are coherent DMA, as on the display processor. */ - dev->coherent_dma_mask = DMA_BIT_MASK(64); - dev->dma_mask = &dev->coherent_dma_mask; - t->dcp.dev = dev; - t->dcp.fw_compat = DCP_FIRMWARE_V_13_5; - afk_ep_init_state(&t->ep); - t->ep.dcp = &t->dcp; - t->ep.endpoint = 0x2a; - ret = afk_stall_ring(test, &t->ep.txbfr) ?: afk_stall_ring(test, &t->ep.rxbfr); - if (ret) - return ret; - t->ep.num_channels = 1; - service = &t->ep.services[0]; - service->ep = &t->ep; - service->ops = &afk_stall_ops; - service->channel = 1; - service->enabled = true; - test->priv = t; - kunit_activate_static_stub(test, afk_send, afk_stall_doorbell); - return 0; -} - -static unsigned int afk_stall_held(struct apple_epic_service *service) -{ - return bitmap_weight(service->cmd_map, MAX_PENDING_CMDS); -} - -static u32 afk_stall_unread(struct apple_dcp_afkep *ep) -{ - return le32_to_cpu(*ep->txbfr.wptr) - le32_to_cpu(*ep->txbfr.rptr); -} - -/* The firmware answers every command still waiting: the test frees their buffers. */ -static void afk_stall_answer_all(struct afk_stall_test *t) -{ - struct apple_epic_service *service = &t->ep.services[0]; - struct epic_cmd reply = {}; - unsigned int i; - - for_each_set_bit(i, service->cmd_map, MAX_PENDING_CMDS) - afk_recv_handle_reply(&t->ep, service->channel, service->cmds[i].tag, - &reply, sizeof(reply)); -} - -/* - * The SAFE state of a port whose firmware stopped answering is retried, and - * each retry lowers HPD again: one request waits in the queue, however often. - */ -static void afk_stall_hpd_low_repeats_test(struct kunit *test) -{ - struct afk_stall_test *t = test->priv; - struct apple_epic_service *service = &t->ep.services[0]; - u32 unread; - int i; - - KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), - -ETIMEDOUT); - KUNIT_EXPECT_EQ(test, t->doorbells, 1U); - KUNIT_EXPECT_EQ(test, afk_stall_held(service), 1U); - unread = afk_stall_unread(&t->ep); - KUNIT_EXPECT_GT(test, unread, 0U); - - /* More retries than there are slots: nothing more is queued. */ - for (i = 0; i < 2 * MAX_PENDING_CMDS; i++) - KUNIT_EXPECT_EQ(test, - dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), - -ETIMEDOUT); - KUNIT_EXPECT_EQ(test, t->doorbells, 1U); - KUNIT_EXPECT_EQ(test, afk_stall_held(service), 1U); - KUNIT_EXPECT_EQ(test, afk_stall_unread(&t->ep), unread); - - /* The next display's HPD still gets a slot, and its unplug another. */ - KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, true, AFK_STALL_TIMEOUT_MS), - -ETIMEDOUT); - KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), - -ETIMEDOUT); - KUNIT_EXPECT_EQ(test, t->doorbells, 3U); - KUNIT_EXPECT_EQ(test, afk_stall_held(service), 3U); - KUNIT_EXPECT_EQ(test, afk_stall_unread(&t->ep), 3 * unread); - - afk_stall_answer_all(t); - KUNIT_EXPECT_EQ(test, afk_stall_held(service), 0U); -} - -/* A late answer frees its slot, and the same request is then sent again. */ -static void afk_stall_answered_resends_test(struct kunit *test) -{ - struct afk_stall_test *t = test->priv; - struct apple_epic_service *service = &t->ep.services[0]; - struct epic_cmd reply = {}; - - KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), - -ETIMEDOUT); - KUNIT_ASSERT_EQ(test, afk_stall_held(service), 1U); - KUNIT_EXPECT_TRUE(test, service->stall_reported); - afk_recv_handle_reply(&t->ep, service->channel, service->last_tag, &reply, - sizeof(reply)); - KUNIT_EXPECT_EQ(test, afk_stall_held(service), 0U); - KUNIT_EXPECT_FALSE(test, service->stall_reported); - - KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), - -ETIMEDOUT); - KUNIT_EXPECT_EQ(test, t->doorbells, 2U); - KUNIT_EXPECT_EQ(test, afk_stall_held(service), 1U); - afk_stall_answer_all(t); -} - -/* Only the call queued last, with the same input, and only once it timed out. */ -static void afk_stall_unanswered_match_test(struct kunit *test) -{ - struct afk_stall_test *t = test->priv; - struct apple_epic_service *service = &t->ep.services[0]; - u8 data[20] = {}, other[20] = { [16] = 1 }; - - KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 8, 8, data, sizeof(data))); - KUNIT_EXPECT_EQ(test, afk_service_call_timeout(service, 8, 8, data, sizeof(data), 12, - NULL, 0, 12, AFK_STALL_TIMEOUT_MS), - -ETIMEDOUT); - KUNIT_EXPECT_TRUE(test, afk_service_call_unanswered(service, 8, 8, data, sizeof(data))); - KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 8, 8, other, sizeof(other))); - KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 8, 7, data, sizeof(data))); - KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 0, 8, data, sizeof(data))); - - /* Another call queued behind it: the copy would no longer be redundant. */ - KUNIT_EXPECT_EQ(test, afk_service_call_timeout(service, 0, 6, NULL, 0, 16, NULL, 0, 16, - AFK_STALL_TIMEOUT_MS), - -ETIMEDOUT); - KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 8, 8, data, sizeof(data))); - KUNIT_EXPECT_TRUE(test, afk_service_call_unanswered(service, 0, 6, NULL, 0)); - - /* Compact EPIC keeps its timed-out commands elsewhere. */ - t->ep.compact = true; - KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 0, 6, NULL, 0)); - t->ep.compact = false; - afk_stall_answer_all(t); - KUNIT_EXPECT_FALSE(test, afk_service_call_unanswered(service, 0, 6, NULL, 0)); -} - -/* - * A doorbell the mailbox did not deliver leaves its entry in the ring for the - * next one: the command keeps its slot until answered, and the loss is noted. - */ -static void afk_stall_lost_doorbell_test(struct kunit *test) -{ - struct afk_stall_test *t = test->priv; - struct apple_epic_service *service = &t->ep.services[0]; - - t->doorbell_ret = -ETIMEDOUT; - KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, true, AFK_STALL_TIMEOUT_MS), - -ETIMEDOUT); - KUNIT_EXPECT_EQ(test, t->ep.doorbells_lost, 1U); - KUNIT_EXPECT_EQ(test, t->ep.doorbell_error, -ETIMEDOUT); - KUNIT_EXPECT_EQ(test, afk_stall_held(service), 1U); - KUNIT_EXPECT_GT(test, afk_stall_unread(&t->ep), 0U); - - t->doorbell_ret = 0; - KUNIT_EXPECT_EQ(test, dptxport_set_hpd_timeout(service, false, AFK_STALL_TIMEOUT_MS), - -ETIMEDOUT); - KUNIT_EXPECT_EQ(test, t->ep.doorbells_lost, 0U); - KUNIT_EXPECT_EQ(test, afk_stall_held(service), 2U); - afk_stall_answer_all(t); -} - -static struct kunit_case afk_stall_cases[] = { - KUNIT_CASE(afk_stall_hpd_low_repeats_test), - KUNIT_CASE(afk_stall_answered_resends_test), - KUNIT_CASE(afk_stall_unanswered_match_test), - KUNIT_CASE(afk_stall_lost_doorbell_test), - {} -}; - -static struct kunit_suite afk_stall_suite = { - .name = "apple-afk-stall", - .init = afk_stall_test_init, - .test_cases = afk_stall_cases, -}; - -kunit_test_suite(afk_stall_suite); diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 6254b5af3bf07..ac3f6d18bd5bd 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -21,7 +21,6 @@ #include #include -#include #include #include "afk.h" @@ -96,10 +95,9 @@ static unsigned int afk_service_timeout_ms(struct apple_epic_service *service) return service ? afk_timeout_ms(service->ep) : MSEC_PER_SEC; } -static int afk_send(struct apple_dcp_afkep *ep, u64 message) +static void afk_send(struct apple_dcp_afkep *ep, u64 message) { - KUNIT_STATIC_STUB_REDIRECT(afk_send, ep, message); - return dcp_send_message(ep->dcp, ep->endpoint, message); + dcp_send_message(ep->dcp, ep->endpoint, message); } static void afk_release_context(void *data) @@ -2116,10 +2114,9 @@ static void afk_recv_handle_reply(struct apple_dcp_afkep *ep, u32 channel, { struct epic_cmd *cmd = payload; struct apple_epic_service *service; - unsigned long flags, waited; + unsigned long flags; u8 idx = tag & (ep->wire_v2 ? 0xf : 0xff); void *rxbuf, *txbuf; - bool late; dma_addr_t rxbuf_dma, txbuf_dma; size_t rxlen, txlen; @@ -2165,11 +2162,6 @@ static void afk_recv_handle_reply(struct apple_dcp_afkep *ep, u32 channel, service->cmds[idx].retcode = le32_to_cpu(cmd->retcode); service->cmds[idx].reply_len = le32_to_cpu(cmd->rxlen); service->cmds[idx].reply_len_valid = true; - /* The firmware answers again: a stall from now on is a new one. */ - service->stall_reported = false; - service->full_reported = false; - late = !service->cmds[idx].completion; - waited = jiffies - service->cmds[idx].queued; if (service->cmds[idx].free_on_ack) { /* defer freeing until we're no longer in atomic context */ rxbuf = service->cmds[idx].rxbuf; @@ -2188,13 +2180,6 @@ static void afk_recv_handle_reply(struct apple_dcp_afkep *ep, u32 channel, spin_unlock_irqrestore(&service->lock, flags); - /* Whether the firmware was only slow, or had stopped: say when it answered. */ - if (late) - dev_info_ratelimited(ep->dcp->dev, - "AFK[ep:%02x]: chan %u tag 0x%04x answered after its timeout, %u ms after it was queued (retcode %#x)\n", - ep->endpoint, channel, tag, jiffies_to_msecs(waited), - le32_to_cpu(cmd->retcode)); - if (rxbuf && rxlen) dma_free_coherent(ep->dcp->dev, rxlen, rxbuf, rxbuf_dma); if (txbuf && txlen) @@ -3295,19 +3280,8 @@ static int afk_send_epic_token(struct apple_dcp_afkep *ep, u32 channel, u16 tag, spin_unlock_irqrestore(&ep->lock, flags); /* Keep tx_lock held across the doorbell so ring writes stay ordered. */ - ret = afk_send(ep, FIELD_PREP(RBEP_TYPE, RBEP_SEND) | - FIELD_PREP(SEND_WPTR, wptr)); - /* - * The entry stays in the ring whatever became of its doorbell: a later - * doorbell carries a later write pointer, and the firmware reads up to - * that. A lost one is only noted, for afk_report_stall(). - */ - if (ret) { - WRITE_ONCE(ep->doorbells_lost, ep->doorbells_lost + 1); - WRITE_ONCE(ep->doorbell_error, ret); - } else { - WRITE_ONCE(ep->doorbells_lost, 0); - } + afk_send(ep, FIELD_PREP(RBEP_TYPE, RBEP_SEND) | + FIELD_PREP(SEND_WPTR, wptr)); mutex_unlock(&ep->tx_lock); return 0; @@ -3360,67 +3334,6 @@ static u16 afk_command_tag(struct apple_epic_service *service, u8 slot) return tag; } -/* - * A command that timed out keeps its slot, and its buffers, for the reply the - * firmware may still send: the firmware writes the reply into the command's - * buffer and names the slot in it, so both stay the command's until then - * (afk_recv_handle_reply()). The number of such commands on @service. - */ -static unsigned int afk_service_unanswered_locked(struct apple_epic_service *service) -{ - unsigned int i, unanswered = 0; - - lockdep_assert_held(&service->lock); - for_each_set_bit(i, service->cmd_map, MAX_PENDING_CMDS) - if (!service->cmds[i].done && !service->cmds[i].completion) - unanswered++; - return unanswered; -} - -/* - * A command of @service, @tag, timed out unanswered. The first one since the - * service's last reply reports what the host sees of the endpoint, once: its - * slots, the rings in shared memory and the doorbells. Nothing here reads the - * coprocessor. - */ -static void afk_report_stall(struct apple_epic_service *service, u16 tag) -{ - struct apple_dcp_afkep *ep = service->ep; - struct apple_rtkit *rtk = ep->dcp->rtk; - unsigned int held, unanswered; - u32 tx_r, tx_w, rx_r, rx_w; - unsigned long flags; - bool first; - - spin_lock_irqsave(&service->lock, flags); - first = !service->stall_reported; - service->stall_reported = true; - held = bitmap_weight(service->cmd_map, MAX_PENDING_CMDS); - unanswered = afk_service_unanswered_locked(service); - spin_unlock_irqrestore(&service->lock, flags); - if (!first || !ep->txbfr.ready || !ep->rxbfr.ready) - return; - - dma_rmb(); - tx_r = le32_to_cpu(READ_ONCE(*ep->txbfr.rptr)); - tx_w = le32_to_cpu(READ_ONCE(*ep->txbfr.wptr)); - rx_r = le32_to_cpu(READ_ONCE(*ep->rxbfr.rptr)); - rx_w = le32_to_cpu(READ_ONCE(*ep->rxbfr.wptr)); - /* - * Unread TX bytes are what the firmware has not taken from the ring; - * equal RX pointers, that the host has read all it was sent. - */ - dev_warn(ep->dcp->dev, - "AFK[ep:%02x]: %s (chan:%u) stalled at tag 0x%04x: %u of %u command slots taken, %u by timed-out commands; TX r/w %#x/%#x (%u of %zu bytes unread), RX r/w %#x/%#x; %u doorbell(s) undelivered (%d); running=%d crashed=%d\n", - ep->endpoint, service->ops->name, service->channel, tag, held, - MAX_PENDING_CMDS, unanswered, tx_r, tx_w, - tx_w >= tx_r ? tx_w - tx_r : (u32)ep->txbfr.bufsz - tx_r + tx_w, - ep->txbfr.bufsz, rx_r, rx_w, - READ_ONCE(ep->doorbells_lost), READ_ONCE(ep->doorbell_error), - !IS_ERR_OR_NULL(rtk) && apple_rtkit_is_running(rtk), - !IS_ERR_OR_NULL(rtk) && apple_rtkit_is_crashed(rtk)); -} - /** * afk_service_call_unanswered - whether a standard call is already queued unanswered * @service: the service the call is for @@ -3433,10 +3346,9 @@ static void afk_report_stall(struct apple_epic_service *service, u16 tag) * input, and it timed out with its answer still to come. The call then still * waits in the firmware's queue with nothing queued behind it, and a copy * would only queue behind it, to the same effect. The copy would also keep a - * command slot of its own until the firmware answers it (see - * afk_service_unanswered_locked()), so a caller that repeats such a call to - * firmware that has stopped answering takes every slot, and the service then - * refuses all of its calls with -ENOSPC. + * command slot of its own until the firmware answers it, so a caller that + * repeats such a call to firmware that has stopped answering takes every slot, + * and the service then refuses all of its calls with -ENOSPC. * * Compact EPIC keeps its timed-out commands apart (compact_keep_orphans), and * is not covered. @@ -3467,7 +3379,6 @@ bool afk_service_call_unanswered(struct apple_epic_service *service, u16 group, spin_unlock_irqrestore(&service->lock, flags); return queued; } -EXPORT_SYMBOL_IF_KUNIT(afk_service_call_unanswered); static int afk_send_command_common(struct apple_epic_service *service, u8 type, const void *payload, size_t payload_len, @@ -3639,13 +3550,6 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type, } idx = bitmap_find_free_region(service->cmd_map, MAX_PENDING_CMDS, 0); if (idx < 0) { - /* Once until the next reply: every call is refused until then. */ - if (!service->full_reported) - dev_warn(ep->dcp->dev, - "AFK[ep:%02x]: %s (chan:%u) has no free command slot: %u of %u held by timed-out commands still unanswered\n", - ep->endpoint, service->ops->name, service->channel, - afk_service_unanswered_locked(service), MAX_PENDING_CMDS); - service->full_reported = true; ret = -ENOSPC; goto err_unlock; } @@ -3666,7 +3570,6 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type, service->cmds[idx].free_on_ack = false; service->cmds[idx].done = false; service->cmds[idx].completion = &completion; - service->cmds[idx].queued = jiffies; service->last_slot = idx; service->last_tag = tag; service->last_valid = true; @@ -3711,7 +3614,21 @@ 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); - afk_report_stall(service, tag); + if (ep->dcp->external && ep->txbfr.ready && ep->rxbfr.ready) { + /* Diagnostic snapshot of shared RAM, not coprocessor MMIO. + * Unequal TX pointers mean firmware has not drained the queue. + */ + dma_rmb(); + dev_warn(ep->dcp->dev, + "AFK timeout channel=%u tag=%#x TX r/w=%#x/%#x RX r/w=%#x/%#x running=%d crashed=%d\n", + service->channel, tag, + le32_to_cpu(READ_ONCE(*ep->txbfr.rptr)), + le32_to_cpu(READ_ONCE(*ep->txbfr.wptr)), + le32_to_cpu(READ_ONCE(*ep->rxbfr.rptr)), + le32_to_cpu(READ_ONCE(*ep->rxbfr.wptr)), + apple_rtkit_is_running(ep->dcp->rtk), + apple_rtkit_is_crashed(ep->dcp->rtk)); + } afk_service_put(service); return -ETIMEDOUT; } @@ -4119,10 +4036,6 @@ static void afk_remove_service_debugfs(struct apple_epic_service *srv) #include "afk-parser-test.c" #endif -#if IS_ENABLED(CONFIG_DRM_APPLE_DISPLAY_KUNIT_TEST) -#include "afk-stall-test.c" -#endif - /* Called after RX and the ordered endpoint worker have been joined. */ bool afk_host_idle(struct apple_dcp_afkep *ep) { diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index 0c3e0834bf8b7..a4c43d46815d6 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -47,8 +47,6 @@ struct epic_cmd_info { bool done; bool free_on_ack; struct completion *completion; - /* When the command was queued, in jiffies: for the report of a late reply. */ - unsigned long queued; }; struct apple_epic_service { @@ -67,14 +65,6 @@ struct apple_epic_service { u16 last_tag; u8 last_slot; bool last_valid; - /* - * A command that timed out unanswered was reported with the state of - * its endpoint, and so was a call refused for want of a free slot. - * The next reply on the service ends both; only a reply frees the slot - * of a timed-out command (lock). - */ - bool stall_reported; - bool full_reported; spinlock_t lock; /* Pins held by owners and synchronous callers; protected by lock. */ unsigned int users; @@ -611,13 +601,6 @@ struct apple_dcp_afkep { /* Serializes TX ring writes and mailbox doorbells outside ep->lock. */ struct mutex tx_lock; u16 qe_seq; - /* - * For the report of a command that goes unanswered: the doorbells not - * delivered since the last one that was, and the error of the last - * (tx_lock). - */ - unsigned int doorbells_lost; - int doorbell_error; bool stopping; /* lock: no new receive work after shutdown. */ /* Compact EPIC, detected from the first compact message (desktop 26.x). */ diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 31d92a1fe8750..c4934fcb3fef3 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -2411,7 +2411,7 @@ static const struct apple_rtkit_ops external_h17p_rtkit_ops = { .shmem_destroy = dcp_rtk_shmem_destroy, }; -int dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message) +void dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message) { bool native_14_7 = dcp->fw_compat == DCP_FIRMWARE_V_14_7; int ret; @@ -2420,7 +2420,7 @@ int dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message) if (READ_ONCE(dcp->quiescing) || (dcp_is_ext26(dcp) && READ_ONCE(dcp->ext26_blocked))) - return -ESHUTDOWN; + return; trace_dcp_send_msg(dcp, endpoint, message); /* * The adopted T6030 14.7 and native26 (ext26) sessions share this @@ -2434,16 +2434,10 @@ int dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message) if (ret && dcp_is_ext26(dcp)) { WRITE_ONCE(dcp->crashed, true); dev_err(dcp->dev, "native26 mailbox send failed ep=%#x: %d\n", endpoint, ret); - } else if (ret) { - /* - * Nothing retries the message: an AFK doorbell lost here leaves - * its command unread until a later doorbell, which a stalled - * mailbox never delivers either. - */ + } + if (ret && native_14_7) dev_warn_ratelimited(dcp->dev, "DCP send ep %02x failed: %d\n", endpoint, ret); - } - return ret; } int dcp_crtc_atomic_check(struct drm_crtc *crtc, struct drm_atomic_commit *commit) diff --git a/drivers/gpu/drm/apple/dcp.h b/drivers/gpu/drm/apple/dcp.h index d28bb27193bbe..495ed1caa5de2 100644 --- a/drivers/gpu/drm/apple/dcp.h +++ b/drivers/gpu/drm/apple/dcp.h @@ -161,7 +161,7 @@ void dcp_panel_read_dimensions(struct apple_dcp *dcp, struct device_node *panel_ void dcp_update_connector_dimensions(struct apple_dcp *dcp, struct drm_connector *connector); /* Must be called from sleepable context; may sleep on 14.7/ext26 mailboxes. */ -int dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message); +void dcp_send_message(struct apple_dcp *dcp, u8 endpoint, u64 message); int dcp_dptx_connect_oob(struct platform_device *pdev, u32 port); bool dcp_t6030_ext_native(const struct device_node *np); diff --git a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py index fc6686dc0c9f2..d90c61f60baf9 100644 --- a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py +++ b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py @@ -54,9 +54,6 @@ def function(pattern, what): #define dma_rmb() do {} while(0) #define dev_warn(...) do {} while(0) #define dev_err(...) do {} while(0) -static void dev_info_ratelimited(const void *dev,const char *fmt,...) { (void)dev;(void)fmt; } -#define jiffies 0UL -#define jiffies_to_msecs(j) ((unsigned int)(j)) #define complete(p) (*(p)=1) #define EPIC_TYPE_COMMAND 3 #define EPIC_CAT_COMMAND 0x30 @@ -70,7 +67,7 @@ def function(pattern, what): u16 tag; u8 type; size_t reply_length; void *rxbuf,*txbuf; dma_addr_t rxbuf_dma,txbuf_dma; size_t rxlen,txlen; u32 retcode; size_t reply_len; bool reply_len_valid; - bool done,free_on_ack; int *completion; unsigned long queued; + bool done,free_on_ack; int *completion; }; struct dcp { void *dev; bool quiescing,external,crashed,ext26; void *rtk; }; static bool dcp_is_ext26(const struct dcp *dcp) { return dcp->ext26; } @@ -81,11 +78,8 @@ def function(pattern, what): struct apple_epic_service { struct apple_dcp_afkep *ep; struct ops *ops; unsigned int channel; unsigned long cmd_map[1]; int lock; unsigned int users; bool enabled,torndown; u8 cmd_tag; struct cmd_info cmds[16]; - u16 last_tag; u8 last_slot; bool last_valid,stall_reported,full_reported; + u16 last_tag; u8 last_slot; bool last_valid; }; -/* The stall report reads the rings and the mailbox state; nothing here checks its text. */ -static void afk_report_stall(struct apple_epic_service *s,u16 tag) { (void)s;(void)tag; } -__attribute__((unused)) static unsigned int afk_service_unanswered_locked(struct apple_epic_service *s) { (void)s; return 0; } static struct apple_epic_service *active; static unsigned int frees, sends; static int wire_fail, timeout, reply_status; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py index 51530135374ba..594556ef3be56 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py @@ -46,9 +46,6 @@ def function(file,signature): #define dev_warn(...) ((void)0) #define dev_err(...) do { diagnostic_errors++; } while (0) #define dev_dbg(dev,fmt,...) do { if(!strcmp(fmt,"DCPAVSERV: legacy EDID service published\n")) legacy_publishes++; } while (0) -#define jiffies 0UL -#define jiffies_to_msecs(j) ((unsigned int)(j)) -static void dev_info_ratelimited(const void *dev,const char *fmt,...) { (void)dev;(void)fmt; } #define dev_info(...) do { diagnostic_information++; } while (0) static unsigned diagnostic_errors,diagnostic_information,legacy_publishes; @@ -89,10 +86,10 @@ def function(file,signature): void afk_service_retry(struct apple_dcp_afkep *ep); /* open_later: a firmware 27 panel service that sends its own OPEN; none is here. */ struct apple_epic_service_ops { char name[32];bool reusable,optional,open_later,singleton;void (*init)(struct apple_epic_service *,const char *,const char *,s64);int (*prepare)(struct apple_epic_service *,const char *,const char *,s64);int (*revalidate)(struct apple_epic_service *,const char *,const char *,s64);void (*init_props)(struct apple_epic_service *,s64,const void *,size_t);void (*teardown)(struct apple_epic_service *); }; -struct epic_cmd_info { u16 tag;u8 type;size_t reply_length,reply_len;bool reply_len_valid;void *rxbuf,*txbuf;dma_addr_t rxbuf_dma,txbuf_dma;size_t rxlen,txlen;u32 retcode;bool done,free_on_ack;int *completion;unsigned long queued; }; +struct epic_cmd_info { u16 tag;u8 type;size_t reply_length,reply_len;bool reply_len_valid;void *rxbuf,*txbuf;dma_addr_t rxbuf_dma,txbuf_dma;size_t rxlen,txlen;u32 retcode;bool done,free_on_ack;int *completion; }; struct apple_epic_service { struct apple_dcp_afkep *ep;const struct apple_epic_service_ops *ops; - bool lock,enabled,torndown,defer_start,started,stall_reported,full_reported;unsigned int users;u32 channel;u8 cmd_tag; + bool lock,enabled,torndown,defer_start,started;unsigned int users;u32 channel;u8 cmd_tag; u16 last_tag;u8 last_slot;bool last_valid; unsigned long cmd_map[1];struct epic_cmd_info cmds[MAX_PENDING_CMDS]; struct { u64 generation,epoch; } admission;void *cookie; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py b/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py index a3c49069688a3..67e9c39e82448 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py @@ -45,9 +45,6 @@ def extract(text,signature): #define kmalloc(n,g) malloc(n) #define EPIC_TYPE_COMMAND 3 #define EPIC_CAT_COMMAND 3 - /* The stall report reads the rings and the mailbox state; nothing here checks its text. */ - static void afk_report_stall(struct apple_epic_service *s,u16 tag) { (void)s;(void)tag; } - __attribute__((unused)) static unsigned int afk_service_unanswered_locked(struct apple_epic_service *s) { (void)s;return 0; } static int bitmap_find_free_region(unsigned long *p,int n,int order) { (void)order;for(int i=0;i Date: Wed, 2 Dec 2026 18:36:11 -0500 Subject: [PATCH 4/8] drm/apple: dptxep: ring again for an unread HPD request The check that keeps an HPD request from being queued behind its own unanswered copy also skipped the copy's doorbell. Main now fails a command whose doorbell the mailbox did not deliver, and holds its slot like that of a timed-out one. Skipping every repeat of such a request would leave it without a doorbell until some other call is queued on the endpoint, and a port in a Type-C SAFE retry queues no other call. A request the firmware did read, but never answered, would also never be sent again. Match the request only while the TX ring still holds unread entries, and ring the doorbell again for it. Every retry then pokes the firmware as before, but takes no slot. Use main's transmit admission, and leave a call that the sender would refuse to the sender, so its error stays the same. The slot of the command queued last is enough to find it: a set slot bit means that it was not reused since. Run the check and the sender together in the host tests, for a request that timed out and for one whose doorbell was lost. Signed-off-by: DjDeveloperr --- drivers/gpu/drm/apple/afk.c | 68 ++++++++++++------- drivers/gpu/drm/apple/afk.h | 11 ++- drivers/gpu/drm/apple/dptxep.c | 19 +++--- .../drivers/gpu/apple-afkv2/extract-hpd.py | 28 ++++---- .../drivers/gpu/apple-afkv2/extract-send.py | 59 ++++++++++++++-- .../selftests/drivers/gpu/apple-dcp/edid.py | 2 +- 6 files changed, 125 insertions(+), 62 deletions(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index dd4c12a57777d..c7057fc47ba39 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -3615,49 +3615,71 @@ static u16 afk_command_tag(struct apple_epic_service *service, u8 slot) } /** - * afk_service_call_unanswered - whether a standard call is already queued unanswered + * 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 * - * True when the command queued last on @service is this call, with the same - * input, and it timed out with its answer still to come. The call then still - * waits in the firmware's queue with nothing queued behind it, and a copy - * would only queue behind it, to the same effect. The copy would also keep a - * command slot of its own until the firmware answers it, so a caller that - * repeats such a call to firmware that has stopped answering takes every slot, - * and the service then refuses all of its calls with -ENOSPC. + * When the command queued last on @service is this call, with the same input, + * it has gone unanswered and the TX ring still holds unread entries, only ring + * the doorbell again and return true: the call itself still 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. * - * Compact EPIC keeps its timed-out commands apart (compact_keep_orphans), and - * is not covered. + * 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_unanswered(struct apple_epic_service *service, u16 group, - u32 command, const void *data, size_t data_len) +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; unsigned long flags; - bool queued; + bool waits; + u32 wptr; 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. */ - queued = service->last_valid && test_bit(service->last_slot, service->cmd_map) && - cmd->tag == service->last_tag && !cmd->done && !cmd->completion && - cmd->type == EPIC_SUBTYPE_STD_SERVICE && call && - 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 && - (!data_len || !memcmp(call + 1, data, data_len)); + 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); spin_unlock_irqrestore(&service->lock, flags); - return queued; + 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->txbfr.rptr) != READ_ONCE(*ep->txbfr.wptr); + if (waits) + ep->tx_active++; + spin_unlock_irqrestore(&ep->lock, flags); + if (waits) { + wptr = le32_to_cpu(READ_ONCE(*ep->txbfr.wptr)); + 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, @@ -3861,8 +3883,6 @@ static int afk_send_command_common(struct apple_epic_service *service, u8 type, service->cmds[idx].done = false; service->cmds[idx].completion = &completion; service->last_slot = idx; - service->last_tag = tag; - service->last_valid = true; init_completion(&completion); spin_unlock_irqrestore(&service->lock, flags); diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index 9d098f2e21cb3..78a6fefe1c365 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -58,13 +58,10 @@ struct apple_epic_service { /* Legacy EPIC sequence; AFKV2 history belongs to the interface. */ u8 cmd_tag; /* - * The command queued last, see afk_service_call_unanswered(). A reused - * service has an empty cmd_map, so this names no command of the new - * instance (lock). + * 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. */ - u16 last_tag; u8 last_slot; - bool last_valid; spinlock_t lock; /* Pins held by owners and synchronous callers; protected by lock. */ unsigned int users; @@ -750,8 +747,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_unanswered(struct apple_epic_service *service, u16 group, - u32 command, const void *data, size_t data_len); +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, diff --git a/drivers/gpu/drm/apple/dptxep.c b/drivers/gpu/drm/apple/dptxep.c index e1981c8870cc4..4c718b6f9ef23 100644 --- a/drivers/gpu/drm/apple/dptxep.c +++ b/drivers/gpu/drm/apple/dptxep.c @@ -259,18 +259,17 @@ int dptxport_set_hpd_timeout(struct apple_epic_service *service, bool hpd, cmd.unk = cpu_to_le32(1); /* - * The same HPD level, queued last on the port, timed out and is still - * unanswered: the firmware takes none of the port's calls for now. A - * copy would queue behind it to the same effect, 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 otherwise - * take every slot: the port then refuses all its calls with -ENOSPC, - * the HPD of the next display among them. + * 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_unanswered(service, DCPDPTX_HOTPLUG_GROUP, - DCPDPTX_HOTPLUG_CMD, &cmd, sizeof(cmd))) { + 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 unanswered, not sent again\n", + "set_hpd(%d): the same request is still unread, only rang again\n", hpd); return -ETIMEDOUT; } diff --git a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-hpd.py b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-hpd.py index 7b5e5981f296f..f57153b12fa99 100644 --- a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-hpd.py +++ b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-hpd.py @@ -3,7 +3,7 @@ """Exercise the production DPTX HPD request with mocked service-call replies. Covers the legacy, AFK-v2 and compact EPIC replies, the 14.7 AV client close, -and a request that is not sent again while its timed-out copy is unanswered. +and a request that is not sent again while the same one still waits unread. """ import pathlib import re @@ -98,16 +98,16 @@ def function(source, name): assert(dcp == expected_dcp && service != NULL); cec_closes++; } -/* The HPD request queued last on the port timed out and is still unanswered. */ -static bool unanswered; -static unsigned int sends, unanswered_asked; -bool afk_service_call_unanswered(struct apple_epic_service *service, u16 group, +/* 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); - unanswered_asked++; - return unanswered; + 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, @@ -221,21 +221,21 @@ def function(source, name): } ep.compact = false; compact_status = 0; } - /* A copy of an unanswered request is not sent, and fails as the request did. */ + /* 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 = unanswered_asked; + unsigned int before = sends, asked = unread_asked; int ret; - unanswered = true; + unread = true; ret = dptxport_set_hpd_timeout(&service, hpd, 1000); - unanswered = false; - expect(ret == -ETIMEDOUT && sends == before && unanswered_asked == asked + 1, - "copy of an unanswered request not sent", hpd, ret); + 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, no copy of an unanswered request"); + 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; } ''' diff --git a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py index e9ee31a347874..dc8f8a215e3cf 100644 --- a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py +++ b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py @@ -2,7 +2,8 @@ # SPDX-License-Identifier: GPL-2.0-only OR MIT """Compile the real command sender against synthetic ring and DMA operations. -Covers AFK-v2, legacy reply-length and compact EPIC requests. +Covers AFK-v2, legacy reply-length and compact EPIC requests, and the repeat of +a call the firmware has not read. """ import pathlib import re @@ -83,6 +84,7 @@ def function(pattern, what): #define DCP_FIRMWARE_V_14_7 2 #define DCP_FIRMWARE_M4 3 #define EPIC_SUBTYPE_STD_SERVICE 0xc0 +#define EPIC_SERVICE_CALL_MAGIC 0x69706378 enum epic_type { EPIC_TYPE_NOTIFY = 0, EPIC_TYPE_COMMAND = 3, EPIC_TYPE_REPLY = 4 }; enum epic_category { EPIC_CAT_REPORT = 0x00, EPIC_CAT_NOTIFY = 0x10, @@ -109,7 +111,7 @@ def function(pattern, what): #define wait_for_completion_timeout(p,t) ((void)(t),*(p)) typedef u64 dma_addr_t; struct epic_cmd { u32 retcode; u64 rxbuf,txbuf; u32 rxlen,txlen; u8 rxcookie,txcookie; }; -struct cmd_info { +struct epic_cmd_info { u16 tag; u8 type; size_t reply_length; void *rxbuf,*txbuf; dma_addr_t rxbuf_dma,txbuf_dma; size_t rxlen,txlen; u32 retcode; size_t reply_len; bool reply_len_valid; @@ -136,14 +138,16 @@ def function(pattern, what): struct afk_ringbuffer txbfr,rxbfr; size_t ext26_command_bytes; unsigned int ext26_command_count; }; struct ops { const char *name; }; +struct epic_service_call { u8 _pad0[2]; u16 group; u32 command,data_len,magic; u8 _pad1[48]; } __attribute__((packed)); struct apple_epic_service { struct apple_dcp_afkep *ep; struct ops *ops; unsigned int channel; unsigned long cmd_map[1]; - int lock; unsigned int users; bool enabled,torndown; u8 cmd_tag; struct cmd_info cmds[16]; - u16 last_tag; u8 last_slot; bool last_valid; + int lock; unsigned int users; bool enabled,torndown; u8 cmd_tag; struct epic_cmd_info cmds[16]; + u8 last_slot; }; static struct apple_epic_service *active; static struct apple_dcp_afkep *active_ep; static unsigned int frees, sends; +static u64 last_doorbell; static int wire_fail, timeout, reply_status; static bool raw_doorbell_mode, inject_overflow_on_alloc, complete_before_doorbell_fail; static u32 tx_consumed_wptr; @@ -191,7 +195,7 @@ def function(pattern, what): { (void)i;(void)order;*map=0; } static void complete_active_command(struct apple_dcp_afkep *ep) { - struct cmd_info *c=&active->cmds[0]; + struct epic_cmd_info *c=&active->cmds[0]; if(real_reply) { struct epic_cmd reply={.retcode=reply_status,.rxlen=legacy_rxlen}; assert(!ep->wire_v2); @@ -215,7 +219,7 @@ def function(pattern, what): assert(dcp->afk_tx_lock); if(!raw_doorbell_mode) assert(active_ep->tx_lock && active_ep->lock); } - sends++; + sends++;last_doorbell=message; if(raw_doorbell_mode) return wire_fail; assert((message >> 48)==RBEP_SEND); @@ -432,6 +436,47 @@ def function(pattern, what): assert(afk_send_epic(&ep,4,0,EPIC_TYPE_NOTIFY,EPIC_CAT_REPORT,AFKV2_OPEN,NULL,0)==-ENOSPC); assert(tx_wptr==panel_wptr); puts("PASS: v27 panel OPEN exception preserves the independent transmit gates"); + + /* The repeat of a call the firmware has not read only rings again. */ + ep.wire_v2=ep.inline_v2=ep.tx_paused=ep.j516s_panel_service=false; + dcp.afk_tx_closed=dcp.afk_rx_failed=false;dcp.sleep_v27_closed=DCP_V27_TX_LIVE; + struct { struct epic_service_call call; u8 data[20]; u8 pad[28]; } low={0},high; + low.call=(struct epic_service_call){.group=8,.command=8,.data_len=32,.magic=EPIC_SERVICE_CALL_MAGIC}; + high=low;high.data[16]=1; + u8 result[112]; +#define RING(group,command,input) afk_service_call_ring_unread(&s,group,command,(input).data,sizeof((input).data)) +#define SEND(input) afk_send_command_timeout_len(&s,0xc0,&(input),sizeof(input),result,sizeof(result),&status,1000,NULL) + assert(sizeof(low)==112 && !RING(8,8,low)); + /* Delivered and timed out, then with its doorbell lost. */ + for(int lost=0;lost<2;lost++) { + struct epic_cmd late={0}; + tx_rptr=tx_wptr;wire_fail=lost?-ETIMEDOUT:0; + assert(SEND(low)==-ETIMEDOUT && s.cmd_map[0] && tx_rptr!=tx_wptr); + wire_fail=0; + u32 queued=tx_wptr;unsigned int rung=sends; + for(int i=0;i<32;i++) assert(RING(8,8,low)); + assert(sends==rung+32 && last_doorbell==((RBEP_SEND<<48)|queued)); + assert(tx_wptr==queued && s.cmd_map[0]==1 && !ep.tx_active && !ep.tx_lock); + /* Another level or call is left to the sender. */ + assert(!RING(8,8,high) && !RING(8,7,low) && !RING(0,8,low)); + /* So is one the sender refuses, with its own error. */ + ep.tx_paused=true;assert(!RING(8,8,low));ep.tx_paused=false; + ep.stopping=true;assert(!RING(8,8,low));ep.stopping=false; + dcp.quiescing=true;assert(!RING(8,8,low));dcp.quiescing=false; + dcp.crashed=true;assert(!RING(8,8,low));dcp.crashed=false; + s.enabled=false;assert(!RING(8,8,low));s.enabled=true; + ep.compact=true;assert(!RING(8,8,low));ep.compact=false; + /* Nor one its caller still waits for, or that is answered with the slot kept. */ + int waiter=0; + s.cmds[0].completion=&waiter;assert(!RING(8,8,low));s.cmds[0].completion=NULL; + s.cmds[0].done=true;assert(!RING(8,8,low));s.cmds[0].done=false; + /* Read without an answer, or answered: the next one is sent. */ + tx_rptr=tx_wptr;assert(!RING(8,8,low));tx_rptr=0;assert(tx_rptr!=tx_wptr && RING(8,8,low)); + assert(sends==rung+33); + afk_recv_handle_reply(&ep,4,s.cmds[0].tag,&late,sizeof(late)); + assert(!s.cmd_map[0] && !RING(8,8,low) && sends==rung+33); + } + puts("PASS: the repeat of an unread call rings again and takes no second slot"); puts("PASS: extracted production sender, transport boundary lock assertions, overflow injection, failed doorbell after publication, and ext26 budget"); } ''' @@ -440,11 +485,13 @@ def function(pattern, what): function(r"static int afk_tx_admit_locked", "TX admission check"), function(r"static int afk_send_raw_locked", "locked doorbell helper"), function(r"static int afk_send_ret", "doorbell helper"), + function(r"static void afk_send", "control doorbell"), function(r"static int afk_send_epic_token", "EPIC ring sender"), function(r"static int afk_send_epic_published", "published EPIC sender"), function(r"int afk_send_epic", "EPIC sender"), function(r"static u32 afk_compact_next_status", "compact status allocator"), function(r"static u16 afk_command_tag", "interface tag allocator"), + function(r"bool afk_service_call_ring_unread", "unread call check"), function(r"static int afk_send_command_common", "command sender"), function(r"static int afk_send_command_timeout_len", "length-aware sender"), function(r"int afk_send_command_with_reply_len", "legacy reply-length sender"), diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py index 41883c06c4b9f..fb3bba7f93eb2 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py @@ -91,7 +91,7 @@ def function(file,signature): struct apple_epic_service { struct apple_dcp_afkep *ep;const struct apple_epic_service_ops *ops; bool lock,enabled,torndown,defer_start,started;unsigned int users;u32 channel;u8 cmd_tag; - u16 last_tag;u8 last_slot;bool last_valid; + u8 last_slot; unsigned long cmd_map[1];struct epic_cmd_info cmds[MAX_PENDING_CMDS]; struct { u64 generation,epoch; } admission;void *cookie; bool pending_open;const char *pending_name,*pending_class;s64 pending_unit; From 444bc2b0e386d56c5ca38648ebc2441c7748caa8 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 18:36:15 -0500 Subject: [PATCH 5/8] drm/apple: afk: log the rings of every command that times out The ring pointers tell whether the firmware still reads its queue, but they were logged only for the T6030 external processor. The J316c stall was on a T600x one (firmware 13.5), where the log held nothing but timeouts. The snapshot reads host memory and RTKit's state, not the coprocessor, so log it for every processor. Signed-off-by: DjDeveloperr --- drivers/gpu/drm/apple/afk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index c7057fc47ba39..6207a3a609bbe 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -3946,7 +3946,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. */ From 8c1bd117f5676df738b9e5533252db153236ba53 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 19:35:26 -0500 Subject: [PATCH 6/8] drm/apple: afk: no second doorbell where the sender would refuse afk_service_call_ring_unread() leaves a call that the sender would refuse to the sender, but it checked only a part of what the sender checks. It also rang while the firmware-27 sleep gate (sleep_v27_closed) is closed, when nothing may reach the firmware, and read the ring pointers without checking that the ring is set up. Check both, as afk_send_epic_token() does. The test covers both, and that with both HPD levels unread only the one queued last is taken for a repeat. It passed before with last_slot never recorded, where the second HPD low of low, high, low is dropped. Signed-off-by: DjDeveloperr --- drivers/gpu/drm/apple/afk.c | 2 ++ .../drivers/gpu/apple-afkv2/extract-send.py | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 6207a3a609bbe..435f3cf9b1669 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -3666,6 +3666,8 @@ bool afk_service_call_ring_unread(struct apple_epic_service *service, u16 group, 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 && READ_ONCE(*ep->txbfr.rptr) != READ_ONCE(*ep->txbfr.wptr); if (waits) ep->tx_active++; diff --git a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py index dc8f8a215e3cf..8a9bb9878dd2b 100644 --- a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py +++ b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py @@ -190,9 +190,9 @@ def function(pattern, what): static void dma_free_coherent(void *dev,size_t n,void *p,dma_addr_t a) { (void)dev;(void)n;assert((uintptr_t)p==a);free(p);frees++; } static int bitmap_find_free_region(unsigned long *map,unsigned int n,int order) -{ (void)n;(void)order;assert(!*map);*map=1;return 0; } +{ assert(!order);for(unsigned int i=0;icmds[0]; @@ -464,6 +464,10 @@ def function(pattern, what): ep.stopping=true;assert(!RING(8,8,low));ep.stopping=false; dcp.quiescing=true;assert(!RING(8,8,low));dcp.quiescing=false; dcp.crashed=true;assert(!RING(8,8,low));dcp.crashed=false; + dcp.sleep_v27_closed=DCP_V27_TX_REPLIES;assert(!RING(8,8,low)); + dcp.sleep_v27_closed=DCP_V27_TX_PANEL_OPEN;assert(!RING(8,8,low)); + dcp.sleep_v27_closed=DCP_V27_TX_LIVE; + ep.txbfr.ready=false;assert(!RING(8,8,low));ep.txbfr.ready=true; s.enabled=false;assert(!RING(8,8,low));s.enabled=true; ep.compact=true;assert(!RING(8,8,low));ep.compact=false; /* Nor one its caller still waits for, or that is answered with the slot kept. */ @@ -476,6 +480,17 @@ def function(pattern, what): afk_recv_handle_reply(&ep,4,s.cmds[0].tag,&late,sizeof(late)); assert(!s.cmd_map[0] && !RING(8,8,low) && sends==rung+33); } + /* With both levels unread, only the one queued last is the port's: an edge is never swallowed. */ + tx_rptr=tx_wptr; + assert(SEND(low)==-ETIMEDOUT && RING(8,8,low) && !RING(8,8,high)); + assert(SEND(high)==-ETIMEDOUT && s.cmd_map[0]==3 && RING(8,8,high) && !RING(8,8,low)); + assert(SEND(low)==-ETIMEDOUT && s.cmd_map[0]==7 && RING(8,8,low) && !RING(8,8,high)); + /* The last one is answered: the one before it, the other level, is not taken for it. */ + struct epic_cmd answer={0}; + afk_recv_handle_reply(&ep,4,s.cmds[2].tag,&answer,sizeof(answer)); + assert(s.cmd_map[0]==3 && !RING(8,8,low) && !RING(8,8,high)); + for(int i=0;i<2;i++) afk_recv_handle_reply(&ep,4,s.cmds[i].tag,&answer,sizeof(answer)); + assert(!s.cmd_map[0]); puts("PASS: the repeat of an unread call rings again and takes no second slot"); puts("PASS: extracted production sender, transport boundary lock assertions, overflow injection, failed doorbell after publication, and ext26 budget"); } From e09a113e0dbbb2325decd111818dea415454d2f8 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 20:26:15 -0500 Subject: [PATCH 7/8] drm/apple: afk: ring again only for a call whose own entry is unread afk_service_call_ring_unread() took any unread entry in the endpoint's TX ring for the call's. If the firmware had read the HPD request without answering it, and another service's entry was still unread, the request was not sent again. Count the bytes the host has published in the ring, and note for each command where its entry ends. The entry is unread if fewer bytes follow it than the firmware has not read. Unlike a ring offset, that also tells it from one that lies in the same place a lap later. The test puts another service's entry behind the request, read and unread, at every place in the ring. Signed-off-by: DjDeveloperr --- drivers/gpu/drm/apple/afk.c | 47 ++++++++++++++----- drivers/gpu/drm/apple/afk.h | 5 ++ .../drivers/gpu/apple-afkv2/extract-send.py | 37 ++++++++++++++- .../selftests/drivers/gpu/apple-dcp/edid.py | 2 +- .../drivers/gpu/apple-dcp/recovery.py | 2 +- 5 files changed, 76 insertions(+), 17 deletions(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 435f3cf9b1669..fed22d02d3ba7 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -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) @@ -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, @@ -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; @@ -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) { @@ -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)) { @@ -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)); @@ -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); @@ -3623,8 +3633,8 @@ static u16 afk_command_tag(struct apple_epic_service *service, u8 slot) * @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 TX ring still holds unread entries, only ring - * the doorbell again and return true: the call itself still waits. A copy would + * 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. @@ -3638,9 +3648,10 @@ bool afk_service_call_ring_unread(struct apple_epic_service *service, u16 group, 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; - u32 wptr; + u64 end; if (!service || !service->ep || READ_ONCE(service->ep->compact)) return false; @@ -3659,6 +3670,7 @@ bool afk_service_call_ring_unread(struct apple_epic_service *service, u16 group, le16_to_cpu(call->group) == group && le32_to_cpu(call->command) == command && !memcmp(call + 1, data, data_len); + end = cmd->tx_end; spin_unlock_irqrestore(&service->lock, flags); if (!waits) return false; @@ -3667,13 +3679,19 @@ bool afk_service_call_ring_unread(struct apple_epic_service *service, u16 group, 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 && - READ_ONCE(*ep->txbfr.rptr) != READ_ONCE(*ep->txbfr.wptr); + 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) { - wptr = le32_to_cpu(READ_ONCE(*ep->txbfr.wptr)); afk_send(ep, FIELD_PREP(RBEP_TYPE, RBEP_SEND) | FIELD_PREP(SEND_WPTR, wptr)); spin_lock_irqsave(&ep->lock, flags); @@ -3734,7 +3752,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; @@ -3883,6 +3901,7 @@ 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); @@ -3907,6 +3926,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; diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index 78a6fefe1c365..ac96943698600 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -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; @@ -601,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. */ diff --git a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py index 8a9bb9878dd2b..d491e753b0997 100644 --- a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py +++ b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py @@ -115,7 +115,7 @@ def function(pattern, what): u16 tag; u8 type; size_t reply_length; void *rxbuf,*txbuf; dma_addr_t rxbuf_dma,txbuf_dma; size_t rxlen,txlen; u32 retcode; size_t reply_len; bool reply_len_valid; - bool done,free_on_ack; int *completion; + bool done,free_on_ack; int *completion; u64 tx_end; }; struct apple_dcp { void *dev; int fw_compat; @@ -134,7 +134,7 @@ def function(pattern, what): struct apple_dcp *dcp; bool wire_v2,inline_v2,compact,stopping,tx_paused,rx_paused,rx_failed; bool j516s_panel_service; - int endpoint,lock,tx_lock; unsigned int tx_active; u16 qe_seq; u8 *command_tags; + int endpoint,lock,tx_lock; unsigned int tx_active; u16 qe_seq; u64 tx_published; u8 *command_tags; struct afk_ringbuffer txbfr,rxbfr; size_t ext26_command_bytes; unsigned int ext26_command_count; }; struct ops { const char *name; }; @@ -480,6 +480,38 @@ def function(pattern, what): afk_recv_handle_reply(&ep,4,s.cmds[0].tag,&late,sizeof(late)); assert(!s.cmd_map[0] && !RING(8,8,low) && sends==rung+33); } + /* Read without an answer, and the unread entry behind it is another's: the next one is sent. */ +#define OTHER() assert(!afk_send_epic(&ep,5,0,EPIC_TYPE_NOTIFY,EPIC_CAT_REPORT,1,note,sizeof(note))) + struct epic_cmd gone={0};u8 note[4]={0}; + tx_rptr=tx_wptr; + assert(SEND(low)==-ETIMEDOUT && RING(8,8,low)); + u32 behind=tx_wptr; + OTHER(); + unsigned int before=sends; + assert(RING(8,8,low) && sends==before+1 && last_doorbell==((RBEP_SEND<<48)|tx_wptr)); + tx_rptr=behind; + assert(tx_rptr!=tx_wptr && !RING(8,8,low) && sends==before+1); + /* Nor is the entry that comes to lie where it was, a lap of the ring later. */ + for(u32 at=tx_wptr,lap=0;lapat?tx_wptr-at:sizeof(tx_ring_buf)-at+tx_wptr; + assert(!RING(8,8,low)); + } + assert(sends>before+1 && s.cmd_map[0]==1); + afk_recv_handle_reply(&ep,4,s.cmds[0].tag,&gone,sizeof(gone)); + assert(!s.cmd_map[0]); + /* The same wherever in the ring the entries lie, around its end too. */ + for(int i=0;i<64;i++) { + tx_rptr=tx_wptr; + assert(SEND(low)==-ETIMEDOUT);behind=tx_wptr; + assert(RING(8,8,low)); + OTHER(); + assert(RING(8,8,low)); + tx_rptr=behind;assert(tx_rptr!=tx_wptr && !RING(8,8,low)); + tx_rptr=tx_wptr;assert(!RING(8,8,low)); + afk_recv_handle_reply(&ep,4,s.cmds[0].tag,&gone,sizeof(gone)); + assert(!s.cmd_map[0]); + } /* With both levels unread, only the one queued last is the port's: an edge is never swallowed. */ tx_rptr=tx_wptr; assert(SEND(low)==-ETIMEDOUT && RING(8,8,low) && !RING(8,8,high)); @@ -501,6 +533,7 @@ def function(pattern, what): function(r"static int afk_send_raw_locked", "locked doorbell helper"), function(r"static int afk_send_ret", "doorbell helper"), function(r"static void afk_send", "control doorbell"), + function(r"static void afk_tx_published_locked", "TX publish count"), function(r"static int afk_send_epic_token", "EPIC ring sender"), function(r"static int afk_send_epic_published", "published EPIC sender"), function(r"int afk_send_epic", "EPIC sender"), diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py index fb3bba7f93eb2..369199cb2630d 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/edid.py @@ -87,7 +87,7 @@ def function(file,signature): void afk_service_retry(struct apple_dcp_afkep *ep); /* open_later: a firmware 27 panel service that sends its own OPEN; none is here. */ struct apple_epic_service_ops { char name[32];bool reusable,optional,open_later,singleton;void (*init)(struct apple_epic_service *,const char *,const char *,s64);int (*prepare)(struct apple_epic_service *,const char *,const char *,s64);int (*revalidate)(struct apple_epic_service *,const char *,const char *,s64);void (*init_props)(struct apple_epic_service *,s64,const void *,size_t);void (*teardown)(struct apple_epic_service *); }; -struct epic_cmd_info { u16 tag;u8 type;size_t reply_length,reply_len;bool reply_len_valid;void *rxbuf,*txbuf;dma_addr_t rxbuf_dma,txbuf_dma;size_t rxlen,txlen;u32 retcode;bool done,free_on_ack;int *completion; }; +struct epic_cmd_info { u16 tag;u8 type;size_t reply_length,reply_len;bool reply_len_valid;void *rxbuf,*txbuf;dma_addr_t rxbuf_dma,txbuf_dma;size_t rxlen,txlen;u64 tx_end;u32 retcode;bool done,free_on_ack;int *completion; }; struct apple_epic_service { struct apple_dcp_afkep *ep;const struct apple_epic_service_ops *ops; bool lock,enabled,torndown,defer_start,started;unsigned int users;u32 channel;u8 cmd_tag; diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py b/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py index 94529dbc719a9..0a07f9531e6b7 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/recovery.py @@ -111,7 +111,7 @@ def extract(text,signature): } '''+marker) unit=unit.replace('static void afk_v2_callback(', - 'static int afk_send_epic_published(struct apple_dcp_afkep *e,u32 channel,u16 tag,int type,int category,u8 subtype,const void *p,size_t n,bool *published)\n' + 'static int afk_send_epic_published(struct apple_dcp_afkep *e,u32 channel,u16 tag,int type,int category,u8 subtype,const void *p,size_t n,u64 *published)\n' '{ int r=afk_send_epic(e,channel,tag,type,category,subtype,p,n); if(published) *published=!r; return r; }\n' 'static void afk_v2_callback(') From 0a1facafabb9b7ba6a82d4c90cccbeadbb512b06 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Thu, 3 Dec 2026 07:29:06 -0500 Subject: [PATCH 8/8] drm/apple: afk: read an unread call's ring position only once its sender is done The retry helper read tx_end under service->lock, but a call that is still in its sender has it written without that lock. Read it only when the slot was given up, which happens under the lock. Signed-off-by: DjDeveloperr --- drivers/gpu/drm/apple/afk.c | 3 ++- .../drivers/gpu/apple-afkv2/extract-send.py | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index fed22d02d3ba7..95262e56dff5b 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -3670,7 +3670,8 @@ bool afk_service_call_ring_unread(struct apple_epic_service *service, u16 group, le16_to_cpu(call->group) == group && le32_to_cpu(call->command) == command && !memcmp(call + 1, data, data_len); - end = cmd->tx_end; + /* 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; diff --git a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py index d491e753b0997..c88f71c1f6472 100644 --- a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py +++ b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-send.py @@ -115,8 +115,12 @@ def function(pattern, what): u16 tag; u8 type; size_t reply_length; void *rxbuf,*txbuf; dma_addr_t rxbuf_dma,txbuf_dma; size_t rxlen,txlen; u32 retcode; size_t reply_len; bool reply_len_valid; - bool done,free_on_ack; int *completion; u64 tx_end; + bool done,free_on_ack; int *completion; struct { u64 end; } tx[1]; }; +/* A retry must leave tx_end alone while the first call's sender, who writes it unlocked, has the slot. */ +static bool retrying; +static int tx_end_index(void) { assert(!retrying); return 0; } +#define tx_end tx[tx_end_index()].end struct apple_dcp { void *dev; int fw_compat; bool quiescing,external,crashed,ext26,afk_tx_closed,afk_rx_failed; @@ -156,6 +160,9 @@ def function(pattern, what): static bool real_reply; static u32 legacy_rxlen; static void afk_recv_handle_reply(struct apple_dcp_afkep *ep,u32 channel,u16 tag,void *payload,size_t payload_size); +/* The input of a call to retry from its own doorbell, before its sender has gone on. */ +static const u8 *retry; +bool afk_service_call_ring_unread(struct apple_epic_service *s,u16 group,u32 command,const void *data,size_t len); /* Compact EPIC goes inline through the ring; this mock stands for that transport. */ static bool compact_echo; static u32 compact_reply,compact_request; @@ -220,6 +227,10 @@ def function(pattern, what): if(!raw_doorbell_mode) assert(active_ep->tx_lock && active_ep->lock); } sends++;last_doorbell=message; + if(retry) { + retrying=true;assert(!afk_service_call_ring_unread(active,8,8,retry,20)); + retrying=false;retry=NULL; + } if(raw_doorbell_mode) return wire_fail; assert((message >> 48)==RBEP_SEND); @@ -450,8 +461,8 @@ def function(pattern, what): /* Delivered and timed out, then with its doorbell lost. */ for(int lost=0;lost<2;lost++) { struct epic_cmd late={0}; - tx_rptr=tx_wptr;wire_fail=lost?-ETIMEDOUT:0; - assert(SEND(low)==-ETIMEDOUT && s.cmd_map[0] && tx_rptr!=tx_wptr); + tx_rptr=tx_wptr;wire_fail=lost?-ETIMEDOUT:0;retry=low.data; + assert(SEND(low)==-ETIMEDOUT && !retry && s.cmd_map[0] && tx_rptr!=tx_wptr); wire_fail=0; u32 queued=tx_wptr;unsigned int rung=sends; for(int i=0;i<32;i++) assert(RING(8,8,low));