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