public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
From: "Rafael J. Wysocki" <rjw@sisk.pl>
To: Jesse Barnes <jbarnes@virtuousgeek.org>
Cc: Linux PCI <linux-pci@vger.kernel.org>,
	pm list <linux-pm@lists.linux-foundation.org>,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH 2/2] PCI PM: Read device power state from register after updating it
Date: Tue, 26 May 2009 21:52:29 +0200	[thread overview]
Message-ID: <200905262152.29489.rjw@sisk.pl> (raw)
In-Reply-To: <200905182252.21478.rjw@sisk.pl>

On Monday 18 May 2009, Rafael J. Wysocki wrote:
> From: Rafael J. Wysocki <rjw@sisk.pl>
> 
> After attempting to change the power state of a PCI device
> pci_raw_set_power_state() doesn't check if the value it wrote into
> the device's PCI_PM_CTRL register has been stored in there.  Still,
> it modifies the device's current_state field as though that's the
> case.  This may cause the driver of the device to think that its
> power state has been changed while in fact it hasn't.
> 
> To prevent such situations from happening modify
> pci_raw_set_power_state() so that it reads the device's PCI_PM_CTRL
> register after writing into it and uses the value read from the
> register to update the device's current_state field.  Also make it
> return -EIO if the new state of the device is not equal to the state
> requested by the called.
> 
> To distinguish this error condition from the other ones make
> pci_raw_set_power_state() return -ENOSYS instead of -EIO when it is
> impossible to change the power state of the device, because it
> doesn't support the native PCI PM at all or the requested target
> state is not supported by it.

Having reconsidered it I think that -ENODEV is probably better than -ENOSYS for
this purpose.  Updated patch follows.

Thanks,
Rafael

---
From: Rafael J. Wysocki <rjw@sisk.pl>
Subject: PCI PM: Read device power state from register after updating it

After attempting to change the power state of a PCI device
pci_raw_set_power_state() doesn't check if the value it wrote into
the device's PCI_PM_CTRL register has been stored in there.  Still,
it modifies the device's current_state field as though that's the
case.  This may cause the driver of the device to think that its
power state has been changed while in fact it hasn't.

To prevent such situations from happening modify
pci_raw_set_power_state() so that it reads the device's PCI_PM_CTRL
register after writing into it and uses the value read from the
register to update the device's current_state field.  Also make it
return -EIO if the new state of the device is not equal to the state
requested by the called.

To distinguish this error condition from the other ones make
pci_raw_set_power_state() return -ENODEV instead of -EIO when it is
impossible to change the power state of the device, because it
doesn't support the native PCI PM at all or the requested target
state is not supported by it.

Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
---
 drivers/pci/pci.c |   10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Index: linux-2.6/drivers/pci/pci.c
===================================================================
--- linux-2.6.orig/drivers/pci/pci.c
+++ linux-2.6/drivers/pci/pci.c
@@ -446,7 +446,7 @@ static int pci_raw_set_power_state(struc
 		return 0;
 
 	if (!dev->pm_cap)
-		return -EIO;
+		return -ENODEV;
 
 	if (state < PCI_D0 || state > PCI_D3hot)
 		return -EINVAL;
@@ -465,7 +465,7 @@ static int pci_raw_set_power_state(struc
 	/* check if this device supports the desired state */
 	if ((state == PCI_D1 && !dev->d1_support)
 	   || (state == PCI_D2 && !dev->d2_support))
-		return -EIO;
+		return -ENODEV;
 
 	pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
 
@@ -502,7 +502,11 @@ static int pci_raw_set_power_state(struc
 	else if (state == PCI_D2 || dev->current_state == PCI_D2)
 		udelay(PCI_PM_D2_DELAY);
 
-	dev->current_state = state;
+	pci_read_config_word(dev, dev->pm_cap + PCI_PM_CTRL, &pmcsr);
+	dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
+	/* Return error code if we have failed to change the state */
+	if (dev->current_state != state)
+		return -EIO;
 
 	/* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
 	 * INTERFACE SPECIFICATION, REV. 1.2", a device transitioning

  parent reply	other threads:[~2009-05-26 19:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <200905182246.41129.rjw@sisk.pl>
2009-05-18 20:51 ` [PATCH 1/2] PCI PM: Follow PCI_PM_CTRL_NO_SOFT_RESET during transitions from D3 Rafael J. Wysocki
2009-05-18 20:52 ` [PATCH 2/2] PCI PM: Read device power state from register after updating it Rafael J. Wysocki
     [not found] ` <200905182252.21478.rjw@sisk.pl>
2009-05-26 19:52   ` Rafael J. Wysocki [this message]
2009-06-11 18:54     ` Jesse Barnes
     [not found]     ` <20090611115458.66650e04@jbarnes-g45>
2009-06-11 19:53       ` Rafael J. Wysocki
     [not found]       ` <200906112153.16853.rjw@sisk.pl>
2009-06-11 20:00         ` Jesse Barnes
     [not found] ` <200905182251.13197.rjw@sisk.pl>
2009-06-11 18:26   ` [PATCH 1/2] PCI PM: Follow PCI_PM_CTRL_NO_SOFT_RESET during transitions from D3 Jesse Barnes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200905262152.29489.rjw@sisk.pl \
    --to=rjw@sisk.pl \
    --cc=jbarnes@virtuousgeek.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-pm@lists.linux-foundation.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox