Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * drivers/base/power/runtime.c - Helper functions for device runtime PM
0004  *
0005  * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
0006  * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
0007  */
0008 #include <linux/sched/mm.h>
0009 #include <linux/ktime.h>
0010 #include <linux/hrtimer.h>
0011 #include <linux/export.h>
0012 #include <linux/pm_runtime.h>
0013 #include <linux/pm_wakeirq.h>
0014 #include <trace/events/rpm.h>
0015 
0016 #include "../base.h"
0017 #include "power.h"
0018 
0019 typedef int (*pm_callback_t)(struct device *);
0020 
0021 static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset)
0022 {
0023     pm_callback_t cb;
0024     const struct dev_pm_ops *ops;
0025 
0026     if (dev->pm_domain)
0027         ops = &dev->pm_domain->ops;
0028     else if (dev->type && dev->type->pm)
0029         ops = dev->type->pm;
0030     else if (dev->class && dev->class->pm)
0031         ops = dev->class->pm;
0032     else if (dev->bus && dev->bus->pm)
0033         ops = dev->bus->pm;
0034     else
0035         ops = NULL;
0036 
0037     if (ops)
0038         cb = *(pm_callback_t *)((void *)ops + cb_offset);
0039     else
0040         cb = NULL;
0041 
0042     if (!cb && dev->driver && dev->driver->pm)
0043         cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset);
0044 
0045     return cb;
0046 }
0047 
0048 #define RPM_GET_CALLBACK(dev, callback) \
0049         __rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback))
0050 
0051 static int rpm_resume(struct device *dev, int rpmflags);
0052 static int rpm_suspend(struct device *dev, int rpmflags);
0053 
0054 /**
0055  * update_pm_runtime_accounting - Update the time accounting of power states
0056  * @dev: Device to update the accounting for
0057  *
0058  * In order to be able to have time accounting of the various power states
0059  * (as used by programs such as PowerTOP to show the effectiveness of runtime
0060  * PM), we need to track the time spent in each state.
0061  * update_pm_runtime_accounting must be called each time before the
0062  * runtime_status field is updated, to account the time in the old state
0063  * correctly.
0064  */
0065 static void update_pm_runtime_accounting(struct device *dev)
0066 {
0067     u64 now, last, delta;
0068 
0069     if (dev->power.disable_depth > 0)
0070         return;
0071 
0072     last = dev->power.accounting_timestamp;
0073 
0074     now = ktime_get_mono_fast_ns();
0075     dev->power.accounting_timestamp = now;
0076 
0077     /*
0078      * Because ktime_get_mono_fast_ns() is not monotonic during
0079      * timekeeping updates, ensure that 'now' is after the last saved
0080      * timesptamp.
0081      */
0082     if (now < last)
0083         return;
0084 
0085     delta = now - last;
0086 
0087     if (dev->power.runtime_status == RPM_SUSPENDED)
0088         dev->power.suspended_time += delta;
0089     else
0090         dev->power.active_time += delta;
0091 }
0092 
0093 static void __update_runtime_status(struct device *dev, enum rpm_status status)
0094 {
0095     update_pm_runtime_accounting(dev);
0096     dev->power.runtime_status = status;
0097 }
0098 
0099 static u64 rpm_get_accounted_time(struct device *dev, bool suspended)
0100 {
0101     u64 time;
0102     unsigned long flags;
0103 
0104     spin_lock_irqsave(&dev->power.lock, flags);
0105 
0106     update_pm_runtime_accounting(dev);
0107     time = suspended ? dev->power.suspended_time : dev->power.active_time;
0108 
0109     spin_unlock_irqrestore(&dev->power.lock, flags);
0110 
0111     return time;
0112 }
0113 
0114 u64 pm_runtime_active_time(struct device *dev)
0115 {
0116     return rpm_get_accounted_time(dev, false);
0117 }
0118 
0119 u64 pm_runtime_suspended_time(struct device *dev)
0120 {
0121     return rpm_get_accounted_time(dev, true);
0122 }
0123 EXPORT_SYMBOL_GPL(pm_runtime_suspended_time);
0124 
0125 /**
0126  * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
0127  * @dev: Device to handle.
0128  */
0129 static void pm_runtime_deactivate_timer(struct device *dev)
0130 {
0131     if (dev->power.timer_expires > 0) {
0132         hrtimer_try_to_cancel(&dev->power.suspend_timer);
0133         dev->power.timer_expires = 0;
0134     }
0135 }
0136 
0137 /**
0138  * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
0139  * @dev: Device to handle.
0140  */
0141 static void pm_runtime_cancel_pending(struct device *dev)
0142 {
0143     pm_runtime_deactivate_timer(dev);
0144     /*
0145      * In case there's a request pending, make sure its work function will
0146      * return without doing anything.
0147      */
0148     dev->power.request = RPM_REQ_NONE;
0149 }
0150 
0151 /*
0152  * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
0153  * @dev: Device to handle.
0154  *
0155  * Compute the autosuspend-delay expiration time based on the device's
0156  * power.last_busy time.  If the delay has already expired or is disabled
0157  * (negative) or the power.use_autosuspend flag isn't set, return 0.
0158  * Otherwise return the expiration time in nanoseconds (adjusted to be nonzero).
0159  *
0160  * This function may be called either with or without dev->power.lock held.
0161  * Either way it can be racy, since power.last_busy may be updated at any time.
0162  */
0163 u64 pm_runtime_autosuspend_expiration(struct device *dev)
0164 {
0165     int autosuspend_delay;
0166     u64 expires;
0167 
0168     if (!dev->power.use_autosuspend)
0169         return 0;
0170 
0171     autosuspend_delay = READ_ONCE(dev->power.autosuspend_delay);
0172     if (autosuspend_delay < 0)
0173         return 0;
0174 
0175     expires  = READ_ONCE(dev->power.last_busy);
0176     expires += (u64)autosuspend_delay * NSEC_PER_MSEC;
0177     if (expires > ktime_get_mono_fast_ns())
0178         return expires; /* Expires in the future */
0179 
0180     return 0;
0181 }
0182 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
0183 
0184 static int dev_memalloc_noio(struct device *dev, void *data)
0185 {
0186     return dev->power.memalloc_noio;
0187 }
0188 
0189 /*
0190  * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
0191  * @dev: Device to handle.
0192  * @enable: True for setting the flag and False for clearing the flag.
0193  *
0194  * Set the flag for all devices in the path from the device to the
0195  * root device in the device tree if @enable is true, otherwise clear
0196  * the flag for devices in the path whose siblings don't set the flag.
0197  *
0198  * The function should only be called by block device, or network
0199  * device driver for solving the deadlock problem during runtime
0200  * resume/suspend:
0201  *
0202  *     If memory allocation with GFP_KERNEL is called inside runtime
0203  *     resume/suspend callback of any one of its ancestors(or the
0204  *     block device itself), the deadlock may be triggered inside the
0205  *     memory allocation since it might not complete until the block
0206  *     device becomes active and the involed page I/O finishes. The
0207  *     situation is pointed out first by Alan Stern. Network device
0208  *     are involved in iSCSI kind of situation.
0209  *
0210  * The lock of dev_hotplug_mutex is held in the function for handling
0211  * hotplug race because pm_runtime_set_memalloc_noio() may be called
0212  * in async probe().
0213  *
0214  * The function should be called between device_add() and device_del()
0215  * on the affected device(block/network device).
0216  */
0217 void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
0218 {
0219     static DEFINE_MUTEX(dev_hotplug_mutex);
0220 
0221     mutex_lock(&dev_hotplug_mutex);
0222     for (;;) {
0223         bool enabled;
0224 
0225         /* hold power lock since bitfield is not SMP-safe. */
0226         spin_lock_irq(&dev->power.lock);
0227         enabled = dev->power.memalloc_noio;
0228         dev->power.memalloc_noio = enable;
0229         spin_unlock_irq(&dev->power.lock);
0230 
0231         /*
0232          * not need to enable ancestors any more if the device
0233          * has been enabled.
0234          */
0235         if (enabled && enable)
0236             break;
0237 
0238         dev = dev->parent;
0239 
0240         /*
0241          * clear flag of the parent device only if all the
0242          * children don't set the flag because ancestor's
0243          * flag was set by any one of the descendants.
0244          */
0245         if (!dev || (!enable &&
0246                  device_for_each_child(dev, NULL,
0247                            dev_memalloc_noio)))
0248             break;
0249     }
0250     mutex_unlock(&dev_hotplug_mutex);
0251 }
0252 EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
0253 
0254 /**
0255  * rpm_check_suspend_allowed - Test whether a device may be suspended.
0256  * @dev: Device to test.
0257  */
0258 static int rpm_check_suspend_allowed(struct device *dev)
0259 {
0260     int retval = 0;
0261 
0262     if (dev->power.runtime_error)
0263         retval = -EINVAL;
0264     else if (dev->power.disable_depth > 0)
0265         retval = -EACCES;
0266     else if (atomic_read(&dev->power.usage_count))
0267         retval = -EAGAIN;
0268     else if (!dev->power.ignore_children &&
0269             atomic_read(&dev->power.child_count))
0270         retval = -EBUSY;
0271 
0272     /* Pending resume requests take precedence over suspends. */
0273     else if ((dev->power.deferred_resume
0274             && dev->power.runtime_status == RPM_SUSPENDING)
0275         || (dev->power.request_pending
0276             && dev->power.request == RPM_REQ_RESUME))
0277         retval = -EAGAIN;
0278     else if (__dev_pm_qos_resume_latency(dev) == 0)
0279         retval = -EPERM;
0280     else if (dev->power.runtime_status == RPM_SUSPENDED)
0281         retval = 1;
0282 
0283     return retval;
0284 }
0285 
0286 static int rpm_get_suppliers(struct device *dev)
0287 {
0288     struct device_link *link;
0289 
0290     list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
0291                 device_links_read_lock_held()) {
0292         int retval;
0293 
0294         if (!(link->flags & DL_FLAG_PM_RUNTIME))
0295             continue;
0296 
0297         retval = pm_runtime_get_sync(link->supplier);
0298         /* Ignore suppliers with disabled runtime PM. */
0299         if (retval < 0 && retval != -EACCES) {
0300             pm_runtime_put_noidle(link->supplier);
0301             return retval;
0302         }
0303         refcount_inc(&link->rpm_active);
0304     }
0305     return 0;
0306 }
0307 
0308 /**
0309  * pm_runtime_release_supplier - Drop references to device link's supplier.
0310  * @link: Target device link.
0311  *
0312  * Drop all runtime PM references associated with @link to its supplier device.
0313  */
0314 void pm_runtime_release_supplier(struct device_link *link)
0315 {
0316     struct device *supplier = link->supplier;
0317 
0318     /*
0319      * The additional power.usage_count check is a safety net in case
0320      * the rpm_active refcount becomes saturated, in which case
0321      * refcount_dec_not_one() would return true forever, but it is not
0322      * strictly necessary.
0323      */
0324     while (refcount_dec_not_one(&link->rpm_active) &&
0325            atomic_read(&supplier->power.usage_count) > 0)
0326         pm_runtime_put_noidle(supplier);
0327 }
0328 
0329 static void __rpm_put_suppliers(struct device *dev, bool try_to_suspend)
0330 {
0331     struct device_link *link;
0332 
0333     list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
0334                 device_links_read_lock_held()) {
0335         pm_runtime_release_supplier(link);
0336         if (try_to_suspend)
0337             pm_request_idle(link->supplier);
0338     }
0339 }
0340 
0341 static void rpm_put_suppliers(struct device *dev)
0342 {
0343     __rpm_put_suppliers(dev, true);
0344 }
0345 
0346 static void rpm_suspend_suppliers(struct device *dev)
0347 {
0348     struct device_link *link;
0349     int idx = device_links_read_lock();
0350 
0351     list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
0352                 device_links_read_lock_held())
0353         pm_request_idle(link->supplier);
0354 
0355     device_links_read_unlock(idx);
0356 }
0357 
0358 /**
0359  * __rpm_callback - Run a given runtime PM callback for a given device.
0360  * @cb: Runtime PM callback to run.
0361  * @dev: Device to run the callback for.
0362  */
0363 static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
0364     __releases(&dev->power.lock) __acquires(&dev->power.lock)
0365 {
0366     int retval = 0, idx;
0367     bool use_links = dev->power.links_count > 0;
0368 
0369     if (dev->power.irq_safe) {
0370         spin_unlock(&dev->power.lock);
0371     } else {
0372         spin_unlock_irq(&dev->power.lock);
0373 
0374         /*
0375          * Resume suppliers if necessary.
0376          *
0377          * The device's runtime PM status cannot change until this
0378          * routine returns, so it is safe to read the status outside of
0379          * the lock.
0380          */
0381         if (use_links && dev->power.runtime_status == RPM_RESUMING) {
0382             idx = device_links_read_lock();
0383 
0384             retval = rpm_get_suppliers(dev);
0385             if (retval) {
0386                 rpm_put_suppliers(dev);
0387                 goto fail;
0388             }
0389 
0390             device_links_read_unlock(idx);
0391         }
0392     }
0393 
0394     if (cb)
0395         retval = cb(dev);
0396 
0397     if (dev->power.irq_safe) {
0398         spin_lock(&dev->power.lock);
0399     } else {
0400         /*
0401          * If the device is suspending and the callback has returned
0402          * success, drop the usage counters of the suppliers that have
0403          * been reference counted on its resume.
0404          *
0405          * Do that if resume fails too.
0406          */
0407         if (use_links
0408             && ((dev->power.runtime_status == RPM_SUSPENDING && !retval)
0409             || (dev->power.runtime_status == RPM_RESUMING && retval))) {
0410             idx = device_links_read_lock();
0411 
0412             __rpm_put_suppliers(dev, false);
0413 
0414 fail:
0415             device_links_read_unlock(idx);
0416         }
0417 
0418         spin_lock_irq(&dev->power.lock);
0419     }
0420 
0421     return retval;
0422 }
0423 
0424 /**
0425  * rpm_idle - Notify device bus type if the device can be suspended.
0426  * @dev: Device to notify the bus type about.
0427  * @rpmflags: Flag bits.
0428  *
0429  * Check if the device's runtime PM status allows it to be suspended.  If
0430  * another idle notification has been started earlier, return immediately.  If
0431  * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
0432  * run the ->runtime_idle() callback directly. If the ->runtime_idle callback
0433  * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag.
0434  *
0435  * This function must be called under dev->power.lock with interrupts disabled.
0436  */
0437 static int rpm_idle(struct device *dev, int rpmflags)
0438 {
0439     int (*callback)(struct device *);
0440     int retval;
0441 
0442     trace_rpm_idle_rcuidle(dev, rpmflags);
0443     retval = rpm_check_suspend_allowed(dev);
0444     if (retval < 0)
0445         ;   /* Conditions are wrong. */
0446 
0447     /* Idle notifications are allowed only in the RPM_ACTIVE state. */
0448     else if (dev->power.runtime_status != RPM_ACTIVE)
0449         retval = -EAGAIN;
0450 
0451     /*
0452      * Any pending request other than an idle notification takes
0453      * precedence over us, except that the timer may be running.
0454      */
0455     else if (dev->power.request_pending &&
0456         dev->power.request > RPM_REQ_IDLE)
0457         retval = -EAGAIN;
0458 
0459     /* Act as though RPM_NOWAIT is always set. */
0460     else if (dev->power.idle_notification)
0461         retval = -EINPROGRESS;
0462     if (retval)
0463         goto out;
0464 
0465     /* Pending requests need to be canceled. */
0466     dev->power.request = RPM_REQ_NONE;
0467 
0468     callback = RPM_GET_CALLBACK(dev, runtime_idle);
0469 
0470     /* If no callback assume success. */
0471     if (!callback || dev->power.no_callbacks)
0472         goto out;
0473 
0474     /* Carry out an asynchronous or a synchronous idle notification. */
0475     if (rpmflags & RPM_ASYNC) {
0476         dev->power.request = RPM_REQ_IDLE;
0477         if (!dev->power.request_pending) {
0478             dev->power.request_pending = true;
0479             queue_work(pm_wq, &dev->power.work);
0480         }
0481         trace_rpm_return_int_rcuidle(dev, _THIS_IP_, 0);
0482         return 0;
0483     }
0484 
0485     dev->power.idle_notification = true;
0486 
0487     retval = __rpm_callback(callback, dev);
0488 
0489     dev->power.idle_notification = false;
0490     wake_up_all(&dev->power.wait_queue);
0491 
0492  out:
0493     trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
0494     return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
0495 }
0496 
0497 /**
0498  * rpm_callback - Run a given runtime PM callback for a given device.
0499  * @cb: Runtime PM callback to run.
0500  * @dev: Device to run the callback for.
0501  */
0502 static int rpm_callback(int (*cb)(struct device *), struct device *dev)
0503 {
0504     int retval;
0505 
0506     if (dev->power.memalloc_noio) {
0507         unsigned int noio_flag;
0508 
0509         /*
0510          * Deadlock might be caused if memory allocation with
0511          * GFP_KERNEL happens inside runtime_suspend and
0512          * runtime_resume callbacks of one block device's
0513          * ancestor or the block device itself. Network
0514          * device might be thought as part of iSCSI block
0515          * device, so network device and its ancestor should
0516          * be marked as memalloc_noio too.
0517          */
0518         noio_flag = memalloc_noio_save();
0519         retval = __rpm_callback(cb, dev);
0520         memalloc_noio_restore(noio_flag);
0521     } else {
0522         retval = __rpm_callback(cb, dev);
0523     }
0524 
0525     dev->power.runtime_error = retval;
0526     return retval != -EACCES ? retval : -EIO;
0527 }
0528 
0529 /**
0530  * rpm_suspend - Carry out runtime suspend of given device.
0531  * @dev: Device to suspend.
0532  * @rpmflags: Flag bits.
0533  *
0534  * Check if the device's runtime PM status allows it to be suspended.
0535  * Cancel a pending idle notification, autosuspend or suspend. If
0536  * another suspend has been started earlier, either return immediately
0537  * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
0538  * flags. If the RPM_ASYNC flag is set then queue a suspend request;
0539  * otherwise run the ->runtime_suspend() callback directly. When
0540  * ->runtime_suspend succeeded, if a deferred resume was requested while
0541  * the callback was running then carry it out, otherwise send an idle
0542  * notification for its parent (if the suspend succeeded and both
0543  * ignore_children of parent->power and irq_safe of dev->power are not set).
0544  * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
0545  * flag is set and the next autosuspend-delay expiration time is in the
0546  * future, schedule another autosuspend attempt.
0547  *
0548  * This function must be called under dev->power.lock with interrupts disabled.
0549  */
0550 static int rpm_suspend(struct device *dev, int rpmflags)
0551     __releases(&dev->power.lock) __acquires(&dev->power.lock)
0552 {
0553     int (*callback)(struct device *);
0554     struct device *parent = NULL;
0555     int retval;
0556 
0557     trace_rpm_suspend_rcuidle(dev, rpmflags);
0558 
0559  repeat:
0560     retval = rpm_check_suspend_allowed(dev);
0561     if (retval < 0)
0562         goto out;   /* Conditions are wrong. */
0563 
0564     /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
0565     if (dev->power.runtime_status == RPM_RESUMING && !(rpmflags & RPM_ASYNC))
0566         retval = -EAGAIN;
0567     if (retval)
0568         goto out;
0569 
0570     /* If the autosuspend_delay time hasn't expired yet, reschedule. */
0571     if ((rpmflags & RPM_AUTO)
0572         && dev->power.runtime_status != RPM_SUSPENDING) {
0573         u64 expires = pm_runtime_autosuspend_expiration(dev);
0574 
0575         if (expires != 0) {
0576             /* Pending requests need to be canceled. */
0577             dev->power.request = RPM_REQ_NONE;
0578 
0579             /*
0580              * Optimization: If the timer is already running and is
0581              * set to expire at or before the autosuspend delay,
0582              * avoid the overhead of resetting it.  Just let it
0583              * expire; pm_suspend_timer_fn() will take care of the
0584              * rest.
0585              */
0586             if (!(dev->power.timer_expires &&
0587                     dev->power.timer_expires <= expires)) {
0588                 /*
0589                  * We add a slack of 25% to gather wakeups
0590                  * without sacrificing the granularity.
0591                  */
0592                 u64 slack = (u64)READ_ONCE(dev->power.autosuspend_delay) *
0593                             (NSEC_PER_MSEC >> 2);
0594 
0595                 dev->power.timer_expires = expires;
0596                 hrtimer_start_range_ns(&dev->power.suspend_timer,
0597                         ns_to_ktime(expires),
0598                         slack,
0599                         HRTIMER_MODE_ABS);
0600             }
0601             dev->power.timer_autosuspends = 1;
0602             goto out;
0603         }
0604     }
0605 
0606     /* Other scheduled or pending requests need to be canceled. */
0607     pm_runtime_cancel_pending(dev);
0608 
0609     if (dev->power.runtime_status == RPM_SUSPENDING) {
0610         DEFINE_WAIT(wait);
0611 
0612         if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
0613             retval = -EINPROGRESS;
0614             goto out;
0615         }
0616 
0617         if (dev->power.irq_safe) {
0618             spin_unlock(&dev->power.lock);
0619 
0620             cpu_relax();
0621 
0622             spin_lock(&dev->power.lock);
0623             goto repeat;
0624         }
0625 
0626         /* Wait for the other suspend running in parallel with us. */
0627         for (;;) {
0628             prepare_to_wait(&dev->power.wait_queue, &wait,
0629                     TASK_UNINTERRUPTIBLE);
0630             if (dev->power.runtime_status != RPM_SUSPENDING)
0631                 break;
0632 
0633             spin_unlock_irq(&dev->power.lock);
0634 
0635             schedule();
0636 
0637             spin_lock_irq(&dev->power.lock);
0638         }
0639         finish_wait(&dev->power.wait_queue, &wait);
0640         goto repeat;
0641     }
0642 
0643     if (dev->power.no_callbacks)
0644         goto no_callback;   /* Assume success. */
0645 
0646     /* Carry out an asynchronous or a synchronous suspend. */
0647     if (rpmflags & RPM_ASYNC) {
0648         dev->power.request = (rpmflags & RPM_AUTO) ?
0649             RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
0650         if (!dev->power.request_pending) {
0651             dev->power.request_pending = true;
0652             queue_work(pm_wq, &dev->power.work);
0653         }
0654         goto out;
0655     }
0656 
0657     __update_runtime_status(dev, RPM_SUSPENDING);
0658 
0659     callback = RPM_GET_CALLBACK(dev, runtime_suspend);
0660 
0661     dev_pm_enable_wake_irq_check(dev, true);
0662     retval = rpm_callback(callback, dev);
0663     if (retval)
0664         goto fail;
0665 
0666     dev_pm_enable_wake_irq_complete(dev);
0667 
0668  no_callback:
0669     __update_runtime_status(dev, RPM_SUSPENDED);
0670     pm_runtime_deactivate_timer(dev);
0671 
0672     if (dev->parent) {
0673         parent = dev->parent;
0674         atomic_add_unless(&parent->power.child_count, -1, 0);
0675     }
0676     wake_up_all(&dev->power.wait_queue);
0677 
0678     if (dev->power.deferred_resume) {
0679         dev->power.deferred_resume = false;
0680         rpm_resume(dev, 0);
0681         retval = -EAGAIN;
0682         goto out;
0683     }
0684 
0685     if (dev->power.irq_safe)
0686         goto out;
0687 
0688     /* Maybe the parent is now able to suspend. */
0689     if (parent && !parent->power.ignore_children) {
0690         spin_unlock(&dev->power.lock);
0691 
0692         spin_lock(&parent->power.lock);
0693         rpm_idle(parent, RPM_ASYNC);
0694         spin_unlock(&parent->power.lock);
0695 
0696         spin_lock(&dev->power.lock);
0697     }
0698     /* Maybe the suppliers are now able to suspend. */
0699     if (dev->power.links_count > 0) {
0700         spin_unlock_irq(&dev->power.lock);
0701 
0702         rpm_suspend_suppliers(dev);
0703 
0704         spin_lock_irq(&dev->power.lock);
0705     }
0706 
0707  out:
0708     trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
0709 
0710     return retval;
0711 
0712  fail:
0713     dev_pm_disable_wake_irq_check(dev, true);
0714     __update_runtime_status(dev, RPM_ACTIVE);
0715     dev->power.deferred_resume = false;
0716     wake_up_all(&dev->power.wait_queue);
0717 
0718     if (retval == -EAGAIN || retval == -EBUSY) {
0719         dev->power.runtime_error = 0;
0720 
0721         /*
0722          * If the callback routine failed an autosuspend, and
0723          * if the last_busy time has been updated so that there
0724          * is a new autosuspend expiration time, automatically
0725          * reschedule another autosuspend.
0726          */
0727         if ((rpmflags & RPM_AUTO) &&
0728             pm_runtime_autosuspend_expiration(dev) != 0)
0729             goto repeat;
0730     } else {
0731         pm_runtime_cancel_pending(dev);
0732     }
0733     goto out;
0734 }
0735 
0736 /**
0737  * rpm_resume - Carry out runtime resume of given device.
0738  * @dev: Device to resume.
0739  * @rpmflags: Flag bits.
0740  *
0741  * Check if the device's runtime PM status allows it to be resumed.  Cancel
0742  * any scheduled or pending requests.  If another resume has been started
0743  * earlier, either return immediately or wait for it to finish, depending on the
0744  * RPM_NOWAIT and RPM_ASYNC flags.  Similarly, if there's a suspend running in
0745  * parallel with this function, either tell the other process to resume after
0746  * suspending (deferred_resume) or wait for it to finish.  If the RPM_ASYNC
0747  * flag is set then queue a resume request; otherwise run the
0748  * ->runtime_resume() callback directly.  Queue an idle notification for the
0749  * device if the resume succeeded.
0750  *
0751  * This function must be called under dev->power.lock with interrupts disabled.
0752  */
0753 static int rpm_resume(struct device *dev, int rpmflags)
0754     __releases(&dev->power.lock) __acquires(&dev->power.lock)
0755 {
0756     int (*callback)(struct device *);
0757     struct device *parent = NULL;
0758     int retval = 0;
0759 
0760     trace_rpm_resume_rcuidle(dev, rpmflags);
0761 
0762  repeat:
0763     if (dev->power.runtime_error) {
0764         retval = -EINVAL;
0765     } else if (dev->power.disable_depth > 0) {
0766         if (dev->power.runtime_status == RPM_ACTIVE &&
0767             dev->power.last_status == RPM_ACTIVE)
0768             retval = 1;
0769         else
0770             retval = -EACCES;
0771     }
0772     if (retval)
0773         goto out;
0774 
0775     /*
0776      * Other scheduled or pending requests need to be canceled.  Small
0777      * optimization: If an autosuspend timer is running, leave it running
0778      * rather than cancelling it now only to restart it again in the near
0779      * future.
0780      */
0781     dev->power.request = RPM_REQ_NONE;
0782     if (!dev->power.timer_autosuspends)
0783         pm_runtime_deactivate_timer(dev);
0784 
0785     if (dev->power.runtime_status == RPM_ACTIVE) {
0786         retval = 1;
0787         goto out;
0788     }
0789 
0790     if (dev->power.runtime_status == RPM_RESUMING
0791         || dev->power.runtime_status == RPM_SUSPENDING) {
0792         DEFINE_WAIT(wait);
0793 
0794         if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
0795             if (dev->power.runtime_status == RPM_SUSPENDING)
0796                 dev->power.deferred_resume = true;
0797             else
0798                 retval = -EINPROGRESS;
0799             goto out;
0800         }
0801 
0802         if (dev->power.irq_safe) {
0803             spin_unlock(&dev->power.lock);
0804 
0805             cpu_relax();
0806 
0807             spin_lock(&dev->power.lock);
0808             goto repeat;
0809         }
0810 
0811         /* Wait for the operation carried out in parallel with us. */
0812         for (;;) {
0813             prepare_to_wait(&dev->power.wait_queue, &wait,
0814                     TASK_UNINTERRUPTIBLE);
0815             if (dev->power.runtime_status != RPM_RESUMING
0816                 && dev->power.runtime_status != RPM_SUSPENDING)
0817                 break;
0818 
0819             spin_unlock_irq(&dev->power.lock);
0820 
0821             schedule();
0822 
0823             spin_lock_irq(&dev->power.lock);
0824         }
0825         finish_wait(&dev->power.wait_queue, &wait);
0826         goto repeat;
0827     }
0828 
0829     /*
0830      * See if we can skip waking up the parent.  This is safe only if
0831      * power.no_callbacks is set, because otherwise we don't know whether
0832      * the resume will actually succeed.
0833      */
0834     if (dev->power.no_callbacks && !parent && dev->parent) {
0835         spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
0836         if (dev->parent->power.disable_depth > 0
0837             || dev->parent->power.ignore_children
0838             || dev->parent->power.runtime_status == RPM_ACTIVE) {
0839             atomic_inc(&dev->parent->power.child_count);
0840             spin_unlock(&dev->parent->power.lock);
0841             retval = 1;
0842             goto no_callback;   /* Assume success. */
0843         }
0844         spin_unlock(&dev->parent->power.lock);
0845     }
0846 
0847     /* Carry out an asynchronous or a synchronous resume. */
0848     if (rpmflags & RPM_ASYNC) {
0849         dev->power.request = RPM_REQ_RESUME;
0850         if (!dev->power.request_pending) {
0851             dev->power.request_pending = true;
0852             queue_work(pm_wq, &dev->power.work);
0853         }
0854         retval = 0;
0855         goto out;
0856     }
0857 
0858     if (!parent && dev->parent) {
0859         /*
0860          * Increment the parent's usage counter and resume it if
0861          * necessary.  Not needed if dev is irq-safe; then the
0862          * parent is permanently resumed.
0863          */
0864         parent = dev->parent;
0865         if (dev->power.irq_safe)
0866             goto skip_parent;
0867         spin_unlock(&dev->power.lock);
0868 
0869         pm_runtime_get_noresume(parent);
0870 
0871         spin_lock(&parent->power.lock);
0872         /*
0873          * Resume the parent if it has runtime PM enabled and not been
0874          * set to ignore its children.
0875          */
0876         if (!parent->power.disable_depth
0877             && !parent->power.ignore_children) {
0878             rpm_resume(parent, 0);
0879             if (parent->power.runtime_status != RPM_ACTIVE)
0880                 retval = -EBUSY;
0881         }
0882         spin_unlock(&parent->power.lock);
0883 
0884         spin_lock(&dev->power.lock);
0885         if (retval)
0886             goto out;
0887         goto repeat;
0888     }
0889  skip_parent:
0890 
0891     if (dev->power.no_callbacks)
0892         goto no_callback;   /* Assume success. */
0893 
0894     __update_runtime_status(dev, RPM_RESUMING);
0895 
0896     callback = RPM_GET_CALLBACK(dev, runtime_resume);
0897 
0898     dev_pm_disable_wake_irq_check(dev, false);
0899     retval = rpm_callback(callback, dev);
0900     if (retval) {
0901         __update_runtime_status(dev, RPM_SUSPENDED);
0902         pm_runtime_cancel_pending(dev);
0903         dev_pm_enable_wake_irq_check(dev, false);
0904     } else {
0905  no_callback:
0906         __update_runtime_status(dev, RPM_ACTIVE);
0907         pm_runtime_mark_last_busy(dev);
0908         if (parent)
0909             atomic_inc(&parent->power.child_count);
0910     }
0911     wake_up_all(&dev->power.wait_queue);
0912 
0913     if (retval >= 0)
0914         rpm_idle(dev, RPM_ASYNC);
0915 
0916  out:
0917     if (parent && !dev->power.irq_safe) {
0918         spin_unlock_irq(&dev->power.lock);
0919 
0920         pm_runtime_put(parent);
0921 
0922         spin_lock_irq(&dev->power.lock);
0923     }
0924 
0925     trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
0926 
0927     return retval;
0928 }
0929 
0930 /**
0931  * pm_runtime_work - Universal runtime PM work function.
0932  * @work: Work structure used for scheduling the execution of this function.
0933  *
0934  * Use @work to get the device object the work is to be done for, determine what
0935  * is to be done and execute the appropriate runtime PM function.
0936  */
0937 static void pm_runtime_work(struct work_struct *work)
0938 {
0939     struct device *dev = container_of(work, struct device, power.work);
0940     enum rpm_request req;
0941 
0942     spin_lock_irq(&dev->power.lock);
0943 
0944     if (!dev->power.request_pending)
0945         goto out;
0946 
0947     req = dev->power.request;
0948     dev->power.request = RPM_REQ_NONE;
0949     dev->power.request_pending = false;
0950 
0951     switch (req) {
0952     case RPM_REQ_NONE:
0953         break;
0954     case RPM_REQ_IDLE:
0955         rpm_idle(dev, RPM_NOWAIT);
0956         break;
0957     case RPM_REQ_SUSPEND:
0958         rpm_suspend(dev, RPM_NOWAIT);
0959         break;
0960     case RPM_REQ_AUTOSUSPEND:
0961         rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
0962         break;
0963     case RPM_REQ_RESUME:
0964         rpm_resume(dev, RPM_NOWAIT);
0965         break;
0966     }
0967 
0968  out:
0969     spin_unlock_irq(&dev->power.lock);
0970 }
0971 
0972 /**
0973  * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
0974  * @timer: hrtimer used by pm_schedule_suspend().
0975  *
0976  * Check if the time is right and queue a suspend request.
0977  */
0978 static enum hrtimer_restart  pm_suspend_timer_fn(struct hrtimer *timer)
0979 {
0980     struct device *dev = container_of(timer, struct device, power.suspend_timer);
0981     unsigned long flags;
0982     u64 expires;
0983 
0984     spin_lock_irqsave(&dev->power.lock, flags);
0985 
0986     expires = dev->power.timer_expires;
0987     /*
0988      * If 'expires' is after the current time, we've been called
0989      * too early.
0990      */
0991     if (expires > 0 && expires < ktime_get_mono_fast_ns()) {
0992         dev->power.timer_expires = 0;
0993         rpm_suspend(dev, dev->power.timer_autosuspends ?
0994             (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
0995     }
0996 
0997     spin_unlock_irqrestore(&dev->power.lock, flags);
0998 
0999     return HRTIMER_NORESTART;
1000 }
1001 
1002 /**
1003  * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
1004  * @dev: Device to suspend.
1005  * @delay: Time to wait before submitting a suspend request, in milliseconds.
1006  */
1007 int pm_schedule_suspend(struct device *dev, unsigned int delay)
1008 {
1009     unsigned long flags;
1010     u64 expires;
1011     int retval;
1012 
1013     spin_lock_irqsave(&dev->power.lock, flags);
1014 
1015     if (!delay) {
1016         retval = rpm_suspend(dev, RPM_ASYNC);
1017         goto out;
1018     }
1019 
1020     retval = rpm_check_suspend_allowed(dev);
1021     if (retval)
1022         goto out;
1023 
1024     /* Other scheduled or pending requests need to be canceled. */
1025     pm_runtime_cancel_pending(dev);
1026 
1027     expires = ktime_get_mono_fast_ns() + (u64)delay * NSEC_PER_MSEC;
1028     dev->power.timer_expires = expires;
1029     dev->power.timer_autosuspends = 0;
1030     hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS);
1031 
1032  out:
1033     spin_unlock_irqrestore(&dev->power.lock, flags);
1034 
1035     return retval;
1036 }
1037 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
1038 
1039 static int rpm_drop_usage_count(struct device *dev)
1040 {
1041     int ret;
1042 
1043     ret = atomic_sub_return(1, &dev->power.usage_count);
1044     if (ret >= 0)
1045         return ret;
1046 
1047     /*
1048      * Because rpm_resume() does not check the usage counter, it will resume
1049      * the device even if the usage counter is 0 or negative, so it is
1050      * sufficient to increment the usage counter here to reverse the change
1051      * made above.
1052      */
1053     atomic_inc(&dev->power.usage_count);
1054     dev_warn(dev, "Runtime PM usage count underflow!\n");
1055     return -EINVAL;
1056 }
1057 
1058 /**
1059  * __pm_runtime_idle - Entry point for runtime idle operations.
1060  * @dev: Device to send idle notification for.
1061  * @rpmflags: Flag bits.
1062  *
1063  * If the RPM_GET_PUT flag is set, decrement the device's usage count and
1064  * return immediately if it is larger than zero (if it becomes negative, log a
1065  * warning, increment it, and return an error).  Then carry out an idle
1066  * notification, either synchronous or asynchronous.
1067  *
1068  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1069  * or if pm_runtime_irq_safe() has been called.
1070  */
1071 int __pm_runtime_idle(struct device *dev, int rpmflags)
1072 {
1073     unsigned long flags;
1074     int retval;
1075 
1076     if (rpmflags & RPM_GET_PUT) {
1077         retval = rpm_drop_usage_count(dev);
1078         if (retval < 0) {
1079             return retval;
1080         } else if (retval > 0) {
1081             trace_rpm_usage_rcuidle(dev, rpmflags);
1082             return 0;
1083         }
1084     }
1085 
1086     might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
1087 
1088     spin_lock_irqsave(&dev->power.lock, flags);
1089     retval = rpm_idle(dev, rpmflags);
1090     spin_unlock_irqrestore(&dev->power.lock, flags);
1091 
1092     return retval;
1093 }
1094 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
1095 
1096 /**
1097  * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
1098  * @dev: Device to suspend.
1099  * @rpmflags: Flag bits.
1100  *
1101  * If the RPM_GET_PUT flag is set, decrement the device's usage count and
1102  * return immediately if it is larger than zero (if it becomes negative, log a
1103  * warning, increment it, and return an error).  Then carry out a suspend,
1104  * either synchronous or asynchronous.
1105  *
1106  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1107  * or if pm_runtime_irq_safe() has been called.
1108  */
1109 int __pm_runtime_suspend(struct device *dev, int rpmflags)
1110 {
1111     unsigned long flags;
1112     int retval;
1113 
1114     if (rpmflags & RPM_GET_PUT) {
1115         retval = rpm_drop_usage_count(dev);
1116         if (retval < 0) {
1117             return retval;
1118         } else if (retval > 0) {
1119             trace_rpm_usage_rcuidle(dev, rpmflags);
1120             return 0;
1121         }
1122     }
1123 
1124     might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
1125 
1126     spin_lock_irqsave(&dev->power.lock, flags);
1127     retval = rpm_suspend(dev, rpmflags);
1128     spin_unlock_irqrestore(&dev->power.lock, flags);
1129 
1130     return retval;
1131 }
1132 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
1133 
1134 /**
1135  * __pm_runtime_resume - Entry point for runtime resume operations.
1136  * @dev: Device to resume.
1137  * @rpmflags: Flag bits.
1138  *
1139  * If the RPM_GET_PUT flag is set, increment the device's usage count.  Then
1140  * carry out a resume, either synchronous or asynchronous.
1141  *
1142  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1143  * or if pm_runtime_irq_safe() has been called.
1144  */
1145 int __pm_runtime_resume(struct device *dev, int rpmflags)
1146 {
1147     unsigned long flags;
1148     int retval;
1149 
1150     might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe &&
1151             dev->power.runtime_status != RPM_ACTIVE);
1152 
1153     if (rpmflags & RPM_GET_PUT)
1154         atomic_inc(&dev->power.usage_count);
1155 
1156     spin_lock_irqsave(&dev->power.lock, flags);
1157     retval = rpm_resume(dev, rpmflags);
1158     spin_unlock_irqrestore(&dev->power.lock, flags);
1159 
1160     return retval;
1161 }
1162 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
1163 
1164 /**
1165  * pm_runtime_get_if_active - Conditionally bump up device usage counter.
1166  * @dev: Device to handle.
1167  * @ign_usage_count: Whether or not to look at the current usage counter value.
1168  *
1169  * Return -EINVAL if runtime PM is disabled for @dev.
1170  *
1171  * Otherwise, if the runtime PM status of @dev is %RPM_ACTIVE and either
1172  * @ign_usage_count is %true or the runtime PM usage counter of @dev is not
1173  * zero, increment the usage counter of @dev and return 1. Otherwise, return 0
1174  * without changing the usage counter.
1175  *
1176  * If @ign_usage_count is %true, this function can be used to prevent suspending
1177  * the device when its runtime PM status is %RPM_ACTIVE.
1178  *
1179  * If @ign_usage_count is %false, this function can be used to prevent
1180  * suspending the device when both its runtime PM status is %RPM_ACTIVE and its
1181  * runtime PM usage counter is not zero.
1182  *
1183  * The caller is responsible for decrementing the runtime PM usage counter of
1184  * @dev after this function has returned a positive value for it.
1185  */
1186 int pm_runtime_get_if_active(struct device *dev, bool ign_usage_count)
1187 {
1188     unsigned long flags;
1189     int retval;
1190 
1191     spin_lock_irqsave(&dev->power.lock, flags);
1192     if (dev->power.disable_depth > 0) {
1193         retval = -EINVAL;
1194     } else if (dev->power.runtime_status != RPM_ACTIVE) {
1195         retval = 0;
1196     } else if (ign_usage_count) {
1197         retval = 1;
1198         atomic_inc(&dev->power.usage_count);
1199     } else {
1200         retval = atomic_inc_not_zero(&dev->power.usage_count);
1201     }
1202     trace_rpm_usage_rcuidle(dev, 0);
1203     spin_unlock_irqrestore(&dev->power.lock, flags);
1204 
1205     return retval;
1206 }
1207 EXPORT_SYMBOL_GPL(pm_runtime_get_if_active);
1208 
1209 /**
1210  * __pm_runtime_set_status - Set runtime PM status of a device.
1211  * @dev: Device to handle.
1212  * @status: New runtime PM status of the device.
1213  *
1214  * If runtime PM of the device is disabled or its power.runtime_error field is
1215  * different from zero, the status may be changed either to RPM_ACTIVE, or to
1216  * RPM_SUSPENDED, as long as that reflects the actual state of the device.
1217  * However, if the device has a parent and the parent is not active, and the
1218  * parent's power.ignore_children flag is unset, the device's status cannot be
1219  * set to RPM_ACTIVE, so -EBUSY is returned in that case.
1220  *
1221  * If successful, __pm_runtime_set_status() clears the power.runtime_error field
1222  * and the device parent's counter of unsuspended children is modified to
1223  * reflect the new status.  If the new status is RPM_SUSPENDED, an idle
1224  * notification request for the parent is submitted.
1225  *
1226  * If @dev has any suppliers (as reflected by device links to them), and @status
1227  * is RPM_ACTIVE, they will be activated upfront and if the activation of one
1228  * of them fails, the status of @dev will be changed to RPM_SUSPENDED (instead
1229  * of the @status value) and the suppliers will be deacticated on exit.  The
1230  * error returned by the failing supplier activation will be returned in that
1231  * case.
1232  */
1233 int __pm_runtime_set_status(struct device *dev, unsigned int status)
1234 {
1235     struct device *parent = dev->parent;
1236     bool notify_parent = false;
1237     unsigned long flags;
1238     int error = 0;
1239 
1240     if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
1241         return -EINVAL;
1242 
1243     spin_lock_irqsave(&dev->power.lock, flags);
1244 
1245     /*
1246      * Prevent PM-runtime from being enabled for the device or return an
1247      * error if it is enabled already and working.
1248      */
1249     if (dev->power.runtime_error || dev->power.disable_depth)
1250         dev->power.disable_depth++;
1251     else
1252         error = -EAGAIN;
1253 
1254     spin_unlock_irqrestore(&dev->power.lock, flags);
1255 
1256     if (error)
1257         return error;
1258 
1259     /*
1260      * If the new status is RPM_ACTIVE, the suppliers can be activated
1261      * upfront regardless of the current status, because next time
1262      * rpm_put_suppliers() runs, the rpm_active refcounts of the links
1263      * involved will be dropped down to one anyway.
1264      */
1265     if (status == RPM_ACTIVE) {
1266         int idx = device_links_read_lock();
1267 
1268         error = rpm_get_suppliers(dev);
1269         if (error)
1270             status = RPM_SUSPENDED;
1271 
1272         device_links_read_unlock(idx);
1273     }
1274 
1275     spin_lock_irqsave(&dev->power.lock, flags);
1276 
1277     if (dev->power.runtime_status == status || !parent)
1278         goto out_set;
1279 
1280     if (status == RPM_SUSPENDED) {
1281         atomic_add_unless(&parent->power.child_count, -1, 0);
1282         notify_parent = !parent->power.ignore_children;
1283     } else {
1284         spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
1285 
1286         /*
1287          * It is invalid to put an active child under a parent that is
1288          * not active, has runtime PM enabled and the
1289          * 'power.ignore_children' flag unset.
1290          */
1291         if (!parent->power.disable_depth
1292             && !parent->power.ignore_children
1293             && parent->power.runtime_status != RPM_ACTIVE) {
1294             dev_err(dev, "runtime PM trying to activate child device %s but parent (%s) is not active\n",
1295                 dev_name(dev),
1296                 dev_name(parent));
1297             error = -EBUSY;
1298         } else if (dev->power.runtime_status == RPM_SUSPENDED) {
1299             atomic_inc(&parent->power.child_count);
1300         }
1301 
1302         spin_unlock(&parent->power.lock);
1303 
1304         if (error) {
1305             status = RPM_SUSPENDED;
1306             goto out;
1307         }
1308     }
1309 
1310  out_set:
1311     __update_runtime_status(dev, status);
1312     if (!error)
1313         dev->power.runtime_error = 0;
1314 
1315  out:
1316     spin_unlock_irqrestore(&dev->power.lock, flags);
1317 
1318     if (notify_parent)
1319         pm_request_idle(parent);
1320 
1321     if (status == RPM_SUSPENDED) {
1322         int idx = device_links_read_lock();
1323 
1324         rpm_put_suppliers(dev);
1325 
1326         device_links_read_unlock(idx);
1327     }
1328 
1329     pm_runtime_enable(dev);
1330 
1331     return error;
1332 }
1333 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1334 
1335 /**
1336  * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1337  * @dev: Device to handle.
1338  *
1339  * Flush all pending requests for the device from pm_wq and wait for all
1340  * runtime PM operations involving the device in progress to complete.
1341  *
1342  * Should be called under dev->power.lock with interrupts disabled.
1343  */
1344 static void __pm_runtime_barrier(struct device *dev)
1345 {
1346     pm_runtime_deactivate_timer(dev);
1347 
1348     if (dev->power.request_pending) {
1349         dev->power.request = RPM_REQ_NONE;
1350         spin_unlock_irq(&dev->power.lock);
1351 
1352         cancel_work_sync(&dev->power.work);
1353 
1354         spin_lock_irq(&dev->power.lock);
1355         dev->power.request_pending = false;
1356     }
1357 
1358     if (dev->power.runtime_status == RPM_SUSPENDING
1359         || dev->power.runtime_status == RPM_RESUMING
1360         || dev->power.idle_notification) {
1361         DEFINE_WAIT(wait);
1362 
1363         /* Suspend, wake-up or idle notification in progress. */
1364         for (;;) {
1365             prepare_to_wait(&dev->power.wait_queue, &wait,
1366                     TASK_UNINTERRUPTIBLE);
1367             if (dev->power.runtime_status != RPM_SUSPENDING
1368                 && dev->power.runtime_status != RPM_RESUMING
1369                 && !dev->power.idle_notification)
1370                 break;
1371             spin_unlock_irq(&dev->power.lock);
1372 
1373             schedule();
1374 
1375             spin_lock_irq(&dev->power.lock);
1376         }
1377         finish_wait(&dev->power.wait_queue, &wait);
1378     }
1379 }
1380 
1381 /**
1382  * pm_runtime_barrier - Flush pending requests and wait for completions.
1383  * @dev: Device to handle.
1384  *
1385  * Prevent the device from being suspended by incrementing its usage counter and
1386  * if there's a pending resume request for the device, wake the device up.
1387  * Next, make sure that all pending requests for the device have been flushed
1388  * from pm_wq and wait for all runtime PM operations involving the device in
1389  * progress to complete.
1390  *
1391  * Return value:
1392  * 1, if there was a resume request pending and the device had to be woken up,
1393  * 0, otherwise
1394  */
1395 int pm_runtime_barrier(struct device *dev)
1396 {
1397     int retval = 0;
1398 
1399     pm_runtime_get_noresume(dev);
1400     spin_lock_irq(&dev->power.lock);
1401 
1402     if (dev->power.request_pending
1403         && dev->power.request == RPM_REQ_RESUME) {
1404         rpm_resume(dev, 0);
1405         retval = 1;
1406     }
1407 
1408     __pm_runtime_barrier(dev);
1409 
1410     spin_unlock_irq(&dev->power.lock);
1411     pm_runtime_put_noidle(dev);
1412 
1413     return retval;
1414 }
1415 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1416 
1417 /**
1418  * __pm_runtime_disable - Disable runtime PM of a device.
1419  * @dev: Device to handle.
1420  * @check_resume: If set, check if there's a resume request for the device.
1421  *
1422  * Increment power.disable_depth for the device and if it was zero previously,
1423  * cancel all pending runtime PM requests for the device and wait for all
1424  * operations in progress to complete.  The device can be either active or
1425  * suspended after its runtime PM has been disabled.
1426  *
1427  * If @check_resume is set and there's a resume request pending when
1428  * __pm_runtime_disable() is called and power.disable_depth is zero, the
1429  * function will wake up the device before disabling its runtime PM.
1430  */
1431 void __pm_runtime_disable(struct device *dev, bool check_resume)
1432 {
1433     spin_lock_irq(&dev->power.lock);
1434 
1435     if (dev->power.disable_depth > 0) {
1436         dev->power.disable_depth++;
1437         goto out;
1438     }
1439 
1440     /*
1441      * Wake up the device if there's a resume request pending, because that
1442      * means there probably is some I/O to process and disabling runtime PM
1443      * shouldn't prevent the device from processing the I/O.
1444      */
1445     if (check_resume && dev->power.request_pending
1446         && dev->power.request == RPM_REQ_RESUME) {
1447         /*
1448          * Prevent suspends and idle notifications from being carried
1449          * out after we have woken up the device.
1450          */
1451         pm_runtime_get_noresume(dev);
1452 
1453         rpm_resume(dev, 0);
1454 
1455         pm_runtime_put_noidle(dev);
1456     }
1457 
1458     /* Update time accounting before disabling PM-runtime. */
1459     update_pm_runtime_accounting(dev);
1460 
1461     if (!dev->power.disable_depth++) {
1462         __pm_runtime_barrier(dev);
1463         dev->power.last_status = dev->power.runtime_status;
1464     }
1465 
1466  out:
1467     spin_unlock_irq(&dev->power.lock);
1468 }
1469 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1470 
1471 /**
1472  * pm_runtime_enable - Enable runtime PM of a device.
1473  * @dev: Device to handle.
1474  */
1475 void pm_runtime_enable(struct device *dev)
1476 {
1477     unsigned long flags;
1478 
1479     spin_lock_irqsave(&dev->power.lock, flags);
1480 
1481     if (!dev->power.disable_depth) {
1482         dev_warn(dev, "Unbalanced %s!\n", __func__);
1483         goto out;
1484     }
1485 
1486     if (--dev->power.disable_depth > 0)
1487         goto out;
1488 
1489     dev->power.last_status = RPM_INVALID;
1490     dev->power.accounting_timestamp = ktime_get_mono_fast_ns();
1491 
1492     if (dev->power.runtime_status == RPM_SUSPENDED &&
1493         !dev->power.ignore_children &&
1494         atomic_read(&dev->power.child_count) > 0)
1495         dev_warn(dev, "Enabling runtime PM for inactive device with active children\n");
1496 
1497 out:
1498     spin_unlock_irqrestore(&dev->power.lock, flags);
1499 }
1500 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1501 
1502 static void pm_runtime_disable_action(void *data)
1503 {
1504     pm_runtime_dont_use_autosuspend(data);
1505     pm_runtime_disable(data);
1506 }
1507 
1508 /**
1509  * devm_pm_runtime_enable - devres-enabled version of pm_runtime_enable.
1510  *
1511  * NOTE: this will also handle calling pm_runtime_dont_use_autosuspend() for
1512  * you at driver exit time if needed.
1513  *
1514  * @dev: Device to handle.
1515  */
1516 int devm_pm_runtime_enable(struct device *dev)
1517 {
1518     pm_runtime_enable(dev);
1519 
1520     return devm_add_action_or_reset(dev, pm_runtime_disable_action, dev);
1521 }
1522 EXPORT_SYMBOL_GPL(devm_pm_runtime_enable);
1523 
1524 /**
1525  * pm_runtime_forbid - Block runtime PM of a device.
1526  * @dev: Device to handle.
1527  *
1528  * Increase the device's usage count and clear its power.runtime_auto flag,
1529  * so that it cannot be suspended at run time until pm_runtime_allow() is called
1530  * for it.
1531  */
1532 void pm_runtime_forbid(struct device *dev)
1533 {
1534     spin_lock_irq(&dev->power.lock);
1535     if (!dev->power.runtime_auto)
1536         goto out;
1537 
1538     dev->power.runtime_auto = false;
1539     atomic_inc(&dev->power.usage_count);
1540     rpm_resume(dev, 0);
1541 
1542  out:
1543     spin_unlock_irq(&dev->power.lock);
1544 }
1545 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1546 
1547 /**
1548  * pm_runtime_allow - Unblock runtime PM of a device.
1549  * @dev: Device to handle.
1550  *
1551  * Decrease the device's usage count and set its power.runtime_auto flag.
1552  */
1553 void pm_runtime_allow(struct device *dev)
1554 {
1555     int ret;
1556 
1557     spin_lock_irq(&dev->power.lock);
1558     if (dev->power.runtime_auto)
1559         goto out;
1560 
1561     dev->power.runtime_auto = true;
1562     ret = rpm_drop_usage_count(dev);
1563     if (ret == 0)
1564         rpm_idle(dev, RPM_AUTO | RPM_ASYNC);
1565     else if (ret > 0)
1566         trace_rpm_usage_rcuidle(dev, RPM_AUTO | RPM_ASYNC);
1567 
1568  out:
1569     spin_unlock_irq(&dev->power.lock);
1570 }
1571 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1572 
1573 /**
1574  * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1575  * @dev: Device to handle.
1576  *
1577  * Set the power.no_callbacks flag, which tells the PM core that this
1578  * device is power-managed through its parent and has no runtime PM
1579  * callbacks of its own.  The runtime sysfs attributes will be removed.
1580  */
1581 void pm_runtime_no_callbacks(struct device *dev)
1582 {
1583     spin_lock_irq(&dev->power.lock);
1584     dev->power.no_callbacks = 1;
1585     spin_unlock_irq(&dev->power.lock);
1586     if (device_is_registered(dev))
1587         rpm_sysfs_remove(dev);
1588 }
1589 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1590 
1591 /**
1592  * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1593  * @dev: Device to handle
1594  *
1595  * Set the power.irq_safe flag, which tells the PM core that the
1596  * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1597  * always be invoked with the spinlock held and interrupts disabled.  It also
1598  * causes the parent's usage counter to be permanently incremented, preventing
1599  * the parent from runtime suspending -- otherwise an irq-safe child might have
1600  * to wait for a non-irq-safe parent.
1601  */
1602 void pm_runtime_irq_safe(struct device *dev)
1603 {
1604     if (dev->parent)
1605         pm_runtime_get_sync(dev->parent);
1606     spin_lock_irq(&dev->power.lock);
1607     dev->power.irq_safe = 1;
1608     spin_unlock_irq(&dev->power.lock);
1609 }
1610 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1611 
1612 /**
1613  * update_autosuspend - Handle a change to a device's autosuspend settings.
1614  * @dev: Device to handle.
1615  * @old_delay: The former autosuspend_delay value.
1616  * @old_use: The former use_autosuspend value.
1617  *
1618  * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1619  * set; otherwise allow it.  Send an idle notification if suspends are allowed.
1620  *
1621  * This function must be called under dev->power.lock with interrupts disabled.
1622  */
1623 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1624 {
1625     int delay = dev->power.autosuspend_delay;
1626 
1627     /* Should runtime suspend be prevented now? */
1628     if (dev->power.use_autosuspend && delay < 0) {
1629 
1630         /* If it used to be allowed then prevent it. */
1631         if (!old_use || old_delay >= 0) {
1632             atomic_inc(&dev->power.usage_count);
1633             rpm_resume(dev, 0);
1634         } else {
1635             trace_rpm_usage_rcuidle(dev, 0);
1636         }
1637     }
1638 
1639     /* Runtime suspend should be allowed now. */
1640     else {
1641 
1642         /* If it used to be prevented then allow it. */
1643         if (old_use && old_delay < 0)
1644             atomic_dec(&dev->power.usage_count);
1645 
1646         /* Maybe we can autosuspend now. */
1647         rpm_idle(dev, RPM_AUTO);
1648     }
1649 }
1650 
1651 /**
1652  * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1653  * @dev: Device to handle.
1654  * @delay: Value of the new delay in milliseconds.
1655  *
1656  * Set the device's power.autosuspend_delay value.  If it changes to negative
1657  * and the power.use_autosuspend flag is set, prevent runtime suspends.  If it
1658  * changes the other way, allow runtime suspends.
1659  */
1660 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1661 {
1662     int old_delay, old_use;
1663 
1664     spin_lock_irq(&dev->power.lock);
1665     old_delay = dev->power.autosuspend_delay;
1666     old_use = dev->power.use_autosuspend;
1667     dev->power.autosuspend_delay = delay;
1668     update_autosuspend(dev, old_delay, old_use);
1669     spin_unlock_irq(&dev->power.lock);
1670 }
1671 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1672 
1673 /**
1674  * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1675  * @dev: Device to handle.
1676  * @use: New value for use_autosuspend.
1677  *
1678  * Set the device's power.use_autosuspend flag, and allow or prevent runtime
1679  * suspends as needed.
1680  */
1681 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1682 {
1683     int old_delay, old_use;
1684 
1685     spin_lock_irq(&dev->power.lock);
1686     old_delay = dev->power.autosuspend_delay;
1687     old_use = dev->power.use_autosuspend;
1688     dev->power.use_autosuspend = use;
1689     update_autosuspend(dev, old_delay, old_use);
1690     spin_unlock_irq(&dev->power.lock);
1691 }
1692 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1693 
1694 /**
1695  * pm_runtime_init - Initialize runtime PM fields in given device object.
1696  * @dev: Device object to initialize.
1697  */
1698 void pm_runtime_init(struct device *dev)
1699 {
1700     dev->power.runtime_status = RPM_SUSPENDED;
1701     dev->power.last_status = RPM_INVALID;
1702     dev->power.idle_notification = false;
1703 
1704     dev->power.disable_depth = 1;
1705     atomic_set(&dev->power.usage_count, 0);
1706 
1707     dev->power.runtime_error = 0;
1708 
1709     atomic_set(&dev->power.child_count, 0);
1710     pm_suspend_ignore_children(dev, false);
1711     dev->power.runtime_auto = true;
1712 
1713     dev->power.request_pending = false;
1714     dev->power.request = RPM_REQ_NONE;
1715     dev->power.deferred_resume = false;
1716     dev->power.needs_force_resume = 0;
1717     INIT_WORK(&dev->power.work, pm_runtime_work);
1718 
1719     dev->power.timer_expires = 0;
1720     hrtimer_init(&dev->power.suspend_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1721     dev->power.suspend_timer.function = pm_suspend_timer_fn;
1722 
1723     init_waitqueue_head(&dev->power.wait_queue);
1724 }
1725 
1726 /**
1727  * pm_runtime_reinit - Re-initialize runtime PM fields in given device object.
1728  * @dev: Device object to re-initialize.
1729  */
1730 void pm_runtime_reinit(struct device *dev)
1731 {
1732     if (!pm_runtime_enabled(dev)) {
1733         if (dev->power.runtime_status == RPM_ACTIVE)
1734             pm_runtime_set_suspended(dev);
1735         if (dev->power.irq_safe) {
1736             spin_lock_irq(&dev->power.lock);
1737             dev->power.irq_safe = 0;
1738             spin_unlock_irq(&dev->power.lock);
1739             if (dev->parent)
1740                 pm_runtime_put(dev->parent);
1741         }
1742     }
1743 }
1744 
1745 /**
1746  * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1747  * @dev: Device object being removed from device hierarchy.
1748  */
1749 void pm_runtime_remove(struct device *dev)
1750 {
1751     __pm_runtime_disable(dev, false);
1752     pm_runtime_reinit(dev);
1753 }
1754 
1755 /**
1756  * pm_runtime_get_suppliers - Resume and reference-count supplier devices.
1757  * @dev: Consumer device.
1758  */
1759 void pm_runtime_get_suppliers(struct device *dev)
1760 {
1761     struct device_link *link;
1762     int idx;
1763 
1764     idx = device_links_read_lock();
1765 
1766     list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
1767                 device_links_read_lock_held())
1768         if (link->flags & DL_FLAG_PM_RUNTIME) {
1769             link->supplier_preactivated = true;
1770             pm_runtime_get_sync(link->supplier);
1771         }
1772 
1773     device_links_read_unlock(idx);
1774 }
1775 
1776 /**
1777  * pm_runtime_put_suppliers - Drop references to supplier devices.
1778  * @dev: Consumer device.
1779  */
1780 void pm_runtime_put_suppliers(struct device *dev)
1781 {
1782     struct device_link *link;
1783     int idx;
1784 
1785     idx = device_links_read_lock();
1786 
1787     list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
1788                 device_links_read_lock_held())
1789         if (link->supplier_preactivated) {
1790             link->supplier_preactivated = false;
1791             pm_runtime_put(link->supplier);
1792         }
1793 
1794     device_links_read_unlock(idx);
1795 }
1796 
1797 void pm_runtime_new_link(struct device *dev)
1798 {
1799     spin_lock_irq(&dev->power.lock);
1800     dev->power.links_count++;
1801     spin_unlock_irq(&dev->power.lock);
1802 }
1803 
1804 static void pm_runtime_drop_link_count(struct device *dev)
1805 {
1806     spin_lock_irq(&dev->power.lock);
1807     WARN_ON(dev->power.links_count == 0);
1808     dev->power.links_count--;
1809     spin_unlock_irq(&dev->power.lock);
1810 }
1811 
1812 /**
1813  * pm_runtime_drop_link - Prepare for device link removal.
1814  * @link: Device link going away.
1815  *
1816  * Drop the link count of the consumer end of @link and decrement the supplier
1817  * device's runtime PM usage counter as many times as needed to drop all of the
1818  * PM runtime reference to it from the consumer.
1819  */
1820 void pm_runtime_drop_link(struct device_link *link)
1821 {
1822     if (!(link->flags & DL_FLAG_PM_RUNTIME))
1823         return;
1824 
1825     pm_runtime_drop_link_count(link->consumer);
1826     pm_runtime_release_supplier(link);
1827     pm_request_idle(link->supplier);
1828 }
1829 
1830 static bool pm_runtime_need_not_resume(struct device *dev)
1831 {
1832     return atomic_read(&dev->power.usage_count) <= 1 &&
1833         (atomic_read(&dev->power.child_count) == 0 ||
1834          dev->power.ignore_children);
1835 }
1836 
1837 /**
1838  * pm_runtime_force_suspend - Force a device into suspend state if needed.
1839  * @dev: Device to suspend.
1840  *
1841  * Disable runtime PM so we safely can check the device's runtime PM status and
1842  * if it is active, invoke its ->runtime_suspend callback to suspend it and
1843  * change its runtime PM status field to RPM_SUSPENDED.  Also, if the device's
1844  * usage and children counters don't indicate that the device was in use before
1845  * the system-wide transition under way, decrement its parent's children counter
1846  * (if there is a parent).  Keep runtime PM disabled to preserve the state
1847  * unless we encounter errors.
1848  *
1849  * Typically this function may be invoked from a system suspend callback to make
1850  * sure the device is put into low power state and it should only be used during
1851  * system-wide PM transitions to sleep states.  It assumes that the analogous
1852  * pm_runtime_force_resume() will be used to resume the device.
1853  */
1854 int pm_runtime_force_suspend(struct device *dev)
1855 {
1856     int (*callback)(struct device *);
1857     int ret;
1858 
1859     pm_runtime_disable(dev);
1860     if (pm_runtime_status_suspended(dev))
1861         return 0;
1862 
1863     callback = RPM_GET_CALLBACK(dev, runtime_suspend);
1864 
1865     dev_pm_enable_wake_irq_check(dev, true);
1866     ret = callback ? callback(dev) : 0;
1867     if (ret)
1868         goto err;
1869 
1870     dev_pm_enable_wake_irq_complete(dev);
1871 
1872     /*
1873      * If the device can stay in suspend after the system-wide transition
1874      * to the working state that will follow, drop the children counter of
1875      * its parent, but set its status to RPM_SUSPENDED anyway in case this
1876      * function will be called again for it in the meantime.
1877      */
1878     if (pm_runtime_need_not_resume(dev)) {
1879         pm_runtime_set_suspended(dev);
1880     } else {
1881         __update_runtime_status(dev, RPM_SUSPENDED);
1882         dev->power.needs_force_resume = 1;
1883     }
1884 
1885     return 0;
1886 
1887 err:
1888     dev_pm_disable_wake_irq_check(dev, true);
1889     pm_runtime_enable(dev);
1890     return ret;
1891 }
1892 EXPORT_SYMBOL_GPL(pm_runtime_force_suspend);
1893 
1894 /**
1895  * pm_runtime_force_resume - Force a device into resume state if needed.
1896  * @dev: Device to resume.
1897  *
1898  * Prior invoking this function we expect the user to have brought the device
1899  * into low power state by a call to pm_runtime_force_suspend(). Here we reverse
1900  * those actions and bring the device into full power, if it is expected to be
1901  * used on system resume.  In the other case, we defer the resume to be managed
1902  * via runtime PM.
1903  *
1904  * Typically this function may be invoked from a system resume callback.
1905  */
1906 int pm_runtime_force_resume(struct device *dev)
1907 {
1908     int (*callback)(struct device *);
1909     int ret = 0;
1910 
1911     if (!pm_runtime_status_suspended(dev) || !dev->power.needs_force_resume)
1912         goto out;
1913 
1914     /*
1915      * The value of the parent's children counter is correct already, so
1916      * just update the status of the device.
1917      */
1918     __update_runtime_status(dev, RPM_ACTIVE);
1919 
1920     callback = RPM_GET_CALLBACK(dev, runtime_resume);
1921 
1922     dev_pm_disable_wake_irq_check(dev, false);
1923     ret = callback ? callback(dev) : 0;
1924     if (ret) {
1925         pm_runtime_set_suspended(dev);
1926         dev_pm_enable_wake_irq_check(dev, false);
1927         goto out;
1928     }
1929 
1930     pm_runtime_mark_last_busy(dev);
1931 out:
1932     dev->power.needs_force_resume = 0;
1933     pm_runtime_enable(dev);
1934     return ret;
1935 }
1936 EXPORT_SYMBOL_GPL(pm_runtime_force_resume);