1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * drivers/base/power/main.c - Where the driver meets power management.
4 *
5 * Copyright (c) 2003 Patrick Mochel
6 * Copyright (c) 2003 Open Source Development Lab
7 *
8 * The driver model core calls device_pm_add() when a device is registered.
9 * This will initialize the embedded device_pm_info object in the device
10 * and add it to the list of power-controlled devices. sysfs entries for
11 * controlling device power management will also be added.
12 *
13 * A separate list is used for keeping track of power info, because the power
14 * domain dependencies may differ from the ancestral dependencies that the
15 * subsystem list maintains.
16 */
17
18 #define pr_fmt(fmt) "PM: " fmt
19 #define dev_fmt pr_fmt
20
21 #include <linux/device.h>
22 #include <linux/export.h>
23 #include <linux/mutex.h>
24 #include <linux/pm.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/pm-trace.h>
27 #include <linux/pm_wakeirq.h>
28 #include <linux/interrupt.h>
29 #include <linux/sched.h>
30 #include <linux/sched/debug.h>
31 #include <linux/async.h>
32 #include <linux/suspend.h>
33 #include <trace/events/power.h>
34 #include <linux/cpufreq.h>
35 #include <linux/cpuidle.h>
36 #include <linux/devfreq.h>
37 #include <linux/timer.h>
38 #include <linux/wakeup_reason.h>
39
40 #include "../base.h"
41 #include "power.h"
42
43 typedef int (*pm_callback_t)(struct device *);
44
45 #define list_for_each_entry_rcu_locked(pos, head, member) \
46 list_for_each_entry_rcu(pos, head, member, \
47 device_links_read_lock_held())
48
49 /*
50 * The entries in the dpm_list list are in a depth first order, simply
51 * because children are guaranteed to be discovered after parents, and
52 * are inserted at the back of the list on discovery.
53 *
54 * Since device_pm_add() may be called with a device lock held,
55 * we must never try to acquire a device lock while holding
56 * dpm_list_mutex.
57 */
58
59 LIST_HEAD(dpm_list);
60 static LIST_HEAD(dpm_prepared_list);
61 static LIST_HEAD(dpm_suspended_list);
62 static LIST_HEAD(dpm_late_early_list);
63 static LIST_HEAD(dpm_noirq_list);
64
65 struct suspend_stats suspend_stats;
66 static DEFINE_MUTEX(dpm_list_mtx);
67 static pm_message_t pm_transition;
68
69 static int async_error;
70
pm_verb(int event)71 static const char *pm_verb(int event)
72 {
73 switch (event) {
74 case PM_EVENT_SUSPEND:
75 return "suspend";
76 case PM_EVENT_RESUME:
77 return "resume";
78 case PM_EVENT_FREEZE:
79 return "freeze";
80 case PM_EVENT_QUIESCE:
81 return "quiesce";
82 case PM_EVENT_HIBERNATE:
83 return "hibernate";
84 case PM_EVENT_THAW:
85 return "thaw";
86 case PM_EVENT_RESTORE:
87 return "restore";
88 case PM_EVENT_RECOVER:
89 return "recover";
90 default:
91 return "(unknown PM event)";
92 }
93 }
94
95 /**
96 * device_pm_sleep_init - Initialize system suspend-related device fields.
97 * @dev: Device object being initialized.
98 */
device_pm_sleep_init(struct device * dev)99 void device_pm_sleep_init(struct device *dev)
100 {
101 dev->power.is_prepared = false;
102 dev->power.is_suspended = false;
103 dev->power.is_noirq_suspended = false;
104 dev->power.is_late_suspended = false;
105 init_completion(&dev->power.completion);
106 complete_all(&dev->power.completion);
107 dev->power.wakeup = NULL;
108 INIT_LIST_HEAD(&dev->power.entry);
109 }
110
111 /**
112 * device_pm_lock - Lock the list of active devices used by the PM core.
113 */
device_pm_lock(void)114 void device_pm_lock(void)
115 {
116 mutex_lock(&dpm_list_mtx);
117 }
118
119 /**
120 * device_pm_unlock - Unlock the list of active devices used by the PM core.
121 */
device_pm_unlock(void)122 void device_pm_unlock(void)
123 {
124 mutex_unlock(&dpm_list_mtx);
125 }
126
127 /**
128 * device_pm_add - Add a device to the PM core's list of active devices.
129 * @dev: Device to add to the list.
130 */
device_pm_add(struct device * dev)131 void device_pm_add(struct device *dev)
132 {
133 /* Skip PM setup/initialization. */
134 if (device_pm_not_required(dev))
135 return;
136
137 pr_debug("Adding info for %s:%s\n",
138 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
139 device_pm_check_callbacks(dev);
140 mutex_lock(&dpm_list_mtx);
141 if (dev->parent && dev->parent->power.is_prepared)
142 dev_warn(dev, "parent %s should not be sleeping\n",
143 dev_name(dev->parent));
144 list_add_tail(&dev->power.entry, &dpm_list);
145 dev->power.in_dpm_list = true;
146 mutex_unlock(&dpm_list_mtx);
147 }
148
149 /**
150 * device_pm_remove - Remove a device from the PM core's list of active devices.
151 * @dev: Device to be removed from the list.
152 */
device_pm_remove(struct device * dev)153 void device_pm_remove(struct device *dev)
154 {
155 if (device_pm_not_required(dev))
156 return;
157
158 pr_debug("Removing info for %s:%s\n",
159 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
160 complete_all(&dev->power.completion);
161 mutex_lock(&dpm_list_mtx);
162 list_del_init(&dev->power.entry);
163 dev->power.in_dpm_list = false;
164 mutex_unlock(&dpm_list_mtx);
165 device_wakeup_disable(dev);
166 pm_runtime_remove(dev);
167 device_pm_check_callbacks(dev);
168 }
169
170 /**
171 * device_pm_move_before - Move device in the PM core's list of active devices.
172 * @deva: Device to move in dpm_list.
173 * @devb: Device @deva should come before.
174 */
device_pm_move_before(struct device * deva,struct device * devb)175 void device_pm_move_before(struct device *deva, struct device *devb)
176 {
177 pr_debug("Moving %s:%s before %s:%s\n",
178 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
179 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
180 /* Delete deva from dpm_list and reinsert before devb. */
181 list_move_tail(&deva->power.entry, &devb->power.entry);
182 }
183
184 /**
185 * device_pm_move_after - Move device in the PM core's list of active devices.
186 * @deva: Device to move in dpm_list.
187 * @devb: Device @deva should come after.
188 */
device_pm_move_after(struct device * deva,struct device * devb)189 void device_pm_move_after(struct device *deva, struct device *devb)
190 {
191 pr_debug("Moving %s:%s after %s:%s\n",
192 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
193 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
194 /* Delete deva from dpm_list and reinsert after devb. */
195 list_move(&deva->power.entry, &devb->power.entry);
196 }
197
198 /**
199 * device_pm_move_last - Move device to end of the PM core's list of devices.
200 * @dev: Device to move in dpm_list.
201 */
device_pm_move_last(struct device * dev)202 void device_pm_move_last(struct device *dev)
203 {
204 pr_debug("Moving %s:%s to end of list\n",
205 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
206 list_move_tail(&dev->power.entry, &dpm_list);
207 }
208
initcall_debug_start(struct device * dev,void * cb)209 static ktime_t initcall_debug_start(struct device *dev, void *cb)
210 {
211 if (!pm_print_times_enabled)
212 return 0;
213
214 dev_info(dev, "calling %pS @ %i, parent: %s\n", cb,
215 task_pid_nr(current),
216 dev->parent ? dev_name(dev->parent) : "none");
217 return ktime_get();
218 }
219
initcall_debug_report(struct device * dev,ktime_t calltime,void * cb,int error)220 static void initcall_debug_report(struct device *dev, ktime_t calltime,
221 void *cb, int error)
222 {
223 ktime_t rettime;
224
225 if (!pm_print_times_enabled)
226 return;
227
228 rettime = ktime_get();
229 dev_info(dev, "%pS returned %d after %Ld usecs\n", cb, error,
230 (unsigned long long)ktime_us_delta(rettime, calltime));
231 }
232
233 /**
234 * dpm_wait - Wait for a PM operation to complete.
235 * @dev: Device to wait for.
236 * @async: If unset, wait only if the device's power.async_suspend flag is set.
237 */
dpm_wait(struct device * dev,bool async)238 static void dpm_wait(struct device *dev, bool async)
239 {
240 if (!dev)
241 return;
242
243 if (async || (pm_async_enabled && dev->power.async_suspend))
244 wait_for_completion(&dev->power.completion);
245 }
246
dpm_wait_fn(struct device * dev,void * async_ptr)247 static int dpm_wait_fn(struct device *dev, void *async_ptr)
248 {
249 dpm_wait(dev, *((bool *)async_ptr));
250 return 0;
251 }
252
dpm_wait_for_children(struct device * dev,bool async)253 static void dpm_wait_for_children(struct device *dev, bool async)
254 {
255 device_for_each_child(dev, &async, dpm_wait_fn);
256 }
257
dpm_wait_for_suppliers(struct device * dev,bool async)258 static void dpm_wait_for_suppliers(struct device *dev, bool async)
259 {
260 struct device_link *link;
261 int idx;
262
263 idx = device_links_read_lock();
264
265 /*
266 * If the supplier goes away right after we've checked the link to it,
267 * we'll wait for its completion to change the state, but that's fine,
268 * because the only things that will block as a result are the SRCU
269 * callbacks freeing the link objects for the links in the list we're
270 * walking.
271 */
272 list_for_each_entry_rcu_locked(link, &dev->links.suppliers, c_node)
273 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
274 dpm_wait(link->supplier, async);
275
276 device_links_read_unlock(idx);
277 }
278
dpm_wait_for_superior(struct device * dev,bool async)279 static bool dpm_wait_for_superior(struct device *dev, bool async)
280 {
281 struct device *parent;
282
283 /*
284 * If the device is resumed asynchronously and the parent's callback
285 * deletes both the device and the parent itself, the parent object may
286 * be freed while this function is running, so avoid that by reference
287 * counting the parent once more unless the device has been deleted
288 * already (in which case return right away).
289 */
290 mutex_lock(&dpm_list_mtx);
291
292 if (!device_pm_initialized(dev)) {
293 mutex_unlock(&dpm_list_mtx);
294 return false;
295 }
296
297 parent = get_device(dev->parent);
298
299 mutex_unlock(&dpm_list_mtx);
300
301 dpm_wait(parent, async);
302 put_device(parent);
303
304 dpm_wait_for_suppliers(dev, async);
305
306 /*
307 * If the parent's callback has deleted the device, attempting to resume
308 * it would be invalid, so avoid doing that then.
309 */
310 return device_pm_initialized(dev);
311 }
312
dpm_wait_for_consumers(struct device * dev,bool async)313 static void dpm_wait_for_consumers(struct device *dev, bool async)
314 {
315 struct device_link *link;
316 int idx;
317
318 idx = device_links_read_lock();
319
320 /*
321 * The status of a device link can only be changed from "dormant" by a
322 * probe, but that cannot happen during system suspend/resume. In
323 * theory it can change to "dormant" at that time, but then it is
324 * reasonable to wait for the target device anyway (eg. if it goes
325 * away, it's better to wait for it to go away completely and then
326 * continue instead of trying to continue in parallel with its
327 * unregistration).
328 */
329 list_for_each_entry_rcu_locked(link, &dev->links.consumers, s_node)
330 if (READ_ONCE(link->status) != DL_STATE_DORMANT)
331 dpm_wait(link->consumer, async);
332
333 device_links_read_unlock(idx);
334 }
335
dpm_wait_for_subordinate(struct device * dev,bool async)336 static void dpm_wait_for_subordinate(struct device *dev, bool async)
337 {
338 dpm_wait_for_children(dev, async);
339 dpm_wait_for_consumers(dev, async);
340 }
341
342 /**
343 * pm_op - Return the PM operation appropriate for given PM event.
344 * @ops: PM operations to choose from.
345 * @state: PM transition of the system being carried out.
346 */
pm_op(const struct dev_pm_ops * ops,pm_message_t state)347 static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
348 {
349 switch (state.event) {
350 #ifdef CONFIG_SUSPEND
351 case PM_EVENT_SUSPEND:
352 return ops->suspend;
353 case PM_EVENT_RESUME:
354 return ops->resume;
355 #endif /* CONFIG_SUSPEND */
356 #ifdef CONFIG_HIBERNATE_CALLBACKS
357 case PM_EVENT_FREEZE:
358 case PM_EVENT_QUIESCE:
359 return ops->freeze;
360 case PM_EVENT_HIBERNATE:
361 return ops->poweroff;
362 case PM_EVENT_THAW:
363 case PM_EVENT_RECOVER:
364 return ops->thaw;
365 case PM_EVENT_RESTORE:
366 return ops->restore;
367 #endif /* CONFIG_HIBERNATE_CALLBACKS */
368 }
369
370 return NULL;
371 }
372
373 /**
374 * pm_late_early_op - Return the PM operation appropriate for given PM event.
375 * @ops: PM operations to choose from.
376 * @state: PM transition of the system being carried out.
377 *
378 * Runtime PM is disabled for @dev while this function is being executed.
379 */
pm_late_early_op(const struct dev_pm_ops * ops,pm_message_t state)380 static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
381 pm_message_t state)
382 {
383 switch (state.event) {
384 #ifdef CONFIG_SUSPEND
385 case PM_EVENT_SUSPEND:
386 return ops->suspend_late;
387 case PM_EVENT_RESUME:
388 return ops->resume_early;
389 #endif /* CONFIG_SUSPEND */
390 #ifdef CONFIG_HIBERNATE_CALLBACKS
391 case PM_EVENT_FREEZE:
392 case PM_EVENT_QUIESCE:
393 return ops->freeze_late;
394 case PM_EVENT_HIBERNATE:
395 return ops->poweroff_late;
396 case PM_EVENT_THAW:
397 case PM_EVENT_RECOVER:
398 return ops->thaw_early;
399 case PM_EVENT_RESTORE:
400 return ops->restore_early;
401 #endif /* CONFIG_HIBERNATE_CALLBACKS */
402 }
403
404 return NULL;
405 }
406
407 /**
408 * pm_noirq_op - Return the PM operation appropriate for given PM event.
409 * @ops: PM operations to choose from.
410 * @state: PM transition of the system being carried out.
411 *
412 * The driver of @dev will not receive interrupts while this function is being
413 * executed.
414 */
pm_noirq_op(const struct dev_pm_ops * ops,pm_message_t state)415 static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
416 {
417 switch (state.event) {
418 #ifdef CONFIG_SUSPEND
419 case PM_EVENT_SUSPEND:
420 return ops->suspend_noirq;
421 case PM_EVENT_RESUME:
422 return ops->resume_noirq;
423 #endif /* CONFIG_SUSPEND */
424 #ifdef CONFIG_HIBERNATE_CALLBACKS
425 case PM_EVENT_FREEZE:
426 case PM_EVENT_QUIESCE:
427 return ops->freeze_noirq;
428 case PM_EVENT_HIBERNATE:
429 return ops->poweroff_noirq;
430 case PM_EVENT_THAW:
431 case PM_EVENT_RECOVER:
432 return ops->thaw_noirq;
433 case PM_EVENT_RESTORE:
434 return ops->restore_noirq;
435 #endif /* CONFIG_HIBERNATE_CALLBACKS */
436 }
437
438 return NULL;
439 }
440
pm_dev_dbg(struct device * dev,pm_message_t state,const char * info)441 static void pm_dev_dbg(struct device *dev, pm_message_t state, const char *info)
442 {
443 dev_dbg(dev, "%s%s%s driver flags: %x\n", info, pm_verb(state.event),
444 ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
445 ", may wakeup" : "", dev->power.driver_flags);
446 }
447
pm_dev_err(struct device * dev,pm_message_t state,const char * info,int error)448 static void pm_dev_err(struct device *dev, pm_message_t state, const char *info,
449 int error)
450 {
451 dev_err(dev, "failed to %s%s: error %d\n", pm_verb(state.event), info,
452 error);
453 }
454
dpm_show_time(ktime_t starttime,pm_message_t state,int error,const char * info)455 static void dpm_show_time(ktime_t starttime, pm_message_t state, int error,
456 const char *info)
457 {
458 ktime_t calltime;
459 u64 usecs64;
460 int usecs;
461
462 calltime = ktime_get();
463 usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
464 do_div(usecs64, NSEC_PER_USEC);
465 usecs = usecs64;
466 if (usecs == 0)
467 usecs = 1;
468
469 pm_pr_dbg("%s%s%s of devices %s after %ld.%03ld msecs\n",
470 info ?: "", info ? " " : "", pm_verb(state.event),
471 error ? "aborted" : "complete",
472 usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
473 }
474
dpm_run_callback(pm_callback_t cb,struct device * dev,pm_message_t state,const char * info)475 static int dpm_run_callback(pm_callback_t cb, struct device *dev,
476 pm_message_t state, const char *info)
477 {
478 ktime_t calltime;
479 int error;
480
481 if (!cb)
482 return 0;
483
484 calltime = initcall_debug_start(dev, cb);
485
486 pm_dev_dbg(dev, state, info);
487 trace_device_pm_callback_start(dev, info, state.event);
488 error = cb(dev);
489 trace_device_pm_callback_end(dev, error);
490 suspend_report_result(cb, error);
491
492 initcall_debug_report(dev, calltime, cb, error);
493
494 return error;
495 }
496
497 #ifdef CONFIG_DPM_WATCHDOG
498 struct dpm_watchdog {
499 struct device *dev;
500 struct task_struct *tsk;
501 struct timer_list timer;
502 };
503
504 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
505 struct dpm_watchdog wd
506
507 /**
508 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
509 * @t: The timer that PM watchdog depends on.
510 *
511 * Called when a driver has timed out suspending or resuming.
512 * There's not much we can do here to recover so panic() to
513 * capture a crash-dump in pstore.
514 */
dpm_watchdog_handler(struct timer_list * t)515 static void dpm_watchdog_handler(struct timer_list *t)
516 {
517 struct dpm_watchdog *wd = from_timer(wd, t, timer);
518
519 dev_emerg(wd->dev, "**** DPM device timeout ****\n");
520 show_stack(wd->tsk, NULL, KERN_EMERG);
521 panic("%s %s: unrecoverable failure\n",
522 dev_driver_string(wd->dev), dev_name(wd->dev));
523 }
524
525 /**
526 * dpm_watchdog_set - Enable pm watchdog for given device.
527 * @wd: Watchdog. Must be allocated on the stack.
528 * @dev: Device to handle.
529 */
dpm_watchdog_set(struct dpm_watchdog * wd,struct device * dev)530 static void dpm_watchdog_set(struct dpm_watchdog *wd, struct device *dev)
531 {
532 struct timer_list *timer = &wd->timer;
533
534 wd->dev = dev;
535 wd->tsk = current;
536
537 timer_setup_on_stack(timer, dpm_watchdog_handler, 0);
538 /* use same timeout value for both suspend and resume */
539 timer->expires = jiffies + HZ * CONFIG_DPM_WATCHDOG_TIMEOUT;
540 add_timer(timer);
541 }
542
543 /**
544 * dpm_watchdog_clear - Disable suspend/resume watchdog.
545 * @wd: Watchdog to disable.
546 */
dpm_watchdog_clear(struct dpm_watchdog * wd)547 static void dpm_watchdog_clear(struct dpm_watchdog *wd)
548 {
549 struct timer_list *timer = &wd->timer;
550
551 del_timer_sync(timer);
552 destroy_timer_on_stack(timer);
553 }
554 #else
555 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
556 #define dpm_watchdog_set(x, y)
557 #define dpm_watchdog_clear(x)
558 #endif
559
560 /*------------------------- Resume routines -------------------------*/
561
562 /**
563 * dev_pm_skip_resume - System-wide device resume optimization check.
564 * @dev: Target device.
565 *
566 * Return:
567 * - %false if the transition under way is RESTORE.
568 * - Return value of dev_pm_skip_suspend() if the transition under way is THAW.
569 * - The logical negation of %power.must_resume otherwise (that is, when the
570 * transition under way is RESUME).
571 */
dev_pm_skip_resume(struct device * dev)572 bool dev_pm_skip_resume(struct device *dev)
573 {
574 if (pm_transition.event == PM_EVENT_RESTORE)
575 return false;
576
577 if (pm_transition.event == PM_EVENT_THAW)
578 return dev_pm_skip_suspend(dev);
579
580 return !dev->power.must_resume;
581 }
582
583 /**
584 * __device_resume_noirq - Execute a "noirq resume" callback for given device.
585 * @dev: Device to handle.
586 * @state: PM transition of the system being carried out.
587 * @async: If true, the device is being resumed asynchronously.
588 *
589 * The driver of @dev will not receive interrupts while this function is being
590 * executed.
591 */
__device_resume_noirq(struct device * dev,pm_message_t state,bool async)592 static void __device_resume_noirq(struct device *dev, pm_message_t state, bool async)
593 {
594 pm_callback_t callback = NULL;
595 const char *info = NULL;
596 bool skip_resume;
597 int error = 0;
598
599 TRACE_DEVICE(dev);
600 TRACE_RESUME(0);
601
602 if (dev->power.syscore || dev->power.direct_complete)
603 goto Out;
604
605 if (!dev->power.is_noirq_suspended)
606 goto Out;
607
608 if (!dpm_wait_for_superior(dev, async))
609 goto Out;
610
611 skip_resume = dev_pm_skip_resume(dev);
612 /*
613 * If the driver callback is skipped below or by the middle layer
614 * callback and device_resume_early() also skips the driver callback for
615 * this device later, it needs to appear as "suspended" to PM-runtime,
616 * so change its status accordingly.
617 *
618 * Otherwise, the device is going to be resumed, so set its PM-runtime
619 * status to "active", but do that only if DPM_FLAG_SMART_SUSPEND is set
620 * to avoid confusing drivers that don't use it.
621 */
622 if (skip_resume)
623 pm_runtime_set_suspended(dev);
624 else if (dev_pm_skip_suspend(dev))
625 pm_runtime_set_active(dev);
626
627 if (dev->pm_domain) {
628 info = "noirq power domain ";
629 callback = pm_noirq_op(&dev->pm_domain->ops, state);
630 } else if (dev->type && dev->type->pm) {
631 info = "noirq type ";
632 callback = pm_noirq_op(dev->type->pm, state);
633 } else if (dev->class && dev->class->pm) {
634 info = "noirq class ";
635 callback = pm_noirq_op(dev->class->pm, state);
636 } else if (dev->bus && dev->bus->pm) {
637 info = "noirq bus ";
638 callback = pm_noirq_op(dev->bus->pm, state);
639 }
640 if (callback)
641 goto Run;
642
643 if (skip_resume)
644 goto Skip;
645
646 if (dev->driver && dev->driver->pm) {
647 info = "noirq driver ";
648 callback = pm_noirq_op(dev->driver->pm, state);
649 }
650
651 Run:
652 error = dpm_run_callback(callback, dev, state, info);
653
654 Skip:
655 dev->power.is_noirq_suspended = false;
656
657 Out:
658 complete_all(&dev->power.completion);
659 TRACE_RESUME(error);
660
661 if (error) {
662 suspend_stats.failed_resume_noirq++;
663 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
664 dpm_save_failed_dev(dev_name(dev));
665 pm_dev_err(dev, state, async ? " async noirq" : " noirq", error);
666 }
667 }
668
is_async(struct device * dev)669 static bool is_async(struct device *dev)
670 {
671 return dev->power.async_suspend && pm_async_enabled
672 && !pm_trace_is_enabled();
673 }
674
dpm_async_fn(struct device * dev,async_func_t func)675 static bool dpm_async_fn(struct device *dev, async_func_t func)
676 {
677 reinit_completion(&dev->power.completion);
678
679 if (!is_async(dev))
680 return false;
681
682 get_device(dev);
683
684 if (async_schedule_dev_nocall(func, dev))
685 return true;
686
687 put_device(dev);
688
689 return false;
690 }
691
async_resume_noirq(void * data,async_cookie_t cookie)692 static void async_resume_noirq(void *data, async_cookie_t cookie)
693 {
694 struct device *dev = data;
695
696 __device_resume_noirq(dev, pm_transition, true);
697 put_device(dev);
698 }
699
device_resume_noirq(struct device * dev)700 static void device_resume_noirq(struct device *dev)
701 {
702 if (dpm_async_fn(dev, async_resume_noirq))
703 return;
704
705 __device_resume_noirq(dev, pm_transition, false);
706 }
707
dpm_noirq_resume_devices(pm_message_t state)708 static void dpm_noirq_resume_devices(pm_message_t state)
709 {
710 struct device *dev;
711 ktime_t starttime = ktime_get();
712
713 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true);
714 mutex_lock(&dpm_list_mtx);
715 pm_transition = state;
716
717 while (!list_empty(&dpm_noirq_list)) {
718 dev = to_device(dpm_noirq_list.next);
719 get_device(dev);
720 list_move_tail(&dev->power.entry, &dpm_late_early_list);
721
722 mutex_unlock(&dpm_list_mtx);
723
724 device_resume_noirq(dev);
725
726 put_device(dev);
727
728 mutex_lock(&dpm_list_mtx);
729 }
730 mutex_unlock(&dpm_list_mtx);
731 async_synchronize_full();
732 dpm_show_time(starttime, state, 0, "noirq");
733 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false);
734 }
735
736 /**
737 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
738 * @state: PM transition of the system being carried out.
739 *
740 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
741 * allow device drivers' interrupt handlers to be called.
742 */
dpm_resume_noirq(pm_message_t state)743 void dpm_resume_noirq(pm_message_t state)
744 {
745 dpm_noirq_resume_devices(state);
746
747 resume_device_irqs();
748 device_wakeup_disarm_wake_irqs();
749
750 cpuidle_resume();
751 }
752
753 /**
754 * __device_resume_early - Execute an "early resume" callback for given device.
755 * @dev: Device to handle.
756 * @state: PM transition of the system being carried out.
757 * @async: If true, the device is being resumed asynchronously.
758 *
759 * Runtime PM is disabled for @dev while this function is being executed.
760 */
__device_resume_early(struct device * dev,pm_message_t state,bool async)761 static void __device_resume_early(struct device *dev, pm_message_t state, bool async)
762 {
763 pm_callback_t callback = NULL;
764 const char *info = NULL;
765 int error = 0;
766
767 TRACE_DEVICE(dev);
768 TRACE_RESUME(0);
769
770 if (dev->power.syscore || dev->power.direct_complete)
771 goto Out;
772
773 if (!dev->power.is_late_suspended)
774 goto Out;
775
776 if (!dpm_wait_for_superior(dev, async))
777 goto Out;
778
779 if (dev->pm_domain) {
780 info = "early power domain ";
781 callback = pm_late_early_op(&dev->pm_domain->ops, state);
782 } else if (dev->type && dev->type->pm) {
783 info = "early type ";
784 callback = pm_late_early_op(dev->type->pm, state);
785 } else if (dev->class && dev->class->pm) {
786 info = "early class ";
787 callback = pm_late_early_op(dev->class->pm, state);
788 } else if (dev->bus && dev->bus->pm) {
789 info = "early bus ";
790 callback = pm_late_early_op(dev->bus->pm, state);
791 }
792 if (callback)
793 goto Run;
794
795 if (dev_pm_skip_resume(dev))
796 goto Skip;
797
798 if (dev->driver && dev->driver->pm) {
799 info = "early driver ";
800 callback = pm_late_early_op(dev->driver->pm, state);
801 }
802
803 Run:
804 error = dpm_run_callback(callback, dev, state, info);
805
806 Skip:
807 dev->power.is_late_suspended = false;
808
809 Out:
810 TRACE_RESUME(error);
811
812 pm_runtime_enable(dev);
813 complete_all(&dev->power.completion);
814
815 if (error) {
816 suspend_stats.failed_resume_early++;
817 dpm_save_failed_step(SUSPEND_RESUME_EARLY);
818 dpm_save_failed_dev(dev_name(dev));
819 pm_dev_err(dev, state, async ? " async early" : " early", error);
820 }
821 }
822
async_resume_early(void * data,async_cookie_t cookie)823 static void async_resume_early(void *data, async_cookie_t cookie)
824 {
825 struct device *dev = data;
826
827 __device_resume_early(dev, pm_transition, true);
828 put_device(dev);
829 }
830
device_resume_early(struct device * dev)831 static void device_resume_early(struct device *dev)
832 {
833 if (dpm_async_fn(dev, async_resume_early))
834 return;
835
836 __device_resume_early(dev, pm_transition, false);
837 }
838
839 /**
840 * dpm_resume_early - Execute "early resume" callbacks for all devices.
841 * @state: PM transition of the system being carried out.
842 */
dpm_resume_early(pm_message_t state)843 void dpm_resume_early(pm_message_t state)
844 {
845 struct device *dev;
846 ktime_t starttime = ktime_get();
847
848 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true);
849 mutex_lock(&dpm_list_mtx);
850 pm_transition = state;
851
852 while (!list_empty(&dpm_late_early_list)) {
853 dev = to_device(dpm_late_early_list.next);
854 get_device(dev);
855 list_move_tail(&dev->power.entry, &dpm_suspended_list);
856
857 mutex_unlock(&dpm_list_mtx);
858
859 device_resume_early(dev);
860
861 put_device(dev);
862
863 mutex_lock(&dpm_list_mtx);
864 }
865 mutex_unlock(&dpm_list_mtx);
866 async_synchronize_full();
867 dpm_show_time(starttime, state, 0, "early");
868 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false);
869 }
870
871 /**
872 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
873 * @state: PM transition of the system being carried out.
874 */
dpm_resume_start(pm_message_t state)875 void dpm_resume_start(pm_message_t state)
876 {
877 dpm_resume_noirq(state);
878 dpm_resume_early(state);
879 }
880 EXPORT_SYMBOL_GPL(dpm_resume_start);
881
882 /**
883 * __device_resume - Execute "resume" callbacks for given device.
884 * @dev: Device to handle.
885 * @state: PM transition of the system being carried out.
886 * @async: If true, the device is being resumed asynchronously.
887 */
__device_resume(struct device * dev,pm_message_t state,bool async)888 static void __device_resume(struct device *dev, pm_message_t state, bool async)
889 {
890 pm_callback_t callback = NULL;
891 const char *info = NULL;
892 int error = 0;
893 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
894
895 TRACE_DEVICE(dev);
896 TRACE_RESUME(0);
897
898 if (dev->power.syscore)
899 goto Complete;
900
901 if (dev->power.direct_complete) {
902 /* Match the pm_runtime_disable() in __device_suspend(). */
903 pm_runtime_enable(dev);
904 goto Complete;
905 }
906
907 if (!dpm_wait_for_superior(dev, async))
908 goto Complete;
909
910 dpm_watchdog_set(&wd, dev);
911 device_lock(dev);
912
913 /*
914 * This is a fib. But we'll allow new children to be added below
915 * a resumed device, even if the device hasn't been completed yet.
916 */
917 dev->power.is_prepared = false;
918
919 if (!dev->power.is_suspended)
920 goto Unlock;
921
922 if (dev->pm_domain) {
923 info = "power domain ";
924 callback = pm_op(&dev->pm_domain->ops, state);
925 goto Driver;
926 }
927
928 if (dev->type && dev->type->pm) {
929 info = "type ";
930 callback = pm_op(dev->type->pm, state);
931 goto Driver;
932 }
933
934 if (dev->class && dev->class->pm) {
935 info = "class ";
936 callback = pm_op(dev->class->pm, state);
937 goto Driver;
938 }
939
940 if (dev->bus) {
941 if (dev->bus->pm) {
942 info = "bus ";
943 callback = pm_op(dev->bus->pm, state);
944 } else if (dev->bus->resume) {
945 info = "legacy bus ";
946 callback = dev->bus->resume;
947 goto End;
948 }
949 }
950
951 Driver:
952 if (!callback && dev->driver && dev->driver->pm) {
953 info = "driver ";
954 callback = pm_op(dev->driver->pm, state);
955 }
956
957 End:
958 error = dpm_run_callback(callback, dev, state, info);
959 dev->power.is_suspended = false;
960
961 Unlock:
962 device_unlock(dev);
963 dpm_watchdog_clear(&wd);
964
965 Complete:
966 complete_all(&dev->power.completion);
967
968 TRACE_RESUME(error);
969
970 if (error) {
971 suspend_stats.failed_resume++;
972 dpm_save_failed_step(SUSPEND_RESUME);
973 dpm_save_failed_dev(dev_name(dev));
974 pm_dev_err(dev, state, async ? " async" : "", error);
975 }
976 }
977
async_resume(void * data,async_cookie_t cookie)978 static void async_resume(void *data, async_cookie_t cookie)
979 {
980 struct device *dev = data;
981
982 __device_resume(dev, pm_transition, true);
983 put_device(dev);
984 }
985
device_resume(struct device * dev)986 static void device_resume(struct device *dev)
987 {
988 if (dpm_async_fn(dev, async_resume))
989 return;
990
991 __device_resume(dev, pm_transition, false);
992 }
993
994 /**
995 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
996 * @state: PM transition of the system being carried out.
997 *
998 * Execute the appropriate "resume" callback for all devices whose status
999 * indicates that they are suspended.
1000 */
dpm_resume(pm_message_t state)1001 void dpm_resume(pm_message_t state)
1002 {
1003 struct device *dev;
1004 ktime_t starttime = ktime_get();
1005
1006 trace_suspend_resume(TPS("dpm_resume"), state.event, true);
1007 might_sleep();
1008
1009 mutex_lock(&dpm_list_mtx);
1010 pm_transition = state;
1011 async_error = 0;
1012
1013 while (!list_empty(&dpm_suspended_list)) {
1014 dev = to_device(dpm_suspended_list.next);
1015
1016 get_device(dev);
1017
1018 mutex_unlock(&dpm_list_mtx);
1019
1020 device_resume(dev);
1021
1022 mutex_lock(&dpm_list_mtx);
1023
1024 if (!list_empty(&dev->power.entry))
1025 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1026
1027 mutex_unlock(&dpm_list_mtx);
1028
1029 put_device(dev);
1030
1031 mutex_lock(&dpm_list_mtx);
1032 }
1033 mutex_unlock(&dpm_list_mtx);
1034 async_synchronize_full();
1035 dpm_show_time(starttime, state, 0, NULL);
1036
1037 cpufreq_resume();
1038 devfreq_resume();
1039 trace_suspend_resume(TPS("dpm_resume"), state.event, false);
1040 }
1041
1042 /**
1043 * device_complete - Complete a PM transition for given device.
1044 * @dev: Device to handle.
1045 * @state: PM transition of the system being carried out.
1046 */
device_complete(struct device * dev,pm_message_t state)1047 static void device_complete(struct device *dev, pm_message_t state)
1048 {
1049 void (*callback)(struct device *) = NULL;
1050 const char *info = NULL;
1051
1052 if (dev->power.syscore)
1053 goto out;
1054
1055 device_lock(dev);
1056
1057 if (dev->pm_domain) {
1058 info = "completing power domain ";
1059 callback = dev->pm_domain->ops.complete;
1060 } else if (dev->type && dev->type->pm) {
1061 info = "completing type ";
1062 callback = dev->type->pm->complete;
1063 } else if (dev->class && dev->class->pm) {
1064 info = "completing class ";
1065 callback = dev->class->pm->complete;
1066 } else if (dev->bus && dev->bus->pm) {
1067 info = "completing bus ";
1068 callback = dev->bus->pm->complete;
1069 }
1070
1071 if (!callback && dev->driver && dev->driver->pm) {
1072 info = "completing driver ";
1073 callback = dev->driver->pm->complete;
1074 }
1075
1076 if (callback) {
1077 pm_dev_dbg(dev, state, info);
1078 callback(dev);
1079 }
1080
1081 device_unlock(dev);
1082
1083 out:
1084 pm_runtime_put(dev);
1085 }
1086
1087 /**
1088 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1089 * @state: PM transition of the system being carried out.
1090 *
1091 * Execute the ->complete() callbacks for all devices whose PM status is not
1092 * DPM_ON (this allows new devices to be registered).
1093 */
dpm_complete(pm_message_t state)1094 void dpm_complete(pm_message_t state)
1095 {
1096 struct list_head list;
1097
1098 trace_suspend_resume(TPS("dpm_complete"), state.event, true);
1099 might_sleep();
1100
1101 INIT_LIST_HEAD(&list);
1102 mutex_lock(&dpm_list_mtx);
1103 while (!list_empty(&dpm_prepared_list)) {
1104 struct device *dev = to_device(dpm_prepared_list.prev);
1105
1106 get_device(dev);
1107 dev->power.is_prepared = false;
1108 list_move(&dev->power.entry, &list);
1109
1110 mutex_unlock(&dpm_list_mtx);
1111
1112 trace_device_pm_callback_start(dev, "", state.event);
1113 device_complete(dev, state);
1114 trace_device_pm_callback_end(dev, 0);
1115
1116 put_device(dev);
1117
1118 mutex_lock(&dpm_list_mtx);
1119 }
1120 list_splice(&list, &dpm_list);
1121 mutex_unlock(&dpm_list_mtx);
1122
1123 /* Allow device probing and trigger re-probing of deferred devices */
1124 device_unblock_probing();
1125 trace_suspend_resume(TPS("dpm_complete"), state.event, false);
1126 }
1127
1128 /**
1129 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1130 * @state: PM transition of the system being carried out.
1131 *
1132 * Execute "resume" callbacks for all devices and complete the PM transition of
1133 * the system.
1134 */
dpm_resume_end(pm_message_t state)1135 void dpm_resume_end(pm_message_t state)
1136 {
1137 dpm_resume(state);
1138 dpm_complete(state);
1139 }
1140 EXPORT_SYMBOL_GPL(dpm_resume_end);
1141
1142
1143 /*------------------------- Suspend routines -------------------------*/
1144
1145 /**
1146 * resume_event - Return a "resume" message for given "suspend" sleep state.
1147 * @sleep_state: PM message representing a sleep state.
1148 *
1149 * Return a PM message representing the resume event corresponding to given
1150 * sleep state.
1151 */
resume_event(pm_message_t sleep_state)1152 static pm_message_t resume_event(pm_message_t sleep_state)
1153 {
1154 switch (sleep_state.event) {
1155 case PM_EVENT_SUSPEND:
1156 return PMSG_RESUME;
1157 case PM_EVENT_FREEZE:
1158 case PM_EVENT_QUIESCE:
1159 return PMSG_RECOVER;
1160 case PM_EVENT_HIBERNATE:
1161 return PMSG_RESTORE;
1162 }
1163 return PMSG_ON;
1164 }
1165
dpm_superior_set_must_resume(struct device * dev)1166 static void dpm_superior_set_must_resume(struct device *dev)
1167 {
1168 struct device_link *link;
1169 int idx;
1170
1171 if (dev->parent)
1172 dev->parent->power.must_resume = true;
1173
1174 idx = device_links_read_lock();
1175
1176 list_for_each_entry_rcu_locked(link, &dev->links.suppliers, c_node)
1177 link->supplier->power.must_resume = true;
1178
1179 device_links_read_unlock(idx);
1180 }
1181
1182 /**
1183 * __device_suspend_noirq - Execute a "noirq suspend" callback for given device.
1184 * @dev: Device to handle.
1185 * @state: PM transition of the system being carried out.
1186 * @async: If true, the device is being suspended asynchronously.
1187 *
1188 * The driver of @dev will not receive interrupts while this function is being
1189 * executed.
1190 */
__device_suspend_noirq(struct device * dev,pm_message_t state,bool async)1191 static int __device_suspend_noirq(struct device *dev, pm_message_t state, bool async)
1192 {
1193 pm_callback_t callback = NULL;
1194 const char *info = NULL;
1195 int error = 0;
1196
1197 TRACE_DEVICE(dev);
1198 TRACE_SUSPEND(0);
1199
1200 dpm_wait_for_subordinate(dev, async);
1201
1202 if (async_error)
1203 goto Complete;
1204
1205 if (dev->power.syscore || dev->power.direct_complete)
1206 goto Complete;
1207
1208 if (dev->pm_domain) {
1209 info = "noirq power domain ";
1210 callback = pm_noirq_op(&dev->pm_domain->ops, state);
1211 } else if (dev->type && dev->type->pm) {
1212 info = "noirq type ";
1213 callback = pm_noirq_op(dev->type->pm, state);
1214 } else if (dev->class && dev->class->pm) {
1215 info = "noirq class ";
1216 callback = pm_noirq_op(dev->class->pm, state);
1217 } else if (dev->bus && dev->bus->pm) {
1218 info = "noirq bus ";
1219 callback = pm_noirq_op(dev->bus->pm, state);
1220 }
1221 if (callback)
1222 goto Run;
1223
1224 if (dev_pm_skip_suspend(dev))
1225 goto Skip;
1226
1227 if (dev->driver && dev->driver->pm) {
1228 info = "noirq driver ";
1229 callback = pm_noirq_op(dev->driver->pm, state);
1230 }
1231
1232 Run:
1233 error = dpm_run_callback(callback, dev, state, info);
1234 if (error) {
1235 async_error = error;
1236 log_suspend_abort_reason("Callback failed on %s in %pS returned %d",
1237 dev_name(dev), callback, error);
1238 goto Complete;
1239 }
1240
1241 Skip:
1242 dev->power.is_noirq_suspended = true;
1243
1244 /*
1245 * Skipping the resume of devices that were in use right before the
1246 * system suspend (as indicated by their PM-runtime usage counters)
1247 * would be suboptimal. Also resume them if doing that is not allowed
1248 * to be skipped.
1249 */
1250 if (atomic_read(&dev->power.usage_count) > 1 ||
1251 !(dev_pm_test_driver_flags(dev, DPM_FLAG_MAY_SKIP_RESUME) &&
1252 dev->power.may_skip_resume))
1253 dev->power.must_resume = true;
1254
1255 if (dev->power.must_resume)
1256 dpm_superior_set_must_resume(dev);
1257
1258 Complete:
1259 complete_all(&dev->power.completion);
1260 TRACE_SUSPEND(error);
1261 return error;
1262 }
1263
async_suspend_noirq(void * data,async_cookie_t cookie)1264 static void async_suspend_noirq(void *data, async_cookie_t cookie)
1265 {
1266 struct device *dev = data;
1267 int error;
1268
1269 error = __device_suspend_noirq(dev, pm_transition, true);
1270 if (error) {
1271 dpm_save_failed_dev(dev_name(dev));
1272 pm_dev_err(dev, pm_transition, " async", error);
1273 }
1274
1275 put_device(dev);
1276 }
1277
device_suspend_noirq(struct device * dev)1278 static int device_suspend_noirq(struct device *dev)
1279 {
1280 if (dpm_async_fn(dev, async_suspend_noirq))
1281 return 0;
1282
1283 return __device_suspend_noirq(dev, pm_transition, false);
1284 }
1285
dpm_noirq_suspend_devices(pm_message_t state)1286 static int dpm_noirq_suspend_devices(pm_message_t state)
1287 {
1288 ktime_t starttime = ktime_get();
1289 int error = 0;
1290
1291 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true);
1292 mutex_lock(&dpm_list_mtx);
1293 pm_transition = state;
1294 async_error = 0;
1295
1296 while (!list_empty(&dpm_late_early_list)) {
1297 struct device *dev = to_device(dpm_late_early_list.prev);
1298
1299 get_device(dev);
1300 mutex_unlock(&dpm_list_mtx);
1301
1302 error = device_suspend_noirq(dev);
1303
1304 mutex_lock(&dpm_list_mtx);
1305
1306 if (error) {
1307 pm_dev_err(dev, state, " noirq", error);
1308 dpm_save_failed_dev(dev_name(dev));
1309 } else if (!list_empty(&dev->power.entry)) {
1310 list_move(&dev->power.entry, &dpm_noirq_list);
1311 }
1312
1313 mutex_unlock(&dpm_list_mtx);
1314
1315 put_device(dev);
1316
1317 mutex_lock(&dpm_list_mtx);
1318
1319 if (error || async_error)
1320 break;
1321 }
1322 mutex_unlock(&dpm_list_mtx);
1323 async_synchronize_full();
1324 if (!error)
1325 error = async_error;
1326
1327 if (error) {
1328 suspend_stats.failed_suspend_noirq++;
1329 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
1330 }
1331 dpm_show_time(starttime, state, error, "noirq");
1332 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false);
1333 return error;
1334 }
1335
1336 /**
1337 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1338 * @state: PM transition of the system being carried out.
1339 *
1340 * Prevent device drivers' interrupt handlers from being called and invoke
1341 * "noirq" suspend callbacks for all non-sysdev devices.
1342 */
dpm_suspend_noirq(pm_message_t state)1343 int dpm_suspend_noirq(pm_message_t state)
1344 {
1345 int ret;
1346
1347 cpuidle_pause();
1348
1349 device_wakeup_arm_wake_irqs();
1350 suspend_device_irqs();
1351
1352 ret = dpm_noirq_suspend_devices(state);
1353 if (ret)
1354 dpm_resume_noirq(resume_event(state));
1355
1356 return ret;
1357 }
1358
dpm_propagate_wakeup_to_parent(struct device * dev)1359 static void dpm_propagate_wakeup_to_parent(struct device *dev)
1360 {
1361 struct device *parent = dev->parent;
1362
1363 if (!parent)
1364 return;
1365
1366 spin_lock_irq(&parent->power.lock);
1367
1368 if (device_wakeup_path(dev) && !parent->power.ignore_children)
1369 parent->power.wakeup_path = true;
1370
1371 spin_unlock_irq(&parent->power.lock);
1372 }
1373
1374 /**
1375 * __device_suspend_late - Execute a "late suspend" callback for given device.
1376 * @dev: Device to handle.
1377 * @state: PM transition of the system being carried out.
1378 * @async: If true, the device is being suspended asynchronously.
1379 *
1380 * Runtime PM is disabled for @dev while this function is being executed.
1381 */
__device_suspend_late(struct device * dev,pm_message_t state,bool async)1382 static int __device_suspend_late(struct device *dev, pm_message_t state, bool async)
1383 {
1384 pm_callback_t callback = NULL;
1385 const char *info = NULL;
1386 int error = 0;
1387
1388 TRACE_DEVICE(dev);
1389 TRACE_SUSPEND(0);
1390
1391 __pm_runtime_disable(dev, false);
1392
1393 dpm_wait_for_subordinate(dev, async);
1394
1395 if (async_error)
1396 goto Complete;
1397
1398 if (pm_wakeup_pending()) {
1399 async_error = -EBUSY;
1400 goto Complete;
1401 }
1402
1403 if (dev->power.syscore || dev->power.direct_complete)
1404 goto Complete;
1405
1406 if (dev->pm_domain) {
1407 info = "late power domain ";
1408 callback = pm_late_early_op(&dev->pm_domain->ops, state);
1409 } else if (dev->type && dev->type->pm) {
1410 info = "late type ";
1411 callback = pm_late_early_op(dev->type->pm, state);
1412 } else if (dev->class && dev->class->pm) {
1413 info = "late class ";
1414 callback = pm_late_early_op(dev->class->pm, state);
1415 } else if (dev->bus && dev->bus->pm) {
1416 info = "late bus ";
1417 callback = pm_late_early_op(dev->bus->pm, state);
1418 }
1419 if (callback)
1420 goto Run;
1421
1422 if (dev_pm_skip_suspend(dev))
1423 goto Skip;
1424
1425 if (dev->driver && dev->driver->pm) {
1426 info = "late driver ";
1427 callback = pm_late_early_op(dev->driver->pm, state);
1428 }
1429
1430 Run:
1431 error = dpm_run_callback(callback, dev, state, info);
1432 if (error) {
1433 async_error = error;
1434 log_suspend_abort_reason("Callback failed on %s in %pS returned %d",
1435 dev_name(dev), callback, error);
1436 goto Complete;
1437 }
1438 dpm_propagate_wakeup_to_parent(dev);
1439
1440 Skip:
1441 dev->power.is_late_suspended = true;
1442
1443 Complete:
1444 TRACE_SUSPEND(error);
1445 complete_all(&dev->power.completion);
1446 return error;
1447 }
1448
async_suspend_late(void * data,async_cookie_t cookie)1449 static void async_suspend_late(void *data, async_cookie_t cookie)
1450 {
1451 struct device *dev = data;
1452 int error;
1453
1454 error = __device_suspend_late(dev, pm_transition, true);
1455 if (error) {
1456 dpm_save_failed_dev(dev_name(dev));
1457 pm_dev_err(dev, pm_transition, " async", error);
1458 }
1459 put_device(dev);
1460 }
1461
device_suspend_late(struct device * dev)1462 static int device_suspend_late(struct device *dev)
1463 {
1464 if (dpm_async_fn(dev, async_suspend_late))
1465 return 0;
1466
1467 return __device_suspend_late(dev, pm_transition, false);
1468 }
1469
1470 /**
1471 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1472 * @state: PM transition of the system being carried out.
1473 */
dpm_suspend_late(pm_message_t state)1474 int dpm_suspend_late(pm_message_t state)
1475 {
1476 ktime_t starttime = ktime_get();
1477 int error = 0;
1478
1479 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true);
1480 mutex_lock(&dpm_list_mtx);
1481 pm_transition = state;
1482 async_error = 0;
1483
1484 while (!list_empty(&dpm_suspended_list)) {
1485 struct device *dev = to_device(dpm_suspended_list.prev);
1486
1487 get_device(dev);
1488
1489 mutex_unlock(&dpm_list_mtx);
1490
1491 error = device_suspend_late(dev);
1492
1493 mutex_lock(&dpm_list_mtx);
1494
1495 if (!list_empty(&dev->power.entry))
1496 list_move(&dev->power.entry, &dpm_late_early_list);
1497
1498 if (error) {
1499 pm_dev_err(dev, state, " late", error);
1500 dpm_save_failed_dev(dev_name(dev));
1501 }
1502
1503 mutex_unlock(&dpm_list_mtx);
1504
1505 put_device(dev);
1506
1507 mutex_lock(&dpm_list_mtx);
1508
1509 if (error || async_error)
1510 break;
1511 }
1512 mutex_unlock(&dpm_list_mtx);
1513 async_synchronize_full();
1514 if (!error)
1515 error = async_error;
1516 if (error) {
1517 suspend_stats.failed_suspend_late++;
1518 dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
1519 dpm_resume_early(resume_event(state));
1520 }
1521 dpm_show_time(starttime, state, error, "late");
1522 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false);
1523 return error;
1524 }
1525
1526 /**
1527 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1528 * @state: PM transition of the system being carried out.
1529 */
dpm_suspend_end(pm_message_t state)1530 int dpm_suspend_end(pm_message_t state)
1531 {
1532 ktime_t starttime = ktime_get();
1533 int error;
1534
1535 error = dpm_suspend_late(state);
1536 if (error)
1537 goto out;
1538
1539 error = dpm_suspend_noirq(state);
1540 if (error)
1541 dpm_resume_early(resume_event(state));
1542
1543 out:
1544 dpm_show_time(starttime, state, error, "end");
1545 return error;
1546 }
1547 EXPORT_SYMBOL_GPL(dpm_suspend_end);
1548
1549 /**
1550 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1551 * @dev: Device to suspend.
1552 * @state: PM transition of the system being carried out.
1553 * @cb: Suspend callback to execute.
1554 * @info: string description of caller.
1555 */
legacy_suspend(struct device * dev,pm_message_t state,int (* cb)(struct device * dev,pm_message_t state),const char * info)1556 static int legacy_suspend(struct device *dev, pm_message_t state,
1557 int (*cb)(struct device *dev, pm_message_t state),
1558 const char *info)
1559 {
1560 int error;
1561 ktime_t calltime;
1562
1563 calltime = initcall_debug_start(dev, cb);
1564
1565 trace_device_pm_callback_start(dev, info, state.event);
1566 error = cb(dev, state);
1567 trace_device_pm_callback_end(dev, error);
1568 suspend_report_result(cb, error);
1569
1570 initcall_debug_report(dev, calltime, cb, error);
1571
1572 return error;
1573 }
1574
dpm_clear_superiors_direct_complete(struct device * dev)1575 static void dpm_clear_superiors_direct_complete(struct device *dev)
1576 {
1577 struct device_link *link;
1578 int idx;
1579
1580 if (dev->parent) {
1581 spin_lock_irq(&dev->parent->power.lock);
1582 dev->parent->power.direct_complete = false;
1583 spin_unlock_irq(&dev->parent->power.lock);
1584 }
1585
1586 idx = device_links_read_lock();
1587
1588 list_for_each_entry_rcu_locked(link, &dev->links.suppliers, c_node) {
1589 spin_lock_irq(&link->supplier->power.lock);
1590 link->supplier->power.direct_complete = false;
1591 spin_unlock_irq(&link->supplier->power.lock);
1592 }
1593
1594 device_links_read_unlock(idx);
1595 }
1596
1597 /**
1598 * __device_suspend - Execute "suspend" callbacks for given device.
1599 * @dev: Device to handle.
1600 * @state: PM transition of the system being carried out.
1601 * @async: If true, the device is being suspended asynchronously.
1602 */
__device_suspend(struct device * dev,pm_message_t state,bool async)1603 static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1604 {
1605 pm_callback_t callback = NULL;
1606 const char *info = NULL;
1607 int error = 0;
1608 DECLARE_DPM_WATCHDOG_ON_STACK(wd);
1609
1610 TRACE_DEVICE(dev);
1611 TRACE_SUSPEND(0);
1612
1613 dpm_wait_for_subordinate(dev, async);
1614
1615 if (async_error) {
1616 dev->power.direct_complete = false;
1617 goto Complete;
1618 }
1619
1620 /*
1621 * Wait for possible runtime PM transitions of the device in progress
1622 * to complete and if there's a runtime resume request pending for it,
1623 * resume it before proceeding with invoking the system-wide suspend
1624 * callbacks for it.
1625 *
1626 * If the system-wide suspend callbacks below change the configuration
1627 * of the device, they must disable runtime PM for it or otherwise
1628 * ensure that its runtime-resume callbacks will not be confused by that
1629 * change in case they are invoked going forward.
1630 */
1631 pm_runtime_barrier(dev);
1632
1633 if (pm_wakeup_pending()) {
1634 dev->power.direct_complete = false;
1635 async_error = -EBUSY;
1636 goto Complete;
1637 }
1638
1639 if (dev->power.syscore)
1640 goto Complete;
1641
1642 /* Avoid direct_complete to let wakeup_path propagate. */
1643 if (device_may_wakeup(dev) || device_wakeup_path(dev))
1644 dev->power.direct_complete = false;
1645
1646 if (dev->power.direct_complete) {
1647 if (pm_runtime_status_suspended(dev)) {
1648 pm_runtime_disable(dev);
1649 if (pm_runtime_status_suspended(dev)) {
1650 pm_dev_dbg(dev, state, "direct-complete ");
1651 goto Complete;
1652 }
1653
1654 pm_runtime_enable(dev);
1655 }
1656 dev->power.direct_complete = false;
1657 }
1658
1659 dev->power.may_skip_resume = true;
1660 dev->power.must_resume = !dev_pm_test_driver_flags(dev, DPM_FLAG_MAY_SKIP_RESUME);
1661
1662 dpm_watchdog_set(&wd, dev);
1663 device_lock(dev);
1664
1665 if (dev->pm_domain) {
1666 info = "power domain ";
1667 callback = pm_op(&dev->pm_domain->ops, state);
1668 goto Run;
1669 }
1670
1671 if (dev->type && dev->type->pm) {
1672 info = "type ";
1673 callback = pm_op(dev->type->pm, state);
1674 goto Run;
1675 }
1676
1677 if (dev->class && dev->class->pm) {
1678 info = "class ";
1679 callback = pm_op(dev->class->pm, state);
1680 goto Run;
1681 }
1682
1683 if (dev->bus) {
1684 if (dev->bus->pm) {
1685 info = "bus ";
1686 callback = pm_op(dev->bus->pm, state);
1687 } else if (dev->bus->suspend) {
1688 pm_dev_dbg(dev, state, "legacy bus ");
1689 error = legacy_suspend(dev, state, dev->bus->suspend,
1690 "legacy bus ");
1691 goto End;
1692 }
1693 }
1694
1695 Run:
1696 if (!callback && dev->driver && dev->driver->pm) {
1697 info = "driver ";
1698 callback = pm_op(dev->driver->pm, state);
1699 }
1700
1701 error = dpm_run_callback(callback, dev, state, info);
1702
1703 End:
1704 if (!error) {
1705 dev->power.is_suspended = true;
1706 if (device_may_wakeup(dev))
1707 dev->power.wakeup_path = true;
1708
1709 dpm_propagate_wakeup_to_parent(dev);
1710 dpm_clear_superiors_direct_complete(dev);
1711 } else {
1712 log_suspend_abort_reason("Callback failed on %s in %pS returned %d",
1713 dev_name(dev), callback, error);
1714 }
1715
1716 device_unlock(dev);
1717 dpm_watchdog_clear(&wd);
1718
1719 Complete:
1720 if (error)
1721 async_error = error;
1722
1723 complete_all(&dev->power.completion);
1724 TRACE_SUSPEND(error);
1725 return error;
1726 }
1727
async_suspend(void * data,async_cookie_t cookie)1728 static void async_suspend(void *data, async_cookie_t cookie)
1729 {
1730 struct device *dev = data;
1731 int error;
1732
1733 error = __device_suspend(dev, pm_transition, true);
1734 if (error) {
1735 dpm_save_failed_dev(dev_name(dev));
1736 pm_dev_err(dev, pm_transition, " async", error);
1737 }
1738
1739 put_device(dev);
1740 }
1741
device_suspend(struct device * dev)1742 static int device_suspend(struct device *dev)
1743 {
1744 if (dpm_async_fn(dev, async_suspend))
1745 return 0;
1746
1747 return __device_suspend(dev, pm_transition, false);
1748 }
1749
1750 /**
1751 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1752 * @state: PM transition of the system being carried out.
1753 */
dpm_suspend(pm_message_t state)1754 int dpm_suspend(pm_message_t state)
1755 {
1756 ktime_t starttime = ktime_get();
1757 int error = 0;
1758
1759 trace_suspend_resume(TPS("dpm_suspend"), state.event, true);
1760 might_sleep();
1761
1762 devfreq_suspend();
1763 cpufreq_suspend();
1764
1765 mutex_lock(&dpm_list_mtx);
1766 pm_transition = state;
1767 async_error = 0;
1768 while (!list_empty(&dpm_prepared_list)) {
1769 struct device *dev = to_device(dpm_prepared_list.prev);
1770
1771 get_device(dev);
1772
1773 mutex_unlock(&dpm_list_mtx);
1774
1775 error = device_suspend(dev);
1776
1777 mutex_lock(&dpm_list_mtx);
1778
1779 if (error) {
1780 pm_dev_err(dev, state, "", error);
1781 dpm_save_failed_dev(dev_name(dev));
1782 } else if (!list_empty(&dev->power.entry)) {
1783 list_move(&dev->power.entry, &dpm_suspended_list);
1784 }
1785
1786 mutex_unlock(&dpm_list_mtx);
1787
1788 put_device(dev);
1789
1790 mutex_lock(&dpm_list_mtx);
1791
1792 if (error || async_error)
1793 break;
1794 }
1795 mutex_unlock(&dpm_list_mtx);
1796 async_synchronize_full();
1797 if (!error)
1798 error = async_error;
1799 if (error) {
1800 suspend_stats.failed_suspend++;
1801 dpm_save_failed_step(SUSPEND_SUSPEND);
1802 }
1803 dpm_show_time(starttime, state, error, NULL);
1804 trace_suspend_resume(TPS("dpm_suspend"), state.event, false);
1805 return error;
1806 }
1807
1808 /**
1809 * device_prepare - Prepare a device for system power transition.
1810 * @dev: Device to handle.
1811 * @state: PM transition of the system being carried out.
1812 *
1813 * Execute the ->prepare() callback(s) for given device. No new children of the
1814 * device may be registered after this function has returned.
1815 */
device_prepare(struct device * dev,pm_message_t state)1816 static int device_prepare(struct device *dev, pm_message_t state)
1817 {
1818 int (*callback)(struct device *) = NULL;
1819 int ret = 0;
1820
1821 /*
1822 * If a device's parent goes into runtime suspend at the wrong time,
1823 * it won't be possible to resume the device. To prevent this we
1824 * block runtime suspend here, during the prepare phase, and allow
1825 * it again during the complete phase.
1826 */
1827 pm_runtime_get_noresume(dev);
1828
1829 if (dev->power.syscore)
1830 return 0;
1831
1832 device_lock(dev);
1833
1834 dev->power.wakeup_path = false;
1835
1836 if (dev->power.no_pm_callbacks)
1837 goto unlock;
1838
1839 if (dev->pm_domain)
1840 callback = dev->pm_domain->ops.prepare;
1841 else if (dev->type && dev->type->pm)
1842 callback = dev->type->pm->prepare;
1843 else if (dev->class && dev->class->pm)
1844 callback = dev->class->pm->prepare;
1845 else if (dev->bus && dev->bus->pm)
1846 callback = dev->bus->pm->prepare;
1847
1848 if (!callback && dev->driver && dev->driver->pm)
1849 callback = dev->driver->pm->prepare;
1850
1851 if (callback)
1852 ret = callback(dev);
1853
1854 unlock:
1855 device_unlock(dev);
1856
1857 if (ret < 0) {
1858 suspend_report_result(callback, ret);
1859 pm_runtime_put(dev);
1860 return ret;
1861 }
1862 /*
1863 * A positive return value from ->prepare() means "this device appears
1864 * to be runtime-suspended and its state is fine, so if it really is
1865 * runtime-suspended, you can leave it in that state provided that you
1866 * will do the same thing with all of its descendants". This only
1867 * applies to suspend transitions, however.
1868 */
1869 spin_lock_irq(&dev->power.lock);
1870 dev->power.direct_complete = state.event == PM_EVENT_SUSPEND &&
1871 (ret > 0 || dev->power.no_pm_callbacks) &&
1872 !dev_pm_test_driver_flags(dev, DPM_FLAG_NO_DIRECT_COMPLETE);
1873 spin_unlock_irq(&dev->power.lock);
1874 return 0;
1875 }
1876
1877 /**
1878 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1879 * @state: PM transition of the system being carried out.
1880 *
1881 * Execute the ->prepare() callback(s) for all devices.
1882 */
dpm_prepare(pm_message_t state)1883 int dpm_prepare(pm_message_t state)
1884 {
1885 int error = 0;
1886
1887 trace_suspend_resume(TPS("dpm_prepare"), state.event, true);
1888 might_sleep();
1889
1890 /*
1891 * Give a chance for the known devices to complete their probes, before
1892 * disable probing of devices. This sync point is important at least
1893 * at boot time + hibernation restore.
1894 */
1895 wait_for_device_probe();
1896 /*
1897 * It is unsafe if probing of devices will happen during suspend or
1898 * hibernation and system behavior will be unpredictable in this case.
1899 * So, let's prohibit device's probing here and defer their probes
1900 * instead. The normal behavior will be restored in dpm_complete().
1901 */
1902 device_block_probing();
1903
1904 mutex_lock(&dpm_list_mtx);
1905 while (!list_empty(&dpm_list) && !error) {
1906 struct device *dev = to_device(dpm_list.next);
1907
1908 get_device(dev);
1909
1910 mutex_unlock(&dpm_list_mtx);
1911
1912 trace_device_pm_callback_start(dev, "", state.event);
1913 error = device_prepare(dev, state);
1914 trace_device_pm_callback_end(dev, error);
1915
1916 mutex_lock(&dpm_list_mtx);
1917
1918 if (!error) {
1919 dev->power.is_prepared = true;
1920 if (!list_empty(&dev->power.entry))
1921 list_move_tail(&dev->power.entry, &dpm_prepared_list);
1922 } else if (error == -EAGAIN) {
1923 error = 0;
1924 } else {
1925 dev_info(dev, "not prepared for power transition: code %d\n",
1926 error);
1927 log_suspend_abort_reason("Device %s not prepared for power transition: code %d",
1928 dev_name(dev), error);
1929 dpm_save_failed_dev(dev_name(dev));
1930 }
1931
1932 mutex_unlock(&dpm_list_mtx);
1933
1934 put_device(dev);
1935
1936 mutex_lock(&dpm_list_mtx);
1937 }
1938 mutex_unlock(&dpm_list_mtx);
1939 trace_suspend_resume(TPS("dpm_prepare"), state.event, false);
1940 return error;
1941 }
1942
1943 /**
1944 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1945 * @state: PM transition of the system being carried out.
1946 *
1947 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1948 * callbacks for them.
1949 */
dpm_suspend_start(pm_message_t state)1950 int dpm_suspend_start(pm_message_t state)
1951 {
1952 ktime_t starttime = ktime_get();
1953 int error;
1954
1955 error = dpm_prepare(state);
1956 if (error) {
1957 suspend_stats.failed_prepare++;
1958 dpm_save_failed_step(SUSPEND_PREPARE);
1959 } else
1960 error = dpm_suspend(state);
1961 dpm_show_time(starttime, state, error, "start");
1962 return error;
1963 }
1964 EXPORT_SYMBOL_GPL(dpm_suspend_start);
1965
__suspend_report_result(const char * function,void * fn,int ret)1966 void __suspend_report_result(const char *function, void *fn, int ret)
1967 {
1968 if (ret)
1969 pr_err("%s(): %pS returns %d\n", function, fn, ret);
1970 }
1971 EXPORT_SYMBOL_GPL(__suspend_report_result);
1972
1973 /**
1974 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1975 * @subordinate: Device that needs to wait for @dev.
1976 * @dev: Device to wait for.
1977 */
device_pm_wait_for_dev(struct device * subordinate,struct device * dev)1978 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1979 {
1980 dpm_wait(dev, subordinate->power.async_suspend);
1981 return async_error;
1982 }
1983 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);
1984
1985 /**
1986 * dpm_for_each_dev - device iterator.
1987 * @data: data for the callback.
1988 * @fn: function to be called for each device.
1989 *
1990 * Iterate over devices in dpm_list, and call @fn for each device,
1991 * passing it @data.
1992 */
dpm_for_each_dev(void * data,void (* fn)(struct device *,void *))1993 void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *))
1994 {
1995 struct device *dev;
1996
1997 if (!fn)
1998 return;
1999
2000 device_pm_lock();
2001 list_for_each_entry(dev, &dpm_list, power.entry)
2002 fn(dev, data);
2003 device_pm_unlock();
2004 }
2005 EXPORT_SYMBOL_GPL(dpm_for_each_dev);
2006
pm_ops_is_empty(const struct dev_pm_ops * ops)2007 static bool pm_ops_is_empty(const struct dev_pm_ops *ops)
2008 {
2009 if (!ops)
2010 return true;
2011
2012 return !ops->prepare &&
2013 !ops->suspend &&
2014 !ops->suspend_late &&
2015 !ops->suspend_noirq &&
2016 !ops->resume_noirq &&
2017 !ops->resume_early &&
2018 !ops->resume &&
2019 !ops->complete;
2020 }
2021
device_pm_check_callbacks(struct device * dev)2022 void device_pm_check_callbacks(struct device *dev)
2023 {
2024 unsigned long flags;
2025
2026 spin_lock_irqsave(&dev->power.lock, flags);
2027 dev->power.no_pm_callbacks =
2028 (!dev->bus || (pm_ops_is_empty(dev->bus->pm) &&
2029 !dev->bus->suspend && !dev->bus->resume)) &&
2030 (!dev->class || pm_ops_is_empty(dev->class->pm)) &&
2031 (!dev->type || pm_ops_is_empty(dev->type->pm)) &&
2032 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) &&
2033 (!dev->driver || (pm_ops_is_empty(dev->driver->pm) &&
2034 !dev->driver->suspend && !dev->driver->resume));
2035 spin_unlock_irqrestore(&dev->power.lock, flags);
2036 }
2037
dev_pm_skip_suspend(struct device * dev)2038 bool dev_pm_skip_suspend(struct device *dev)
2039 {
2040 return dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) &&
2041 pm_runtime_status_suspended(dev);
2042 }
2043