diff --git a/Documentation/devicetree/bindings/display/apple,dcp.yaml b/Documentation/devicetree/bindings/display/apple,dcp.yaml index 57616c37cda25..eba0a74ce6783 100644 --- a/Documentation/devicetree/bindings/display/apple,dcp.yaml +++ b/Documentation/devicetree/bindings/display/apple,dcp.yaml @@ -387,6 +387,17 @@ properties: $ref: /schemas/types.yaml#/definitions/uint32 description: Display coprocessor index on multi-DCP systems. + apple,hdcp-channels: + $ref: /schemas/types.yaml#/definitions/uint32-array + minItems: 1 + maxItems: 2 + items: + maximum: 14 + description: + The Secure Enclave's HDCP interface channels assigned to this display + coprocessor. The first is the HDCP 2.2 transmitter channel of its + external output. Absent when the coprocessor has no HDCP transmitter. + apple,sleep-host-stop: type: boolean description: diff --git a/arch/arm64/boot/dts/apple/t604x-extdisp.dtsi b/arch/arm64/boot/dts/apple/t604x-extdisp.dtsi index 3b60ac6afcb78..1cec4e06a66d7 100644 --- a/arch/arm64/boot/dts/apple/t604x-extdisp.dtsi +++ b/arch/arm64/boot/dts/apple/t604x-extdisp.dtsi @@ -18,6 +18,7 @@ mboxes = <&dcpext0_mbox>; mbox-names = "mbox"; iommus = <&dcpext0_dart 23>; + apple,hdcp-channels = <7 8>; /* guessed: the internal DCP uses 0x20000, J700 DISPEXT0 is +0x8000 */ apple,bw-scratch = <&pmgr_dcp 0 4 0x28000>; /* firmware Disp_Index, the dual-pipe endpoint ID */ @@ -56,6 +57,7 @@ mboxes = <&dcpext1_mbox>; mbox-names = "mbox"; iommus = <&dcpext1_dart 23>; + apple,hdcp-channels = <9 10>; /* guessed, as dcpext0 */ apple,bw-scratch = <&pmgr_dcp 0 4 0x30000>; apple,dcp-endpoint = <2>; diff --git a/arch/arm64/boot/dts/apple/t604x.dtsi b/arch/arm64/boot/dts/apple/t604x.dtsi index b269473755b87..b67f4c3e10dd2 100644 --- a/arch/arm64/boot/dts/apple/t604x.dtsi +++ b/arch/arm64/boot/dts/apple/t604x.dtsi @@ -667,6 +667,7 @@ mboxes = <&dcp_mbox>; mbox-names = "mbox"; iommus = <&dcp_dart 23>; + apple,hdcp-channels = <5 6>; apple,bw-scratch = <&pmgr_dcp 0 4 0x20000>; apple,iomfb-surfaces = <1 1 0 0>; /* the boot loader describes the piodma's regions with 2+2 cells */ diff --git a/arch/arm64/boot/dts/apple/t8112.dtsi b/arch/arm64/boot/dts/apple/t8112.dtsi index fd13d3c654e93..9d06d392525c1 100644 --- a/arch/arm64/boot/dts/apple/t8112.dtsi +++ b/arch/arm64/boot/dts/apple/t8112.dtsi @@ -793,6 +793,7 @@ mboxes = <&dcp_mbox>; mbox-names = "mbox"; iommus = <&dcp_dart 5 0x8 0x00000000 0x7 0xffff0000>; + apple,hdcp-channels = <5 6>; /* the ADT has 2 additional regs which seems to be unused */ reg-names = "coproc", "disp-0", "disp-1", "disp-2", "disp-3"; @@ -2007,6 +2008,7 @@ mboxes = <&dcpext_mbox>; mbox-names = "mbox"; iommus = <&dcpext_dart 5 0x8 0x00000000 0x7 0xffff0000>; + apple,hdcp-channels = <7 8>; phandle = <&dcpext>; /* the ADT has 2 additional regs which seems to be unused */ diff --git a/drivers/gpu/drm/apple/Kconfig b/drivers/gpu/drm/apple/Kconfig index 689171db4ebe0..bff991d363a37 100644 --- a/drivers/gpu/drm/apple/Kconfig +++ b/drivers/gpu/drm/apple/Kconfig @@ -36,6 +36,21 @@ config DRM_APPLE_CEC 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_HDCP + bool "HDCP 2.2 content protection on external outputs" + default y + depends on DRM_APPLE + select DRM_DISPLAY_HELPER + select DRM_DISPLAY_HDCP_HELPER + help + Support the standard "Content Protection" and "HDCP Content Type" + connector properties on external DisplayPort, HDMI and USB-C + outputs. The SEP holds the transmitter's keys and does the HDCP 2.2 + cryptography; DCP carries the messages to the receiver. The + property is attached on display processors whose device tree node + has "apple,hdcp-channels", with the SEP HDCP service + (APPLE_SEP_HDCP_INTERFACE) built in. + 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 6098e8efb7c59..fa82d847891ef 100644 --- a/drivers/gpu/drm/apple/Makefile +++ b/drivers/gpu/drm/apple/Makefile @@ -41,6 +41,7 @@ appledrm-y += epic/dpavservep.o appledrm-y += epic/panelavep.o appledrm-y += plane.o appledrm-y += dcp-indicator.o +appledrm-$(CONFIG_DRM_APPLE_HDCP) += dcp-hdcp.o dcp-hdcp-tx.o appledrm-$(CONFIG_TRACING) += trace.o @@ -50,6 +51,8 @@ obj-$(CONFIG_DRM_APPLE_KUNIT_TEST) += afk_test.o obj-$(CONFIG_DRM_APPLE_KUNIT_TEST) += dcp_backlight_test.o 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) += dcp-hdcp-test.o +obj-$(CONFIG_DRM_APPLE_KUNIT_TEST) += dcp-hdcp-wire-test.o obj-$(CONFIG_DRM_APPLE_KUNIT_TEST) += afk-service-test.o obj-$(CONFIG_DRM_APPLE_KUNIT_TEST) += dcp_cec_test.o diff --git a/drivers/gpu/drm/apple/afk.c b/drivers/gpu/drm/apple/afk.c index 50c0c10bd5ea7..b21d34ac8cef6 100644 --- a/drivers/gpu/drm/apple/afk.c +++ b/drivers/gpu/drm/apple/afk.c @@ -24,6 +24,7 @@ #include #include "afk.h" +#include "dcp-hdcp-drv.h" #include "afkv2-codec.h" #include "afk_reply.h" #include "trace.h" @@ -458,7 +459,7 @@ afk_match_service(struct apple_dcp_afkep *ep, const char *name) return ops; } - return NULL; + return dcp_hdcp_match_service(ep, name, false); } static struct apple_epic_service *afk_epic_find_service(struct apple_dcp_afkep *ep, @@ -830,6 +831,7 @@ afk_compact_match_service(struct apple_dcp_afkep *ep, const char *name) const struct apple_epic_service_ops *ops, *any = NULL; const char *service_name; size_t len; + bool cut; if (!name[0] || !ep->ops) return NULL; @@ -856,13 +858,14 @@ afk_compact_match_service(struct apple_dcp_afkep *ep, const char *name) } /* An announcement that filled its field may have lost its end. */ - if (strlen(name) < AFK_COMPACT_NAME_LEN - 1) - return any; - for (ops = ep->ops; ops->name[0]; ops++) - if (afk_compact_cut_of(ops, service_name)) - return ops; + cut = strlen(name) >= AFK_COMPACT_NAME_LEN - 1; + if (cut) + for (ops = ep->ops; ops->name[0]; ops++) + if (afk_compact_cut_of(ops, service_name)) + return ops; - return any; + /* The HDCP services, by name, wherever they are announced. */ + return dcp_hdcp_match_service(ep, service_name, cut) ?: any; } /* @@ -1901,9 +1904,15 @@ static void afk_recv_handle_init(struct apple_dcp_afkep *ep, u32 channel, } } - if (ep->match_epic_name) + /* + * The HDCP services go by their EPIC name: their provider classes are + * shared with other services (the DP device class also names the AV + * device, the interface class names the port controllers). + */ + ops = dcp_hdcp_match_service(ep, epic_name, false); + if (!ops && ep->match_epic_name) ops = afk_match_service(ep, epic_name); - else + else if (!ops) ops = afk_match_service(ep, service_name); if (dcp_is_ext26(ep->dcp)) dev_dbg(ep->dcp->dev, @@ -3015,7 +3024,12 @@ static int afk_send_epic_token(struct apple_dcp_afkep *ep, u32 channel, u16 tag, if (ep->wire_v2) { if (!channel || channel > U16_MAX || tag > U8_MAX || payload_len > SZ_1M) return -EINVAL; - if (ecat == EPIC_CAT_REPORT) { + if (ecat == EPIC_CAT_REPORT && stype == EPIC_SUBTYPE_STD_SERVICE) { + /* A service report with its data (an HDCP session's progress). */ + if (!payload_len) + return -EINVAL; + v2_category = AFKV2_REPORT; + } else if (ecat == EPIC_CAT_REPORT) { if ((stype != AFKV2_OPEN && stype != AFKV2_CLOSE) || payload_len) return -EOPNOTSUPP; v2_flags = 1; diff --git a/drivers/gpu/drm/apple/apple_drv.c b/drivers/gpu/drm/apple/apple_drv.c index f39238a60bb22..2f7f86e5e0308 100644 --- a/drivers/gpu/drm/apple/apple_drv.c +++ b/drivers/gpu/drm/apple/apple_drv.c @@ -54,6 +54,7 @@ #include "apple_resume.h" #include "dcp-hdr-metadata.h" #include "dcp.h" +#include "dcp-hdcp-drv.h" #include "plane.h" #include "iomfb_m4.h" #include "t8132_ext_drm.h" @@ -1035,6 +1036,8 @@ static void apple_atomic_commit_tail(struct drm_atomic_commit *state) DRM_PLANE_COMMIT_ACTIVE_ONLY); drm_atomic_helper_fake_vblank(state); drm_atomic_helper_commit_hw_done(state); + /* Content Protection requests go to the display processors' queues. */ + dcp_hdcp_commit(state); drm_atomic_helper_wait_for_flip_done(state->dev, state); apple_fail_lost_flips(state); drm_atomic_helper_cleanup_planes(state->dev, state); @@ -1150,6 +1153,7 @@ static int apple_connector_atomic_check(struct drm_connector *conn, const struct drm_property_blob *blob = new->hdr_output_metadata; struct drm_crtc_state *crtc_state; + dcp_hdcp_atomic_check(conn, state); if (blob && (blob->length != sizeof(struct hdr_output_metadata) || !dcp_hdr_is_type1(blob->data))) return -EINVAL; @@ -1356,6 +1360,9 @@ static int apple_connector_create(struct drm_device *drm, dcp_ext_color_props_supported(dcp)); if (ret) goto err_cleanup; + ret = dcp_hdcp_attach_connector(connector, &dcp, 1, connector_type); + if (ret) + goto err_cleanup; connector->base.polled = DRM_CONNECTOR_POLL_HPD; connector->connected = false; @@ -1718,6 +1725,17 @@ static int apple_probe_typec_ports(struct drm_device *drm, connector->connected = false; connector->dcp = NULL; + /* Content Protection where a pipeline that can drive the port has HDCP. */ + for (i = 0; i < num_dcp; i++) { + if (pipeline ? dcp[i] != pipeline : + !dcp_typec_port_has_candidate(port_idx, dcp[i])) + continue; + ret = dcp_hdcp_attach_connector(connector, &dcp[i], 1, + DRM_MODE_CONNECTOR_DisplayPort); + if (ret) + return ret; + } + /* * One encoder per connector, covering its allowed pipelines. * T6050 connectors each cover exactly one. Attaching several diff --git a/drivers/gpu/drm/apple/connector.h b/drivers/gpu/drm/apple/connector.h index 5d8c1674503fc..ff01bb3b5cef4 100644 --- a/drivers/gpu/drm/apple/connector.h +++ b/drivers/gpu/drm/apple/connector.h @@ -89,6 +89,15 @@ struct apple_connector { * chosen one; -1 until then, and nothing is sent. */ int bl_level; + + /* + * Content Protection (dcp-hdcp-drv.h): the property is attached, and + * what the last commit asked for. Written by the commit tail, read by + * the display processor that drives the connector. + */ + bool hdcp_attached; + bool hdcp_want; + u8 hdcp_type; }; #define to_apple_connector(x) container_of(x, struct apple_connector, base) diff --git a/drivers/gpu/drm/apple/dcp-hdcp-drv.h b/drivers/gpu/drm/apple/dcp-hdcp-drv.h new file mode 100644 index 0000000000000..8b1600fcd74b4 --- /dev/null +++ b/drivers/gpu/drm/apple/dcp-hdcp-drv.h @@ -0,0 +1,79 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* Copyright 2026 Ryan Murray */ +#ifndef __APPLE_DCP_HDCP_DRV_H__ +#define __APPLE_DCP_HDCP_DRV_H__ + +#include +#include + +struct apple_connector; +struct apple_dcp; +struct apple_dcp_afkep; +struct apple_epic_service_ops; +struct drm_atomic_commit; +struct drm_connector; +struct platform_device; + +/* + * HDCP 2.2 on the display processors' external outputs: the DCP services + * (dcp-hdcp-wire.h), the transmitter (dcp-hdcp-tx.h) and the Content + * Protection policy (dcp-hdcp.h), joined to the connector properties. + */ +/* Whether something already drives @endpoint (dcp.c). */ +bool dcp_endpoint_routed(struct apple_dcp *dcp, u8 endpoint); + +#if IS_ENABLED(CONFIG_DRM_APPLE_HDCP) +/* A display processor's HDCP state, if it has an HDCP channel; idempotent. */ +struct dcp_hdcp *dcp_hdcp_init(struct apple_dcp *dcp); +void dcp_hdcp_fini(struct apple_dcp *dcp); +/* Starts the DP device and HDCP interface endpoints when no other path did. */ +void dcp_hdcp_start_endpoints(struct apple_dcp *dcp); +/* Routes the endpoints this file started. */ +struct apple_dcp_afkep *dcp_hdcp_route(struct apple_dcp *dcp, u32 endpoint); +/* The HDCP services, matched by name ahead of a catch-all. */ +const struct apple_epic_service_ops * +dcp_hdcp_match_service(struct apple_dcp_afkep *ep, const char *service_name, + bool cut); +/* Hotplug, power off or on, suspend, a new route: a new link generation. */ +void dcp_hdcp_link_changed(struct apple_dcp *dcp); +/* The same, and its teardown finished before returning (suspend, DPMS off). */ +void dcp_hdcp_quiesce(struct apple_dcp *dcp); + +int dcp_hdcp_attach_connector(struct apple_connector *connector, + struct platform_device **dcps, unsigned int n, + int connector_type); +void dcp_hdcp_atomic_check(struct drm_connector *conn, + struct drm_atomic_commit *state); +void dcp_hdcp_commit(struct drm_atomic_commit *state); +#else +static inline struct dcp_hdcp *dcp_hdcp_init(struct apple_dcp *dcp) { return NULL; } +static inline void dcp_hdcp_fini(struct apple_dcp *dcp) { } +static inline void dcp_hdcp_start_endpoints(struct apple_dcp *dcp) { } +static inline struct apple_dcp_afkep *dcp_hdcp_route(struct apple_dcp *dcp, + u32 endpoint) +{ + return NULL; +} + +static inline const struct apple_epic_service_ops * +dcp_hdcp_match_service(struct apple_dcp_afkep *ep, const char *service_name, + bool cut) +{ + return NULL; +} + +static inline void dcp_hdcp_link_changed(struct apple_dcp *dcp) { } +static inline void dcp_hdcp_quiesce(struct apple_dcp *dcp) { } +static inline int dcp_hdcp_attach_connector(struct apple_connector *connector, + struct platform_device **dcps, + unsigned int n, int connector_type) +{ + return 0; +} + +static inline void dcp_hdcp_atomic_check(struct drm_connector *conn, + struct drm_atomic_commit *state) { } +static inline void dcp_hdcp_commit(struct drm_atomic_commit *state) { } +#endif + +#endif /* __APPLE_DCP_HDCP_DRV_H__ */ diff --git a/drivers/gpu/drm/apple/dcp-hdcp-glue-test.c b/drivers/gpu/drm/apple/dcp-hdcp-glue-test.c new file mode 100644 index 0000000000000..0ed30dc541d5d --- /dev/null +++ b/drivers/gpu/drm/apple/dcp-hdcp-glue-test.c @@ -0,0 +1,175 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* Copyright 2026 Ryan Murray */ + +/* The HDCP services' matching and the session's answers. Included from dcp-hdcp.c. */ + +#include + +struct glue_fixture { + struct apple_dcp dcp; + struct apple_dcp_afkep ep; + struct dcp_hdcp h; + struct apple_epic_service service; +}; + +static struct glue_fixture *glue_new(struct kunit *test, bool with_hdcp) +{ + struct glue_fixture *g = kunit_kzalloc(test, sizeof(*g), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, g); + g->ep.dcp = &g->dcp; + g->service.ep = &g->ep; + g->h.dcp = &g->dcp; + if (with_hdcp) + g->dcp.hdcp = &g->h; + return g; +} + +static void glue_match_by_name(struct kunit *test) +{ + struct glue_fixture *g = glue_new(test, true); + const struct apple_epic_service_ops *ops; + + ops = dcp_hdcp_match_service(&g->ep, "dcpdp-device-epic:0", false); + KUNIT_ASSERT_NOT_NULL(test, ops); + KUNIT_EXPECT_PTR_EQ(test, (void *)ops->init, (void *)dcp_hdcp_relay_init); + ops = dcp_hdcp_match_service(&g->ep, "dcpdptx-hdcp-interface", false); + KUNIT_ASSERT_NOT_NULL(test, ops); + KUNIT_EXPECT_PTR_EQ(test, (void *)ops->init, (void *)dcp_hdcp_iface_init); + ops = dcp_hdcp_match_service(&g->ep, "dcpdptx-hdcp-auth-session", false); + KUNIT_ASSERT_NOT_NULL(test, ops); + KUNIT_EXPECT_PTR_EQ(test, (void *)ops->call_v2, (void *)dcp_hdcp_session_call); + + /* A cut long name matches only when the announcement filled its field. */ + KUNIT_EXPECT_NOT_NULL(test, dcp_hdcp_match_service(&g->ep, + "dcpdptx-hdcp-auth-sess", true)); + KUNIT_EXPECT_NULL(test, dcp_hdcp_match_service(&g->ep, + "dcpdptx-hdcp-auth-sess", false)); + /* Other names, and a longer name that only starts the same. */ + KUNIT_EXPECT_NULL(test, dcp_hdcp_match_service(&g->ep, "dcpav-device-epic", true)); + KUNIT_EXPECT_NULL(test, dcp_hdcp_match_service(&g->ep, "dcpdp-device-epicx", false)); + KUNIT_EXPECT_NULL(test, dcp_hdcp_match_service(&g->ep, NULL, false)); + + /* Without HDCP on this processor nothing is taken. */ + g = glue_new(test, false); + KUNIT_EXPECT_NULL(test, dcp_hdcp_match_service(&g->ep, "dcpdp-device-epic", false)); +} + +static void glue_session_without_hdcp_declines(struct kunit *test) +{ + struct glue_fixture *g = glue_new(test, false); + struct dcp_hdcp_word in = {}, out; + + KUNIT_EXPECT_EQ(test, dcp_hdcp_session_call(&g->service, 0, DCP_HDCP_AUTH_OPEN, + &in, sizeof(in), &out, sizeof(out)), 0); + KUNIT_EXPECT_EQ(test, le32_to_cpu(out.value), 0); + KUNIT_EXPECT_EQ(test, dcp_hdcp_session_call(&g->service, 0, 0x55, &in, + sizeof(in), &out, sizeof(out)), + -EOPNOTSUPP); +} + +/* A declined open in the slot of an earlier session leaves it not ours. */ +static void glue_declined_open_forgets_slot(struct kunit *test) +{ + struct glue_fixture *g = glue_new(test, true); + struct dcp_hdcp_word in = {}, out; + + mutex_init(&g->h.lock); + spin_lock_init(&g->h.svc_lock); + init_waitqueue_head(&g->h.ev_wq); + g->h.wq = alloc_ordered_workqueue("hdcp-test", 0); + KUNIT_ASSERT_NOT_NULL(test, g->h.wq); + INIT_WORK(&g->h.event_work, dcp_hdcp_event_work); + INIT_WORK(&g->h.teardown_work, dcp_hdcp_teardown_work); + INIT_WORK(&g->h.prop_work, dcp_hdcp_prop_work); + INIT_WORK(&g->h.caps_work, dcp_hdcp_caps_work); + g->h.session = &g->service; + + /* Nothing asks for protection here: the open is declined. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_session_call(&g->service, 0, DCP_HDCP_AUTH_OPEN, + &in, sizeof(in), &out, sizeof(out)), 0); + KUNIT_EXPECT_EQ(test, le32_to_cpu(out.value), 0); + KUNIT_EXPECT_NULL(test, g->h.session); + /* Its later events are answered and not acted on. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_session_call(&g->service, 0, DCP_HDCP_AUTH_H_PRIME, + &in, sizeof(in), &out, sizeof(out)), 0); + KUNIT_EXPECT_EQ(test, g->h.ev_bits, 0); + KUNIT_EXPECT_EQ(test, g->h.q_head, 0); + flush_workqueue(g->h.wq); + destroy_workqueue(g->h.wq); +} + +/* Sessions announced for HDCP 1.x are recorded, never taken, and forgotten on teardown. */ +static void glue_hdcp1_session_declined(struct kunit *test) +{ + struct glue_fixture *g = glue_new(test, true); + struct apple_epic_service other = { .ep = &g->ep }; + const struct apple_epic_service_ops *ops; + + spin_lock_init(&g->h.svc_lock); + ops = dcp_hdcp_match_service(&g->ep, "dcpdptx-hdcp-auth-session", false); + KUNIT_ASSERT_NOT_NULL(test, ops); + /* Every announced session keeps its own handler. */ + KUNIT_EXPECT_FALSE(test, ops->singleton); + + ops->init(&g->service, "dcpdptx-hdcp-auth-session", "TxHDCP1Session", 0); + ops->init(&other, "dcpdptx-hdcp-auth-session", "TxHDCP2Session", 0); + KUNIT_EXPECT_TRUE(test, dcp_hdcp_is_hdcp1(&g->h, &g->service)); + KUNIT_EXPECT_FALSE(test, dcp_hdcp_is_hdcp1(&g->h, &other)); + /* No class: taken for HDCP 2.2. */ + ops->init(&other, "dcpdptx-hdcp-auth-session", NULL, 0); + KUNIT_EXPECT_FALSE(test, dcp_hdcp_is_hdcp1(&g->h, &other)); + + dcp_hdcp_session_teardown(&g->service); + KUNIT_EXPECT_FALSE(test, dcp_hdcp_is_hdcp1(&g->h, &g->service)); +} + +/* A teardown asked for before DCP opened a newer session leaves that session alone. */ +static void glue_stale_teardown_skipped(struct kunit *test) +{ + struct glue_fixture *g = glue_new(test, true); + + spin_lock_init(&g->h.svc_lock); + g->h.td_seq = 1; + g->h.open_seq = 2; + g->h.tx.sep_open = true; + dcp_hdcp_teardown_work(&g->h.teardown_work); + KUNIT_EXPECT_TRUE(test, g->h.tx.sep_open); +} + +/* Unbind stops all work and unpublishes the state. */ +static void glue_fini_unpublishes(struct kunit *test) +{ + struct glue_fixture *g = glue_new(test, true); + + mutex_init(&g->h.lock); + spin_lock_init(&g->h.svc_lock); + init_waitqueue_head(&g->h.ev_wq); + INIT_WORK(&g->h.event_work, dcp_hdcp_event_work); + INIT_WORK(&g->h.teardown_work, dcp_hdcp_teardown_work); + INIT_WORK(&g->h.prop_work, dcp_hdcp_prop_work); + INIT_WORK(&g->h.relay_work, dcp_hdcp_relay_work); + INIT_WORK(&g->h.caps_work, dcp_hdcp_caps_work); + dcp_hdcp_fini(&g->dcp); + KUNIT_EXPECT_NULL(test, READ_ONCE(g->dcp.hdcp)); + KUNIT_EXPECT_TRUE(test, g->h.cancel); + /* Work queued after unbind never runs. */ + KUNIT_EXPECT_FALSE(test, queue_work(system_wq, &g->h.teardown_work)); + KUNIT_EXPECT_NULL(test, dcp_hdcp_match_service(&g->ep, "dcpdp-device-epic", false)); +} + +static struct kunit_case dcp_hdcp_glue_cases[] = { + KUNIT_CASE(glue_match_by_name), + KUNIT_CASE(glue_session_without_hdcp_declines), + KUNIT_CASE(glue_declined_open_forgets_slot), + KUNIT_CASE(glue_hdcp1_session_declined), + KUNIT_CASE(glue_stale_teardown_skipped), + KUNIT_CASE(glue_fini_unpublishes), + { } +}; + +static struct kunit_suite dcp_hdcp_glue_suite = { + .name = "apple-dcp-hdcp-glue", + .test_cases = dcp_hdcp_glue_cases, +}; +kunit_test_suite(dcp_hdcp_glue_suite); diff --git a/drivers/gpu/drm/apple/dcp-hdcp-test.c b/drivers/gpu/drm/apple/dcp-hdcp-test.c new file mode 100644 index 0000000000000..09650183487e7 --- /dev/null +++ b/drivers/gpu/drm/apple/dcp-hdcp-test.c @@ -0,0 +1,237 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* Copyright 2026 Ryan Murray */ + +#include +#include + +#include "dcp-hdcp.h" + +static const unsigned long T0 = 1000; + +static struct dcp_hdcp_link bound(void) +{ + struct dcp_hdcp_link l = { .relay = true, .sep = true }; + + return l; +} + +static void hdcp_property_needs_all(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, dcp_hdcp_attach_property(true, true, true)); + KUNIT_EXPECT_FALSE(test, dcp_hdcp_attach_property(false, true, true)); + KUNIT_EXPECT_FALSE(test, dcp_hdcp_attach_property(true, false, true)); + KUNIT_EXPECT_FALSE(test, dcp_hdcp_attach_property(true, true, false)); +} + +static void hdcp_unbound_declines(struct kunit *test) +{ + struct dcp_hdcp_link l = { }; + + KUNIT_EXPECT_EQ(test, dcp_hdcp_request(&l, true, 0, T0), DCP_HDCP_NONE); + KUNIT_EXPECT_FALSE(test, dcp_hdcp_open(&l, 1, T0)); + KUNIT_EXPECT_EQ(test, dcp_hdcp_property(&l), + DRM_MODE_CONTENT_PROTECTION_DESIRED); + l.relay = true; + KUNIT_EXPECT_FALSE(test, dcp_hdcp_open(&l, 1, T0)); + /* Both sides bound: ask DCP for a session. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_bind(&l, true, true, T0), DCP_HDCP_KICK); + KUNIT_EXPECT_TRUE(test, dcp_hdcp_open(&l, 1, T0)); +} + +static void hdcp_undesired_declines(struct kunit *test) +{ + struct dcp_hdcp_link l = bound(); + + KUNIT_EXPECT_EQ(test, dcp_hdcp_property(&l), + DRM_MODE_CONTENT_PROTECTION_UNDESIRED); + KUNIT_EXPECT_FALSE(test, dcp_hdcp_open(&l, 1, T0)); + KUNIT_EXPECT_EQ(test, dcp_hdcp_request(&l, true, 0, T0), DCP_HDCP_KICK); + KUNIT_EXPECT_TRUE(test, dcp_hdcp_open(&l, 1, T0)); + /* One session at a time. */ + KUNIT_EXPECT_FALSE(test, dcp_hdcp_open(&l, 1, T0)); +} + +static void hdcp_session_enables(struct kunit *test) +{ + struct dcp_hdcp_link l = bound(); + + dcp_hdcp_request(&l, true, 1, T0); + KUNIT_ASSERT_TRUE(test, dcp_hdcp_open(&l, 1, T0)); + KUNIT_EXPECT_EQ(test, dcp_hdcp_property(&l), + DRM_MODE_CONTENT_PROTECTION_DESIRED); + KUNIT_EXPECT_EQ(test, dcp_hdcp_auth_done(&l, 1, 0, T0), DCP_HDCP_NONE); + KUNIT_EXPECT_EQ(test, dcp_hdcp_property(&l), + DRM_MODE_CONTENT_PROTECTION_ENABLED); + KUNIT_EXPECT_EQ(test, l.type, 1); + + /* Userspace turns it off: teardown, UNDESIRED. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_request(&l, false, 1, T0), + DCP_HDCP_TEARDOWN); + KUNIT_EXPECT_EQ(test, dcp_hdcp_property(&l), + DRM_MODE_CONTENT_PROTECTION_UNDESIRED); + KUNIT_EXPECT_EQ(test, dcp_hdcp_request(&l, false, 1, T0), DCP_HDCP_NONE); +} + +static void hdcp_type_change_reauths(struct kunit *test) +{ + struct dcp_hdcp_link l = bound(); + + dcp_hdcp_request(&l, true, 0, T0); + dcp_hdcp_open(&l, 1, T0); + dcp_hdcp_auth_done(&l, 1, 0, T0); + /* Same type again: nothing. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_request(&l, true, 0, T0), DCP_HDCP_NONE); + KUNIT_EXPECT_EQ(test, l.state, DCP_HDCP_ENABLED); + /* Type 1 now: the running session goes, no backoff. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_request(&l, true, 1, T0), + DCP_HDCP_TEARDOWN); + KUNIT_EXPECT_EQ(test, l.state, DCP_HDCP_DESIRED); + KUNIT_EXPECT_EQ(test, l.failures, 0); + KUNIT_EXPECT_TRUE(test, dcp_hdcp_open(&l, 1, T0)); + KUNIT_EXPECT_EQ(test, l.type, 1); +} + +static void hdcp_failure_backs_off(struct kunit *test) +{ + struct dcp_hdcp_link l = bound(); + unsigned long t = T0; + unsigned int i; + + dcp_hdcp_request(&l, true, 0, t); + dcp_hdcp_open(&l, 1, t); + KUNIT_EXPECT_EQ(test, dcp_hdcp_auth_done(&l, 1, -EACCES, t), + DCP_HDCP_TEARDOWN); + KUNIT_EXPECT_EQ(test, l.state, DCP_HDCP_DESIRED); + /* DCP's next open is declined until the backoff is over. */ + KUNIT_EXPECT_FALSE(test, dcp_hdcp_open(&l, 1, t + 1)); + t += msecs_to_jiffies(DCP_HDCP_RETRY_MS); + KUNIT_EXPECT_TRUE(test, dcp_hdcp_open(&l, 1, t)); + + /* Doubling, capped. */ + for (i = 2; i < 12; i++) { + dcp_hdcp_auth_done(&l, 1, -EACCES, t); + KUNIT_EXPECT_EQ(test, l.failures, i); + t = l.retry_at; + KUNIT_EXPECT_TRUE(test, dcp_hdcp_open(&l, 1, t)); + } + KUNIT_EXPECT_EQ(test, dcp_hdcp_backoff(20), + msecs_to_jiffies(DCP_HDCP_RETRY_MAX_MS)); + KUNIT_EXPECT_EQ(test, dcp_hdcp_backoff(2), + msecs_to_jiffies(2 * DCP_HDCP_RETRY_MS)); + /* A success resets it. */ + dcp_hdcp_auth_done(&l, 1, 0, t); + KUNIT_EXPECT_EQ(test, l.failures, 0); +} + +static void hdcp_close_mid_auth(struct kunit *test) +{ + struct dcp_hdcp_link l = bound(); + + /* DCP gives up on an attempt (a receiver timeout): a failure. */ + dcp_hdcp_request(&l, true, 0, T0); + dcp_hdcp_open(&l, 1, T0); + KUNIT_EXPECT_EQ(test, dcp_hdcp_closed(&l, 1, T0), DCP_HDCP_TEARDOWN); + KUNIT_EXPECT_EQ(test, l.failures, 1); + KUNIT_EXPECT_EQ(test, dcp_hdcp_property(&l), + DRM_MODE_CONTENT_PROTECTION_DESIRED); + /* The attempt's late result changes nothing. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_auth_done(&l, 1, 0, T0), DCP_HDCP_NONE); + KUNIT_EXPECT_EQ(test, l.state, DCP_HDCP_DESIRED); + + /* Closing an enabled session (re-authentication) is no failure. */ + l = bound(); + dcp_hdcp_request(&l, true, 0, T0); + dcp_hdcp_open(&l, 1, T0); + dcp_hdcp_auth_done(&l, 1, 0, T0); + KUNIT_EXPECT_EQ(test, dcp_hdcp_closed(&l, 1, T0), DCP_HDCP_TEARDOWN); + KUNIT_EXPECT_EQ(test, l.failures, 0); + KUNIT_EXPECT_TRUE(test, dcp_hdcp_open(&l, 1, T0)); + /* A close for another generation is ignored. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_closed(&l, 7, T0), DCP_HDCP_NONE); + KUNIT_EXPECT_EQ(test, l.state, DCP_HDCP_AUTH); +} + +static void hdcp_link_change_drops_enabled(struct kunit *test) +{ + struct dcp_hdcp_link l = bound(); + + dcp_hdcp_request(&l, true, 0, T0); + dcp_hdcp_open(&l, 1, T0); + dcp_hdcp_auth_done(&l, 1, 0, T0); + KUNIT_EXPECT_EQ(test, l.state, DCP_HDCP_ENABLED); + + /* Same generation: nothing. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_link_event(&l, 1, T0), DCP_HDCP_NONE); + /* Hotplug, DPMS, resume: back to DESIRED, no backoff. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_link_event(&l, 2, T0), DCP_HDCP_TEARDOWN); + KUNIT_EXPECT_EQ(test, dcp_hdcp_property(&l), + DRM_MODE_CONTENT_PROTECTION_DESIRED); + KUNIT_EXPECT_TRUE(test, dcp_hdcp_open(&l, 2, T0)); + /* A result from generation 1 is late. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_auth_done(&l, 1, 0, T0), DCP_HDCP_NONE); + KUNIT_EXPECT_EQ(test, l.state, DCP_HDCP_AUTH); + /* A link change clears an earlier backoff. */ + dcp_hdcp_auth_done(&l, 2, -EACCES, T0); + KUNIT_EXPECT_EQ(test, l.failures, 1); + dcp_hdcp_link_event(&l, 3, T0); + KUNIT_EXPECT_EQ(test, l.failures, 0); + KUNIT_EXPECT_TRUE(test, dcp_hdcp_open(&l, 3, T0)); +} + +static void hdcp_unbind_drops(struct kunit *test) +{ + struct dcp_hdcp_link l = bound(); + + dcp_hdcp_request(&l, true, 0, T0); + dcp_hdcp_open(&l, 1, T0); + dcp_hdcp_auth_done(&l, 1, 0, T0); + KUNIT_EXPECT_EQ(test, dcp_hdcp_bind(&l, true, false, T0), + DCP_HDCP_TEARDOWN); + KUNIT_EXPECT_EQ(test, l.state, DCP_HDCP_DESIRED); + KUNIT_EXPECT_FALSE(test, dcp_hdcp_open(&l, 1, T0)); + KUNIT_EXPECT_EQ(test, dcp_hdcp_bind(&l, true, true, T0), DCP_HDCP_KICK); + KUNIT_EXPECT_TRUE(test, dcp_hdcp_open(&l, 1, T0)); +} + +static void hdcp_sep_busy_holds(struct kunit *test) +{ + struct dcp_hdcp_link l = bound(); + unsigned long hold = msecs_to_jiffies(DCP_HDCP_RETRY_MS); + + dcp_hdcp_request(&l, true, 0, T0); + dcp_hdcp_open(&l, 1, T0); + /* The SEP is busy: DESIRED, no failure counted, a short hold. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_auth_done(&l, 1, -EAGAIN, T0), + DCP_HDCP_TEARDOWN); + KUNIT_EXPECT_EQ(test, l.failures, 0); + KUNIT_EXPECT_EQ(test, dcp_hdcp_property(&l), + DRM_MODE_CONTENT_PROTECTION_DESIRED); + KUNIT_EXPECT_FALSE(test, dcp_hdcp_open(&l, 1, T0 + hold - 1)); + KUNIT_EXPECT_TRUE(test, dcp_hdcp_open(&l, 1, T0 + hold)); + dcp_hdcp_auth_done(&l, 1, 0, T0 + hold); + KUNIT_EXPECT_FALSE(test, l.hold); + KUNIT_EXPECT_EQ(test, l.state, DCP_HDCP_ENABLED); +} + +static struct kunit_case dcp_hdcp_cases[] = { + KUNIT_CASE(hdcp_property_needs_all), + KUNIT_CASE(hdcp_unbound_declines), + KUNIT_CASE(hdcp_undesired_declines), + KUNIT_CASE(hdcp_session_enables), + KUNIT_CASE(hdcp_type_change_reauths), + KUNIT_CASE(hdcp_failure_backs_off), + KUNIT_CASE(hdcp_close_mid_auth), + KUNIT_CASE(hdcp_link_change_drops_enabled), + KUNIT_CASE(hdcp_unbind_drops), + KUNIT_CASE(hdcp_sep_busy_holds), + { } +}; + +static struct kunit_suite dcp_hdcp_suite = { + .name = "apple-dcp-hdcp", + .test_cases = dcp_hdcp_cases, +}; +kunit_test_suite(dcp_hdcp_suite); + +MODULE_DESCRIPTION("KUnit tests for the Apple DCP HDCP policy"); +MODULE_LICENSE("Dual MIT/GPL"); diff --git a/drivers/gpu/drm/apple/dcp-hdcp-tx-test.c b/drivers/gpu/drm/apple/dcp-hdcp-tx-test.c new file mode 100644 index 0000000000000..344adca03ea35 --- /dev/null +++ b/drivers/gpu/drm/apple/dcp-hdcp-tx-test.c @@ -0,0 +1,581 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* Copyright 2026 Ryan Murray */ + +/* + * The transmitter procedure against a fake receiver (its HDCP 2.2 register + * map) and a fake SEP. Included from dcp-hdcp-tx.c. + */ + +#include + +#define FAKE_REG_BASE DP_HDCP_2_2_REG_RTX_OFFSET +#define FAKE_REG_SIZE 0x600 +#define FAKE_LOG 64 + +struct fake_write { + u32 addr; + size_t len; + u8 first; +}; + +struct fake { + struct kunit *test; + u8 regs[FAKE_REG_SIZE]; + + struct fake_write writes[FAKE_LOG]; + unsigned int nwrites; + enum dcp_hdcp_progress_code reports[FAKE_LOG]; + unsigned int nreports; + char sep[FAKE_LOG]; /* one letter per SEP call */ + unsigned int nsep; + unsigned int slept_ms; + + unsigned long events; /* BIT(op): DCP sends it */ + int cancel_op; /* -1, or the op whose wait is cancelled */ + bool stored; + int revoked_ids; /* returned by revoked() */ + unsigned int m_rejects; /* M' checks the SEP fails first */ + unsigned int l_rejects; /* L' checks the SEP fails first */ + bool sep_busy; /* the SEP is not available */ + bool encryption; + bool closed; + bool read_rxstatus; /* the TX read RxStatus */ +}; + +static int fake_read(void *ctx, u32 addr, void *buf, size_t len) +{ + struct fake *f = ctx; + + if (addr < FAKE_REG_BASE || addr + len > FAKE_REG_BASE + FAKE_REG_SIZE) + return -EIO; + if (addr == DP_HDCP_2_2_REG_RXSTATUS_OFFSET) + f->read_rxstatus = true; + memcpy(buf, f->regs + addr - FAKE_REG_BASE, len); + return 0; +} + +static int fake_write(void *ctx, u32 addr, const void *buf, size_t len) +{ + struct fake *f = ctx; + + if (addr < FAKE_REG_BASE || addr + len > FAKE_REG_BASE + FAKE_REG_SIZE) + return -EIO; + memcpy(f->regs + addr - FAKE_REG_BASE, buf, len); + if (f->nwrites < FAKE_LOG) + f->writes[f->nwrites++] = (struct fake_write){ addr, len, + ((u8 *)buf)[0] }; + return 0; +} + +static int fake_wait(void *ctx, enum dcp_hdcp_auth_op op, unsigned int ms) +{ + struct fake *f = ctx; + + if (f->cancel_op == op) + return -ECANCELED; + return (f->events & BIT(op)) ? 0 : -ETIMEDOUT; +} + +static void fake_report(void *ctx, enum dcp_hdcp_progress_code code, + bool active, bool repeater, bool encrypted) +{ + struct fake *f = ctx; + + if (f->nreports < FAKE_LOG) + f->reports[f->nreports++] = code; +} + +static int fake_revoked(void *ctx, u8 *ids, unsigned int count) +{ + return ((struct fake *)ctx)->revoked_ids; +} + +static void fake_sleep(void *ctx, unsigned int ms) +{ + ((struct fake *)ctx)->slept_ms += ms; +} + +static void sep_log(struct fake *f, char c) +{ + if (f->nsep < FAKE_LOG - 1) + f->sep[f->nsep++] = c; +} + +static int fake_sep_open(void *ctx, u32 ch, u8 *rtx) +{ + if (((struct fake *)ctx)->sep_busy) + return -EAGAIN; + sep_log(ctx, 'o'); + memset(rtx, 0xa1, HDCP_2_2_RTX_LEN); + return 0; +} + +static int fake_sep_ake_cert(void *ctx, u32 ch, const u8 *cert, const u8 *rrx, + bool *stored, u8 *ekh_km_m, u8 *ekpub_km) +{ + struct fake *f = ctx; + + sep_log(f, 'c'); + *stored = f->stored; + memset(ekh_km_m, 0xb2, HDCP_2_2_E_KH_KM_M_LEN); + memset(ekpub_km, 0xc3, HDCP_2_2_E_KPUB_KM_LEN); + return 0; +} + +static int fake_sep_h_prime(void *ctx, u32 ch, const u8 *rxcaps, + const u8 *txcaps, const u8 *h_prime) +{ + struct fake *f = ctx; + + sep_log(f, 'h'); + /* H' covers RxCaps and TxCaps as the receiver sent and got them. */ + KUNIT_EXPECT_EQ(f->test, txcaps[0], HDCP_2_2_RX_CAPS_VERSION_VAL); + KUNIT_EXPECT_EQ(f->test, rxcaps[0], HDCP_2_2_RX_CAPS_VERSION_VAL); + return 0; +} + +static int fake_sep_pairing(void *ctx, u32 ch, const u8 *ekh_km) +{ + sep_log(ctx, 'p'); + return 0; +} + +static int fake_sep_lc_init(void *ctx, u32 ch, u8 *rn) +{ + sep_log(ctx, 'l'); + memset(rn, 0xd4, HDCP_2_2_RN_LEN); + return 0; +} + +static int fake_sep_l_prime(void *ctx, u32 ch, const u8 *l_prime) +{ + struct fake *f = ctx; + + sep_log(f, 'L'); + if (f->l_rejects) { + f->l_rejects--; + return -EACCES; + } + return 0; +} + +static int fake_sep_ske(void *ctx, u32 ch, u8 *edkey_ks, u8 *riv) +{ + sep_log(ctx, 's'); + memset(edkey_ks, 0xe5, HDCP_2_2_E_DKEY_KS_LEN); + memset(riv, 0xf6, HDCP_2_2_RIV_LEN); + return 0; +} + +static int fake_sep_list(void *ctx, u32 ch, const u8 *ids, unsigned int count, + const u8 *rxinfo, const u8 *seq_num_v, + const u8 *v_prime, u8 *v) +{ + sep_log(ctx, 'r'); + memset(v, 0x11, HDCP_2_2_V_PRIME_HALF_LEN); + memset(v + HDCP_2_2_V_PRIME_HALF_LEN, 0x22, HDCP_2_2_V_PRIME_HALF_LEN); + return 0; +} + +static int fake_sep_stream(void *ctx, u32 ch, u8 stream_id, u8 type, + const u8 *seq_num_m, const u8 *m_prime) +{ + struct fake *f = ctx; + + sep_log(f, 'm'); + if (f->m_rejects) { + f->m_rejects--; + return -EACCES; + } + return 0; +} + +static int fake_sep_finish(void *ctx, u32 ch) +{ + sep_log(ctx, 'f'); + return 0; +} + +static int fake_sep_set_encryption(void *ctx, u32 ch, bool on) +{ + struct fake *f = ctx; + + sep_log(f, on ? 'E' : 'e'); + f->encryption = on; + return 0; +} + +static void fake_sep_close(void *ctx, u32 ch) +{ + struct fake *f = ctx; + + sep_log(f, 'x'); + f->closed = true; +} + +static const struct dcp_hdcp_tx_ops fake_ops = { + .read = fake_read, + .write = fake_write, + .wait = fake_wait, + .report = fake_report, + .revoked = fake_revoked, + .sleep = fake_sleep, + .sep_open = fake_sep_open, + .sep_ake_cert = fake_sep_ake_cert, + .sep_h_prime = fake_sep_h_prime, + .sep_pairing = fake_sep_pairing, + .sep_lc_init = fake_sep_lc_init, + .sep_l_prime = fake_sep_l_prime, + .sep_ske = fake_sep_ske, + .sep_repeater_list = fake_sep_list, + .sep_repeater_stream = fake_sep_stream, + .sep_finish = fake_sep_finish, + .sep_set_encryption = fake_sep_set_encryption, + .sep_close = fake_sep_close, +}; + +static u8 *reg(struct fake *f, u32 addr) +{ + return f->regs + addr - FAKE_REG_BASE; +} + +/* A receiver that does HDCP 2.2, a repeater or not. */ +static void fake_receiver(struct fake *f, bool repeater) +{ + u8 *caps = reg(f, DP_HDCP_2_2_REG_RX_CAPS_OFFSET); + + caps[0] = HDCP_2_2_RX_CAPS_VERSION_VAL; + caps[1] = 0; + caps[2] = BIT(1) | (repeater ? BIT(0) : 0); + memset(reg(f, DP_HDCP_2_2_REG_CERT_RX_OFFSET), 0x5a, + sizeof(struct hdcp2_cert_rx)); + f->events = BIT(DCP_HDCP_AUTH_H_PRIME) | BIT(DCP_HDCP_AUTH_PAIRING); + if (repeater) { + u8 *rxinfo = reg(f, DP_HDCP_2_2_REG_RXINFO_OFFSET); + + /* Depth 1, one device. */ + rxinfo[0] = 0x02; + rxinfo[1] = 0x10; + f->events |= BIT(DCP_HDCP_AUTH_LIST); + } +} + +static struct fake *fake_new(struct kunit *test, struct dcp_hdcp_tx *tx, + bool repeater, u8 type) +{ + struct fake *f = kunit_kzalloc(test, sizeof(*f), GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, f); + f->test = test; + f->cancel_op = -1; + fake_receiver(f, repeater); + memset(tx, 0, sizeof(*tx)); + tx->ops = &fake_ops; + tx->ctx = f; + tx->channel = 7; + tx->type = type; + return f; +} + +static bool wrote(struct fake *f, u32 addr, size_t len) +{ + unsigned int i; + + for (i = 0; i < f->nwrites; i++) + if (f->writes[i].addr == addr && f->writes[i].len == len) + return true; + return false; +} + +static bool reported(struct fake *f, enum dcp_hdcp_progress_code code) +{ + unsigned int i; + + for (i = 0; i < f->nreports; i++) + if (f->reports[i] == code) + return true; + return false; +} + +static void tx_stored_km_type0(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f = fake_new(test, &tx, false, 0); + + f->stored = true; + KUNIT_ASSERT_EQ(test, dcp_hdcp_authenticate(&tx), 0); + KUNIT_EXPECT_STREQ(test, f->sep, "ochlLsfE"); + KUNIT_EXPECT_TRUE(test, wrote(f, DP_HDCP_2_2_REG_RTX_OFFSET, 8)); + KUNIT_EXPECT_TRUE(test, wrote(f, DP_HDCP_2_2_REG_TXCAPS_OFFSET, 3)); + KUNIT_EXPECT_TRUE(test, wrote(f, DP_HDCP_2_2_REG_EKH_KM_WR_OFFSET, 32)); + KUNIT_EXPECT_FALSE(test, wrote(f, DP_HDCP_2_2_REG_EKPUB_KM_OFFSET, 128)); + KUNIT_EXPECT_TRUE(test, wrote(f, DP_HDCP_2_2_REG_RN_OFFSET, 8)); + KUNIT_EXPECT_TRUE(test, wrote(f, DP_HDCP_2_2_REG_EDKEY_KS_OFFSET, 16)); + KUNIT_EXPECT_TRUE(test, wrote(f, DP_HDCP_2_2_REG_RIV_OFFSET, 8)); + KUNIT_EXPECT_TRUE(test, wrote(f, DP_HDCP_2_2_REG_STREAM_TYPE_OFFSET, 1)); + KUNIT_EXPECT_EQ(test, *reg(f, DP_HDCP_2_2_REG_STREAM_TYPE_OFFSET), 0); + KUNIT_EXPECT_FALSE(test, reported(f, DCP_HDCP_PROGRESS_H_VERIFIED)); + KUNIT_EXPECT_EQ(test, f->reports[f->nreports - 1], + DCP_HDCP_PROGRESS_ENCRYPTED); + /* The cert after 100 ms, L' after 16 ms, encryption 200 ms after the keys. */ + KUNIT_EXPECT_EQ(test, f->slept_ms, HDCP_2_2_CERT_TIMEOUT_MS + + HDCP_2_2_DP_LPRIME_TIMEOUT_MS + + HDCP_2_2_DELAY_BEFORE_ENCRYPTION_EN); + KUNIT_EXPECT_TRUE(test, f->encryption); + KUNIT_EXPECT_FALSE(test, tx.repeater); + + dcp_hdcp_abort(&tx); + KUNIT_EXPECT_FALSE(test, f->encryption); + KUNIT_EXPECT_TRUE(test, f->closed); +} + +static void tx_first_pairing(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f = fake_new(test, &tx, false, 0); + + KUNIT_ASSERT_EQ(test, dcp_hdcp_authenticate(&tx), 0); + KUNIT_EXPECT_STREQ(test, f->sep, "ochplLsfE"); + KUNIT_EXPECT_TRUE(test, wrote(f, DP_HDCP_2_2_REG_EKPUB_KM_OFFSET, 128)); + KUNIT_EXPECT_TRUE(test, reported(f, DCP_HDCP_PROGRESS_H_VERIFIED)); + + /* No pairing information: the attempt fails, nothing is encrypted. */ + f = fake_new(test, &tx, false, 0); + f->events &= ~BIT(DCP_HDCP_AUTH_PAIRING); + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -ETIMEDOUT); + KUNIT_EXPECT_FALSE(test, f->encryption); +} + +static void tx_repeater_types(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f; + u8 *sm; + u8 type; + + for (type = 0; type <= 1; type++) { + f = fake_new(test, &tx, true, type); + f->stored = true; + KUNIT_ASSERT_EQ(test, dcp_hdcp_authenticate(&tx), 0); + KUNIT_EXPECT_STREQ(test, f->sep, "ochlLsrmfE"); + KUNIT_EXPECT_TRUE(test, tx.repeater); + KUNIT_EXPECT_EQ(test, tx.device_count, 1); + KUNIT_EXPECT_EQ(test, tx.depth, 1); + /* The ack carries V's low half. */ + KUNIT_EXPECT_TRUE(test, wrote(f, DP_HDCP_2_2_REG_V_OFFSET, 16)); + KUNIT_EXPECT_EQ(test, *reg(f, DP_HDCP_2_2_REG_V_OFFSET), 0x22); + /* seq_num_M 0, k 1, stream 0 with the type asked for. */ + KUNIT_EXPECT_TRUE(test, wrote(f, DP_HDCP_2_2_REG_SEQ_NUM_M_OFFSET, 7)); + sm = reg(f, DP_HDCP_2_2_REG_SEQ_NUM_M_OFFSET); + KUNIT_EXPECT_EQ(test, get_unaligned_be24(sm), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_be16(sm + 3), 1); + KUNIT_EXPECT_EQ(test, sm[5], 0); + KUNIT_EXPECT_EQ(test, sm[6], type); + KUNIT_EXPECT_EQ(test, tx.seq_num_m, 1); + KUNIT_EXPECT_FALSE(test, wrote(f, DP_HDCP_2_2_REG_STREAM_TYPE_OFFSET, 1)); + KUNIT_EXPECT_TRUE(test, reported(f, DCP_HDCP_PROGRESS_LIST_VERIFIED)); + KUNIT_EXPECT_TRUE(test, reported(f, DCP_HDCP_PROGRESS_STREAM_READY)); + } +} + +static void tx_type1_refused_downstream_legacy(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f; + u8 legacy[] = { BIT(0), BIT(1) }; /* HDCP 1.x device, 2.0 repeater */ + int i; + + for (i = 0; i < ARRAY_SIZE(legacy); i++) { + f = fake_new(test, &tx, true, 1); + reg(f, DP_HDCP_2_2_REG_RXINFO_OFFSET)[1] |= legacy[i]; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -EPERM); + KUNIT_EXPECT_FALSE(test, f->encryption); + KUNIT_EXPECT_TRUE(test, tx.legacy_downstream); + dcp_hdcp_abort(&tx); + KUNIT_EXPECT_TRUE(test, f->closed); + /* Never asked to turn encryption off: it never went on. */ + KUNIT_EXPECT_EQ(test, f->sep[f->nsep - 1], 'x'); + + /* Type 0 goes through the same topology. */ + f = fake_new(test, &tx, true, 0); + reg(f, DP_HDCP_2_2_REG_RXINFO_OFFSET)[1] |= legacy[i]; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), 0); + } +} + +static void tx_topology_exceeded(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f = fake_new(test, &tx, true, 0); + + reg(f, DP_HDCP_2_2_REG_RXINFO_OFFSET)[1] |= BIT(3); + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -E2BIG); + f = fake_new(test, &tx, true, 0); + reg(f, DP_HDCP_2_2_REG_RXINFO_OFFSET)[1] |= BIT(2); + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -E2BIG); + /* A list that names no device. */ + f = fake_new(test, &tx, true, 0); + reg(f, DP_HDCP_2_2_REG_RXINFO_OFFSET)[1] = 0; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -E2BIG); + /* The first list of a session must carry seq_num_V 0. */ + f = fake_new(test, &tx, true, 0); + reg(f, DP_HDCP_2_2_REG_SEQ_NUM_V_OFFSET)[2] = 1; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -EPROTO); + KUNIT_EXPECT_FALSE(test, f->encryption); +} + +/* A certificate read too early or garbled never reaches the SEP. */ +static void tx_bad_receiver_id(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f = fake_new(test, &tx, false, 0); + + memset(reg(f, DP_HDCP_2_2_REG_CERT_RX_OFFSET), 0xff, HDCP_2_2_RECEIVER_ID_LEN); + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -EPROTO); + KUNIT_EXPECT_STREQ(test, tx.step, "check Receiver ID"); + KUNIT_EXPECT_FALSE(test, f->encryption); +} + +static void tx_revoked(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f = fake_new(test, &tx, false, 0); + + /* The receiver's own ID: refused before the SEP sees the cert. */ + f->revoked_ids = 1; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -EPERM); + KUNIT_EXPECT_STREQ(test, f->sep, "o"); + + /* An SRM that cannot be read fails closed. */ + f = fake_new(test, &tx, false, 0); + f->revoked_ids = -EINVAL; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -EINVAL); + KUNIT_EXPECT_FALSE(test, f->encryption); +} + +static void tx_timeout_and_rxstatus(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f = fake_new(test, &tx, false, 0); + + f->stored = true; + f->events = 0; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -ETIMEDOUT); + KUNIT_EXPECT_FALSE(test, f->encryption); + + /* An overdue event, but RxStatus says H' is there. */ + f = fake_new(test, &tx, false, 0); + f->stored = true; + f->events = 0; + *reg(f, DP_HDCP_2_2_REG_RXSTATUS_OFFSET) = BIT(1); + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), 0); + + /* An event on time: RxStatus is left to DCP. */ + f = fake_new(test, &tx, false, 0); + f->stored = true; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), 0); + KUNIT_EXPECT_FALSE(test, f->read_rxstatus); +} + +static void tx_close_mid_auth(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f = fake_new(test, &tx, true, 0); + + /* DCP closes while the ReceiverID list is awaited. */ + f->cancel_op = DCP_HDCP_AUTH_LIST; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -ECANCELED); + KUNIT_EXPECT_FALSE(test, reported(f, DCP_HDCP_PROGRESS_LIST_VERIFIED)); + dcp_hdcp_abort(&tx); + KUNIT_EXPECT_TRUE(test, f->closed); + KUNIT_EXPECT_FALSE(test, f->encryption); + /* A second abort is harmless. */ + f->nsep = 0; + memset(f->sep, 0, sizeof(f->sep)); + dcp_hdcp_abort(&tx); + KUNIT_EXPECT_EQ(test, f->nsep, 0); +} + +static void tx_m_prime_retry(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f = fake_new(test, &tx, true, 1); + + f->m_rejects = 1; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), 0); + KUNIT_EXPECT_EQ(test, tx.seq_num_m, 2); + KUNIT_EXPECT_EQ(test, get_unaligned_be24(reg(f, DP_HDCP_2_2_REG_SEQ_NUM_M_OFFSET)), 1); + + f = fake_new(test, &tx, true, 1); + f->m_rejects = 2; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -EACCES); + KUNIT_EXPECT_FALSE(test, f->encryption); +} + +static void tx_not_capable(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f = fake_new(test, &tx, false, 0); + + KUNIT_EXPECT_EQ(test, dcp_hdcp_capable(&tx), 1); + reg(f, DP_HDCP_2_2_REG_RX_CAPS_OFFSET)[2] = 0; + KUNIT_EXPECT_EQ(test, dcp_hdcp_capable(&tx), 0); + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -ENODEV); + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&(struct dcp_hdcp_tx){ + .ops = &fake_ops, .ctx = f, .type = 2 }), + -EINVAL); + +} + +static void tx_l_prime_retry(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f = fake_new(test, &tx, false, 0); + + /* Each retry starts with a fresh r_n. */ + f->stored = true; + f->l_rejects = DCP_HDCP_LC_TRIES - 1; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), 0); + KUNIT_EXPECT_STREQ(test, f->sep, "ochlLlLlLsfE"); + + f = fake_new(test, &tx, false, 0); + f->stored = true; + f->l_rejects = DCP_HDCP_LC_TRIES; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -EACCES); + KUNIT_EXPECT_STREQ(test, f->sep, "ochlLlLlL"); +} + +static void tx_sep_busy(struct kunit *test) +{ + struct dcp_hdcp_tx tx; + struct fake *f = fake_new(test, &tx, false, 0); + + f->sep_busy = true; + KUNIT_EXPECT_EQ(test, dcp_hdcp_authenticate(&tx), -EAGAIN); + KUNIT_EXPECT_EQ(test, f->nwrites, 0); + dcp_hdcp_abort(&tx); + KUNIT_EXPECT_EQ(test, f->nsep, 0); +} + +static struct kunit_case dcp_hdcp_tx_cases[] = { + KUNIT_CASE(tx_stored_km_type0), + KUNIT_CASE(tx_first_pairing), + KUNIT_CASE(tx_repeater_types), + KUNIT_CASE(tx_type1_refused_downstream_legacy), + KUNIT_CASE(tx_topology_exceeded), + KUNIT_CASE(tx_revoked), + KUNIT_CASE(tx_bad_receiver_id), + KUNIT_CASE(tx_timeout_and_rxstatus), + KUNIT_CASE(tx_close_mid_auth), + KUNIT_CASE(tx_m_prime_retry), + KUNIT_CASE(tx_l_prime_retry), + KUNIT_CASE(tx_sep_busy), + KUNIT_CASE(tx_not_capable), + { } +}; + +static struct kunit_suite dcp_hdcp_tx_suite = { + .name = "apple-dcp-hdcp-tx", + .test_cases = dcp_hdcp_tx_cases, +}; +kunit_test_suite(dcp_hdcp_tx_suite); diff --git a/drivers/gpu/drm/apple/dcp-hdcp-tx.c b/drivers/gpu/drm/apple/dcp-hdcp-tx.c new file mode 100644 index 0000000000000..222c0a74ab6d9 --- /dev/null +++ b/drivers/gpu/drm/apple/dcp-hdcp-tx.c @@ -0,0 +1,374 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* Copyright 2026 Ryan Murray */ + +#include +#include +#include +#include +#include + +#include "dcp-hdcp-tx.h" + +/* Each step names itself, so a failed attempt says where it stopped. */ +#define TX_READ(tx, reg, buf, len) \ + ((tx)->step = "read " #reg, (tx)->ops->read((tx)->ctx, reg, buf, len)) +#define TX_WRITE(tx, reg, buf, len) \ + ((tx)->step = "write " #reg, (tx)->ops->write((tx)->ctx, reg, buf, len)) +#define TX_SEP(tx, fn) ((tx)->step = #fn, (tx)->ops->fn) + +static void tx_report(struct dcp_hdcp_tx *tx, enum dcp_hdcp_progress_code code, + bool encrypted) +{ + tx->ops->report(tx->ctx, code, true, tx->repeater, encrypted); +} + +/* + * Waits for a session event. DCP turns the receiver's RxStatus into these + * events, so RxStatus is read only once the event is overdue (spec time plus + * slack): a DisplayPort read clears the bits DCP waits for. Some paths (an + * on-board DP to HDMI converter) never raise the event although the receiver + * has the message ready. + */ +static int tx_wait(struct dcp_hdcp_tx *tx, enum dcp_hdcp_auth_op op, + unsigned int spec_ms) +{ + u8 status; + int ret; + + tx->step = "wait for DCP event"; + ret = tx->ops->wait(tx->ctx, op, spec_ms + DCP_HDCP_EVENT_SLACK_MS); + if (ret != -ETIMEDOUT) + return ret; + if (TX_READ(tx, DP_HDCP_2_2_REG_RXSTATUS_OFFSET, &status, 1)) + return -ETIMEDOUT; + switch (op) { + case DCP_HDCP_AUTH_H_PRIME: + return HDCP_2_2_DP_RXSTATUS_H_PRIME(status) ? 0 : -ETIMEDOUT; + case DCP_HDCP_AUTH_PAIRING: + return HDCP_2_2_DP_RXSTATUS_PAIRING(status) ? 0 : -ETIMEDOUT; + case DCP_HDCP_AUTH_LIST: + return HDCP_2_2_DP_RXSTATUS_READY(status) ? 0 : -ETIMEDOUT; + default: + return -ETIMEDOUT; + } +} + +/* Any revoked ID, or an SRM that cannot be read, fails the attempt. */ +static int tx_revocation(struct dcp_hdcp_tx *tx, u8 *ids, unsigned int count) +{ + int ret = tx->ops->revoked(tx->ctx, ids, count); + + if (ret > 0) + return -EPERM; + return ret; +} + +/* Twenty ones and twenty zeros, as every HDCP Receiver ID. */ +static bool dcp_hdcp_receiver_id_valid(const u8 *id) +{ + unsigned int i, ones = 0; + + for (i = 0; i < HDCP_2_2_RECEIVER_ID_LEN; i++) + ones += hweight8(id[i]); + return ones == 20; +} + +int dcp_hdcp_capable(struct dcp_hdcp_tx *tx) +{ + int ret = TX_READ(tx, DP_HDCP_2_2_REG_RX_CAPS_OFFSET, tx->rxcaps, + sizeof(tx->rxcaps)); + + if (ret) + return ret; + return dcp_hdcp_rxcaps_capable(tx->rxcaps); +} + +static int tx_ake(struct dcp_hdcp_tx *tx, bool *stored) +{ + u8 rtx[HDCP_2_2_RTX_LEN]; + u8 ekh_km_m[HDCP_2_2_E_KH_KM_M_LEN]; + u8 ekpub_km[HDCP_2_2_E_KPUB_KM_LEN]; + u8 h_prime[HDCP_2_2_H_PRIME_LEN]; + u8 ekh_km[HDCP_2_2_E_KH_KM_LEN]; + int ret; + + ret = TX_SEP(tx, sep_open)(tx->ctx, tx->channel, rtx); + if (ret) + return ret; + tx->sep_open = true; + + /* AKE_Init */ + tx->txcaps.version = HDCP_2_2_RX_CAPS_VERSION_VAL; + tx->txcaps.tx_cap_mask[0] = 0; + tx->txcaps.tx_cap_mask[1] = 0; + ret = TX_WRITE(tx, DP_HDCP_2_2_REG_RTX_OFFSET, rtx, sizeof(rtx)); + if (!ret) + ret = TX_WRITE(tx, DP_HDCP_2_2_REG_TXCAPS_OFFSET, &tx->txcaps, + sizeof(tx->txcaps)); + if (ret) + return ret; + + /* AKE_Send_Cert: the receiver has 100 ms to make it readable. */ + tx->step = "wait for AKE_Send_Cert"; + tx->ops->sleep(tx->ctx, HDCP_2_2_CERT_TIMEOUT_MS); + ret = TX_READ(tx, DP_HDCP_2_2_REG_CERT_RX_OFFSET, &tx->cert, + sizeof(tx->cert)); + /* A Receiver ID has twenty ones and twenty zeros; anything else is a bad read. */ + if (!ret && !dcp_hdcp_receiver_id_valid(tx->cert.receiver_id)) { + tx->step = "check Receiver ID"; + ret = -EPROTO; + } + if (!ret) + ret = TX_READ(tx, DP_HDCP_2_2_REG_RRX_OFFSET, tx->rrx, + sizeof(tx->rrx)); + if (!ret) + ret = TX_READ(tx, DP_HDCP_2_2_REG_RX_CAPS_OFFSET, tx->rxcaps, + sizeof(tx->rxcaps)); + if (ret) + return ret; + if (!dcp_hdcp_rxcaps_capable(tx->rxcaps)) + return -ENODEV; + tx->repeater = HDCP_2_2_RX_REPEATER(tx->rxcaps[2]); + ret = tx_revocation(tx, tx->cert.receiver_id, 1); + if (ret) + return ret; + ret = TX_SEP(tx, sep_ake_cert)(tx->ctx, tx->channel, (const u8 *)&tx->cert, + tx->rrx, stored, ekh_km_m, ekpub_km); + if (ret) + return ret; + + /* AKE_Stored_km (Ekh(km) then m, contiguous) or AKE_No_Stored_km */ + if (*stored) + ret = TX_WRITE(tx, DP_HDCP_2_2_REG_EKH_KM_WR_OFFSET, ekh_km_m, + sizeof(ekh_km_m)); + else + ret = TX_WRITE(tx, DP_HDCP_2_2_REG_EKPUB_KM_OFFSET, ekpub_km, + sizeof(ekpub_km)); + if (ret) + return ret; + tx_report(tx, DCP_HDCP_PROGRESS_KM_SENT, false); + + /* AKE_Send_H_prime */ + ret = tx_wait(tx, DCP_HDCP_AUTH_H_PRIME, + *stored ? HDCP_2_2_HPRIME_PAIRED_TIMEOUT_MS : + HDCP_2_2_HPRIME_NO_PAIRED_TIMEOUT_MS); + if (!ret) + ret = TX_READ(tx, DP_HDCP_2_2_REG_HPRIME_OFFSET, h_prime, + sizeof(h_prime)); + if (!ret) + ret = TX_SEP(tx, sep_h_prime)(tx->ctx, tx->channel, tx->rxcaps, + (const u8 *)&tx->txcaps, h_prime); + if (ret) + return ret; + if (*stored) + return 0; + + /* AKE_Send_Pairing_Info */ + tx_report(tx, DCP_HDCP_PROGRESS_H_VERIFIED, false); + ret = tx_wait(tx, DCP_HDCP_AUTH_PAIRING, HDCP_2_2_PAIRING_TIMEOUT_MS); + if (!ret) + ret = TX_READ(tx, DP_HDCP_2_2_REG_EKH_KM_RD_OFFSET, ekh_km, + sizeof(ekh_km)); + if (!ret) + ret = TX_SEP(tx, sep_pairing)(tx->ctx, tx->channel, ekh_km); + return ret; +} + +static int tx_lc_ske(struct dcp_hdcp_tx *tx) +{ + u8 rn[HDCP_2_2_RN_LEN]; + u8 l_prime[HDCP_2_2_L_PRIME_LEN]; + u8 edkey_ks[HDCP_2_2_E_DKEY_KS_LEN]; + u8 riv[HDCP_2_2_RIV_LEN]; + int attempt, ret; + + /* + * LC_Init, then LC_Send_L_prime once the receiver had its time. A + * wrong L' is retried with a fresh r_n, a bounded number of times. + */ + for (attempt = 0; attempt < DCP_HDCP_LC_TRIES; attempt++) { + ret = TX_SEP(tx, sep_lc_init)(tx->ctx, tx->channel, rn); + if (!ret) + ret = TX_WRITE(tx, DP_HDCP_2_2_REG_RN_OFFSET, rn, sizeof(rn)); + if (ret) + return ret; + tx->ops->sleep(tx->ctx, HDCP_2_2_DP_LPRIME_TIMEOUT_MS); + ret = TX_READ(tx, DP_HDCP_2_2_REG_LPRIME_OFFSET, l_prime, + sizeof(l_prime)); + if (!ret) + ret = TX_SEP(tx, sep_l_prime)(tx->ctx, tx->channel, l_prime); + if (ret != -EACCES) + break; + } + if (ret) + return ret; + + /* SKE_Send_Eks */ + ret = TX_SEP(tx, sep_ske)(tx->ctx, tx->channel, edkey_ks, riv); + if (!ret) + ret = TX_WRITE(tx, DP_HDCP_2_2_REG_EDKEY_KS_OFFSET, edkey_ks, + sizeof(edkey_ks)); + if (!ret) + ret = TX_WRITE(tx, DP_HDCP_2_2_REG_RIV_OFFSET, riv, sizeof(riv)); + if (!ret) + tx_report(tx, DCP_HDCP_PROGRESS_SKE_SENT, false); + return ret; +} + +static int tx_receiver_list(struct dcp_hdcp_tx *tx) +{ + u8 rxinfo[HDCP_2_2_RXINFO_LEN]; + u8 seq_num_v[HDCP_2_2_SEQ_NUM_LEN]; + u8 v_prime[HDCP_2_2_V_PRIME_HALF_LEN]; + u8 v[2 * HDCP_2_2_V_PRIME_HALF_LEN]; + unsigned int count; + int ret; + + ret = tx_wait(tx, DCP_HDCP_AUTH_LIST, HDCP_2_2_RECVID_LIST_TIMEOUT_MS); + if (!ret) + ret = TX_READ(tx, DP_HDCP_2_2_REG_RXINFO_OFFSET, rxinfo, + sizeof(rxinfo)); + if (!ret) + ret = TX_READ(tx, DP_HDCP_2_2_REG_SEQ_NUM_V_OFFSET, seq_num_v, + sizeof(seq_num_v)); + if (!ret) + ret = TX_READ(tx, DP_HDCP_2_2_REG_VPRIME_OFFSET, v_prime, + sizeof(v_prime)); + if (ret) + return ret; + + /* Topology: too many devices or levels downstream fails the attempt. */ + if (HDCP_2_2_MAX_CASCADE_EXCEEDED(rxinfo[1]) || + HDCP_2_2_MAX_DEVS_EXCEEDED(rxinfo[1])) + return -E2BIG; + count = (HDCP_2_2_DEV_COUNT_HI(rxinfo[0]) << 4) | + HDCP_2_2_DEV_COUNT_LO(rxinfo[1]); + if (!count || count > HDCP_2_2_MAX_DEVICE_COUNT) + return -E2BIG; + tx->device_count = count; + tx->depth = HDCP_2_2_DEPTH(rxinfo[0]); + tx->legacy_downstream = HDCP_2_2_HDCP1_DEVICE_CONNECTED(rxinfo[1]) || + HDCP_2_2_HDCP_2_0_REP_CONNECTED(rxinfo[1]); + /* The first list of a session carries seq_num_V 0. */ + if (get_unaligned_be24(seq_num_v)) + return -EPROTO; + + ret = TX_READ(tx, DP_HDCP_2_2_REG_RECV_ID_LIST_OFFSET, tx->ids, + count * HDCP_2_2_RECEIVER_ID_LEN); + if (ret) + return ret; + ret = tx_revocation(tx, tx->ids, count); + if (ret) + return ret; + ret = TX_SEP(tx, sep_repeater_list)(tx->ctx, tx->channel, tx->ids, count, + rxinfo, seq_num_v, v_prime, v); + if (ret) + return ret; + + /* RepeaterAuth_Send_Ack: the least significant 128 bits of V. */ + ret = TX_WRITE(tx, DP_HDCP_2_2_REG_V_OFFSET, v + HDCP_2_2_V_PRIME_HALF_LEN, + HDCP_2_2_V_PRIME_HALF_LEN); + if (!ret) + tx_report(tx, DCP_HDCP_PROGRESS_LIST_VERIFIED, false); + return ret; +} + +/* + * RepeaterAuth_Stream_Manage for the one stream, then Stream_Ready. A wrong + * M' is retried once with the next seq_num_M, as the specification allows. + */ +static int tx_stream_manage(struct dcp_hdcp_tx *tx) +{ + u8 msg[HDCP_2_2_SEQ_NUM_LEN + 2 + sizeof(struct hdcp2_streamid_type)]; + u8 m_prime[HDCP_2_2_MPRIME_LEN]; + int attempt, ret = -EACCES; + + /* Type 1 must not reach an HDCP 1.x device or an HDCP 2.0 repeater. */ + if (tx->type && tx->legacy_downstream) + return -EPERM; + + for (attempt = 0; attempt < 2 && ret == -EACCES; attempt++) { + if (tx->seq_num_m > HDCP_2_2_SEQ_NUM_MAX) + return -ERANGE; + put_unaligned_be24(tx->seq_num_m, msg); + put_unaligned_be16(1, msg + HDCP_2_2_SEQ_NUM_LEN); + msg[HDCP_2_2_SEQ_NUM_LEN + 2] = 0; /* stream ID */ + msg[HDCP_2_2_SEQ_NUM_LEN + 3] = tx->type; + ret = TX_WRITE(tx, DP_HDCP_2_2_REG_SEQ_NUM_M_OFFSET, msg, + sizeof(msg)); + if (ret) + return ret; + tx->ops->sleep(tx->ctx, HDCP_2_2_STREAM_READY_TIMEOUT_MS); + ret = TX_READ(tx, DP_HDCP_2_2_REG_MPRIME_OFFSET, m_prime, + sizeof(m_prime)); + if (ret) + return ret; + ret = TX_SEP(tx, sep_repeater_stream)(tx->ctx, tx->channel, 0, + tx->type, msg, m_prime); + tx->seq_num_m++; + } + if (!ret) + tx_report(tx, DCP_HDCP_PROGRESS_STREAM_READY, false); + return ret; +} + +int dcp_hdcp_authenticate(struct dcp_hdcp_tx *tx) +{ + bool stored = false; + int ret; + + tx->step = NULL; + if (tx->type > 1) + return -EINVAL; + tx->repeater = false; + tx->legacy_downstream = false; + tx->device_count = 0; + tx->depth = 0; + tx->seq_num_m = 0; + tx_report(tx, DCP_HDCP_PROGRESS_AKE, false); + + ret = tx_ake(tx, &stored); + if (ret) + return ret; + tx_report(tx, DCP_HDCP_PROGRESS_AKE_DONE, false); + ret = tx_lc_ske(tx); + if (ret) + return ret; + + if (tx->repeater) { + ret = tx_receiver_list(tx); + if (!ret) + ret = tx_stream_manage(tx); + } else { + ret = TX_WRITE(tx, DP_HDCP_2_2_REG_STREAM_TYPE_OFFSET, &tx->type, + sizeof(tx->type)); + } + if (ret) + return ret; + + /* Encryption no sooner than 200 ms after SKE_Send_Eks. */ + ret = TX_SEP(tx, sep_finish)(tx->ctx, tx->channel); + if (ret) + return ret; + tx->ops->sleep(tx->ctx, HDCP_2_2_DELAY_BEFORE_ENCRYPTION_EN); + tx->encrypting = true; + ret = TX_SEP(tx, sep_set_encryption)(tx->ctx, tx->channel, true); + if (ret) + return ret; + tx_report(tx, DCP_HDCP_PROGRESS_ENCRYPTED, true); + return 0; +} + +void dcp_hdcp_abort(struct dcp_hdcp_tx *tx) +{ + if (tx->encrypting) + TX_SEP(tx, sep_set_encryption)(tx->ctx, tx->channel, false); + tx->encrypting = false; + if (tx->sep_open) + TX_SEP(tx, sep_close)(tx->ctx, tx->channel); + tx->sep_open = false; +} + +#if IS_ENABLED(CONFIG_DRM_APPLE_KUNIT_TEST) +#include "dcp-hdcp-tx-test.c" +#endif diff --git a/drivers/gpu/drm/apple/dcp-hdcp-tx.h b/drivers/gpu/drm/apple/dcp-hdcp-tx.h new file mode 100644 index 0000000000000..7f1446e86f239 --- /dev/null +++ b/drivers/gpu/drm/apple/dcp-hdcp-tx.h @@ -0,0 +1,118 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* Copyright 2026 Ryan Murray */ +#ifndef __APPLE_DCP_HDCP_TX_H__ +#define __APPLE_DCP_HDCP_TX_H__ + +#include +#include +#include + +#include "dcp-hdcp-wire.h" + +/* + * One HDCP 2.2 transmitter authentication, as the AP runs it with the SEP + * doing the cryptography: every message is built from the fields the SEP + * returns and written to, or read from, the receiver's HDCP 2.2 registers + * through DCP. The procedure runs once DCP has opened a session and started + * it, and blocks: it waits for DCP's session events and sleeps where the + * specification asks for it. It never touches the display. + * + * DCP owns the receiver timeouts and closes an attempt that runs out of + * time; the waits below only guard against a session DCP forgets. An event + * travels receiver CP_IRQ -> DCP -> mailbox -> this driver's work queue on + * top of the spec time, so the guard is the spec time plus this slack; it is + * not a spec value and only decides when RxStatus is read as a fallback. + */ +#define DCP_HDCP_EVENT_SLACK_MS 500 +/* Locality check attempts, each with a fresh r_n. */ +#define DCP_HDCP_LC_TRIES 3 + +struct dcp_hdcp_tx_ops { + /* Receiver registers (DP_HDCP_2_2_REG_*). */ + int (*read)(void *ctx, u32 addr, void *buf, size_t len); + int (*write)(void *ctx, u32 addr, const void *buf, size_t len); + /* + * Waits up to @ms for session event @op. Returns 0 when it came, + * -ETIMEDOUT, or -ECANCELED when the session was closed or the + * protection is no longer wanted. + */ + int (*wait)(void *ctx, enum dcp_hdcp_auth_op op, unsigned int ms); + void (*report)(void *ctx, enum dcp_hdcp_progress_code code, + bool active, bool repeater, bool encrypted); + /* drm_hdcp_check_ksvs_revoked(): the count of revoked IDs, or <0. */ + int (*revoked)(void *ctx, u8 *ids, unsigned int count); + void (*sleep)(void *ctx, unsigned int ms); + + /* The SEP service, include/linux/apple-sep-hdcp.h. */ + int (*sep_open)(void *ctx, u32 ch, u8 *rtx); + int (*sep_ake_cert)(void *ctx, u32 ch, const u8 *cert, const u8 *rrx, + bool *stored, u8 *ekh_km_m, u8 *ekpub_km); + int (*sep_h_prime)(void *ctx, u32 ch, const u8 *rxcaps, + const u8 *txcaps, const u8 *h_prime); + int (*sep_pairing)(void *ctx, u32 ch, const u8 *ekh_km); + int (*sep_lc_init)(void *ctx, u32 ch, u8 *rn); + int (*sep_l_prime)(void *ctx, u32 ch, const u8 *l_prime); + int (*sep_ske)(void *ctx, u32 ch, u8 *edkey_ks, u8 *riv); + int (*sep_repeater_list)(void *ctx, u32 ch, const u8 *ids, + unsigned int count, const u8 *rxinfo, + const u8 *seq_num_v, const u8 *v_prime, u8 *v); + int (*sep_repeater_stream)(void *ctx, u32 ch, u8 stream_id, u8 type, + const u8 *seq_num_m, const u8 *m_prime); + int (*sep_finish)(void *ctx, u32 ch); + int (*sep_set_encryption)(void *ctx, u32 ch, bool on); + void (*sep_close)(void *ctx, u32 ch); +}; + +struct dcp_hdcp_tx { + const struct dcp_hdcp_tx_ops *ops; + void *ctx; + u32 channel; + /* The content type asked for: 0 or 1. */ + u8 type; + + /* Results, valid after dcp_hdcp_authenticate() returned 0. */ + bool repeater; + bool legacy_downstream; + unsigned int device_count; + unsigned int depth; + + /* Session state; @step names the last step tried. */ + const char *step; + bool sep_open; + bool encrypting; + u32 seq_num_m; + + /* Scratch: the messages of one attempt. */ + u8 rxcaps[HDCP_2_2_RXCAPS_LEN]; + struct hdcp2_tx_caps txcaps; + struct hdcp2_cert_rx cert; + u8 rrx[HDCP_2_2_RRX_LEN]; + u8 ids[HDCP_2_2_RECEIVER_IDS_MAX_LEN]; +}; + +/* The receiver says it does HDCP 2.2 over DP. */ +static inline bool dcp_hdcp_rxcaps_capable(const u8 *rxcaps) +{ + return rxcaps[0] == HDCP_2_2_RX_CAPS_VERSION_VAL && + HDCP_2_2_DP_HDCP_CAPABLE(rxcaps[2]); +} + +/* Reads RxCaps: 1 when the receiver can do HDCP 2.2, 0 when not, or <0. */ +int dcp_hdcp_capable(struct dcp_hdcp_tx *tx); + +/* + * Runs one authentication to encryption. On 0 the link is encrypted with + * tx->type; on failure the caller ends it with dcp_hdcp_abort(). -EAGAIN + * means the SEP was not available for now: no failure of the receiver. + */ +int dcp_hdcp_authenticate(struct dcp_hdcp_tx *tx); + +/* Encryption off and the SEP session closed; safe at any point. */ +void dcp_hdcp_abort(struct dcp_hdcp_tx *tx); + +/* + * The receiver's status on an encrypted link: 0 while it holds, -EAGAIN when + * it asks for re-authentication or reports a link integrity failure. + */ + +#endif /* __APPLE_DCP_HDCP_TX_H__ */ diff --git a/drivers/gpu/drm/apple/dcp-hdcp-wire-test.c b/drivers/gpu/drm/apple/dcp-hdcp-wire-test.c new file mode 100644 index 0000000000000..2ca3e8c463dc1 --- /dev/null +++ b/drivers/gpu/drm/apple/dcp-hdcp-wire-test.c @@ -0,0 +1,156 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* Copyright 2026 Ryan Murray */ + +#include +#include +#include + +#include "dcp-hdcp-wire.h" + +static void wire_read_encode(struct kunit *test) +{ + u8 body[DCP_HDCP_REG_ARGS_LEN + 522]; + + memset(body, 0xff, sizeof(body)); + KUNIT_ASSERT_EQ(test, dcp_hdcp_reg_read_encode(body, sizeof(body), + DP_HDCP_2_2_REG_CERT_RX_OFFSET, + 522, DCP_HDCP_REG_TIMEOUT_MS), + (int)sizeof(body)); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(body), 0x6900b); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(body + 16), 522); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(body + 32), 500); + KUNIT_EXPECT_EQ(test, body[4], 0); + KUNIT_EXPECT_EQ(test, body[sizeof(body) - 1], 0); + + /* No room, nothing, too much. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_read_encode(body, sizeof(body) - 1, + 0x6900b, 522, 500), -EINVAL); + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_read_encode(body, sizeof(body), + 0x69000, 0, 500), -EINVAL); + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_read_encode(body, sizeof(body), + 0x69000, DCP_HDCP_REG_MAX + 1, + 500), -EINVAL); +} + +static void wire_read_check(struct kunit *test) +{ + u8 body[DCP_HDCP_REG_ARGS_LEN + 32]; + + dcp_hdcp_reg_read_encode(body, sizeof(body), DP_HDCP_2_2_REG_HPRIME_OFFSET, + 32, 500); + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_read_check(body, sizeof(body), + DP_HDCP_2_2_REG_HPRIME_OFFSET, + 32), 0); + /* Truncated: the data is not all there. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_read_check(body, sizeof(body) - 1, + DP_HDCP_2_2_REG_HPRIME_OFFSET, + 32), -EPROTO); + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_read_check(body, 8, + DP_HDCP_2_2_REG_HPRIME_OFFSET, + 32), -EPROTO); + /* An answer for another register or another length. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_read_check(body, sizeof(body), + DP_HDCP_2_2_REG_LPRIME_OFFSET, + 32), -EPROTO); + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_read_check(body, sizeof(body), + DP_HDCP_2_2_REG_HPRIME_OFFSET, + 16), -EPROTO); + /* An oversized ask is refused before any compare. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_read_check(body, SIZE_MAX, + DP_HDCP_2_2_REG_HPRIME_OFFSET, + DCP_HDCP_REG_MAX + 1), -EPROTO); +} + +static void wire_write(struct kunit *test) +{ + u8 body[DCP_HDCP_REG_ARGS_LEN + 8]; + u8 rtx[8] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + + KUNIT_ASSERT_EQ(test, dcp_hdcp_reg_write_encode(body, sizeof(body), + DP_HDCP_2_2_REG_RTX_OFFSET, + rtx, sizeof(rtx)), + (int)sizeof(body)); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(body), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(body + 16), 0x69000); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(body + 32), 8); + /* A write carries the AUX timeout too, at +48. */ + KUNIT_EXPECT_EQ(test, get_unaligned_le32(body + 48), DCP_HDCP_REG_TIMEOUT_MS); + KUNIT_EXPECT_MEMEQ(test, body + DCP_HDCP_REG_ARGS_LEN, rtx, sizeof(rtx)); + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_write_check(body, sizeof(body), + 0x69000, 8), 0); + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_write_check(body, 63, 0x69000, 8), + -EPROTO); + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_write_check(body, sizeof(body), + 0x69008, 8), -EPROTO); + KUNIT_EXPECT_EQ(test, dcp_hdcp_reg_write_encode(body, sizeof(body) - 1, + 0x69000, rtx, 8), -EINVAL); +} + +static void wire_auth_reply(struct kunit *test) +{ + struct dcp_hdcp_word in = { .value = cpu_to_le32(0xffffffff) }; + struct dcp_hdcp_word out; + u8 small[8]; + + /* The open: the AP's answer says whether it takes the session. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_auth_reply(0, DCP_HDCP_AUTH_OPEN, &in, + sizeof(in), &out, sizeof(out), + true), 0); + KUNIT_EXPECT_EQ(test, le32_to_cpu(out.value), 1); + KUNIT_EXPECT_EQ(test, dcp_hdcp_auth_reply(0, DCP_HDCP_AUTH_OPEN, &in, + sizeof(in), &out, sizeof(out), + false), 0); + KUNIT_EXPECT_EQ(test, le32_to_cpu(out.value), 0); + /* Others come back as they were. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_auth_reply(0, DCP_HDCP_AUTH_CONFIG, &in, + sizeof(in), &out, sizeof(out), + false), 0); + KUNIT_EXPECT_EQ(test, le32_to_cpu(out.value), 0xffffffff); + /* A short open answer, another group, an unknown call. */ + KUNIT_EXPECT_EQ(test, dcp_hdcp_auth_reply(0, DCP_HDCP_AUTH_OPEN, &in, + sizeof(in), small, sizeof(small), + true), -EPROTO); + KUNIT_EXPECT_EQ(test, dcp_hdcp_auth_reply(1, DCP_HDCP_AUTH_OPEN, &in, + sizeof(in), &out, sizeof(out), + true), -EOPNOTSUPP); + KUNIT_EXPECT_EQ(test, dcp_hdcp_auth_reply(0, 0x55, &in, sizeof(in), + &out, sizeof(out), true), + -EOPNOTSUPP); +} + +static void wire_report(struct kunit *test) +{ + struct dcp_hdcp_report r; + u8 *b = (u8 *)&r; + + dcp_hdcp_report_encode(&r, DCP_HDCP_PROGRESS_LIST_VERIFIED, true, true, + false); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(b + 64), 0x78); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(b + 16), 1); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(b + 48), 1); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(b + 32), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(b + 0), 0); + dcp_hdcp_report_encode(&r, DCP_HDCP_PROGRESS_ENCRYPTED, true, false, true); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(b + 64), 0x74); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(b + 32), 1); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(b + 0), 1); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(b + 48), 0); +} + +static struct kunit_case dcp_hdcp_wire_cases[] = { + KUNIT_CASE(wire_read_encode), + KUNIT_CASE(wire_read_check), + KUNIT_CASE(wire_write), + KUNIT_CASE(wire_auth_reply), + KUNIT_CASE(wire_report), + { } +}; + +static struct kunit_suite dcp_hdcp_wire_suite = { + .name = "apple-dcp-hdcp-wire", + .test_cases = dcp_hdcp_wire_cases, +}; +kunit_test_suite(dcp_hdcp_wire_suite); + +MODULE_DESCRIPTION("KUnit tests for the Apple DCP HDCP service layouts"); +MODULE_LICENSE("Dual MIT/GPL"); diff --git a/drivers/gpu/drm/apple/dcp-hdcp-wire.h b/drivers/gpu/drm/apple/dcp-hdcp-wire.h new file mode 100644 index 0000000000000..e364264710924 --- /dev/null +++ b/drivers/gpu/drm/apple/dcp-hdcp-wire.h @@ -0,0 +1,247 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* Copyright 2026 Ryan Murray */ +#ifndef __APPLE_DCP_HDCP_WIRE_H__ +#define __APPLE_DCP_HDCP_WIRE_H__ + +#include +#include +#include +#include +#include +#include + +/* + * The DCP services that carry HDCP 2.2, as standard EPIC service calls + * (struct epic_service_call, then the body described here): + * + * - The sink's DP device service ("dcpdp-device-epic") reads and writes the + * receiver's HDCP 2.2 registers, addressed as in the DisplayPort HDCP 2.2 + * register map (DP_HDCP_2_2_REG_*). DCP runs the AUX transaction. A + * built-in HDMI port behind an on-board DP to HDMI converter is reached + * the same way: the converter is the HDCP repeater. + * - The HDCP interface service ("dcpdptx-hdcp-interface"), one per port, + * takes the transmitter's capability once it is opened. + * - An authentication session service ("dcpdptx-hdcp-auth-session"), + * announced by DCP for each attempt, calls the AP with the session's + * events (open, start, H' ready, pairing ready, ReceiverID list ready, + * close) and takes progress reports. DCP owns the receiver timeouts: an + * attempt that runs out of time is closed by DCP and opened again. + * + * Every body field is a little-endian 32-bit word at a 16-byte stride. + */ + +/* Register access on the DP device service. */ +#define DCP_HDCP_REG_GROUP 1 +#define DCP_HDCP_REG_READ 6 +#define DCP_HDCP_REG_WRITE 7 +/* The register arguments, then the data. */ +#define DCP_HDCP_REG_ARGS_LEN 64 +/* The longest register block: the receiver certificate. */ +#define DCP_HDCP_REG_MAX 1024 +/* The timeout DCP applies to a register read or write on the AUX side. */ +#define DCP_HDCP_REG_TIMEOUT_MS 500 + +/* The device service's query, the first call of a session. */ +#define DCP_HDCP_DEV_GROUP 0 +#define DCP_HDCP_DEV_QUERY 4 +#define DCP_HDCP_DEV_QUERY_LEN 0x20 +/* Its counterpart when the session ends: DCP waits for it before power-down. */ +#define DCP_HDCP_DEV_END 5 +#define DCP_HDCP_DEV_END_LEN 0x10 +/* The device service's information call, once after it is opened. */ +#define DCP_HDCP_DEV_INFO 0xc +#define DCP_HDCP_DEV_INFO_LEN 0x50 + +/* The HDCP interface service's capability call. */ +#define DCP_HDCP_IF_GROUP 4 +#define DCP_HDCP_IF_CAPS 6 +#define DCP_HDCP_IF_CAPS_LEN 0x20 +#define DCP_HDCP_IF_CAPS_VALUE 3 + +/* The authentication session's calls to the AP (group 0). */ +enum dcp_hdcp_auth_op { + DCP_HDCP_AUTH_START = 0x0, + DCP_HDCP_AUTH_CONFIG = 0x1, + DCP_HDCP_AUTH_OPEN = 0x7, + DCP_HDCP_AUTH_CLOSE = 0x8, + DCP_HDCP_AUTH_H_PRIME = 0x9, /* H' is available */ + DCP_HDCP_AUTH_PAIRING = 0xa, /* pairing information is available */ + DCP_HDCP_AUTH_LIST = 0xb, /* the ReceiverID list is available */ +}; + +/* The AP's progress report on the session. */ +#define DCP_HDCP_REPORT_GROUP 2 +#define DCP_HDCP_REPORT_PROGRESS 6 +#define DCP_HDCP_REPORT_LEN 0x50 + +enum dcp_hdcp_progress_code { + DCP_HDCP_PROGRESS_IDLE = 0x65, /* opened, or closed again */ + DCP_HDCP_PROGRESS_STARTED = 0x66, + DCP_HDCP_PROGRESS_AKE = 0x69, /* AKE running */ + DCP_HDCP_PROGRESS_KM_SENT = 0x6c, + DCP_HDCP_PROGRESS_H_VERIFIED = 0x6d, /* pairing still to come */ + DCP_HDCP_PROGRESS_AKE_DONE = 0x70, + DCP_HDCP_PROGRESS_STREAM_READY = 0x72, + DCP_HDCP_PROGRESS_ENCRYPTED = 0x74, + DCP_HDCP_PROGRESS_SKE_SENT = 0x75, + DCP_HDCP_PROGRESS_LIST_VERIFIED = 0x78, +}; + +struct dcp_hdcp_reg_read { + __le32 addr; + __le32 rsvd0[3]; + __le32 len; + __le32 rsvd1[3]; + __le32 timeout_ms; + __le32 rsvd2[7]; +} __packed; +static_assert(sizeof(struct dcp_hdcp_reg_read) == DCP_HDCP_REG_ARGS_LEN); + +struct dcp_hdcp_reg_write { + __le32 rsvd0[4]; + __le32 addr; + __le32 rsvd1[3]; + __le32 len; + __le32 rsvd2[3]; + __le32 timeout_ms; + __le32 rsvd3[3]; +} __packed; +static_assert(sizeof(struct dcp_hdcp_reg_write) == DCP_HDCP_REG_ARGS_LEN); + +/* One word at offset 16: the interface capability, the open's acceptance. */ +struct dcp_hdcp_word { + __le32 rsvd0[4]; + __le32 value; + __le32 rsvd1[3]; +} __packed; +static_assert(sizeof(struct dcp_hdcp_word) == DCP_HDCP_IF_CAPS_LEN); + +struct dcp_hdcp_report { + __le32 authenticated; + __le32 rsvd0[3]; + __le32 active; + __le32 rsvd1[3]; + __le32 encrypted; + __le32 rsvd2[3]; + __le32 repeater; + __le32 rsvd3[3]; + __le32 code; + __le32 rsvd4[3]; +} __packed; +static_assert(sizeof(struct dcp_hdcp_report) == DCP_HDCP_REPORT_LEN); + +static inline bool dcp_hdcp_reg_len_ok(size_t len) +{ + return len && len <= DCP_HDCP_REG_MAX; +} + +/* A read request: the arguments, then @len zero bytes for the data. */ +static inline int dcp_hdcp_reg_read_encode(void *body, size_t room, u32 addr, + size_t len, u32 timeout_ms) +{ + struct dcp_hdcp_reg_read *a = body; + + if (!dcp_hdcp_reg_len_ok(len) || room < DCP_HDCP_REG_ARGS_LEN + len) + return -EINVAL; + memset(body, 0, DCP_HDCP_REG_ARGS_LEN + len); + a->addr = cpu_to_le32(addr); + a->len = cpu_to_le32(len); + a->timeout_ms = cpu_to_le32(timeout_ms); + return DCP_HDCP_REG_ARGS_LEN + len; +} + +/* + * A read answer: it must name the register block asked for and carry all of + * its bytes. On success the data starts at body + DCP_HDCP_REG_ARGS_LEN. + */ +static inline int dcp_hdcp_reg_read_check(const void *body, size_t received, + u32 addr, size_t len) +{ + const struct dcp_hdcp_reg_read *a = body; + + if (!dcp_hdcp_reg_len_ok(len) || received < DCP_HDCP_REG_ARGS_LEN + len) + return -EPROTO; + if (le32_to_cpu(a->addr) != addr || le32_to_cpu(a->len) != len) + return -EPROTO; + return 0; +} + +static inline int dcp_hdcp_reg_write_encode(void *body, size_t room, u32 addr, + const void *data, size_t len) +{ + struct dcp_hdcp_reg_write *a = body; + + if (!dcp_hdcp_reg_len_ok(len) || room < DCP_HDCP_REG_ARGS_LEN + len) + return -EINVAL; + memset(body, 0, DCP_HDCP_REG_ARGS_LEN); + a->addr = cpu_to_le32(addr); + a->len = cpu_to_le32(len); + a->timeout_ms = cpu_to_le32(DCP_HDCP_REG_TIMEOUT_MS); + memcpy(body + DCP_HDCP_REG_ARGS_LEN, data, len); + return DCP_HDCP_REG_ARGS_LEN + len; +} + +static inline int dcp_hdcp_reg_write_check(const void *body, size_t received, + u32 addr, size_t len) +{ + const struct dcp_hdcp_reg_write *a = body; + + if (received < DCP_HDCP_REG_ARGS_LEN) + return -EPROTO; + if (le32_to_cpu(a->addr) != addr || le32_to_cpu(a->len) != len) + return -EPROTO; + return 0; +} + +static inline bool dcp_hdcp_auth_op_known(u32 op) +{ + switch (op) { + case DCP_HDCP_AUTH_START: + case DCP_HDCP_AUTH_CONFIG: + case DCP_HDCP_AUTH_OPEN: + case DCP_HDCP_AUTH_CLOSE: + case DCP_HDCP_AUTH_H_PRIME: + case DCP_HDCP_AUTH_PAIRING: + case DCP_HDCP_AUTH_LIST: + return true; + default: + return false; + } +} + +/* + * The AP's answer to a session call: the call's body back, and for the open + * whether the AP takes the session (1) or not (0). + */ +static inline int dcp_hdcp_auth_reply(u16 group, u32 op, const void *data, + size_t size, void *reply, size_t room, + bool accept) +{ + if (group || !dcp_hdcp_auth_op_known(op)) + return -EOPNOTSUPP; + memset(reply, 0, room); + memcpy(reply, data, min(size, room)); + if (op == DCP_HDCP_AUTH_OPEN) { + struct dcp_hdcp_word *w = reply; + + if (room < sizeof(*w)) + return -EPROTO; + w->value = cpu_to_le32(accept); + } + return 0; +} + +static inline void dcp_hdcp_report_encode(struct dcp_hdcp_report *r, + enum dcp_hdcp_progress_code code, + bool active, bool repeater, + bool encrypted) +{ + memset(r, 0, sizeof(*r)); + r->code = cpu_to_le32(code); + r->active = cpu_to_le32(active); + r->repeater = cpu_to_le32(repeater); + r->encrypted = cpu_to_le32(encrypted); + r->authenticated = cpu_to_le32(encrypted); +} + +#endif /* __APPLE_DCP_HDCP_WIRE_H__ */ diff --git a/drivers/gpu/drm/apple/dcp-hdcp.c b/drivers/gpu/drm/apple/dcp-hdcp.c new file mode 100644 index 0000000000000..d08fd99170628 --- /dev/null +++ b/drivers/gpu/drm/apple/dcp-hdcp.c @@ -0,0 +1,1165 @@ +// SPDX-License-Identifier: GPL-2.0-only OR MIT +/* Copyright 2026 Ryan Murray */ + +/* + * HDCP 2.2 on the display processors' external outputs. + * + * DCP opens an authentication session after the link comes up and calls the + * AP with the session's events; the AP answers each at once and runs the + * transmitter (dcp-hdcp-tx.c) on its own ordered queue, with the SEP doing + * the cryptography (include/linux/apple-sep-hdcp.h) and the sink's DP device + * service moving the receiver's messages. The connector's Content + * Protection property follows the policy in dcp-hdcp.h. No key material is + * seen here: only the protocol fields the HDCP 2.2 messages carry in the + * clear. + * + * Only a display processor whose node has "apple,hdcp-channels" takes + * part; elsewhere the property is not attached and nothing changes. While + * userspace does not ask for protection, DCP's sessions are declined. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "afk.h" +#include "connector.h" +#include "dcp.h" +#include "dcp-hdcp.h" +#include "dcp-hdcp-drv.h" +#include "dcp-hdcp-tx.h" +#include "dcp-hdcp-wire.h" +#include "dcp-internal.h" + + +/* + * How long a register access may take: DCP applies its own AUX timeout + * (DCP_HDCP_REG_TIMEOUT_MS, carried in the request); the extra 500 ms covers + * the mailbox round trip, so a slow transaction ends with DCP's answer, not + * ours. Not a spec value. + */ +#define DCP_HDCP_CALL_MS (DCP_HDCP_REG_TIMEOUT_MS + 500) +/* The receiver's status on an encrypted link. */ +#define DCP_HDCP_IFACES 4 +#define DCP_HDCP_QUEUE 16 + +struct dcp_hdcp_event { + u8 op; + struct apple_epic_service *service; +}; + +struct dcp_hdcp { + struct apple_dcp *dcp; + u32 channel; + + /* The policy, the link generation and the session's identity. */ + struct mutex lock; + struct dcp_hdcp_link link; + u32 gen; + u32 session_gen; + + /* Service pointers and session events, also touched from teardown. */ + spinlock_t svc_lock; + struct apple_epic_service *relay; + struct apple_epic_service *iface[DCP_HDCP_IFACES]; + struct apple_epic_service *session; + /* Sessions DCP announced for HDCP 1.x: declined, this is HDCP 2.2 only. */ + struct apple_epic_service *hdcp1[DCP_HDCP_IFACES]; + unsigned long ev_bits; + /* The device service was asked for a session (query) and not yet told it ended. */ + bool dev_begun; + bool closed; + bool cancel; + struct dcp_hdcp_event queue[DCP_HDCP_QUEUE]; + unsigned int q_head, q_tail; + wait_queue_head_t ev_wq; + + struct apple_dcp_afkep *dpdev_ep; + /* The HDCP interface endpoint, when this file started it. */ + struct apple_dcp_afkep *if_ep; + + struct workqueue_struct *wq; + struct work_struct event_work; + struct work_struct teardown_work; + struct work_struct prop_work; + struct work_struct relay_work; + struct work_struct caps_work; + /* Accepted session opens; a teardown older than the latest is stale. */ + unsigned int open_seq; + unsigned int td_seq; + + struct dcp_hdcp_tx tx; +}; + +static struct dcp_hdcp *dcp_hdcp_of(struct apple_epic_service *service) +{ + return service && service->ep ? READ_ONCE(service->ep->dcp->hdcp) : NULL; +} + +/* Pins the service in @slot, NULL if there is none or it is gone. */ +static struct apple_epic_service *dcp_hdcp_pin(struct dcp_hdcp *h, + struct apple_epic_service **slot) +{ + struct apple_epic_service *service; + unsigned long flags; + + spin_lock_irqsave(&h->svc_lock, flags); + service = afk_service_get(*slot); + spin_unlock_irqrestore(&h->svc_lock, flags); + return service; +} + +/* The DP device service: register access. */ + +static int dcp_hdcp_reg_read(void *ctx, u32 addr, void *buf, size_t len) +{ + struct dcp_hdcp *h = ctx; + struct apple_epic_service *service = dcp_hdcp_pin(h, &h->relay); + size_t received = 0; + void *body; + int n, ret; + + if (!service) + return -ENODEV; + body = kzalloc(DCP_HDCP_REG_ARGS_LEN + len, GFP_KERNEL); + if (!body) { + afk_service_put(service); + return -ENOMEM; + } + n = dcp_hdcp_reg_read_encode(body, DCP_HDCP_REG_ARGS_LEN + len, addr, + len, DCP_HDCP_REG_TIMEOUT_MS); + ret = n < 0 ? n : + afk_service_call_timeout_len(service, DCP_HDCP_REG_GROUP, + DCP_HDCP_REG_READ, body, n, 0, body, n, + 0, DCP_HDCP_CALL_MS, &received); + if (!ret) + ret = dcp_hdcp_reg_read_check(body, received, addr, len); + if (!ret) + memcpy(buf, body + DCP_HDCP_REG_ARGS_LEN, len); + kfree_sensitive(body); + afk_service_put(service); + return ret; +} + +static int dcp_hdcp_reg_write(void *ctx, u32 addr, const void *buf, size_t len) +{ + struct dcp_hdcp *h = ctx; + struct apple_epic_service *service = dcp_hdcp_pin(h, &h->relay); + size_t received = 0; + void *body; + int n, ret; + + if (!service) + return -ENODEV; + body = kzalloc(DCP_HDCP_REG_ARGS_LEN + len, GFP_KERNEL); + if (!body) { + afk_service_put(service); + return -ENOMEM; + } + n = dcp_hdcp_reg_write_encode(body, DCP_HDCP_REG_ARGS_LEN + len, addr, + buf, len); + ret = n < 0 ? n : + afk_service_call_timeout_len(service, DCP_HDCP_REG_GROUP, + DCP_HDCP_REG_WRITE, body, n, 0, body, n, + 0, DCP_HDCP_CALL_MS, &received); + if (!ret) + ret = dcp_hdcp_reg_write_check(body, received, addr, len); + kfree_sensitive(body); + afk_service_put(service); + return ret; +} + +/* A call with a zero body whose answer is not needed. */ +static int dcp_hdcp_plain_call(struct apple_epic_service *service, u16 group, + u32 command, size_t len, u32 value) +{ + struct dcp_hdcp_word *w; + void *body = kzalloc(len, GFP_KERNEL); + int ret; + + if (!body) + return -ENOMEM; + if (value && len >= sizeof(*w)) { + w = body; + w->value = cpu_to_le32(value); + } + ret = afk_service_call_timeout(service, group, command, body, len, 0, + body, len, 0, DCP_HDCP_CALL_MS); + kfree(body); + return ret; +} + +/* The session service: events in, progress reports out. */ + +static void dcp_hdcp_report(void *ctx, enum dcp_hdcp_progress_code code, + bool active, bool repeater, bool encrypted) +{ + struct dcp_hdcp *h = ctx; + struct apple_epic_service *service = dcp_hdcp_pin(h, &h->session); + struct { + struct epic_service_call call; + struct dcp_hdcp_report report; + } __packed msg = {}; + int ret; + + if (!service) + return; + msg.call.group = cpu_to_le16(DCP_HDCP_REPORT_GROUP); + msg.call.command = cpu_to_le32(DCP_HDCP_REPORT_PROGRESS); + msg.call.data_len = cpu_to_le32(sizeof(msg.report)); + msg.call.magic = cpu_to_le32(EPIC_SERVICE_CALL_MAGIC); + dcp_hdcp_report_encode(&msg.report, code, active, repeater, encrypted); + ret = afk_send_epic(service->ep, service->channel, 0, EPIC_TYPE_NOTIFY, + EPIC_CAT_REPORT, EPIC_SUBTYPE_STD_SERVICE, &msg, + sizeof(msg)); + if (ret) + dev_dbg(h->dcp->dev, "HDCP: progress %#x not reported: %d\n", + code, ret); + afk_service_put(service); +} + +static int dcp_hdcp_wait(void *ctx, enum dcp_hdcp_auth_op op, unsigned int ms) +{ + struct dcp_hdcp *h = ctx; + unsigned long flags; + bool got; + + wait_event_timeout(h->ev_wq, + test_bit(op, &h->ev_bits) || READ_ONCE(h->closed) || + READ_ONCE(h->cancel), msecs_to_jiffies(ms)); + spin_lock_irqsave(&h->svc_lock, flags); + if (h->closed || h->cancel) { + spin_unlock_irqrestore(&h->svc_lock, flags); + return -ECANCELED; + } + got = __test_and_clear_bit(op, &h->ev_bits); + spin_unlock_irqrestore(&h->svc_lock, flags); + return got ? 0 : -ETIMEDOUT; +} + +static int dcp_hdcp_revoked(void *ctx, u8 *ids, unsigned int count) +{ + struct dcp_hdcp *h = ctx; + struct apple_connector *connector = READ_ONCE(h->dcp->connector); + + if (!connector) + return -ENODEV; + return drm_hdcp_check_ksvs_revoked(connector->base.dev, ids, count); +} + +static void dcp_hdcp_sleep(void *ctx, unsigned int ms) +{ + msleep(ms); +} + +/* The SEP service. */ + +static int sep_open(void *ctx, u32 ch, u8 *rtx) +{ + return apple_sep_hdcp_open(ch, rtx); +} + +static int sep_ake_cert(void *ctx, u32 ch, const u8 *cert, const u8 *rrx, + bool *stored, u8 *ekh_km_m, u8 *ekpub_km) +{ + struct apple_sep_hdcp_stored_km km; + int ret; + + static_assert(sizeof(km) == HDCP_2_2_E_KH_KM_M_LEN); + ret = apple_sep_hdcp_ake_cert(ch, cert, rrx, stored, &km, ekpub_km); + if (!ret && *stored) + memcpy(ekh_km_m, &km, sizeof(km)); + memzero_explicit(&km, sizeof(km)); + return ret; +} + +static int sep_h_prime(void *ctx, u32 ch, const u8 *rxcaps, const u8 *txcaps, + const u8 *h_prime) +{ + return apple_sep_hdcp_ake_h_prime(ch, rxcaps, txcaps, h_prime); +} + +static int sep_pairing(void *ctx, u32 ch, const u8 *ekh_km) +{ + return apple_sep_hdcp_ake_pairing(ch, ekh_km); +} + +static int sep_lc_init(void *ctx, u32 ch, u8 *rn) +{ + return apple_sep_hdcp_lc_init(ch, rn); +} + +static int sep_l_prime(void *ctx, u32 ch, const u8 *l_prime) +{ + return apple_sep_hdcp_lc_l_prime(ch, l_prime); +} + +static int sep_ske(void *ctx, u32 ch, u8 *edkey_ks, u8 *riv) +{ + return apple_sep_hdcp_ske(ch, edkey_ks, riv); +} + +static int sep_repeater_list(void *ctx, u32 ch, const u8 *ids, + unsigned int count, const u8 *rxinfo, + const u8 *seq_num_v, const u8 *v_prime, u8 *v) +{ + return apple_sep_hdcp_repeater_list(ch, ids, count, rxinfo, seq_num_v, + v_prime, v); +} + +static int sep_repeater_stream(void *ctx, u32 ch, u8 stream_id, u8 type, + const u8 *seq_num_m, const u8 *m_prime) +{ + return apple_sep_hdcp_repeater_stream(ch, stream_id, type, seq_num_m, + m_prime); +} + +static int sep_finish(void *ctx, u32 ch) +{ + return apple_sep_hdcp_finish(ch); +} + +static int sep_set_encryption(void *ctx, u32 ch, bool on) +{ + return apple_sep_hdcp_set_encryption(ch, on); +} + +static void sep_close(void *ctx, u32 ch) +{ + apple_sep_hdcp_close(ch); +} + +static const struct dcp_hdcp_tx_ops dcp_hdcp_tx_ops = { + .read = dcp_hdcp_reg_read, + .write = dcp_hdcp_reg_write, + .wait = dcp_hdcp_wait, + .report = dcp_hdcp_report, + .revoked = dcp_hdcp_revoked, + .sleep = dcp_hdcp_sleep, + .sep_open = sep_open, + .sep_ake_cert = sep_ake_cert, + .sep_h_prime = sep_h_prime, + .sep_pairing = sep_pairing, + .sep_lc_init = sep_lc_init, + .sep_l_prime = sep_l_prime, + .sep_ske = sep_ske, + .sep_repeater_list = sep_repeater_list, + .sep_repeater_stream = sep_repeater_stream, + .sep_finish = sep_finish, + .sep_set_encryption = sep_set_encryption, + .sep_close = sep_close, +}; + +/* Policy glue. */ + +/* What the connector this processor drives asks for. */ +static void dcp_hdcp_want(struct dcp_hdcp *h, bool *want, u8 *type) +{ + struct apple_connector *connector = READ_ONCE(h->dcp->connector); + + *want = connector && READ_ONCE(connector->hdcp_attached) && + READ_ONCE(connector->hdcp_want); + *type = connector ? READ_ONCE(connector->hdcp_type) : 0; +} + +static void dcp_hdcp_apply(struct dcp_hdcp *h, enum dcp_hdcp_action action) +{ + unsigned long flags; + + if (action == DCP_HDCP_TEARDOWN) { + spin_lock_irqsave(&h->svc_lock, flags); + h->cancel = true; + h->td_seq = h->open_seq; + spin_unlock_irqrestore(&h->svc_lock, flags); + wake_up_all(&h->ev_wq); + queue_work(h->wq, &h->teardown_work); + } + queue_work(h->wq, &h->prop_work); +} + +/* The bound state of both sides, then what the connector asks for. */ +static void dcp_hdcp_sync_locked(struct dcp_hdcp *h) +{ + unsigned long now = jiffies; + enum dcp_hdcp_action a, b; + bool want, relay; + u8 type; + + relay = READ_ONCE(h->relay) != NULL; + a = dcp_hdcp_bind(&h->link, relay, apple_sep_hdcp_available(), now); + dcp_hdcp_want(h, &want, &type); + b = dcp_hdcp_request(&h->link, want, type, now); + dcp_hdcp_apply(h, a == DCP_HDCP_TEARDOWN ? a : b); +} + +static void dcp_hdcp_sync(struct dcp_hdcp *h) +{ + mutex_lock(&h->lock); + dcp_hdcp_sync_locked(h); + mutex_unlock(&h->lock); +} + +static void dcp_hdcp_prop_work(struct work_struct *work) +{ + struct dcp_hdcp *h = container_of(work, struct dcp_hdcp, prop_work); + struct apple_connector *connector = READ_ONCE(h->dcp->connector); + struct drm_device *drm; + u64 value; + + if (!connector || !READ_ONCE(connector->hdcp_attached)) + return; + mutex_lock(&h->lock); + value = dcp_hdcp_property(&h->link); + mutex_unlock(&h->lock); + /* UNDESIRED is userspace's to set, never the driver's. */ + if (value == DRM_MODE_CONTENT_PROTECTION_UNDESIRED || + !READ_ONCE(connector->hdcp_want)) + return; + drm = connector->base.dev; + drm_modeset_lock(&drm->mode_config.connection_mutex, NULL); + if (READ_ONCE(connector->hdcp_want)) + drm_hdcp_update_content_protection(&connector->base, value); + drm_modeset_unlock(&drm->mode_config.connection_mutex); +} + +/* + * The transmitter's capability, once per HDCP interface after it opens (as + * macOS does at boot): DCP then offers sessions at each link-up. + */ +static void dcp_hdcp_caps_work(struct work_struct *work) +{ + struct dcp_hdcp *h = container_of(work, struct dcp_hdcp, caps_work); + struct apple_epic_service *service; + int i; + + for (i = 0; i < DCP_HDCP_IFACES; i++) { + service = dcp_hdcp_pin(h, &h->iface[i]); + if (!service) + continue; + dcp_hdcp_plain_call(service, DCP_HDCP_IF_GROUP, DCP_HDCP_IF_CAPS, + DCP_HDCP_IF_CAPS_LEN, DCP_HDCP_IF_CAPS_VALUE); + afk_service_put(service); + } +} + +/* Tells the DP device service the session it was asked for has ended. */ +static void dcp_hdcp_dev_end(struct dcp_hdcp *h) +{ + struct apple_epic_service *relay; + + if (!xchg(&h->dev_begun, false)) + return; + relay = dcp_hdcp_pin(h, &h->relay); + if (!relay) + return; + dcp_hdcp_plain_call(relay, DCP_HDCP_DEV_GROUP, DCP_HDCP_DEV_END, + DCP_HDCP_DEV_END_LEN, 0); + afk_service_put(relay); +} + +/* Encryption off, the SEP session closed, DCP told; the picture stays. */ +static void dcp_hdcp_teardown_work(struct work_struct *work) +{ + struct dcp_hdcp *h = container_of(work, struct dcp_hdcp, teardown_work); + unsigned long flags; + bool was_open, stale; + + /* + * A session DCP opened after this teardown was asked for has already + * replaced what it would end (dcp_hdcp_run() aborts a leftover first). + */ + spin_lock_irqsave(&h->svc_lock, flags); + stale = h->open_seq != h->td_seq; + spin_unlock_irqrestore(&h->svc_lock, flags); + if (stale) + return; + was_open = h->tx.sep_open; + dcp_hdcp_abort(&h->tx); + if (was_open) + dcp_hdcp_report(h, DCP_HDCP_PROGRESS_IDLE, false, false, false); + dcp_hdcp_dev_end(h); +} + +/* One authentication, from DCP's start of the session to its result. */ +static void dcp_hdcp_run(struct dcp_hdcp *h) +{ + struct apple_epic_service *relay = dcp_hdcp_pin(h, &h->relay); + enum dcp_hdcp_action action; + u32 gen; + int ret; + + mutex_lock(&h->lock); + gen = h->session_gen; + h->tx.type = h->link.type; + /* Only a session the policy took, on its generation. */ + if (h->link.state != DCP_HDCP_AUTH || h->link.generation != gen) { + mutex_unlock(&h->lock); + afk_service_put(relay); + return; + } + mutex_unlock(&h->lock); + /* What an earlier session left open (its teardown has not run yet). */ + if (h->tx.sep_open) + dcp_hdcp_abort(&h->tx); + h->tx.ops = &dcp_hdcp_tx_ops; + h->tx.ctx = h; + h->tx.channel = h->channel; + + if (relay) { + /* The device service's query opens each session; dcp_hdcp_dev_end() closes it. */ + dcp_hdcp_dev_end(h); + if (!dcp_hdcp_plain_call(relay, DCP_HDCP_DEV_GROUP, DCP_HDCP_DEV_QUERY, + DCP_HDCP_DEV_QUERY_LEN, 0)) + WRITE_ONCE(h->dev_begun, true); + afk_service_put(relay); + ret = dcp_hdcp_authenticate(&h->tx); + } else { + ret = -ENODEV; + } + if (ret) { + dev_info(h->dcp->dev, "HDCP: authentication failed at %s: %d\n", + h->tx.step ?: "start", ret); + dcp_hdcp_abort(&h->tx); + } else { + dev_info(h->dcp->dev, "HDCP: encrypted, type %u%s\n", h->tx.type, + h->tx.repeater ? " (repeater)" : ""); + } + mutex_lock(&h->lock); + action = dcp_hdcp_auth_done(&h->link, gen, ret, jiffies); + mutex_unlock(&h->lock); + /* The SEP session is already closed after a failure. */ + if (action != DCP_HDCP_TEARDOWN) + dcp_hdcp_apply(h, action); + else + queue_work(h->wq, &h->prop_work); +} + +static void dcp_hdcp_close(struct dcp_hdcp *h, struct apple_epic_service *service) +{ + enum dcp_hdcp_action action; + unsigned long flags; + bool ours; + + /* A close of an earlier session, after DCP opened a newer one: nothing to end. */ + spin_lock_irqsave(&h->svc_lock, flags); + ours = !h->session || h->session == service; + spin_unlock_irqrestore(&h->svc_lock, flags); + if (!ours) + return; + dcp_hdcp_abort(&h->tx); + mutex_lock(&h->lock); + action = dcp_hdcp_closed(&h->link, h->session_gen, jiffies); + mutex_unlock(&h->lock); + if (action != DCP_HDCP_TEARDOWN) + dcp_hdcp_apply(h, action); + /* The session goes back to idle on DCP's side. */ + if (service) + dcp_hdcp_report(h, DCP_HDCP_PROGRESS_IDLE, false, false, false); + dcp_hdcp_dev_end(h); + spin_lock_irqsave(&h->svc_lock, flags); + if (h->session == service) + h->session = NULL; + spin_unlock_irqrestore(&h->svc_lock, flags); + queue_work(h->wq, &h->prop_work); +} + +static void dcp_hdcp_event_work(struct work_struct *work) +{ + struct dcp_hdcp *h = container_of(work, struct dcp_hdcp, event_work); + struct dcp_hdcp_event ev; + unsigned long flags; + + for (;;) { + spin_lock_irqsave(&h->svc_lock, flags); + if (h->q_head == h->q_tail) { + spin_unlock_irqrestore(&h->svc_lock, flags); + return; + } + ev = h->queue[h->q_tail++ % DCP_HDCP_QUEUE]; + spin_unlock_irqrestore(&h->svc_lock, flags); + + switch (ev.op) { + case DCP_HDCP_AUTH_OPEN: + dcp_hdcp_report(h, DCP_HDCP_PROGRESS_IDLE, false, false, + false); + break; + case DCP_HDCP_AUTH_START: + dcp_hdcp_report(h, DCP_HDCP_PROGRESS_STARTED, false, + false, false); + break; + case DCP_HDCP_AUTH_CONFIG: + dcp_hdcp_run(h); + break; + case DCP_HDCP_AUTH_CLOSE: + dcp_hdcp_close(h, ev.service); + break; + } + afk_service_put(ev.service); + } +} + +static void dcp_hdcp_queue(struct dcp_hdcp *h, struct apple_epic_service *service, + u8 op) +{ + struct apple_epic_service *pinned = afk_service_get(service); + unsigned long flags; + bool full; + + if (!pinned) + return; + spin_lock_irqsave(&h->svc_lock, flags); + full = h->q_head - h->q_tail >= DCP_HDCP_QUEUE; + if (!full) + h->queue[h->q_head++ % DCP_HDCP_QUEUE] = + (struct dcp_hdcp_event){ op, pinned }; + spin_unlock_irqrestore(&h->svc_lock, flags); + if (full) { + dev_warn_ratelimited(h->dcp->dev, "HDCP: session event %#x dropped\n", op); + afk_service_put(pinned); + return; + } + queue_work(h->wq, &h->event_work); +} + +/* Whether DCP announced @service as an HDCP 1.x session. */ +static bool dcp_hdcp_is_hdcp1(struct dcp_hdcp *h, struct apple_epic_service *service) +{ + unsigned long flags; + bool ret = false; + int i; + + spin_lock_irqsave(&h->svc_lock, flags); + for (i = 0; i < DCP_HDCP_IFACES; i++) + ret |= h->hdcp1[i] == service; + spin_unlock_irqrestore(&h->svc_lock, flags); + return ret; +} + +/* + * DCP announces one session per protocol version; the provider class says + * which, where the announcement carries one. A session without a class is + * taken for HDCP 2.2: the receiver registers it reads decide. + */ +static void dcp_hdcp_session_init(struct apple_epic_service *service, + const char *name, const char *class, s64 unit) +{ + struct dcp_hdcp *h = dcp_hdcp_of(service); + unsigned long flags; + int i; + + if (!h || !class || !strstr(class, "HDCP1")) + return; + spin_lock_irqsave(&h->svc_lock, flags); + for (i = 0; i < DCP_HDCP_IFACES; i++) { + if (h->hdcp1[i] == service) + break; + if (!h->hdcp1[i]) { + h->hdcp1[i] = service; + break; + } + } + spin_unlock_irqrestore(&h->svc_lock, flags); + dev_dbg(h->dcp->dev, "HDCP: HDCP 1.x session announced, declined\n"); +} + +static int dcp_hdcp_session_call(struct apple_epic_service *service, u16 group, + u32 command, const void *data, size_t size, + void *reply, size_t room) +{ + struct dcp_hdcp *h = dcp_hdcp_of(service); + unsigned long flags; + bool accept = false, ours; + int ret; + + if (h && !group && command == DCP_HDCP_AUTH_OPEN) { + mutex_lock(&h->lock); + dcp_hdcp_sync_locked(h); + accept = !dcp_hdcp_is_hdcp1(h, service) && + dcp_hdcp_open(&h->link, h->gen, jiffies); + if (accept) + h->session_gen = h->gen; + mutex_unlock(&h->lock); + spin_lock_irqsave(&h->svc_lock, flags); + if (accept) { + h->session = service; + h->ev_bits = 0; + h->closed = false; + h->cancel = false; + h->open_seq++; + } else if (h->session == service) { + /* The slot of an earlier session, declined this time. */ + h->session = NULL; + } + spin_unlock_irqrestore(&h->svc_lock, flags); + } + ret = dcp_hdcp_auth_reply(group, command, data, size, reply, room, + accept); + if (ret == -EOPNOTSUPP && h && !group) { + /* Not a session step: answered and logged. */ + memset(reply, 0, room); + memcpy(reply, data, min(size, room)); + dev_dbg(h->dcp->dev, "HDCP: session call %#x\n", command); + return 0; + } + if (ret || !h) + return ret; + + spin_lock_irqsave(&h->svc_lock, flags); + ours = h->session == service; + switch (command) { + case DCP_HDCP_AUTH_H_PRIME: + case DCP_HDCP_AUTH_PAIRING: + case DCP_HDCP_AUTH_LIST: + if (ours) + __set_bit(command, &h->ev_bits); + break; + case DCP_HDCP_AUTH_CLOSE: + if (ours) + h->closed = true; + break; + } + spin_unlock_irqrestore(&h->svc_lock, flags); + wake_up_all(&h->ev_wq); + + if (ours && (command == DCP_HDCP_AUTH_OPEN || command == DCP_HDCP_AUTH_START || + command == DCP_HDCP_AUTH_CONFIG || command == DCP_HDCP_AUTH_CLOSE)) + dcp_hdcp_queue(h, service, command); + return 0; +} + +static void dcp_hdcp_session_teardown(struct apple_epic_service *service) +{ + struct dcp_hdcp *h = dcp_hdcp_of(service); + unsigned long flags; + bool ours; + int i; + + if (!h) + return; + spin_lock_irqsave(&h->svc_lock, flags); + for (i = 0; i < DCP_HDCP_IFACES; i++) + if (h->hdcp1[i] == service) + h->hdcp1[i] = NULL; + ours = h->session == service; + if (ours) { + h->closed = true; + h->session = NULL; + } + spin_unlock_irqrestore(&h->svc_lock, flags); + if (ours) { + wake_up_all(&h->ev_wq); + dcp_hdcp_apply(h, DCP_HDCP_TEARDOWN); + } +} + +static void dcp_hdcp_relay_work(struct work_struct *work) +{ + struct dcp_hdcp *h = container_of(work, struct dcp_hdcp, relay_work); + struct apple_epic_service *service = dcp_hdcp_pin(h, &h->relay); + + if (!service) + return; + /* The device service's information call, once after it is opened. */ + dcp_hdcp_plain_call(service, DCP_HDCP_DEV_GROUP, DCP_HDCP_DEV_INFO, + DCP_HDCP_DEV_INFO_LEN, 0); + afk_service_put(service); + dcp_hdcp_sync(h); +} + +static void dcp_hdcp_relay_init(struct apple_epic_service *service, + const char *name, const char *class, s64 unit) +{ + struct dcp_hdcp *h = dcp_hdcp_of(service); + unsigned long flags; + + if (!h) + return; + spin_lock_irqsave(&h->svc_lock, flags); + h->relay = service; + spin_unlock_irqrestore(&h->svc_lock, flags); + dev_dbg(h->dcp->dev, "HDCP: DP device service on interface %u\n", + service->channel); + queue_work(h->wq, &h->relay_work); +} + +static void dcp_hdcp_relay_teardown(struct apple_epic_service *service) +{ + struct dcp_hdcp *h = dcp_hdcp_of(service); + unsigned long flags; + bool ours; + + if (!h) + return; + spin_lock_irqsave(&h->svc_lock, flags); + ours = h->relay == service; + if (ours) + h->relay = NULL; + spin_unlock_irqrestore(&h->svc_lock, flags); + if (ours) + dcp_hdcp_apply(h, DCP_HDCP_TEARDOWN); +} + +static void dcp_hdcp_iface_init(struct apple_epic_service *service, + const char *name, const char *class, s64 unit) +{ + struct dcp_hdcp *h = dcp_hdcp_of(service); + unsigned long flags; + int i; + + if (!h) + return; + spin_lock_irqsave(&h->svc_lock, flags); + for (i = 0; i < DCP_HDCP_IFACES; i++) { + if (h->iface[i] == service) + break; + if (!h->iface[i]) { + h->iface[i] = service; + break; + } + } + spin_unlock_irqrestore(&h->svc_lock, flags); + queue_work(h->wq, &h->caps_work); +} + +static void dcp_hdcp_iface_teardown(struct apple_epic_service *service) +{ + struct dcp_hdcp *h = dcp_hdcp_of(service); + unsigned long flags; + int i; + + if (!h) + return; + spin_lock_irqsave(&h->svc_lock, flags); + for (i = 0; i < DCP_HDCP_IFACES; i++) + if (h->iface[i] == service) + h->iface[i] = NULL; + spin_unlock_irqrestore(&h->svc_lock, flags); +} + +static void dcp_hdcp_other_init(struct apple_epic_service *service, + const char *name, const char *class, s64 unit) +{ +} + +#define DCP_HDCP_RELAY_OPS { \ + .name = "dcpdp-device-epic", \ + .compact_full = "dcpdp-device-epic:0", \ + .singleton = true, \ + .init = dcp_hdcp_relay_init, \ + .teardown = dcp_hdcp_relay_teardown, \ +} + +static const struct apple_epic_service_ops dcp_hdcp_service_ops[] = { + DCP_HDCP_RELAY_OPS, + { + .name = "dcpdptx-hdcp-interface", + .init = dcp_hdcp_iface_init, + .teardown = dcp_hdcp_iface_teardown, + }, + { + .name = "dcpdptx-hdcp-auth-session", + .compact_full = "dcpdptx-hdcp-auth-session", + /* + * Not a singleton: DCP announces one session per protocol + * version and closes each before it powers down, so every + * one needs its handler to answer. + */ + .init = dcp_hdcp_session_init, + .call_v2 = dcp_hdcp_session_call, + .teardown = dcp_hdcp_session_teardown, + }, + {}, +}; + +/* The DP device endpoint when this file starts it: the sink's services. */ +static const struct apple_epic_service_ops dcp_hdcp_dpdev_ops[] = { + DCP_HDCP_RELAY_OPS, + { .name = "*", .init = dcp_hdcp_other_init }, + {}, +}; + +const struct apple_epic_service_ops * +dcp_hdcp_match_service(struct apple_dcp_afkep *ep, const char *service_name, + bool cut) +{ + const struct apple_epic_service_ops *ops; + size_t len; + + if (!ep || !READ_ONCE(ep->dcp->hdcp) || !service_name) + return NULL; + for (ops = dcp_hdcp_service_ops; ops->name[0]; ops++) { + len = strlen(ops->name); + if (!strncmp(ops->name, service_name, len) && + (!service_name[len] || service_name[len] == ':')) + return ops; + if (cut && afk_compact_cut_of(ops, service_name)) + return ops; + } + return NULL; +} + +/* + * The endpoints that carry the HDCP interface services, by firmware: the + * first one advertised and not driven by anything else is started here. + */ +static const u8 dcp_hdcp_if_endpoints[] = { AV_AUDIO_ENDPOINT_DESKTOP, DPTX_HDCP_ENDPOINT }; + +static struct apple_dcp_afkep *dcp_hdcp_start_one(struct apple_dcp *dcp, u8 endpoint, + const struct apple_epic_service_ops *ops, + struct apple_dcp_afkep **slot, const char *what) +{ + struct apple_dcp_afkep *ep; + int ret; + + if (!apple_rtkit_has_endpoint(dcp->rtk, endpoint) || dcp_endpoint_routed(dcp, endpoint)) + return NULL; + ep = afk_init(dcp, endpoint, ops); + if (IS_ERR(ep)) { + dev_warn(dcp->dev, "HDCP: %s endpoint %#x not set up: %ld\n", what, + endpoint, PTR_ERR(ep)); + return NULL; + } + ep->debugfs_entry = dcp->ep_debugfs[endpoint - 0x20]; + /* Routed before the start: afk_start() waits for the firmware's answer. */ + WRITE_ONCE(*slot, ep); + ret = afk_start(ep); + if (ret) + dev_warn(dcp->dev, "HDCP: %s endpoint %#x not started: %d\n", what, + endpoint, ret); + else + dev_info(dcp->dev, "HDCP: started the %s endpoint %#x\n", what, endpoint); + return ep; +} + +void dcp_hdcp_start_endpoints(struct apple_dcp *dcp) +{ + struct dcp_hdcp *h = READ_ONCE(dcp->hdcp); + int i; + + if (!h || IS_ERR_OR_NULL(dcp->rtk)) + return; + if (!h->dpdev_ep) + dcp_hdcp_start_one(dcp, DPDEV_ENDPOINT, dcp_hdcp_dpdev_ops, + &h->dpdev_ep, "DP device"); + for (i = 0; i < ARRAY_SIZE(dcp_hdcp_if_endpoints) && !h->if_ep; i++) + dcp_hdcp_start_one(dcp, dcp_hdcp_if_endpoints[i], dcp_hdcp_service_ops, + &h->if_ep, "HDCP interface"); +} + +struct apple_dcp_afkep *dcp_hdcp_route(struct apple_dcp *dcp, u32 endpoint) +{ + struct dcp_hdcp *h = READ_ONCE(dcp->hdcp); + struct apple_dcp_afkep *ep; + + if (!h) + return NULL; + ep = READ_ONCE(h->dpdev_ep); + if (ep && ep->endpoint == endpoint) + return ep; + ep = READ_ONCE(h->if_ep); + return ep && ep->endpoint == endpoint ? ep : NULL; +} + +void dcp_hdcp_link_changed(struct apple_dcp *dcp) +{ + struct dcp_hdcp *h = READ_ONCE(dcp->hdcp); + enum dcp_hdcp_action action; + + if (!h) + return; + mutex_lock(&h->lock); + h->gen++; + action = dcp_hdcp_link_event(&h->link, h->gen, jiffies); + mutex_unlock(&h->lock); + if (action == DCP_HDCP_TEARDOWN) + dcp_hdcp_apply(h, action); + dcp_hdcp_sync(h); +} + +static bool dcp_hdcp_channel(struct apple_dcp *dcp, u32 *channel) +{ + return !of_property_read_u32_index(dcp->dev->of_node, + "apple,hdcp-channels", 0, channel); +} + +static void dcp_hdcp_destroy_wq(void *data) +{ + destroy_workqueue(data); +} + +struct dcp_hdcp *dcp_hdcp_init(struct apple_dcp *dcp) +{ + struct dcp_hdcp *h = READ_ONCE(dcp->hdcp); + u32 channel; + + if (h || dcp_has_panel(dcp) || !dcp_hdcp_channel(dcp, &channel)) + return h; + h = devm_kzalloc(dcp->dev, sizeof(*h), GFP_KERNEL); + if (!h) + return NULL; + h->dcp = dcp; + h->channel = channel; + mutex_init(&h->lock); + spin_lock_init(&h->svc_lock); + init_waitqueue_head(&h->ev_wq); + INIT_WORK(&h->event_work, dcp_hdcp_event_work); + INIT_WORK(&h->teardown_work, dcp_hdcp_teardown_work); + INIT_WORK(&h->prop_work, dcp_hdcp_prop_work); + INIT_WORK(&h->relay_work, dcp_hdcp_relay_work); + INIT_WORK(&h->caps_work, dcp_hdcp_caps_work); + /* Not freezable: a suspend's teardown must reach DCP before it sleeps. */ + h->wq = alloc_ordered_workqueue("apple-dcp-hdcp", 0); + if (!h->wq) + return NULL; + if (devm_add_action_or_reset(dcp->dev, dcp_hdcp_destroy_wq, h->wq)) + return NULL; + h->tx.ops = &dcp_hdcp_tx_ops; + h->tx.ctx = h; + h->tx.channel = channel; + WRITE_ONCE(dcp->hdcp, h); + dev_info(dcp->dev, "HDCP: transmitter on SEP channel %u\n", channel); + return h; +} + +/* + * Component unbind: the state is device-managed and goes with the bind, so + * nothing may reach it afterwards. Stop all work, end what is open, shut the + * endpoints this file started, and unpublish it. + */ +void dcp_hdcp_fini(struct apple_dcp *dcp) +{ + struct dcp_hdcp *h = READ_ONCE(dcp->hdcp); + struct apple_dcp_afkep *ep; + unsigned long flags; + + if (!h) + return; + spin_lock_irqsave(&h->svc_lock, flags); + h->cancel = true; + spin_unlock_irqrestore(&h->svc_lock, flags); + wake_up_all(&h->ev_wq); + disable_work_sync(&h->event_work); + disable_work_sync(&h->teardown_work); + disable_work_sync(&h->prop_work); + disable_work_sync(&h->relay_work); + disable_work_sync(&h->caps_work); + dcp_hdcp_abort(&h->tx); + dcp_hdcp_dev_end(h); + ep = xchg(&h->if_ep, NULL); + if (ep) + afk_shutdown(ep); + ep = xchg(&h->dpdev_ep, NULL); + if (ep) + afk_shutdown(ep); + WRITE_ONCE(dcp->hdcp, NULL); +} + +/* + * Before DCP sleeps or powers a pipe down: the link change, then its + * teardown done here, so encryption is off and DCP has the device service's + * end call before it waits for it. + */ +void dcp_hdcp_quiesce(struct apple_dcp *dcp) +{ + struct dcp_hdcp *h = READ_ONCE(dcp->hdcp); + + dcp_hdcp_link_changed(dcp); + if (h) + flush_work(&h->teardown_work); +} + +/* DRM. */ + +int dcp_hdcp_attach_connector(struct apple_connector *connector, + struct platform_device **dcps, unsigned int n, + int connector_type) +{ + bool channel = false; + unsigned int i; + int ret; + + if (connector->hdcp_attached || + connector_type == DRM_MODE_CONNECTOR_eDP) + return 0; + for (i = 0; i < n; i++) { + struct apple_dcp *dcp = dcps[i] ? platform_get_drvdata(dcps[i]) : NULL; + + if (dcp && dcp_hdcp_init(dcp)) + channel = true; + } + if (!dcp_hdcp_attach_property(true, channel, + IS_ENABLED(CONFIG_APPLE_SEP_HDCP_INTERFACE))) + return 0; + ret = drm_connector_attach_content_protection_property(&connector->base, + true); + if (ret) + return ret; + connector->hdcp_attached = true; + return 0; +} + +void dcp_hdcp_atomic_check(struct drm_connector *conn, + struct drm_atomic_commit *state) +{ + struct drm_connector_state *old = drm_atomic_get_old_connector_state(state, conn); + struct drm_connector_state *new = drm_atomic_get_new_connector_state(state, conn); + struct drm_crtc_state *crtc_state; + + if (!to_apple_connector(conn)->hdcp_attached || !old || !new) + return; + /* A connector switched off keeps asking: DESIRED again when it is back. */ + if (!new->crtc) { + if (old->content_protection == DRM_MODE_CONTENT_PROTECTION_ENABLED) + new->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; + return; + } + crtc_state = drm_atomic_get_new_crtc_state(state, new->crtc); + if (old->content_protection != DRM_MODE_CONTENT_PROTECTION_ENABLED || + new->content_protection == DRM_MODE_CONTENT_PROTECTION_UNDESIRED) + return; + /* A mode set or another type: protected again only after a new session. */ + if ((crtc_state && drm_atomic_crtc_needs_modeset(crtc_state)) || + old->hdcp_content_type != new->hdcp_content_type) + new->content_protection = DRM_MODE_CONTENT_PROTECTION_DESIRED; +} + +void dcp_hdcp_commit(struct drm_atomic_commit *state) +{ + struct drm_connector_state *new; + struct drm_connector *conn; + int i; + + for_each_new_connector_in_state(state, conn, new, i) { + struct apple_connector *connector = to_apple_connector(conn); + struct platform_device *pdev = READ_ONCE(connector->dcp); + struct apple_dcp *dcp; + + if (!connector->hdcp_attached) + continue; + WRITE_ONCE(connector->hdcp_type, new->hdcp_content_type); + WRITE_ONCE(connector->hdcp_want, new->content_protection != + DRM_MODE_CONTENT_PROTECTION_UNDESIRED); + dcp = pdev ? platform_get_drvdata(pdev) : NULL; + if (dcp && READ_ONCE(dcp->hdcp)) + dcp_hdcp_sync(dcp->hdcp); + } +} + +#if IS_ENABLED(CONFIG_DRM_APPLE_KUNIT_TEST) +#include "dcp-hdcp-glue-test.c" +#endif diff --git a/drivers/gpu/drm/apple/dcp-hdcp.h b/drivers/gpu/drm/apple/dcp-hdcp.h new file mode 100644 index 0000000000000..0c4f09a0e57fa --- /dev/null +++ b/drivers/gpu/drm/apple/dcp-hdcp.h @@ -0,0 +1,230 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ +/* Copyright 2026 Ryan Murray */ +#ifndef __APPLE_DCP_HDCP_H__ +#define __APPLE_DCP_HDCP_H__ + +#include +#include +#include +#include +#include +#include +#include + +/* + * HDCP 2.2 on an external connector, as the AP runs it: the AP is the + * transmitter, the SEP holds its secrets and does the cryptography, and DCP + * carries the receiver's messages over the link (see dcp-hdcp-wire.h). The + * built-in panel never gets a receiver session, so this never applies to it. + * + * This file is the Content Protection policy, with no hardware behind it: + * + * - DCP opens an authentication session after the link comes up. The AP + * takes it only while protection is DESIRED, the SEP service and the DCP + * relay are bound and no backoff is pending; otherwise it declines. + * - ENABLED needs a complete authentication, the 200 ms wait and an + * acknowledged encryption enable, all in one session on one link + * generation. + * - A failure, a session DCP closes before the end, a new link generation, + * or the SEP service or relay going away drops the session to DESIRED and + * asks for teardown: encryption off and the SEP session closed. A late + * result from a session or generation that is gone is ignored. The display + * itself is never touched: a failed session leaves an unprotected, working + * output. + * - A changed content type re-authenticates; UNDESIRED tears down. + * - Retries back off: DCP_HDCP_RETRY_MS doubled per failure, capped at + * DCP_HDCP_RETRY_MAX_MS, reset by a success or a new link generation. + */ +#define DCP_HDCP_RETRY_MS 1000 +#define DCP_HDCP_RETRY_MAX_MS 60000 + +/* + * Message sizes, msg_id byte included, as the public structures define + * them. The register blocks DCP moves are these without the msg_id byte. + */ +static_assert(sizeof(struct hdcp2_ake_init) == 12); +static_assert(sizeof(struct hdcp2_ake_send_cert) == 534); +static_assert(sizeof(struct hdcp2_ake_stored_km) == 33); +static_assert(sizeof(struct hdcp2_ake_send_hprime) == 33); +static_assert(sizeof(struct hdcp2_lc_init) == 9); +static_assert(sizeof(struct hdcp2_lc_send_lprime) == 33); +static_assert(sizeof(struct hdcp2_ske_send_eks) == 25); + +enum dcp_hdcp_state { + DCP_HDCP_OFF, /* userspace does not want protection */ + DCP_HDCP_DESIRED, /* wanted; no session running */ + DCP_HDCP_AUTH, /* a session is running */ + DCP_HDCP_ENABLED, /* encryption on, acknowledged */ +}; + +enum dcp_hdcp_action { + DCP_HDCP_NONE, + DCP_HDCP_TEARDOWN, /* encryption off and close, display untouched */ + DCP_HDCP_KICK, /* ready: the next session DCP offers is taken */ +}; + +struct dcp_hdcp_link { + enum dcp_hdcp_state state; + u8 type; /* the content type asked for: 0 or 1 */ + u32 generation; /* link generation of the running session */ + unsigned long retry_at; + unsigned int failures; + bool hold; /* the SEP was busy: wait until retry_at */ + bool relay; /* the DCP relay is bound */ + bool sep; /* the SEP service is up */ +}; + +/* + * The property exists on an external connector whose display processor has + * an HDCP channel, with the SEP service built in. Whether a session can run + * is decided at runtime: until then the property stays DESIRED. + */ +static inline bool dcp_hdcp_attach_property(bool external, bool channel, bool sep) +{ + return external && channel && sep; +} + +static inline u64 dcp_hdcp_property(const struct dcp_hdcp_link *l) +{ + switch (l->state) { + case DCP_HDCP_OFF: + return DRM_MODE_CONTENT_PROTECTION_UNDESIRED; + case DCP_HDCP_ENABLED: + return DRM_MODE_CONTENT_PROTECTION_ENABLED; + default: + return DRM_MODE_CONTENT_PROTECTION_DESIRED; + } +} + +static inline unsigned long dcp_hdcp_backoff(unsigned int failures) +{ + unsigned long ms = DCP_HDCP_RETRY_MS; + + while (failures-- > 1 && ms < DCP_HDCP_RETRY_MAX_MS) + ms *= 2; + return msecs_to_jiffies(min_t(unsigned long, ms, DCP_HDCP_RETRY_MAX_MS)); +} + +/* A session may start now. */ +static inline bool dcp_hdcp_ready(const struct dcp_hdcp_link *l, + unsigned long now) +{ + return l->state == DCP_HDCP_DESIRED && l->relay && l->sep && + !((l->failures || l->hold) && time_before(now, l->retry_at)); +} + +/* Drops a session; teardown is owed if one was running. */ +static inline enum dcp_hdcp_action dcp_hdcp_drop(struct dcp_hdcp_link *l, + bool failed, unsigned long now) +{ + bool running = l->state == DCP_HDCP_AUTH || l->state == DCP_HDCP_ENABLED; + + if (l->state != DCP_HDCP_OFF) + l->state = DCP_HDCP_DESIRED; + if (failed) { + l->failures++; + l->retry_at = now + dcp_hdcp_backoff(l->failures); + } + return running ? DCP_HDCP_TEARDOWN : DCP_HDCP_NONE; +} + +/* + * Userspace set the property: @want is DESIRED (true) or UNDESIRED, @type + * the content type. + */ +static inline enum dcp_hdcp_action dcp_hdcp_request(struct dcp_hdcp_link *l, + bool want, u8 type, + unsigned long now) +{ + if (!want) { + enum dcp_hdcp_action a = dcp_hdcp_drop(l, false, now); + + l->state = DCP_HDCP_OFF; + return a; + } + if (l->state == DCP_HDCP_OFF) + l->state = DCP_HDCP_DESIRED; + if (type != l->type) { + l->type = type; + /* A running session protects the old type: start again. */ + if (dcp_hdcp_drop(l, false, now) == DCP_HDCP_TEARDOWN) + return DCP_HDCP_TEARDOWN; + } + return dcp_hdcp_ready(l, now) ? DCP_HDCP_KICK : DCP_HDCP_NONE; +} + +/* DCP opened a session on link generation @gen: true if the AP takes it. */ +static inline bool dcp_hdcp_open(struct dcp_hdcp_link *l, u32 gen, + unsigned long now) +{ + if (!dcp_hdcp_ready(l, now)) + return false; + l->state = DCP_HDCP_AUTH; + l->generation = gen; + return true; +} + +/* + * The session on generation @gen ended its authentication with @result: 0, + * -EAGAIN when the SEP was not available for now (retried after the first + * backoff step, not counted as a failure), or another error. + */ +static inline enum dcp_hdcp_action dcp_hdcp_auth_done(struct dcp_hdcp_link *l, + u32 gen, int result, + unsigned long now) +{ + /* A late result, from a session or generation that is gone. */ + if (l->state != DCP_HDCP_AUTH || gen != l->generation) + return DCP_HDCP_NONE; + if (result == -EAGAIN) { + l->hold = true; + l->retry_at = now + dcp_hdcp_backoff(1); + return dcp_hdcp_drop(l, false, now); + } + if (result) + return dcp_hdcp_drop(l, true, now); + l->state = DCP_HDCP_ENABLED; + l->failures = 0; + l->hold = false; + return DCP_HDCP_NONE; +} + +/* + * DCP closed the session, or the receiver asked for re-authentication: a + * session that had not finished failed; an enabled one is redone at once. + */ +static inline enum dcp_hdcp_action dcp_hdcp_closed(struct dcp_hdcp_link *l, + u32 gen, unsigned long now) +{ + if (gen != l->generation) + return DCP_HDCP_NONE; + if (l->state == DCP_HDCP_AUTH) + return dcp_hdcp_drop(l, true, now); + return dcp_hdcp_drop(l, false, now); +} + +/* The link retrained, went away or came back: it is now generation @gen. */ +static inline enum dcp_hdcp_action dcp_hdcp_link_event(struct dcp_hdcp_link *l, + u32 gen, unsigned long now) +{ + if (gen == l->generation) + return DCP_HDCP_NONE; + l->failures = 0; + l->hold = false; + l->generation = gen; + return dcp_hdcp_drop(l, false, now); +} + +/* The SEP service or the DCP relay came or went. */ +static inline enum dcp_hdcp_action dcp_hdcp_bind(struct dcp_hdcp_link *l, + bool relay, bool sep, + unsigned long now) +{ + l->relay = relay; + l->sep = sep; + if (relay && sep) + return dcp_hdcp_ready(l, now) ? DCP_HDCP_KICK : DCP_HDCP_NONE; + return dcp_hdcp_drop(l, false, now); +} + +#endif /* __APPLE_DCP_HDCP_H__ */ diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index fd561bc7358cc..b3ba6616b8a72 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -1313,6 +1313,8 @@ struct apple_dcp { /* Internal panel identity; NULL without an internal panel. */ struct dcp_panel_identity *panel_identity; struct dcp_cec *cec; + /* HDCP 2.2 transmitter state, NULL without HDCP (dcp-hdcp.c). */ + struct dcp_hdcp *hdcp; }; /* A DCP driven through the firmware's iBoot interface alone. */ diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index bd867d04bb072..968fb42db22d2 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -72,6 +72,7 @@ #include #include "dcp-fabric-effects.h" #include "dcp-fabric.h" +#include "dcp-hdcp-drv.h" #include "dcp-hdmi.h" #include "dcp-syslog.h" #include "dcpext_scanout.h" @@ -1650,6 +1651,9 @@ static struct apple_dcp_afkep *dcp_afkep(struct apple_dcp *dcp, u8 endpoint) if (ep) return ep; } + /* The DP device endpoint HDCP started. */ + if (dcp_hdcp_route(dcp, endpoint)) + return dcp_hdcp_route(dcp, endpoint); /* DPAV 0x2a on H17P overlaps the legacy DPTX endpoint number. */ if (dcp->dcpavservep && endpoint == dcp->dcpavservep->endpoint) return dcp->dcpavservep; @@ -1669,6 +1673,12 @@ static struct apple_dcp_afkep *dcp_afkep(struct apple_dcp *dcp, u8 endpoint) } } +/* Whether something already drives @endpoint (HDCP starts only idle ones). */ +bool dcp_endpoint_routed(struct apple_dcp *dcp, u8 endpoint) +{ + return !!dcp_afkep(dcp, endpoint); +} + /* RX and the watchdog may race to finish one present, never both. */ bool dcp_desktop_present_complete(struct apple_dcp *dcp, u32 id) { @@ -4913,6 +4923,9 @@ int dcp_start(struct platform_device *pdev) } + /* Before any endpoint announces its services: they are matched by name. */ + dcp_hdcp_init(dcp); + /* Even if nothing starts, dcp_wait_ready() must find it initialised. */ init_completion(&dcp->start_done); init_completion(&dcp->s2r_dart_done); @@ -5064,6 +5077,7 @@ int dcp_start(struct platform_device *pdev) dcp_cec_register(dcp); } #endif + dcp_hdcp_start_endpoints(dcp); return ret; } @@ -5344,6 +5358,9 @@ void dcp_poweron(struct platform_device *pdev) unsigned long remaining; int ret; + /* A new link: DCP opens a new HDCP session once it is up. */ + dcp_hdcp_link_changed(dcp); + /* The takeover trial powered it on before the eviction. */ if (dcp->fw_compat == DCP_FIRMWARE_V_26_6_DESKTOP && dcp->desktop->trial_powered) { @@ -5527,6 +5544,9 @@ void dcp_poweroff(struct platform_device *pdev) bool display_gone = false; int ret; + /* DPMS off ends any HDCP session before the pipe goes down; the next power-on starts another. */ + dcp_hdcp_quiesce(dcp); + if (iomfb_is_m4(dcp)) { ret = dcp_backlight_dpms(dcp, false); if (!ret) @@ -7460,6 +7480,7 @@ VISIBLE_IF_KUNIT void dcp_comp_unbind(struct device *dev, struct device *main, v if (!dcp) return; + dcp_hdcp_fini(dcp); if (IS_ENABLED(CONFIG_APPLE_SOC_SLEEP)) apple_sleep_v27_unregister(dev); dcp_indicator_release(dcp); @@ -9913,6 +9934,9 @@ static int dcp_s2r_suspend(struct apple_dcp *dcp) bool was_off; int ret; + /* Encryption off and the HDCP session ended while DCP still runs. */ + dcp_hdcp_quiesce(dcp); + /* * Not bound into the display subsystem (a component unbind released the RTKit * session): nothing of the firmware is this driver's to stop. diff --git a/drivers/gpu/drm/apple/iomfb.c b/drivers/gpu/drm/apple/iomfb.c index cbc093e132fc8..a8c09cd8378cc 100644 --- a/drivers/gpu/drm/apple/iomfb.c +++ b/drivers/gpu/drm/apple/iomfb.c @@ -41,6 +41,7 @@ #include "cec.h" #include "dcp.h" #include "dcp-hdr-metadata.h" +#include "dcp-hdcp-drv.h" #include "dcp-internal.h" #include "iomfb.h" #include "iomfb_internal.h" @@ -1992,6 +1993,9 @@ void dcp_hotplug(struct work_struct *work) connector->connected, READ_ONCE(dcp->mode_state.valid), dcp->nr_modes); if (READ_ONCE(dcp->mode_state.valid)) dcp->tunnel_power_cycles = 0; + /* An unplugged sink ends its HDCP session; a new link brings another. */ + if (!READ_ONCE(connector->connected)) + dcp_hdcp_link_changed(dcp); scoped_guard(mutex, &connector->base.dev->mode_config.mutex) { if (READ_ONCE(connector->dcp) == pdev)