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.runtime_status == RPM_SUSPENDING)
147 || (dev->power.request_pending
148 && dev->power.request == RPM_REQ_RESUME))
150 else if (dev->power.runtime_status == RPM_SUSPENDED)
157 * rpm_idle - Notify device bus type if the device can be suspended.
158 * @dev: Device to notify the bus type about.
159 * @rpmflags: Flag bits.
161 * Check if the device's run-time PM status allows it to be suspended. If
162 * another idle notification has been started earlier, return immediately. If
163 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
164 * run the ->runtime_idle() callback directly.
166 * This function must be called under dev->power.lock with interrupts disabled.
168 static int rpm_idle(struct device *dev, int rpmflags)
170 int (*callback)(struct device *);
173 retval = rpm_check_suspend_allowed(dev);
175 ; /* Conditions are wrong. */
177 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
178 else if (dev->power.runtime_status != RPM_ACTIVE)
182 * Any pending request other than an idle notification takes
183 * precedence over us, except that the timer may be running.
185 else if (dev->power.request_pending &&
186 dev->power.request > RPM_REQ_IDLE)
189 /* Act as though RPM_NOWAIT is always set. */
190 else if (dev->power.idle_notification)
191 retval = -EINPROGRESS;
195 /* Pending requests need to be canceled. */
196 dev->power.request = RPM_REQ_NONE;
198 if (dev->power.no_callbacks) {
199 /* Assume ->runtime_idle() callback would have suspended. */
200 retval = rpm_suspend(dev, rpmflags);
204 /* Carry out an asynchronous or a synchronous idle notification. */
205 if (rpmflags & RPM_ASYNC) {
206 dev->power.request = RPM_REQ_IDLE;
207 if (!dev->power.request_pending) {
208 dev->power.request_pending = true;
209 queue_work(pm_wq, &dev->power.work);
214 dev->power.idle_notification = true;
217 callback = dev->pwr_domain->ops.runtime_idle;
218 else if (dev->type && dev->type->pm)
219 callback = dev->type->pm->runtime_idle;
220 else if (dev->class && dev->class->pm)
221 callback = dev->class->pm->runtime_idle;
222 else if (dev->bus && dev->bus->pm)
223 callback = dev->bus->pm->runtime_idle;
228 spin_unlock_irq(&dev->power.lock);
232 spin_lock_irq(&dev->power.lock);
235 dev->power.idle_notification = false;
236 wake_up_all(&dev->power.wait_queue);
243 * rpm_callback - Run a given runtime PM callback for a given device.
244 * @cb: Runtime PM callback to run.
245 * @dev: Device to run the callback for.
247 static int rpm_callback(int (*cb)(struct device *), struct device *dev)
248 __releases(&dev->power.lock) __acquires(&dev->power.lock)
255 if (dev->power.irq_safe) {
258 spin_unlock_irq(&dev->power.lock);
262 spin_lock_irq(&dev->power.lock);
264 dev->power.runtime_error = retval;
269 * rpm_suspend - Carry out run-time suspend of given device.
270 * @dev: Device to suspend.
271 * @rpmflags: Flag bits.
273 * Check if the device's run-time PM status allows it to be suspended. If
274 * another suspend has been started earlier, either return immediately or wait
275 * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a
276 * pending idle notification. If the RPM_ASYNC flag is set then queue a
277 * suspend request; otherwise run the ->runtime_suspend() callback directly.
278 * If a deferred resume was requested while the callback was running then carry
279 * it out; otherwise send an idle notification for the device (if the suspend
280 * failed) or for its parent (if the suspend succeeded).
281 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
282 * flag is set and the next autosuspend-delay expiration time is in the
283 * future, schedule another autosuspend attempt.
285 * This function must be called under dev->power.lock with interrupts disabled.
287 static int rpm_suspend(struct device *dev, int rpmflags)
288 __releases(&dev->power.lock) __acquires(&dev->power.lock)
290 int (*callback)(struct device *);
291 struct device *parent = NULL;
294 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
297 retval = rpm_check_suspend_allowed(dev);
300 ; /* Conditions are wrong. */
302 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
303 else if (dev->power.runtime_status == RPM_RESUMING &&
304 !(rpmflags & RPM_ASYNC))
309 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
310 if ((rpmflags & RPM_AUTO)
311 && dev->power.runtime_status != RPM_SUSPENDING) {
312 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
315 /* Pending requests need to be canceled. */
316 dev->power.request = RPM_REQ_NONE;
319 * Optimization: If the timer is already running and is
320 * set to expire at or before the autosuspend delay,
321 * avoid the overhead of resetting it. Just let it
322 * expire; pm_suspend_timer_fn() will take care of the
325 if (!(dev->power.timer_expires && time_before_eq(
326 dev->power.timer_expires, expires))) {
327 dev->power.timer_expires = expires;
328 mod_timer(&dev->power.suspend_timer, expires);
330 dev->power.timer_autosuspends = 1;
335 /* Other scheduled or pending requests need to be canceled. */
336 pm_runtime_cancel_pending(dev);
338 if (dev->power.runtime_status == RPM_SUSPENDING) {
341 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
342 retval = -EINPROGRESS;
346 /* Wait for the other suspend running in parallel with us. */
348 prepare_to_wait(&dev->power.wait_queue, &wait,
349 TASK_UNINTERRUPTIBLE);
350 if (dev->power.runtime_status != RPM_SUSPENDING)
353 spin_unlock_irq(&dev->power.lock);
357 spin_lock_irq(&dev->power.lock);
359 finish_wait(&dev->power.wait_queue, &wait);
363 dev->power.deferred_resume = false;
364 if (dev->power.no_callbacks)
365 goto no_callback; /* Assume success. */
367 /* Carry out an asynchronous or a synchronous suspend. */
368 if (rpmflags & RPM_ASYNC) {
369 dev->power.request = (rpmflags & RPM_AUTO) ?
370 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
371 if (!dev->power.request_pending) {
372 dev->power.request_pending = true;
373 queue_work(pm_wq, &dev->power.work);
378 __update_runtime_status(dev, RPM_SUSPENDING);
381 callback = dev->pwr_domain->ops.runtime_suspend;
382 else if (dev->type && dev->type->pm)
383 callback = dev->type->pm->runtime_suspend;
384 else if (dev->class && dev->class->pm)
385 callback = dev->class->pm->runtime_suspend;
386 else if (dev->bus && dev->bus->pm)
387 callback = dev->bus->pm->runtime_suspend;
391 retval = rpm_callback(callback, dev);
393 __update_runtime_status(dev, RPM_ACTIVE);
394 dev->power.deferred_resume = 0;
395 if (retval == -EAGAIN || retval == -EBUSY) {
396 dev->power.runtime_error = 0;
399 * If the callback routine failed an autosuspend, and
400 * if the last_busy time has been updated so that there
401 * is a new autosuspend expiration time, automatically
402 * reschedule another autosuspend.
404 if ((rpmflags & RPM_AUTO) &&
405 pm_runtime_autosuspend_expiration(dev) != 0)
408 pm_runtime_cancel_pending(dev);
412 __update_runtime_status(dev, RPM_SUSPENDED);
413 pm_runtime_deactivate_timer(dev);
416 parent = dev->parent;
417 atomic_add_unless(&parent->power.child_count, -1, 0);
420 wake_up_all(&dev->power.wait_queue);
422 if (dev->power.deferred_resume) {
428 /* Maybe the parent is now able to suspend. */
429 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
430 spin_unlock(&dev->power.lock);
432 spin_lock(&parent->power.lock);
433 rpm_idle(parent, RPM_ASYNC);
434 spin_unlock(&parent->power.lock);
436 spin_lock(&dev->power.lock);
440 dev_dbg(dev, "%s returns %d\n", __func__, retval);
446 * rpm_resume - Carry out run-time resume of given device.
447 * @dev: Device to resume.
448 * @rpmflags: Flag bits.
450 * Check if the device's run-time PM status allows it to be resumed. Cancel
451 * any scheduled or pending requests. If another resume has been started
452 * earlier, either return immediately or wait for it to finish, depending on the
453 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
454 * parallel with this function, either tell the other process to resume after
455 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
456 * flag is set then queue a resume request; otherwise run the
457 * ->runtime_resume() callback directly. Queue an idle notification for the
458 * device if the resume succeeded.
460 * This function must be called under dev->power.lock with interrupts disabled.
462 static int rpm_resume(struct device *dev, int rpmflags)
463 __releases(&dev->power.lock) __acquires(&dev->power.lock)
465 int (*callback)(struct device *);
466 struct device *parent = NULL;
469 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
472 if (dev->power.runtime_error)
474 else if (dev->power.disable_depth > 0)
480 * Other scheduled or pending requests need to be canceled. Small
481 * optimization: If an autosuspend timer is running, leave it running
482 * rather than cancelling it now only to restart it again in the near
485 dev->power.request = RPM_REQ_NONE;
486 if (!dev->power.timer_autosuspends)
487 pm_runtime_deactivate_timer(dev);
489 if (dev->power.runtime_status == RPM_ACTIVE) {
494 if (dev->power.runtime_status == RPM_RESUMING
495 || dev->power.runtime_status == RPM_SUSPENDING) {
498 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
499 if (dev->power.runtime_status == RPM_SUSPENDING)
500 dev->power.deferred_resume = true;
502 retval = -EINPROGRESS;
506 /* Wait for the operation carried out in parallel with us. */
508 prepare_to_wait(&dev->power.wait_queue, &wait,
509 TASK_UNINTERRUPTIBLE);
510 if (dev->power.runtime_status != RPM_RESUMING
511 && dev->power.runtime_status != RPM_SUSPENDING)
514 spin_unlock_irq(&dev->power.lock);
518 spin_lock_irq(&dev->power.lock);
520 finish_wait(&dev->power.wait_queue, &wait);
525 * See if we can skip waking up the parent. This is safe only if
526 * power.no_callbacks is set, because otherwise we don't know whether
527 * the resume will actually succeed.
529 if (dev->power.no_callbacks && !parent && dev->parent) {
530 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
531 if (dev->parent->power.disable_depth > 0
532 || dev->parent->power.ignore_children
533 || dev->parent->power.runtime_status == RPM_ACTIVE) {
534 atomic_inc(&dev->parent->power.child_count);
535 spin_unlock(&dev->parent->power.lock);
536 goto no_callback; /* Assume success. */
538 spin_unlock(&dev->parent->power.lock);
541 /* Carry out an asynchronous or a synchronous resume. */
542 if (rpmflags & RPM_ASYNC) {
543 dev->power.request = RPM_REQ_RESUME;
544 if (!dev->power.request_pending) {
545 dev->power.request_pending = true;
546 queue_work(pm_wq, &dev->power.work);
552 if (!parent && dev->parent) {
554 * Increment the parent's usage counter and resume it if
555 * necessary. Not needed if dev is irq-safe; then the
556 * parent is permanently resumed.
558 parent = dev->parent;
559 if (dev->power.irq_safe)
561 spin_unlock(&dev->power.lock);
563 pm_runtime_get_noresume(parent);
565 spin_lock(&parent->power.lock);
567 * We can resume if the parent's run-time PM is disabled or it
568 * is set to ignore children.
570 if (!parent->power.disable_depth
571 && !parent->power.ignore_children) {
572 rpm_resume(parent, 0);
573 if (parent->power.runtime_status != RPM_ACTIVE)
576 spin_unlock(&parent->power.lock);
578 spin_lock(&dev->power.lock);
585 if (dev->power.no_callbacks)
586 goto no_callback; /* Assume success. */
588 __update_runtime_status(dev, RPM_RESUMING);
591 callback = dev->pwr_domain->ops.runtime_resume;
592 else if (dev->type && dev->type->pm)
593 callback = dev->type->pm->runtime_resume;
594 else if (dev->class && dev->class->pm)
595 callback = dev->class->pm->runtime_resume;
596 else if (dev->bus && dev->bus->pm)
597 callback = dev->bus->pm->runtime_resume;
601 retval = rpm_callback(callback, dev);
603 __update_runtime_status(dev, RPM_SUSPENDED);
604 pm_runtime_cancel_pending(dev);
607 __update_runtime_status(dev, RPM_ACTIVE);
609 atomic_inc(&parent->power.child_count);
611 wake_up_all(&dev->power.wait_queue);
614 rpm_idle(dev, RPM_ASYNC);
617 if (parent && !dev->power.irq_safe) {
618 spin_unlock_irq(&dev->power.lock);
620 pm_runtime_put(parent);
622 spin_lock_irq(&dev->power.lock);
625 dev_dbg(dev, "%s returns %d\n", __func__, retval);
631 * pm_runtime_work - Universal run-time PM work function.
632 * @work: Work structure used for scheduling the execution of this function.
634 * Use @work to get the device object the work is to be done for, determine what
635 * is to be done and execute the appropriate run-time PM function.
637 static void pm_runtime_work(struct work_struct *work)
639 struct device *dev = container_of(work, struct device, power.work);
640 enum rpm_request req;
642 spin_lock_irq(&dev->power.lock);
644 if (!dev->power.request_pending)
647 req = dev->power.request;
648 dev->power.request = RPM_REQ_NONE;
649 dev->power.request_pending = false;
655 rpm_idle(dev, RPM_NOWAIT);
657 case RPM_REQ_SUSPEND:
658 rpm_suspend(dev, RPM_NOWAIT);
660 case RPM_REQ_AUTOSUSPEND:
661 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
664 rpm_resume(dev, RPM_NOWAIT);
669 spin_unlock_irq(&dev->power.lock);
673 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
674 * @data: Device pointer passed by pm_schedule_suspend().
676 * Check if the time is right and queue a suspend request.
678 static void pm_suspend_timer_fn(unsigned long data)
680 struct device *dev = (struct device *)data;
682 unsigned long expires;
684 spin_lock_irqsave(&dev->power.lock, flags);
686 expires = dev->power.timer_expires;
687 /* If 'expire' is after 'jiffies' we've been called too early. */
688 if (expires > 0 && !time_after(expires, jiffies)) {
689 dev->power.timer_expires = 0;
690 rpm_suspend(dev, dev->power.timer_autosuspends ?
691 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
694 spin_unlock_irqrestore(&dev->power.lock, flags);
698 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
699 * @dev: Device to suspend.
700 * @delay: Time to wait before submitting a suspend request, in milliseconds.
702 int pm_schedule_suspend(struct device *dev, unsigned int delay)
707 spin_lock_irqsave(&dev->power.lock, flags);
710 retval = rpm_suspend(dev, RPM_ASYNC);
714 retval = rpm_check_suspend_allowed(dev);
718 /* Other scheduled or pending requests need to be canceled. */
719 pm_runtime_cancel_pending(dev);
721 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
722 dev->power.timer_expires += !dev->power.timer_expires;
723 dev->power.timer_autosuspends = 0;
724 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
727 spin_unlock_irqrestore(&dev->power.lock, flags);
731 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
734 * __pm_runtime_idle - Entry point for run-time idle operations.
735 * @dev: Device to send idle notification for.
736 * @rpmflags: Flag bits.
738 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
739 * return immediately if it is larger than zero. Then carry out an idle
740 * notification, either synchronous or asynchronous.
742 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
744 int __pm_runtime_idle(struct device *dev, int rpmflags)
749 if (rpmflags & RPM_GET_PUT) {
750 if (!atomic_dec_and_test(&dev->power.usage_count))
754 spin_lock_irqsave(&dev->power.lock, flags);
755 retval = rpm_idle(dev, rpmflags);
756 spin_unlock_irqrestore(&dev->power.lock, flags);
760 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
763 * __pm_runtime_suspend - Entry point for run-time put/suspend operations.
764 * @dev: Device to suspend.
765 * @rpmflags: Flag bits.
767 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
768 * return immediately if it is larger than zero. Then carry out a suspend,
769 * either synchronous or asynchronous.
771 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
773 int __pm_runtime_suspend(struct device *dev, int rpmflags)
778 if (rpmflags & RPM_GET_PUT) {
779 if (!atomic_dec_and_test(&dev->power.usage_count))
783 spin_lock_irqsave(&dev->power.lock, flags);
784 retval = rpm_suspend(dev, rpmflags);
785 spin_unlock_irqrestore(&dev->power.lock, flags);
789 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
792 * __pm_runtime_resume - Entry point for run-time resume operations.
793 * @dev: Device to resume.
794 * @rpmflags: Flag bits.
796 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
797 * carry out a resume, either synchronous or asynchronous.
799 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
801 int __pm_runtime_resume(struct device *dev, int rpmflags)
806 if (rpmflags & RPM_GET_PUT)
807 atomic_inc(&dev->power.usage_count);
809 spin_lock_irqsave(&dev->power.lock, flags);
810 retval = rpm_resume(dev, rpmflags);
811 spin_unlock_irqrestore(&dev->power.lock, flags);
815 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
818 * __pm_runtime_set_status - Set run-time PM status of a device.
819 * @dev: Device to handle.
820 * @status: New run-time PM status of the device.
822 * If run-time PM of the device is disabled or its power.runtime_error field is
823 * different from zero, the status may be changed either to RPM_ACTIVE, or to
824 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
825 * However, if the device has a parent and the parent is not active, and the
826 * parent's power.ignore_children flag is unset, the device's status cannot be
827 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
829 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
830 * and the device parent's counter of unsuspended children is modified to
831 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
832 * notification request for the parent is submitted.
834 int __pm_runtime_set_status(struct device *dev, unsigned int status)
836 struct device *parent = dev->parent;
838 bool notify_parent = false;
841 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
844 spin_lock_irqsave(&dev->power.lock, flags);
846 if (!dev->power.runtime_error && !dev->power.disable_depth) {
851 if (dev->power.runtime_status == status)
854 if (status == RPM_SUSPENDED) {
855 /* It always is possible to set the status to 'suspended'. */
857 atomic_add_unless(&parent->power.child_count, -1, 0);
858 notify_parent = !parent->power.ignore_children;
864 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
867 * It is invalid to put an active child under a parent that is
868 * not active, has run-time PM enabled and the
869 * 'power.ignore_children' flag unset.
871 if (!parent->power.disable_depth
872 && !parent->power.ignore_children
873 && parent->power.runtime_status != RPM_ACTIVE)
875 else if (dev->power.runtime_status == RPM_SUSPENDED)
876 atomic_inc(&parent->power.child_count);
878 spin_unlock(&parent->power.lock);
885 __update_runtime_status(dev, status);
886 dev->power.runtime_error = 0;
888 spin_unlock_irqrestore(&dev->power.lock, flags);
891 pm_request_idle(parent);
895 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
898 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
899 * @dev: Device to handle.
901 * Flush all pending requests for the device from pm_wq and wait for all
902 * run-time PM operations involving the device in progress to complete.
904 * Should be called under dev->power.lock with interrupts disabled.
906 static void __pm_runtime_barrier(struct device *dev)
908 pm_runtime_deactivate_timer(dev);
910 if (dev->power.request_pending) {
911 dev->power.request = RPM_REQ_NONE;
912 spin_unlock_irq(&dev->power.lock);
914 cancel_work_sync(&dev->power.work);
916 spin_lock_irq(&dev->power.lock);
917 dev->power.request_pending = false;
920 if (dev->power.runtime_status == RPM_SUSPENDING
921 || dev->power.runtime_status == RPM_RESUMING
922 || dev->power.idle_notification) {
925 /* Suspend, wake-up or idle notification in progress. */
927 prepare_to_wait(&dev->power.wait_queue, &wait,
928 TASK_UNINTERRUPTIBLE);
929 if (dev->power.runtime_status != RPM_SUSPENDING
930 && dev->power.runtime_status != RPM_RESUMING
931 && !dev->power.idle_notification)
933 spin_unlock_irq(&dev->power.lock);
937 spin_lock_irq(&dev->power.lock);
939 finish_wait(&dev->power.wait_queue, &wait);
944 * pm_runtime_barrier - Flush pending requests and wait for completions.
945 * @dev: Device to handle.
947 * Prevent the device from being suspended by incrementing its usage counter and
948 * if there's a pending resume request for the device, wake the device up.
949 * Next, make sure that all pending requests for the device have been flushed
950 * from pm_wq and wait for all run-time PM operations involving the device in
951 * progress to complete.
954 * 1, if there was a resume request pending and the device had to be woken up,
957 int pm_runtime_barrier(struct device *dev)
961 pm_runtime_get_noresume(dev);
962 spin_lock_irq(&dev->power.lock);
964 if (dev->power.request_pending
965 && dev->power.request == RPM_REQ_RESUME) {
970 __pm_runtime_barrier(dev);
972 spin_unlock_irq(&dev->power.lock);
973 pm_runtime_put_noidle(dev);
977 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
980 * __pm_runtime_disable - Disable run-time PM of a device.
981 * @dev: Device to handle.
982 * @check_resume: If set, check if there's a resume request for the device.
984 * Increment power.disable_depth for the device and if was zero previously,
985 * cancel all pending run-time PM requests for the device and wait for all
986 * operations in progress to complete. The device can be either active or
987 * suspended after its run-time PM has been disabled.
989 * If @check_resume is set and there's a resume request pending when
990 * __pm_runtime_disable() is called and power.disable_depth is zero, the
991 * function will wake up the device before disabling its run-time PM.
993 void __pm_runtime_disable(struct device *dev, bool check_resume)
995 spin_lock_irq(&dev->power.lock);
997 if (dev->power.disable_depth > 0) {
998 dev->power.disable_depth++;
1003 * Wake up the device if there's a resume request pending, because that
1004 * means there probably is some I/O to process and disabling run-time PM
1005 * shouldn't prevent the device from processing the I/O.
1007 if (check_resume && dev->power.request_pending
1008 && dev->power.request == RPM_REQ_RESUME) {
1010 * Prevent suspends and idle notifications from being carried
1011 * out after we have woken up the device.
1013 pm_runtime_get_noresume(dev);
1017 pm_runtime_put_noidle(dev);
1020 if (!dev->power.disable_depth++)
1021 __pm_runtime_barrier(dev);
1024 spin_unlock_irq(&dev->power.lock);
1026 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1029 * pm_runtime_enable - Enable run-time PM of a device.
1030 * @dev: Device to handle.
1032 void pm_runtime_enable(struct device *dev)
1034 unsigned long flags;
1036 spin_lock_irqsave(&dev->power.lock, flags);
1038 if (dev->power.disable_depth > 0)
1039 dev->power.disable_depth--;
1041 dev_warn(dev, "Unbalanced %s!\n", __func__);
1043 spin_unlock_irqrestore(&dev->power.lock, flags);
1045 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1048 * pm_runtime_forbid - Block run-time PM of a device.
1049 * @dev: Device to handle.
1051 * Increase the device's usage count and clear its power.runtime_auto flag,
1052 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1055 void pm_runtime_forbid(struct device *dev)
1057 spin_lock_irq(&dev->power.lock);
1058 if (!dev->power.runtime_auto)
1061 dev->power.runtime_auto = false;
1062 atomic_inc(&dev->power.usage_count);
1066 spin_unlock_irq(&dev->power.lock);
1068 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1071 * pm_runtime_allow - Unblock run-time PM of a device.
1072 * @dev: Device to handle.
1074 * Decrease the device's usage count and set its power.runtime_auto flag.
1076 void pm_runtime_allow(struct device *dev)
1078 spin_lock_irq(&dev->power.lock);
1079 if (dev->power.runtime_auto)
1082 dev->power.runtime_auto = true;
1083 if (atomic_dec_and_test(&dev->power.usage_count))
1084 rpm_idle(dev, RPM_AUTO);
1087 spin_unlock_irq(&dev->power.lock);
1089 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1092 * pm_runtime_no_callbacks - Ignore run-time PM callbacks for a device.
1093 * @dev: Device to handle.
1095 * Set the power.no_callbacks flag, which tells the PM core that this
1096 * device is power-managed through its parent and has no run-time PM
1097 * callbacks of its own. The run-time sysfs attributes will be removed.
1099 void pm_runtime_no_callbacks(struct device *dev)
1101 spin_lock_irq(&dev->power.lock);
1102 dev->power.no_callbacks = 1;
1103 spin_unlock_irq(&dev->power.lock);
1104 if (device_is_registered(dev))
1105 rpm_sysfs_remove(dev);
1107 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1110 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1111 * @dev: Device to handle
1113 * Set the power.irq_safe flag, which tells the PM core that the
1114 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1115 * always be invoked with the spinlock held and interrupts disabled. It also
1116 * causes the parent's usage counter to be permanently incremented, preventing
1117 * the parent from runtime suspending -- otherwise an irq-safe child might have
1118 * to wait for a non-irq-safe parent.
1120 void pm_runtime_irq_safe(struct device *dev)
1123 pm_runtime_get_sync(dev->parent);
1124 spin_lock_irq(&dev->power.lock);
1125 dev->power.irq_safe = 1;
1126 spin_unlock_irq(&dev->power.lock);
1128 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1131 * update_autosuspend - Handle a change to a device's autosuspend settings.
1132 * @dev: Device to handle.
1133 * @old_delay: The former autosuspend_delay value.
1134 * @old_use: The former use_autosuspend value.
1136 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1137 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1139 * This function must be called under dev->power.lock with interrupts disabled.
1141 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1143 int delay = dev->power.autosuspend_delay;
1145 /* Should runtime suspend be prevented now? */
1146 if (dev->power.use_autosuspend && delay < 0) {
1148 /* If it used to be allowed then prevent it. */
1149 if (!old_use || old_delay >= 0) {
1150 atomic_inc(&dev->power.usage_count);
1155 /* Runtime suspend should be allowed now. */
1158 /* If it used to be prevented then allow it. */
1159 if (old_use && old_delay < 0)
1160 atomic_dec(&dev->power.usage_count);
1162 /* Maybe we can autosuspend now. */
1163 rpm_idle(dev, RPM_AUTO);
1168 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1169 * @dev: Device to handle.
1170 * @delay: Value of the new delay in milliseconds.
1172 * Set the device's power.autosuspend_delay value. If it changes to negative
1173 * and the power.use_autosuspend flag is set, prevent run-time suspends. If it
1174 * changes the other way, allow run-time suspends.
1176 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1178 int old_delay, old_use;
1180 spin_lock_irq(&dev->power.lock);
1181 old_delay = dev->power.autosuspend_delay;
1182 old_use = dev->power.use_autosuspend;
1183 dev->power.autosuspend_delay = delay;
1184 update_autosuspend(dev, old_delay, old_use);
1185 spin_unlock_irq(&dev->power.lock);
1187 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1190 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1191 * @dev: Device to handle.
1192 * @use: New value for use_autosuspend.
1194 * Set the device's power.use_autosuspend flag, and allow or prevent run-time
1195 * suspends as needed.
1197 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1199 int old_delay, old_use;
1201 spin_lock_irq(&dev->power.lock);
1202 old_delay = dev->power.autosuspend_delay;
1203 old_use = dev->power.use_autosuspend;
1204 dev->power.use_autosuspend = use;
1205 update_autosuspend(dev, old_delay, old_use);
1206 spin_unlock_irq(&dev->power.lock);
1208 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1211 * pm_runtime_init - Initialize run-time PM fields in given device object.
1212 * @dev: Device object to initialize.
1214 void pm_runtime_init(struct device *dev)
1216 dev->power.runtime_status = RPM_SUSPENDED;
1217 dev->power.idle_notification = false;
1219 dev->power.disable_depth = 1;
1220 atomic_set(&dev->power.usage_count, 0);
1222 dev->power.runtime_error = 0;
1224 atomic_set(&dev->power.child_count, 0);
1225 pm_suspend_ignore_children(dev, false);
1226 dev->power.runtime_auto = true;
1228 dev->power.request_pending = false;
1229 dev->power.request = RPM_REQ_NONE;
1230 dev->power.deferred_resume = false;
1231 dev->power.accounting_timestamp = jiffies;
1232 INIT_WORK(&dev->power.work, pm_runtime_work);
1234 dev->power.timer_expires = 0;
1235 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1236 (unsigned long)dev);
1238 init_waitqueue_head(&dev->power.wait_queue);
1242 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1243 * @dev: Device object being removed from device hierarchy.
1245 void pm_runtime_remove(struct device *dev)
1247 __pm_runtime_disable(dev, false);
1249 /* Change the status back to 'suspended' to match the initial status. */
1250 if (dev->power.runtime_status == RPM_ACTIVE)
1251 pm_runtime_set_suspended(dev);
1252 if (dev->power.irq_safe && dev->parent)
1253 pm_runtime_put_sync(dev->parent);