PM / Runtime: Implement autosuspend support

This patch (as1427) implements the "autosuspend" facility for runtime
PM. A few new fields are added to the dev_pm_info structure and
several new PM helper functions are defined, for telling the PM core
whether or not a device uses autosuspend, for setting the autosuspend
delay, and for marking periods of device activity.

Drivers that do not want to use autosuspend can continue using the
same helper functions as before; their behavior will not change. In
addition, drivers supporting autosuspend can also call the old helper
functions to get the old behavior.

The details are all explained in Documentation/power/runtime_pm.txt
and Documentation/ABI/testing/sysfs-devices-power.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>

authored by Alan Stern and committed by Rafael J. Wysocki 15bcb91d 7490e442

+473 -14
+18
Documentation/ABI/testing/sysfs-devices-power
··· 147 147 milliseconds. This attribute is read-only. If the device is 148 148 not enabled to wake up the system from sleep states, this 149 149 attribute is empty. 150 + 151 + What: /sys/devices/.../power/autosuspend_delay_ms 152 + Date: September 2010 153 + Contact: Alan Stern <stern@rowland.harvard.edu> 154 + Description: 155 + The /sys/devices/.../power/autosuspend_delay_ms attribute 156 + contains the autosuspend delay value (in milliseconds). Some 157 + drivers do not want their device to suspend as soon as it 158 + becomes idle at run time; they want the device to remain 159 + inactive for a certain minimum period of time first. That 160 + period is called the autosuspend delay. Negative values will 161 + prevent the device from being suspended at run time (similar 162 + to writing "on" to the power/control attribute). Values >= 163 + 1000 will cause the autosuspend timer expiration to be rounded 164 + up to the nearest second. 165 + 166 + Not all drivers support this attribute. If it isn't supported, 167 + attempts to read or write it will yield I/O errors.
+183 -7
Documentation/power/runtime_pm.txt
··· 158 158 to execute it, the other callbacks will not be executed for the same device. 159 159 160 160 * A request to execute ->runtime_resume() will cancel any pending or 161 - scheduled requests to execute the other callbacks for the same device. 161 + scheduled requests to execute the other callbacks for the same device, 162 + except for scheduled autosuspends. 162 163 163 164 3. Run-time PM Device Fields 164 165 ··· 167 166 defined in include/linux/pm.h: 168 167 169 168 struct timer_list suspend_timer; 170 - - timer used for scheduling (delayed) suspend request 169 + - timer used for scheduling (delayed) suspend and autosuspend requests 171 170 172 171 unsigned long timer_expires; 173 172 - timer expiration time, in jiffies (if this is different from zero, the ··· 237 236 Section 8); it may be modified only by the pm_runtime_no_callbacks() 238 237 helper function 239 238 239 + unsigned int use_autosuspend; 240 + - indicates that the device's driver supports delayed autosuspend (see 241 + Section 9); it may be modified only by the 242 + pm_runtime{_dont}_use_autosuspend() helper functions 243 + 244 + unsigned int timer_autosuspends; 245 + - indicates that the PM core should attempt to carry out an autosuspend 246 + when the timer expires rather than a normal suspend 247 + 248 + int autosuspend_delay; 249 + - the delay time (in milliseconds) to be used for autosuspend 250 + 251 + unsigned long last_busy; 252 + - the time (in jiffies) when the pm_runtime_mark_last_busy() helper 253 + function was last called for this device; used in calculating inactivity 254 + periods for autosuspend 255 + 240 256 All of the above fields are members of the 'power' member of 'struct device'. 241 257 242 258 4. Run-time PM Device Helper Functions ··· 279 261 error code on failure, where -EAGAIN or -EBUSY means it is safe to attempt 280 262 to suspend the device again in future 281 263 264 + int pm_runtime_autosuspend(struct device *dev); 265 + - same as pm_runtime_suspend() except that the autosuspend delay is taken 266 + into account; if pm_runtime_autosuspend_expiration() says the delay has 267 + not yet expired then an autosuspend is scheduled for the appropriate time 268 + and 0 is returned 269 + 282 270 int pm_runtime_resume(struct device *dev); 283 271 - execute the subsystem-level resume callback for the device; returns 0 on 284 272 success, 1 if the device's run-time PM status was already 'active' or ··· 296 272 - submit a request to execute the subsystem-level idle callback for the 297 273 device (the request is represented by a work item in pm_wq); returns 0 on 298 274 success or error code if the request has not been queued up 275 + 276 + int pm_request_autosuspend(struct device *dev); 277 + - schedule the execution of the subsystem-level suspend callback for the 278 + device when the autosuspend delay has expired; if the delay has already 279 + expired then the work item is queued up immediately 299 280 300 281 int pm_schedule_suspend(struct device *dev, unsigned int delay); 301 282 - schedule the execution of the subsystem-level suspend callback for the ··· 333 304 - decrement the device's usage counter 334 305 335 306 int pm_runtime_put(struct device *dev); 336 - - decrement the device's usage counter, run pm_request_idle(dev) and return 337 - its result 307 + - decrement the device's usage counter; if the result is 0 then run 308 + pm_request_idle(dev) and return its result 309 + 310 + int pm_runtime_put_autosuspend(struct device *dev); 311 + - decrement the device's usage counter; if the result is 0 then run 312 + pm_request_autosuspend(dev) and return its result 338 313 339 314 int pm_runtime_put_sync(struct device *dev); 340 - - decrement the device's usage counter, run pm_runtime_idle(dev) and return 341 - its result 315 + - decrement the device's usage counter; if the result is 0 then run 316 + pm_runtime_idle(dev) and return its result 317 + 318 + int pm_runtime_put_sync_autosuspend(struct device *dev); 319 + - decrement the device's usage counter; if the result is 0 then run 320 + pm_runtime_autosuspend(dev) and return its result 342 321 343 322 void pm_runtime_enable(struct device *dev); 344 323 - enable the run-time PM helper functions to run the device bus type's ··· 397 360 PM attributes from /sys/devices/.../power (or prevent them from being 398 361 added when the device is registered) 399 362 363 + void pm_runtime_mark_last_busy(struct device *dev); 364 + - set the power.last_busy field to the current time 365 + 366 + void pm_runtime_use_autosuspend(struct device *dev); 367 + - set the power.use_autosuspend flag, enabling autosuspend delays 368 + 369 + void pm_runtime_dont_use_autosuspend(struct device *dev); 370 + - clear the power.use_autosuspend flag, disabling autosuspend delays 371 + 372 + void pm_runtime_set_autosuspend_delay(struct device *dev, int delay); 373 + - set the power.autosuspend_delay value to 'delay' (expressed in 374 + milliseconds); if 'delay' is negative then run-time suspends are 375 + prevented 376 + 377 + unsigned long pm_runtime_autosuspend_expiration(struct device *dev); 378 + - calculate the time when the current autosuspend delay period will expire, 379 + based on power.last_busy and power.autosuspend_delay; if the delay time 380 + is 1000 ms or larger then the expiration time is rounded up to the 381 + nearest second; returns 0 if the delay period has already expired or 382 + power.use_autosuspend isn't set, otherwise returns the expiration time 383 + in jiffies 384 + 400 385 It is safe to execute the following helper functions from interrupt context: 401 386 402 387 pm_request_idle() 388 + pm_request_autosuspend() 403 389 pm_schedule_suspend() 404 390 pm_request_resume() 405 391 pm_runtime_get_noresume() 406 392 pm_runtime_get() 407 393 pm_runtime_put_noidle() 408 394 pm_runtime_put() 395 + pm_runtime_put_autosuspend() 396 + pm_runtime_enable() 409 397 pm_suspend_ignore_children() 410 398 pm_runtime_set_active() 411 399 pm_runtime_set_suspended() 412 - pm_runtime_enable() 400 + pm_runtime_suspended() 401 + pm_runtime_mark_last_busy() 402 + pm_runtime_autosuspend_expiration() 413 403 414 404 5. Run-time PM Initialization, Device Probing and Removal 415 405 ··· 625 561 or driver about run-time power changes. Instead, the driver for the device's 626 562 parent must take responsibility for telling the device's driver when the 627 563 parent's power state changes. 564 + 565 + 9. Autosuspend, or automatically-delayed suspends 566 + 567 + Changing a device's power state isn't free; it requires both time and energy. 568 + A device should be put in a low-power state only when there's some reason to 569 + think it will remain in that state for a substantial time. A common heuristic 570 + says that a device which hasn't been used for a while is liable to remain 571 + unused; following this advice, drivers should not allow devices to be suspended 572 + at run-time until they have been inactive for some minimum period. Even when 573 + the heuristic ends up being non-optimal, it will still prevent devices from 574 + "bouncing" too rapidly between low-power and full-power states. 575 + 576 + The term "autosuspend" is an historical remnant. It doesn't mean that the 577 + device is automatically suspended (the subsystem or driver still has to call 578 + the appropriate PM routines); rather it means that run-time suspends will 579 + automatically be delayed until the desired period of inactivity has elapsed. 580 + 581 + Inactivity is determined based on the power.last_busy field. Drivers should 582 + call pm_runtime_mark_last_busy() to update this field after carrying out I/O, 583 + typically just before calling pm_runtime_put_autosuspend(). The desired length 584 + of the inactivity period is a matter of policy. Subsystems can set this length 585 + initially by calling pm_runtime_set_autosuspend_delay(), but after device 586 + registration the length should be controlled by user space, using the 587 + /sys/devices/.../power/autosuspend_delay_ms attribute. 588 + 589 + In order to use autosuspend, subsystems or drivers must call 590 + pm_runtime_use_autosuspend() (preferably before registering the device), and 591 + thereafter they should use the various *_autosuspend() helper functions instead 592 + of the non-autosuspend counterparts: 593 + 594 + Instead of: pm_runtime_suspend use: pm_runtime_autosuspend; 595 + Instead of: pm_schedule_suspend use: pm_request_autosuspend; 596 + Instead of: pm_runtime_put use: pm_runtime_put_autosuspend; 597 + Instead of: pm_runtime_put_sync use: pm_runtime_put_sync_autosuspend. 598 + 599 + Drivers may also continue to use the non-autosuspend helper functions; they 600 + will behave normally, not taking the autosuspend delay into account. 601 + Similarly, if the power.use_autosuspend field isn't set then the autosuspend 602 + helper functions will behave just like the non-autosuspend counterparts. 603 + 604 + The implementation is well suited for asynchronous use in interrupt contexts. 605 + However such use inevitably involves races, because the PM core can't 606 + synchronize ->runtime_suspend() callbacks with the arrival of I/O requests. 607 + This synchronization must be handled by the driver, using its private lock. 608 + Here is a schematic pseudo-code example: 609 + 610 + foo_read_or_write(struct foo_priv *foo, void *data) 611 + { 612 + lock(&foo->private_lock); 613 + add_request_to_io_queue(foo, data); 614 + if (foo->num_pending_requests++ == 0) 615 + pm_runtime_get(&foo->dev); 616 + if (!foo->is_suspended) 617 + foo_process_next_request(foo); 618 + unlock(&foo->private_lock); 619 + } 620 + 621 + foo_io_completion(struct foo_priv *foo, void *req) 622 + { 623 + lock(&foo->private_lock); 624 + if (--foo->num_pending_requests == 0) { 625 + pm_runtime_mark_last_busy(&foo->dev); 626 + pm_runtime_put_autosuspend(&foo->dev); 627 + } else { 628 + foo_process_next_request(foo); 629 + } 630 + unlock(&foo->private_lock); 631 + /* Send req result back to the user ... */ 632 + } 633 + 634 + int foo_runtime_suspend(struct device *dev) 635 + { 636 + struct foo_priv foo = container_of(dev, ...); 637 + int ret = 0; 638 + 639 + lock(&foo->private_lock); 640 + if (foo->num_pending_requests > 0) { 641 + ret = -EBUSY; 642 + } else { 643 + /* ... suspend the device ... */ 644 + foo->is_suspended = 1; 645 + } 646 + unlock(&foo->private_lock); 647 + return ret; 648 + } 649 + 650 + int foo_runtime_resume(struct device *dev) 651 + { 652 + struct foo_priv foo = container_of(dev, ...); 653 + 654 + lock(&foo->private_lock); 655 + /* ... resume the device ... */ 656 + foo->is_suspended = 0; 657 + pm_runtime_mark_last_busy(&foo->dev); 658 + if (foo->num_pending_requests > 0) 659 + foo_process_requests(foo); 660 + unlock(&foo->private_lock); 661 + return 0; 662 + } 663 + 664 + The important point is that after foo_io_completion() asks for an autosuspend, 665 + the foo_runtime_suspend() callback may race with foo_read_or_write(). 666 + Therefore foo_runtime_suspend() has to check whether there are any pending I/O 667 + requests (while holding the private lock) before allowing the suspend to 668 + proceed. 669 + 670 + In addition, the power.autosuspend_delay field can be changed by user space at 671 + any time. If a driver cares about this, it can call 672 + pm_runtime_autosuspend_expiration() from within the ->runtime_suspend() 673 + callback while holding its private lock. If the function returns a nonzero 674 + value then the delay has not yet expired and the callback should return 675 + -EAGAIN.
+179 -7
drivers/base/power/runtime.c
··· 9 9 10 10 #include <linux/sched.h> 11 11 #include <linux/pm_runtime.h> 12 - #include <linux/jiffies.h> 13 12 #include "power.h" 14 13 15 14 static int rpm_resume(struct device *dev, int rpmflags); ··· 77 78 */ 78 79 dev->power.request = RPM_REQ_NONE; 79 80 } 81 + 82 + /* 83 + * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time. 84 + * @dev: Device to handle. 85 + * 86 + * Compute the autosuspend-delay expiration time based on the device's 87 + * power.last_busy time. If the delay has already expired or is disabled 88 + * (negative) or the power.use_autosuspend flag isn't set, return 0. 89 + * Otherwise return the expiration time in jiffies (adjusted to be nonzero). 90 + * 91 + * This function may be called either with or without dev->power.lock held. 92 + * Either way it can be racy, since power.last_busy may be updated at any time. 93 + */ 94 + unsigned long pm_runtime_autosuspend_expiration(struct device *dev) 95 + { 96 + int autosuspend_delay; 97 + long elapsed; 98 + unsigned long last_busy; 99 + unsigned long expires = 0; 100 + 101 + if (!dev->power.use_autosuspend) 102 + goto out; 103 + 104 + autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay); 105 + if (autosuspend_delay < 0) 106 + goto out; 107 + 108 + last_busy = ACCESS_ONCE(dev->power.last_busy); 109 + elapsed = jiffies - last_busy; 110 + if (elapsed < 0) 111 + goto out; /* jiffies has wrapped around. */ 112 + 113 + /* 114 + * If the autosuspend_delay is >= 1 second, align the timer by rounding 115 + * up to the nearest second. 116 + */ 117 + expires = last_busy + msecs_to_jiffies(autosuspend_delay); 118 + if (autosuspend_delay >= 1000) 119 + expires = round_jiffies(expires); 120 + expires += !expires; 121 + if (elapsed >= expires - last_busy) 122 + expires = 0; /* Already expired. */ 123 + 124 + out: 125 + return expires; 126 + } 127 + EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration); 80 128 81 129 /** 82 130 * rpm_check_suspend_allowed - Test whether a device may be suspended. ··· 280 234 if (retval) 281 235 goto out; 282 236 237 + /* If the autosuspend_delay time hasn't expired yet, reschedule. */ 238 + if ((rpmflags & RPM_AUTO) 239 + && dev->power.runtime_status != RPM_SUSPENDING) { 240 + unsigned long expires = pm_runtime_autosuspend_expiration(dev); 241 + 242 + if (expires != 0) { 243 + /* Pending requests need to be canceled. */ 244 + dev->power.request = RPM_REQ_NONE; 245 + 246 + /* 247 + * Optimization: If the timer is already running and is 248 + * set to expire at or before the autosuspend delay, 249 + * avoid the overhead of resetting it. Just let it 250 + * expire; pm_suspend_timer_fn() will take care of the 251 + * rest. 252 + */ 253 + if (!(dev->power.timer_expires && time_before_eq( 254 + dev->power.timer_expires, expires))) { 255 + dev->power.timer_expires = expires; 256 + mod_timer(&dev->power.suspend_timer, expires); 257 + } 258 + dev->power.timer_autosuspends = 1; 259 + goto out; 260 + } 261 + } 262 + 283 263 /* Other scheduled or pending requests need to be canceled. */ 284 264 pm_runtime_cancel_pending(dev); 285 265 ··· 340 268 341 269 /* Carry out an asynchronous or a synchronous suspend. */ 342 270 if (rpmflags & RPM_ASYNC) { 343 - dev->power.request = RPM_REQ_SUSPEND; 271 + dev->power.request = (rpmflags & RPM_AUTO) ? 272 + RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND; 344 273 if (!dev->power.request_pending) { 345 274 dev->power.request_pending = true; 346 275 queue_work(pm_wq, &dev->power.work); ··· 456 383 if (retval) 457 384 goto out; 458 385 459 - /* Other scheduled or pending requests need to be canceled. */ 460 - pm_runtime_cancel_pending(dev); 386 + /* 387 + * Other scheduled or pending requests need to be canceled. Small 388 + * optimization: If an autosuspend timer is running, leave it running 389 + * rather than cancelling it now only to restart it again in the near 390 + * future. 391 + */ 392 + dev->power.request = RPM_REQ_NONE; 393 + if (!dev->power.timer_autosuspends) 394 + pm_runtime_deactivate_timer(dev); 461 395 462 396 if (dev->power.runtime_status == RPM_ACTIVE) { 463 397 retval = 1; ··· 648 568 case RPM_REQ_SUSPEND: 649 569 rpm_suspend(dev, RPM_NOWAIT); 650 570 break; 571 + case RPM_REQ_AUTOSUSPEND: 572 + rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO); 573 + break; 651 574 case RPM_REQ_RESUME: 652 575 rpm_resume(dev, RPM_NOWAIT); 653 576 break; ··· 678 595 /* If 'expire' is after 'jiffies' we've been called too early. */ 679 596 if (expires > 0 && !time_after(expires, jiffies)) { 680 597 dev->power.timer_expires = 0; 681 - rpm_suspend(dev, RPM_ASYNC); 598 + rpm_suspend(dev, dev->power.timer_autosuspends ? 599 + (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC); 682 600 } 683 601 684 602 spin_unlock_irqrestore(&dev->power.lock, flags); ··· 711 627 712 628 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay); 713 629 dev->power.timer_expires += !dev->power.timer_expires; 630 + dev->power.timer_autosuspends = 0; 714 631 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires); 715 632 716 633 out: ··· 755 670 * @dev: Device to suspend. 756 671 * @rpmflags: Flag bits. 757 672 * 758 - * Carry out a suspend, either synchronous or asynchronous. 673 + * If the RPM_GET_PUT flag is set, decrement the device's usage count and 674 + * return immediately if it is larger than zero. Then carry out a suspend, 675 + * either synchronous or asynchronous. 759 676 * 760 677 * This routine may be called in atomic context if the RPM_ASYNC flag is set. 761 678 */ ··· 765 678 { 766 679 unsigned long flags; 767 680 int retval; 681 + 682 + if (rpmflags & RPM_GET_PUT) { 683 + if (!atomic_dec_and_test(&dev->power.usage_count)) 684 + return 0; 685 + } 768 686 769 687 spin_lock_irqsave(&dev->power.lock, flags); 770 688 retval = rpm_suspend(dev, rpmflags); ··· 1072 980 1073 981 dev->power.runtime_auto = true; 1074 982 if (atomic_dec_and_test(&dev->power.usage_count)) 1075 - rpm_idle(dev, 0); 983 + rpm_idle(dev, RPM_AUTO); 1076 984 1077 985 out: 1078 986 spin_unlock_irq(&dev->power.lock); ··· 1097 1005 rpm_sysfs_remove(dev); 1098 1006 } 1099 1007 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks); 1008 + 1009 + /** 1010 + * update_autosuspend - Handle a change to a device's autosuspend settings. 1011 + * @dev: Device to handle. 1012 + * @old_delay: The former autosuspend_delay value. 1013 + * @old_use: The former use_autosuspend value. 1014 + * 1015 + * Prevent runtime suspend if the new delay is negative and use_autosuspend is 1016 + * set; otherwise allow it. Send an idle notification if suspends are allowed. 1017 + * 1018 + * This function must be called under dev->power.lock with interrupts disabled. 1019 + */ 1020 + static void update_autosuspend(struct device *dev, int old_delay, int old_use) 1021 + { 1022 + int delay = dev->power.autosuspend_delay; 1023 + 1024 + /* Should runtime suspend be prevented now? */ 1025 + if (dev->power.use_autosuspend && delay < 0) { 1026 + 1027 + /* If it used to be allowed then prevent it. */ 1028 + if (!old_use || old_delay >= 0) { 1029 + atomic_inc(&dev->power.usage_count); 1030 + rpm_resume(dev, 0); 1031 + } 1032 + } 1033 + 1034 + /* Runtime suspend should be allowed now. */ 1035 + else { 1036 + 1037 + /* If it used to be prevented then allow it. */ 1038 + if (old_use && old_delay < 0) 1039 + atomic_dec(&dev->power.usage_count); 1040 + 1041 + /* Maybe we can autosuspend now. */ 1042 + rpm_idle(dev, RPM_AUTO); 1043 + } 1044 + } 1045 + 1046 + /** 1047 + * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value. 1048 + * @dev: Device to handle. 1049 + * @delay: Value of the new delay in milliseconds. 1050 + * 1051 + * Set the device's power.autosuspend_delay value. If it changes to negative 1052 + * and the power.use_autosuspend flag is set, prevent run-time suspends. If it 1053 + * changes the other way, allow run-time suspends. 1054 + */ 1055 + void pm_runtime_set_autosuspend_delay(struct device *dev, int delay) 1056 + { 1057 + int old_delay, old_use; 1058 + 1059 + spin_lock_irq(&dev->power.lock); 1060 + old_delay = dev->power.autosuspend_delay; 1061 + old_use = dev->power.use_autosuspend; 1062 + dev->power.autosuspend_delay = delay; 1063 + update_autosuspend(dev, old_delay, old_use); 1064 + spin_unlock_irq(&dev->power.lock); 1065 + } 1066 + EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay); 1067 + 1068 + /** 1069 + * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag. 1070 + * @dev: Device to handle. 1071 + * @use: New value for use_autosuspend. 1072 + * 1073 + * Set the device's power.use_autosuspend flag, and allow or prevent run-time 1074 + * suspends as needed. 1075 + */ 1076 + void __pm_runtime_use_autosuspend(struct device *dev, bool use) 1077 + { 1078 + int old_delay, old_use; 1079 + 1080 + spin_lock_irq(&dev->power.lock); 1081 + old_delay = dev->power.autosuspend_delay; 1082 + old_use = dev->power.use_autosuspend; 1083 + dev->power.use_autosuspend = use; 1084 + update_autosuspend(dev, old_delay, old_use); 1085 + spin_unlock_irq(&dev->power.lock); 1086 + } 1087 + EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend); 1100 1088 1101 1089 /** 1102 1090 * pm_runtime_init - Initialize run-time PM fields in given device object.
+40
drivers/base/power/sysfs.c
··· 75 75 * attribute is set to "enabled" by bus type code or device drivers and in 76 76 * that cases it should be safe to leave the default value. 77 77 * 78 + * autosuspend_delay_ms - Report/change a device's autosuspend_delay value 79 + * 80 + * Some drivers don't want to carry out a runtime suspend as soon as a 81 + * device becomes idle; they want it always to remain idle for some period 82 + * of time before suspending it. This period is the autosuspend_delay 83 + * value (expressed in milliseconds) and it can be controlled by the user. 84 + * If the value is negative then the device will never be runtime 85 + * suspended. 86 + * 87 + * NOTE: The autosuspend_delay_ms attribute and the autosuspend_delay 88 + * value are used only if the driver calls pm_runtime_use_autosuspend(). 89 + * 78 90 * wakeup_count - Report the number of wakeup events related to the device 79 91 */ 80 92 ··· 185 173 } 186 174 187 175 static DEVICE_ATTR(runtime_status, 0444, rtpm_status_show, NULL); 176 + 177 + static ssize_t autosuspend_delay_ms_show(struct device *dev, 178 + struct device_attribute *attr, char *buf) 179 + { 180 + if (!dev->power.use_autosuspend) 181 + return -EIO; 182 + return sprintf(buf, "%d\n", dev->power.autosuspend_delay); 183 + } 184 + 185 + static ssize_t autosuspend_delay_ms_store(struct device *dev, 186 + struct device_attribute *attr, const char *buf, size_t n) 187 + { 188 + long delay; 189 + 190 + if (!dev->power.use_autosuspend) 191 + return -EIO; 192 + 193 + if (strict_strtol(buf, 10, &delay) != 0 || delay != (int) delay) 194 + return -EINVAL; 195 + 196 + pm_runtime_set_autosuspend_delay(dev, delay); 197 + return n; 198 + } 199 + 200 + static DEVICE_ATTR(autosuspend_delay_ms, 0644, autosuspend_delay_ms_show, 201 + autosuspend_delay_ms_store); 202 + 188 203 #endif 189 204 190 205 static ssize_t ··· 467 428 &dev_attr_control.attr, 468 429 &dev_attr_runtime_suspended_time.attr, 469 430 &dev_attr_runtime_active_time.attr, 431 + &dev_attr_autosuspend_delay_ms.attr, 470 432 NULL, 471 433 }; 472 434 static struct attribute_group pm_runtime_attr_group = {
+8
include/linux/pm.h
··· 444 444 * 445 445 * RPM_REQ_SUSPEND Run the device bus type's ->runtime_suspend() callback 446 446 * 447 + * RPM_REQ_AUTOSUSPEND Same as RPM_REQ_SUSPEND, but not until the device has 448 + * been inactive for as long as power.autosuspend_delay 449 + * 447 450 * RPM_REQ_RESUME Run the device bus type's ->runtime_resume() callback 448 451 */ 449 452 ··· 454 451 RPM_REQ_NONE = 0, 455 452 RPM_REQ_IDLE, 456 453 RPM_REQ_SUSPEND, 454 + RPM_REQ_AUTOSUSPEND, 457 455 RPM_REQ_RESUME, 458 456 }; 459 457 ··· 486 482 unsigned int run_wake:1; 487 483 unsigned int runtime_auto:1; 488 484 unsigned int no_callbacks:1; 485 + unsigned int use_autosuspend:1; 486 + unsigned int timer_autosuspends:1; 489 487 enum rpm_request request; 490 488 enum rpm_status runtime_status; 491 489 int runtime_error; 490 + int autosuspend_delay; 491 + unsigned long last_busy; 492 492 unsigned long active_jiffies; 493 493 unsigned long suspended_jiffies; 494 494 unsigned long accounting_timestamp;
+45
include/linux/pm_runtime.h
··· 12 12 #include <linux/device.h> 13 13 #include <linux/pm.h> 14 14 15 + #include <linux/jiffies.h> 16 + 15 17 /* Runtime PM flag argument bits */ 16 18 #define RPM_ASYNC 0x01 /* Request is asynchronous */ 17 19 #define RPM_NOWAIT 0x02 /* Don't wait for concurrent 18 20 state change */ 19 21 #define RPM_GET_PUT 0x04 /* Increment/decrement the 20 22 usage_count */ 23 + #define RPM_AUTO 0x08 /* Use autosuspend_delay */ 21 24 22 25 #ifdef CONFIG_PM_RUNTIME 23 26 ··· 40 37 extern int pm_generic_runtime_suspend(struct device *dev); 41 38 extern int pm_generic_runtime_resume(struct device *dev); 42 39 extern void pm_runtime_no_callbacks(struct device *dev); 40 + extern void __pm_runtime_use_autosuspend(struct device *dev, bool use); 41 + extern void pm_runtime_set_autosuspend_delay(struct device *dev, int delay); 42 + extern unsigned long pm_runtime_autosuspend_expiration(struct device *dev); 43 43 44 44 static inline bool pm_children_suspended(struct device *dev) 45 45 { ··· 78 72 static inline bool pm_runtime_suspended(struct device *dev) 79 73 { 80 74 return dev->power.runtime_status == RPM_SUSPENDED; 75 + } 76 + 77 + static inline void pm_runtime_mark_last_busy(struct device *dev) 78 + { 79 + ACCESS_ONCE(dev->power.last_busy) = jiffies; 81 80 } 82 81 83 82 #else /* !CONFIG_PM_RUNTIME */ ··· 124 113 static inline int pm_generic_runtime_resume(struct device *dev) { return 0; } 125 114 static inline void pm_runtime_no_callbacks(struct device *dev) {} 126 115 116 + static inline void pm_runtime_mark_last_busy(struct device *dev) {} 117 + static inline void __pm_runtime_use_autosuspend(struct device *dev, 118 + bool use) {} 119 + static inline void pm_runtime_set_autosuspend_delay(struct device *dev, 120 + int delay) {} 121 + static inline unsigned long pm_runtime_autosuspend_expiration( 122 + struct device *dev) { return 0; } 123 + 127 124 #endif /* !CONFIG_PM_RUNTIME */ 128 125 129 126 static inline int pm_runtime_idle(struct device *dev) ··· 142 123 static inline int pm_runtime_suspend(struct device *dev) 143 124 { 144 125 return __pm_runtime_suspend(dev, 0); 126 + } 127 + 128 + static inline int pm_runtime_autosuspend(struct device *dev) 129 + { 130 + return __pm_runtime_suspend(dev, RPM_AUTO); 145 131 } 146 132 147 133 static inline int pm_runtime_resume(struct device *dev) ··· 179 155 return __pm_runtime_idle(dev, RPM_GET_PUT | RPM_ASYNC); 180 156 } 181 157 158 + static inline int pm_runtime_put_autosuspend(struct device *dev) 159 + { 160 + return __pm_runtime_suspend(dev, 161 + RPM_GET_PUT | RPM_ASYNC | RPM_AUTO); 162 + } 163 + 182 164 static inline int pm_runtime_put_sync(struct device *dev) 183 165 { 184 166 return __pm_runtime_idle(dev, RPM_GET_PUT); 167 + } 168 + 169 + static inline int pm_runtime_put_sync_autosuspend(struct device *dev) 170 + { 171 + return __pm_runtime_suspend(dev, RPM_GET_PUT | RPM_AUTO); 185 172 } 186 173 187 174 static inline int pm_runtime_set_active(struct device *dev) ··· 208 173 static inline void pm_runtime_disable(struct device *dev) 209 174 { 210 175 __pm_runtime_disable(dev, true); 176 + } 177 + 178 + static inline void pm_runtime_use_autosuspend(struct device *dev) 179 + { 180 + __pm_runtime_use_autosuspend(dev, true); 181 + } 182 + 183 + static inline void pm_runtime_dont_use_autosuspend(struct device *dev) 184 + { 185 + __pm_runtime_use_autosuspend(dev, false); 211 186 } 212 187 213 188 #endif