OSDN Git Service

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