PM / Runtime: Automatically retry failed autosuspends
[firefly-linux-kernel-4.4.55.git] / drivers / base / power / runtime.c
1 /*
2  * drivers/base/power/runtime.c - Helper functions for device run-time 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.h>
11 #include <linux/pm_runtime.h>
12 #include "power.h"
13
14 static int rpm_resume(struct device *dev, int rpmflags);
15 static int rpm_suspend(struct device *dev, int rpmflags);
16
17 /**
18  * update_pm_runtime_accounting - Update the time accounting of power states
19  * @dev: Device to update the accounting for
20  *
21  * In order to be able to have time accounting of the various power states
22  * (as used by programs such as PowerTOP to show the effectiveness of runtime
23  * PM), we need to track the time spent in each state.
24  * update_pm_runtime_accounting must be called each time before the
25  * runtime_status field is updated, to account the time in the old state
26  * correctly.
27  */
28 void update_pm_runtime_accounting(struct device *dev)
29 {
30         unsigned long now = jiffies;
31         int delta;
32
33         delta = now - dev->power.accounting_timestamp;
34
35         if (delta < 0)
36                 delta = 0;
37
38         dev->power.accounting_timestamp = now;
39
40         if (dev->power.disable_depth > 0)
41                 return;
42
43         if (dev->power.runtime_status == RPM_SUSPENDED)
44                 dev->power.suspended_jiffies += delta;
45         else
46                 dev->power.active_jiffies += delta;
47 }
48
49 static void __update_runtime_status(struct device *dev, enum rpm_status status)
50 {
51         update_pm_runtime_accounting(dev);
52         dev->power.runtime_status = status;
53 }
54
55 /**
56  * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
57  * @dev: Device to handle.
58  */
59 static void pm_runtime_deactivate_timer(struct device *dev)
60 {
61         if (dev->power.timer_expires > 0) {
62                 del_timer(&dev->power.suspend_timer);
63                 dev->power.timer_expires = 0;
64         }
65 }
66
67 /**
68  * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
69  * @dev: Device to handle.
70  */
71 static void pm_runtime_cancel_pending(struct device *dev)
72 {
73         pm_runtime_deactivate_timer(dev);
74         /*
75          * In case there's a request pending, make sure its work function will
76          * return without doing anything.
77          */
78         dev->power.request = RPM_REQ_NONE;
79 }
80
81 /*
82  * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
83  * @dev: Device to handle.
84  *
85  * Compute the autosuspend-delay expiration time based on the device's
86  * power.last_busy time.  If the delay has already expired or is disabled
87  * (negative) or the power.use_autosuspend flag isn't set, return 0.
88  * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
89  *
90  * This function may be called either with or without dev->power.lock held.
91  * Either way it can be racy, since power.last_busy may be updated at any time.
92  */
93 unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
94 {
95         int autosuspend_delay;
96         long elapsed;
97         unsigned long last_busy;
98         unsigned long expires = 0;
99
100         if (!dev->power.use_autosuspend)
101                 goto out;
102
103         autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
104         if (autosuspend_delay < 0)
105                 goto out;
106
107         last_busy = ACCESS_ONCE(dev->power.last_busy);
108         elapsed = jiffies - last_busy;
109         if (elapsed < 0)
110                 goto out;       /* jiffies has wrapped around. */
111
112         /*
113          * If the autosuspend_delay is >= 1 second, align the timer by rounding
114          * up to the nearest second.
115          */
116         expires = last_busy + msecs_to_jiffies(autosuspend_delay);
117         if (autosuspend_delay >= 1000)
118                 expires = round_jiffies(expires);
119         expires += !expires;
120         if (elapsed >= expires - last_busy)
121                 expires = 0;    /* Already expired. */
122
123  out:
124         return expires;
125 }
126 EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
127
128 /**
129  * rpm_check_suspend_allowed - Test whether a device may be suspended.
130  * @dev: Device to test.
131  */
132 static int rpm_check_suspend_allowed(struct device *dev)
133 {
134         int retval = 0;
135
136         if (dev->power.runtime_error)
137                 retval = -EINVAL;
138         else if (atomic_read(&dev->power.usage_count) > 0
139             || dev->power.disable_depth > 0)
140                 retval = -EAGAIN;
141         else if (!pm_children_suspended(dev))
142                 retval = -EBUSY;
143
144         /* Pending resume requests take precedence over suspends. */
145         else if ((dev->power.deferred_resume
146                         && dev->power.runtime_status == RPM_SUSPENDING)
147             || (dev->power.request_pending
148                         && dev->power.request == RPM_REQ_RESUME))
149                 retval = -EAGAIN;
150         else if (dev->power.runtime_status == RPM_SUSPENDED)
151                 retval = 1;
152
153         return retval;
154 }
155
156 /**
157  * rpm_idle - Notify device bus type if the device can be suspended.
158  * @dev: Device to notify the bus type about.
159  * @rpmflags: Flag bits.
160  *
161  * Check if the device's run-time PM status allows it to be suspended.  If
162  * another idle notification has been started earlier, return immediately.  If
163  * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
164  * run the ->runtime_idle() callback directly.
165  *
166  * This function must be called under dev->power.lock with interrupts disabled.
167  */
168 static int rpm_idle(struct device *dev, int rpmflags)
169 {
170         int (*callback)(struct device *);
171         int retval;
172
173         retval = rpm_check_suspend_allowed(dev);
174         if (retval < 0)
175                 ;       /* Conditions are wrong. */
176
177         /* Idle notifications are allowed only in the RPM_ACTIVE state. */
178         else if (dev->power.runtime_status != RPM_ACTIVE)
179                 retval = -EAGAIN;
180
181         /*
182          * Any pending request other than an idle notification takes
183          * precedence over us, except that the timer may be running.
184          */
185         else if (dev->power.request_pending &&
186             dev->power.request > RPM_REQ_IDLE)
187                 retval = -EAGAIN;
188
189         /* Act as though RPM_NOWAIT is always set. */
190         else if (dev->power.idle_notification)
191                 retval = -EINPROGRESS;
192         if (retval)
193                 goto out;
194
195         /* Pending requests need to be canceled. */
196         dev->power.request = RPM_REQ_NONE;
197
198         if (dev->power.no_callbacks) {
199                 /* Assume ->runtime_idle() callback would have suspended. */
200                 retval = rpm_suspend(dev, rpmflags);
201                 goto out;
202         }
203
204         /* Carry out an asynchronous or a synchronous idle notification. */
205         if (rpmflags & RPM_ASYNC) {
206                 dev->power.request = RPM_REQ_IDLE;
207                 if (!dev->power.request_pending) {
208                         dev->power.request_pending = true;
209                         queue_work(pm_wq, &dev->power.work);
210                 }
211                 goto out;
212         }
213
214         dev->power.idle_notification = true;
215
216         if (dev->pwr_domain)
217                 callback = dev->pwr_domain->ops.runtime_idle;
218         else if (dev->type && dev->type->pm)
219                 callback = dev->type->pm->runtime_idle;
220         else if (dev->class && dev->class->pm)
221                 callback = dev->class->pm->runtime_idle;
222         else if (dev->bus && dev->bus->pm)
223                 callback = dev->bus->pm->runtime_idle;
224         else
225                 callback = NULL;
226
227         if (callback) {
228                 spin_unlock_irq(&dev->power.lock);
229
230                 callback(dev);
231
232                 spin_lock_irq(&dev->power.lock);
233         }
234
235         dev->power.idle_notification = false;
236         wake_up_all(&dev->power.wait_queue);
237
238  out:
239         return retval;
240 }
241
242 /**
243  * rpm_callback - Run a given runtime PM callback for a given device.
244  * @cb: Runtime PM callback to run.
245  * @dev: Device to run the callback for.
246  */
247 static int rpm_callback(int (*cb)(struct device *), struct device *dev)
248         __releases(&dev->power.lock) __acquires(&dev->power.lock)
249 {
250         int retval;
251
252         if (!cb)
253                 return -ENOSYS;
254
255         if (dev->power.irq_safe) {
256                 retval = cb(dev);
257         } else {
258                 spin_unlock_irq(&dev->power.lock);
259
260                 retval = cb(dev);
261
262                 spin_lock_irq(&dev->power.lock);
263         }
264         dev->power.runtime_error = retval;
265         return retval;
266 }
267
268 /**
269  * rpm_suspend - Carry out run-time suspend of given device.
270  * @dev: Device to suspend.
271  * @rpmflags: Flag bits.
272  *
273  * Check if the device's run-time PM status allows it to be suspended.  If
274  * another suspend has been started earlier, either return immediately or wait
275  * for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags.  Cancel a
276  * pending idle notification.  If the RPM_ASYNC flag is set then queue a
277  * suspend request; otherwise run the ->runtime_suspend() callback directly.
278  * If a deferred resume was requested while the callback was running then carry
279  * it out; otherwise send an idle notification for the device (if the suspend
280  * failed) or for its parent (if the suspend succeeded).
281  * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
282  * flag is set and the next autosuspend-delay expiration time is in the
283  * future, schedule another autosuspend attempt.
284  *
285  * This function must be called under dev->power.lock with interrupts disabled.
286  */
287 static int rpm_suspend(struct device *dev, int rpmflags)
288         __releases(&dev->power.lock) __acquires(&dev->power.lock)
289 {
290         int (*callback)(struct device *);
291         struct device *parent = NULL;
292         int retval;
293
294         dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
295
296  repeat:
297         retval = rpm_check_suspend_allowed(dev);
298
299         if (retval < 0)
300                 ;       /* Conditions are wrong. */
301
302         /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
303         else if (dev->power.runtime_status == RPM_RESUMING &&
304             !(rpmflags & RPM_ASYNC))
305                 retval = -EAGAIN;
306         if (retval)
307                 goto out;
308
309         /* If the autosuspend_delay time hasn't expired yet, reschedule. */
310         if ((rpmflags & RPM_AUTO)
311             && dev->power.runtime_status != RPM_SUSPENDING) {
312                 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
313
314                 if (expires != 0) {
315                         /* Pending requests need to be canceled. */
316                         dev->power.request = RPM_REQ_NONE;
317
318                         /*
319                          * Optimization: If the timer is already running and is
320                          * set to expire at or before the autosuspend delay,
321                          * avoid the overhead of resetting it.  Just let it
322                          * expire; pm_suspend_timer_fn() will take care of the
323                          * rest.
324                          */
325                         if (!(dev->power.timer_expires && time_before_eq(
326                             dev->power.timer_expires, expires))) {
327                                 dev->power.timer_expires = expires;
328                                 mod_timer(&dev->power.suspend_timer, expires);
329                         }
330                         dev->power.timer_autosuspends = 1;
331                         goto out;
332                 }
333         }
334
335         /* Other scheduled or pending requests need to be canceled. */
336         pm_runtime_cancel_pending(dev);
337
338         if (dev->power.runtime_status == RPM_SUSPENDING) {
339                 DEFINE_WAIT(wait);
340
341                 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
342                         retval = -EINPROGRESS;
343                         goto out;
344                 }
345
346                 /* Wait for the other suspend running in parallel with us. */
347                 for (;;) {
348                         prepare_to_wait(&dev->power.wait_queue, &wait,
349                                         TASK_UNINTERRUPTIBLE);
350                         if (dev->power.runtime_status != RPM_SUSPENDING)
351                                 break;
352
353                         spin_unlock_irq(&dev->power.lock);
354
355                         schedule();
356
357                         spin_lock_irq(&dev->power.lock);
358                 }
359                 finish_wait(&dev->power.wait_queue, &wait);
360                 goto repeat;
361         }
362
363         dev->power.deferred_resume = false;
364         if (dev->power.no_callbacks)
365                 goto no_callback;       /* Assume success. */
366
367         /* Carry out an asynchronous or a synchronous suspend. */
368         if (rpmflags & RPM_ASYNC) {
369                 dev->power.request = (rpmflags & RPM_AUTO) ?
370                     RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
371                 if (!dev->power.request_pending) {
372                         dev->power.request_pending = true;
373                         queue_work(pm_wq, &dev->power.work);
374                 }
375                 goto out;
376         }
377
378         __update_runtime_status(dev, RPM_SUSPENDING);
379
380         if (dev->pwr_domain)
381                 callback = dev->pwr_domain->ops.runtime_suspend;
382         else if (dev->type && dev->type->pm)
383                 callback = dev->type->pm->runtime_suspend;
384         else if (dev->class && dev->class->pm)
385                 callback = dev->class->pm->runtime_suspend;
386         else if (dev->bus && dev->bus->pm)
387                 callback = dev->bus->pm->runtime_suspend;
388         else
389                 callback = NULL;
390
391         retval = rpm_callback(callback, dev);
392         if (retval) {
393                 __update_runtime_status(dev, RPM_ACTIVE);
394                 dev->power.deferred_resume = 0;
395                 if (retval == -EAGAIN || retval == -EBUSY) {
396                         dev->power.runtime_error = 0;
397
398                         /*
399                          * If the callback routine failed an autosuspend, and
400                          * if the last_busy time has been updated so that there
401                          * is a new autosuspend expiration time, automatically
402                          * reschedule another autosuspend.
403                          */
404                         if ((rpmflags & RPM_AUTO) &&
405                             pm_runtime_autosuspend_expiration(dev) != 0)
406                                 goto repeat;
407                 } else {
408                         pm_runtime_cancel_pending(dev);
409                 }
410         } else {
411  no_callback:
412                 __update_runtime_status(dev, RPM_SUSPENDED);
413                 pm_runtime_deactivate_timer(dev);
414
415                 if (dev->parent) {
416                         parent = dev->parent;
417                         atomic_add_unless(&parent->power.child_count, -1, 0);
418                 }
419         }
420         wake_up_all(&dev->power.wait_queue);
421
422         if (dev->power.deferred_resume) {
423                 rpm_resume(dev, 0);
424                 retval = -EAGAIN;
425                 goto out;
426         }
427
428         /* Maybe the parent is now able to suspend. */
429         if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
430                 spin_unlock(&dev->power.lock);
431
432                 spin_lock(&parent->power.lock);
433                 rpm_idle(parent, RPM_ASYNC);
434                 spin_unlock(&parent->power.lock);
435
436                 spin_lock(&dev->power.lock);
437         }
438
439  out:
440         dev_dbg(dev, "%s returns %d\n", __func__, retval);
441
442         return retval;
443 }
444
445 /**
446  * rpm_resume - Carry out run-time resume of given device.
447  * @dev: Device to resume.
448  * @rpmflags: Flag bits.
449  *
450  * Check if the device's run-time PM status allows it to be resumed.  Cancel
451  * any scheduled or pending requests.  If another resume has been started
452  * earlier, either return immediately or wait for it to finish, depending on the
453  * RPM_NOWAIT and RPM_ASYNC flags.  Similarly, if there's a suspend running in
454  * parallel with this function, either tell the other process to resume after
455  * suspending (deferred_resume) or wait for it to finish.  If the RPM_ASYNC
456  * flag is set then queue a resume request; otherwise run the
457  * ->runtime_resume() callback directly.  Queue an idle notification for the
458  * device if the resume succeeded.
459  *
460  * This function must be called under dev->power.lock with interrupts disabled.
461  */
462 static int rpm_resume(struct device *dev, int rpmflags)
463         __releases(&dev->power.lock) __acquires(&dev->power.lock)
464 {
465         int (*callback)(struct device *);
466         struct device *parent = NULL;
467         int retval = 0;
468
469         dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
470
471  repeat:
472         if (dev->power.runtime_error)
473                 retval = -EINVAL;
474         else if (dev->power.disable_depth > 0)
475                 retval = -EAGAIN;
476         if (retval)
477                 goto out;
478
479         /*
480          * Other scheduled or pending requests need to be canceled.  Small
481          * optimization: If an autosuspend timer is running, leave it running
482          * rather than cancelling it now only to restart it again in the near
483          * future.
484          */
485         dev->power.request = RPM_REQ_NONE;
486         if (!dev->power.timer_autosuspends)
487                 pm_runtime_deactivate_timer(dev);
488
489         if (dev->power.runtime_status == RPM_ACTIVE) {
490                 retval = 1;
491                 goto out;
492         }
493
494         if (dev->power.runtime_status == RPM_RESUMING
495             || dev->power.runtime_status == RPM_SUSPENDING) {
496                 DEFINE_WAIT(wait);
497
498                 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
499                         if (dev->power.runtime_status == RPM_SUSPENDING)
500                                 dev->power.deferred_resume = true;
501                         else
502                                 retval = -EINPROGRESS;
503                         goto out;
504                 }
505
506                 /* Wait for the operation carried out in parallel with us. */
507                 for (;;) {
508                         prepare_to_wait(&dev->power.wait_queue, &wait,
509                                         TASK_UNINTERRUPTIBLE);
510                         if (dev->power.runtime_status != RPM_RESUMING
511                             && dev->power.runtime_status != RPM_SUSPENDING)
512                                 break;
513
514                         spin_unlock_irq(&dev->power.lock);
515
516                         schedule();
517
518                         spin_lock_irq(&dev->power.lock);
519                 }
520                 finish_wait(&dev->power.wait_queue, &wait);
521                 goto repeat;
522         }
523
524         /*
525          * See if we can skip waking up the parent.  This is safe only if
526          * power.no_callbacks is set, because otherwise we don't know whether
527          * the resume will actually succeed.
528          */
529         if (dev->power.no_callbacks && !parent && dev->parent) {
530                 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
531                 if (dev->parent->power.disable_depth > 0
532                     || dev->parent->power.ignore_children
533                     || dev->parent->power.runtime_status == RPM_ACTIVE) {
534                         atomic_inc(&dev->parent->power.child_count);
535                         spin_unlock(&dev->parent->power.lock);
536                         goto no_callback;       /* Assume success. */
537                 }
538                 spin_unlock(&dev->parent->power.lock);
539         }
540
541         /* Carry out an asynchronous or a synchronous resume. */
542         if (rpmflags & RPM_ASYNC) {
543                 dev->power.request = RPM_REQ_RESUME;
544                 if (!dev->power.request_pending) {
545                         dev->power.request_pending = true;
546                         queue_work(pm_wq, &dev->power.work);
547                 }
548                 retval = 0;
549                 goto out;
550         }
551
552         if (!parent && dev->parent) {
553                 /*
554                  * Increment the parent's usage counter and resume it if
555                  * necessary.  Not needed if dev is irq-safe; then the
556                  * parent is permanently resumed.
557                  */
558                 parent = dev->parent;
559                 if (dev->power.irq_safe)
560                         goto skip_parent;
561                 spin_unlock(&dev->power.lock);
562
563                 pm_runtime_get_noresume(parent);
564
565                 spin_lock(&parent->power.lock);
566                 /*
567                  * We can resume if the parent's run-time PM is disabled or it
568                  * is set to ignore children.
569                  */
570                 if (!parent->power.disable_depth
571                     && !parent->power.ignore_children) {
572                         rpm_resume(parent, 0);
573                         if (parent->power.runtime_status != RPM_ACTIVE)
574                                 retval = -EBUSY;
575                 }
576                 spin_unlock(&parent->power.lock);
577
578                 spin_lock(&dev->power.lock);
579                 if (retval)
580                         goto out;
581                 goto repeat;
582         }
583  skip_parent:
584
585         if (dev->power.no_callbacks)
586                 goto no_callback;       /* Assume success. */
587
588         __update_runtime_status(dev, RPM_RESUMING);
589
590         if (dev->pwr_domain)
591                 callback = dev->pwr_domain->ops.runtime_resume;
592         else if (dev->type && dev->type->pm)
593                 callback = dev->type->pm->runtime_resume;
594         else if (dev->class && dev->class->pm)
595                 callback = dev->class->pm->runtime_resume;
596         else if (dev->bus && dev->bus->pm)
597                 callback = dev->bus->pm->runtime_resume;
598         else
599                 callback = NULL;
600
601         retval = rpm_callback(callback, dev);
602         if (retval) {
603                 __update_runtime_status(dev, RPM_SUSPENDED);
604                 pm_runtime_cancel_pending(dev);
605         } else {
606  no_callback:
607                 __update_runtime_status(dev, RPM_ACTIVE);
608                 if (parent)
609                         atomic_inc(&parent->power.child_count);
610         }
611         wake_up_all(&dev->power.wait_queue);
612
613         if (!retval)
614                 rpm_idle(dev, RPM_ASYNC);
615
616  out:
617         if (parent && !dev->power.irq_safe) {
618                 spin_unlock_irq(&dev->power.lock);
619
620                 pm_runtime_put(parent);
621
622                 spin_lock_irq(&dev->power.lock);
623         }
624
625         dev_dbg(dev, "%s returns %d\n", __func__, retval);
626
627         return retval;
628 }
629
630 /**
631  * pm_runtime_work - Universal run-time PM work function.
632  * @work: Work structure used for scheduling the execution of this function.
633  *
634  * Use @work to get the device object the work is to be done for, determine what
635  * is to be done and execute the appropriate run-time PM function.
636  */
637 static void pm_runtime_work(struct work_struct *work)
638 {
639         struct device *dev = container_of(work, struct device, power.work);
640         enum rpm_request req;
641
642         spin_lock_irq(&dev->power.lock);
643
644         if (!dev->power.request_pending)
645                 goto out;
646
647         req = dev->power.request;
648         dev->power.request = RPM_REQ_NONE;
649         dev->power.request_pending = false;
650
651         switch (req) {
652         case RPM_REQ_NONE:
653                 break;
654         case RPM_REQ_IDLE:
655                 rpm_idle(dev, RPM_NOWAIT);
656                 break;
657         case RPM_REQ_SUSPEND:
658                 rpm_suspend(dev, RPM_NOWAIT);
659                 break;
660         case RPM_REQ_AUTOSUSPEND:
661                 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
662                 break;
663         case RPM_REQ_RESUME:
664                 rpm_resume(dev, RPM_NOWAIT);
665                 break;
666         }
667
668  out:
669         spin_unlock_irq(&dev->power.lock);
670 }
671
672 /**
673  * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
674  * @data: Device pointer passed by pm_schedule_suspend().
675  *
676  * Check if the time is right and queue a suspend request.
677  */
678 static void pm_suspend_timer_fn(unsigned long data)
679 {
680         struct device *dev = (struct device *)data;
681         unsigned long flags;
682         unsigned long expires;
683
684         spin_lock_irqsave(&dev->power.lock, flags);
685
686         expires = dev->power.timer_expires;
687         /* If 'expire' is after 'jiffies' we've been called too early. */
688         if (expires > 0 && !time_after(expires, jiffies)) {
689                 dev->power.timer_expires = 0;
690                 rpm_suspend(dev, dev->power.timer_autosuspends ?
691                     (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
692         }
693
694         spin_unlock_irqrestore(&dev->power.lock, flags);
695 }
696
697 /**
698  * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
699  * @dev: Device to suspend.
700  * @delay: Time to wait before submitting a suspend request, in milliseconds.
701  */
702 int pm_schedule_suspend(struct device *dev, unsigned int delay)
703 {
704         unsigned long flags;
705         int retval;
706
707         spin_lock_irqsave(&dev->power.lock, flags);
708
709         if (!delay) {
710                 retval = rpm_suspend(dev, RPM_ASYNC);
711                 goto out;
712         }
713
714         retval = rpm_check_suspend_allowed(dev);
715         if (retval)
716                 goto out;
717
718         /* Other scheduled or pending requests need to be canceled. */
719         pm_runtime_cancel_pending(dev);
720
721         dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
722         dev->power.timer_expires += !dev->power.timer_expires;
723         dev->power.timer_autosuspends = 0;
724         mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
725
726  out:
727         spin_unlock_irqrestore(&dev->power.lock, flags);
728
729         return retval;
730 }
731 EXPORT_SYMBOL_GPL(pm_schedule_suspend);
732
733 /**
734  * __pm_runtime_idle - Entry point for run-time idle operations.
735  * @dev: Device to send idle notification for.
736  * @rpmflags: Flag bits.
737  *
738  * If the RPM_GET_PUT flag is set, decrement the device's usage count and
739  * return immediately if it is larger than zero.  Then carry out an idle
740  * notification, either synchronous or asynchronous.
741  *
742  * This routine may be called in atomic context if the RPM_ASYNC flag is set.
743  */
744 int __pm_runtime_idle(struct device *dev, int rpmflags)
745 {
746         unsigned long flags;
747         int retval;
748
749         if (rpmflags & RPM_GET_PUT) {
750                 if (!atomic_dec_and_test(&dev->power.usage_count))
751                         return 0;
752         }
753
754         spin_lock_irqsave(&dev->power.lock, flags);
755         retval = rpm_idle(dev, rpmflags);
756         spin_unlock_irqrestore(&dev->power.lock, flags);
757
758         return retval;
759 }
760 EXPORT_SYMBOL_GPL(__pm_runtime_idle);
761
762 /**
763  * __pm_runtime_suspend - Entry point for run-time put/suspend operations.
764  * @dev: Device to suspend.
765  * @rpmflags: Flag bits.
766  *
767  * If the RPM_GET_PUT flag is set, decrement the device's usage count and
768  * return immediately if it is larger than zero.  Then carry out a suspend,
769  * either synchronous or asynchronous.
770  *
771  * This routine may be called in atomic context if the RPM_ASYNC flag is set.
772  */
773 int __pm_runtime_suspend(struct device *dev, int rpmflags)
774 {
775         unsigned long flags;
776         int retval;
777
778         if (rpmflags & RPM_GET_PUT) {
779                 if (!atomic_dec_and_test(&dev->power.usage_count))
780                         return 0;
781         }
782
783         spin_lock_irqsave(&dev->power.lock, flags);
784         retval = rpm_suspend(dev, rpmflags);
785         spin_unlock_irqrestore(&dev->power.lock, flags);
786
787         return retval;
788 }
789 EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
790
791 /**
792  * __pm_runtime_resume - Entry point for run-time resume operations.
793  * @dev: Device to resume.
794  * @rpmflags: Flag bits.
795  *
796  * If the RPM_GET_PUT flag is set, increment the device's usage count.  Then
797  * carry out a resume, either synchronous or asynchronous.
798  *
799  * This routine may be called in atomic context if the RPM_ASYNC flag is set.
800  */
801 int __pm_runtime_resume(struct device *dev, int rpmflags)
802 {
803         unsigned long flags;
804         int retval;
805
806         if (rpmflags & RPM_GET_PUT)
807                 atomic_inc(&dev->power.usage_count);
808
809         spin_lock_irqsave(&dev->power.lock, flags);
810         retval = rpm_resume(dev, rpmflags);
811         spin_unlock_irqrestore(&dev->power.lock, flags);
812
813         return retval;
814 }
815 EXPORT_SYMBOL_GPL(__pm_runtime_resume);
816
817 /**
818  * __pm_runtime_set_status - Set run-time PM status of a device.
819  * @dev: Device to handle.
820  * @status: New run-time PM status of the device.
821  *
822  * If run-time PM of the device is disabled or its power.runtime_error field is
823  * different from zero, the status may be changed either to RPM_ACTIVE, or to
824  * RPM_SUSPENDED, as long as that reflects the actual state of the device.
825  * However, if the device has a parent and the parent is not active, and the
826  * parent's power.ignore_children flag is unset, the device's status cannot be
827  * set to RPM_ACTIVE, so -EBUSY is returned in that case.
828  *
829  * If successful, __pm_runtime_set_status() clears the power.runtime_error field
830  * and the device parent's counter of unsuspended children is modified to
831  * reflect the new status.  If the new status is RPM_SUSPENDED, an idle
832  * notification request for the parent is submitted.
833  */
834 int __pm_runtime_set_status(struct device *dev, unsigned int status)
835 {
836         struct device *parent = dev->parent;
837         unsigned long flags;
838         bool notify_parent = false;
839         int error = 0;
840
841         if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
842                 return -EINVAL;
843
844         spin_lock_irqsave(&dev->power.lock, flags);
845
846         if (!dev->power.runtime_error && !dev->power.disable_depth) {
847                 error = -EAGAIN;
848                 goto out;
849         }
850
851         if (dev->power.runtime_status == status)
852                 goto out_set;
853
854         if (status == RPM_SUSPENDED) {
855                 /* It always is possible to set the status to 'suspended'. */
856                 if (parent) {
857                         atomic_add_unless(&parent->power.child_count, -1, 0);
858                         notify_parent = !parent->power.ignore_children;
859                 }
860                 goto out_set;
861         }
862
863         if (parent) {
864                 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
865
866                 /*
867                  * It is invalid to put an active child under a parent that is
868                  * not active, has run-time PM enabled and the
869                  * 'power.ignore_children' flag unset.
870                  */
871                 if (!parent->power.disable_depth
872                     && !parent->power.ignore_children
873                     && parent->power.runtime_status != RPM_ACTIVE)
874                         error = -EBUSY;
875                 else if (dev->power.runtime_status == RPM_SUSPENDED)
876                         atomic_inc(&parent->power.child_count);
877
878                 spin_unlock(&parent->power.lock);
879
880                 if (error)
881                         goto out;
882         }
883
884  out_set:
885         __update_runtime_status(dev, status);
886         dev->power.runtime_error = 0;
887  out:
888         spin_unlock_irqrestore(&dev->power.lock, flags);
889
890         if (notify_parent)
891                 pm_request_idle(parent);
892
893         return error;
894 }
895 EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
896
897 /**
898  * __pm_runtime_barrier - Cancel pending requests and wait for completions.
899  * @dev: Device to handle.
900  *
901  * Flush all pending requests for the device from pm_wq and wait for all
902  * run-time PM operations involving the device in progress to complete.
903  *
904  * Should be called under dev->power.lock with interrupts disabled.
905  */
906 static void __pm_runtime_barrier(struct device *dev)
907 {
908         pm_runtime_deactivate_timer(dev);
909
910         if (dev->power.request_pending) {
911                 dev->power.request = RPM_REQ_NONE;
912                 spin_unlock_irq(&dev->power.lock);
913
914                 cancel_work_sync(&dev->power.work);
915
916                 spin_lock_irq(&dev->power.lock);
917                 dev->power.request_pending = false;
918         }
919
920         if (dev->power.runtime_status == RPM_SUSPENDING
921             || dev->power.runtime_status == RPM_RESUMING
922             || dev->power.idle_notification) {
923                 DEFINE_WAIT(wait);
924
925                 /* Suspend, wake-up or idle notification in progress. */
926                 for (;;) {
927                         prepare_to_wait(&dev->power.wait_queue, &wait,
928                                         TASK_UNINTERRUPTIBLE);
929                         if (dev->power.runtime_status != RPM_SUSPENDING
930                             && dev->power.runtime_status != RPM_RESUMING
931                             && !dev->power.idle_notification)
932                                 break;
933                         spin_unlock_irq(&dev->power.lock);
934
935                         schedule();
936
937                         spin_lock_irq(&dev->power.lock);
938                 }
939                 finish_wait(&dev->power.wait_queue, &wait);
940         }
941 }
942
943 /**
944  * pm_runtime_barrier - Flush pending requests and wait for completions.
945  * @dev: Device to handle.
946  *
947  * Prevent the device from being suspended by incrementing its usage counter and
948  * if there's a pending resume request for the device, wake the device up.
949  * Next, make sure that all pending requests for the device have been flushed
950  * from pm_wq and wait for all run-time PM operations involving the device in
951  * progress to complete.
952  *
953  * Return value:
954  * 1, if there was a resume request pending and the device had to be woken up,
955  * 0, otherwise
956  */
957 int pm_runtime_barrier(struct device *dev)
958 {
959         int retval = 0;
960
961         pm_runtime_get_noresume(dev);
962         spin_lock_irq(&dev->power.lock);
963
964         if (dev->power.request_pending
965             && dev->power.request == RPM_REQ_RESUME) {
966                 rpm_resume(dev, 0);
967                 retval = 1;
968         }
969
970         __pm_runtime_barrier(dev);
971
972         spin_unlock_irq(&dev->power.lock);
973         pm_runtime_put_noidle(dev);
974
975         return retval;
976 }
977 EXPORT_SYMBOL_GPL(pm_runtime_barrier);
978
979 /**
980  * __pm_runtime_disable - Disable run-time PM of a device.
981  * @dev: Device to handle.
982  * @check_resume: If set, check if there's a resume request for the device.
983  *
984  * Increment power.disable_depth for the device and if was zero previously,
985  * cancel all pending run-time PM requests for the device and wait for all
986  * operations in progress to complete.  The device can be either active or
987  * suspended after its run-time PM has been disabled.
988  *
989  * If @check_resume is set and there's a resume request pending when
990  * __pm_runtime_disable() is called and power.disable_depth is zero, the
991  * function will wake up the device before disabling its run-time PM.
992  */
993 void __pm_runtime_disable(struct device *dev, bool check_resume)
994 {
995         spin_lock_irq(&dev->power.lock);
996
997         if (dev->power.disable_depth > 0) {
998                 dev->power.disable_depth++;
999                 goto out;
1000         }
1001
1002         /*
1003          * Wake up the device if there's a resume request pending, because that
1004          * means there probably is some I/O to process and disabling run-time PM
1005          * shouldn't prevent the device from processing the I/O.
1006          */
1007         if (check_resume && dev->power.request_pending
1008             && dev->power.request == RPM_REQ_RESUME) {
1009                 /*
1010                  * Prevent suspends and idle notifications from being carried
1011                  * out after we have woken up the device.
1012                  */
1013                 pm_runtime_get_noresume(dev);
1014
1015                 rpm_resume(dev, 0);
1016
1017                 pm_runtime_put_noidle(dev);
1018         }
1019
1020         if (!dev->power.disable_depth++)
1021                 __pm_runtime_barrier(dev);
1022
1023  out:
1024         spin_unlock_irq(&dev->power.lock);
1025 }
1026 EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1027
1028 /**
1029  * pm_runtime_enable - Enable run-time PM of a device.
1030  * @dev: Device to handle.
1031  */
1032 void pm_runtime_enable(struct device *dev)
1033 {
1034         unsigned long flags;
1035
1036         spin_lock_irqsave(&dev->power.lock, flags);
1037
1038         if (dev->power.disable_depth > 0)
1039                 dev->power.disable_depth--;
1040         else
1041                 dev_warn(dev, "Unbalanced %s!\n", __func__);
1042
1043         spin_unlock_irqrestore(&dev->power.lock, flags);
1044 }
1045 EXPORT_SYMBOL_GPL(pm_runtime_enable);
1046
1047 /**
1048  * pm_runtime_forbid - Block run-time PM of a device.
1049  * @dev: Device to handle.
1050  *
1051  * Increase the device's usage count and clear its power.runtime_auto flag,
1052  * so that it cannot be suspended at run time until pm_runtime_allow() is called
1053  * for it.
1054  */
1055 void pm_runtime_forbid(struct device *dev)
1056 {
1057         spin_lock_irq(&dev->power.lock);
1058         if (!dev->power.runtime_auto)
1059                 goto out;
1060
1061         dev->power.runtime_auto = false;
1062         atomic_inc(&dev->power.usage_count);
1063         rpm_resume(dev, 0);
1064
1065  out:
1066         spin_unlock_irq(&dev->power.lock);
1067 }
1068 EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1069
1070 /**
1071  * pm_runtime_allow - Unblock run-time PM of a device.
1072  * @dev: Device to handle.
1073  *
1074  * Decrease the device's usage count and set its power.runtime_auto flag.
1075  */
1076 void pm_runtime_allow(struct device *dev)
1077 {
1078         spin_lock_irq(&dev->power.lock);
1079         if (dev->power.runtime_auto)
1080                 goto out;
1081
1082         dev->power.runtime_auto = true;
1083         if (atomic_dec_and_test(&dev->power.usage_count))
1084                 rpm_idle(dev, RPM_AUTO);
1085
1086  out:
1087         spin_unlock_irq(&dev->power.lock);
1088 }
1089 EXPORT_SYMBOL_GPL(pm_runtime_allow);
1090
1091 /**
1092  * pm_runtime_no_callbacks - Ignore run-time PM callbacks for a device.
1093  * @dev: Device to handle.
1094  *
1095  * Set the power.no_callbacks flag, which tells the PM core that this
1096  * device is power-managed through its parent and has no run-time PM
1097  * callbacks of its own.  The run-time sysfs attributes will be removed.
1098  */
1099 void pm_runtime_no_callbacks(struct device *dev)
1100 {
1101         spin_lock_irq(&dev->power.lock);
1102         dev->power.no_callbacks = 1;
1103         spin_unlock_irq(&dev->power.lock);
1104         if (device_is_registered(dev))
1105                 rpm_sysfs_remove(dev);
1106 }
1107 EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1108
1109 /**
1110  * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1111  * @dev: Device to handle
1112  *
1113  * Set the power.irq_safe flag, which tells the PM core that the
1114  * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1115  * always be invoked with the spinlock held and interrupts disabled.  It also
1116  * causes the parent's usage counter to be permanently incremented, preventing
1117  * the parent from runtime suspending -- otherwise an irq-safe child might have
1118  * to wait for a non-irq-safe parent.
1119  */
1120 void pm_runtime_irq_safe(struct device *dev)
1121 {
1122         if (dev->parent)
1123                 pm_runtime_get_sync(dev->parent);
1124         spin_lock_irq(&dev->power.lock);
1125         dev->power.irq_safe = 1;
1126         spin_unlock_irq(&dev->power.lock);
1127 }
1128 EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1129
1130 /**
1131  * update_autosuspend - Handle a change to a device's autosuspend settings.
1132  * @dev: Device to handle.
1133  * @old_delay: The former autosuspend_delay value.
1134  * @old_use: The former use_autosuspend value.
1135  *
1136  * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1137  * set; otherwise allow it.  Send an idle notification if suspends are allowed.
1138  *
1139  * This function must be called under dev->power.lock with interrupts disabled.
1140  */
1141 static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1142 {
1143         int delay = dev->power.autosuspend_delay;
1144
1145         /* Should runtime suspend be prevented now? */
1146         if (dev->power.use_autosuspend && delay < 0) {
1147
1148                 /* If it used to be allowed then prevent it. */
1149                 if (!old_use || old_delay >= 0) {
1150                         atomic_inc(&dev->power.usage_count);
1151                         rpm_resume(dev, 0);
1152                 }
1153         }
1154
1155         /* Runtime suspend should be allowed now. */
1156         else {
1157
1158                 /* If it used to be prevented then allow it. */
1159                 if (old_use && old_delay < 0)
1160                         atomic_dec(&dev->power.usage_count);
1161
1162                 /* Maybe we can autosuspend now. */
1163                 rpm_idle(dev, RPM_AUTO);
1164         }
1165 }
1166
1167 /**
1168  * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1169  * @dev: Device to handle.
1170  * @delay: Value of the new delay in milliseconds.
1171  *
1172  * Set the device's power.autosuspend_delay value.  If it changes to negative
1173  * and the power.use_autosuspend flag is set, prevent run-time suspends.  If it
1174  * changes the other way, allow run-time suspends.
1175  */
1176 void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1177 {
1178         int old_delay, old_use;
1179
1180         spin_lock_irq(&dev->power.lock);
1181         old_delay = dev->power.autosuspend_delay;
1182         old_use = dev->power.use_autosuspend;
1183         dev->power.autosuspend_delay = delay;
1184         update_autosuspend(dev, old_delay, old_use);
1185         spin_unlock_irq(&dev->power.lock);
1186 }
1187 EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1188
1189 /**
1190  * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1191  * @dev: Device to handle.
1192  * @use: New value for use_autosuspend.
1193  *
1194  * Set the device's power.use_autosuspend flag, and allow or prevent run-time
1195  * suspends as needed.
1196  */
1197 void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1198 {
1199         int old_delay, old_use;
1200
1201         spin_lock_irq(&dev->power.lock);
1202         old_delay = dev->power.autosuspend_delay;
1203         old_use = dev->power.use_autosuspend;
1204         dev->power.use_autosuspend = use;
1205         update_autosuspend(dev, old_delay, old_use);
1206         spin_unlock_irq(&dev->power.lock);
1207 }
1208 EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1209
1210 /**
1211  * pm_runtime_init - Initialize run-time PM fields in given device object.
1212  * @dev: Device object to initialize.
1213  */
1214 void pm_runtime_init(struct device *dev)
1215 {
1216         dev->power.runtime_status = RPM_SUSPENDED;
1217         dev->power.idle_notification = false;
1218
1219         dev->power.disable_depth = 1;
1220         atomic_set(&dev->power.usage_count, 0);
1221
1222         dev->power.runtime_error = 0;
1223
1224         atomic_set(&dev->power.child_count, 0);
1225         pm_suspend_ignore_children(dev, false);
1226         dev->power.runtime_auto = true;
1227
1228         dev->power.request_pending = false;
1229         dev->power.request = RPM_REQ_NONE;
1230         dev->power.deferred_resume = false;
1231         dev->power.accounting_timestamp = jiffies;
1232         INIT_WORK(&dev->power.work, pm_runtime_work);
1233
1234         dev->power.timer_expires = 0;
1235         setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1236                         (unsigned long)dev);
1237
1238         init_waitqueue_head(&dev->power.wait_queue);
1239 }
1240
1241 /**
1242  * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1243  * @dev: Device object being removed from device hierarchy.
1244  */
1245 void pm_runtime_remove(struct device *dev)
1246 {
1247         __pm_runtime_disable(dev, false);
1248
1249         /* Change the status back to 'suspended' to match the initial status. */
1250         if (dev->power.runtime_status == RPM_ACTIVE)
1251                 pm_runtime_set_suspended(dev);
1252         if (dev->power.irq_safe && dev->parent)
1253                 pm_runtime_put_sync(dev->parent);
1254 }