GNU Linux-libre 5.10.153-gnu1
[releases.git] / drivers / base / power / runtime.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * drivers/base/power/runtime.c - Helper functions for device runtime PM
4  *
5  * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
6  * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
7  */
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>
15
16 #include "../base.h"
17 #include "power.h"
18
19 typedef int (*pm_callback_t)(struct device *);
20
21 static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset)
22 {
23         pm_callback_t cb;
24         const struct dev_pm_ops *ops;
25
26         if (dev->pm_domain)
27                 ops = &dev->pm_domain->ops;
28         else if (dev->type && dev->type->pm)
29                 ops = dev->type->pm;
30         else if (dev->class && dev->class->pm)
31                 ops = dev->class->pm;
32         else if (dev->bus && dev->bus->pm)
33                 ops = dev->bus->pm;
34         else
35                 ops = NULL;
36
37         if (ops)
38                 cb = *(pm_callback_t *)((void *)ops + cb_offset);
39         else
40                 cb = NULL;
41
42         if (!cb && dev->driver && dev->driver->pm)
43                 cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset);
44
45         return cb;
46 }
47
48 #define RPM_GET_CALLBACK(dev, callback) \
49                 __rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback))
50
51 static int rpm_resume(struct device *dev, int rpmflags);
52 static int rpm_suspend(struct device *dev, int rpmflags);
53
54 /**
55  * update_pm_runtime_accounting - Update the time accounting of power states
56  * @dev: Device to update the accounting for
57  *
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
63  * correctly.
64  */
65 static void update_pm_runtime_accounting(struct device *dev)
66 {
67         u64 now, last, delta;
68
69         if (dev->power.disable_depth > 0)
70                 return;
71
72         last = dev->power.accounting_timestamp;
73
74         now = ktime_get_mono_fast_ns();
75         dev->power.accounting_timestamp = now;
76
77         /*
78          * Because ktime_get_mono_fast_ns() is not monotonic during
79          * timekeeping updates, ensure that 'now' is after the last saved
80          * timesptamp.
81          */
82         if (now < last)
83                 return;
84
85         delta = now - last;
86
87         if (dev->power.runtime_status == RPM_SUSPENDED)
88                 dev->power.suspended_time += delta;
89         else
90                 dev->power.active_time += delta;
91 }
92
93 static void __update_runtime_status(struct device *dev, enum rpm_status status)
94 {
95         update_pm_runtime_accounting(dev);
96         dev->power.runtime_status = status;
97 }
98
99 static u64 rpm_get_accounted_time(struct device *dev, bool suspended)
100 {
101         u64 time;
102         unsigned long flags;
103
104         spin_lock_irqsave(&dev->power.lock, flags);
105
106         update_pm_runtime_accounting(dev);
107         time = suspended ? dev->power.suspended_time : dev->power.active_time;
108
109         spin_unlock_irqrestore(&dev->power.lock, flags);
110
111         return time;
112 }
113
114 u64 pm_runtime_active_time(struct device *dev)
115 {
116         return rpm_get_accounted_time(dev, false);
117 }
118
119 u64 pm_runtime_suspended_time(struct device *dev)
120 {
121         return rpm_get_accounted_time(dev, true);
122 }
123 EXPORT_SYMBOL_GPL(pm_runtime_suspended_time);
124
125 /**
126  * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
127  * @dev: Device to handle.
128  */
129 static void pm_runtime_deactivate_timer(struct device *dev)
130 {
131         if (dev->power.timer_expires > 0) {
132                 hrtimer_try_to_cancel(&dev->power.suspend_timer);
133                 dev->power.timer_expires = 0;
134         }
135 }
136
137 /**
138  * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
139  * @dev: Device to handle.
140  */
141 static void pm_runtime_cancel_pending(struct device *dev)
142 {
143         pm_runtime_deactivate_timer(dev);
144         /*
145          * In case there's a request pending, make sure its work function will
146          * return without doing anything.
147          */
148         dev->power.request = RPM_REQ_NONE;
149 }
150
151 /*
152  * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
153  * @dev: Device to handle.
154  *
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).
159  *
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.
162  */
163 u64 pm_runtime_autosuspend_expiration(struct device *dev)
164 {
165         int autosuspend_delay;
166         u64 expires;
167
168         if (!dev->power.use_autosuspend)
169                 return 0;
170
171         autosuspend_delay = READ_ONCE(dev->power.autosuspend_delay);
172         if (autosuspend_delay < 0)
173                 return 0;
174
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 */
179
180         return 0;
181 }
182 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
183
184 static int dev_memalloc_noio(struct device *dev, void *data)
185 {
186         return dev->power.memalloc_noio;
187 }
188
189 /*
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.
193  *
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.
197  *
198  * The function should only be called by block device, or network
199  * device driver for solving the deadlock problem during runtime
200  * resume/suspend:
201  *
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.
209  *
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
212  * in async probe().
213  *
214  * The function should be called between device_add() and device_del()
215  * on the affected device(block/network device).
216  */
217 void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
218 {
219         static DEFINE_MUTEX(dev_hotplug_mutex);
220
221         mutex_lock(&dev_hotplug_mutex);
222         for (;;) {
223                 bool enabled;
224
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);
230
231                 /*
232                  * not need to enable ancestors any more if the device
233                  * has been enabled.
234                  */
235                 if (enabled && enable)
236                         break;
237
238                 dev = dev->parent;
239
240                 /*
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.
244                  */
245                 if (!dev || (!enable &&
246                              device_for_each_child(dev, NULL,
247                                                    dev_memalloc_noio)))
248                         break;
249         }
250         mutex_unlock(&dev_hotplug_mutex);
251 }
252 EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
253
254 /**
255  * rpm_check_suspend_allowed - Test whether a device may be suspended.
256  * @dev: Device to test.
257  */
258 static int rpm_check_suspend_allowed(struct device *dev)
259 {
260         int retval = 0;
261
262         if (dev->power.runtime_error)
263                 retval = -EINVAL;
264         else if (dev->power.disable_depth > 0)
265                 retval = -EACCES;
266         else if (atomic_read(&dev->power.usage_count) > 0)
267                 retval = -EAGAIN;
268         else if (!dev->power.ignore_children &&
269                         atomic_read(&dev->power.child_count))
270                 retval = -EBUSY;
271
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))
277                 retval = -EAGAIN;
278         else if (__dev_pm_qos_resume_latency(dev) == 0)
279                 retval = -EPERM;
280         else if (dev->power.runtime_status == RPM_SUSPENDED)
281                 retval = 1;
282
283         return retval;
284 }
285
286 static int rpm_get_suppliers(struct device *dev)
287 {
288         struct device_link *link;
289
290         list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
291                                 device_links_read_lock_held()) {
292                 int retval;
293
294                 if (!(link->flags & DL_FLAG_PM_RUNTIME))
295                         continue;
296
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);
301                         return retval;
302                 }
303                 refcount_inc(&link->rpm_active);
304         }
305         return 0;
306 }
307
308 /**
309  * pm_runtime_release_supplier - Drop references to device link's supplier.
310  * @link: Target device link.
311  *
312  * Drop all runtime PM references associated with @link to its supplier device.
313  */
314 void pm_runtime_release_supplier(struct device_link *link)
315 {
316         struct device *supplier = link->supplier;
317
318         /*
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.
323          */
324         while (refcount_dec_not_one(&link->rpm_active) &&
325                atomic_read(&supplier->power.usage_count) > 0)
326                 pm_runtime_put_noidle(supplier);
327 }
328
329 static void __rpm_put_suppliers(struct device *dev, bool try_to_suspend)
330 {
331         struct device_link *link;
332
333         list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
334                                 device_links_read_lock_held()) {
335                 pm_runtime_release_supplier(link);
336                 if (try_to_suspend)
337                         pm_request_idle(link->supplier);
338         }
339 }
340
341 static void rpm_put_suppliers(struct device *dev)
342 {
343         __rpm_put_suppliers(dev, true);
344 }
345
346 static void rpm_suspend_suppliers(struct device *dev)
347 {
348         struct device_link *link;
349         int idx = device_links_read_lock();
350
351         list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
352                                 device_links_read_lock_held())
353                 pm_request_idle(link->supplier);
354
355         device_links_read_unlock(idx);
356 }
357
358 /**
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.
362  */
363 static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
364         __releases(&dev->power.lock) __acquires(&dev->power.lock)
365 {
366         int retval, idx;
367         bool use_links = dev->power.links_count > 0;
368
369         if (dev->power.irq_safe) {
370                 spin_unlock(&dev->power.lock);
371         } else {
372                 spin_unlock_irq(&dev->power.lock);
373
374                 /*
375                  * Resume suppliers if necessary.
376                  *
377                  * The device's runtime PM status cannot change until this
378                  * routine returns, so it is safe to read the status outside of
379                  * the lock.
380                  */
381                 if (use_links && dev->power.runtime_status == RPM_RESUMING) {
382                         idx = device_links_read_lock();
383
384                         retval = rpm_get_suppliers(dev);
385                         if (retval) {
386                                 rpm_put_suppliers(dev);
387                                 goto fail;
388                         }
389
390                         device_links_read_unlock(idx);
391                 }
392         }
393
394         retval = cb(dev);
395
396         if (dev->power.irq_safe) {
397                 spin_lock(&dev->power.lock);
398         } else {
399                 /*
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.
403                  *
404                  * Do that if resume fails too.
405                  */
406                 if (use_links
407                     && ((dev->power.runtime_status == RPM_SUSPENDING && !retval)
408                     || (dev->power.runtime_status == RPM_RESUMING && retval))) {
409                         idx = device_links_read_lock();
410
411                         __rpm_put_suppliers(dev, false);
412
413 fail:
414                         device_links_read_unlock(idx);
415                 }
416
417                 spin_lock_irq(&dev->power.lock);
418         }
419
420         return retval;
421 }
422
423 /**
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.
427  *
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.
433  *
434  * This function must be called under dev->power.lock with interrupts disabled.
435  */
436 static int rpm_idle(struct device *dev, int rpmflags)
437 {
438         int (*callback)(struct device *);
439         int retval;
440
441         trace_rpm_idle_rcuidle(dev, rpmflags);
442         retval = rpm_check_suspend_allowed(dev);
443         if (retval < 0)
444                 ;       /* Conditions are wrong. */
445
446         /* Idle notifications are allowed only in the RPM_ACTIVE state. */
447         else if (dev->power.runtime_status != RPM_ACTIVE)
448                 retval = -EAGAIN;
449
450         /*
451          * Any pending request other than an idle notification takes
452          * precedence over us, except that the timer may be running.
453          */
454         else if (dev->power.request_pending &&
455             dev->power.request > RPM_REQ_IDLE)
456                 retval = -EAGAIN;
457
458         /* Act as though RPM_NOWAIT is always set. */
459         else if (dev->power.idle_notification)
460                 retval = -EINPROGRESS;
461         if (retval)
462                 goto out;
463
464         /* Pending requests need to be canceled. */
465         dev->power.request = RPM_REQ_NONE;
466
467         if (dev->power.no_callbacks)
468                 goto out;
469
470         /* Carry out an asynchronous or a synchronous idle notification. */
471         if (rpmflags & RPM_ASYNC) {
472                 dev->power.request = RPM_REQ_IDLE;
473                 if (!dev->power.request_pending) {
474                         dev->power.request_pending = true;
475                         queue_work(pm_wq, &dev->power.work);
476                 }
477                 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, 0);
478                 return 0;
479         }
480
481         dev->power.idle_notification = true;
482
483         callback = RPM_GET_CALLBACK(dev, runtime_idle);
484
485         if (callback)
486                 retval = __rpm_callback(callback, dev);
487
488         dev->power.idle_notification = false;
489         wake_up_all(&dev->power.wait_queue);
490
491  out:
492         trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
493         return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
494 }
495
496 /**
497  * rpm_callback - Run a given runtime PM callback for a given device.
498  * @cb: Runtime PM callback to run.
499  * @dev: Device to run the callback for.
500  */
501 static int rpm_callback(int (*cb)(struct device *), struct device *dev)
502 {
503         int retval;
504
505         if (!cb)
506                 return -ENOSYS;
507
508         if (dev->power.memalloc_noio) {
509                 unsigned int noio_flag;
510
511                 /*
512                  * Deadlock might be caused if memory allocation with
513                  * GFP_KERNEL happens inside runtime_suspend and
514                  * runtime_resume callbacks of one block device's
515                  * ancestor or the block device itself. Network
516                  * device might be thought as part of iSCSI block
517                  * device, so network device and its ancestor should
518                  * be marked as memalloc_noio too.
519                  */
520                 noio_flag = memalloc_noio_save();
521                 retval = __rpm_callback(cb, dev);
522                 memalloc_noio_restore(noio_flag);
523         } else {
524                 retval = __rpm_callback(cb, dev);
525         }
526
527         dev->power.runtime_error = retval;
528         return retval != -EACCES ? retval : -EIO;
529 }
530
531 /**
532  * rpm_suspend - Carry out runtime suspend of given device.
533  * @dev: Device to suspend.
534  * @rpmflags: Flag bits.
535  *
536  * Check if the device's runtime PM status allows it to be suspended.
537  * Cancel a pending idle notification, autosuspend or suspend. If
538  * another suspend has been started earlier, either return immediately
539  * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
540  * flags. If the RPM_ASYNC flag is set then queue a suspend request;
541  * otherwise run the ->runtime_suspend() callback directly. When
542  * ->runtime_suspend succeeded, if a deferred resume was requested while
543  * the callback was running then carry it out, otherwise send an idle
544  * notification for its parent (if the suspend succeeded and both
545  * ignore_children of parent->power and irq_safe of dev->power are not set).
546  * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
547  * flag is set and the next autosuspend-delay expiration time is in the
548  * future, schedule another autosuspend attempt.
549  *
550  * This function must be called under dev->power.lock with interrupts disabled.
551  */
552 static int rpm_suspend(struct device *dev, int rpmflags)
553         __releases(&dev->power.lock) __acquires(&dev->power.lock)
554 {
555         int (*callback)(struct device *);
556         struct device *parent = NULL;
557         int retval;
558
559         trace_rpm_suspend_rcuidle(dev, rpmflags);
560
561  repeat:
562         retval = rpm_check_suspend_allowed(dev);
563         if (retval < 0)
564                 goto out;       /* Conditions are wrong. */
565
566         /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
567         if (dev->power.runtime_status == RPM_RESUMING && !(rpmflags & RPM_ASYNC))
568                 retval = -EAGAIN;
569         if (retval)
570                 goto out;
571
572         /* If the autosuspend_delay time hasn't expired yet, reschedule. */
573         if ((rpmflags & RPM_AUTO)
574             && dev->power.runtime_status != RPM_SUSPENDING) {
575                 u64 expires = pm_runtime_autosuspend_expiration(dev);
576
577                 if (expires != 0) {
578                         /* Pending requests need to be canceled. */
579                         dev->power.request = RPM_REQ_NONE;
580
581                         /*
582                          * Optimization: If the timer is already running and is
583                          * set to expire at or before the autosuspend delay,
584                          * avoid the overhead of resetting it.  Just let it
585                          * expire; pm_suspend_timer_fn() will take care of the
586                          * rest.
587                          */
588                         if (!(dev->power.timer_expires &&
589                                         dev->power.timer_expires <= expires)) {
590                                 /*
591                                  * We add a slack of 25% to gather wakeups
592                                  * without sacrificing the granularity.
593                                  */
594                                 u64 slack = (u64)READ_ONCE(dev->power.autosuspend_delay) *
595                                                     (NSEC_PER_MSEC >> 2);
596
597                                 dev->power.timer_expires = expires;
598                                 hrtimer_start_range_ns(&dev->power.suspend_timer,
599                                                 ns_to_ktime(expires),
600                                                 slack,
601                                                 HRTIMER_MODE_ABS);
602                         }
603                         dev->power.timer_autosuspends = 1;
604                         goto out;
605                 }
606         }
607
608         /* Other scheduled or pending requests need to be canceled. */
609         pm_runtime_cancel_pending(dev);
610
611         if (dev->power.runtime_status == RPM_SUSPENDING) {
612                 DEFINE_WAIT(wait);
613
614                 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
615                         retval = -EINPROGRESS;
616                         goto out;
617                 }
618
619                 if (dev->power.irq_safe) {
620                         spin_unlock(&dev->power.lock);
621
622                         cpu_relax();
623
624                         spin_lock(&dev->power.lock);
625                         goto repeat;
626                 }
627
628                 /* Wait for the other suspend running in parallel with us. */
629                 for (;;) {
630                         prepare_to_wait(&dev->power.wait_queue, &wait,
631                                         TASK_UNINTERRUPTIBLE);
632                         if (dev->power.runtime_status != RPM_SUSPENDING)
633                                 break;
634
635                         spin_unlock_irq(&dev->power.lock);
636
637                         schedule();
638
639                         spin_lock_irq(&dev->power.lock);
640                 }
641                 finish_wait(&dev->power.wait_queue, &wait);
642                 goto repeat;
643         }
644
645         if (dev->power.no_callbacks)
646                 goto no_callback;       /* Assume success. */
647
648         /* Carry out an asynchronous or a synchronous suspend. */
649         if (rpmflags & RPM_ASYNC) {
650                 dev->power.request = (rpmflags & RPM_AUTO) ?
651                     RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
652                 if (!dev->power.request_pending) {
653                         dev->power.request_pending = true;
654                         queue_work(pm_wq, &dev->power.work);
655                 }
656                 goto out;
657         }
658
659         __update_runtime_status(dev, RPM_SUSPENDING);
660
661         callback = RPM_GET_CALLBACK(dev, runtime_suspend);
662
663         dev_pm_enable_wake_irq_check(dev, true);
664         retval = rpm_callback(callback, dev);
665         if (retval)
666                 goto fail;
667
668  no_callback:
669         __update_runtime_status(dev, RPM_SUSPENDED);
670         pm_runtime_deactivate_timer(dev);
671
672         if (dev->parent) {
673                 parent = dev->parent;
674                 atomic_add_unless(&parent->power.child_count, -1, 0);
675         }
676         wake_up_all(&dev->power.wait_queue);
677
678         if (dev->power.deferred_resume) {
679                 dev->power.deferred_resume = false;
680                 rpm_resume(dev, 0);
681                 retval = -EAGAIN;
682                 goto out;
683         }
684
685         if (dev->power.irq_safe)
686                 goto out;
687
688         /* Maybe the parent is now able to suspend. */
689         if (parent && !parent->power.ignore_children) {
690                 spin_unlock(&dev->power.lock);
691
692                 spin_lock(&parent->power.lock);
693                 rpm_idle(parent, RPM_ASYNC);
694                 spin_unlock(&parent->power.lock);
695
696                 spin_lock(&dev->power.lock);
697         }
698         /* Maybe the suppliers are now able to suspend. */
699         if (dev->power.links_count > 0) {
700                 spin_unlock_irq(&dev->power.lock);
701
702                 rpm_suspend_suppliers(dev);
703
704                 spin_lock_irq(&dev->power.lock);
705         }
706
707  out:
708         trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
709
710         return retval;
711
712  fail:
713         dev_pm_disable_wake_irq_check(dev);
714         __update_runtime_status(dev, RPM_ACTIVE);
715         dev->power.deferred_resume = false;
716         wake_up_all(&dev->power.wait_queue);
717
718         if (retval == -EAGAIN || retval == -EBUSY) {
719                 dev->power.runtime_error = 0;
720
721                 /*
722                  * If the callback routine failed an autosuspend, and
723                  * if the last_busy time has been updated so that there
724                  * is a new autosuspend expiration time, automatically
725                  * reschedule another autosuspend.
726                  */
727                 if ((rpmflags & RPM_AUTO) &&
728                     pm_runtime_autosuspend_expiration(dev) != 0)
729                         goto repeat;
730         } else {
731                 pm_runtime_cancel_pending(dev);
732         }
733         goto out;
734 }
735
736 /**
737  * rpm_resume - Carry out runtime resume of given device.
738  * @dev: Device to resume.
739  * @rpmflags: Flag bits.
740  *
741  * Check if the device's runtime PM status allows it to be resumed.  Cancel
742  * any scheduled or pending requests.  If another resume has been started
743  * earlier, either return immediately or wait for it to finish, depending on the
744  * RPM_NOWAIT and RPM_ASYNC flags.  Similarly, if there's a suspend running in
745  * parallel with this function, either tell the other process to resume after
746  * suspending (deferred_resume) or wait for it to finish.  If the RPM_ASYNC
747  * flag is set then queue a resume request; otherwise run the
748  * ->runtime_resume() callback directly.  Queue an idle notification for the
749  * device if the resume succeeded.
750  *
751  * This function must be called under dev->power.lock with interrupts disabled.
752  */
753 static int rpm_resume(struct device *dev, int rpmflags)
754         __releases(&dev->power.lock) __acquires(&dev->power.lock)
755 {
756         int (*callback)(struct device *);
757         struct device *parent = NULL;
758         int retval = 0;
759
760         trace_rpm_resume_rcuidle(dev, rpmflags);
761
762  repeat:
763         if (dev->power.runtime_error)
764                 retval = -EINVAL;
765         else if (dev->power.disable_depth == 1 && dev->power.is_suspended
766             && dev->power.runtime_status == RPM_ACTIVE)
767                 retval = 1;
768         else if (dev->power.disable_depth > 0)
769                 retval = -EACCES;
770         if (retval)
771                 goto out;
772
773         /*
774          * Other scheduled or pending requests need to be canceled.  Small
775          * optimization: If an autosuspend timer is running, leave it running
776          * rather than cancelling it now only to restart it again in the near
777          * future.
778          */
779         dev->power.request = RPM_REQ_NONE;
780         if (!dev->power.timer_autosuspends)
781                 pm_runtime_deactivate_timer(dev);
782
783         if (dev->power.runtime_status == RPM_ACTIVE) {
784                 retval = 1;
785                 goto out;
786         }
787
788         if (dev->power.runtime_status == RPM_RESUMING
789             || dev->power.runtime_status == RPM_SUSPENDING) {
790                 DEFINE_WAIT(wait);
791
792                 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
793                         if (dev->power.runtime_status == RPM_SUSPENDING)
794                                 dev->power.deferred_resume = true;
795                         else
796                                 retval = -EINPROGRESS;
797                         goto out;
798                 }
799
800                 if (dev->power.irq_safe) {
801                         spin_unlock(&dev->power.lock);
802
803                         cpu_relax();
804
805                         spin_lock(&dev->power.lock);
806                         goto repeat;
807                 }
808
809                 /* Wait for the operation carried out in parallel with us. */
810                 for (;;) {
811                         prepare_to_wait(&dev->power.wait_queue, &wait,
812                                         TASK_UNINTERRUPTIBLE);
813                         if (dev->power.runtime_status != RPM_RESUMING
814                             && dev->power.runtime_status != RPM_SUSPENDING)
815                                 break;
816
817                         spin_unlock_irq(&dev->power.lock);
818
819                         schedule();
820
821                         spin_lock_irq(&dev->power.lock);
822                 }
823                 finish_wait(&dev->power.wait_queue, &wait);
824                 goto repeat;
825         }
826
827         /*
828          * See if we can skip waking up the parent.  This is safe only if
829          * power.no_callbacks is set, because otherwise we don't know whether
830          * the resume will actually succeed.
831          */
832         if (dev->power.no_callbacks && !parent && dev->parent) {
833                 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
834                 if (dev->parent->power.disable_depth > 0
835                     || dev->parent->power.ignore_children
836                     || dev->parent->power.runtime_status == RPM_ACTIVE) {
837                         atomic_inc(&dev->parent->power.child_count);
838                         spin_unlock(&dev->parent->power.lock);
839                         retval = 1;
840                         goto no_callback;       /* Assume success. */
841                 }
842                 spin_unlock(&dev->parent->power.lock);
843         }
844
845         /* Carry out an asynchronous or a synchronous resume. */
846         if (rpmflags & RPM_ASYNC) {
847                 dev->power.request = RPM_REQ_RESUME;
848                 if (!dev->power.request_pending) {
849                         dev->power.request_pending = true;
850                         queue_work(pm_wq, &dev->power.work);
851                 }
852                 retval = 0;
853                 goto out;
854         }
855
856         if (!parent && dev->parent) {
857                 /*
858                  * Increment the parent's usage counter and resume it if
859                  * necessary.  Not needed if dev is irq-safe; then the
860                  * parent is permanently resumed.
861                  */
862                 parent = dev->parent;
863                 if (dev->power.irq_safe)
864                         goto skip_parent;
865                 spin_unlock(&dev->power.lock);
866
867                 pm_runtime_get_noresume(parent);
868
869                 spin_lock(&parent->power.lock);
870                 /*
871                  * Resume the parent if it has runtime PM enabled and not been
872                  * set to ignore its children.
873                  */
874                 if (!parent->power.disable_depth
875                     && !parent->power.ignore_children) {
876                         rpm_resume(parent, 0);
877                         if (parent->power.runtime_status != RPM_ACTIVE)
878                                 retval = -EBUSY;
879                 }
880                 spin_unlock(&parent->power.lock);
881
882                 spin_lock(&dev->power.lock);
883                 if (retval)
884                         goto out;
885                 goto repeat;
886         }
887  skip_parent:
888
889         if (dev->power.no_callbacks)
890                 goto no_callback;       /* Assume success. */
891
892         __update_runtime_status(dev, RPM_RESUMING);
893
894         callback = RPM_GET_CALLBACK(dev, runtime_resume);
895
896         dev_pm_disable_wake_irq_check(dev);
897         retval = rpm_callback(callback, dev);
898         if (retval) {
899                 __update_runtime_status(dev, RPM_SUSPENDED);
900                 pm_runtime_cancel_pending(dev);
901                 dev_pm_enable_wake_irq_check(dev, false);
902         } else {
903  no_callback:
904                 __update_runtime_status(dev, RPM_ACTIVE);
905                 pm_runtime_mark_last_busy(dev);
906                 if (parent)
907                         atomic_inc(&parent->power.child_count);
908         }
909         wake_up_all(&dev->power.wait_queue);
910
911         if (retval >= 0)
912                 rpm_idle(dev, RPM_ASYNC);
913
914  out:
915         if (parent && !dev->power.irq_safe) {
916                 spin_unlock_irq(&dev->power.lock);
917
918                 pm_runtime_put(parent);
919
920                 spin_lock_irq(&dev->power.lock);
921         }
922
923         trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
924
925         return retval;
926 }
927
928 /**
929  * pm_runtime_work - Universal runtime PM work function.
930  * @work: Work structure used for scheduling the execution of this function.
931  *
932  * Use @work to get the device object the work is to be done for, determine what
933  * is to be done and execute the appropriate runtime PM function.
934  */
935 static void pm_runtime_work(struct work_struct *work)
936 {
937         struct device *dev = container_of(work, struct device, power.work);
938         enum rpm_request req;
939
940         spin_lock_irq(&dev->power.lock);
941
942         if (!dev->power.request_pending)
943                 goto out;
944
945         req = dev->power.request;
946         dev->power.request = RPM_REQ_NONE;
947         dev->power.request_pending = false;
948
949         switch (req) {
950         case RPM_REQ_NONE:
951                 break;
952         case RPM_REQ_IDLE:
953                 rpm_idle(dev, RPM_NOWAIT);
954                 break;
955         case RPM_REQ_SUSPEND:
956                 rpm_suspend(dev, RPM_NOWAIT);
957                 break;
958         case RPM_REQ_AUTOSUSPEND:
959                 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
960                 break;
961         case RPM_REQ_RESUME:
962                 rpm_resume(dev, RPM_NOWAIT);
963                 break;
964         }
965
966  out:
967         spin_unlock_irq(&dev->power.lock);
968 }
969
970 /**
971  * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
972  * @data: Device pointer passed by pm_schedule_suspend().
973  *
974  * Check if the time is right and queue a suspend request.
975  */
976 static enum hrtimer_restart  pm_suspend_timer_fn(struct hrtimer *timer)
977 {
978         struct device *dev = container_of(timer, struct device, power.suspend_timer);
979         unsigned long flags;
980         u64 expires;
981
982         spin_lock_irqsave(&dev->power.lock, flags);
983
984         expires = dev->power.timer_expires;
985         /*
986          * If 'expires' is after the current time, we've been called
987          * too early.
988          */
989         if (expires > 0 && expires < ktime_get_mono_fast_ns()) {
990                 dev->power.timer_expires = 0;
991                 rpm_suspend(dev, dev->power.timer_autosuspends ?
992                     (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
993         }
994
995         spin_unlock_irqrestore(&dev->power.lock, flags);
996
997         return HRTIMER_NORESTART;
998 }
999
1000 /**
1001  * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
1002  * @dev: Device to suspend.
1003  * @delay: Time to wait before submitting a suspend request, in milliseconds.
1004  */
1005 int pm_schedule_suspend(struct device *dev, unsigned int delay)
1006 {
1007         unsigned long flags;
1008         u64 expires;
1009         int retval;
1010
1011         spin_lock_irqsave(&dev->power.lock, flags);
1012
1013         if (!delay) {
1014                 retval = rpm_suspend(dev, RPM_ASYNC);
1015                 goto out;
1016         }
1017
1018         retval = rpm_check_suspend_allowed(dev);
1019         if (retval)
1020                 goto out;
1021
1022         /* Other scheduled or pending requests need to be canceled. */
1023         pm_runtime_cancel_pending(dev);
1024
1025         expires = ktime_get_mono_fast_ns() + (u64)delay * NSEC_PER_MSEC;
1026         dev->power.timer_expires = expires;
1027         dev->power.timer_autosuspends = 0;
1028         hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS);
1029
1030  out:
1031         spin_unlock_irqrestore(&dev->power.lock, flags);
1032
1033         return retval;
1034 }
1035 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
1036
1037 /**
1038  * __pm_runtime_idle - Entry point for runtime idle operations.
1039  * @dev: Device to send idle notification for.
1040  * @rpmflags: Flag bits.
1041  *
1042  * If the RPM_GET_PUT flag is set, decrement the device's usage count and
1043  * return immediately if it is larger than zero.  Then carry out an idle
1044  * notification, either synchronous or asynchronous.
1045  *
1046  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1047  * or if pm_runtime_irq_safe() has been called.
1048  */
1049 int __pm_runtime_idle(struct device *dev, int rpmflags)
1050 {
1051         unsigned long flags;
1052         int retval;
1053
1054         if (rpmflags & RPM_GET_PUT) {
1055                 if (!atomic_dec_and_test(&dev->power.usage_count)) {
1056                         trace_rpm_usage_rcuidle(dev, rpmflags);
1057                         return 0;
1058                 }
1059         }
1060
1061         might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
1062
1063         spin_lock_irqsave(&dev->power.lock, flags);
1064         retval = rpm_idle(dev, rpmflags);
1065         spin_unlock_irqrestore(&dev->power.lock, flags);
1066
1067         return retval;
1068 }
1069 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
1070
1071 /**
1072  * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
1073  * @dev: Device to suspend.
1074  * @rpmflags: Flag bits.
1075  *
1076  * If the RPM_GET_PUT flag is set, decrement the device's usage count and
1077  * return immediately if it is larger than zero.  Then carry out a suspend,
1078  * either synchronous or asynchronous.
1079  *
1080  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1081  * or if pm_runtime_irq_safe() has been called.
1082  */
1083 int __pm_runtime_suspend(struct device *dev, int rpmflags)
1084 {
1085         unsigned long flags;
1086         int retval;
1087
1088         if (rpmflags & RPM_GET_PUT) {
1089                 if (!atomic_dec_and_test(&dev->power.usage_count)) {
1090                         trace_rpm_usage_rcuidle(dev, rpmflags);
1091                         return 0;
1092                 }
1093         }
1094
1095         might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
1096
1097         spin_lock_irqsave(&dev->power.lock, flags);
1098         retval = rpm_suspend(dev, rpmflags);
1099         spin_unlock_irqrestore(&dev->power.lock, flags);
1100
1101         return retval;
1102 }
1103 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
1104
1105 /**
1106  * __pm_runtime_resume - Entry point for runtime resume operations.
1107  * @dev: Device to resume.
1108  * @rpmflags: Flag bits.
1109  *
1110  * If the RPM_GET_PUT flag is set, increment the device's usage count.  Then
1111  * carry out a resume, either synchronous or asynchronous.
1112  *
1113  * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1114  * or if pm_runtime_irq_safe() has been called.
1115  */
1116 int __pm_runtime_resume(struct device *dev, int rpmflags)
1117 {
1118         unsigned long flags;
1119         int retval;
1120
1121         might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe &&
1122                         dev->power.runtime_status != RPM_ACTIVE);
1123
1124         if (rpmflags & RPM_GET_PUT)
1125                 atomic_inc(&dev->power.usage_count);
1126
1127         spin_lock_irqsave(&dev->power.lock, flags);
1128         retval = rpm_resume(dev, rpmflags);
1129         spin_unlock_irqrestore(&dev->power.lock, flags);
1130
1131         return retval;
1132 }
1133 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
1134
1135 /**
1136  * pm_runtime_get_if_active - Conditionally bump up device usage counter.
1137  * @dev: Device to handle.
1138  * @ign_usage_count: Whether or not to look at the current usage counter value.
1139  *
1140  * Return -EINVAL if runtime PM is disabled for @dev.
1141  *
1142  * Otherwise, if the runtime PM status of @dev is %RPM_ACTIVE and either
1143  * @ign_usage_count is %true or the runtime PM usage counter of @dev is not
1144  * zero, increment the usage counter of @dev and return 1. Otherwise, return 0
1145  * without changing the usage counter.
1146  *
1147  * If @ign_usage_count is %true, this function can be used to prevent suspending
1148  * the device when its runtime PM status is %RPM_ACTIVE.
1149  *
1150  * If @ign_usage_count is %false, this function can be used to prevent
1151  * suspending the device when both its runtime PM status is %RPM_ACTIVE and its
1152  * runtime PM usage counter is not zero.
1153  *
1154  * The caller is resposible for decrementing the runtime PM usage counter of
1155  * @dev after this function has returned a positive value for it.
1156  */
1157 int pm_runtime_get_if_active(struct device *dev, bool ign_usage_count)
1158 {
1159         unsigned long flags;
1160         int retval;
1161
1162         spin_lock_irqsave(&dev->power.lock, flags);
1163         if (dev->power.disable_depth > 0) {
1164                 retval = -EINVAL;
1165         } else if (dev->power.runtime_status != RPM_ACTIVE) {
1166                 retval = 0;
1167         } else if (ign_usage_count) {
1168                 retval = 1;
1169                 atomic_inc(&dev->power.usage_count);
1170         } else {
1171                 retval = atomic_inc_not_zero(&dev->power.usage_count);
1172         }
1173         trace_rpm_usage_rcuidle(dev, 0);
1174         spin_unlock_irqrestore(&dev->power.lock, flags);
1175
1176         return retval;
1177 }
1178 EXPORT_SYMBOL_GPL(pm_runtime_get_if_active);
1179
1180 /**
1181  * __pm_runtime_set_status - Set runtime PM status of a device.
1182  * @dev: Device to handle.
1183  * @status: New runtime PM status of the device.
1184  *
1185  * If runtime PM of the device is disabled or its power.runtime_error field is
1186  * different from zero, the status may be changed either to RPM_ACTIVE, or to
1187  * RPM_SUSPENDED, as long as that reflects the actual state of the device.
1188  * However, if the device has a parent and the parent is not active, and the
1189  * parent's power.ignore_children flag is unset, the device's status cannot be
1190  * set to RPM_ACTIVE, so -EBUSY is returned in that case.
1191  *
1192  * If successful, __pm_runtime_set_status() clears the power.runtime_error field
1193  * and the device parent's counter of unsuspended children is modified to
1194  * reflect the new status.  If the new status is RPM_SUSPENDED, an idle
1195  * notification request for the parent is submitted.
1196  *
1197  * If @dev has any suppliers (as reflected by device links to them), and @status
1198  * is RPM_ACTIVE, they will be activated upfront and if the activation of one
1199  * of them fails, the status of @dev will be changed to RPM_SUSPENDED (instead
1200  * of the @status value) and the suppliers will be deacticated on exit.  The
1201  * error returned by the failing supplier activation will be returned in that
1202  * case.
1203  */
1204 int __pm_runtime_set_status(struct device *dev, unsigned int status)
1205 {
1206         struct device *parent = dev->parent;
1207         bool notify_parent = false;
1208         int error = 0;
1209
1210         if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
1211                 return -EINVAL;
1212
1213         spin_lock_irq(&dev->power.lock);
1214
1215         /*
1216          * Prevent PM-runtime from being enabled for the device or return an
1217          * error if it is enabled already and working.
1218          */
1219         if (dev->power.runtime_error || dev->power.disable_depth)
1220                 dev->power.disable_depth++;
1221         else
1222                 error = -EAGAIN;
1223
1224         spin_unlock_irq(&dev->power.lock);
1225
1226         if (error)
1227                 return error;
1228
1229         /*
1230          * If the new status is RPM_ACTIVE, the suppliers can be activated
1231          * upfront regardless of the current status, because next time
1232          * rpm_put_suppliers() runs, the rpm_active refcounts of the links
1233          * involved will be dropped down to one anyway.
1234          */
1235         if (status == RPM_ACTIVE) {
1236                 int idx = device_links_read_lock();
1237
1238                 error = rpm_get_suppliers(dev);
1239                 if (error)
1240                         status = RPM_SUSPENDED;
1241
1242                 device_links_read_unlock(idx);
1243         }
1244
1245         spin_lock_irq(&dev->power.lock);
1246
1247         if (dev->power.runtime_status == status || !parent)
1248                 goto out_set;
1249
1250         if (status == RPM_SUSPENDED) {
1251                 atomic_add_unless(&parent->power.child_count, -1, 0);
1252                 notify_parent = !parent->power.ignore_children;
1253         } else {
1254                 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
1255
1256                 /*
1257                  * It is invalid to put an active child under a parent that is
1258                  * not active, has runtime PM enabled and the
1259                  * 'power.ignore_children' flag unset.
1260                  */
1261                 if (!parent->power.disable_depth
1262                     && !parent->power.ignore_children
1263                     && parent->power.runtime_status != RPM_ACTIVE) {
1264                         dev_err(dev, "runtime PM trying to activate child device %s but parent (%s) is not active\n",
1265                                 dev_name(dev),
1266                                 dev_name(parent));
1267                         error = -EBUSY;
1268                 } else if (dev->power.runtime_status == RPM_SUSPENDED) {
1269                         atomic_inc(&parent->power.child_count);
1270                 }
1271
1272                 spin_unlock(&parent->power.lock);
1273
1274                 if (error) {
1275                         status = RPM_SUSPENDED;
1276                         goto out;
1277                 }
1278         }
1279
1280  out_set:
1281         __update_runtime_status(dev, status);
1282         if (!error)
1283                 dev->power.runtime_error = 0;
1284
1285  out:
1286         spin_unlock_irq(&dev->power.lock);
1287
1288         if (notify_parent)
1289                 pm_request_idle(parent);
1290
1291         if (status == RPM_SUSPENDED) {
1292                 int idx = device_links_read_lock();
1293
1294                 rpm_put_suppliers(dev);
1295
1296                 device_links_read_unlock(idx);
1297         }
1298
1299         pm_runtime_enable(dev);
1300
1301         return error;
1302 }
1303 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1304
1305 /**
1306  * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1307  * @dev: Device to handle.
1308  *
1309  * Flush all pending requests for the device from pm_wq and wait for all
1310  * runtime PM operations involving the device in progress to complete.
1311  *
1312  * Should be called under dev->power.lock with interrupts disabled.
1313  */
1314 static void __pm_runtime_barrier(struct device *dev)
1315 {
1316         pm_runtime_deactivate_timer(dev);
1317
1318         if (dev->power.request_pending) {
1319                 dev->power.request = RPM_REQ_NONE;
1320                 spin_unlock_irq(&dev->power.lock);
1321
1322                 cancel_work_sync(&dev->power.work);
1323
1324                 spin_lock_irq(&dev->power.lock);
1325                 dev->power.request_pending = false;
1326         }
1327
1328         if (dev->power.runtime_status == RPM_SUSPENDING
1329             || dev->power.runtime_status == RPM_RESUMING
1330             || dev->power.idle_notification) {
1331                 DEFINE_WAIT(wait);
1332
1333                 /* Suspend, wake-up or idle notification in progress. */
1334                 for (;;) {
1335                         prepare_to_wait(&dev->power.wait_queue, &wait,
1336                                         TASK_UNINTERRUPTIBLE);
1337                         if (dev->power.runtime_status != RPM_SUSPENDING
1338                             && dev->power.runtime_status != RPM_RESUMING
1339                             && !dev->power.idle_notification)
1340                                 break;
1341                         spin_unlock_irq(&dev->power.lock);
1342
1343                         schedule();
1344
1345                         spin_lock_irq(&dev->power.lock);
1346                 }
1347                 finish_wait(&dev->power.wait_queue, &wait);
1348         }
1349 }
1350
1351 /**
1352  * pm_runtime_barrier - Flush pending requests and wait for completions.
1353  * @dev: Device to handle.
1354  *
1355  * Prevent the device from being suspended by incrementing its usage counter and
1356  * if there's a pending resume request for the device, wake the device up.
1357  * Next, make sure that all pending requests for the device have been flushed
1358  * from pm_wq and wait for all runtime PM operations involving the device in
1359  * progress to complete.
1360  *
1361  * Return value:
1362  * 1, if there was a resume request pending and the device had to be woken up,
1363  * 0, otherwise
1364  */
1365 int pm_runtime_barrier(struct device *dev)
1366 {
1367         int retval = 0;
1368
1369         pm_runtime_get_noresume(dev);
1370         spin_lock_irq(&dev->power.lock);
1371
1372         if (dev->power.request_pending
1373             && dev->power.request == RPM_REQ_RESUME) {
1374                 rpm_resume(dev, 0);
1375                 retval = 1;
1376         }
1377
1378         __pm_runtime_barrier(dev);
1379
1380         spin_unlock_irq(&dev->power.lock);
1381         pm_runtime_put_noidle(dev);
1382
1383         return retval;
1384 }
1385 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1386
1387 /**
1388  * __pm_runtime_disable - Disable runtime PM of a device.
1389  * @dev: Device to handle.
1390  * @check_resume: If set, check if there's a resume request for the device.
1391  *
1392  * Increment power.disable_depth for the device and if it was zero previously,
1393  * cancel all pending runtime PM requests for the device and wait for all
1394  * operations in progress to complete.  The device can be either active or
1395  * suspended after its runtime PM has been disabled.
1396  *
1397  * If @check_resume is set and there's a resume request pending when
1398  * __pm_runtime_disable() is called and power.disable_depth is zero, the
1399  * function will wake up the device before disabling its runtime PM.
1400  */
1401 void __pm_runtime_disable(struct device *dev, bool check_resume)
1402 {
1403         spin_lock_irq(&dev->power.lock);
1404
1405         if (dev->power.disable_depth > 0) {
1406                 dev->power.disable_depth++;
1407                 goto out;
1408         }
1409
1410         /*
1411          * Wake up the device if there's a resume request pending, because that
1412          * means there probably is some I/O to process and disabling runtime PM
1413          * shouldn't prevent the device from processing the I/O.
1414          */
1415         if (check_resume && dev->power.request_pending
1416             && dev->power.request == RPM_REQ_RESUME) {
1417                 /*
1418                  * Prevent suspends and idle notifications from being carried
1419                  * out after we have woken up the device.
1420                  */
1421                 pm_runtime_get_noresume(dev);
1422
1423                 rpm_resume(dev, 0);
1424
1425                 pm_runtime_put_noidle(dev);
1426         }
1427
1428         /* Update time accounting before disabling PM-runtime. */
1429         update_pm_runtime_accounting(dev);
1430
1431         if (!dev->power.disable_depth++)
1432                 __pm_runtime_barrier(dev);
1433
1434  out:
1435         spin_unlock_irq(&dev->power.lock);
1436 }
1437 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1438
1439 /**
1440  * pm_runtime_enable - Enable runtime PM of a device.
1441  * @dev: Device to handle.
1442  */
1443 void pm_runtime_enable(struct device *dev)
1444 {
1445         unsigned long flags;
1446
1447         spin_lock_irqsave(&dev->power.lock, flags);
1448
1449         if (dev->power.disable_depth > 0) {
1450                 dev->power.disable_depth--;
1451
1452                 /* About to enable runtime pm, set accounting_timestamp to now */
1453                 if (!dev->power.disable_depth)
1454                         dev->power.accounting_timestamp = ktime_get_mono_fast_ns();
1455         } else {
1456                 dev_warn(dev, "Unbalanced %s!\n", __func__);
1457         }
1458
1459         WARN(!dev->power.disable_depth &&
1460              dev->power.runtime_status == RPM_SUSPENDED &&
1461              !dev->power.ignore_children &&
1462              atomic_read(&dev->power.child_count) > 0,
1463              "Enabling runtime PM for inactive device (%s) with active children\n",
1464              dev_name(dev));
1465
1466         spin_unlock_irqrestore(&dev->power.lock, flags);
1467 }
1468 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1469
1470 /**
1471  * pm_runtime_forbid - Block runtime PM of a device.
1472  * @dev: Device to handle.
1473  *
1474  * Increase the device's usage count and clear its power.runtime_auto flag,
1475  * so that it cannot be suspended at run time until pm_runtime_allow() is called
1476  * for it.
1477  */
1478 void pm_runtime_forbid(struct device *dev)
1479 {
1480         spin_lock_irq(&dev->power.lock);
1481         if (!dev->power.runtime_auto)
1482                 goto out;
1483
1484         dev->power.runtime_auto = false;
1485         atomic_inc(&dev->power.usage_count);
1486         rpm_resume(dev, 0);
1487
1488  out:
1489         spin_unlock_irq(&dev->power.lock);
1490 }
1491 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1492
1493 /**
1494  * pm_runtime_allow - Unblock runtime PM of a device.
1495  * @dev: Device to handle.
1496  *
1497  * Decrease the device's usage count and set its power.runtime_auto flag.
1498  */
1499 void pm_runtime_allow(struct device *dev)
1500 {
1501         spin_lock_irq(&dev->power.lock);
1502         if (dev->power.runtime_auto)
1503                 goto out;
1504
1505         dev->power.runtime_auto = true;
1506         if (atomic_dec_and_test(&dev->power.usage_count))
1507                 rpm_idle(dev, RPM_AUTO | RPM_ASYNC);
1508         else
1509                 trace_rpm_usage_rcuidle(dev, RPM_AUTO | RPM_ASYNC);
1510
1511  out:
1512         spin_unlock_irq(&dev->power.lock);
1513 }
1514 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1515
1516 /**
1517  * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1518  * @dev: Device to handle.
1519  *
1520  * Set the power.no_callbacks flag, which tells the PM core that this
1521  * device is power-managed through its parent and has no runtime PM
1522  * callbacks of its own.  The runtime sysfs attributes will be removed.
1523  */
1524 void pm_runtime_no_callbacks(struct device *dev)
1525 {
1526         spin_lock_irq(&dev->power.lock);
1527         dev->power.no_callbacks = 1;
1528         spin_unlock_irq(&dev->power.lock);
1529         if (device_is_registered(dev))
1530                 rpm_sysfs_remove(dev);
1531 }
1532 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1533
1534 /**
1535  * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1536  * @dev: Device to handle
1537  *
1538  * Set the power.irq_safe flag, which tells the PM core that the
1539  * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1540  * always be invoked with the spinlock held and interrupts disabled.  It also
1541  * causes the parent's usage counter to be permanently incremented, preventing
1542  * the parent from runtime suspending -- otherwise an irq-safe child might have
1543  * to wait for a non-irq-safe parent.
1544  */
1545 void pm_runtime_irq_safe(struct device *dev)
1546 {
1547         if (dev->parent)
1548                 pm_runtime_get_sync(dev->parent);
1549         spin_lock_irq(&dev->power.lock);
1550         dev->power.irq_safe = 1;
1551         spin_unlock_irq(&dev->power.lock);
1552 }
1553 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1554
1555 /**
1556  * update_autosuspend - Handle a change to a device's autosuspend settings.
1557  * @dev: Device to handle.
1558  * @old_delay: The former autosuspend_delay value.
1559  * @old_use: The former use_autosuspend value.
1560  *
1561  * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1562  * set; otherwise allow it.  Send an idle notification if suspends are allowed.
1563  *
1564  * This function must be called under dev->power.lock with interrupts disabled.
1565  */
1566 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1567 {
1568         int delay = dev->power.autosuspend_delay;
1569
1570         /* Should runtime suspend be prevented now? */
1571         if (dev->power.use_autosuspend && delay < 0) {
1572
1573                 /* If it used to be allowed then prevent it. */
1574                 if (!old_use || old_delay >= 0) {
1575                         atomic_inc(&dev->power.usage_count);
1576                         rpm_resume(dev, 0);
1577                 } else {
1578                         trace_rpm_usage_rcuidle(dev, 0);
1579                 }
1580         }
1581
1582         /* Runtime suspend should be allowed now. */
1583         else {
1584
1585                 /* If it used to be prevented then allow it. */
1586                 if (old_use && old_delay < 0)
1587                         atomic_dec(&dev->power.usage_count);
1588
1589                 /* Maybe we can autosuspend now. */
1590                 rpm_idle(dev, RPM_AUTO);
1591         }
1592 }
1593
1594 /**
1595  * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1596  * @dev: Device to handle.
1597  * @delay: Value of the new delay in milliseconds.
1598  *
1599  * Set the device's power.autosuspend_delay value.  If it changes to negative
1600  * and the power.use_autosuspend flag is set, prevent runtime suspends.  If it
1601  * changes the other way, allow runtime suspends.
1602  */
1603 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1604 {
1605         int old_delay, old_use;
1606
1607         spin_lock_irq(&dev->power.lock);
1608         old_delay = dev->power.autosuspend_delay;
1609         old_use = dev->power.use_autosuspend;
1610         dev->power.autosuspend_delay = delay;
1611         update_autosuspend(dev, old_delay, old_use);
1612         spin_unlock_irq(&dev->power.lock);
1613 }
1614 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1615
1616 /**
1617  * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1618  * @dev: Device to handle.
1619  * @use: New value for use_autosuspend.
1620  *
1621  * Set the device's power.use_autosuspend flag, and allow or prevent runtime
1622  * suspends as needed.
1623  */
1624 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1625 {
1626         int old_delay, old_use;
1627
1628         spin_lock_irq(&dev->power.lock);
1629         old_delay = dev->power.autosuspend_delay;
1630         old_use = dev->power.use_autosuspend;
1631         dev->power.use_autosuspend = use;
1632         update_autosuspend(dev, old_delay, old_use);
1633         spin_unlock_irq(&dev->power.lock);
1634 }
1635 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1636
1637 /**
1638  * pm_runtime_init - Initialize runtime PM fields in given device object.
1639  * @dev: Device object to initialize.
1640  */
1641 void pm_runtime_init(struct device *dev)
1642 {
1643         dev->power.runtime_status = RPM_SUSPENDED;
1644         dev->power.idle_notification = false;
1645
1646         dev->power.disable_depth = 1;
1647         atomic_set(&dev->power.usage_count, 0);
1648
1649         dev->power.runtime_error = 0;
1650
1651         atomic_set(&dev->power.child_count, 0);
1652         pm_suspend_ignore_children(dev, false);
1653         dev->power.runtime_auto = true;
1654
1655         dev->power.request_pending = false;
1656         dev->power.request = RPM_REQ_NONE;
1657         dev->power.deferred_resume = false;
1658         dev->power.needs_force_resume = 0;
1659         INIT_WORK(&dev->power.work, pm_runtime_work);
1660
1661         dev->power.timer_expires = 0;
1662         hrtimer_init(&dev->power.suspend_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1663         dev->power.suspend_timer.function = pm_suspend_timer_fn;
1664
1665         init_waitqueue_head(&dev->power.wait_queue);
1666 }
1667
1668 /**
1669  * pm_runtime_reinit - Re-initialize runtime PM fields in given device object.
1670  * @dev: Device object to re-initialize.
1671  */
1672 void pm_runtime_reinit(struct device *dev)
1673 {
1674         if (!pm_runtime_enabled(dev)) {
1675                 if (dev->power.runtime_status == RPM_ACTIVE)
1676                         pm_runtime_set_suspended(dev);
1677                 if (dev->power.irq_safe) {
1678                         spin_lock_irq(&dev->power.lock);
1679                         dev->power.irq_safe = 0;
1680                         spin_unlock_irq(&dev->power.lock);
1681                         if (dev->parent)
1682                                 pm_runtime_put(dev->parent);
1683                 }
1684         }
1685 }
1686
1687 /**
1688  * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1689  * @dev: Device object being removed from device hierarchy.
1690  */
1691 void pm_runtime_remove(struct device *dev)
1692 {
1693         __pm_runtime_disable(dev, false);
1694         pm_runtime_reinit(dev);
1695 }
1696
1697 /**
1698  * pm_runtime_get_suppliers - Resume and reference-count supplier devices.
1699  * @dev: Consumer device.
1700  */
1701 void pm_runtime_get_suppliers(struct device *dev)
1702 {
1703         struct device_link *link;
1704         int idx;
1705
1706         idx = device_links_read_lock();
1707
1708         list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
1709                                 device_links_read_lock_held())
1710                 if (link->flags & DL_FLAG_PM_RUNTIME) {
1711                         link->supplier_preactivated = true;
1712                         pm_runtime_get_sync(link->supplier);
1713                         refcount_inc(&link->rpm_active);
1714                 }
1715
1716         device_links_read_unlock(idx);
1717 }
1718
1719 /**
1720  * pm_runtime_put_suppliers - Drop references to supplier devices.
1721  * @dev: Consumer device.
1722  */
1723 void pm_runtime_put_suppliers(struct device *dev)
1724 {
1725         struct device_link *link;
1726         unsigned long flags;
1727         bool put;
1728         int idx;
1729
1730         idx = device_links_read_lock();
1731
1732         list_for_each_entry_rcu(link, &dev->links.suppliers, c_node,
1733                                 device_links_read_lock_held())
1734                 if (link->supplier_preactivated) {
1735                         link->supplier_preactivated = false;
1736                         spin_lock_irqsave(&dev->power.lock, flags);
1737                         put = pm_runtime_status_suspended(dev) &&
1738                               refcount_dec_not_one(&link->rpm_active);
1739                         spin_unlock_irqrestore(&dev->power.lock, flags);
1740                         if (put)
1741                                 pm_runtime_put(link->supplier);
1742                 }
1743
1744         device_links_read_unlock(idx);
1745 }
1746
1747 void pm_runtime_new_link(struct device *dev)
1748 {
1749         spin_lock_irq(&dev->power.lock);
1750         dev->power.links_count++;
1751         spin_unlock_irq(&dev->power.lock);
1752 }
1753
1754 static void pm_runtime_drop_link_count(struct device *dev)
1755 {
1756         spin_lock_irq(&dev->power.lock);
1757         WARN_ON(dev->power.links_count == 0);
1758         dev->power.links_count--;
1759         spin_unlock_irq(&dev->power.lock);
1760 }
1761
1762 /**
1763  * pm_runtime_drop_link - Prepare for device link removal.
1764  * @link: Device link going away.
1765  *
1766  * Drop the link count of the consumer end of @link and decrement the supplier
1767  * device's runtime PM usage counter as many times as needed to drop all of the
1768  * PM runtime reference to it from the consumer.
1769  */
1770 void pm_runtime_drop_link(struct device_link *link)
1771 {
1772         if (!(link->flags & DL_FLAG_PM_RUNTIME))
1773                 return;
1774
1775         pm_runtime_drop_link_count(link->consumer);
1776         pm_runtime_release_supplier(link);
1777         pm_request_idle(link->supplier);
1778 }
1779
1780 static bool pm_runtime_need_not_resume(struct device *dev)
1781 {
1782         return atomic_read(&dev->power.usage_count) <= 1 &&
1783                 (atomic_read(&dev->power.child_count) == 0 ||
1784                  dev->power.ignore_children);
1785 }
1786
1787 /**
1788  * pm_runtime_force_suspend - Force a device into suspend state if needed.
1789  * @dev: Device to suspend.
1790  *
1791  * Disable runtime PM so we safely can check the device's runtime PM status and
1792  * if it is active, invoke its ->runtime_suspend callback to suspend it and
1793  * change its runtime PM status field to RPM_SUSPENDED.  Also, if the device's
1794  * usage and children counters don't indicate that the device was in use before
1795  * the system-wide transition under way, decrement its parent's children counter
1796  * (if there is a parent).  Keep runtime PM disabled to preserve the state
1797  * unless we encounter errors.
1798  *
1799  * Typically this function may be invoked from a system suspend callback to make
1800  * sure the device is put into low power state and it should only be used during
1801  * system-wide PM transitions to sleep states.  It assumes that the analogous
1802  * pm_runtime_force_resume() will be used to resume the device.
1803  */
1804 int pm_runtime_force_suspend(struct device *dev)
1805 {
1806         int (*callback)(struct device *);
1807         int ret;
1808
1809         pm_runtime_disable(dev);
1810         if (pm_runtime_status_suspended(dev))
1811                 return 0;
1812
1813         callback = RPM_GET_CALLBACK(dev, runtime_suspend);
1814
1815         ret = callback ? callback(dev) : 0;
1816         if (ret)
1817                 goto err;
1818
1819         /*
1820          * If the device can stay in suspend after the system-wide transition
1821          * to the working state that will follow, drop the children counter of
1822          * its parent, but set its status to RPM_SUSPENDED anyway in case this
1823          * function will be called again for it in the meantime.
1824          */
1825         if (pm_runtime_need_not_resume(dev)) {
1826                 pm_runtime_set_suspended(dev);
1827         } else {
1828                 __update_runtime_status(dev, RPM_SUSPENDED);
1829                 dev->power.needs_force_resume = 1;
1830         }
1831
1832         return 0;
1833
1834 err:
1835         pm_runtime_enable(dev);
1836         return ret;
1837 }
1838 EXPORT_SYMBOL_GPL(pm_runtime_force_suspend);
1839
1840 /**
1841  * pm_runtime_force_resume - Force a device into resume state if needed.
1842  * @dev: Device to resume.
1843  *
1844  * Prior invoking this function we expect the user to have brought the device
1845  * into low power state by a call to pm_runtime_force_suspend(). Here we reverse
1846  * those actions and bring the device into full power, if it is expected to be
1847  * used on system resume.  In the other case, we defer the resume to be managed
1848  * via runtime PM.
1849  *
1850  * Typically this function may be invoked from a system resume callback.
1851  */
1852 int pm_runtime_force_resume(struct device *dev)
1853 {
1854         int (*callback)(struct device *);
1855         int ret = 0;
1856
1857         if (!pm_runtime_status_suspended(dev) || !dev->power.needs_force_resume)
1858                 goto out;
1859
1860         /*
1861          * The value of the parent's children counter is correct already, so
1862          * just update the status of the device.
1863          */
1864         __update_runtime_status(dev, RPM_ACTIVE);
1865
1866         callback = RPM_GET_CALLBACK(dev, runtime_resume);
1867
1868         ret = callback ? callback(dev) : 0;
1869         if (ret) {
1870                 pm_runtime_set_suspended(dev);
1871                 goto out;
1872         }
1873
1874         pm_runtime_mark_last_busy(dev);
1875 out:
1876         dev->power.needs_force_resume = 0;
1877         pm_runtime_enable(dev);
1878         return ret;
1879 }
1880 EXPORT_SYMBOL_GPL(pm_runtime_force_resume);