2 * drivers/base/power/runtime.c - Helper functions for device run-time PM
4 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
5 * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
7 * This file is released under the GPLv2.
10 #include <linux/sched.h>
11 #include <linux/pm_runtime.h>
14 static int rpm_resume(struct device *dev, int rpmflags);
15 static int rpm_suspend(struct device *dev, int rpmflags);
18 * update_pm_runtime_accounting - Update the time accounting of power states
19 * @dev: Device to update the accounting for
21 * In order to be able to have time accounting of the various power states
22 * (as used by programs such as PowerTOP to show the effectiveness of runtime
23 * PM), we need to track the time spent in each state.
24 * update_pm_runtime_accounting must be called each time before the
25 * runtime_status field is updated, to account the time in the old state
28 void update_pm_runtime_accounting(struct device *dev)
30 unsigned long now = jiffies;
33 delta = now - dev->power.accounting_timestamp;
38 dev->power.accounting_timestamp = now;
40 if (dev->power.disable_depth > 0)
43 if (dev->power.runtime_status == RPM_SUSPENDED)
44 dev->power.suspended_jiffies += delta;
46 dev->power.active_jiffies += delta;
49 static void __update_runtime_status(struct device *dev, enum rpm_status status)
51 update_pm_runtime_accounting(dev);
52 dev->power.runtime_status = status;
56 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
57 * @dev: Device to handle.
59 static void pm_runtime_deactivate_timer(struct device *dev)
61 if (dev->power.timer_expires > 0) {
62 del_timer(&dev->power.suspend_timer);
63 dev->power.timer_expires = 0;
68 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
69 * @dev: Device to handle.
71 static void pm_runtime_cancel_pending(struct device *dev)
73 pm_runtime_deactivate_timer(dev);
75 * In case there's a request pending, make sure its work function will
76 * return without doing anything.
78 dev->power.request = RPM_REQ_NONE;
82 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
83 * @dev: Device to handle.
85 * Compute the autosuspend-delay expiration time based on the device's
86 * power.last_busy time. If the delay has already expired or is disabled
87 * (negative) or the power.use_autosuspend flag isn't set, return 0.
88 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
90 * This function may be called either with or without dev->power.lock held.
91 * Either way it can be racy, since power.last_busy may be updated at any time.
93 unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
95 int autosuspend_delay;
97 unsigned long last_busy;
98 unsigned long expires = 0;
100 if (!dev->power.use_autosuspend)
103 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
104 if (autosuspend_delay < 0)
107 last_busy = ACCESS_ONCE(dev->power.last_busy);
108 elapsed = jiffies - last_busy;
110 goto out; /* jiffies has wrapped around. */
113 * If the autosuspend_delay is >= 1 second, align the timer by rounding
114 * up to the nearest second.
116 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
117 if (autosuspend_delay >= 1000)
118 expires = round_jiffies(expires);
120 if (elapsed >= expires - last_busy)
121 expires = 0; /* Already expired. */
126 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
129 * rpm_check_suspend_allowed - Test whether a device may be suspended.
130 * @dev: Device to test.
132 static int rpm_check_suspend_allowed(struct device *dev)
136 if (dev->power.runtime_error)
138 else if (atomic_read(&dev->power.usage_count) > 0
139 || dev->power.disable_depth > 0)
141 else if (!pm_children_suspended(dev))
144 /* Pending resume requests take precedence over suspends. */
145 else if ((dev->power.deferred_resume
146 && dev->power.status == RPM_SUSPENDING)
147 || (dev->power.request_pending
148 && dev->power.request == RPM_REQ_RESUME))
150 else if (dev->power.runtime_status == RPM_SUSPENDED)
158 * rpm_idle - Notify device bus type if the device can be suspended.
159 * @dev: Device to notify the bus type about.
160 * @rpmflags: Flag bits.
162 * Check if the device's run-time PM status allows it to be suspended. If
163 * another idle notification has been started earlier, return immediately. If
164 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
165 * run the ->runtime_idle() callback directly.
167 * This function must be called under dev->power.lock with interrupts disabled.
169 static int rpm_idle(struct device *dev, int rpmflags)
170 __releases(&dev->power.lock) __acquires(&dev->power.lock)
174 retval = rpm_check_suspend_allowed(dev);
176 ; /* Conditions are wrong. */
178 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
179 else if (dev->power.runtime_status != RPM_ACTIVE)
183 * Any pending request other than an idle notification takes
184 * precedence over us, except that the timer may be running.
186 else if (dev->power.request_pending &&
187 dev->power.request > RPM_REQ_IDLE)
190 /* Act as though RPM_NOWAIT is always set. */
191 else if (dev->power.idle_notification)
192 retval = -EINPROGRESS;
196 /* Pending requests need to be canceled. */
197 dev->power.request = RPM_REQ_NONE;
199 if (dev->power.no_callbacks) {
200 /* Assume ->runtime_idle() callback would have suspended. */
201 retval = rpm_suspend(dev, rpmflags);
205 /* Carry out an asynchronous or a synchronous idle notification. */
206 if (rpmflags & RPM_ASYNC) {
207 dev->power.request = RPM_REQ_IDLE;
208 if (!dev->power.request_pending) {
209 dev->power.request_pending = true;
210 queue_work(pm_wq, &dev->power.work);
215 dev->power.idle_notification = true;
217 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_idle) {
218 spin_unlock_irq(&dev->power.lock);
220 dev->bus->pm->runtime_idle(dev);
222 spin_lock_irq(&dev->power.lock);
223 } else if (dev->type && dev->type->pm && dev->type->pm->runtime_idle) {
224 spin_unlock_irq(&dev->power.lock);
226 dev->type->pm->runtime_idle(dev);
228 spin_lock_irq(&dev->power.lock);
229 } else if (dev->class && dev->class->pm
230 && dev->class->pm->runtime_idle) {
231 spin_unlock_irq(&dev->power.lock);
233 dev->class->pm->runtime_idle(dev);
235 spin_lock_irq(&dev->power.lock);
238 dev->power.idle_notification = false;
239 wake_up_all(&dev->power.wait_queue);
246 * rpm_suspend - Carry out run-time suspend of given device.
247 * @dev: Device to suspend.
248 * @rpmflags: Flag bits.
250 * Check if the device's run-time PM status allows it to be suspended. If
251 * another suspend has been started earlier, either return immediately or wait
252 * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a
253 * pending idle notification. If the RPM_ASYNC flag is set then queue a
254 * suspend request; otherwise run the ->runtime_suspend() callback directly.
255 * If a deferred resume was requested while the callback was running then carry
256 * it out; otherwise send an idle notification for the device (if the suspend
257 * failed) or for its parent (if the suspend succeeded).
259 * This function must be called under dev->power.lock with interrupts disabled.
261 static int rpm_suspend(struct device *dev, int rpmflags)
262 __releases(&dev->power.lock) __acquires(&dev->power.lock)
264 struct device *parent = NULL;
268 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
271 retval = rpm_check_suspend_allowed(dev);
274 ; /* Conditions are wrong. */
276 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
277 else if (dev->power.runtime_status == RPM_RESUMING &&
278 !(rpmflags & RPM_ASYNC))
283 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
284 if ((rpmflags & RPM_AUTO)
285 && dev->power.runtime_status != RPM_SUSPENDING) {
286 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
289 /* Pending requests need to be canceled. */
290 dev->power.request = RPM_REQ_NONE;
293 * Optimization: If the timer is already running and is
294 * set to expire at or before the autosuspend delay,
295 * avoid the overhead of resetting it. Just let it
296 * expire; pm_suspend_timer_fn() will take care of the
299 if (!(dev->power.timer_expires && time_before_eq(
300 dev->power.timer_expires, expires))) {
301 dev->power.timer_expires = expires;
302 mod_timer(&dev->power.suspend_timer, expires);
304 dev->power.timer_autosuspends = 1;
309 /* Other scheduled or pending requests need to be canceled. */
310 pm_runtime_cancel_pending(dev);
312 if (dev->power.runtime_status == RPM_SUSPENDING) {
315 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
316 retval = -EINPROGRESS;
320 /* Wait for the other suspend running in parallel with us. */
322 prepare_to_wait(&dev->power.wait_queue, &wait,
323 TASK_UNINTERRUPTIBLE);
324 if (dev->power.runtime_status != RPM_SUSPENDING)
327 spin_unlock_irq(&dev->power.lock);
331 spin_lock_irq(&dev->power.lock);
333 finish_wait(&dev->power.wait_queue, &wait);
337 dev->power.deferred_resume = false;
338 if (dev->power.no_callbacks)
339 goto no_callback; /* Assume success. */
341 /* Carry out an asynchronous or a synchronous suspend. */
342 if (rpmflags & RPM_ASYNC) {
343 dev->power.request = (rpmflags & RPM_AUTO) ?
344 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
345 if (!dev->power.request_pending) {
346 dev->power.request_pending = true;
347 queue_work(pm_wq, &dev->power.work);
352 __update_runtime_status(dev, RPM_SUSPENDING);
354 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_suspend) {
355 spin_unlock_irq(&dev->power.lock);
357 retval = dev->bus->pm->runtime_suspend(dev);
359 spin_lock_irq(&dev->power.lock);
360 dev->power.runtime_error = retval;
361 } else if (dev->type && dev->type->pm
362 && dev->type->pm->runtime_suspend) {
363 spin_unlock_irq(&dev->power.lock);
365 retval = dev->type->pm->runtime_suspend(dev);
367 spin_lock_irq(&dev->power.lock);
368 dev->power.runtime_error = retval;
369 } else if (dev->class && dev->class->pm
370 && dev->class->pm->runtime_suspend) {
371 spin_unlock_irq(&dev->power.lock);
373 retval = dev->class->pm->runtime_suspend(dev);
375 spin_lock_irq(&dev->power.lock);
376 dev->power.runtime_error = retval;
382 __update_runtime_status(dev, RPM_ACTIVE);
383 dev->power.deferred_resume = 0;
384 if (retval == -EAGAIN || retval == -EBUSY) {
385 if (dev->power.timer_expires == 0)
387 dev->power.runtime_error = 0;
389 pm_runtime_cancel_pending(dev);
393 __update_runtime_status(dev, RPM_SUSPENDED);
394 pm_runtime_deactivate_timer(dev);
397 parent = dev->parent;
398 atomic_add_unless(&parent->power.child_count, -1, 0);
401 wake_up_all(&dev->power.wait_queue);
403 if (dev->power.deferred_resume) {
412 if (parent && !parent->power.ignore_children) {
413 spin_unlock_irq(&dev->power.lock);
415 pm_request_idle(parent);
417 spin_lock_irq(&dev->power.lock);
421 dev_dbg(dev, "%s returns %d\n", __func__, retval);
427 * rpm_resume - Carry out run-time resume of given device.
428 * @dev: Device to resume.
429 * @rpmflags: Flag bits.
431 * Check if the device's run-time PM status allows it to be resumed. Cancel
432 * any scheduled or pending requests. If another resume has been started
433 * earlier, either return imediately or wait for it to finish, depending on the
434 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
435 * parallel with this function, either tell the other process to resume after
436 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
437 * flag is set then queue a resume request; otherwise run the
438 * ->runtime_resume() callback directly. Queue an idle notification for the
439 * device if the resume succeeded.
441 * This function must be called under dev->power.lock with interrupts disabled.
443 static int rpm_resume(struct device *dev, int rpmflags)
444 __releases(&dev->power.lock) __acquires(&dev->power.lock)
446 struct device *parent = NULL;
449 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
452 if (dev->power.runtime_error)
454 else if (dev->power.disable_depth > 0)
460 * Other scheduled or pending requests need to be canceled. Small
461 * optimization: If an autosuspend timer is running, leave it running
462 * rather than cancelling it now only to restart it again in the near
465 dev->power.request = RPM_REQ_NONE;
466 if (!dev->power.timer_autosuspends)
467 pm_runtime_deactivate_timer(dev);
469 if (dev->power.runtime_status == RPM_ACTIVE) {
474 if (dev->power.runtime_status == RPM_RESUMING
475 || dev->power.runtime_status == RPM_SUSPENDING) {
478 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
479 if (dev->power.runtime_status == RPM_SUSPENDING)
480 dev->power.deferred_resume = true;
482 retval = -EINPROGRESS;
486 /* Wait for the operation carried out in parallel with us. */
488 prepare_to_wait(&dev->power.wait_queue, &wait,
489 TASK_UNINTERRUPTIBLE);
490 if (dev->power.runtime_status != RPM_RESUMING
491 && dev->power.runtime_status != RPM_SUSPENDING)
494 spin_unlock_irq(&dev->power.lock);
498 spin_lock_irq(&dev->power.lock);
500 finish_wait(&dev->power.wait_queue, &wait);
505 * See if we can skip waking up the parent. This is safe only if
506 * power.no_callbacks is set, because otherwise we don't know whether
507 * the resume will actually succeed.
509 if (dev->power.no_callbacks && !parent && dev->parent) {
510 spin_lock(&dev->parent->power.lock);
511 if (dev->parent->power.disable_depth > 0
512 || dev->parent->power.ignore_children
513 || dev->parent->power.runtime_status == RPM_ACTIVE) {
514 atomic_inc(&dev->parent->power.child_count);
515 spin_unlock(&dev->parent->power.lock);
516 goto no_callback; /* Assume success. */
518 spin_unlock(&dev->parent->power.lock);
521 /* Carry out an asynchronous or a synchronous resume. */
522 if (rpmflags & RPM_ASYNC) {
523 dev->power.request = RPM_REQ_RESUME;
524 if (!dev->power.request_pending) {
525 dev->power.request_pending = true;
526 queue_work(pm_wq, &dev->power.work);
532 if (!parent && dev->parent) {
534 * Increment the parent's resume counter and resume it if
537 parent = dev->parent;
538 spin_unlock(&dev->power.lock);
540 pm_runtime_get_noresume(parent);
542 spin_lock(&parent->power.lock);
544 * We can resume if the parent's run-time PM is disabled or it
545 * is set to ignore children.
547 if (!parent->power.disable_depth
548 && !parent->power.ignore_children) {
549 rpm_resume(parent, 0);
550 if (parent->power.runtime_status != RPM_ACTIVE)
553 spin_unlock(&parent->power.lock);
555 spin_lock(&dev->power.lock);
561 if (dev->power.no_callbacks)
562 goto no_callback; /* Assume success. */
564 __update_runtime_status(dev, RPM_RESUMING);
566 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_resume) {
567 spin_unlock_irq(&dev->power.lock);
569 retval = dev->bus->pm->runtime_resume(dev);
571 spin_lock_irq(&dev->power.lock);
572 dev->power.runtime_error = retval;
573 } else if (dev->type && dev->type->pm
574 && dev->type->pm->runtime_resume) {
575 spin_unlock_irq(&dev->power.lock);
577 retval = dev->type->pm->runtime_resume(dev);
579 spin_lock_irq(&dev->power.lock);
580 dev->power.runtime_error = retval;
581 } else if (dev->class && dev->class->pm
582 && dev->class->pm->runtime_resume) {
583 spin_unlock_irq(&dev->power.lock);
585 retval = dev->class->pm->runtime_resume(dev);
587 spin_lock_irq(&dev->power.lock);
588 dev->power.runtime_error = retval;
594 __update_runtime_status(dev, RPM_SUSPENDED);
595 pm_runtime_cancel_pending(dev);
598 __update_runtime_status(dev, RPM_ACTIVE);
600 atomic_inc(&parent->power.child_count);
602 wake_up_all(&dev->power.wait_queue);
605 rpm_idle(dev, RPM_ASYNC);
609 spin_unlock_irq(&dev->power.lock);
611 pm_runtime_put(parent);
613 spin_lock_irq(&dev->power.lock);
616 dev_dbg(dev, "%s returns %d\n", __func__, retval);
622 * pm_runtime_work - Universal run-time PM work function.
623 * @work: Work structure used for scheduling the execution of this function.
625 * Use @work to get the device object the work is to be done for, determine what
626 * is to be done and execute the appropriate run-time PM function.
628 static void pm_runtime_work(struct work_struct *work)
630 struct device *dev = container_of(work, struct device, power.work);
631 enum rpm_request req;
633 spin_lock_irq(&dev->power.lock);
635 if (!dev->power.request_pending)
638 req = dev->power.request;
639 dev->power.request = RPM_REQ_NONE;
640 dev->power.request_pending = false;
646 rpm_idle(dev, RPM_NOWAIT);
648 case RPM_REQ_SUSPEND:
649 rpm_suspend(dev, RPM_NOWAIT);
651 case RPM_REQ_AUTOSUSPEND:
652 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
655 rpm_resume(dev, RPM_NOWAIT);
660 spin_unlock_irq(&dev->power.lock);
664 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
665 * @data: Device pointer passed by pm_schedule_suspend().
667 * Check if the time is right and queue a suspend request.
669 static void pm_suspend_timer_fn(unsigned long data)
671 struct device *dev = (struct device *)data;
673 unsigned long expires;
675 spin_lock_irqsave(&dev->power.lock, flags);
677 expires = dev->power.timer_expires;
678 /* If 'expire' is after 'jiffies' we've been called too early. */
679 if (expires > 0 && !time_after(expires, jiffies)) {
680 dev->power.timer_expires = 0;
681 rpm_suspend(dev, dev->power.timer_autosuspends ?
682 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
685 spin_unlock_irqrestore(&dev->power.lock, flags);
689 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
690 * @dev: Device to suspend.
691 * @delay: Time to wait before submitting a suspend request, in milliseconds.
693 int pm_schedule_suspend(struct device *dev, unsigned int delay)
698 spin_lock_irqsave(&dev->power.lock, flags);
701 retval = rpm_suspend(dev, RPM_ASYNC);
705 retval = rpm_check_suspend_allowed(dev);
709 /* Other scheduled or pending requests need to be canceled. */
710 pm_runtime_cancel_pending(dev);
712 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
713 dev->power.timer_expires += !dev->power.timer_expires;
714 dev->power.timer_autosuspends = 0;
715 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
718 spin_unlock_irqrestore(&dev->power.lock, flags);
722 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
725 * __pm_runtime_idle - Entry point for run-time idle operations.
726 * @dev: Device to send idle notification for.
727 * @rpmflags: Flag bits.
729 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
730 * return immediately if it is larger than zero. Then carry out an idle
731 * notification, either synchronous or asynchronous.
733 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
735 int __pm_runtime_idle(struct device *dev, int rpmflags)
740 if (rpmflags & RPM_GET_PUT) {
741 if (!atomic_dec_and_test(&dev->power.usage_count))
745 spin_lock_irqsave(&dev->power.lock, flags);
746 retval = rpm_idle(dev, rpmflags);
747 spin_unlock_irqrestore(&dev->power.lock, flags);
751 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
754 * __pm_runtime_suspend - Entry point for run-time put/suspend operations.
755 * @dev: Device to suspend.
756 * @rpmflags: Flag bits.
758 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
759 * return immediately if it is larger than zero. Then carry out a suspend,
760 * either synchronous or asynchronous.
762 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
764 int __pm_runtime_suspend(struct device *dev, int rpmflags)
769 if (rpmflags & RPM_GET_PUT) {
770 if (!atomic_dec_and_test(&dev->power.usage_count))
774 spin_lock_irqsave(&dev->power.lock, flags);
775 retval = rpm_suspend(dev, rpmflags);
776 spin_unlock_irqrestore(&dev->power.lock, flags);
780 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
783 * __pm_runtime_resume - Entry point for run-time resume operations.
784 * @dev: Device to resume.
785 * @rpmflags: Flag bits.
787 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
788 * carry out a resume, either synchronous or asynchronous.
790 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
792 int __pm_runtime_resume(struct device *dev, int rpmflags)
797 if (rpmflags & RPM_GET_PUT)
798 atomic_inc(&dev->power.usage_count);
800 spin_lock_irqsave(&dev->power.lock, flags);
801 retval = rpm_resume(dev, rpmflags);
802 spin_unlock_irqrestore(&dev->power.lock, flags);
806 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
809 * __pm_runtime_set_status - Set run-time PM status of a device.
810 * @dev: Device to handle.
811 * @status: New run-time PM status of the device.
813 * If run-time PM of the device is disabled or its power.runtime_error field is
814 * different from zero, the status may be changed either to RPM_ACTIVE, or to
815 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
816 * However, if the device has a parent and the parent is not active, and the
817 * parent's power.ignore_children flag is unset, the device's status cannot be
818 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
820 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
821 * and the device parent's counter of unsuspended children is modified to
822 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
823 * notification request for the parent is submitted.
825 int __pm_runtime_set_status(struct device *dev, unsigned int status)
827 struct device *parent = dev->parent;
829 bool notify_parent = false;
832 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
835 spin_lock_irqsave(&dev->power.lock, flags);
837 if (!dev->power.runtime_error && !dev->power.disable_depth) {
842 if (dev->power.runtime_status == status)
845 if (status == RPM_SUSPENDED) {
846 /* It always is possible to set the status to 'suspended'. */
848 atomic_add_unless(&parent->power.child_count, -1, 0);
849 notify_parent = !parent->power.ignore_children;
855 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
858 * It is invalid to put an active child under a parent that is
859 * not active, has run-time PM enabled and the
860 * 'power.ignore_children' flag unset.
862 if (!parent->power.disable_depth
863 && !parent->power.ignore_children
864 && parent->power.runtime_status != RPM_ACTIVE)
866 else if (dev->power.runtime_status == RPM_SUSPENDED)
867 atomic_inc(&parent->power.child_count);
869 spin_unlock(&parent->power.lock);
876 __update_runtime_status(dev, status);
877 dev->power.runtime_error = 0;
879 spin_unlock_irqrestore(&dev->power.lock, flags);
882 pm_request_idle(parent);
886 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
889 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
890 * @dev: Device to handle.
892 * Flush all pending requests for the device from pm_wq and wait for all
893 * run-time PM operations involving the device in progress to complete.
895 * Should be called under dev->power.lock with interrupts disabled.
897 static void __pm_runtime_barrier(struct device *dev)
899 pm_runtime_deactivate_timer(dev);
901 if (dev->power.request_pending) {
902 dev->power.request = RPM_REQ_NONE;
903 spin_unlock_irq(&dev->power.lock);
905 cancel_work_sync(&dev->power.work);
907 spin_lock_irq(&dev->power.lock);
908 dev->power.request_pending = false;
911 if (dev->power.runtime_status == RPM_SUSPENDING
912 || dev->power.runtime_status == RPM_RESUMING
913 || dev->power.idle_notification) {
916 /* Suspend, wake-up or idle notification in progress. */
918 prepare_to_wait(&dev->power.wait_queue, &wait,
919 TASK_UNINTERRUPTIBLE);
920 if (dev->power.runtime_status != RPM_SUSPENDING
921 && dev->power.runtime_status != RPM_RESUMING
922 && !dev->power.idle_notification)
924 spin_unlock_irq(&dev->power.lock);
928 spin_lock_irq(&dev->power.lock);
930 finish_wait(&dev->power.wait_queue, &wait);
935 * pm_runtime_barrier - Flush pending requests and wait for completions.
936 * @dev: Device to handle.
938 * Prevent the device from being suspended by incrementing its usage counter and
939 * if there's a pending resume request for the device, wake the device up.
940 * Next, make sure that all pending requests for the device have been flushed
941 * from pm_wq and wait for all run-time PM operations involving the device in
942 * progress to complete.
945 * 1, if there was a resume request pending and the device had to be woken up,
948 int pm_runtime_barrier(struct device *dev)
952 pm_runtime_get_noresume(dev);
953 spin_lock_irq(&dev->power.lock);
955 if (dev->power.request_pending
956 && dev->power.request == RPM_REQ_RESUME) {
961 __pm_runtime_barrier(dev);
963 spin_unlock_irq(&dev->power.lock);
964 pm_runtime_put_noidle(dev);
968 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
971 * __pm_runtime_disable - Disable run-time PM of a device.
972 * @dev: Device to handle.
973 * @check_resume: If set, check if there's a resume request for the device.
975 * Increment power.disable_depth for the device and if was zero previously,
976 * cancel all pending run-time PM requests for the device and wait for all
977 * operations in progress to complete. The device can be either active or
978 * suspended after its run-time PM has been disabled.
980 * If @check_resume is set and there's a resume request pending when
981 * __pm_runtime_disable() is called and power.disable_depth is zero, the
982 * function will wake up the device before disabling its run-time PM.
984 void __pm_runtime_disable(struct device *dev, bool check_resume)
986 spin_lock_irq(&dev->power.lock);
988 if (dev->power.disable_depth > 0) {
989 dev->power.disable_depth++;
994 * Wake up the device if there's a resume request pending, because that
995 * means there probably is some I/O to process and disabling run-time PM
996 * shouldn't prevent the device from processing the I/O.
998 if (check_resume && dev->power.request_pending
999 && dev->power.request == RPM_REQ_RESUME) {
1001 * Prevent suspends and idle notifications from being carried
1002 * out after we have woken up the device.
1004 pm_runtime_get_noresume(dev);
1008 pm_runtime_put_noidle(dev);
1011 if (!dev->power.disable_depth++)
1012 __pm_runtime_barrier(dev);
1015 spin_unlock_irq(&dev->power.lock);
1017 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1020 * pm_runtime_enable - Enable run-time PM of a device.
1021 * @dev: Device to handle.
1023 void pm_runtime_enable(struct device *dev)
1025 unsigned long flags;
1027 spin_lock_irqsave(&dev->power.lock, flags);
1029 if (dev->power.disable_depth > 0)
1030 dev->power.disable_depth--;
1032 dev_warn(dev, "Unbalanced %s!\n", __func__);
1034 spin_unlock_irqrestore(&dev->power.lock, flags);
1036 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1039 * pm_runtime_forbid - Block run-time PM of a device.
1040 * @dev: Device to handle.
1042 * Increase the device's usage count and clear its power.runtime_auto flag,
1043 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1046 void pm_runtime_forbid(struct device *dev)
1048 spin_lock_irq(&dev->power.lock);
1049 if (!dev->power.runtime_auto)
1052 dev->power.runtime_auto = false;
1053 atomic_inc(&dev->power.usage_count);
1057 spin_unlock_irq(&dev->power.lock);
1059 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1062 * pm_runtime_allow - Unblock run-time PM of a device.
1063 * @dev: Device to handle.
1065 * Decrease the device's usage count and set its power.runtime_auto flag.
1067 void pm_runtime_allow(struct device *dev)
1069 spin_lock_irq(&dev->power.lock);
1070 if (dev->power.runtime_auto)
1073 dev->power.runtime_auto = true;
1074 if (atomic_dec_and_test(&dev->power.usage_count))
1075 rpm_idle(dev, RPM_AUTO);
1078 spin_unlock_irq(&dev->power.lock);
1080 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1083 * pm_runtime_no_callbacks - Ignore run-time PM callbacks for a device.
1084 * @dev: Device to handle.
1086 * Set the power.no_callbacks flag, which tells the PM core that this
1087 * device is power-managed through its parent and has no run-time PM
1088 * callbacks of its own. The run-time sysfs attributes will be removed.
1091 void pm_runtime_no_callbacks(struct device *dev)
1093 spin_lock_irq(&dev->power.lock);
1094 dev->power.no_callbacks = 1;
1095 spin_unlock_irq(&dev->power.lock);
1096 if (device_is_registered(dev))
1097 rpm_sysfs_remove(dev);
1099 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1102 * update_autosuspend - Handle a change to a device's autosuspend settings.
1103 * @dev: Device to handle.
1104 * @old_delay: The former autosuspend_delay value.
1105 * @old_use: The former use_autosuspend value.
1107 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1108 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1110 * This function must be called under dev->power.lock with interrupts disabled.
1112 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1114 int delay = dev->power.autosuspend_delay;
1116 /* Should runtime suspend be prevented now? */
1117 if (dev->power.use_autosuspend && delay < 0) {
1119 /* If it used to be allowed then prevent it. */
1120 if (!old_use || old_delay >= 0) {
1121 atomic_inc(&dev->power.usage_count);
1126 /* Runtime suspend should be allowed now. */
1129 /* If it used to be prevented then allow it. */
1130 if (old_use && old_delay < 0)
1131 atomic_dec(&dev->power.usage_count);
1133 /* Maybe we can autosuspend now. */
1134 rpm_idle(dev, RPM_AUTO);
1139 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1140 * @dev: Device to handle.
1141 * @delay: Value of the new delay in milliseconds.
1143 * Set the device's power.autosuspend_delay value. If it changes to negative
1144 * and the power.use_autosuspend flag is set, prevent run-time suspends. If it
1145 * changes the other way, allow run-time suspends.
1147 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1149 int old_delay, old_use;
1151 spin_lock_irq(&dev->power.lock);
1152 old_delay = dev->power.autosuspend_delay;
1153 old_use = dev->power.use_autosuspend;
1154 dev->power.autosuspend_delay = delay;
1155 update_autosuspend(dev, old_delay, old_use);
1156 spin_unlock_irq(&dev->power.lock);
1158 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1161 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1162 * @dev: Device to handle.
1163 * @use: New value for use_autosuspend.
1165 * Set the device's power.use_autosuspend flag, and allow or prevent run-time
1166 * suspends as needed.
1168 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1170 int old_delay, old_use;
1172 spin_lock_irq(&dev->power.lock);
1173 old_delay = dev->power.autosuspend_delay;
1174 old_use = dev->power.use_autosuspend;
1175 dev->power.use_autosuspend = use;
1176 update_autosuspend(dev, old_delay, old_use);
1177 spin_unlock_irq(&dev->power.lock);
1179 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1182 * pm_runtime_init - Initialize run-time PM fields in given device object.
1183 * @dev: Device object to initialize.
1185 void pm_runtime_init(struct device *dev)
1187 dev->power.runtime_status = RPM_SUSPENDED;
1188 dev->power.idle_notification = false;
1190 dev->power.disable_depth = 1;
1191 atomic_set(&dev->power.usage_count, 0);
1193 dev->power.runtime_error = 0;
1195 atomic_set(&dev->power.child_count, 0);
1196 pm_suspend_ignore_children(dev, false);
1197 dev->power.runtime_auto = true;
1199 dev->power.request_pending = false;
1200 dev->power.request = RPM_REQ_NONE;
1201 dev->power.deferred_resume = false;
1202 dev->power.accounting_timestamp = jiffies;
1203 INIT_WORK(&dev->power.work, pm_runtime_work);
1205 dev->power.timer_expires = 0;
1206 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1207 (unsigned long)dev);
1209 init_waitqueue_head(&dev->power.wait_queue);
1213 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1214 * @dev: Device object being removed from device hierarchy.
1216 void pm_runtime_remove(struct device *dev)
1218 __pm_runtime_disable(dev, false);
1220 /* Change the status back to 'suspended' to match the initial status. */
1221 if (dev->power.runtime_status == RPM_ACTIVE)
1222 pm_runtime_set_suspended(dev);