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.set_active 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->power.set_active) { 665 pm_runtime_set_active(dev); 666 dev->power.set_active = false; 667 } 668 669 if (dev->pm_domain) { 670 info = "noirq power domain "; 671 callback = pm_noirq_op(&dev->pm_domain->ops, state); 672 } else if (dev->type && dev->type->pm) { 673 info = "noirq type "; 674 callback = pm_noirq_op(dev->type->pm, state); 675 } else if (dev->class && dev->class->pm) { 676 info = "noirq class "; 677 callback = pm_noirq_op(dev->class->pm, state); 678 } else if (dev->bus && dev->bus->pm) { 679 info = "noirq bus "; 680 callback = pm_noirq_op(dev->bus->pm, state); 681 } 682 if (callback) 683 goto Run; 684 685 if (skip_resume) 686 goto Skip; 687 688 if (dev->driver && dev->driver->pm) { 689 info = "noirq driver "; 690 callback = pm_noirq_op(dev->driver->pm, state); 691 } 692 693 Run: 694 error = dpm_run_callback(callback, dev, state, info); 695 696 Skip: 697 dev->power.is_noirq_suspended = false; 698 699 Out: 700 complete_all(&dev->power.completion); 701 TRACE_RESUME(error); 702 703 if (error) { 704 async_error = error; 705 dpm_save_failed_dev(dev_name(dev)); 706 pm_dev_err(dev, state, async ? " async noirq" : " noirq", error); 707 } 708 } 709 710 static void async_resume_noirq(void *data, async_cookie_t cookie) 711 { 712 struct device *dev = data; 713 714 device_resume_noirq(dev, pm_transition, true); 715 put_device(dev); 716 } 717 718 static void dpm_noirq_resume_devices(pm_message_t state) 719 { 720 struct device *dev; 721 ktime_t starttime = ktime_get(); 722 723 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, true); 724 725 async_error = 0; 726 pm_transition = state; 727 728 mutex_lock(&dpm_list_mtx); 729 730 /* 731 * Trigger the resume of "async" devices upfront so they don't have to 732 * wait for the "non-async" ones they don't depend on. 733 */ 734 list_for_each_entry(dev, &dpm_noirq_list, power.entry) 735 dpm_async_fn(dev, async_resume_noirq); 736 737 while (!list_empty(&dpm_noirq_list)) { 738 dev = to_device(dpm_noirq_list.next); 739 list_move_tail(&dev->power.entry, &dpm_late_early_list); 740 741 if (!dev->power.async_in_progress) { 742 get_device(dev); 743 744 mutex_unlock(&dpm_list_mtx); 745 746 device_resume_noirq(dev, state, false); 747 748 put_device(dev); 749 750 mutex_lock(&dpm_list_mtx); 751 } 752 } 753 mutex_unlock(&dpm_list_mtx); 754 async_synchronize_full(); 755 dpm_show_time(starttime, state, 0, "noirq"); 756 if (async_error) 757 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ); 758 759 trace_suspend_resume(TPS("dpm_resume_noirq"), state.event, false); 760 } 761 762 /** 763 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices. 764 * @state: PM transition of the system being carried out. 765 * 766 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and 767 * allow device drivers' interrupt handlers to be called. 768 */ 769 void dpm_resume_noirq(pm_message_t state) 770 { 771 dpm_noirq_resume_devices(state); 772 773 resume_device_irqs(); 774 device_wakeup_disarm_wake_irqs(); 775 } 776 777 /** 778 * device_resume_early - Execute an "early resume" callback for given device. 779 * @dev: Device to handle. 780 * @state: PM transition of the system being carried out. 781 * @async: If true, the device is being resumed asynchronously. 782 * 783 * Runtime PM is disabled for @dev while this function is being executed. 784 */ 785 static void device_resume_early(struct device *dev, pm_message_t state, bool async) 786 { 787 pm_callback_t callback = NULL; 788 const char *info = NULL; 789 int error = 0; 790 791 TRACE_DEVICE(dev); 792 TRACE_RESUME(0); 793 794 if (dev->power.syscore || dev->power.direct_complete) 795 goto Out; 796 797 if (!dev->power.is_late_suspended) 798 goto Out; 799 800 if (!dpm_wait_for_superior(dev, async)) 801 goto Out; 802 803 if (dev->pm_domain) { 804 info = "early power domain "; 805 callback = pm_late_early_op(&dev->pm_domain->ops, state); 806 } else if (dev->type && dev->type->pm) { 807 info = "early type "; 808 callback = pm_late_early_op(dev->type->pm, state); 809 } else if (dev->class && dev->class->pm) { 810 info = "early class "; 811 callback = pm_late_early_op(dev->class->pm, state); 812 } else if (dev->bus && dev->bus->pm) { 813 info = "early bus "; 814 callback = pm_late_early_op(dev->bus->pm, state); 815 } 816 if (callback) 817 goto Run; 818 819 if (dev_pm_skip_resume(dev)) 820 goto Skip; 821 822 if (dev->driver && dev->driver->pm) { 823 info = "early driver "; 824 callback = pm_late_early_op(dev->driver->pm, state); 825 } 826 827 Run: 828 error = dpm_run_callback(callback, dev, state, info); 829 830 Skip: 831 dev->power.is_late_suspended = false; 832 833 Out: 834 TRACE_RESUME(error); 835 836 pm_runtime_enable(dev); 837 complete_all(&dev->power.completion); 838 839 if (error) { 840 async_error = error; 841 dpm_save_failed_dev(dev_name(dev)); 842 pm_dev_err(dev, state, async ? " async early" : " early", error); 843 } 844 } 845 846 static void async_resume_early(void *data, async_cookie_t cookie) 847 { 848 struct device *dev = data; 849 850 device_resume_early(dev, pm_transition, true); 851 put_device(dev); 852 } 853 854 /** 855 * dpm_resume_early - Execute "early resume" callbacks for all devices. 856 * @state: PM transition of the system being carried out. 857 */ 858 void dpm_resume_early(pm_message_t state) 859 { 860 struct device *dev; 861 ktime_t starttime = ktime_get(); 862 863 trace_suspend_resume(TPS("dpm_resume_early"), state.event, true); 864 865 async_error = 0; 866 pm_transition = state; 867 868 mutex_lock(&dpm_list_mtx); 869 870 /* 871 * Trigger the resume of "async" devices upfront so they don't have to 872 * wait for the "non-async" ones they don't depend on. 873 */ 874 list_for_each_entry(dev, &dpm_late_early_list, power.entry) 875 dpm_async_fn(dev, async_resume_early); 876 877 while (!list_empty(&dpm_late_early_list)) { 878 dev = to_device(dpm_late_early_list.next); 879 list_move_tail(&dev->power.entry, &dpm_suspended_list); 880 881 if (!dev->power.async_in_progress) { 882 get_device(dev); 883 884 mutex_unlock(&dpm_list_mtx); 885 886 device_resume_early(dev, state, false); 887 888 put_device(dev); 889 890 mutex_lock(&dpm_list_mtx); 891 } 892 } 893 mutex_unlock(&dpm_list_mtx); 894 async_synchronize_full(); 895 dpm_show_time(starttime, state, 0, "early"); 896 if (async_error) 897 dpm_save_failed_step(SUSPEND_RESUME_EARLY); 898 899 trace_suspend_resume(TPS("dpm_resume_early"), state.event, false); 900 } 901 902 /** 903 * dpm_resume_start - Execute "noirq" and "early" device callbacks. 904 * @state: PM transition of the system being carried out. 905 */ 906 void dpm_resume_start(pm_message_t state) 907 { 908 dpm_resume_noirq(state); 909 dpm_resume_early(state); 910 } 911 EXPORT_SYMBOL_GPL(dpm_resume_start); 912 913 /** 914 * device_resume - Execute "resume" callbacks for given device. 915 * @dev: Device to handle. 916 * @state: PM transition of the system being carried out. 917 * @async: If true, the device is being resumed asynchronously. 918 */ 919 static void device_resume(struct device *dev, pm_message_t state, bool async) 920 { 921 pm_callback_t callback = NULL; 922 const char *info = NULL; 923 int error = 0; 924 DECLARE_DPM_WATCHDOG_ON_STACK(wd); 925 926 TRACE_DEVICE(dev); 927 TRACE_RESUME(0); 928 929 if (dev->power.syscore) 930 goto Complete; 931 932 if (dev->power.direct_complete) { 933 /* Match the pm_runtime_disable() in device_suspend(). */ 934 pm_runtime_enable(dev); 935 goto Complete; 936 } 937 938 if (!dpm_wait_for_superior(dev, async)) 939 goto Complete; 940 941 dpm_watchdog_set(&wd, dev); 942 device_lock(dev); 943 944 /* 945 * This is a fib. But we'll allow new children to be added below 946 * a resumed device, even if the device hasn't been completed yet. 947 */ 948 dev->power.is_prepared = false; 949 950 if (!dev->power.is_suspended) 951 goto Unlock; 952 953 if (dev->pm_domain) { 954 info = "power domain "; 955 callback = pm_op(&dev->pm_domain->ops, state); 956 goto Driver; 957 } 958 959 if (dev->type && dev->type->pm) { 960 info = "type "; 961 callback = pm_op(dev->type->pm, state); 962 goto Driver; 963 } 964 965 if (dev->class && dev->class->pm) { 966 info = "class "; 967 callback = pm_op(dev->class->pm, state); 968 goto Driver; 969 } 970 971 if (dev->bus) { 972 if (dev->bus->pm) { 973 info = "bus "; 974 callback = pm_op(dev->bus->pm, state); 975 } else if (dev->bus->resume) { 976 info = "legacy bus "; 977 callback = dev->bus->resume; 978 goto End; 979 } 980 } 981 982 Driver: 983 if (!callback && dev->driver && dev->driver->pm) { 984 info = "driver "; 985 callback = pm_op(dev->driver->pm, state); 986 } 987 988 End: 989 error = dpm_run_callback(callback, dev, state, info); 990 dev->power.is_suspended = false; 991 992 Unlock: 993 device_unlock(dev); 994 dpm_watchdog_clear(&wd); 995 996 Complete: 997 complete_all(&dev->power.completion); 998 999 TRACE_RESUME(error); 1000 1001 if (error) { 1002 async_error = error; 1003 dpm_save_failed_dev(dev_name(dev)); 1004 pm_dev_err(dev, state, async ? " async" : "", error); 1005 } 1006 } 1007 1008 static void async_resume(void *data, async_cookie_t cookie) 1009 { 1010 struct device *dev = data; 1011 1012 device_resume(dev, pm_transition, true); 1013 put_device(dev); 1014 } 1015 1016 /** 1017 * dpm_resume - Execute "resume" callbacks for non-sysdev devices. 1018 * @state: PM transition of the system being carried out. 1019 * 1020 * Execute the appropriate "resume" callback for all devices whose status 1021 * indicates that they are suspended. 1022 */ 1023 void dpm_resume(pm_message_t state) 1024 { 1025 struct device *dev; 1026 ktime_t starttime = ktime_get(); 1027 1028 trace_suspend_resume(TPS("dpm_resume"), state.event, true); 1029 might_sleep(); 1030 1031 pm_transition = state; 1032 async_error = 0; 1033 1034 mutex_lock(&dpm_list_mtx); 1035 1036 /* 1037 * Trigger the resume of "async" devices upfront so they don't have to 1038 * wait for the "non-async" ones they don't depend on. 1039 */ 1040 list_for_each_entry(dev, &dpm_suspended_list, power.entry) 1041 dpm_async_fn(dev, async_resume); 1042 1043 while (!list_empty(&dpm_suspended_list)) { 1044 dev = to_device(dpm_suspended_list.next); 1045 list_move_tail(&dev->power.entry, &dpm_prepared_list); 1046 1047 if (!dev->power.async_in_progress) { 1048 get_device(dev); 1049 1050 mutex_unlock(&dpm_list_mtx); 1051 1052 device_resume(dev, state, false); 1053 1054 put_device(dev); 1055 1056 mutex_lock(&dpm_list_mtx); 1057 } 1058 } 1059 mutex_unlock(&dpm_list_mtx); 1060 async_synchronize_full(); 1061 dpm_show_time(starttime, state, 0, NULL); 1062 if (async_error) 1063 dpm_save_failed_step(SUSPEND_RESUME); 1064 1065 cpufreq_resume(); 1066 devfreq_resume(); 1067 trace_suspend_resume(TPS("dpm_resume"), state.event, false); 1068 } 1069 1070 /** 1071 * device_complete - Complete a PM transition for given device. 1072 * @dev: Device to handle. 1073 * @state: PM transition of the system being carried out. 1074 */ 1075 static void device_complete(struct device *dev, pm_message_t state) 1076 { 1077 void (*callback)(struct device *) = NULL; 1078 const char *info = NULL; 1079 1080 if (dev->power.syscore) 1081 goto out; 1082 1083 device_lock(dev); 1084 1085 if (dev->pm_domain) { 1086 info = "completing power domain "; 1087 callback = dev->pm_domain->ops.complete; 1088 } else if (dev->type && dev->type->pm) { 1089 info = "completing type "; 1090 callback = dev->type->pm->complete; 1091 } else if (dev->class && dev->class->pm) { 1092 info = "completing class "; 1093 callback = dev->class->pm->complete; 1094 } else if (dev->bus && dev->bus->pm) { 1095 info = "completing bus "; 1096 callback = dev->bus->pm->complete; 1097 } 1098 1099 if (!callback && dev->driver && dev->driver->pm) { 1100 info = "completing driver "; 1101 callback = dev->driver->pm->complete; 1102 } 1103 1104 if (callback) { 1105 pm_dev_dbg(dev, state, info); 1106 callback(dev); 1107 } 1108 1109 device_unlock(dev); 1110 1111 out: 1112 /* If enabling runtime PM for the device is blocked, unblock it. */ 1113 pm_runtime_unblock(dev); 1114 pm_runtime_put(dev); 1115 } 1116 1117 /** 1118 * dpm_complete - Complete a PM transition for all non-sysdev devices. 1119 * @state: PM transition of the system being carried out. 1120 * 1121 * Execute the ->complete() callbacks for all devices whose PM status is not 1122 * DPM_ON (this allows new devices to be registered). 1123 */ 1124 void dpm_complete(pm_message_t state) 1125 { 1126 struct list_head list; 1127 1128 trace_suspend_resume(TPS("dpm_complete"), state.event, true); 1129 might_sleep(); 1130 1131 INIT_LIST_HEAD(&list); 1132 mutex_lock(&dpm_list_mtx); 1133 while (!list_empty(&dpm_prepared_list)) { 1134 struct device *dev = to_device(dpm_prepared_list.prev); 1135 1136 get_device(dev); 1137 dev->power.is_prepared = false; 1138 list_move(&dev->power.entry, &list); 1139 1140 mutex_unlock(&dpm_list_mtx); 1141 1142 trace_device_pm_callback_start(dev, "", state.event); 1143 device_complete(dev, state); 1144 trace_device_pm_callback_end(dev, 0); 1145 1146 put_device(dev); 1147 1148 mutex_lock(&dpm_list_mtx); 1149 } 1150 list_splice(&list, &dpm_list); 1151 mutex_unlock(&dpm_list_mtx); 1152 1153 /* Allow device probing and trigger re-probing of deferred devices */ 1154 device_unblock_probing(); 1155 trace_suspend_resume(TPS("dpm_complete"), state.event, false); 1156 } 1157 1158 /** 1159 * dpm_resume_end - Execute "resume" callbacks and complete system transition. 1160 * @state: PM transition of the system being carried out. 1161 * 1162 * Execute "resume" callbacks for all devices and complete the PM transition of 1163 * the system. 1164 */ 1165 void dpm_resume_end(pm_message_t state) 1166 { 1167 dpm_resume(state); 1168 dpm_complete(state); 1169 } 1170 EXPORT_SYMBOL_GPL(dpm_resume_end); 1171 1172 1173 /*------------------------- Suspend routines -------------------------*/ 1174 1175 /** 1176 * resume_event - Return a "resume" message for given "suspend" sleep state. 1177 * @sleep_state: PM message representing a sleep state. 1178 * 1179 * Return a PM message representing the resume event corresponding to given 1180 * sleep state. 1181 */ 1182 static pm_message_t resume_event(pm_message_t sleep_state) 1183 { 1184 switch (sleep_state.event) { 1185 case PM_EVENT_SUSPEND: 1186 return PMSG_RESUME; 1187 case PM_EVENT_FREEZE: 1188 case PM_EVENT_QUIESCE: 1189 return PMSG_RECOVER; 1190 case PM_EVENT_HIBERNATE: 1191 return PMSG_RESTORE; 1192 } 1193 return PMSG_ON; 1194 } 1195 1196 static void dpm_superior_set_must_resume(struct device *dev) 1197 { 1198 struct device_link *link; 1199 int idx; 1200 1201 if (dev->parent) 1202 dev->parent->power.must_resume = true; 1203 1204 idx = device_links_read_lock(); 1205 1206 list_for_each_entry_rcu_locked(link, &dev->links.suppliers, c_node) 1207 link->supplier->power.must_resume = true; 1208 1209 device_links_read_unlock(idx); 1210 } 1211 1212 /** 1213 * device_suspend_noirq - Execute a "noirq suspend" callback for given device. 1214 * @dev: Device to handle. 1215 * @state: PM transition of the system being carried out. 1216 * @async: If true, the device is being suspended asynchronously. 1217 * 1218 * The driver of @dev will not receive interrupts while this function is being 1219 * executed. 1220 */ 1221 static int device_suspend_noirq(struct device *dev, pm_message_t state, bool async) 1222 { 1223 pm_callback_t callback = NULL; 1224 const char *info = NULL; 1225 int error = 0; 1226 1227 TRACE_DEVICE(dev); 1228 TRACE_SUSPEND(0); 1229 1230 dpm_wait_for_subordinate(dev, async); 1231 1232 if (async_error) 1233 goto Complete; 1234 1235 if (dev->power.syscore || dev->power.direct_complete) 1236 goto Complete; 1237 1238 if (dev->pm_domain) { 1239 info = "noirq power domain "; 1240 callback = pm_noirq_op(&dev->pm_domain->ops, state); 1241 } else if (dev->type && dev->type->pm) { 1242 info = "noirq type "; 1243 callback = pm_noirq_op(dev->type->pm, state); 1244 } else if (dev->class && dev->class->pm) { 1245 info = "noirq class "; 1246 callback = pm_noirq_op(dev->class->pm, state); 1247 } else if (dev->bus && dev->bus->pm) { 1248 info = "noirq bus "; 1249 callback = pm_noirq_op(dev->bus->pm, state); 1250 } 1251 if (callback) 1252 goto Run; 1253 1254 if (dev_pm_skip_suspend(dev)) 1255 goto Skip; 1256 1257 if (dev->driver && dev->driver->pm) { 1258 info = "noirq driver "; 1259 callback = pm_noirq_op(dev->driver->pm, state); 1260 } 1261 1262 Run: 1263 error = dpm_run_callback(callback, dev, state, info); 1264 if (error) { 1265 async_error = error; 1266 dpm_save_failed_dev(dev_name(dev)); 1267 pm_dev_err(dev, state, async ? " async noirq" : " noirq", error); 1268 goto Complete; 1269 } 1270 1271 Skip: 1272 dev->power.is_noirq_suspended = true; 1273 1274 /* 1275 * Skipping the resume of devices that were in use right before the 1276 * system suspend (as indicated by their PM-runtime usage counters) 1277 * would be suboptimal. Also resume them if doing that is not allowed 1278 * to be skipped. 1279 */ 1280 if (atomic_read(&dev->power.usage_count) > 1 || 1281 !(dev_pm_test_driver_flags(dev, DPM_FLAG_MAY_SKIP_RESUME) && 1282 dev->power.may_skip_resume)) 1283 dev->power.must_resume = true; 1284 1285 if (dev->power.must_resume) { 1286 if (dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND)) { 1287 dev->power.set_active = true; 1288 if (dev->parent && !dev->parent->power.ignore_children) 1289 dev->parent->power.set_active = true; 1290 } 1291 dpm_superior_set_must_resume(dev); 1292 } 1293 1294 Complete: 1295 complete_all(&dev->power.completion); 1296 TRACE_SUSPEND(error); 1297 return error; 1298 } 1299 1300 static void async_suspend_noirq(void *data, async_cookie_t cookie) 1301 { 1302 struct device *dev = data; 1303 1304 device_suspend_noirq(dev, pm_transition, true); 1305 put_device(dev); 1306 } 1307 1308 static int dpm_noirq_suspend_devices(pm_message_t state) 1309 { 1310 ktime_t starttime = ktime_get(); 1311 int error = 0; 1312 1313 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, true); 1314 1315 pm_transition = state; 1316 async_error = 0; 1317 1318 mutex_lock(&dpm_list_mtx); 1319 1320 while (!list_empty(&dpm_late_early_list)) { 1321 struct device *dev = to_device(dpm_late_early_list.prev); 1322 1323 list_move(&dev->power.entry, &dpm_noirq_list); 1324 1325 if (dpm_async_fn(dev, async_suspend_noirq)) 1326 continue; 1327 1328 get_device(dev); 1329 1330 mutex_unlock(&dpm_list_mtx); 1331 1332 error = device_suspend_noirq(dev, state, false); 1333 1334 put_device(dev); 1335 1336 mutex_lock(&dpm_list_mtx); 1337 1338 if (error || async_error) 1339 break; 1340 } 1341 1342 mutex_unlock(&dpm_list_mtx); 1343 1344 async_synchronize_full(); 1345 if (!error) 1346 error = async_error; 1347 1348 if (error) 1349 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ); 1350 1351 dpm_show_time(starttime, state, error, "noirq"); 1352 trace_suspend_resume(TPS("dpm_suspend_noirq"), state.event, false); 1353 return error; 1354 } 1355 1356 /** 1357 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices. 1358 * @state: PM transition of the system being carried out. 1359 * 1360 * Prevent device drivers' interrupt handlers from being called and invoke 1361 * "noirq" suspend callbacks for all non-sysdev devices. 1362 */ 1363 int dpm_suspend_noirq(pm_message_t state) 1364 { 1365 int ret; 1366 1367 device_wakeup_arm_wake_irqs(); 1368 suspend_device_irqs(); 1369 1370 ret = dpm_noirq_suspend_devices(state); 1371 if (ret) 1372 dpm_resume_noirq(resume_event(state)); 1373 1374 return ret; 1375 } 1376 1377 static void dpm_propagate_wakeup_to_parent(struct device *dev) 1378 { 1379 struct device *parent = dev->parent; 1380 1381 if (!parent) 1382 return; 1383 1384 spin_lock_irq(&parent->power.lock); 1385 1386 if (device_wakeup_path(dev) && !parent->power.ignore_children) 1387 parent->power.wakeup_path = true; 1388 1389 spin_unlock_irq(&parent->power.lock); 1390 } 1391 1392 /** 1393 * device_suspend_late - Execute a "late suspend" callback for given device. 1394 * @dev: Device to handle. 1395 * @state: PM transition of the system being carried out. 1396 * @async: If true, the device is being suspended asynchronously. 1397 * 1398 * Runtime PM is disabled for @dev while this function is being executed. 1399 */ 1400 static int device_suspend_late(struct device *dev, pm_message_t state, bool async) 1401 { 1402 pm_callback_t callback = NULL; 1403 const char *info = NULL; 1404 int error = 0; 1405 1406 TRACE_DEVICE(dev); 1407 TRACE_SUSPEND(0); 1408 1409 /* 1410 * Disable runtime PM for the device without checking if there is a 1411 * pending resume request for it. 1412 */ 1413 __pm_runtime_disable(dev, false); 1414 1415 dpm_wait_for_subordinate(dev, async); 1416 1417 if (async_error) 1418 goto Complete; 1419 1420 if (pm_wakeup_pending()) { 1421 async_error = -EBUSY; 1422 goto Complete; 1423 } 1424 1425 if (dev->power.syscore || dev->power.direct_complete) 1426 goto Complete; 1427 1428 if (dev->pm_domain) { 1429 info = "late power domain "; 1430 callback = pm_late_early_op(&dev->pm_domain->ops, state); 1431 } else if (dev->type && dev->type->pm) { 1432 info = "late type "; 1433 callback = pm_late_early_op(dev->type->pm, state); 1434 } else if (dev->class && dev->class->pm) { 1435 info = "late class "; 1436 callback = pm_late_early_op(dev->class->pm, state); 1437 } else if (dev->bus && dev->bus->pm) { 1438 info = "late bus "; 1439 callback = pm_late_early_op(dev->bus->pm, state); 1440 } 1441 if (callback) 1442 goto Run; 1443 1444 if (dev_pm_skip_suspend(dev)) 1445 goto Skip; 1446 1447 if (dev->driver && dev->driver->pm) { 1448 info = "late driver "; 1449 callback = pm_late_early_op(dev->driver->pm, state); 1450 } 1451 1452 Run: 1453 error = dpm_run_callback(callback, dev, state, info); 1454 if (error) { 1455 async_error = error; 1456 dpm_save_failed_dev(dev_name(dev)); 1457 pm_dev_err(dev, state, async ? " async late" : " late", error); 1458 goto Complete; 1459 } 1460 dpm_propagate_wakeup_to_parent(dev); 1461 1462 Skip: 1463 dev->power.is_late_suspended = true; 1464 1465 Complete: 1466 TRACE_SUSPEND(error); 1467 complete_all(&dev->power.completion); 1468 return error; 1469 } 1470 1471 static void async_suspend_late(void *data, async_cookie_t cookie) 1472 { 1473 struct device *dev = data; 1474 1475 device_suspend_late(dev, pm_transition, true); 1476 put_device(dev); 1477 } 1478 1479 /** 1480 * dpm_suspend_late - Execute "late suspend" callbacks for all devices. 1481 * @state: PM transition of the system being carried out. 1482 */ 1483 int dpm_suspend_late(pm_message_t state) 1484 { 1485 ktime_t starttime = ktime_get(); 1486 int error = 0; 1487 1488 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, true); 1489 1490 pm_transition = state; 1491 async_error = 0; 1492 1493 wake_up_all_idle_cpus(); 1494 1495 mutex_lock(&dpm_list_mtx); 1496 1497 while (!list_empty(&dpm_suspended_list)) { 1498 struct device *dev = to_device(dpm_suspended_list.prev); 1499 1500 list_move(&dev->power.entry, &dpm_late_early_list); 1501 1502 if (dpm_async_fn(dev, async_suspend_late)) 1503 continue; 1504 1505 get_device(dev); 1506 1507 mutex_unlock(&dpm_list_mtx); 1508 1509 error = device_suspend_late(dev, state, false); 1510 1511 put_device(dev); 1512 1513 mutex_lock(&dpm_list_mtx); 1514 1515 if (error || async_error) 1516 break; 1517 } 1518 1519 mutex_unlock(&dpm_list_mtx); 1520 1521 async_synchronize_full(); 1522 if (!error) 1523 error = async_error; 1524 1525 if (error) { 1526 dpm_save_failed_step(SUSPEND_SUSPEND_LATE); 1527 dpm_resume_early(resume_event(state)); 1528 } 1529 dpm_show_time(starttime, state, error, "late"); 1530 trace_suspend_resume(TPS("dpm_suspend_late"), state.event, false); 1531 return error; 1532 } 1533 1534 /** 1535 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks. 1536 * @state: PM transition of the system being carried out. 1537 */ 1538 int dpm_suspend_end(pm_message_t state) 1539 { 1540 ktime_t starttime = ktime_get(); 1541 int error; 1542 1543 error = dpm_suspend_late(state); 1544 if (error) 1545 goto out; 1546 1547 error = dpm_suspend_noirq(state); 1548 if (error) 1549 dpm_resume_early(resume_event(state)); 1550 1551 out: 1552 dpm_show_time(starttime, state, error, "end"); 1553 return error; 1554 } 1555 EXPORT_SYMBOL_GPL(dpm_suspend_end); 1556 1557 /** 1558 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device. 1559 * @dev: Device to suspend. 1560 * @state: PM transition of the system being carried out. 1561 * @cb: Suspend callback to execute. 1562 * @info: string description of caller. 1563 */ 1564 static int legacy_suspend(struct device *dev, pm_message_t state, 1565 int (*cb)(struct device *dev, pm_message_t state), 1566 const char *info) 1567 { 1568 int error; 1569 ktime_t calltime; 1570 1571 calltime = initcall_debug_start(dev, cb); 1572 1573 trace_device_pm_callback_start(dev, info, state.event); 1574 error = cb(dev, state); 1575 trace_device_pm_callback_end(dev, error); 1576 suspend_report_result(dev, cb, error); 1577 1578 initcall_debug_report(dev, calltime, cb, error); 1579 1580 return error; 1581 } 1582 1583 static void dpm_clear_superiors_direct_complete(struct device *dev) 1584 { 1585 struct device_link *link; 1586 int idx; 1587 1588 if (dev->parent) { 1589 spin_lock_irq(&dev->parent->power.lock); 1590 dev->parent->power.direct_complete = false; 1591 spin_unlock_irq(&dev->parent->power.lock); 1592 } 1593 1594 idx = device_links_read_lock(); 1595 1596 list_for_each_entry_rcu_locked(link, &dev->links.suppliers, c_node) { 1597 spin_lock_irq(&link->supplier->power.lock); 1598 link->supplier->power.direct_complete = false; 1599 spin_unlock_irq(&link->supplier->power.lock); 1600 } 1601 1602 device_links_read_unlock(idx); 1603 } 1604 1605 /** 1606 * device_suspend - Execute "suspend" callbacks for given device. 1607 * @dev: Device to handle. 1608 * @state: PM transition of the system being carried out. 1609 * @async: If true, the device is being suspended asynchronously. 1610 */ 1611 static int device_suspend(struct device *dev, pm_message_t state, bool async) 1612 { 1613 pm_callback_t callback = NULL; 1614 const char *info = NULL; 1615 int error = 0; 1616 DECLARE_DPM_WATCHDOG_ON_STACK(wd); 1617 1618 TRACE_DEVICE(dev); 1619 TRACE_SUSPEND(0); 1620 1621 dpm_wait_for_subordinate(dev, async); 1622 1623 if (async_error) { 1624 dev->power.direct_complete = false; 1625 goto Complete; 1626 } 1627 1628 /* 1629 * Wait for possible runtime PM transitions of the device in progress 1630 * to complete and if there's a runtime resume request pending for it, 1631 * resume it before proceeding with invoking the system-wide suspend 1632 * callbacks for it. 1633 * 1634 * If the system-wide suspend callbacks below change the configuration 1635 * of the device, they must disable runtime PM for it or otherwise 1636 * ensure that its runtime-resume callbacks will not be confused by that 1637 * change in case they are invoked going forward. 1638 */ 1639 pm_runtime_barrier(dev); 1640 1641 if (pm_wakeup_pending()) { 1642 dev->power.direct_complete = false; 1643 async_error = -EBUSY; 1644 goto Complete; 1645 } 1646 1647 if (dev->power.syscore) 1648 goto Complete; 1649 1650 /* Avoid direct_complete to let wakeup_path propagate. */ 1651 if (device_may_wakeup(dev) || device_wakeup_path(dev)) 1652 dev->power.direct_complete = false; 1653 1654 if (dev->power.direct_complete) { 1655 if (pm_runtime_status_suspended(dev)) { 1656 pm_runtime_disable(dev); 1657 if (pm_runtime_status_suspended(dev)) { 1658 pm_dev_dbg(dev, state, "direct-complete "); 1659 goto Complete; 1660 } 1661 1662 pm_runtime_enable(dev); 1663 } 1664 dev->power.direct_complete = false; 1665 } 1666 1667 dev->power.may_skip_resume = true; 1668 dev->power.must_resume = !dev_pm_test_driver_flags(dev, DPM_FLAG_MAY_SKIP_RESUME); 1669 1670 dpm_watchdog_set(&wd, dev); 1671 device_lock(dev); 1672 1673 if (dev->pm_domain) { 1674 info = "power domain "; 1675 callback = pm_op(&dev->pm_domain->ops, state); 1676 goto Run; 1677 } 1678 1679 if (dev->type && dev->type->pm) { 1680 info = "type "; 1681 callback = pm_op(dev->type->pm, state); 1682 goto Run; 1683 } 1684 1685 if (dev->class && dev->class->pm) { 1686 info = "class "; 1687 callback = pm_op(dev->class->pm, state); 1688 goto Run; 1689 } 1690 1691 if (dev->bus) { 1692 if (dev->bus->pm) { 1693 info = "bus "; 1694 callback = pm_op(dev->bus->pm, state); 1695 } else if (dev->bus->suspend) { 1696 pm_dev_dbg(dev, state, "legacy bus "); 1697 error = legacy_suspend(dev, state, dev->bus->suspend, 1698 "legacy bus "); 1699 goto End; 1700 } 1701 } 1702 1703 Run: 1704 if (!callback && dev->driver && dev->driver->pm) { 1705 info = "driver "; 1706 callback = pm_op(dev->driver->pm, state); 1707 } 1708 1709 error = dpm_run_callback(callback, dev, state, info); 1710 1711 End: 1712 if (!error) { 1713 dev->power.is_suspended = true; 1714 if (device_may_wakeup(dev)) 1715 dev->power.wakeup_path = true; 1716 1717 dpm_propagate_wakeup_to_parent(dev); 1718 dpm_clear_superiors_direct_complete(dev); 1719 } 1720 1721 device_unlock(dev); 1722 dpm_watchdog_clear(&wd); 1723 1724 Complete: 1725 if (error) { 1726 async_error = error; 1727 dpm_save_failed_dev(dev_name(dev)); 1728 pm_dev_err(dev, state, async ? " async" : "", error); 1729 } 1730 1731 complete_all(&dev->power.completion); 1732 TRACE_SUSPEND(error); 1733 return error; 1734 } 1735 1736 static void async_suspend(void *data, async_cookie_t cookie) 1737 { 1738 struct device *dev = data; 1739 1740 device_suspend(dev, pm_transition, true); 1741 put_device(dev); 1742 } 1743 1744 /** 1745 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices. 1746 * @state: PM transition of the system being carried out. 1747 */ 1748 int dpm_suspend(pm_message_t state) 1749 { 1750 ktime_t starttime = ktime_get(); 1751 int error = 0; 1752 1753 trace_suspend_resume(TPS("dpm_suspend"), state.event, true); 1754 might_sleep(); 1755 1756 devfreq_suspend(); 1757 cpufreq_suspend(); 1758 1759 pm_transition = state; 1760 async_error = 0; 1761 1762 mutex_lock(&dpm_list_mtx); 1763 1764 while (!list_empty(&dpm_prepared_list)) { 1765 struct device *dev = to_device(dpm_prepared_list.prev); 1766 1767 list_move(&dev->power.entry, &dpm_suspended_list); 1768 1769 if (dpm_async_fn(dev, async_suspend)) 1770 continue; 1771 1772 get_device(dev); 1773 1774 mutex_unlock(&dpm_list_mtx); 1775 1776 error = device_suspend(dev, state, false); 1777 1778 put_device(dev); 1779 1780 mutex_lock(&dpm_list_mtx); 1781 1782 if (error || async_error) 1783 break; 1784 } 1785 1786 mutex_unlock(&dpm_list_mtx); 1787 1788 async_synchronize_full(); 1789 if (!error) 1790 error = async_error; 1791 1792 if (error) 1793 dpm_save_failed_step(SUSPEND_SUSPEND); 1794 1795 dpm_show_time(starttime, state, error, NULL); 1796 trace_suspend_resume(TPS("dpm_suspend"), state.event, false); 1797 return error; 1798 } 1799 1800 /** 1801 * device_prepare - Prepare a device for system power transition. 1802 * @dev: Device to handle. 1803 * @state: PM transition of the system being carried out. 1804 * 1805 * Execute the ->prepare() callback(s) for given device. No new children of the 1806 * device may be registered after this function has returned. 1807 */ 1808 static int device_prepare(struct device *dev, pm_message_t state) 1809 { 1810 int (*callback)(struct device *) = NULL; 1811 int ret = 0; 1812 1813 /* 1814 * If a device's parent goes into runtime suspend at the wrong time, 1815 * it won't be possible to resume the device. To prevent this we 1816 * block runtime suspend here, during the prepare phase, and allow 1817 * it again during the complete phase. 1818 */ 1819 pm_runtime_get_noresume(dev); 1820 /* 1821 * If runtime PM is disabled for the device at this point and it has 1822 * never been enabled so far, it should not be enabled until this system 1823 * suspend-resume cycle is complete, so prepare to trigger a warning on 1824 * subsequent attempts to enable it. 1825 */ 1826 pm_runtime_block_if_disabled(dev); 1827 1828 if (dev->power.syscore) 1829 return 0; 1830 1831 device_lock(dev); 1832 1833 dev->power.wakeup_path = false; 1834 1835 if (dev->power.no_pm_callbacks) 1836 goto unlock; 1837 1838 if (dev->pm_domain) 1839 callback = dev->pm_domain->ops.prepare; 1840 else if (dev->type && dev->type->pm) 1841 callback = dev->type->pm->prepare; 1842 else if (dev->class && dev->class->pm) 1843 callback = dev->class->pm->prepare; 1844 else if (dev->bus && dev->bus->pm) 1845 callback = dev->bus->pm->prepare; 1846 1847 if (!callback && dev->driver && dev->driver->pm) 1848 callback = dev->driver->pm->prepare; 1849 1850 if (callback) 1851 ret = callback(dev); 1852 1853 unlock: 1854 device_unlock(dev); 1855 1856 if (ret < 0) { 1857 suspend_report_result(dev, callback, ret); 1858 pm_runtime_put(dev); 1859 return ret; 1860 } 1861 /* 1862 * A positive return value from ->prepare() means "this device appears 1863 * to be runtime-suspended and its state is fine, so if it really is 1864 * runtime-suspended, you can leave it in that state provided that you 1865 * will do the same thing with all of its descendants". This only 1866 * applies to suspend transitions, however. 1867 */ 1868 spin_lock_irq(&dev->power.lock); 1869 dev->power.direct_complete = state.event == PM_EVENT_SUSPEND && 1870 (ret > 0 || dev->power.no_pm_callbacks) && 1871 !dev_pm_test_driver_flags(dev, DPM_FLAG_NO_DIRECT_COMPLETE); 1872 spin_unlock_irq(&dev->power.lock); 1873 return 0; 1874 } 1875 1876 /** 1877 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition. 1878 * @state: PM transition of the system being carried out. 1879 * 1880 * Execute the ->prepare() callback(s) for all devices. 1881 */ 1882 int dpm_prepare(pm_message_t state) 1883 { 1884 int error = 0; 1885 1886 trace_suspend_resume(TPS("dpm_prepare"), state.event, true); 1887 might_sleep(); 1888 1889 /* 1890 * Give a chance for the known devices to complete their probes, before 1891 * disable probing of devices. This sync point is important at least 1892 * at boot time + hibernation restore. 1893 */ 1894 wait_for_device_probe(); 1895 /* 1896 * It is unsafe if probing of devices will happen during suspend or 1897 * hibernation and system behavior will be unpredictable in this case. 1898 * So, let's prohibit device's probing here and defer their probes 1899 * instead. The normal behavior will be restored in dpm_complete(). 1900 */ 1901 device_block_probing(); 1902 1903 mutex_lock(&dpm_list_mtx); 1904 while (!list_empty(&dpm_list) && !error) { 1905 struct device *dev = to_device(dpm_list.next); 1906 1907 get_device(dev); 1908 1909 mutex_unlock(&dpm_list_mtx); 1910 1911 trace_device_pm_callback_start(dev, "", state.event); 1912 error = device_prepare(dev, state); 1913 trace_device_pm_callback_end(dev, error); 1914 1915 mutex_lock(&dpm_list_mtx); 1916 1917 if (!error) { 1918 dev->power.is_prepared = true; 1919 if (!list_empty(&dev->power.entry)) 1920 list_move_tail(&dev->power.entry, &dpm_prepared_list); 1921 } else if (error == -EAGAIN) { 1922 error = 0; 1923 } else { 1924 dev_info(dev, "not prepared for power transition: code %d\n", 1925 error); 1926 } 1927 1928 mutex_unlock(&dpm_list_mtx); 1929 1930 put_device(dev); 1931 1932 mutex_lock(&dpm_list_mtx); 1933 } 1934 mutex_unlock(&dpm_list_mtx); 1935 trace_suspend_resume(TPS("dpm_prepare"), state.event, false); 1936 return error; 1937 } 1938 1939 /** 1940 * dpm_suspend_start - Prepare devices for PM transition and suspend them. 1941 * @state: PM transition of the system being carried out. 1942 * 1943 * Prepare all non-sysdev devices for system PM transition and execute "suspend" 1944 * callbacks for them. 1945 */ 1946 int dpm_suspend_start(pm_message_t state) 1947 { 1948 ktime_t starttime = ktime_get(); 1949 int error; 1950 1951 error = dpm_prepare(state); 1952 if (error) 1953 dpm_save_failed_step(SUSPEND_PREPARE); 1954 else 1955 error = dpm_suspend(state); 1956 1957 dpm_show_time(starttime, state, error, "start"); 1958 return error; 1959 } 1960 EXPORT_SYMBOL_GPL(dpm_suspend_start); 1961 1962 void __suspend_report_result(const char *function, struct device *dev, void *fn, int ret) 1963 { 1964 if (ret) 1965 dev_err(dev, "%s(): %ps returns %d\n", function, fn, ret); 1966 } 1967 EXPORT_SYMBOL_GPL(__suspend_report_result); 1968 1969 /** 1970 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete. 1971 * @subordinate: Device that needs to wait for @dev. 1972 * @dev: Device to wait for. 1973 */ 1974 int device_pm_wait_for_dev(struct device *subordinate, struct device *dev) 1975 { 1976 dpm_wait(dev, subordinate->power.async_suspend); 1977 return async_error; 1978 } 1979 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev); 1980 1981 /** 1982 * dpm_for_each_dev - device iterator. 1983 * @data: data for the callback. 1984 * @fn: function to be called for each device. 1985 * 1986 * Iterate over devices in dpm_list, and call @fn for each device, 1987 * passing it @data. 1988 */ 1989 void dpm_for_each_dev(void *data, void (*fn)(struct device *, void *)) 1990 { 1991 struct device *dev; 1992 1993 if (!fn) 1994 return; 1995 1996 device_pm_lock(); 1997 list_for_each_entry(dev, &dpm_list, power.entry) 1998 fn(dev, data); 1999 device_pm_unlock(); 2000 } 2001 EXPORT_SYMBOL_GPL(dpm_for_each_dev); 2002 2003 static bool pm_ops_is_empty(const struct dev_pm_ops *ops) 2004 { 2005 if (!ops) 2006 return true; 2007 2008 return !ops->prepare && 2009 !ops->suspend && 2010 !ops->suspend_late && 2011 !ops->suspend_noirq && 2012 !ops->resume_noirq && 2013 !ops->resume_early && 2014 !ops->resume && 2015 !ops->complete; 2016 } 2017 2018 void device_pm_check_callbacks(struct device *dev) 2019 { 2020 unsigned long flags; 2021 2022 spin_lock_irqsave(&dev->power.lock, flags); 2023 dev->power.no_pm_callbacks = 2024 (!dev->bus || (pm_ops_is_empty(dev->bus->pm) && 2025 !dev->bus->suspend && !dev->bus->resume)) && 2026 (!dev->class || pm_ops_is_empty(dev->class->pm)) && 2027 (!dev->type || pm_ops_is_empty(dev->type->pm)) && 2028 (!dev->pm_domain || pm_ops_is_empty(&dev->pm_domain->ops)) && 2029 (!dev->driver || (pm_ops_is_empty(dev->driver->pm) && 2030 !dev->driver->suspend && !dev->driver->resume)); 2031 spin_unlock_irqrestore(&dev->power.lock, flags); 2032 } 2033 2034 bool dev_pm_skip_suspend(struct device *dev) 2035 { 2036 return dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) && 2037 pm_runtime_status_suspended(dev); 2038 } 2039