main.c 26.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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.
 * This will intialize the embedded device_pm_info object in the device
 * 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 54 55 56 57
/*
 * Set once the preparation of devices for a PM transition has started, reset
 * before starting to resume devices.  Protected by dpm_list_mtx.
 */
static bool transition_started;

58 59
static int async_error;

60
/**
61
 * device_pm_init - Initialize the PM-related part of a device object.
62 63 64 65 66
 * @dev: Device object being initialized.
 */
void device_pm_init(struct device *dev)
{
	dev->power.status = DPM_ON;
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 72 73
	pm_runtime_init(dev);
}

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

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

90
/**
91 92
 * device_pm_add - Add a device to the PM core's list of active devices.
 * @dev: Device to add to the list.
93
 */
94
void device_pm_add(struct device *dev)
L
Linus Torvalds 已提交
95 96
{
	pr_debug("PM: Adding info for %s:%s\n",
97 98
		 dev->bus ? dev->bus->name : "No Bus",
		 kobject_name(&dev->kobj));
99
	mutex_lock(&dpm_list_mtx);
100
	if (dev->parent) {
101 102
		if (dev->parent->power.status >= DPM_SUSPENDING)
			dev_warn(dev, "parent %s should not be sleeping\n",
103
				 dev_name(dev->parent));
104 105 106 107 108 109
	} else if (transition_started) {
		/*
		 * We refuse to register parentless devices while a PM
		 * transition is in progress in order to avoid leaving them
		 * unhandled down the road
		 */
110
		dev_WARN(dev, "Parentless device registered during a PM transaction\n");
111
	}
112 113

	list_add_tail(&dev->power.entry, &dpm_list);
114
	mutex_unlock(&dpm_list_mtx);
L
Linus Torvalds 已提交
115 116
}

117
/**
118 119
 * device_pm_remove - Remove a device from the PM core's list of active devices.
 * @dev: Device to be removed from the list.
120
 */
121
void device_pm_remove(struct device *dev)
L
Linus Torvalds 已提交
122 123
{
	pr_debug("PM: Removing info for %s:%s\n",
124 125
		 dev->bus ? dev->bus->name : "No Bus",
		 kobject_name(&dev->kobj));
126
	complete_all(&dev->power.completion);
127
	mutex_lock(&dpm_list_mtx);
L
Linus Torvalds 已提交
128
	list_del_init(&dev->power.entry);
129
	mutex_unlock(&dpm_list_mtx);
130
	device_wakeup_disable(dev);
131
	pm_runtime_remove(dev);
132 133
}

134
/**
135 136 137
 * 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.
138 139 140 141 142 143 144 145 146 147 148 149 150
 */
void device_pm_move_before(struct device *deva, struct device *devb)
{
	pr_debug("PM: Moving %s:%s before %s:%s\n",
		 deva->bus ? deva->bus->name : "No Bus",
		 kobject_name(&deva->kobj),
		 devb->bus ? devb->bus->name : "No Bus",
		 kobject_name(&devb->kobj));
	/* Delete deva from dpm_list and reinsert before devb. */
	list_move_tail(&deva->power.entry, &devb->power.entry);
}

/**
151 152 153
 * 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.
154 155 156 157 158 159 160 161 162 163 164 165 166
 */
void device_pm_move_after(struct device *deva, struct device *devb)
{
	pr_debug("PM: Moving %s:%s after %s:%s\n",
		 deva->bus ? deva->bus->name : "No Bus",
		 kobject_name(&deva->kobj),
		 devb->bus ? devb->bus->name : "No Bus",
		 kobject_name(&devb->kobj));
	/* Delete deva from dpm_list and reinsert after devb. */
	list_move(&deva->power.entry, &devb->power.entry);
}

/**
167 168
 * device_pm_move_last - Move device to end of the PM core's list of devices.
 * @dev: Device to move in dpm_list.
169 170 171 172 173 174 175 176 177
 */
void device_pm_move_last(struct device *dev)
{
	pr_debug("PM: Moving %s:%s to end of list\n",
		 dev->bus ? dev->bus->name : "No Bus",
		 kobject_name(&dev->kobj));
	list_move_tail(&dev->power.entry, &dpm_list);
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
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);
	}
}

204 205 206 207 208 209 210 211 212 213
/**
 * 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;

214
	if (async || (pm_async_enabled && dev->power.async_suspend))
215 216 217 218 219 220 221 222 223 224 225 226 227 228
		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);
}

229
/**
230 231 232 233
 * 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.
234
 */
235 236 237
static int pm_op(struct device *dev,
		 const struct dev_pm_ops *ops,
		 pm_message_t state)
238 239
{
	int error = 0;
240
	ktime_t calltime;
241

242
	calltime = initcall_debug_start(dev);
243 244 245 246 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 283 284 285 286 287 288 289

	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 */
#ifdef CONFIG_HIBERNATION
	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;
#endif /* CONFIG_HIBERNATION */
	default:
		error = -EINVAL;
	}
290

291
	initcall_debug_report(dev, calltime, error);
292

293 294 295 296
	return error;
}

/**
297 298 299 300
 * 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.
301
 *
302 303
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
304
 */
305 306
static int pm_noirq_op(struct device *dev,
			const struct dev_pm_ops *ops,
307 308 309
			pm_message_t state)
{
	int error = 0;
310
	ktime_t calltime = ktime_set(0, 0), delta, rettime;
311 312

	if (initcall_debug) {
313 314 315
		pr_info("calling  %s+ @ %i, parent: %s\n",
				dev_name(dev), task_pid_nr(current),
				dev->parent ? dev_name(dev->parent) : "none");
316 317
		calltime = ktime_get();
	}
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 352 353 354 355 356 357 358 359 360 361 362 363 364

	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 */
#ifdef CONFIG_HIBERNATION
	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;
#endif /* CONFIG_HIBERNATION */
	default:
		error = -EINVAL;
	}
365 366 367 368

	if (initcall_debug) {
		rettime = ktime_get();
		delta = ktime_sub(rettime, calltime);
369 370 371
		printk("initcall %s_i+ returned %d after %Ld usecs\n",
			dev_name(dev), error,
			(unsigned long long)ktime_to_ns(delta) >> 10);
372 373
	}

374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	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",
		kobject_name(&dev->kobj), pm_verb(state.event), info, error);
}

415 416 417
static void dpm_show_time(ktime_t starttime, pm_message_t state, char *info)
{
	ktime_t calltime;
418
	u64 usecs64;
419 420 421 422 423 424 425 426 427 428 429 430 431
	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);
}

432 433 434
/*------------------------- Resume routines -------------------------*/

/**
435 436 437
 * device_resume_noirq - Execute an "early resume" callback for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
438
 *
439 440
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
441
 */
442
static int device_resume_noirq(struct device *dev, pm_message_t state)
443 444 445 446 447 448
{
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);

449
	if (dev->bus && dev->bus->pm) {
450 451
		pm_dev_dbg(dev, state, "EARLY ");
		error = pm_noirq_op(dev, dev->bus->pm, state);
452 453
		if (error)
			goto End;
454
	}
455

456 457 458 459 460 461 462 463 464 465 466 467 468
	if (dev->type && dev->type->pm) {
		pm_dev_dbg(dev, state, "EARLY type ");
		error = pm_noirq_op(dev, dev->type->pm, state);
		if (error)
			goto End;
	}

	if (dev->class && dev->class->pm) {
		pm_dev_dbg(dev, state, "EARLY class ");
		error = pm_noirq_op(dev, dev->class->pm, state);
	}

End:
469 470 471 472 473
	TRACE_RESUME(error);
	return error;
}

/**
474 475
 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
 * @state: PM transition of the system being carried out.
476
 *
477 478
 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
 * enable device drivers to receive interrupts.
479
 */
480
void dpm_resume_noirq(pm_message_t state)
481
{
482
	ktime_t starttime = ktime_get();
483

484
	mutex_lock(&dpm_list_mtx);
485
	transition_started = false;
486 487
	while (!list_empty(&dpm_noirq_list)) {
		struct device *dev = to_device(dpm_noirq_list.next);
488 489

		get_device(dev);
490 491
		if (dev->power.status > DPM_OFF) {
			int error;
492

493
			dev->power.status = DPM_OFF;
494 495
			mutex_unlock(&dpm_list_mtx);

496
			error = device_resume_noirq(dev, state);
497 498

			mutex_lock(&dpm_list_mtx);
499 500 501
			if (error)
				pm_dev_err(dev, state, " early", error);
		}
502
		if (!list_empty(&dev->power.entry))
503
			list_move_tail(&dev->power.entry, &dpm_suspended_list);
504 505
		put_device(dev);
	}
506
	mutex_unlock(&dpm_list_mtx);
507
	dpm_show_time(starttime, state, "early");
508
	resume_device_irqs();
509
}
510
EXPORT_SYMBOL_GPL(dpm_resume_noirq);
511

512 513
/**
 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
R
Randy Dunlap 已提交
514 515
 * @dev: Device to resume.
 * @cb: Resume callback to execute.
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
 */
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;
}

532
/**
533
 * device_resume - Execute "resume" callbacks for given device.
534 535
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
536
 * @async: If true, the device is being resumed asynchronously.
537
 */
538
static int device_resume(struct device *dev, pm_message_t state, bool async)
539 540 541 542 543
{
	int error = 0;

	TRACE_DEVICE(dev);
	TRACE_RESUME(0);
544

545
	dpm_wait(dev->parent, async);
546
	device_lock(dev);
547

548 549
	dev->power.status = DPM_RESUMING;

550 551 552
	if (dev->bus) {
		if (dev->bus->pm) {
			pm_dev_dbg(dev, state, "");
553
			error = pm_op(dev, dev->bus->pm, state);
554 555
		} else if (dev->bus->resume) {
			pm_dev_dbg(dev, state, "legacy ");
556
			error = legacy_resume(dev, dev->bus->resume);
557 558 559
		}
		if (error)
			goto End;
560 561
	}

562 563 564 565 566 567 568
	if (dev->type) {
		if (dev->type->pm) {
			pm_dev_dbg(dev, state, "type ");
			error = pm_op(dev, dev->type->pm, state);
		}
		if (error)
			goto End;
569 570
	}

571 572 573 574 575 576
	if (dev->class) {
		if (dev->class->pm) {
			pm_dev_dbg(dev, state, "class ");
			error = pm_op(dev, dev->class->pm, state);
		} else if (dev->class->resume) {
			pm_dev_dbg(dev, state, "legacy class ");
577
			error = legacy_resume(dev, dev->class->resume);
578
		}
579
	}
580
 End:
581
	device_unlock(dev);
582
	complete_all(&dev->power.completion);
583

584 585 586 587
	TRACE_RESUME(error);
	return error;
}

588 589 590 591 592
static void async_resume(void *data, async_cookie_t cookie)
{
	struct device *dev = (struct device *)data;
	int error;

593
	error = device_resume(dev, pm_transition, true);
594 595 596 597 598
	if (error)
		pm_dev_err(dev, pm_transition, " async", error);
	put_device(dev);
}

599
static bool is_async(struct device *dev)
600
{
601 602
	return dev->power.async_suspend && pm_async_enabled
		&& !pm_trace_is_enabled();
603 604
}

605
/**
606 607
 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
 * @state: PM transition of the system being carried out.
608
 *
609 610
 * Execute the appropriate "resume" callback for all devices whose status
 * indicates that they are suspended.
611 612 613
 */
static void dpm_resume(pm_message_t state)
{
614
	struct device *dev;
615
	ktime_t starttime = ktime_get();
616 617

	mutex_lock(&dpm_list_mtx);
618
	pm_transition = state;
619
	async_error = 0;
620

621
	list_for_each_entry(dev, &dpm_suspended_list, power.entry) {
622 623 624 625 626 627 628 629 630 631
		if (dev->power.status < DPM_OFF)
			continue;

		INIT_COMPLETION(dev->power.completion);
		if (is_async(dev)) {
			get_device(dev);
			async_schedule(async_resume, dev);
		}
	}

632 633
	while (!list_empty(&dpm_suspended_list)) {
		dev = to_device(dpm_suspended_list.next);
634
		get_device(dev);
635
		if (dev->power.status >= DPM_OFF && !is_async(dev)) {
636 637 638 639
			int error;

			mutex_unlock(&dpm_list_mtx);

640
			error = device_resume(dev, state, false);
641 642 643 644 645 646

			mutex_lock(&dpm_list_mtx);
			if (error)
				pm_dev_err(dev, state, "", error);
		}
		if (!list_empty(&dev->power.entry))
647
			list_move_tail(&dev->power.entry, &dpm_prepared_list);
648 649 650
		put_device(dev);
	}
	mutex_unlock(&dpm_list_mtx);
651
	async_synchronize_full();
652
	dpm_show_time(starttime, state, NULL);
653 654 655
}

/**
656 657 658
 * device_complete - Complete a PM transition for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
659
 */
660
static void device_complete(struct device *dev, pm_message_t state)
661
{
662
	device_lock(dev);
663 664 665 666 667 668 669 670 671 672 673

	if (dev->class && dev->class->pm && dev->class->pm->complete) {
		pm_dev_dbg(dev, state, "completing class ");
		dev->class->pm->complete(dev);
	}

	if (dev->type && dev->type->pm && dev->type->pm->complete) {
		pm_dev_dbg(dev, state, "completing type ");
		dev->type->pm->complete(dev);
	}

674
	if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
675
		pm_dev_dbg(dev, state, "completing ");
676
		dev->bus->pm->complete(dev);
677 678
	}

679
	device_unlock(dev);
680 681 682
}

/**
683 684
 * dpm_complete - Complete a PM transition for all non-sysdev devices.
 * @state: PM transition of the system being carried out.
685
 *
686 687
 * Execute the ->complete() callbacks for all devices whose PM status is not
 * DPM_ON (this allows new devices to be registered).
688
 */
689
static void dpm_complete(pm_message_t state)
690
{
691 692 693
	struct list_head list;

	INIT_LIST_HEAD(&list);
694
	mutex_lock(&dpm_list_mtx);
R
Romit Dasgupta 已提交
695
	transition_started = false;
696 697
	while (!list_empty(&dpm_prepared_list)) {
		struct device *dev = to_device(dpm_prepared_list.prev);
698

699 700 701 702 703
		get_device(dev);
		if (dev->power.status > DPM_ON) {
			dev->power.status = DPM_ON;
			mutex_unlock(&dpm_list_mtx);

704
			device_complete(dev, state);
705
			pm_runtime_put_sync(dev);
706 707 708 709 710 711

			mutex_lock(&dpm_list_mtx);
		}
		if (!list_empty(&dev->power.entry))
			list_move(&dev->power.entry, &list);
		put_device(dev);
712
	}
713
	list_splice(&list, &dpm_list);
714 715 716 717
	mutex_unlock(&dpm_list_mtx);
}

/**
718 719
 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
 * @state: PM transition of the system being carried out.
720
 *
721 722
 * Execute "resume" callbacks for all devices and complete the PM transition of
 * the system.
723
 */
724
void dpm_resume_end(pm_message_t state)
725
{
726
	might_sleep();
727 728
	dpm_resume(state);
	dpm_complete(state);
729
}
730
EXPORT_SYMBOL_GPL(dpm_resume_end);
731 732 733 734


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

735
/**
736 737 738 739 740
 * 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.
741 742
 */
static pm_message_t resume_event(pm_message_t sleep_state)
743
{
744 745 746 747 748 749 750 751
	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;
752
	}
753
	return PMSG_ON;
754 755 756
}

/**
757 758 759
 * device_suspend_noirq - Execute a "late suspend" callback for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
760
 *
761 762
 * The driver of @dev will not receive interrupts while this function is being
 * executed.
763
 */
764
static int device_suspend_noirq(struct device *dev, pm_message_t state)
765 766
{
	int error = 0;
767

768 769 770 771 772 773 774 775 776 777 778 779 780 781
	if (dev->class && dev->class->pm) {
		pm_dev_dbg(dev, state, "LATE class ");
		error = pm_noirq_op(dev, dev->class->pm, state);
		if (error)
			goto End;
	}

	if (dev->type && dev->type->pm) {
		pm_dev_dbg(dev, state, "LATE type ");
		error = pm_noirq_op(dev, dev->type->pm, state);
		if (error)
			goto End;
	}

782
	if (dev->bus && dev->bus->pm) {
783 784
		pm_dev_dbg(dev, state, "LATE ");
		error = pm_noirq_op(dev, dev->bus->pm, state);
785
	}
786 787

End:
788 789 790 791
	return error;
}

/**
792 793
 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
 * @state: PM transition of the system being carried out.
794
 *
795 796
 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
 * handlers for all non-sysdev devices.
797
 */
798
int dpm_suspend_noirq(pm_message_t state)
799
{
800
	ktime_t starttime = ktime_get();
801 802
	int error = 0;

803
	suspend_device_irqs();
804
	mutex_lock(&dpm_list_mtx);
805 806
	while (!list_empty(&dpm_suspended_list)) {
		struct device *dev = to_device(dpm_suspended_list.prev);
807 808 809 810

		get_device(dev);
		mutex_unlock(&dpm_list_mtx);

811
		error = device_suspend_noirq(dev, state);
812 813

		mutex_lock(&dpm_list_mtx);
814
		if (error) {
815
			pm_dev_err(dev, state, " late", error);
816
			put_device(dev);
817 818
			break;
		}
819
		dev->power.status = DPM_OFF_IRQ;
820
		if (!list_empty(&dev->power.entry))
821
			list_move(&dev->power.entry, &dpm_noirq_list);
822
		put_device(dev);
823
	}
824
	mutex_unlock(&dpm_list_mtx);
825
	if (error)
826
		dpm_resume_noirq(resume_event(state));
827 828
	else
		dpm_show_time(starttime, state, "late");
829 830
	return error;
}
831
EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
832

833 834
/**
 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
R
Randy Dunlap 已提交
835 836 837
 * @dev: Device to suspend.
 * @state: PM transition of the system being carried out.
 * @cb: Suspend callback to execute.
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
 */
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;
}

855
/**
856 857 858
 * device_suspend - Execute "suspend" callbacks for given device.
 * @dev: Device to handle.
 * @state: PM transition of the system being carried out.
859
 * @async: If true, the device is being suspended asynchronously.
860
 */
861
static int __device_suspend(struct device *dev, pm_message_t state, bool async)
862 863 864
{
	int error = 0;

865
	dpm_wait_for_children(dev, async);
866
	device_lock(dev);
867

868 869 870
	if (async_error)
		goto End;

871 872 873 874 875
	if (pm_wakeup_pending()) {
		async_error = -EBUSY;
		goto End;
	}

876 877 878 879 880 881
	if (dev->class) {
		if (dev->class->pm) {
			pm_dev_dbg(dev, state, "class ");
			error = pm_op(dev, dev->class->pm, state);
		} else if (dev->class->suspend) {
			pm_dev_dbg(dev, state, "legacy class ");
882
			error = legacy_suspend(dev, state, dev->class->suspend);
883 884 885
		}
		if (error)
			goto End;
886 887
	}

888 889 890 891 892 893 894
	if (dev->type) {
		if (dev->type->pm) {
			pm_dev_dbg(dev, state, "type ");
			error = pm_op(dev, dev->type->pm, state);
		}
		if (error)
			goto End;
895 896
	}

897 898 899
	if (dev->bus) {
		if (dev->bus->pm) {
			pm_dev_dbg(dev, state, "");
900
			error = pm_op(dev, dev->bus->pm, state);
901 902
		} else if (dev->bus->suspend) {
			pm_dev_dbg(dev, state, "legacy ");
903
			error = legacy_suspend(dev, state, dev->bus->suspend);
904
		}
905
	}
906 907 908 909

	if (!error)
		dev->power.status = DPM_OFF;

910
 End:
911
	device_unlock(dev);
912
	complete_all(&dev->power.completion);
913

914 915 916
	if (error)
		async_error = error;

917 918 919
	return error;
}

920 921 922 923 924 925
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);
926
	if (error)
927 928 929 930 931 932 933 934 935
		pm_dev_err(dev, pm_transition, " async", error);

	put_device(dev);
}

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

936
	if (pm_async_enabled && dev->power.async_suspend) {
937 938 939 940 941 942 943 944
		get_device(dev);
		async_schedule(async_suspend, dev);
		return 0;
	}

	return __device_suspend(dev, pm_transition, false);
}

945
/**
946 947
 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
 * @state: PM transition of the system being carried out.
948
 */
949
static int dpm_suspend(pm_message_t state)
950
{
951
	ktime_t starttime = ktime_get();
952 953 954
	int error = 0;

	mutex_lock(&dpm_list_mtx);
955 956
	pm_transition = state;
	async_error = 0;
957 958
	while (!list_empty(&dpm_prepared_list)) {
		struct device *dev = to_device(dpm_prepared_list.prev);
959

960
		get_device(dev);
961
		mutex_unlock(&dpm_list_mtx);
962

963
		error = device_suspend(dev);
964

965
		mutex_lock(&dpm_list_mtx);
966
		if (error) {
967 968
			pm_dev_err(dev, state, "", error);
			put_device(dev);
969 970
			break;
		}
971
		if (!list_empty(&dev->power.entry))
972
			list_move(&dev->power.entry, &dpm_suspended_list);
973
		put_device(dev);
974 975
		if (async_error)
			break;
976 977
	}
	mutex_unlock(&dpm_list_mtx);
978 979 980
	async_synchronize_full();
	if (!error)
		error = async_error;
981 982
	if (!error)
		dpm_show_time(starttime, state, NULL);
983 984 985 986
	return error;
}

/**
987 988 989 990 991 992
 * 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.
993
 */
994
static int device_prepare(struct device *dev, pm_message_t state)
995 996 997
{
	int error = 0;

998
	device_lock(dev);
999

1000
	if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
1001
		pm_dev_dbg(dev, state, "preparing ");
1002 1003
		error = dev->bus->pm->prepare(dev);
		suspend_report_result(dev->bus->pm->prepare, error);
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
		if (error)
			goto End;
	}

	if (dev->type && dev->type->pm && dev->type->pm->prepare) {
		pm_dev_dbg(dev, state, "preparing type ");
		error = dev->type->pm->prepare(dev);
		suspend_report_result(dev->type->pm->prepare, error);
		if (error)
			goto End;
	}

	if (dev->class && dev->class->pm && dev->class->pm->prepare) {
		pm_dev_dbg(dev, state, "preparing class ");
		error = dev->class->pm->prepare(dev);
		suspend_report_result(dev->class->pm->prepare, error);
	}
 End:
1022
	device_unlock(dev);
1023 1024 1025

	return error;
}
1026

1027
/**
1028 1029
 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
 * @state: PM transition of the system being carried out.
1030
 *
1031
 * Execute the ->prepare() callback(s) for all devices.
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
 */
static int dpm_prepare(pm_message_t state)
{
	int error = 0;

	mutex_lock(&dpm_list_mtx);
	transition_started = true;
	while (!list_empty(&dpm_list)) {
		struct device *dev = to_device(dpm_list.next);

		get_device(dev);
		dev->power.status = DPM_PREPARING;
		mutex_unlock(&dpm_list_mtx);

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

1050
		if (pm_wakeup_pending()) {
1051
			pm_runtime_put_sync(dev);
1052 1053 1054 1055
			error = -EBUSY;
		} else {
			error = device_prepare(dev, state);
		}
1056 1057 1058 1059 1060 1061

		mutex_lock(&dpm_list_mtx);
		if (error) {
			dev->power.status = DPM_ON;
			if (error == -EAGAIN) {
				put_device(dev);
S
Sebastian Ott 已提交
1062
				error = 0;
1063 1064
				continue;
			}
1065 1066
			printk(KERN_INFO "PM: Device %s not prepared "
				"for power transition: code %d\n",
1067 1068 1069 1070 1071 1072
				kobject_name(&dev->kobj), error);
			put_device(dev);
			break;
		}
		dev->power.status = DPM_SUSPENDING;
		if (!list_empty(&dev->power.entry))
1073
			list_move_tail(&dev->power.entry, &dpm_prepared_list);
1074 1075 1076
		put_device(dev);
	}
	mutex_unlock(&dpm_list_mtx);
1077 1078 1079
	return error;
}

1080
/**
1081 1082
 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
 * @state: PM transition of the system being carried out.
1083
 *
1084 1085
 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
 * callbacks for them.
1086
 */
1087
int dpm_suspend_start(pm_message_t state)
1088 1089
{
	int error;
1090

1091
	might_sleep();
1092 1093 1094
	error = dpm_prepare(state);
	if (!error)
		error = dpm_suspend(state);
1095 1096
	return error;
}
1097
EXPORT_SYMBOL_GPL(dpm_suspend_start);
1098 1099 1100

void __suspend_report_result(const char *function, void *fn, int ret)
{
1101 1102
	if (ret)
		printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
1103 1104
}
EXPORT_SYMBOL_GPL(__suspend_report_result);
1105 1106 1107 1108 1109 1110

/**
 * 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.
 */
1111
int device_pm_wait_for_dev(struct device *subordinate, struct device *dev)
1112 1113
{
	dpm_wait(dev, subordinate->power.async_suspend);
1114
	return async_error;
1115 1116
}
EXPORT_SYMBOL_GPL(device_pm_wait_for_dev);