diff --git a/arch/arm64/configs/asahi.config b/arch/arm64/configs/asahi.config index 9921de094ba9f..3173021d4f19d 100644 --- a/arch/arm64/configs/asahi.config +++ b/arch/arm64/configs/asahi.config @@ -88,6 +88,7 @@ CONFIG_DRM_ASAHI_M3_T8122=y CONFIG_DRM_ADP=m CONFIG_DRM_APPLE=m CONFIG_DRM_APPLE_DCP_T8132=y +CONFIG_DRM_APPLE_CEC=y CONFIG_DRM_APPLE_AUDIO=y CONFIG_DRM_APPLE_DCP_T6030_AUDIO=y CONFIG_DRM_ACCEL=y diff --git a/drivers/gpu/drm/apple/Kconfig b/drivers/gpu/drm/apple/Kconfig index 3f09ef36c656d..99632ffe1535c 100644 --- a/drivers/gpu/drm/apple/Kconfig +++ b/drivers/gpu/drm/apple/Kconfig @@ -21,10 +21,21 @@ config DRM_APPLE depends on BACKLIGHT_CLASS_DEVICE select MULTIPLEXER select DRM_APPLE_FABRIC_CORE + select CEC_CORE if DRM_APPLE_CEC help Say Y if you have an Apple Silicon chipset. This includes the Type-C display fabric and Thunderbolt display routing. +config DRM_APPLE_CEC + bool "HDMI CEC support" + default y + depends on DRM_APPLE + help + Enable HDMI Consumer Electronics Control (CEC) support on Apple + Silicon machines with a built-in HDMI port via the compact EPIC + dcpav-cec-interface-epic service. Say Y here to expose a standard + Linux /dev/cecX adapter bound to the built-in HDMI connector. + config DRM_APPLE_AUDIO bool "DisplayPort/HDMI Audio support" default y diff --git a/drivers/gpu/drm/apple/Makefile b/drivers/gpu/drm/apple/Makefile index 47d6d6ff5e22d..a6687d7cccd65 100644 --- a/drivers/gpu/drm/apple/Makefile +++ b/drivers/gpu/drm/apple/Makefile @@ -5,6 +5,7 @@ CFLAGS_trace.o = -I$(src) appledrm-y := apple_drv.o apple_resume.o appledrm-y += afk.o dcp.o dcp-fabric.o dcp_backlight.o dptxep.o iomfb.o parser.o systemep.o appledrm-y += dcp-hdmi.o +appledrm-$(CONFIG_DRM_APPLE_CEC) += cec.o appledrm-$(CONFIG_DRM_APPLE_AUDIO) += audio.o appledrm-$(CONFIG_DRM_APPLE_AUDIO) += av.o appledrm-y += connector.o dcp-panel-adt.o dcp-display-metadata.o dcp-180hz.o @@ -47,6 +48,7 @@ obj-$(CONFIG_DRM_APPLE_KUNIT_TEST) += dcp-indicator-test.o obj-$(CONFIG_DRM_APPLE_KUNIT_TEST) += dcp-announce-test.o obj-$(CONFIG_DRM_APPLE_KUNIT_TEST) += afk-service-test.o +obj-$(CONFIG_DRM_APPLE_KUNIT_TEST) += dcp_cec_test.o obj-$(CONFIG_DRM_APPLE_PARSER_KUNIT_TEST) += afkv2-test.o diff --git a/drivers/gpu/drm/apple/afk-parser-test.c b/drivers/gpu/drm/apple/afk-parser-test.c index 94bbea976b85e..62dfe90798723 100644 --- a/drivers/gpu/drm/apple/afk-parser-test.c +++ b/drivers/gpu/drm/apple/afk-parser-test.c @@ -103,6 +103,85 @@ static void afk_serialized_report_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, afk_compact_report_dictionary(dictionary, sizeof(dictionary))); } +struct compact_call_report_capture { + unsigned int count; + size_t size; + u16 group; + u32 command; + u32 magic; + u16 pa; +}; + +static int compact_call_report_cb(struct apple_epic_service *service, + enum epic_subtype type, + const void *data, size_t data_size) +{ + struct compact_call_report_capture *cap = service->cookie; + const struct epic_service_call *call = data; + + cap->count++; + cap->size = data_size; + if (data_size >= sizeof(*call) + sizeof(u16)) { + cap->group = le16_to_cpu(call->group); + cap->command = le32_to_cpu(call->command); + cap->magic = le32_to_cpu(call->magic); + cap->pa = get_unaligned_le16((const u8 *)(call + 1)); + } + return 0; +} + +static const struct apple_epic_service_ops compact_call_report_ops = { + .name = "compact-report-test", + .report = compact_call_report_cb, +}; + +static void afk_compact_service_call_report_test(struct kunit *test) +{ + struct apple_dcp_afkep *ep = test->priv; + struct compact_call_report_capture cap = {}; + u8 wire[16 + sizeof(struct epic_compact_desc) + + sizeof(struct epic_service_call) + 0x50] = {}; + struct epic_compact_sub_hdr *sub = (void *)(wire + 8); + struct epic_service_call *call; + + ep->services[0].ops = &compact_call_report_ops; + ep->services[0].cookie = ∩ + sub->category = 0; + sub->type = EPIC_SUBTYPE_STD_SERVICE; + + /* The bare service-call header immediately follows the subheader. */ + call = (void *)(sub + 1); + call->group = cpu_to_le16(2); + call->command = cpu_to_le32(4); + call->data_len = cpu_to_le32(0x50); + call->magic = cpu_to_le32(EPIC_SERVICE_CALL_MAGIC); + put_unaligned_le16(0x2100, (u8 *)(call + 1)); + + afk_recv_compact_message(ep, 1, wire, + 16 + sizeof(*call) + 0x50); + KUNIT_EXPECT_EQ(test, cap.count, 1U); + KUNIT_EXPECT_EQ(test, cap.size, sizeof(*call) + 0x50); + KUNIT_EXPECT_EQ(test, cap.magic, (u32)EPIC_SERVICE_CALL_MAGIC); + KUNIT_EXPECT_EQ(test, cap.group, 2); + KUNIT_EXPECT_EQ(test, cap.command, 4U); + KUNIT_EXPECT_EQ(test, cap.pa, 0x2100); + + /* Descriptor-wrapped struct epic_service_call report still strips 8 bytes. */ + memset(wire + 16, 0, sizeof(wire) - 16); + call = (void *)((u8 *)(sub + 1) + sizeof(struct epic_compact_desc)); + call->group = cpu_to_le16(2); + call->command = cpu_to_le32(4); + call->data_len = cpu_to_le32(0x50); + call->magic = cpu_to_le32(EPIC_SERVICE_CALL_MAGIC); + put_unaligned_le16(0x3400, (u8 *)(call + 1)); + + afk_recv_compact_message(ep, 1, wire, sizeof(wire)); + KUNIT_EXPECT_EQ(test, cap.count, 2U); + KUNIT_EXPECT_EQ(test, cap.size, sizeof(*call) + 0x50); + KUNIT_EXPECT_EQ(test, cap.magic, (u32)EPIC_SERVICE_CALL_MAGIC); + KUNIT_EXPECT_EQ(test, cap.pa, 0x3400); +} + /* * Slot reuse for re-announced compact services. The firmware re-announces a * service under a fresh interface on every DP relink without closing the old @@ -210,6 +289,7 @@ static void afk_compact_siblings_kept_test(struct kunit *test) static struct kunit_case afk_parser_cases[] = { KUNIT_CASE(afk_serialized_report_test), + KUNIT_CASE(afk_compact_service_call_report_test), KUNIT_CASE(afk_compact_header_test), KUNIT_CASE(afk_short_epic_test), KUNIT_CASE(afk_short_reply_test), diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index c081f57761543..7b4a2124a7968 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -476,6 +476,14 @@ static bool afk_compact_capable(struct apple_dcp_afkep *ep) ep->endpoint == AV_AUDIO_ENDPOINT_DESKTOP); } +bool afk_compact_av_capable(struct apple_dcp *dcp) +{ + return (dcp->fw_compat == DCP_FIRMWARE_V_26_6_DESKTOP && + dcp->desktop_external) || + dcp->hw.iomfb_method_profile == DCP_IOMFB_METHODS_H17P_EXTERNAL || + dcp_is_v27_external(dcp); +} + static u32 afk_compact_next_status(struct apple_epic_service *service) { unsigned long flags; @@ -1199,6 +1207,18 @@ static bool afk_compact_report_dictionary(const void *payload, size_t size) ((get_unaligned_le32(payload + sizeof(u32)) >> 24) & 0x1f) == 1; } +/* + * A bare compact report starts with the service-call header. Its magic + * is at the usual offset from the start of the payload. + */ +static bool afk_compact_report_service_call(const void *payload, size_t size) +{ + const struct epic_service_call *call = payload; + + return size >= sizeof(*call) && + get_unaligned_le32(&call->magic) == EPIC_SERVICE_CALL_MAGIC; +} + static void afk_recv_compact_message(struct apple_dcp_afkep *ep, u16 intf_id, const void *body, size_t size) { @@ -1221,10 +1241,11 @@ static void afk_recv_compact_message(struct apple_dcp_afkep *ep, u16 intf_id, return; } if (sub->category == 0) { - /* Preserve bare dictionaries; command-style reports have a descriptor. */ + /* Preserve bare dictionaries and EPIC call headers; others carry a descriptor. */ if (sub->type == EPIC_SUBTYPE_STD_SERVICE && payload_size >= sizeof(struct epic_compact_desc) && - !afk_compact_report_dictionary(payload, payload_size)) { + !afk_compact_report_dictionary(payload, payload_size) && + !afk_compact_report_service_call(payload, payload_size)) { payload += sizeof(struct epic_compact_desc); payload_size -= sizeof(struct epic_compact_desc); } diff --git a/drivers/gpu/drm/apple/afk.h b/drivers/gpu/drm/apple/afk.h index f1be4fc0082d4..b2c7c5deddb29 100644 --- a/drivers/gpu/drm/apple/afk.h +++ b/drivers/gpu/drm/apple/afk.h @@ -241,6 +241,23 @@ static inline void afk_service_retire(struct apple_epic_service *service) spin_unlock_irqrestore(&service->lock, flags); } +/* + * Retire @service only if it still serves @channel; a singleton slot reused + * in place for a later announcement keeps its new channel enabled. + */ +static inline void afk_service_retire_channel(struct apple_epic_service *service, + u32 channel) +{ + unsigned long flags; + + spin_lock_irqsave(&service->lock, flags); + if (service->channel == channel) { + service->enabled = false; + service->torndown = true; + } + spin_unlock_irqrestore(&service->lock, flags); +} + static inline bool afk_service_matches(struct apple_epic_service *service, u32 channel) { @@ -595,6 +612,7 @@ static inline void afk_ep_init_state(struct apple_dcp_afkep *ep) struct apple_dcp_afkep *afk_init(struct apple_dcp *dcp, u32 endpoint, const struct apple_epic_service_ops *ops); +bool afk_compact_av_capable(struct apple_dcp *dcp); int afk_v2_copy_owned(struct apple_epic_service *service, u64 dva, void *buffer, size_t size, bool to_peer); int afk_start(struct apple_dcp_afkep *ep); diff --git a/drivers/gpu/drm/apple/av.c b/drivers/gpu/drm/apple/av.c index 9f34030bd209e..c7a298af8ecc0 100644 --- a/drivers/gpu/drm/apple/av.c +++ b/drivers/gpu/drm/apple/av.c @@ -19,6 +19,7 @@ #include "audio.h" #include "afk.h" #include "av.h" +#include "cec.h" #include "dcp.h" #include "dcp-internal.h" @@ -994,19 +995,7 @@ static const struct apple_epic_service_ops av_desktop_ops[] = { .report = av_audiosrv_report, .teardown = av_audiosrv_teardown, }, - { - /* - * The same endpoint also publishes the processor's CEC - * interface, cut to "dcpav-cec-interface-ep" as above. The - * host makes no use of CEC: take it and drop its reports. - */ - .name = "dcpav-cec-interface-epic", - .compact_full = "dcpav-cec-interface-epic:0", - .singleton = true, - .init = av_cec_init, - .report = av_cec_report, - .teardown = av_cec_teardown, - }, + DCP_CEC_COMPACT_SERVICE_OPS, { /* * And its video interface, cut to "dcpav-video-interface-" on diff --git a/drivers/gpu/drm/apple/cec-proto.h b/drivers/gpu/drm/apple/cec-proto.h new file mode 100644 index 0000000000000..8c263b18021c5 --- /dev/null +++ b/drivers/gpu/drm/apple/cec-proto.h @@ -0,0 +1,237 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* + * Wire protocol for the compact EPIC CEC service ("dcpav-cec-interface-epic", + * unit 0) on RTKit endpoint 0x2b (AV_AUDIO_ENDPOINT_DESKTOP). + * + * The following layouts apply to endpoint 0x2b on T6041 (J616c, firmware + * 27.2). Offsets are relative to the start of the message body. + * + * Group 0 calls: + * 4 Open (0x20 B): u32 status at +0x00, u64 opened at +0x10. + * 5 Close (0x10 B): u32 status at +0x00. + * 6 Set logical addresses (0x20 B): u16 mask at +0x10. + * 7 Read EDID attributes (0x50 B): __le16 physical address at +0x00. + * 9 Transmit (0x40 B): up to 16 frame bytes at +0x00, length minus one + * at +0x10, retry count (0..5) at +0x20, u32 reply status at +0x30. + * + * Group 2 reports carry a struct epic_service_call header: + * 1 Terminate (0x20 B). + * 2 Receive (0x20 B): up to 16 frame bytes at +0x00, length minus one + * in the low nibble at +0x10. + * 3 HPD (0x10 B): u8 connected at +0x00. + * 4 EDID attributes (0x50 B): __le16 physical address at +0x00. + * + * The driver only decodes this layout on endpoint 0x2b. The report layout + * on endpoint 0x29 (14.x firmware) still needs a hardware capture. + */ +#ifndef __APPLE_DCP_CEC_PROTO_H__ +#define __APPLE_DCP_CEC_PROTO_H__ + +#include +#include +#include +#include +#include +#include +#include + +#include "afk.h" + +#define DCP_CEC_GROUP_CALL 0 +#define DCP_CEC_GROUP_REPORT 2 + +/* Group 0 commands and body sizes */ +#define DCP_CEC_CMD_OPEN 4 +#define DCP_CEC_OPEN_LEN 0x20 + +#define DCP_CEC_CMD_CLOSE 5 +#define DCP_CEC_CLOSE_LEN 0x10 + +#define DCP_CEC_CMD_SET_LOGICAL_ADDR_MASK 6 +#define DCP_CEC_SET_LA_MASK_LEN 0x20 + +#define DCP_CEC_CMD_GET_EDID_ATTRS 7 +#define DCP_CEC_EDID_ATTRS_LEN 0x50 + +#define DCP_CEC_CMD_SEND_CEC 9 +#define DCP_CEC_SEND_LEN 0x40 + +/* Group 2 async reports and body sizes */ +#define DCP_CEC_RPT_TERMINATE 1 +#define DCP_CEC_RPT_TERMINATE_LEN 0x20 + +#define DCP_CEC_RPT_RX_FRAME 2 +#define DCP_CEC_RPT_RX_FRAME_LEN 0x20 + +#define DCP_CEC_RPT_HPD 3 +#define DCP_CEC_RPT_HPD_LEN 0x10 + +#define DCP_CEC_RPT_EDID_ATTRS 4 +#define DCP_CEC_RPT_EDID_ATTRS_LEN 0x50 + +/* + * Timeouts (ms): synchronous AFK transmit call (7500 ms), driver completion + * guard window (8500 ms), and CEC core transfer watchdog (9500 ms). Open runs + * outside tx_work, so tx_work spends at most DCP_CEC_TX_TIMEOUT_MS in AFK. + */ +#define DCP_CEC_TX_TIMEOUT_MS 7500 +#define DCP_CEC_TX_COMPLETE_GUARD_MS 8500 +#define DCP_CEC_ADAP_XFER_TIMEOUT_MS 9500 + +/* Transmit reply status at body offset +0x30 */ +#define DCP_CEC_RET_OK 0x00000000U +#define DCP_CEC_RET_NACK 0xe0118000U +#define DCP_CEC_RET_LINE_ERR 0xe0118001U +#define DCP_CEC_RET_TIMEOUT 0xe00002d6U +#define DCP_CEC_RET_OFFLINE 0xe00002d7U +#define DCP_CEC_RET_ABORTED 0xe00002ebU + +struct dcp_cec_parsed_report { + u32 cmd; + bool hpd; + u16 phys_addr; + struct cec_msg rx_msg; +}; + +static inline int dcp_cec_decode_open_resp(const u8 resp[DCP_CEC_OPEN_LEN]) +{ + u32 status = get_unaligned_le32(resp + 0x00); + u64 opened = get_unaligned_le64(resp + 0x10); + + return (status == 0 && opened != 0) ? 0 : -EIO; +} + +static inline int dcp_cec_decode_status_resp(const u8 *resp) +{ + return get_unaligned_le32(resp + 0x00) == 0 ? 0 : -EIO; +} + +/* + * Whenever CEC is enabled, OR in BIT(CEC_LOG_ADDR_BROADCAST) (0x8000) so + * the adapter receives frames addressed to all logical devices. + */ +static inline u16 dcp_cec_encode_la_mask(bool enabled, u16 la_mask) +{ + if (!enabled) + return 0x0000; + + return (la_mask & GENMASK(14, 0)) | BIT(CEC_LOG_ADDR_BROADCAST); +} + +static inline void dcp_cec_build_la_mask_req(u8 req[DCP_CEC_SET_LA_MASK_LEN], + bool enabled, u16 la_mask) +{ + memset(req, 0, DCP_CEC_SET_LA_MASK_LEN); + put_unaligned_le16(dcp_cec_encode_la_mask(enabled, la_mask), + req + 0x10); +} + +static inline u16 dcp_cec_decode_edid_attrs(const u8 resp[DCP_CEC_EDID_ATTRS_LEN]) +{ + return get_unaligned_le16(resp + 0x00); +} + +static inline int dcp_cec_build_send_req(u8 req[DCP_CEC_SEND_LEN], + const struct cec_msg *msg, + u8 attempts) +{ + if (!msg || msg->len < 1 || msg->len > CEC_MAX_MSG_SIZE) + return -EINVAL; + + memset(req, 0, DCP_CEC_SEND_LEN); + memcpy(req + 0x00, msg->msg, msg->len); + req[0x10] = (u8)((msg->len - 1) & 0x0f); + req[0x20] = min_t(u8, 5, attempts ? attempts - 1 : 0); + return 0; +} + +static inline u8 dcp_cec_tx_status(int afk_ret, u32 retcode) +{ + if (afk_ret < 0) + return CEC_TX_STATUS_ERROR | CEC_TX_STATUS_MAX_RETRIES; + + switch (retcode) { + case DCP_CEC_RET_OK: + return CEC_TX_STATUS_OK; + case DCP_CEC_RET_NACK: + return CEC_TX_STATUS_NACK | CEC_TX_STATUS_MAX_RETRIES; + case DCP_CEC_RET_LINE_ERR: + return CEC_TX_STATUS_ERROR | CEC_TX_STATUS_MAX_RETRIES; + case DCP_CEC_RET_TIMEOUT: + return CEC_TX_STATUS_TIMEOUT | CEC_TX_STATUS_MAX_RETRIES; + case DCP_CEC_RET_OFFLINE: + case DCP_CEC_RET_ABORTED: + return CEC_TX_STATUS_ABORTED; + default: + return CEC_TX_STATUS_ERROR | CEC_TX_STATUS_MAX_RETRIES; + } +} + +static inline u8 dcp_cec_decode_send_resp(int afk_ret, + const u8 resp[DCP_CEC_SEND_LEN]) +{ + if (afk_ret < 0) + return dcp_cec_tx_status(afk_ret, 0); + + return dcp_cec_tx_status(0, get_unaligned_le32(resp + 0x30)); +} + +static inline int dcp_cec_parse_report(const void *data, size_t data_size, + struct dcp_cec_parsed_report *out) +{ + const struct epic_service_call *call = data; + const u8 *body; + size_t body_avail; + u32 cmd, data_len; + u8 rx_len; + + if (!data || !out || data_size < sizeof(*call)) + return -EMSGSIZE; + + if (le32_to_cpu(call->magic) != EPIC_SERVICE_CALL_MAGIC || + le16_to_cpu(call->group) != DCP_CEC_GROUP_REPORT) + return -EINVAL; + + cmd = le32_to_cpu(call->command); + data_len = le32_to_cpu(call->data_len); + body = (const u8 *)(call + 1); + body_avail = data_size - sizeof(*call); + if (data_len > body_avail) + return -EMSGSIZE; + + memset(out, 0, sizeof(*out)); + out->cmd = cmd; + out->phys_addr = CEC_PHYS_ADDR_INVALID; + + switch (cmd) { + case DCP_CEC_RPT_TERMINATE: + if (body_avail < DCP_CEC_RPT_TERMINATE_LEN || + (data_len && data_len < DCP_CEC_RPT_TERMINATE_LEN)) + return -EMSGSIZE; + return 0; + case DCP_CEC_RPT_RX_FRAME: + if (body_avail < DCP_CEC_RPT_RX_FRAME_LEN || + (data_len && data_len < DCP_CEC_RPT_RX_FRAME_LEN)) + return -EMSGSIZE; + rx_len = (body[0x10] & 0x0f) + 1; + out->rx_msg.len = rx_len; + memcpy(out->rx_msg.msg, body + 0x00, rx_len); + return 0; + case DCP_CEC_RPT_HPD: + if (body_avail < DCP_CEC_RPT_HPD_LEN || + (data_len && data_len < DCP_CEC_RPT_HPD_LEN)) + return -EMSGSIZE; + out->hpd = body[0x00] != 0; + return 0; + case DCP_CEC_RPT_EDID_ATTRS: + if (body_avail < DCP_CEC_RPT_EDID_ATTRS_LEN || + (data_len && data_len < DCP_CEC_RPT_EDID_ATTRS_LEN)) + return -EMSGSIZE; + out->phys_addr = get_unaligned_le16(body + 0x00); + return 0; + default: + return -EOPNOTSUPP; + } +} + +#endif /* __APPLE_DCP_CEC_PROTO_H__ */ diff --git a/drivers/gpu/drm/apple/cec.c b/drivers/gpu/drm/apple/cec.c new file mode 100644 index 0000000000000..ce1f02cb28da1 --- /dev/null +++ b/drivers/gpu/drm/apple/cec.c @@ -0,0 +1,1196 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Apple Silicon DCP HDMI-CEC adapter driver + * + * Drives the compact EPIC "dcpav-cec-interface-epic" service on RTKit endpoint + * 0x2b (AV_AUDIO_ENDPOINT_DESKTOP) for built-in HDMI ports behind an on-board + * DisplayPort-to-HDMI converter with CEC tunneling over AUX. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "afk.h" +#include "cec.h" +#include "connector.h" +#include "dcp.h" +#include "dcp-internal.h" + +#define DCP_CEC_RX_RING_SIZE 32 + +struct dcp_cec_rx_item { + struct cec_msg msg; + ktime_t ts; +}; + +struct dcp_cec { + struct apple_dcp *dcp; + struct cec_adapter *adap; + struct apple_dcp_afkep *owned_avep; + bool avep_cleanup_registered; + + /* + * Serializes Open and Close RPCs across lifecycle_work, adap_enable, + * and HPD-low close. Never take adap->lock inside open_lock. + */ + struct mutex open_lock; + + /* Leaf lock: protects service state, LA mask, flags, and queued TX. */ + spinlock_t lock; + struct apple_epic_service *srv; + u64 gen; + u64 opened_gen; + u64 open_attempted_gen; + u64 close_gen; + u64 terminate_gen; + u32 terminate_chan; + u16 la_mask; + bool enabled; + bool rx_enabled; + bool stopping; + bool terminate_pending; + bool pa_invalidate_pending; + bool hpd_event_pending; + bool hpd_event_val; + bool pa_event_pending; + u16 pa_event_val; + bool poll_pa_pending; + + u64 tx_seq; + u64 tx_active_seq; + unsigned long tx_started; + bool tx_pending; + struct cec_msg tx_msg; + u8 tx_attempts; + + /* Leaf lock: protects received frames queued from ep->wq to rx_work. */ + spinlock_t rx_lock; + struct dcp_cec_rx_item rx_ring[DCP_CEC_RX_RING_SIZE]; + unsigned int rx_head; + unsigned int rx_tail; + unsigned int rx_count; + + struct work_struct lifecycle_work; + struct work_struct tx_work; + struct work_struct rx_work; +#if IS_ENABLED(CONFIG_DRM_APPLE_KUNIT_TEST) + int (*test_call)(struct apple_epic_service *srv, u32 cmd, + const void *req, void *resp, size_t body_len, + unsigned int timeout_ms); +#endif +}; + +static int dcp_cec_call(struct apple_epic_service *srv, u32 cmd, + const void *req, void *resp, size_t body_len, + unsigned int timeout_ms) +{ + struct apple_dcp *dcp; + + if (!srv || !srv->ep) + return -ENODEV; + + dcp = srv->ep->dcp; + if (!dcp || READ_ONCE(dcp->crashed) || READ_ONCE(dcp->quiescing) || + READ_ONCE(dcp->sleep_v27_closed)) + return -ESHUTDOWN; + +#if IS_ENABLED(CONFIG_DRM_APPLE_KUNIT_TEST) + if (dcp->cec && dcp->cec->test_call) + return dcp->cec->test_call(srv, cmd, req, resp, body_len, + timeout_ms); +#endif + + return afk_service_call_timeout_len(srv, DCP_CEC_GROUP_CALL, cmd, + req, body_len, 0, resp, body_len, 0, + timeout_ms, NULL); +} + +static int dcp_cec_cmd_open(struct apple_epic_service *srv) +{ + u8 req[DCP_CEC_OPEN_LEN] = {}; + u8 resp[DCP_CEC_OPEN_LEN] = {}; + int ret; + + ret = dcp_cec_call(srv, DCP_CEC_CMD_OPEN, req, resp, sizeof(req), + MSEC_PER_SEC); + if (ret) + return ret; + + return dcp_cec_decode_open_resp(resp); +} + +static int dcp_cec_cmd_close(struct apple_epic_service *srv) +{ + u8 req[DCP_CEC_CLOSE_LEN] = {}; + u8 resp[DCP_CEC_CLOSE_LEN] = {}; + int ret; + + ret = dcp_cec_call(srv, DCP_CEC_CMD_CLOSE, req, resp, sizeof(req), + MSEC_PER_SEC); + if (ret == -ENODEV) + return 0; + if (ret) + return ret; + + return dcp_cec_decode_status_resp(resp); +} + +static int dcp_cec_cmd_set_la_mask(struct apple_epic_service *srv, + bool enabled, u16 la_mask) +{ + u8 req[DCP_CEC_SET_LA_MASK_LEN]; + u8 resp[DCP_CEC_SET_LA_MASK_LEN] = {}; + int ret; + + dcp_cec_build_la_mask_req(req, enabled, la_mask); + ret = dcp_cec_call(srv, DCP_CEC_CMD_SET_LOGICAL_ADDR_MASK, req, resp, + sizeof(req), MSEC_PER_SEC); + if (ret) + return ret; + + return dcp_cec_decode_status_resp(resp); +} + +static int dcp_cec_cmd_get_edid_attrs(struct apple_epic_service *srv, + u16 *phys_addr) +{ + u8 req[DCP_CEC_EDID_ATTRS_LEN] = {}; + u8 resp[DCP_CEC_EDID_ATTRS_LEN] = {}; + int ret; + + ret = dcp_cec_call(srv, DCP_CEC_CMD_GET_EDID_ATTRS, req, resp, + sizeof(req), MSEC_PER_SEC); + if (ret) + return ret; + + *phys_addr = dcp_cec_decode_edid_attrs(resp); + return 0; +} + +static u8 dcp_cec_cmd_send(struct apple_epic_service *srv, + const struct cec_msg *msg, u8 attempts) +{ + u8 req[DCP_CEC_SEND_LEN]; + u8 resp[DCP_CEC_SEND_LEN] = {}; + int ret; + + ret = dcp_cec_build_send_req(req, msg, attempts); + if (ret) + return CEC_TX_STATUS_ERROR | CEC_TX_STATUS_MAX_RETRIES; + + ret = dcp_cec_call(srv, DCP_CEC_CMD_SEND_CEC, req, resp, sizeof(req), + DCP_CEC_TX_TIMEOUT_MS); + return dcp_cec_decode_send_resp(ret, resp); +} + +static bool dcp_cec_gen_current(struct dcp_cec *cec, + struct apple_epic_service *srv, u64 gen) +{ + guard(spinlock_irqsave)(&cec->lock); + return !cec->stopping && cec->srv == srv && cec->gen == gen; +} + +/* + * Ensure @srv (at @gen) has received Open. Returns 1 when newly opened, 0 when + * already open, -ECANCELED if closed or terminated while opening (closing the + * newly opened client again), or negative errno on failure / stale generation. + * Caller must NOT acquire adap->lock inside open_lock. + */ +static int dcp_cec_ensure_open(struct dcp_cec *cec, + struct apple_epic_service *srv, u64 gen) +{ + bool cancel_close = false; + u64 cgen; + int ret; + + guard(mutex)(&cec->open_lock); + + scoped_guard(spinlock_irqsave, &cec->lock) { + if (cec->stopping || cec->srv != srv || cec->gen != gen) + return -ESTALE; + if (cec->opened_gen == gen) + return 0; + cgen = cec->close_gen; + } + + ret = dcp_cec_cmd_open(srv); + if (ret) { + dev_warn(cec->dcp->dev, "CEC: failed to open EPIC service: %d\n", + ret); + return ret; + } + + scoped_guard(spinlock_irqsave, &cec->lock) { + if (cec->srv == srv && cec->gen == gen && + !cec->stopping && cec->close_gen == cgen) { + cec->opened_gen = gen; + } else { + /* An instance replaced meanwhile took its open with it. */ + if (cec->srv == srv && cec->gen == gen) + cancel_close = true; + ret = -ECANCELED; + } + } + + if (cancel_close) + dcp_cec_cmd_close(srv); + + return ret ?: 1; +} + +static void dcp_cec_close_client(struct dcp_cec *cec, bool queue_invalidate) +{ + struct apple_epic_service *srv = NULL; + bool wake = false; + + scoped_guard(spinlock_irqsave, &cec->lock) { + cec->close_gen++; + cec->tx_seq++; + cec->tx_pending = false; + if (cec->srv && cec->opened_gen == cec->gen) { + srv = afk_service_get(cec->srv); + cec->opened_gen = 0; + } + if (queue_invalidate && !cec->stopping) { + cec->pa_event_pending = false; + cec->poll_pa_pending = false; + cec->pa_invalidate_pending = true; + wake = true; + } + } + + if (srv) { + scoped_guard(mutex, &cec->open_lock) + dcp_cec_cmd_close(srv); + afk_service_put(srv); + } + + if (wake) + queue_work(system_dfl_wq, &cec->lifecycle_work); +} + +static void dcp_cec_complete_tx(struct cec_adapter *adap, u8 status) +{ + /* + * cec_transmit_attempt_done_ts() only accepts OK, ARB_LOST, NACK, + * LOW_DRIVE and ERROR; report TIMEOUT and ABORTED via cec_transmit_done. + */ + if (status & (CEC_TX_STATUS_TIMEOUT | CEC_TX_STATUS_ABORTED)) + cec_transmit_done(adap, status | CEC_TX_STATUS_MAX_RETRIES, + 0, 0, 0, 0); + else + cec_transmit_attempt_done(adap, status); +} + +static void dcp_cec_tx_work(struct work_struct *work) +{ + struct dcp_cec *cec = container_of(work, struct dcp_cec, tx_work); + + while (!READ_ONCE(cec->stopping)) { + struct cec_adapter *adap = READ_ONCE(cec->adap); + struct apple_epic_service *srv = NULL; + struct cec_msg msg; + unsigned long started = 0; + bool is_open = false; + bool complete = false; + u8 attempts = 0; + u64 seq = 0; + u8 status; + + scoped_guard(spinlock_irqsave, &cec->lock) { + if (cec->stopping || !cec->tx_pending || !cec->enabled) { + cec->tx_pending = false; + return; + } + msg = cec->tx_msg; + attempts = cec->tx_attempts; + seq = cec->tx_active_seq; + started = cec->tx_started; + cec->tx_pending = false; + srv = afk_service_get(cec->srv); + is_open = srv && cec->opened_gen == cec->gen; + } + + if (!srv) + status = CEC_TX_STATUS_ERROR | CEC_TX_STATUS_MAX_RETRIES; + else if (!is_open) + status = CEC_TX_STATUS_ABORTED; + else + status = dcp_cec_cmd_send(srv, &msg, attempts); + + if (srv) + afk_service_put(srv); + + scoped_guard(spinlock_irqsave, &cec->lock) { + unsigned long guard_jiffies = + msecs_to_jiffies(DCP_CEC_TX_COMPLETE_GUARD_MS); + + complete = adap && !cec->stopping && cec->enabled && + cec->tx_seq == seq && + time_before(jiffies, started + guard_jiffies); + } + + if (complete) + dcp_cec_complete_tx(adap, status); + } +} + +static void dcp_cec_rx_work(struct work_struct *work) +{ + struct dcp_cec *cec = container_of(work, struct dcp_cec, rx_work); + struct cec_adapter *adap; + + for (;;) { + struct dcp_cec_rx_item item; + bool have_item = false; + + scoped_guard(spinlock_irqsave, &cec->rx_lock) { + if (cec->rx_count > 0) { + item = cec->rx_ring[cec->rx_head]; + cec->rx_head = (cec->rx_head + 1) % + DCP_CEC_RX_RING_SIZE; + cec->rx_count--; + have_item = true; + } + } + + if (!have_item) + break; + + adap = READ_ONCE(cec->adap); + if (adap && !READ_ONCE(cec->stopping) && + READ_ONCE(cec->rx_enabled)) + cec_received_msg_ts(adap, &item.msg, item.ts); + } +} + +static void dcp_cec_lifecycle_open(struct dcp_cec *cec, + struct cec_adapter *adap, + struct apple_epic_service *srv, u64 gen) +{ + bool is_enabled = false; + bool has_newer_pa = false; + u16 la_mask = 0; + u16 pa = CEC_PHYS_ADDR_INVALID; + int ret; + + ret = dcp_cec_ensure_open(cec, srv, gen); + if (ret < 0) { + if (ret == -ESTALE || ret == -ECANCELED) + return; + scoped_guard(spinlock_irqsave, &cec->lock) { + if (cec->gen == gen) + cec->open_attempted_gen = gen; + } + return; + } + + scoped_guard(spinlock_irqsave, &cec->lock) { + if (!cec->stopping && cec->srv == srv && cec->gen == gen) { + is_enabled = cec->enabled; + la_mask = cec->la_mask; + } + } + + if (is_enabled) + dcp_cec_cmd_set_la_mask(srv, true, la_mask); + + if (!adap || !dcp_cec_gen_current(cec, srv, gen)) + return; + + /* + * HPD status can still read low on plug. Query the physical address + * directly. If EDID is not ready, the EDID attributes report or the + * poll after the hotplug EDID read can supply the address later. + */ + if (dcp_cec_cmd_get_edid_attrs(srv, &pa) != 0 || + pa == CEC_PHYS_ADDR_INVALID) + return; + + scoped_guard(spinlock_irqsave, &cec->lock) + has_newer_pa = cec->pa_event_pending || + cec->pa_invalidate_pending; + if (!has_newer_pa && dcp_cec_gen_current(cec, srv, gen)) + cec_s_phys_addr(adap, pa, false); +} + +static void dcp_cec_lifecycle_work(struct work_struct *work) +{ + struct dcp_cec *cec = container_of(work, struct dcp_cec, lifecycle_work); + + while (!READ_ONCE(cec->stopping)) { + struct cec_adapter *adap = READ_ONCE(cec->adap); + struct apple_epic_service *term_srv = NULL; + struct apple_epic_service *srv = NULL; + u32 term_chan = 0; + bool term_was_open = false; + bool do_pa_inv = false; + bool do_open = false; + bool do_hpd = false; + bool hpd_val = false; + bool do_pa = false; + u16 pa_val = CEC_PHYS_ADDR_INVALID; + bool do_poll_pa = false; + u64 gen = 0; + + scoped_guard(spinlock_irqsave, &cec->lock) { + if (cec->stopping) + return; + + if (cec->terminate_pending) { + cec->terminate_pending = false; + if (cec->srv && cec->gen == cec->terminate_gen) { + term_srv = cec->srv; + term_chan = cec->terminate_chan; + term_was_open = cec->opened_gen == cec->gen; + cec->srv = NULL; + cec->opened_gen = 0; + cec->gen++; + } + } else if (cec->pa_invalidate_pending) { + cec->pa_invalidate_pending = false; + do_pa_inv = true; + } else if (cec->srv && cec->opened_gen != cec->gen && + cec->open_attempted_gen != cec->gen) { + cec->open_attempted_gen = cec->gen; + srv = afk_service_get(cec->srv); + gen = cec->gen; + do_open = !!srv; + } else if (cec->hpd_event_pending) { + cec->hpd_event_pending = false; + hpd_val = cec->hpd_event_val; + srv = afk_service_get(cec->srv); + gen = cec->gen; + do_hpd = true; + } else if (cec->pa_event_pending) { + cec->pa_event_pending = false; + pa_val = cec->pa_event_val; + do_pa = true; + } else if (cec->poll_pa_pending) { + cec->poll_pa_pending = false; + srv = afk_service_get(cec->srv); + gen = cec->gen; + do_poll_pa = !!srv; + } + } + + if (term_srv) { + bool invalidate = false; + + if (term_was_open) { + scoped_guard(mutex, &cec->open_lock) + dcp_cec_cmd_close(term_srv); + } + afk_service_retire_channel(term_srv, term_chan); + afk_service_put(term_srv); + + scoped_guard(spinlock_irqsave, &cec->lock) + invalidate = !cec->srv && !cec->stopping; + if (invalidate && adap) + cec_phys_addr_invalidate(adap); + continue; + } + + if (do_pa_inv) { + if (adap && !READ_ONCE(cec->stopping)) + cec_phys_addr_invalidate(adap); + continue; + } + + if (do_open) { + dcp_cec_lifecycle_open(cec, adap, srv, gen); + afk_service_put(srv); + continue; + } + + if (do_hpd) { + u16 pa = CEC_PHYS_ADDR_INVALID; + + if (!hpd_val) { + dcp_cec_close_client(cec, false); + if (adap && !READ_ONCE(cec->stopping)) + cec_phys_addr_invalidate(adap); + } else if (srv && dcp_cec_ensure_open(cec, srv, gen) >= 0 && + dcp_cec_cmd_get_edid_attrs(srv, &pa) == 0) { + if (adap && dcp_cec_gen_current(cec, srv, gen) && + pa != CEC_PHYS_ADDR_INVALID) + cec_s_phys_addr(adap, pa, false); + } + if (srv) + afk_service_put(srv); + continue; + } + + if (do_pa) { + if (adap && !READ_ONCE(cec->stopping)) + cec_s_phys_addr(adap, pa_val, false); + continue; + } + + if (do_poll_pa) { + u16 pa = CEC_PHYS_ADDR_INVALID; + + if (adap && + READ_ONCE(adap->phys_addr) == CEC_PHYS_ADDR_INVALID && + dcp_cec_ensure_open(cec, srv, gen) >= 0 && + dcp_cec_cmd_get_edid_attrs(srv, &pa) == 0 && + pa != CEC_PHYS_ADDR_INVALID && + dcp_cec_gen_current(cec, srv, gen)) + cec_s_phys_addr(adap, pa, false); + afk_service_put(srv); + continue; + } + + break; + } +} + +static int dcp_cec_adap_enable(struct cec_adapter *adap, bool enable) +{ + struct dcp_cec *cec = cec_get_drvdata(adap); + struct apple_epic_service *srv = NULL; + u64 gen = 0; + int ret = 0; + + scoped_guard(spinlock_irqsave, &cec->rx_lock) { + cec->rx_head = 0; + cec->rx_tail = 0; + cec->rx_count = 0; + } + + scoped_guard(spinlock_irqsave, &cec->lock) { + if (cec->stopping || cec->adap != adap) + return enable ? -ENODEV : 0; + cec->la_mask = 0; + WRITE_ONCE(cec->enabled, enable); + WRITE_ONCE(cec->rx_enabled, enable); + if (!enable) { + cec->tx_seq++; + cec->tx_pending = false; + } + srv = afk_service_get(cec->srv); + gen = cec->gen; + } + + if (!srv) + return 0; + + if (enable) { + ret = dcp_cec_ensure_open(cec, srv, gen); + if (ret >= 0) + ret = dcp_cec_cmd_set_la_mask(srv, true, 0); + /* + * If the service was concurrently replaced or closed, + * lifecycle_work will open the successor and set the mask. + */ + if (ret < 0 && + (ret == -ECANCELED || !dcp_cec_gen_current(cec, srv, gen))) + ret = 0; + } else { + /* CEC core wraps adap_enable(adap, false) in WARN_ON(). */ + if (READ_ONCE(cec->opened_gen) == gen) + dcp_cec_cmd_set_la_mask(srv, false, 0); + ret = 0; + } + + afk_service_put(srv); + return enable ? ret : 0; +} + +static int dcp_cec_adap_log_addr(struct cec_adapter *adap, u8 logical_addr) +{ + struct dcp_cec *cec = cec_get_drvdata(adap); + struct apple_epic_service *srv = NULL; + bool enabled = false; + u16 la_mask = 0; + u64 gen = 0; + int ret = 0; + + scoped_guard(spinlock_irqsave, &cec->lock) { + if (cec->stopping || cec->adap != adap) + return logical_addr == CEC_LOG_ADDR_INVALID ? + 0 : -ENODEV; + if (logical_addr == CEC_LOG_ADDR_INVALID) + cec->la_mask = 0; + else if (logical_addr < CEC_LOG_ADDR_BROADCAST) + cec->la_mask |= BIT(logical_addr); + la_mask = cec->la_mask; + enabled = cec->enabled; + srv = afk_service_get(cec->srv); + gen = cec->gen; + } + + if (!srv) + return 0; + + if (logical_addr == CEC_LOG_ADDR_INVALID) { + /* CEC core wraps adap_log_addr(INVALID) in WARN_ON(). */ + if (enabled && READ_ONCE(cec->opened_gen) == gen) + dcp_cec_cmd_set_la_mask(srv, true, 0); + afk_service_put(srv); + return 0; + } + + ret = dcp_cec_ensure_open(cec, srv, gen); + if (ret >= 0) + ret = dcp_cec_cmd_set_la_mask(srv, true, la_mask); + if (ret < 0 && + (ret == -ECANCELED || !dcp_cec_gen_current(cec, srv, gen))) + ret = 0; + + afk_service_put(srv); + return ret; +} + +static int dcp_cec_adap_transmit(struct cec_adapter *adap, u8 attempts, + u32 signal_free_time, struct cec_msg *msg) +{ + struct dcp_cec *cec = cec_get_drvdata(adap); + + /* Called under adap->lock: never block or call cec_transmit_* here. */ + scoped_guard(spinlock_irqsave, &cec->lock) { + if (cec->stopping || cec->adap != adap) + return -ENODEV; + cec->tx_active_seq = ++cec->tx_seq; + cec->tx_started = jiffies; + cec->tx_msg = *msg; + cec->tx_attempts = attempts; + cec->tx_pending = true; + } + + queue_work(system_dfl_wq, &cec->tx_work); + return 0; +} + +static const struct cec_adap_ops dcp_cec_adap_ops = { + .adap_enable = dcp_cec_adap_enable, + .adap_log_addr = dcp_cec_adap_log_addr, + .adap_transmit = dcp_cec_adap_transmit, +}; + +static void dcp_cec_devm_release(void *data) +{ + struct dcp_cec *cec = data; + struct apple_epic_service *srv = NULL; + + dcp_cec_unregister(cec->dcp); + + scoped_guard(spinlock_irqsave, &cec->lock) { + srv = cec->srv; + cec->srv = NULL; + cec->opened_gen = 0; + cec->gen++; + } + if (srv) + afk_service_put(srv); + + if (cec->dcp->cec == cec) + cec->dcp->cec = NULL; +} + +static struct dcp_cec *dcp_cec_ensure_state(struct apple_dcp *dcp) +{ + struct dcp_cec *cec; + int ret; + + if (!dcp || dcp->fixed_connector_type != DRM_MODE_CONNECTOR_HDMIA) + return NULL; + if (dcp->cec) + return dcp->cec; + + cec = devm_kzalloc(dcp->dev, sizeof(*cec), GFP_KERNEL); + if (!cec) + return NULL; + + cec->dcp = dcp; + mutex_init(&cec->open_lock); + spin_lock_init(&cec->lock); + spin_lock_init(&cec->rx_lock); + INIT_WORK(&cec->lifecycle_work, dcp_cec_lifecycle_work); + INIT_WORK(&cec->tx_work, dcp_cec_tx_work); + INIT_WORK(&cec->rx_work, dcp_cec_rx_work); + + ret = devm_add_action_or_reset(dcp->dev, dcp_cec_devm_release, cec); + if (ret) + return NULL; + + dcp->cec = cec; + return cec; +} + +/* + * Only compact endpoint 0x2b publishes dcpav-cec-interface-epic (13.x/14.x use + * 0x29 and do not get an adapter). Firmware 27.x announces the interface only + * once HPD rises, so register as soon as 0x2b is live to let CEC_CAP_NEEDS_HPD + * clients configure /dev/cecN before a sink is plugged in. + */ +static bool dcp_cec_has_endpoint(struct apple_dcp *dcp) +{ + if (!IS_ERR_OR_NULL(dcp->avep) && + dcp->avep->endpoint == AV_AUDIO_ENDPOINT_DESKTOP) + return true; + return !!READ_ONCE(dcp->sleep_v27_ep[AV_AUDIO_ENDPOINT_DESKTOP - 0x20]); +} + +int dcp_cec_register(struct apple_dcp *dcp) +{ + struct cec_connector_info conn_info; + struct cec_adapter *adap; + struct dcp_cec *cec; + bool need_lifecycle = false; + int ret; + + if (!dcp || dcp->fixed_connector_type != DRM_MODE_CONNECTOR_HDMIA || + !dcp->fixed_connector || !dcp_cec_has_endpoint(dcp)) + return 0; + + cec = dcp_cec_ensure_state(dcp); + if (!cec) + return -ENOMEM; + if (READ_ONCE(cec->adap)) + return 0; + + scoped_guard(spinlock_irqsave, &cec->rx_lock) { + cec->rx_head = 0; + cec->rx_tail = 0; + cec->rx_count = 0; + } + + scoped_guard(spinlock_irqsave, &cec->lock) { + cec->stopping = false; + cec->enabled = false; + cec->rx_enabled = false; + cec->la_mask = 0; + cec->tx_pending = false; + /* Re-open a pinned service across component unbind/rebind. */ + cec->opened_gen = 0; + cec->open_attempted_gen = 0; + need_lifecycle = !!cec->srv; + } + + adap = cec_allocate_adapter(&dcp_cec_adap_ops, cec, dev_name(dcp->dev), + CEC_CAP_DEFAULTS | CEC_CAP_CONNECTOR_INFO | + CEC_CAP_NEEDS_HPD, CEC_MAX_LOG_ADDRS); + if (IS_ERR(adap)) + return PTR_ERR(adap); + + adap->xfer_timeout_ms = DCP_CEC_ADAP_XFER_TIMEOUT_MS; + cec_fill_conn_info_from_drm(&conn_info, &dcp->fixed_connector->base); + cec_s_conn_info(adap, &conn_info); + + WRITE_ONCE(cec->adap, adap); + ret = cec_register_adapter(adap, dcp->dev); + if (ret) { + WRITE_ONCE(cec->adap, NULL); + cec_delete_adapter(adap); + dev_warn(dcp->dev, "CEC: failed to register adapter: %d\n", ret); + return ret; + } + + if (need_lifecycle) + queue_work(system_dfl_wq, &cec->lifecycle_work); + + return 0; +} + +void dcp_cec_unregister(struct apple_dcp *dcp) +{ + struct apple_epic_service *srv = NULL; + struct cec_adapter *adap; + struct dcp_cec *cec; + + if (!dcp || !dcp->cec) + return; + + cec = dcp->cec; + scoped_guard(spinlock_irqsave, &cec->lock) { + cec->stopping = true; + cec->rx_enabled = false; + cec->tx_seq++; + cec->tx_pending = false; + cec->close_gen++; + cec->terminate_pending = false; + cec->pa_invalidate_pending = false; + cec->hpd_event_pending = false; + cec->pa_event_pending = false; + cec->poll_pa_pending = false; + if (cec->srv && cec->opened_gen == cec->gen) { + srv = afk_service_get(cec->srv); + cec->opened_gen = 0; + } + } + + /* + * Drain all works before unregistering the adapter so no worker can + * touch a freed struct cec_adapter. Keep cec->srv pinned across + * component unbind so a rebind re-opens the live service without + * waiting for a hotplug relink. + */ + cancel_work_sync(&cec->tx_work); + cancel_work_sync(&cec->lifecycle_work); + cancel_work_sync(&cec->rx_work); + + if (srv) { + scoped_guard(mutex, &cec->open_lock) + dcp_cec_cmd_close(srv); + afk_service_put(srv); + } + + adap = READ_ONCE(cec->adap); + if (adap) { + WRITE_ONCE(cec->adap, NULL); + cec_unregister_adapter(adap); + } +} + +void dcp_cec_close_port(struct apple_dcp *dcp, + struct apple_epic_service *port_srv) +{ + if (!dcp || !dcp->cec || !port_srv || + dcp->fixed_connector_type != DRM_MODE_CONNECTOR_HDMIA || + port_srv != READ_ONCE(dcp->dptxport[0].service)) + return; + + dcp_cec_close_client(dcp->cec, true); +} + +void dcp_cec_poll_edid(struct apple_dcp *dcp) +{ + struct dcp_cec *cec; + bool wake = false; + + if (!dcp || !dcp->cec || + dcp->fixed_connector_type != DRM_MODE_CONNECTOR_HDMIA) + return; + + cec = dcp->cec; + scoped_guard(spinlock_irqsave, &cec->lock) { + if (!cec->stopping && cec->srv) { + cec->poll_pa_pending = true; + wake = true; + } + } + + if (wake) + queue_work(system_dfl_wq, &cec->lifecycle_work); +} + +bool dcp_cec_busy(struct apple_dcp *dcp) +{ + struct dcp_cec *cec; + + if (!dcp || !dcp->cec) + return false; + + cec = dcp->cec; + return work_busy(&cec->lifecycle_work) || work_busy(&cec->tx_work); +} + +void dcp_cec_service_init(struct apple_epic_service *service, const char *name, + const char *class, s64 unit) +{ + struct apple_dcp *dcp = service->ep->dcp; + struct apple_epic_service *old = NULL; + struct dcp_cec *cec; + bool queue = false; + + if (!dcp || dcp->fixed_connector_type != DRM_MODE_CONNECTOR_HDMIA || + READ_ONCE(dcp->active_typec_route) || + service->ep->endpoint != AV_AUDIO_ENDPOINT_DESKTOP) + return; + + cec = dcp_cec_ensure_state(dcp); + if (!cec) + return; + + /* + * Start the compact interface on ep->wq before publishing cec->srv so + * lifecycle_work or adap_enable cannot send Open ahead of compact 0x12. + */ +#if IS_ENABLED(CONFIG_DRM_APPLE_KUNIT_TEST) + if (!cec->test_call && afk_start_service(service)) + return; +#else + if (afk_start_service(service)) + return; +#endif + + service = afk_service_get(service); + if (!service) + return; + + scoped_guard(spinlock_irqsave, &cec->lock) { + old = cec->srv; + cec->srv = service; + cec->gen++; + cec->opened_gen = 0; + cec->open_attempted_gen = 0; + cec->terminate_pending = false; + cec->pa_invalidate_pending = false; + queue = !cec->stopping && !!cec->adap; + } + + if (old) + afk_service_put(old); + + if (queue) + queue_work(system_dfl_wq, &cec->lifecycle_work); +} + +int dcp_cec_service_report(struct apple_epic_service *service, + enum epic_subtype type, + const void *data, size_t data_size) +{ + struct apple_dcp *dcp = service->ep->dcp; + struct dcp_cec *cec = dcp->cec; + struct dcp_cec_parsed_report rpt; + bool queue_lifecycle = false; + bool queue_rx = false; + ktime_t now; + int ret; + + if (!cec || READ_ONCE(cec->stopping) || + service->ep->endpoint != AV_AUDIO_ENDPOINT_DESKTOP) + return 0; + + ret = dcp_cec_parse_report(data, data_size, &rpt); + if (ret) + return 0; + + switch (rpt.cmd) { + case DCP_CEC_RPT_TERMINATE: + scoped_guard(spinlock_irqsave, &cec->lock) { + if (!cec->stopping && cec->srv == service) { + cec->terminate_pending = true; + cec->terminate_gen = cec->gen; + cec->terminate_chan = READ_ONCE(service->channel); + cec->close_gen++; + cec->tx_seq++; + cec->tx_pending = false; + queue_lifecycle = true; + } + } + break; + + case DCP_CEC_RPT_RX_FRAME: + if (!READ_ONCE(cec->rx_enabled)) + return 0; + scoped_guard(spinlock_irqsave, &cec->lock) { + if (cec->stopping || cec->srv != service || + !cec->rx_enabled) + return 0; + } + now = ktime_get(); + scoped_guard(spinlock_irqsave, &cec->rx_lock) { + if (cec->rx_count == DCP_CEC_RX_RING_SIZE) { + /* Drop oldest unread frame on ring overflow. */ + cec->rx_head = (cec->rx_head + 1) % + DCP_CEC_RX_RING_SIZE; + cec->rx_count--; + } + cec->rx_ring[cec->rx_tail].msg = rpt.rx_msg; + cec->rx_ring[cec->rx_tail].ts = now; + cec->rx_tail = (cec->rx_tail + 1) % + DCP_CEC_RX_RING_SIZE; + cec->rx_count++; + queue_rx = true; + } + break; + + case DCP_CEC_RPT_HPD: + scoped_guard(spinlock_irqsave, &cec->lock) { + if (!cec->stopping && cec->srv == service) { + cec->hpd_event_pending = true; + cec->hpd_event_val = rpt.hpd; + if (!rpt.hpd) { + cec->close_gen++; + cec->tx_seq++; + cec->tx_pending = false; + cec->pa_event_pending = false; + cec->poll_pa_pending = false; + } + queue_lifecycle = true; + } + } + break; + + case DCP_CEC_RPT_EDID_ATTRS: + scoped_guard(spinlock_irqsave, &cec->lock) { + if (!cec->stopping && cec->srv == service) { + cec->pa_invalidate_pending = false; + cec->pa_event_pending = true; + cec->pa_event_val = rpt.phys_addr; + queue_lifecycle = true; + } + } + break; + + default: + break; + } + + if (queue_lifecycle) + queue_work(system_dfl_wq, &cec->lifecycle_work); + if (queue_rx) + queue_work(system_dfl_wq, &cec->rx_work); + + return 0; +} + +void dcp_cec_service_teardown(struct apple_epic_service *service) +{ + struct apple_dcp *dcp = service->ep->dcp; + struct dcp_cec *cec = dcp->cec; + bool owned = false; + bool queue_inv = false; + + afk_service_disable(service); + if (!cec) + return; + + /* + * Drop the owner pin synchronously and never send an async Close + * here: afk_compact_slot() reuses singleton slots in place right after + * ops->teardown(). + */ + scoped_guard(spinlock_irqsave, &cec->lock) { + if (cec->srv == service) { + cec->srv = NULL; + cec->opened_gen = 0; + cec->close_gen++; + cec->tx_seq++; + cec->tx_pending = false; + cec->terminate_pending = false; + cec->gen++; + owned = true; + if (!cec->stopping) { + cec->pa_invalidate_pending = true; + queue_inv = true; + } + } + } + + if (owned) + afk_service_put(service); + if (queue_inv) + queue_work(system_dfl_wq, &cec->lifecycle_work); +} + +static void dcp_cec_unused_init(struct apple_epic_service *service, + const char *name, const char *class, s64 unit) +{ +} + +static int dcp_cec_unused_report(struct apple_epic_service *service, + enum epic_subtype type, + const void *data, size_t data_size) +{ + return 0; +} + +static void dcp_cec_unused_teardown(struct apple_epic_service *service) +{ + afk_service_disable(service); +} + +static const struct apple_epic_service_ops dcp_cec_desktop_ops[] = { + { + .name = "dcpav-audio-interface-epic", + .compact_full = "dcpav-audio-interface-epic:0", + .singleton = true, + .init = dcp_cec_unused_init, + .report = dcp_cec_unused_report, + .teardown = dcp_cec_unused_teardown, + }, + DCP_CEC_COMPACT_SERVICE_OPS, + { + .name = "dcpav-video-interface-epic", + .compact_full = "dcpav-video-interface-epic:0", + .singleton = true, + .init = dcp_cec_unused_init, + .report = dcp_cec_unused_report, + .teardown = dcp_cec_unused_teardown, + }, + {} +}; + +static void dcp_cec_avep_cleanup(void *data) +{ + struct dcp_cec *cec = data; + struct apple_dcp *dcp = cec->dcp; + struct apple_dcp_afkep *ep = cec->owned_avep; + + if (IS_ERR_OR_NULL(ep)) + return; + + afk_quiesce(ep); + if (dcp->fw_compat == DCP_FIRMWARE_V_26_6_DESKTOP && + dcp->desktop && + READ_ONCE(dcp->desktop->graph[ep->endpoint - 0x20]) == ep) + WRITE_ONCE(dcp->desktop->graph[ep->endpoint - 0x20], NULL); + if (dcp->avep == ep) + dcp->avep = NULL; + cec->owned_avep = NULL; +} + +/* + * Start compact endpoint 0x2b on HDMI DCPs when audio did not start dcp->avep + * (e.g. appledrm.hdmi_audio=0 or !CONFIG_DRM_APPLE_AUDIO). dcp_start() and + * dcp_start_desktop_external() are one-shot per device bind, and + * avep_cleanup_registered guards against registering duplicate devm actions. + */ +int dcp_cec_init_avep(struct apple_dcp *dcp) +{ + struct apple_dcp_afkep *ep; + struct dcp_cec *cec; + int ret; + + if (!dcp || dcp->fixed_connector_type != DRM_MODE_CONNECTOR_HDMIA || + !IS_ERR_OR_NULL(dcp->avep) || !afk_compact_av_capable(dcp) || + !apple_rtkit_has_endpoint(dcp->rtk, AV_AUDIO_ENDPOINT_DESKTOP)) + return 0; + + cec = dcp_cec_ensure_state(dcp); + if (!cec) + return -ENOMEM; + + ep = afk_init(dcp, AV_AUDIO_ENDPOINT_DESKTOP, dcp_cec_desktop_ops); + if (IS_ERR(ep)) + return PTR_ERR(ep); + + cec->owned_avep = ep; + if (!cec->avep_cleanup_registered) { + ret = devm_add_action_or_reset(dcp->dev, dcp_cec_avep_cleanup, + cec); + if (ret) + return ret; + cec->avep_cleanup_registered = true; + } + + ep->debugfs_entry = dcp->ep_debugfs[AV_AUDIO_ENDPOINT_DESKTOP - 0x20]; + dcp->avep = ep; + if (dcp->fw_compat == DCP_FIRMWARE_V_26_6_DESKTOP) + WRITE_ONCE(dcp->desktop->graph[AV_AUDIO_ENDPOINT_DESKTOP - 0x20], + ep); + + ret = afk_start(ep); + if (ret) + dcp_cec_avep_cleanup(cec); + + return ret; +} + +#if IS_ENABLED(CONFIG_DRM_APPLE_KUNIT_TEST) +#include "cec_adap_test.c" +#endif diff --git a/drivers/gpu/drm/apple/cec.h b/drivers/gpu/drm/apple/cec.h new file mode 100644 index 0000000000000..982f45cc829f7 --- /dev/null +++ b/drivers/gpu/drm/apple/cec.h @@ -0,0 +1,89 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +#ifndef __APPLE_DCP_CEC_H__ +#define __APPLE_DCP_CEC_H__ + +#include +#include + +#include "afk.h" +#include "cec-proto.h" + +struct apple_dcp; + +#if IS_ENABLED(CONFIG_DRM_APPLE_CEC) +int dcp_cec_register(struct apple_dcp *dcp); +void dcp_cec_unregister(struct apple_dcp *dcp); +int dcp_cec_init_avep(struct apple_dcp *dcp); +void dcp_cec_close_port(struct apple_dcp *dcp, + struct apple_epic_service *port_srv); +void dcp_cec_poll_edid(struct apple_dcp *dcp); +bool dcp_cec_busy(struct apple_dcp *dcp); + +void dcp_cec_service_init(struct apple_epic_service *service, const char *name, + const char *class, s64 unit); +int dcp_cec_service_report(struct apple_epic_service *service, + enum epic_subtype type, + const void *data, size_t data_size); +void dcp_cec_service_teardown(struct apple_epic_service *service); +#else +static inline int dcp_cec_register(struct apple_dcp *dcp) +{ + return 0; +} + +static inline void dcp_cec_unregister(struct apple_dcp *dcp) +{ +} + +static inline int dcp_cec_init_avep(struct apple_dcp *dcp) +{ + return 0; +} + +static inline void dcp_cec_close_port(struct apple_dcp *dcp, + struct apple_epic_service *port_srv) +{ +} + +static inline void dcp_cec_poll_edid(struct apple_dcp *dcp) +{ +} + +static inline bool dcp_cec_busy(struct apple_dcp *dcp) +{ + return false; +} + +static inline void dcp_cec_service_init(struct apple_epic_service *service, + const char *name, const char *class, + s64 unit) +{ +} + +static inline int dcp_cec_service_report(struct apple_epic_service *service, + enum epic_subtype type, + const void *data, size_t data_size) +{ + return 0; +} + +static inline void dcp_cec_service_teardown(struct apple_epic_service *service) +{ + afk_service_disable(service); +} +#endif + +/* + * Shared compact EPIC service entry for "dcpav-cec-interface-epic:0" on + * endpoint 0x2b (announced cut to "dispext0:dcpav-cec-interface-ep"). + */ +#define DCP_CEC_COMPACT_SERVICE_OPS { \ + .name = "dcpav-cec-interface-epic", \ + .compact_full = "dcpav-cec-interface-epic:0", \ + .singleton = true, \ + .init = dcp_cec_service_init, \ + .report = dcp_cec_service_report, \ + .teardown = dcp_cec_service_teardown, \ +} + +#endif /* __APPLE_DCP_CEC_H__ */ diff --git a/drivers/gpu/drm/apple/cec_adap_test.c b/drivers/gpu/drm/apple/cec_adap_test.c new file mode 100644 index 0000000000000..61a30501b3ef7 --- /dev/null +++ b/drivers/gpu/drm/apple/cec_adap_test.c @@ -0,0 +1,488 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * KUnit integration tests for Apple Silicon DCP HDMI-CEC adapter lifecycle, + * deferred EDID PA, HPD-low close, singleton reuse on Terminate, and TX + * sequence guarding. + */ + +#include +#include + +struct dcp_cec_test_ctx { + struct device *dev; + struct apple_dcp *dcp; + struct apple_dcp_afkep *ep; + struct apple_epic_service srv[2]; + struct apple_epic_service port_srv; + /* Serializes mock RPC counters and return state across kworker threads. */ + spinlock_t lock; + + unsigned int open_calls; + unsigned int close_calls; + unsigned int set_la_calls; + unsigned int get_edid_calls; + unsigned int send_calls; + + u16 last_la_mask; + struct cec_msg last_tx_msg; + u8 last_tx_retries; + unsigned int last_tx_timeout_ms; + + u16 edid_pa; + u32 send_retcode; + int forced_err; + + void (*on_open)(struct dcp_cec_test_ctx *ctx); + void (*on_close)(struct dcp_cec_test_ctx *ctx); + void (*on_send)(struct dcp_cec_test_ctx *ctx); +}; + +static struct dcp_cec_test_ctx *dcp_cec_active_ctx; + +static int dcp_cec_stub_call(struct apple_epic_service *srv, u32 cmd, + const void *req, void *resp, size_t body_len, + unsigned int timeout_ms) +{ + struct dcp_cec_test_ctx *ctx = dcp_cec_active_ctx; + void (*hook)(struct dcp_cec_test_ctx *ctx) = NULL; + const u8 *in = req; + u8 *out = resp; + + if (!ctx) + return -ENODEV; + + memset(out, 0, body_len); + + scoped_guard(spinlock_irqsave, &ctx->lock) { + if (ctx->forced_err) + return ctx->forced_err; + + switch (cmd) { + case DCP_CEC_CMD_OPEN: + if (body_len != DCP_CEC_OPEN_LEN) + return -EMSGSIZE; + ctx->open_calls++; + put_unaligned_le32(0, out + 0x00); + put_unaligned_le64(1, out + 0x10); + hook = ctx->on_open; + ctx->on_open = NULL; + break; + + case DCP_CEC_CMD_CLOSE: + if (body_len != DCP_CEC_CLOSE_LEN) + return -EMSGSIZE; + ctx->close_calls++; + put_unaligned_le32(0, out + 0x00); + hook = ctx->on_close; + ctx->on_close = NULL; + break; + + case DCP_CEC_CMD_SET_LOGICAL_ADDR_MASK: + if (body_len != DCP_CEC_SET_LA_MASK_LEN) + return -EMSGSIZE; + ctx->set_la_calls++; + ctx->last_la_mask = get_unaligned_le16(in + 0x10); + put_unaligned_le32(0, out + 0x00); + break; + + case DCP_CEC_CMD_GET_EDID_ATTRS: + if (body_len != DCP_CEC_EDID_ATTRS_LEN) + return -EMSGSIZE; + ctx->get_edid_calls++; + put_unaligned_le16(ctx->edid_pa, out + 0x00); + break; + + case DCP_CEC_CMD_SEND_CEC: + if (body_len != DCP_CEC_SEND_LEN) + return -EMSGSIZE; + ctx->send_calls++; + ctx->last_tx_msg.len = (in[0x10] & 0x0f) + 1; + memcpy(ctx->last_tx_msg.msg, in + 0x00, + ctx->last_tx_msg.len); + ctx->last_tx_retries = in[0x20]; + ctx->last_tx_timeout_ms = timeout_ms; + put_unaligned_le32(ctx->send_retcode, out + 0x30); + hook = ctx->on_send; + ctx->on_send = NULL; + break; + + default: + return -EOPNOTSUPP; + } + } + + if (hook) + hook(ctx); + + return 0; +} + +static void dcp_cec_init_fake_srv(struct dcp_cec_test_ctx *ctx, unsigned int idx, + u32 channel) +{ + struct apple_epic_service *s = &ctx->srv[idx]; + + memset(s, 0, sizeof(*s)); + spin_lock_init(&s->lock); + s->ep = ctx->ep; + s->ops = &dcp_cec_desktop_ops[1]; + s->channel = channel; + s->enabled = true; +} + +static void dcp_cec_emit_report(struct apple_epic_service *srv, u32 cmd, + const void *body, u32 body_len) +{ + const size_t hdr = sizeof(struct epic_service_call); + u8 buf[sizeof(struct epic_service_call) + DCP_CEC_RPT_EDID_ATTRS_LEN] = {}; + struct epic_service_call *call = (struct epic_service_call *)buf; + + call->group = cpu_to_le16(DCP_CEC_GROUP_REPORT); + call->command = cpu_to_le32(cmd); + call->data_len = cpu_to_le32(body_len); + call->magic = cpu_to_le32(EPIC_SERVICE_CALL_MAGIC); + if (body && body_len) + memcpy(buf + hdr, body, body_len); + + dcp_cec_service_report(srv, EPIC_SUBTYPE_STD_SERVICE, buf, + hdr + body_len); +} + +static struct dcp_cec_test_ctx *dcp_cec_test_setup(struct kunit *test) +{ + struct dcp_cec_test_ctx *ctx; + struct cec_adapter *adap; + struct dcp_cec *cec; + int ret; + + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx); + spin_lock_init(&ctx->lock); + + ctx->dev = kunit_device_register(test, "apple-dcp-cec-kunit"); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->dev); + + ctx->dcp = kunit_kzalloc(test, sizeof(*ctx->dcp), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->dcp); + ctx->dcp->dev = ctx->dev; + ctx->dcp->fixed_connector_type = DRM_MODE_CONNECTOR_HDMIA; + + ctx->ep = kunit_kzalloc(test, sizeof(*ctx->ep), GFP_KERNEL); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->ep); + ctx->ep->dcp = ctx->dcp; + ctx->ep->endpoint = AV_AUDIO_ENDPOINT_DESKTOP; + ctx->ep->compact = true; + + ctx->port_srv.ep = ctx->ep; + ctx->dcp->dptxport[0].service = &ctx->port_srv; + + dcp_cec_init_fake_srv(ctx, 0, 1); + dcp_cec_init_fake_srv(ctx, 1, 2); + + cec = dcp_cec_ensure_state(ctx->dcp); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cec); + dcp_cec_active_ctx = ctx; + cec->test_call = dcp_cec_stub_call; + + adap = cec_allocate_adapter(&dcp_cec_adap_ops, cec, "apple-cec-test", + CEC_CAP_DEFAULTS | CEC_CAP_NEEDS_HPD, + CEC_MAX_LOG_ADDRS); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adap); + adap->xfer_timeout_ms = DCP_CEC_ADAP_XFER_TIMEOUT_MS; + + WRITE_ONCE(cec->adap, adap); + ret = cec_register_adapter(adap, ctx->dev); + if (ret) { + WRITE_ONCE(cec->adap, NULL); + cec_delete_adapter(adap); + } + KUNIT_ASSERT_EQ(test, ret, 0); + + return ctx; +} + +static void dcp_cec_test_teardown(struct dcp_cec_test_ctx *ctx) +{ + dcp_cec_unregister(ctx->dcp); + dcp_cec_active_ctx = NULL; +} + +static void dcp_cec_reuse_slot_on_close(struct dcp_cec_test_ctx *ctx) +{ + /* Simulate afk_compact_slot() reusing singleton srv[0] in place. */ + scoped_guard(spinlock_irqsave, &ctx->srv[0].lock) { + ctx->srv[0].channel = 9; + ctx->srv[0].enabled = true; + } +} + +static void cec_adap_pa_and_terminate_test(struct kunit *test) +{ + struct dcp_cec_test_ctx *ctx = dcp_cec_test_setup(test); + struct dcp_cec *cec = ctx->dcp->cec; + struct cec_adapter *adap = cec->adap; + u8 edid_body[DCP_CEC_RPT_EDID_ATTRS_LEN] = {}; + u8 term_body[DCP_CEC_RPT_TERMINATE_LEN] = {}; + u8 hpd_body[DCP_CEC_RPT_HPD_LEN] = {}; + + /* + * On initial plug, the EDID query can return an invalid physical + * address until a later poll or EDID attributes report. + */ + ctx->edid_pa = CEC_PHYS_ADDR_INVALID; + dcp_cec_service_init(&ctx->srv[0], "dcpav-cec-interface-epic", + "DCPAVCECInterface", 0); + flush_work(&cec->lifecycle_work); + + KUNIT_EXPECT_EQ(test, ctx->open_calls, 1U); + KUNIT_EXPECT_EQ(test, ctx->get_edid_calls, 1U); + KUNIT_EXPECT_EQ(test, adap->phys_addr, CEC_PHYS_ADDR_INVALID); + + /* Hotplug EDID poll picks up PA once DDC EDID finishes. */ + ctx->edid_pa = 0x2100; + dcp_cec_poll_edid(ctx->dcp); + flush_work(&cec->lifecycle_work); + KUNIT_EXPECT_EQ(test, adap->phys_addr, 0x2100); + + /* EDID_ATTRS async report updates PA to 3.4.0.0. */ + put_unaligned_le16(0x3400, edid_body + 0x00); + dcp_cec_emit_report(&ctx->srv[0], DCP_CEC_RPT_EDID_ATTRS, + edid_body, sizeof(edid_body)); + flush_work(&cec->lifecycle_work); + KUNIT_EXPECT_EQ(test, adap->phys_addr, 0x3400); + + /* HPD=0 async report closes the client and invalidates PA. */ + hpd_body[0] = 0; + dcp_cec_emit_report(&ctx->srv[0], DCP_CEC_RPT_HPD, + hpd_body, sizeof(hpd_body)); + flush_work(&cec->lifecycle_work); + KUNIT_EXPECT_EQ(test, ctx->close_calls, 1U); + KUNIT_EXPECT_EQ(test, adap->phys_addr, CEC_PHYS_ADDR_INVALID); + + /* An HPD-high report triggers a new open and EDID query. */ + ctx->edid_pa = 0x1000; + hpd_body[0] = 1; + dcp_cec_emit_report(&ctx->srv[0], DCP_CEC_RPT_HPD, + hpd_body, sizeof(hpd_body)); + flush_work(&cec->lifecycle_work); + KUNIT_EXPECT_EQ(test, ctx->open_calls, 2U); + KUNIT_EXPECT_EQ(test, adap->phys_addr, 0x1000); + + /* + * Terminate while afk_compact_slot() reuses singleton srv[0] in place: + * afk_service_retire_channel(srv[0], 1) must NOT disable channel 9. + */ + ctx->on_close = dcp_cec_reuse_slot_on_close; + dcp_cec_emit_report(&ctx->srv[0], DCP_CEC_RPT_TERMINATE, + term_body, sizeof(term_body)); + flush_work(&cec->lifecycle_work); + + KUNIT_EXPECT_EQ(test, ctx->close_calls, 2U); + KUNIT_EXPECT_TRUE(test, ctx->srv[0].enabled); + KUNIT_EXPECT_EQ(test, ctx->srv[0].channel, 9U); + + /* Retiring matching channel 9 disables and marks the slot retired. */ + afk_service_retire_channel(&ctx->srv[0], 9); + KUNIT_EXPECT_TRUE(test, afk_service_retired(&ctx->srv[0], 9)); + KUNIT_EXPECT_FALSE(test, ctx->srv[0].enabled); + + dcp_cec_test_teardown(ctx); +} + +static void dcp_cec_close_during_open(struct dcp_cec_test_ctx *ctx) +{ + /* Simulate HPD-low port close racing an in-flight Open. */ + scoped_guard(spinlock_irqsave, &ctx->dcp->cec->lock) + ctx->dcp->cec->close_gen++; +} + +static void cec_adap_la_mask_and_singleton_replace_test(struct kunit *test) +{ + struct dcp_cec_test_ctx *ctx = dcp_cec_test_setup(test); + struct dcp_cec *cec = ctx->dcp->cec; + struct cec_adapter *adap = cec->adap; + + ctx->edid_pa = 0x1000; + + dcp_cec_service_init(&ctx->srv[0], "dcpav-cec-interface-epic", + "DCPAVCECInterface", 0); + flush_work(&cec->lifecycle_work); + + /* Enable + claim LA 4 -> 0x8000 then 0x8010. */ + KUNIT_EXPECT_EQ(test, dcp_cec_adap_enable(adap, true), 0); + KUNIT_EXPECT_EQ(test, ctx->last_la_mask, 0x8000); + KUNIT_EXPECT_EQ(test, dcp_cec_adap_log_addr(adap, 4), 0); + KUNIT_EXPECT_EQ(test, ctx->last_la_mask, 0x8010); + + /* + * Singleton replacement via teardown + init: teardown must NOT send + * CMD_CLOSE (slot is reused in place) and must drop users to 0; + * init on replacement opens it and restores 0x8010 automatically. + */ + afk_service_retire(&ctx->srv[0]); + dcp_cec_service_teardown(&ctx->srv[0]); + KUNIT_EXPECT_EQ(test, ctx->close_calls, 0U); + KUNIT_EXPECT_EQ(test, ctx->srv[0].users, 0U); + + ctx->last_la_mask = 0; + dcp_cec_service_init(&ctx->srv[1], "dcpav-cec-interface-epic", + "DCPAVCECInterface", 0); + flush_work(&cec->lifecycle_work); + KUNIT_EXPECT_EQ(test, ctx->open_calls, 2U); + KUNIT_EXPECT_EQ(test, ctx->last_la_mask, 0x8010); + + /* Pre-HPD-low close closes the active CEC client and invalidates PA. */ + dcp_cec_close_port(ctx->dcp, &ctx->port_srv); + flush_work(&cec->lifecycle_work); + KUNIT_EXPECT_EQ(test, ctx->close_calls, 1U); + KUNIT_EXPECT_EQ(test, adap->phys_addr, CEC_PHYS_ADDR_INVALID); + + /* An Open raced by HPD-low close immediately closes again. */ + ctx->on_open = dcp_cec_close_during_open; + KUNIT_EXPECT_EQ(test, dcp_cec_adap_enable(adap, true), 0); + KUNIT_EXPECT_EQ(test, ctx->open_calls, 3U); + KUNIT_EXPECT_EQ(test, ctx->close_calls, 2U); + KUNIT_EXPECT_EQ(test, cec->opened_gen, 0ULL); + + /* Re-open cleanly, then verify unregister keeps cec->srv pinned. */ + KUNIT_EXPECT_EQ(test, dcp_cec_adap_enable(adap, true), 0); + KUNIT_EXPECT_EQ(test, ctx->open_calls, 4U); + + /* adap_log_addr(INVALID) and adap_enable(false) always return 0 even on AFK error. */ + KUNIT_EXPECT_EQ(test, dcp_cec_adap_log_addr(adap, CEC_LOG_ADDR_INVALID), 0); + KUNIT_EXPECT_EQ(test, ctx->last_la_mask, 0x8000); + + ctx->forced_err = -EIO; + KUNIT_EXPECT_EQ(test, dcp_cec_adap_log_addr(adap, CEC_LOG_ADDR_INVALID), 0); + KUNIT_EXPECT_EQ(test, dcp_cec_adap_enable(adap, false), 0); + ctx->forced_err = 0; + + dcp_cec_unregister(ctx->dcp); + KUNIT_EXPECT_PTR_EQ(test, cec->srv, &ctx->srv[1]); + KUNIT_EXPECT_EQ(test, ctx->close_calls, 3U); + + /* + * Component rebind re-registers an adapter and re-opens the pinned + * cec->srv without waiting for a hotplug relink. + */ + adap = cec_allocate_adapter(&dcp_cec_adap_ops, cec, "apple-cec-rebind", + CEC_CAP_DEFAULTS | CEC_CAP_NEEDS_HPD, + CEC_MAX_LOG_ADDRS); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, adap); + cec->stopping = false; + cec->opened_gen = 0; + cec->open_attempted_gen = 0; + WRITE_ONCE(cec->adap, adap); + KUNIT_ASSERT_EQ(test, cec_register_adapter(adap, ctx->dev), 0); + queue_work(system_dfl_wq, &cec->lifecycle_work); + flush_work(&cec->lifecycle_work); + KUNIT_EXPECT_EQ(test, ctx->open_calls, 5U); + KUNIT_EXPECT_EQ(test, adap->phys_addr, 0x1000); + + dcp_cec_test_teardown(ctx); +} + +static void dcp_cec_supersede_tx_during_send(struct dcp_cec_test_ctx *ctx) +{ + struct dcp_cec *cec = ctx->dcp->cec; + struct cec_adapter *adap = cec->adap; + struct cec_msg newer = { .len = 2, .msg = { 0x40, 0x83 } }; + + /* + * Simulate CEC core aborting TX #1 (adap_enable(false) -> true) and + * starting TX #2 before the AFK reply for TX #1 arrives. + */ + dcp_cec_adap_enable(adap, false); + dcp_cec_adap_enable(adap, true); + ctx->send_retcode = DCP_CEC_RET_OK; + adap->transmit_in_progress = true; + dcp_cec_adap_transmit(adap, 1, 0, &newer); +} + +static void cec_adap_tx_and_rx_test(struct kunit *test) +{ + struct dcp_cec_test_ctx *ctx = dcp_cec_test_setup(test); + struct dcp_cec *cec = ctx->dcp->cec; + struct cec_adapter *adap = cec->adap; + u8 rx_body[DCP_CEC_RPT_RX_FRAME_LEN] = {}; + struct cec_msg tx = {}; + + ctx->edid_pa = 0x1000; + + dcp_cec_service_init(&ctx->srv[0], "dcpav-cec-interface-epic", + "DCPAVCECInterface", 0); + flush_work(&cec->lifecycle_work); + KUNIT_EXPECT_EQ(test, dcp_cec_adap_enable(adap, true), 0); + + /* Non-blocking adap_transmit queues tx_work and calls SEND_CEC (7500 ms). */ + tx.len = 2; + tx.msg[0] = 0x40; + tx.msg[1] = 0x8f; /* Give Device Power Status */ + ctx->send_retcode = DCP_CEC_RET_OK; + adap->transmit_in_progress = true; + KUNIT_EXPECT_EQ(test, dcp_cec_adap_transmit(adap, 2, 0, &tx), 0); + flush_work(&cec->tx_work); + KUNIT_EXPECT_FALSE(test, adap->transmit_in_progress); + + KUNIT_EXPECT_EQ(test, ctx->send_calls, 1U); + KUNIT_EXPECT_EQ(test, ctx->last_tx_msg.len, 2U); + KUNIT_EXPECT_EQ(test, ctx->last_tx_msg.msg[0], 0x40); + KUNIT_EXPECT_EQ(test, ctx->last_tx_msg.msg[1], 0x8f); + KUNIT_EXPECT_EQ(test, ctx->last_tx_retries, 1U); + KUNIT_EXPECT_EQ(test, ctx->last_tx_timeout_ms, + (unsigned int)DCP_CEC_TX_TIMEOUT_MS); + + /* Verify TIMEOUT and ABORTED complete cleanly without WARN_ON. */ + ctx->send_retcode = DCP_CEC_RET_TIMEOUT; + adap->transmit_in_progress = true; + KUNIT_EXPECT_EQ(test, dcp_cec_adap_transmit(adap, 1, 0, &tx), 0); + flush_work(&cec->tx_work); + KUNIT_EXPECT_FALSE(test, adap->transmit_in_progress); + KUNIT_EXPECT_EQ(test, ctx->send_calls, 2U); + + ctx->send_retcode = DCP_CEC_RET_ABORTED; + adap->transmit_in_progress = true; + KUNIT_EXPECT_EQ(test, dcp_cec_adap_transmit(adap, 1, 0, &tx), 0); + flush_work(&cec->tx_work); + KUNIT_EXPECT_FALSE(test, adap->transmit_in_progress); + KUNIT_EXPECT_EQ(test, ctx->send_calls, 3U); + + /* + * Stale TX #1 returning NACK after TX #2 was queued must not complete + * TX #2 with NACK; tx_work loops to send and complete TX #2 with OK. + */ + ctx->send_retcode = DCP_CEC_RET_NACK; + ctx->on_send = dcp_cec_supersede_tx_during_send; + adap->transmit_in_progress = true; + KUNIT_EXPECT_EQ(test, dcp_cec_adap_transmit(adap, 1, 0, &tx), 0); + flush_work(&cec->tx_work); + KUNIT_EXPECT_FALSE(test, adap->transmit_in_progress); + KUNIT_EXPECT_EQ(test, ctx->send_calls, 5U); + KUNIT_EXPECT_EQ(test, ctx->last_tx_msg.msg[1], 0x83); + + /* RX_FRAME report queues to rx_work and delivers via cec_received_msg_ts. */ + rx_body[0x00] = 0x04; + rx_body[0x01] = 0x90; + rx_body[0x02] = 0x00; + rx_body[0x10] = 0x02; /* len = 3 */ + dcp_cec_emit_report(&ctx->srv[0], DCP_CEC_RPT_RX_FRAME, + rx_body, sizeof(rx_body)); + flush_work(&cec->rx_work); + KUNIT_EXPECT_EQ(test, cec->rx_count, 0U); + + dcp_cec_test_teardown(ctx); +} + +static struct kunit_case dcp_cec_adap_cases[] = { + KUNIT_CASE(cec_adap_pa_and_terminate_test), + KUNIT_CASE(cec_adap_la_mask_and_singleton_replace_test), + KUNIT_CASE(cec_adap_tx_and_rx_test), + {} +}; + +static struct kunit_suite dcp_cec_adap_suite = { + .name = "apple-dcp-cec-adap", + .test_cases = dcp_cec_adap_cases, +}; + +kunit_test_suite(dcp_cec_adap_suite); diff --git a/drivers/gpu/drm/apple/dcp-fabric.c b/drivers/gpu/drm/apple/dcp-fabric.c index fcfd7bdb8843f..08f5057cf1a9d 100644 --- a/drivers/gpu/drm/apple/dcp-fabric.c +++ b/drivers/gpu/drm/apple/dcp-fabric.c @@ -22,6 +22,7 @@ #include #include "afk.h" +#include "cec.h" #include "dcp.h" #include "dcp-fabric.h" #include "dcp-fabric-effects.h" @@ -5207,22 +5208,33 @@ static int dcp_fixed_output_select(struct apple_dcp *dcp) &dcp_fixed_ops, dcp); } +void dcp_dp2hdmi_record_edge(struct apple_dcp *dcp) +{ + if (dcp->fw_compat == DCP_FIRMWARE_V_26_6_DESKTOP) + return; + + WRITE_ONCE(dcp->hdmi_edge_jiffies, jiffies); + WRITE_ONCE(dcp->hdmi_edge_seen, true); + dcp_hdmi_edge(dcp); +} + /* * Any HDMI HPD edge: a display is there or just was. Start the hold at the * edge itself, before a borrower can take the fabric lock ahead of the - * thread. + * thread. Short IRQ_HPD pulses while the link is up stay in hardirq context + * so the IRQF_ONESHOT threaded handler does not mask the rising edge. */ irqreturn_t dcp_dp2hdmi_hpd_edge(int irq, void *data) { struct apple_dcp *dcp = data; + if (dcp_hdmi_hpd_edge(dcp)) + return IRQ_HANDLED; + if (dcp->fw_compat == DCP_FIRMWARE_V_26_6_DESKTOP) return IRQ_WAKE_THREAD; - WRITE_ONCE(dcp->hdmi_edge_jiffies, jiffies); - WRITE_ONCE(dcp->hdmi_edge_seen, true); - dcp_hdmi_hpd_edge(data); - dcp_hdmi_edge(data); + dcp_dp2hdmi_record_edge(dcp); return IRQ_WAKE_THREAD; } @@ -5744,6 +5756,7 @@ void dcp_link(struct platform_device *pdev, struct apple_crtc *crtc, } if (iomfb_is_m4(dcp)) WRITE_ONCE(connector->connected, !iomfb_m4_error(dcp) && dcp->active); + dcp_cec_register(dcp); } static void dcp_typec_port_unlink(struct apple_dcp_typec_port *port, @@ -5769,6 +5782,17 @@ void dcp_unlink(struct drm_device *drm) struct apple_dcp_typec_port *port; struct drm_crtc *crtc; + drm_for_each_crtc(crtc, drm) { + struct apple_crtc *apple_crtc = to_apple_crtc(crtc); + struct apple_dcp *dcp; + + if (t8132_ext_crtc(crtc) || !apple_crtc->dcp) + continue; + dcp = platform_get_drvdata(apple_crtc->dcp); + if (dcp->crtc == apple_crtc) + dcp_cec_unregister(dcp); + } + guard(mutex)(&dcp_typec_fabric_lock); list_for_each_entry(port, &dcp_typec_ports, link) diff --git a/drivers/gpu/drm/apple/dcp-fabric.h b/drivers/gpu/drm/apple/dcp-fabric.h index d8f92da233445..7876718cbfdfa 100644 --- a/drivers/gpu/drm/apple/dcp-fabric.h +++ b/drivers/gpu/drm/apple/dcp-fabric.h @@ -138,6 +138,7 @@ void dcp_fabric_init(struct apple_dcp *dcp); void dcp_fabric_hdmi_resume(struct apple_dcp *dcp); void dcp_fabric_hdmi_reinit(struct apple_dcp *dcp, const char *why); void dcp_fabric_hdmi_retry(struct apple_dcp *dcp); +void dcp_dp2hdmi_record_edge(struct apple_dcp *dcp); irqreturn_t dcp_dp2hdmi_hpd_edge(int irq, void *data); irqreturn_t dcp_dp2hdmi_hpd(int irq, void *data); void dcp_fabric_shutdown_dptx(struct apple_dcp *dcp); diff --git a/drivers/gpu/drm/apple/dcp-hdmi.c b/drivers/gpu/drm/apple/dcp-hdmi.c index 0603de2a0e229..d39af2083c7ab 100644 --- a/drivers/gpu/drm/apple/dcp-hdmi.c +++ b/drivers/gpu/drm/apple/dcp-hdmi.c @@ -1,45 +1,54 @@ // SPDX-License-Identifier: GPL-2.0-only OR MIT /* - * HDMI port of the T6030 (M3 Pro) MacBook Pros. - * - * The port hangs off the first external display coprocessor: it drives - * ATC3 as a four-lane DisplayPort PHY into an on-board DP-to-HDMI - * converter. Power, hotplug and the fixed route work as for the M1/M2 Pro - * MacBook Pro HDMI port (hdmi-hpd, hdmi-pwren and dp2hdmi-pwren GPIOs, see + * Native HDMI ports: an external display coprocessor driving a DisplayPort + * PHY into an on-board DP-to-HDMI converter, as on the M1 and later Pro/Max + * MacBook Pros, the Mac minis and the Mac Studios. Power, hotplug and the + * fixed route use the hdmi-hpd, hdmi-pwren and dp2hdmi-pwren GPIOs (see * dcp.c and dcp-fabric.c). * * Like any DisplayPort sink, the converter asks the source for service by * pulsing HPD low for 0.5 to 1 ms (IRQ_HPD). It does that on link status - * changes and while it trains an HDMI FRL link to the display, which the - * modes beyond HDMI 2.0 rates need. The DPTX firmware services a request - * only once it is told that the sink raised one. This file tells it: + * changes, on HDMI-CEC TX completion and RX frame arrival, and while it trains + * an HDMI FRL link to the display, which the modes beyond HDMI 2.0 rates need. + * The DPTX firmware services a request only once it is told that the sink + * raised one. This file tells it: * - * - for every short low pulse on the HPD GPIO while the HDMI link is up; + * - for every short low pulse on the HPD GPIO while the HDMI link is up, + * handling the falling edge in hard IRQ context with a 2 ms hrtimer so an + * IRQF_ONESHOT threaded handler does not mask the rising edge for 500 ms, + * and waking the threaded HPD handler via irq_wake_thread() if HPD stays + * low past 2 ms; * - every 100 ms for a while after the firmware configured an HBR3 link, * the rate those modes use, because not every request of the converter * shows up as a pulse on the GPIO. * - * Only the T6030 external coprocessor with a fixed HDMI route uses this. + * Every coprocessor with a fixed HDMI-A connector and an HDMI HPD GPIO in + * its DT node uses this: IRQ_HPD is DisplayPort protocol, and the converter + * sits behind the same DPTX port on every such board. A firmware that does + * not take the request answers it with an error; that stops the polling. */ #include +#include #include #include +#include +#include #include #include -#include #include +#include #include #include #include "afk.h" +#include "connector.h" +#include "dcp-fabric.h" #include "dcp-hdmi.h" #include "dcp-internal.h" #include "dptxep.h" -/* Longest HPD low time still taken as a service request (DP: at most 1 ms). */ -#define DCP_HDMI_IRQ_HPD_MAX_NS (2 * NSEC_PER_MSEC) /* Requests are polled this long after an HBR3 link configuration ... */ #define DCP_HDMI_SERVICE_WINDOW_MS 10000 /* ... at this period. */ @@ -53,8 +62,21 @@ struct dcp_hdmi { struct apple_dcp *dcp; struct delayed_work work; - /* Hard IRQ context only: time of the last falling HPD edge, or 0. */ + struct hrtimer pulse_timer; + /* Protects falling_ns, late_rising, claimed_ns, and enabled. */ + spinlock_t lock; + /* Time of the last falling HPD edge while link is up, or 0. */ u64 falling_ns; + /* Time when 2 ms timer counted a high pulse, bounding double-count skip. */ + u64 claimed_ns; + /* True after 2 ms timer expired low, so the next rising edge wakes HPD. */ + bool late_rising; + /* True if pulse_timer woke the threaded HPD IRQ handler since enable. */ + bool thread_woken; + /* True between dcp_hdmi_enable() and dcp_hdmi_disable(). */ + bool enabled; + /* Set if DPTX firmware refuses DPTX_CMD_SINK_IRQ. */ + bool unsupported; /* Short pulses seen and not yet passed on. */ atomic_t pulses; /* Poll requests until this time (jiffies); 0 when not polling. */ @@ -67,6 +89,14 @@ struct dcp_hdmi { static bool dcp_hdmi_link_up(struct apple_dcp *dcp) { + /* + * Desktop 26.x keeps dptxport[0] connected while idle so its firmware + * is ready for hotplug; only an attached sink raises IRQ_HPD pulses. + */ + if (dcp->fw_compat == DCP_FIRMWARE_V_26_6_DESKTOP && + (!dcp->connector || !READ_ONCE(dcp->connector->connected))) + return false; + /* A Type-C display that borrowed this pipeline has its own HPD. */ return !READ_ONCE(dcp->crashed) && !READ_ONCE(dcp->active_typec_route) && READ_ONCE(dcp->dptxport[0].enabled) && @@ -99,7 +129,7 @@ static void dcp_hdmi_work(struct work_struct *work) bool poll = until && time_before(jiffies, until); int ret; - if (!pulse && !poll) + if ((!pulse && !poll) || READ_ONCE(hdmi->unsupported)) return; /* The link rate can be set before the connection is marked up. */ if (!dcp_hdmi_link_up(dcp)) @@ -110,8 +140,10 @@ static void dcp_hdmi_work(struct work_struct *work) dev_warn_ratelimited(dcp->dev, "HDMI: converter service request not delivered: %d\n", ret); - /* Don't keep polling a firmware that refuses the request. */ + /* Don't keep polling or forwarding to firmware that refuses it. */ cmpxchg(&hdmi->poll_until, until, 0); + if (ret != -ENODEV && ret != -ETIMEDOUT) + WRITE_ONCE(hdmi->unsupported, true); return; } if (!hdmi->announced) { @@ -129,36 +161,109 @@ static void dcp_hdmi_work(struct work_struct *work) msecs_to_jiffies(DCP_HDMI_SERVICE_PERIOD_MS)); } +static enum hrtimer_restart dcp_hdmi_pulse_timer(struct hrtimer *timer) +{ + struct dcp_hdmi *hdmi = container_of(timer, struct dcp_hdmi, pulse_timer); + struct apple_dcp *dcp = hdmi->dcp; + bool real_unplug = false; + bool short_pulse = false; + unsigned long flags; + u64 now; + int level; + + now = ktime_get_ns(); + level = gpiod_get_value(dcp->hdmi_hpd); + + spin_lock_irqsave(&hdmi->lock, flags); + if (hdmi->enabled && hdmi->falling_ns) { + hdmi->falling_ns = 0; + if (level <= 0) { + hdmi->late_rising = true; + hdmi->claimed_ns = 0; + hdmi->thread_woken = dcp->hdmi_hpd_irq > 0; + real_unplug = true; + } else { + hdmi->late_rising = false; + hdmi->claimed_ns = now ?: 1; + short_pulse = true; + } + } + spin_unlock_irqrestore(&hdmi->lock, flags); + + if (real_unplug) { + dcp_dp2hdmi_record_edge(dcp); + if (dcp->hdmi_hpd_irq > 0) + irq_wake_thread(dcp->hdmi_hpd_irq, dcp); + } else if (short_pulse && !READ_ONCE(hdmi->unsupported)) { + atomic_inc(&hdmi->pulses); + mod_delayed_work(system_freezable_wq, &hdmi->work, 0); + } + + return HRTIMER_NORESTART; +} + /* * Hard IRQ context, from the HPD GPIO's edge handler, on every edge. - * Records the low time of each pulse and queues short ones. + * Returns true when handled as short-pulse tracking (keeping the IRQF_ONESHOT + * GPIO edge detector armed), or false when the threaded HPD handler should run. */ -void dcp_hdmi_hpd_edge(struct apple_dcp *dcp) +bool dcp_hdmi_hpd_edge(struct apple_dcp *dcp) { struct dcp_hdmi *hdmi = dcp->hdmi; - u64 now, low; + enum dcp_hdmi_pulse_action action; + bool cancel_timer = false; + unsigned long flags; + u64 now; int level; - if (!hdmi || !hdmi->timed_edges) - return; + if (!hdmi || !hdmi->timed_edges || READ_ONCE(hdmi->unsupported)) + return false; + + if (!dcp_hdmi_link_up(dcp)) { + spin_lock_irqsave(&hdmi->lock, flags); + if (hdmi->falling_ns || hdmi->late_rising || hdmi->claimed_ns) { + hdmi->falling_ns = 0; + hdmi->late_rising = false; + hdmi->claimed_ns = 0; + cancel_timer = true; + } + spin_unlock_irqrestore(&hdmi->lock, flags); + if (cancel_timer) + hrtimer_try_to_cancel(&hdmi->pulse_timer); + return false; + } now = ktime_get_ns(); level = gpiod_get_value(dcp->hdmi_hpd); - if (level < 0) - return; - if (!level) { - hdmi->falling_ns = now; - return; - } - /* A rising edge without a falling one: the pulse ended before we read it. */ - low = hdmi->falling_ns ? now - hdmi->falling_ns : 0; - hdmi->falling_ns = 0; - if (low > DCP_HDMI_IRQ_HPD_MAX_NS) - return; /* A cable change, handled by the HPD presence logic. */ + spin_lock_irqsave(&hdmi->lock, flags); + if (!hdmi->enabled) { + spin_unlock_irqrestore(&hdmi->lock, flags); + return false; + } + action = dcp_hdmi_classify_edge(&hdmi->falling_ns, &hdmi->late_rising, + &hdmi->claimed_ns, now, level); + if (action == DCP_HDMI_PULSE_ARM_TIMER) + hrtimer_start(&hdmi->pulse_timer, + ns_to_ktime(DCP_HDMI_IRQ_HPD_MAX_NS), + HRTIMER_MODE_REL); + spin_unlock_irqrestore(&hdmi->lock, flags); - atomic_inc(&hdmi->pulses); - mod_delayed_work(system_freezable_wq, &hdmi->work, 0); + switch (action) { + case DCP_HDMI_PULSE_ARM_TIMER: + return true; + case DCP_HDMI_PULSE_CLAIMED: + return true; + case DCP_HDMI_PULSE_SHORT: + hrtimer_try_to_cancel(&hdmi->pulse_timer); + atomic_inc(&hdmi->pulses); + mod_delayed_work(system_freezable_wq, &hdmi->work, 0); + return true; + case DCP_HDMI_PULSE_PASS: + default: + hrtimer_try_to_cancel(&hdmi->pulse_timer); + return false; + } } /* From DPTX DidChangeLinkConfiguration, with the rate that is now set. */ @@ -167,7 +272,7 @@ void dcp_hdmi_link_configured(struct apple_dcp *dcp, u32 link_rate) struct dcp_hdmi *hdmi = dcp->hdmi; unsigned long until; - if (!hdmi) + if (!hdmi || READ_ONCE(hdmi->unsupported)) return; if (link_rate != LINK_RATE_HBR3) { WRITE_ONCE(hdmi->poll_until, 0); @@ -181,15 +286,44 @@ void dcp_hdmi_link_configured(struct apple_dcp *dcp, u32 link_rate) void dcp_hdmi_enable(struct apple_dcp *dcp) { - if (dcp->hdmi) - enable_delayed_work(&dcp->hdmi->work); + struct dcp_hdmi *hdmi = dcp->hdmi; + + if (!hdmi) + return; + + enable_delayed_work(&hdmi->work); + scoped_guard(spinlock_irqsave, &hdmi->lock) { + hdmi->falling_ns = 0; + hdmi->claimed_ns = 0; + hdmi->late_rising = false; + hdmi->thread_woken = false; + hdmi->enabled = true; + } } /* Returns with no request in flight; later edges queue nothing. */ void dcp_hdmi_disable(struct apple_dcp *dcp) { - if (dcp->hdmi) - disable_delayed_work_sync(&dcp->hdmi->work); + struct dcp_hdmi *hdmi = dcp->hdmi; + bool wait_irq = false; + + if (!hdmi) + return; + + scoped_guard(spinlock_irqsave, &hdmi->lock) { + hdmi->enabled = false; + hdmi->falling_ns = 0; + hdmi->claimed_ns = 0; + hdmi->late_rising = false; + } + hrtimer_cancel(&hdmi->pulse_timer); + scoped_guard(spinlock_irqsave, &hdmi->lock) { + wait_irq = hdmi->thread_woken; + hdmi->thread_woken = false; + } + if (wait_irq && dcp->hdmi_hpd_irq > 0) + synchronize_irq(dcp->hdmi_hpd_irq); + disable_delayed_work_sync(&hdmi->work); } static void dcp_hdmi_release(void *data) @@ -198,6 +332,13 @@ static void dcp_hdmi_release(void *data) /* The HPD IRQ was requested later, so it is already freed. */ hdmi->dcp->hdmi = NULL; + scoped_guard(spinlock_irqsave, &hdmi->lock) { + hdmi->enabled = false; + hdmi->falling_ns = 0; + hdmi->claimed_ns = 0; + hdmi->late_rising = false; + } + hrtimer_cancel(&hdmi->pulse_timer); disable_delayed_work_sync(&hdmi->work); } @@ -211,8 +352,7 @@ int dcp_hdmi_init(struct apple_dcp *dcp) int ret; if (!dcp->hdmi_hpd || - dcp->fixed_connector_type != DRM_MODE_CONNECTOR_HDMIA || - !of_device_is_compatible(dcp->dev->of_node, "apple,t6030-dcpext")) + dcp->fixed_connector_type != DRM_MODE_CONNECTOR_HDMIA) return 0; hdmi = devm_kzalloc(dcp->dev, sizeof(*hdmi), GFP_KERNEL); @@ -220,8 +360,11 @@ int dcp_hdmi_init(struct apple_dcp *dcp) return -ENOMEM; hdmi->dcp = dcp; + spin_lock_init(&hdmi->lock); atomic_set(&hdmi->pulses, 0); hdmi->timed_edges = !gpiod_cansleep(dcp->hdmi_hpd); + hrtimer_setup(&hdmi->pulse_timer, dcp_hdmi_pulse_timer, CLOCK_MONOTONIC, + HRTIMER_MODE_REL); INIT_DELAYED_WORK(&hdmi->work, dcp_hdmi_work); /* Balanced by dcp_hdmi_enable() once the DPTX endpoint may be used. */ disable_delayed_work(&hdmi->work); diff --git a/drivers/gpu/drm/apple/dcp-hdmi.h b/drivers/gpu/drm/apple/dcp-hdmi.h index 2138b400d8c98..cfb6aa5166c55 100644 --- a/drivers/gpu/drm/apple/dcp-hdmi.h +++ b/drivers/gpu/drm/apple/dcp-hdmi.h @@ -2,18 +2,82 @@ #ifndef __APPLE_DCP_HDMI_H__ #define __APPLE_DCP_HDMI_H__ +#include #include struct apple_dcp; +/* Longest HPD low time still taken as a service request (DP: at most 1 ms). */ +#define DCP_HDMI_IRQ_HPD_MAX_NS (2 * NSEC_PER_MSEC) + +enum dcp_hdmi_pulse_action { + /* Pass edge to the threaded HPD presence handler (IRQ_WAKE_THREAD). */ + DCP_HDMI_PULSE_PASS, + /* Falling edge: arm 2 ms timer and return IRQ_HANDLED. */ + DCP_HDMI_PULSE_ARM_TIMER, + /* Timer already counted this short pulse: return IRQ_HANDLED. */ + DCP_HDMI_PULSE_CLAIMED, + /* Valid short IRQ_HPD pulse: cancel timer, forward sink IRQ, IRQ_HANDLED. */ + DCP_HDMI_PULSE_SHORT, +}; + +/* + * Classify an HPD GPIO edge observed while the HDMI link is up. + * Caller serializes @falling_ns, @late_rising, and @claimed_ns. + */ +static inline enum dcp_hdmi_pulse_action +dcp_hdmi_classify_edge(u64 *falling_ns, bool *late_rising, u64 *claimed_ns, + u64 now_ns, int level) +{ + u64 dt; + + if (level < 0) + return DCP_HDMI_PULSE_PASS; + + if (!level) { + *late_rising = false; + *claimed_ns = 0; + if (!*falling_ns) + *falling_ns = now_ns ?: 1; + return DCP_HDMI_PULSE_ARM_TIMER; + } + + if (!*falling_ns) { + if (*claimed_ns) { + u64 since = now_ns >= *claimed_ns ? + now_ns - *claimed_ns : 0; + + *claimed_ns = 0; + if (since <= DCP_HDMI_IRQ_HPD_MAX_NS) + return DCP_HDMI_PULSE_CLAIMED; + } + if (*late_rising) { + *late_rising = false; + return DCP_HDMI_PULSE_PASS; + } + /* Pulse started and ended before GPIO was sampled in hardirq. */ + return DCP_HDMI_PULSE_SHORT; + } + + dt = now_ns >= *falling_ns ? now_ns - *falling_ns : 0; + *falling_ns = 0; + *late_rising = false; + *claimed_ns = 0; + + if (dt <= DCP_HDMI_IRQ_HPD_MAX_NS) + return DCP_HDMI_PULSE_SHORT; + + return DCP_HDMI_PULSE_PASS; +} + /* - * Service requests of the DP-to-HDMI converter behind the T6030 HDMI port. + * Service requests of the DP-to-HDMI converter behind a built-in HDMI port. * Every call is a no-op for any other DCP. */ int dcp_hdmi_init(struct apple_dcp *dcp); void dcp_hdmi_enable(struct apple_dcp *dcp); void dcp_hdmi_disable(struct apple_dcp *dcp); -void dcp_hdmi_hpd_edge(struct apple_dcp *dcp); +bool dcp_hdmi_hpd_edge(struct apple_dcp *dcp); void dcp_hdmi_link_configured(struct apple_dcp *dcp, u32 link_rate); #endif /* __APPLE_DCP_HDMI_H__ */ diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index ad103aa458d34..e5dd066b69e80 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -432,6 +432,7 @@ struct dcp_desktop_state { /* TODO: move IOMFB members to its own struct */ struct dcpext_scanout; struct dcp_hdmi; +struct dcp_cec; bool dcp_desktop_present_complete(struct apple_dcp *dcp, u32 id); void dcp_panel_metadata_replace(struct apple_dcp *dcp, @@ -1163,6 +1164,7 @@ struct apple_dcp { struct dcp_desktop_state *desktop; /* Internal panel identity; NULL without an internal panel. */ struct dcp_panel_identity *panel_identity; + struct dcp_cec *cec; }; /* T8132 firmware uses its own wire codec, with the common DRM/KMS owner. */ diff --git a/drivers/gpu/drm/apple/dcp-v27-sleep.inc b/drivers/gpu/drm/apple/dcp-v27-sleep.inc index 5357249a46edb..62c5e829045c5 100644 --- a/drivers/gpu/drm/apple/dcp-v27-sleep.inc +++ b/drivers/gpu/drm/apple/dcp-v27-sleep.inc @@ -502,6 +502,12 @@ static bool dcp_v27_ext_busy(struct apple_dcp *dcp, char *what, size_t len) strscpy(what, "hotplug work", len); return true; } +#if IS_ENABLED(CONFIG_DRM_APPLE_CEC) + if (dcp_cec_busy(dcp)) { + strscpy(what, "CEC work", len); + return true; + } +#endif if (!apple_rtkit_v27_rx_drained(dcp->rtk)) { strscpy(what, "messages from the processor", len); return true; @@ -941,9 +947,7 @@ static const struct apple_epic_service_ops dcp_v27_ext_av_services[] = { { .name = "dcpav-audio-interface-epic", .compact_full = "dcpav-audio-interface-epic:0", .singleton = true, .init = dcp_v27_ext_service_init, .teardown = dcp_v27_ext_av_teardown }, - { .name = "dcpav-cec-interface-epic", .compact_full = "dcpav-cec-interface-epic:0", - .singleton = true, .init = dcp_v27_ext_service_init, - .teardown = dcp_v27_ext_av_teardown }, + DCP_CEC_COMPACT_SERVICE_OPS, {}, }; diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index ecaaaf607d50a..7d0dc3acaae68 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -59,6 +59,7 @@ #include "afk.h" #include "av.h" +#include "cec.h" #include "dcp.h" #include "dcp-edr.h" #include "dcp-panel-adt.h" @@ -4396,6 +4397,19 @@ static int dcp_start_desktop_external(struct apple_dcp *dcp, u32 map) else if (dcp->avep) d->graph_started |= BIT(AV_AUDIO_ENDPOINT_DESKTOP - 0x20); } +#endif +#if IS_ENABLED(CONFIG_DRM_APPLE_CEC) + if (IS_ERR_OR_NULL(dcp->avep) && + dcp->fixed_connector_type == DRM_MODE_CONNECTOR_HDMIA && + (map & BIT(AV_AUDIO_ENDPOINT_DESKTOP - 0x20))) { + ret = dcp_cec_init_avep(dcp); + if (ret) + dcp_desktop_graph_skip(dcp, AV_AUDIO_ENDPOINT_DESKTOP, + "CEC", "CEC init failed", ret); + else if (dcp->avep) + d->graph_started |= BIT(AV_AUDIO_ENDPOINT_DESKTOP - 0x20); + } + dcp_cec_register(dcp); #endif ret = dcp_desktop_start_ext_ep(dcp, map, DPTX_HDCP_ENDPOINT, "DPTXHDCP"); @@ -4730,6 +4744,17 @@ int dcp_start(struct platform_device *pdev) ret = 0; } #endif +#if IS_ENABLED(CONFIG_DRM_APPLE_CEC) + if (!dcp_is_ext26(dcp) && !dcp->external_native) { + if (IS_ERR_OR_NULL(dcp->avep)) { + ret = dcp_cec_init_avep(dcp); + if (ret) + dev_warn(dcp->dev, "Failed to start AV CEC endpoint: %d\n", ret); + ret = 0; + } + dcp_cec_register(dcp); + } +#endif return ret; } @@ -6026,6 +6051,7 @@ VISIBLE_IF_KUNIT void dcp_disable_typec_work(struct apple_dcp *dcp, bool release disable_delayed_work_sync(&dcp->announce_wq); cancel_work_sync(&dcp->external_stop_work); cancel_delayed_work_sync(&dcp->external_stop_retry_wq); + dcp_hdmi_disable(dcp); disable_delayed_work_sync(&dcp->hdmi_settle_wq); disable_delayed_work_sync(&dcp->hdmi_recheck_wq); disable_delayed_work_sync(&dcp->link_recovery_wq); @@ -6036,7 +6062,6 @@ VISIBLE_IF_KUNIT void dcp_disable_typec_work(struct apple_dcp *dcp, bool release dcp->tb_event_generation = 0; } disable_delayed_work_sync(&dcp->hybrid_wq); - dcp_hdmi_disable(dcp); } VISIBLE_IF_KUNIT void dcp_enable_typec_work(struct apple_dcp *dcp) @@ -6668,6 +6693,9 @@ VISIBLE_IF_KUNIT void dcp_comp_unbind(struct device *dev, struct device *main, v */ if (dcp->external_native) disable_work_sync(&dcp->external_audio_work); +#if IS_ENABLED(CONFIG_DRM_APPLE_CEC) + dcp_cec_unregister(dcp); +#endif av_audiosrv_quiesce(dcp); if (READ_ONCE(dcp->ext_dead)) return; diff --git a/drivers/gpu/drm/apple/dcp_cec_test.c b/drivers/gpu/drm/apple/dcp_cec_test.c new file mode 100644 index 0000000000000..cc9bd7a89aaa9 --- /dev/null +++ b/drivers/gpu/drm/apple/dcp_cec_test.c @@ -0,0 +1,359 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* + * Standalone KUnit tests for Apple Silicon DCP HDMI-CEC wire framing, + * TX status mapping, report parsing, and 2 ms IRQ_HPD pulse classification. + */ + +#include +#include +#include +#include + +#include "cec-proto.h" +#include "dcp-hdmi.h" + +static void build_epic_report(u8 *buf, u16 group, u32 cmd, u32 body_len, + u32 magic) +{ + struct epic_service_call *call = (struct epic_service_call *)buf; + + memset(buf, 0, sizeof(*call) + body_len); + call->group = cpu_to_le16(group); + call->command = cpu_to_le32(cmd); + call->data_len = cpu_to_le32(body_len); + call->magic = cpu_to_le32(magic); +} + +static void cec_open_close_decode_test(struct kunit *test) +{ + u8 open_resp[DCP_CEC_OPEN_LEN] = {}; + u8 close_resp[DCP_CEC_CLOSE_LEN] = {}; + + /* OPEN requires status == 0 at +0x00 and opened != 0 at +0x10. */ + KUNIT_EXPECT_EQ(test, dcp_cec_decode_open_resp(open_resp), -EIO); + + put_unaligned_le64(1, open_resp + 0x10); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_open_resp(open_resp), 0); + + put_unaligned_le32(0xe00002bcU, open_resp + 0x00); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_open_resp(open_resp), -EIO); + + /* CLOSE checks status == 0 at +0x00. */ + KUNIT_EXPECT_EQ(test, dcp_cec_decode_status_resp(close_resp), 0); + put_unaligned_le32(1, close_resp + 0x00); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_status_resp(close_resp), -EIO); +} + +static void cec_la_mask_framing_test(struct kunit *test) +{ + u8 req[DCP_CEC_SET_LA_MASK_LEN]; + unsigned int i; + + /* Enabled with no unicast LA claimed still sets broadcast (0x8000). */ + dcp_cec_build_la_mask_req(req, true, 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(req + 0x10), 0x8000); + for (i = 0; i < sizeof(req); i++) { + if (i == 0x10 || i == 0x11) + continue; + KUNIT_EXPECT_EQ(test, req[i], 0); + } + + /* Enabled with Playback 1 (LA 4) and Playback 2 (LA 8) -> 0x8110. */ + dcp_cec_build_la_mask_req(req, true, BIT(4) | BIT(8)); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(req + 0x10), 0x8110); + + /* Disabled clears mask to 0x0000 even if LAs were passed. */ + dcp_cec_build_la_mask_req(req, false, BIT(4) | BIT(8)); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(req + 0x10), 0x0000); +} + +static void cec_edid_decode_test(struct kunit *test) +{ + u8 edid_resp[DCP_CEC_EDID_ATTRS_LEN] = {}; + + put_unaligned_le16(0x2100, edid_resp + 0x00); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_edid_attrs(edid_resp), 0x2100); + + put_unaligned_le16(CEC_PHYS_ADDR_INVALID, edid_resp + 0x00); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_edid_attrs(edid_resp), + CEC_PHYS_ADDR_INVALID); +} + +static void cec_send_framing_test(struct kunit *test) +{ + u8 req[DCP_CEC_SEND_LEN]; + struct cec_msg msg = {}; + unsigned int len, i; + + /* Invalid lengths are rejected. */ + msg.len = 0; + KUNIT_EXPECT_EQ(test, dcp_cec_build_send_req(req, &msg, 2), -EINVAL); + msg.len = 17; + KUNIT_EXPECT_EQ(test, dcp_cec_build_send_req(req, &msg, 2), -EINVAL); + + /* Lengths 1..16 encode (len - 1) & 0x0f at +0x10. */ + for (len = 1; len <= CEC_MAX_MSG_SIZE; len++) { + memset(&msg, 0, sizeof(msg)); + msg.len = len; + for (i = 0; i < len; i++) + msg.msg[i] = 0x40 + i; + + KUNIT_ASSERT_EQ(test, dcp_cec_build_send_req(req, &msg, 2), 0); + KUNIT_EXPECT_MEMEQ(test, req + 0x00, msg.msg, len); + for (i = len; i < CEC_MAX_MSG_SIZE; i++) + KUNIT_EXPECT_EQ(test, req[i], 0); + KUNIT_EXPECT_EQ(test, req[0x10], len - 1); + KUNIT_EXPECT_EQ(test, req[0x20], 1); + } + + /* Retry count encoding at +0x20: min(5, attempts ? attempts - 1 : 0). */ + msg.len = 2; + msg.msg[0] = 0x40; + msg.msg[1] = 0x83; + KUNIT_ASSERT_EQ(test, dcp_cec_build_send_req(req, &msg, 0), 0); + KUNIT_EXPECT_EQ(test, req[0x20], 0); + KUNIT_ASSERT_EQ(test, dcp_cec_build_send_req(req, &msg, 1), 0); + KUNIT_EXPECT_EQ(test, req[0x20], 0); + KUNIT_ASSERT_EQ(test, dcp_cec_build_send_req(req, &msg, 4), 0); + KUNIT_EXPECT_EQ(test, req[0x20], 3); + KUNIT_ASSERT_EQ(test, dcp_cec_build_send_req(req, &msg, 6), 0); + KUNIT_EXPECT_EQ(test, req[0x20], 5); + KUNIT_ASSERT_EQ(test, dcp_cec_build_send_req(req, &msg, 12), 0); + KUNIT_EXPECT_EQ(test, req[0x20], 5); +} + +static void cec_tx_status_mapping_test(struct kunit *test) +{ + u8 resp[DCP_CEC_SEND_LEN] = {}; + + put_unaligned_le32(DCP_CEC_RET_OK, resp + 0x30); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_send_resp(0, resp), + CEC_TX_STATUS_OK); + + put_unaligned_le32(DCP_CEC_RET_NACK, resp + 0x30); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_send_resp(0, resp), + CEC_TX_STATUS_NACK | CEC_TX_STATUS_MAX_RETRIES); + + put_unaligned_le32(DCP_CEC_RET_LINE_ERR, resp + 0x30); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_send_resp(0, resp), + CEC_TX_STATUS_ERROR | CEC_TX_STATUS_MAX_RETRIES); + + put_unaligned_le32(DCP_CEC_RET_TIMEOUT, resp + 0x30); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_send_resp(0, resp), + CEC_TX_STATUS_TIMEOUT | CEC_TX_STATUS_MAX_RETRIES); + + put_unaligned_le32(DCP_CEC_RET_OFFLINE, resp + 0x30); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_send_resp(0, resp), + CEC_TX_STATUS_ABORTED); + + put_unaligned_le32(DCP_CEC_RET_ABORTED, resp + 0x30); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_send_resp(0, resp), + CEC_TX_STATUS_ABORTED); + + put_unaligned_le32(0xdeadbeefU, resp + 0x30); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_send_resp(0, resp), + CEC_TX_STATUS_ERROR | CEC_TX_STATUS_MAX_RETRIES); + + /* Negative AFK return maps to ERROR | MAX_RETRIES regardless of body. */ + put_unaligned_le32(DCP_CEC_RET_OK, resp + 0x30); + KUNIT_EXPECT_EQ(test, dcp_cec_decode_send_resp(-ETIMEDOUT, resp), + CEC_TX_STATUS_ERROR | CEC_TX_STATUS_MAX_RETRIES); +} + +static void cec_report_parsing_test(struct kunit *test) +{ + const size_t hdr = sizeof(struct epic_service_call); + u8 buf[sizeof(struct epic_service_call) + DCP_CEC_RPT_EDID_ATTRS_LEN]; + struct dcp_cec_parsed_report rpt; + unsigned int i; + + /* Terminate (cmd 1, 0x20 B). */ + build_epic_report(buf, DCP_CEC_GROUP_REPORT, DCP_CEC_RPT_TERMINATE, + DCP_CEC_RPT_TERMINATE_LEN, EPIC_SERVICE_CALL_MAGIC); + KUNIT_ASSERT_EQ(test, + dcp_cec_parse_report(buf, + hdr + DCP_CEC_RPT_TERMINATE_LEN, + &rpt), + 0); + KUNIT_EXPECT_EQ(test, rpt.cmd, DCP_CEC_RPT_TERMINATE); + + /* RX_FRAME (cmd 2, 0x20 B): 1-byte polling frame and 16-byte frame. */ + build_epic_report(buf, DCP_CEC_GROUP_REPORT, DCP_CEC_RPT_RX_FRAME, + DCP_CEC_RPT_RX_FRAME_LEN, EPIC_SERVICE_CALL_MAGIC); + buf[hdr + 0x00] = 0x04; + buf[hdr + 0x10] = 0xf0; /* upper nibble masked off */ + KUNIT_ASSERT_EQ(test, + dcp_cec_parse_report(buf, + hdr + DCP_CEC_RPT_RX_FRAME_LEN, + &rpt), + 0); + KUNIT_EXPECT_EQ(test, rpt.cmd, DCP_CEC_RPT_RX_FRAME); + KUNIT_EXPECT_EQ(test, rpt.rx_msg.len, 1); + KUNIT_EXPECT_EQ(test, rpt.rx_msg.msg[0], 0x04); + + for (i = 0; i < CEC_MAX_MSG_SIZE; i++) + buf[hdr + i] = 0x10 + i; + buf[hdr + 0x10] = 0x0f; + KUNIT_ASSERT_EQ(test, + dcp_cec_parse_report(buf, + hdr + DCP_CEC_RPT_RX_FRAME_LEN, + &rpt), + 0); + KUNIT_EXPECT_EQ(test, rpt.rx_msg.len, 16); + KUNIT_EXPECT_EQ(test, rpt.rx_msg.msg[15], 0x1f); + + /* HPD (cmd 3, 0x10 B). */ + build_epic_report(buf, DCP_CEC_GROUP_REPORT, DCP_CEC_RPT_HPD, + DCP_CEC_RPT_HPD_LEN, EPIC_SERVICE_CALL_MAGIC); + buf[hdr + 0x00] = 1; + KUNIT_ASSERT_EQ(test, + dcp_cec_parse_report(buf, hdr + DCP_CEC_RPT_HPD_LEN, + &rpt), + 0); + KUNIT_EXPECT_EQ(test, rpt.cmd, DCP_CEC_RPT_HPD); + KUNIT_EXPECT_TRUE(test, rpt.hpd); + + /* EDID_ATTRS (cmd 4, 0x50 B). */ + build_epic_report(buf, DCP_CEC_GROUP_REPORT, DCP_CEC_RPT_EDID_ATTRS, + DCP_CEC_RPT_EDID_ATTRS_LEN, EPIC_SERVICE_CALL_MAGIC); + put_unaligned_le16(0x3200, buf + hdr + 0x00); + KUNIT_ASSERT_EQ(test, + dcp_cec_parse_report(buf, sizeof(buf), &rpt), 0); + KUNIT_EXPECT_EQ(test, rpt.cmd, DCP_CEC_RPT_EDID_ATTRS); + KUNIT_EXPECT_EQ(test, rpt.phys_addr, 0x3200); + + /* Reject bad magic, wrong group, and truncated bodies. */ + build_epic_report(buf, DCP_CEC_GROUP_REPORT, DCP_CEC_RPT_HPD, + DCP_CEC_RPT_HPD_LEN, 0x12345678U); + KUNIT_EXPECT_EQ(test, + dcp_cec_parse_report(buf, hdr + DCP_CEC_RPT_HPD_LEN, + &rpt), + -EINVAL); + + build_epic_report(buf, DCP_CEC_GROUP_CALL, DCP_CEC_RPT_HPD, + DCP_CEC_RPT_HPD_LEN, EPIC_SERVICE_CALL_MAGIC); + KUNIT_EXPECT_EQ(test, + dcp_cec_parse_report(buf, hdr + DCP_CEC_RPT_HPD_LEN, + &rpt), + -EINVAL); + + build_epic_report(buf, DCP_CEC_GROUP_REPORT, DCP_CEC_RPT_EDID_ATTRS, + DCP_CEC_RPT_EDID_ATTRS_LEN, EPIC_SERVICE_CALL_MAGIC); + KUNIT_EXPECT_EQ(test, + dcp_cec_parse_report(buf, hdr + 0x20, &rpt), + -EMSGSIZE); +} + +static void hdmi_irq_hpd_pulse_classify_test(struct kunit *test) +{ + u64 base = 10 * NSEC_PER_MSEC; + bool late_rising = false; + u64 claimed_ns = 0; + u64 falling_ns = 0; + + /* 0.7 ms converter IRQ_HPD pulse: arm on falling, forward on rising. */ + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, base, 0), + DCP_HDMI_PULSE_ARM_TIMER); + KUNIT_EXPECT_EQ(test, falling_ns, base); + + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, + base + 700 * NSEC_PER_USEC, 1), + DCP_HDMI_PULSE_SHORT); + KUNIT_EXPECT_EQ(test, falling_ns, 0ULL); + + /* Sub-100 us pulse (or delayed falling hardirq) still forwards IRQ_HPD. */ + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, base, 0), + DCP_HDMI_PULSE_ARM_TIMER); + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, + base + 50 * NSEC_PER_USEC, 1), + DCP_HDMI_PULSE_SHORT); + KUNIT_EXPECT_EQ(test, falling_ns, 0ULL); + + /* Pulse that already rose before GPIO was sampled forwards IRQ_HPD. */ + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, base, 1), + DCP_HDMI_PULSE_SHORT); + + /* Near 2.0 ms: if hrtimer already counted the pulse, do not double count. */ + claimed_ns = base + 2 * NSEC_PER_MSEC; + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, + base + 2 * NSEC_PER_MSEC + + 10 * NSEC_PER_USEC, 1), + DCP_HDMI_PULSE_CLAIMED); + KUNIT_EXPECT_EQ(test, claimed_ns, 0ULL); + + /* Stale claimed_ns (> 2 ms ago) never swallows a later fast pulse. */ + claimed_ns = base + 2 * NSEC_PER_MSEC; + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, + base + 10 * NSEC_PER_MSEC, 1), + DCP_HDMI_PULSE_SHORT); + KUNIT_EXPECT_EQ(test, claimed_ns, 0ULL); + + /* Exact 2.0 ms boundary is still accepted as IRQ_HPD. */ + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, base, 0), + DCP_HDMI_PULSE_ARM_TIMER); + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, + base + DCP_HDMI_IRQ_HPD_MAX_NS, + 1), + DCP_HDMI_PULSE_SHORT); + + /* > 2.0 ms low pulse passes to the threaded HPD presence handler. */ + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, base, 0), + DCP_HDMI_PULSE_ARM_TIMER); + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, + base + DCP_HDMI_IRQ_HPD_MAX_NS + 1, + 1), + DCP_HDMI_PULSE_PASS); + + /* Rising edge after 2 ms timer already expired low wakes HPD thread. */ + falling_ns = 0; + late_rising = true; + KUNIT_EXPECT_EQ(test, + dcp_hdmi_classify_edge(&falling_ns, &late_rising, + &claimed_ns, + base + 20 * NSEC_PER_MSEC, 1), + DCP_HDMI_PULSE_PASS); + KUNIT_EXPECT_FALSE(test, late_rising); +} + +static struct kunit_case dcp_cec_cases[] = { + KUNIT_CASE(cec_open_close_decode_test), + KUNIT_CASE(cec_la_mask_framing_test), + KUNIT_CASE(cec_edid_decode_test), + KUNIT_CASE(cec_send_framing_test), + KUNIT_CASE(cec_tx_status_mapping_test), + KUNIT_CASE(cec_report_parsing_test), + KUNIT_CASE(hdmi_irq_hpd_pulse_classify_test), + {} +}; + +static struct kunit_suite dcp_cec_suite = { + .name = "apple-dcp-cec", + .test_cases = dcp_cec_cases, +}; + +kunit_test_suite(dcp_cec_suite); + +MODULE_DESCRIPTION("KUnit tests for Apple Silicon DCP HDMI-CEC and IRQ_HPD"); +MODULE_LICENSE("Dual MIT/GPL"); diff --git a/drivers/gpu/drm/apple/dptxep.c b/drivers/gpu/drm/apple/dptxep.c index 71bf6ea2e995e..ab02ec25eb19e 100644 --- a/drivers/gpu/drm/apple/dptxep.c +++ b/drivers/gpu/drm/apple/dptxep.c @@ -15,6 +15,7 @@ #include #include "afk.h" +#include "cec.h" #include "dcp.h" #include "dcp-hdmi.h" #include "dptxep.h" @@ -232,6 +233,7 @@ int dptxport_release_display(struct apple_epic_service *service) { dcp_180hz_invalidate(service->ep->dcp); dcpavserv_v14_close(service->ep->dcp); + dcp_cec_close_port(service->ep->dcp, service); return afk_service_call(service, 0, 7, NULL, 0, 16, NULL, 0, 16); } @@ -242,9 +244,11 @@ int dptxport_set_hpd_timeout(struct apple_epic_service *service, bool hpd, size_t received; int ret; - /* The 14.7 external role completes an HPD low only once its AV client closed. */ - if (!hpd) + /* Close active AV and CEC clients before telling firmware HPD went low. */ + if (!hpd) { dcpavserv_v14_close(service->ep->dcp); + dcp_cec_close_port(service->ep->dcp, service); + } memset(&cmd, 0, sizeof(cmd)); if (hpd) diff --git a/drivers/gpu/drm/apple/iomfb.c b/drivers/gpu/drm/apple/iomfb.c index 75f58c0327d1a..b01746bdb09b1 100644 --- a/drivers/gpu/drm/apple/iomfb.c +++ b/drivers/gpu/drm/apple/iomfb.c @@ -37,6 +37,7 @@ #include #include +#include "cec.h" #include "dcp.h" #include "dcp-hdr-metadata.h" #include "dcp-internal.h" @@ -2036,6 +2037,8 @@ void dcp_hotplug(struct work_struct *work) } dcp_test_point(dcp, DCP_TEST_HOTPLUG_EVENT); + if (connected && connector == dcp->fixed_connector) + dcp_cec_poll_edid(dcp); /* The announcement already decided what an H17P external reports. */ if (gated) drm_kms_helper_connector_hotplug_event(&connector->base); 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 b21cba6c7665c..bdfd0f2325ba5 100644 --- a/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-hpd.py +++ b/tools/testing/selftests/drivers/gpu/apple-afkv2/extract-hpd.py @@ -80,13 +80,19 @@ def function(source, name): static u32 echo_flip, compact_status; /* An HPD low closes the 14.7 external AV client first; the stub counts it. */ static struct dcp *expected_dcp; -static unsigned int v14_closes, closes_at_send; +static unsigned int v14_closes, cec_closes, closes_at_send, cec_closes_at_send; /* Kept even if a mutant drops the call, so the count check is what fails. */ __attribute__((unused)) static void dcpavserv_v14_close(struct dcp *dcp) { assert(dcp == expected_dcp); v14_closes++; } +__attribute__((unused)) static void dcp_cec_close_port(struct dcp *dcp, + struct apple_epic_service *service) +{ + assert(dcp == expected_dcp && service != NULL); + cec_closes++; +} 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) @@ -100,6 +106,7 @@ def function(source, name): assert(output_len == sizeof(*call) + 32 && call->data_len == 32); assert(call->group == 8 && call->command == 8); closes_at_send = v14_closes; + cec_closes_at_send = cec_closes; if (wire_v2 && !received && arrived < output_len - sizeof(*call)) return -EMSGSIZE; memcpy(&echo, body + 16, sizeof(echo)); @@ -130,11 +137,12 @@ def function(source, name): /* Every request closes the AV client exactly when it lowers HPD, and before it is sent. */ static int set_hpd(struct apple_epic_service *service, int hpd) { - unsigned int before = v14_closes; + unsigned int before = v14_closes, cec_before = cec_closes; int ret = dptxport_set_hpd_timeout(service, hpd, 1000); - expect(v14_closes == before + !hpd && closes_at_send == before + !hpd, - "AV client closed before an HPD low only", hpd, ret); + expect(v14_closes == before + !hpd && closes_at_send == before + !hpd && + cec_closes == cec_before + !hpd && cec_closes_at_send == cec_before + !hpd, + "AV/CEC clients closed before an HPD low only", hpd, ret); return ret; } int main(void) diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/desktop.py b/tools/testing/selftests/drivers/gpu/apple-dcp/desktop.py index d540d6dd08b6d..11e2862e635ca 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/desktop.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/desktop.py @@ -591,8 +591,10 @@ def powerlog_framing(root): system = (root / 'drivers/gpu/drm/apple/systemep.c').read_text() prefix = f['prefix'] + f['metadata'] + f['epoch'].replace('static inline ', 'static inline __attribute__((unused)) ') + f['header'] + f['edid_header'] + f['parser'] prefix += block(header, 'struct dcp_system_ev_mnits {') + ';\n' + prefix += '#define __le64 u64\n#define EPIC_SERVICE_CALL_MAGIC 0x69706378\n' prefix += block(afkh, 'struct epic_compact_sub_hdr {') + ';\n' prefix += block(afkh, 'struct epic_compact_desc {') + ';\n' + prefix += block(afkh, 'struct epic_service_call {') + ' __attribute__((packed));\n' prefix += '#undef kfree\n#define kfree(p) free((void *)(p))\n#define pr_err(...) ((void)0)\n' prefix += block(parser, 'int parse_system_log_mnits(') prefix += block(parser, 'int parse_system_log_mnits_pairs(') @@ -606,7 +608,6 @@ def powerlog_framing(root): #define dev_dbg(...) ((void)0) #define dev_info(...) ((void)0) #define dev_warn(...) ((void)0) -#define __le64 u64 #define EPIC_SUBTYPE_STD_SERVICE 0xc0 enum epic_subtype { REPORT = 0xc0 }; struct work_struct { unsigned scheduled; }; @@ -631,10 +632,20 @@ def powerlog_framing(root): {(void)e;(void)id;(void)p;(void)n;assert(false);} static void afk_recv_handle_compact_reply(struct apple_dcp_afkep *e,u16 id,u8 t,const void *p,size_t n) {(void)e;(void)id;(void)t;(void)p;(void)n;assert(false);} +static unsigned call_reports;static u32 call_magic,call_cmd; +static int call_report_cb(struct apple_epic_service *s,enum epic_subtype t,const void *p,size_t n) +{ + const struct epic_service_call *c=p; + (void)s;(void)t; + assert(n==sizeof(*c)+0x10); + call_reports++;call_magic=get_unaligned_le32(&c->magic);call_cmd=get_unaligned_le32(&c->command); + return 0; +} ''' functions = '\n'.join(block(source, name) for source, name in ( (system, 'static int powerlog_report('), (afk, 'static bool afk_compact_report_dictionary('), + (afk, 'static bool afk_compact_report_service_call('), (afk, 'static void afk_recv_compact_message('))) return prefix + '\n' + functions + '\nstatic const u8 report[]={'+','.join(map(str,raw))+'};\n' + r''' int main(void) @@ -680,6 +691,24 @@ def powerlog_framing(root): memcpy(wire+16,report,sizeof(report)); afk_recv_compact_message(&ep,3,wire,16+sizeof(report)); assert(hints==4 && hint==123000 && !service.users); + /* Bare and descriptor-wrapped EPIC service-call reports both preserve their header. */ + { + u8 cwire[16+sizeof(struct epic_compact_desc)+sizeof(struct epic_service_call)+0x10]={0}; + struct epic_compact_sub_hdr *csub=(void *)(cwire+8); + struct epic_service_call *ccall=(void *)(cwire+16); + csub->category=0;csub->type=EPIC_SUBTYPE_STD_SERVICE; + ops.report=call_report_cb; + ccall->group=2;ccall->command=3;ccall->data_len=0x10; + ccall->magic=EPIC_SERVICE_CALL_MAGIC; + afk_recv_compact_message(&ep,3,cwire,16+sizeof(*ccall)+0x10); + assert(call_reports==1 && call_magic==EPIC_SERVICE_CALL_MAGIC && call_cmd==3 && !service.users); + memset(cwire+16,0,sizeof(cwire)-16); + ccall=(void *)(cwire+16+sizeof(struct epic_compact_desc)); + ccall->group=2;ccall->command=4;ccall->data_len=0x10; + ccall->magic=EPIC_SERVICE_CALL_MAGIC; + afk_recv_compact_message(&ep,3,cwire,sizeof(cwire)); + assert(call_reports==2 && call_magic==EPIC_SERVICE_CALL_MAGIC && call_cmd==4 && !service.users); + } return 0; } '''