diff --git a/drivers/pci/controller/dwc/pcie-gs201-cp.c b/drivers/pci/controller/dwc/pcie-gs201-cp.c index 1d1cacce4f..7ed4650b2c 100644 --- a/drivers/pci/controller/dwc/pcie-gs201-cp.c +++ b/drivers/pci/controller/dwc/pcie-gs201-cp.c @@ -99,6 +99,8 @@ #include #include #include +#include +#include #include #include #include @@ -245,12 +247,16 @@ static const char * const cp_input_names[CP_NUM_INPUTS] = { * @host_atu: no secure-ATU SMC; the host programs the outbound iATU * @sysreg_optional: without samsung,sysreg the link runs without I/O * coherency + * @sleep_guard: system sleep with the modem up has been tried on this SoC + * (gs201): the noirq sleep callbacks run, and the root port is kept out + * of the PCI core's D3 handling; elsewhere neither happens */ struct gs201_pcie_cp_data { bool zuma_ctrl; bool phy_bypass_once; bool host_atu; bool sysreg_optional; + bool sleep_guard; }; struct gs201_pcie_cp { @@ -276,6 +282,7 @@ struct gs201_pcie_cp { struct pci_host_bridge *bridge; struct resource msi_page; /* memory-region: the modem's MSI page */ struct dentry *debugfs; + struct notifier_block pm_nb; /* sleep_guard: the early refusal */ struct mutex lock; /* serializes the steps */ raw_spinlock_t cfg_lock; /* config accesses against link changes */ bool cfg_root_ok; /* root port (DBI) accessible */ @@ -757,6 +764,13 @@ static int cp_msi_setup(struct gs201_pcie_cp *cp) return 0; } +static int cp_ep_sleep_setup(struct pci_dev *pdev, void *data) +{ + pdev->d3cold_delay = 0; + dev_pm_syscore_device(&pdev->dev, true); + return 0; +} + static int cp_bus_report(struct pci_dev *pdev, void *data) { struct gs201_pcie_cp *cp = data; @@ -804,6 +818,7 @@ static void cp_root_win_restore(struct gs201_pcie_cp *cp) static int cp_bus(struct gs201_pcie_cp *cp) { + struct pci_dev *rp; int ret; if (!cp->link_up || !cp->atu_done) @@ -822,6 +837,45 @@ static int cp_bus(struct gs201_pcie_cp *cp) ret = pci_host_probe(cp->bridge); if (ret) return ret; + /* + * The link is down between transfers, and with it the root port's + * configuration space. Its power state is the modem driver's call: + * left to the PCI core, a system sleep with the link down marked the + * root port D3cold, and resume then took the modem as disconnected, + * since the link only comes back when the modem driver asks for it. + * PCI_DEV_FLAGS_NO_D3 keeps it in D0, but a read of its PM control + * register with the link down (pci_power_up() at resume, through + * cp_root_map_bus(), which maps nothing then, so the read is all + * ones) still marks it D3cold, and the next resume would wait up to + * d3cold_delay for a link only the modem driver raises, then mark the + * modem disconnected. The modem keeps its power across s2idle, and + * its driver waits for its own link up and restores the endpoint + * before any access, so that wait has nothing to wait for: keep + * d3cold_delay zero below the port. The modem itself is left out of + * system sleep altogether (a syscore device for the PM core): its + * driver keeps the modem's power and PCI state, and the PCI core's + * own suspend and resume of it (disable and save, then the power-up + * read, restore and re-enable) ran behind that driver's back when a + * sleep was aborted, after which the driver could not reach the modem. + * The root port is left out of system sleep the same way: once a + * sleep with the link down had marked it D3cold, the next sleep, a + * refused one included, ran the PCI core's whole resume of it on a + * live link (power-up, config restore, PME restore, then a resume of + * the bus below), and with the link down its suspend only saved and + * read back all ones. Its windows are this driver's, restored at + * every link up (cp_root_win_restore()). + * All of this only where system sleep with the modem up has been + * tried (sleep_guard, gs201). + */ + if (cp->data->sleep_guard) { + list_for_each_entry(rp, &cp->bridge->bus->devices, bus_list) { + rp->dev_flags |= PCI_DEV_FLAGS_NO_D3; + dev_pm_syscore_device(&rp->dev, true); + if (rp->subordinate) + pci_walk_bus(rp->subordinate, + cp_ep_sleep_setup, NULL); + } + } pci_walk_bus(cp->bridge->bus, cp_bus_report, cp); cp_root_win_save(cp); @@ -2131,6 +2185,39 @@ static int cp_get_sysreg(struct gs201_pcie_cp *cp) return 0; } +/* + * The same check before anything is frozen or suspended (sleep_guard): a + * sleep refused here costs one notifier call, where the noirq refusal + * (gs201_pcie_cp_suspend_noirq()) comes after every task was frozen and + * every device suspended, and resumes them all again. Stock gets the + * early refusal from CPIF's wake lock, held while the link is up; that + * lock holds a sleep off only for a caller that wrote + * /sys/power/wakeup_count (Android's autosleep), which systemd doesn't. + * The noirq check stays for a link that comes up while the system is + * being frozen. + */ +static int gs201_pcie_cp_pm_notify(struct notifier_block *nb, + unsigned long event, void *unused) +{ + struct gs201_pcie_cp *cp = container_of(nb, struct gs201_pcie_cp, + pm_nb); + int ret = 0; + + if (event != PM_SUSPEND_PREPARE) + return NOTIFY_DONE; + + mutex_lock(&cp->lock); + if (cp->link_up || cp->dbi_ok) { + dev_info(cp->dev, "sleep refused before the freeze: link %s, PHY %s\n", + cp->link_up ? "up" : "down", + cp->dbi_ok ? "out of isolation" : "isolated"); + ret = -EBUSY; + } + mutex_unlock(&cp->lock); + + return notifier_from_errno(ret); +} + static int gs201_pcie_cp_probe(struct platform_device *pdev) { struct device *dev = &pdev->dev; @@ -2263,6 +2350,15 @@ static int gs201_pcie_cp_probe(struct platform_device *pdev) gs201_cp = cp; mutex_unlock(&gs201_cp_lock); + if (IS_ENABLED(CONFIG_PM_SLEEP) && cp->data->sleep_guard) { + cp->pm_nb.notifier_call = gs201_pcie_cp_pm_notify; + ret = register_pm_notifier(&cp->pm_nb); + if (ret) { + cp->pm_nb.notifier_call = NULL; + dev_warn(dev, "no early sleep refusal: %d\n", ret); + } + } + dev_info(dev, "modem PCIe ch0 bring-up: %u lanes, steps %s, modem lines %s\n", cp->num_lanes, allow_hw ? "enabled" : "disabled (allow_hw=0)", cp->has_lines ? "here" : "left to the modem driver"); @@ -2274,6 +2370,8 @@ static void gs201_pcie_cp_remove(struct platform_device *pdev) { struct gs201_pcie_cp *cp = platform_get_drvdata(pdev); + if (cp->pm_nb.notifier_call) + unregister_pm_notifier(&cp->pm_nb); mutex_lock(&gs201_cp_lock); gs201_cp = NULL; mutex_unlock(&gs201_cp_lock); @@ -2298,12 +2396,73 @@ static void gs201_pcie_cp_remove(struct platform_device *pdev) cancel_work_sync(&cp->event_work); } +/* + * System sleep (s2idle) with the modem up, on gs201 only (sleep_guard). + * The modem stays powered; its driver lets the AP sleep only once + * CP2AP_WAKEUP has dropped and the idle link has been powered down (cpif + * holds a wake lock while the link is up, and its suspend refuses while + * the line is high), as on stock, whose root complex does no link-down at + * suspend either and only isolates a link that is already down, as + * link_down does here. Should those paths ever race, this refuses the + * sleep. It is a late abort, not a guard: the root port and the modem are + * children of this device, but both are syscore devices (see cp_bus()), + * so the PM core leaves them alone; the refusal fails the sleep with the + * modem driver's state of the link and the modem untouched. Most refusals + * come earlier, from gs201_pcie_cp_pm_notify(). The PMU's isolation bit + * is only logged: a phone whose modem never started has never isolated + * the PHY, and must still sleep. + */ +static int gs201_pcie_cp_suspend_noirq(struct device *dev) +{ + struct gs201_pcie_cp *cp = dev_get_drvdata(dev); + unsigned int iso = 0; + int ret = 0; + + if (!cp->data->sleep_guard) + return 0; + if (!mutex_trylock(&cp->lock)) { + dev_info(dev, "sleep refused: a link step is running\n"); + return -EBUSY; + } + if (cp->link_up || cp->dbi_ok) { + dev_info(dev, "sleep refused: link %s, PHY %s\n", + cp->link_up ? "up" : "down", + cp->dbi_ok ? "out of isolation" : "isolated"); + ret = -EBUSY; + } + mutex_unlock(&cp->lock); + if (ret) + return ret; + + if (regmap_read(cp->pmu, cp->pmu_offset, &iso)) + dev_dbg(dev, "sleep: link down\n"); + else + dev_dbg(dev, "sleep: link down, PMU %#x = %#x\n", + cp->pmu_offset, iso); + return 0; +} + +static int gs201_pcie_cp_resume_noirq(struct device *dev) +{ + struct gs201_pcie_cp *cp = dev_get_drvdata(dev); + + if (cp->data->sleep_guard) + dev_dbg(dev, "resume: link down\n"); + return 0; +} + +static const struct dev_pm_ops gs201_pcie_cp_pm_ops = { + NOIRQ_SYSTEM_SLEEP_PM_OPS(gs201_pcie_cp_suspend_noirq, + gs201_pcie_cp_resume_noirq) +}; + static void gs201_pcie_cp_shutdown(struct platform_device *pdev) { gs201_pcie_cp_remove(pdev); } static const struct gs201_pcie_cp_data gs201_pcie_cp_data = { + .sleep_guard = true, }; static const struct gs201_pcie_cp_data zumapro_pcie_cp_data = { @@ -2327,6 +2486,7 @@ static struct platform_driver gs201_pcie_cp_driver = { .driver = { .name = "gs201-pcie-cp", .of_match_table = gs201_pcie_cp_of_match, + .pm = pm_sleep_ptr(&gs201_pcie_cp_pm_ops), .suppress_bind_attrs = true, /* the modem driver uses it */ }, };