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