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