Linux Audio

Check our new training course

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