Skip to contentSkip to conversation

PCI PM: Rework pci_set_power_state

RFC PATCH In review Rafael J. Wysocki 3 participants 17 messages Jun 17, 2008 – Jun 20, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover PCI PM: Rework pci_set_power_state

Hi,

I've recently discovered that pci_set_power_state() calls the platform callback intended for changing the state of the device after it has done that using the native PCI mechanism. In my opinion this is not correct, because, for example, if the device is being put into D0 from a low power state, it may require some platform-controlled power resources to be turned on before that (in theory).

The following series of patches changes this function so that the platform callback is performed first and then the native mechanism is used. Additionally, however, it checks if the devices is power manageable by the platform and only in that case the platform callback is invoked. For this purpose, I needed to add an ACPI function allowing PCI to check the power manageability of given device.

Comments welcome.

Thanks,
Rafael

#1 lore raw

RFC PATCH 1/3 ACPI: Introduce acpi_bus_power_manageable

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

Introduce function acpi_bus_power_manageable() allowing other (dependent) subsystems to check if ACPI is able to power manage given device. This may be useful, for example, for PCI device power management.

---
 drivers/acpi/bus.c      |   11 +++++++++++
 include/acpi/acpi_bus.h |    1 +
 2 files changed, 12 insertions(+)
drivers/acpi/bus.c +11 -0
Index: linux-2.6/drivers/acpi/bus.c
===================================================================
@@ -295,6 +295,17 @@ int acpi_bus_set_power(acpi_handle handl
 
 EXPORT_SYMBOL(acpi_bus_set_power);
 
+bool acpi_bus_power_manageable(acpi_handle handle)
+{
+	struct acpi_device *device;
+	int result;
+
+	result = acpi_bus_get_device(handle, &device);
+	return result ? false : device->flags.power_manageable;
+}
+
+EXPORT_SYMBOL(acpi_bus_power_manageable);
+
 /* --------------------------------------------------------------------------
                                 Event Management
    -------------------------------------------------------------------------- */
include/acpi/acpi_bus.h +1 -0
Index: linux-2.6/include/acpi/acpi_bus.h
===================================================================
@@ -335,6 +335,7 @@ void acpi_bus_data_handler(acpi_handle h
 int acpi_bus_get_status(struct acpi_device *device);
 int acpi_bus_get_power(acpi_handle handle, int *state);
 int acpi_bus_set_power(acpi_handle handle, int state);
+bool acpi_bus_power_manageable(acpi_handle handle);
 #ifdef CONFIG_ACPI_PROC_EVENT
 int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data);
 int acpi_bus_generate_proc_event4(const char *class, const char *bid, u8 type, int data);
#2 lore raw

RFC PATCH 1/3 ACPI: Introduce acpi_bus_power_manageable

In reply to Rafael J. Wysocki

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

Introduce function acpi_bus_power_manageable() allowing other (dependent) subsystems to check if ACPI is able to power manage given device. This may be useful, for example, for PCI device power management.

---
 drivers/acpi/bus.c      |   11 +++++++++++
 include/acpi/acpi_bus.h |    1 +
 2 files changed, 12 insertions(+)
drivers/acpi/bus.c +11 -0
Index: linux-2.6/drivers/acpi/bus.c
===================================================================
@@ -295,6 +295,17 @@ int acpi_bus_set_power(acpi_handle handl
 
 EXPORT_SYMBOL(acpi_bus_set_power);
 
+bool acpi_bus_power_manageable(acpi_handle handle)
+{
+	struct acpi_device *device;
+	int result;
+
+	result = acpi_bus_get_device(handle, &device);
+	return result ? false : device->flags.power_manageable;
+}
+
+EXPORT_SYMBOL(acpi_bus_power_manageable);
+
 /* --------------------------------------------------------------------------
                                 Event Management
    -------------------------------------------------------------------------- */
include/acpi/acpi_bus.h +1 -0
Index: linux-2.6/include/acpi/acpi_bus.h
===================================================================
@@ -335,6 +335,7 @@ void acpi_bus_data_handler(acpi_handle h
 int acpi_bus_get_status(struct acpi_device *device);
 int acpi_bus_get_power(acpi_handle handle, int *state);
 int acpi_bus_set_power(acpi_handle handle, int state);
+bool acpi_bus_power_manageable(acpi_handle handle);
 #ifdef CONFIG_ACPI_PROC_EVENT
 int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data);
 int acpi_bus_generate_proc_event4(const char *class, const char *bid, u8 type, int data);
#3 lore raw

RFC PATCH 2/3 PCI PM: Introduce platform_pci_power_manageable

In reply to Rafael J. Wysocki

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

Introduce function pointer platform_pci_power_manageable to be used by the platform-related code to point to a function allowing us to check if given device is power manageable by the platform.

Introduce acpi_pci_power_manageable() playing that role for ACPI.

---
 drivers/pci/pci-acpi.c |   11 +++++++++++
 drivers/pci/pci.c      |    1 +
 drivers/pci/pci.h      |    1 +
 3 files changed, 13 insertions(+)
drivers/pci/pci-acpi.c +11 -0
Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
@@ -315,6 +315,16 @@ static pci_power_t acpi_pci_choose_state
 }
 #endif
 
+static bool acpi_pci_power_manageable(struct pci_dev *dev)
+{
+	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+
+	if (!handle)
+		return -ENODEV;
+
+	return acpi_bus_power_manageable(handle);
+}
+
 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
 {
 	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
@@ -398,6 +408,7 @@ static int __init acpi_pci_init(void)
 #ifdef	CONFIG_ACPI_SLEEP
 	platform_pci_choose_state = acpi_pci_choose_state;
 #endif
+	platform_pci_power_manageable = acpi_pci_power_manageable;
 	platform_pci_set_power_state = acpi_pci_set_power_state;
 	return 0;
 }
drivers/pci/pci.c +1 -0
Index: linux-2.6/drivers/pci/pci.c
===================================================================
@@ -378,6 +378,7 @@ pci_restore_bars(struct pci_dev *dev)
 		pci_update_resource(dev, &dev->resource[i], i);
 }
 
+bool (*platform_pci_power_manageable)(struct pci_dev *dev);
 int (*platform_pci_set_power_state)(struct pci_dev *dev, pci_power_t t);
 
 /**
drivers/pci/pci.h +1 -0
Index: linux-2.6/drivers/pci/pci.h
===================================================================
@@ -6,6 +6,7 @@ extern void pci_remove_sysfs_dev_files(s
 extern void pci_cleanup_rom(struct pci_dev *dev);
 
 /* Firmware callbacks */
+extern bool (*platform_pci_power_manageable)(struct pci_dev *dev);
 extern pci_power_t (*platform_pci_choose_state)(struct pci_dev *dev);
 extern int (*platform_pci_set_power_state)(struct pci_dev *dev,
 						pci_power_t state);
#4 lore raw

RFC PATCH 2/3 PCI PM: Introduce platform_pci_power_manageable

In reply to Rafael J. Wysocki

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

Introduce function pointer platform_pci_power_manageable to be used by the platform-related code to point to a function allowing us to check if given device is power manageable by the platform.

Introduce acpi_pci_power_manageable() playing that role for ACPI.

---
 drivers/pci/pci-acpi.c |   11 +++++++++++
 drivers/pci/pci.c      |    1 +
 drivers/pci/pci.h      |    1 +
 3 files changed, 13 insertions(+)
drivers/pci/pci-acpi.c +11 -0
Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
@@ -315,6 +315,16 @@ static pci_power_t acpi_pci_choose_state
 }
 #endif
 
+static bool acpi_pci_power_manageable(struct pci_dev *dev)
+{
+	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+
+	if (!handle)
+		return -ENODEV;
+
+	return acpi_bus_power_manageable(handle);
+}
+
 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
 {
 	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
@@ -398,6 +408,7 @@ static int __init acpi_pci_init(void)
 #ifdef	CONFIG_ACPI_SLEEP
 	platform_pci_choose_state = acpi_pci_choose_state;
 #endif
+	platform_pci_power_manageable = acpi_pci_power_manageable;
 	platform_pci_set_power_state = acpi_pci_set_power_state;
 	return 0;
 }
drivers/pci/pci.c +1 -0
Index: linux-2.6/drivers/pci/pci.c
===================================================================
@@ -378,6 +378,7 @@ pci_restore_bars(struct pci_dev *dev)
 		pci_update_resource(dev, &dev->resource[i], i);
 }
 
+bool (*platform_pci_power_manageable)(struct pci_dev *dev);
 int (*platform_pci_set_power_state)(struct pci_dev *dev, pci_power_t t);
 
 /**
drivers/pci/pci.h +1 -0
Index: linux-2.6/drivers/pci/pci.h
===================================================================
@@ -6,6 +6,7 @@ extern void pci_remove_sysfs_dev_files(s
 extern void pci_cleanup_rom(struct pci_dev *dev);
 
 /* Firmware callbacks */
+extern bool (*platform_pci_power_manageable)(struct pci_dev *dev);
 extern pci_power_t (*platform_pci_choose_state)(struct pci_dev *dev);
 extern int (*platform_pci_set_power_state)(struct pci_dev *dev,
 						pci_power_t state);
#5 lore raw

Re: [RFC][PATCH 2/3] PCI PM: Introduce platform_pci_power_manageable

Quoted 20 lines · Rafael J. Wysocki

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

Introduce function pointer platform_pci_power_manageable to be used by the platform-related code to point to a function allowing us to check if given device is power manageable by the platform.

Introduce acpi_pci_power_manageable() playing that role for ACPI.

---
 drivers/pci/pci-acpi.c |   11 +++++++++++
 drivers/pci/pci.c      |    1 +
 drivers/pci/pci.h      |    1 +
 3 files changed, 13 insertions(+)

Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================

drivers/pci/pci-acpi.c static pci_power_t acpi_pci_choose_state · line 323

Quoted 9 lines
drivers/pci/pci-acpi.c +6 -0
@@ -315,6 +315,16 @@ static pci_power_t acpi_pci_choose_state
 }
 #endif
 
+static bool acpi_pci_power_manageable(struct pci_dev *dev)
+{
+	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+
+	if (!handle)
+		return -ENODEV;

Seems like a bad idea to pass non-true/false values using a bool. Wouldn't an int return type be more appropriate here?

<snip>

Cheers,
-Matt Helsley

#6 lore raw

Re: [linux-pm] [RFC][PATCH 2/3] PCI PM: Introduce platform_pci_power_manageable

In reply to Rafael J. Wysocki
Quoted 20 lines · Rafael J. Wysocki

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

Introduce function pointer platform_pci_power_manageable to be used by the platform-related code to point to a function allowing us to check if given device is power manageable by the platform.

Introduce acpi_pci_power_manageable() playing that role for ACPI.

---
 drivers/pci/pci-acpi.c |   11 +++++++++++
 drivers/pci/pci.c      |    1 +
 drivers/pci/pci.h      |    1 +
 3 files changed, 13 insertions(+)

Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================

drivers/pci/pci-acpi.c static pci_power_t acpi_pci_choose_state · line 323

Quoted 9 lines
drivers/pci/pci-acpi.c +6 -0
@@ -315,6 +315,16 @@ static pci_power_t acpi_pci_choose_state
 }
 #endif
 
+static bool acpi_pci_power_manageable(struct pci_dev *dev)
+{
+	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+
+	if (!handle)
+		return -ENODEV;

Seems like a bad idea to pass non-true/false values using a bool. Wouldn't an int return type be more appropriate here?

<snip>

Cheers,
-Matt Helsley

#7 lore raw

Re: [linux-pm] [RFC][PATCH 2/3] PCI PM: Introduce platform_pci_power_manageable

Quoted 25 lines · Matt Helsley
Quoted 23 lines · Rafael J. Wysocki

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

Introduce function pointer platform_pci_power_manageable to be used by the platform-related code to point to a function allowing us to check if given device is power manageable by the platform.

Introduce acpi_pci_power_manageable() playing that role for ACPI.

---
 drivers/pci/pci-acpi.c |   11 +++++++++++
 drivers/pci/pci.c      |    1 +
 drivers/pci/pci.h      |    1 +
 3 files changed, 13 insertions(+)

Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================

drivers/pci/pci-acpi.c +0 -0
@@ -315,6 +315,16 @@ static pci_power_t acpi_pci_choose_state
 }
 #endif
 
Quoted 9 lines

drivers/pci/pci-acpi.c static pci_power_t acpi_pci_choose_state · line 323

Quoted 6 lines
drivers/pci/pci-acpi.c +6 -0
+static bool acpi_pci_power_manageable(struct pci_dev *dev)
+{
+	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+
+	if (!handle)
+		return -ENODEV;

Seems like a bad idea to pass non-true/false values using a bool. Wouldn't an int return type be more appropriate here?

That's a bug actually. It should be 'false'.

Thanks,
Rafael

#8 lore raw

Re: [linux-pm] [RFC][PATCH 2/3] PCI PM: Introduce platform_pci_power_manageable

Quoted 36 lines · Rafael J. Wysocki
Quoted 25 lines · Matt Helsley
Quoted 23 lines · Rafael J. Wysocki

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

Introduce function pointer platform_pci_power_manageable to be used by the platform-related code to point to a function allowing us to check if given device is power manageable by the platform.

Introduce acpi_pci_power_manageable() playing that role for ACPI.

---
 drivers/pci/pci-acpi.c |   11 +++++++++++
 drivers/pci/pci.c      |    1 +
 drivers/pci/pci.h      |    1 +
 3 files changed, 13 insertions(+)

Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================

drivers/pci/pci-acpi.c +0 -0
@@ -315,6 +315,16 @@ static pci_power_t acpi_pci_choose_state
 }
 #endif
 
Quoted 9 lines

drivers/pci/pci-acpi.c static pci_power_t acpi_pci_choose_state · line 323

Quoted 6 lines
drivers/pci/pci-acpi.c +6 -0
+static bool acpi_pci_power_manageable(struct pci_dev *dev)
+{
+	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+
+	if (!handle)
+		return -ENODEV;

Seems like a bad idea to pass non-true/false values using a bool. Wouldn't an int return type be more appropriate here?

Quoted 1 line

That's a bug actually. It should be 'false'.

Updated patch follows.

Thanks,
Rafael

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

Introduce function pointer platform_pci_power_manageable to be used by the platform-related code to point to a function allowing us to check if given device is power manageable by the platform.

Introduce acpi_pci_power_manageable() playing that role for ACPI.

---
 drivers/pci/pci-acpi.c |    8 ++++++++
 drivers/pci/pci.c      |    1 +
 drivers/pci/pci.h      |    1 +
 3 files changed, 10 insertions(+)
drivers/pci/pci-acpi.c +8 -0
Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
@@ -315,6 +315,13 @@ static pci_power_t acpi_pci_choose_state
 }
 #endif
 
+static bool acpi_pci_power_manageable(struct pci_dev *dev)
+{
+	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+
+	return handle ? acpi_bus_power_manageable(handle) : false;
+}
+
 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
 {
 	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
@@ -398,6 +405,7 @@ static int __init acpi_pci_init(void)
 #ifdef	CONFIG_ACPI_SLEEP
 	platform_pci_choose_state = acpi_pci_choose_state;
 #endif
+	platform_pci_power_manageable = acpi_pci_power_manageable;
 	platform_pci_set_power_state = acpi_pci_set_power_state;
 	return 0;
 }
drivers/pci/pci.c +1 -0
Index: linux-2.6/drivers/pci/pci.c
===================================================================
@@ -378,6 +378,7 @@ pci_restore_bars(struct pci_dev *dev)
 		pci_update_resource(dev, &dev->resource[i], i);
 }
 
+bool (*platform_pci_power_manageable)(struct pci_dev *dev);
 int (*platform_pci_set_power_state)(struct pci_dev *dev, pci_power_t t);
 
 /**
drivers/pci/pci.h +1 -0
Index: linux-2.6/drivers/pci/pci.h
===================================================================
@@ -6,6 +6,7 @@ extern void pci_remove_sysfs_dev_files(s
 extern void pci_cleanup_rom(struct pci_dev *dev);
 
 /* Firmware callbacks */
+extern bool (*platform_pci_power_manageable)(struct pci_dev *dev);
 extern pci_power_t (*platform_pci_choose_state)(struct pci_dev *dev);
 extern int (*platform_pci_set_power_state)(struct pci_dev *dev,
 						pci_power_t state);
#9 lore raw

Re: [RFC][PATCH 2/3] PCI PM: Introduce platform_pci_power_manageable

In reply to Rafael J. Wysocki
Quoted 36 lines · Rafael J. Wysocki
Quoted 25 lines · Matt Helsley
Quoted 23 lines · Rafael J. Wysocki

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

Introduce function pointer platform_pci_power_manageable to be used by the platform-related code to point to a function allowing us to check if given device is power manageable by the platform.

Introduce acpi_pci_power_manageable() playing that role for ACPI.

---
 drivers/pci/pci-acpi.c |   11 +++++++++++
 drivers/pci/pci.c      |    1 +
 drivers/pci/pci.h      |    1 +
 3 files changed, 13 insertions(+)

Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================

drivers/pci/pci-acpi.c +0 -0
@@ -315,6 +315,16 @@ static pci_power_t acpi_pci_choose_state
 }
 #endif
 
Quoted 9 lines

drivers/pci/pci-acpi.c static pci_power_t acpi_pci_choose_state · line 323

Quoted 6 lines
drivers/pci/pci-acpi.c +6 -0
+static bool acpi_pci_power_manageable(struct pci_dev *dev)
+{
+	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+
+	if (!handle)
+		return -ENODEV;

Seems like a bad idea to pass non-true/false values using a bool. Wouldn't an int return type be more appropriate here?

Quoted 1 line

That's a bug actually. It should be 'false'.

Updated patch follows.

Thanks,
Rafael

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

Introduce function pointer platform_pci_power_manageable to be used by the platform-related code to point to a function allowing us to check if given device is power manageable by the platform.

Introduce acpi_pci_power_manageable() playing that role for ACPI.

---
 drivers/pci/pci-acpi.c |    8 ++++++++
 drivers/pci/pci.c      |    1 +
 drivers/pci/pci.h      |    1 +
 3 files changed, 10 insertions(+)
drivers/pci/pci-acpi.c +8 -0
Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
@@ -315,6 +315,13 @@ static pci_power_t acpi_pci_choose_state
 }
 #endif
 
+static bool acpi_pci_power_manageable(struct pci_dev *dev)
+{
+	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+
+	return handle ? acpi_bus_power_manageable(handle) : false;
+}
+
 static int acpi_pci_set_power_state(struct pci_dev *dev, pci_power_t state)
 {
 	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
@@ -398,6 +405,7 @@ static int __init acpi_pci_init(void)
 #ifdef	CONFIG_ACPI_SLEEP
 	platform_pci_choose_state = acpi_pci_choose_state;
 #endif
+	platform_pci_power_manageable = acpi_pci_power_manageable;
 	platform_pci_set_power_state = acpi_pci_set_power_state;
 	return 0;
 }
drivers/pci/pci.c +1 -0
Index: linux-2.6/drivers/pci/pci.c
===================================================================
@@ -378,6 +378,7 @@ pci_restore_bars(struct pci_dev *dev)
 		pci_update_resource(dev, &dev->resource[i], i);
 }
 
+bool (*platform_pci_power_manageable)(struct pci_dev *dev);
 int (*platform_pci_set_power_state)(struct pci_dev *dev, pci_power_t t);
 
 /**
drivers/pci/pci.h +1 -0
Index: linux-2.6/drivers/pci/pci.h
===================================================================
@@ -6,6 +6,7 @@ extern void pci_remove_sysfs_dev_files(s
 extern void pci_cleanup_rom(struct pci_dev *dev);
 
 /* Firmware callbacks */
+extern bool (*platform_pci_power_manageable)(struct pci_dev *dev);
 extern pci_power_t (*platform_pci_choose_state)(struct pci_dev *dev);
 extern int (*platform_pci_set_power_state)(struct pci_dev *dev,
 						pci_power_t state);
#10 lore raw

Re: [RFC][PATCH 2/3] PCI PM: Introduce platform_pci_power_manageable

In reply to Matt Helsley
Quoted 25 lines · Matt Helsley
Quoted 23 lines · Rafael J. Wysocki

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

Introduce function pointer platform_pci_power_manageable to be used by the platform-related code to point to a function allowing us to check if given device is power manageable by the platform.

Introduce acpi_pci_power_manageable() playing that role for ACPI.

---
 drivers/pci/pci-acpi.c |   11 +++++++++++
 drivers/pci/pci.c      |    1 +
 drivers/pci/pci.h      |    1 +
 3 files changed, 13 insertions(+)

Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================

drivers/pci/pci-acpi.c +0 -0
@@ -315,6 +315,16 @@ static pci_power_t acpi_pci_choose_state
 }
 #endif
 
Quoted 9 lines

drivers/pci/pci-acpi.c static pci_power_t acpi_pci_choose_state · line 323

Quoted 6 lines
drivers/pci/pci-acpi.c +6 -0
+static bool acpi_pci_power_manageable(struct pci_dev *dev)
+{
+	acpi_handle handle = DEVICE_ACPI_HANDLE(&dev->dev);
+
+	if (!handle)
+		return -ENODEV;

Seems like a bad idea to pass non-true/false values using a bool. Wouldn't an int return type be more appropriate here?

That's a bug actually. It should be 'false'.

Thanks,
Rafael

#11 lore raw

RFC PATCH 3/3 PCI PM: Rework pci_set_power_state

In reply to Rafael J. Wysocki

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

Rework pci_set_power_state() so that the platform callback is invoked before the native mechanism, if necessary. Also, make the function check if the device is power manageable by the platform before invoking the platform callback.

This may matter if the device dependent on additional power resources controlled by the platform is being put into D0, in which case those power resources must be turned on before we attempt to handle the device itself.

---
 drivers/pci/pci-acpi.c |    2 +-
 drivers/pci/pci.c      |   44 +++++++++++++++++++++++++++++++-------------
 2 files changed, 32 insertions(+), 14 deletions(-)
drivers/pci/pci.c +31 -13
Index: linux-2.6/drivers/pci/pci.c
===================================================================
@@ -398,7 +398,8 @@ int (*platform_pci_set_power_state)(stru
 int
 pci_set_power_state(struct pci_dev *dev, pci_power_t state)
 {
-	int pm, need_restore = 0;
+	int pm;
+	bool platform_pm, platform_done = false, need_restore = false;
 	u16 pmcsr, pmc;
 
 	/* bound the state we're entering */
@@ -413,12 +414,35 @@ pci_set_power_state(struct pci_dev *dev,
 	if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
 		return 0;
 
+	platform_pm = (platform_pci_power_manageable
+			&& platform_pci_power_manageable(dev));
+	if (platform_pm) {
+		/*
+		 * Allow the platform to change the state, for example via ACPI
+		 * _PRx, _PSx and some such, but do not trust it.
+		 */
+		platform_done = !platform_pci_set_power_state(dev, state);
+		if (platform_done)
+			printk(KERN_INFO
+			"PCI: Power state of %s set by the platform\n",
+			pci_name(dev));
+	}
+
 	/* find PCI PM capability in list */
 	pm = pci_find_capability(dev, PCI_CAP_ID_PM);
 
 	/* abort if the device doesn't support PM capabilities */
 	if (!pm)
-		return -EIO;
+		return platform_done ? 0 : -EIO;
+
+	if (platform_pm) {
+		/*
+		 * The platform might have changed the state, make sure that
+		 * the cached value is still valid.
+		 */
+		pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
+		dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
+	}
 
 	/* Validate current state:
 	 * Can enter D0 from any state, but if we can only go deeper 
@@ -427,12 +451,12 @@ pci_set_power_state(struct pci_dev *dev,
 	if (state != PCI_D0 && dev->current_state > state) {
 		printk(KERN_ERR "%s(): %s: state=%d, current state=%d\n",
 			__func__, pci_name(dev), state, dev->current_state);
-		return -EINVAL;
+		return platform_done ? 0 : -EINVAL;
 	} else if (dev->current_state == state)
 		return 0;        /* we're already there */
 
 
-	pci_read_config_word(dev,pm + PCI_PM_PMC,&pmc);
+	pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
 	if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
 		printk(KERN_DEBUG
 		       "PCI: %s has unsupported PM cap regs version (%u)\n",
@@ -446,7 +470,8 @@ pci_set_power_state(struct pci_dev *dev,
 	else if (state == PCI_D2 && !(pmc & PCI_PM_CAP_D2))
 		return -EIO;
 
-	pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
+	if (!platform_pm)
+		pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
 
 	/* If we're (effectively) in D3, force entire word to 0.
 	 * This doesn't affect PME_Status, disables PME_En, and
@@ -462,7 +487,7 @@ pci_set_power_state(struct pci_dev *dev,
 	case PCI_UNKNOWN: /* Boot-up */
 		if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
 		 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
-			need_restore = 1;
+			need_restore = true;
 		/* Fall-through: force to D0 */
 	default:
 		pmcsr = 0;
@@ -479,13 +504,6 @@ pci_set_power_state(struct pci_dev *dev,
 	else if (state == PCI_D2 || dev->current_state == PCI_D2)
 		udelay(200);
 
-	/*
-	 * Give firmware a chance to be called, such as ACPI _PRx, _PSx
-	 * Firmware method after native method ?
-	 */
-	if (platform_pci_set_power_state)
-		platform_pci_set_power_state(dev, state);
-
 	dev->current_state = state;
 
 	/* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
drivers/pci/pci-acpi.c +1 -1
Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
@@ -341,7 +341,7 @@ static int acpi_pci_set_power_state(stru
 		return -ENODEV;
 	/* If the ACPI device has _EJ0, ignore the device */
 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
-		return 0;
+		return -EINVAL;
 
 	switch (state) {
 	case PCI_D0:
#12 lore raw

RFC PATCH 3/3 PCI PM: Rework pci_set_power_state

In reply to Rafael J. Wysocki

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

Rework pci_set_power_state() so that the platform callback is invoked before the native mechanism, if necessary. Also, make the function check if the device is power manageable by the platform before invoking the platform callback.

This may matter if the device dependent on additional power resources controlled by the platform is being put into D0, in which case those power resources must be turned on before we attempt to handle the device itself.

---
 drivers/pci/pci-acpi.c |    2 +-
 drivers/pci/pci.c      |   44 +++++++++++++++++++++++++++++++-------------
 2 files changed, 32 insertions(+), 14 deletions(-)
drivers/pci/pci.c +31 -13
Index: linux-2.6/drivers/pci/pci.c
===================================================================
@@ -398,7 +398,8 @@ int (*platform_pci_set_power_state)(stru
 int
 pci_set_power_state(struct pci_dev *dev, pci_power_t state)
 {
-	int pm, need_restore = 0;
+	int pm;
+	bool platform_pm, platform_done = false, need_restore = false;
 	u16 pmcsr, pmc;
 
 	/* bound the state we're entering */
@@ -413,12 +414,35 @@ pci_set_power_state(struct pci_dev *dev,
 	if ((state == PCI_D1 || state == PCI_D2) && pci_no_d1d2(dev))
 		return 0;
 
+	platform_pm = (platform_pci_power_manageable
+			&& platform_pci_power_manageable(dev));
+	if (platform_pm) {
+		/*
+		 * Allow the platform to change the state, for example via ACPI
+		 * _PRx, _PSx and some such, but do not trust it.
+		 */
+		platform_done = !platform_pci_set_power_state(dev, state);
+		if (platform_done)
+			printk(KERN_INFO
+			"PCI: Power state of %s set by the platform\n",
+			pci_name(dev));
+	}
+
 	/* find PCI PM capability in list */
 	pm = pci_find_capability(dev, PCI_CAP_ID_PM);
 
 	/* abort if the device doesn't support PM capabilities */
 	if (!pm)
-		return -EIO;
+		return platform_done ? 0 : -EIO;
+
+	if (platform_pm) {
+		/*
+		 * The platform might have changed the state, make sure that
+		 * the cached value is still valid.
+		 */
+		pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
+		dev->current_state = (pmcsr & PCI_PM_CTRL_STATE_MASK);
+	}
 
 	/* Validate current state:
 	 * Can enter D0 from any state, but if we can only go deeper 
@@ -427,12 +451,12 @@ pci_set_power_state(struct pci_dev *dev,
 	if (state != PCI_D0 && dev->current_state > state) {
 		printk(KERN_ERR "%s(): %s: state=%d, current state=%d\n",
 			__func__, pci_name(dev), state, dev->current_state);
-		return -EINVAL;
+		return platform_done ? 0 : -EINVAL;
 	} else if (dev->current_state == state)
 		return 0;        /* we're already there */
 
 
-	pci_read_config_word(dev,pm + PCI_PM_PMC,&pmc);
+	pci_read_config_word(dev, pm + PCI_PM_PMC, &pmc);
 	if ((pmc & PCI_PM_CAP_VER_MASK) > 3) {
 		printk(KERN_DEBUG
 		       "PCI: %s has unsupported PM cap regs version (%u)\n",
@@ -446,7 +470,8 @@ pci_set_power_state(struct pci_dev *dev,
 	else if (state == PCI_D2 && !(pmc & PCI_PM_CAP_D2))
 		return -EIO;
 
-	pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
+	if (!platform_pm)
+		pci_read_config_word(dev, pm + PCI_PM_CTRL, &pmcsr);
 
 	/* If we're (effectively) in D3, force entire word to 0.
 	 * This doesn't affect PME_Status, disables PME_En, and
@@ -462,7 +487,7 @@ pci_set_power_state(struct pci_dev *dev,
 	case PCI_UNKNOWN: /* Boot-up */
 		if ((pmcsr & PCI_PM_CTRL_STATE_MASK) == PCI_D3hot
 		 && !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET))
-			need_restore = 1;
+			need_restore = true;
 		/* Fall-through: force to D0 */
 	default:
 		pmcsr = 0;
@@ -479,13 +504,6 @@ pci_set_power_state(struct pci_dev *dev,
 	else if (state == PCI_D2 || dev->current_state == PCI_D2)
 		udelay(200);
 
-	/*
-	 * Give firmware a chance to be called, such as ACPI _PRx, _PSx
-	 * Firmware method after native method ?
-	 */
-	if (platform_pci_set_power_state)
-		platform_pci_set_power_state(dev, state);
-
 	dev->current_state = state;
 
 	/* According to section 5.4.1 of the "PCI BUS POWER MANAGEMENT
drivers/pci/pci-acpi.c +1 -1
Index: linux-2.6/drivers/pci/pci-acpi.c
===================================================================
@@ -341,7 +341,7 @@ static int acpi_pci_set_power_state(stru
 		return -ENODEV;
 	/* If the ACPI device has _EJ0, ignore the device */
 	if (ACPI_SUCCESS(acpi_get_handle(handle, "_EJ0", &tmp)))
-		return 0;
+		return -EINVAL;
 
 	switch (state) {
 	case PCI_D0:
reviewer
#13 lore raw
In reply to Rafael J. Wysocki
Quoted 9 lines

I've recently discovered that pci_set_power_state() calls the platform callback intended for changing the state of the device after it has done that using the native PCI mechanism. In my opinion this is not correct, because, for example, if the device is being put into D0 from a low power state, it may require some platform-controlled power resources to be turned on before that (in theory).

Quoted 6 lines

The following series of patches changes this function so that the platform callback is performed first and then the native mechanism is used. Additionally, however, it checks if the devices is power manageable by the platform and only in that case the platform callback is invoked. For this purpose, I needed to add an ACPI function allowing PCI to check the power manageability of given device.

We have 3 cases
1. device has PCI PM only
2. device has ACPI PM only
3. device has both PCI and ACPI PM

For #1 and #2, order is moot.
For #3, how can we be sure that the new order is better than the old order? Is this a theoretical patch, or is there a failure case?

thanks,
-Len

reviewer
#14 lore raw
In reply to Rafael J. Wysocki
Quoted 9 lines

I've recently discovered that pci_set_power_state() calls the platform callback intended for changing the state of the device after it has done that using the native PCI mechanism. In my opinion this is not correct, because, for example, if the device is being put into D0 from a low power state, it may require some platform-controlled power resources to be turned on before that (in theory).

Quoted 6 lines

The following series of patches changes this function so that the platform callback is performed first and then the native mechanism is used. Additionally, however, it checks if the devices is power manageable by the platform and only in that case the platform callback is invoked. For this purpose, I needed to add an ACPI function allowing PCI to check the power manageability of given device.

We have 3 cases
1. device has PCI PM only
2. device has ACPI PM only
3. device has both PCI and ACPI PM

For #1 and #2, order is moot.
For #3, how can we be sure that the new order is better than the old order? Is this a theoretical patch, or is there a failure case?

thanks,
-Len

#15 lore raw
Quoted 15 lines · Len Brown
Quoted 8 lines

I've recently discovered that pci_set_power_state() calls the platform callback intended for changing the state of the device after it has done that using the native PCI mechanism. In my opinion this is not correct, because, for example, if the device is being put into D0 from a low power state, it may require some platform-controlled power resources to be turned on before that (in theory).

Quoted 6 lines

The following series of patches changes this function so that the platform callback is performed first and then the native mechanism is used. Additionally, however, it checks if the devices is power manageable by the platform and only in that case the platform callback is invoked. For this purpose, I needed to add an ACPI function allowing PCI to check the power manageability of given device.

Quoted 8 lines

We have 3 cases
1. device has PCI PM only
2. device has ACPI PM only
3. device has both PCI and ACPI PM

For #1 and #2, order is moot.
For #3, how can we be sure that the new order is better than the old order? Is this a theoretical patch, or is there a failure case?

This was a theoretical patch, but please see the new series posted.

The difference is that in the new series the order is only changed for transitions to D0.

Thanks,
Rafael

#16 lore raw
In reply to Len Brown
Quoted 15 lines · Len Brown
Quoted 8 lines

I've recently discovered that pci_set_power_state() calls the platform callback intended for changing the state of the device after it has done that using the native PCI mechanism. In my opinion this is not correct, because, for example, if the device is being put into D0 from a low power state, it may require some platform-controlled power resources to be turned on before that (in theory).

Quoted 6 lines

The following series of patches changes this function so that the platform callback is performed first and then the native mechanism is used. Additionally, however, it checks if the devices is power manageable by the platform and only in that case the platform callback is invoked. For this purpose, I needed to add an ACPI function allowing PCI to check the power manageability of given device.

Quoted 8 lines

We have 3 cases
1. device has PCI PM only
2. device has ACPI PM only
3. device has both PCI and ACPI PM

For #1 and #2, order is moot.
For #3, how can we be sure that the new order is better than the old order? Is this a theoretical patch, or is there a failure case?

This was a theoretical patch, but please see the new series posted.

The difference is that in the new series the order is only changed for transitions to D0.

Thanks,
Rafael