diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 0666748f436d0..04e7d08211d07 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -2308,6 +2308,37 @@ static unsigned long dcp_dptx_wait_linkcfg(struct apple_dcp *dcp, u32 port, } } +/* + * A fixed HDMI port can come up from the boot loader with a default surface + * already active, in which case the firmware does not start fresh link + * training while AP HPD is withheld. Assert AP HPD from the converter's real + * HPD GPIO before waiting for link configuration, except on paths where HPD + * has another owner (such as 13.5 DP2HDMI, where the converter holds HPD on + * its own and linkcfg timeout is non-fatal, or an adopted session whose + * post-wait path drives HPD). + */ +static int dcp_assert_hpd_before_link_wait(struct apple_dcp *dcp, u32 port, + bool late_hpd, bool skip_fixed_hdmi) +{ + bool typec = dcp_is_typec_output(dcp); + bool fixed_hdmi = !typec && !skip_fixed_hdmi && + dcp->fixed_connector_type == DRM_MODE_CONNECTOR_HDMIA && + dcp->hdmi_hpd; + int level; + + if (late_hpd || (!typec && !fixed_hdmi)) + return 0; + if (fixed_hdmi) { + level = gpiod_get_value_cansleep(dcp->hdmi_hpd); + if (level <= 0) + return level < 0 ? level : -ENOLINK; + } + if (dcp_uses_t6020_tunnel_flow(dcp)) + return dptxport_set_hpd_timeout(dcp->dptxport[port].service, + true, 8000); + return dptxport_set_hpd(dcp->dptxport[port].service, true); +} + /* * Connect DPTX port @port. On failure, @retryable (if given) tells whether * the attempt left nothing outstanding in the firmware, so that a later @@ -2460,19 +2491,17 @@ static int dcp_dptx_connect_raw(struct apple_dcp *dcp, u32 port, bool *retryable goto out_release; } dcp->dptxport[port].connected = true; - if (dcp_is_typec_output(dcp) && !late_hpd) { - if (dcp_uses_t6020_tunnel_flow(dcp)) - ret = dptxport_set_hpd_timeout(dcp->dptxport[port].service, - true, 8000); + ret = dcp_assert_hpd_before_link_wait(dcp, port, late_hpd, dp2hdmi); + if (ret) { + if (ret == -ENOLINK) + dev_dbg(dcp->dev, + "dcp_dptx_connect: HDMI HPD dropped before assert\n"); else - ret = dptxport_set_hpd(dcp->dptxport[port].service, true); - if (ret) { dev_err(dcp->dev, - "dcp_dptx_connect: failed to assert Type-C HPD: %d\n", + "dcp_dptx_connect: failed to assert AP HPD: %d\n", ret); - dcp->dptxport[port].connected = false; - goto out_release; - } + dcp->dptxport[port].connected = false; + goto out_release; } mutex_unlock(&dcp->hpd_mutex); diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp-early-hpd.py b/tools/testing/selftests/drivers/gpu/apple-dcp-early-hpd.py new file mode 100755 index 0000000000000..577d6cb04c757 --- /dev/null +++ b/tools/testing/selftests/drivers/gpu/apple-dcp-early-hpd.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Compile the real early-HPD helper with GPIO/RemotePort leaves modelled. + +No kernel or hardware executes. Tests that an inherited HDMI link can receive +its training trigger before the waiter, plus detach/read/RPC failure behavior +and the existing Type-C, tunnel and late-HPD paths. +""" +import os +from pathlib import Path +import subprocess +import tempfile + +root = Path(__file__).resolve().parents[5] +source = Path(os.environ.get('DCP_SOURCE', root / 'drivers/gpu/drm/apple/dcp.c')).read_text() +a = source.index('static int dcp_assert_hpd_before_link_wait(') +b = source.index('\n}\n', a) + 3 +body = source[a:b] +prefix = r''' +#include +#include +#include +#include +#include +typedef uint32_t u32; +#define DRM_MODE_CONNECTOR_HDMIA 11 +struct apple_dcp { + int fixed_connector_type; void *hdmi_hpd; + bool typec, tunnel; struct {void *service;} dptxport[2]; +}; +static int level=1, rpc_error, samples, calls, timeout_ms; +static bool link_configured; +static bool dcp_is_typec_output(struct apple_dcp*d){return d->typec;} +static bool dcp_uses_t6020_tunnel_flow(struct apple_dcp*d){return d->tunnel;} +static int gpiod_get_value_cansleep(void*p){assert(p);samples++;return level;} +static int dptxport_set_hpd(void*p,bool high) +{assert(p==(void*)123&&high);calls++;if(!rpc_error)link_configured=true;return rpc_error;} +static int dptxport_set_hpd_timeout(void*p,bool high,unsigned ms) +{timeout_ms=ms;return dptxport_set_hpd(p,high);} +''' +cases = r''' +int main(void) +{ + struct apple_dcp d={.fixed_connector_type=DRM_MODE_CONNECTOR_HDMIA,.hdmi_hpd=(void*)1, + .dptxport={{(void*)123},{0}}}; + /* Model the observed inherited-display dependency: without AP HPD the + * request is accepted but the link waiter receives no training event. */ + assert(!link_configured);assert(!dcp_assert_hpd_before_link_wait(&d,0,false,false)); + assert(link_configured&&samples==1&&calls==1&&!timeout_ms); + calls=samples=0;link_configured=false;level=0; + assert(dcp_assert_hpd_before_link_wait(&d,0,false,false)==-ENOLINK); + assert(samples==1&&!calls&&!link_configured); + level=-EIO;samples=0; + assert(dcp_assert_hpd_before_link_wait(&d,0,false,false)==-EIO); + assert(samples==1&&!calls); + level=1;rpc_error=-ETIMEDOUT; + assert(dcp_assert_hpd_before_link_wait(&d,0,false,false)==-ETIMEDOUT); + assert(!link_configured);rpc_error=0; + calls=samples=0; + assert(!dcp_assert_hpd_before_link_wait(&d,0,true,false));assert(!calls&&!samples); + /* 13.5 DP2HDMI and adopted sessions keep their own HPD owner. */ + assert(!dcp_assert_hpd_before_link_wait(&d,0,false,true));assert(!calls&&!samples); + d.fixed_connector_type=0; + assert(!dcp_assert_hpd_before_link_wait(&d,0,false,false));assert(!calls&&!samples); + d.fixed_connector_type=DRM_MODE_CONNECTOR_HDMIA; + /* A borrowed Type-C route on an HDMI-capable pipe must not sample HDMI. */ + d.typec=true;level=0;calls=samples=0; + assert(!dcp_assert_hpd_before_link_wait(&d,0,false,true));assert(calls==1&&!samples); + d.tunnel=true;calls=timeout_ms=0; + assert(!dcp_assert_hpd_before_link_wait(&d,0,false,false));assert(calls==1&&timeout_ms==8000&&!samples); + d.typec=d.tunnel=false;d.hdmi_hpd=0;calls=0; + assert(!dcp_assert_hpd_before_link_wait(&d,0,false,false));assert(!calls&&!samples); + puts("early AP HPD: inherited HDMI trigger, errors, late-HPD, Type-C and tunnel regressions pass"); +} +''' +# Check real call ordering/error unwind in the production caller as well. +start = source.index('static int dcp_dptx_connect_raw(') +caller = source[start:source.index('\n}\n', start)] +request = caller.index('dptxport_request_display(') +hpd = caller.index('dcp_assert_hpd_before_link_wait(') +wait = caller.index('dcp_dptx_wait_linkcfg(') +assert request < hpd < wait +assert 'goto out_release;' in caller[hpd:wait] +with tempfile.TemporaryDirectory(prefix='dcp-hpd-') as tmp: + p=Path(tmp);(p/'test.c').write_text(prefix+body+cases) + for mode,flags in [('normal',[]),('sanitizer',['-fsanitize=address,undefined','-fno-sanitize-recover=all'])]: + exe=p/mode + subprocess.run([os.environ.get('CC','cc'),'-std=gnu11','-O1','-g','-Wall','-Wextra','-Werror',*flags,str(p/'test.c'),'-o',str(exe)],check=True) + subprocess.run([exe],check=True) diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py b/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py index e41e10447eb22..732d675dc4986 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/lifecycle.py @@ -795,6 +795,15 @@ def swap(old, new): 'static bool dcp_tb_tunnel_withdrawn(struct apple_dcp *d, u32 port)\n' '{ (void)d; (void)port; assert(!"tunnel watch in an H17P scenario"); return false; }\n' + apple_function(root, 'dcp_dptx_wait_linkcfg')) + if 'dcp_assert_hpd_before_link_wait(' in raw: + # The HPD assertion before the link wait, as the driver has it. This scenario is a + # Type-C H17P link, so the fixed-HDMI branch is not taken. + swap(' bool hdmi_hpd;', + ' int fixed_connector_type; void *hdmi_hpd;') + firmware += ('static int __attribute__((unused)) gpiod_get_value_cansleep(void *gpio)\n' + '{ (void)gpio; assert(!"fixed HDMI GPIO in a Type-C scenario"); return 0; }\n' + '#ifndef DRM_MODE_CONNECTOR_HDMIA\n#define DRM_MODE_CONNECTOR_HDMIA 11\n#endif\n' + + apple_function(root, 'dcp_assert_hpd_before_link_wait')) # unless the update work already needed the clock clock = 'static unsigned jiffies_to_msecs(unsigned long t) { return t; }\n' if clock in unit: