Skip to contentSkip to conversation

ACPI: Fix handling of power resources during initialization

PATCH New Rafael J. Wysocki 1 participant 7 messages Oct 28, 2010 ACPI

lore
Share & tools
lore
#0 lore raw

cover ACPI: Fix handling of power resources during initialization

Hi,

The following series of patches attempts to fix the problems with the handling of power resources during initialization.

[1/3] allows power resource devices to be initialized by acpi_power driver as
soon as they are found.

[2/3] creates power resource device objects as soon as they appear in the _PRn
output for any devices.

[3/3] uses acpi_power_transition() to set up the initial reference counting of
power resources as appropriate.

I have only tested them on machines without ACPI power resource objects, so all this is kind of theoretical at the moment, but I think the patches are simple enough.

Please test if you can and let me know what's wrong. :-)

Thanks,
Rafael

#1 lore raw

PATCH 1/3 ACPI / PM: Register acpi_power_driver early

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

The ACPI device driver used for handling power resources, acpi_power_driver, creates a struct acpi_power_resource object for each ACPI device representing a power resource. These objects are then used when setting and reading the power states of devices using the corresponding power resources. Unfortunately, acpi_power_driver is registered after acpi_scan_init() that may add devices using the power resources before acpi_power_driver has a chance to create struct acpi_power_resource objects for them (specifically, the power resources may be referred to during the scanning process through acpi_bus_get_power() before they have been initialized).

As the first step towards fixing this issue, move the registration of acpi_power_driver into acpi_scan_init() so that power resource devices can be initialized by it as soon as they have been found in the namespace.

---
 drivers/acpi/bus.c  |    1 -
 drivers/acpi/scan.c |    2 ++
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/acpi/bus.c +0 -1
Index: linux-2.6/drivers/acpi/bus.c
===================================================================
@@ -1025,7 +1025,6 @@ static int __init acpi_init(void)
 
 	acpi_scan_init();
 	acpi_ec_init();
-	acpi_power_init();
 	acpi_sysfs_init();
 	acpi_debugfs_init();
 	acpi_sleep_proc_init();
drivers/acpi/scan.c +2 -0
Index: linux-2.6/drivers/acpi/scan.c
===================================================================
@@ -1543,6 +1543,8 @@ int __init acpi_scan_init(void)
 		printk(KERN_ERR PREFIX "Could not register bus type\n");
 	}
 
+	acpi_power_init();
+
 	/*
 	 * Enumerate devices in the ACPI namespace.
 	 */
#2 lore raw

PATCH 1/3 ACPI / PM: Register acpi_power_driver early

In reply to Rafael J. Wysocki

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

The ACPI device driver used for handling power resources, acpi_power_driver, creates a struct acpi_power_resource object for each ACPI device representing a power resource. These objects are then used when setting and reading the power states of devices using the corresponding power resources. Unfortunately, acpi_power_driver is registered after acpi_scan_init() that may add devices using the power resources before acpi_power_driver has a chance to create struct acpi_power_resource objects for them (specifically, the power resources may be referred to during the scanning process through acpi_bus_get_power() before they have been initialized).

As the first step towards fixing this issue, move the registration of acpi_power_driver into acpi_scan_init() so that power resource devices can be initialized by it as soon as they have been found in the namespace.

---
 drivers/acpi/bus.c  |    1 -
 drivers/acpi/scan.c |    2 ++
 2 files changed, 2 insertions(+), 1 deletion(-)
drivers/acpi/bus.c +0 -1
Index: linux-2.6/drivers/acpi/bus.c
===================================================================
@@ -1025,7 +1025,6 @@ static int __init acpi_init(void)
 
 	acpi_scan_init();
 	acpi_ec_init();
-	acpi_power_init();
 	acpi_sysfs_init();
 	acpi_debugfs_init();
 	acpi_sleep_proc_init();
drivers/acpi/scan.c +2 -0
Index: linux-2.6/drivers/acpi/scan.c
===================================================================
@@ -1543,6 +1543,8 @@ int __init acpi_scan_init(void)
 		printk(KERN_ERR PREFIX "Could not register bus type\n");
 	}
 
+	acpi_power_init();
+
 	/*
 	 * Enumerate devices in the ACPI namespace.
 	 */
#3 lore raw

PATCH 2/3 ACPI / PM: Register power resource devices as soon as they are needed

In reply to Rafael J. Wysocki

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

Depending on the organization of the ACPI namespace, power resource device objects may generally be scanned after the "regular" device objects they are referred from through _PRn. This, in turn, may cause acpi_bus_get_power_flags() to attempt to access them through acpi_bus_get_power() before they are registered (and initialized by acpi_power_driver). In those cases, acpi_bus_get_power() called by acpi_bus_get_power() will fail.

To fix this problem, make acpi_bus_get_power_flags() attempt to register power resource devices as soon as they have been found in the _PRn output for any other devices.

---
 drivers/acpi/scan.c |   20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/acpi/scan.c +20 -0
Index: linux-2.6/drivers/acpi/scan.c
===================================================================
@@ -818,6 +818,8 @@ end:
 	return 0;
 }
 
+static void acpi_bus_add_power_resource(acpi_handle handle);
+
 static int acpi_bus_get_power_flags(struct acpi_device *device)
 {
 	acpi_status status = 0;
@@ -846,8 +848,12 @@ 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;
 			ps->flags.valid = 1;
+			for (j = 0; j < ps->resources.count; j++)
+				acpi_bus_add_power_resource(ps->resources.handles[j]);
 		}
 
 		/* Evaluate "_PSx" to see if we can do explicit sets */
@@ -1304,6 +1310,20 @@ end:
 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
 			  ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING)
 
+static void acpi_bus_add_power_resource(acpi_handle handle)
+{
+	struct acpi_bus_ops ops = {
+		.acpi_op_add = 1,
+		.acpi_op_start = 1,
+	};
+	struct acpi_device *device = NULL;
+
+	acpi_bus_get_device(handle, &device);
+	if (!device)
+		acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
+					ACPI_STA_DEFAULT, &ops);
+}
+
 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
 				    unsigned long long *sta)
 {
#4 lore raw

PATCH 2/3 ACPI / PM: Register power resource devices as soon as they are needed

In reply to Rafael J. Wysocki

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

Depending on the organization of the ACPI namespace, power resource device objects may generally be scanned after the "regular" device objects they are referred from through _PRn. This, in turn, may cause acpi_bus_get_power_flags() to attempt to access them through acpi_bus_get_power() before they are registered (and initialized by acpi_power_driver). In those cases, acpi_bus_get_power() called by acpi_bus_get_power() will fail.

To fix this problem, make acpi_bus_get_power_flags() attempt to register power resource devices as soon as they have been found in the _PRn output for any other devices.

---
 drivers/acpi/scan.c |   20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
drivers/acpi/scan.c +20 -0
Index: linux-2.6/drivers/acpi/scan.c
===================================================================
@@ -818,6 +818,8 @@ end:
 	return 0;
 }
 
+static void acpi_bus_add_power_resource(acpi_handle handle);
+
 static int acpi_bus_get_power_flags(struct acpi_device *device)
 {
 	acpi_status status = 0;
@@ -846,8 +848,12 @@ 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;
 			ps->flags.valid = 1;
+			for (j = 0; j < ps->resources.count; j++)
+				acpi_bus_add_power_resource(ps->resources.handles[j]);
 		}
 
 		/* Evaluate "_PSx" to see if we can do explicit sets */
@@ -1304,6 +1310,20 @@ end:
 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
 			  ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING)
 
+static void acpi_bus_add_power_resource(acpi_handle handle)
+{
+	struct acpi_bus_ops ops = {
+		.acpi_op_add = 1,
+		.acpi_op_start = 1,
+	};
+	struct acpi_device *device = NULL;
+
+	acpi_bus_get_device(handle, &device);
+	if (!device)
+		acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
+					ACPI_STA_DEFAULT, &ops);
+}
+
 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
 				    unsigned long long *sta)
 {
#5 lore raw

PATCH 3/3 ACPI / PM: Fix initial reference counting of power resources

In reply to Rafael J. Wysocki

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

ACPI power resources are only reference counted during device power state changes. The initial device power states have no effect on the reference counting of power resources. This leads to the problem that some power resources may be turned off too early when devices are powered off during system suspend or via runtime PM. Moreover, the state of power resource reference counters after powering a device on may be different than it had been before the device was powered off.

To fix this issue, update power resource reference counting directly during initialization by calling acpi_power_transition() for every device using power resources.

---
 drivers/acpi/scan.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/acpi/scan.c +4 -2
Index: linux-2.6/drivers/acpi/scan.c
===================================================================
@@ -880,8 +880,10 @@ static int acpi_bus_get_power_flags(stru
 
 	/* TBD: System wake support and resource requirements. */
 
-	device->power.state = ACPI_STATE_UNKNOWN;
-	acpi_bus_get_power(device->handle, &(device->power.state));
+	acpi_bus_get_power(device->handle, &device->power.state);
+	/* Update power resources reference counting. */
+	if (device->power.flags.power_resources)
+		acpi_power_transition(device, device->power.state);
 
 	return 0;
 }
#6 lore raw

PATCH 3/3 ACPI / PM: Fix initial reference counting of power resources

In reply to Rafael J. Wysocki

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

ACPI power resources are only reference counted during device power state changes. The initial device power states have no effect on the reference counting of power resources. This leads to the problem that some power resources may be turned off too early when devices are powered off during system suspend or via runtime PM. Moreover, the state of power resource reference counters after powering a device on may be different than it had been before the device was powered off.

To fix this issue, update power resource reference counting directly during initialization by calling acpi_power_transition() for every device using power resources.

---
 drivers/acpi/scan.c |    6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/acpi/scan.c +4 -2
Index: linux-2.6/drivers/acpi/scan.c
===================================================================
@@ -880,8 +880,10 @@ static int acpi_bus_get_power_flags(stru
 
 	/* TBD: System wake support and resource requirements. */
 
-	device->power.state = ACPI_STATE_UNKNOWN;
-	acpi_bus_get_power(device->handle, &(device->power.state));
+	acpi_bus_get_power(device->handle, &device->power.state);
+	/* Update power resources reference counting. */
+	if (device->power.flags.power_resources)
+		acpi_power_transition(device, device->power.state);
 
 	return 0;
 }