diff --git a/drivers/gpu/drm/apple/dcp-internal.h b/drivers/gpu/drm/apple/dcp-internal.h index 3eac22a57aa069..81fe7bdeb635ef 100644 --- a/drivers/gpu/drm/apple/dcp-internal.h +++ b/drivers/gpu/drm/apple/dcp-internal.h @@ -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; diff --git a/drivers/gpu/drm/apple/dcp.c b/drivers/gpu/drm/apple/dcp.c index 8cb040d16d552c..5ee3ccd6f53897 100644 --- a/drivers/gpu/drm/apple/dcp.c +++ b/drivers/gpu/drm/apple/dcp.c @@ -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); @@ -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; } @@ -4933,11 +4946,8 @@ 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) @@ -4945,6 +4955,50 @@ static const char *dcp_external_sleep_vetoed(struct apple_dcp *dcp) 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. @@ -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; @@ -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); } diff --git a/drivers/gpu/drm/apple/dcp.h b/drivers/gpu/drm/apple/dcp.h index baff04ee201e72..e1886ea1877ae2 100644 --- a/drivers/gpu/drm/apple/dcp.h +++ b/drivers/gpu/drm/apple/dcp.h @@ -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); diff --git a/drivers/gpu/drm/apple/iomfb_v14_7.c b/drivers/gpu/drm/apple/iomfb_v14_7.c index 7a6c96dfa1f74f..798d916c6e3abe 100644 --- a/drivers/gpu/drm/apple/iomfb_v14_7.c +++ b/drivers/gpu/drm/apple/iomfb_v14_7.c @@ -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 @@ -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 @@ -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); @@ -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); } } @@ -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); @@ -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) { @@ -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; } @@ -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); } /* diff --git a/drivers/gpu/drm/apple/iomfb_v14_7.h b/drivers/gpu/drm/apple/iomfb_v14_7.h index 7eb6ada2b1c26c..528bec7e36ccba 100644 --- a/drivers/gpu/drm/apple/iomfb_v14_7.h +++ b/drivers/gpu/drm/apple/iomfb_v14_7.h @@ -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. */ diff --git a/drivers/pmdomain/apple/pmp-report.c b/drivers/pmdomain/apple/pmp-report.c index adda6374407e01..53e758dac34749 100644 --- a/drivers/pmdomain/apple/pmp-report.c +++ b/drivers/pmdomain/apple/pmp-report.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -405,6 +406,19 @@ struct apple_pmp_start { u64 startup_ack; /* Seeded bits that are soc-device record indices. */ u64 record_seed; + /* + * Seeded requests withdrawn once the PMP has started, until their + * consumer first asks for them (apple,release-until-used), and those + * still withdrawn. + */ + u64 idle_release; + u64 released; + /* + * Requests apple_pmp_report_use() has written and awaits the ack of, + * without apple_pmp_start_mutex. They stay in "released" until the + * ack is in; meanwhile idle() and another use() of them get -EBUSY. + */ + u64 asserting; struct of_changeset pmp_cs; const struct apple_pmp_start_ops *ops; const struct apple_pmp_start_timing *timing; @@ -419,6 +433,16 @@ struct apple_pmp_start { struct completion waiters_done; }; +static int apple_pmp_report_set_state(struct apple_pmp_report *rep, u32 id, + bool enable, bool no_ack); +static int apple_pmp_report_request(struct apple_pmp_report *rep, u32 id, + bool enable, unsigned long timeout); +static bool apple_pmp_report_acked(struct apple_pmp_report *rep, u64 bit, + bool enable); + +/* How long a withdrawn request may take to be acknowledged. */ +#define APPLE_PMP_IDLE_ACK_TIMEOUT msecs_to_jiffies(100) + /* Reports that start their PMP; serializes startup requests and waiters. */ static LIST_HEAD(apple_pmp_starts); static DEFINE_MUTEX(apple_pmp_start_mutex); @@ -673,6 +697,8 @@ static int apple_pmp_start_describe(struct apple_pmp_report *rep, if (record) st->record_seed |= BIT_ULL(id); st->seed |= BIT_ULL(id); + if (of_property_read_bool(child, "apple,release-until-used")) + st->idle_release |= BIT_ULL(id); if (!of_property_read_bool(child, "apple,no-ack")) { st->ack |= BIT_ULL(id); if (!of_property_read_bool(child, "apple,defer-startup-ack")) @@ -692,11 +718,45 @@ static int apple_pmp_start_describe(struct apple_pmp_report *rep, return 0; } +/* + * A request the PMP took at its start, but whose consumer is not using it + * yet: an external display processor with no display ever attached. Holding + * it keeps the PMP's external display state up, about 0.25 W of idle power + * on J514S; macOS keeps an unused processor's floor off. Withdraw it until + * apple_pmp_report_use() asks for it again. The request is written before + * the acknowledgment is awaited, so it is recorded as withdrawn even when the + * acknowledgment does not come: the first use then asserts it again, whether + * or not the PMP still shows it acknowledged. + */ +static void apple_pmp_start_release_idle(struct apple_pmp_start *st) +{ + unsigned long bits = st->idle_release; + unsigned int id; + + lockdep_assert_held(&apple_pmp_start_mutex); + for_each_set_bit(id, &bits, 64) { + int ret = apple_pmp_report_request(st->rep, id, false, + APPLE_PMP_IDLE_ACK_TIMEOUT); + + st->released |= BIT_ULL(id); + if (ret) + dev_warn(st->rep->dev, + "request %u withdrawn while unused, not acknowledged: %d\n", + id, ret); + } + if (st->released) + dev_info(st->rep->dev, "withdrew unused requests %#llx until first use\n", + st->released); +} + static void apple_pmp_start_finish(struct apple_pmp_start *st, int result) { scoped_guard(mutex, &apple_pmp_start_mutex) { - if (!st->stopping) + if (!st->stopping) { st->result = result; + if (!result) + apple_pmp_start_release_idle(st); + } complete_all(&st->started); } if (result && READ_ONCE(st->enabled)) { @@ -1106,6 +1166,159 @@ int apple_pmp_report_wait_ready(struct device_node *entry, unsigned long timeout } EXPORT_SYMBOL_GPL(apple_pmp_report_wait_ready); +/** + * apple_pmp_report_use() - ask for a request and wait for its acknowledgment + * @entry: report entry node, as for apple_pmp_report_wait_ready() + * @timeout: in jiffies, for both waits together + * + * As apple_pmp_report_wait_ready(), but a request that was withdrawn after + * the PMP started because nothing used it yet (apple,release-until-used) is + * asserted again first, and its acknowledgment awaited for the rest of + * @timeout, without apple_pmp_start_mutex held. A request withdrawn while + * unused is asserted again even if the PMP still shows it acknowledged: its + * withdraw may have been acknowledged late. If the new acknowledgment does + * not come in time, the request is withdrawn again and stays marked so, and + * the next call writes it and waits again: the request, its record here and + * the caller's view agree either way. The request stays marked withdrawn + * until it is acknowledged. + * + * Context: Process context; sleeps. + * Return: as apple_pmp_report_wait_ready(); -EIO, with nothing written, if + * the PMP is not running; -EBUSY if another call is asserting it; + * -ETIMEDOUT if a request asserted again was not acknowledged in time; + * -ENODEV if the report went away meanwhile. A request asserted again is + * withdrawn on every error. + */ +int apple_pmp_report_use(struct device_node *entry, unsigned long timeout) +{ + unsigned long deadline = jiffies + timeout; + struct apple_pmp_start *st = NULL, *it; + struct device_node *parent; + bool record, acked; + ktime_t start; + u64 status; + u32 id; + int ret; + + ret = apple_pmp_report_wait_ready(entry, timeout); + /* + * -EIO: the PMP runs, but has not acknowledged this request. 0 can still + * mean a withdrawn request whose withdraw was acknowledged late. + */ + if (ret && ret != -EIO) + return ret; + + parent = of_get_parent(entry); + scoped_guard(mutex, &apple_pmp_start_mutex) { + list_for_each_entry(it, &apple_pmp_starts, list) { + if (it->stopping || it->rep->dev->of_node != parent || + apple_pmp_report_bit(it, entry, &id, &record) || + !(it->released & BIT_ULL(id))) + continue; + if (it->asserting & BIT_ULL(id)) { + ret = -EBUSY; + break; + } + /* No acknowledgment can come: fail now, and leave it withdrawn. */ + status = readq(it->rep->base + it->rep->offsets->status); + if (!(status & PMP_REPORT_READY)) { + ret = -EIO; + break; + } + /* A waiter keeps the report until the acknowledgment is in. */ + apple_pmp_report_set_state(it->rep, id, true, true); + it->asserting |= BIT_ULL(id); + it->waiters++; + st = it; + break; + } + } + of_node_put(parent); + if (!st) + return ret; + + start = ktime_get(); + ret = read_poll_timeout(apple_pmp_report_acked, acked, acked, 1000, + jiffies_to_usecs(max_t(long, deadline - jiffies, 1)), + false, st->rep, BIT_ULL(id), true); + + scoped_guard(mutex, &apple_pmp_start_mutex) { + st->asserting &= ~BIT_ULL(id); + /* The waiter keeps the report mapped while it is torn down. */ + if (st->stopping) + ret = -ENODEV; + if (ret) { + /* Roll back, so that "released" still means withdrawn. */ + apple_pmp_report_set_state(st->rep, id, false, true); + dev_warn(st->rep->dev, + "request %u asserted for use, not acknowledged: %d; withdrawn again\n", + id, ret); + } else { + st->released &= ~BIT_ULL(id); + dev_info(st->rep->dev, "request %u asserted for use, acknowledged in %lld us\n", + id, ktime_us_delta(ktime_get(), start)); + } + if (!--st->waiters && st->stopping) + complete(&st->waiters_done); + } + return ret; +} +EXPORT_SYMBOL_GPL(apple_pmp_report_use); + +/** + * apple_pmp_report_idle() - withdraw a request while its consumer is idle + * @entry: report entry node of a request marked apple,release-until-used + * + * The consumer asks for it again with apple_pmp_report_use(). An external + * display processor withdraws its request once no display is attached; its + * firmware keeps running, as it does when the request is withdrawn for + * system sleep. + * + * The request is recorded as withdrawn once it is written; an acknowledgment + * that does not come in time is logged, and the next use asserts it again. + * One recorded as withdrawn already is withdrawn in the hardware too. + * + * Context: Process context; sleeps. + * Return: 0 once the request is withdrawn, -EBUSY while apple_pmp_report_use() + * is asserting it, or -EINVAL if @entry is not such a request of a started + * PMP. + */ +int apple_pmp_report_idle(struct device_node *entry) +{ + struct device_node *parent = of_get_parent(entry); + struct apple_pmp_start *st; + int ret = -EINVAL; + bool record; + u32 id; + + scoped_guard(mutex, &apple_pmp_start_mutex) { + list_for_each_entry(st, &apple_pmp_starts, list) { + if (st->stopping || st->rep->dev->of_node != parent || + !completion_done(&st->started) || st->result || + apple_pmp_report_bit(st, entry, &id, &record) || + !(st->idle_release & BIT_ULL(id))) + continue; + /* apple_pmp_report_use() is asserting it: leave it to that. */ + if (st->asserting & BIT_ULL(id)) { + ret = -EBUSY; + break; + } + ret = 0; + if (st->released & BIT_ULL(id)) + break; + st->released |= BIT_ULL(id); + if (apple_pmp_report_request(st->rep, id, false, + APPLE_PMP_IDLE_ACK_TIMEOUT)) + dev_warn(st->rep->dev, + "request %u withdrawn while idle, not acknowledged\n", id); + break; + } + } + of_node_put(parent); + return ret; +} +EXPORT_SYMBOL_GPL(apple_pmp_report_idle); + static int apple_pmp_report_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -1323,6 +1536,30 @@ static int apple_pmp_report_set_state(struct apple_pmp_report *rep, u32 id, 50000); } +static bool apple_pmp_report_acked(struct apple_pmp_report *rep, u64 bit, bool enable) +{ + if (!(readq(rep->base + rep->offsets->status) & PMP_REPORT_READY)) + return false; + return !!(readq(rep->base + rep->offsets->actual) & bit) == enable; +} + +/* + * Write a request, then wait up to @timeout for the running PMP to + * acknowledge it. The request stays written if the acknowledgment does not + * come. Unlike apple_pmp_report_set_state(), a PMP that is not running is no + * acknowledgment. + */ +static int apple_pmp_report_request(struct apple_pmp_report *rep, u32 id, + bool enable, unsigned long timeout) +{ + bool acked; + + apple_pmp_report_set_state(rep, id, enable, true); + return read_poll_timeout(apple_pmp_report_acked, acked, acked, 1000, + jiffies_to_usecs(timeout), false, rep, + BIT_ULL(id), enable); +} + /* Find and check the report before taking the GPU vote; optionally update it. */ static int apple_pmp_report_t6030_gfx(struct device *supplier, const bool *enable) { @@ -1419,6 +1656,10 @@ static int apple_pmp_report_entry_sleep(struct apple_pmp_report_entry *ent, bool if (release && (!(readq(rep->base + rep->offsets->status) & PMP_REPORT_READY) || !(readq(rep->base + rep->offsets->actual) & BIT_ULL(ent->id)))) ret = -EALREADY; + /* Withdrawn until used, with its ack late at suspend: leave it so. */ + else if (!release && rep->start && + (READ_ONCE(rep->start->released) & BIT_ULL(ent->id))) + ret = 0; else ret = apple_pmp_report_set_state(rep, ent->id, !release, ent->no_ack); break; diff --git a/drivers/soc/apple/t6030-display-gate.c b/drivers/soc/apple/t6030-display-gate.c index 07ba8b9f60f571..c172d91f029dd9 100644 --- a/drivers/soc/apple/t6030-display-gate.c +++ b/drivers/soc/apple/t6030-display-gate.c @@ -1581,6 +1581,12 @@ static int __init gate_ext_apply(struct gate_ext *ext, bool native) /* No external display is attached in system sleep: the PMP may drop it. */ if (!ret) ret = gate_add_bool(cs, ext->entry, "apple,release-for-sleep"); + /* + * Nor before the first attach: the PMP takes the request at its start + * and withdraws it until the driver starts this processor. + */ + if (!ret) + ret = gate_add_bool(cs, ext->entry, "apple,release-until-used"); if (!ret) ret = of_changeset_update_prop_string(cs, ext->entry, "status", "okay"); if (!ret) diff --git a/include/linux/soc/apple/pmp-report.h b/include/linux/soc/apple/pmp-report.h index 1d0689f31c2039..193b1f2f4df98e 100644 --- a/include/linux/soc/apple/pmp-report.h +++ b/include/linux/soc/apple/pmp-report.h @@ -19,6 +19,8 @@ int apple_pmp_report_wait_supplier_ready(struct device *supplier, unsigned int t int apple_pmp_report_check_t6030_gfx(struct device *supplier); int apple_pmp_report_set_t6030_gfx(struct device *supplier, bool enable); int apple_pmp_report_wait_ready(struct device_node *entry, unsigned long timeout); +int apple_pmp_report_use(struct device_node *entry, unsigned long timeout); +int apple_pmp_report_idle(struct device_node *entry); int apple_pmp_report_fast_die_effort(const struct device_node *np, unsigned int lane, u8 *effort); #else @@ -44,6 +46,16 @@ static inline int apple_pmp_report_wait_ready(struct device_node *entry, return -ENODEV; } +static inline int apple_pmp_report_use(struct device_node *entry, unsigned long timeout) +{ + return -ENODEV; +} + +static inline int apple_pmp_report_idle(struct device_node *entry) +{ + return -ENODEV; +} + static inline int apple_pmp_report_fast_die_effort(const struct device_node *np, unsigned int lane, u8 *effort) { diff --git a/tools/asahi/t6030-pmp-graph-tests.c b/tools/asahi/t6030-pmp-graph-tests.c index d7d079a9673a64..b30c88e47a085f 100644 --- a/tools/asahi/t6030-pmp-graph-tests.c +++ b/tools/asahi/t6030-pmp-graph-tests.c @@ -46,6 +46,7 @@ static void host_log(const char *format, ...) struct device_node { const char *name, *compatible; bool available, always_on, no_ack, deferred, uuid, soc_record, release_sleep; + bool release_idle; u32 reg; int refs; struct device_node *parent, *child, *sibling, *pmp; @@ -136,6 +137,10 @@ struct apple_pmp_start { bool stopping; unsigned int waiters; struct completion waiters_done; + /* apple,release-until-used requests, and those withdrawn now. */ + u64 idle_release; + u64 released; + u64 asserting; }; struct apple_pmp_report_entry { struct generic_pm_domain genpd; @@ -246,6 +251,11 @@ static u8 ranges[64], devices[17 * 0x7c]; static struct work_struct *pending; static unsigned long jiffies; static unsigned int ack_polls; +/* The timeout of the last read_poll_timeout(), in us, and when it began. */ +static unsigned long last_poll_us, last_poll_jiffies; +/* Run once inside the next read_poll_timeout(), as another caller would. */ +static void (*poll_hook)(void); +static unsigned int writes; #define msecs_to_jiffies(v) (v) #define time_before(a, b) ((long)((a) - (b)) < 0) #define time_after(a, b) time_before(b, a) @@ -262,6 +272,7 @@ static void complete_all(struct completion *c) c->done = true; } #define complete complete_all +#define completion_done(c) ((c)->done) #define INIT_WORK(w, f) ((w)->func = (f), (w)->pending = false) #define system_unbound_wq NULL static bool queue_work(void *q, struct work_struct *w) @@ -304,6 +315,7 @@ static unsigned int ack_late; static void writeq(u64 v, void *a) { assert(a == (char *)regs + 0x108c0); + writes++; *(u64 *)a = v; regs[0x1180 / 8] = v; if (ack_follows && ack_late) @@ -327,6 +339,37 @@ static void writeq(u64 v, void *a) r; \ }) #define readq_poll_timeout_atomic readq_poll_timeout +#define read_poll_timeout(op, value, condition, sleep_us, timeout_us, sleep_before, ...) \ + ({ \ + int r = -ETIMEDOUT; \ + void (*hook)(void) = poll_hook; \ + ack_polls++; \ + last_poll_us = (timeout_us); \ + last_poll_jiffies = jiffies; \ + poll_hook = NULL; \ + if (hook) \ + hook(); \ + for (int i = 0; i < 4; i++) { \ + (value) = op(__VA_ARGS__); \ + if (condition) { \ + r = 0; \ + break; \ + } \ + } \ + (void)(sleep_us); \ + (void)(sleep_before); \ + r; \ + }) +#define jiffies_to_usecs(j) ((unsigned int)(j) * 1000) +#define max_t(t, a, b) ((t)(a) > (t)(b) ? (t)(a) : (t)(b)) +#define ktime_t long long +#define ktime_get() 0LL +#define ktime_us_delta(a, b) ((a) - (b)) +#define lockdep_assert_held(l) ((void)(l)) +/* The bits set in *@addr (an unsigned long, so @size is at most its width). */ +#define for_each_set_bit(b, addr, size) \ + for (unsigned long __bits = *(addr) & (~0UL >> (64 - (size))); \ + __bits && ((b) = __builtin_ctzl(__bits), true); __bits &= __bits - 1) static u32 get_unaligned_le32(const void *p) { u32 v; @@ -374,6 +417,8 @@ static bool of_property_read_bool(const struct device_node *n, const char *p) return n->soc_record; if (!strcmp(p, "apple,release-for-sleep")) return n->release_sleep; + if (!strcmp(p, "apple,release-until-used")) + return n->release_idle; return false; } static bool of_property_present(const struct device_node *n, const char *p) @@ -694,10 +739,37 @@ static int g15_vote(void) commands++; return ret; } + +#ifdef HAVE_IDLE_RELEASE +static bool hook_ran; +static void mid_poll(void) +{ + unsigned int w = writes, p = ack_polls; + + assert(!apple_pmp_start_mutex); + assert(apple_pmp_report_idle(&entries[1]) == -EBUSY); + assert(apple_pmp_report_use(&entries[1], 3000) == -EBUSY); + assert(writes == w && ack_polls == p); + hook_ran = true; +} + +/* As apple_pmp_start_drain() would, with this use() still a waiter. */ +static void mid_poll_stop(void) +{ + struct apple_pmp_start *st = ((struct apple_pmp_report *)report_dev.dev.data)->start; + + assert(st->waiters == 1 && !apple_pmp_start_mutex); + st->stopping = true; +} +#endif int main(void) { unsigned int cases = 0; (void)apple_pmp_start_init; + (void)last_poll_us; + (void)last_poll_jiffies; + (void)poll_hook; + (void)writes; (void)apple_pmp_report_wait_t6030_14; setup(); assert(apple_pmp_report_probe(&report_dev) == 0); @@ -810,6 +882,108 @@ int main(void) assert(g15_vote() == -EINVAL && commands == 0 && !enables); cases++; cleanup(); +#ifdef HAVE_IDLE_RELEASE + /* + * apple,release-until-used: the request (R), its acknowledgment (A) and + * the "released" record (S) must agree after every call, also when an + * acknowledgment comes late. The PMP here acknowledges nothing by itself + * (ack_follows false), so each write below is acknowledged late unless + * the case sets the ack bit. + */ + setup(); + entries[1].release_idle = true; + entries[1].release_sleep = true; + hw_ack = BIT_ULL(7) | BIT_ULL(8); + assert(apple_pmp_report_probe(&report_dev) == 0); + assert(apple_pmp_report_wait_ready(&entries[0], 30000) == 0); + struct apple_pmp_start *ist = ((struct apple_pmp_report *)report_dev.dev.data)->start; + + /* Boot withdraw acknowledged late: R=0, A=1, S=1. */ + assert(!(regs[0x1180 / 8] & BIT_ULL(8)) && (regs[0x11c0 / 8] & BIT_ULL(8)) && + (ist->released & BIT_ULL(8))); + /* use() must assert it again although the PMP still shows it acknowledged. */ + assert(apple_pmp_report_use(&entries[1], 3000) == 0); + assert((regs[0x1180 / 8] & BIT_ULL(8)) && !(ist->released & BIT_ULL(8))); + cases++; + /* idle() withdraws it; that ack is late too: R=0, A=1, S=1. */ + assert(apple_pmp_report_idle(&entries[1]) == 0); + assert(!(regs[0x1180 / 8] & BIT_ULL(8)) && (ist->released & BIT_ULL(8))); + cases++; + /* A re-assert never acknowledged is withdrawn again and stays recorded so. */ + regs[0x11c0 / 8] &= ~BIT_ULL(8); + assert(apple_pmp_report_use(&entries[1], 3000) == -ETIMEDOUT); + assert(!(regs[0x1180 / 8] & BIT_ULL(8)) && (ist->released & BIT_ULL(8))); + cases++; + /* idle() on a request recorded withdrawn writes and waits for nothing. */ + unsigned int ipolls = ack_polls; + + assert(apple_pmp_report_idle(&entries[1]) == 0 && ack_polls == ipolls); + cases++; + /* + * A PMP that is not running cannot acknowledge: use() fails at once, + * writes nothing and leaves it recorded withdrawn. + */ + unsigned int w0 = writes; + + regs[0x10 / 8] &= ~1ULL; + assert(apple_pmp_report_use(&entries[1], 30000) == -EIO); + assert(writes == w0 && ack_polls == ipolls && !(regs[0x1180 / 8] & BIT_ULL(8)) && + (ist->released & BIT_ULL(8))); + regs[0x10 / 8] |= 1; + cases++; + /* + * The ack is awaited without apple_pmp_start_mutex, for what is left of + * the caller's timeout; meanwhile idle() and another use() of the same + * request write nothing and get -EBUSY. + */ + poll_hook = mid_poll; + unsigned long j0 = jiffies; + + assert(apple_pmp_report_use(&entries[1], 30000) == -ETIMEDOUT); + assert(hook_ran); + assert(last_poll_jiffies > j0); + assert(last_poll_us == (j0 + 30000 - last_poll_jiffies) * 1000); + assert(!(regs[0x1180 / 8] & BIT_ULL(8)) && (ist->released & BIT_ULL(8))); + assert(!ist->asserting); + assert(!ist->waiters); + cases++; + j0 = jiffies; + assert(apple_pmp_report_use(&entries[1], 100) == -ETIMEDOUT); + assert(last_poll_us == (j0 + 100 - last_poll_jiffies) * 1000); + cases++; + /* A timeout used up by the start wait still polls once: never 0 (no limit). */ + assert(apple_pmp_report_use(&entries[1], 1) == -ETIMEDOUT); + assert(last_poll_us == 1000); + cases++; + /* The next use writes it again and, acknowledged now, clears the record. */ + ack_follows = true; + assert(apple_pmp_report_use(&entries[1], 3000) == 0); + assert((regs[0x1180 / 8] & BIT_ULL(8)) && (regs[0x11c0 / 8] & BIT_ULL(8)) && + !(ist->released & BIT_ULL(8))); + cases++; + /* + * An idle withdraw acknowledged late, then system sleep: suspend still + * sees the ack and withdraws it, and resume must not assert it again. + */ + ack_follows = false; + assert(apple_pmp_report_idle(&entries[1]) == 0); + assert(!(regs[0x1180 / 8] & BIT_ULL(8)) && (regs[0x11c0 / 8] & BIT_ULL(8))); + assert(apple_pmp_report_entry_suspend_noirq(&entry_devs[1].dev) == 0); + assert(apple_pmp_report_entry_resume_noirq(&entry_devs[1].dev) == 0); + assert(!(regs[0x1180 / 8] & BIT_ULL(8)) && (ist->released & BIT_ULL(8))); + cases++; + /* + * The report torn down while use() awaits the ack: its waiter held the + * report, and it withdraws the request again and returns -ENODEV. + */ + poll_hook = mid_poll_stop; + assert(apple_pmp_report_use(&entries[1], 3000) == -ENODEV); + assert(!(regs[0x1180 / 8] & BIT_ULL(8)) && (ist->released & BIT_ULL(8))); + assert(!ist->asserting); + assert(!ist->waiters && ist->waiters_done.done); + cases++; + cleanup(); +#endif #ifdef HAVE_SLEEP_RELEASE /* System sleep withdraws an acknowledged request marked for it, then asserts it again. */ setup(); diff --git a/tools/asahi/t6030-pmp-graph-tests.py b/tools/asahi/t6030-pmp-graph-tests.py index 8757ee04cde1d6..3cc997a009b960 100644 --- a/tools/asahi/t6030-pmp-graph-tests.py +++ b/tools/asahi/t6030-pmp-graph-tests.py @@ -52,6 +52,23 @@ def main(): names[at:at] = ['static int apple_pmp_report_entry_sleep(', 'static int apple_pmp_report_entry_suspend_noirq(', 'static int apple_pmp_report_entry_resume_noirq('] + # Requests withdrawn until used (apple,release-until-used), where the source has them. + prototypes = '' + if 'static void apple_pmp_start_release_idle(' in source: + names.insert(names.index('static void apple_pmp_start_finish('), 'static void apple_pmp_start_release_idle(') + at = names.index('static int apple_pmp_report_set_state(') + 1 + names[at:at] = ['static bool apple_pmp_report_acked(', 'static int apple_pmp_report_request('] + prototypes = (re.search(r'static int apple_pmp_report_request\([^;{]*\);', source)[0] + '\n' + + re.search(r'#define APPLE_PMP_IDLE_ACK_TIMEOUT.*', source)[0] + '\n') + # apple_pmp_report_use() and _idle(), where the source has them. + idle_api = 'int apple_pmp_report_use(' in source + if idle_api: + at = names.index('int apple_pmp_report_wait_ready(') + 1 + names[at:at] = ['int apple_pmp_report_use(', 'int apple_pmp_report_idle('] + m = re.search(r'static int apple_pmp_report_set_state\([^;{]*\);', source) + prototypes += m[0] + '\n' if m else '' + m = re.search(r'static bool apple_pmp_report_acked\([^;{]*\);', source) + prototypes += m[0] + '\n' if m else '' offsets = re.search(r'static const struct apple_pmp_report_offsets apple_pmp_offsets_t6030 = \{.*?\n\};', source, re.S)[0] functions = '\n'.join(extract.function(source, n) for n in names) # The T8122 14.x wait (CONFIG_APPLE_T8122_GPU_PMP_LINK=0 here) precedes the dispatch; it is @@ -61,7 +78,7 @@ def main(): def with_t8122(text): return text.replace(dispatch, t8122 + dispatch, 1) validation = re.sub(r'^#include .*\n', '', (ROOT / 'drivers/pmdomain/apple/pmp-report-validation.h').read_text(), flags=re.M) - code = ('#define HAVE_SLEEP_RELEASE 1\n' if sleep else '') + offsets + '\n' + validation + '\n' + functions + code = ('#define HAVE_SLEEP_RELEASE 1\n' if sleep else '') + ('#define HAVE_IDLE_RELEASE 1\n' if idle_api and sleep else '') + offsets + '\n' + validation + '\n' + prototypes + functions # The actual overlay must still describe a halted supplier and disabled DMA/mailbox. assert overlay.count('status = \"disabled\";') >= 3 (args.output / 'production.h').write_text(with_t8122(code)) @@ -101,6 +118,29 @@ def with_t8122(text): ('sleep-ignores-mark', '!ent->sleep_release ||', ''), ('sleep-not-rewritten', '++tries < 3', '++tries < 1'), ] + if idle_api and sleep: + controls += [ + ('use-skips-late-withdraw', 'if (ret && ret != -EIO)', 'if (ret != -EIO)'), + ('reassert-not-rolled-back', 'apple_pmp_report_set_state(st->rep, id, false, true);', '(void)0;'), + ('reassert-clears-record-early', '/* Roll back, so that "released" still means withdrawn. */', + 'st->released &= ~BIT_ULL(id);'), + ('resume-reasserts-idle', '(READ_ONCE(rep->start->released) & BIT_ULL(ent->id))', '0'), + ('use-writes-unready', 'if (!(status & PMP_REPORT_READY)) {', 'if ((void)status, 0) {'), + ('use-polls-locked', 'start = ktime_get();\n\tret = read_poll_timeout(apple_pmp_report_acked, acked, acked, 1000,\n\t\t\t\tjiffies_to_usecs(max_t(long, deadline - jiffies, 1)),\n\t\t\t\tfalse, st->rep, BIT_ULL(id), true);\n', + 'mutex_lock(&apple_pmp_start_mutex);\n\tstart = ktime_get();\n\tret = read_poll_timeout(apple_pmp_report_acked, acked, acked, 1000,\n\t\t\t\tjiffies_to_usecs(max_t(long, deadline - jiffies, 1)),\n\t\t\t\tfalse, st->rep, BIT_ULL(id), true);\n\tmutex_unlock(&apple_pmp_start_mutex);\n'), + ('use-poll-unclamped', 'max_t(long, deadline - jiffies, 1)', '(deadline - jiffies)'), + ('use-ignores-stop', '\t\tif (st->stopping)\n\t\t\tret = -ENODEV;\n', ''), + ('use-stop-not-rolled-back', '\t\tif (st->stopping)\n\t\t\tret = -ENODEV;\n\t\tif (ret) {', '\t\tif (st->stopping)\n\t\t\tret = -ENODEV;\n\t\telse if (ret) {'), + ('use-no-waiter', '\t\t\tit->waiters++;\n', ''), + ('use-polls-full-timeout', 'jiffies_to_usecs(max_t(long, deadline - jiffies, 1))', '((void)deadline, jiffies_to_usecs(timeout))'), + ('use-not-busy', 'if (it->asserting & BIT_ULL(id)) {', 'if (0) {'), + ('idle-not-busy', 'if (st->asserting & BIT_ULL(id)) {', 'if (0) {'), + ('use-keeps-asserting', 'st->asserting &= ~BIT_ULL(id);', '(void)0;'), + ('use-leaks-waiter', 'ktime_us_delta(ktime_get(), start));\n\t\t}\n\t\tif (!--st->waiters', + 'ktime_us_delta(ktime_get(), start));\n\t\t}\n\t\tif (!st->waiters'), + ('idle-rewrites-withdrawn', 'ret = 0;\n\t\t\tif (st->released & BIT_ULL(id))\n\t\t\t\tbreak;\n', + 'ret = 0;\n'), + ] for name, old, new in controls: assert code.count(old) == 1, (name, code.count(old)) (args.output / 'production.h').write_text(with_t8122(code.replace(old, new))) diff --git a/tools/asahi/t6030-pmp-supplier-tests.py b/tools/asahi/t6030-pmp-supplier-tests.py index 57b4900843cf43..548ddaabf2d389 100755 --- a/tools/asahi/t6030-pmp-supplier-tests.py +++ b/tools/asahi/t6030-pmp-supplier-tests.py @@ -29,7 +29,10 @@ def function(source, signature): + # The definition, not a prototype: its body opens before any ';'. start = source.index(signature) + while source.find(";", start) < source.index("{", start): + start = source.index(signature, start + 1) end = source.index("{", start) + 1 depth = 1 while depth: