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