1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/power/runtime.c - Helper functions for device runtime PM
5 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
6 * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
8 #include <linux/sched/mm.h>
9 #include <linux/ktime.h>
10 #include <linux/hrtimer.h>
11 #include <linux/export.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/pm_wakeirq.h>
14 #include <trace/events/rpm.h>
19 typedef int (*pm_callback_t)(struct device *);
21 static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset)
24 const struct dev_pm_ops *ops;
27 ops = &dev->pm_domain->ops;
28 else if (dev->type && dev->type->pm)
30 else if (dev->class && dev->class->pm)
32 else if (dev->bus && dev->bus->pm)
38 cb = *(pm_callback_t *)((void *)ops + cb_offset);
42 if (!cb && dev->driver && dev->driver->pm)
43 cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset);
48 #define RPM_GET_CALLBACK(dev, callback) \
49 __rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback))
51 static int rpm_resume(struct device *dev, int rpmflags);
52 static int rpm_suspend(struct device *dev, int rpmflags);
55 * update_pm_runtime_accounting - Update the time accounting of power states
56 * @dev: Device to update the accounting for
58 * In order to be able to have time accounting of the various power states
59 * (as used by programs such as PowerTOP to show the effectiveness of runtime
60 * PM), we need to track the time spent in each state.
61 * update_pm_runtime_accounting must be called each time before the
62 * runtime_status field is updated, to account the time in the old state
65 static void update_pm_runtime_accounting(struct device *dev)
69 if (dev->power.disable_depth > 0)
72 last = dev->power.accounting_timestamp;
74 now = ktime_get_mono_fast_ns();
75 dev->power.accounting_timestamp = now;
78 * Because ktime_get_mono_fast_ns() is not monotonic during
79 * timekeeping updates, ensure that 'now' is after the last saved
87 if (dev->power.runtime_status == RPM_SUSPENDED)
88 dev->power.suspended_time += delta;
90 dev->power.active_time += delta;
93 static void __update_runtime_status(struct device *dev, enum rpm_status status)
95 update_pm_runtime_accounting(dev);
96 dev->power.runtime_status = status;
99 static u64 rpm_get_accounted_time(struct device *dev, bool suspended)
104 spin_lock_irqsave(&dev->power.lock, flags);
106 update_pm_runtime_accounting(dev);
107 time = suspended ? dev->power.suspended_time : dev->power.active_time;
109 spin_unlock_irqrestore(&dev->power.lock, flags);
114 u64 pm_runtime_active_time(struct device *dev)
116 return rpm_get_accounted_time(dev, false);
119 u64 pm_runtime_suspended_time(struct device *dev)
121 return rpm_get_accounted_time(dev, true);
123 EXPORT_SYMBOL_GPL(pm_runtime_suspended_time);
126 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
127 * @dev: Device to handle.
129 static void pm_runtime_deactivate_timer(struct device *dev)
131 if (dev->power.timer_expires > 0) {
132 hrtimer_try_to_cancel(&dev->power.suspend_timer);
133 dev->power.timer_expires = 0;
138 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
139 * @dev: Device to handle.
141 static void pm_runtime_cancel_pending(struct device *dev)
143 pm_runtime_deactivate_timer(dev);
145 * In case there's a request pending, make sure its work function will
146 * return without doing anything.
148 dev->power.request = RPM_REQ_NONE;
152 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
153 * @dev: Device to handle.
155 * Compute the autosuspend-delay expiration time based on the device's
156 * power.last_busy time. If the delay has already expired or is disabled
157 * (negative) or the power.use_autosuspend flag isn't set, return 0.
158 * Otherwise return the expiration time in nanoseconds (adjusted to be nonzero).
160 * This function may be called either with or without dev->power.lock held.
161 * Either way it can be racy, since power.last_busy may be updated at any time.
163 u64 pm_runtime_autosuspend_expiration(struct device *dev)
165 int autosuspend_delay;
168 if (!dev->power.use_autosuspend)
171 autosuspend_delay = READ_ONCE(dev->power.autosuspend_delay);
172 if (autosuspend_delay < 0)
175 expires = READ_ONCE(dev->power.last_busy);
176 expires += (u64)autosuspend_delay * NSEC_PER_MSEC;
177 if (expires > ktime_get_mono_fast_ns())
178 return expires; /* Expires in the future */
182 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
184 static int dev_memalloc_noio(struct device *dev, void *data)
186 return dev->power.memalloc_noio;
190 * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
191 * @dev: Device to handle.
192 * @enable: True for setting the flag and False for clearing the flag.
194 * Set the flag for all devices in the path from the device to the
195 * root device in the device tree if @enable is true, otherwise clear
196 * the flag for devices in the path whose siblings don't set the flag.
198 * The function should only be called by block device, or network
199 * device driver for solving the deadlock problem during runtime
202 * If memory allocation with GFP_KERNEL is called inside runtime
203 * resume/suspend callback of any one of its ancestors(or the
204 * block device itself), the deadlock may be triggered inside the
205 * memory allocation since it might not complete until the block
206 * device becomes active and the involed page I/O finishes. The
207 * situation is pointed out first by Alan Stern. Network device
208 * are involved in iSCSI kind of situation.
210 * The lock of dev_hotplug_mutex is held in the function for handling
211 * hotplug race because pm_runtime_set_memalloc_noio() may be called
214 * The function should be called between device_add() and device_del()
215 * on the affected device(block/network device).
217 void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
219 static DEFINE_MUTEX(dev_hotplug_mutex);
221 mutex_lock(&dev_hotplug_mutex);
225 /* hold power lock since bitfield is not SMP-safe. */
226 spin_lock_irq(&dev->power.lock);
227 enabled = dev->power.memalloc_noio;
228 dev->power.memalloc_noio = enable;
229 spin_unlock_irq(&dev->power.lock);
232 * not need to enable ancestors any more if the device
235 if (enabled && enable)
241 * clear flag of the parent device only if all the
242 * children don't set the flag because ancestor's
243 * flag was set by any one of the descendants.
245 if (!dev || (!enable &&
246 device_for_each_child(dev, NULL,
250 mutex_unlock(&dev_hotplug_mutex);
252 EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
255 * rpm_check_suspend_allowed - Test whether a device may be suspended.
256 * @dev: Device to test.
258 static int rpm_check_suspend_allowed(struct device *dev)
262 if (dev->power.runtime_error)
264 else if (dev->power.disable_depth > 0)
266 else if (atomic_read(&dev->power.usage_count) > 0)
268 else if (!dev->power.ignore_children &&
269 atomic_read(&dev->power.child_count))
272 /* Pending resume requests take precedence over suspends. */
273 else if ((dev->power.deferred_resume
274 && dev->power.runtime_status == RPM_SUSPENDING)
275 || (dev->power.request_pending
276 && dev->power.request == RPM_REQ_RESUME))
278 else if (__dev_pm_qos_resume_latency(dev) == 0)
280 else if (dev->power.runtime_status == RPM_SUSPENDED)
286 static int rpm_get_suppliers(struct device *dev)
288 struct device_link *link;
290 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
291 device_links_read_lock_held()) {
294 if (!(link->flags & DL_FLAG_PM_RUNTIME))
297 retval = pm_runtime_get_sync(link->supplier);
298 /* Ignore suppliers with disabled runtime PM. */
299 if (retval < 0 && retval != -EACCES) {
300 pm_runtime_put_noidle(link->supplier);
303 refcount_inc(&link->rpm_active);
309 * pm_runtime_release_supplier - Drop references to device link's supplier.
310 * @link: Target device link.
312 * Drop all runtime PM references associated with @link to its supplier device.
314 void pm_runtime_release_supplier(struct device_link *link)
316 struct device *supplier = link->supplier;
319 * The additional power.usage_count check is a safety net in case
320 * the rpm_active refcount becomes saturated, in which case
321 * refcount_dec_not_one() would return true forever, but it is not
322 * strictly necessary.
324 while (refcount_dec_not_one(&link->rpm_active) &&
325 atomic_read(&supplier->power.usage_count) > 0)
326 pm_runtime_put_noidle(supplier);
329 static void __rpm_put_suppliers(struct device *dev, bool try_to_suspend)
331 struct device_link *link;
333 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
334 device_links_read_lock_held()) {
335 pm_runtime_release_supplier(link);
337 pm_request_idle(link->supplier);
341 static void rpm_put_suppliers(struct device *dev)
343 __rpm_put_suppliers(dev, true);
346 static void rpm_suspend_suppliers(struct device *dev)
348 struct device_link *link;
349 int idx = device_links_read_lock();
351 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
352 device_links_read_lock_held())
353 pm_request_idle(link->supplier);
355 device_links_read_unlock(idx);
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)
364 __releases(&dev->power.lock) __acquires(&dev->power.lock)
367 bool use_links = dev->power.links_count > 0;
369 if (dev->power.irq_safe) {
370 spin_unlock(&dev->power.lock);
372 spin_unlock_irq(&dev->power.lock);
375 * Resume suppliers if necessary.
377 * The device's runtime PM status cannot change until this
378 * routine returns, so it is safe to read the status outside of
381 if (use_links && dev->power.runtime_status == RPM_RESUMING) {
382 idx = device_links_read_lock();
384 retval = rpm_get_suppliers(dev);
386 rpm_put_suppliers(dev);
390 device_links_read_unlock(idx);
396 if (dev->power.irq_safe) {
397 spin_lock(&dev->power.lock);
400 * If the device is suspending and the callback has returned
401 * success, drop the usage counters of the suppliers that have
402 * been reference counted on its resume.
404 * Do that if resume fails too.
407 && ((dev->power.runtime_status == RPM_SUSPENDING && !retval)
408 || (dev->power.runtime_status == RPM_RESUMING && retval))) {
409 idx = device_links_read_lock();
411 __rpm_put_suppliers(dev, false);
414 device_links_read_unlock(idx);
417 spin_lock_irq(&dev->power.lock);
424 * rpm_idle - Notify device bus type if the device can be suspended.
425 * @dev: Device to notify the bus type about.
426 * @rpmflags: Flag bits.
428 * Check if the device's runtime PM status allows it to be suspended. If
429 * another idle notification has been started earlier, return immediately. If
430 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
431 * run the ->runtime_idle() callback directly. If the ->runtime_idle callback
432 * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag.
434 * This function must be called under dev->power.lock with interrupts disabled.
436 static int rpm_idle(struct device *dev, int rpmflags)
438 int (*callback)(struct device *);
441 trace_rpm_idle_rcuidle(dev, rpmflags);
442 retval = rpm_check_suspend_allowed(dev);
444 ; /* Conditions are wrong. */
446 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
447 else if (dev->power.runtime_status != RPM_ACTIVE)
451 * Any pending request other than an idle notification takes
452 * precedence over us, except that the timer may be running.
454 else if (dev->power.request_pending &&
455 dev->power.request > RPM_REQ_IDLE)
458 /* Act as though RPM_NOWAIT is always set. */
459 else if (dev->power.idle_notification)
460 retval = -EINPROGRESS;
464 /* Pending requests need to be canceled. */
465 dev->power.request = RPM_REQ_NONE;
467 callback = RPM_GET_CALLBACK(dev, runtime_idle);
469 /* If no callback assume success. */
470 if (!callback || dev->power.no_callbacks)
473 /* Carry out an asynchronous or a synchronous idle notification. */
474 if (rpmflags & RPM_ASYNC) {
475 dev->power.request = RPM_REQ_IDLE;
476 if (!dev->power.request_pending) {
477 dev->power.request_pending = true;
478 queue_work(pm_wq, &dev->power.work);
480 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, 0);
484 dev->power.idle_notification = true;
486 if (dev->power.irq_safe)
487 spin_unlock(&dev->power.lock);
489 spin_unlock_irq(&dev->power.lock);
491 retval = callback(dev);
493 if (dev->power.irq_safe)
494 spin_lock(&dev->power.lock);
496 spin_lock_irq(&dev->power.lock);
498 dev->power.idle_notification = false;
499 wake_up_all(&dev->power.wait_queue);
502 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
503 return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
507 * rpm_callback - Run a given runtime PM callback for a given device.
508 * @cb: Runtime PM callback to run.
509 * @dev: Device to run the callback for.
511 static int rpm_callback(int (*cb)(struct device *), struct device *dev)
518 if (dev->power.memalloc_noio) {
519 unsigned int noio_flag;
522 * Deadlock might be caused if memory allocation with
523 * GFP_KERNEL happens inside runtime_suspend and
524 * runtime_resume callbacks of one block device's
525 * ancestor or the block device itself. Network
526 * device might be thought as part of iSCSI block
527 * device, so network device and its ancestor should
528 * be marked as memalloc_noio too.
530 noio_flag = memalloc_noio_save();
531 retval = __rpm_callback(cb, dev);
532 memalloc_noio_restore(noio_flag);
534 retval = __rpm_callback(cb, dev);
537 dev->power.runtime_error = retval;
538 return retval != -EACCES ? retval : -EIO;
542 * rpm_suspend - Carry out runtime suspend of given device.
543 * @dev: Device to suspend.
544 * @rpmflags: Flag bits.
546 * Check if the device's runtime PM status allows it to be suspended.
547 * Cancel a pending idle notification, autosuspend or suspend. If
548 * another suspend has been started earlier, either return immediately
549 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
550 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
551 * otherwise run the ->runtime_suspend() callback directly. When
552 * ->runtime_suspend succeeded, if a deferred resume was requested while
553 * the callback was running then carry it out, otherwise send an idle
554 * notification for its parent (if the suspend succeeded and both
555 * ignore_children of parent->power and irq_safe of dev->power are not set).
556 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
557 * flag is set and the next autosuspend-delay expiration time is in the
558 * future, schedule another autosuspend attempt.
560 * This function must be called under dev->power.lock with interrupts disabled.
562 static int rpm_suspend(struct device *dev, int rpmflags)
563 __releases(&dev->power.lock) __acquires(&dev->power.lock)
565 int (*callback)(struct device *);
566 struct device *parent = NULL;
569 trace_rpm_suspend_rcuidle(dev, rpmflags);
572 retval = rpm_check_suspend_allowed(dev);
574 goto out; /* Conditions are wrong. */
576 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
577 if (dev->power.runtime_status == RPM_RESUMING && !(rpmflags & RPM_ASYNC))
582 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
583 if ((rpmflags & RPM_AUTO)
584 && dev->power.runtime_status != RPM_SUSPENDING) {
585 u64 expires = pm_runtime_autosuspend_expiration(dev);
588 /* Pending requests need to be canceled. */
589 dev->power.request = RPM_REQ_NONE;
592 * Optimization: If the timer is already running and is
593 * set to expire at or before the autosuspend delay,
594 * avoid the overhead of resetting it. Just let it
595 * expire; pm_suspend_timer_fn() will take care of the
598 if (!(dev->power.timer_expires &&
599 dev->power.timer_expires <= expires)) {
601 * We add a slack of 25% to gather wakeups
602 * without sacrificing the granularity.
604 u64 slack = (u64)READ_ONCE(dev->power.autosuspend_delay) *
605 (NSEC_PER_MSEC >> 2);
607 dev->power.timer_expires = expires;
608 hrtimer_start_range_ns(&dev->power.suspend_timer,
609 ns_to_ktime(expires),
613 dev->power.timer_autosuspends = 1;
618 /* Other scheduled or pending requests need to be canceled. */
619 pm_runtime_cancel_pending(dev);
621 if (dev->power.runtime_status == RPM_SUSPENDING) {
624 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
625 retval = -EINPROGRESS;
629 if (dev->power.irq_safe) {
630 spin_unlock(&dev->power.lock);
634 spin_lock(&dev->power.lock);
638 /* Wait for the other suspend running in parallel with us. */
640 prepare_to_wait(&dev->power.wait_queue, &wait,
641 TASK_UNINTERRUPTIBLE);
642 if (dev->power.runtime_status != RPM_SUSPENDING)
645 spin_unlock_irq(&dev->power.lock);
649 spin_lock_irq(&dev->power.lock);
651 finish_wait(&dev->power.wait_queue, &wait);
655 if (dev->power.no_callbacks)
656 goto no_callback; /* Assume success. */
658 /* Carry out an asynchronous or a synchronous suspend. */
659 if (rpmflags & RPM_ASYNC) {
660 dev->power.request = (rpmflags & RPM_AUTO) ?
661 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
662 if (!dev->power.request_pending) {
663 dev->power.request_pending = true;
664 queue_work(pm_wq, &dev->power.work);
669 __update_runtime_status(dev, RPM_SUSPENDING);
671 callback = RPM_GET_CALLBACK(dev, runtime_suspend);
673 dev_pm_enable_wake_irq_check(dev, true);
674 retval = rpm_callback(callback, dev);
678 dev_pm_enable_wake_irq_complete(dev);
681 __update_runtime_status(dev, RPM_SUSPENDED);
682 pm_runtime_deactivate_timer(dev);
685 parent = dev->parent;
686 atomic_add_unless(&parent->power.child_count, -1, 0);
688 wake_up_all(&dev->power.wait_queue);
690 if (dev->power.deferred_resume) {
691 dev->power.deferred_resume = false;
697 if (dev->power.irq_safe)
700 /* Maybe the parent is now able to suspend. */
701 if (parent && !parent->power.ignore_children) {
702 spin_unlock(&dev->power.lock);
704 spin_lock(&parent->power.lock);
705 rpm_idle(parent, RPM_ASYNC);
706 spin_unlock(&parent->power.lock);
708 spin_lock(&dev->power.lock);
710 /* Maybe the suppliers are now able to suspend. */
711 if (dev->power.links_count > 0) {
712 spin_unlock_irq(&dev->power.lock);
714 rpm_suspend_suppliers(dev);
716 spin_lock_irq(&dev->power.lock);
720 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
725 dev_pm_disable_wake_irq_check(dev, true);
726 __update_runtime_status(dev, RPM_ACTIVE);
727 dev->power.deferred_resume = false;
728 wake_up_all(&dev->power.wait_queue);
730 if (retval == -EAGAIN || retval == -EBUSY) {
731 dev->power.runtime_error = 0;
734 * If the callback routine failed an autosuspend, and
735 * if the last_busy time has been updated so that there
736 * is a new autosuspend expiration time, automatically
737 * reschedule another autosuspend.
739 if ((rpmflags & RPM_AUTO) &&
740 pm_runtime_autosuspend_expiration(dev) != 0)
743 pm_runtime_cancel_pending(dev);
749 * rpm_resume - Carry out runtime resume of given device.
750 * @dev: Device to resume.
751 * @rpmflags: Flag bits.
753 * Check if the device's runtime PM status allows it to be resumed. Cancel
754 * any scheduled or pending requests. If another resume has been started
755 * earlier, either return immediately or wait for it to finish, depending on the
756 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
757 * parallel with this function, either tell the other process to resume after
758 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
759 * flag is set then queue a resume request; otherwise run the
760 * ->runtime_resume() callback directly. Queue an idle notification for the
761 * device if the resume succeeded.
763 * This function must be called under dev->power.lock with interrupts disabled.
765 static int rpm_resume(struct device *dev, int rpmflags)
766 __releases(&dev->power.lock) __acquires(&dev->power.lock)
768 int (*callback)(struct device *);
769 struct device *parent = NULL;
772 trace_rpm_resume_rcuidle(dev, rpmflags);
775 if (dev->power.runtime_error)
777 else if (dev->power.disable_depth == 1 && dev->power.is_suspended
778 && dev->power.runtime_status == RPM_ACTIVE)
780 else if (dev->power.disable_depth > 0)
786 * Other scheduled or pending requests need to be canceled. Small
787 * optimization: If an autosuspend timer is running, leave it running
788 * rather than cancelling it now only to restart it again in the near
791 dev->power.request = RPM_REQ_NONE;
792 if (!dev->power.timer_autosuspends)
793 pm_runtime_deactivate_timer(dev);
795 if (dev->power.runtime_status == RPM_ACTIVE) {
800 if (dev->power.runtime_status == RPM_RESUMING
801 || dev->power.runtime_status == RPM_SUSPENDING) {
804 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
805 if (dev->power.runtime_status == RPM_SUSPENDING)
806 dev->power.deferred_resume = true;
808 retval = -EINPROGRESS;
812 if (dev->power.irq_safe) {
813 spin_unlock(&dev->power.lock);
817 spin_lock(&dev->power.lock);
821 /* Wait for the operation carried out in parallel with us. */
823 prepare_to_wait(&dev->power.wait_queue, &wait,
824 TASK_UNINTERRUPTIBLE);
825 if (dev->power.runtime_status != RPM_RESUMING
826 && dev->power.runtime_status != RPM_SUSPENDING)
829 spin_unlock_irq(&dev->power.lock);
833 spin_lock_irq(&dev->power.lock);
835 finish_wait(&dev->power.wait_queue, &wait);
840 * See if we can skip waking up the parent. This is safe only if
841 * power.no_callbacks is set, because otherwise we don't know whether
842 * the resume will actually succeed.
844 if (dev->power.no_callbacks && !parent && dev->parent) {
845 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
846 if (dev->parent->power.disable_depth > 0
847 || dev->parent->power.ignore_children
848 || dev->parent->power.runtime_status == RPM_ACTIVE) {
849 atomic_inc(&dev->parent->power.child_count);
850 spin_unlock(&dev->parent->power.lock);
852 goto no_callback; /* Assume success. */
854 spin_unlock(&dev->parent->power.lock);
857 /* Carry out an asynchronous or a synchronous resume. */
858 if (rpmflags & RPM_ASYNC) {
859 dev->power.request = RPM_REQ_RESUME;
860 if (!dev->power.request_pending) {
861 dev->power.request_pending = true;
862 queue_work(pm_wq, &dev->power.work);
868 if (!parent && dev->parent) {
870 * Increment the parent's usage counter and resume it if
871 * necessary. Not needed if dev is irq-safe; then the
872 * parent is permanently resumed.
874 parent = dev->parent;
875 if (dev->power.irq_safe)
877 spin_unlock(&dev->power.lock);
879 pm_runtime_get_noresume(parent);
881 spin_lock(&parent->power.lock);
883 * Resume the parent if it has runtime PM enabled and not been
884 * set to ignore its children.
886 if (!parent->power.disable_depth
887 && !parent->power.ignore_children) {
888 rpm_resume(parent, 0);
889 if (parent->power.runtime_status != RPM_ACTIVE)
892 spin_unlock(&parent->power.lock);
894 spin_lock(&dev->power.lock);
901 if (dev->power.no_callbacks)
902 goto no_callback; /* Assume success. */
904 __update_runtime_status(dev, RPM_RESUMING);
906 callback = RPM_GET_CALLBACK(dev, runtime_resume);
908 dev_pm_disable_wake_irq_check(dev, false);
909 retval = rpm_callback(callback, dev);
911 __update_runtime_status(dev, RPM_SUSPENDED);
912 pm_runtime_cancel_pending(dev);
913 dev_pm_enable_wake_irq_check(dev, false);
916 __update_runtime_status(dev, RPM_ACTIVE);
917 pm_runtime_mark_last_busy(dev);
919 atomic_inc(&parent->power.child_count);
921 wake_up_all(&dev->power.wait_queue);
924 rpm_idle(dev, RPM_ASYNC);
927 if (parent && !dev->power.irq_safe) {
928 spin_unlock_irq(&dev->power.lock);
930 pm_runtime_put(parent);
932 spin_lock_irq(&dev->power.lock);
935 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
941 * pm_runtime_work - Universal runtime PM work function.
942 * @work: Work structure used for scheduling the execution of this function.
944 * Use @work to get the device object the work is to be done for, determine what
945 * is to be done and execute the appropriate runtime PM function.
947 static void pm_runtime_work(struct work_struct *work)
949 struct device *dev = container_of(work, struct device, power.work);
950 enum rpm_request req;
952 spin_lock_irq(&dev->power.lock);
954 if (!dev->power.request_pending)
957 req = dev->power.request;
958 dev->power.request = RPM_REQ_NONE;
959 dev->power.request_pending = false;
965 rpm_idle(dev, RPM_NOWAIT);
967 case RPM_REQ_SUSPEND:
968 rpm_suspend(dev, RPM_NOWAIT);
970 case RPM_REQ_AUTOSUSPEND:
971 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
974 rpm_resume(dev, RPM_NOWAIT);
979 spin_unlock_irq(&dev->power.lock);
983 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
984 * @data: Device pointer passed by pm_schedule_suspend().
986 * Check if the time is right and queue a suspend request.
988 static enum hrtimer_restart pm_suspend_timer_fn(struct hrtimer *timer)
990 struct device *dev = container_of(timer, struct device, power.suspend_timer);
994 spin_lock_irqsave(&dev->power.lock, flags);
996 expires = dev->power.timer_expires;
998 * If 'expires' is after the current time, we've been called
1001 if (expires > 0 && expires < ktime_get_mono_fast_ns()) {
1002 dev->power.timer_expires = 0;
1003 rpm_suspend(dev, dev->power.timer_autosuspends ?
1004 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
1007 spin_unlock_irqrestore(&dev->power.lock, flags);
1009 return HRTIMER_NORESTART;
1013 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
1014 * @dev: Device to suspend.
1015 * @delay: Time to wait before submitting a suspend request, in milliseconds.
1017 int pm_schedule_suspend(struct device *dev, unsigned int delay)
1019 unsigned long flags;
1023 spin_lock_irqsave(&dev->power.lock, flags);
1026 retval = rpm_suspend(dev, RPM_ASYNC);
1030 retval = rpm_check_suspend_allowed(dev);
1034 /* Other scheduled or pending requests need to be canceled. */
1035 pm_runtime_cancel_pending(dev);
1037 expires = ktime_get_mono_fast_ns() + (u64)delay * NSEC_PER_MSEC;
1038 dev->power.timer_expires = expires;
1039 dev->power.timer_autosuspends = 0;
1040 hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS);
1043 spin_unlock_irqrestore(&dev->power.lock, flags);
1047 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
1050 * __pm_runtime_idle - Entry point for runtime idle operations.
1051 * @dev: Device to send idle notification for.
1052 * @rpmflags: Flag bits.
1054 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
1055 * return immediately if it is larger than zero. Then carry out an idle
1056 * notification, either synchronous or asynchronous.
1058 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1059 * or if pm_runtime_irq_safe() has been called.
1061 int __pm_runtime_idle(struct device *dev, int rpmflags)
1063 unsigned long flags;
1066 if (rpmflags & RPM_GET_PUT) {
1067 if (!atomic_dec_and_test(&dev->power.usage_count)) {
1068 trace_rpm_usage_rcuidle(dev, rpmflags);
1073 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
1075 spin_lock_irqsave(&dev->power.lock, flags);
1076 retval = rpm_idle(dev, rpmflags);
1077 spin_unlock_irqrestore(&dev->power.lock, flags);
1081 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
1084 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
1085 * @dev: Device to suspend.
1086 * @rpmflags: Flag bits.
1088 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
1089 * return immediately if it is larger than zero. Then carry out a suspend,
1090 * either synchronous or asynchronous.
1092 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1093 * or if pm_runtime_irq_safe() has been called.
1095 int __pm_runtime_suspend(struct device *dev, int rpmflags)
1097 unsigned long flags;
1100 if (rpmflags & RPM_GET_PUT) {
1101 if (!atomic_dec_and_test(&dev->power.usage_count)) {
1102 trace_rpm_usage_rcuidle(dev, rpmflags);
1107 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
1109 spin_lock_irqsave(&dev->power.lock, flags);
1110 retval = rpm_suspend(dev, rpmflags);
1111 spin_unlock_irqrestore(&dev->power.lock, flags);
1115 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
1118 * __pm_runtime_resume - Entry point for runtime resume operations.
1119 * @dev: Device to resume.
1120 * @rpmflags: Flag bits.
1122 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
1123 * carry out a resume, either synchronous or asynchronous.
1125 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1126 * or if pm_runtime_irq_safe() has been called.
1128 int __pm_runtime_resume(struct device *dev, int rpmflags)
1130 unsigned long flags;
1133 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe &&
1134 dev->power.runtime_status != RPM_ACTIVE);
1136 if (rpmflags & RPM_GET_PUT)
1137 atomic_inc(&dev->power.usage_count);
1139 spin_lock_irqsave(&dev->power.lock, flags);
1140 retval = rpm_resume(dev, rpmflags);
1141 spin_unlock_irqrestore(&dev->power.lock, flags);
1145 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
1148 * pm_runtime_get_if_active - Conditionally bump up device usage counter.
1149 * @dev: Device to handle.
1150 * @ign_usage_count: Whether or not to look at the current usage counter value.
1152 * Return -EINVAL if runtime PM is disabled for @dev.
1154 * Otherwise, if the runtime PM status of @dev is %RPM_ACTIVE and either
1155 * @ign_usage_count is %true or the runtime PM usage counter of @dev is not
1156 * zero, increment the usage counter of @dev and return 1. Otherwise, return 0
1157 * without changing the usage counter.
1159 * If @ign_usage_count is %true, this function can be used to prevent suspending
1160 * the device when its runtime PM status is %RPM_ACTIVE.
1162 * If @ign_usage_count is %false, this function can be used to prevent
1163 * suspending the device when both its runtime PM status is %RPM_ACTIVE and its
1164 * runtime PM usage counter is not zero.
1166 * The caller is resposible for decrementing the runtime PM usage counter of
1167 * @dev after this function has returned a positive value for it.
1169 int pm_runtime_get_if_active(struct device *dev, bool ign_usage_count)
1171 unsigned long flags;
1174 spin_lock_irqsave(&dev->power.lock, flags);
1175 if (dev->power.disable_depth > 0) {
1177 } else if (dev->power.runtime_status != RPM_ACTIVE) {
1179 } else if (ign_usage_count) {
1181 atomic_inc(&dev->power.usage_count);
1183 retval = atomic_inc_not_zero(&dev->power.usage_count);
1185 trace_rpm_usage_rcuidle(dev, 0);
1186 spin_unlock_irqrestore(&dev->power.lock, flags);
1190 EXPORT_SYMBOL_GPL(pm_runtime_get_if_active);
1193 * __pm_runtime_set_status - Set runtime PM status of a device.
1194 * @dev: Device to handle.
1195 * @status: New runtime PM status of the device.
1197 * If runtime PM of the device is disabled or its power.runtime_error field is
1198 * different from zero, the status may be changed either to RPM_ACTIVE, or to
1199 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
1200 * However, if the device has a parent and the parent is not active, and the
1201 * parent's power.ignore_children flag is unset, the device's status cannot be
1202 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
1204 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
1205 * and the device parent's counter of unsuspended children is modified to
1206 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
1207 * notification request for the parent is submitted.
1209 * If @dev has any suppliers (as reflected by device links to them), and @status
1210 * is RPM_ACTIVE, they will be activated upfront and if the activation of one
1211 * of them fails, the status of @dev will be changed to RPM_SUSPENDED (instead
1212 * of the @status value) and the suppliers will be deacticated on exit. The
1213 * error returned by the failing supplier activation will be returned in that
1216 int __pm_runtime_set_status(struct device *dev, unsigned int status)
1218 struct device *parent = dev->parent;
1219 bool notify_parent = false;
1222 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
1225 spin_lock_irq(&dev->power.lock);
1228 * Prevent PM-runtime from being enabled for the device or return an
1229 * error if it is enabled already and working.
1231 if (dev->power.runtime_error || dev->power.disable_depth)
1232 dev->power.disable_depth++;
1236 spin_unlock_irq(&dev->power.lock);
1242 * If the new status is RPM_ACTIVE, the suppliers can be activated
1243 * upfront regardless of the current status, because next time
1244 * rpm_put_suppliers() runs, the rpm_active refcounts of the links
1245 * involved will be dropped down to one anyway.
1247 if (status == RPM_ACTIVE) {
1248 int idx = device_links_read_lock();
1250 error = rpm_get_suppliers(dev);
1252 status = RPM_SUSPENDED;
1254 device_links_read_unlock(idx);
1257 spin_lock_irq(&dev->power.lock);
1259 if (dev->power.runtime_status == status || !parent)
1262 if (status == RPM_SUSPENDED) {
1263 atomic_add_unless(&parent->power.child_count, -1, 0);
1264 notify_parent = !parent->power.ignore_children;
1266 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
1269 * It is invalid to put an active child under a parent that is
1270 * not active, has runtime PM enabled and the
1271 * 'power.ignore_children' flag unset.
1273 if (!parent->power.disable_depth
1274 && !parent->power.ignore_children
1275 && parent->power.runtime_status != RPM_ACTIVE) {
1276 dev_err(dev, "runtime PM trying to activate child device %s but parent (%s) is not active\n",
1280 } else if (dev->power.runtime_status == RPM_SUSPENDED) {
1281 atomic_inc(&parent->power.child_count);
1284 spin_unlock(&parent->power.lock);
1287 status = RPM_SUSPENDED;
1293 __update_runtime_status(dev, status);
1295 dev->power.runtime_error = 0;
1298 spin_unlock_irq(&dev->power.lock);
1301 pm_request_idle(parent);
1303 if (status == RPM_SUSPENDED) {
1304 int idx = device_links_read_lock();
1306 rpm_put_suppliers(dev);
1308 device_links_read_unlock(idx);
1311 pm_runtime_enable(dev);
1315 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1318 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1319 * @dev: Device to handle.
1321 * Flush all pending requests for the device from pm_wq and wait for all
1322 * runtime PM operations involving the device in progress to complete.
1324 * Should be called under dev->power.lock with interrupts disabled.
1326 static void __pm_runtime_barrier(struct device *dev)
1328 pm_runtime_deactivate_timer(dev);
1330 if (dev->power.request_pending) {
1331 dev->power.request = RPM_REQ_NONE;
1332 spin_unlock_irq(&dev->power.lock);
1334 cancel_work_sync(&dev->power.work);
1336 spin_lock_irq(&dev->power.lock);
1337 dev->power.request_pending = false;
1340 if (dev->power.runtime_status == RPM_SUSPENDING
1341 || dev->power.runtime_status == RPM_RESUMING
1342 || dev->power.idle_notification) {
1345 /* Suspend, wake-up or idle notification in progress. */
1347 prepare_to_wait(&dev->power.wait_queue, &wait,
1348 TASK_UNINTERRUPTIBLE);
1349 if (dev->power.runtime_status != RPM_SUSPENDING
1350 && dev->power.runtime_status != RPM_RESUMING
1351 && !dev->power.idle_notification)
1353 spin_unlock_irq(&dev->power.lock);
1357 spin_lock_irq(&dev->power.lock);
1359 finish_wait(&dev->power.wait_queue, &wait);
1364 * pm_runtime_barrier - Flush pending requests and wait for completions.
1365 * @dev: Device to handle.
1367 * Prevent the device from being suspended by incrementing its usage counter and
1368 * if there's a pending resume request for the device, wake the device up.
1369 * Next, make sure that all pending requests for the device have been flushed
1370 * from pm_wq and wait for all runtime PM operations involving the device in
1371 * progress to complete.
1374 * 1, if there was a resume request pending and the device had to be woken up,
1377 int pm_runtime_barrier(struct device *dev)
1381 pm_runtime_get_noresume(dev);
1382 spin_lock_irq(&dev->power.lock);
1384 if (dev->power.request_pending
1385 && dev->power.request == RPM_REQ_RESUME) {
1390 __pm_runtime_barrier(dev);
1392 spin_unlock_irq(&dev->power.lock);
1393 pm_runtime_put_noidle(dev);
1397 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1400 * __pm_runtime_disable - Disable runtime PM of a device.
1401 * @dev: Device to handle.
1402 * @check_resume: If set, check if there's a resume request for the device.
1404 * Increment power.disable_depth for the device and if it was zero previously,
1405 * cancel all pending runtime PM requests for the device and wait for all
1406 * operations in progress to complete. The device can be either active or
1407 * suspended after its runtime PM has been disabled.
1409 * If @check_resume is set and there's a resume request pending when
1410 * __pm_runtime_disable() is called and power.disable_depth is zero, the
1411 * function will wake up the device before disabling its runtime PM.
1413 void __pm_runtime_disable(struct device *dev, bool check_resume)
1415 spin_lock_irq(&dev->power.lock);
1417 if (dev->power.disable_depth > 0) {
1418 dev->power.disable_depth++;
1423 * Wake up the device if there's a resume request pending, because that
1424 * means there probably is some I/O to process and disabling runtime PM
1425 * shouldn't prevent the device from processing the I/O.
1427 if (check_resume && dev->power.request_pending
1428 && dev->power.request == RPM_REQ_RESUME) {
1430 * Prevent suspends and idle notifications from being carried
1431 * out after we have woken up the device.
1433 pm_runtime_get_noresume(dev);
1437 pm_runtime_put_noidle(dev);
1440 /* Update time accounting before disabling PM-runtime. */
1441 update_pm_runtime_accounting(dev);
1443 if (!dev->power.disable_depth++)
1444 __pm_runtime_barrier(dev);
1447 spin_unlock_irq(&dev->power.lock);
1449 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1452 * pm_runtime_enable - Enable runtime PM of a device.
1453 * @dev: Device to handle.
1455 void pm_runtime_enable(struct device *dev)
1457 unsigned long flags;
1459 spin_lock_irqsave(&dev->power.lock, flags);
1461 if (dev->power.disable_depth > 0) {
1462 dev->power.disable_depth--;
1464 /* About to enable runtime pm, set accounting_timestamp to now */
1465 if (!dev->power.disable_depth)
1466 dev->power.accounting_timestamp = ktime_get_mono_fast_ns();
1468 dev_warn(dev, "Unbalanced %s!\n", __func__);
1471 WARN(!dev->power.disable_depth &&
1472 dev->power.runtime_status == RPM_SUSPENDED &&
1473 !dev->power.ignore_children &&
1474 atomic_read(&dev->power.child_count) > 0,
1475 "Enabling runtime PM for inactive device (%s) with active children\n",
1478 spin_unlock_irqrestore(&dev->power.lock, flags);
1480 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1482 static void pm_runtime_disable_action(void *data)
1484 pm_runtime_dont_use_autosuspend(data);
1485 pm_runtime_disable(data);
1489 * devm_pm_runtime_enable - devres-enabled version of pm_runtime_enable.
1491 * NOTE: this will also handle calling pm_runtime_dont_use_autosuspend() for
1492 * you at driver exit time if needed.
1494 * @dev: Device to handle.
1496 int devm_pm_runtime_enable(struct device *dev)
1498 pm_runtime_enable(dev);
1500 return devm_add_action_or_reset(dev, pm_runtime_disable_action, dev);
1502 EXPORT_SYMBOL_GPL(devm_pm_runtime_enable);
1505 * pm_runtime_forbid - Block runtime PM of a device.
1506 * @dev: Device to handle.
1508 * Increase the device's usage count and clear its power.runtime_auto flag,
1509 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1512 void pm_runtime_forbid(struct device *dev)
1514 spin_lock_irq(&dev->power.lock);
1515 if (!dev->power.runtime_auto)
1518 dev->power.runtime_auto = false;
1519 atomic_inc(&dev->power.usage_count);
1523 spin_unlock_irq(&dev->power.lock);
1525 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1528 * pm_runtime_allow - Unblock runtime PM of a device.
1529 * @dev: Device to handle.
1531 * Decrease the device's usage count and set its power.runtime_auto flag.
1533 void pm_runtime_allow(struct device *dev)
1535 spin_lock_irq(&dev->power.lock);
1536 if (dev->power.runtime_auto)
1539 dev->power.runtime_auto = true;
1540 if (atomic_dec_and_test(&dev->power.usage_count))
1541 rpm_idle(dev, RPM_AUTO | RPM_ASYNC);
1543 trace_rpm_usage_rcuidle(dev, RPM_AUTO | RPM_ASYNC);
1546 spin_unlock_irq(&dev->power.lock);
1548 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1551 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1552 * @dev: Device to handle.
1554 * Set the power.no_callbacks flag, which tells the PM core that this
1555 * device is power-managed through its parent and has no runtime PM
1556 * callbacks of its own. The runtime sysfs attributes will be removed.
1558 void pm_runtime_no_callbacks(struct device *dev)
1560 spin_lock_irq(&dev->power.lock);
1561 dev->power.no_callbacks = 1;
1562 spin_unlock_irq(&dev->power.lock);
1563 if (device_is_registered(dev))
1564 rpm_sysfs_remove(dev);
1566 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1569 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1570 * @dev: Device to handle
1572 * Set the power.irq_safe flag, which tells the PM core that the
1573 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1574 * always be invoked with the spinlock held and interrupts disabled. It also
1575 * causes the parent's usage counter to be permanently incremented, preventing
1576 * the parent from runtime suspending -- otherwise an irq-safe child might have
1577 * to wait for a non-irq-safe parent.
1579 void pm_runtime_irq_safe(struct device *dev)
1582 pm_runtime_get_sync(dev->parent);
1583 spin_lock_irq(&dev->power.lock);
1584 dev->power.irq_safe = 1;
1585 spin_unlock_irq(&dev->power.lock);
1587 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1590 * update_autosuspend - Handle a change to a device's autosuspend settings.
1591 * @dev: Device to handle.
1592 * @old_delay: The former autosuspend_delay value.
1593 * @old_use: The former use_autosuspend value.
1595 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1596 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1598 * This function must be called under dev->power.lock with interrupts disabled.
1600 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1602 int delay = dev->power.autosuspend_delay;
1604 /* Should runtime suspend be prevented now? */
1605 if (dev->power.use_autosuspend && delay < 0) {
1607 /* If it used to be allowed then prevent it. */
1608 if (!old_use || old_delay >= 0) {
1609 atomic_inc(&dev->power.usage_count);
1612 trace_rpm_usage_rcuidle(dev, 0);
1616 /* Runtime suspend should be allowed now. */
1619 /* If it used to be prevented then allow it. */
1620 if (old_use && old_delay < 0)
1621 atomic_dec(&dev->power.usage_count);
1623 /* Maybe we can autosuspend now. */
1624 rpm_idle(dev, RPM_AUTO);
1629 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1630 * @dev: Device to handle.
1631 * @delay: Value of the new delay in milliseconds.
1633 * Set the device's power.autosuspend_delay value. If it changes to negative
1634 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1635 * changes the other way, allow runtime suspends.
1637 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1639 int old_delay, old_use;
1641 spin_lock_irq(&dev->power.lock);
1642 old_delay = dev->power.autosuspend_delay;
1643 old_use = dev->power.use_autosuspend;
1644 dev->power.autosuspend_delay = delay;
1645 update_autosuspend(dev, old_delay, old_use);
1646 spin_unlock_irq(&dev->power.lock);
1648 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1651 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1652 * @dev: Device to handle.
1653 * @use: New value for use_autosuspend.
1655 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
1656 * suspends as needed.
1658 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1660 int old_delay, old_use;
1662 spin_lock_irq(&dev->power.lock);
1663 old_delay = dev->power.autosuspend_delay;
1664 old_use = dev->power.use_autosuspend;
1665 dev->power.use_autosuspend = use;
1666 update_autosuspend(dev, old_delay, old_use);
1667 spin_unlock_irq(&dev->power.lock);
1669 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1672 * pm_runtime_init - Initialize runtime PM fields in given device object.
1673 * @dev: Device object to initialize.
1675 void pm_runtime_init(struct device *dev)
1677 dev->power.runtime_status = RPM_SUSPENDED;
1678 dev->power.idle_notification = false;
1680 dev->power.disable_depth = 1;
1681 atomic_set(&dev->power.usage_count, 0);
1683 dev->power.runtime_error = 0;
1685 atomic_set(&dev->power.child_count, 0);
1686 pm_suspend_ignore_children(dev, false);
1687 dev->power.runtime_auto = true;
1689 dev->power.request_pending = false;
1690 dev->power.request = RPM_REQ_NONE;
1691 dev->power.deferred_resume = false;
1692 dev->power.needs_force_resume = 0;
1693 INIT_WORK(&dev->power.work, pm_runtime_work);
1695 dev->power.timer_expires = 0;
1696 hrtimer_init(&dev->power.suspend_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1697 dev->power.suspend_timer.function = pm_suspend_timer_fn;
1699 init_waitqueue_head(&dev->power.wait_queue);
1703 * pm_runtime_reinit - Re-initialize runtime PM fields in given device object.
1704 * @dev: Device object to re-initialize.
1706 void pm_runtime_reinit(struct device *dev)
1708 if (!pm_runtime_enabled(dev)) {
1709 if (dev->power.runtime_status == RPM_ACTIVE)
1710 pm_runtime_set_suspended(dev);
1711 if (dev->power.irq_safe) {
1712 spin_lock_irq(&dev->power.lock);
1713 dev->power.irq_safe = 0;
1714 spin_unlock_irq(&dev->power.lock);
1716 pm_runtime_put(dev->parent);
1722 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1723 * @dev: Device object being removed from device hierarchy.
1725 void pm_runtime_remove(struct device *dev)
1727 __pm_runtime_disable(dev, false);
1728 pm_runtime_reinit(dev);
1732 * pm_runtime_get_suppliers - Resume and reference-count supplier devices.
1733 * @dev: Consumer device.
1735 void pm_runtime_get_suppliers(struct device *dev)
1737 struct device_link *link;
1740 idx = device_links_read_lock();
1742 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
1743 device_links_read_lock_held())
1744 if (link->flags & DL_FLAG_PM_RUNTIME) {
1745 link->supplier_preactivated = true;
1746 pm_runtime_get_sync(link->supplier);
1747 refcount_inc(&link->rpm_active);
1750 device_links_read_unlock(idx);
1754 * pm_runtime_put_suppliers - Drop references to supplier devices.
1755 * @dev: Consumer device.
1757 void pm_runtime_put_suppliers(struct device *dev)
1759 struct device_link *link;
1760 unsigned long flags;
1764 idx = device_links_read_lock();
1766 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
1767 device_links_read_lock_held())
1768 if (link->supplier_preactivated) {
1769 link->supplier_preactivated = false;
1770 spin_lock_irqsave(&dev->power.lock, flags);
1771 put = pm_runtime_status_suspended(dev) &&
1772 refcount_dec_not_one(&link->rpm_active);
1773 spin_unlock_irqrestore(&dev->power.lock, flags);
1775 pm_runtime_put(link->supplier);
1778 device_links_read_unlock(idx);
1781 void pm_runtime_new_link(struct device *dev)
1783 spin_lock_irq(&dev->power.lock);
1784 dev->power.links_count++;
1785 spin_unlock_irq(&dev->power.lock);
1788 static void pm_runtime_drop_link_count(struct device *dev)
1790 spin_lock_irq(&dev->power.lock);
1791 WARN_ON(dev->power.links_count == 0);
1792 dev->power.links_count--;
1793 spin_unlock_irq(&dev->power.lock);
1797 * pm_runtime_drop_link - Prepare for device link removal.
1798 * @link: Device link going away.
1800 * Drop the link count of the consumer end of @link and decrement the supplier
1801 * device's runtime PM usage counter as many times as needed to drop all of the
1802 * PM runtime reference to it from the consumer.
1804 void pm_runtime_drop_link(struct device_link *link)
1806 if (!(link->flags & DL_FLAG_PM_RUNTIME))
1809 pm_runtime_drop_link_count(link->consumer);
1810 pm_runtime_release_supplier(link);
1811 pm_request_idle(link->supplier);
1814 static bool pm_runtime_need_not_resume(struct device *dev)
1816 return atomic_read(&dev->power.usage_count) <= 1 &&
1817 (atomic_read(&dev->power.child_count) == 0 ||
1818 dev->power.ignore_children);
1822 * pm_runtime_force_suspend - Force a device into suspend state if needed.
1823 * @dev: Device to suspend.
1825 * Disable runtime PM so we safely can check the device's runtime PM status and
1826 * if it is active, invoke its ->runtime_suspend callback to suspend it and
1827 * change its runtime PM status field to RPM_SUSPENDED. Also, if the device's
1828 * usage and children counters don't indicate that the device was in use before
1829 * the system-wide transition under way, decrement its parent's children counter
1830 * (if there is a parent). Keep runtime PM disabled to preserve the state
1831 * unless we encounter errors.
1833 * Typically this function may be invoked from a system suspend callback to make
1834 * sure the device is put into low power state and it should only be used during
1835 * system-wide PM transitions to sleep states. It assumes that the analogous
1836 * pm_runtime_force_resume() will be used to resume the device.
1838 int pm_runtime_force_suspend(struct device *dev)
1840 int (*callback)(struct device *);
1843 pm_runtime_disable(dev);
1844 if (pm_runtime_status_suspended(dev))
1847 callback = RPM_GET_CALLBACK(dev, runtime_suspend);
1849 ret = callback ? callback(dev) : 0;
1854 * If the device can stay in suspend after the system-wide transition
1855 * to the working state that will follow, drop the children counter of
1856 * its parent, but set its status to RPM_SUSPENDED anyway in case this
1857 * function will be called again for it in the meantime.
1859 if (pm_runtime_need_not_resume(dev)) {
1860 pm_runtime_set_suspended(dev);
1862 __update_runtime_status(dev, RPM_SUSPENDED);
1863 dev->power.needs_force_resume = 1;
1869 pm_runtime_enable(dev);
1872 EXPORT_SYMBOL_GPL(pm_runtime_force_suspend);
1875 * pm_runtime_force_resume - Force a device into resume state if needed.
1876 * @dev: Device to resume.
1878 * Prior invoking this function we expect the user to have brought the device
1879 * into low power state by a call to pm_runtime_force_suspend(). Here we reverse
1880 * those actions and bring the device into full power, if it is expected to be
1881 * used on system resume. In the other case, we defer the resume to be managed
1884 * Typically this function may be invoked from a system resume callback.
1886 int pm_runtime_force_resume(struct device *dev)
1888 int (*callback)(struct device *);
1891 if (!pm_runtime_status_suspended(dev) || !dev->power.needs_force_resume)
1895 * The value of the parent's children counter is correct already, so
1896 * just update the status of the device.
1898 __update_runtime_status(dev, RPM_ACTIVE);
1900 callback = RPM_GET_CALLBACK(dev, runtime_resume);
1902 ret = callback ? callback(dev) : 0;
1904 pm_runtime_set_suspended(dev);
1908 pm_runtime_mark_last_busy(dev);
1910 dev->power.needs_force_resume = 0;
1911 pm_runtime_enable(dev);
1914 EXPORT_SYMBOL_GPL(pm_runtime_force_resume);