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
*);
171 int (*domain_callback
)(struct device
*);
174 retval
= rpm_check_suspend_allowed(dev
);
176 ; /* Conditions are wrong. */
178 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
179 else if (dev
->power
.runtime_status
!= RPM_ACTIVE
)
183 * Any pending request other than an idle notification takes
184 * precedence over us, except that the timer may be running.
186 else if (dev
->power
.request_pending
&&
187 dev
->power
.request
> RPM_REQ_IDLE
)
190 /* Act as though RPM_NOWAIT is always set. */
191 else if (dev
->power
.idle_notification
)
192 retval
= -EINPROGRESS
;
196 /* Pending requests need to be canceled. */
197 dev
->power
.request
= RPM_REQ_NONE
;
199 if (dev
->power
.no_callbacks
) {
200 /* Assume ->runtime_idle() callback would have suspended. */
201 retval
= rpm_suspend(dev
, rpmflags
);
205 /* Carry out an asynchronous or a synchronous idle notification. */
206 if (rpmflags
& RPM_ASYNC
) {
207 dev
->power
.request
= RPM_REQ_IDLE
;
208 if (!dev
->power
.request_pending
) {
209 dev
->power
.request_pending
= true;
210 queue_work(pm_wq
, &dev
->power
.work
);
215 dev
->power
.idle_notification
= true;
217 if (dev
->type
&& dev
->type
->pm
)
218 callback
= dev
->type
->pm
->runtime_idle
;
219 else if (dev
->class && dev
->class->pm
)
220 callback
= dev
->class->pm
->runtime_idle
;
221 else if (dev
->bus
&& dev
->bus
->pm
)
222 callback
= dev
->bus
->pm
->runtime_idle
;
227 domain_callback
= dev
->pwr_domain
->ops
.runtime_idle
;
229 domain_callback
= NULL
;
231 if (callback
|| domain_callback
) {
232 spin_unlock_irq(&dev
->power
.lock
);
235 retval
= domain_callback(dev
);
237 if (!retval
&& callback
)
240 spin_lock_irq(&dev
->power
.lock
);
243 dev
->power
.idle_notification
= false;
244 wake_up_all(&dev
->power
.wait_queue
);
251 * rpm_callback - Run a given runtime PM callback for a given device.
252 * @cb: Runtime PM callback to run.
253 * @dev: Device to run the callback for.
255 static int rpm_callback(int (*cb
)(struct device
*), struct device
*dev
)
256 __releases(&dev
->power
.lock
) __acquires(&dev
->power
.lock
)
263 if (dev
->power
.irq_safe
) {
266 spin_unlock_irq(&dev
->power
.lock
);
270 spin_lock_irq(&dev
->power
.lock
);
272 dev
->power
.runtime_error
= retval
;
277 * rpm_suspend - Carry out run-time suspend of given device.
278 * @dev: Device to suspend.
279 * @rpmflags: Flag bits.
281 * Check if the device's run-time PM status allows it to be suspended. If
282 * another suspend has been started earlier, either return immediately or wait
283 * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a
284 * pending idle notification. If the RPM_ASYNC flag is set then queue a
285 * suspend request; otherwise run the ->runtime_suspend() callback directly.
286 * If a deferred resume was requested while the callback was running then carry
287 * it out; otherwise send an idle notification for the device (if the suspend
288 * failed) or for its parent (if the suspend succeeded).
290 * This function must be called under dev->power.lock with interrupts disabled.
292 static int rpm_suspend(struct device
*dev
, int rpmflags
)
293 __releases(&dev
->power
.lock
) __acquires(&dev
->power
.lock
)
295 int (*callback
)(struct device
*);
296 struct device
*parent
= NULL
;
299 dev_dbg(dev
, "%s flags 0x%x\n", __func__
, rpmflags
);
302 retval
= rpm_check_suspend_allowed(dev
);
305 ; /* Conditions are wrong. */
307 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
308 else if (dev
->power
.runtime_status
== RPM_RESUMING
&&
309 !(rpmflags
& RPM_ASYNC
))
314 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
315 if ((rpmflags
& RPM_AUTO
)
316 && dev
->power
.runtime_status
!= RPM_SUSPENDING
) {
317 unsigned long expires
= pm_runtime_autosuspend_expiration(dev
);
320 /* Pending requests need to be canceled. */
321 dev
->power
.request
= RPM_REQ_NONE
;
324 * Optimization: If the timer is already running and is
325 * set to expire at or before the autosuspend delay,
326 * avoid the overhead of resetting it. Just let it
327 * expire; pm_suspend_timer_fn() will take care of the
330 if (!(dev
->power
.timer_expires
&& time_before_eq(
331 dev
->power
.timer_expires
, expires
))) {
332 dev
->power
.timer_expires
= expires
;
333 mod_timer(&dev
->power
.suspend_timer
, expires
);
335 dev
->power
.timer_autosuspends
= 1;
340 /* Other scheduled or pending requests need to be canceled. */
341 pm_runtime_cancel_pending(dev
);
343 if (dev
->power
.runtime_status
== RPM_SUSPENDING
) {
346 if (rpmflags
& (RPM_ASYNC
| RPM_NOWAIT
)) {
347 retval
= -EINPROGRESS
;
351 /* Wait for the other suspend running in parallel with us. */
353 prepare_to_wait(&dev
->power
.wait_queue
, &wait
,
354 TASK_UNINTERRUPTIBLE
);
355 if (dev
->power
.runtime_status
!= RPM_SUSPENDING
)
358 spin_unlock_irq(&dev
->power
.lock
);
362 spin_lock_irq(&dev
->power
.lock
);
364 finish_wait(&dev
->power
.wait_queue
, &wait
);
368 dev
->power
.deferred_resume
= false;
369 if (dev
->power
.no_callbacks
)
370 goto no_callback
; /* Assume success. */
372 /* Carry out an asynchronous or a synchronous suspend. */
373 if (rpmflags
& RPM_ASYNC
) {
374 dev
->power
.request
= (rpmflags
& RPM_AUTO
) ?
375 RPM_REQ_AUTOSUSPEND
: RPM_REQ_SUSPEND
;
376 if (!dev
->power
.request_pending
) {
377 dev
->power
.request_pending
= true;
378 queue_work(pm_wq
, &dev
->power
.work
);
383 __update_runtime_status(dev
, RPM_SUSPENDING
);
385 if (dev
->type
&& dev
->type
->pm
)
386 callback
= dev
->type
->pm
->runtime_suspend
;
387 else if (dev
->class && dev
->class->pm
)
388 callback
= dev
->class->pm
->runtime_suspend
;
389 else if (dev
->bus
&& dev
->bus
->pm
)
390 callback
= dev
->bus
->pm
->runtime_suspend
;
394 retval
= rpm_callback(callback
, dev
);
396 __update_runtime_status(dev
, RPM_ACTIVE
);
397 dev
->power
.deferred_resume
= 0;
398 if (retval
== -EAGAIN
|| retval
== -EBUSY
)
399 dev
->power
.runtime_error
= 0;
401 pm_runtime_cancel_pending(dev
);
404 rpm_callback(dev
->pwr_domain
->ops
.runtime_suspend
, dev
);
406 __update_runtime_status(dev
, RPM_SUSPENDED
);
407 pm_runtime_deactivate_timer(dev
);
410 parent
= dev
->parent
;
411 atomic_add_unless(&parent
->power
.child_count
, -1, 0);
414 wake_up_all(&dev
->power
.wait_queue
);
416 if (dev
->power
.deferred_resume
) {
422 /* Maybe the parent is now able to suspend. */
423 if (parent
&& !parent
->power
.ignore_children
&& !dev
->power
.irq_safe
) {
424 spin_unlock(&dev
->power
.lock
);
426 spin_lock(&parent
->power
.lock
);
427 rpm_idle(parent
, RPM_ASYNC
);
428 spin_unlock(&parent
->power
.lock
);
430 spin_lock(&dev
->power
.lock
);
434 dev_dbg(dev
, "%s returns %d\n", __func__
, retval
);
440 * rpm_resume - Carry out run-time resume of given device.
441 * @dev: Device to resume.
442 * @rpmflags: Flag bits.
444 * Check if the device's run-time PM status allows it to be resumed. Cancel
445 * any scheduled or pending requests. If another resume has been started
446 * earlier, either return imediately or wait for it to finish, depending on the
447 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
448 * parallel with this function, either tell the other process to resume after
449 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
450 * flag is set then queue a resume request; otherwise run the
451 * ->runtime_resume() callback directly. Queue an idle notification for the
452 * device if the resume succeeded.
454 * This function must be called under dev->power.lock with interrupts disabled.
456 static int rpm_resume(struct device
*dev
, int rpmflags
)
457 __releases(&dev
->power
.lock
) __acquires(&dev
->power
.lock
)
459 int (*callback
)(struct device
*);
460 struct device
*parent
= NULL
;
463 dev_dbg(dev
, "%s flags 0x%x\n", __func__
, rpmflags
);
466 if (dev
->power
.runtime_error
)
468 else if (dev
->power
.disable_depth
> 0)
474 * Other scheduled or pending requests need to be canceled. Small
475 * optimization: If an autosuspend timer is running, leave it running
476 * rather than cancelling it now only to restart it again in the near
479 dev
->power
.request
= RPM_REQ_NONE
;
480 if (!dev
->power
.timer_autosuspends
)
481 pm_runtime_deactivate_timer(dev
);
483 if (dev
->power
.runtime_status
== RPM_ACTIVE
) {
488 if (dev
->power
.runtime_status
== RPM_RESUMING
489 || dev
->power
.runtime_status
== RPM_SUSPENDING
) {
492 if (rpmflags
& (RPM_ASYNC
| RPM_NOWAIT
)) {
493 if (dev
->power
.runtime_status
== RPM_SUSPENDING
)
494 dev
->power
.deferred_resume
= true;
496 retval
= -EINPROGRESS
;
500 /* Wait for the operation carried out in parallel with us. */
502 prepare_to_wait(&dev
->power
.wait_queue
, &wait
,
503 TASK_UNINTERRUPTIBLE
);
504 if (dev
->power
.runtime_status
!= RPM_RESUMING
505 && dev
->power
.runtime_status
!= RPM_SUSPENDING
)
508 spin_unlock_irq(&dev
->power
.lock
);
512 spin_lock_irq(&dev
->power
.lock
);
514 finish_wait(&dev
->power
.wait_queue
, &wait
);
519 * See if we can skip waking up the parent. This is safe only if
520 * power.no_callbacks is set, because otherwise we don't know whether
521 * the resume will actually succeed.
523 if (dev
->power
.no_callbacks
&& !parent
&& dev
->parent
) {
524 spin_lock_nested(&dev
->parent
->power
.lock
, SINGLE_DEPTH_NESTING
);
525 if (dev
->parent
->power
.disable_depth
> 0
526 || dev
->parent
->power
.ignore_children
527 || dev
->parent
->power
.runtime_status
== RPM_ACTIVE
) {
528 atomic_inc(&dev
->parent
->power
.child_count
);
529 spin_unlock(&dev
->parent
->power
.lock
);
530 goto no_callback
; /* Assume success. */
532 spin_unlock(&dev
->parent
->power
.lock
);
535 /* Carry out an asynchronous or a synchronous resume. */
536 if (rpmflags
& RPM_ASYNC
) {
537 dev
->power
.request
= RPM_REQ_RESUME
;
538 if (!dev
->power
.request_pending
) {
539 dev
->power
.request_pending
= true;
540 queue_work(pm_wq
, &dev
->power
.work
);
546 if (!parent
&& dev
->parent
) {
548 * Increment the parent's usage counter and resume it if
549 * necessary. Not needed if dev is irq-safe; then the
550 * parent is permanently resumed.
552 parent
= dev
->parent
;
553 if (dev
->power
.irq_safe
)
555 spin_unlock(&dev
->power
.lock
);
557 pm_runtime_get_noresume(parent
);
559 spin_lock(&parent
->power
.lock
);
561 * We can resume if the parent's run-time PM is disabled or it
562 * is set to ignore children.
564 if (!parent
->power
.disable_depth
565 && !parent
->power
.ignore_children
) {
566 rpm_resume(parent
, 0);
567 if (parent
->power
.runtime_status
!= RPM_ACTIVE
)
570 spin_unlock(&parent
->power
.lock
);
572 spin_lock(&dev
->power
.lock
);
579 if (dev
->power
.no_callbacks
)
580 goto no_callback
; /* Assume success. */
582 __update_runtime_status(dev
, RPM_RESUMING
);
585 rpm_callback(dev
->pwr_domain
->ops
.runtime_resume
, dev
);
587 if (dev
->type
&& dev
->type
->pm
)
588 callback
= dev
->type
->pm
->runtime_resume
;
589 else if (dev
->class && dev
->class->pm
)
590 callback
= dev
->class->pm
->runtime_resume
;
591 else if (dev
->bus
&& dev
->bus
->pm
)
592 callback
= dev
->bus
->pm
->runtime_resume
;
596 retval
= rpm_callback(callback
, dev
);
598 __update_runtime_status(dev
, RPM_SUSPENDED
);
599 pm_runtime_cancel_pending(dev
);
602 __update_runtime_status(dev
, RPM_ACTIVE
);
604 atomic_inc(&parent
->power
.child_count
);
606 wake_up_all(&dev
->power
.wait_queue
);
609 rpm_idle(dev
, RPM_ASYNC
);
612 if (parent
&& !dev
->power
.irq_safe
) {
613 spin_unlock_irq(&dev
->power
.lock
);
615 pm_runtime_put(parent
);
617 spin_lock_irq(&dev
->power
.lock
);
620 dev_dbg(dev
, "%s returns %d\n", __func__
, retval
);
626 * pm_runtime_work - Universal run-time PM work function.
627 * @work: Work structure used for scheduling the execution of this function.
629 * Use @work to get the device object the work is to be done for, determine what
630 * is to be done and execute the appropriate run-time PM function.
632 static void pm_runtime_work(struct work_struct
*work
)
634 struct device
*dev
= container_of(work
, struct device
, power
.work
);
635 enum rpm_request req
;
637 spin_lock_irq(&dev
->power
.lock
);
639 if (!dev
->power
.request_pending
)
642 req
= dev
->power
.request
;
643 dev
->power
.request
= RPM_REQ_NONE
;
644 dev
->power
.request_pending
= false;
650 rpm_idle(dev
, RPM_NOWAIT
);
652 case RPM_REQ_SUSPEND
:
653 rpm_suspend(dev
, RPM_NOWAIT
);
655 case RPM_REQ_AUTOSUSPEND
:
656 rpm_suspend(dev
, RPM_NOWAIT
| RPM_AUTO
);
659 rpm_resume(dev
, RPM_NOWAIT
);
664 spin_unlock_irq(&dev
->power
.lock
);
668 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
669 * @data: Device pointer passed by pm_schedule_suspend().
671 * Check if the time is right and queue a suspend request.
673 static void pm_suspend_timer_fn(unsigned long data
)
675 struct device
*dev
= (struct device
*)data
;
677 unsigned long expires
;
679 spin_lock_irqsave(&dev
->power
.lock
, flags
);
681 expires
= dev
->power
.timer_expires
;
682 /* If 'expire' is after 'jiffies' we've been called too early. */
683 if (expires
> 0 && !time_after(expires
, jiffies
)) {
684 dev
->power
.timer_expires
= 0;
685 rpm_suspend(dev
, dev
->power
.timer_autosuspends
?
686 (RPM_ASYNC
| RPM_AUTO
) : RPM_ASYNC
);
689 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
693 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
694 * @dev: Device to suspend.
695 * @delay: Time to wait before submitting a suspend request, in milliseconds.
697 int pm_schedule_suspend(struct device
*dev
, unsigned int delay
)
702 spin_lock_irqsave(&dev
->power
.lock
, flags
);
705 retval
= rpm_suspend(dev
, RPM_ASYNC
);
709 retval
= rpm_check_suspend_allowed(dev
);
713 /* Other scheduled or pending requests need to be canceled. */
714 pm_runtime_cancel_pending(dev
);
716 dev
->power
.timer_expires
= jiffies
+ msecs_to_jiffies(delay
);
717 dev
->power
.timer_expires
+= !dev
->power
.timer_expires
;
718 dev
->power
.timer_autosuspends
= 0;
719 mod_timer(&dev
->power
.suspend_timer
, dev
->power
.timer_expires
);
722 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
726 EXPORT_SYMBOL_GPL(pm_schedule_suspend
);
729 * __pm_runtime_idle - Entry point for run-time idle operations.
730 * @dev: Device to send idle notification for.
731 * @rpmflags: Flag bits.
733 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
734 * return immediately if it is larger than zero. Then carry out an idle
735 * notification, either synchronous or asynchronous.
737 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
739 int __pm_runtime_idle(struct device
*dev
, int rpmflags
)
744 if (rpmflags
& RPM_GET_PUT
) {
745 if (!atomic_dec_and_test(&dev
->power
.usage_count
))
749 spin_lock_irqsave(&dev
->power
.lock
, flags
);
750 retval
= rpm_idle(dev
, rpmflags
);
751 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
755 EXPORT_SYMBOL_GPL(__pm_runtime_idle
);
758 * __pm_runtime_suspend - Entry point for run-time put/suspend operations.
759 * @dev: Device to suspend.
760 * @rpmflags: Flag bits.
762 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
763 * return immediately if it is larger than zero. Then carry out a suspend,
764 * either synchronous or asynchronous.
766 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
768 int __pm_runtime_suspend(struct device
*dev
, int rpmflags
)
773 if (rpmflags
& RPM_GET_PUT
) {
774 if (!atomic_dec_and_test(&dev
->power
.usage_count
))
778 spin_lock_irqsave(&dev
->power
.lock
, flags
);
779 retval
= rpm_suspend(dev
, rpmflags
);
780 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
784 EXPORT_SYMBOL_GPL(__pm_runtime_suspend
);
787 * __pm_runtime_resume - Entry point for run-time resume operations.
788 * @dev: Device to resume.
789 * @rpmflags: Flag bits.
791 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
792 * carry out a resume, either synchronous or asynchronous.
794 * This routine may be called in atomic context if the RPM_ASYNC flag is set.
796 int __pm_runtime_resume(struct device
*dev
, int rpmflags
)
801 if (rpmflags
& RPM_GET_PUT
)
802 atomic_inc(&dev
->power
.usage_count
);
804 spin_lock_irqsave(&dev
->power
.lock
, flags
);
805 retval
= rpm_resume(dev
, rpmflags
);
806 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
810 EXPORT_SYMBOL_GPL(__pm_runtime_resume
);
813 * __pm_runtime_set_status - Set run-time PM status of a device.
814 * @dev: Device to handle.
815 * @status: New run-time PM status of the device.
817 * If run-time PM of the device is disabled or its power.runtime_error field is
818 * different from zero, the status may be changed either to RPM_ACTIVE, or to
819 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
820 * However, if the device has a parent and the parent is not active, and the
821 * parent's power.ignore_children flag is unset, the device's status cannot be
822 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
824 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
825 * and the device parent's counter of unsuspended children is modified to
826 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
827 * notification request for the parent is submitted.
829 int __pm_runtime_set_status(struct device
*dev
, unsigned int status
)
831 struct device
*parent
= dev
->parent
;
833 bool notify_parent
= false;
836 if (status
!= RPM_ACTIVE
&& status
!= RPM_SUSPENDED
)
839 spin_lock_irqsave(&dev
->power
.lock
, flags
);
841 if (!dev
->power
.runtime_error
&& !dev
->power
.disable_depth
) {
846 if (dev
->power
.runtime_status
== status
)
849 if (status
== RPM_SUSPENDED
) {
850 /* It always is possible to set the status to 'suspended'. */
852 atomic_add_unless(&parent
->power
.child_count
, -1, 0);
853 notify_parent
= !parent
->power
.ignore_children
;
859 spin_lock_nested(&parent
->power
.lock
, SINGLE_DEPTH_NESTING
);
862 * It is invalid to put an active child under a parent that is
863 * not active, has run-time PM enabled and the
864 * 'power.ignore_children' flag unset.
866 if (!parent
->power
.disable_depth
867 && !parent
->power
.ignore_children
868 && parent
->power
.runtime_status
!= RPM_ACTIVE
)
870 else if (dev
->power
.runtime_status
== RPM_SUSPENDED
)
871 atomic_inc(&parent
->power
.child_count
);
873 spin_unlock(&parent
->power
.lock
);
880 __update_runtime_status(dev
, status
);
881 dev
->power
.runtime_error
= 0;
883 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
886 pm_request_idle(parent
);
890 EXPORT_SYMBOL_GPL(__pm_runtime_set_status
);
893 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
894 * @dev: Device to handle.
896 * Flush all pending requests for the device from pm_wq and wait for all
897 * run-time PM operations involving the device in progress to complete.
899 * Should be called under dev->power.lock with interrupts disabled.
901 static void __pm_runtime_barrier(struct device
*dev
)
903 pm_runtime_deactivate_timer(dev
);
905 if (dev
->power
.request_pending
) {
906 dev
->power
.request
= RPM_REQ_NONE
;
907 spin_unlock_irq(&dev
->power
.lock
);
909 cancel_work_sync(&dev
->power
.work
);
911 spin_lock_irq(&dev
->power
.lock
);
912 dev
->power
.request_pending
= false;
915 if (dev
->power
.runtime_status
== RPM_SUSPENDING
916 || dev
->power
.runtime_status
== RPM_RESUMING
917 || dev
->power
.idle_notification
) {
920 /* Suspend, wake-up or idle notification in progress. */
922 prepare_to_wait(&dev
->power
.wait_queue
, &wait
,
923 TASK_UNINTERRUPTIBLE
);
924 if (dev
->power
.runtime_status
!= RPM_SUSPENDING
925 && dev
->power
.runtime_status
!= RPM_RESUMING
926 && !dev
->power
.idle_notification
)
928 spin_unlock_irq(&dev
->power
.lock
);
932 spin_lock_irq(&dev
->power
.lock
);
934 finish_wait(&dev
->power
.wait_queue
, &wait
);
939 * pm_runtime_barrier - Flush pending requests and wait for completions.
940 * @dev: Device to handle.
942 * Prevent the device from being suspended by incrementing its usage counter and
943 * if there's a pending resume request for the device, wake the device up.
944 * Next, make sure that all pending requests for the device have been flushed
945 * from pm_wq and wait for all run-time PM operations involving the device in
946 * progress to complete.
949 * 1, if there was a resume request pending and the device had to be woken up,
952 int pm_runtime_barrier(struct device
*dev
)
956 pm_runtime_get_noresume(dev
);
957 spin_lock_irq(&dev
->power
.lock
);
959 if (dev
->power
.request_pending
960 && dev
->power
.request
== RPM_REQ_RESUME
) {
965 __pm_runtime_barrier(dev
);
967 spin_unlock_irq(&dev
->power
.lock
);
968 pm_runtime_put_noidle(dev
);
972 EXPORT_SYMBOL_GPL(pm_runtime_barrier
);
975 * __pm_runtime_disable - Disable run-time PM of a device.
976 * @dev: Device to handle.
977 * @check_resume: If set, check if there's a resume request for the device.
979 * Increment power.disable_depth for the device and if was zero previously,
980 * cancel all pending run-time PM requests for the device and wait for all
981 * operations in progress to complete. The device can be either active or
982 * suspended after its run-time PM has been disabled.
984 * If @check_resume is set and there's a resume request pending when
985 * __pm_runtime_disable() is called and power.disable_depth is zero, the
986 * function will wake up the device before disabling its run-time PM.
988 void __pm_runtime_disable(struct device
*dev
, bool check_resume
)
990 spin_lock_irq(&dev
->power
.lock
);
992 if (dev
->power
.disable_depth
> 0) {
993 dev
->power
.disable_depth
++;
998 * Wake up the device if there's a resume request pending, because that
999 * means there probably is some I/O to process and disabling run-time PM
1000 * shouldn't prevent the device from processing the I/O.
1002 if (check_resume
&& dev
->power
.request_pending
1003 && dev
->power
.request
== RPM_REQ_RESUME
) {
1005 * Prevent suspends and idle notifications from being carried
1006 * out after we have woken up the device.
1008 pm_runtime_get_noresume(dev
);
1012 pm_runtime_put_noidle(dev
);
1015 if (!dev
->power
.disable_depth
++)
1016 __pm_runtime_barrier(dev
);
1019 spin_unlock_irq(&dev
->power
.lock
);
1021 EXPORT_SYMBOL_GPL(__pm_runtime_disable
);
1024 * pm_runtime_enable - Enable run-time PM of a device.
1025 * @dev: Device to handle.
1027 void pm_runtime_enable(struct device
*dev
)
1029 unsigned long flags
;
1031 spin_lock_irqsave(&dev
->power
.lock
, flags
);
1033 if (dev
->power
.disable_depth
> 0)
1034 dev
->power
.disable_depth
--;
1036 dev_warn(dev
, "Unbalanced %s!\n", __func__
);
1038 spin_unlock_irqrestore(&dev
->power
.lock
, flags
);
1040 EXPORT_SYMBOL_GPL(pm_runtime_enable
);
1043 * pm_runtime_forbid - Block run-time PM of a device.
1044 * @dev: Device to handle.
1046 * Increase the device's usage count and clear its power.runtime_auto flag,
1047 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1050 void pm_runtime_forbid(struct device
*dev
)
1052 spin_lock_irq(&dev
->power
.lock
);
1053 if (!dev
->power
.runtime_auto
)
1056 dev
->power
.runtime_auto
= false;
1057 atomic_inc(&dev
->power
.usage_count
);
1061 spin_unlock_irq(&dev
->power
.lock
);
1063 EXPORT_SYMBOL_GPL(pm_runtime_forbid
);
1066 * pm_runtime_allow - Unblock run-time PM of a device.
1067 * @dev: Device to handle.
1069 * Decrease the device's usage count and set its power.runtime_auto flag.
1071 void pm_runtime_allow(struct device
*dev
)
1073 spin_lock_irq(&dev
->power
.lock
);
1074 if (dev
->power
.runtime_auto
)
1077 dev
->power
.runtime_auto
= true;
1078 if (atomic_dec_and_test(&dev
->power
.usage_count
))
1079 rpm_idle(dev
, RPM_AUTO
);
1082 spin_unlock_irq(&dev
->power
.lock
);
1084 EXPORT_SYMBOL_GPL(pm_runtime_allow
);
1087 * pm_runtime_no_callbacks - Ignore run-time PM callbacks for a device.
1088 * @dev: Device to handle.
1090 * Set the power.no_callbacks flag, which tells the PM core that this
1091 * device is power-managed through its parent and has no run-time PM
1092 * callbacks of its own. The run-time sysfs attributes will be removed.
1094 void pm_runtime_no_callbacks(struct device
*dev
)
1096 spin_lock_irq(&dev
->power
.lock
);
1097 dev
->power
.no_callbacks
= 1;
1098 spin_unlock_irq(&dev
->power
.lock
);
1099 if (device_is_registered(dev
))
1100 rpm_sysfs_remove(dev
);
1102 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks
);
1105 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1106 * @dev: Device to handle
1108 * Set the power.irq_safe flag, which tells the PM core that the
1109 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1110 * always be invoked with the spinlock held and interrupts disabled. It also
1111 * causes the parent's usage counter to be permanently incremented, preventing
1112 * the parent from runtime suspending -- otherwise an irq-safe child might have
1113 * to wait for a non-irq-safe parent.
1115 void pm_runtime_irq_safe(struct device
*dev
)
1118 pm_runtime_get_sync(dev
->parent
);
1119 spin_lock_irq(&dev
->power
.lock
);
1120 dev
->power
.irq_safe
= 1;
1121 spin_unlock_irq(&dev
->power
.lock
);
1123 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe
);
1126 * update_autosuspend - Handle a change to a device's autosuspend settings.
1127 * @dev: Device to handle.
1128 * @old_delay: The former autosuspend_delay value.
1129 * @old_use: The former use_autosuspend value.
1131 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1132 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1134 * This function must be called under dev->power.lock with interrupts disabled.
1136 static void update_autosuspend(struct device
*dev
, int old_delay
, int old_use
)
1138 int delay
= dev
->power
.autosuspend_delay
;
1140 /* Should runtime suspend be prevented now? */
1141 if (dev
->power
.use_autosuspend
&& delay
< 0) {
1143 /* If it used to be allowed then prevent it. */
1144 if (!old_use
|| old_delay
>= 0) {
1145 atomic_inc(&dev
->power
.usage_count
);
1150 /* Runtime suspend should be allowed now. */
1153 /* If it used to be prevented then allow it. */
1154 if (old_use
&& old_delay
< 0)
1155 atomic_dec(&dev
->power
.usage_count
);
1157 /* Maybe we can autosuspend now. */
1158 rpm_idle(dev
, RPM_AUTO
);
1163 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1164 * @dev: Device to handle.
1165 * @delay: Value of the new delay in milliseconds.
1167 * Set the device's power.autosuspend_delay value. If it changes to negative
1168 * and the power.use_autosuspend flag is set, prevent run-time suspends. If it
1169 * changes the other way, allow run-time suspends.
1171 void pm_runtime_set_autosuspend_delay(struct device
*dev
, int delay
)
1173 int old_delay
, old_use
;
1175 spin_lock_irq(&dev
->power
.lock
);
1176 old_delay
= dev
->power
.autosuspend_delay
;
1177 old_use
= dev
->power
.use_autosuspend
;
1178 dev
->power
.autosuspend_delay
= delay
;
1179 update_autosuspend(dev
, old_delay
, old_use
);
1180 spin_unlock_irq(&dev
->power
.lock
);
1182 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay
);
1185 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1186 * @dev: Device to handle.
1187 * @use: New value for use_autosuspend.
1189 * Set the device's power.use_autosuspend flag, and allow or prevent run-time
1190 * suspends as needed.
1192 void __pm_runtime_use_autosuspend(struct device
*dev
, bool use
)
1194 int old_delay
, old_use
;
1196 spin_lock_irq(&dev
->power
.lock
);
1197 old_delay
= dev
->power
.autosuspend_delay
;
1198 old_use
= dev
->power
.use_autosuspend
;
1199 dev
->power
.use_autosuspend
= use
;
1200 update_autosuspend(dev
, old_delay
, old_use
);
1201 spin_unlock_irq(&dev
->power
.lock
);
1203 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend
);
1206 * pm_runtime_init - Initialize run-time PM fields in given device object.
1207 * @dev: Device object to initialize.
1209 void pm_runtime_init(struct device
*dev
)
1211 dev
->power
.runtime_status
= RPM_SUSPENDED
;
1212 dev
->power
.idle_notification
= false;
1214 dev
->power
.disable_depth
= 1;
1215 atomic_set(&dev
->power
.usage_count
, 0);
1217 dev
->power
.runtime_error
= 0;
1219 atomic_set(&dev
->power
.child_count
, 0);
1220 pm_suspend_ignore_children(dev
, false);
1221 dev
->power
.runtime_auto
= true;
1223 dev
->power
.request_pending
= false;
1224 dev
->power
.request
= RPM_REQ_NONE
;
1225 dev
->power
.deferred_resume
= false;
1226 dev
->power
.accounting_timestamp
= jiffies
;
1227 INIT_WORK(&dev
->power
.work
, pm_runtime_work
);
1229 dev
->power
.timer_expires
= 0;
1230 setup_timer(&dev
->power
.suspend_timer
, pm_suspend_timer_fn
,
1231 (unsigned long)dev
);
1233 init_waitqueue_head(&dev
->power
.wait_queue
);
1237 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1238 * @dev: Device object being removed from device hierarchy.
1240 void pm_runtime_remove(struct device
*dev
)
1242 __pm_runtime_disable(dev
, false);
1244 /* Change the status back to 'suspended' to match the initial status. */
1245 if (dev
->power
.runtime_status
== RPM_ACTIVE
)
1246 pm_runtime_set_suspended(dev
);
1247 if (dev
->power
.irq_safe
&& dev
->parent
)
1248 pm_runtime_put_sync(dev
->parent
);