main.c 32.4 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
#include <linux/cpuidle.h>
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
			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();
470
	cpuidle_resume();
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 535
}

/**
 * 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));
536
			pm_dev_err(dev, state, " early", error);
537
		}
538

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

/**
 * 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);
556 557

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

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);
572

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

576 577 578 579 580
	/*
	 * 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;
581

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

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

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

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

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

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

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

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

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

637
	TRACE_RESUME(error);
638 639 640 641

	if (put)
		pm_runtime_put_sync(dev);

642 643 644
	return error;
}

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

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

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

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

674 675
	might_sleep();

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

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

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

			mutex_unlock(&dpm_list_mtx);

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

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

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

725
	device_lock(dev);
726

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

751
	device_unlock(dev);
752 753 754
}

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

765 766
	might_sleep();

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

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

777
		device_complete(dev, state);
778

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

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


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

803
/**
804 805 806 807 808
 * 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.
809 810
 */
static pm_message_t resume_event(pm_message_t sleep_state)
811
{
812 813 814 815 816 817 818 819
	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;
820
	}
821
	return PMSG_ON;
822 823 824
}

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

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

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

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

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

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

		get_device(dev);
		mutex_unlock(&dpm_list_mtx);

880
		error = device_suspend_noirq(dev, state);
881 882

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

		if (pm_wakeup_pending()) {
			error = -EBUSY;
			break;
		}
899
	}
900
	mutex_unlock(&dpm_list_mtx);
901
	if (error)
902
		dpm_resume_noirq(resume_event(state));
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 970 971
	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);
972 973 974 975 976

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

984 985
	return error;
}
986 987 988 989 990 991 992 993

/**
 * 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);
994 995 996 997 998
	if (error)
		return error;

	error = dpm_suspend_noirq(state);
	if (error) {
999
		dpm_resume_early(resume_event(state));
1000 1001
		return error;
	}
1002

1003
	return 0;
1004 1005
}
EXPORT_SYMBOL_GPL(dpm_suspend_end);
1006

1007 1008
/**
 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
R
Randy Dunlap 已提交
1009 1010 1011
 * @dev: Device to suspend.
 * @state: PM transition of the system being carried out.
 * @cb: Suspend callback to execute.
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
 */
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;
}

1029
/**
1030 1031 1032
 * device_suspend - Execute "suspend" callbacks for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
1033
 * @async: If true, the device is being suspended asynchronously.
1034
 */
1035
static int __device_suspend(struct device *dev, pm_message_t state, bool async)
1036
{
1037 1038
	pm_callback_t callback = NULL;
	char *info = NULL;
1039 1040
	int error = 0;

1041
	dpm_wait_for_children(dev, async);
1042

1043
	if (async_error)
1044
		goto Complete;
1045 1046 1047 1048

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

1050
	if (pm_wakeup_pending()) {
1051
		pm_runtime_put_sync(dev);
1052
		async_error = -EBUSY;
1053
		goto Complete;
1054 1055
	}

1056 1057
	device_lock(dev);

1058
	if (dev->pm_domain) {
1059 1060 1061
		info = "power domain ";
		callback = pm_op(&dev->pm_domain->ops, state);
		goto Run;
1062 1063
	}

1064
	if (dev->type && dev->type->pm) {
1065 1066 1067
		info = "type ";
		callback = pm_op(dev->type->pm, state);
		goto Run;
1068 1069
	}

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

1082 1083
	if (dev->bus) {
		if (dev->bus->pm) {
1084
			info = "bus ";
1085
			callback = pm_op(dev->bus->pm, state);
1086
		} else if (dev->bus->suspend) {
1087
			pm_dev_dbg(dev, state, "legacy bus ");
1088
			error = legacy_suspend(dev, state, dev->bus->suspend);
1089
			goto End;
1090
		}
1091 1092
	}

1093
 Run:
1094 1095 1096 1097 1098
	if (!callback && dev->driver && dev->driver->pm) {
		info = "driver ";
		callback = pm_op(dev->driver->pm, state);
	}

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

1101
 End:
1102 1103
	if (!error) {
		dev->power.is_suspended = true;
1104 1105
		if (dev->power.wakeup_path
		    && dev->parent && !dev->parent->power.ignore_children)
1106 1107
			dev->parent->power.wakeup_path = true;
	}
1108

1109
	device_unlock(dev);
1110 1111

 Complete:
1112
	complete_all(&dev->power.completion);
1113

1114 1115
	if (error) {
		pm_runtime_put_sync(dev);
1116
		async_error = error;
1117 1118 1119
	} else if (dev->power.is_suspended) {
		__pm_runtime_disable(dev, false);
	}
1120

1121 1122 1123
	return error;
}

1124 1125 1126 1127 1128 1129
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);
1130 1131
	if (error) {
		dpm_save_failed_dev(dev_name(dev));
1132
		pm_dev_err(dev, pm_transition, " async", error);
1133
	}
1134 1135 1136 1137 1138 1139 1140 1141

	put_device(dev);
}

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

1142
	if (pm_async_enabled && dev->power.async_suspend) {
1143 1144 1145 1146 1147 1148 1149 1150
		get_device(dev);
		async_schedule(async_suspend, dev);
		return 0;
	}

	return __device_suspend(dev, pm_transition, false);
}

1151
/**
1152 1153
 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
 * @state: PM transition of the system being carried out.
1154
 */
1155
int dpm_suspend(pm_message_t state)
1156
{
1157
	ktime_t starttime = ktime_get();
1158 1159
	int error = 0;

1160 1161
	might_sleep();

1162
	mutex_lock(&dpm_list_mtx);
1163 1164
	pm_transition = state;
	async_error = 0;
1165 1166
	while (!list_empty(&dpm_prepared_list)) {
		struct device *dev = to_device(dpm_prepared_list.prev);
1167

1168
		get_device(dev);
1169
		mutex_unlock(&dpm_list_mtx);
1170

1171
		error = device_suspend(dev);
1172

1173
		mutex_lock(&dpm_list_mtx);
1174
		if (error) {
1175
			pm_dev_err(dev, state, "", error);
1176
			dpm_save_failed_dev(dev_name(dev));
1177
			put_device(dev);
1178 1179
			break;
		}
1180
		if (!list_empty(&dev->power.entry))
1181
			list_move(&dev->power.entry, &dpm_suspended_list);
1182
		put_device(dev);
1183 1184
		if (async_error)
			break;
1185 1186
	}
	mutex_unlock(&dpm_list_mtx);
1187 1188 1189
	async_synchronize_full();
	if (!error)
		error = async_error;
1190 1191 1192 1193
	if (error) {
		suspend_stats.failed_suspend++;
		dpm_save_failed_step(SUSPEND_SUSPEND);
	} else
1194
		dpm_show_time(starttime, state, NULL);
1195 1196 1197 1198
	return error;
}

/**
1199 1200 1201 1202 1203 1204
 * 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.
1205
 */
1206
static int device_prepare(struct device *dev, pm_message_t state)
1207
{
1208 1209
	int (*callback)(struct device *) = NULL;
	char *info = NULL;
1210 1211
	int error = 0;

1212
	device_lock(dev);
1213

1214 1215
	dev->power.wakeup_path = device_may_wakeup(dev);

1216
	if (dev->pm_domain) {
1217 1218
		info = "preparing power domain ";
		callback = dev->pm_domain->ops.prepare;
1219
	} else if (dev->type && dev->type->pm) {
1220 1221
		info = "preparing type ";
		callback = dev->type->pm->prepare;
1222
	} else if (dev->class && dev->class->pm) {
1223 1224
		info = "preparing class ";
		callback = dev->class->pm->prepare;
1225
	} else if (dev->bus && dev->bus->pm) {
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
		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);
1238
	}
1239

1240
	device_unlock(dev);
1241 1242 1243

	return error;
}
1244

1245
/**
1246 1247
 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
 * @state: PM transition of the system being carried out.
1248
 *
1249
 * Execute the ->prepare() callback(s) for all devices.
1250
 */
1251
int dpm_prepare(pm_message_t state)
1252 1253 1254
{
	int error = 0;

1255 1256
	might_sleep();

1257 1258 1259 1260 1261 1262 1263
	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);

1264
		error = device_prepare(dev, state);
1265 1266 1267 1268 1269

		mutex_lock(&dpm_list_mtx);
		if (error) {
			if (error == -EAGAIN) {
				put_device(dev);
S
Sebastian Ott 已提交
1270
				error = 0;
1271 1272
				continue;
			}
1273 1274
			printk(KERN_INFO "PM: Device %s not prepared "
				"for power transition: code %d\n",
1275
				dev_name(dev), error);
1276 1277 1278
			put_device(dev);
			break;
		}
1279
		dev->power.is_prepared = true;
1280
		if (!list_empty(&dev->power.entry))
1281
			list_move_tail(&dev->power.entry, &dpm_prepared_list);
1282 1283 1284
		put_device(dev);
	}
	mutex_unlock(&dpm_list_mtx);
1285 1286 1287
	return error;
}

1288
/**
1289 1290
 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
 * @state: PM transition of the system being carried out.
1291
 *
1292 1293
 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
 * callbacks for them.
1294
 */
1295
int dpm_suspend_start(pm_message_t state)
1296 1297
{
	int error;
1298

1299
	error = dpm_prepare(state);
1300 1301 1302 1303
	if (error) {
		suspend_stats.failed_prepare++;
		dpm_save_failed_step(SUSPEND_PREPARE);
	} else
1304
		error = dpm_suspend(state);
1305 1306
	return error;
}
1307
EXPORT_SYMBOL_GPL(dpm_suspend_start);
1308 1309 1310

void __suspend_report_result(const char *function, void *fn, int ret)
{
1311 1312
	if (ret)
		printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1313 1314
}
EXPORT_SYMBOL_GPL(__suspend_report_result);
1315 1316 1317 1318 1319 1320

/**
 * 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.
 */
1321
int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1322 1323
{
	dpm_wait(dev, subordinate->power.async_suspend);
1324
	return async_error;
1325 1326
}
EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);