Skip to content
Open
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
7 changes: 7 additions & 0 deletions drivers/gpu/drm/apple/dcp-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,13 @@ struct apple_dcp {
unsigned int external_phase;
/* Native: bounded recovery of a display link or mode, see dcp.c. */
struct delayed_work external_retry_wq;
/*
* Native: once no display is attached, the PMP request is withdrawn
* (external_pmp_released) until the next attach asks for it again.
*/
struct delayed_work external_idle_wq;
atomic_t external_idle_tries;
bool external_pmp_released;
atomic_t external_retries;
/* typec_generation when the pending retry was queued */
u64 external_retry_generation;
Expand Down
64 changes: 60 additions & 4 deletions drivers/gpu/drm/apple/dcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,18 @@ static int dcp_dptx_connect_raw(struct apple_dcp *dcp, u32 port, bool *retryable
ret = -ESTALE;
goto out_unlock;
}
/*
* Under hpd_mutex: an idle withdraw cannot come between this and the
* link. On failure nothing reached the firmware, so a retry may follow.
*/
if (dcp->external_native) {
ret = iomfb_v14_7_external_pmp_reassert(dcp);
if (ret) {
if (retryable)
*retryable = true;
goto out_unlock;
}
}
generation = dcp->typec_generation;
if (dcp->external && dcpext_scanout_terminal(dcp)) {
mutex_unlock(&dcp->hpd_mutex);
Expand Down Expand Up @@ -2197,6 +2209,7 @@ int dcp_dptx_disconnect(struct apple_dcp *dcp, u32 port)
if (!ret)
WRITE_ONCE(dcp->dptxport[port].tile_hint, false);
mutex_unlock(&dcp->hpd_mutex);
dcp_external_idle_schedule(dcp);

return ret;
}
Expand Down Expand Up @@ -4933,18 +4946,59 @@ static const char *dcp_external_sleep_vetoed(struct apple_dcp *dcp)
why = iomfb_v14_7_external_busy(dcp);
if (why)
return why;
if (iomfb_v14_7_external_failed(dcp)) {
if (dcp->v14)
dev_info(dcp->dev, "external display processor failed earlier: sleeping with it as it is\n");
if (iomfb_v14_7_external_failed(dcp))
return NULL;
}
if (READ_ONCE(dcp->active_typec_route) || READ_ONCE(dcp->typec_cable_connected))
return "a USB-C display is attached";
if (dcp->hdmi_hpd && gpiod_get_value_cansleep(dcp->hdmi_hpd) > 0)
return "an HDMI display is attached";
return NULL;
}

/* For system sleep, which dcp_external_sleep_vetoed() let through. */
static void dcp_external_sleep_note(struct apple_dcp *dcp)
{
if (dcp->external_native && dcp->v14 && iomfb_v14_7_external_failed(dcp))
dev_info(dcp->dev, "external display processor failed earlier: sleeping with it as it is\n");
}

#define DCP_EXT_IDLE_DELAY_MS 5000
#define DCP_EXT_IDLE_TRIES 12

/*
* A native external processor whose display went away keeps running, but
* its PMP request keeps the PMP's external display state up, about 0.25 W of
* idle power on J514S. Once nothing is attached, by the same test system sleep
* uses before it withdraws that request, withdraw it until the next attach.
* A display still being taken down (powered, its link up) is checked again,
* for about a minute.
*/
void dcp_external_idle_work(struct work_struct *work)
{
struct apple_dcp *dcp = container_of(to_delayed_work(work), struct apple_dcp,
external_idle_wq);
bool idle;

scoped_guard(mutex, &dcp->hpd_mutex) {
idle = !READ_ONCE(dcp->external_suspended) && !dcp_external_sleep_vetoed(dcp);
if (idle)
iomfb_v14_7_external_pmp_release(dcp);
}
if (!idle && atomic_inc_return(&dcp->external_idle_tries) < DCP_EXT_IDLE_TRIES)
queue_delayed_work(system_freezable_wq, &dcp->external_idle_wq,
msecs_to_jiffies(DCP_EXT_IDLE_DELAY_MS));
}

void dcp_external_idle_schedule(struct apple_dcp *dcp)
{
/* Set up by iomfb_v14_7_external_prepare(), not on every native path. */
if (!dcp->external_native || !dcp->external_idle_wq.work.func)
return;
atomic_set(&dcp->external_idle_tries, 0);
mod_delayed_work(system_freezable_wq, &dcp->external_idle_wq,
msecs_to_jiffies(DCP_EXT_IDLE_DELAY_MS));
}

/* dpm_prepare completes for every device before any dpm_suspend callback.
* Veto early so Thunderbolt cannot tear down a working tunnel first. The
* same HPD lock serializes this gate against explicit firmware startup.
Expand All @@ -4966,6 +5020,7 @@ static int dcp_platform_prepare(struct device *dev)
dev_warn(dev, "external firmware/scanout attempted: refusing PM prepare; reboot required for retained DMA\n");
return -EBUSY;
}
dcp_external_sleep_note(dcp);
WRITE_ONCE(dcp->external_suspended, true);
mutex_unlock(&dcp->hpd_mutex);
return 0;
Expand Down Expand Up @@ -5007,6 +5062,7 @@ static int dcp_platform_suspend(struct device *dev)
dev_warn(dev, "external firmware/scanout attempted: suspend refused; retained DMA requires reboot\n");
return -EBUSY;
}
dcp_external_sleep_note(dcp);
WRITE_ONCE(dcp->external_suspended, true);
mutex_unlock(&dcp->hpd_mutex);
}
Expand Down
2 changes: 2 additions & 0 deletions drivers/gpu/drm/apple/dcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ void dcp_external_ready(struct apple_dcp *dcp);
void dcp_external_retry(struct apple_dcp *dcp, const char *why, int error,
unsigned int base_ms);
void dcp_external_retry_work(struct work_struct *work);
void dcp_external_idle_work(struct work_struct *work);
void dcp_external_idle_schedule(struct apple_dcp *dcp);
void dcp_external_sink_irq(struct apple_dcp *dcp);
void dcp_queue_typec_reconnect(struct apple_dcp *dcp, unsigned long delay);
int dcp_dptx_recover_irq(struct apple_dcp *dcp);
Expand Down
103 changes: 101 additions & 2 deletions drivers/gpu/drm/apple/iomfb_v14_7.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
#define DCP_V14_CPU_CONTROL 0x44
#define DCP_V14_CPU_STATUS 0x48
#define DCP_V14_PMP_TIMEOUT msecs_to_jiffies(30000)
/*
* A running PMP acknowledges a request asserted again at once; the attach
* holds hpd_mutex (and on HDMI the fabric lock) while it waits.
*/
#define DCP_V14_PMP_REASSERT_TIMEOUT msecs_to_jiffies(500)

/*
* The PMP report's wait-ready entry point is optional at link time: until a
Expand All @@ -77,6 +82,34 @@ static int dcp_v14_pmp_wait_ready(struct device_node *entry,
symbol_put(apple_pmp_report_wait_ready);
return ret;
}

/* Withdraw the request of an external processor with no display attached. */
static int dcp_v14_pmp_release(struct device_node *entry)
{
typeof(&apple_pmp_report_idle) release;
int ret;

release = symbol_get(apple_pmp_report_idle);
if (!release)
return -ENODEV;
ret = release(entry);
symbol_put(apple_pmp_report_idle);
return ret;
}

/* As dcp_v14_pmp_wait_ready(), asserting a request withdrawn while unused. */
static int dcp_v14_pmp_use(struct device_node *entry, unsigned long timeout)
{
typeof(&apple_pmp_report_use) use;
int ret;

use = symbol_get(apple_pmp_report_use);
if (!use)
return dcp_v14_pmp_wait_ready(entry, timeout);
ret = use(entry, timeout);
symbol_put(apple_pmp_report_use);
return ret;
}
#define DCP_V14_RTKIT_RETRIES 4
#define DCP_V14_MAX_PROPERTIES 256
#define DCP_V14_MAX_RAW 32
Expand Down Expand Up @@ -831,8 +864,10 @@ void dcp_v14_external_hotplug(struct apple_dcp *dcp, bool connected)
atomic_set(&dcp->external_held, 0);
if (connected && dcp_is_typec_output(dcp))
complete_all(&dcp->typec_iomfb_hpd_ready);
if (!connected)
if (!connected) {
WRITE_ONCE(dcp->ext_backlight, false);
dcp_external_idle_schedule(dcp);
}
if (!connector)
return;
apple_connector_edid_set_live(connector, connected);
Expand Down Expand Up @@ -2028,6 +2063,7 @@ static void dcpext_cancel(void *data)
if (dcp->external_native) {
cancel_work_sync(&dcp->external_ready_work);
cancel_delayed_work_sync(&dcp->external_retry_wq);
disable_delayed_work_sync(&dcp->external_idle_wq);
}
}

Expand Down Expand Up @@ -2081,6 +2117,7 @@ int iomfb_v14_7_external_prepare(struct apple_dcp *dcp)
INIT_WORK(&dcp->external_work, dcpext_bringup);
INIT_WORK(&dcp->external_ready_work, dcpext_ready_work);
INIT_DELAYED_WORK(&dcp->external_retry_wq, dcp_external_retry_work);
INIT_DELAYED_WORK(&dcp->external_idle_wq, dcp_external_idle_work);
/* Component bind enables it, once a connector can be retrained. */
disable_delayed_work(&dcp->external_retry_wq);
ret = devm_add_action_or_reset(dev, dcpext_cancel, dcp);
Expand Down Expand Up @@ -2121,6 +2158,61 @@ void iomfb_v14_7_external_refuse(struct apple_dcp *dcp, const char *why)
dev_err(dcp->dev, "external display processor unusable: %s\n", why);
}

/*
* A running processor whose display went away withdrew its PMP request
* (dcp_external_idle_work()). Assert it again, and wait for the PMP to
* acknowledge it, before the next display's link. Called with hpd_mutex
* held up to the link, so the idle withdraw cannot slip in between.
*/
int iomfb_v14_7_external_pmp_reassert(struct apple_dcp *dcp)
{
struct device_node *entry;
int ret;

lockdep_assert_held(&dcp->hpd_mutex);
/* Pairs with dcpext_bringup(): RUNNING is published after the endpoints. */
if (smp_load_acquire(&dcp->external_phase) != DCPEXT_RUNNING ||
!READ_ONCE(dcp->external_pmp_released))
return 0;
entry = of_parse_phandle(dcp->dev->of_node, "apple,pmp-report", 0);
if (!entry)
return -ENODEV;
ret = dcp_v14_pmp_use(entry, DCP_V14_PMP_REASSERT_TIMEOUT);
of_node_put(entry);
if (ret) {
dev_warn(dcp->dev, "PMP request not asserted for the attach: %d\n", ret);
return ret;
}
WRITE_ONCE(dcp->external_pmp_released, false);
dev_info(dcp->dev, "display attached: PMP request asserted again\n");
return 0;
}

/* Under hpd_mutex, with no display attached: see dcp_external_idle_work(). */
void iomfb_v14_7_external_pmp_release(struct apple_dcp *dcp)
{
struct device_node *entry;
int ret;

lockdep_assert_held(&dcp->hpd_mutex);
/* Pairs with dcpext_bringup(): RUNNING is published after the endpoints. */
if (smp_load_acquire(&dcp->external_phase) != DCPEXT_RUNNING ||
READ_ONCE(dcp->external_pmp_released))
return;
entry = of_parse_phandle(dcp->dev->of_node, "apple,pmp-report", 0);
if (!entry)
return;
ret = dcp_v14_pmp_release(entry);
of_node_put(entry);
if (ret) {
dev_warn(dcp->dev, "no display attached, but the PMP request was not withdrawn: %d\n",
ret);
return;
}
WRITE_ONCE(dcp->external_pmp_released, true);
dev_info(dcp->dev, "no display attached: PMP request withdrawn until the next attach\n");
}

/* True once DPTX can be used; asks for the start if nothing has. */
bool iomfb_v14_7_external_ready(struct apple_dcp *dcp)
{
Expand Down Expand Up @@ -2219,7 +2311,8 @@ static int dcpext_check_power(struct apple_dcp *dcp)
entry = of_parse_phandle(dev->of_node, "apple,pmp-report", 0);
if (!entry)
return -ENODEV;
ret = dcp_v14_pmp_wait_ready(entry, DCP_V14_PMP_TIMEOUT);
/* The request is withdrawn until this first start (see the gate). */
ret = dcp_v14_pmp_use(entry, DCP_V14_PMP_TIMEOUT);
of_node_put(entry);
return ret;
}
Expand Down Expand Up @@ -2383,6 +2476,12 @@ static void dcpext_bringup(struct work_struct *work)
}
dev_info(dcp->dev, "external display processor running\n");
dcp_external_ready(dcp);
/*
* The attach that started it may have been routed to the other
* processor (a Thunderbolt display goes to dpin0): then no display
* ever disconnects from this one, so check now whether it is in use.
*/
dcp_external_idle_schedule(dcp);
}

/*
Expand Down
4 changes: 4 additions & 0 deletions drivers/gpu/drm/apple/iomfb_v14_7.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ int iomfb_v14_7_external_open(struct apple_dcp *dcp);
const char *iomfb_v14_7_external_busy(struct apple_dcp *dcp);
/* The external session can no longer be used until reboot. */
bool iomfb_v14_7_external_failed(struct apple_dcp *dcp);
/* Withdraw a running processor's PMP request; hpd_mutex held, none attached. */
void iomfb_v14_7_external_pmp_release(struct apple_dcp *dcp);
/* Assert it again before an attach's link; hpd_mutex held. */
int iomfb_v14_7_external_pmp_reassert(struct apple_dcp *dcp);
/* The attached display is powered (A472 on) in the mode last set. */
bool iomfb_v14_7_external_powered(struct apple_dcp *dcp);
/* The firmware runs this mode, set last, and shows swaps in it. */
Expand Down
Loading