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