Skip to contentSkip to conversation

PCI / PM: Restore the original behavior of pci_set_power_state()

PATCH Applied Rafael J. Wysocki 2 participants 4 messages Jun 28, 2012 – Jul 5, 2012 PCI

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI / PM: Restore the original behavior of pci_set_power_state()

From: Rafael J. Wysocki <[email protected]>

Commit cc2893b6 (PCI: Ensure we re-enable devices on resume) addressed the problem with USB not being powered after resume on recent Lenovo machines, but it did that in a suboptimal way. Namely, it should have changed the relevant code paths only, which are pci_pm_resume_noirq() and pci_pm_restore_noirq() supposed to restore the device's power and standard configuration registers after system resume from suspend or hibernation. Instead, however, it modified pci_set_power_state() which is executed in several other situations too. That resulted in some undesirable effects, like attempting to change a device's power state in the same way multiple times in a row (up to as many as 4 times in a row in the snd_hda_intel driver).

Fix the bug addressed by commit cc2893b6 in an alternative way, by forcibly powering up all devices in pci_pm_default_resume_early(), which is called by pci_pm_resume_noirq() and pci_pm_restore_noirq() to restore the device's power and standard configuration registers, and modifying pci_pm_runtime_resume() to avoid the forcible power-up if not necessary. Then, revert the changes made by commit cc2893b6 to make the confusion introduced by it go away.

---
 drivers/pci/pci-driver.c |   14 ++++++++------
 drivers/pci/pci.c        |   19 ++++++++++++++++++-
 drivers/pci/pci.h        |    1 +
 3 files changed, 27 insertions(+), 7 deletions(-)
drivers/pci/pci.c +18 -1
Index: linux/drivers/pci/pci.c
===================================================================
@@ -662,6 +662,19 @@ void pci_update_current_state(struct pci
 }
 
 /**
+ * pci_power_up - Put the given device into D0 forcibly
+ * @dev: PCI device to power up
+ */
+void pci_power_up(struct pci_dev *dev)
+{
+	if (platform_pci_power_manageable(dev))
+		platform_pci_set_power_state(dev, PCI_D0);
+
+	pci_raw_set_power_state(dev, PCI_D0);
+	pci_update_current_state(dev, PCI_D0);
+}
+
+/**
  * pci_platform_power_transition - Use platform to change device power state
  * @dev: PCI device to handle.
  * @state: State to put the device into.
@@ -707,7 +720,7 @@ static void __pci_start_power_transition
  */
 int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
 {
-	return state >= PCI_D0 ?
+	return state > PCI_D0 ?
 			pci_platform_power_transition(dev, state) : -EINVAL;
 }
 EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
@@ -744,6 +757,10 @@ int pci_set_power_state(struct pci_dev *
 		 */
 		return 0;
 
+	/* Check if we're already there */
+	if (dev->current_state == state)
+		return 0;
+
 	__pci_start_power_transition(dev, state);
 
 	/* This device is quirked not to be put into D3, so
drivers/pci/pci-driver.c +8 -6
Index: linux/drivers/pci/pci-driver.c
===================================================================
@@ -459,16 +459,17 @@ static int pci_restore_standard_config(s
 	return 0;
 }
 
+#endif
+
+#ifdef CONFIG_PM_SLEEP
+
 static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
 {
-	pci_restore_standard_config(pci_dev);
+	pci_power_up(pci_dev);
+	pci_restore_state(pci_dev);
 	pci_fixup_device(pci_fixup_resume_early, pci_dev);
 }
 
-#endif
-
-#ifdef CONFIG_PM_SLEEP
-
 /*
  * Default "suspend" method for devices that have no driver provided suspend,
  * or not even a driver at all (second part).
@@ -1050,7 +1051,8 @@ static int pci_pm_runtime_resume(struct
 	if (!pm || !pm->runtime_resume)
 		return -ENOSYS;
 
-	pci_pm_default_resume_early(pci_dev);
+	pci_restore_standard_config(pci_dev);
+ 	pci_fixup_device(pci_fixup_resume_early, pci_dev);
 	__pci_enable_wake(pci_dev, PCI_D0, true, false);
 	pci_fixup_device(pci_fixup_resume, pci_dev);
 
drivers/pci/pci.h +1 -0
Index: linux/drivers/pci/pci.h
===================================================================
@@ -67,6 +67,7 @@ struct pci_platform_pm_ops {
 
 extern int pci_set_platform_pm(struct pci_platform_pm_ops *ops);
 extern void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
+extern void pci_power_up(struct pci_dev *dev);
 extern void pci_disable_enabled_device(struct pci_dev *dev);
 extern int pci_finish_runtime_suspend(struct pci_dev *dev);
 extern int __pci_pme_wakeup(struct pci_dev *dev, void *ign);
maintainer
#1 lore raw
Quoted 55 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Commit cc2893b6 (PCI: Ensure we re-enable devices on resume) addressed the problem with USB not being powered after resume on recent Lenovo machines, but it did that in a suboptimal way. Namely, it should have changed the relevant code paths only, which are pci_pm_resume_noirq() and pci_pm_restore_noirq() supposed to restore the device's power and standard configuration registers after system resume from suspend or hibernation. Instead, however, it modified pci_set_power_state() which is executed in several other situations too. That resulted in some undesirable effects, like attempting to change a device's power state in the same way multiple times in a row (up to as many as 4 times in a row in the snd_hda_intel driver).

Fix the bug addressed by commit cc2893b6 in an alternative way, by forcibly powering up all devices in pci_pm_default_resume_early(), which is called by pci_pm_resume_noirq() and pci_pm_restore_noirq() to restore the device's power and standard configuration registers, and modifying pci_pm_runtime_resume() to avoid the forcible power-up if not necessary. Then, revert the changes made by commit cc2893b6 to make the confusion introduced by it go away.

---
 drivers/pci/pci-driver.c |   14 ++++++++------
 drivers/pci/pci.c        |   19 ++++++++++++++++++-
 drivers/pci/pci.h        |    1 +
 3 files changed, 27 insertions(+), 7 deletions(-)

Index: linux/drivers/pci/pci.c
===================================================================

drivers/pci/pci.c +13 -0
@@ -662,6 +662,19 @@ void pci_update_current_state(struct pci
 }
 
 /**
+ * pci_power_up - Put the given device into D0 forcibly
+ * @dev: PCI device to power up
+ */
+void pci_power_up(struct pci_dev *dev)
+{
+       if (platform_pci_power_manageable(dev))
+               platform_pci_set_power_state(dev, PCI_D0);
+
+       pci_raw_set_power_state(dev, PCI_D0);
+       pci_update_current_state(dev, PCI_D0);
+}
+
+/**
  * pci_platform_power_transition - Use platform to change device power state
  * @dev: PCI device to handle.
  * @state: State to put the device into.

drivers/pci/pci.c static void __pci_start_power_transition · line 724

Quoted 6 lines
drivers/pci/pci.c +1 -1
@@ -707,7 +720,7 @@ static void __pci_start_power_transition
  */
 int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
 {
-       return state >= PCI_D0 ?
+       return state > PCI_D0 ?
                        pci_platform_power_transition(dev, state) : -EINVAL;

This hunk conflicted with Huang's D3_COLD patches. I fixed it up; can you check and make sure I did it correctly?

I pushed the resolution to here:
git://git.kernel.org/pub/scm/linux/kernel/git/helgaas/pci.git on the pci/rafael-pci_set_power_state-rebase branch.

Bjorn

Quoted 62 lines
drivers/pci/pci.c +4 -0
 }
 EXPORT_SYMBOL_GPL(__pci_complete_power_transition);
@@ -744,6 +757,10 @@ int pci_set_power_state(struct pci_dev *
                 */
                return 0;
 
+       /* Check if we're already there */
+       if (dev->current_state == state)
+               return 0;
+
        __pci_start_power_transition(dev, state);
 
        /* This device is quirked not to be put into D3, so

Index: linux/drivers/pci/pci-driver.c
===================================================================

drivers/pci/pci-driver.c +8 -6
@@ -459,16 +459,17 @@ static int pci_restore_standard_config(s
        return 0;
 }
 
+#endif
+
+#ifdef CONFIG_PM_SLEEP
+
 static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
 {
-       pci_restore_standard_config(pci_dev);
+       pci_power_up(pci_dev);
+       pci_restore_state(pci_dev);
        pci_fixup_device(pci_fixup_resume_early, pci_dev);
 }
 
-#endif
-
-#ifdef CONFIG_PM_SLEEP
-
 /*
  * Default "suspend" method for devices that have no driver provided suspend,
  * or not even a driver at all (second part).
@@ -1050,7 +1051,8 @@ static int pci_pm_runtime_resume(struct
        if (!pm || !pm->runtime_resume)
                return -ENOSYS;
 
-       pci_pm_default_resume_early(pci_dev);
+       pci_restore_standard_config(pci_dev);
+       pci_fixup_device(pci_fixup_resume_early, pci_dev);
        __pci_enable_wake(pci_dev, PCI_D0, true, false);
        pci_fixup_device(pci_fixup_resume, pci_dev);
 

Index: linux/drivers/pci/pci.h
===================================================================

drivers/pci/pci.h +1 -0
@@ -67,6 +67,7 @@ struct pci_platform_pm_ops {
 
 extern int pci_set_platform_pm(struct pci_platform_pm_ops *ops);
 extern void pci_update_current_state(struct pci_dev *dev, pci_power_t state);
+extern void pci_power_up(struct pci_dev *dev);
 extern void pci_disable_enabled_device(struct pci_dev *dev);
 extern int pci_finish_runtime_suspend(struct pci_dev *dev);
 extern int __pci_pme_wakeup(struct pci_dev *dev, void *ign);
#2 lore raw
Quoted 66 lines · Bjorn Helgaas
Quoted 55 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Commit cc2893b6 (PCI: Ensure we re-enable devices on resume) addressed the problem with USB not being powered after resume on recent Lenovo machines, but it did that in a suboptimal way. Namely, it should have changed the relevant code paths only, which are pci_pm_resume_noirq() and pci_pm_restore_noirq() supposed to restore the device's power and standard configuration registers after system resume from suspend or hibernation. Instead, however, it modified pci_set_power_state() which is executed in several other situations too. That resulted in some undesirable effects, like attempting to change a device's power state in the same way multiple times in a row (up to as many as 4 times in a row in the snd_hda_intel driver).

Fix the bug addressed by commit cc2893b6 in an alternative way, by forcibly powering up all devices in pci_pm_default_resume_early(), which is called by pci_pm_resume_noirq() and pci_pm_restore_noirq() to restore the device's power and standard configuration registers, and modifying pci_pm_runtime_resume() to avoid the forcible power-up if not necessary. Then, revert the changes made by commit cc2893b6 to make the confusion introduced by it go away.

---
 drivers/pci/pci-driver.c |   14 ++++++++------
 drivers/pci/pci.c        |   19 ++++++++++++++++++-
 drivers/pci/pci.h        |    1 +
 3 files changed, 27 insertions(+), 7 deletions(-)

Index: linux/drivers/pci/pci.c
===================================================================

drivers/pci/pci.c +13 -0
@@ -662,6 +662,19 @@ void pci_update_current_state(struct pci
 }
 
 /**
+ * pci_power_up - Put the given device into D0 forcibly
+ * @dev: PCI device to power up
+ */
+void pci_power_up(struct pci_dev *dev)
+{
+       if (platform_pci_power_manageable(dev))
+               platform_pci_set_power_state(dev, PCI_D0);
+
+       pci_raw_set_power_state(dev, PCI_D0);
+       pci_update_current_state(dev, PCI_D0);
+}
+
+/**
  * pci_platform_power_transition - Use platform to change device power state
  * @dev: PCI device to handle.
  * @state: State to put the device into.

drivers/pci/pci.c static void __pci_start_power_transition · line 724

Quoted 6 lines
drivers/pci/pci.c +1 -1
@@ -707,7 +720,7 @@ static void __pci_start_power_transition
  */
 int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
 {
-       return state >= PCI_D0 ?
+       return state > PCI_D0 ?
                        pci_platform_power_transition(dev, state) : -EINVAL;

This hunk conflicted with Huang's D3_COLD patches. I fixed it up; can you check and make sure I did it correctly?

Quoted 3 lines

I pushed the resolution to here:
git://git.kernel.org/pub/scm/linux/kernel/git/helgaas/pci.git on the pci/rafael-pci_set_power_state-rebase branch.

Yes, it looks good. The point is to make __pci_complete_power_transition() return the error code for PCI_DO too.

Thanks,
Rafael

maintainer Applied
#3 lore raw
Quoted 67 lines · Rafael J. Wysocki
Quoted 66 lines · Bjorn Helgaas
Quoted 55 lines · Rafael J. Wysocki

From: Rafael J. Wysocki <[email protected]>

Commit cc2893b6 (PCI: Ensure we re-enable devices on resume) addressed the problem with USB not being powered after resume on recent Lenovo machines, but it did that in a suboptimal way. Namely, it should have changed the relevant code paths only, which are pci_pm_resume_noirq() and pci_pm_restore_noirq() supposed to restore the device's power and standard configuration registers after system resume from suspend or hibernation. Instead, however, it modified pci_set_power_state() which is executed in several other situations too. That resulted in some undesirable effects, like attempting to change a device's power state in the same way multiple times in a row (up to as many as 4 times in a row in the snd_hda_intel driver).

Fix the bug addressed by commit cc2893b6 in an alternative way, by forcibly powering up all devices in pci_pm_default_resume_early(), which is called by pci_pm_resume_noirq() and pci_pm_restore_noirq() to restore the device's power and standard configuration registers, and modifying pci_pm_runtime_resume() to avoid the forcible power-up if not necessary. Then, revert the changes made by commit cc2893b6 to make the confusion introduced by it go away.

---
 drivers/pci/pci-driver.c |   14 ++++++++------
 drivers/pci/pci.c        |   19 ++++++++++++++++++-
 drivers/pci/pci.h        |    1 +
 3 files changed, 27 insertions(+), 7 deletions(-)

Index: linux/drivers/pci/pci.c
===================================================================

drivers/pci/pci.c +13 -0
@@ -662,6 +662,19 @@ void pci_update_current_state(struct pci
 }
 
 /**
+ * pci_power_up - Put the given device into D0 forcibly
+ * @dev: PCI device to power up
+ */
+void pci_power_up(struct pci_dev *dev)
+{
+       if (platform_pci_power_manageable(dev))
+               platform_pci_set_power_state(dev, PCI_D0);
+
+       pci_raw_set_power_state(dev, PCI_D0);
+       pci_update_current_state(dev, PCI_D0);
+}
+
+/**
  * pci_platform_power_transition - Use platform to change device power state
  * @dev: PCI device to handle.
  * @state: State to put the device into.

drivers/pci/pci.c static void __pci_start_power_transition · line 724

Quoted 6 lines
drivers/pci/pci.c +1 -1
@@ -707,7 +720,7 @@ static void __pci_start_power_transition
  */
 int __pci_complete_power_transition(struct pci_dev *dev, pci_power_t state)
 {
-       return state >= PCI_D0 ?
+       return state > PCI_D0 ?
                        pci_platform_power_transition(dev, state) : -EINVAL;

This hunk conflicted with Huang's D3_COLD patches. I fixed it up; can you check and make sure I did it correctly?

Quoted 6 lines
Quoted 3 lines

I pushed the resolution to here:
git://git.kernel.org/pub/scm/linux/kernel/git/helgaas/pci.git on the pci/rafael-pci_set_power_state-rebase branch.

Yes, it looks good. The point is to make __pci_complete_power_transition() return the error code for PCI_DO too.

Applied to my "next" branch and pushed, thanks a lot!

Bjorn