2 * drivers/base/power/main.c - Where the driver meets power management.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
10 * The driver model core calls device_pm_add() when a device is registered.
11 * This will intialize the embedded device_pm_info object in the device
12 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added.
15 * A separate list is used for keeping track of power info, because the power
16 * domain dependencies may differ from the ancestral dependencies that the
17 * subsystem list maintains.
20 #include <linux/device.h>
21 #include <linux/kallsyms.h>
22 #include <linux/mutex.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/resume-trace.h>
26 #include <linux/rwsem.h>
27 #include <linux/interrupt.h>
28 #include <linux/sched.h>
34 * The entries in the dpm_list list are in a depth first order, simply
35 * because children are guaranteed to be discovered after parents, and
36 * are inserted at the back of the list on discovery.
38 * Since device_pm_add() may be called with a device semaphore held,
39 * we must never try to acquire a device semaphore while holding
45 static DEFINE_MUTEX(dpm_list_mtx);
48 * Set once the preparation of devices for a PM transition has started, reset
49 * before starting to resume devices. Protected by dpm_list_mtx.
51 static bool transition_started;
54 * device_pm_init - Initialize the PM-related part of a device object.
55 * @dev: Device object being initialized.
57 void device_pm_init(struct device *dev)
59 dev->power.status = DPM_ON;
64 * device_pm_lock - Lock the list of active devices used by the PM core.
66 void device_pm_lock(void)
68 mutex_lock(&dpm_list_mtx);
72 * device_pm_unlock - Unlock the list of active devices used by the PM core.
74 void device_pm_unlock(void)
76 mutex_unlock(&dpm_list_mtx);
80 * device_pm_add - Add a device to the PM core's list of active devices.
81 * @dev: Device to add to the list.
83 void device_pm_add(struct device *dev)
85 pr_debug("PM: Adding info for %s:%s\n",
86 dev->bus ? dev->bus->name : "No Bus",
87 kobject_name(&dev->kobj));
88 mutex_lock(&dpm_list_mtx);
90 if (dev->parent->power.status >= DPM_SUSPENDING)
91 dev_warn(dev, "parent %s should not be sleeping\n",
92 dev_name(dev->parent));
93 } else if (transition_started) {
95 * We refuse to register parentless devices while a PM
96 * transition is in progress in order to avoid leaving them
97 * unhandled down the road
99 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
102 list_add_tail(&dev->power.entry, &dpm_list);
103 mutex_unlock(&dpm_list_mtx);
107 * device_pm_remove - Remove a device from the PM core's list of active devices.
108 * @dev: Device to be removed from the list.
110 void device_pm_remove(struct device *dev)
112 pr_debug("PM: Removing info for %s:%s\n",
113 dev->bus ? dev->bus->name : "No Bus",
114 kobject_name(&dev->kobj));
115 mutex_lock(&dpm_list_mtx);
116 list_del_init(&dev->power.entry);
117 mutex_unlock(&dpm_list_mtx);
118 pm_runtime_remove(dev);
122 * device_pm_move_before - Move device in the PM core's list of active devices.
123 * @deva: Device to move in dpm_list.
124 * @devb: Device @deva should come before.
126 void device_pm_move_before(struct device *deva, struct device *devb)
128 pr_debug("PM: Moving %s:%s before %s:%s\n",
129 deva->bus ? deva->bus->name : "No Bus",
130 kobject_name(&deva->kobj),
131 devb->bus ? devb->bus->name : "No Bus",
132 kobject_name(&devb->kobj));
133 /* Delete deva from dpm_list and reinsert before devb. */
134 list_move_tail(&deva->power.entry, &devb->power.entry);
138 * device_pm_move_after - Move device in the PM core's list of active devices.
139 * @deva: Device to move in dpm_list.
140 * @devb: Device @deva should come after.
142 void device_pm_move_after(struct device *deva, struct device *devb)
144 pr_debug("PM: Moving %s:%s after %s:%s\n",
145 deva->bus ? deva->bus->name : "No Bus",
146 kobject_name(&deva->kobj),
147 devb->bus ? devb->bus->name : "No Bus",
148 kobject_name(&devb->kobj));
149 /* Delete deva from dpm_list and reinsert after devb. */
150 list_move(&deva->power.entry, &devb->power.entry);
154 * device_pm_move_last - Move device to end of the PM core's list of devices.
155 * @dev: Device to move in dpm_list.
157 void device_pm_move_last(struct device *dev)
159 pr_debug("PM: Moving %s:%s to end of list\n",
160 dev->bus ? dev->bus->name : "No Bus",
161 kobject_name(&dev->kobj));
162 list_move_tail(&dev->power.entry, &dpm_list);
166 * pm_op - Execute the PM operation appropriate for given PM event.
167 * @dev: Device to handle.
168 * @ops: PM operations to choose from.
169 * @state: PM transition of the system being carried out.
171 static int pm_op(struct device *dev,
172 const struct dev_pm_ops *ops,
176 ktime_t calltime, delta, rettime;
178 if (initcall_debug) {
179 pr_info("calling %s+ @ %i\n",
180 dev_name(dev), task_pid_nr(current));
181 calltime = ktime_get();
184 switch (state.event) {
185 #ifdef CONFIG_SUSPEND
186 case PM_EVENT_SUSPEND:
188 error = ops->suspend(dev);
189 suspend_report_result(ops->suspend, error);
192 case PM_EVENT_RESUME:
194 error = ops->resume(dev);
195 suspend_report_result(ops->resume, error);
198 #endif /* CONFIG_SUSPEND */
199 #ifdef CONFIG_HIBERNATION
200 case PM_EVENT_FREEZE:
201 case PM_EVENT_QUIESCE:
203 error = ops->freeze(dev);
204 suspend_report_result(ops->freeze, error);
207 case PM_EVENT_HIBERNATE:
209 error = ops->poweroff(dev);
210 suspend_report_result(ops->poweroff, error);
214 case PM_EVENT_RECOVER:
216 error = ops->thaw(dev);
217 suspend_report_result(ops->thaw, error);
220 case PM_EVENT_RESTORE:
222 error = ops->restore(dev);
223 suspend_report_result(ops->restore, error);
226 #endif /* CONFIG_HIBERNATION */
231 if (initcall_debug) {
232 rettime = ktime_get();
233 delta = ktime_sub(rettime, calltime);
234 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
235 error, (unsigned long long)ktime_to_ns(delta) >> 10);
242 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
243 * @dev: Device to handle.
244 * @ops: PM operations to choose from.
245 * @state: PM transition of the system being carried out.
247 * The driver of @dev will not receive interrupts while this function is being
250 static int pm_noirq_op(struct device *dev,
251 const struct dev_pm_ops *ops,
255 ktime_t calltime, delta, rettime;
257 if (initcall_debug) {
258 pr_info("calling %s_i+ @ %i\n",
259 dev_name(dev), task_pid_nr(current));
260 calltime = ktime_get();
263 switch (state.event) {
264 #ifdef CONFIG_SUSPEND
265 case PM_EVENT_SUSPEND:
266 if (ops->suspend_noirq) {
267 error = ops->suspend_noirq(dev);
268 suspend_report_result(ops->suspend_noirq, error);
271 case PM_EVENT_RESUME:
272 if (ops->resume_noirq) {
273 error = ops->resume_noirq(dev);
274 suspend_report_result(ops->resume_noirq, error);
277 #endif /* CONFIG_SUSPEND */
278 #ifdef CONFIG_HIBERNATION
279 case PM_EVENT_FREEZE:
280 case PM_EVENT_QUIESCE:
281 if (ops->freeze_noirq) {
282 error = ops->freeze_noirq(dev);
283 suspend_report_result(ops->freeze_noirq, error);
286 case PM_EVENT_HIBERNATE:
287 if (ops->poweroff_noirq) {
288 error = ops->poweroff_noirq(dev);
289 suspend_report_result(ops->poweroff_noirq, error);
293 case PM_EVENT_RECOVER:
294 if (ops->thaw_noirq) {
295 error = ops->thaw_noirq(dev);
296 suspend_report_result(ops->thaw_noirq, error);
299 case PM_EVENT_RESTORE:
300 if (ops->restore_noirq) {
301 error = ops->restore_noirq(dev);
302 suspend_report_result(ops->restore_noirq, error);
305 #endif /* CONFIG_HIBERNATION */
310 if (initcall_debug) {
311 rettime = ktime_get();
312 delta = ktime_sub(rettime, calltime);
313 printk("initcall %s_i+ returned %d after %Ld usecs\n", dev_name(dev),
314 error, (unsigned long long)ktime_to_ns(delta) >> 10);
320 static char *pm_verb(int event)
323 case PM_EVENT_SUSPEND:
325 case PM_EVENT_RESUME:
327 case PM_EVENT_FREEZE:
329 case PM_EVENT_QUIESCE:
331 case PM_EVENT_HIBERNATE:
335 case PM_EVENT_RESTORE:
337 case PM_EVENT_RECOVER:
340 return "(unknown PM event)";
344 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
346 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
347 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
348 ", may wakeup" : "");
351 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
354 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
355 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
358 /*------------------------- Resume routines -------------------------*/
361 * device_resume_noirq - Execute an "early resume" callback for given device.
362 * @dev: Device to handle.
363 * @state: PM transition of the system being carried out.
365 * The driver of @dev will not receive interrupts while this function is being
368 static int device_resume_noirq(struct device *dev, pm_message_t state)
379 pm_dev_dbg(dev, state, "EARLY ");
380 error = pm_noirq_op(dev, dev->bus->pm, state);
388 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
389 * @state: PM transition of the system being carried out.
391 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
392 * enable device drivers to receive interrupts.
394 void dpm_resume_noirq(pm_message_t state)
398 mutex_lock(&dpm_list_mtx);
399 transition_started = false;
400 list_for_each_entry(dev, &dpm_list, power.entry)
401 if (dev->power.status > DPM_OFF) {
404 dev->power.status = DPM_OFF;
405 error = device_resume_noirq(dev, state);
407 pm_dev_err(dev, state, " early", error);
409 mutex_unlock(&dpm_list_mtx);
410 resume_device_irqs();
412 EXPORT_SYMBOL_GPL(dpm_resume_noirq);
415 * device_resume - Execute "resume" callbacks for given device.
416 * @dev: Device to handle.
417 * @state: PM transition of the system being carried out.
419 static int device_resume(struct device *dev, pm_message_t state)
430 pm_dev_dbg(dev, state, "");
431 error = pm_op(dev, dev->bus->pm, state);
432 } else if (dev->bus->resume) {
433 pm_dev_dbg(dev, state, "legacy ");
434 error = dev->bus->resume(dev);
442 pm_dev_dbg(dev, state, "type ");
443 error = pm_op(dev, dev->type->pm, state);
450 if (dev->class->pm) {
451 pm_dev_dbg(dev, state, "class ");
452 error = pm_op(dev, dev->class->pm, state);
453 } else if (dev->class->resume) {
454 pm_dev_dbg(dev, state, "legacy class ");
455 error = dev->class->resume(dev);
466 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
467 * @state: PM transition of the system being carried out.
469 * Execute the appropriate "resume" callback for all devices whose status
470 * indicates that they are suspended.
472 static void dpm_resume(pm_message_t state)
474 struct list_head list;
476 INIT_LIST_HEAD(&list);
477 mutex_lock(&dpm_list_mtx);
478 while (!list_empty(&dpm_list)) {
479 struct device *dev = to_device(dpm_list.next);
482 if (dev->power.status >= DPM_OFF) {
485 dev->power.status = DPM_RESUMING;
486 mutex_unlock(&dpm_list_mtx);
488 error = device_resume(dev, state);
490 mutex_lock(&dpm_list_mtx);
492 pm_dev_err(dev, state, "", error);
493 } else if (dev->power.status == DPM_SUSPENDING) {
494 /* Allow new children of the device to be registered */
495 dev->power.status = DPM_RESUMING;
497 if (!list_empty(&dev->power.entry))
498 list_move_tail(&dev->power.entry, &list);
501 list_splice(&list, &dpm_list);
502 mutex_unlock(&dpm_list_mtx);
506 * device_complete - Complete a PM transition for given device.
507 * @dev: Device to handle.
508 * @state: PM transition of the system being carried out.
510 static void device_complete(struct device *dev, pm_message_t state)
514 if (dev->class && dev->class->pm && dev->class->pm->complete) {
515 pm_dev_dbg(dev, state, "completing class ");
516 dev->class->pm->complete(dev);
519 if (dev->type && dev->type->pm && dev->type->pm->complete) {
520 pm_dev_dbg(dev, state, "completing type ");
521 dev->type->pm->complete(dev);
524 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
525 pm_dev_dbg(dev, state, "completing ");
526 dev->bus->pm->complete(dev);
533 * dpm_complete - Complete a PM transition for all non-sysdev devices.
534 * @state: PM transition of the system being carried out.
536 * Execute the ->complete() callbacks for all devices whose PM status is not
537 * DPM_ON (this allows new devices to be registered).
539 static void dpm_complete(pm_message_t state)
541 struct list_head list;
543 INIT_LIST_HEAD(&list);
544 mutex_lock(&dpm_list_mtx);
545 transition_started = false;
546 while (!list_empty(&dpm_list)) {
547 struct device *dev = to_device(dpm_list.prev);
550 if (dev->power.status > DPM_ON) {
551 dev->power.status = DPM_ON;
552 mutex_unlock(&dpm_list_mtx);
554 device_complete(dev, state);
555 pm_runtime_put_noidle(dev);
557 mutex_lock(&dpm_list_mtx);
559 if (!list_empty(&dev->power.entry))
560 list_move(&dev->power.entry, &list);
563 list_splice(&list, &dpm_list);
564 mutex_unlock(&dpm_list_mtx);
568 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
569 * @state: PM transition of the system being carried out.
571 * Execute "resume" callbacks for all devices and complete the PM transition of
574 void dpm_resume_end(pm_message_t state)
580 EXPORT_SYMBOL_GPL(dpm_resume_end);
583 /*------------------------- Suspend routines -------------------------*/
586 * resume_event - Return a "resume" message for given "suspend" sleep state.
587 * @sleep_state: PM message representing a sleep state.
589 * Return a PM message representing the resume event corresponding to given
592 static pm_message_t resume_event(pm_message_t sleep_state)
594 switch (sleep_state.event) {
595 case PM_EVENT_SUSPEND:
597 case PM_EVENT_FREEZE:
598 case PM_EVENT_QUIESCE:
600 case PM_EVENT_HIBERNATE:
607 * device_suspend_noirq - Execute a "late suspend" callback for given device.
608 * @dev: Device to handle.
609 * @state: PM transition of the system being carried out.
611 * The driver of @dev will not receive interrupts while this function is being
614 static int device_suspend_noirq(struct device *dev, pm_message_t state)
622 pm_dev_dbg(dev, state, "LATE ");
623 error = pm_noirq_op(dev, dev->bus->pm, state);
629 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
630 * @state: PM transition of the system being carried out.
632 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
633 * handlers for all non-sysdev devices.
635 int dpm_suspend_noirq(pm_message_t state)
640 suspend_device_irqs();
641 mutex_lock(&dpm_list_mtx);
642 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
643 error = device_suspend_noirq(dev, state);
645 pm_dev_err(dev, state, " late", error);
648 dev->power.status = DPM_OFF_IRQ;
650 mutex_unlock(&dpm_list_mtx);
652 dpm_resume_noirq(resume_event(state));
655 EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
658 * device_suspend - Execute "suspend" callbacks for given device.
659 * @dev: Device to handle.
660 * @state: PM transition of the system being carried out.
662 static int device_suspend(struct device *dev, pm_message_t state)
669 if (dev->class->pm) {
670 pm_dev_dbg(dev, state, "class ");
671 error = pm_op(dev, dev->class->pm, state);
672 } else if (dev->class->suspend) {
673 pm_dev_dbg(dev, state, "legacy class ");
674 error = dev->class->suspend(dev, state);
675 suspend_report_result(dev->class->suspend, error);
683 pm_dev_dbg(dev, state, "type ");
684 error = pm_op(dev, dev->type->pm, state);
692 pm_dev_dbg(dev, state, "");
693 error = pm_op(dev, dev->bus->pm, state);
694 } else if (dev->bus->suspend) {
695 pm_dev_dbg(dev, state, "legacy ");
696 error = dev->bus->suspend(dev, state);
697 suspend_report_result(dev->bus->suspend, error);
707 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
708 * @state: PM transition of the system being carried out.
710 static int dpm_suspend(pm_message_t state)
712 struct list_head list;
715 INIT_LIST_HEAD(&list);
716 mutex_lock(&dpm_list_mtx);
717 while (!list_empty(&dpm_list)) {
718 struct device *dev = to_device(dpm_list.prev);
721 mutex_unlock(&dpm_list_mtx);
723 error = device_suspend(dev, state);
725 mutex_lock(&dpm_list_mtx);
727 pm_dev_err(dev, state, "", error);
731 dev->power.status = DPM_OFF;
732 if (!list_empty(&dev->power.entry))
733 list_move(&dev->power.entry, &list);
736 list_splice(&list, dpm_list.prev);
737 mutex_unlock(&dpm_list_mtx);
742 * device_prepare - Prepare a device for system power transition.
743 * @dev: Device to handle.
744 * @state: PM transition of the system being carried out.
746 * Execute the ->prepare() callback(s) for given device. No new children of the
747 * device may be registered after this function has returned.
749 static int device_prepare(struct device *dev, pm_message_t state)
755 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
756 pm_dev_dbg(dev, state, "preparing ");
757 error = dev->bus->pm->prepare(dev);
758 suspend_report_result(dev->bus->pm->prepare, error);
763 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
764 pm_dev_dbg(dev, state, "preparing type ");
765 error = dev->type->pm->prepare(dev);
766 suspend_report_result(dev->type->pm->prepare, error);
771 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
772 pm_dev_dbg(dev, state, "preparing class ");
773 error = dev->class->pm->prepare(dev);
774 suspend_report_result(dev->class->pm->prepare, error);
783 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
784 * @state: PM transition of the system being carried out.
786 * Execute the ->prepare() callback(s) for all devices.
788 static int dpm_prepare(pm_message_t state)
790 struct list_head list;
793 INIT_LIST_HEAD(&list);
794 mutex_lock(&dpm_list_mtx);
795 transition_started = true;
796 while (!list_empty(&dpm_list)) {
797 struct device *dev = to_device(dpm_list.next);
800 dev->power.status = DPM_PREPARING;
801 mutex_unlock(&dpm_list_mtx);
803 pm_runtime_get_noresume(dev);
804 if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
805 /* Wake-up requested during system sleep transition. */
806 pm_runtime_put_noidle(dev);
809 error = device_prepare(dev, state);
812 mutex_lock(&dpm_list_mtx);
814 dev->power.status = DPM_ON;
815 if (error == -EAGAIN) {
820 printk(KERN_ERR "PM: Failed to prepare device %s "
821 "for power transition: error %d\n",
822 kobject_name(&dev->kobj), error);
826 dev->power.status = DPM_SUSPENDING;
827 if (!list_empty(&dev->power.entry))
828 list_move_tail(&dev->power.entry, &list);
831 list_splice(&list, &dpm_list);
832 mutex_unlock(&dpm_list_mtx);
837 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
838 * @state: PM transition of the system being carried out.
840 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
841 * callbacks for them.
843 int dpm_suspend_start(pm_message_t state)
848 error = dpm_prepare(state);
850 error = dpm_suspend(state);
853 EXPORT_SYMBOL_GPL(dpm_suspend_start);
855 void __suspend_report_result(const char *function, void *fn, int ret)
858 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
860 EXPORT_SYMBOL_GPL(__suspend_report_result);