Merge commit 'v3.0-rc5' into android-3.0
[firefly-linux-kernel-4.4.55.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->pwr_domain) {
436                 pm_dev_dbg(dev, state, "EARLY power domain ");
437                 error = pm_noirq_op(dev, &dev->pwr_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
516         TRACE_DEVICE(dev);
517         TRACE_RESUME(0);
518
519         dpm_wait(dev->parent, async);
520         device_lock(dev);
521
522         /*
523          * This is a fib.  But we'll allow new children to be added below
524          * a resumed device, even if the device hasn't been completed yet.
525          */
526         dev->power.is_prepared = false;
527
528         if (!dev->power.is_suspended)
529                 goto Unlock;
530
531         if (dev->pwr_domain) {
532                 pm_dev_dbg(dev, state, "power domain ");
533                 error = pm_op(dev, &dev->pwr_domain->ops, state);
534                 goto End;
535         }
536
537         if (dev->type && dev->type->pm) {
538                 pm_dev_dbg(dev, state, "type ");
539                 error = pm_op(dev, dev->type->pm, state);
540                 goto End;
541         }
542
543         if (dev->class) {
544                 if (dev->class->pm) {
545                         pm_dev_dbg(dev, state, "class ");
546                         error = pm_op(dev, dev->class->pm, state);
547                         goto End;
548                 } else if (dev->class->resume) {
549                         pm_dev_dbg(dev, state, "legacy class ");
550                         error = legacy_resume(dev, dev->class->resume);
551                         goto End;
552                 }
553         }
554
555         if (dev->bus) {
556                 if (dev->bus->pm) {
557                         pm_dev_dbg(dev, state, "");
558                         error = pm_op(dev, dev->bus->pm, state);
559                 } else if (dev->bus->resume) {
560                         pm_dev_dbg(dev, state, "legacy ");
561                         error = legacy_resume(dev, dev->bus->resume);
562                 }
563         }
564
565  End:
566         dev->power.is_suspended = false;
567
568  Unlock:
569         device_unlock(dev);
570         complete_all(&dev->power.completion);
571
572         TRACE_RESUME(error);
573         return error;
574 }
575
576 static void async_resume(void *data, async_cookie_t cookie)
577 {
578         struct device *dev = (struct device *)data;
579         int error;
580
581         error = device_resume(dev, pm_transition, true);
582         if (error)
583                 pm_dev_err(dev, pm_transition, " async", error);
584         put_device(dev);
585 }
586
587 static bool is_async(struct device *dev)
588 {
589         return dev->power.async_suspend && pm_async_enabled
590                 && !pm_trace_is_enabled();
591 }
592
593 /**
594  *      dpm_drv_timeout - Driver suspend / resume watchdog handler
595  *      @data: struct device which timed out
596  *
597  *      Called when a driver has timed out suspending or resuming.
598  *      There's not much we can do here to recover so
599  *      BUG() out for a crash-dump
600  *
601  */
602 static void dpm_drv_timeout(unsigned long data)
603 {
604         struct dpm_drv_wd_data *wd_data = (void *)data;
605         struct device *dev = wd_data->dev;
606         struct task_struct *tsk = wd_data->tsk;
607
608         printk(KERN_EMERG "**** DPM device timeout: %s (%s)\n", dev_name(dev),
609                (dev->driver ? dev->driver->name : "no driver"));
610
611         printk(KERN_EMERG "dpm suspend stack:\n");
612         show_stack(tsk, NULL);
613
614         BUG();
615 }
616
617 /**
618  * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
619  * @state: PM transition of the system being carried out.
620  *
621  * Execute the appropriate "resume" callback for all devices whose status
622  * indicates that they are suspended.
623  */
624 void dpm_resume(pm_message_t state)
625 {
626         struct device *dev;
627         ktime_t starttime = ktime_get();
628
629         might_sleep();
630
631         mutex_lock(&dpm_list_mtx);
632         pm_transition = state;
633         async_error = 0;
634
635         list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
636                 INIT_COMPLETION(dev->power.completion);
637                 if (is_async(dev)) {
638                         get_device(dev);
639                         async_schedule(async_resume, dev);
640                 }
641         }
642
643         while (!list_empty(&dpm_suspended_list)) {
644                 dev = to_device(dpm_suspended_list.next);
645                 get_device(dev);
646                 if (!is_async(dev)) {
647                         int error;
648
649                         mutex_unlock(&dpm_list_mtx);
650
651                         error = device_resume(dev, state, false);
652                         if (error)
653                                 pm_dev_err(dev, state, "", error);
654
655                         mutex_lock(&dpm_list_mtx);
656                 }
657                 if (!list_empty(&dev->power.entry))
658                         list_move_tail(&dev->power.entry, &dpm_prepared_list);
659                 put_device(dev);
660         }
661         mutex_unlock(&dpm_list_mtx);
662         async_synchronize_full();
663         dpm_show_time(starttime, state, NULL);
664 }
665
666 /**
667  * device_complete - Complete a PM transition for given device.
668  * @dev: Device to handle.
669  * @state: PM transition of the system being carried out.
670  */
671 static void device_complete(struct device *dev, pm_message_t state)
672 {
673         device_lock(dev);
674
675         if (dev->pwr_domain) {
676                 pm_dev_dbg(dev, state, "completing power domain ");
677                 if (dev->pwr_domain->ops.complete)
678                         dev->pwr_domain->ops.complete(dev);
679         } else if (dev->type && dev->type->pm) {
680                 pm_dev_dbg(dev, state, "completing type ");
681                 if (dev->type->pm->complete)
682                         dev->type->pm->complete(dev);
683         } else if (dev->class && dev->class->pm) {
684                 pm_dev_dbg(dev, state, "completing class ");
685                 if (dev->class->pm->complete)
686                         dev->class->pm->complete(dev);
687         } else if (dev->bus && dev->bus->pm) {
688                 pm_dev_dbg(dev, state, "completing ");
689                 if (dev->bus->pm->complete)
690                         dev->bus->pm->complete(dev);
691         }
692
693         device_unlock(dev);
694 }
695
696 /**
697  * dpm_complete - Complete a PM transition for all non-sysdev devices.
698  * @state: PM transition of the system being carried out.
699  *
700  * Execute the ->complete() callbacks for all devices whose PM status is not
701  * DPM_ON (this allows new devices to be registered).
702  */
703 void dpm_complete(pm_message_t state)
704 {
705         struct list_head list;
706
707         might_sleep();
708
709         INIT_LIST_HEAD(&list);
710         mutex_lock(&dpm_list_mtx);
711         while (!list_empty(&dpm_prepared_list)) {
712                 struct device *dev = to_device(dpm_prepared_list.prev);
713
714                 get_device(dev);
715                 dev->power.is_prepared = false;
716                 list_move(&dev->power.entry, &list);
717                 mutex_unlock(&dpm_list_mtx);
718
719                 device_complete(dev, state);
720
721                 mutex_lock(&dpm_list_mtx);
722                 put_device(dev);
723         }
724         list_splice(&list, &dpm_list);
725         mutex_unlock(&dpm_list_mtx);
726 }
727
728 /**
729  * dpm_resume_end - Execute "resume" callbacks and complete system transition.
730  * @state: PM transition of the system being carried out.
731  *
732  * Execute "resume" callbacks for all devices and complete the PM transition of
733  * the system.
734  */
735 void dpm_resume_end(pm_message_t state)
736 {
737         dpm_resume(state);
738         dpm_complete(state);
739 }
740 EXPORT_SYMBOL_GPL(dpm_resume_end);
741
742
743 /*------------------------- Suspend routines -------------------------*/
744
745 /**
746  * resume_event - Return a "resume" message for given "suspend" sleep state.
747  * @sleep_state: PM message representing a sleep state.
748  *
749  * Return a PM message representing the resume event corresponding to given
750  * sleep state.
751  */
752 static pm_message_t resume_event(pm_message_t sleep_state)
753 {
754         switch (sleep_state.event) {
755         case PM_EVENT_SUSPEND:
756                 return PMSG_RESUME;
757         case PM_EVENT_FREEZE:
758         case PM_EVENT_QUIESCE:
759                 return PMSG_RECOVER;
760         case PM_EVENT_HIBERNATE:
761                 return PMSG_RESTORE;
762         }
763         return PMSG_ON;
764 }
765
766 /**
767  * device_suspend_noirq - Execute a "late suspend" callback for given device.
768  * @dev: Device to handle.
769  * @state: PM transition of the system being carried out.
770  *
771  * The driver of @dev will not receive interrupts while this function is being
772  * executed.
773  */
774 static int device_suspend_noirq(struct device *dev, pm_message_t state)
775 {
776         int error;
777
778         if (dev->pwr_domain) {
779                 pm_dev_dbg(dev, state, "LATE power domain ");
780                 error = pm_noirq_op(dev, &dev->pwr_domain->ops, state);
781                 if (error)
782                         return error;
783         } else if (dev->type && dev->type->pm) {
784                 pm_dev_dbg(dev, state, "LATE type ");
785                 error = pm_noirq_op(dev, dev->type->pm, state);
786                 if (error)
787                         return error;
788         } else if (dev->class && dev->class->pm) {
789                 pm_dev_dbg(dev, state, "LATE class ");
790                 error = pm_noirq_op(dev, dev->class->pm, state);
791                 if (error)
792                         return error;
793         } else if (dev->bus && dev->bus->pm) {
794                 pm_dev_dbg(dev, state, "LATE ");
795                 error = pm_noirq_op(dev, dev->bus->pm, state);
796                 if (error)
797                         return error;
798         }
799
800         return 0;
801 }
802
803 /**
804  * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
805  * @state: PM transition of the system being carried out.
806  *
807  * Prevent device drivers from receiving interrupts and call the "noirq" suspend
808  * handlers for all non-sysdev devices.
809  */
810 int dpm_suspend_noirq(pm_message_t state)
811 {
812         ktime_t starttime = ktime_get();
813         int error = 0;
814
815         suspend_device_irqs();
816         mutex_lock(&dpm_list_mtx);
817         while (!list_empty(&dpm_suspended_list)) {
818                 struct device *dev = to_device(dpm_suspended_list.prev);
819
820                 get_device(dev);
821                 mutex_unlock(&dpm_list_mtx);
822
823                 error = device_suspend_noirq(dev, state);
824
825                 mutex_lock(&dpm_list_mtx);
826                 if (error) {
827                         pm_dev_err(dev, state, " late", error);
828                         put_device(dev);
829                         break;
830                 }
831                 if (!list_empty(&dev->power.entry))
832                         list_move(&dev->power.entry, &dpm_noirq_list);
833                 put_device(dev);
834         }
835         mutex_unlock(&dpm_list_mtx);
836         if (error)
837                 dpm_resume_noirq(resume_event(state));
838         else
839                 dpm_show_time(starttime, state, "late");
840         return error;
841 }
842 EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
843
844 /**
845  * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
846  * @dev: Device to suspend.
847  * @state: PM transition of the system being carried out.
848  * @cb: Suspend callback to execute.
849  */
850 static int legacy_suspend(struct device *dev, pm_message_t state,
851                           int (*cb)(struct device *dev, pm_message_t state))
852 {
853         int error;
854         ktime_t calltime;
855
856         calltime = initcall_debug_start(dev);
857
858         error = cb(dev, state);
859         suspend_report_result(cb, error);
860
861         initcall_debug_report(dev, calltime, error);
862
863         return error;
864 }
865
866 /**
867  * device_suspend - Execute "suspend" callbacks for given device.
868  * @dev: Device to handle.
869  * @state: PM transition of the system being carried out.
870  * @async: If true, the device is being suspended asynchronously.
871  */
872 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
873 {
874         int error = 0;
875         struct timer_list timer;
876         struct dpm_drv_wd_data data;
877
878         dpm_wait_for_children(dev, async);
879
880         data.dev = dev;
881         data.tsk = get_current();
882         init_timer_on_stack(&timer);
883         timer.expires = jiffies + HZ * 12;
884         timer.function = dpm_drv_timeout;
885         timer.data = (unsigned long)&data;
886         add_timer(&timer);
887
888         device_lock(dev);
889
890         if (async_error)
891                 goto Unlock;
892
893         if (pm_wakeup_pending()) {
894                 async_error = -EBUSY;
895                 goto Unlock;
896         }
897
898         if (dev->pwr_domain) {
899                 pm_dev_dbg(dev, state, "power domain ");
900                 error = pm_op(dev, &dev->pwr_domain->ops, state);
901                 goto End;
902         }
903
904         if (dev->type && dev->type->pm) {
905                 pm_dev_dbg(dev, state, "type ");
906                 error = pm_op(dev, dev->type->pm, state);
907                 goto End;
908         }
909
910         if (dev->class) {
911                 if (dev->class->pm) {
912                         pm_dev_dbg(dev, state, "class ");
913                         error = pm_op(dev, dev->class->pm, state);
914                         goto End;
915                 } else if (dev->class->suspend) {
916                         pm_dev_dbg(dev, state, "legacy class ");
917                         error = legacy_suspend(dev, state, dev->class->suspend);
918                         goto End;
919                 }
920         }
921
922         if (dev->bus) {
923                 if (dev->bus->pm) {
924                         pm_dev_dbg(dev, state, "");
925                         error = pm_op(dev, dev->bus->pm, state);
926                 } else if (dev->bus->suspend) {
927                         pm_dev_dbg(dev, state, "legacy ");
928                         error = legacy_suspend(dev, state, dev->bus->suspend);
929                 }
930         }
931
932  End:
933         dev->power.is_suspended = !error;
934
935  Unlock:
936         device_unlock(dev);
937
938         del_timer_sync(&timer);
939         destroy_timer_on_stack(&timer);
940
941         complete_all(&dev->power.completion);
942
943         if (error)
944                 async_error = error;
945
946         return error;
947 }
948
949 static void async_suspend(void *data, async_cookie_t cookie)
950 {
951         struct device *dev = (struct device *)data;
952         int error;
953
954         error = __device_suspend(dev, pm_transition, true);
955         if (error)
956                 pm_dev_err(dev, pm_transition, " async", error);
957
958         put_device(dev);
959 }
960
961 static int device_suspend(struct device *dev)
962 {
963         INIT_COMPLETION(dev->power.completion);
964
965         if (pm_async_enabled && dev->power.async_suspend) {
966                 get_device(dev);
967                 async_schedule(async_suspend, dev);
968                 return 0;
969         }
970
971         return __device_suspend(dev, pm_transition, false);
972 }
973
974 /**
975  * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
976  * @state: PM transition of the system being carried out.
977  */
978 int dpm_suspend(pm_message_t state)
979 {
980         ktime_t starttime = ktime_get();
981         int error = 0;
982
983         might_sleep();
984
985         mutex_lock(&dpm_list_mtx);
986         pm_transition = state;
987         async_error = 0;
988         while (!list_empty(&dpm_prepared_list)) {
989                 struct device *dev = to_device(dpm_prepared_list.prev);
990
991                 get_device(dev);
992                 mutex_unlock(&dpm_list_mtx);
993
994                 error = device_suspend(dev);
995
996                 mutex_lock(&dpm_list_mtx);
997                 if (error) {
998                         pm_dev_err(dev, state, "", error);
999                         put_device(dev);
1000                         break;
1001                 }
1002                 if (!list_empty(&dev->power.entry))
1003                         list_move(&dev->power.entry, &dpm_suspended_list);
1004                 put_device(dev);
1005                 if (async_error)
1006                         break;
1007         }
1008         mutex_unlock(&dpm_list_mtx);
1009         async_synchronize_full();
1010         if (!error)
1011                 error = async_error;
1012         if (!error)
1013                 dpm_show_time(starttime, state, NULL);
1014         return error;
1015 }
1016
1017 /**
1018  * device_prepare - Prepare a device for system power transition.
1019  * @dev: Device to handle.
1020  * @state: PM transition of the system being carried out.
1021  *
1022  * Execute the ->prepare() callback(s) for given device.  No new children of the
1023  * device may be registered after this function has returned.
1024  */
1025 static int device_prepare(struct device *dev, pm_message_t state)
1026 {
1027         int error = 0;
1028
1029         device_lock(dev);
1030
1031         if (dev->pwr_domain) {
1032                 pm_dev_dbg(dev, state, "preparing power domain ");
1033                 if (dev->pwr_domain->ops.prepare)
1034                         error = dev->pwr_domain->ops.prepare(dev);
1035                 suspend_report_result(dev->pwr_domain->ops.prepare, error);
1036                 if (error)
1037                         goto End;
1038         } else if (dev->type && dev->type->pm) {
1039                 pm_dev_dbg(dev, state, "preparing type ");
1040                 if (dev->type->pm->prepare)
1041                         error = dev->type->pm->prepare(dev);
1042                 suspend_report_result(dev->type->pm->prepare, error);
1043                 if (error)
1044                         goto End;
1045         } else if (dev->class && dev->class->pm) {
1046                 pm_dev_dbg(dev, state, "preparing class ");
1047                 if (dev->class->pm->prepare)
1048                         error = dev->class->pm->prepare(dev);
1049                 suspend_report_result(dev->class->pm->prepare, error);
1050                 if (error)
1051                         goto End;
1052         } else if (dev->bus && dev->bus->pm) {
1053                 pm_dev_dbg(dev, state, "preparing ");
1054                 if (dev->bus->pm->prepare)
1055                         error = dev->bus->pm->prepare(dev);
1056                 suspend_report_result(dev->bus->pm->prepare, error);
1057         }
1058
1059  End:
1060         device_unlock(dev);
1061
1062         return error;
1063 }
1064
1065 /**
1066  * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1067  * @state: PM transition of the system being carried out.
1068  *
1069  * Execute the ->prepare() callback(s) for all devices.
1070  */
1071 int dpm_prepare(pm_message_t state)
1072 {
1073         int error = 0;
1074
1075         might_sleep();
1076
1077         mutex_lock(&dpm_list_mtx);
1078         while (!list_empty(&dpm_list)) {
1079                 struct device *dev = to_device(dpm_list.next);
1080
1081                 get_device(dev);
1082                 mutex_unlock(&dpm_list_mtx);
1083
1084                 pm_runtime_get_noresume(dev);
1085                 if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
1086                         pm_wakeup_event(dev, 0);
1087
1088                 pm_runtime_put_sync(dev);
1089                 error = pm_wakeup_pending() ?
1090                                 -EBUSY : device_prepare(dev, state);
1091
1092                 mutex_lock(&dpm_list_mtx);
1093                 if (error) {
1094                         if (error == -EAGAIN) {
1095                                 put_device(dev);
1096                                 error = 0;
1097                                 continue;
1098                         }
1099                         printk(KERN_INFO "PM: Device %s not prepared "
1100                                 "for power transition: code %d\n",
1101                                 dev_name(dev), error);
1102                         put_device(dev);
1103                         break;
1104                 }
1105                 dev->power.is_prepared = true;
1106                 if (!list_empty(&dev->power.entry))
1107                         list_move_tail(&dev->power.entry, &dpm_prepared_list);
1108                 put_device(dev);
1109         }
1110         mutex_unlock(&dpm_list_mtx);
1111         return error;
1112 }
1113
1114 /**
1115  * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1116  * @state: PM transition of the system being carried out.
1117  *
1118  * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1119  * callbacks for them.
1120  */
1121 int dpm_suspend_start(pm_message_t state)
1122 {
1123         int error;
1124
1125         error = dpm_prepare(state);
1126         if (!error)
1127                 error = dpm_suspend(state);
1128         return error;
1129 }
1130 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1131
1132 void __suspend_report_result(const char *function, void *fn, int ret)
1133 {
1134         if (ret)
1135                 printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1136 }
1137 EXPORT_SYMBOL_GPL(__suspend_report_result);
1138
1139 /**
1140  * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1141  * @dev: Device to wait for.
1142  * @subordinate: Device that needs to wait for @dev.
1143  */
1144 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1145 {
1146         dpm_wait(dev, subordinate->power.async_suspend);
1147         return async_error;
1148 }
1149 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);