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