2 * drivers/base/power/runtime.c - Helper functions for device runtime 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>
12 #include <trace/events/rpm.h>
15 static int rpm_resume(struct device *dev, int rpmflags);
16 static int rpm_suspend(struct device *dev, int rpmflags);
19 * update_pm_runtime_accounting - Update the time accounting of power states
20 * @dev: Device to update the accounting for
22 * In order to be able to have time accounting of the various power states
23 * (as used by programs such as PowerTOP to show the effectiveness of runtime
24 * PM), we need to track the time spent in each state.
25 * update_pm_runtime_accounting must be called each time before the
26 * runtime_status field is updated, to account the time in the old state
29 void update_pm_runtime_accounting(struct device *dev)
31 unsigned long now = jiffies;
34 delta = now - dev->power.accounting_timestamp;
39 dev->power.accounting_timestamp = now;
41 if (dev->power.disable_depth > 0)
44 if (dev->power.runtime_status == RPM_SUSPENDED)
45 dev->power.suspended_jiffies += delta;
47 dev->power.active_jiffies += delta;
50 static void __update_runtime_status(struct device *dev, enum rpm_status status)
52 update_pm_runtime_accounting(dev);
53 dev->power.runtime_status = status;
57 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
58 * @dev: Device to handle.
60 static void pm_runtime_deactivate_timer(struct device *dev)
62 if (dev->power.timer_expires > 0) {
63 del_timer(&dev->power.suspend_timer);
64 dev->power.timer_expires = 0;
69 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
70 * @dev: Device to handle.
72 static void pm_runtime_cancel_pending(struct device *dev)
74 pm_runtime_deactivate_timer(dev);
76 * In case there's a request pending, make sure its work function will
77 * return without doing anything.
79 dev->power.request = RPM_REQ_NONE;
83 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
84 * @dev: Device to handle.
86 * Compute the autosuspend-delay expiration time based on the device's
87 * power.last_busy time. If the delay has already expired or is disabled
88 * (negative) or the power.use_autosuspend flag isn't set, return 0.
89 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
91 * This function may be called either with or without dev->power.lock held.
92 * Either way it can be racy, since power.last_busy may be updated at any time.
94 unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
96 int autosuspend_delay;
98 unsigned long last_busy;
99 unsigned long expires = 0;
101 if (!dev->power.use_autosuspend)
104 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
105 if (autosuspend_delay < 0)
108 last_busy = ACCESS_ONCE(dev->power.last_busy);
109 elapsed = jiffies - last_busy;
111 goto out; /* jiffies has wrapped around. */
114 * If the autosuspend_delay is >= 1 second, align the timer by rounding
115 * up to the nearest second.
117 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
118 if (autosuspend_delay >= 1000)
119 expires = round_jiffies(expires);
121 if (elapsed >= expires - last_busy)
122 expires = 0; /* Already expired. */
127 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
130 * rpm_check_suspend_allowed - Test whether a device may be suspended.
131 * @dev: Device to test.
133 static int rpm_check_suspend_allowed(struct device *dev)
137 if (dev->power.runtime_error)
139 else if (dev->power.disable_depth > 0)
141 else if (atomic_read(&dev->power.usage_count) > 0)
143 else if (!pm_children_suspended(dev))
146 /* Pending resume requests take precedence over suspends. */
147 else if ((dev->power.deferred_resume
148 && dev->power.runtime_status == RPM_SUSPENDING)
149 || (dev->power.request_pending
150 && dev->power.request == RPM_REQ_RESUME))
152 else if (dev->power.runtime_status == RPM_SUSPENDED)
159 * __rpm_callback - Run a given runtime PM callback for a given device.
160 * @cb: Runtime PM callback to run.
161 * @dev: Device to run the callback for.
163 static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
164 __releases(&dev->power.lock) __acquires(&dev->power.lock)
168 if (dev->power.irq_safe)
169 spin_unlock(&dev->power.lock);
171 spin_unlock_irq(&dev->power.lock);
175 if (dev->power.irq_safe)
176 spin_lock(&dev->power.lock);
178 spin_lock_irq(&dev->power.lock);
184 * rpm_idle - Notify device bus type if the device can be suspended.
185 * @dev: Device to notify the bus type about.
186 * @rpmflags: Flag bits.
188 * Check if the device's runtime PM status allows it to be suspended. If
189 * another idle notification has been started earlier, return immediately. If
190 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
191 * run the ->runtime_idle() callback directly.
193 * This function must be called under dev->power.lock with interrupts disabled.
195 static int rpm_idle(struct device *dev, int rpmflags)
197 int (*callback)(struct device *);
200 trace_rpm_idle(dev, rpmflags);
201 retval = rpm_check_suspend_allowed(dev);
203 ; /* Conditions are wrong. */
205 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
206 else if (dev->power.runtime_status != RPM_ACTIVE)
210 * Any pending request other than an idle notification takes
211 * precedence over us, except that the timer may be running.
213 else if (dev->power.request_pending &&
214 dev->power.request > RPM_REQ_IDLE)
217 /* Act as though RPM_NOWAIT is always set. */
218 else if (dev->power.idle_notification)
219 retval = -EINPROGRESS;
223 /* Pending requests need to be canceled. */
224 dev->power.request = RPM_REQ_NONE;
226 if (dev->power.no_callbacks) {
227 /* Assume ->runtime_idle() callback would have suspended. */
228 retval = rpm_suspend(dev, rpmflags);
232 /* Carry out an asynchronous or a synchronous idle notification. */
233 if (rpmflags & RPM_ASYNC) {
234 dev->power.request = RPM_REQ_IDLE;
235 if (!dev->power.request_pending) {
236 dev->power.request_pending = true;
237 queue_work(pm_wq, &dev->power.work);
242 dev->power.idle_notification = true;
245 callback = dev->pm_domain->ops.runtime_idle;
246 else if (dev->type && dev->type->pm)
247 callback = dev->type->pm->runtime_idle;
248 else if (dev->class && dev->class->pm)
249 callback = dev->class->pm->runtime_idle;
250 else if (dev->bus && dev->bus->pm)
251 callback = dev->bus->pm->runtime_idle;
256 __rpm_callback(callback, dev);
258 dev->power.idle_notification = false;
259 wake_up_all(&dev->power.wait_queue);
262 trace_rpm_return_int(dev, _THIS_IP_, retval);
267 * rpm_callback - Run a given runtime PM callback for a given device.
268 * @cb: Runtime PM callback to run.
269 * @dev: Device to run the callback for.
271 static int rpm_callback(int (*cb)(struct device *), struct device *dev)
278 retval = __rpm_callback(cb, dev);
280 dev->power.runtime_error = retval;
281 return retval != -EACCES ? retval : -EIO;
285 * rpm_suspend - Carry out runtime suspend of given device.
286 * @dev: Device to suspend.
287 * @rpmflags: Flag bits.
289 * Check if the device's runtime PM status allows it to be suspended.
290 * Cancel a pending idle notification, autosuspend or suspend. If
291 * another suspend has been started earlier, either return immediately
292 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
293 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
294 * otherwise run the ->runtime_suspend() callback directly. When
295 * ->runtime_suspend succeeded, if a deferred resume was requested while
296 * the callback was running then carry it out, otherwise send an idle
297 * notification for its parent (if the suspend succeeded and both
298 * ignore_children of parent->power and irq_safe of dev->power are not set).
300 * This function must be called under dev->power.lock with interrupts disabled.
302 static int rpm_suspend(struct device *dev, int rpmflags)
303 __releases(&dev->power.lock) __acquires(&dev->power.lock)
305 int (*callback)(struct device *);
306 struct device *parent = NULL;
309 trace_rpm_suspend(dev, rpmflags);
312 retval = rpm_check_suspend_allowed(dev);
315 ; /* Conditions are wrong. */
317 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
318 else if (dev->power.runtime_status == RPM_RESUMING &&
319 !(rpmflags & RPM_ASYNC))
324 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
325 if ((rpmflags & RPM_AUTO)
326 && dev->power.runtime_status != RPM_SUSPENDING) {
327 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
330 /* Pending requests need to be canceled. */
331 dev->power.request = RPM_REQ_NONE;
334 * Optimization: If the timer is already running and is
335 * set to expire at or before the autosuspend delay,
336 * avoid the overhead of resetting it. Just let it
337 * expire; pm_suspend_timer_fn() will take care of the
340 if (!(dev->power.timer_expires && time_before_eq(
341 dev->power.timer_expires, expires))) {
342 dev->power.timer_expires = expires;
343 mod_timer(&dev->power.suspend_timer, expires);
345 dev->power.timer_autosuspends = 1;
350 /* Other scheduled or pending requests need to be canceled. */
351 pm_runtime_cancel_pending(dev);
353 if (dev->power.runtime_status == RPM_SUSPENDING) {
356 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
357 retval = -EINPROGRESS;
361 if (dev->power.irq_safe) {
362 spin_unlock(&dev->power.lock);
366 spin_lock(&dev->power.lock);
370 /* Wait for the other suspend running in parallel with us. */
372 prepare_to_wait(&dev->power.wait_queue, &wait,
373 TASK_UNINTERRUPTIBLE);
374 if (dev->power.runtime_status != RPM_SUSPENDING)
377 spin_unlock_irq(&dev->power.lock);
381 spin_lock_irq(&dev->power.lock);
383 finish_wait(&dev->power.wait_queue, &wait);
387 dev->power.deferred_resume = false;
388 if (dev->power.no_callbacks)
389 goto no_callback; /* Assume success. */
391 /* Carry out an asynchronous or a synchronous suspend. */
392 if (rpmflags & RPM_ASYNC) {
393 dev->power.request = (rpmflags & RPM_AUTO) ?
394 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
395 if (!dev->power.request_pending) {
396 dev->power.request_pending = true;
397 queue_work(pm_wq, &dev->power.work);
402 __update_runtime_status(dev, RPM_SUSPENDING);
405 callback = dev->pm_domain->ops.runtime_suspend;
406 else if (dev->type && dev->type->pm)
407 callback = dev->type->pm->runtime_suspend;
408 else if (dev->class && dev->class->pm)
409 callback = dev->class->pm->runtime_suspend;
410 else if (dev->bus && dev->bus->pm)
411 callback = dev->bus->pm->runtime_suspend;
415 retval = rpm_callback(callback, dev);
417 __update_runtime_status(dev, RPM_ACTIVE);
418 dev->power.deferred_resume = false;
419 if (retval == -EAGAIN || retval == -EBUSY)
420 dev->power.runtime_error = 0;
422 pm_runtime_cancel_pending(dev);
423 wake_up_all(&dev->power.wait_queue);
427 __update_runtime_status(dev, RPM_SUSPENDED);
428 pm_runtime_deactivate_timer(dev);
431 parent = dev->parent;
432 atomic_add_unless(&parent->power.child_count, -1, 0);
434 wake_up_all(&dev->power.wait_queue);
436 if (dev->power.deferred_resume) {
442 /* Maybe the parent is now able to suspend. */
443 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
444 spin_unlock(&dev->power.lock);
446 spin_lock(&parent->power.lock);
447 rpm_idle(parent, RPM_ASYNC);
448 spin_unlock(&parent->power.lock);
450 spin_lock(&dev->power.lock);
454 trace_rpm_return_int(dev, _THIS_IP_, retval);
460 * rpm_resume - Carry out runtime resume of given device.
461 * @dev: Device to resume.
462 * @rpmflags: Flag bits.
464 * Check if the device's runtime PM status allows it to be resumed. Cancel
465 * any scheduled or pending requests. If another resume has been started
466 * earlier, either return immediately or wait for it to finish, depending on the
467 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
468 * parallel with this function, either tell the other process to resume after
469 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
470 * flag is set then queue a resume request; otherwise run the
471 * ->runtime_resume() callback directly. Queue an idle notification for the
472 * device if the resume succeeded.
474 * This function must be called under dev->power.lock with interrupts disabled.
476 static int rpm_resume(struct device *dev, int rpmflags)
477 __releases(&dev->power.lock) __acquires(&dev->power.lock)
479 int (*callback)(struct device *);
480 struct device *parent = NULL;
483 trace_rpm_resume(dev, rpmflags);
486 if (dev->power.runtime_error)
488 else if (dev->power.disable_depth > 0)
494 * Other scheduled or pending requests need to be canceled. Small
495 * optimization: If an autosuspend timer is running, leave it running
496 * rather than cancelling it now only to restart it again in the near
499 dev->power.request = RPM_REQ_NONE;
500 if (!dev->power.timer_autosuspends)
501 pm_runtime_deactivate_timer(dev);
503 if (dev->power.runtime_status == RPM_ACTIVE) {
508 if (dev->power.runtime_status == RPM_RESUMING
509 || dev->power.runtime_status == RPM_SUSPENDING) {
512 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
513 if (dev->power.runtime_status == RPM_SUSPENDING)
514 dev->power.deferred_resume = true;
516 retval = -EINPROGRESS;
520 if (dev->power.irq_safe) {
521 spin_unlock(&dev->power.lock);
525 spin_lock(&dev->power.lock);
529 /* Wait for the operation carried out in parallel with us. */
531 prepare_to_wait(&dev->power.wait_queue, &wait,
532 TASK_UNINTERRUPTIBLE);
533 if (dev->power.runtime_status != RPM_RESUMING
534 && dev->power.runtime_status != RPM_SUSPENDING)
537 spin_unlock_irq(&dev->power.lock);
541 spin_lock_irq(&dev->power.lock);
543 finish_wait(&dev->power.wait_queue, &wait);
548 * See if we can skip waking up the parent. This is safe only if
549 * power.no_callbacks is set, because otherwise we don't know whether
550 * the resume will actually succeed.
552 if (dev->power.no_callbacks && !parent && dev->parent) {
553 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
554 if (dev->parent->power.disable_depth > 0
555 || dev->parent->power.ignore_children
556 || dev->parent->power.runtime_status == RPM_ACTIVE) {
557 atomic_inc(&dev->parent->power.child_count);
558 spin_unlock(&dev->parent->power.lock);
559 goto no_callback; /* Assume success. */
561 spin_unlock(&dev->parent->power.lock);
564 /* Carry out an asynchronous or a synchronous resume. */
565 if (rpmflags & RPM_ASYNC) {
566 dev->power.request = RPM_REQ_RESUME;
567 if (!dev->power.request_pending) {
568 dev->power.request_pending = true;
569 queue_work(pm_wq, &dev->power.work);
575 if (!parent && dev->parent) {
577 * Increment the parent's usage counter and resume it if
578 * necessary. Not needed if dev is irq-safe; then the
579 * parent is permanently resumed.
581 parent = dev->parent;
582 if (dev->power.irq_safe)
584 spin_unlock(&dev->power.lock);
586 pm_runtime_get_noresume(parent);
588 spin_lock(&parent->power.lock);
590 * We can resume if the parent's runtime PM is disabled or it
591 * is set to ignore children.
593 if (!parent->power.disable_depth
594 && !parent->power.ignore_children) {
595 rpm_resume(parent, 0);
596 if (parent->power.runtime_status != RPM_ACTIVE)
599 spin_unlock(&parent->power.lock);
601 spin_lock(&dev->power.lock);
608 if (dev->power.no_callbacks)
609 goto no_callback; /* Assume success. */
611 __update_runtime_status(dev, RPM_RESUMING);
614 callback = dev->pm_domain->ops.runtime_resume;
615 else if (dev->type && dev->type->pm)
616 callback = dev->type->pm->runtime_resume;
617 else if (dev->class && dev->class->pm)
618 callback = dev->class->pm->runtime_resume;
619 else if (dev->bus && dev->bus->pm)
620 callback = dev->bus->pm->runtime_resume;
624 retval = rpm_callback(callback, dev);
626 __update_runtime_status(dev, RPM_SUSPENDED);
627 pm_runtime_cancel_pending(dev);
630 __update_runtime_status(dev, RPM_ACTIVE);
632 atomic_inc(&parent->power.child_count);
634 wake_up_all(&dev->power.wait_queue);
637 rpm_idle(dev, RPM_ASYNC);
640 if (parent && !dev->power.irq_safe) {
641 spin_unlock_irq(&dev->power.lock);
643 pm_runtime_put(parent);
645 spin_lock_irq(&dev->power.lock);
648 trace_rpm_return_int(dev, _THIS_IP_, retval);
654 * pm_runtime_work - Universal runtime PM work function.
655 * @work: Work structure used for scheduling the execution of this function.
657 * Use @work to get the device object the work is to be done for, determine what
658 * is to be done and execute the appropriate runtime PM function.
660 static void pm_runtime_work(struct work_struct *work)
662 struct device *dev = container_of(work, struct device, power.work);
663 enum rpm_request req;
665 spin_lock_irq(&dev->power.lock);
667 if (!dev->power.request_pending)
670 req = dev->power.request;
671 dev->power.request = RPM_REQ_NONE;
672 dev->power.request_pending = false;
678 rpm_idle(dev, RPM_NOWAIT);
680 case RPM_REQ_SUSPEND:
681 rpm_suspend(dev, RPM_NOWAIT);
683 case RPM_REQ_AUTOSUSPEND:
684 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
687 rpm_resume(dev, RPM_NOWAIT);
692 spin_unlock_irq(&dev->power.lock);
696 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
697 * @data: Device pointer passed by pm_schedule_suspend().
699 * Check if the time is right and queue a suspend request.
701 static void pm_suspend_timer_fn(unsigned long data)
703 struct device *dev = (struct device *)data;
705 unsigned long expires;
707 spin_lock_irqsave(&dev->power.lock, flags);
709 expires = dev->power.timer_expires;
710 /* If 'expire' is after 'jiffies' we've been called too early. */
711 if (expires > 0 && !time_after(expires, jiffies)) {
712 dev->power.timer_expires = 0;
713 rpm_suspend(dev, dev->power.timer_autosuspends ?
714 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
717 spin_unlock_irqrestore(&dev->power.lock, flags);
721 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
722 * @dev: Device to suspend.
723 * @delay: Time to wait before submitting a suspend request, in milliseconds.
725 int pm_schedule_suspend(struct device *dev, unsigned int delay)
730 spin_lock_irqsave(&dev->power.lock, flags);
733 retval = rpm_suspend(dev, RPM_ASYNC);
737 retval = rpm_check_suspend_allowed(dev);
741 /* Other scheduled or pending requests need to be canceled. */
742 pm_runtime_cancel_pending(dev);
744 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
745 dev->power.timer_expires += !dev->power.timer_expires;
746 dev->power.timer_autosuspends = 0;
747 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
750 spin_unlock_irqrestore(&dev->power.lock, flags);
754 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
757 * __pm_runtime_idle - Entry point for runtime idle operations.
758 * @dev: Device to send idle notification for.
759 * @rpmflags: Flag bits.
761 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
762 * return immediately if it is larger than zero. Then carry out an idle
763 * notification, either synchronous or asynchronous.
765 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
766 * or if pm_runtime_irq_safe() has been called.
768 int __pm_runtime_idle(struct device *dev, int rpmflags)
773 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
775 if (rpmflags & RPM_GET_PUT) {
776 if (!atomic_dec_and_test(&dev->power.usage_count))
780 spin_lock_irqsave(&dev->power.lock, flags);
781 retval = rpm_idle(dev, rpmflags);
782 spin_unlock_irqrestore(&dev->power.lock, flags);
786 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
789 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
790 * @dev: Device to suspend.
791 * @rpmflags: Flag bits.
793 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
794 * return immediately if it is larger than zero. Then carry out a suspend,
795 * either synchronous or asynchronous.
797 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
798 * or if pm_runtime_irq_safe() has been called.
800 int __pm_runtime_suspend(struct device *dev, int rpmflags)
805 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
807 if (rpmflags & RPM_GET_PUT) {
808 if (!atomic_dec_and_test(&dev->power.usage_count))
812 spin_lock_irqsave(&dev->power.lock, flags);
813 retval = rpm_suspend(dev, rpmflags);
814 spin_unlock_irqrestore(&dev->power.lock, flags);
818 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
821 * __pm_runtime_resume - Entry point for runtime resume operations.
822 * @dev: Device to resume.
823 * @rpmflags: Flag bits.
825 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
826 * carry out a resume, either synchronous or asynchronous.
828 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
829 * or if pm_runtime_irq_safe() has been called.
831 int __pm_runtime_resume(struct device *dev, int rpmflags)
836 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
838 if (rpmflags & RPM_GET_PUT)
839 atomic_inc(&dev->power.usage_count);
841 spin_lock_irqsave(&dev->power.lock, flags);
842 retval = rpm_resume(dev, rpmflags);
843 spin_unlock_irqrestore(&dev->power.lock, flags);
847 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
850 * __pm_runtime_set_status - Set runtime PM status of a device.
851 * @dev: Device to handle.
852 * @status: New runtime PM status of the device.
854 * If runtime PM of the device is disabled or its power.runtime_error field is
855 * different from zero, the status may be changed either to RPM_ACTIVE, or to
856 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
857 * However, if the device has a parent and the parent is not active, and the
858 * parent's power.ignore_children flag is unset, the device's status cannot be
859 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
861 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
862 * and the device parent's counter of unsuspended children is modified to
863 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
864 * notification request for the parent is submitted.
866 int __pm_runtime_set_status(struct device *dev, unsigned int status)
868 struct device *parent = dev->parent;
870 bool notify_parent = false;
873 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
876 spin_lock_irqsave(&dev->power.lock, flags);
878 if (!dev->power.runtime_error && !dev->power.disable_depth) {
883 if (dev->power.runtime_status == status)
886 if (status == RPM_SUSPENDED) {
887 /* It always is possible to set the status to 'suspended'. */
889 atomic_add_unless(&parent->power.child_count, -1, 0);
890 notify_parent = !parent->power.ignore_children;
896 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
899 * It is invalid to put an active child under a parent that is
900 * not active, has runtime PM enabled and the
901 * 'power.ignore_children' flag unset.
903 if (!parent->power.disable_depth
904 && !parent->power.ignore_children
905 && parent->power.runtime_status != RPM_ACTIVE)
907 else if (dev->power.runtime_status == RPM_SUSPENDED)
908 atomic_inc(&parent->power.child_count);
910 spin_unlock(&parent->power.lock);
917 __update_runtime_status(dev, status);
918 dev->power.runtime_error = 0;
920 spin_unlock_irqrestore(&dev->power.lock, flags);
923 pm_request_idle(parent);
927 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
930 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
931 * @dev: Device to handle.
933 * Flush all pending requests for the device from pm_wq and wait for all
934 * runtime PM operations involving the device in progress to complete.
936 * Should be called under dev->power.lock with interrupts disabled.
938 static void __pm_runtime_barrier(struct device *dev)
940 pm_runtime_deactivate_timer(dev);
942 if (dev->power.request_pending) {
943 dev->power.request = RPM_REQ_NONE;
944 spin_unlock_irq(&dev->power.lock);
946 cancel_work_sync(&dev->power.work);
948 spin_lock_irq(&dev->power.lock);
949 dev->power.request_pending = false;
952 if (dev->power.runtime_status == RPM_SUSPENDING
953 || dev->power.runtime_status == RPM_RESUMING
954 || dev->power.idle_notification) {
957 /* Suspend, wake-up or idle notification in progress. */
959 prepare_to_wait(&dev->power.wait_queue, &wait,
960 TASK_UNINTERRUPTIBLE);
961 if (dev->power.runtime_status != RPM_SUSPENDING
962 && dev->power.runtime_status != RPM_RESUMING
963 && !dev->power.idle_notification)
965 spin_unlock_irq(&dev->power.lock);
969 spin_lock_irq(&dev->power.lock);
971 finish_wait(&dev->power.wait_queue, &wait);
976 * pm_runtime_barrier - Flush pending requests and wait for completions.
977 * @dev: Device to handle.
979 * Prevent the device from being suspended by incrementing its usage counter and
980 * if there's a pending resume request for the device, wake the device up.
981 * Next, make sure that all pending requests for the device have been flushed
982 * from pm_wq and wait for all runtime PM operations involving the device in
983 * progress to complete.
986 * 1, if there was a resume request pending and the device had to be woken up,
989 int pm_runtime_barrier(struct device *dev)
993 pm_runtime_get_noresume(dev);
994 spin_lock_irq(&dev->power.lock);
996 if (dev->power.request_pending
997 && dev->power.request == RPM_REQ_RESUME) {
1002 __pm_runtime_barrier(dev);
1004 spin_unlock_irq(&dev->power.lock);
1005 pm_runtime_put_noidle(dev);
1009 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1012 * __pm_runtime_disable - Disable runtime PM of a device.
1013 * @dev: Device to handle.
1014 * @check_resume: If set, check if there's a resume request for the device.
1016 * Increment power.disable_depth for the device and if was zero previously,
1017 * cancel all pending runtime PM requests for the device and wait for all
1018 * operations in progress to complete. The device can be either active or
1019 * suspended after its runtime PM has been disabled.
1021 * If @check_resume is set and there's a resume request pending when
1022 * __pm_runtime_disable() is called and power.disable_depth is zero, the
1023 * function will wake up the device before disabling its runtime PM.
1025 void __pm_runtime_disable(struct device *dev, bool check_resume)
1027 spin_lock_irq(&dev->power.lock);
1029 if (dev->power.disable_depth > 0) {
1030 dev->power.disable_depth++;
1035 * Wake up the device if there's a resume request pending, because that
1036 * means there probably is some I/O to process and disabling runtime PM
1037 * shouldn't prevent the device from processing the I/O.
1039 if (check_resume && dev->power.request_pending
1040 && dev->power.request == RPM_REQ_RESUME) {
1042 * Prevent suspends and idle notifications from being carried
1043 * out after we have woken up the device.
1045 pm_runtime_get_noresume(dev);
1049 pm_runtime_put_noidle(dev);
1052 if (!dev->power.disable_depth++)
1053 __pm_runtime_barrier(dev);
1056 spin_unlock_irq(&dev->power.lock);
1058 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1061 * pm_runtime_enable - Enable runtime PM of a device.
1062 * @dev: Device to handle.
1064 void pm_runtime_enable(struct device *dev)
1066 unsigned long flags;
1068 spin_lock_irqsave(&dev->power.lock, flags);
1070 if (dev->power.disable_depth > 0)
1071 dev->power.disable_depth--;
1073 dev_warn(dev, "Unbalanced %s!\n", __func__);
1075 spin_unlock_irqrestore(&dev->power.lock, flags);
1077 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1080 * pm_runtime_forbid - Block runtime PM of a device.
1081 * @dev: Device to handle.
1083 * Increase the device's usage count and clear its power.runtime_auto flag,
1084 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1087 void pm_runtime_forbid(struct device *dev)
1089 spin_lock_irq(&dev->power.lock);
1090 if (!dev->power.runtime_auto)
1093 dev->power.runtime_auto = false;
1094 atomic_inc(&dev->power.usage_count);
1098 spin_unlock_irq(&dev->power.lock);
1100 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1103 * pm_runtime_allow - Unblock runtime PM of a device.
1104 * @dev: Device to handle.
1106 * Decrease the device's usage count and set its power.runtime_auto flag.
1108 void pm_runtime_allow(struct device *dev)
1110 spin_lock_irq(&dev->power.lock);
1111 if (dev->power.runtime_auto)
1114 dev->power.runtime_auto = true;
1115 if (atomic_dec_and_test(&dev->power.usage_count))
1116 rpm_idle(dev, RPM_AUTO);
1119 spin_unlock_irq(&dev->power.lock);
1121 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1124 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1125 * @dev: Device to handle.
1127 * Set the power.no_callbacks flag, which tells the PM core that this
1128 * device is power-managed through its parent and has no runtime PM
1129 * callbacks of its own. The runtime sysfs attributes will be removed.
1131 void pm_runtime_no_callbacks(struct device *dev)
1133 spin_lock_irq(&dev->power.lock);
1134 dev->power.no_callbacks = 1;
1135 spin_unlock_irq(&dev->power.lock);
1136 if (device_is_registered(dev))
1137 rpm_sysfs_remove(dev);
1139 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1142 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1143 * @dev: Device to handle
1145 * Set the power.irq_safe flag, which tells the PM core that the
1146 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1147 * always be invoked with the spinlock held and interrupts disabled. It also
1148 * causes the parent's usage counter to be permanently incremented, preventing
1149 * the parent from runtime suspending -- otherwise an irq-safe child might have
1150 * to wait for a non-irq-safe parent.
1152 void pm_runtime_irq_safe(struct device *dev)
1155 pm_runtime_get_sync(dev->parent);
1156 spin_lock_irq(&dev->power.lock);
1157 dev->power.irq_safe = 1;
1158 spin_unlock_irq(&dev->power.lock);
1160 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1163 * update_autosuspend - Handle a change to a device's autosuspend settings.
1164 * @dev: Device to handle.
1165 * @old_delay: The former autosuspend_delay value.
1166 * @old_use: The former use_autosuspend value.
1168 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1169 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1171 * This function must be called under dev->power.lock with interrupts disabled.
1173 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1175 int delay = dev->power.autosuspend_delay;
1177 /* Should runtime suspend be prevented now? */
1178 if (dev->power.use_autosuspend && delay < 0) {
1180 /* If it used to be allowed then prevent it. */
1181 if (!old_use || old_delay >= 0) {
1182 atomic_inc(&dev->power.usage_count);
1187 /* Runtime suspend should be allowed now. */
1190 /* If it used to be prevented then allow it. */
1191 if (old_use && old_delay < 0)
1192 atomic_dec(&dev->power.usage_count);
1194 /* Maybe we can autosuspend now. */
1195 rpm_idle(dev, RPM_AUTO);
1200 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1201 * @dev: Device to handle.
1202 * @delay: Value of the new delay in milliseconds.
1204 * Set the device's power.autosuspend_delay value. If it changes to negative
1205 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1206 * changes the other way, allow runtime suspends.
1208 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1210 int old_delay, old_use;
1212 spin_lock_irq(&dev->power.lock);
1213 old_delay = dev->power.autosuspend_delay;
1214 old_use = dev->power.use_autosuspend;
1215 dev->power.autosuspend_delay = delay;
1216 update_autosuspend(dev, old_delay, old_use);
1217 spin_unlock_irq(&dev->power.lock);
1219 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1222 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1223 * @dev: Device to handle.
1224 * @use: New value for use_autosuspend.
1226 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
1227 * suspends as needed.
1229 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1231 int old_delay, old_use;
1233 spin_lock_irq(&dev->power.lock);
1234 old_delay = dev->power.autosuspend_delay;
1235 old_use = dev->power.use_autosuspend;
1236 dev->power.use_autosuspend = use;
1237 update_autosuspend(dev, old_delay, old_use);
1238 spin_unlock_irq(&dev->power.lock);
1240 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1243 * pm_runtime_init - Initialize runtime PM fields in given device object.
1244 * @dev: Device object to initialize.
1246 void pm_runtime_init(struct device *dev)
1248 dev->power.runtime_status = RPM_SUSPENDED;
1249 dev->power.idle_notification = false;
1251 dev->power.disable_depth = 1;
1252 atomic_set(&dev->power.usage_count, 0);
1254 dev->power.runtime_error = 0;
1256 atomic_set(&dev->power.child_count, 0);
1257 pm_suspend_ignore_children(dev, false);
1258 dev->power.runtime_auto = true;
1260 dev->power.request_pending = false;
1261 dev->power.request = RPM_REQ_NONE;
1262 dev->power.deferred_resume = false;
1263 dev->power.accounting_timestamp = jiffies;
1264 INIT_WORK(&dev->power.work, pm_runtime_work);
1266 dev->power.timer_expires = 0;
1267 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1268 (unsigned long)dev);
1270 init_waitqueue_head(&dev->power.wait_queue);
1274 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1275 * @dev: Device object being removed from device hierarchy.
1277 void pm_runtime_remove(struct device *dev)
1279 __pm_runtime_disable(dev, false);
1281 /* Change the status back to 'suspended' to match the initial status. */
1282 if (dev->power.runtime_status == RPM_ACTIVE)
1283 pm_runtime_set_suspended(dev);
1284 if (dev->power.irq_safe && dev->parent)
1285 pm_runtime_put_sync(dev->parent);