Skip to contentSkip to conversation

PM: Introduce core framework for run-time PM of I/O devices (rev. 10)

PATCH New Rafael J. Wysocki 2 participants 10 messages Jul 22, 2009 – Aug 3, 2009

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

PATCH 1/1 PM: Introduce core framework for run-time PM of I/O devices (rev. 10)

Hi,

Below is rev. 10 of the run-time PM framework patch.

The most visible change from the previous one is that the documentation is now present again. :-)

Second, there's a check in pm_runtime_add() that will only increment the parent's counter of 'active' children if the device current status is not 'suspended'.

Comments welcome.

Best,
Rafael

---
From: Rafael J. Wysocki <[email protected]>
Subject: PM: Introduce core framework for run-time PM of I/O devices (rev. 10)

Introduce a core framework for run-time power management of I/O devices. Add device run-time PM fields to 'struct dev_pm_info' and device run-time PM callbacks to 'struct dev_pm_ops'. Introduce a run-time PM workqueue and define some device run-time PM helper functions at the core level.

---
 Documentation/power/runtime_pm.txt |  341 +++++++++++++
 drivers/base/dd.c                  |    7 
 drivers/base/power/Makefile        |    1 
 drivers/base/power/main.c          |   22 
 drivers/base/power/power.h         |   11 
 drivers/base/power/runtime.c       |  959 +++++++++++++++++++++++++++++++++++++
 include/linux/pm.h                 |  102 +++
 include/linux/pm_runtime.h         |  113 ++++
 kernel/power/Kconfig               |   14 
 kernel/power/main.c                |   17 
 10 files changed, 1576 insertions(+), 11 deletions(-)
kernel/power/Kconfig +14 -0
Index: linux-2.6/kernel/power/Kconfig
===================================================================
@@ -208,3 +208,17 @@ config APM_EMULATION
 	  random kernel OOPSes or reboots that don't seem to be related to
 	  anything, try disabling/enabling this option (or disabling/enabling
 	  APM in your BIOS).
+
+config PM_RUNTIME
+	bool "Run-time PM core functionality"
+	depends on PM
+	---help---
+	  Enable functionality allowing I/O devices to be put into energy-saving
+	  (low power) states at run time (or autosuspended) after a specified
+	  period of inactivity and woken up in response to a hardware-generated
+	  wake-up event or a driver's request.
+
+	  Hardware support is generally required for this functionality to work
+	  and the bus type drivers of the buses the devices are on are
+	  responsible for the actual handling of the autosuspend requests and
+	  wake-up events.
kernel/power/main.c +17 -0
Index: linux-2.6/kernel/power/main.c
===================================================================
@@ -11,6 +11,7 @@
 #include <linux/kobject.h>
 #include <linux/string.h>
 #include <linux/resume-trace.h>
+#include <linux/workqueue.h>
 
 #include "power.h"
 
@@ -217,8 +218,24 @@ static struct attribute_group attr_group
 	.attrs = g,
 };
 
+#ifdef CONFIG_PM_RUNTIME
+struct workqueue_struct *pm_wq;
+
+static int __init pm_start_workqueue(void)
+{
+	pm_wq = create_freezeable_workqueue("pm");
+
+	return pm_wq ? 0 : -ENOMEM;
+}
+#else
+static inline int pm_start_workqueue(void) { return 0; }
+#endif
+
 static int __init pm_init(void)
 {
+	int error = pm_start_workqueue();
+	if (error)
+		return error;
 	power_kobj = kobject_create_and_add("power", NULL);
 	if (!power_kobj)
 		return -ENOMEM;
include/linux/pm.h +99 -3

131 lines not on this page · raw message

drivers/base/power/Makefile +1 -0
Index: linux-2.6/drivers/base/power/Makefile
===================================================================
@@ -1,5 +1,6 @@
 obj-$(CONFIG_PM)	+= sysfs.o
 obj-$(CONFIG_PM_SLEEP)	+= main.o
+obj-$(CONFIG_PM_RUNTIME)	+= runtime.o
 obj-$(CONFIG_PM_TRACE_RTC)	+= trace.o
 
 ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG
drivers/base/power/runtime.c new file +959 -0

959 lines not on this page · raw message

include/linux/pm_runtime.h new file +113 -0

113 lines not on this page · raw message

drivers/base/power/main.c +21 -1
Index: linux-2.6/drivers/base/power/main.c
===================================================================
@@ -21,6 +21,7 @@
 #include <linux/kallsyms.h>
 #include <linux/mutex.h>
 #include <linux/pm.h>
+#include <linux/pm_runtime.h>
 #include <linux/resume-trace.h>
 #include <linux/rwsem.h>
 #include <linux/interrupt.h>
@@ -49,6 +50,16 @@ static DEFINE_MUTEX(dpm_list_mtx);
 static bool transition_started;
 
 /**
+ * device_pm_init - Initialize the PM-related part of a device object
+ * @dev: Device object to initialize.
+ */
+void device_pm_init(struct device *dev)
+{
+	dev->power.status = DPM_ON;
+	pm_runtime_init(dev);
+}
+
+/**
  *	device_pm_lock - lock the list of active devices used by the PM core
  */
 void device_pm_lock(void)
@@ -89,6 +100,8 @@ void device_pm_add(struct device *dev)
 
 	list_add_tail(&dev->power.entry, &dpm_list);
 	mutex_unlock(&dpm_list_mtx);
+
+	pm_runtime_add(dev);
 }
 
 /**
@@ -105,6 +118,8 @@ void device_pm_remove(struct device *dev
 	mutex_lock(&dpm_list_mtx);
 	list_del_init(&dev->power.entry);
 	mutex_unlock(&dpm_list_mtx);
+
+	pm_runtime_remove(dev);
 }
 
 /**
@@ -512,6 +527,7 @@ static void dpm_complete(pm_message_t st
 			mutex_unlock(&dpm_list_mtx);
 
 			device_complete(dev, state);
+			pm_runtime_enable(dev);
 
 			mutex_lock(&dpm_list_mtx);
 		}
@@ -757,11 +773,15 @@ static int dpm_prepare(pm_message_t stat
 		dev->power.status = DPM_PREPARING;
 		mutex_unlock(&dpm_list_mtx);
 
-		error = device_prepare(dev, state);
+		if (pm_runtime_disable(dev) && device_may_wakeup(dev))
+			error = -EBUSY;
+		else
+			error = device_prepare(dev, state);
 
 		mutex_lock(&dpm_list_mtx);
 		if (error) {
 			dev->power.status = DPM_ON;
+			pm_runtime_enable(dev);
 			if (error == -EAGAIN) {
 				put_device(dev);
 				error = 0;
drivers/base/dd.c +7 -0
Index: linux-2.6/drivers/base/dd.c
===================================================================
@@ -23,6 +23,7 @@
 #include <linux/kthread.h>
 #include <linux/wait.h>
 #include <linux/async.h>
+#include <linux/pm_runtime.h>
 
 #include "base.h"
 #include "power/power.h"
@@ -202,7 +203,9 @@ int driver_probe_device(struct device_dr
 	pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
 		 drv->bus->name, __func__, dev_name(dev), drv->name);
 
+	pm_runtime_get_noresume(dev);
 	ret = really_probe(dev, drv);
+	pm_runtime_put_noidle(dev);
 
 	return ret;
 }
@@ -306,6 +309,8 @@ static void __device_release_driver(stru
 
 	drv = dev->driver;
 	if (drv) {
+		pm_runtime_disable(dev);
+
 		driver_sysfs_remove(dev);
 
 		if (dev->bus)
@@ -324,6 +329,8 @@ static void __device_release_driver(stru
 			blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
 						     BUS_NOTIFY_UNBOUND_DRIVER,
 						     dev);
+
+		pm_runtime_enable(dev);
 	}
 }
 
drivers/base/power/power.h +4 -7
Index: linux-2.6/drivers/base/power/power.h
===================================================================
@@ -1,8 +1,3 @@
-static inline void device_pm_init(struct device *dev)
-{
-	dev->power.status = DPM_ON;
-}
-
 #ifdef CONFIG_PM_SLEEP
 
 /*
@@ -16,14 +11,16 @@ static inline struct device *to_device(s
 	return container_of(entry, struct device, power.entry);
 }
 
+extern void device_pm_init(struct device *dev);
 extern void device_pm_add(struct device *);
 extern void device_pm_remove(struct device *);
 extern void device_pm_move_before(struct device *, struct device *);
 extern void device_pm_move_after(struct device *, struct device *);
 extern void device_pm_move_last(struct device *);
 
-#else /* CONFIG_PM_SLEEP */
+#else /* !CONFIG_PM_SLEEP */
 
+static inline void device_pm_init(struct device *dev) {}
 static inline void device_pm_add(struct device *dev) {}
 static inline void device_pm_remove(struct device *dev) {}
 static inline void device_pm_move_before(struct device *deva,
@@ -32,7 +29,7 @@ static inline void device_pm_move_after(
 					struct device *devb) {}
 static inline void device_pm_move_last(struct device *dev) {}
 
-#endif
+#endif /* !CONFIG_PM_SLEEP */
 
 #ifdef CONFIG_PM
 
Documentation/power/runtime_pm.txt new file +341 -0

341 lines not on this page · raw message

#1 lore raw

PATCH 1/1 PM: Introduce core framework for run-time PM of I/O devices (rev. 11)

Quoted 4 lines · Rafael J. Wysocki

Hi,

Below is rev. 10 of the run-time PM framework patch.

Quoted 8 lines

The most visible change from the previous one is that the documentation is now present again. :-)

Second, there's a check in pm_runtime_add() that will only increment the parent's counter of 'active' children if the device current status is not 'suspended'.

Comments welcome.

One more fix.

I noticed that it might be invalid to increment the parent's power.child_count in pm_runtime_add() even with the check added previously. For this reason I changed the initial power.runtime_status to RPM_SUSPENDED, which made pm_runtime_add() redundant and which should allow us to avoid the problem with increasing power.child_count at a wrong time.

Best,
Rafael

---
From: Rafael J. Wysocki <[email protected]>
Subject: PM: Introduce core framework for run-time PM of I/O devices (rev. 11)

Introduce a core framework for run-time power management of I/O devices. Add device run-time PM fields to 'struct dev_pm_info' and device run-time PM callbacks to 'struct dev_pm_ops'. Introduce a run-time PM workqueue and define some device run-time PM helper functions at the core level. Document all these things.

---
 Documentation/power/runtime_pm.txt |  340 +++++++++++++
 drivers/base/dd.c                  |    7 
 drivers/base/power/Makefile        |    1 
 drivers/base/power/main.c          |   20 
 drivers/base/power/power.h         |   11 
 drivers/base/power/runtime.c       |  946 +++++++++++++++++++++++++++++++++++++
 include/linux/pm.h                 |  102 +++
 include/linux/pm_runtime.h         |  111 ++++
 kernel/power/Kconfig               |   14 
 kernel/power/main.c                |   17 
 10 files changed, 1558 insertions(+), 11 deletions(-)
kernel/power/Kconfig +14 -0
Index: linux-2.6/kernel/power/Kconfig
===================================================================
@@ -208,3 +208,17 @@ config APM_EMULATION
 	  random kernel OOPSes or reboots that don't seem to be related to
 	  anything, try disabling/enabling this option (or disabling/enabling
 	  APM in your BIOS).
+
+config PM_RUNTIME
+	bool "Run-time PM core functionality"
+	depends on PM
+	---help---
+	  Enable functionality allowing I/O devices to be put into energy-saving
+	  (low power) states at run time (or autosuspended) after a specified
+	  period of inactivity and woken up in response to a hardware-generated
+	  wake-up event or a driver's request.
+
+	  Hardware support is generally required for this functionality to work
+	  and the bus type drivers of the buses the devices are on are
+	  responsible for the actual handling of the autosuspend requests and
+	  wake-up events.
kernel/power/main.c +17 -0
Index: linux-2.6/kernel/power/main.c
===================================================================
@@ -11,6 +11,7 @@
 #include <linux/kobject.h>
 #include <linux/string.h>
 #include <linux/resume-trace.h>
+#include <linux/workqueue.h>
 
 #include "power.h"
 
@@ -217,8 +218,24 @@ static struct attribute_group attr_group
 	.attrs = g,
 };
 
+#ifdef CONFIG_PM_RUNTIME
+struct workqueue_struct *pm_wq;
+
+static int __init pm_start_workqueue(void)
+{
+	pm_wq = create_freezeable_workqueue("pm");
+
+	return pm_wq ? 0 : -ENOMEM;
+}
+#else
+static inline int pm_start_workqueue(void) { return 0; }
+#endif
+
 static int __init pm_init(void)
 {
+	int error = pm_start_workqueue();
+	if (error)
+		return error;
 	power_kobj = kobject_create_and_add("power", NULL);
 	if (!power_kobj)
 		return -ENOMEM;
include/linux/pm.h +99 -3

131 lines not on this page · raw message

drivers/base/power/Makefile +1 -0
Index: linux-2.6/drivers/base/power/Makefile
===================================================================
@@ -1,5 +1,6 @@
 obj-$(CONFIG_PM)	+= sysfs.o
 obj-$(CONFIG_PM_SLEEP)	+= main.o
+obj-$(CONFIG_PM_RUNTIME)	+= runtime.o
 obj-$(CONFIG_PM_TRACE_RTC)	+= trace.o
 
 ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG
drivers/base/power/runtime.c new file +946 -0

946 lines not on this page · raw message

include/linux/pm_runtime.h new file +111 -0

111 lines not on this page · raw message

drivers/base/power/main.c +19 -1
Index: linux-2.6/drivers/base/power/main.c
===================================================================
@@ -21,6 +21,7 @@
 #include <linux/kallsyms.h>
 #include <linux/mutex.h>
 #include <linux/pm.h>
+#include <linux/pm_runtime.h>
 #include <linux/resume-trace.h>
 #include <linux/rwsem.h>
 #include <linux/interrupt.h>
@@ -49,6 +50,16 @@ static DEFINE_MUTEX(dpm_list_mtx);
 static bool transition_started;
 
 /**
+ * device_pm_init - Initialize the PM-related part of a device object
+ * @dev: Device object to initialize.
+ */
+void device_pm_init(struct device *dev)
+{
+	dev->power.status = DPM_ON;
+	pm_runtime_init(dev);
+}
+
+/**
  *	device_pm_lock - lock the list of active devices used by the PM core
  */
 void device_pm_lock(void)
@@ -105,6 +116,8 @@ void device_pm_remove(struct device *dev
 	mutex_lock(&dpm_list_mtx);
 	list_del_init(&dev->power.entry);
 	mutex_unlock(&dpm_list_mtx);
+
+	pm_runtime_remove(dev);
 }
 
 /**
@@ -512,6 +525,7 @@ static void dpm_complete(pm_message_t st
 			mutex_unlock(&dpm_list_mtx);
 
 			device_complete(dev, state);
+			pm_runtime_enable(dev);
 
 			mutex_lock(&dpm_list_mtx);
 		}
@@ -757,11 +771,15 @@ static int dpm_prepare(pm_message_t stat
 		dev->power.status = DPM_PREPARING;
 		mutex_unlock(&dpm_list_mtx);
 
-		error = device_prepare(dev, state);
+		if (pm_runtime_disable(dev) && device_may_wakeup(dev))
+			error = -EBUSY;
+		else
+			error = device_prepare(dev, state);
 
 		mutex_lock(&dpm_list_mtx);
 		if (error) {
 			dev->power.status = DPM_ON;
+			pm_runtime_enable(dev);
 			if (error == -EAGAIN) {
 				put_device(dev);
 				error = 0;
drivers/base/dd.c +7 -0
Index: linux-2.6/drivers/base/dd.c
===================================================================
@@ -23,6 +23,7 @@
 #include <linux/kthread.h>
 #include <linux/wait.h>
 #include <linux/async.h>
+#include <linux/pm_runtime.h>
 
 #include "base.h"
 #include "power/power.h"
@@ -202,7 +203,9 @@ int driver_probe_device(struct device_dr
 	pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
 		 drv->bus->name, __func__, dev_name(dev), drv->name);
 
+	pm_runtime_get_noresume(dev);
 	ret = really_probe(dev, drv);
+	pm_runtime_put_noidle(dev);
 
 	return ret;
 }
@@ -306,6 +309,8 @@ static void __device_release_driver(stru
 
 	drv = dev->driver;
 	if (drv) {
+		pm_runtime_disable(dev);
+
 		driver_sysfs_remove(dev);
 
 		if (dev->bus)
@@ -324,6 +329,8 @@ static void __device_release_driver(stru
 			blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
 						     BUS_NOTIFY_UNBOUND_DRIVER,
 						     dev);
+
+		pm_runtime_enable(dev);
 	}
 }
 
drivers/base/power/power.h +4 -7
Index: linux-2.6/drivers/base/power/power.h
===================================================================
@@ -1,8 +1,3 @@
-static inline void device_pm_init(struct device *dev)
-{
-	dev->power.status = DPM_ON;
-}
-
 #ifdef CONFIG_PM_SLEEP
 
 /*
@@ -16,14 +11,16 @@ static inline struct device *to_device(s
 	return container_of(entry, struct device, power.entry);
 }
 
+extern void device_pm_init(struct device *dev);
 extern void device_pm_add(struct device *);
 extern void device_pm_remove(struct device *);
 extern void device_pm_move_before(struct device *, struct device *);
 extern void device_pm_move_after(struct device *, struct device *);
 extern void device_pm_move_last(struct device *);
 
-#else /* CONFIG_PM_SLEEP */
+#else /* !CONFIG_PM_SLEEP */
 
+static inline void device_pm_init(struct device *dev) {}
 static inline void device_pm_add(struct device *dev) {}
 static inline void device_pm_remove(struct device *dev) {}
 static inline void device_pm_move_before(struct device *deva,
@@ -32,7 +29,7 @@ static inline void device_pm_move_after(
 					struct device *devb) {}
 static inline void device_pm_move_last(struct device *dev) {}
 
-#endif
+#endif /* !CONFIG_PM_SLEEP */
 
 #ifdef CONFIG_PM
 
Documentation/power/runtime_pm.txt new file +340 -0

340 lines not on this page · raw message

#2 lore raw

Re: [PATCH update] PM: Introduce core framework for run-time PM of I/O devices (rev. 11)

Hi Rafail

[Runtime PM v11]

Thanks for your work on this. The code is getting better and better. I've just finished posting a bunch of patches related to v11 of your Runtime PM patch. Basically everything seems fine except a few minor details and the code below: =)

Quoted 14 lines · Rafael J. Wysocki

On Wednesday 22 July 2009, Rafael J. Wysocki wrote:
Index: linux-2.6/drivers/base/dd.c
===================================================================

drivers/base/dd.c +1 -0
@@ -23,6 +23,7 @@
  #include <linux/kthread.h>
  #include <linux/wait.h>
  #include <linux/async.h>
+#include <linux/pm_runtime.h>
 
  #include "base.h"
  #include "power/power.h"

drivers/base/dd.c int driver_probe_device(struct device_dr · line 211

Quoted 9 lines
drivers/base/dd.c +2 -0
@@ -202,7 +203,9 @@ int driver_probe_device(struct device_dr
        pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
                 drv->bus->name, __func__, dev_name(dev), drv->name);
 
+       pm_runtime_get_noresume(dev);
        ret = really_probe(dev, drv);
+       pm_runtime_put_noidle(dev);
 
        return ret;
  }

This creates problems when drivers want to performing runtime resume from within probe(). For more details please have a look at "[PATCH 04/04] video: Runtime PM hack for SuperH LCDC driver".

Cheers,

/ magnus
--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw

Re: [PATCH update] PM: Introduce core framework for run-time PM of I/O devices (rev. 11)

Quoted 1 line · Magnus Damm

Hi Rafail

Hi,

Quoted 6 lines

[Runtime PM v11]

Thanks for your work on this. The code is getting better and better. I've just finished posting a bunch of patches related to v11 of your Runtime PM patch. Basically everything seems fine except a few minor details and the code below: =)

Thanks a lot for your feedback.

Quoted 22 lines · Rafael J. Wysocki
Quoted 21 lines · Rafael J. Wysocki

Index: linux-2.6/drivers/base/dd.c
===================================================================

drivers/base/dd.c +3 -0
@@ -23,6 +23,7 @@
 #include <linux/kthread.h>
 #include <linux/wait.h>
 #include <linux/async.h>
+#include <linux/pm_runtime.h>
 
 #include "base.h"
 #include "power/power.h"
@@ -202,7 +203,9 @@ int driver_probe_device(struct device_dr
       pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
                drv->bus->name, __func__, dev_name(dev), drv->name);
 
+       pm_runtime_get_noresume(dev);
       ret = really_probe(dev, drv);
+       pm_runtime_put_noidle(dev);
 
Quoted 6 lines

drivers/base/dd.c int driver_probe_device(struct device_dr · line 211

Quoted 2 lines
drivers/base/dd.c +0 -0
       return ret;
 }

This creates problems when drivers want to performing runtime resume from within probe(). For more details please have a look at "[PATCH 04/04] video: Runtime PM hack for SuperH LCDC driver".

Ah, I see. You'd like to call pm_runtime_get_sync() from .probe(), but that sees the usage counter different from zero and exits immediately.

OTOH, I think we should prevent suspends from racing with .probe() at the core level. Hmm.

One possible approach could be to call pm_runtime_resume() from sh_mobile_lcdc_probe() instead of pm_runtime_put_noidle(). Then, the platform code will have a chance to turn the device on and the later pm_runtime_get*() and pm_runtime_put*() calls will be balanced. Of course, in that case the pm_runtime_get_noresume() in sh_mobile_lcdc_probe() won't be necessary any more. Am I overlooking anything?

Rafael

#4 lore raw

Re: [PATCH update] PM: Introduce core framework for run-time PM of I/O devices (rev. 11)

Hi again Rafael,

Quoted 18 lines · Rafael J. Wysocki
Quoted 10 lines · Magnus Damm

[Runtime PM v11]

Quoted 8 lines
drivers/base/dd.c +2 -0
@@ -202,7 +203,9 @@ int driver_probe_device(struct device_dr
        pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
                 drv->bus->name, __func__, dev_name(dev), drv->name);
 
+       pm_runtime_get_noresume(dev);
        ret = really_probe(dev, drv);
+       pm_runtime_put_noidle(dev);
 
Quoted 6 lines

drivers/base/dd.c int driver_probe_device(struct device_dr · line 211

Quoted 2 lines
drivers/base/dd.c +0 -0
        return ret;
  }

This creates problems when drivers want to performing runtime resume from within probe(). For more details please have a look at "[PATCH 04/04] video: Runtime PM hack for SuperH LCDC driver".

Quoted 2 lines

Ah, I see.  You'd like to call pm_runtime_get_sync() from .probe(), but that sees the usage counter different from zero and exits immediately.

Exactly.

Quoted 2 lines

OTOH, I think we should prevent suspends from racing with .probe() at the core level.  Hmm.

Doesn't it make more sense to allow runtime suspend and resume to happen after the pm_runtime_enable() call? What case are you trying to protect against?

Quoted 6 lines

One possible approach could be to call pm_runtime_resume() from sh_mobile_lcdc_probe() instead of pm_runtime_put_noidle().  Then, the platform code will have a chance to turn the device on and the later pm_runtime_get*() and pm_runtime_put*() calls will be balanced.  Of course, in that case the pm_runtime_get_noresume() in sh_mobile_lcdc_probe() won't be necessary any more.  Am I overlooking anything?

So for drivers that want to access hardware from .probe(), calling pm_runtime_resume() after pm_runtime_enable() in should be enough?

Thanks for your help!

/ magnus
--
To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#5 lore raw

Re: [PATCH update] PM: Introduce core framework for run-time PM of I/O devices (rev. 11)

Quoted 1 line · Magnus Damm

Hi again Rafael,

Hi,

Quoted 22 lines · Rafael J. Wysocki
Quoted 18 lines · Magnus Damm
Quoted 10 lines

[Runtime PM v11]

Quoted 8 lines
drivers/base/dd.c +2 -0
@@ -202,7 +203,9 @@ int driver_probe_device(struct device_dr
       pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
                drv->bus->name, __func__, dev_name(dev), drv->name);
 
+       pm_runtime_get_noresume(dev);
       ret = really_probe(dev, drv);
+       pm_runtime_put_noidle(dev);
 
Quoted 6 lines

drivers/base/dd.c int driver_probe_device(struct device_dr · line 211

Quoted 2 lines
drivers/base/dd.c +0 -0
       return ret;
 }

This creates problems when drivers want to performing runtime resume from within probe(). For more details please have a look at "[PATCH 04/04] video: Runtime PM hack for SuperH LCDC driver".

Quoted 2 lines

Ah, I see. You'd like to call pm_runtime_get_sync() from .probe(), but that sees the usage counter different from zero and exits immediately.

Quoted 8 lines

Exactly.

Quoted 2 lines

OTOH, I think we should prevent suspends from racing with .probe() at the core level. Hmm.

Doesn't it make more sense to allow runtime suspend and resume to happen after the pm_runtime_enable() call? What case are you trying to protect against?

If runtime PM is enabled before .probe() and then .probe() itself doesn't use pm_runtime_get_*(), then theory it is possible to have ->runtime_suspend() called while .probe() is running and there's no synchronization between the two. So, we prevent ->runtime_suspend() from being called while .proble() is running with the help of the usage counter.

Quoted 9 lines
Quoted 6 lines

One possible approach could be to call pm_runtime_resume() from sh_mobile_lcdc_probe() instead of pm_runtime_put_noidle(). Then, the platform code will have a chance to turn the device on and the later pm_runtime_get*() and pm_runtime_put*() calls will be balanced. Of course, in that case the pm_runtime_get_noresume() in sh_mobile_lcdc_probe() won't be necessary any more. Am I overlooking anything?

So for drivers that want to access hardware from .probe(), calling pm_runtime_resume() after pm_runtime_enable() in should be enough?

Yes, that should be sufficient (as long as the pm_runtime_resume() is successful).

Best,
Rafael

#6 lore raw

Re: [PATCH update] PM: Introduce core framework for run-time PM of I/O devices (rev. 11)

In reply to Magnus Damm
Quoted 1 line · Magnus Damm

Hi again Rafael,

Hi,

Quoted 22 lines · Rafael J. Wysocki
Quoted 18 lines · Magnus Damm
Quoted 10 lines

[Runtime PM v11]

Quoted 8 lines
drivers/base/dd.c +2 -0
@@ -202,7 +203,9 @@ int driver_probe_device(struct device_dr
       pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
                drv->bus->name, __func__, dev_name(dev), drv->name);
 
+       pm_runtime_get_noresume(dev);
       ret = really_probe(dev, drv);
+       pm_runtime_put_noidle(dev);
 
Quoted 6 lines

drivers/base/dd.c int driver_probe_device(struct device_dr · line 211

Quoted 2 lines
drivers/base/dd.c +0 -0
       return ret;
 }

This creates problems when drivers want to performing runtime resume from within probe(). For more details please have a look at "[PATCH 04/04] video: Runtime PM hack for SuperH LCDC driver".

Quoted 2 lines

Ah, I see. You'd like to call pm_runtime_get_sync() from .probe(), but that sees the usage counter different from zero and exits immediately.

Quoted 8 lines

Exactly.

Quoted 2 lines

OTOH, I think we should prevent suspends from racing with .probe() at the core level. Hmm.

Doesn't it make more sense to allow runtime suspend and resume to happen after the pm_runtime_enable() call? What case are you trying to protect against?

If runtime PM is enabled before .probe() and then .probe() itself doesn't use pm_runtime_get_*(), then theory it is possible to have ->runtime_suspend() called while .probe() is running and there's no synchronization between the two. So, we prevent ->runtime_suspend() from being called while .proble() is running with the help of the usage counter.

Quoted 9 lines
Quoted 6 lines

One possible approach could be to call pm_runtime_resume() from sh_mobile_lcdc_probe() instead of pm_runtime_put_noidle(). Then, the platform code will have a chance to turn the device on and the later pm_runtime_get*() and pm_runtime_put*() calls will be balanced. Of course, in that case the pm_runtime_get_noresume() in sh_mobile_lcdc_probe() won't be necessary any more. Am I overlooking anything?

So for drivers that want to access hardware from .probe(), calling pm_runtime_resume() after pm_runtime_enable() in should be enough?

Yes, that should be sufficient (as long as the pm_runtime_resume() is successful).

Best,
Rafael

#7 lore raw

Re: [PATCH update] PM: Introduce core framework for run-time PM of I/O devices (rev. 11)

In reply to Rafael J. Wysocki

Hi again Rafael,

Quoted 18 lines · Rafael J. Wysocki
Quoted 10 lines · Magnus Damm

[Runtime PM v11]

Quoted 8 lines
drivers/base/dd.c +2 -0
@@ -202,7 +203,9 @@ int driver_probe_device(struct device_dr
        pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
                 drv->bus->name, __func__, dev_name(dev), drv->name);
 
+       pm_runtime_get_noresume(dev);
        ret = really_probe(dev, drv);
+       pm_runtime_put_noidle(dev);
 
Quoted 6 lines

drivers/base/dd.c int driver_probe_device(struct device_dr · line 211

Quoted 2 lines
drivers/base/dd.c +0 -0
        return ret;
  }

This creates problems when drivers want to performing runtime resume from within probe(). For more details please have a look at "[PATCH 04/04] video: Runtime PM hack for SuperH LCDC driver".

Quoted 2 lines

Ah, I see.  You'd like to call pm_runtime_get_sync() from .probe(), but that sees the usage counter different from zero and exits immediately.

Exactly.

Quoted 2 lines

OTOH, I think we should prevent suspends from racing with .probe() at the core level.  Hmm.

Doesn't it make more sense to allow runtime suspend and resume to happen after the pm_runtime_enable() call? What case are you trying to protect against?

Quoted 6 lines

One possible approach could be to call pm_runtime_resume() from sh_mobile_lcdc_probe() instead of pm_runtime_put_noidle().  Then, the platform code will have a chance to turn the device on and the later pm_runtime_get*() and pm_runtime_put*() calls will be balanced.  Of course, in that case the pm_runtime_get_noresume() in sh_mobile_lcdc_probe() won't be necessary any more.  Am I overlooking anything?

So for drivers that want to access hardware from .probe(), calling pm_runtime_resume() after pm_runtime_enable() in should be enough?

Thanks for your help!

/ magnus

#8 lore raw

Re: [PATCH update] PM: Introduce core framework for run-time PM of I/O devices (rev. 11)

In reply to Rafael J. Wysocki

Hi Rafail

[Runtime PM v11]

Thanks for your work on this. The code is getting better and better. I've just finished posting a bunch of patches related to v11 of your Runtime PM patch. Basically everything seems fine except a few minor details and the code below: =)

Quoted 14 lines · Rafael J. Wysocki

On Wednesday 22 July 2009, Rafael J. Wysocki wrote:
Index: linux-2.6/drivers/base/dd.c
===================================================================

drivers/base/dd.c +1 -0
@@ -23,6 +23,7 @@
  #include <linux/kthread.h>
  #include <linux/wait.h>
  #include <linux/async.h>
+#include <linux/pm_runtime.h>
 
  #include "base.h"
  #include "power/power.h"

drivers/base/dd.c int driver_probe_device(struct device_dr · line 211

Quoted 9 lines
drivers/base/dd.c +2 -0
@@ -202,7 +203,9 @@ int driver_probe_device(struct device_dr
        pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
                 drv->bus->name, __func__, dev_name(dev), drv->name);
 
+       pm_runtime_get_noresume(dev);
        ret = really_probe(dev, drv);
+       pm_runtime_put_noidle(dev);
 
        return ret;
  }

This creates problems when drivers want to performing runtime resume from within probe(). For more details please have a look at "[PATCH 04/04] video: Runtime PM hack for SuperH LCDC driver".

Cheers,

/ magnus

#9 lore raw

PATCH 1/1 PM: Introduce core framework for run-time PM of I/O devices (rev. 11)

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

Hi,

Below is rev. 10 of the run-time PM framework patch.

Quoted 8 lines

The most visible change from the previous one is that the documentation is now present again. :-)

Second, there's a check in pm_runtime_add() that will only increment the parent's counter of 'active' children if the device current status is not 'suspended'.

Comments welcome.

One more fix.

I noticed that it might be invalid to increment the parent's power.child_count in pm_runtime_add() even with the check added previously. For this reason I changed the initial power.runtime_status to RPM_SUSPENDED, which made pm_runtime_add() redundant and which should allow us to avoid the problem with increasing power.child_count at a wrong time.

Best,
Rafael

---
From: Rafael J. Wysocki <[email protected]>
Subject: PM: Introduce core framework for run-time PM of I/O devices (rev. 11)

Introduce a core framework for run-time power management of I/O devices. Add device run-time PM fields to 'struct dev_pm_info' and device run-time PM callbacks to 'struct dev_pm_ops'. Introduce a run-time PM workqueue and define some device run-time PM helper functions at the core level. Document all these things.

---
 Documentation/power/runtime_pm.txt |  340 +++++++++++++
 drivers/base/dd.c                  |    7 
 drivers/base/power/Makefile        |    1 
 drivers/base/power/main.c          |   20 
 drivers/base/power/power.h         |   11 
 drivers/base/power/runtime.c       |  946 +++++++++++++++++++++++++++++++++++++
 include/linux/pm.h                 |  102 +++
 include/linux/pm_runtime.h         |  111 ++++
 kernel/power/Kconfig               |   14 
 kernel/power/main.c                |   17 
 10 files changed, 1558 insertions(+), 11 deletions(-)
kernel/power/Kconfig +14 -0
Index: linux-2.6/kernel/power/Kconfig
===================================================================
@@ -208,3 +208,17 @@ config APM_EMULATION
 	  random kernel OOPSes or reboots that don't seem to be related to
 	  anything, try disabling/enabling this option (or disabling/enabling
 	  APM in your BIOS).
+
+config PM_RUNTIME
+	bool "Run-time PM core functionality"
+	depends on PM
+	---help---
+	  Enable functionality allowing I/O devices to be put into energy-saving
+	  (low power) states at run time (or autosuspended) after a specified
+	  period of inactivity and woken up in response to a hardware-generated
+	  wake-up event or a driver's request.
+
+	  Hardware support is generally required for this functionality to work
+	  and the bus type drivers of the buses the devices are on are
+	  responsible for the actual handling of the autosuspend requests and
+	  wake-up events.
kernel/power/main.c +17 -0
Index: linux-2.6/kernel/power/main.c
===================================================================
@@ -11,6 +11,7 @@
 #include <linux/kobject.h>
 #include <linux/string.h>
 #include <linux/resume-trace.h>
+#include <linux/workqueue.h>
 
 #include "power.h"
 
@@ -217,8 +218,24 @@ static struct attribute_group attr_group
 	.attrs = g,
 };
 
+#ifdef CONFIG_PM_RUNTIME
+struct workqueue_struct *pm_wq;
+
+static int __init pm_start_workqueue(void)
+{
+	pm_wq = create_freezeable_workqueue("pm");
+
+	return pm_wq ? 0 : -ENOMEM;
+}
+#else
+static inline int pm_start_workqueue(void) { return 0; }
+#endif
+
 static int __init pm_init(void)
 {
+	int error = pm_start_workqueue();
+	if (error)
+		return error;
 	power_kobj = kobject_create_and_add("power", NULL);
 	if (!power_kobj)
 		return -ENOMEM;
include/linux/pm.h +99 -3

131 lines not on this page · raw message

drivers/base/power/Makefile +1 -0
Index: linux-2.6/drivers/base/power/Makefile
===================================================================
@@ -1,5 +1,6 @@
 obj-$(CONFIG_PM)	+= sysfs.o
 obj-$(CONFIG_PM_SLEEP)	+= main.o
+obj-$(CONFIG_PM_RUNTIME)	+= runtime.o
 obj-$(CONFIG_PM_TRACE_RTC)	+= trace.o
 
 ccflags-$(CONFIG_DEBUG_DRIVER) := -DDEBUG
drivers/base/power/runtime.c new file +946 -0

946 lines not on this page · raw message

include/linux/pm_runtime.h new file +111 -0

111 lines not on this page · raw message

drivers/base/power/main.c +19 -1
Index: linux-2.6/drivers/base/power/main.c
===================================================================
@@ -21,6 +21,7 @@
 #include <linux/kallsyms.h>
 #include <linux/mutex.h>
 #include <linux/pm.h>
+#include <linux/pm_runtime.h>
 #include <linux/resume-trace.h>
 #include <linux/rwsem.h>
 #include <linux/interrupt.h>
@@ -49,6 +50,16 @@ static DEFINE_MUTEX(dpm_list_mtx);
 static bool transition_started;
 
 /**
+ * device_pm_init - Initialize the PM-related part of a device object
+ * @dev: Device object to initialize.
+ */
+void device_pm_init(struct device *dev)
+{
+	dev->power.status = DPM_ON;
+	pm_runtime_init(dev);
+}
+
+/**
  *	device_pm_lock - lock the list of active devices used by the PM core
  */
 void device_pm_lock(void)
@@ -105,6 +116,8 @@ void device_pm_remove(struct device *dev
 	mutex_lock(&dpm_list_mtx);
 	list_del_init(&dev->power.entry);
 	mutex_unlock(&dpm_list_mtx);
+
+	pm_runtime_remove(dev);
 }
 
 /**
@@ -512,6 +525,7 @@ static void dpm_complete(pm_message_t st
 			mutex_unlock(&dpm_list_mtx);
 
 			device_complete(dev, state);
+			pm_runtime_enable(dev);
 
 			mutex_lock(&dpm_list_mtx);
 		}
@@ -757,11 +771,15 @@ static int dpm_prepare(pm_message_t stat
 		dev->power.status = DPM_PREPARING;
 		mutex_unlock(&dpm_list_mtx);
 
-		error = device_prepare(dev, state);
+		if (pm_runtime_disable(dev) && device_may_wakeup(dev))
+			error = -EBUSY;
+		else
+			error = device_prepare(dev, state);
 
 		mutex_lock(&dpm_list_mtx);
 		if (error) {
 			dev->power.status = DPM_ON;
+			pm_runtime_enable(dev);
 			if (error == -EAGAIN) {
 				put_device(dev);
 				error = 0;
drivers/base/dd.c +7 -0
Index: linux-2.6/drivers/base/dd.c
===================================================================
@@ -23,6 +23,7 @@
 #include <linux/kthread.h>
 #include <linux/wait.h>
 #include <linux/async.h>
+#include <linux/pm_runtime.h>
 
 #include "base.h"
 #include "power/power.h"
@@ -202,7 +203,9 @@ int driver_probe_device(struct device_dr
 	pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
 		 drv->bus->name, __func__, dev_name(dev), drv->name);
 
+	pm_runtime_get_noresume(dev);
 	ret = really_probe(dev, drv);
+	pm_runtime_put_noidle(dev);
 
 	return ret;
 }
@@ -306,6 +309,8 @@ static void __device_release_driver(stru
 
 	drv = dev->driver;
 	if (drv) {
+		pm_runtime_disable(dev);
+
 		driver_sysfs_remove(dev);
 
 		if (dev->bus)
@@ -324,6 +329,8 @@ static void __device_release_driver(stru
 			blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
 						     BUS_NOTIFY_UNBOUND_DRIVER,
 						     dev);
+
+		pm_runtime_enable(dev);
 	}
 }
 
drivers/base/power/power.h +4 -7
Index: linux-2.6/drivers/base/power/power.h
===================================================================
@@ -1,8 +1,3 @@
-static inline void device_pm_init(struct device *dev)
-{
-	dev->power.status = DPM_ON;
-}
-
 #ifdef CONFIG_PM_SLEEP
 
 /*
@@ -16,14 +11,16 @@ static inline struct device *to_device(s
 	return container_of(entry, struct device, power.entry);
 }
 
+extern void device_pm_init(struct device *dev);
 extern void device_pm_add(struct device *);
 extern void device_pm_remove(struct device *);
 extern void device_pm_move_before(struct device *, struct device *);
 extern void device_pm_move_after(struct device *, struct device *);
 extern void device_pm_move_last(struct device *);
 
-#else /* CONFIG_PM_SLEEP */
+#else /* !CONFIG_PM_SLEEP */
 
+static inline void device_pm_init(struct device *dev) {}
 static inline void device_pm_add(struct device *dev) {}
 static inline void device_pm_remove(struct device *dev) {}
 static inline void device_pm_move_before(struct device *deva,
@@ -32,7 +29,7 @@ static inline void device_pm_move_after(
 					struct device *devb) {}
 static inline void device_pm_move_last(struct device *dev) {}
 
-#endif
+#endif /* !CONFIG_PM_SLEEP */
 
 #ifdef CONFIG_PM
 
Documentation/power/runtime_pm.txt new file +340 -0

340 lines not on this page · raw message