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