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/export.h>
12 #include <linux/pm_runtime.h>
13 #include <trace/events/rpm.h>
16 typedef int (*pm_callback_t)(struct device *);
18 static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset)
21 const struct dev_pm_ops *ops;
24 ops = &dev->pm_domain->ops;
25 else if (dev->type && dev->type->pm)
27 else if (dev->class && dev->class->pm)
29 else if (dev->bus && dev->bus->pm)
35 cb = *(pm_callback_t *)((void *)ops + cb_offset);
39 if (!cb && dev->driver && dev->driver->pm)
40 cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset);
45 #define RPM_GET_CALLBACK(dev, callback) \
46 __rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback))
48 static int rpm_resume(struct device *dev, int rpmflags);
49 static int rpm_suspend(struct device *dev, int rpmflags);
52 * update_pm_runtime_accounting - Update the time accounting of power states
53 * @dev: Device to update the accounting for
55 * In order to be able to have time accounting of the various power states
56 * (as used by programs such as PowerTOP to show the effectiveness of runtime
57 * PM), we need to track the time spent in each state.
58 * update_pm_runtime_accounting must be called each time before the
59 * runtime_status field is updated, to account the time in the old state
62 void update_pm_runtime_accounting(struct device *dev)
64 unsigned long now = jiffies;
67 delta = now - dev->power.accounting_timestamp;
69 dev->power.accounting_timestamp = now;
71 if (dev->power.disable_depth > 0)
74 if (dev->power.runtime_status == RPM_SUSPENDED)
75 dev->power.suspended_jiffies += delta;
77 dev->power.active_jiffies += delta;
80 static void __update_runtime_status(struct device *dev, enum rpm_status status)
82 update_pm_runtime_accounting(dev);
83 dev->power.runtime_status = status;
87 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
88 * @dev: Device to handle.
90 static void pm_runtime_deactivate_timer(struct device *dev)
92 if (dev->power.timer_expires > 0) {
93 del_timer(&dev->power.suspend_timer);
94 dev->power.timer_expires = 0;
99 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
100 * @dev: Device to handle.
102 static void pm_runtime_cancel_pending(struct device *dev)
104 pm_runtime_deactivate_timer(dev);
106 * In case there's a request pending, make sure its work function will
107 * return without doing anything.
109 dev->power.request = RPM_REQ_NONE;
113 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
114 * @dev: Device to handle.
116 * Compute the autosuspend-delay expiration time based on the device's
117 * power.last_busy time. If the delay has already expired or is disabled
118 * (negative) or the power.use_autosuspend flag isn't set, return 0.
119 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
121 * This function may be called either with or without dev->power.lock held.
122 * Either way it can be racy, since power.last_busy may be updated at any time.
124 unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
126 int autosuspend_delay;
128 unsigned long last_busy;
129 unsigned long expires = 0;
131 if (!dev->power.use_autosuspend)
134 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
135 if (autosuspend_delay < 0)
138 last_busy = ACCESS_ONCE(dev->power.last_busy);
139 elapsed = jiffies - last_busy;
141 goto out; /* jiffies has wrapped around. */
144 * If the autosuspend_delay is >= 1 second, align the timer by rounding
145 * up to the nearest second.
147 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
148 if (autosuspend_delay >= 1000)
149 expires = round_jiffies(expires);
151 if (elapsed >= expires - last_busy)
152 expires = 0; /* Already expired. */
157 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
159 static int dev_memalloc_noio(struct device *dev, void *data)
161 return dev->power.memalloc_noio;
165 * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
166 * @dev: Device to handle.
167 * @enable: True for setting the flag and False for clearing the flag.
169 * Set the flag for all devices in the path from the device to the
170 * root device in the device tree if @enable is true, otherwise clear
171 * the flag for devices in the path whose siblings don't set the flag.
173 * The function should only be called by block device, or network
174 * device driver for solving the deadlock problem during runtime
177 * If memory allocation with GFP_KERNEL is called inside runtime
178 * resume/suspend callback of any one of its ancestors(or the
179 * block device itself), the deadlock may be triggered inside the
180 * memory allocation since it might not complete until the block
181 * device becomes active and the involed page I/O finishes. The
182 * situation is pointed out first by Alan Stern. Network device
183 * are involved in iSCSI kind of situation.
185 * The lock of dev_hotplug_mutex is held in the function for handling
186 * hotplug race because pm_runtime_set_memalloc_noio() may be called
189 * The function should be called between device_add() and device_del()
190 * on the affected device(block/network device).
192 void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
194 static DEFINE_MUTEX(dev_hotplug_mutex);
196 mutex_lock(&dev_hotplug_mutex);
200 /* hold power lock since bitfield is not SMP-safe. */
201 spin_lock_irq(&dev->power.lock);
202 enabled = dev->power.memalloc_noio;
203 dev->power.memalloc_noio = enable;
204 spin_unlock_irq(&dev->power.lock);
207 * not need to enable ancestors any more if the device
210 if (enabled && enable)
216 * clear flag of the parent device only if all the
217 * children don't set the flag because ancestor's
218 * flag was set by any one of the descendants.
220 if (!dev || (!enable &&
221 device_for_each_child(dev, NULL,
225 mutex_unlock(&dev_hotplug_mutex);
227 EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
230 * rpm_check_suspend_allowed - Test whether a device may be suspended.
231 * @dev: Device to test.
233 static int rpm_check_suspend_allowed(struct device *dev)
237 if (dev->power.runtime_error)
239 else if (dev->power.disable_depth > 0)
241 else if (atomic_read(&dev->power.usage_count) > 0)
243 else if (!pm_children_suspended(dev))
246 /* Pending resume requests take precedence over suspends. */
247 else if ((dev->power.deferred_resume
248 && dev->power.runtime_status == RPM_SUSPENDING)
249 || (dev->power.request_pending
250 && dev->power.request == RPM_REQ_RESUME))
252 else if (__dev_pm_qos_read_value(dev) < 0)
254 else if (dev->power.runtime_status == RPM_SUSPENDED)
261 * __rpm_callback - Run a given runtime PM callback for a given device.
262 * @cb: Runtime PM callback to run.
263 * @dev: Device to run the callback for.
265 static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
266 __releases(&dev->power.lock) __acquires(&dev->power.lock)
270 if (dev->power.irq_safe)
271 spin_unlock(&dev->power.lock);
273 spin_unlock_irq(&dev->power.lock);
277 if (dev->power.irq_safe)
278 spin_lock(&dev->power.lock);
280 spin_lock_irq(&dev->power.lock);
286 * rpm_idle - Notify device bus type if the device can be suspended.
287 * @dev: Device to notify the bus type about.
288 * @rpmflags: Flag bits.
290 * Check if the device's runtime PM status allows it to be suspended. If
291 * another idle notification has been started earlier, return immediately. If
292 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
293 * run the ->runtime_idle() callback directly. If the ->runtime_idle callback
294 * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag.
296 * This function must be called under dev->power.lock with interrupts disabled.
298 static int rpm_idle(struct device *dev, int rpmflags)
300 int (*callback)(struct device *);
303 trace_rpm_idle(dev, rpmflags);
304 retval = rpm_check_suspend_allowed(dev);
306 ; /* Conditions are wrong. */
308 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
309 else if (dev->power.runtime_status != RPM_ACTIVE)
313 * Any pending request other than an idle notification takes
314 * precedence over us, except that the timer may be running.
316 else if (dev->power.request_pending &&
317 dev->power.request > RPM_REQ_IDLE)
320 /* Act as though RPM_NOWAIT is always set. */
321 else if (dev->power.idle_notification)
322 retval = -EINPROGRESS;
326 /* Pending requests need to be canceled. */
327 dev->power.request = RPM_REQ_NONE;
329 if (dev->power.no_callbacks)
332 /* Carry out an asynchronous or a synchronous idle notification. */
333 if (rpmflags & RPM_ASYNC) {
334 dev->power.request = RPM_REQ_IDLE;
335 if (!dev->power.request_pending) {
336 dev->power.request_pending = true;
337 queue_work(pm_wq, &dev->power.work);
339 trace_rpm_return_int(dev, _THIS_IP_, 0);
343 dev->power.idle_notification = true;
345 callback = RPM_GET_CALLBACK(dev, runtime_idle);
348 retval = __rpm_callback(callback, dev);
350 dev->power.idle_notification = false;
351 wake_up_all(&dev->power.wait_queue);
354 trace_rpm_return_int(dev, _THIS_IP_, retval);
355 return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
359 * rpm_callback - Run a given runtime PM callback for a given device.
360 * @cb: Runtime PM callback to run.
361 * @dev: Device to run the callback for.
363 static int rpm_callback(int (*cb)(struct device *), struct device *dev)
370 if (dev->power.memalloc_noio) {
371 unsigned int noio_flag;
374 * Deadlock might be caused if memory allocation with
375 * GFP_KERNEL happens inside runtime_suspend and
376 * runtime_resume callbacks of one block device's
377 * ancestor or the block device itself. Network
378 * device might be thought as part of iSCSI block
379 * device, so network device and its ancestor should
380 * be marked as memalloc_noio too.
382 noio_flag = memalloc_noio_save();
383 retval = __rpm_callback(cb, dev);
384 memalloc_noio_restore(noio_flag);
386 retval = __rpm_callback(cb, dev);
389 dev->power.runtime_error = retval;
390 return retval != -EACCES ? retval : -EIO;
394 * rpm_suspend - Carry out runtime suspend of given device.
395 * @dev: Device to suspend.
396 * @rpmflags: Flag bits.
398 * Check if the device's runtime PM status allows it to be suspended.
399 * Cancel a pending idle notification, autosuspend or suspend. If
400 * another suspend has been started earlier, either return immediately
401 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
402 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
403 * otherwise run the ->runtime_suspend() callback directly. When
404 * ->runtime_suspend succeeded, if a deferred resume was requested while
405 * the callback was running then carry it out, otherwise send an idle
406 * notification for its parent (if the suspend succeeded and both
407 * ignore_children of parent->power and irq_safe of dev->power are not set).
408 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
409 * flag is set and the next autosuspend-delay expiration time is in the
410 * future, schedule another autosuspend attempt.
412 * This function must be called under dev->power.lock with interrupts disabled.
414 static int rpm_suspend(struct device *dev, int rpmflags)
415 __releases(&dev->power.lock) __acquires(&dev->power.lock)
417 int (*callback)(struct device *);
418 struct device *parent = NULL;
421 trace_rpm_suspend(dev, rpmflags);
424 retval = rpm_check_suspend_allowed(dev);
427 ; /* Conditions are wrong. */
429 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
430 else if (dev->power.runtime_status == RPM_RESUMING &&
431 !(rpmflags & RPM_ASYNC))
436 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
437 if ((rpmflags & RPM_AUTO)
438 && dev->power.runtime_status != RPM_SUSPENDING) {
439 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
442 /* Pending requests need to be canceled. */
443 dev->power.request = RPM_REQ_NONE;
446 * Optimization: If the timer is already running and is
447 * set to expire at or before the autosuspend delay,
448 * avoid the overhead of resetting it. Just let it
449 * expire; pm_suspend_timer_fn() will take care of the
452 if (!(dev->power.timer_expires && time_before_eq(
453 dev->power.timer_expires, expires))) {
454 dev->power.timer_expires = expires;
455 mod_timer(&dev->power.suspend_timer, expires);
457 dev->power.timer_autosuspends = 1;
462 /* Other scheduled or pending requests need to be canceled. */
463 pm_runtime_cancel_pending(dev);
465 if (dev->power.runtime_status == RPM_SUSPENDING) {
468 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
469 retval = -EINPROGRESS;
473 if (dev->power.irq_safe) {
474 spin_unlock(&dev->power.lock);
478 spin_lock(&dev->power.lock);
482 /* Wait for the other suspend running in parallel with us. */
484 prepare_to_wait(&dev->power.wait_queue, &wait,
485 TASK_UNINTERRUPTIBLE);
486 if (dev->power.runtime_status != RPM_SUSPENDING)
489 spin_unlock_irq(&dev->power.lock);
493 spin_lock_irq(&dev->power.lock);
495 finish_wait(&dev->power.wait_queue, &wait);
499 if (dev->power.no_callbacks)
500 goto no_callback; /* Assume success. */
502 /* Carry out an asynchronous or a synchronous suspend. */
503 if (rpmflags & RPM_ASYNC) {
504 dev->power.request = (rpmflags & RPM_AUTO) ?
505 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
506 if (!dev->power.request_pending) {
507 dev->power.request_pending = true;
508 queue_work(pm_wq, &dev->power.work);
513 __update_runtime_status(dev, RPM_SUSPENDING);
515 callback = RPM_GET_CALLBACK(dev, runtime_suspend);
517 retval = rpm_callback(callback, dev);
522 __update_runtime_status(dev, RPM_SUSPENDED);
523 pm_runtime_deactivate_timer(dev);
526 parent = dev->parent;
527 atomic_add_unless(&parent->power.child_count, -1, 0);
529 wake_up_all(&dev->power.wait_queue);
531 if (dev->power.deferred_resume) {
532 dev->power.deferred_resume = false;
538 /* Maybe the parent is now able to suspend. */
539 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
540 spin_unlock(&dev->power.lock);
542 spin_lock(&parent->power.lock);
543 rpm_idle(parent, RPM_ASYNC);
544 spin_unlock(&parent->power.lock);
546 spin_lock(&dev->power.lock);
550 trace_rpm_return_int(dev, _THIS_IP_, retval);
555 __update_runtime_status(dev, RPM_ACTIVE);
556 dev->power.deferred_resume = false;
557 wake_up_all(&dev->power.wait_queue);
559 if (retval == -EAGAIN || retval == -EBUSY) {
560 dev->power.runtime_error = 0;
563 * If the callback routine failed an autosuspend, and
564 * if the last_busy time has been updated so that there
565 * is a new autosuspend expiration time, automatically
566 * reschedule another autosuspend.
568 if ((rpmflags & RPM_AUTO) &&
569 pm_runtime_autosuspend_expiration(dev) != 0)
572 pm_runtime_cancel_pending(dev);
578 * rpm_resume - Carry out runtime resume of given device.
579 * @dev: Device to resume.
580 * @rpmflags: Flag bits.
582 * Check if the device's runtime PM status allows it to be resumed. Cancel
583 * any scheduled or pending requests. If another resume has been started
584 * earlier, either return immediately or wait for it to finish, depending on the
585 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
586 * parallel with this function, either tell the other process to resume after
587 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
588 * flag is set then queue a resume request; otherwise run the
589 * ->runtime_resume() callback directly. Queue an idle notification for the
590 * device if the resume succeeded.
592 * This function must be called under dev->power.lock with interrupts disabled.
594 static int rpm_resume(struct device *dev, int rpmflags)
595 __releases(&dev->power.lock) __acquires(&dev->power.lock)
597 int (*callback)(struct device *);
598 struct device *parent = NULL;
601 trace_rpm_resume(dev, rpmflags);
604 if (dev->power.runtime_error)
606 else if (dev->power.disable_depth == 1 && dev->power.is_suspended
607 && dev->power.runtime_status == RPM_ACTIVE)
609 else if (dev->power.disable_depth > 0)
615 * Other scheduled or pending requests need to be canceled. Small
616 * optimization: If an autosuspend timer is running, leave it running
617 * rather than cancelling it now only to restart it again in the near
620 dev->power.request = RPM_REQ_NONE;
621 if (!dev->power.timer_autosuspends)
622 pm_runtime_deactivate_timer(dev);
624 if (dev->power.runtime_status == RPM_ACTIVE) {
629 if (dev->power.runtime_status == RPM_RESUMING
630 || dev->power.runtime_status == RPM_SUSPENDING) {
633 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
634 if (dev->power.runtime_status == RPM_SUSPENDING)
635 dev->power.deferred_resume = true;
637 retval = -EINPROGRESS;
641 if (dev->power.irq_safe) {
642 spin_unlock(&dev->power.lock);
646 spin_lock(&dev->power.lock);
650 /* Wait for the operation carried out in parallel with us. */
652 prepare_to_wait(&dev->power.wait_queue, &wait,
653 TASK_UNINTERRUPTIBLE);
654 if (dev->power.runtime_status != RPM_RESUMING
655 && dev->power.runtime_status != RPM_SUSPENDING)
658 spin_unlock_irq(&dev->power.lock);
662 spin_lock_irq(&dev->power.lock);
664 finish_wait(&dev->power.wait_queue, &wait);
669 * See if we can skip waking up the parent. This is safe only if
670 * power.no_callbacks is set, because otherwise we don't know whether
671 * the resume will actually succeed.
673 if (dev->power.no_callbacks && !parent && dev->parent) {
674 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
675 if (dev->parent->power.disable_depth > 0
676 || dev->parent->power.ignore_children
677 || dev->parent->power.runtime_status == RPM_ACTIVE) {
678 atomic_inc(&dev->parent->power.child_count);
679 spin_unlock(&dev->parent->power.lock);
681 goto no_callback; /* Assume success. */
683 spin_unlock(&dev->parent->power.lock);
686 /* Carry out an asynchronous or a synchronous resume. */
687 if (rpmflags & RPM_ASYNC) {
688 dev->power.request = RPM_REQ_RESUME;
689 if (!dev->power.request_pending) {
690 dev->power.request_pending = true;
691 queue_work(pm_wq, &dev->power.work);
697 if (!parent && dev->parent) {
699 * Increment the parent's usage counter and resume it if
700 * necessary. Not needed if dev is irq-safe; then the
701 * parent is permanently resumed.
703 parent = dev->parent;
704 if (dev->power.irq_safe)
706 spin_unlock(&dev->power.lock);
708 pm_runtime_get_noresume(parent);
710 spin_lock(&parent->power.lock);
712 * We can resume if the parent's runtime PM is disabled or it
713 * is set to ignore children.
715 if (!parent->power.disable_depth
716 && !parent->power.ignore_children) {
717 rpm_resume(parent, 0);
718 if (parent->power.runtime_status != RPM_ACTIVE)
721 spin_unlock(&parent->power.lock);
723 spin_lock(&dev->power.lock);
730 if (dev->power.no_callbacks)
731 goto no_callback; /* Assume success. */
733 __update_runtime_status(dev, RPM_RESUMING);
735 callback = RPM_GET_CALLBACK(dev, runtime_resume);
737 retval = rpm_callback(callback, dev);
739 __update_runtime_status(dev, RPM_SUSPENDED);
740 pm_runtime_cancel_pending(dev);
743 __update_runtime_status(dev, RPM_ACTIVE);
744 pm_runtime_mark_last_busy(dev);
746 atomic_inc(&parent->power.child_count);
748 wake_up_all(&dev->power.wait_queue);
751 rpm_idle(dev, RPM_ASYNC);
754 if (parent && !dev->power.irq_safe) {
755 spin_unlock_irq(&dev->power.lock);
757 pm_runtime_put(parent);
759 spin_lock_irq(&dev->power.lock);
762 trace_rpm_return_int(dev, _THIS_IP_, retval);
768 * pm_runtime_work - Universal runtime PM work function.
769 * @work: Work structure used for scheduling the execution of this function.
771 * Use @work to get the device object the work is to be done for, determine what
772 * is to be done and execute the appropriate runtime PM function.
774 static void pm_runtime_work(struct work_struct *work)
776 struct device *dev = container_of(work, struct device, power.work);
777 enum rpm_request req;
779 spin_lock_irq(&dev->power.lock);
781 if (!dev->power.request_pending)
784 req = dev->power.request;
785 dev->power.request = RPM_REQ_NONE;
786 dev->power.request_pending = false;
792 rpm_idle(dev, RPM_NOWAIT);
794 case RPM_REQ_SUSPEND:
795 rpm_suspend(dev, RPM_NOWAIT);
797 case RPM_REQ_AUTOSUSPEND:
798 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
801 rpm_resume(dev, RPM_NOWAIT);
806 spin_unlock_irq(&dev->power.lock);
810 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
811 * @data: Device pointer passed by pm_schedule_suspend().
813 * Check if the time is right and queue a suspend request.
815 static void pm_suspend_timer_fn(unsigned long data)
817 struct device *dev = (struct device *)data;
819 unsigned long expires;
821 spin_lock_irqsave(&dev->power.lock, flags);
823 expires = dev->power.timer_expires;
824 /* If 'expire' is after 'jiffies' we've been called too early. */
825 if (expires > 0 && !time_after(expires, jiffies)) {
826 dev->power.timer_expires = 0;
827 rpm_suspend(dev, dev->power.timer_autosuspends ?
828 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
831 spin_unlock_irqrestore(&dev->power.lock, flags);
835 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
836 * @dev: Device to suspend.
837 * @delay: Time to wait before submitting a suspend request, in milliseconds.
839 int pm_schedule_suspend(struct device *dev, unsigned int delay)
844 spin_lock_irqsave(&dev->power.lock, flags);
847 retval = rpm_suspend(dev, RPM_ASYNC);
851 retval = rpm_check_suspend_allowed(dev);
855 /* Other scheduled or pending requests need to be canceled. */
856 pm_runtime_cancel_pending(dev);
858 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
859 dev->power.timer_expires += !dev->power.timer_expires;
860 dev->power.timer_autosuspends = 0;
861 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
864 spin_unlock_irqrestore(&dev->power.lock, flags);
868 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
871 * __pm_runtime_idle - Entry point for runtime idle operations.
872 * @dev: Device to send idle notification for.
873 * @rpmflags: Flag bits.
875 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
876 * return immediately if it is larger than zero. Then carry out an idle
877 * notification, either synchronous or asynchronous.
879 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
880 * or if pm_runtime_irq_safe() has been called.
882 int __pm_runtime_idle(struct device *dev, int rpmflags)
887 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
889 if (rpmflags & RPM_GET_PUT) {
890 if (!atomic_dec_and_test(&dev->power.usage_count))
894 spin_lock_irqsave(&dev->power.lock, flags);
895 retval = rpm_idle(dev, rpmflags);
896 spin_unlock_irqrestore(&dev->power.lock, flags);
900 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
903 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
904 * @dev: Device to suspend.
905 * @rpmflags: Flag bits.
907 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
908 * return immediately if it is larger than zero. Then carry out a suspend,
909 * either synchronous or asynchronous.
911 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
912 * or if pm_runtime_irq_safe() has been called.
914 int __pm_runtime_suspend(struct device *dev, int rpmflags)
919 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
921 if (rpmflags & RPM_GET_PUT) {
922 if (!atomic_dec_and_test(&dev->power.usage_count))
926 spin_lock_irqsave(&dev->power.lock, flags);
927 retval = rpm_suspend(dev, rpmflags);
928 spin_unlock_irqrestore(&dev->power.lock, flags);
932 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
935 * __pm_runtime_resume - Entry point for runtime resume operations.
936 * @dev: Device to resume.
937 * @rpmflags: Flag bits.
939 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
940 * carry out a resume, either synchronous or asynchronous.
942 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
943 * or if pm_runtime_irq_safe() has been called.
945 int __pm_runtime_resume(struct device *dev, int rpmflags)
950 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
952 if (rpmflags & RPM_GET_PUT)
953 atomic_inc(&dev->power.usage_count);
955 spin_lock_irqsave(&dev->power.lock, flags);
956 retval = rpm_resume(dev, rpmflags);
957 spin_unlock_irqrestore(&dev->power.lock, flags);
961 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
964 * __pm_runtime_set_status - Set runtime PM status of a device.
965 * @dev: Device to handle.
966 * @status: New runtime PM status of the device.
968 * If runtime PM of the device is disabled or its power.runtime_error field is
969 * different from zero, the status may be changed either to RPM_ACTIVE, or to
970 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
971 * However, if the device has a parent and the parent is not active, and the
972 * parent's power.ignore_children flag is unset, the device's status cannot be
973 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
975 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
976 * and the device parent's counter of unsuspended children is modified to
977 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
978 * notification request for the parent is submitted.
980 int __pm_runtime_set_status(struct device *dev, unsigned int status)
982 struct device *parent = dev->parent;
984 bool notify_parent = false;
987 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
990 spin_lock_irqsave(&dev->power.lock, flags);
992 if (!dev->power.runtime_error && !dev->power.disable_depth) {
997 if (dev->power.runtime_status == status)
1000 if (status == RPM_SUSPENDED) {
1001 /* It always is possible to set the status to 'suspended'. */
1003 atomic_add_unless(&parent->power.child_count, -1, 0);
1004 notify_parent = !parent->power.ignore_children;
1010 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
1013 * It is invalid to put an active child under a parent that is
1014 * not active, has runtime PM enabled and the
1015 * 'power.ignore_children' flag unset.
1017 if (!parent->power.disable_depth
1018 && !parent->power.ignore_children
1019 && parent->power.runtime_status != RPM_ACTIVE)
1021 else if (dev->power.runtime_status == RPM_SUSPENDED)
1022 atomic_inc(&parent->power.child_count);
1024 spin_unlock(&parent->power.lock);
1031 __update_runtime_status(dev, status);
1032 dev->power.runtime_error = 0;
1034 spin_unlock_irqrestore(&dev->power.lock, flags);
1037 pm_request_idle(parent);
1041 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1044 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1045 * @dev: Device to handle.
1047 * Flush all pending requests for the device from pm_wq and wait for all
1048 * runtime PM operations involving the device in progress to complete.
1050 * Should be called under dev->power.lock with interrupts disabled.
1052 static void __pm_runtime_barrier(struct device *dev)
1054 pm_runtime_deactivate_timer(dev);
1056 if (dev->power.request_pending) {
1057 dev->power.request = RPM_REQ_NONE;
1058 spin_unlock_irq(&dev->power.lock);
1060 cancel_work_sync(&dev->power.work);
1062 spin_lock_irq(&dev->power.lock);
1063 dev->power.request_pending = false;
1066 if (dev->power.runtime_status == RPM_SUSPENDING
1067 || dev->power.runtime_status == RPM_RESUMING
1068 || dev->power.idle_notification) {
1071 /* Suspend, wake-up or idle notification in progress. */
1073 prepare_to_wait(&dev->power.wait_queue, &wait,
1074 TASK_UNINTERRUPTIBLE);
1075 if (dev->power.runtime_status != RPM_SUSPENDING
1076 && dev->power.runtime_status != RPM_RESUMING
1077 && !dev->power.idle_notification)
1079 spin_unlock_irq(&dev->power.lock);
1083 spin_lock_irq(&dev->power.lock);
1085 finish_wait(&dev->power.wait_queue, &wait);
1090 * pm_runtime_barrier - Flush pending requests and wait for completions.
1091 * @dev: Device to handle.
1093 * Prevent the device from being suspended by incrementing its usage counter and
1094 * if there's a pending resume request for the device, wake the device up.
1095 * Next, make sure that all pending requests for the device have been flushed
1096 * from pm_wq and wait for all runtime PM operations involving the device in
1097 * progress to complete.
1100 * 1, if there was a resume request pending and the device had to be woken up,
1103 int pm_runtime_barrier(struct device *dev)
1107 pm_runtime_get_noresume(dev);
1108 spin_lock_irq(&dev->power.lock);
1110 if (dev->power.request_pending
1111 && dev->power.request == RPM_REQ_RESUME) {
1116 __pm_runtime_barrier(dev);
1118 spin_unlock_irq(&dev->power.lock);
1119 pm_runtime_put_noidle(dev);
1123 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1126 * __pm_runtime_disable - Disable runtime PM of a device.
1127 * @dev: Device to handle.
1128 * @check_resume: If set, check if there's a resume request for the device.
1130 * Increment power.disable_depth for the device and if it was zero previously,
1131 * cancel all pending runtime PM requests for the device and wait for all
1132 * operations in progress to complete. The device can be either active or
1133 * suspended after its runtime PM has been disabled.
1135 * If @check_resume is set and there's a resume request pending when
1136 * __pm_runtime_disable() is called and power.disable_depth is zero, the
1137 * function will wake up the device before disabling its runtime PM.
1139 void __pm_runtime_disable(struct device *dev, bool check_resume)
1141 spin_lock_irq(&dev->power.lock);
1143 if (dev->power.disable_depth > 0) {
1144 dev->power.disable_depth++;
1149 * Wake up the device if there's a resume request pending, because that
1150 * means there probably is some I/O to process and disabling runtime PM
1151 * shouldn't prevent the device from processing the I/O.
1153 if (check_resume && dev->power.request_pending
1154 && dev->power.request == RPM_REQ_RESUME) {
1156 * Prevent suspends and idle notifications from being carried
1157 * out after we have woken up the device.
1159 pm_runtime_get_noresume(dev);
1163 pm_runtime_put_noidle(dev);
1166 if (!dev->power.disable_depth++)
1167 __pm_runtime_barrier(dev);
1170 spin_unlock_irq(&dev->power.lock);
1172 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1175 * pm_runtime_enable - Enable runtime PM of a device.
1176 * @dev: Device to handle.
1178 void pm_runtime_enable(struct device *dev)
1180 unsigned long flags;
1182 spin_lock_irqsave(&dev->power.lock, flags);
1184 if (dev->power.disable_depth > 0)
1185 dev->power.disable_depth--;
1187 dev_warn(dev, "Unbalanced %s!\n", __func__);
1189 spin_unlock_irqrestore(&dev->power.lock, flags);
1191 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1194 * pm_runtime_forbid - Block runtime PM of a device.
1195 * @dev: Device to handle.
1197 * Increase the device's usage count and clear its power.runtime_auto flag,
1198 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1201 void pm_runtime_forbid(struct device *dev)
1203 spin_lock_irq(&dev->power.lock);
1204 if (!dev->power.runtime_auto)
1207 dev->power.runtime_auto = false;
1208 atomic_inc(&dev->power.usage_count);
1212 spin_unlock_irq(&dev->power.lock);
1214 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1217 * pm_runtime_allow - Unblock runtime PM of a device.
1218 * @dev: Device to handle.
1220 * Decrease the device's usage count and set its power.runtime_auto flag.
1222 void pm_runtime_allow(struct device *dev)
1224 spin_lock_irq(&dev->power.lock);
1225 if (dev->power.runtime_auto)
1228 dev->power.runtime_auto = true;
1229 if (atomic_dec_and_test(&dev->power.usage_count))
1230 rpm_idle(dev, RPM_AUTO);
1233 spin_unlock_irq(&dev->power.lock);
1235 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1238 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1239 * @dev: Device to handle.
1241 * Set the power.no_callbacks flag, which tells the PM core that this
1242 * device is power-managed through its parent and has no runtime PM
1243 * callbacks of its own. The runtime sysfs attributes will be removed.
1245 void pm_runtime_no_callbacks(struct device *dev)
1247 spin_lock_irq(&dev->power.lock);
1248 dev->power.no_callbacks = 1;
1249 spin_unlock_irq(&dev->power.lock);
1250 if (device_is_registered(dev))
1251 rpm_sysfs_remove(dev);
1253 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1256 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1257 * @dev: Device to handle
1259 * Set the power.irq_safe flag, which tells the PM core that the
1260 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1261 * always be invoked with the spinlock held and interrupts disabled. It also
1262 * causes the parent's usage counter to be permanently incremented, preventing
1263 * the parent from runtime suspending -- otherwise an irq-safe child might have
1264 * to wait for a non-irq-safe parent.
1266 void pm_runtime_irq_safe(struct device *dev)
1269 pm_runtime_get_sync(dev->parent);
1270 spin_lock_irq(&dev->power.lock);
1271 dev->power.irq_safe = 1;
1272 spin_unlock_irq(&dev->power.lock);
1274 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1277 * update_autosuspend - Handle a change to a device's autosuspend settings.
1278 * @dev: Device to handle.
1279 * @old_delay: The former autosuspend_delay value.
1280 * @old_use: The former use_autosuspend value.
1282 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1283 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1285 * This function must be called under dev->power.lock with interrupts disabled.
1287 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1289 int delay = dev->power.autosuspend_delay;
1291 /* Should runtime suspend be prevented now? */
1292 if (dev->power.use_autosuspend && delay < 0) {
1294 /* If it used to be allowed then prevent it. */
1295 if (!old_use || old_delay >= 0) {
1296 atomic_inc(&dev->power.usage_count);
1301 /* Runtime suspend should be allowed now. */
1304 /* If it used to be prevented then allow it. */
1305 if (old_use && old_delay < 0)
1306 atomic_dec(&dev->power.usage_count);
1308 /* Maybe we can autosuspend now. */
1309 rpm_idle(dev, RPM_AUTO);
1314 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1315 * @dev: Device to handle.
1316 * @delay: Value of the new delay in milliseconds.
1318 * Set the device's power.autosuspend_delay value. If it changes to negative
1319 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1320 * changes the other way, allow runtime suspends.
1322 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1324 int old_delay, old_use;
1326 spin_lock_irq(&dev->power.lock);
1327 old_delay = dev->power.autosuspend_delay;
1328 old_use = dev->power.use_autosuspend;
1329 dev->power.autosuspend_delay = delay;
1330 update_autosuspend(dev, old_delay, old_use);
1331 spin_unlock_irq(&dev->power.lock);
1333 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1336 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1337 * @dev: Device to handle.
1338 * @use: New value for use_autosuspend.
1340 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
1341 * suspends as needed.
1343 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1345 int old_delay, old_use;
1347 spin_lock_irq(&dev->power.lock);
1348 old_delay = dev->power.autosuspend_delay;
1349 old_use = dev->power.use_autosuspend;
1350 dev->power.use_autosuspend = use;
1351 update_autosuspend(dev, old_delay, old_use);
1352 spin_unlock_irq(&dev->power.lock);
1354 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1357 * pm_runtime_init - Initialize runtime PM fields in given device object.
1358 * @dev: Device object to initialize.
1360 void pm_runtime_init(struct device *dev)
1362 dev->power.runtime_status = RPM_SUSPENDED;
1363 dev->power.idle_notification = false;
1365 dev->power.disable_depth = 1;
1366 atomic_set(&dev->power.usage_count, 0);
1368 dev->power.runtime_error = 0;
1370 atomic_set(&dev->power.child_count, 0);
1371 pm_suspend_ignore_children(dev, false);
1372 dev->power.runtime_auto = true;
1374 dev->power.request_pending = false;
1375 dev->power.request = RPM_REQ_NONE;
1376 dev->power.deferred_resume = false;
1377 dev->power.accounting_timestamp = jiffies;
1378 INIT_WORK(&dev->power.work, pm_runtime_work);
1380 dev->power.timer_expires = 0;
1381 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1382 (unsigned long)dev);
1384 init_waitqueue_head(&dev->power.wait_queue);
1388 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1389 * @dev: Device object being removed from device hierarchy.
1391 void pm_runtime_remove(struct device *dev)
1393 __pm_runtime_disable(dev, false);
1395 /* Change the status back to 'suspended' to match the initial status. */
1396 if (dev->power.runtime_status == RPM_ACTIVE)
1397 pm_runtime_set_suspended(dev);
1398 if (dev->power.irq_safe && dev->parent)
1399 pm_runtime_put(dev->parent);
1403 * pm_runtime_force_suspend - Force a device into suspend state if needed.
1404 * @dev: Device to suspend.
1406 * Disable runtime PM so we safely can check the device's runtime PM status and
1407 * if it is active, invoke it's .runtime_suspend callback to bring it into
1408 * suspend state. Keep runtime PM disabled to preserve the state unless we
1411 * Typically this function may be invoked from a system suspend callback to make
1412 * sure the device is put into low power state.
1414 int pm_runtime_force_suspend(struct device *dev)
1416 int (*callback)(struct device *);
1419 pm_runtime_disable(dev);
1420 if (pm_runtime_status_suspended(dev))
1423 callback = RPM_GET_CALLBACK(dev, runtime_suspend);
1430 ret = callback(dev);
1434 pm_runtime_set_suspended(dev);
1437 pm_runtime_enable(dev);
1440 EXPORT_SYMBOL_GPL(pm_runtime_force_suspend);
1443 * pm_runtime_force_resume - Force a device into resume state.
1444 * @dev: Device to resume.
1446 * Prior invoking this function we expect the user to have brought the device
1447 * into low power state by a call to pm_runtime_force_suspend(). Here we reverse
1448 * those actions and brings the device into full power. We update the runtime PM
1449 * status and re-enables runtime PM.
1451 * Typically this function may be invoked from a system resume callback to make
1452 * sure the device is put into full power state.
1454 int pm_runtime_force_resume(struct device *dev)
1456 int (*callback)(struct device *);
1459 callback = RPM_GET_CALLBACK(dev, runtime_resume);
1466 ret = callback(dev);
1470 pm_runtime_set_active(dev);
1471 pm_runtime_mark_last_busy(dev);
1473 pm_runtime_enable(dev);
1476 EXPORT_SYMBOL_GPL(pm_runtime_force_resume);