main.c 32.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10
/*
 * drivers/base/power/main.c - Where the driver meets power management.
 *
 * Copyright (c) 2003 Patrick Mochel
 * Copyright (c) 2003 Open Source Development Lab
 *
 * This file is released under the GPLv2
 *
 *
 * 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
L
Linus Torvalds 已提交
12 13 14
 * and add it to the list of power-controlled devices. sysfs entries for
 * controlling device power management will also be added.
 *
15 16 17
 * A separate list is used for keeping track of power info, because the power
 * domain dependencies may differ from the ancestral dependencies that the
 * subsystem list maintains.
L
Linus Torvalds 已提交
18 19 20
 */

#include <linux/device.h>
21
#include <linux/kallsyms.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/resume-trace.h>
27
#include <linux/interrupt.h>
28
#include <linux/sched.h>
29
#include <linux/async.h>
30
#include <linux/suspend.h>
31

32
#include "../base.h"
L
Linus Torvalds 已提交
33 34
#include "power.h"

35 36
typedef int (*pm_callback_t)(struct device *);

37
/*
38
 * The entries in the dpm_list list are in a depth first order, simply
39 40 41
 * because children are guaranteed to be discovered after parents, and
 * are inserted at the back of the list on discovery.
 *
42 43
 * Since device_pm_add() may be called with a device lock held,
 * we must never try to acquire a device lock while holding
44 45 46
 * dpm_list_mutex.
 */

47
LIST_HEAD(dpm_list);
48 49 50 51
static LIST_HEAD(dpm_prepared_list);
static LIST_HEAD(dpm_suspended_list);
static LIST_HEAD(dpm_late_early_list);
static LIST_HEAD(dpm_noirq_list);
L
Linus Torvalds 已提交
52

53
struct suspend_stats suspend_stats;
54
static DEFINE_MUTEX(dpm_list_mtx);
55
static pm_message_t pm_transition;
L
Linus Torvalds 已提交
56

57 58
static int async_error;

59
/**
60
 * device_pm_init - Initialize the PM-related part of a device object.
61 62 63 64
 * @dev: Device object being initialized.
 */
void device_pm_init(struct device *dev)
{
65
	dev->power.is_prepared = false;
66
	dev->power.is_suspended = false;
67
	init_completion(&dev->power.completion);
68
	complete_all(&dev->power.completion);
69 70
	dev->power.wakeup = NULL;
	spin_lock_init(&dev->power.lock);
71
	pm_runtime_init(dev);
72
	INIT_LIST_HEAD(&dev->power.entry);
73
	dev->power.power_state = PMSG_INVALID;
74 75
}

76
/**
77
 * device_pm_lock - Lock the list of active devices used by the PM core.
78 79 80 81 82 83 84
 */
void device_pm_lock(void)
{
	mutex_lock(&dpm_list_mtx);
}

/**
85
 * device_pm_unlock - Unlock the list of active devices used by the PM core.
86 87 88 89 90
 */
void device_pm_unlock(void)
{
	mutex_unlock(&dpm_list_mtx);
}
91

92
/**
93 94
 * device_pm_add - Add a device to the PM core's list of active devices.
 * @dev: Device to add to the list.
95
 */
96
void device_pm_add(struct device *dev)
L
Linus Torvalds 已提交
97 98
{
	pr_debug("PM: Adding info for %s:%s\n",
99
		 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
100
	mutex_lock(&dpm_list_mtx);
101
	if (dev->parent && dev->parent->power.is_prepared)
102 103
		dev_warn(dev, "parent %s should not be sleeping\n",
			dev_name(dev->parent));
104
	list_add_tail(&dev->power.entry, &dpm_list);
105
	dev_pm_qos_constraints_init(dev);
106
	mutex_unlock(&dpm_list_mtx);
L
Linus Torvalds 已提交
107 108
}

109
/**
110 111
 * device_pm_remove - Remove a device from the PM core's list of active devices.
 * @dev: Device to be removed from the list.
112
 */
113
void device_pm_remove(struct device *dev)
L
Linus Torvalds 已提交
114 115
{
	pr_debug("PM: Removing info for %s:%s\n",
116
		 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
117
	complete_all(&dev->power.completion);
118
	mutex_lock(&dpm_list_mtx);
119
	dev_pm_qos_constraints_destroy(dev);
L
Linus Torvalds 已提交
120
	list_del_init(&dev->power.entry);
121
	mutex_unlock(&dpm_list_mtx);
122
	device_wakeup_disable(dev);
123
	pm_runtime_remove(dev);
124 125
}

126
/**
127 128 129
 * device_pm_move_before - Move device in the PM core's list of active devices.
 * @deva: Device to move in dpm_list.
 * @devb: Device @deva should come before.
130 131 132 133
 */
void device_pm_move_before(struct device *deva, struct device *devb)
{
	pr_debug("PM: Moving %s:%s before %s:%s\n",
134 135
		 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
		 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
136 137 138 139 140
	/* Delete deva from dpm_list and reinsert before devb. */
	list_move_tail(&deva->power.entry, &devb->power.entry);
}

/**
141 142 143
 * device_pm_move_after - Move device in the PM core's list of active devices.
 * @deva: Device to move in dpm_list.
 * @devb: Device @deva should come after.
144 145 146 147
 */
void device_pm_move_after(struct device *deva, struct device *devb)
{
	pr_debug("PM: Moving %s:%s after %s:%s\n",
148 149
		 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
		 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
150 151 152 153 154
	/* Delete deva from dpm_list and reinsert after devb. */
	list_move(&deva->power.entry, &devb->power.entry);
}

/**
155 156
 * device_pm_move_last - Move device to end of the PM core's list of devices.
 * @dev: Device to move in dpm_list.
157 158 159 160
 */
void device_pm_move_last(struct device *dev)
{
	pr_debug("PM: Moving %s:%s to end of list\n",
161
		 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
162 163 164
	list_move_tail(&dev->power.entry, &dpm_list);
}

165 166 167 168
static ktime_t initcall_debug_start(struct device *dev)
{
	ktime_t calltime = ktime_set(0, 0);

169
	if (pm_print_times_enabled) {
170 171 172
		pr_info("calling  %s+ @ %i, parent: %s\n",
			dev_name(dev), task_pid_nr(current),
			dev->parent ? dev_name(dev->parent) : "none");
173 174 175 176 177 178 179 180 181 182 183
		calltime = ktime_get();
	}

	return calltime;
}

static void initcall_debug_report(struct device *dev, ktime_t calltime,
				  int error)
{
	ktime_t delta, rettime;

184
	if (pm_print_times_enabled) {
185 186 187 188 189 190 191
		rettime = ktime_get();
		delta = ktime_sub(rettime, calltime);
		pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev),
			error, (unsigned long long)ktime_to_ns(delta) >> 10);
	}
}

192 193 194 195 196 197 198 199 200 201
/**
 * dpm_wait - Wait for a PM operation to complete.
 * @dev: Device to wait for.
 * @async: If unset, wait only if the device's power.async_suspend flag is set.
 */
static void dpm_wait(struct device *dev, bool async)
{
	if (!dev)
		return;

202
	if (async || (pm_async_enabled && dev->power.async_suspend))
203 204 205 206 207 208 209 210 211 212 213 214 215 216
		wait_for_completion(&dev->power.completion);
}

static int dpm_wait_fn(struct device *dev, void *async_ptr)
{
	dpm_wait(dev, *((bool *)async_ptr));
	return 0;
}

static void dpm_wait_for_children(struct device *dev, bool async)
{
       device_for_each_child(dev, &async, dpm_wait_fn);
}

217
/**
218
 * pm_op - Return the PM operation appropriate for given PM event.
219 220
 * @ops: PM operations to choose from.
 * @state: PM transition of the system being carried out.
221
 */
222
static pm_callback_t pm_op(const struct dev_pm_ops *ops, pm_message_t state)
223 224 225 226
{
	switch (state.event) {
#ifdef CONFIG_SUSPEND
	case PM_EVENT_SUSPEND:
227
		return ops->suspend;
228
	case PM_EVENT_RESUME:
229
		return ops->resume;
230
#endif /* CONFIG_SUSPEND */
231
#ifdef CONFIG_HIBERNATE_CALLBACKS
232 233
	case PM_EVENT_FREEZE:
	case PM_EVENT_QUIESCE:
234
		return ops->freeze;
235
	case PM_EVENT_HIBERNATE:
236
		return ops->poweroff;
237 238
	case PM_EVENT_THAW:
	case PM_EVENT_RECOVER:
239
		return ops->thaw;
240 241
		break;
	case PM_EVENT_RESTORE:
242
		return ops->restore;
243
#endif /* CONFIG_HIBERNATE_CALLBACKS */
244
	}
245

246
	return NULL;
247 248
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
/**
 * pm_late_early_op - Return the PM operation appropriate for given PM event.
 * @ops: PM operations to choose from.
 * @state: PM transition of the system being carried out.
 *
 * Runtime PM is disabled for @dev while this function is being executed.
 */
static pm_callback_t pm_late_early_op(const struct dev_pm_ops *ops,
				      pm_message_t state)
{
	switch (state.event) {
#ifdef CONFIG_SUSPEND
	case PM_EVENT_SUSPEND:
		return ops->suspend_late;
	case PM_EVENT_RESUME:
		return ops->resume_early;
#endif /* CONFIG_SUSPEND */
#ifdef CONFIG_HIBERNATE_CALLBACKS
	case PM_EVENT_FREEZE:
	case PM_EVENT_QUIESCE:
		return ops->freeze_late;
	case PM_EVENT_HIBERNATE:
		return ops->poweroff_late;
	case PM_EVENT_THAW:
	case PM_EVENT_RECOVER:
		return ops->thaw_early;
	case PM_EVENT_RESTORE:
		return ops->restore_early;
#endif /* CONFIG_HIBERNATE_CALLBACKS */
	}

	return NULL;
}

283
/**
284
 * pm_noirq_op - Return the PM operation appropriate for given PM event.
285 286
 * @ops: PM operations to choose from.
 * @state: PM transition of the system being carried out.
287
 *
288 289
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
290
 */
291
static pm_callback_t pm_noirq_op(const struct dev_pm_ops *ops, pm_message_t state)
292 293 294 295
{
	switch (state.event) {
#ifdef CONFIG_SUSPEND
	case PM_EVENT_SUSPEND:
296
		return ops->suspend_noirq;
297
	case PM_EVENT_RESUME:
298
		return ops->resume_noirq;
299
#endif /* CONFIG_SUSPEND */
300
#ifdef CONFIG_HIBERNATE_CALLBACKS
301 302
	case PM_EVENT_FREEZE:
	case PM_EVENT_QUIESCE:
303
		return ops->freeze_noirq;
304
	case PM_EVENT_HIBERNATE:
305
		return ops->poweroff_noirq;
306 307
	case PM_EVENT_THAW:
	case PM_EVENT_RECOVER:
308
		return ops->thaw_noirq;
309
	case PM_EVENT_RESTORE:
310
		return ops->restore_noirq;
311
#endif /* CONFIG_HIBERNATE_CALLBACKS */
312
	}
313

314
	return NULL;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
}

static char *pm_verb(int event)
{
	switch (event) {
	case PM_EVENT_SUSPEND:
		return "suspend";
	case PM_EVENT_RESUME:
		return "resume";
	case PM_EVENT_FREEZE:
		return "freeze";
	case PM_EVENT_QUIESCE:
		return "quiesce";
	case PM_EVENT_HIBERNATE:
		return "hibernate";
	case PM_EVENT_THAW:
		return "thaw";
	case PM_EVENT_RESTORE:
		return "restore";
	case PM_EVENT_RECOVER:
		return "recover";
	default:
		return "(unknown PM event)";
	}
}

static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
{
	dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
		((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
		", may wakeup" : "");
}

static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
			int error)
{
	printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
352
		dev_name(dev), pm_verb(state.event), info, error);
353 354
}

355 356 357
static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
{
	ktime_t calltime;
358
	u64 usecs64;
359 360 361 362 363 364 365 366 367 368 369 370 371
	int usecs;

	calltime = ktime_get();
	usecs64 = ktime_to_ns(ktime_sub(calltime, starttime));
	do_div(usecs64, NSEC_PER_USEC);
	usecs = usecs64;
	if (usecs == 0)
		usecs = 1;
	pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
		info ?: "", info ? " " : "", pm_verb(state.event),
		usecs / USEC_PER_MSEC, usecs % USEC_PER_MSEC);
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
static int dpm_run_callback(pm_callback_t cb, struct device *dev,
			    pm_message_t state, char *info)
{
	ktime_t calltime;
	int error;

	if (!cb)
		return 0;

	calltime = initcall_debug_start(dev);

	pm_dev_dbg(dev, state, info);
	error = cb(dev);
	suspend_report_result(cb, error);

	initcall_debug_report(dev, calltime, error);

	return error;
}

392 393 394
/*------------------------- Resume routines -------------------------*/

/**
395 396 397
 * device_resume_noirq - Execute an "early resume" callback for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
398
 *
399 400
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
401
 */
402
static int device_resume_noirq(struct device *dev, pm_message_t state)
403
{
404 405
	pm_callback_t callback = NULL;
	char *info = NULL;
406 407 408 409 410
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);

411
	if (dev->pm_domain) {
412
		info = "noirq power domain ";
413
		callback = pm_noirq_op(&dev->pm_domain->ops, state);
414
	} else if (dev->type && dev->type->pm) {
415
		info = "noirq type ";
416
		callback = pm_noirq_op(dev->type->pm, state);
417
	} else if (dev->class && dev->class->pm) {
418
		info = "noirq class ";
419
		callback = pm_noirq_op(dev->class->pm, state);
420
	} else if (dev->bus && dev->bus->pm) {
421
		info = "noirq bus ";
422
		callback = pm_noirq_op(dev->bus->pm, state);
423 424
	}

425
	if (!callback && dev->driver && dev->driver->pm) {
426
		info = "noirq driver ";
427 428 429
		callback = pm_noirq_op(dev->driver->pm, state);
	}

430 431
	error = dpm_run_callback(callback, dev, state, info);

432 433 434 435 436
	TRACE_RESUME(error);
	return error;
}

/**
437
 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
438
 * @state: PM transition of the system being carried out.
439
 *
440
 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
441
 * enable device drivers to receive interrupts.
442
 */
443
static void dpm_resume_noirq(pm_message_t state)
444
{
445
	ktime_t starttime = ktime_get();
446

447
	mutex_lock(&dpm_list_mtx);
448 449
	while (!list_empty(&dpm_noirq_list)) {
		struct device *dev = to_device(dpm_noirq_list.next);
450
		int error;
451 452

		get_device(dev);
453
		list_move_tail(&dev->power.entry, &dpm_late_early_list);
454
		mutex_unlock(&dpm_list_mtx);
455

456
		error = device_resume_noirq(dev, state);
457 458 459 460
		if (error) {
			suspend_stats.failed_resume_noirq++;
			dpm_save_failed_step(SUSPEND_RESUME_NOIRQ);
			dpm_save_failed_dev(dev_name(dev));
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
			pm_dev_err(dev, state, " noirq", error);
		}

		mutex_lock(&dpm_list_mtx);
		put_device(dev);
	}
	mutex_unlock(&dpm_list_mtx);
	dpm_show_time(starttime, state, "noirq");
	resume_device_irqs();
}

/**
 * device_resume_early - Execute an "early resume" callback for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
 *
 * Runtime PM is disabled for @dev while this function is being executed.
 */
static int device_resume_early(struct device *dev, pm_message_t state)
{
	pm_callback_t callback = NULL;
	char *info = NULL;
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);

	if (dev->pm_domain) {
		info = "early power domain ";
		callback = pm_late_early_op(&dev->pm_domain->ops, state);
	} else if (dev->type && dev->type->pm) {
		info = "early type ";
		callback = pm_late_early_op(dev->type->pm, state);
	} else if (dev->class && dev->class->pm) {
		info = "early class ";
		callback = pm_late_early_op(dev->class->pm, state);
	} else if (dev->bus && dev->bus->pm) {
		info = "early bus ";
		callback = pm_late_early_op(dev->bus->pm, state);
	}

	if (!callback && dev->driver && dev->driver->pm) {
		info = "early driver ";
		callback = pm_late_early_op(dev->driver->pm, state);
	}

	error = dpm_run_callback(callback, dev, state, info);

	TRACE_RESUME(error);
	return error;
}

/**
 * dpm_resume_early - Execute "early resume" callbacks for all devices.
 * @state: PM transition of the system being carried out.
 */
static void dpm_resume_early(pm_message_t state)
{
	ktime_t starttime = ktime_get();

	mutex_lock(&dpm_list_mtx);
	while (!list_empty(&dpm_late_early_list)) {
		struct device *dev = to_device(dpm_late_early_list.next);
		int error;

		get_device(dev);
		list_move_tail(&dev->power.entry, &dpm_suspended_list);
		mutex_unlock(&dpm_list_mtx);

		error = device_resume_early(dev, state);
		if (error) {
			suspend_stats.failed_resume_early++;
			dpm_save_failed_step(SUSPEND_RESUME_EARLY);
			dpm_save_failed_dev(dev_name(dev));
535
			pm_dev_err(dev, state, " early", error);
536
		}
537

538
		mutex_lock(&dpm_list_mtx);
539 540
		put_device(dev);
	}
541
	mutex_unlock(&dpm_list_mtx);
542
	dpm_show_time(starttime, state, "early");
543
}
544 545 546 547 548 549 550 551 552 553 554

/**
 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
 * @state: PM transition of the system being carried out.
 */
void dpm_resume_start(pm_message_t state)
{
	dpm_resume_noirq(state);
	dpm_resume_early(state);
}
EXPORT_SYMBOL_GPL(dpm_resume_start);
555 556

/**
557
 * device_resume - Execute "resume" callbacks for given device.
558 559
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
560
 * @async: If true, the device is being resumed asynchronously.
561
 */
562
static int device_resume(struct device *dev, pm_message_t state, bool async)
563
{
564 565
	pm_callback_t callback = NULL;
	char *info = NULL;
566
	int error = 0;
567
	bool put = false;
568 569 570

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);
571

572
	dpm_wait(dev->parent, async);
573
	device_lock(dev);
574

575 576 577 578 579
	/*
	 * This is a fib.  But we'll allow new children to be added below
	 * a resumed device, even if the device hasn't been completed yet.
	 */
	dev->power.is_prepared = false;
580

581 582 583
	if (!dev->power.is_suspended)
		goto Unlock;

584 585 586
	pm_runtime_enable(dev);
	put = true;

587
	if (dev->pm_domain) {
588 589
		info = "power domain ";
		callback = pm_op(&dev->pm_domain->ops, state);
590
		goto Driver;
591 592
	}

593
	if (dev->type && dev->type->pm) {
594 595
		info = "type ";
		callback = pm_op(dev->type->pm, state);
596
		goto Driver;
597 598
	}

599 600
	if (dev->class) {
		if (dev->class->pm) {
601 602
			info = "class ";
			callback = pm_op(dev->class->pm, state);
603
			goto Driver;
604
		} else if (dev->class->resume) {
605 606
			info = "legacy class ";
			callback = dev->class->resume;
607
			goto End;
608
		}
609
	}
610 611 612

	if (dev->bus) {
		if (dev->bus->pm) {
613
			info = "bus ";
614
			callback = pm_op(dev->bus->pm, state);
615
		} else if (dev->bus->resume) {
616
			info = "legacy bus ";
617
			callback = dev->bus->resume;
618
			goto End;
619 620 621
		}
	}

622 623 624 625 626 627
 Driver:
	if (!callback && dev->driver && dev->driver->pm) {
		info = "driver ";
		callback = pm_op(dev->driver->pm, state);
	}

628
 End:
629
	error = dpm_run_callback(callback, dev, state, info);
630 631 632
	dev->power.is_suspended = false;

 Unlock:
633
	device_unlock(dev);
634
	complete_all(&dev->power.completion);
635

636
	TRACE_RESUME(error);
637 638 639 640

	if (put)
		pm_runtime_put_sync(dev);

641 642 643
	return error;
}

644 645 646 647 648
static void async_resume(void *data, async_cookie_t cookie)
{
	struct device *dev = (struct device *)data;
	int error;

649
	error = device_resume(dev, pm_transition, true);
650 651 652 653 654
	if (error)
		pm_dev_err(dev, pm_transition, " async", error);
	put_device(dev);
}

655
static bool is_async(struct device *dev)
656
{
657 658
	return dev->power.async_suspend && pm_async_enabled
		&& !pm_trace_is_enabled();
659 660
}

661
/**
662 663
 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
 * @state: PM transition of the system being carried out.
664
 *
665 666
 * Execute the appropriate "resume" callback for all devices whose status
 * indicates that they are suspended.
667
 */
668
void dpm_resume(pm_message_t state)
669
{
670
	struct device *dev;
671
	ktime_t starttime = ktime_get();
672

673 674
	might_sleep();

675
	mutex_lock(&dpm_list_mtx);
676
	pm_transition = state;
677
	async_error = 0;
678

679
	list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
680 681 682 683 684 685 686
		INIT_COMPLETION(dev->power.completion);
		if (is_async(dev)) {
			get_device(dev);
			async_schedule(async_resume, dev);
		}
	}

687 688
	while (!list_empty(&dpm_suspended_list)) {
		dev = to_device(dpm_suspended_list.next);
689
		get_device(dev);
690
		if (!is_async(dev)) {
691 692 693 694
			int error;

			mutex_unlock(&dpm_list_mtx);

695
			error = device_resume(dev, state, false);
696 697 698 699
			if (error) {
				suspend_stats.failed_resume++;
				dpm_save_failed_step(SUSPEND_RESUME);
				dpm_save_failed_dev(dev_name(dev));
700
				pm_dev_err(dev, state, "", error);
701
			}
702 703

			mutex_lock(&dpm_list_mtx);
704 705
		}
		if (!list_empty(&dev->power.entry))
706
			list_move_tail(&dev->power.entry, &dpm_prepared_list);
707 708 709
		put_device(dev);
	}
	mutex_unlock(&dpm_list_mtx);
710
	async_synchronize_full();
711
	dpm_show_time(starttime, state, NULL);
712 713 714
}

/**
715 716 717
 * device_complete - Complete a PM transition for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
718
 */
719
static void device_complete(struct device *dev, pm_message_t state)
720
{
721 722 723
	void (*callback)(struct device *) = NULL;
	char *info = NULL;

724
	device_lock(dev);
725

726
	if (dev->pm_domain) {
727 728
		info = "completing power domain ";
		callback = dev->pm_domain->ops.complete;
729
	} else if (dev->type && dev->type->pm) {
730 731
		info = "completing type ";
		callback = dev->type->pm->complete;
732
	} else if (dev->class && dev->class->pm) {
733 734
		info = "completing class ";
		callback = dev->class->pm->complete;
735
	} else if (dev->bus && dev->bus->pm) {
736 737 738 739 740 741 742 743 744 745 746 747
		info = "completing bus ";
		callback = dev->bus->pm->complete;
	}

	if (!callback && dev->driver && dev->driver->pm) {
		info = "completing driver ";
		callback = dev->driver->pm->complete;
	}

	if (callback) {
		pm_dev_dbg(dev, state, info);
		callback(dev);
748 749
	}

750
	device_unlock(dev);
751 752 753
}

/**
754 755
 * dpm_complete - Complete a PM transition for all non-sysdev devices.
 * @state: PM transition of the system being carried out.
756
 *
757 758
 * Execute the ->complete() callbacks for all devices whose PM status is not
 * DPM_ON (this allows new devices to be registered).
759
 */
760
void dpm_complete(pm_message_t state)
761
{
762 763
	struct list_head list;

764 765
	might_sleep();

766
	INIT_LIST_HEAD(&list);
767
	mutex_lock(&dpm_list_mtx);
768 769
	while (!list_empty(&dpm_prepared_list)) {
		struct device *dev = to_device(dpm_prepared_list.prev);
770

771
		get_device(dev);
772
		dev->power.is_prepared = false;
773 774
		list_move(&dev->power.entry, &list);
		mutex_unlock(&dpm_list_mtx);
775

776
		device_complete(dev, state);
777

778
		mutex_lock(&dpm_list_mtx);
779
		put_device(dev);
780
	}
781
	list_splice(&list, &dpm_list);
782 783 784 785
	mutex_unlock(&dpm_list_mtx);
}

/**
786 787
 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
 * @state: PM transition of the system being carried out.
788
 *
789 790
 * Execute "resume" callbacks for all devices and complete the PM transition of
 * the system.
791
 */
792
void dpm_resume_end(pm_message_t state)
793
{
794 795
	dpm_resume(state);
	dpm_complete(state);
796
}
797
EXPORT_SYMBOL_GPL(dpm_resume_end);
798 799 800 801


/*------------------------- Suspend routines -------------------------*/

802
/**
803 804 805 806 807
 * resume_event - Return a "resume" message for given "suspend" sleep state.
 * @sleep_state: PM message representing a sleep state.
 *
 * Return a PM message representing the resume event corresponding to given
 * sleep state.
808 809
 */
static pm_message_t resume_event(pm_message_t sleep_state)
810
{
811 812 813 814 815 816 817 818
	switch (sleep_state.event) {
	case PM_EVENT_SUSPEND:
		return PMSG_RESUME;
	case PM_EVENT_FREEZE:
	case PM_EVENT_QUIESCE:
		return PMSG_RECOVER;
	case PM_EVENT_HIBERNATE:
		return PMSG_RESTORE;
819
	}
820
	return PMSG_ON;
821 822 823
}

/**
824 825 826
 * device_suspend_noirq - Execute a "late suspend" callback for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
827
 *
828 829
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
830
 */
831
static int device_suspend_noirq(struct device *dev, pm_message_t state)
832
{
833 834
	pm_callback_t callback = NULL;
	char *info = NULL;
835

836
	if (dev->pm_domain) {
837
		info = "noirq power domain ";
838
		callback = pm_noirq_op(&dev->pm_domain->ops, state);
839
	} else if (dev->type && dev->type->pm) {
840
		info = "noirq type ";
841
		callback = pm_noirq_op(dev->type->pm, state);
842
	} else if (dev->class && dev->class->pm) {
843
		info = "noirq class ";
844
		callback = pm_noirq_op(dev->class->pm, state);
845
	} else if (dev->bus && dev->bus->pm) {
846
		info = "noirq bus ";
847
		callback = pm_noirq_op(dev->bus->pm, state);
848 849
	}

850
	if (!callback && dev->driver && dev->driver->pm) {
851
		info = "noirq driver ";
852 853 854
		callback = pm_noirq_op(dev->driver->pm, state);
	}

855
	return dpm_run_callback(callback, dev, state, info);
856 857 858
}

/**
859
 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
860
 * @state: PM transition of the system being carried out.
861
 *
862 863
 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
 * handlers for all non-sysdev devices.
864
 */
865
static int dpm_suspend_noirq(pm_message_t state)
866
{
867
	ktime_t starttime = ktime_get();
868 869
	int error = 0;

870
	suspend_device_irqs();
871
	mutex_lock(&dpm_list_mtx);
872 873
	while (!list_empty(&dpm_late_early_list)) {
		struct device *dev = to_device(dpm_late_early_list.prev);
874 875 876 877

		get_device(dev);
		mutex_unlock(&dpm_list_mtx);

878
		error = device_suspend_noirq(dev, state);
879 880

		mutex_lock(&dpm_list_mtx);
881
		if (error) {
882
			pm_dev_err(dev, state, " noirq", error);
883 884 885
			suspend_stats.failed_suspend_noirq++;
			dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ);
			dpm_save_failed_dev(dev_name(dev));
886
			put_device(dev);
887 888
			break;
		}
889
		if (!list_empty(&dev->power.entry))
890
			list_move(&dev->power.entry, &dpm_noirq_list);
891
		put_device(dev);
892 893 894 895 896

		if (pm_wakeup_pending()) {
			error = -EBUSY;
			break;
		}
897
	}
898
	mutex_unlock(&dpm_list_mtx);
899
	if (error)
900
		dpm_resume_noirq(resume_event(state));
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
	else
		dpm_show_time(starttime, state, "noirq");
	return error;
}

/**
 * device_suspend_late - Execute a "late suspend" callback for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
 *
 * Runtime PM is disabled for @dev while this function is being executed.
 */
static int device_suspend_late(struct device *dev, pm_message_t state)
{
	pm_callback_t callback = NULL;
	char *info = NULL;

	if (dev->pm_domain) {
		info = "late power domain ";
		callback = pm_late_early_op(&dev->pm_domain->ops, state);
	} else if (dev->type && dev->type->pm) {
		info = "late type ";
		callback = pm_late_early_op(dev->type->pm, state);
	} else if (dev->class && dev->class->pm) {
		info = "late class ";
		callback = pm_late_early_op(dev->class->pm, state);
	} else if (dev->bus && dev->bus->pm) {
		info = "late bus ";
		callback = pm_late_early_op(dev->bus->pm, state);
	}

	if (!callback && dev->driver && dev->driver->pm) {
		info = "late driver ";
		callback = pm_late_early_op(dev->driver->pm, state);
	}

	return dpm_run_callback(callback, dev, state, info);
}

/**
 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
 * @state: PM transition of the system being carried out.
 */
static int dpm_suspend_late(pm_message_t state)
{
	ktime_t starttime = ktime_get();
	int error = 0;

	mutex_lock(&dpm_list_mtx);
	while (!list_empty(&dpm_suspended_list)) {
		struct device *dev = to_device(dpm_suspended_list.prev);

		get_device(dev);
		mutex_unlock(&dpm_list_mtx);

		error = device_suspend_late(dev, state);

		mutex_lock(&dpm_list_mtx);
		if (error) {
			pm_dev_err(dev, state, " late", error);
			suspend_stats.failed_suspend_late++;
			dpm_save_failed_step(SUSPEND_SUSPEND_LATE);
			dpm_save_failed_dev(dev_name(dev));
			put_device(dev);
			break;
		}
		if (!list_empty(&dev->power.entry))
			list_move(&dev->power.entry, &dpm_late_early_list);
		put_device(dev);
970 971 972 973 974

		if (pm_wakeup_pending()) {
			error = -EBUSY;
			break;
		}
975 976 977 978
	}
	mutex_unlock(&dpm_list_mtx);
	if (error)
		dpm_resume_early(resume_event(state));
979 980
	else
		dpm_show_time(starttime, state, "late");
981

982 983
	return error;
}
984 985 986 987 988 989 990 991 992 993 994 995

/**
 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
 * @state: PM transition of the system being carried out.
 */
int dpm_suspend_end(pm_message_t state)
{
	int error = dpm_suspend_late(state);

	return error ? : dpm_suspend_noirq(state);
}
EXPORT_SYMBOL_GPL(dpm_suspend_end);
996

997 998
/**
 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
R
Randy Dunlap 已提交
999 1000 1001
 * @dev: Device to suspend.
 * @state: PM transition of the system being carried out.
 * @cb: Suspend callback to execute.
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
 */
static int legacy_suspend(struct device *dev, pm_message_t state,
			  int (*cb)(struct device *dev, pm_message_t state))
{
	int error;
	ktime_t calltime;

	calltime = initcall_debug_start(dev);

	error = cb(dev, state);
	suspend_report_result(cb, error);

	initcall_debug_report(dev, calltime, error);

	return error;
}

1019
/**
1020 1021 1022
 * device_suspend - Execute "suspend" callbacks for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
1023
 * @async: If true, the device is being suspended asynchronously.
1024
 */
1025
static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1026
{
1027 1028
	pm_callback_t callback = NULL;
	char *info = NULL;
1029 1030
	int error = 0;

1031
	dpm_wait_for_children(dev, async);
1032

1033
	if (async_error)
1034
		goto Complete;
1035 1036 1037 1038

	pm_runtime_get_noresume(dev);
	if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
		pm_wakeup_event(dev, 0);
1039

1040
	if (pm_wakeup_pending()) {
1041
		pm_runtime_put_sync(dev);
1042
		async_error = -EBUSY;
1043
		goto Complete;
1044 1045
	}

1046 1047
	device_lock(dev);

1048
	if (dev->pm_domain) {
1049 1050 1051
		info = "power domain ";
		callback = pm_op(&dev->pm_domain->ops, state);
		goto Run;
1052 1053
	}

1054
	if (dev->type && dev->type->pm) {
1055 1056 1057
		info = "type ";
		callback = pm_op(dev->type->pm, state);
		goto Run;
1058 1059
	}

1060 1061
	if (dev->class) {
		if (dev->class->pm) {
1062 1063 1064
			info = "class ";
			callback = pm_op(dev->class->pm, state);
			goto Run;
1065 1066
		} else if (dev->class->suspend) {
			pm_dev_dbg(dev, state, "legacy class ");
1067
			error = legacy_suspend(dev, state, dev->class->suspend);
1068
			goto End;
1069
		}
1070 1071
	}

1072 1073
	if (dev->bus) {
		if (dev->bus->pm) {
1074
			info = "bus ";
1075
			callback = pm_op(dev->bus->pm, state);
1076
		} else if (dev->bus->suspend) {
1077
			pm_dev_dbg(dev, state, "legacy bus ");
1078
			error = legacy_suspend(dev, state, dev->bus->suspend);
1079
			goto End;
1080
		}
1081 1082
	}

1083
 Run:
1084 1085 1086 1087 1088
	if (!callback && dev->driver && dev->driver->pm) {
		info = "driver ";
		callback = pm_op(dev->driver->pm, state);
	}

1089 1090
	error = dpm_run_callback(callback, dev, state, info);

1091
 End:
1092 1093
	if (!error) {
		dev->power.is_suspended = true;
1094 1095
		if (dev->power.wakeup_path
		    && dev->parent && !dev->parent->power.ignore_children)
1096 1097
			dev->parent->power.wakeup_path = true;
	}
1098

1099
	device_unlock(dev);
1100 1101

 Complete:
1102
	complete_all(&dev->power.completion);
1103

1104 1105
	if (error) {
		pm_runtime_put_sync(dev);
1106
		async_error = error;
1107 1108 1109
	} else if (dev->power.is_suspended) {
		__pm_runtime_disable(dev, false);
	}
1110

1111 1112 1113
	return error;
}

1114 1115 1116 1117 1118 1119
static void async_suspend(void *data, async_cookie_t cookie)
{
	struct device *dev = (struct device *)data;
	int error;

	error = __device_suspend(dev, pm_transition, true);
1120 1121
	if (error) {
		dpm_save_failed_dev(dev_name(dev));
1122
		pm_dev_err(dev, pm_transition, " async", error);
1123
	}
1124 1125 1126 1127 1128 1129 1130 1131

	put_device(dev);
}

static int device_suspend(struct device *dev)
{
	INIT_COMPLETION(dev->power.completion);

1132
	if (pm_async_enabled && dev->power.async_suspend) {
1133 1134 1135 1136 1137 1138 1139 1140
		get_device(dev);
		async_schedule(async_suspend, dev);
		return 0;
	}

	return __device_suspend(dev, pm_transition, false);
}

1141
/**
1142 1143
 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
 * @state: PM transition of the system being carried out.
1144
 */
1145
int dpm_suspend(pm_message_t state)
1146
{
1147
	ktime_t starttime = ktime_get();
1148 1149
	int error = 0;

1150 1151
	might_sleep();

1152
	mutex_lock(&dpm_list_mtx);
1153 1154
	pm_transition = state;
	async_error = 0;
1155 1156
	while (!list_empty(&dpm_prepared_list)) {
		struct device *dev = to_device(dpm_prepared_list.prev);
1157

1158
		get_device(dev);
1159
		mutex_unlock(&dpm_list_mtx);
1160

1161
		error = device_suspend(dev);
1162

1163
		mutex_lock(&dpm_list_mtx);
1164
		if (error) {
1165
			pm_dev_err(dev, state, "", error);
1166
			dpm_save_failed_dev(dev_name(dev));
1167
			put_device(dev);
1168 1169
			break;
		}
1170
		if (!list_empty(&dev->power.entry))
1171
			list_move(&dev->power.entry, &dpm_suspended_list);
1172
		put_device(dev);
1173 1174
		if (async_error)
			break;
1175 1176
	}
	mutex_unlock(&dpm_list_mtx);
1177 1178 1179
	async_synchronize_full();
	if (!error)
		error = async_error;
1180 1181 1182 1183
	if (error) {
		suspend_stats.failed_suspend++;
		dpm_save_failed_step(SUSPEND_SUSPEND);
	} else
1184
		dpm_show_time(starttime, state, NULL);
1185 1186 1187 1188
	return error;
}

/**
1189 1190 1191 1192 1193 1194
 * device_prepare - Prepare a device for system power transition.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
 *
 * Execute the ->prepare() callback(s) for given device.  No new children of the
 * device may be registered after this function has returned.
1195
 */
1196
static int device_prepare(struct device *dev, pm_message_t state)
1197
{
1198 1199
	int (*callback)(struct device *) = NULL;
	char *info = NULL;
1200 1201
	int error = 0;

1202
	device_lock(dev);
1203

1204 1205
	dev->power.wakeup_path = device_may_wakeup(dev);

1206
	if (dev->pm_domain) {
1207 1208
		info = "preparing power domain ";
		callback = dev->pm_domain->ops.prepare;
1209
	} else if (dev->type && dev->type->pm) {
1210 1211
		info = "preparing type ";
		callback = dev->type->pm->prepare;
1212
	} else if (dev->class && dev->class->pm) {
1213 1214
		info = "preparing class ";
		callback = dev->class->pm->prepare;
1215
	} else if (dev->bus && dev->bus->pm) {
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
		info = "preparing bus ";
		callback = dev->bus->pm->prepare;
	}

	if (!callback && dev->driver && dev->driver->pm) {
		info = "preparing driver ";
		callback = dev->driver->pm->prepare;
	}

	if (callback) {
		error = callback(dev);
		suspend_report_result(callback, error);
1228
	}
1229

1230
	device_unlock(dev);
1231 1232 1233

	return error;
}
1234

1235
/**
1236 1237
 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
 * @state: PM transition of the system being carried out.
1238
 *
1239
 * Execute the ->prepare() callback(s) for all devices.
1240
 */
1241
int dpm_prepare(pm_message_t state)
1242 1243 1244
{
	int error = 0;

1245 1246
	might_sleep();

1247 1248 1249 1250 1251 1252 1253
	mutex_lock(&dpm_list_mtx);
	while (!list_empty(&dpm_list)) {
		struct device *dev = to_device(dpm_list.next);

		get_device(dev);
		mutex_unlock(&dpm_list_mtx);

1254
		error = device_prepare(dev, state);
1255 1256 1257 1258 1259

		mutex_lock(&dpm_list_mtx);
		if (error) {
			if (error == -EAGAIN) {
				put_device(dev);
S
Sebastian Ott 已提交
1260
				error = 0;
1261 1262
				continue;
			}
1263 1264
			printk(KERN_INFO "PM: Device %s not prepared "
				"for power transition: code %d\n",
1265
				dev_name(dev), error);
1266 1267 1268
			put_device(dev);
			break;
		}
1269
		dev->power.is_prepared = true;
1270
		if (!list_empty(&dev->power.entry))
1271
			list_move_tail(&dev->power.entry, &dpm_prepared_list);
1272 1273 1274
		put_device(dev);
	}
	mutex_unlock(&dpm_list_mtx);
1275 1276 1277
	return error;
}

1278
/**
1279 1280
 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
 * @state: PM transition of the system being carried out.
1281
 *
1282 1283
 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
 * callbacks for them.
1284
 */
1285
int dpm_suspend_start(pm_message_t state)
1286 1287
{
	int error;
1288

1289
	error = dpm_prepare(state);
1290 1291 1292 1293
	if (error) {
		suspend_stats.failed_prepare++;
		dpm_save_failed_step(SUSPEND_PREPARE);
	} else
1294
		error = dpm_suspend(state);
1295 1296
	return error;
}
1297
EXPORT_SYMBOL_GPL(dpm_suspend_start);
1298 1299 1300

void __suspend_report_result(const char *function, void *fn, int ret)
{
1301 1302
	if (ret)
		printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1303 1304
}
EXPORT_SYMBOL_GPL(__suspend_report_result);
1305 1306 1307 1308 1309 1310

/**
 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
 * @dev: Device to wait for.
 * @subordinate: Device that needs to wait for @dev.
 */
1311
int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1312 1313
{
	dpm_wait(dev, subordinate->power.async_suspend);
1314
	return async_error;
1315 1316
}
EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);