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.
6 * This file is released under the GPLv2.
9 #include <linux/sched.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/jiffies.h>
13 static int __pm_runtime_resume(struct device *dev, int rpmflags);
14 static int __pm_request_idle(struct device *dev);
15 static int __pm_request_resume(struct device *dev);
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_idle - Notify device bus type if the device can be suspended.
83 * @dev: Device to notify the bus type about.
85 * This function must be called under dev->power.lock with interrupts disabled.
87 static int __pm_runtime_idle(struct device *dev)
88 __releases(&dev->power.lock) __acquires(&dev->power.lock)
92 if (dev->power.runtime_error)
94 else if (dev->power.idle_notification)
95 retval = -EINPROGRESS;
96 else if (atomic_read(&dev->power.usage_count) > 0
97 || dev->power.disable_depth > 0
98 || dev->power.runtime_status != RPM_ACTIVE)
100 else if (!pm_children_suspended(dev))
105 if (dev->power.request_pending) {
107 * If an idle notification request is pending, cancel it. Any
108 * other pending request takes precedence over us.
110 if (dev->power.request == RPM_REQ_IDLE) {
111 dev->power.request = RPM_REQ_NONE;
112 } else if (dev->power.request != RPM_REQ_NONE) {
118 dev->power.idle_notification = true;
120 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_idle) {
121 spin_unlock_irq(&dev->power.lock);
123 dev->bus->pm->runtime_idle(dev);
125 spin_lock_irq(&dev->power.lock);
126 } else if (dev->type && dev->type->pm && dev->type->pm->runtime_idle) {
127 spin_unlock_irq(&dev->power.lock);
129 dev->type->pm->runtime_idle(dev);
131 spin_lock_irq(&dev->power.lock);
132 } else if (dev->class && dev->class->pm
133 && dev->class->pm->runtime_idle) {
134 spin_unlock_irq(&dev->power.lock);
136 dev->class->pm->runtime_idle(dev);
138 spin_lock_irq(&dev->power.lock);
141 dev->power.idle_notification = false;
142 wake_up_all(&dev->power.wait_queue);
149 * pm_runtime_idle - Notify device bus type if the device can be suspended.
150 * @dev: Device to notify the bus type about.
152 int pm_runtime_idle(struct device *dev)
156 spin_lock_irq(&dev->power.lock);
157 retval = __pm_runtime_idle(dev);
158 spin_unlock_irq(&dev->power.lock);
162 EXPORT_SYMBOL_GPL(pm_runtime_idle);
165 * __pm_runtime_suspend - Carry out run-time suspend of given device.
166 * @dev: Device to suspend.
167 * @rpmflags: Flag bits.
169 * Check if the device can be suspended and run the ->runtime_suspend() callback
170 * provided by its bus type. If another suspend has been started earlier,
171 * either return immediately or wait for it to finish, depending on the
172 * RPM_NOWAIT flag. If an idle notification or suspend request is pending or
173 * scheduled, cancel it.
175 * This function must be called under dev->power.lock with interrupts disabled.
177 static int __pm_runtime_suspend(struct device *dev, int rpmflags)
178 __releases(&dev->power.lock) __acquires(&dev->power.lock)
180 struct device *parent = NULL;
184 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
187 if (dev->power.runtime_error) {
192 /* Pending resume requests take precedence over us. */
193 if (dev->power.request_pending
194 && dev->power.request == RPM_REQ_RESUME) {
199 /* Other scheduled or pending requests need to be canceled. */
200 pm_runtime_cancel_pending(dev);
202 if (dev->power.runtime_status == RPM_SUSPENDED)
204 else if (dev->power.runtime_status == RPM_RESUMING
205 || dev->power.disable_depth > 0
206 || atomic_read(&dev->power.usage_count) > 0)
208 else if (!pm_children_suspended(dev))
213 if (dev->power.runtime_status == RPM_SUSPENDING) {
216 if (rpmflags & RPM_NOWAIT) {
217 retval = -EINPROGRESS;
221 /* Wait for the other suspend running in parallel with us. */
223 prepare_to_wait(&dev->power.wait_queue, &wait,
224 TASK_UNINTERRUPTIBLE);
225 if (dev->power.runtime_status != RPM_SUSPENDING)
228 spin_unlock_irq(&dev->power.lock);
232 spin_lock_irq(&dev->power.lock);
234 finish_wait(&dev->power.wait_queue, &wait);
238 __update_runtime_status(dev, RPM_SUSPENDING);
239 dev->power.deferred_resume = false;
241 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_suspend) {
242 spin_unlock_irq(&dev->power.lock);
244 retval = dev->bus->pm->runtime_suspend(dev);
246 spin_lock_irq(&dev->power.lock);
247 dev->power.runtime_error = retval;
248 } else if (dev->type && dev->type->pm
249 && dev->type->pm->runtime_suspend) {
250 spin_unlock_irq(&dev->power.lock);
252 retval = dev->type->pm->runtime_suspend(dev);
254 spin_lock_irq(&dev->power.lock);
255 dev->power.runtime_error = retval;
256 } else if (dev->class && dev->class->pm
257 && dev->class->pm->runtime_suspend) {
258 spin_unlock_irq(&dev->power.lock);
260 retval = dev->class->pm->runtime_suspend(dev);
262 spin_lock_irq(&dev->power.lock);
263 dev->power.runtime_error = retval;
269 __update_runtime_status(dev, RPM_ACTIVE);
270 if (retval == -EAGAIN || retval == -EBUSY) {
271 if (dev->power.timer_expires == 0)
273 dev->power.runtime_error = 0;
275 pm_runtime_cancel_pending(dev);
278 __update_runtime_status(dev, RPM_SUSPENDED);
279 pm_runtime_deactivate_timer(dev);
282 parent = dev->parent;
283 atomic_add_unless(&parent->power.child_count, -1, 0);
286 wake_up_all(&dev->power.wait_queue);
288 if (dev->power.deferred_resume) {
289 __pm_runtime_resume(dev, 0);
295 __pm_runtime_idle(dev);
297 if (parent && !parent->power.ignore_children) {
298 spin_unlock_irq(&dev->power.lock);
300 pm_request_idle(parent);
302 spin_lock_irq(&dev->power.lock);
306 dev_dbg(dev, "%s returns %d\n", __func__, retval);
312 * pm_runtime_suspend - Carry out run-time suspend of given device.
313 * @dev: Device to suspend.
315 int pm_runtime_suspend(struct device *dev)
319 spin_lock_irq(&dev->power.lock);
320 retval = __pm_runtime_suspend(dev, 0);
321 spin_unlock_irq(&dev->power.lock);
325 EXPORT_SYMBOL_GPL(pm_runtime_suspend);
328 * __pm_runtime_resume - Carry out run-time resume of given device.
329 * @dev: Device to resume.
330 * @rpmflags: Flag bits.
332 * Check if the device can be woken up and run the ->runtime_resume() callback
333 * provided by its bus type. If another resume has been started earlier,
334 * either return imediately or wait for it to finish, depending on the
335 * RPM_NOWAIT flag. If there's a suspend running in parallel with this
336 * function, either tell the other process to resume after suspending
337 * (deferred_resume) or wait for it to finish, depending on the RPM_NOWAIT
338 * flag. Cancel any scheduled or pending requests.
340 * This function must be called under dev->power.lock with interrupts disabled.
342 static int __pm_runtime_resume(struct device *dev, int rpmflags)
343 __releases(&dev->power.lock) __acquires(&dev->power.lock)
345 struct device *parent = NULL;
348 dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
351 if (dev->power.runtime_error) {
356 pm_runtime_cancel_pending(dev);
358 if (dev->power.runtime_status == RPM_ACTIVE)
360 else if (dev->power.disable_depth > 0)
365 if (dev->power.runtime_status == RPM_RESUMING
366 || dev->power.runtime_status == RPM_SUSPENDING) {
369 if (rpmflags & RPM_NOWAIT) {
370 if (dev->power.runtime_status == RPM_SUSPENDING)
371 dev->power.deferred_resume = true;
372 retval = -EINPROGRESS;
376 /* Wait for the operation carried out in parallel with us. */
378 prepare_to_wait(&dev->power.wait_queue, &wait,
379 TASK_UNINTERRUPTIBLE);
380 if (dev->power.runtime_status != RPM_RESUMING
381 && dev->power.runtime_status != RPM_SUSPENDING)
384 spin_unlock_irq(&dev->power.lock);
388 spin_lock_irq(&dev->power.lock);
390 finish_wait(&dev->power.wait_queue, &wait);
394 if (!parent && dev->parent) {
396 * Increment the parent's resume counter and resume it if
399 parent = dev->parent;
400 spin_unlock(&dev->power.lock);
402 pm_runtime_get_noresume(parent);
404 spin_lock(&parent->power.lock);
406 * We can resume if the parent's run-time PM is disabled or it
407 * is set to ignore children.
409 if (!parent->power.disable_depth
410 && !parent->power.ignore_children) {
411 __pm_runtime_resume(parent, 0);
412 if (parent->power.runtime_status != RPM_ACTIVE)
415 spin_unlock(&parent->power.lock);
417 spin_lock(&dev->power.lock);
423 __update_runtime_status(dev, RPM_RESUMING);
425 if (dev->bus && dev->bus->pm && dev->bus->pm->runtime_resume) {
426 spin_unlock_irq(&dev->power.lock);
428 retval = dev->bus->pm->runtime_resume(dev);
430 spin_lock_irq(&dev->power.lock);
431 dev->power.runtime_error = retval;
432 } else if (dev->type && dev->type->pm
433 && dev->type->pm->runtime_resume) {
434 spin_unlock_irq(&dev->power.lock);
436 retval = dev->type->pm->runtime_resume(dev);
438 spin_lock_irq(&dev->power.lock);
439 dev->power.runtime_error = retval;
440 } else if (dev->class && dev->class->pm
441 && dev->class->pm->runtime_resume) {
442 spin_unlock_irq(&dev->power.lock);
444 retval = dev->class->pm->runtime_resume(dev);
446 spin_lock_irq(&dev->power.lock);
447 dev->power.runtime_error = retval;
453 __update_runtime_status(dev, RPM_SUSPENDED);
454 pm_runtime_cancel_pending(dev);
456 __update_runtime_status(dev, RPM_ACTIVE);
458 atomic_inc(&parent->power.child_count);
460 wake_up_all(&dev->power.wait_queue);
463 __pm_request_idle(dev);
467 spin_unlock_irq(&dev->power.lock);
469 pm_runtime_put(parent);
471 spin_lock_irq(&dev->power.lock);
474 dev_dbg(dev, "%s returns %d\n", __func__, retval);
480 * pm_runtime_resume - Carry out run-time resume of given device.
481 * @dev: Device to suspend.
483 int pm_runtime_resume(struct device *dev)
487 spin_lock_irq(&dev->power.lock);
488 retval = __pm_runtime_resume(dev, 0);
489 spin_unlock_irq(&dev->power.lock);
493 EXPORT_SYMBOL_GPL(pm_runtime_resume);
496 * pm_runtime_work - Universal run-time PM work function.
497 * @work: Work structure used for scheduling the execution of this function.
499 * Use @work to get the device object the work is to be done for, determine what
500 * is to be done and execute the appropriate run-time PM function.
502 static void pm_runtime_work(struct work_struct *work)
504 struct device *dev = container_of(work, struct device, power.work);
505 enum rpm_request req;
507 spin_lock_irq(&dev->power.lock);
509 if (!dev->power.request_pending)
512 req = dev->power.request;
513 dev->power.request = RPM_REQ_NONE;
514 dev->power.request_pending = false;
520 __pm_runtime_idle(dev);
522 case RPM_REQ_SUSPEND:
523 __pm_runtime_suspend(dev, RPM_NOWAIT);
526 __pm_runtime_resume(dev, RPM_NOWAIT);
531 spin_unlock_irq(&dev->power.lock);
535 * __pm_request_idle - Submit an idle notification request for given device.
536 * @dev: Device to handle.
538 * Check if the device's run-time PM status is correct for suspending the device
539 * and queue up a request to run __pm_runtime_idle() for it.
541 * This function must be called under dev->power.lock with interrupts disabled.
543 static int __pm_request_idle(struct device *dev)
547 if (dev->power.runtime_error)
549 else if (atomic_read(&dev->power.usage_count) > 0
550 || dev->power.disable_depth > 0
551 || dev->power.runtime_status == RPM_SUSPENDED
552 || dev->power.runtime_status == RPM_SUSPENDING)
554 else if (!pm_children_suspended(dev))
559 if (dev->power.request_pending) {
560 /* Any requests other then RPM_REQ_IDLE take precedence. */
561 if (dev->power.request == RPM_REQ_NONE)
562 dev->power.request = RPM_REQ_IDLE;
563 else if (dev->power.request != RPM_REQ_IDLE)
568 dev->power.request = RPM_REQ_IDLE;
569 dev->power.request_pending = true;
570 queue_work(pm_wq, &dev->power.work);
576 * pm_request_idle - Submit an idle notification request for given device.
577 * @dev: Device to handle.
579 int pm_request_idle(struct device *dev)
584 spin_lock_irqsave(&dev->power.lock, flags);
585 retval = __pm_request_idle(dev);
586 spin_unlock_irqrestore(&dev->power.lock, flags);
590 EXPORT_SYMBOL_GPL(pm_request_idle);
593 * __pm_request_suspend - Submit a suspend request for given device.
594 * @dev: Device to suspend.
596 * This function must be called under dev->power.lock with interrupts disabled.
598 static int __pm_request_suspend(struct device *dev)
602 if (dev->power.runtime_error)
605 if (dev->power.runtime_status == RPM_SUSPENDED)
607 else if (atomic_read(&dev->power.usage_count) > 0
608 || dev->power.disable_depth > 0)
610 else if (dev->power.runtime_status == RPM_SUSPENDING)
611 retval = -EINPROGRESS;
612 else if (!pm_children_suspended(dev))
617 pm_runtime_deactivate_timer(dev);
619 if (dev->power.request_pending) {
621 * Pending resume requests take precedence over us, but we can
622 * overtake any other pending request.
624 if (dev->power.request == RPM_REQ_RESUME)
626 else if (dev->power.request != RPM_REQ_SUSPEND)
627 dev->power.request = retval ?
628 RPM_REQ_NONE : RPM_REQ_SUSPEND;
634 dev->power.request = RPM_REQ_SUSPEND;
635 dev->power.request_pending = true;
636 queue_work(pm_wq, &dev->power.work);
642 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
643 * @data: Device pointer passed by pm_schedule_suspend().
645 * Check if the time is right and execute __pm_request_suspend() in that case.
647 static void pm_suspend_timer_fn(unsigned long data)
649 struct device *dev = (struct device *)data;
651 unsigned long expires;
653 spin_lock_irqsave(&dev->power.lock, flags);
655 expires = dev->power.timer_expires;
656 /* If 'expire' is after 'jiffies' we've been called too early. */
657 if (expires > 0 && !time_after(expires, jiffies)) {
658 dev->power.timer_expires = 0;
659 __pm_request_suspend(dev);
662 spin_unlock_irqrestore(&dev->power.lock, flags);
666 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
667 * @dev: Device to suspend.
668 * @delay: Time to wait before submitting a suspend request, in milliseconds.
670 int pm_schedule_suspend(struct device *dev, unsigned int delay)
675 spin_lock_irqsave(&dev->power.lock, flags);
677 if (dev->power.runtime_error) {
683 retval = __pm_request_suspend(dev);
687 pm_runtime_deactivate_timer(dev);
689 if (dev->power.request_pending) {
691 * Pending resume requests take precedence over us, but any
692 * other pending requests have to be canceled.
694 if (dev->power.request == RPM_REQ_RESUME) {
698 dev->power.request = RPM_REQ_NONE;
701 if (dev->power.runtime_status == RPM_SUSPENDED)
703 else if (atomic_read(&dev->power.usage_count) > 0
704 || dev->power.disable_depth > 0)
706 else if (!pm_children_suspended(dev))
711 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
712 if (!dev->power.timer_expires)
713 dev->power.timer_expires = 1;
714 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
717 spin_unlock_irqrestore(&dev->power.lock, flags);
721 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
724 * pm_request_resume - Submit a resume request for given device.
725 * @dev: Device to resume.
727 * This function must be called under dev->power.lock with interrupts disabled.
729 static int __pm_request_resume(struct device *dev)
733 if (dev->power.runtime_error)
736 if (dev->power.runtime_status == RPM_ACTIVE)
738 else if (dev->power.runtime_status == RPM_RESUMING)
739 retval = -EINPROGRESS;
740 else if (dev->power.disable_depth > 0)
745 pm_runtime_deactivate_timer(dev);
747 if (dev->power.runtime_status == RPM_SUSPENDING) {
748 dev->power.deferred_resume = true;
751 if (dev->power.request_pending) {
752 /* If non-resume request is pending, we can overtake it. */
753 dev->power.request = retval ? RPM_REQ_NONE : RPM_REQ_RESUME;
759 dev->power.request = RPM_REQ_RESUME;
760 dev->power.request_pending = true;
761 queue_work(pm_wq, &dev->power.work);
767 * pm_request_resume - Submit a resume request for given device.
768 * @dev: Device to resume.
770 int pm_request_resume(struct device *dev)
775 spin_lock_irqsave(&dev->power.lock, flags);
776 retval = __pm_request_resume(dev);
777 spin_unlock_irqrestore(&dev->power.lock, flags);
781 EXPORT_SYMBOL_GPL(pm_request_resume);
784 * __pm_runtime_get - Reference count a device and wake it up, if necessary.
785 * @dev: Device to handle.
786 * @rpmflags: Flag bits.
788 * Increment the usage count of the device and resume it or submit a resume
789 * request for it, depending on the RPM_ASYNC flag bit.
791 int __pm_runtime_get(struct device *dev, int rpmflags)
795 atomic_inc(&dev->power.usage_count);
796 retval = (rpmflags & RPM_ASYNC) ?
797 pm_request_resume(dev) : pm_runtime_resume(dev);
801 EXPORT_SYMBOL_GPL(__pm_runtime_get);
804 * __pm_runtime_put - Decrement the device's usage counter and notify its bus.
805 * @dev: Device to handle.
806 * @rpmflags: Flag bits.
808 * Decrement the usage count of the device and if it reaches zero, carry out a
809 * synchronous idle notification or submit an idle notification request for it,
810 * depending on the RPM_ASYNC flag bit.
812 int __pm_runtime_put(struct device *dev, int rpmflags)
816 if (atomic_dec_and_test(&dev->power.usage_count))
817 retval = (rpmflags & RPM_ASYNC) ?
818 pm_request_idle(dev) : pm_runtime_idle(dev);
822 EXPORT_SYMBOL_GPL(__pm_runtime_put);
825 * __pm_runtime_set_status - Set run-time PM status of a device.
826 * @dev: Device to handle.
827 * @status: New run-time PM status of the device.
829 * If run-time PM of the device is disabled or its power.runtime_error field is
830 * different from zero, the status may be changed either to RPM_ACTIVE, or to
831 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
832 * However, if the device has a parent and the parent is not active, and the
833 * parent's power.ignore_children flag is unset, the device's status cannot be
834 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
836 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
837 * and the device parent's counter of unsuspended children is modified to
838 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
839 * notification request for the parent is submitted.
841 int __pm_runtime_set_status(struct device *dev, unsigned int status)
843 struct device *parent = dev->parent;
845 bool notify_parent = false;
848 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
851 spin_lock_irqsave(&dev->power.lock, flags);
853 if (!dev->power.runtime_error && !dev->power.disable_depth) {
858 if (dev->power.runtime_status == status)
861 if (status == RPM_SUSPENDED) {
862 /* It always is possible to set the status to 'suspended'. */
864 atomic_add_unless(&parent->power.child_count, -1, 0);
865 notify_parent = !parent->power.ignore_children;
871 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
874 * It is invalid to put an active child under a parent that is
875 * not active, has run-time PM enabled and the
876 * 'power.ignore_children' flag unset.
878 if (!parent->power.disable_depth
879 && !parent->power.ignore_children
880 && parent->power.runtime_status != RPM_ACTIVE)
882 else if (dev->power.runtime_status == RPM_SUSPENDED)
883 atomic_inc(&parent->power.child_count);
885 spin_unlock(&parent->power.lock);
892 __update_runtime_status(dev, status);
893 dev->power.runtime_error = 0;
895 spin_unlock_irqrestore(&dev->power.lock, flags);
898 pm_request_idle(parent);
902 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
905 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
906 * @dev: Device to handle.
908 * Flush all pending requests for the device from pm_wq and wait for all
909 * run-time PM operations involving the device in progress to complete.
911 * Should be called under dev->power.lock with interrupts disabled.
913 static void __pm_runtime_barrier(struct device *dev)
915 pm_runtime_deactivate_timer(dev);
917 if (dev->power.request_pending) {
918 dev->power.request = RPM_REQ_NONE;
919 spin_unlock_irq(&dev->power.lock);
921 cancel_work_sync(&dev->power.work);
923 spin_lock_irq(&dev->power.lock);
924 dev->power.request_pending = false;
927 if (dev->power.runtime_status == RPM_SUSPENDING
928 || dev->power.runtime_status == RPM_RESUMING
929 || dev->power.idle_notification) {
932 /* Suspend, wake-up or idle notification in progress. */
934 prepare_to_wait(&dev->power.wait_queue, &wait,
935 TASK_UNINTERRUPTIBLE);
936 if (dev->power.runtime_status != RPM_SUSPENDING
937 && dev->power.runtime_status != RPM_RESUMING
938 && !dev->power.idle_notification)
940 spin_unlock_irq(&dev->power.lock);
944 spin_lock_irq(&dev->power.lock);
946 finish_wait(&dev->power.wait_queue, &wait);
951 * pm_runtime_barrier - Flush pending requests and wait for completions.
952 * @dev: Device to handle.
954 * Prevent the device from being suspended by incrementing its usage counter and
955 * if there's a pending resume request for the device, wake the device up.
956 * Next, make sure that all pending requests for the device have been flushed
957 * from pm_wq and wait for all run-time PM operations involving the device in
958 * progress to complete.
961 * 1, if there was a resume request pending and the device had to be woken up,
964 int pm_runtime_barrier(struct device *dev)
968 pm_runtime_get_noresume(dev);
969 spin_lock_irq(&dev->power.lock);
971 if (dev->power.request_pending
972 && dev->power.request == RPM_REQ_RESUME) {
973 __pm_runtime_resume(dev, 0);
977 __pm_runtime_barrier(dev);
979 spin_unlock_irq(&dev->power.lock);
980 pm_runtime_put_noidle(dev);
984 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
987 * __pm_runtime_disable - Disable run-time PM of a device.
988 * @dev: Device to handle.
989 * @check_resume: If set, check if there's a resume request for the device.
991 * Increment power.disable_depth for the device and if was zero previously,
992 * cancel all pending run-time PM requests for the device and wait for all
993 * operations in progress to complete. The device can be either active or
994 * suspended after its run-time PM has been disabled.
996 * If @check_resume is set and there's a resume request pending when
997 * __pm_runtime_disable() is called and power.disable_depth is zero, the
998 * function will wake up the device before disabling its run-time PM.
1000 void __pm_runtime_disable(struct device *dev, bool check_resume)
1002 spin_lock_irq(&dev->power.lock);
1004 if (dev->power.disable_depth > 0) {
1005 dev->power.disable_depth++;
1010 * Wake up the device if there's a resume request pending, because that
1011 * means there probably is some I/O to process and disabling run-time PM
1012 * shouldn't prevent the device from processing the I/O.
1014 if (check_resume && dev->power.request_pending
1015 && dev->power.request == RPM_REQ_RESUME) {
1017 * Prevent suspends and idle notifications from being carried
1018 * out after we have woken up the device.
1020 pm_runtime_get_noresume(dev);
1022 __pm_runtime_resume(dev, 0);
1024 pm_runtime_put_noidle(dev);
1027 if (!dev->power.disable_depth++)
1028 __pm_runtime_barrier(dev);
1031 spin_unlock_irq(&dev->power.lock);
1033 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1036 * pm_runtime_enable - Enable run-time PM of a device.
1037 * @dev: Device to handle.
1039 void pm_runtime_enable(struct device *dev)
1041 unsigned long flags;
1043 spin_lock_irqsave(&dev->power.lock, flags);
1045 if (dev->power.disable_depth > 0)
1046 dev->power.disable_depth--;
1048 dev_warn(dev, "Unbalanced %s!\n", __func__);
1050 spin_unlock_irqrestore(&dev->power.lock, flags);
1052 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1055 * pm_runtime_forbid - Block run-time PM of a device.
1056 * @dev: Device to handle.
1058 * Increase the device's usage count and clear its power.runtime_auto flag,
1059 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1062 void pm_runtime_forbid(struct device *dev)
1064 spin_lock_irq(&dev->power.lock);
1065 if (!dev->power.runtime_auto)
1068 dev->power.runtime_auto = false;
1069 atomic_inc(&dev->power.usage_count);
1070 __pm_runtime_resume(dev, 0);
1073 spin_unlock_irq(&dev->power.lock);
1075 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1078 * pm_runtime_allow - Unblock run-time PM of a device.
1079 * @dev: Device to handle.
1081 * Decrease the device's usage count and set its power.runtime_auto flag.
1083 void pm_runtime_allow(struct device *dev)
1085 spin_lock_irq(&dev->power.lock);
1086 if (dev->power.runtime_auto)
1089 dev->power.runtime_auto = true;
1090 if (atomic_dec_and_test(&dev->power.usage_count))
1091 __pm_runtime_idle(dev);
1094 spin_unlock_irq(&dev->power.lock);
1096 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1099 * pm_runtime_init - Initialize run-time PM fields in given device object.
1100 * @dev: Device object to initialize.
1102 void pm_runtime_init(struct device *dev)
1104 dev->power.runtime_status = RPM_SUSPENDED;
1105 dev->power.idle_notification = false;
1107 dev->power.disable_depth = 1;
1108 atomic_set(&dev->power.usage_count, 0);
1110 dev->power.runtime_error = 0;
1112 atomic_set(&dev->power.child_count, 0);
1113 pm_suspend_ignore_children(dev, false);
1114 dev->power.runtime_auto = true;
1116 dev->power.request_pending = false;
1117 dev->power.request = RPM_REQ_NONE;
1118 dev->power.deferred_resume = false;
1119 dev->power.accounting_timestamp = jiffies;
1120 INIT_WORK(&dev->power.work, pm_runtime_work);
1122 dev->power.timer_expires = 0;
1123 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1124 (unsigned long)dev);
1126 init_waitqueue_head(&dev->power.wait_queue);
1130 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1131 * @dev: Device object being removed from device hierarchy.
1133 void pm_runtime_remove(struct device *dev)
1135 __pm_runtime_disable(dev, false);
1137 /* Change the status back to 'suspended' to match the initial status. */
1138 if (dev->power.runtime_status == RPM_ACTIVE)
1139 pm_runtime_set_suspended(dev);