Skip to contentSkip to conversation

ACPI / PM: Rework the handling of devices depending on power resources

PATCH New Rafael J. Wysocki 1 participant 4 messages Dec 29, 2012 – Dec 31, 2012

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

PATCH 1/1 ACPI / PM: Rework the handling of devices depending on power resources

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

Commit 0090def6 (ACPI: Add interface to register/unregister device to/from power resources) made it possible to indicate to the ACPI core that if the given device depends on any power resources, then it should be resumed as soon as all of the power resources required by it to transition to the D0 power state have been turned on.

Unfortunately, however, this was a mistake, because all devices depending on power resources should be treated this way (i.e. they should be resumed when all power resources required by their D0 state have been turned on) and the ACPI core can figure out by itself which (physical) devices depend on what power resources.

For this reason, replace the code added by commit 0090def6 with a new, much more straightforward, mechanism that will be used internally by the ACPI core and remove all references to that code from kernel subsystems using ACPI.

---
 drivers/acpi/internal.h   |    2 
 drivers/acpi/power.c      |  207 +++++++++++++++-------------------------------
 drivers/acpi/scan.c       |   26 ++++-
 drivers/ata/libata-acpi.c |    4 
 drivers/pci/pci-acpi.c    |    2 
 include/acpi/acpi_bus.h   |    2 
 6 files changed, 93 insertions(+), 150 deletions(-)
drivers/acpi/scan.c +22 -4
Index: linux/drivers/acpi/scan.c
===================================================================
@@ -755,6 +755,14 @@ static void acpi_device_unregister(struc
 
 	acpi_device_remove_files(device);
 	device_unregister(&device->dev);
+
+	/*
+	 * Remove the device from the dependent devices lists of all of its
+	 * power resources, drop the reference counts of all power resources it
+	 * depends on and turn off the ones that have no more references.
+	 */
+	acpi_power_remove_device(device);
+	acpi_power_transition(device, ACPI_STATE_D3_COLD);
 }
 
 /* --------------------------------------------------------------------------
@@ -1053,6 +1061,19 @@ static void acpi_bus_get_wakeup_device_f
 
 static void acpi_bus_add_power_resource(acpi_handle handle);
 
+static void acpi_bus_update_power_resources(struct acpi_device *device,
+					    struct acpi_device_power_state *ps)
+{
+	int j;
+
+	for (j = 0; j < ps->resources.count; j++) {
+		acpi_handle rhandle = ps->resources.handles[j];
+
+		acpi_bus_add_power_resource(rhandle);
+		acpi_power_add_dependent(rhandle, device);
+	}
+}
+
 static int acpi_bus_get_power_flags(struct acpi_device *device)
 {
 	acpi_status status = 0;
@@ -1081,11 +1102,8 @@ static int acpi_bus_get_power_flags(stru
 		acpi_evaluate_reference(device->handle, object_name, NULL,
 					&ps->resources);
 		if (ps->resources.count) {
-			int j;
-
 			device->power.flags.power_resources = 1;
-			for (j = 0; j < ps->resources.count; j++)
-				acpi_bus_add_power_resource(ps->resources.handles[j]);
+			acpi_bus_update_power_resources(device, ps);
 		}
 
 		/* Evaluate "_PSx" to see if we can do explicit sets */
drivers/acpi/power.c +69 -138

292 lines not on this page · raw message

drivers/acpi/internal.h +2 -0
Index: linux/drivers/acpi/internal.h
===================================================================
@@ -42,6 +42,8 @@ static inline void acpi_debugfs_init(voi
                                   Power Resource
    -------------------------------------------------------------------------- */
 int acpi_power_init(void);
+void acpi_power_add_dependent(acpi_handle rhandle, struct acpi_device *adev);
+void acpi_power_remove_device(struct acpi_device *adev);
 int acpi_device_sleep_wake(struct acpi_device *dev,
                            int enable, int sleep_state, int dev_state);
 int acpi_power_get_inferred_state(struct acpi_device *device, int *state);
drivers/pci/pci-acpi.c +0 -2
Index: linux/drivers/pci/pci-acpi.c
===================================================================
@@ -330,7 +330,6 @@ static void pci_acpi_setup(struct device
 		acpi_pci_irq_add_prt(handle, pci_domain_nr(pci_dev->bus), bus);
 	}
 
-	acpi_power_resource_register_device(dev, handle);
 	if (acpi_bus_get_device(handle, &adev) || !adev->wakeup.flags.valid)
 		return;
 
@@ -353,7 +352,6 @@ static void pci_acpi_cleanup(struct devi
 		device_set_run_wake(dev, false);
 		pci_acpi_remove_pm_notifier(adev);
 	}
-	acpi_power_resource_unregister_device(dev, handle);
 
 	if (pci_dev->subordinate)
 		acpi_pci_irq_del_prt(pci_domain_nr(pci_dev->bus),
include/acpi/acpi_bus.h +0 -2
Index: linux/include/acpi/acpi_bus.h
===================================================================
@@ -377,8 +377,6 @@ static inline bool acpi_bus_can_wakeup(a
 }
 #endif /* !CONFIG_PM */
 
-int acpi_power_resource_register_device(struct device *dev, acpi_handle handle);
-void acpi_power_resource_unregister_device(struct device *dev, 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);
drivers/ata/libata-acpi.c +0 -4
Index: linux/drivers/ata/libata-acpi.c
===================================================================
@@ -1036,8 +1036,6 @@ static void ata_acpi_register_power_reso
 		return;
 
 	device = &sdev->sdev_gendev;
-
-	acpi_power_resource_register_device(device, handle);
 }
 
 static void ata_acpi_unregister_power_resource(struct ata_device *dev)
@@ -1051,8 +1049,6 @@ static void ata_acpi_unregister_power_re
 		return;
 
 	device = &sdev->sdev_gendev;
-
-	acpi_power_resource_unregister_device(device, handle);
 }
 
 void ata_acpi_bind(struct ata_device *dev)
#1 lore raw

PATCH 1/1 ACPI / PM: Rework the handling of devices depending on power resources

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

Commit 0090def6 (ACPI: Add interface to register/unregister device to/from power resources) made it possible to indicate to the ACPI core that if the given device depends on any power resources, then it should be resumed as soon as all of the power resources required by it to transition to the D0 power state have been turned on.

Unfortunately, however, this was a mistake, because all devices depending on power resources should be treated this way (i.e. they should be resumed when all power resources required by their D0 state have been turned on) and the ACPI core can figure out by itself which (physical) devices depend on what power resources.

For this reason, replace the code added by commit 0090def6 with a new, much more straightforward, mechanism that will be used internally by the ACPI core and remove all references to that code from kernel subsystems using ACPI.

---

The update is necessary to prevent device nodes from being added multiple times to the list of dependent devices of a given power resource. To be precise, acpi_power_add_dependent() needs to check if there is an entry in resource->dependent for the given device and only add a new one if that's not the case.

Thanks,
Rafael

---
 drivers/acpi/internal.h   |    2 
 drivers/acpi/power.c      |  210 ++++++++++++++++------------------------------
 drivers/acpi/scan.c       |   26 ++++-
 drivers/ata/libata-acpi.c |    4 
 drivers/pci/pci-acpi.c    |    2 
 include/acpi/acpi_bus.h   |    2 
 6 files changed, 98 insertions(+), 148 deletions(-)
drivers/acpi/scan.c +22 -4
Index: linux/drivers/acpi/scan.c
===================================================================
@@ -755,6 +755,14 @@ static void acpi_device_unregister(struc
 
 	acpi_device_remove_files(device);
 	device_unregister(&device->dev);
+
+	/*
+	 * Remove the device from the dependent devices lists of all of its
+	 * power resources, drop the reference counts of all power resources it
+	 * depends on and turn off the ones that have no more references.
+	 */
+	acpi_power_remove_device(device);
+	acpi_power_transition(device, ACPI_STATE_D3_COLD);
 }
 
 /* --------------------------------------------------------------------------
@@ -1053,6 +1061,19 @@ static void acpi_bus_get_wakeup_device_f
 
 static void acpi_bus_add_power_resource(acpi_handle handle);
 
+static void acpi_bus_update_power_resources(struct acpi_device *device,
+					    struct acpi_device_power_state *ps)
+{
+	int j;
+
+	for (j = 0; j < ps->resources.count; j++) {
+		acpi_handle rhandle = ps->resources.handles[j];
+
+		acpi_bus_add_power_resource(rhandle);
+		acpi_power_add_dependent(rhandle, device);
+	}
+}
+
 static int acpi_bus_get_power_flags(struct acpi_device *device)
 {
 	acpi_status status = 0;
@@ -1081,11 +1102,8 @@ static int acpi_bus_get_power_flags(stru
 		acpi_evaluate_reference(device->handle, object_name, NULL,
 					&ps->resources);
 		if (ps->resources.count) {
-			int j;
-
 			device->power.flags.power_resources = 1;
-			for (j = 0; j < ps->resources.count; j++)
-				acpi_bus_add_power_resource(ps->resources.handles[j]);
+			acpi_bus_update_power_resources(device, ps);
 		}
 
 		/* Evaluate "_PSx" to see if we can do explicit sets */
drivers/acpi/power.c +74 -136

297 lines not on this page · raw message

drivers/acpi/internal.h +2 -0
Index: linux/drivers/acpi/internal.h
===================================================================
@@ -42,6 +42,8 @@ static inline void acpi_debugfs_init(voi
                                   Power Resource
    -------------------------------------------------------------------------- */
 int acpi_power_init(void);
+void acpi_power_add_dependent(acpi_handle rhandle, struct acpi_device *adev);
+void acpi_power_remove_device(struct acpi_device *adev);
 int acpi_device_sleep_wake(struct acpi_device *dev,
                            int enable, int sleep_state, int dev_state);
 int acpi_power_get_inferred_state(struct acpi_device *device, int *state);
drivers/pci/pci-acpi.c +0 -2
Index: linux/drivers/pci/pci-acpi.c
===================================================================
@@ -330,7 +330,6 @@ static void pci_acpi_setup(struct device
 		acpi_pci_irq_add_prt(handle, pci_domain_nr(pci_dev->bus), bus);
 	}
 
-	acpi_power_resource_register_device(dev, handle);
 	if (acpi_bus_get_device(handle, &adev) || !adev->wakeup.flags.valid)
 		return;
 
@@ -353,7 +352,6 @@ static void pci_acpi_cleanup(struct devi
 		device_set_run_wake(dev, false);
 		pci_acpi_remove_pm_notifier(adev);
 	}
-	acpi_power_resource_unregister_device(dev, handle);
 
 	if (pci_dev->subordinate)
 		acpi_pci_irq_del_prt(pci_domain_nr(pci_dev->bus),
include/acpi/acpi_bus.h +0 -2
Index: linux/include/acpi/acpi_bus.h
===================================================================
@@ -377,8 +377,6 @@ static inline bool acpi_bus_can_wakeup(a
 }
 #endif /* !CONFIG_PM */
 
-int acpi_power_resource_register_device(struct device *dev, acpi_handle handle);
-void acpi_power_resource_unregister_device(struct device *dev, 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);
drivers/ata/libata-acpi.c +0 -4
Index: linux/drivers/ata/libata-acpi.c
===================================================================
@@ -1036,8 +1036,6 @@ static void ata_acpi_register_power_reso
 		return;
 
 	device = &sdev->sdev_gendev;
-
-	acpi_power_resource_register_device(device, handle);
 }
 
 static void ata_acpi_unregister_power_resource(struct ata_device *dev)
@@ -1051,8 +1049,6 @@ static void ata_acpi_unregister_power_re
 		return;
 
 	device = &sdev->sdev_gendev;
-
-	acpi_power_resource_unregister_device(device, handle);
 }
 
 void ata_acpi_bind(struct ata_device *dev)
#2 lore raw

PATCH 1/1 ACPI / PM: Rework the handling of devices depending on power resources

From: Rafael J. Wysocki <[email protected]>
Subject: ACPI / PM: Rework the handling of devices depending on power resources

Commit 0090def6 (ACPI: Add interface to register/unregister device to/from power resources) made it possible to indicate to the ACPI core that if the given device depends on any power resources, then it should be resumed as soon as all of the power resources required by it to transition to the D0 power state have been turned on.

Unfortunately, however, this was a mistake, because all devices depending on power resources should be treated this way (i.e. they should be resumed when all power resources required by their D0 state have been turned on) and the ACPI core can figure out by itself which (physical) devices depend on what power resources.

For this reason, replace the code added by commit 0090def6 with a new, much more straightforward, mechanism that will be used internally by the ACPI core and remove all references to that code from kernel subsystems using ACPI.

---

Second update: add the missing initialization of resource->dependent in acpi_power_add().

Thanks,
Rafael

---
 drivers/acpi/internal.h   |    2 
 drivers/acpi/power.c      |  211 ++++++++++++++++------------------------------
 drivers/acpi/scan.c       |   26 ++++-
 drivers/ata/libata-acpi.c |    4 
 drivers/pci/pci-acpi.c    |    2 
 include/acpi/acpi_bus.h   |    2 
 6 files changed, 99 insertions(+), 148 deletions(-)
drivers/acpi/scan.c +22 -4
Index: linux/drivers/acpi/scan.c
===================================================================
@@ -755,6 +755,14 @@ static void acpi_device_unregister(struc
 
 	acpi_device_remove_files(device);
 	device_unregister(&device->dev);
+
+	/*
+	 * Remove the device from the dependent devices lists of all of its
+	 * power resources, drop the reference counts of all power resources it
+	 * depends on and turn off the ones that have no more references.
+	 */
+	acpi_power_remove_device(device);
+	acpi_power_transition(device, ACPI_STATE_D3_COLD);
 }
 
 /* --------------------------------------------------------------------------
@@ -1053,6 +1061,19 @@ static void acpi_bus_get_wakeup_device_f
 
 static void acpi_bus_add_power_resource(acpi_handle handle);
 
+static void acpi_bus_update_power_resources(struct acpi_device *device,
+					    struct acpi_device_power_state *ps)
+{
+	int j;
+
+	for (j = 0; j < ps->resources.count; j++) {
+		acpi_handle rhandle = ps->resources.handles[j];
+
+		acpi_bus_add_power_resource(rhandle);
+		acpi_power_add_dependent(rhandle, device);
+	}
+}
+
 static int acpi_bus_get_power_flags(struct acpi_device *device)
 {
 	acpi_status status = 0;
@@ -1081,11 +1102,8 @@ static int acpi_bus_get_power_flags(stru
 		acpi_evaluate_reference(device->handle, object_name, NULL,
 					&ps->resources);
 		if (ps->resources.count) {
-			int j;
-
 			device->power.flags.power_resources = 1;
-			for (j = 0; j < ps->resources.count; j++)
-				acpi_bus_add_power_resource(ps->resources.handles[j]);
+			acpi_bus_update_power_resources(device, ps);
 		}
 
 		/* Evaluate "_PSx" to see if we can do explicit sets */
drivers/acpi/power.c +75 -136

298 lines not on this page · raw message

drivers/acpi/internal.h +2 -0
Index: linux/drivers/acpi/internal.h
===================================================================
@@ -42,6 +42,8 @@ static inline void acpi_debugfs_init(voi
                                   Power Resource
    -------------------------------------------------------------------------- */
 int acpi_power_init(void);
+void acpi_power_add_dependent(acpi_handle rhandle, struct acpi_device *adev);
+void acpi_power_remove_device(struct acpi_device *adev);
 int acpi_device_sleep_wake(struct acpi_device *dev,
                            int enable, int sleep_state, int dev_state);
 int acpi_power_get_inferred_state(struct acpi_device *device, int *state);
drivers/pci/pci-acpi.c +0 -2
Index: linux/drivers/pci/pci-acpi.c
===================================================================
@@ -330,7 +330,6 @@ static void pci_acpi_setup(struct device
 		acpi_pci_irq_add_prt(handle, pci_domain_nr(pci_dev->bus), bus);
 	}
 
-	acpi_power_resource_register_device(dev, handle);
 	if (acpi_bus_get_device(handle, &adev) || !adev->wakeup.flags.valid)
 		return;
 
@@ -353,7 +352,6 @@ static void pci_acpi_cleanup(struct devi
 		device_set_run_wake(dev, false);
 		pci_acpi_remove_pm_notifier(adev);
 	}
-	acpi_power_resource_unregister_device(dev, handle);
 
 	if (pci_dev->subordinate)
 		acpi_pci_irq_del_prt(pci_domain_nr(pci_dev->bus),
include/acpi/acpi_bus.h +0 -2
Index: linux/include/acpi/acpi_bus.h
===================================================================
@@ -377,8 +377,6 @@ static inline bool acpi_bus_can_wakeup(a
 }
 #endif /* !CONFIG_PM */
 
-int acpi_power_resource_register_device(struct device *dev, acpi_handle handle);
-void acpi_power_resource_unregister_device(struct device *dev, 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);
drivers/ata/libata-acpi.c +0 -4
Index: linux/drivers/ata/libata-acpi.c
===================================================================
@@ -1036,8 +1036,6 @@ static void ata_acpi_register_power_reso
 		return;
 
 	device = &sdev->sdev_gendev;
-
-	acpi_power_resource_register_device(device, handle);
 }
 
 static void ata_acpi_unregister_power_resource(struct ata_device *dev)
@@ -1051,8 +1049,6 @@ static void ata_acpi_unregister_power_re
 		return;
 
 	device = &sdev->sdev_gendev;
-
-	acpi_power_resource_unregister_device(device, handle);
 }
 
 void ata_acpi_bind(struct ata_device *dev)
#3 lore raw

PATCH 1/1 ACPI / PM: Rework the handling of devices depending on power resources

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

Commit 0090def6 (ACPI: Add interface to register/unregister device to/from power resources) made it possible to indicate to the ACPI core that if the given device depends on any power resources, then it should be resumed as soon as all of the power resources required by it to transition to the D0 power state have been turned on.

Unfortunately, however, this was a mistake, because all devices depending on power resources should be treated this way (i.e. they should be resumed when all power resources required by their D0 state have been turned on) and the ACPI core can figure out by itself which (physical) devices depend on what power resources.

For this reason, replace the code added by commit 0090def6 with a new, much more straightforward, mechanism that will be used internally by the ACPI core and remove all references to that code from kernel subsystems using ACPI.

---

One more update, hopefully the last one.

The previous version added devices to power resources' "dependent devices" lists too early and they might be left on those lists inadvertently if there were problems with registering them. To fix this make the addition of devices to those lists symmetrical with respect to the removal (this also simplifies the patch slightly).

Thanks,
Rafael

---
 drivers/acpi/internal.h   |    1 
 drivers/acpi/power.c      |  215 ++++++++++++++++------------------------------
 drivers/acpi/scan.c       |    7 +
 drivers/ata/libata-acpi.c |    4 
 drivers/pci/pci-acpi.c    |    2 
 include/acpi/acpi_bus.h   |    2 
 6 files changed, 87 insertions(+), 144 deletions(-)
drivers/acpi/scan.c +7 -0
Index: linux/drivers/acpi/scan.c
===================================================================
@@ -725,6 +725,7 @@ static int acpi_device_register(struct a
 		dev_err(&device->dev, "Error registering device\n");
 		goto end;
 	}
+	acpi_power_add_remove_device(device, true);
 
 	result = acpi_device_setup_files(device);
 	if (result)
@@ -754,7 +755,13 @@ static void acpi_device_unregister(struc
 	acpi_detach_data(device->handle, acpi_bus_data_handler);
 
 	acpi_device_remove_files(device);
+	acpi_power_add_remove_device(device, false);
 	device_unregister(&device->dev);
+	/*
+	 * Drop the reference counts of all power resources the device depends
+	 * on and turn off the ones that have no more references.
+	 */
+	acpi_power_transition(device, ACPI_STATE_D3_COLD);
 }
 
 /* --------------------------------------------------------------------------
drivers/acpi/power.c +79 -136

302 lines not on this page · raw message

drivers/acpi/internal.h +1 -0
Index: linux/drivers/acpi/internal.h
===================================================================
@@ -42,6 +42,7 @@ static inline void acpi_debugfs_init(voi
                                   Power Resource
    -------------------------------------------------------------------------- */
 int acpi_power_init(void);
+void acpi_power_add_remove_device(struct acpi_device *adev, bool add);
 int acpi_device_sleep_wake(struct acpi_device *dev,
                            int enable, int sleep_state, int dev_state);
 int acpi_power_get_inferred_state(struct acpi_device *device, int *state);
drivers/pci/pci-acpi.c +0 -2
Index: linux/drivers/pci/pci-acpi.c
===================================================================
@@ -330,7 +330,6 @@ static void pci_acpi_setup(struct device
 		acpi_pci_irq_add_prt(handle, pci_domain_nr(pci_dev->bus), bus);
 	}
 
-	acpi_power_resource_register_device(dev, handle);
 	if (acpi_bus_get_device(handle, &adev) || !adev->wakeup.flags.valid)
 		return;
 
@@ -353,7 +352,6 @@ static void pci_acpi_cleanup(struct devi
 		device_set_run_wake(dev, false);
 		pci_acpi_remove_pm_notifier(adev);
 	}
-	acpi_power_resource_unregister_device(dev, handle);
 
 	if (pci_dev->subordinate)
 		acpi_pci_irq_del_prt(pci_domain_nr(pci_dev->bus),
include/acpi/acpi_bus.h +0 -2
Index: linux/include/acpi/acpi_bus.h
===================================================================
@@ -377,8 +377,6 @@ static inline bool acpi_bus_can_wakeup(a
 }
 #endif /* !CONFIG_PM */
 
-int acpi_power_resource_register_device(struct device *dev, acpi_handle handle);
-void acpi_power_resource_unregister_device(struct device *dev, 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);
drivers/ata/libata-acpi.c +0 -4
Index: linux/drivers/ata/libata-acpi.c
===================================================================
@@ -1036,8 +1036,6 @@ static void ata_acpi_register_power_reso
 		return;
 
 	device = &sdev->sdev_gendev;
-
-	acpi_power_resource_register_device(device, handle);
 }
 
 static void ata_acpi_unregister_power_resource(struct ata_device *dev)
@@ -1051,8 +1049,6 @@ static void ata_acpi_unregister_power_re
 		return;
 
 	device = &sdev->sdev_gendev;
-
-	acpi_power_resource_unregister_device(device, handle);
 }
 
 void ata_acpi_bind(struct ata_device *dev)