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)
375 if (dev->bus && dev->bus->pm) {
376 pm_dev_dbg(dev, state, "EARLY ");
377 error = pm_noirq_op(dev, dev->bus->pm, state);
385 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
386 * @state: PM transition of the system being carried out.
388 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
389 * enable device drivers to receive interrupts.
391 void dpm_resume_noirq(pm_message_t state)
395 mutex_lock(&dpm_list_mtx);
396 transition_started = false;
397 list_for_each_entry(dev, &dpm_list, power.entry)
398 if (dev->power.status > DPM_OFF) {
401 dev->power.status = DPM_OFF;
402 error = device_resume_noirq(dev, state);
404 pm_dev_err(dev, state, " early", error);
406 mutex_unlock(&dpm_list_mtx);
407 resume_device_irqs();
409 EXPORT_SYMBOL_GPL(dpm_resume_noirq);
412 * device_resume - Execute "resume" callbacks for given device.
413 * @dev: Device to handle.
414 * @state: PM transition of the system being carried out.
416 static int device_resume(struct device *dev, pm_message_t state)
427 pm_dev_dbg(dev, state, "");
428 error = pm_op(dev, dev->bus->pm, state);
429 } else if (dev->bus->resume) {
430 pm_dev_dbg(dev, state, "legacy ");
431 error = dev->bus->resume(dev);
439 pm_dev_dbg(dev, state, "type ");
440 error = pm_op(dev, dev->type->pm, state);
447 if (dev->class->pm) {
448 pm_dev_dbg(dev, state, "class ");
449 error = pm_op(dev, dev->class->pm, state);
450 } else if (dev->class->resume) {
451 pm_dev_dbg(dev, state, "legacy class ");
452 error = dev->class->resume(dev);
463 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
464 * @state: PM transition of the system being carried out.
466 * Execute the appropriate "resume" callback for all devices whose status
467 * indicates that they are suspended.
469 static void dpm_resume(pm_message_t state)
471 struct list_head list;
473 INIT_LIST_HEAD(&list);
474 mutex_lock(&dpm_list_mtx);
475 while (!list_empty(&dpm_list)) {
476 struct device *dev = to_device(dpm_list.next);
479 if (dev->power.status >= DPM_OFF) {
482 dev->power.status = DPM_RESUMING;
483 mutex_unlock(&dpm_list_mtx);
485 error = device_resume(dev, state);
487 mutex_lock(&dpm_list_mtx);
489 pm_dev_err(dev, state, "", error);
490 } else if (dev->power.status == DPM_SUSPENDING) {
491 /* Allow new children of the device to be registered */
492 dev->power.status = DPM_RESUMING;
494 if (!list_empty(&dev->power.entry))
495 list_move_tail(&dev->power.entry, &list);
498 list_splice(&list, &dpm_list);
499 mutex_unlock(&dpm_list_mtx);
503 * device_complete - Complete a PM transition for given device.
504 * @dev: Device to handle.
505 * @state: PM transition of the system being carried out.
507 static void device_complete(struct device *dev, pm_message_t state)
511 if (dev->class && dev->class->pm && dev->class->pm->complete) {
512 pm_dev_dbg(dev, state, "completing class ");
513 dev->class->pm->complete(dev);
516 if (dev->type && dev->type->pm && dev->type->pm->complete) {
517 pm_dev_dbg(dev, state, "completing type ");
518 dev->type->pm->complete(dev);
521 if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
522 pm_dev_dbg(dev, state, "completing ");
523 dev->bus->pm->complete(dev);
530 * dpm_complete - Complete a PM transition for all non-sysdev devices.
531 * @state: PM transition of the system being carried out.
533 * Execute the ->complete() callbacks for all devices whose PM status is not
534 * DPM_ON (this allows new devices to be registered).
536 static void dpm_complete(pm_message_t state)
538 struct list_head list;
540 INIT_LIST_HEAD(&list);
541 mutex_lock(&dpm_list_mtx);
542 transition_started = false;
543 while (!list_empty(&dpm_list)) {
544 struct device *dev = to_device(dpm_list.prev);
547 if (dev->power.status > DPM_ON) {
548 dev->power.status = DPM_ON;
549 mutex_unlock(&dpm_list_mtx);
551 device_complete(dev, state);
552 pm_runtime_put_noidle(dev);
554 mutex_lock(&dpm_list_mtx);
556 if (!list_empty(&dev->power.entry))
557 list_move(&dev->power.entry, &list);
560 list_splice(&list, &dpm_list);
561 mutex_unlock(&dpm_list_mtx);
565 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
566 * @state: PM transition of the system being carried out.
568 * Execute "resume" callbacks for all devices and complete the PM transition of
571 void dpm_resume_end(pm_message_t state)
577 EXPORT_SYMBOL_GPL(dpm_resume_end);
580 /*------------------------- Suspend routines -------------------------*/
583 * resume_event - Return a "resume" message for given "suspend" sleep state.
584 * @sleep_state: PM message representing a sleep state.
586 * Return a PM message representing the resume event corresponding to given
589 static pm_message_t resume_event(pm_message_t sleep_state)
591 switch (sleep_state.event) {
592 case PM_EVENT_SUSPEND:
594 case PM_EVENT_FREEZE:
595 case PM_EVENT_QUIESCE:
597 case PM_EVENT_HIBERNATE:
604 * device_suspend_noirq - Execute a "late suspend" callback for given device.
605 * @dev: Device to handle.
606 * @state: PM transition of the system being carried out.
608 * The driver of @dev will not receive interrupts while this function is being
611 static int device_suspend_noirq(struct device *dev, pm_message_t state)
615 if (dev->bus && dev->bus->pm) {
616 pm_dev_dbg(dev, state, "LATE ");
617 error = pm_noirq_op(dev, dev->bus->pm, state);
623 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
624 * @state: PM transition of the system being carried out.
626 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
627 * handlers for all non-sysdev devices.
629 int dpm_suspend_noirq(pm_message_t state)
634 suspend_device_irqs();
635 mutex_lock(&dpm_list_mtx);
636 list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
637 error = device_suspend_noirq(dev, state);
639 pm_dev_err(dev, state, " late", error);
642 dev->power.status = DPM_OFF_IRQ;
644 mutex_unlock(&dpm_list_mtx);
646 dpm_resume_noirq(resume_event(state));
649 EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
652 * device_suspend - Execute "suspend" callbacks for given device.
653 * @dev: Device to handle.
654 * @state: PM transition of the system being carried out.
656 static int device_suspend(struct device *dev, pm_message_t state)
663 if (dev->class->pm) {
664 pm_dev_dbg(dev, state, "class ");
665 error = pm_op(dev, dev->class->pm, state);
666 } else if (dev->class->suspend) {
667 pm_dev_dbg(dev, state, "legacy class ");
668 error = dev->class->suspend(dev, state);
669 suspend_report_result(dev->class->suspend, error);
677 pm_dev_dbg(dev, state, "type ");
678 error = pm_op(dev, dev->type->pm, state);
686 pm_dev_dbg(dev, state, "");
687 error = pm_op(dev, dev->bus->pm, state);
688 } else if (dev->bus->suspend) {
689 pm_dev_dbg(dev, state, "legacy ");
690 error = dev->bus->suspend(dev, state);
691 suspend_report_result(dev->bus->suspend, error);
701 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
702 * @state: PM transition of the system being carried out.
704 static int dpm_suspend(pm_message_t state)
706 struct list_head list;
709 INIT_LIST_HEAD(&list);
710 mutex_lock(&dpm_list_mtx);
711 while (!list_empty(&dpm_list)) {
712 struct device *dev = to_device(dpm_list.prev);
715 mutex_unlock(&dpm_list_mtx);
717 error = device_suspend(dev, state);
719 mutex_lock(&dpm_list_mtx);
721 pm_dev_err(dev, state, "", error);
725 dev->power.status = DPM_OFF;
726 if (!list_empty(&dev->power.entry))
727 list_move(&dev->power.entry, &list);
730 list_splice(&list, dpm_list.prev);
731 mutex_unlock(&dpm_list_mtx);
736 * device_prepare - Prepare a device for system power transition.
737 * @dev: Device to handle.
738 * @state: PM transition of the system being carried out.
740 * Execute the ->prepare() callback(s) for given device. No new children of the
741 * device may be registered after this function has returned.
743 static int device_prepare(struct device *dev, pm_message_t state)
749 if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
750 pm_dev_dbg(dev, state, "preparing ");
751 error = dev->bus->pm->prepare(dev);
752 suspend_report_result(dev->bus->pm->prepare, error);
757 if (dev->type && dev->type->pm && dev->type->pm->prepare) {
758 pm_dev_dbg(dev, state, "preparing type ");
759 error = dev->type->pm->prepare(dev);
760 suspend_report_result(dev->type->pm->prepare, error);
765 if (dev->class && dev->class->pm && dev->class->pm->prepare) {
766 pm_dev_dbg(dev, state, "preparing class ");
767 error = dev->class->pm->prepare(dev);
768 suspend_report_result(dev->class->pm->prepare, error);
777 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
778 * @state: PM transition of the system being carried out.
780 * Execute the ->prepare() callback(s) for all devices.
782 static int dpm_prepare(pm_message_t state)
784 struct list_head list;
787 INIT_LIST_HEAD(&list);
788 mutex_lock(&dpm_list_mtx);
789 transition_started = true;
790 while (!list_empty(&dpm_list)) {
791 struct device *dev = to_device(dpm_list.next);
794 dev->power.status = DPM_PREPARING;
795 mutex_unlock(&dpm_list_mtx);
797 pm_runtime_get_noresume(dev);
798 if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
799 /* Wake-up requested during system sleep transition. */
800 pm_runtime_put_noidle(dev);
803 error = device_prepare(dev, state);
806 mutex_lock(&dpm_list_mtx);
808 dev->power.status = DPM_ON;
809 if (error == -EAGAIN) {
814 printk(KERN_ERR "PM: Failed to prepare device %s "
815 "for power transition: error %d\n",
816 kobject_name(&dev->kobj), error);
820 dev->power.status = DPM_SUSPENDING;
821 if (!list_empty(&dev->power.entry))
822 list_move_tail(&dev->power.entry, &list);
825 list_splice(&list, &dpm_list);
826 mutex_unlock(&dpm_list_mtx);
831 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
832 * @state: PM transition of the system being carried out.
834 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
835 * callbacks for them.
837 int dpm_suspend_start(pm_message_t state)
842 error = dpm_prepare(state);
844 error = dpm_suspend(state);
847 EXPORT_SYMBOL_GPL(dpm_suspend_start);
849 void __suspend_report_result(const char *function, void *fn, int ret)
852 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
854 EXPORT_SYMBOL_GPL(__suspend_report_result);