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