1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * drivers/base/power/runtime.c - Helper functions for device runtime PM 4 * 5 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. 6 * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu> 7 */ 8 #include <linux/sched/mm.h> 9 #include <linux/ktime.h> 10 #include <linux/hrtimer.h> 11 #include <linux/export.h> 12 #include <linux/pm_runtime.h> 13 #include <linux/pm_wakeirq.h> 14 #include <linux/rculist.h> 15 #include <trace/events/rpm.h> 16 17 #include "../base.h" 18 #include "power.h" 19 20 typedef int (*pm_callback_t)(struct device *); 21 22 static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset) 23 { 24 pm_callback_t cb; 25 const struct dev_pm_ops *ops; 26 27 if (dev->pm_domain) 28 ops = &dev->pm_domain->ops; 29 else if (dev->type && dev->type->pm) 30 ops = dev->type->pm; 31 else if (dev->class && dev->class->pm) 32 ops = dev->class->pm; 33 else if (dev->bus && dev->bus->pm) 34 ops = dev->bus->pm; 35 else 36 ops = NULL; 37 38 if (ops) 39 cb = *(pm_callback_t *)((void *)ops + cb_offset); 40 else 41 cb = NULL; 42 43 if (!cb && dev->driver && dev->driver->pm) 44 cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset); 45 46 return cb; 47 } 48 49 #define RPM_GET_CALLBACK(dev, callback) \ 50 __rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback)) 51 52 static int rpm_resume(struct device *dev, int rpmflags); 53 static int rpm_suspend(struct device *dev, int rpmflags); 54 55 /** 56 * update_pm_runtime_accounting - Update the time accounting of power states 57 * @dev: Device to update the accounting for 58 * 59 * In order to be able to have time accounting of the various power states 60 * (as used by programs such as PowerTOP to show the effectiveness of runtime 61 * PM), we need to track the time spent in each state. 62 * update_pm_runtime_accounting must be called each time before the 63 * runtime_status field is updated, to account the time in the old state 64 * correctly. 65 */ 66 static void update_pm_runtime_accounting(struct device *dev) 67 { 68 u64 now, last, delta; 69 70 if (dev->power.disable_depth > 0) 71 return; 72 73 last = dev->power.accounting_timestamp; 74 75 now = ktime_get_mono_fast_ns(); 76 dev->power.accounting_timestamp = now; 77 78 /* 79 * Because ktime_get_mono_fast_ns() is not monotonic during 80 * timekeeping updates, ensure that 'now' is after the last saved 81 * timesptamp. 82 */ 83 if (now < last) 84 return; 85 86 delta = now - last; 87 88 if (dev->power.runtime_status == RPM_SUSPENDED) 89 dev->power.suspended_time += delta; 90 else 91 dev->power.active_time += delta; 92 } 93 94 static void __update_runtime_status(struct device *dev, enum rpm_status status) 95 { 96 update_pm_runtime_accounting(dev); 97 trace_rpm_status(dev, status); 98 dev->power.runtime_status = status; 99 } 100 101 static u64 rpm_get_accounted_time(struct device *dev, bool suspended) 102 { 103 u64 time; 104 unsigned long flags; 105 106 spin_lock_irqsave(&dev->power.lock, flags); 107 108 update_pm_runtime_accounting(dev); 109 time = suspended ? dev->power.suspended_time : dev->power.active_time; 110 111 spin_unlock_irqrestore(&dev->power.lock, flags); 112 113 return time; 114 } 115 116 u64 pm_runtime_active_time(struct device *dev) 117 { 118 return rpm_get_accounted_time(dev, false); 119 } 120 121 u64 pm_runtime_suspended_time(struct device *dev) 122 { 123 return rpm_get_accounted_time(dev, true); 124 } 125 EXPORT_SYMBOL_GPL(pm_runtime_suspended_time); 126 127 /** 128 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer. 129 * @dev: Device to handle. 130 */ 131 static void pm_runtime_deactivate_timer(struct device *dev) 132 { 133 if (dev->power.timer_expires > 0) { 134 hrtimer_try_to_cancel(&dev->power.suspend_timer); 135 dev->power.timer_expires = 0; 136 } 137 } 138 139 /** 140 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests. 141 * @dev: Device to handle. 142 */ 143 static void pm_runtime_cancel_pending(struct device *dev) 144 { 145 pm_runtime_deactivate_timer(dev); 146 /* 147 * In case there's a request pending, make sure its work function will 148 * return without doing anything. 149 */ 150 dev->power.request = RPM_REQ_NONE; 151 } 152 153 /* 154 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time. 155 * @dev: Device to handle. 156 * 157 * Compute the autosuspend-delay expiration time based on the device's 158 * power.last_busy time. If the delay has already expired or is disabled 159 * (negative) or the power.use_autosuspend flag isn't set, return 0. 160 * Otherwise return the expiration time in nanoseconds (adjusted to be nonzero). 161 * 162 * This function may be called either with or without dev->power.lock held. 163 * Either way it can be racy, since power.last_busy may be updated at any time. 164 */ 165 u64 pm_runtime_autosuspend_expiration(struct device *dev) 166 { 167 int autosuspend_delay; 168 u64 expires; 169 170 if (!dev->power.use_autosuspend) 171 return 0; 172 173 autosuspend_delay = READ_ONCE(dev->power.autosuspend_delay); 174 if (autosuspend_delay < 0) 175 return 0; 176 177 expires = READ_ONCE(dev->power.last_busy); 178 expires += (u64)autosuspend_delay * NSEC_PER_MSEC; 179 if (expires > ktime_get_mono_fast_ns()) 180 return expires; /* Expires in the future */ 181 182 return 0; 183 } 184 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration); 185 186 static int dev_memalloc_noio(struct device *dev, void *data) 187 { 188 return dev->power.memalloc_noio; 189 } 190 191 /* 192 * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag. 193 * @dev: Device to handle. 194 * @enable: True for setting the flag and False for clearing the flag. 195 * 196 * Set the flag for all devices in the path from the device to the 197 * root device in the device tree if @enable is true, otherwise clear 198 * the flag for devices in the path whose siblings don't set the flag. 199 * 200 * The function should only be called by block device, or network 201 * device driver for solving the deadlock problem during runtime 202 * resume/suspend: 203 * 204 * If memory allocation with GFP_KERNEL is called inside runtime 205 * resume/suspend callback of any one of its ancestors(or the 206 * block device itself), the deadlock may be triggered inside the 207 * memory allocation since it might not complete until the block 208 * device becomes active and the involed page I/O finishes. The 209 * situation is pointed out first by Alan Stern. Network device 210 * are involved in iSCSI kind of situation. 211 * 212 * The lock of dev_hotplug_mutex is held in the function for handling 213 * hotplug race because pm_runtime_set_memalloc_noio() may be called 214 * in async probe(). 215 * 216 * The function should be called between device_add() and device_del() 217 * on the affected device(block/network device). 218 */ 219 void pm_runtime_set_memalloc_noio(struct device *dev, bool enable) 220 { 221 static DEFINE_MUTEX(dev_hotplug_mutex); 222 223 mutex_lock(&dev_hotplug_mutex); 224 for (;;) { 225 bool enabled; 226 227 /* hold power lock since bitfield is not SMP-safe. */ 228 spin_lock_irq(&dev->power.lock); 229 enabled = dev->power.memalloc_noio; 230 dev->power.memalloc_noio = enable; 231 spin_unlock_irq(&dev->power.lock); 232 233 /* 234 * not need to enable ancestors any more if the device 235 * has been enabled. 236 */ 237 if (enabled && enable) 238 break; 239 240 dev = dev->parent; 241 242 /* 243 * clear flag of the parent device only if all the 244 * children don't set the flag because ancestor's 245 * flag was set by any one of the descendants. 246 */ 247 if (!dev || (!enable && 248 device_for_each_child(dev, NULL, dev_memalloc_noio))) 249 break; 250 } 251 mutex_unlock(&dev_hotplug_mutex); 252 } 253 EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio); 254 255 /** 256 * rpm_check_suspend_allowed - Test whether a device may be suspended. 257 * @dev: Device to test. 258 */ 259 static int rpm_check_suspend_allowed(struct device *dev) 260 { 261 int retval = 0; 262 263 if (dev->power.runtime_error) 264 retval = -EINVAL; 265 else if (dev->power.disable_depth > 0) 266 retval = -EACCES; 267 else if (atomic_read(&dev->power.usage_count)) 268 retval = -EAGAIN; 269 else if (!dev->power.ignore_children && atomic_read(&dev->power.child_count)) 270 retval = -EBUSY; 271 272 /* Pending resume requests take precedence over suspends. */ 273 else if ((dev->power.deferred_resume && 274 dev->power.runtime_status == RPM_SUSPENDING) || 275 (dev->power.request_pending && dev->power.request == RPM_REQ_RESUME)) 276 retval = -EAGAIN; 277 else if (__dev_pm_qos_resume_latency(dev) == 0) 278 retval = -EPERM; 279 else if (dev->power.runtime_status == RPM_SUSPENDED) 280 retval = 1; 281 282 return retval; 283 } 284 285 static int rpm_get_suppliers(struct device *dev) 286 { 287 struct device_link *link; 288 289 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, 290 device_links_read_lock_held()) { 291 int retval; 292 293 if (!(link->flags & DL_FLAG_PM_RUNTIME)) 294 continue; 295 296 retval = pm_runtime_get_sync(link->supplier); 297 /* Ignore suppliers with disabled runtime PM. */ 298 if (retval < 0 && retval != -EACCES) { 299 pm_runtime_put_noidle(link->supplier); 300 return retval; 301 } 302 refcount_inc(&link->rpm_active); 303 } 304 return 0; 305 } 306 307 /** 308 * pm_runtime_release_supplier - Drop references to device link's supplier. 309 * @link: Target device link. 310 * 311 * Drop all runtime PM references associated with @link to its supplier device. 312 */ 313 void pm_runtime_release_supplier(struct device_link *link) 314 { 315 struct device *supplier = link->supplier; 316 317 /* 318 * The additional power.usage_count check is a safety net in case 319 * the rpm_active refcount becomes saturated, in which case 320 * refcount_dec_not_one() would return true forever, but it is not 321 * strictly necessary. 322 */ 323 while (refcount_dec_not_one(&link->rpm_active) && 324 atomic_read(&supplier->power.usage_count) > 0) 325 pm_runtime_put_noidle(supplier); 326 } 327 328 static void __rpm_put_suppliers(struct device *dev, bool try_to_suspend) 329 { 330 struct device_link *link; 331 332 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, 333 device_links_read_lock_held()) { 334 pm_runtime_release_supplier(link); 335 if (try_to_suspend) 336 pm_request_idle(link->supplier); 337 } 338 } 339 340 static void rpm_put_suppliers(struct device *dev) 341 { 342 __rpm_put_suppliers(dev, true); 343 } 344 345 static void rpm_suspend_suppliers(struct device *dev) 346 { 347 struct device_link *link; 348 int idx = device_links_read_lock(); 349 350 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, 351 device_links_read_lock_held()) 352 pm_request_idle(link->supplier); 353 354 device_links_read_unlock(idx); 355 } 356 357 /** 358 * __rpm_callback - Run a given runtime PM callback for a given device. 359 * @cb: Runtime PM callback to run. 360 * @dev: Device to run the callback for. 361 */ 362 static int __rpm_callback(int (*cb)(struct device *), struct device *dev) 363 __releases(&dev->power.lock) __acquires(&dev->power.lock) 364 { 365 int retval = 0, idx; 366 bool use_links = dev->power.links_count > 0; 367 368 if (dev->power.irq_safe) { 369 spin_unlock(&dev->power.lock); 370 } else { 371 spin_unlock_irq(&dev->power.lock); 372 373 /* 374 * Resume suppliers if necessary. 375 * 376 * The device's runtime PM status cannot change until this 377 * routine returns, so it is safe to read the status outside of 378 * the lock. 379 */ 380 if (use_links && dev->power.runtime_status == RPM_RESUMING) { 381 idx = device_links_read_lock(); 382 383 retval = rpm_get_suppliers(dev); 384 if (retval) { 385 rpm_put_suppliers(dev); 386 goto fail; 387 } 388 389 device_links_read_unlock(idx); 390 } 391 } 392 393 if (cb) 394 retval = cb(dev); 395 396 if (dev->power.irq_safe) { 397 spin_lock(&dev->power.lock); 398 } else { 399 /* 400 * If the device is suspending and the callback has returned 401 * success, drop the usage counters of the suppliers that have 402 * been reference counted on its resume. 403 * 404 * Do that if resume fails too. 405 */ 406 if (use_links && 407 ((dev->power.runtime_status == RPM_SUSPENDING && !retval) || 408 (dev->power.runtime_status == RPM_RESUMING && retval))) { 409 idx = device_links_read_lock(); 410 411 __rpm_put_suppliers(dev, false); 412 413 fail: 414 device_links_read_unlock(idx); 415 } 416 417 spin_lock_irq(&dev->power.lock); 418 } 419 420 return retval; 421 } 422 423 /** 424 * rpm_callback - Run a given runtime PM callback for a given device. 425 * @cb: Runtime PM callback to run. 426 * @dev: Device to run the callback for. 427 */ 428 static int rpm_callback(int (*cb)(struct device *), struct device *dev) 429 { 430 int retval; 431 432 if (dev->power.memalloc_noio) { 433 unsigned int noio_flag; 434 435 /* 436 * Deadlock might be caused if memory allocation with 437 * GFP_KERNEL happens inside runtime_suspend and 438 * runtime_resume callbacks of one block device's 439 * ancestor or the block device itself. Network 440 * device might be thought as part of iSCSI block 441 * device, so network device and its ancestor should 442 * be marked as memalloc_noio too. 443 */ 444 noio_flag = memalloc_noio_save(); 445 retval = __rpm_callback(cb, dev); 446 memalloc_noio_restore(noio_flag); 447 } else { 448 retval = __rpm_callback(cb, dev); 449 } 450 451 /* 452 * Since -EACCES means that runtime PM is disabled for the given device, 453 * it should not be returned by runtime PM callbacks. If it is returned 454 * nevertheless, assume it to be a transient error and convert it to 455 * -EAGAIN. 456 */ 457 if (retval == -EACCES) 458 retval = -EAGAIN; 459 460 if (retval != -EAGAIN && retval != -EBUSY) 461 dev->power.runtime_error = retval; 462 463 return retval; 464 } 465 466 /** 467 * rpm_idle - Notify device bus type if the device can be suspended. 468 * @dev: Device to notify the bus type about. 469 * @rpmflags: Flag bits. 470 * 471 * Check if the device's runtime PM status allows it to be suspended. If 472 * another idle notification has been started earlier, return immediately. If 473 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise 474 * run the ->runtime_idle() callback directly. If the ->runtime_idle callback 475 * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag. 476 * 477 * This function must be called under dev->power.lock with interrupts disabled. 478 */ 479 static int rpm_idle(struct device *dev, int rpmflags) 480 { 481 int (*callback)(struct device *); 482 int retval; 483 484 trace_rpm_idle(dev, rpmflags); 485 retval = rpm_check_suspend_allowed(dev); 486 if (retval < 0) 487 ; /* Conditions are wrong. */ 488 489 /* Idle notifications are allowed only in the RPM_ACTIVE state. */ 490 else if (dev->power.runtime_status != RPM_ACTIVE) 491 retval = -EAGAIN; 492 493 /* 494 * Any pending request other than an idle notification takes 495 * precedence over us, except that the timer may be running. 496 */ 497 else if (dev->power.request_pending && 498 dev->power.request > RPM_REQ_IDLE) 499 retval = -EAGAIN; 500 501 /* Act as though RPM_NOWAIT is always set. */ 502 else if (dev->power.idle_notification) 503 retval = -EINPROGRESS; 504 505 if (retval) 506 goto out; 507 508 /* Pending requests need to be canceled. */ 509 dev->power.request = RPM_REQ_NONE; 510 511 callback = RPM_GET_CALLBACK(dev, runtime_idle); 512 513 /* If no callback assume success. */ 514 if (!callback || dev->power.no_callbacks) 515 goto out; 516 517 /* Carry out an asynchronous or a synchronous idle notification. */ 518 if (rpmflags & RPM_ASYNC) { 519 dev->power.request = RPM_REQ_IDLE; 520 if (!dev->power.request_pending) { 521 dev->power.request_pending = true; 522 queue_work(pm_wq, &dev->power.work); 523 } 524 trace_rpm_return_int(dev, _THIS_IP_, 0); 525 return 0; 526 } 527 528 dev->power.idle_notification = true; 529 530 if (dev->power.irq_safe) 531 spin_unlock(&dev->power.lock); 532 else 533 spin_unlock_irq(&dev->power.lock); 534 535 retval = callback(dev); 536 537 if (dev->power.irq_safe) 538 spin_lock(&dev->power.lock); 539 else 540 spin_lock_irq(&dev->power.lock); 541 542 dev->power.idle_notification = false; 543 wake_up_all(&dev->power.wait_queue); 544 545 out: 546 trace_rpm_return_int(dev, _THIS_IP_, retval); 547 return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO); 548 } 549 550 /** 551 * rpm_suspend - Carry out runtime suspend of given device. 552 * @dev: Device to suspend. 553 * @rpmflags: Flag bits. 554 * 555 * Check if the device's runtime PM status allows it to be suspended. 556 * Cancel a pending idle notification, autosuspend or suspend. If 557 * another suspend has been started earlier, either return immediately 558 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC 559 * flags. If the RPM_ASYNC flag is set then queue a suspend request; 560 * otherwise run the ->runtime_suspend() callback directly. When 561 * ->runtime_suspend succeeded, if a deferred resume was requested while 562 * the callback was running then carry it out, otherwise send an idle 563 * notification for its parent (if the suspend succeeded and both 564 * ignore_children of parent->power and irq_safe of dev->power are not set). 565 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO 566 * flag is set and the next autosuspend-delay expiration time is in the 567 * future, schedule another autosuspend attempt. 568 * 569 * This function must be called under dev->power.lock with interrupts disabled. 570 */ 571 static int rpm_suspend(struct device *dev, int rpmflags) 572 __releases(&dev->power.lock) __acquires(&dev->power.lock) 573 { 574 int (*callback)(struct device *); 575 struct device *parent = NULL; 576 int retval; 577 578 trace_rpm_suspend(dev, rpmflags); 579 580 repeat: 581 retval = rpm_check_suspend_allowed(dev); 582 if (retval < 0) 583 goto out; /* Conditions are wrong. */ 584 585 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */ 586 if (dev->power.runtime_status == RPM_RESUMING && !(rpmflags & RPM_ASYNC)) 587 retval = -EAGAIN; 588 589 if (retval) 590 goto out; 591 592 /* If the autosuspend_delay time hasn't expired yet, reschedule. */ 593 if ((rpmflags & RPM_AUTO) && dev->power.runtime_status != RPM_SUSPENDING) { 594 u64 expires = pm_runtime_autosuspend_expiration(dev); 595 596 if (expires != 0) { 597 /* Pending requests need to be canceled. */ 598 dev->power.request = RPM_REQ_NONE; 599 600 /* 601 * Optimization: If the timer is already running and is 602 * set to expire at or before the autosuspend delay, 603 * avoid the overhead of resetting it. Just let it 604 * expire; pm_suspend_timer_fn() will take care of the 605 * rest. 606 */ 607 if (!(dev->power.timer_expires && 608 dev->power.timer_expires <= expires)) { 609 /* 610 * We add a slack of 25% to gather wakeups 611 * without sacrificing the granularity. 612 */ 613 u64 slack = (u64)READ_ONCE(dev->power.autosuspend_delay) * 614 (NSEC_PER_MSEC >> 2); 615 616 dev->power.timer_expires = expires; 617 hrtimer_start_range_ns(&dev->power.suspend_timer, 618 ns_to_ktime(expires), 619 slack, 620 HRTIMER_MODE_ABS); 621 } 622 dev->power.timer_autosuspends = 1; 623 goto out; 624 } 625 } 626 627 /* Other scheduled or pending requests need to be canceled. */ 628 pm_runtime_cancel_pending(dev); 629 630 if (dev->power.runtime_status == RPM_SUSPENDING) { 631 DEFINE_WAIT(wait); 632 633 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) { 634 retval = -EINPROGRESS; 635 goto out; 636 } 637 638 if (dev->power.irq_safe) { 639 spin_unlock(&dev->power.lock); 640 641 cpu_relax(); 642 643 spin_lock(&dev->power.lock); 644 goto repeat; 645 } 646 647 /* Wait for the other suspend running in parallel with us. */ 648 for (;;) { 649 prepare_to_wait(&dev->power.wait_queue, &wait, 650 TASK_UNINTERRUPTIBLE); 651 if (dev->power.runtime_status != RPM_SUSPENDING) 652 break; 653 654 spin_unlock_irq(&dev->power.lock); 655 656 schedule(); 657 658 spin_lock_irq(&dev->power.lock); 659 } 660 finish_wait(&dev->power.wait_queue, &wait); 661 goto repeat; 662 } 663 664 if (dev->power.no_callbacks) 665 goto no_callback; /* Assume success. */ 666 667 /* Carry out an asynchronous or a synchronous suspend. */ 668 if (rpmflags & RPM_ASYNC) { 669 dev->power.request = (rpmflags & RPM_AUTO) ? 670 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND; 671 if (!dev->power.request_pending) { 672 dev->power.request_pending = true; 673 queue_work(pm_wq, &dev->power.work); 674 } 675 goto out; 676 } 677 678 __update_runtime_status(dev, RPM_SUSPENDING); 679 680 callback = RPM_GET_CALLBACK(dev, runtime_suspend); 681 682 dev_pm_enable_wake_irq_check(dev, true); 683 retval = rpm_callback(callback, dev); 684 if (retval) 685 goto fail; 686 687 dev_pm_enable_wake_irq_complete(dev); 688 689 no_callback: 690 __update_runtime_status(dev, RPM_SUSPENDED); 691 pm_runtime_deactivate_timer(dev); 692 693 if (dev->parent) { 694 parent = dev->parent; 695 atomic_add_unless(&parent->power.child_count, -1, 0); 696 } 697 wake_up_all(&dev->power.wait_queue); 698 699 if (dev->power.deferred_resume) { 700 dev->power.deferred_resume = false; 701 rpm_resume(dev, 0); 702 retval = -EAGAIN; 703 goto out; 704 } 705 706 if (dev->power.irq_safe) 707 goto out; 708 709 /* Maybe the parent is now able to suspend. */ 710 if (parent && !parent->power.ignore_children) { 711 spin_unlock(&dev->power.lock); 712 713 spin_lock(&parent->power.lock); 714 rpm_idle(parent, RPM_ASYNC); 715 spin_unlock(&parent->power.lock); 716 717 spin_lock(&dev->power.lock); 718 } 719 /* Maybe the suppliers are now able to suspend. */ 720 if (dev->power.links_count > 0) { 721 spin_unlock_irq(&dev->power.lock); 722 723 rpm_suspend_suppliers(dev); 724 725 spin_lock_irq(&dev->power.lock); 726 } 727 728 out: 729 trace_rpm_return_int(dev, _THIS_IP_, retval); 730 731 return retval; 732 733 fail: 734 dev_pm_disable_wake_irq_check(dev, true); 735 __update_runtime_status(dev, RPM_ACTIVE); 736 dev->power.deferred_resume = false; 737 wake_up_all(&dev->power.wait_queue); 738 739 /* 740 * On transient errors, if the callback routine failed an autosuspend, 741 * and if the last_busy time has been updated so that there is a new 742 * autosuspend expiration time, automatically reschedule another 743 * autosuspend. 744 */ 745 if (!dev->power.runtime_error && (rpmflags & RPM_AUTO) && 746 pm_runtime_autosuspend_expiration(dev) != 0) 747 goto repeat; 748 749 pm_runtime_cancel_pending(dev); 750 751 goto out; 752 } 753 754 /** 755 * rpm_resume - Carry out runtime resume of given device. 756 * @dev: Device to resume. 757 * @rpmflags: Flag bits. 758 * 759 * Check if the device's runtime PM status allows it to be resumed. Cancel 760 * any scheduled or pending requests. If another resume has been started 761 * earlier, either return immediately or wait for it to finish, depending on the 762 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in 763 * parallel with this function, either tell the other process to resume after 764 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC 765 * flag is set then queue a resume request; otherwise run the 766 * ->runtime_resume() callback directly. Queue an idle notification for the 767 * device if the resume succeeded. 768 * 769 * This function must be called under dev->power.lock with interrupts disabled. 770 */ 771 static int rpm_resume(struct device *dev, int rpmflags) 772 __releases(&dev->power.lock) __acquires(&dev->power.lock) 773 { 774 int (*callback)(struct device *); 775 struct device *parent = NULL; 776 int retval = 0; 777 778 trace_rpm_resume(dev, rpmflags); 779 780 repeat: 781 if (dev->power.runtime_error) { 782 retval = -EINVAL; 783 } else if (dev->power.disable_depth > 0) { 784 if (dev->power.runtime_status == RPM_ACTIVE && 785 dev->power.last_status == RPM_ACTIVE) 786 retval = 1; 787 else 788 retval = -EACCES; 789 } 790 if (retval) 791 goto out; 792 793 /* 794 * Other scheduled or pending requests need to be canceled. Small 795 * optimization: If an autosuspend timer is running, leave it running 796 * rather than cancelling it now only to restart it again in the near 797 * future. 798 */ 799 dev->power.request = RPM_REQ_NONE; 800 if (!dev->power.timer_autosuspends) 801 pm_runtime_deactivate_timer(dev); 802 803 if (dev->power.runtime_status == RPM_ACTIVE) { 804 retval = 1; 805 goto out; 806 } 807 808 if (dev->power.runtime_status == RPM_RESUMING || 809 dev->power.runtime_status == RPM_SUSPENDING) { 810 DEFINE_WAIT(wait); 811 812 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) { 813 if (dev->power.runtime_status == RPM_SUSPENDING) { 814 dev->power.deferred_resume = true; 815 if (rpmflags & RPM_NOWAIT) 816 retval = -EINPROGRESS; 817 } else { 818 retval = -EINPROGRESS; 819 } 820 goto out; 821 } 822 823 if (dev->power.irq_safe) { 824 spin_unlock(&dev->power.lock); 825 826 cpu_relax(); 827 828 spin_lock(&dev->power.lock); 829 goto repeat; 830 } 831 832 /* Wait for the operation carried out in parallel with us. */ 833 for (;;) { 834 prepare_to_wait(&dev->power.wait_queue, &wait, 835 TASK_UNINTERRUPTIBLE); 836 if (dev->power.runtime_status != RPM_RESUMING && 837 dev->power.runtime_status != RPM_SUSPENDING) 838 break; 839 840 spin_unlock_irq(&dev->power.lock); 841 842 schedule(); 843 844 spin_lock_irq(&dev->power.lock); 845 } 846 finish_wait(&dev->power.wait_queue, &wait); 847 goto repeat; 848 } 849 850 /* 851 * See if we can skip waking up the parent. This is safe only if 852 * power.no_callbacks is set, because otherwise we don't know whether 853 * the resume will actually succeed. 854 */ 855 if (dev->power.no_callbacks && !parent && dev->parent) { 856 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING); 857 if (dev->parent->power.disable_depth > 0 || 858 dev->parent->power.ignore_children || 859 dev->parent->power.runtime_status == RPM_ACTIVE) { 860 atomic_inc(&dev->parent->power.child_count); 861 spin_unlock(&dev->parent->power.lock); 862 retval = 1; 863 goto no_callback; /* Assume success. */ 864 } 865 spin_unlock(&dev->parent->power.lock); 866 } 867 868 /* Carry out an asynchronous or a synchronous resume. */ 869 if (rpmflags & RPM_ASYNC) { 870 dev->power.request = RPM_REQ_RESUME; 871 if (!dev->power.request_pending) { 872 dev->power.request_pending = true; 873 queue_work(pm_wq, &dev->power.work); 874 } 875 retval = 0; 876 goto out; 877 } 878 879 if (!parent && dev->parent) { 880 /* 881 * Increment the parent's usage counter and resume it if 882 * necessary. Not needed if dev is irq-safe; then the 883 * parent is permanently resumed. 884 */ 885 parent = dev->parent; 886 if (dev->power.irq_safe) 887 goto skip_parent; 888 889 spin_unlock(&dev->power.lock); 890 891 pm_runtime_get_noresume(parent); 892 893 spin_lock(&parent->power.lock); 894 /* 895 * Resume the parent if it has runtime PM enabled and not been 896 * set to ignore its children. 897 */ 898 if (!parent->power.disable_depth && 899 !parent->power.ignore_children) { 900 rpm_resume(parent, 0); 901 if (parent->power.runtime_status != RPM_ACTIVE) 902 retval = -EBUSY; 903 } 904 spin_unlock(&parent->power.lock); 905 906 spin_lock(&dev->power.lock); 907 if (retval) 908 goto out; 909 910 goto repeat; 911 } 912 skip_parent: 913 914 if (dev->power.no_callbacks) 915 goto no_callback; /* Assume success. */ 916 917 __update_runtime_status(dev, RPM_RESUMING); 918 919 callback = RPM_GET_CALLBACK(dev, runtime_resume); 920 921 dev_pm_disable_wake_irq_check(dev, false); 922 retval = rpm_callback(callback, dev); 923 if (retval) { 924 __update_runtime_status(dev, RPM_SUSPENDED); 925 pm_runtime_cancel_pending(dev); 926 dev_pm_enable_wake_irq_check(dev, false); 927 } else { 928 no_callback: 929 __update_runtime_status(dev, RPM_ACTIVE); 930 pm_runtime_mark_last_busy(dev); 931 if (parent) 932 atomic_inc(&parent->power.child_count); 933 } 934 wake_up_all(&dev->power.wait_queue); 935 936 if (retval >= 0) 937 rpm_idle(dev, RPM_ASYNC); 938 939 out: 940 if (parent && !dev->power.irq_safe) { 941 spin_unlock_irq(&dev->power.lock); 942 943 pm_runtime_put(parent); 944 945 spin_lock_irq(&dev->power.lock); 946 } 947 948 trace_rpm_return_int(dev, _THIS_IP_, retval); 949 950 return retval; 951 } 952 953 /** 954 * pm_runtime_work - Universal runtime PM work function. 955 * @work: Work structure used for scheduling the execution of this function. 956 * 957 * Use @work to get the device object the work is to be done for, determine what 958 * is to be done and execute the appropriate runtime PM function. 959 */ 960 static void pm_runtime_work(struct work_struct *work) 961 { 962 struct device *dev = container_of(work, struct device, power.work); 963 enum rpm_request req; 964 965 spin_lock_irq(&dev->power.lock); 966 967 if (!dev->power.request_pending) 968 goto out; 969 970 req = dev->power.request; 971 dev->power.request = RPM_REQ_NONE; 972 dev->power.request_pending = false; 973 974 switch (req) { 975 case RPM_REQ_NONE: 976 break; 977 case RPM_REQ_IDLE: 978 rpm_idle(dev, RPM_NOWAIT); 979 break; 980 case RPM_REQ_SUSPEND: 981 rpm_suspend(dev, RPM_NOWAIT); 982 break; 983 case RPM_REQ_AUTOSUSPEND: 984 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO); 985 break; 986 case RPM_REQ_RESUME: 987 rpm_resume(dev, RPM_NOWAIT); 988 break; 989 } 990 991 out: 992 spin_unlock_irq(&dev->power.lock); 993 } 994 995 /** 996 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend(). 997 * @timer: hrtimer used by pm_schedule_suspend(). 998 * 999 * Check if the time is right and queue a suspend request. 1000 */ 1001 static enum hrtimer_restart pm_suspend_timer_fn(struct hrtimer *timer) 1002 { 1003 struct device *dev = container_of(timer, struct device, power.suspend_timer); 1004 unsigned long flags; 1005 u64 expires; 1006 1007 spin_lock_irqsave(&dev->power.lock, flags); 1008 1009 expires = dev->power.timer_expires; 1010 /* 1011 * If 'expires' is after the current time, we've been called 1012 * too early. 1013 */ 1014 if (expires > 0 && expires < ktime_get_mono_fast_ns()) { 1015 dev->power.timer_expires = 0; 1016 rpm_suspend(dev, dev->power.timer_autosuspends ? 1017 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC); 1018 } 1019 1020 spin_unlock_irqrestore(&dev->power.lock, flags); 1021 1022 return HRTIMER_NORESTART; 1023 } 1024 1025 /** 1026 * pm_schedule_suspend - Set up a timer to submit a suspend request in future. 1027 * @dev: Device to suspend. 1028 * @delay: Time to wait before submitting a suspend request, in milliseconds. 1029 */ 1030 int pm_schedule_suspend(struct device *dev, unsigned int delay) 1031 { 1032 unsigned long flags; 1033 u64 expires; 1034 int retval; 1035 1036 spin_lock_irqsave(&dev->power.lock, flags); 1037 1038 if (!delay) { 1039 retval = rpm_suspend(dev, RPM_ASYNC); 1040 goto out; 1041 } 1042 1043 retval = rpm_check_suspend_allowed(dev); 1044 if (retval) 1045 goto out; 1046 1047 /* Other scheduled or pending requests need to be canceled. */ 1048 pm_runtime_cancel_pending(dev); 1049 1050 expires = ktime_get_mono_fast_ns() + (u64)delay * NSEC_PER_MSEC; 1051 dev->power.timer_expires = expires; 1052 dev->power.timer_autosuspends = 0; 1053 hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS); 1054 1055 out: 1056 spin_unlock_irqrestore(&dev->power.lock, flags); 1057 1058 return retval; 1059 } 1060 EXPORT_SYMBOL_GPL(pm_schedule_suspend); 1061 1062 static int rpm_drop_usage_count(struct device *dev) 1063 { 1064 int ret; 1065 1066 ret = atomic_sub_return(1, &dev->power.usage_count); 1067 if (ret >= 0) 1068 return ret; 1069 1070 /* 1071 * Because rpm_resume() does not check the usage counter, it will resume 1072 * the device even if the usage counter is 0 or negative, so it is 1073 * sufficient to increment the usage counter here to reverse the change 1074 * made above. 1075 */ 1076 atomic_inc(&dev->power.usage_count); 1077 dev_warn(dev, "Runtime PM usage count underflow!\n"); 1078 return -EINVAL; 1079 } 1080 1081 /** 1082 * __pm_runtime_idle - Entry point for runtime idle operations. 1083 * @dev: Device to send idle notification for. 1084 * @rpmflags: Flag bits. 1085 * 1086 * If the RPM_GET_PUT flag is set, decrement the device's usage count and 1087 * return immediately if it is larger than zero (if it becomes negative, log a 1088 * warning, increment it, and return an error). Then carry out an idle 1089 * notification, either synchronous or asynchronous. 1090 * 1091 * This routine may be called in atomic context if the RPM_ASYNC flag is set, 1092 * or if pm_runtime_irq_safe() has been called. 1093 */ 1094 int __pm_runtime_idle(struct device *dev, int rpmflags) 1095 { 1096 unsigned long flags; 1097 int retval; 1098 1099 if (rpmflags & RPM_GET_PUT) { 1100 retval = rpm_drop_usage_count(dev); 1101 if (retval < 0) { 1102 return retval; 1103 } else if (retval > 0) { 1104 trace_rpm_usage(dev, rpmflags); 1105 return 0; 1106 } 1107 } 1108 1109 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); 1110 1111 spin_lock_irqsave(&dev->power.lock, flags); 1112 retval = rpm_idle(dev, rpmflags); 1113 spin_unlock_irqrestore(&dev->power.lock, flags); 1114 1115 return retval; 1116 } 1117 EXPORT_SYMBOL_GPL(__pm_runtime_idle); 1118 1119 /** 1120 * __pm_runtime_suspend - Entry point for runtime put/suspend operations. 1121 * @dev: Device to suspend. 1122 * @rpmflags: Flag bits. 1123 * 1124 * If the RPM_GET_PUT flag is set, decrement the device's usage count and 1125 * return immediately if it is larger than zero (if it becomes negative, log a 1126 * warning, increment it, and return an error). Then carry out a suspend, 1127 * either synchronous or asynchronous. 1128 * 1129 * This routine may be called in atomic context if the RPM_ASYNC flag is set, 1130 * or if pm_runtime_irq_safe() has been called. 1131 */ 1132 int __pm_runtime_suspend(struct device *dev, int rpmflags) 1133 { 1134 unsigned long flags; 1135 int retval; 1136 1137 if (rpmflags & RPM_GET_PUT) { 1138 retval = rpm_drop_usage_count(dev); 1139 if (retval < 0) { 1140 return retval; 1141 } else if (retval > 0) { 1142 trace_rpm_usage(dev, rpmflags); 1143 return 0; 1144 } 1145 } 1146 1147 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); 1148 1149 spin_lock_irqsave(&dev->power.lock, flags); 1150 retval = rpm_suspend(dev, rpmflags); 1151 spin_unlock_irqrestore(&dev->power.lock, flags); 1152 1153 return retval; 1154 } 1155 EXPORT_SYMBOL_GPL(__pm_runtime_suspend); 1156 1157 /** 1158 * __pm_runtime_resume - Entry point for runtime resume operations. 1159 * @dev: Device to resume. 1160 * @rpmflags: Flag bits. 1161 * 1162 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then 1163 * carry out a resume, either synchronous or asynchronous. 1164 * 1165 * This routine may be called in atomic context if the RPM_ASYNC flag is set, 1166 * or if pm_runtime_irq_safe() has been called. 1167 */ 1168 int __pm_runtime_resume(struct device *dev, int rpmflags) 1169 { 1170 unsigned long flags; 1171 int retval; 1172 1173 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe && 1174 dev->power.runtime_status != RPM_ACTIVE); 1175 1176 if (rpmflags & RPM_GET_PUT) 1177 atomic_inc(&dev->power.usage_count); 1178 1179 spin_lock_irqsave(&dev->power.lock, flags); 1180 retval = rpm_resume(dev, rpmflags); 1181 spin_unlock_irqrestore(&dev->power.lock, flags); 1182 1183 return retval; 1184 } 1185 EXPORT_SYMBOL_GPL(__pm_runtime_resume); 1186 1187 /** 1188 * pm_runtime_get_conditional - Conditionally bump up device usage counter. 1189 * @dev: Device to handle. 1190 * @ign_usage_count: Whether or not to look at the current usage counter value. 1191 * 1192 * Return -EINVAL if runtime PM is disabled for @dev. 1193 * 1194 * Otherwise, if the runtime PM status of @dev is %RPM_ACTIVE and either 1195 * @ign_usage_count is %true or the runtime PM usage counter of @dev is not 1196 * zero, increment the usage counter of @dev and return 1. Otherwise, return 0 1197 * without changing the usage counter. 1198 * 1199 * If @ign_usage_count is %true, this function can be used to prevent suspending 1200 * the device when its runtime PM status is %RPM_ACTIVE. 1201 * 1202 * If @ign_usage_count is %false, this function can be used to prevent 1203 * suspending the device when both its runtime PM status is %RPM_ACTIVE and its 1204 * runtime PM usage counter is not zero. 1205 * 1206 * The caller is responsible for decrementing the runtime PM usage counter of 1207 * @dev after this function has returned a positive value for it. 1208 */ 1209 static int pm_runtime_get_conditional(struct device *dev, bool ign_usage_count) 1210 { 1211 unsigned long flags; 1212 int retval; 1213 1214 spin_lock_irqsave(&dev->power.lock, flags); 1215 if (dev->power.disable_depth > 0) { 1216 retval = -EINVAL; 1217 } else if (dev->power.runtime_status != RPM_ACTIVE) { 1218 retval = 0; 1219 } else if (ign_usage_count) { 1220 retval = 1; 1221 atomic_inc(&dev->power.usage_count); 1222 } else { 1223 retval = atomic_inc_not_zero(&dev->power.usage_count); 1224 } 1225 trace_rpm_usage(dev, 0); 1226 spin_unlock_irqrestore(&dev->power.lock, flags); 1227 1228 return retval; 1229 } 1230 1231 /** 1232 * pm_runtime_get_if_active - Bump up runtime PM usage counter if the device is 1233 * in active state 1234 * @dev: Target device. 1235 * 1236 * Increment the runtime PM usage counter of @dev if its runtime PM status is 1237 * %RPM_ACTIVE, in which case it returns 1. If the device is in a different 1238 * state, 0 is returned. -EINVAL is returned if runtime PM is disabled for the 1239 * device, in which case also the usage_count will remain unmodified. 1240 */ 1241 int pm_runtime_get_if_active(struct device *dev) 1242 { 1243 return pm_runtime_get_conditional(dev, true); 1244 } 1245 EXPORT_SYMBOL_GPL(pm_runtime_get_if_active); 1246 1247 /** 1248 * pm_runtime_get_if_in_use - Conditionally bump up runtime PM usage counter. 1249 * @dev: Target device. 1250 * 1251 * Increment the runtime PM usage counter of @dev if its runtime PM status is 1252 * %RPM_ACTIVE and its runtime PM usage counter is greater than 0, in which case 1253 * it returns 1. If the device is in a different state or its usage_count is 0, 1254 * 0 is returned. -EINVAL is returned if runtime PM is disabled for the device, 1255 * in which case also the usage_count will remain unmodified. 1256 */ 1257 int pm_runtime_get_if_in_use(struct device *dev) 1258 { 1259 return pm_runtime_get_conditional(dev, false); 1260 } 1261 EXPORT_SYMBOL_GPL(pm_runtime_get_if_in_use); 1262 1263 /** 1264 * __pm_runtime_set_status - Set runtime PM status of a device. 1265 * @dev: Device to handle. 1266 * @status: New runtime PM status of the device. 1267 * 1268 * If runtime PM of the device is disabled or its power.runtime_error field is 1269 * different from zero, the status may be changed either to RPM_ACTIVE, or to 1270 * RPM_SUSPENDED, as long as that reflects the actual state of the device. 1271 * However, if the device has a parent and the parent is not active, and the 1272 * parent's power.ignore_children flag is unset, the device's status cannot be 1273 * set to RPM_ACTIVE, so -EBUSY is returned in that case. 1274 * 1275 * If successful, __pm_runtime_set_status() clears the power.runtime_error field 1276 * and the device parent's counter of unsuspended children is modified to 1277 * reflect the new status. If the new status is RPM_SUSPENDED, an idle 1278 * notification request for the parent is submitted. 1279 * 1280 * If @dev has any suppliers (as reflected by device links to them), and @status 1281 * is RPM_ACTIVE, they will be activated upfront and if the activation of one 1282 * of them fails, the status of @dev will be changed to RPM_SUSPENDED (instead 1283 * of the @status value) and the suppliers will be deacticated on exit. The 1284 * error returned by the failing supplier activation will be returned in that 1285 * case. 1286 */ 1287 int __pm_runtime_set_status(struct device *dev, unsigned int status) 1288 { 1289 struct device *parent = dev->parent; 1290 bool notify_parent = false; 1291 unsigned long flags; 1292 int error = 0; 1293 1294 if (status != RPM_ACTIVE && status != RPM_SUSPENDED) 1295 return -EINVAL; 1296 1297 spin_lock_irqsave(&dev->power.lock, flags); 1298 1299 /* 1300 * Prevent PM-runtime from being enabled for the device or return an 1301 * error if it is enabled already and working. 1302 */ 1303 if (dev->power.runtime_error || dev->power.disable_depth) 1304 dev->power.disable_depth++; 1305 else 1306 error = -EAGAIN; 1307 1308 spin_unlock_irqrestore(&dev->power.lock, flags); 1309 1310 if (error) 1311 return error; 1312 1313 /* 1314 * If the new status is RPM_ACTIVE, the suppliers can be activated 1315 * upfront regardless of the current status, because next time 1316 * rpm_put_suppliers() runs, the rpm_active refcounts of the links 1317 * involved will be dropped down to one anyway. 1318 */ 1319 if (status == RPM_ACTIVE) { 1320 int idx = device_links_read_lock(); 1321 1322 error = rpm_get_suppliers(dev); 1323 if (error) 1324 status = RPM_SUSPENDED; 1325 1326 device_links_read_unlock(idx); 1327 } 1328 1329 spin_lock_irqsave(&dev->power.lock, flags); 1330 1331 if (dev->power.runtime_status == status || !parent) 1332 goto out_set; 1333 1334 if (status == RPM_SUSPENDED) { 1335 atomic_add_unless(&parent->power.child_count, -1, 0); 1336 notify_parent = !parent->power.ignore_children; 1337 } else { 1338 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING); 1339 1340 /* 1341 * It is invalid to put an active child under a parent that is 1342 * not active, has runtime PM enabled and the 1343 * 'power.ignore_children' flag unset. 1344 */ 1345 if (!parent->power.disable_depth && 1346 !parent->power.ignore_children && 1347 parent->power.runtime_status != RPM_ACTIVE) { 1348 dev_err(dev, "runtime PM trying to activate child device %s but parent (%s) is not active\n", 1349 dev_name(dev), 1350 dev_name(parent)); 1351 error = -EBUSY; 1352 } else if (dev->power.runtime_status == RPM_SUSPENDED) { 1353 atomic_inc(&parent->power.child_count); 1354 } 1355 1356 spin_unlock(&parent->power.lock); 1357 1358 if (error) { 1359 status = RPM_SUSPENDED; 1360 goto out; 1361 } 1362 } 1363 1364 out_set: 1365 __update_runtime_status(dev, status); 1366 if (!error) 1367 dev->power.runtime_error = 0; 1368 1369 out: 1370 spin_unlock_irqrestore(&dev->power.lock, flags); 1371 1372 if (notify_parent) 1373 pm_request_idle(parent); 1374 1375 if (status == RPM_SUSPENDED) { 1376 int idx = device_links_read_lock(); 1377 1378 rpm_put_suppliers(dev); 1379 1380 device_links_read_unlock(idx); 1381 } 1382 1383 pm_runtime_enable(dev); 1384 1385 return error; 1386 } 1387 EXPORT_SYMBOL_GPL(__pm_runtime_set_status); 1388 1389 /** 1390 * __pm_runtime_barrier - Cancel pending requests and wait for completions. 1391 * @dev: Device to handle. 1392 * 1393 * Flush all pending requests for the device from pm_wq and wait for all 1394 * runtime PM operations involving the device in progress to complete. 1395 * 1396 * Should be called under dev->power.lock with interrupts disabled. 1397 */ 1398 static void __pm_runtime_barrier(struct device *dev) 1399 { 1400 pm_runtime_deactivate_timer(dev); 1401 1402 if (dev->power.request_pending) { 1403 dev->power.request = RPM_REQ_NONE; 1404 spin_unlock_irq(&dev->power.lock); 1405 1406 cancel_work_sync(&dev->power.work); 1407 1408 spin_lock_irq(&dev->power.lock); 1409 dev->power.request_pending = false; 1410 } 1411 1412 if (dev->power.runtime_status == RPM_SUSPENDING || 1413 dev->power.runtime_status == RPM_RESUMING || 1414 dev->power.idle_notification) { 1415 DEFINE_WAIT(wait); 1416 1417 /* Suspend, wake-up or idle notification in progress. */ 1418 for (;;) { 1419 prepare_to_wait(&dev->power.wait_queue, &wait, 1420 TASK_UNINTERRUPTIBLE); 1421 if (dev->power.runtime_status != RPM_SUSPENDING 1422 && dev->power.runtime_status != RPM_RESUMING 1423 && !dev->power.idle_notification) 1424 break; 1425 spin_unlock_irq(&dev->power.lock); 1426 1427 schedule(); 1428 1429 spin_lock_irq(&dev->power.lock); 1430 } 1431 finish_wait(&dev->power.wait_queue, &wait); 1432 } 1433 } 1434 1435 /** 1436 * pm_runtime_barrier - Flush pending requests and wait for completions. 1437 * @dev: Device to handle. 1438 * 1439 * Prevent the device from being suspended by incrementing its usage counter and 1440 * if there's a pending resume request for the device, wake the device up. 1441 * Next, make sure that all pending requests for the device have been flushed 1442 * from pm_wq and wait for all runtime PM operations involving the device in 1443 * progress to complete. 1444 * 1445 * Return value: 1446 * 1, if there was a resume request pending and the device had to be woken up, 1447 * 0, otherwise 1448 */ 1449 int pm_runtime_barrier(struct device *dev) 1450 { 1451 int retval = 0; 1452 1453 pm_runtime_get_noresume(dev); 1454 spin_lock_irq(&dev->power.lock); 1455 1456 if (dev->power.request_pending 1457 && dev->power.request == RPM_REQ_RESUME) { 1458 rpm_resume(dev, 0); 1459 retval = 1; 1460 } 1461 1462 __pm_runtime_barrier(dev); 1463 1464 spin_unlock_irq(&dev->power.lock); 1465 pm_runtime_put_noidle(dev); 1466 1467 return retval; 1468 } 1469 EXPORT_SYMBOL_GPL(pm_runtime_barrier); 1470 1471 bool pm_runtime_block_if_disabled(struct device *dev) 1472 { 1473 bool ret; 1474 1475 spin_lock_irq(&dev->power.lock); 1476 1477 ret = !pm_runtime_enabled(dev); 1478 if (ret && dev->power.last_status == RPM_INVALID) 1479 dev->power.last_status = RPM_BLOCKED; 1480 1481 spin_unlock_irq(&dev->power.lock); 1482 1483 return ret; 1484 } 1485 1486 void pm_runtime_unblock(struct device *dev) 1487 { 1488 spin_lock_irq(&dev->power.lock); 1489 1490 if (dev->power.last_status == RPM_BLOCKED) 1491 dev->power.last_status = RPM_INVALID; 1492 1493 spin_unlock_irq(&dev->power.lock); 1494 } 1495 1496 void __pm_runtime_disable(struct device *dev, bool check_resume) 1497 { 1498 spin_lock_irq(&dev->power.lock); 1499 1500 if (dev->power.disable_depth > 0) { 1501 dev->power.disable_depth++; 1502 goto out; 1503 } 1504 1505 /* 1506 * Wake up the device if there's a resume request pending, because that 1507 * means there probably is some I/O to process and disabling runtime PM 1508 * shouldn't prevent the device from processing the I/O. 1509 */ 1510 if (check_resume && dev->power.request_pending && 1511 dev->power.request == RPM_REQ_RESUME) { 1512 /* 1513 * Prevent suspends and idle notifications from being carried 1514 * out after we have woken up the device. 1515 */ 1516 pm_runtime_get_noresume(dev); 1517 1518 rpm_resume(dev, 0); 1519 1520 pm_runtime_put_noidle(dev); 1521 } 1522 1523 /* Update time accounting before disabling PM-runtime. */ 1524 update_pm_runtime_accounting(dev); 1525 1526 if (!dev->power.disable_depth++) { 1527 __pm_runtime_barrier(dev); 1528 dev->power.last_status = dev->power.runtime_status; 1529 } 1530 1531 out: 1532 spin_unlock_irq(&dev->power.lock); 1533 } 1534 EXPORT_SYMBOL_GPL(__pm_runtime_disable); 1535 1536 /** 1537 * pm_runtime_enable - Enable runtime PM of a device. 1538 * @dev: Device to handle. 1539 */ 1540 void pm_runtime_enable(struct device *dev) 1541 { 1542 unsigned long flags; 1543 1544 spin_lock_irqsave(&dev->power.lock, flags); 1545 1546 if (!dev->power.disable_depth) { 1547 dev_warn(dev, "Unbalanced %s!\n", __func__); 1548 goto out; 1549 } 1550 1551 if (--dev->power.disable_depth > 0) 1552 goto out; 1553 1554 if (dev->power.last_status == RPM_BLOCKED) { 1555 dev_warn(dev, "Attempt to enable runtime PM when it is blocked\n"); 1556 dump_stack(); 1557 } 1558 dev->power.last_status = RPM_INVALID; 1559 dev->power.accounting_timestamp = ktime_get_mono_fast_ns(); 1560 1561 if (dev->power.runtime_status == RPM_SUSPENDED && 1562 !dev->power.ignore_children && 1563 atomic_read(&dev->power.child_count) > 0) 1564 dev_warn(dev, "Enabling runtime PM for inactive device with active children\n"); 1565 1566 out: 1567 spin_unlock_irqrestore(&dev->power.lock, flags); 1568 } 1569 EXPORT_SYMBOL_GPL(pm_runtime_enable); 1570 1571 static void pm_runtime_disable_action(void *data) 1572 { 1573 pm_runtime_dont_use_autosuspend(data); 1574 pm_runtime_disable(data); 1575 } 1576 1577 /** 1578 * devm_pm_runtime_enable - devres-enabled version of pm_runtime_enable. 1579 * 1580 * NOTE: this will also handle calling pm_runtime_dont_use_autosuspend() for 1581 * you at driver exit time if needed. 1582 * 1583 * @dev: Device to handle. 1584 */ 1585 int devm_pm_runtime_enable(struct device *dev) 1586 { 1587 pm_runtime_enable(dev); 1588 1589 return devm_add_action_or_reset(dev, pm_runtime_disable_action, dev); 1590 } 1591 EXPORT_SYMBOL_GPL(devm_pm_runtime_enable); 1592 1593 /** 1594 * pm_runtime_forbid - Block runtime PM of a device. 1595 * @dev: Device to handle. 1596 * 1597 * Increase the device's usage count and clear its power.runtime_auto flag, 1598 * so that it cannot be suspended at run time until pm_runtime_allow() is called 1599 * for it. 1600 */ 1601 void pm_runtime_forbid(struct device *dev) 1602 { 1603 spin_lock_irq(&dev->power.lock); 1604 if (!dev->power.runtime_auto) 1605 goto out; 1606 1607 dev->power.runtime_auto = false; 1608 atomic_inc(&dev->power.usage_count); 1609 rpm_resume(dev, 0); 1610 1611 out: 1612 spin_unlock_irq(&dev->power.lock); 1613 } 1614 EXPORT_SYMBOL_GPL(pm_runtime_forbid); 1615 1616 /** 1617 * pm_runtime_allow - Unblock runtime PM of a device. 1618 * @dev: Device to handle. 1619 * 1620 * Decrease the device's usage count and set its power.runtime_auto flag. 1621 */ 1622 void pm_runtime_allow(struct device *dev) 1623 { 1624 int ret; 1625 1626 spin_lock_irq(&dev->power.lock); 1627 if (dev->power.runtime_auto) 1628 goto out; 1629 1630 dev->power.runtime_auto = true; 1631 ret = rpm_drop_usage_count(dev); 1632 if (ret == 0) 1633 rpm_idle(dev, RPM_AUTO | RPM_ASYNC); 1634 else if (ret > 0) 1635 trace_rpm_usage(dev, RPM_AUTO | RPM_ASYNC); 1636 1637 out: 1638 spin_unlock_irq(&dev->power.lock); 1639 } 1640 EXPORT_SYMBOL_GPL(pm_runtime_allow); 1641 1642 /** 1643 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device. 1644 * @dev: Device to handle. 1645 * 1646 * Set the power.no_callbacks flag, which tells the PM core that this 1647 * device is power-managed through its parent and has no runtime PM 1648 * callbacks of its own. The runtime sysfs attributes will be removed. 1649 */ 1650 void pm_runtime_no_callbacks(struct device *dev) 1651 { 1652 spin_lock_irq(&dev->power.lock); 1653 dev->power.no_callbacks = 1; 1654 spin_unlock_irq(&dev->power.lock); 1655 if (device_is_registered(dev)) 1656 rpm_sysfs_remove(dev); 1657 } 1658 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks); 1659 1660 /** 1661 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks. 1662 * @dev: Device to handle 1663 * 1664 * Set the power.irq_safe flag, which tells the PM core that the 1665 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should 1666 * always be invoked with the spinlock held and interrupts disabled. It also 1667 * causes the parent's usage counter to be permanently incremented, preventing 1668 * the parent from runtime suspending -- otherwise an irq-safe child might have 1669 * to wait for a non-irq-safe parent. 1670 */ 1671 void pm_runtime_irq_safe(struct device *dev) 1672 { 1673 if (dev->parent) 1674 pm_runtime_get_sync(dev->parent); 1675 1676 spin_lock_irq(&dev->power.lock); 1677 dev->power.irq_safe = 1; 1678 spin_unlock_irq(&dev->power.lock); 1679 } 1680 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe); 1681 1682 /** 1683 * update_autosuspend - Handle a change to a device's autosuspend settings. 1684 * @dev: Device to handle. 1685 * @old_delay: The former autosuspend_delay value. 1686 * @old_use: The former use_autosuspend value. 1687 * 1688 * Prevent runtime suspend if the new delay is negative and use_autosuspend is 1689 * set; otherwise allow it. Send an idle notification if suspends are allowed. 1690 * 1691 * This function must be called under dev->power.lock with interrupts disabled. 1692 */ 1693 static void update_autosuspend(struct device *dev, int old_delay, int old_use) 1694 { 1695 int delay = dev->power.autosuspend_delay; 1696 1697 /* Should runtime suspend be prevented now? */ 1698 if (dev->power.use_autosuspend && delay < 0) { 1699 1700 /* If it used to be allowed then prevent it. */ 1701 if (!old_use || old_delay >= 0) { 1702 atomic_inc(&dev->power.usage_count); 1703 rpm_resume(dev, 0); 1704 } else { 1705 trace_rpm_usage(dev, 0); 1706 } 1707 } 1708 1709 /* Runtime suspend should be allowed now. */ 1710 else { 1711 1712 /* If it used to be prevented then allow it. */ 1713 if (old_use && old_delay < 0) 1714 atomic_dec(&dev->power.usage_count); 1715 1716 /* Maybe we can autosuspend now. */ 1717 rpm_idle(dev, RPM_AUTO); 1718 } 1719 } 1720 1721 /** 1722 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value. 1723 * @dev: Device to handle. 1724 * @delay: Value of the new delay in milliseconds. 1725 * 1726 * Set the device's power.autosuspend_delay value. If it changes to negative 1727 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it 1728 * changes the other way, allow runtime suspends. 1729 */ 1730 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay) 1731 { 1732 int old_delay, old_use; 1733 1734 spin_lock_irq(&dev->power.lock); 1735 old_delay = dev->power.autosuspend_delay; 1736 old_use = dev->power.use_autosuspend; 1737 dev->power.autosuspend_delay = delay; 1738 update_autosuspend(dev, old_delay, old_use); 1739 spin_unlock_irq(&dev->power.lock); 1740 } 1741 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay); 1742 1743 /** 1744 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag. 1745 * @dev: Device to handle. 1746 * @use: New value for use_autosuspend. 1747 * 1748 * Set the device's power.use_autosuspend flag, and allow or prevent runtime 1749 * suspends as needed. 1750 */ 1751 void __pm_runtime_use_autosuspend(struct device *dev, bool use) 1752 { 1753 int old_delay, old_use; 1754 1755 spin_lock_irq(&dev->power.lock); 1756 old_delay = dev->power.autosuspend_delay; 1757 old_use = dev->power.use_autosuspend; 1758 dev->power.use_autosuspend = use; 1759 update_autosuspend(dev, old_delay, old_use); 1760 spin_unlock_irq(&dev->power.lock); 1761 } 1762 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend); 1763 1764 /** 1765 * pm_runtime_init - Initialize runtime PM fields in given device object. 1766 * @dev: Device object to initialize. 1767 */ 1768 void pm_runtime_init(struct device *dev) 1769 { 1770 dev->power.runtime_status = RPM_SUSPENDED; 1771 dev->power.last_status = RPM_INVALID; 1772 dev->power.idle_notification = false; 1773 1774 dev->power.disable_depth = 1; 1775 atomic_set(&dev->power.usage_count, 0); 1776 1777 dev->power.runtime_error = 0; 1778 1779 atomic_set(&dev->power.child_count, 0); 1780 pm_suspend_ignore_children(dev, false); 1781 dev->power.runtime_auto = true; 1782 1783 dev->power.request_pending = false; 1784 dev->power.request = RPM_REQ_NONE; 1785 dev->power.deferred_resume = false; 1786 dev->power.needs_force_resume = 0; 1787 INIT_WORK(&dev->power.work, pm_runtime_work); 1788 1789 dev->power.timer_expires = 0; 1790 hrtimer_setup(&dev->power.suspend_timer, pm_suspend_timer_fn, CLOCK_MONOTONIC, 1791 HRTIMER_MODE_ABS); 1792 1793 init_waitqueue_head(&dev->power.wait_queue); 1794 } 1795 1796 /** 1797 * pm_runtime_reinit - Re-initialize runtime PM fields in given device object. 1798 * @dev: Device object to re-initialize. 1799 */ 1800 void pm_runtime_reinit(struct device *dev) 1801 { 1802 if (!pm_runtime_enabled(dev)) { 1803 if (dev->power.runtime_status == RPM_ACTIVE) 1804 pm_runtime_set_suspended(dev); 1805 if (dev->power.irq_safe) { 1806 spin_lock_irq(&dev->power.lock); 1807 dev->power.irq_safe = 0; 1808 spin_unlock_irq(&dev->power.lock); 1809 if (dev->parent) 1810 pm_runtime_put(dev->parent); 1811 } 1812 } 1813 } 1814 1815 /** 1816 * pm_runtime_remove - Prepare for removing a device from device hierarchy. 1817 * @dev: Device object being removed from device hierarchy. 1818 */ 1819 void pm_runtime_remove(struct device *dev) 1820 { 1821 __pm_runtime_disable(dev, false); 1822 pm_runtime_reinit(dev); 1823 } 1824 1825 /** 1826 * pm_runtime_get_suppliers - Resume and reference-count supplier devices. 1827 * @dev: Consumer device. 1828 */ 1829 void pm_runtime_get_suppliers(struct device *dev) 1830 { 1831 struct device_link *link; 1832 int idx; 1833 1834 idx = device_links_read_lock(); 1835 1836 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, 1837 device_links_read_lock_held()) 1838 if (link->flags & DL_FLAG_PM_RUNTIME) { 1839 link->supplier_preactivated = true; 1840 pm_runtime_get_sync(link->supplier); 1841 } 1842 1843 device_links_read_unlock(idx); 1844 } 1845 1846 /** 1847 * pm_runtime_put_suppliers - Drop references to supplier devices. 1848 * @dev: Consumer device. 1849 */ 1850 void pm_runtime_put_suppliers(struct device *dev) 1851 { 1852 struct device_link *link; 1853 int idx; 1854 1855 idx = device_links_read_lock(); 1856 1857 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, 1858 device_links_read_lock_held()) 1859 if (link->supplier_preactivated) { 1860 link->supplier_preactivated = false; 1861 pm_runtime_put(link->supplier); 1862 } 1863 1864 device_links_read_unlock(idx); 1865 } 1866 1867 void pm_runtime_new_link(struct device *dev) 1868 { 1869 spin_lock_irq(&dev->power.lock); 1870 dev->power.links_count++; 1871 spin_unlock_irq(&dev->power.lock); 1872 } 1873 1874 static void pm_runtime_drop_link_count(struct device *dev) 1875 { 1876 spin_lock_irq(&dev->power.lock); 1877 WARN_ON(dev->power.links_count == 0); 1878 dev->power.links_count--; 1879 spin_unlock_irq(&dev->power.lock); 1880 } 1881 1882 /** 1883 * pm_runtime_drop_link - Prepare for device link removal. 1884 * @link: Device link going away. 1885 * 1886 * Drop the link count of the consumer end of @link and decrement the supplier 1887 * device's runtime PM usage counter as many times as needed to drop all of the 1888 * PM runtime reference to it from the consumer. 1889 */ 1890 void pm_runtime_drop_link(struct device_link *link) 1891 { 1892 if (!(link->flags & DL_FLAG_PM_RUNTIME)) 1893 return; 1894 1895 pm_runtime_drop_link_count(link->consumer); 1896 pm_runtime_release_supplier(link); 1897 pm_request_idle(link->supplier); 1898 } 1899 1900 bool pm_runtime_need_not_resume(struct device *dev) 1901 { 1902 return atomic_read(&dev->power.usage_count) <= 1 && 1903 (atomic_read(&dev->power.child_count) == 0 || 1904 dev->power.ignore_children); 1905 } 1906 1907 /** 1908 * pm_runtime_force_suspend - Force a device into suspend state if needed. 1909 * @dev: Device to suspend. 1910 * 1911 * Disable runtime PM so we safely can check the device's runtime PM status and 1912 * if it is active, invoke its ->runtime_suspend callback to suspend it and 1913 * change its runtime PM status field to RPM_SUSPENDED. Also, if the device's 1914 * usage and children counters don't indicate that the device was in use before 1915 * the system-wide transition under way, decrement its parent's children counter 1916 * (if there is a parent). Keep runtime PM disabled to preserve the state 1917 * unless we encounter errors. 1918 * 1919 * Typically this function may be invoked from a system suspend callback to make 1920 * sure the device is put into low power state and it should only be used during 1921 * system-wide PM transitions to sleep states. It assumes that the analogous 1922 * pm_runtime_force_resume() will be used to resume the device. 1923 * 1924 * Do not use with DPM_FLAG_SMART_SUSPEND as this can lead to an inconsistent 1925 * state where this function has called the ->runtime_suspend callback but the 1926 * PM core marks the driver as runtime active. 1927 */ 1928 int pm_runtime_force_suspend(struct device *dev) 1929 { 1930 int (*callback)(struct device *); 1931 int ret; 1932 1933 pm_runtime_disable(dev); 1934 if (pm_runtime_status_suspended(dev)) 1935 return 0; 1936 1937 callback = RPM_GET_CALLBACK(dev, runtime_suspend); 1938 1939 dev_pm_enable_wake_irq_check(dev, true); 1940 ret = callback ? callback(dev) : 0; 1941 if (ret) 1942 goto err; 1943 1944 dev_pm_enable_wake_irq_complete(dev); 1945 1946 /* 1947 * If the device can stay in suspend after the system-wide transition 1948 * to the working state that will follow, drop the children counter of 1949 * its parent, but set its status to RPM_SUSPENDED anyway in case this 1950 * function will be called again for it in the meantime. 1951 */ 1952 if (pm_runtime_need_not_resume(dev)) { 1953 pm_runtime_set_suspended(dev); 1954 } else { 1955 __update_runtime_status(dev, RPM_SUSPENDED); 1956 dev->power.needs_force_resume = 1; 1957 } 1958 1959 return 0; 1960 1961 err: 1962 dev_pm_disable_wake_irq_check(dev, true); 1963 pm_runtime_enable(dev); 1964 return ret; 1965 } 1966 EXPORT_SYMBOL_GPL(pm_runtime_force_suspend); 1967 1968 /** 1969 * pm_runtime_force_resume - Force a device into resume state if needed. 1970 * @dev: Device to resume. 1971 * 1972 * Prior invoking this function we expect the user to have brought the device 1973 * into low power state by a call to pm_runtime_force_suspend(). Here we reverse 1974 * those actions and bring the device into full power, if it is expected to be 1975 * used on system resume. In the other case, we defer the resume to be managed 1976 * via runtime PM. 1977 * 1978 * Typically this function may be invoked from a system resume callback. 1979 */ 1980 int pm_runtime_force_resume(struct device *dev) 1981 { 1982 int (*callback)(struct device *); 1983 int ret = 0; 1984 1985 if (!dev->power.needs_force_resume) 1986 goto out; 1987 1988 /* 1989 * The value of the parent's children counter is correct already, so 1990 * just update the status of the device. 1991 */ 1992 __update_runtime_status(dev, RPM_ACTIVE); 1993 1994 callback = RPM_GET_CALLBACK(dev, runtime_resume); 1995 1996 dev_pm_disable_wake_irq_check(dev, false); 1997 ret = callback ? callback(dev) : 0; 1998 if (ret) { 1999 pm_runtime_set_suspended(dev); 2000 dev_pm_enable_wake_irq_check(dev, false); 2001 goto out; 2002 } 2003 2004 pm_runtime_mark_last_busy(dev); 2005 out: 2006 dev->power.needs_force_resume = 0; 2007 pm_runtime_enable(dev); 2008 return ret; 2009 } 2010 EXPORT_SYMBOL_GPL(pm_runtime_force_resume); 2011