Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions drivers/gpu/drm/apple/dcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down
91 changes: 91 additions & 0 deletions drivers/gpu/drm/apple/dcp_s2r_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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),
{}
};

Expand Down
13 changes: 9 additions & 4 deletions tools/asahi/dcpext26-lifecycle-tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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];
Expand All @@ -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(used<sizeof(trace)-1);trace[used++]=c;trace[used]=0; }
static int result(char c) { note(c);return c==fault?-EIO:0; }
Expand All @@ -91,8 +98,6 @@ def body(file, name):
static void av_service_disconnect(struct apple_dcp *d) { (void)d;note('A'); }
static int system_dfl_wq;
static void mod_delayed_work(int q,int *w,int t) { (void)q;(void)w;(void)t;note('D'); }
struct completion { unsigned signaled; };
static void complete_all(struct completion *c) { c->signaled++; }
#define DCPEXT26_SUBMIT_BYTES 0x1bd8
struct ext26_control { struct dcpext26_exchange exchange; struct completion done; u8 wire[DCPEXT26_SUBMIT_BYTES]; };
'''
Expand Down Expand Up @@ -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+'(')
Expand Down