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