[PATCH v1 1/2] PCI: PM: Avoid leaving devices in D0-uninitialized in pci_power_up()
From: Rafael J. Wysocki
Date: Mon Apr 04 2022 - 11:42:46 EST
From: Rafael J. Wysocki <rafael.j.wysocki@xxxxxxxxx>
In theory, pci_power_up() may leave a device in D0-uninitialized
during a transition from D3cold to D0.
Say, a PCIe device depending on some ACPI power resources is put into
D3cold, so the power resources in question are all turned off. Then,
pci_power_up() is called to put it into D0.
It first calls pci_platform_power_transition() which invokes
platform_pci_set_power_state() to turn on the ACPI power resources
depended on by the device and, if that is successful, it calls
pci_update_current_state() to update the current_state field of
the PCI device object. If the device's configuration space is
accessible at this point, which is the case if
platform_pci_set_power_state() leaves it in D0-uninitialized (and
there's nothing to prevent it from doing so), current_state will be
set to PCI_D0 and the pci_raw_set_power_state() called subsequently
will notice that the device is in D0 already and do nothing.
However, that is not correct, because it may be still necessary to
restore the device's BARs at this point.
To address this issue, set current_state temporarily to PCI_D3hot
in the cases in which pci_raw_set_power_state() may need to do more
than just changing the power state of the device.
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@xxxxxxxxx>
---
drivers/pci/pci.c | 10 ++++++++++
1 file changed, 10 insertions(+)
Index: linux-pm/drivers/pci/pci.c
===================================================================
--- linux-pm.orig/drivers/pci/pci.c
+++ linux-pm/drivers/pci/pci.c
@@ -1309,6 +1309,8 @@ static int pci_dev_wait(struct pci_dev *
*/
int pci_power_up(struct pci_dev *dev)
{
+ pci_power_t old_state = dev->current_state;
+
pci_platform_power_transition(dev, PCI_D0);
/*
@@ -1325,6 +1327,14 @@ int pci_power_up(struct pci_dev *dev)
pci_resume_bus(dev->subordinate);
}
+ /*
+ * For transitions from D3hot or deeper and initial power-up, force
+ * PCI_PM_CTRL register write, D3*->D0 transition delay and BARS
+ * restoration.
+ */
+ if (old_state >= PCI_D3hot)
+ dev->current_state = PCI_D3hot;
+
return pci_raw_set_power_state(dev, PCI_D0);
}