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