diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index a4d13c775b0ff..36ce6839bda95 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -3668,6 +3668,18 @@ int dcp_dptx_connect(struct apple_dcp *dcp, u32 port) #define DCP_EXTERNAL_STOP_RETRIES 3 +/* Withdraw the DPTX link for an external stop; stubbed in S2R KUnit tests. */ +static int dcp_external_withdraw_dptx_locked(struct apple_dcp *dcp) +{ + int ret; + + KUNIT_STATIC_STUB_REDIRECT(dcp_external_withdraw_dptx_locked, dcp); + ret = dptxport_set_hpd(dcp->dptxport[0].service, false); + if (!ret) + ret = dptxport_release_display(dcp->dptxport[0].service); + return ret; +} + /* The generation token belongs to the CRTC that was actually enabled. */ static int dcp_external_stop_locked(struct apple_dcp *dcp, u64 generation) { @@ -3736,9 +3748,23 @@ static int dcp_external_stop_locked(struct apple_dcp *dcp, u64 generation) ret = -ENODEV; goto failed; } - ret = dptxport_set_hpd(dcp->dptxport[0].service, false); - if (!ret) - ret = dptxport_release_display(dcp->dptxport[0].service); + /* + * Non-retained Type-C pipes that keep DPTX up across + * DPMS off (dcp_typec_keep_dptx_on_dpms()) release the + * link only here for deep sleep. Prepare the same + * handoff to dcp_poweron() that dcp_poweroff() does on + * other Type-C pipes: reset typec_iomfb_hpd_ready so + * resume waits for IOMFB's new VideoInterface/HPD + * before powering on and setting the mode, and mark the + * CRTC off before lowering HPD so the synthetic unplug + * does not drop the still-attached connector. + */ + if (!dcp_is_retained_external(dcp) && dcp_is_typec_output(dcp)) { + reinit_completion(&dcp->typec_iomfb_hpd_ready); + if (READ_ONCE(dcp->typec_cable_connected)) + WRITE_ONCE(dcp->typec_crtc_off, true); + } + ret = dcp_external_withdraw_dptx_locked(dcp); if (ret) goto failed; dcp->dptxport[0].connected = false; diff --git a/drivers/gpu/drm/apple/dcp_s2r_test.c b/drivers/gpu/drm/apple/dcp_s2r_test.c index 0be73a814f063..02dc4694722a1 100644 --- a/drivers/gpu/drm/apple/dcp_s2r_test.c +++ b/drivers/gpu/drm/apple/dcp_s2r_test.c @@ -13,6 +13,7 @@ enum dcp_s2r_test_op { DCP_S2R_TEST_EXT_RTKIT_CYCLED, DCP_S2R_TEST_EXT_RTKIT_SLEEP, DCP_S2R_TEST_WARM_RTKIT_BOOT, + DCP_S2R_TEST_EXT_WITHDRAW_DPTX, }; struct dcp_s2r_test_ctx { @@ -25,6 +26,8 @@ struct dcp_s2r_test_ctx { u32 dcp_streams; u32 disp_streams; unsigned int nops; + bool withdraw_saw_hpd_done; + bool withdraw_saw_crtc_off; }; static struct dcp_s2r_test_ctx *dcp_s2r_test_current(struct kunit **out_test) @@ -124,6 +127,18 @@ static int dcp_s2r_test_warm_rtkit_boot(struct apple_dcp *dcp) return 0; } +static int dcp_s2r_test_ext_withdraw_dptx(struct apple_dcp *dcp) +{ + struct kunit *test; + struct dcp_s2r_test_ctx *ctx = dcp_s2r_test_current(&test); + + KUNIT_EXPECT_PTR_EQ(test, dcp, ctx->dcp); + ctx->withdraw_saw_hpd_done = completion_done(&dcp->typec_iomfb_hpd_ready); + ctx->withdraw_saw_crtc_off = READ_ONCE(dcp->typec_crtc_off); + dcp_s2r_test_push(ctx, DCP_S2R_TEST_EXT_WITHDRAW_DPTX); + return 0; +} + static struct dcp_s2r_test_ctx *dcp_s2r_test_setup(struct kunit *test, const struct apple_dcp_hw_data *hw) { @@ -155,6 +170,8 @@ static struct dcp_s2r_test_ctx *dcp_s2r_test_setup(struct kunit *test, dcp_s2r_test_display_streams); kunit_activate_static_stub(test, dcp_s2r_ext_rtkit_wake, dcp_s2r_test_ext_rtkit_wake); kunit_activate_static_stub(test, dcp_s2r_warm_rtkit_boot, dcp_s2r_test_warm_rtkit_boot); + kunit_activate_static_stub(test, dcp_external_withdraw_dptx_locked, + dcp_s2r_test_ext_withdraw_dptx); return ctx; } @@ -220,10 +237,84 @@ static void dcp_s2r_t8103_warm_restart_wake_order(struct kunit *test) KUNIT_EXPECT_EQ(test, ctx->disp_streams, dcp_s2r_t8103.disp_streams); } +/* + * T8142 DCPExt (V27 Type-C): DPMS power-off keeps DPTX up, so deep-sleep + * dcp_external_stop_locked() is what drops the link. It must reinitialize + * typec_iomfb_hpd_ready and mark typec_crtc_off before lowering HPD so + * resume waits for IOMFB's fresh VideoInterface/HPD before powering on and + * setting the mode, and the synthetic unplug does not drop the connector. + */ +static void dcp_s2r_t8142_dcpext_stop_rearms_hpd_ready(struct kunit *test) +{ + struct dcp_s2r_test_ctx *ctx = dcp_s2r_test_setup(test, &apple_dcp_hw_t8142_dcpext); + struct apple_dcp *dcp = ctx->dcp; + + mutex_init(&dcp->external_lock); + mutex_init(&dcp->hpd_mutex); + mutex_init(&dcp->iomfb.lock); + init_completion(&dcp->typec_iomfb_hpd_ready); + complete_all(&dcp->typec_iomfb_hpd_ready); + + dcp->fw_compat = DCP_FIRMWARE_V_27_0; + dcp->fixed_connector_type = DRM_MODE_CONNECTOR_USB; + dcp->typec_cable_connected = true; + dcp->dptxport[0].enabled = true; + dcp->dptxport[0].connected = true; + + scoped_guard(mutex, &dcp->external_lock) + KUNIT_ASSERT_EQ(test, dcp_external_stop_locked(dcp, dcp->external_generation), 0); + + KUNIT_EXPECT_EQ(test, ctx->nops, 1U); + KUNIT_EXPECT_EQ(test, ctx->ops[0], DCP_S2R_TEST_EXT_WITHDRAW_DPTX); + KUNIT_EXPECT_FALSE(test, ctx->withdraw_saw_hpd_done); + KUNIT_EXPECT_TRUE(test, ctx->withdraw_saw_crtc_off); + KUNIT_EXPECT_FALSE(test, completion_done(&dcp->typec_iomfb_hpd_ready)); + KUNIT_EXPECT_FALSE(test, dcp->dptxport[0].connected); + KUNIT_EXPECT_TRUE(test, dcp->external_stopped); + KUNIT_EXPECT_TRUE(test, dcp->typec_crtc_off); +} + +/* + * Retained external H17P pipes handle unplugs through dcp_external_hotplug() + * while presentation is paused; link withdrawal must not pre-set + * typec_crtc_off or disturb typec_iomfb_hpd_ready. + */ +static void dcp_s2r_t8140_retained_ext_stop_unchanged(struct kunit *test) +{ + struct dcp_s2r_test_ctx *ctx = dcp_s2r_test_setup(test, &apple_dcp_hw_t8140_external); + struct apple_dcp *dcp = ctx->dcp; + + mutex_init(&dcp->external_lock); + mutex_init(&dcp->hpd_mutex); + mutex_init(&dcp->iomfb.lock); + init_completion(&dcp->typec_iomfb_hpd_ready); + complete_all(&dcp->typec_iomfb_hpd_ready); + + dcp->fw_compat = DCP_FIRMWARE_H17P; + dcp->fixed_connector_type = DRM_MODE_CONNECTOR_USB; + dcp->typec_cable_connected = true; + dcp->dptxport[0].enabled = true; + dcp->dptxport[0].connected = true; + + scoped_guard(mutex, &dcp->external_lock) + KUNIT_ASSERT_EQ(test, dcp_external_stop_locked(dcp, dcp->external_generation), 0); + + KUNIT_EXPECT_EQ(test, ctx->nops, 1U); + KUNIT_EXPECT_EQ(test, ctx->ops[0], DCP_S2R_TEST_EXT_WITHDRAW_DPTX); + KUNIT_EXPECT_TRUE(test, ctx->withdraw_saw_hpd_done); + KUNIT_EXPECT_FALSE(test, ctx->withdraw_saw_crtc_off); + KUNIT_EXPECT_TRUE(test, completion_done(&dcp->typec_iomfb_hpd_ready)); + KUNIT_EXPECT_FALSE(test, dcp->dptxport[0].connected); + KUNIT_EXPECT_TRUE(test, dcp->external_stopped); + KUNIT_EXPECT_TRUE(test, dcp->typec_crtc_off); +} + static struct kunit_case dcp_s2r_cases[] = { KUNIT_CASE(dcp_s2r_t8103_dcpext_wake_order), KUNIT_CASE(dcp_s2r_t8140_dcpext_wake_order), KUNIT_CASE(dcp_s2r_t8103_warm_restart_wake_order), + KUNIT_CASE(dcp_s2r_t8142_dcpext_stop_rearms_hpd_ready), + KUNIT_CASE(dcp_s2r_t8140_retained_ext_stop_unchanged), {} }; diff --git a/tools/asahi/dcpext26-lifecycle-tests.py b/tools/asahi/dcpext26-lifecycle-tests.py index 4335d19d14646..8eae34a4762d2 100644 --- a/tools/asahi/dcpext26-lifecycle-tests.py +++ b/tools/asahi/dcpext26-lifecycle-tests.py @@ -28,6 +28,7 @@ def body(file, name): typedef uint32_t u32; typedef uint64_t u64; typedef uint8_t u8; #define READ_ONCE(x) (x) #define WRITE_ONCE(x,y) ((x)=(y)) +#define KUNIT_STATIC_STUB_REDIRECT(...) ((void)0) #define lockdep_assert_held(x) ((void)0) #define ARRAY_SIZE(x) (sizeof(x)/sizeof(*(x))) #define IS_ERR_OR_NULL(x) (!(x)) @@ -56,11 +57,15 @@ def body(file, name): struct drm_framebuffer { struct drm_gem_object *obj[1]; }; struct iomfb_scanout_h17p { struct drm_framebuffer *fb[4]; struct { struct iomfb_scanout_h17p *next; } link; }; #define list_for_each_entry(pos,head,member) for ((pos)=(head)->first;(pos);(pos)=(pos)->member.next) +struct completion { unsigned signaled; }; +static void complete_all(struct completion *c) { c->signaled++; } +static void reinit_completion(struct completion *c) { c->signaled = 0; } struct apple_dcp { struct { int lock; } iomfb; struct { struct iomfb_scanout_h17p *first; } ext26_retained; bool external_suspended,ext26_pm_preparing,ext26_pm_work_off;u8 *coproc_reg; u64 external_generation; u32 afk_power_gen; int dev,external_error; bool external_stopped,external_power_attempted,external_running; - bool external_stop_deferred,pipe_enabled_h17p,typec_crtc_off,ext26_normal,ext26_stopping; + bool external_stop_deferred,pipe_enabled_h17p,typec_crtc_off,typec_cable_connected,ext26_normal,ext26_stopping; + struct completion typec_iomfb_hpd_ready; unsigned external_stop_retries; int external_lock,hpd_mutex,external_stop_retry_wq,rtk,avep; struct port dptxport[1]; struct apple_dcp_afkep *ext26_aux[3]; @@ -73,6 +78,8 @@ def body(file, name): static unsigned discoveries; static void dcp_enable_typec_work(struct apple_dcp *d) { (void)d;discoveries++; } static bool dcp_is_ext26(struct apple_dcp *d) { (void)d;return true; } +static bool dcp_is_retained_external(struct apple_dcp *d) { return dcp_is_ext26(d); } +static bool dcp_is_typec_output(struct apple_dcp *d) { (void)d;return true; } static char trace[128]; static unsigned used; static char fault; static unsigned settlements; static void note(char c) { assert(usedsignaled++; } #define DCPEXT26_SUBMIT_BYTES 0x1bd8 struct ext26_control { struct dcpext26_exchange exchange; struct completion done; u8 wire[DCPEXT26_SUBMIT_BYTES]; }; ''' @@ -167,7 +172,7 @@ def body(file, name): body('drivers/gpu/drm/apple/dcp.c','dcp_external_stop_locked')]) + checks # dcp.c has a forward declaration; select the actual definition. s=(ROOT/'drivers/gpu/drm/apple/dcp.c').read_text();pos=s.index('static int dcp_external_stop_locked(struct apple_dcp *dcp, u64 generation)\n{');actual=s[pos:s.index('\n}',pos)+2] -code=code[:code.index('static int dcp_external_stop_locked')] + actual + '\n' + body('drivers/gpu/drm/apple/dcp.c','dcp_external_start_locked') + checks +code=code[:code.index('static int dcp_external_stop_locked')] + body('drivers/gpu/drm/apple/dcp.c','dcp_external_withdraw_dptx_locked') + '\n' + actual + '\n' + body('drivers/gpu/drm/apple/dcp.c','dcp_external_start_locked') + checks # Execute the native branches of the actual PM hooks, preserving their returns. def native_branch(name): pos=s.index('static '+('void' if name.endswith('complete') else 'int')+' '+name+'(')