main.c 26.6 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/mutex.h>
23
#include <linux/pm.h>
24
#include <linux/pm_runtime.h>
25
#include <linux/resume-trace.h>
26
#include <linux/interrupt.h>
27
#include <linux/sched.h>
28
#include <linux/async.h>
29
#include <linux/suspend.h>
30

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

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

44
LIST_HEAD(dpm_list);
45 46 47
LIST_HEAD(dpm_prepared_list);
LIST_HEAD(dpm_suspended_list);
LIST_HEAD(dpm_noirq_list);
L
Linus Torvalds 已提交
48

49
static DEFINE_MUTEX(dpm_list_mtx);
50
static pm_message_t pm_transition;
L
Linus Torvalds 已提交
51

52 53
static int async_error;

54
/**
55
 * device_pm_init - Initialize the PM-related part of a device object.
56 57 58 59
 * @dev: Device object being initialized.
 */
void device_pm_init(struct device *dev)
{
60
	dev->power.in_suspend = false;
61
	init_completion(&dev->power.completion);
62
	complete_all(&dev->power.completion);
63 64
	dev->power.wakeup = NULL;
	spin_lock_init(&dev->power.lock);
65
	pm_runtime_init(dev);
66
	INIT_LIST_HEAD(&dev->power.entry);
67 68
}

69
/**
70
 * device_pm_lock - Lock the list of active devices used by the PM core.
71 72 73 74 75 76 77
 */
void device_pm_lock(void)
{
	mutex_lock(&dpm_list_mtx);
}

/**
78
 * device_pm_unlock - Unlock the list of active devices used by the PM core.
79 80 81 82 83
 */
void device_pm_unlock(void)
{
	mutex_unlock(&dpm_list_mtx);
}
84

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

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

117
/**
118 119 120
 * 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.
121 122 123 124
 */
void device_pm_move_before(struct device *deva, struct device *devb)
{
	pr_debug("PM: Moving %s:%s before %s:%s\n",
125 126
		 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
		 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
127 128 129 130 131
	/* Delete deva from dpm_list and reinsert before devb. */
	list_move_tail(&deva->power.entry, &devb->power.entry);
}

/**
132 133 134
 * 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.
135 136 137 138
 */
void device_pm_move_after(struct device *deva, struct device *devb)
{
	pr_debug("PM: Moving %s:%s after %s:%s\n",
139 140
		 deva->bus ? deva->bus->name : "No Bus", dev_name(deva),
		 devb->bus ? devb->bus->name : "No Bus", dev_name(devb));
141 142 143 144 145
	/* Delete deva from dpm_list and reinsert after devb. */
	list_move(&deva->power.entry, &devb->power.entry);
}

/**
146 147
 * device_pm_move_last - Move device to end of the PM core's list of devices.
 * @dev: Device to move in dpm_list.
148 149 150 151
 */
void device_pm_move_last(struct device *dev)
{
	pr_debug("PM: Moving %s:%s to end of list\n",
152
		 dev->bus ? dev->bus->name : "No Bus", dev_name(dev));
153 154 155
	list_move_tail(&dev->power.entry, &dpm_list);
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
static ktime_t initcall_debug_start(struct device *dev)
{
	ktime_t calltime = ktime_set(0, 0);

	if (initcall_debug) {
		pr_info("calling  %s+ @ %i\n",
				dev_name(dev), task_pid_nr(current));
		calltime = ktime_get();
	}

	return calltime;
}

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

	if (initcall_debug) {
		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);
	}
}

182 183 184 185 186 187 188 189 190 191
/**
 * 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;

192
	if (async || (pm_async_enabled && dev->power.async_suspend))
193 194 195 196 197 198 199 200 201 202 203 204 205 206
		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);
}

207
/**
208 209 210 211
 * pm_op - Execute the PM operation appropriate for given PM event.
 * @dev: Device to handle.
 * @ops: PM operations to choose from.
 * @state: PM transition of the system being carried out.
212
 */
213 214 215
static int pm_op(struct device *dev,
		 const struct dev_pm_ops *ops,
		 pm_message_t state)
216 217
{
	int error = 0;
218
	ktime_t calltime;
219

220
	calltime = initcall_debug_start(dev);
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

	switch (state.event) {
#ifdef CONFIG_SUSPEND
	case PM_EVENT_SUSPEND:
		if (ops->suspend) {
			error = ops->suspend(dev);
			suspend_report_result(ops->suspend, error);
		}
		break;
	case PM_EVENT_RESUME:
		if (ops->resume) {
			error = ops->resume(dev);
			suspend_report_result(ops->resume, error);
		}
		break;
#endif /* CONFIG_SUSPEND */
237
#ifdef CONFIG_HIBERNATE_CALLBACKS
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
	case PM_EVENT_FREEZE:
	case PM_EVENT_QUIESCE:
		if (ops->freeze) {
			error = ops->freeze(dev);
			suspend_report_result(ops->freeze, error);
		}
		break;
	case PM_EVENT_HIBERNATE:
		if (ops->poweroff) {
			error = ops->poweroff(dev);
			suspend_report_result(ops->poweroff, error);
		}
		break;
	case PM_EVENT_THAW:
	case PM_EVENT_RECOVER:
		if (ops->thaw) {
			error = ops->thaw(dev);
			suspend_report_result(ops->thaw, error);
		}
		break;
	case PM_EVENT_RESTORE:
		if (ops->restore) {
			error = ops->restore(dev);
			suspend_report_result(ops->restore, error);
		}
		break;
264
#endif /* CONFIG_HIBERNATE_CALLBACKS */
265 266 267
	default:
		error = -EINVAL;
	}
268

269
	initcall_debug_report(dev, calltime, error);
270

271 272 273 274
	return error;
}

/**
275 276 277 278
 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
 * @dev: Device to handle.
 * @ops: PM operations to choose from.
 * @state: PM transition of the system being carried out.
279
 *
280 281
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
282
 */
283 284
static int pm_noirq_op(struct device *dev,
			const struct dev_pm_ops *ops,
285 286 287
			pm_message_t state)
{
	int error = 0;
288
	ktime_t calltime = ktime_set(0, 0), delta, rettime;
289 290

	if (initcall_debug) {
291 292 293
		pr_info("calling  %s+ @ %i, parent: %s\n",
				dev_name(dev), task_pid_nr(current),
				dev->parent ? dev_name(dev->parent) : "none");
294 295
		calltime = ktime_get();
	}
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

	switch (state.event) {
#ifdef CONFIG_SUSPEND
	case PM_EVENT_SUSPEND:
		if (ops->suspend_noirq) {
			error = ops->suspend_noirq(dev);
			suspend_report_result(ops->suspend_noirq, error);
		}
		break;
	case PM_EVENT_RESUME:
		if (ops->resume_noirq) {
			error = ops->resume_noirq(dev);
			suspend_report_result(ops->resume_noirq, error);
		}
		break;
#endif /* CONFIG_SUSPEND */
312
#ifdef CONFIG_HIBERNATE_CALLBACKS
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
	case PM_EVENT_FREEZE:
	case PM_EVENT_QUIESCE:
		if (ops->freeze_noirq) {
			error = ops->freeze_noirq(dev);
			suspend_report_result(ops->freeze_noirq, error);
		}
		break;
	case PM_EVENT_HIBERNATE:
		if (ops->poweroff_noirq) {
			error = ops->poweroff_noirq(dev);
			suspend_report_result(ops->poweroff_noirq, error);
		}
		break;
	case PM_EVENT_THAW:
	case PM_EVENT_RECOVER:
		if (ops->thaw_noirq) {
			error = ops->thaw_noirq(dev);
			suspend_report_result(ops->thaw_noirq, error);
		}
		break;
	case PM_EVENT_RESTORE:
		if (ops->restore_noirq) {
			error = ops->restore_noirq(dev);
			suspend_report_result(ops->restore_noirq, error);
		}
		break;
339
#endif /* CONFIG_HIBERNATE_CALLBACKS */
340 341 342
	default:
		error = -EINVAL;
	}
343 344 345 346

	if (initcall_debug) {
		rettime = ktime_get();
		delta = ktime_sub(rettime, calltime);
347 348 349
		printk("initcall %s_i+ returned %d after %Ld usecs\n",
			dev_name(dev), error,
			(unsigned long long)ktime_to_ns(delta) >> 10);
350 351
	}

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
	return error;
}

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",
390
		dev_name(dev), pm_verb(state.event), info, error);
391 392
}

393 394 395
static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
{
	ktime_t calltime;
396
	u64 usecs64;
397 398 399 400 401 402 403 404 405 406 407 408 409
	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);
}

410 411 412
/*------------------------- Resume routines -------------------------*/

/**
413 414 415
 * device_resume_noirq - Execute an "early resume" callback for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
416
 *
417 418
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
419
 */
420
static int device_resume_noirq(struct device *dev, pm_message_t state)
421 422 423 424 425 426
{
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);

427 428 429 430 431
	if (dev->pwr_domain) {
		pm_dev_dbg(dev, state, "EARLY power domain ");
		pm_noirq_op(dev, &dev->pwr_domain->ops, state);
	}

432 433 434
	if (dev->type && dev->type->pm) {
		pm_dev_dbg(dev, state, "EARLY type ");
		error = pm_noirq_op(dev, dev->type->pm, state);
435
	} else if (dev->class && dev->class->pm) {
436 437
		pm_dev_dbg(dev, state, "EARLY class ");
		error = pm_noirq_op(dev, dev->class->pm, state);
438 439 440
	} else if (dev->bus && dev->bus->pm) {
		pm_dev_dbg(dev, state, "EARLY ");
		error = pm_noirq_op(dev, dev->bus->pm, state);
441 442
	}

443 444 445 446 447
	TRACE_RESUME(error);
	return error;
}

/**
448 449
 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
 * @state: PM transition of the system being carried out.
450
 *
451 452
 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
 * enable device drivers to receive interrupts.
453
 */
454
void dpm_resume_noirq(pm_message_t state)
455
{
456
	ktime_t starttime = ktime_get();
457

458
	mutex_lock(&dpm_list_mtx);
459 460
	while (!list_empty(&dpm_noirq_list)) {
		struct device *dev = to_device(dpm_noirq_list.next);
461
		int error;
462 463

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

467 468 469
		error = device_resume_noirq(dev, state);
		if (error)
			pm_dev_err(dev, state, " early", error);
470

471
		mutex_lock(&dpm_list_mtx);
472 473
		put_device(dev);
	}
474
	mutex_unlock(&dpm_list_mtx);
475
	dpm_show_time(starttime, state, "early");
476
	resume_device_irqs();
477
}
478
EXPORT_SYMBOL_GPL(dpm_resume_noirq);
479

480 481
/**
 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
R
Randy Dunlap 已提交
482 483
 * @dev: Device to resume.
 * @cb: Resume callback to execute.
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
 */
static int legacy_resume(struct device *dev, int (*cb)(struct device *dev))
{
	int error;
	ktime_t calltime;

	calltime = initcall_debug_start(dev);

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

	initcall_debug_report(dev, calltime, error);

	return error;
}

500
/**
501
 * device_resume - Execute "resume" callbacks for given device.
502 503
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
504
 * @async: If true, the device is being resumed asynchronously.
505
 */
506
static int device_resume(struct device *dev, pm_message_t state, bool async)
507 508 509 510 511
{
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);
512

513
	dpm_wait(dev->parent, async);
514
	device_lock(dev);
515

516
	dev->power.in_suspend = false;
517

518 519 520 521 522
	if (dev->pwr_domain) {
		pm_dev_dbg(dev, state, "power domain ");
		pm_op(dev, &dev->pwr_domain->ops, state);
	}

523 524 525 526
	if (dev->type && dev->type->pm) {
		pm_dev_dbg(dev, state, "type ");
		error = pm_op(dev, dev->type->pm, state);
		goto End;
527 528
	}

529 530 531 532
	if (dev->class) {
		if (dev->class->pm) {
			pm_dev_dbg(dev, state, "class ");
			error = pm_op(dev, dev->class->pm, state);
533
			goto End;
534 535
		} else if (dev->class->resume) {
			pm_dev_dbg(dev, state, "legacy class ");
536
			error = legacy_resume(dev, dev->class->resume);
537
			goto End;
538
		}
539
	}
540 541 542 543 544 545 546 547 548 549 550

	if (dev->bus) {
		if (dev->bus->pm) {
			pm_dev_dbg(dev, state, "");
			error = pm_op(dev, dev->bus->pm, state);
		} else if (dev->bus->resume) {
			pm_dev_dbg(dev, state, "legacy ");
			error = legacy_resume(dev, dev->bus->resume);
		}
	}

551
 End:
552
	device_unlock(dev);
553
	complete_all(&dev->power.completion);
554

555 556 557 558
	TRACE_RESUME(error);
	return error;
}

559 560 561 562 563
static void async_resume(void *data, async_cookie_t cookie)
{
	struct device *dev = (struct device *)data;
	int error;

564
	error = device_resume(dev, pm_transition, true);
565 566 567 568 569
	if (error)
		pm_dev_err(dev, pm_transition, " async", error);
	put_device(dev);
}

570
static bool is_async(struct device *dev)
571
{
572 573
	return dev->power.async_suspend && pm_async_enabled
		&& !pm_trace_is_enabled();
574 575
}

576
/**
577 578
 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
 * @state: PM transition of the system being carried out.
579
 *
580 581
 * Execute the appropriate "resume" callback for all devices whose status
 * indicates that they are suspended.
582 583 584
 */
static void dpm_resume(pm_message_t state)
{
585
	struct device *dev;
586
	ktime_t starttime = ktime_get();
587 588

	mutex_lock(&dpm_list_mtx);
589
	pm_transition = state;
590
	async_error = 0;
591

592
	list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
593 594 595 596 597 598 599
		INIT_COMPLETION(dev->power.completion);
		if (is_async(dev)) {
			get_device(dev);
			async_schedule(async_resume, dev);
		}
	}

600 601
	while (!list_empty(&dpm_suspended_list)) {
		dev = to_device(dpm_suspended_list.next);
602
		get_device(dev);
603
		if (!is_async(dev)) {
604 605 606 607
			int error;

			mutex_unlock(&dpm_list_mtx);

608
			error = device_resume(dev, state, false);
609 610
			if (error)
				pm_dev_err(dev, state, "", error);
611 612

			mutex_lock(&dpm_list_mtx);
613 614
		}
		if (!list_empty(&dev->power.entry))
615
			list_move_tail(&dev->power.entry, &dpm_prepared_list);
616 617 618
		put_device(dev);
	}
	mutex_unlock(&dpm_list_mtx);
619
	async_synchronize_full();
620
	dpm_show_time(starttime, state, NULL);
621 622 623
}

/**
624 625 626
 * device_complete - Complete a PM transition for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
627
 */
628
static void device_complete(struct device *dev, pm_message_t state)
629
{
630
	device_lock(dev);
631

632 633 634 635 636
	if (dev->pwr_domain && dev->pwr_domain->ops.complete) {
		pm_dev_dbg(dev, state, "completing power domain ");
		dev->pwr_domain->ops.complete(dev);
	}

637
	if (dev->type && dev->type->pm) {
638
		pm_dev_dbg(dev, state, "completing type ");
639 640 641 642 643 644 645
		if (dev->type->pm->complete)
			dev->type->pm->complete(dev);
	} else if (dev->class && dev->class->pm) {
		pm_dev_dbg(dev, state, "completing class ");
		if (dev->class->pm->complete)
			dev->class->pm->complete(dev);
	} else if (dev->bus && dev->bus->pm) {
646
		pm_dev_dbg(dev, state, "completing ");
647 648
		if (dev->bus->pm->complete)
			dev->bus->pm->complete(dev);
649 650
	}

651
	device_unlock(dev);
652 653 654
}

/**
655 656
 * dpm_complete - Complete a PM transition for all non-sysdev devices.
 * @state: PM transition of the system being carried out.
657
 *
658 659
 * Execute the ->complete() callbacks for all devices whose PM status is not
 * DPM_ON (this allows new devices to be registered).
660
 */
661
static void dpm_complete(pm_message_t state)
662
{
663 664 665
	struct list_head list;

	INIT_LIST_HEAD(&list);
666
	mutex_lock(&dpm_list_mtx);
667 668
	while (!list_empty(&dpm_prepared_list)) {
		struct device *dev = to_device(dpm_prepared_list.prev);
669

670
		get_device(dev);
671
		dev->power.in_suspend = false;
672 673
		list_move(&dev->power.entry, &list);
		mutex_unlock(&dpm_list_mtx);
674

675
		device_complete(dev, state);
676

677
		mutex_lock(&dpm_list_mtx);
678
		put_device(dev);
679
	}
680
	list_splice(&list, &dpm_list);
681 682 683 684
	mutex_unlock(&dpm_list_mtx);
}

/**
685 686
 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
 * @state: PM transition of the system being carried out.
687
 *
688 689
 * Execute "resume" callbacks for all devices and complete the PM transition of
 * the system.
690
 */
691
void dpm_resume_end(pm_message_t state)
692
{
693
	might_sleep();
694 695
	dpm_resume(state);
	dpm_complete(state);
696
}
697
EXPORT_SYMBOL_GPL(dpm_resume_end);
698 699 700 701


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

702
/**
703 704 705 706 707
 * 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.
708 709
 */
static pm_message_t resume_event(pm_message_t sleep_state)
710
{
711 712 713 714 715 716 717 718
	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;
719
	}
720
	return PMSG_ON;
721 722 723
}

/**
724 725 726
 * device_suspend_noirq - Execute a "late suspend" callback for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
727
 *
728 729
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
730
 */
731
static int device_suspend_noirq(struct device *dev, pm_message_t state)
732
{
733
	int error;
734 735 736 737 738

	if (dev->type && dev->type->pm) {
		pm_dev_dbg(dev, state, "LATE type ");
		error = pm_noirq_op(dev, dev->type->pm, state);
		if (error)
739 740 741 742 743 744 745
			return error;
	} else if (dev->class && dev->class->pm) {
		pm_dev_dbg(dev, state, "LATE class ");
		error = pm_noirq_op(dev, dev->class->pm, state);
		if (error)
			return error;
	} else if (dev->bus && dev->bus->pm) {
746 747
		pm_dev_dbg(dev, state, "LATE ");
		error = pm_noirq_op(dev, dev->bus->pm, state);
748
		if (error)
749
			return error;
750 751 752 753 754
	}

	if (dev->pwr_domain) {
		pm_dev_dbg(dev, state, "LATE power domain ");
		pm_noirq_op(dev, &dev->pwr_domain->ops, state);
755
	}
756

757
	return 0;
758 759 760
}

/**
761 762
 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
 * @state: PM transition of the system being carried out.
763
 *
764 765
 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
 * handlers for all non-sysdev devices.
766
 */
767
int dpm_suspend_noirq(pm_message_t state)
768
{
769
	ktime_t starttime = ktime_get();
770 771
	int error = 0;

772
	suspend_device_irqs();
773
	mutex_lock(&dpm_list_mtx);
774 775
	while (!list_empty(&dpm_suspended_list)) {
		struct device *dev = to_device(dpm_suspended_list.prev);
776 777 778 779

		get_device(dev);
		mutex_unlock(&dpm_list_mtx);

780
		error = device_suspend_noirq(dev, state);
781 782

		mutex_lock(&dpm_list_mtx);
783
		if (error) {
784
			pm_dev_err(dev, state, " late", error);
785
			put_device(dev);
786 787
			break;
		}
788
		if (!list_empty(&dev->power.entry))
789
			list_move(&dev->power.entry, &dpm_noirq_list);
790
		put_device(dev);
791
	}
792
	mutex_unlock(&dpm_list_mtx);
793
	if (error)
794
		dpm_resume_noirq(resume_event(state));
795 796
	else
		dpm_show_time(starttime, state, "late");
797 798
	return error;
}
799
EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
800

801 802
/**
 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
R
Randy Dunlap 已提交
803 804 805
 * @dev: Device to suspend.
 * @state: PM transition of the system being carried out.
 * @cb: Suspend callback to execute.
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
 */
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;
}

823
/**
824 825 826
 * device_suspend - Execute "suspend" callbacks for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
827
 * @async: If true, the device is being suspended asynchronously.
828
 */
829
static int __device_suspend(struct device *dev, pm_message_t state, bool async)
830 831 832
{
	int error = 0;

833
	dpm_wait_for_children(dev, async);
834
	device_lock(dev);
835

836 837 838
	if (async_error)
		goto End;

839 840 841 842 843
	if (pm_wakeup_pending()) {
		async_error = -EBUSY;
		goto End;
	}

844 845 846 847 848 849
	if (dev->type && dev->type->pm) {
		pm_dev_dbg(dev, state, "type ");
		error = pm_op(dev, dev->type->pm, state);
		goto Domain;
	}

850 851 852 853
	if (dev->class) {
		if (dev->class->pm) {
			pm_dev_dbg(dev, state, "class ");
			error = pm_op(dev, dev->class->pm, state);
854
			goto Domain;
855 856
		} else if (dev->class->suspend) {
			pm_dev_dbg(dev, state, "legacy class ");
857
			error = legacy_suspend(dev, state, dev->class->suspend);
858
			goto Domain;
859
		}
860 861
	}

862 863 864
	if (dev->bus) {
		if (dev->bus->pm) {
			pm_dev_dbg(dev, state, "");
865
			error = pm_op(dev, dev->bus->pm, state);
866 867
		} else if (dev->bus->suspend) {
			pm_dev_dbg(dev, state, "legacy ");
868
			error = legacy_suspend(dev, state, dev->bus->suspend);
869
		}
870 871
	}

872 873
 Domain:
	if (!error && dev->pwr_domain) {
874 875
		pm_dev_dbg(dev, state, "power domain ");
		pm_op(dev, &dev->pwr_domain->ops, state);
876
	}
877

878
 End:
879
	device_unlock(dev);
880
	complete_all(&dev->power.completion);
881

882 883 884
	if (error)
		async_error = error;

885 886 887
	return error;
}

888 889 890 891 892 893
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);
894
	if (error)
895 896 897 898 899 900 901 902 903
		pm_dev_err(dev, pm_transition, " async", error);

	put_device(dev);
}

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

904
	if (pm_async_enabled && dev->power.async_suspend) {
905 906 907 908 909 910 911 912
		get_device(dev);
		async_schedule(async_suspend, dev);
		return 0;
	}

	return __device_suspend(dev, pm_transition, false);
}

913
/**
914 915
 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
 * @state: PM transition of the system being carried out.
916
 */
917
static int dpm_suspend(pm_message_t state)
918
{
919
	ktime_t starttime = ktime_get();
920 921 922
	int error = 0;

	mutex_lock(&dpm_list_mtx);
923 924
	pm_transition = state;
	async_error = 0;
925 926
	while (!list_empty(&dpm_prepared_list)) {
		struct device *dev = to_device(dpm_prepared_list.prev);
927

928
		get_device(dev);
929
		mutex_unlock(&dpm_list_mtx);
930

931
		error = device_suspend(dev);
932

933
		mutex_lock(&dpm_list_mtx);
934
		if (error) {
935 936
			pm_dev_err(dev, state, "", error);
			put_device(dev);
937 938
			break;
		}
939
		if (!list_empty(&dev->power.entry))
940
			list_move(&dev->power.entry, &dpm_suspended_list);
941
		put_device(dev);
942 943
		if (async_error)
			break;
944 945
	}
	mutex_unlock(&dpm_list_mtx);
946 947 948
	async_synchronize_full();
	if (!error)
		error = async_error;
949 950
	if (!error)
		dpm_show_time(starttime, state, NULL);
951 952 953 954
	return error;
}

/**
955 956 957 958 959 960
 * 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.
961
 */
962
static int device_prepare(struct device *dev, pm_message_t state)
963 964 965
{
	int error = 0;

966
	device_lock(dev);
967

968
	if (dev->type && dev->type->pm) {
969
		pm_dev_dbg(dev, state, "preparing type ");
970 971
		if (dev->type->pm->prepare)
			error = dev->type->pm->prepare(dev);
972 973 974
		suspend_report_result(dev->type->pm->prepare, error);
		if (error)
			goto End;
975
	} else if (dev->class && dev->class->pm) {
976
		pm_dev_dbg(dev, state, "preparing class ");
977 978
		if (dev->class->pm->prepare)
			error = dev->class->pm->prepare(dev);
979
		suspend_report_result(dev->class->pm->prepare, error);
980 981
		if (error)
			goto End;
982 983 984 985 986 987 988
	} else if (dev->bus && dev->bus->pm) {
		pm_dev_dbg(dev, state, "preparing ");
		if (dev->bus->pm->prepare)
			error = dev->bus->pm->prepare(dev);
		suspend_report_result(dev->bus->pm->prepare, error);
		if (error)
			goto End;
989 990 991 992 993
	}

	if (dev->pwr_domain && dev->pwr_domain->ops.prepare) {
		pm_dev_dbg(dev, state, "preparing power domain ");
		dev->pwr_domain->ops.prepare(dev);
994
	}
995

996
 End:
997
	device_unlock(dev);
998 999 1000

	return error;
}
1001

1002
/**
1003 1004
 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
 * @state: PM transition of the system being carried out.
1005
 *
1006
 * Execute the ->prepare() callback(s) for all devices.
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
 */
static int dpm_prepare(pm_message_t state)
{
	int error = 0;

	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);

1019
		pm_runtime_get_noresume(dev);
1020 1021 1022
		if (pm_runtime_barrier(dev) && device_may_wakeup(dev))
			pm_wakeup_event(dev, 0);

1023 1024 1025
		pm_runtime_put_sync(dev);
		error = pm_wakeup_pending() ?
				-EBUSY : device_prepare(dev, state);
1026 1027 1028 1029 1030

		mutex_lock(&dpm_list_mtx);
		if (error) {
			if (error == -EAGAIN) {
				put_device(dev);
S
Sebastian Ott 已提交
1031
				error = 0;
1032 1033
				continue;
			}
1034 1035
			printk(KERN_INFO "PM: Device %s not prepared "
				"for power transition: code %d\n",
1036
				dev_name(dev), error);
1037 1038 1039
			put_device(dev);
			break;
		}
1040
		dev->power.in_suspend = true;
1041
		if (!list_empty(&dev->power.entry))
1042
			list_move_tail(&dev->power.entry, &dpm_prepared_list);
1043 1044 1045
		put_device(dev);
	}
	mutex_unlock(&dpm_list_mtx);
1046 1047 1048
	return error;
}

1049
/**
1050 1051
 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
 * @state: PM transition of the system being carried out.
1052
 *
1053 1054
 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
 * callbacks for them.
1055
 */
1056
int dpm_suspend_start(pm_message_t state)
1057 1058
{
	int error;
1059

1060
	might_sleep();
1061 1062 1063
	error = dpm_prepare(state);
	if (!error)
		error = dpm_suspend(state);
1064 1065
	return error;
}
1066
EXPORT_SYMBOL_GPL(dpm_suspend_start);
1067 1068 1069

void __suspend_report_result(const char *function, void *fn, int ret)
{
1070 1071
	if (ret)
		printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1072 1073
}
EXPORT_SYMBOL_GPL(__suspend_report_result);
1074 1075 1076 1077 1078 1079

/**
 * 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.
 */
1080
int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1081 1082
{
	dpm_wait(dev, subordinate->power.async_suspend);
1083
	return async_error;
1084 1085
}
EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);