linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] PCI: PM: Optimization and cleanup
@ 2019-06-06 22:28 Rafael J. Wysocki
  2019-06-06 22:30 ` [PATCH v2 1/2] PCI: PM: Avoid resuming devices in D3hot during system suspend Rafael J. Wysocki
  2019-06-06 22:32 ` [PATCH 2/2] PCI: PM: Replace pci_dev_keep_suspended() with two functions Rafael J. Wysocki
  0 siblings, 2 replies; 5+ messages in thread
From: Rafael J. Wysocki @ 2019-06-06 22:28 UTC (permalink / raw)
  To: Linux PCI; +Cc: Linux PM, Mika Westerberg, Bjorn Helgaas, LKML

Hi,

This is a v2 of the patch at

https://patchwork.kernel.org/patch/10969867/

and a cleanup of the PCI PM code modified by it as discussed with Bjorn in the
thread started by that patch.

Thanks,
Rafael




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] PCI: PM: Avoid resuming devices in D3hot during system suspend
  2019-06-06 22:28 [PATCH 0/2] PCI: PM: Optimization and cleanup Rafael J. Wysocki
@ 2019-06-06 22:30 ` Rafael J. Wysocki
  2019-06-07 11:17   ` Mika Westerberg
  2019-06-06 22:32 ` [PATCH 2/2] PCI: PM: Replace pci_dev_keep_suspended() with two functions Rafael J. Wysocki
  1 sibling, 1 reply; 5+ messages in thread
From: Rafael J. Wysocki @ 2019-06-06 22:30 UTC (permalink / raw)
  To: Linux PCI; +Cc: Linux PM, Mika Westerberg, Bjorn Helgaas, LKML

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

The current code resumes devices in D3hot during system suspend if
the target power state for them is D3cold, but that is not necessary
in general.  It only is necessary to do that if the platform firmware
requires the device to be resumed, but that should be covered by
the platform_pci_need_resume() check anyway, so rework
pci_dev_keep_suspended() to avoid returning 'false' for devices
in D3hot which need not be resumed due to platform firmware
requirements.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---

-> v2: Add an empty line as requested during review.

---
 drivers/pci/pci.c |   16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

Index: linux-pm/drivers/pci/pci.c
===================================================================
--- linux-pm.orig/drivers/pci/pci.c
+++ linux-pm/drivers/pci/pci.c
@@ -2474,10 +2474,20 @@ bool pci_dev_keep_suspended(struct pci_d
 {
 	struct device *dev = &pci_dev->dev;
 	bool wakeup = device_may_wakeup(dev);
+	pci_power_t target_state;
 
-	if (!pm_runtime_suspended(dev)
-	    || pci_target_state(pci_dev, wakeup) != pci_dev->current_state
-	    || platform_pci_need_resume(pci_dev))
+	if (!pm_runtime_suspended(dev) || platform_pci_need_resume(pci_dev))
+		return false;
+
+	target_state = pci_target_state(pci_dev, wakeup);
+
+	/*
+	 * If the earlier platform check has not triggered, D3cold is just power
+	 * removal on top of D3hot, so no need to resume the device in that
+	 * case.
+	 */
+	if (target_state != pci_dev->current_state &&
+	    target_state != PCI_D3cold && pci_dev->current_state != PCI_D3hot)
 		return false;
 
 	/*




^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/2] PCI: PM: Replace pci_dev_keep_suspended() with two functions
  2019-06-06 22:28 [PATCH 0/2] PCI: PM: Optimization and cleanup Rafael J. Wysocki
  2019-06-06 22:30 ` [PATCH v2 1/2] PCI: PM: Avoid resuming devices in D3hot during system suspend Rafael J. Wysocki
@ 2019-06-06 22:32 ` Rafael J. Wysocki
  2019-06-07 11:20   ` Mika Westerberg
  1 sibling, 1 reply; 5+ messages in thread
From: Rafael J. Wysocki @ 2019-06-06 22:32 UTC (permalink / raw)
  To: Linux PCI; +Cc: Linux PM, Mika Westerberg, Bjorn Helgaas, LKML

From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

The code in pci_dev_keep_suspended() is relatively hard to follow due
to the negative checks in it and in its callers and the function has
a possible side-effect (disabling the PME) which doesn't really match
its role.

For this reason, move the PME disablig from pci_dev_keep_suspended()
to a separate function and change the semantics (and name) of the
rest of it, so that 'true' is returned when the device needs to be
resumed (and not the other way around).  Change the callers of
pci_dev_keep_suspended() accordingly.

While at it, make the code flow in pci_pm_poweroff() reflect the
pci_pm_suspend() more closely to avoid arbitrary differences between
them.

This is a cosmetic change with no intention to alter behavior.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/pci/pci-driver.c |   22 +++++++++++++++---
 drivers/pci/pci.c        |   55 +++++++++++++++++++++++------------------------
 drivers/pci/pci.h        |    3 +-
 3 files changed, 48 insertions(+), 32 deletions(-)

Index: linux-pm/drivers/pci/pci.c
===================================================================
--- linux-pm.orig/drivers/pci/pci.c
+++ linux-pm/drivers/pci/pci.c
@@ -2459,55 +2459,56 @@ bool pci_dev_run_wake(struct pci_dev *de
 EXPORT_SYMBOL_GPL(pci_dev_run_wake);
 
 /**
- * pci_dev_keep_suspended - Check if the device can stay in the suspended state.
+ * pci_dev_need_resume - Check if it is necessary to resume the device.
  * @pci_dev: Device to check.
  *
- * Return 'true' if the device is runtime-suspended, it doesn't have to be
+ * Return 'true' if the device is not runtime-suspended or it has to be
  * reconfigured due to wakeup settings difference between system and runtime
- * suspend and the current power state of it is suitable for the upcoming
- * (system) transition.
- *
- * If the device is not configured for system wakeup, disable PME for it before
- * returning 'true' to prevent it from waking up the system unnecessarily.
+ * suspend, or the current power state of it is not suitable for the upcoming
+ * (system-wide) transition.
  */
-bool pci_dev_keep_suspended(struct pci_dev *pci_dev)
+bool pci_dev_need_resume(struct pci_dev *pci_dev)
 {
 	struct device *dev = &pci_dev->dev;
-	bool wakeup = device_may_wakeup(dev);
 	pci_power_t target_state;
 
 	if (!pm_runtime_suspended(dev) || platform_pci_need_resume(pci_dev))
-		return false;
+		return true;
 
-	target_state = pci_target_state(pci_dev, wakeup);
+	target_state = pci_target_state(pci_dev, device_may_wakeup(dev));
 
 	/*
 	 * If the earlier platform check has not triggered, D3cold is just power
 	 * removal on top of D3hot, so no need to resume the device in that
 	 * case.
 	 */
-	if (target_state != pci_dev->current_state &&
-	    target_state != PCI_D3cold && pci_dev->current_state != PCI_D3hot)
-		return false;
+	return target_state != pci_dev->current_state &&
+		target_state != PCI_D3cold &&
+		pci_dev->current_state != PCI_D3hot;
+}
+
+/**
+ * pci_dev_adjust_pme - Adjust PME setting for a suspended device.
+ * @pci_dev: Device to check.
+ *
+ * If the device is suspended and it is not configured for system wakeup,
+ * disable PME for it to prevent it from waking up the system unnecessarily.
+ *
+ * Note that if the device's power state is D3cold and the platform check in
+ * pci_dev_need_resume() has not triggered, the device's configuration need not
+ * be changed.
+ */
+void pci_dev_adjust_pme(struct pci_dev *pci_dev)
+{
+	struct device *dev = &pci_dev->dev;
 
-	/*
-	 * At this point the device is good to go unless it's been configured
-	 * to generate PME at the runtime suspend time, but it is not supposed
-	 * to wake up the system.  In that case, simply disable PME for it
-	 * (it will have to be re-enabled on exit from system resume).
-	 *
-	 * If the device's power state is D3cold and the platform check above
-	 * hasn't triggered, the device's configuration is suitable and we don't
-	 * need to manipulate it at all.
-	 */
 	spin_lock_irq(&dev->power.lock);
 
-	if (pm_runtime_suspended(dev) && pci_dev->current_state < PCI_D3cold &&
-	    !wakeup)
+	if (pm_runtime_suspended(dev) && !device_may_wakeup(dev) &&
+	    pci_dev->current_state < PCI_D3cold)
 		__pci_pme_active(pci_dev, false);
 
 	spin_unlock_irq(&dev->power.lock);
-	return true;
 }
 
 /**
Index: linux-pm/drivers/pci/pci-driver.c
===================================================================
--- linux-pm.orig/drivers/pci/pci-driver.c
+++ linux-pm/drivers/pci/pci-driver.c
@@ -679,6 +679,7 @@ static bool pci_has_legacy_pm_support(st
 static int pci_pm_prepare(struct device *dev)
 {
 	struct device_driver *drv = dev->driver;
+	struct pci_dev *pci_dev = to_pci_dev(dev);
 
 	if (drv && drv->pm && drv->pm->prepare) {
 		int error = drv->pm->prepare(dev);
@@ -688,7 +689,15 @@ static int pci_pm_prepare(struct device
 		if (!error && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
 			return 0;
 	}
-	return pci_dev_keep_suspended(to_pci_dev(dev));
+	if (pci_dev_need_resume(pci_dev))
+		return 0;
+
+	/*
+	 * The PME setting needs to be adjusted here in case the direct-complete
+	 * optimization is used with respect to this device.
+	 */
+	pci_dev_adjust_pme(pci_dev);
+	return 1;
 }
 
 static void pci_pm_complete(struct device *dev)
@@ -758,9 +767,11 @@ static int pci_pm_suspend(struct device
 	 * better to resume the device from runtime suspend here.
 	 */
 	if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
-	    !pci_dev_keep_suspended(pci_dev)) {
+	    pci_dev_need_resume(pci_dev)) {
 		pm_runtime_resume(dev);
 		pci_dev->state_saved = false;
+	} else {
+		pci_dev_adjust_pme(pci_dev);
 	}
 
 	if (pm->suspend) {
@@ -1108,10 +1119,13 @@ static int pci_pm_poweroff(struct device
 
 	/* The reason to do that is the same as in pci_pm_suspend(). */
 	if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
-	    !pci_dev_keep_suspended(pci_dev))
+	    pci_dev_need_resume(pci_dev)) {
 		pm_runtime_resume(dev);
+		pci_dev->state_saved = false;
+	} else {
+		pci_dev_adjust_pme(pci_dev);
+	}
 
-	pci_dev->state_saved = false;
 	if (pm->poweroff) {
 		int error;
 
Index: linux-pm/drivers/pci/pci.h
===================================================================
--- linux-pm.orig/drivers/pci/pci.h
+++ linux-pm/drivers/pci/pci.h
@@ -82,7 +82,8 @@ int pci_finish_runtime_suspend(struct pc
 void pcie_clear_root_pme_status(struct pci_dev *dev);
 int __pci_pme_wakeup(struct pci_dev *dev, void *ign);
 void pci_pme_restore(struct pci_dev *dev);
-bool pci_dev_keep_suspended(struct pci_dev *dev);
+bool pci_dev_need_resume(struct pci_dev *dev);
+void pci_dev_adjust_pme(struct pci_dev *dev);
 void pci_dev_complete_resume(struct pci_dev *pci_dev);
 void pci_config_pm_runtime_get(struct pci_dev *dev);
 void pci_config_pm_runtime_put(struct pci_dev *dev);




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] PCI: PM: Avoid resuming devices in D3hot during system suspend
  2019-06-06 22:30 ` [PATCH v2 1/2] PCI: PM: Avoid resuming devices in D3hot during system suspend Rafael J. Wysocki
@ 2019-06-07 11:17   ` Mika Westerberg
  0 siblings, 0 replies; 5+ messages in thread
From: Mika Westerberg @ 2019-06-07 11:17 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: Linux PCI, Linux PM, Bjorn Helgaas, LKML

On Fri, Jun 07, 2019 at 12:30:58AM +0200, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> 
> The current code resumes devices in D3hot during system suspend if
> the target power state for them is D3cold, but that is not necessary
> in general.  It only is necessary to do that if the platform firmware
> requires the device to be resumed, but that should be covered by
> the platform_pci_need_resume() check anyway, so rework
> pci_dev_keep_suspended() to avoid returning 'false' for devices
> in D3hot which need not be resumed due to platform firmware
> requirements.
> 
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] PCI: PM: Replace pci_dev_keep_suspended() with two functions
  2019-06-06 22:32 ` [PATCH 2/2] PCI: PM: Replace pci_dev_keep_suspended() with two functions Rafael J. Wysocki
@ 2019-06-07 11:20   ` Mika Westerberg
  0 siblings, 0 replies; 5+ messages in thread
From: Mika Westerberg @ 2019-06-07 11:20 UTC (permalink / raw)
  To: Rafael J. Wysocki; +Cc: Linux PCI, Linux PM, Bjorn Helgaas, LKML

On Fri, Jun 07, 2019 at 12:32:31AM +0200, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
> 
> The code in pci_dev_keep_suspended() is relatively hard to follow due
> to the negative checks in it and in its callers and the function has
> a possible side-effect (disabling the PME) which doesn't really match
> its role.
> 
> For this reason, move the PME disablig from pci_dev_keep_suspended()
                                ^^^^^^^^
disabling

> to a separate function and change the semantics (and name) of the
> rest of it, so that 'true' is returned when the device needs to be
> resumed (and not the other way around).  Change the callers of
> pci_dev_keep_suspended() accordingly.
> 
> While at it, make the code flow in pci_pm_poweroff() reflect the
> pci_pm_suspend() more closely to avoid arbitrary differences between
> them.
> 
> This is a cosmetic change with no intention to alter behavior.
> 
> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-06-07 11:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-06 22:28 [PATCH 0/2] PCI: PM: Optimization and cleanup Rafael J. Wysocki
2019-06-06 22:30 ` [PATCH v2 1/2] PCI: PM: Avoid resuming devices in D3hot during system suspend Rafael J. Wysocki
2019-06-07 11:17   ` Mika Westerberg
2019-06-06 22:32 ` [PATCH 2/2] PCI: PM: Replace pci_dev_keep_suspended() with two functions Rafael J. Wysocki
2019-06-07 11:20   ` Mika Westerberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).