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/interrupt.h>
27 #include <linux/sched.h>
33 * The entries in the dpm_list list are in a depth first order, simply
34 * because children are guaranteed to be discovered after parents, and
35 * are inserted at the back of the list on discovery.
37 * Since device_pm_add() may be called with a device semaphore held,
38 * we must never try to acquire a device semaphore while holding
44 static DEFINE_MUTEX(dpm_list_mtx);
47 * Set once the preparation of devices for a PM transition has started, reset
48 * before starting to resume devices. Protected by dpm_list_mtx.
50 static bool transition_started;
53 * device_pm_init - Initialize the PM-related part of a device object.
54 * @dev: Device object being initialized.
56 void device_pm_init(struct device *dev)
58 dev->power.status = DPM_ON;
63 * device_pm_lock - Lock the list of active devices used by the PM core.
65 void device_pm_lock(void)
67 mutex_lock(&dpm_list_mtx);
71 * device_pm_unlock - Unlock the list of active devices used by the PM core.
73 void device_pm_unlock(void)
75 mutex_unlock(&dpm_list_mtx);
79 * device_pm_add - Add a device to the PM core's list of active devices.
80 * @dev: Device to add to the list.
82 void device_pm_add(struct device *dev)
84 pr_debug("PM: Adding info for %s:%s\n",
85 dev->bus ? dev->bus->name : "No Bus",
86 kobject_name(&dev->kobj));
87 mutex_lock(&dpm_list_mtx);
89 if (dev->parent->power.status >= DPM_SUSPENDING)
90 dev_warn(dev, "parent %s should not be sleeping\n",
91 dev_name(dev->parent));
92 } else if (transition_started) {
94 * We refuse to register parentless devices while a PM
95 * transition is in progress in order to avoid leaving them
96 * unhandled down the road
98 dev_WARN(dev, "Parentless device registered during a PM transaction\n");
101 list_add_tail(&dev->power.entry, &dpm_list);
102 mutex_unlock(&dpm_list_mtx);
106 * device_pm_remove - Remove a device from the PM core's list of active devices.
107 * @dev: Device to be removed from the list.
109 void device_pm_remove(struct device *dev)
111 pr_debug("PM: Removing info for %s:%s\n",
112 dev->bus ? dev->bus->name : "No Bus",
113 kobject_name(&dev->kobj));
114 mutex_lock(&dpm_list_mtx);
115 list_del_init(&dev->power.entry);
116 mutex_unlock(&dpm_list_mtx);
117 pm_runtime_remove(dev);
121 * device_pm_move_before - Move device in the PM core's list of active devices.
122 * @deva: Device to move in dpm_list.
123 * @devb: Device @deva should come before.
125 void device_pm_move_before(struct device *deva, struct device *devb)
127 pr_debug("PM: Moving %s:%s before %s:%s\n",
128 deva->bus ? deva->bus->name : "No Bus",
129 kobject_name(&deva->kobj),
130 devb->bus ? devb->bus->name : "No Bus",
131 kobject_name(&devb->kobj));
132 /* Delete deva from dpm_list and reinsert before devb. */
133 list_move_tail(&deva->power.entry, &devb->power.entry);
137 * device_pm_move_after - Move device in the PM core's list of active devices.
138 * @deva: Device to move in dpm_list.
139 * @devb: Device @deva should come after.
141 void device_pm_move_after(struct device *deva, struct device *devb)
143 pr_debug("PM: Moving %s:%s after %s:%s\n",
144 deva->bus ? deva->bus->name : "No Bus",
145 kobject_name(&deva->kobj),
146 devb->bus ? devb->bus->name : "No Bus",
147 kobject_name(&devb->kobj));
148 /* Delete deva from dpm_list and reinsert after devb. */
149 list_move(&deva->power.entry, &devb->power.entry);
153 * device_pm_move_last - Move device to end of the PM core's list of devices.
154 * @dev: Device to move in dpm_list.
156 void device_pm_move_last(struct device *dev)
158 pr_debug("PM: Moving %s:%s to end of list\n",
159 dev->bus ? dev->bus->name : "No Bus",
160 kobject_name(&dev->kobj));
161 list_move_tail(&dev->power.entry, &dpm_list);
164 static ktime_t initcall_debug_start(struct device *dev)
166 ktime_t calltime = ktime_set(0, 0);
168 if (initcall_debug) {
169 pr_info("calling %s+ @ %i\n",
170 dev_name(dev), task_pid_nr(current));
171 calltime = ktime_get();
177 static void initcall_debug_report(struct device *dev, ktime_t calltime,
180 ktime_t delta, rettime;
182 if (initcall_debug) {
183 rettime = ktime_get();
184 delta = ktime_sub(rettime, calltime);
185 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
186 error, (unsigned long long)ktime_to_ns(delta) >> 10);
191 * pm_op - Execute the PM operation appropriate for given PM event.
192 * @dev: Device to handle.
193 * @ops: PM operations to choose from.
194 * @state: PM transition of the system being carried out.
196 static int pm_op(struct device *dev,
197 const struct dev_pm_ops *ops,
203 calltime = initcall_debug_start(dev);
205 switch (state.event) {
206 #ifdef CONFIG_SUSPEND
207 case PM_EVENT_SUSPEND:
209 error = ops->suspend(dev);
210 suspend_report_result(ops->suspend, error);
213 case PM_EVENT_RESUME:
215 error = ops->resume(dev);
216 suspend_report_result(ops->resume, error);
219 #endif /* CONFIG_SUSPEND */
220 #ifdef CONFIG_HIBERNATION
221 case PM_EVENT_FREEZE:
222 case PM_EVENT_QUIESCE:
224 error = ops->freeze(dev);
225 suspend_report_result(ops->freeze, error);
228 case PM_EVENT_HIBERNATE:
230 error = ops->poweroff(dev);
231 suspend_report_result(ops->poweroff, error);
235 case PM_EVENT_RECOVER:
237 error = ops->thaw(dev);
238 suspend_report_result(ops->thaw, error);
241 case PM_EVENT_RESTORE:
243 error = ops->restore(dev);
244 suspend_report_result(ops->restore, error);
247 #endif /* CONFIG_HIBERNATION */
252 initcall_debug_report(dev, calltime, error);
258 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
259 * @dev: Device to handle.
260 * @ops: PM operations to choose from.
261 * @state: PM transition of the system being carried out.
263 * The driver of @dev will not receive interrupts while this function is being
266 static int pm_noirq_op(struct device *dev,
267 const struct dev_pm_ops *ops,
271 ktime_t calltime, delta, rettime;
273 if (initcall_debug) {
274 pr_info("calling %s_i+ @ %i\n",
275 dev_name(dev), task_pid_nr(current));
276 calltime = ktime_get();
279 switch (state.event) {
280 #ifdef CONFIG_SUSPEND
281 case PM_EVENT_SUSPEND:
282 if (ops->suspend_noirq) {
283 error = ops->suspend_noirq(dev);
284 suspend_report_result(ops->suspend_noirq, error);
287 case PM_EVENT_RESUME:
288 if (ops->resume_noirq) {
289 error = ops->resume_noirq(dev);
290 suspend_report_result(ops->resume_noirq, error);
293 #endif /* CONFIG_SUSPEND */
294 #ifdef CONFIG_HIBERNATION
295 case PM_EVENT_FREEZE:
296 case PM_EVENT_QUIESCE:
297 if (ops->freeze_noirq) {
298 error = ops->freeze_noirq(dev);
299 suspend_report_result(ops->freeze_noirq, error);
302 case PM_EVENT_HIBERNATE:
303 if (ops->poweroff_noirq) {
304 error = ops->poweroff_noirq(dev);
305 suspend_report_result(ops->poweroff_noirq, error);
309 case PM_EVENT_RECOVER:
310 if (ops->thaw_noirq) {
311 error = ops->thaw_noirq(dev);
312 suspend_report_result(ops->thaw_noirq, error);
315 case PM_EVENT_RESTORE:
316 if (ops->restore_noirq) {
317 error = ops->restore_noirq(dev);
318 suspend_report_result(ops->restore_noirq, error);
321 #endif /* CONFIG_HIBERNATION */
326 if (initcall_debug) {
327 rettime = ktime_get();
328 delta = ktime_sub(rettime, calltime);
329 printk("initcall %s_i+ returned %d after %Ld usecs\n",
330 dev_name(dev), error,
331 (unsigned long long)ktime_to_ns(delta) >> 10);
337 static char *pm_verb(int event)
340 case PM_EVENT_SUSPEND:
342 case PM_EVENT_RESUME:
344 case PM_EVENT_FREEZE:
346 case PM_EVENT_QUIESCE:
348 case PM_EVENT_HIBERNATE:
352 case PM_EVENT_RESTORE:
354 case PM_EVENT_RECOVER:
357 return "(unknown PM event)";
361 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
363 dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
364 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
365 ", may wakeup" : "");
368 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
371 printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
372 kobject_name(&dev->kobj), pm_verb(state.event), info, error);
375 static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
381 calltime = ktime_get();
382 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
383 do_div(usecs64, NSEC_PER_USEC);
387 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
388 info ?: "", info ? " " : "", pm_verb(state.event),
389 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
392 /*------------------------- Resume routines -------------------------*/
395 * device_resume_noirq - Execute an "early resume" callback for given device.
396 * @dev: Device to handle.
397 * @state: PM transition of the system being carried out.
399 * The driver of @dev will not receive interrupts while this function is being
402 static int device_resume_noirq(struct device *dev, pm_message_t state)
409 if (dev->bus && dev->bus->pm) {
410 pm_dev_dbg(dev, state, "EARLY ");
411 error = pm_noirq_op(dev, dev->bus->pm, state);
419 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
420 * @state: PM transition of the system being carried out.
422 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
423 * enable device drivers to receive interrupts.
425 void dpm_resume_noirq(pm_message_t state)
428 ktime_t starttime = ktime_get();
430 mutex_lock(&dpm_list_mtx);
431 transition_started = false;
432 list_for_each_entry(dev, &dpm_list, power.entry)
433 if (dev->power.status > DPM_OFF) {
436 dev->power.status = DPM_OFF;
437 error = device_resume_noirq(dev, state);
439 pm_dev_err(dev, state, " early", error);
441 mutex_unlock(&dpm_list_mtx);
442 dpm_show_time(starttime, state, "early");
443 resume_device_irqs();
445 EXPORT_SYMBOL_GPL(dpm_resume_noirq);
448 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
449 * dev: Device to resume.
450 * cb: Resume callback to execute.
452 static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
457 calltime = initcall_debug_start(dev);
460 suspend_report_result(cb, error);
462 initcall_debug_report(dev, calltime, error);
468 * device_resume - Execute "resume" callbacks for given device.
469 * @dev: Device to handle.
470 * @state: PM transition of the system being carried out.
472 static int device_resume(struct device *dev, pm_message_t state)
483 pm_dev_dbg(dev, state, "");
484 error = pm_op(dev, dev->bus->pm, state);
485 } else if (dev->bus->resume) {
486 pm_dev_dbg(dev, state, "legacy ");
487 error = legacy_resume(dev, dev->bus->resume);
495 pm_dev_dbg(dev, state, "type ");
496 error = pm_op(dev, dev->type->pm, state);
503 if (dev->class->pm) {
504 pm_dev_dbg(dev, state, "class ");
505 error = pm_op(dev, dev->class->pm, state);
506 } else if (dev->class->resume) {
507 pm_dev_dbg(dev, state, "legacy class ");
508 error = legacy_resume(dev, dev->class->resume);
519 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
520 * @state: PM transition of the system being carried out.
522 * Execute the appropriate "resume" callback for all devices whose status
523 * indicates that they are suspended.
525 static void dpm_resume(pm_message_t state)
527 struct list_head list;
528 ktime_t starttime = ktime_get();
530 INIT_LIST_HEAD(&list);
531 mutex_lock(&dpm_list_mtx);
532 while (!list_empty(&dpm_list)) {
533 struct device *dev = to_device(dpm_list.next);
536 if (dev->power.status >= DPM_OFF) {
539 dev->power.status = DPM_RESUMING;
540 mutex_unlock(&dpm_list_mtx);
542 error = device_resume(dev, state);
544 mutex_lock(&dpm_list_mtx);
546 pm_dev_err(dev, state, "", error);
547 } else if (dev->power.status == DPM_SUSPENDING) {
548 /* Allow new children of the device to be registered */
549 dev->power.status = DPM_RESUMING;
551 if (!list_empty(&dev->power.entry))
552 list_move_tail(&dev->power.entry, &list);
555 list_splice(&list, &dpm_list);
556 mutex_unlock(&dpm_list_mtx);
557 dpm_show_time(starttime, state, NULL);
561 * device_complete - Complete a PM transition for given device.
562 * @dev: Device to handle.
563 * @state: PM transition of the system being carried out.
565 static void device_complete(struct device *dev, pm_message_t state)
569 if (dev->class && dev->class->pm && dev->class->pm->complete) {
570 pm_dev_dbg(dev, state, "completing class ");
571 dev->class->pm->complete(dev);
574 if (dev->type && dev->type->pm && dev->type->pm->complete) {
575 pm_dev_dbg(dev, state, "completing type ");
576 dev->type->pm->complete(dev);
579 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
580 pm_dev_dbg(dev, state, "completing ");
581 dev->bus->pm->complete(dev);
588 * dpm_complete - Complete a PM transition for all non-sysdev devices.
589 * @state: PM transition of the system being carried out.
591 * Execute the ->complete() callbacks for all devices whose PM status is not
592 * DPM_ON (this allows new devices to be registered).
594 static void dpm_complete(pm_message_t state)
596 struct list_head list;
598 INIT_LIST_HEAD(&list);
599 mutex_lock(&dpm_list_mtx);
600 transition_started = false;
601 while (!list_empty(&dpm_list)) {
602 struct device *dev = to_device(dpm_list.prev);
605 if (dev->power.status > DPM_ON) {
606 dev->power.status = DPM_ON;
607 mutex_unlock(&dpm_list_mtx);
609 device_complete(dev, state);
610 pm_runtime_put_sync(dev);
612 mutex_lock(&dpm_list_mtx);
614 if (!list_empty(&dev->power.entry))
615 list_move(&dev->power.entry, &list);
618 list_splice(&list, &dpm_list);
619 mutex_unlock(&dpm_list_mtx);
623 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
624 * @state: PM transition of the system being carried out.
626 * Execute "resume" callbacks for all devices and complete the PM transition of
629 void dpm_resume_end(pm_message_t state)
635 EXPORT_SYMBOL_GPL(dpm_resume_end);
638 /*------------------------- Suspend routines -------------------------*/
641 * resume_event - Return a "resume" message for given "suspend" sleep state.
642 * @sleep_state: PM message representing a sleep state.
644 * Return a PM message representing the resume event corresponding to given
647 static pm_message_t resume_event(pm_message_t sleep_state)
649 switch (sleep_state.event) {
650 case PM_EVENT_SUSPEND:
652 case PM_EVENT_FREEZE:
653 case PM_EVENT_QUIESCE:
655 case PM_EVENT_HIBERNATE:
662 * device_suspend_noirq - Execute a "late suspend" callback for given device.
663 * @dev: Device to handle.
664 * @state: PM transition of the system being carried out.
666 * The driver of @dev will not receive interrupts while this function is being
669 static int device_suspend_noirq(struct device *dev, pm_message_t state)
673 if (dev->bus && dev->bus->pm) {
674 pm_dev_dbg(dev, state, "LATE ");
675 error = pm_noirq_op(dev, dev->bus->pm, state);
681 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
682 * @state: PM transition of the system being carried out.
684 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
685 * handlers for all non-sysdev devices.
687 int dpm_suspend_noirq(pm_message_t state)
690 ktime_t starttime = ktime_get();
693 suspend_device_irqs();
694 mutex_lock(&dpm_list_mtx);
695 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
696 error = device_suspend_noirq(dev, state);
698 pm_dev_err(dev, state, " late", error);
701 dev->power.status = DPM_OFF_IRQ;
703 mutex_unlock(&dpm_list_mtx);
705 dpm_resume_noirq(resume_event(state));
707 dpm_show_time(starttime, state, "late");
710 EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
713 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
714 * dev: Device to suspend.
715 * cb: Suspend callback to execute.
717 static int legacy_suspend(struct device *dev, pm_message_t state,
718 int (*cb)(struct device *dev, pm_message_t state))
723 calltime = initcall_debug_start(dev);
725 error = cb(dev, state);
726 suspend_report_result(cb, error);
728 initcall_debug_report(dev, calltime, error);
734 * device_suspend - Execute "suspend" callbacks for given device.
735 * @dev: Device to handle.
736 * @state: PM transition of the system being carried out.
738 static int device_suspend(struct device *dev, pm_message_t state)
745 if (dev->class->pm) {
746 pm_dev_dbg(dev, state, "class ");
747 error = pm_op(dev, dev->class->pm, state);
748 } else if (dev->class->suspend) {
749 pm_dev_dbg(dev, state, "legacy class ");
750 error = legacy_suspend(dev, state, dev->class->suspend);
758 pm_dev_dbg(dev, state, "type ");
759 error = pm_op(dev, dev->type->pm, state);
767 pm_dev_dbg(dev, state, "");
768 error = pm_op(dev, dev->bus->pm, state);
769 } else if (dev->bus->suspend) {
770 pm_dev_dbg(dev, state, "legacy ");
771 error = legacy_suspend(dev, state, dev->bus->suspend);
781 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
782 * @state: PM transition of the system being carried out.
784 static int dpm_suspend(pm_message_t state)
786 struct list_head list;
787 ktime_t starttime = ktime_get();
790 INIT_LIST_HEAD(&list);
791 mutex_lock(&dpm_list_mtx);
792 while (!list_empty(&dpm_list)) {
793 struct device *dev = to_device(dpm_list.prev);
796 mutex_unlock(&dpm_list_mtx);
798 error = device_suspend(dev, state);
800 mutex_lock(&dpm_list_mtx);
802 pm_dev_err(dev, state, "", error);
806 dev->power.status = DPM_OFF;
807 if (!list_empty(&dev->power.entry))
808 list_move(&dev->power.entry, &list);
811 list_splice(&list, dpm_list.prev);
812 mutex_unlock(&dpm_list_mtx);
814 dpm_show_time(starttime, state, NULL);
819 * device_prepare - Prepare a device for system power transition.
820 * @dev: Device to handle.
821 * @state: PM transition of the system being carried out.
823 * Execute the ->prepare() callback(s) for given device. No new children of the
824 * device may be registered after this function has returned.
826 static int device_prepare(struct device *dev, pm_message_t state)
832 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
833 pm_dev_dbg(dev, state, "preparing ");
834 error = dev->bus->pm->prepare(dev);
835 suspend_report_result(dev->bus->pm->prepare, error);
840 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
841 pm_dev_dbg(dev, state, "preparing type ");
842 error = dev->type->pm->prepare(dev);
843 suspend_report_result(dev->type->pm->prepare, error);
848 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
849 pm_dev_dbg(dev, state, "preparing class ");
850 error = dev->class->pm->prepare(dev);
851 suspend_report_result(dev->class->pm->prepare, error);
860 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
861 * @state: PM transition of the system being carried out.
863 * Execute the ->prepare() callback(s) for all devices.
865 static int dpm_prepare(pm_message_t state)
867 struct list_head list;
870 INIT_LIST_HEAD(&list);
871 mutex_lock(&dpm_list_mtx);
872 transition_started = true;
873 while (!list_empty(&dpm_list)) {
874 struct device *dev = to_device(dpm_list.next);
877 dev->power.status = DPM_PREPARING;
878 mutex_unlock(&dpm_list_mtx);
880 pm_runtime_get_noresume(dev);
881 if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
882 /* Wake-up requested during system sleep transition. */
883 pm_runtime_put_sync(dev);
886 error = device_prepare(dev, state);
889 mutex_lock(&dpm_list_mtx);
891 dev->power.status = DPM_ON;
892 if (error == -EAGAIN) {
897 printk(KERN_ERR "PM: Failed to prepare device %s "
898 "for power transition: error %d\n",
899 kobject_name(&dev->kobj), error);
903 dev->power.status = DPM_SUSPENDING;
904 if (!list_empty(&dev->power.entry))
905 list_move_tail(&dev->power.entry, &list);
908 list_splice(&list, &dpm_list);
909 mutex_unlock(&dpm_list_mtx);
914 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
915 * @state: PM transition of the system being carried out.
917 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
918 * callbacks for them.
920 int dpm_suspend_start(pm_message_t state)
925 error = dpm_prepare(state);
927 error = dpm_suspend(state);
930 EXPORT_SYMBOL_GPL(dpm_suspend_start);
932 void __suspend_report_result(const char *function, void *fn, int ret)
935 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
937 EXPORT_SYMBOL_GPL(__suspend_report_result);