18d66624b499e0c789626228b82926fd0685c64f
[linux-2.6.git] / drivers / base / power / main.c
1 /*
2  * drivers/base/power/main.c - Where the driver meets power management.
3  *
4  * Copyright (c) 2003 Patrick Mochel
5  * Copyright (c) 2003 Open Source Development Lab
6  *
7  * This file is released under the GPLv2
8  *
9  *
10  * The driver model core calls device_pm_add() when a device is registered.
11  * This will initialize 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.
14  *
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.
18  */
19
20 #include <linux/device.h>
21 #include <linux/kallsyms.h>
22 #include <linux/mutex.h>
23 #include <linux/pm.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/resume-trace.h>
26 #include <linux/interrupt.h>
27 #include <linux/sched.h>
28 #include <linux/async.h>
29 #include <linux/suspend.h>
30 #include <linux/timer.h>
31
32 #include "../base.h"
33 #include "power.h"
34
35 /*
36  * The entries in the dpm_list list are in a depth first order, simply
37  * because children are guaranteed to be discovered after parents, and
38  * are inserted at the back of the list on discovery.
39  *
40  * Since device_pm_add() may be called with a device lock held,
41  * we must never try to acquire a device lock while holding
42  * dpm_list_mutex.
43  */
44
45 LIST_HEAD(dpm_list);
46 LIST_HEAD(dpm_prepared_list);
47 LIST_HEAD(dpm_suspended_list);
48 LIST_HEAD(dpm_noirq_list);
49
50 static DEFINE_MUTEX(dpm_list_mtx);
51 static pm_message_t pm_transition;
52
53 static void dpm_drv_timeout(unsigned long data);
54 struct dpm_drv_wd_data {
55         struct device *dev;
56         struct task_struct *tsk;
57 };
58
59 static int async_error;
60
61 /**
62  * device_pm_init - Initialize the PM-related part of a device object.
63  * @dev: Device object being initialized.
64  */
65 void device_pm_init(struct device *dev)
66 {
67         dev->power.is_prepared = false;
68         dev->power.is_suspended = false;
69         init_completion(&dev->power.completion);
70         complete_all(&dev->power.completion);
71         dev->power.wakeup = NULL;
72         spin_lock_init(&dev->power.lock);
73         pm_runtime_init(dev);
74         INIT_LIST_HEAD(&dev->power.entry);
75 }
76
77 /**
78  * device_pm_lock - Lock the list of active devices used by the PM core.
79  */
80 void device_pm_lock(void)
81 {
82         mutex_lock(&dpm_list_mtx);
83 }
84
85 /**
86  * device_pm_unlock - Unlock the list of active devices used by the PM core.
87  */
88 void device_pm_unlock(void)
89 {
90         mutex_unlock(&dpm_list_mtx);
91 }
92
93 /**
94  * device_pm_add - Add a device to the PM core's list of active devices.
95  * @dev: Device to add to the list.
96  */
97 void device_pm_add(struct device *dev)
98 {
99         pr_debug("PM: Adding info for %s:%s\n",
100                  dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
101         mutex_lock(&dpm_list_mtx);
102         if (dev->parent && dev->parent->power.is_prepared)
103                 dev_warn(dev, "parent %s should not be sleeping\n",
104                         dev_name(dev->parent));
105         list_add_tail(&dev->power.entry, &dpm_list);
106         mutex_unlock(&dpm_list_mtx);
107 }
108
109 /**
110  * device_pm_remove - Remove a device from the PM core's list of active devices.
111  * @dev: Device to be removed from the list.
112  */
113 void device_pm_remove(struct device *dev)
114 {
115         pr_debug("PM: Removing info for %s:%s\n",
116                  dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
117         complete_all(&dev->power.completion);
118         mutex_lock(&dpm_list_mtx);
119         list_del_init(&dev->power.entry);
120         mutex_unlock(&dpm_list_mtx);
121         device_wakeup_disable(dev);
122         pm_runtime_remove(dev);
123 }
124
125 /**
126  * device_pm_move_before - Move device in the PM core's list of active devices.
127  * @deva: Device to move in dpm_list.
128  * @devb: Device @deva should come before.
129  */
130 void device_pm_move_before(struct device *deva, struct device *devb)
131 {
132         pr_debug("PM: Moving %s:%s before %s:%s\n",
133                  deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
134                  devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
135         /* Delete deva from dpm_list and reinsert before devb. */
136         list_move_tail(&deva->power.entry, &devb->power.entry);
137 }
138
139 /**
140  * device_pm_move_after - Move device in the PM core's list of active devices.
141  * @deva: Device to move in dpm_list.
142  * @devb: Device @deva should come after.
143  */
144 void device_pm_move_after(struct device *deva, struct device *devb)
145 {
146         pr_debug("PM: Moving %s:%s after %s:%s\n",
147                  deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
148                  devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
149         /* Delete deva from dpm_list and reinsert after devb. */
150         list_move(&deva->power.entry, &devb->power.entry);
151 }
152
153 /**
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.
156  */
157 void device_pm_move_last(struct device *dev)
158 {
159         pr_debug("PM: Moving %s:%s to end of list\n",
160                  dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
161         list_move_tail(&dev->power.entry, &dpm_list);
162 }
163
164 static ktime_t initcall_debug_start(struct device *dev)
165 {
166         ktime_t calltime = ktime_set(0, 0);
167
168         if (initcall_debug) {
169                 pr_info("calling  %s+ @ %i\n",
170                                 dev_name(dev), task_pid_nr(current));
171                 calltime = ktime_get();
172         }
173
174         return calltime;
175 }
176
177 static void initcall_debug_report(struct device *dev, ktime_t calltime,
178                                   int error)
179 {
180         ktime_t delta, rettime;
181
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);
187         }
188 }
189
190 /**
191  * dpm_wait - Wait for a PM operation to complete.
192  * @dev: Device to wait for.
193  * @async: If unset, wait only if the device's power.async_suspend flag is set.
194  */
195 static void dpm_wait(struct device *dev, bool async)
196 {
197         if (!dev)
198                 return;
199
200         if (async || (pm_async_enabled && dev->power.async_suspend))
201                 wait_for_completion(&dev->power.completion);
202 }
203
204 static int dpm_wait_fn(struct device *dev, void *async_ptr)
205 {
206         dpm_wait(dev, *((bool *)async_ptr));
207         return 0;
208 }
209
210 static void dpm_wait_for_children(struct device *dev, bool async)
211 {
212        device_for_each_child(dev, &async, dpm_wait_fn);
213 }
214
215 /**
216  * pm_op - Execute the PM operation appropriate for given PM event.
217  * @dev: Device to handle.
218  * @ops: PM operations to choose from.
219  * @state: PM transition of the system being carried out.
220  */
221 static int pm_op(struct device *dev,
222                  const struct dev_pm_ops *ops,
223                  pm_message_t state)
224 {
225         int error = 0;
226         ktime_t calltime;
227
228         calltime = initcall_debug_start(dev);
229
230         switch (state.event) {
231 #ifdef CONFIG_SUSPEND
232         case PM_EVENT_SUSPEND:
233                 if (ops->suspend) {
234                         error = ops->suspend(dev);
235                         suspend_report_result(ops->suspend, error);
236                 }
237                 break;
238         case PM_EVENT_RESUME:
239                 if (ops->resume) {
240                         error = ops->resume(dev);
241                         suspend_report_result(ops->resume, error);
242                 }
243                 break;
244 #endif /* CONFIG_SUSPEND */
245 #ifdef CONFIG_HIBERNATE_CALLBACKS
246         case PM_EVENT_FREEZE:
247         case PM_EVENT_QUIESCE:
248                 if (ops->freeze) {
249                         error = ops->freeze(dev);
250                         suspend_report_result(ops->freeze, error);
251                 }
252                 break;
253         case PM_EVENT_HIBERNATE:
254                 if (ops->poweroff) {
255                         error = ops->poweroff(dev);
256                         suspend_report_result(ops->poweroff, error);
257                 }
258                 break;
259         case PM_EVENT_THAW:
260         case PM_EVENT_RECOVER:
261                 if (ops->thaw) {
262                         error = ops->thaw(dev);
263                         suspend_report_result(ops->thaw, error);
264                 }
265                 break;
266         case PM_EVENT_RESTORE:
267                 if (ops->restore) {
268                         error = ops->restore(dev);
269                         suspend_report_result(ops->restore, error);
270                 }
271                 break;
272 #endif /* CONFIG_HIBERNATE_CALLBACKS */
273         default:
274                 error = -EINVAL;
275         }
276
277         initcall_debug_report(dev, calltime, error);
278
279         return error;
280 }
281
282 /**
283  * pm_noirq_op - Execute the PM operation appropriate for given PM event.
284  * @dev: Device to handle.
285  * @ops: PM operations to choose from.
286  * @state: PM transition of the system being carried out.
287  *
288  * The driver of @dev will not receive interrupts while this function is being
289  * executed.
290  */
291 static int pm_noirq_op(struct device *dev,
292                         const struct dev_pm_ops *ops,
293                         pm_message_t state)
294 {
295         int error = 0;
296         ktime_t calltime = ktime_set(0, 0), delta, rettime;
297
298         if (initcall_debug) {
299                 pr_info("calling  %s+ @ %i, parent: %s\n",
300                                 dev_name(dev), task_pid_nr(current),
301                                 dev->parent ? dev_name(dev->parent) : "none");
302                 calltime = ktime_get();
303         }
304
305         switch (state.event) {
306 #ifdef CONFIG_SUSPEND
307         case PM_EVENT_SUSPEND:
308                 if (ops->suspend_noirq) {
309                         error = ops->suspend_noirq(dev);
310                         suspend_report_result(ops->suspend_noirq, error);
311                 }
312                 break;
313         case PM_EVENT_RESUME:
314                 if (ops->resume_noirq) {
315                         error = ops->resume_noirq(dev);
316                         suspend_report_result(ops->resume_noirq, error);
317                 }
318                 break;
319 #endif /* CONFIG_SUSPEND */
320 #ifdef CONFIG_HIBERNATE_CALLBACKS
321         case PM_EVENT_FREEZE:
322         case PM_EVENT_QUIESCE:
323                 if (ops->freeze_noirq) {
324                         error = ops->freeze_noirq(dev);
325                         suspend_report_result(ops->freeze_noirq, error);
326                 }
327                 break;
328         case PM_EVENT_HIBERNATE:
329                 if (ops->poweroff_noirq) {
330                         error = ops->poweroff_noirq(dev);
331                         suspend_report_result(ops->poweroff_noirq, error);
332                 }
333                 break;
334         case PM_EVENT_THAW:
335         case PM_EVENT_RECOVER:
336                 if (ops->thaw_noirq) {
337                         error = ops->thaw_noirq(dev);
338                         suspend_report_result(ops->thaw_noirq, error);
339                 }
340                 break;
341         case PM_EVENT_RESTORE:
342                 if (ops->restore_noirq) {
343                         error = ops->restore_noirq(dev);
344                         suspend_report_result(ops->restore_noirq, error);
345                 }
346                 break;
347 #endif /* CONFIG_HIBERNATE_CALLBACKS */
348         default:
349                 error = -EINVAL;
350         }
351
352         if (initcall_debug) {
353                 rettime = ktime_get();
354                 delta = ktime_sub(rettime, calltime);
355                 printk("initcall %s_i+ returned %d after %Ld usecs\n",
356                         dev_name(dev), error,
357                         (unsigned long long)ktime_to_ns(delta) >> 10);
358         }
359
360         return error;
361 }
362
363 static char *pm_verb(int event)
364 {
365         switch (event) {
366         case PM_EVENT_SUSPEND:
367                 return "suspend";
368         case PM_EVENT_RESUME:
369                 return "resume";
370         case PM_EVENT_FREEZE:
371                 return "freeze";
372         case PM_EVENT_QUIESCE:
373                 return "quiesce";
374         case PM_EVENT_HIBERNATE:
375                 return "hibernate";
376         case PM_EVENT_THAW:
377                 return "thaw";
378         case PM_EVENT_RESTORE:
379                 return "restore";
380         case PM_EVENT_RECOVER:
381                 return "recover";
382         default:
383                 return "(unknown PM event)";
384         }
385 }
386
387 static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
388 {
389         dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
390                 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
391                 ", may wakeup" : "");
392 }
393
394 static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
395                         int error)
396 {
397         printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
398                 dev_name(dev), pm_verb(state.event), info, error);
399 }
400
401 static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
402 {
403         ktime_t calltime;
404         u64 usecs64;
405         int usecs;
406
407         calltime = ktime_get();
408         usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
409         do_div(usecs64, NSEC_PER_USEC);
410         usecs = usecs64;
411         if (usecs == 0)
412                 usecs = 1;
413         pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
414                 info ?: "", info ? " " : "", pm_verb(state.event),
415                 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
416 }
417
418 /*------------------------- Resume routines -------------------------*/
419
420 /**
421  * device_resume_noirq - Execute an "early resume" callback for given device.
422  * @dev: Device to handle.
423  * @state: PM transition of the system being carried out.
424  *
425  * The driver of @dev will not receive interrupts while this function is being
426  * executed.
427  */
428 static int device_resume_noirq(struct device *dev, pm_message_t state)
429 {
430         int error = 0;
431
432         TRACE_DEVICE(dev);
433         TRACE_RESUME(0);
434
435         if (dev->pm_domain) {
436                 pm_dev_dbg(dev, state, "EARLY power domain ");
437                 error = pm_noirq_op(dev, &dev->pm_domain->ops, state);
438         } else if (dev->type && dev->type->pm) {
439                 pm_dev_dbg(dev, state, "EARLY type ");
440                 error = pm_noirq_op(dev, dev->type->pm, state);
441         } else if (dev->class && dev->class->pm) {
442                 pm_dev_dbg(dev, state, "EARLY class ");
443                 error = pm_noirq_op(dev, dev->class->pm, state);
444         } else if (dev->bus && dev->bus->pm) {
445                 pm_dev_dbg(dev, state, "EARLY ");
446                 error = pm_noirq_op(dev, dev->bus->pm, state);
447         }
448
449         TRACE_RESUME(error);
450         return error;
451 }
452
453 /**
454  * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
455  * @state: PM transition of the system being carried out.
456  *
457  * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
458  * enable device drivers to receive interrupts.
459  */
460 void dpm_resume_noirq(pm_message_t state)
461 {
462         ktime_t starttime = ktime_get();
463
464         mutex_lock(&dpm_list_mtx);
465         while (!list_empty(&dpm_noirq_list)) {
466                 struct device *dev = to_device(dpm_noirq_list.next);
467                 int error;
468
469                 get_device(dev);
470                 list_move_tail(&dev->power.entry, &dpm_suspended_list);
471                 mutex_unlock(&dpm_list_mtx);
472
473                 error = device_resume_noirq(dev, state);
474                 if (error)
475                         pm_dev_err(dev, state, " early", error);
476
477                 mutex_lock(&dpm_list_mtx);
478                 put_device(dev);
479         }
480         mutex_unlock(&dpm_list_mtx);
481         dpm_show_time(starttime, state, "early");
482         resume_device_irqs();
483 }
484 EXPORT_SYMBOL_GPL(dpm_resume_noirq);
485
486 /**
487  * legacy_resume - Execute a legacy (bus or class) resume callback for device.
488  * @dev: Device to resume.
489  * @cb: Resume callback to execute.
490  */
491 static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
492 {
493         int error;
494         ktime_t calltime;
495
496         calltime = initcall_debug_start(dev);
497
498         error = cb(dev);
499         suspend_report_result(cb, error);
500
501         initcall_debug_report(dev, calltime, error);
502
503         return error;
504 }
505
506 /**
507  * device_resume - Execute "resume" callbacks for given device.
508  * @dev: Device to handle.
509  * @state: PM transition of the system being carried out.
510  * @async: If true, the device is being resumed asynchronously.
511  */
512 static int device_resume(struct device *dev, pm_message_t state, bool async)
513 {
514         int error = 0;
515         bool put = false;
516
517         TRACE_DEVICE(dev);
518         TRACE_RESUME(0);
519
520         if (dev->parent && dev->parent->power.in_suspend)
521                 dpm_wait(dev->parent, async);
522         device_lock(dev);
523
524         /*
525          * This is a fib.  But we'll allow new children to be added below
526          * a resumed device, even if the device hasn't been completed yet.
527          */
528         dev->power.is_prepared = false;
529
530         if (!dev->power.is_suspended)
531                 goto Unlock;
532
533         pm_runtime_enable(dev);
534         put = true;
535
536         if (dev->pm_domain) {
537                 pm_dev_dbg(dev, state, "power domain ");
538                 error = pm_op(dev, &dev->pm_domain->ops, state);
539                 goto End;
540         }
541
542         if (dev->type && dev->type->pm) {
543                 pm_dev_dbg(dev, state, "type ");
544                 error = pm_op(dev, dev->type->pm, state);
545                 goto End;
546         }
547
548         if (dev->class) {
549                 if (dev->class->pm) {
550                         pm_dev_dbg(dev, state, "class ");
551                         error = pm_op(dev, dev->class->pm, state);
552                         goto End;
553                 } else if (dev->class->resume) {
554                         pm_dev_dbg(dev, state, "legacy class ");
555                         error = legacy_resume(dev, dev->class->resume);
556                         goto End;
557                 }
558         }
559
560         if (dev->bus) {
561                 if (dev->bus->pm) {
562                         pm_dev_dbg(dev, state, "");
563                         error = pm_op(dev, dev->bus->pm, state);
564                 } else if (dev->bus->resume) {
565                         pm_dev_dbg(dev, state, "legacy ");
566                         error = legacy_resume(dev, dev->bus->resume);
567                 }
568         }
569
570  End:
571         dev->power.is_suspended = false;
572
573  Unlock:
574         device_unlock(dev);
575         complete_all(&dev->power.completion);
576
577         TRACE_RESUME(error);
578
579         if (put)
580                 pm_runtime_put_sync(dev);
581
582         return error;
583 }
584
585 static void async_resume(void *data, async_cookie_t cookie)
586 {
587         struct device *dev = (struct device *)data;
588         int error;
589
590         error = device_resume(dev, pm_transition, true);
591         if (error)
592                 pm_dev_err(dev, pm_transition, " async", error);
593         put_device(dev);
594 }
595
596 static bool is_async(struct device *dev)
597 {
598         return dev->power.async_suspend && pm_async_enabled
599                 && !pm_trace_is_enabled();
600 }
601
602 /**
603  *      dpm_drv_timeout - Driver suspend / resume watchdog handler
604  *      @data: struct device which timed out
605  *
606  *      Called when a driver has timed out suspending or resuming.
607  *      There's not much we can do here to recover so
608  *      BUG() out for a crash-dump
609  *
610  */
611 static void dpm_drv_timeout(unsigned long data)
612 {
613         struct dpm_drv_wd_data *wd_data = (void *)data;
614         struct device *dev = wd_data->dev;
615         struct task_struct *tsk = wd_data->tsk;
616
617         printk(KERN_EMERG "**** DPM device timeout: %s (%s)\n", dev_name(dev),
618                (dev->driver ? dev->driver->name : "no driver"));
619
620         printk(KERN_EMERG "dpm suspend stack:\n");
621         show_stack(tsk, NULL);
622
623         BUG();
624 }
625
626 /**
627  * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
628  * @state: PM transition of the system being carried out.
629  *
630  * Execute the appropriate "resume" callback for all devices whose status
631  * indicates that they are suspended.
632  */
633 void dpm_resume(pm_message_t state)
634 {
635         struct device *dev;
636         ktime_t starttime = ktime_get();
637
638         might_sleep();
639
640         mutex_lock(&dpm_list_mtx);
641         pm_transition = state;
642         async_error = 0;
643
644         list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
645                 INIT_COMPLETION(dev->power.completion);
646                 if (is_async(dev)) {
647                         get_device(dev);
648                         async_schedule(async_resume, dev);
649                 }
650         }
651
652         while (!list_empty(&dpm_suspended_list)) {
653                 dev = to_device(dpm_suspended_list.next);
654                 get_device(dev);
655                 if (!is_async(dev)) {
656                         int error;
657
658                         mutex_unlock(&dpm_list_mtx);
659
660                         error = device_resume(dev, state, false);
661                         if (error)
662                                 pm_dev_err(dev, state, "", error);
663
664                         mutex_lock(&dpm_list_mtx);
665                 }
666                 if (!list_empty(&dev->power.entry))
667                         list_move_tail(&dev->power.entry, &dpm_prepared_list);
668                 put_device(dev);
669         }
670         mutex_unlock(&dpm_list_mtx);
671         async_synchronize_full();
672         dpm_show_time(starttime, state, NULL);
673 }
674
675 /**
676  * device_complete - Complete a PM transition for given device.
677  * @dev: Device to handle.
678  * @state: PM transition of the system being carried out.
679  */
680 static void device_complete(struct device *dev, pm_message_t state)
681 {
682         device_lock(dev);
683
684         if (dev->pm_domain) {
685                 pm_dev_dbg(dev, state, "completing power domain ");
686                 if (dev->pm_domain->ops.complete)
687                         dev->pm_domain->ops.complete(dev);
688         } else if (dev->type && dev->type->pm) {
689                 pm_dev_dbg(dev, state, "completing type ");
690                 if (dev->type->pm->complete)
691                         dev->type->pm->complete(dev);
692         } else if (dev->class && dev->class->pm) {
693                 pm_dev_dbg(dev, state, "completing class ");
694                 if (dev->class->pm->complete)
695                         dev->class->pm->complete(dev);
696         } else if (dev->bus && dev->bus->pm) {
697                 pm_dev_dbg(dev, state, "completing ");
698                 if (dev->bus->pm->complete)
699                         dev->bus->pm->complete(dev);
700         }
701
702         device_unlock(dev);
703 }
704
705 /**
706  * dpm_complete - Complete a PM transition for all non-sysdev devices.
707  * @state: PM transition of the system being carried out.
708  *
709  * Execute the ->complete() callbacks for all devices whose PM status is not
710  * DPM_ON (this allows new devices to be registered).
711  */
712 void dpm_complete(pm_message_t state)
713 {
714         struct list_head list;
715
716         might_sleep();
717
718         INIT_LIST_HEAD(&list);
719         mutex_lock(&dpm_list_mtx);
720         while (!list_empty(&dpm_prepared_list)) {
721                 struct device *dev = to_device(dpm_prepared_list.prev);
722
723                 get_device(dev);
724                 dev->power.is_prepared = false;
725                 list_move(&dev->power.entry, &list);
726                 mutex_unlock(&dpm_list_mtx);
727
728                 device_complete(dev, state);
729
730                 mutex_lock(&dpm_list_mtx);
731                 put_device(dev);
732         }
733         list_splice(&list, &dpm_list);
734         mutex_unlock(&dpm_list_mtx);
735 }
736
737 /**
738  * dpm_resume_end - Execute "resume" callbacks and complete system transition.
739  * @state: PM transition of the system being carried out.
740  *
741  * Execute "resume" callbacks for all devices and complete the PM transition of
742  * the system.
743  */
744 void dpm_resume_end(pm_message_t state)
745 {
746         dpm_resume(state);
747         dpm_complete(state);
748 }
749 EXPORT_SYMBOL_GPL(dpm_resume_end);
750
751
752 /*------------------------- Suspend routines -------------------------*/
753
754 /**
755  * resume_event - Return a "resume" message for given "suspend" sleep state.
756  * @sleep_state: PM message representing a sleep state.
757  *
758  * Return a PM message representing the resume event corresponding to given
759  * sleep state.
760  */
761 static pm_message_t resume_event(pm_message_t sleep_state)
762 {
763         switch (sleep_state.event) {
764         case PM_EVENT_SUSPEND:
765                 return PMSG_RESUME;
766         case PM_EVENT_FREEZE:
767         case PM_EVENT_QUIESCE:
768                 return PMSG_RECOVER;
769         case PM_EVENT_HIBERNATE:
770                 return PMSG_RESTORE;
771         }
772         return PMSG_ON;
773 }
774
775 /**
776  * device_suspend_noirq - Execute a "late suspend" callback for given device.
777  * @dev: Device to handle.
778  * @state: PM transition of the system being carried out.
779  *
780  * The driver of @dev will not receive interrupts while this function is being
781  * executed.
782  */
783 static int device_suspend_noirq(struct device *dev, pm_message_t state)
784 {
785         int error;
786
787         if (dev->pm_domain) {
788                 pm_dev_dbg(dev, state, "LATE power domain ");
789                 error = pm_noirq_op(dev, &dev->pm_domain->ops, state);
790                 if (error)
791                         return error;
792         } else if (dev->type && dev->type->pm) {
793                 pm_dev_dbg(dev, state, "LATE type ");
794                 error = pm_noirq_op(dev, dev->type->pm, state);
795                 if (error)
796                         return error;
797         } else if (dev->class && dev->class->pm) {
798                 pm_dev_dbg(dev, state, "LATE class ");
799                 error = pm_noirq_op(dev, dev->class->pm, state);
800                 if (error)
801                         return error;
802         } else if (dev->bus && dev->bus->pm) {
803                 pm_dev_dbg(dev, state, "LATE ");
804                 error = pm_noirq_op(dev, dev->bus->pm, state);
805                 if (error)
806                         return error;
807         }
808
809         return 0;
810 }
811
812 /**
813  * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
814  * @state: PM transition of the system being carried out.
815  *
816  * Prevent device drivers from receiving interrupts and call the "noirq" suspend
817  * handlers for all non-sysdev devices.
818  */
819 int dpm_suspend_noirq(pm_message_t state)
820 {
821         ktime_t starttime = ktime_get();
822         int error = 0;
823
824         suspend_device_irqs();
825         mutex_lock(&dpm_list_mtx);
826         while (!list_empty(&dpm_suspended_list)) {
827                 struct device *dev = to_device(dpm_suspended_list.prev);
828
829                 get_device(dev);
830                 mutex_unlock(&dpm_list_mtx);
831
832                 error = device_suspend_noirq(dev, state);
833
834                 mutex_lock(&dpm_list_mtx);
835                 if (error) {
836                         pm_dev_err(dev, state, " late", error);
837                         put_device(dev);
838                         break;
839                 }
840                 if (!list_empty(&dev->power.entry))
841                         list_move(&dev->power.entry, &dpm_noirq_list);
842                 put_device(dev);
843         }
844         mutex_unlock(&dpm_list_mtx);
845         if (error)
846                 dpm_resume_noirq(resume_event(state));
847         else
848                 dpm_show_time(starttime, state, "late");
849         return error;
850 }
851 EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
852
853 /**
854  * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
855  * @dev: Device to suspend.
856  * @state: PM transition of the system being carried out.
857  * @cb: Suspend callback to execute.
858  */
859 static int legacy_suspend(struct device *dev, pm_message_t state,
860                           int (*cb)(struct device *dev, pm_message_t state))
861 {
862         int error;
863         ktime_t calltime;
864
865         calltime = initcall_debug_start(dev);
866
867         error = cb(dev, state);
868         suspend_report_result(cb, error);
869
870         initcall_debug_report(dev, calltime, error);
871
872         return error;
873 }
874
875 /**
876  * device_suspend - Execute "suspend" callbacks for given device.
877  * @dev: Device to handle.
878  * @state: PM transition of the system being carried out.
879  * @async: If true, the device is being suspended asynchronously.
880  */
881 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
882 {
883         int error = 0;
884         struct timer_list timer;
885         struct dpm_drv_wd_data data;
886
887         dpm_wait_for_children(dev, async);
888
889         data.dev = dev;
890         data.tsk = get_current();
891         init_timer_on_stack(&timer);
892         timer.expires = jiffies + HZ * 3;
893         timer.function = dpm_drv_timeout;
894         timer.data = (unsigned long)&data;
895         add_timer(&timer);
896
897         if (async_error)
898                 return 0;
899
900         pm_runtime_get_noresume(dev);
901         if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
902                 pm_wakeup_event(dev, 0);
903
904         if (pm_wakeup_pending()) {
905                 pm_runtime_put_sync(dev);
906                 async_error = -EBUSY;
907                 return 0;
908         }
909
910         device_lock(dev);
911
912         if (dev->pm_domain) {
913                 pm_dev_dbg(dev, state, "power domain ");
914                 error = pm_op(dev, &dev->pm_domain->ops, state);
915                 goto End;
916         }
917
918         if (dev->type && dev->type->pm) {
919                 pm_dev_dbg(dev, state, "type ");
920                 error = pm_op(dev, dev->type->pm, state);
921                 goto End;
922         }
923
924         if (dev->class) {
925                 if (dev->class->pm) {
926                         pm_dev_dbg(dev, state, "class ");
927                         error = pm_op(dev, dev->class->pm, state);
928                         goto End;
929                 } else if (dev->class->suspend) {
930                         pm_dev_dbg(dev, state, "legacy class ");
931                         error = legacy_suspend(dev, state, dev->class->suspend);
932                         goto End;
933                 }
934         }
935
936         if (dev->bus) {
937                 if (dev->bus->pm) {
938                         pm_dev_dbg(dev, state, "");
939                         error = pm_op(dev, dev->bus->pm, state);
940                 } else if (dev->bus->suspend) {
941                         pm_dev_dbg(dev, state, "legacy ");
942                         error = legacy_suspend(dev, state, dev->bus->suspend);
943                 }
944         }
945
946  End:
947         dev->power.is_suspended = !error;
948
949         device_unlock(dev);
950
951         del_timer_sync(&timer);
952         destroy_timer_on_stack(&timer);
953
954         complete_all(&dev->power.completion);
955
956         if (error) {
957                 pm_runtime_put_sync(dev);
958                 async_error = error;
959         } else if (dev->power.is_suspended) {
960                 __pm_runtime_disable(dev, false);
961         }
962
963         return error;
964 }
965
966 static void async_suspend(void *data, async_cookie_t cookie)
967 {
968         struct device *dev = (struct device *)data;
969         int error;
970
971         error = __device_suspend(dev, pm_transition, true);
972         if (error)
973                 pm_dev_err(dev, pm_transition, " async", error);
974
975         put_device(dev);
976 }
977
978 static int device_suspend(struct device *dev)
979 {
980         INIT_COMPLETION(dev->power.completion);
981
982         if (pm_async_enabled && dev->power.async_suspend) {
983                 get_device(dev);
984                 async_schedule(async_suspend, dev);
985                 return 0;
986         }
987
988         return __device_suspend(dev, pm_transition, false);
989 }
990
991 /**
992  * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
993  * @state: PM transition of the system being carried out.
994  */
995 int dpm_suspend(pm_message_t state)
996 {
997         ktime_t starttime = ktime_get();
998         int error = 0;
999
1000         might_sleep();
1001
1002         mutex_lock(&dpm_list_mtx);
1003         pm_transition = state;
1004         async_error = 0;
1005         while (!list_empty(&dpm_prepared_list)) {
1006                 struct device *dev = to_device(dpm_prepared_list.prev);
1007
1008                 get_device(dev);
1009                 mutex_unlock(&dpm_list_mtx);
1010
1011                 error = device_suspend(dev);
1012
1013                 mutex_lock(&dpm_list_mtx);
1014                 if (error) {
1015                         pm_dev_err(dev, state, "", error);
1016                         put_device(dev);
1017                         break;
1018                 }
1019                 if (!list_empty(&dev->power.entry))
1020                         list_move(&dev->power.entry, &dpm_suspended_list);
1021                 put_device(dev);
1022                 if (async_error)
1023                         break;
1024         }
1025         mutex_unlock(&dpm_list_mtx);
1026         async_synchronize_full();
1027         if (!error)
1028                 error = async_error;
1029         if (!error)
1030                 dpm_show_time(starttime, state, NULL);
1031         return error;
1032 }
1033
1034 /**
1035  * device_prepare - Prepare a device for system power transition.
1036  * @dev: Device to handle.
1037  * @state: PM transition of the system being carried out.
1038  *
1039  * Execute the ->prepare() callback(s) for given device.  No new children of the
1040  * device may be registered after this function has returned.
1041  */
1042 static int device_prepare(struct device *dev, pm_message_t state)
1043 {
1044         int error = 0;
1045
1046         device_lock(dev);
1047
1048         if (dev->pm_domain) {
1049                 pm_dev_dbg(dev, state, "preparing power domain ");
1050                 if (dev->pm_domain->ops.prepare)
1051                         error = dev->pm_domain->ops.prepare(dev);
1052                 suspend_report_result(dev->pm_domain->ops.prepare, error);
1053                 if (error)
1054                         goto End;
1055         } else if (dev->type && dev->type->pm) {
1056                 pm_dev_dbg(dev, state, "preparing type ");
1057                 if (dev->type->pm->prepare)
1058                         error = dev->type->pm->prepare(dev);
1059                 suspend_report_result(dev->type->pm->prepare, error);
1060                 if (error)
1061                         goto End;
1062         } else if (dev->class && dev->class->pm) {
1063                 pm_dev_dbg(dev, state, "preparing class ");
1064                 if (dev->class->pm->prepare)
1065                         error = dev->class->pm->prepare(dev);
1066                 suspend_report_result(dev->class->pm->prepare, error);
1067                 if (error)
1068                         goto End;
1069         } else if (dev->bus && dev->bus->pm) {
1070                 pm_dev_dbg(dev, state, "preparing ");
1071                 if (dev->bus->pm->prepare)
1072                         error = dev->bus->pm->prepare(dev);
1073                 suspend_report_result(dev->bus->pm->prepare, error);
1074         }
1075
1076  End:
1077         device_unlock(dev);
1078
1079         return error;
1080 }
1081
1082 /**
1083  * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1084  * @state: PM transition of the system being carried out.
1085  *
1086  * Execute the ->prepare() callback(s) for all devices.
1087  */
1088 int dpm_prepare(pm_message_t state)
1089 {
1090         int error = 0;
1091
1092         might_sleep();
1093
1094         mutex_lock(&dpm_list_mtx);
1095         while (!list_empty(&dpm_list)) {
1096                 struct device *dev = to_device(dpm_list.next);
1097
1098                 get_device(dev);
1099                 mutex_unlock(&dpm_list_mtx);
1100
1101                 error = device_prepare(dev, state);
1102
1103                 mutex_lock(&dpm_list_mtx);
1104                 if (error) {
1105                         if (error == -EAGAIN) {
1106                                 put_device(dev);
1107                                 error = 0;
1108                                 continue;
1109                         }
1110                         printk(KERN_INFO "PM: Device %s not prepared "
1111                                 "for power transition: code %d\n",
1112                                 dev_name(dev), error);
1113                         put_device(dev);
1114                         break;
1115                 }
1116                 dev->power.is_prepared = true;
1117                 if (!list_empty(&dev->power.entry))
1118                         list_move_tail(&dev->power.entry, &dpm_prepared_list);
1119                 put_device(dev);
1120         }
1121         mutex_unlock(&dpm_list_mtx);
1122         return error;
1123 }
1124
1125 /**
1126  * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1127  * @state: PM transition of the system being carried out.
1128  *
1129  * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1130  * callbacks for them.
1131  */
1132 int dpm_suspend_start(pm_message_t state)
1133 {
1134         int error;
1135
1136         error = dpm_prepare(state);
1137         if (!error)
1138                 error = dpm_suspend(state);
1139         return error;
1140 }
1141 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1142
1143 void __suspend_report_result(const char *function, void *fn, int ret)
1144 {
1145         if (ret)
1146                 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1147 }
1148 EXPORT_SYMBOL_GPL(__suspend_report_result);
1149
1150 /**
1151  * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1152  * @dev: Device to wait for.
1153  * @subordinate: Device that needs to wait for @dev.
1154  */
1155 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1156 {
1157         dpm_wait(dev, subordinate->power.async_suspend);
1158         return async_error;
1159 }
1160 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);