driver core: Do not resume suppliers under device_links_write_lock()
[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
5b3cc15a 10#include <linux/sched/mm.h>
8234f673
VG
11#include <linux/ktime.h>
12#include <linux/hrtimer.h>
1b6bc32f 13#include <linux/export.h>
5e928f77 14#include <linux/pm_runtime.h>
4990d4fe 15#include <linux/pm_wakeirq.h>
c3dc2f14 16#include <trace/events/rpm.h>
21d5c57b
RW
17
18#include "../base.h"
7490e442 19#include "power.h"
5e928f77 20
dbcd2d72 21typedef int (*pm_callback_t)(struct device *);
5f59df79 22
dbcd2d72 23static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset)
5f59df79 24{
dbcd2d72
AH
25 pm_callback_t cb;
26 const struct dev_pm_ops *ops;
27
28 if (dev->pm_domain)
29 ops = &dev->pm_domain->ops;
30 else if (dev->type && dev->type->pm)
31 ops = dev->type->pm;
32 else if (dev->class && dev->class->pm)
33 ops = dev->class->pm;
34 else if (dev->bus && dev->bus->pm)
35 ops = dev->bus->pm;
36 else
37 ops = NULL;
38
39 if (ops)
40 cb = *(pm_callback_t *)((void *)ops + cb_offset);
41 else
42 cb = NULL;
43
44 if (!cb && dev->driver && dev->driver->pm)
45 cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset);
46
47 return cb;
5f59df79
UH
48}
49
dbcd2d72
AH
50#define RPM_GET_CALLBACK(dev, callback) \
51 __rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback))
52
140a6c94 53static int rpm_resume(struct device *dev, int rpmflags);
7490e442 54static int rpm_suspend(struct device *dev, int rpmflags);
5e928f77 55
4769373c
AS
56/**
57 * update_pm_runtime_accounting - Update the time accounting of power states
58 * @dev: Device to update the accounting for
59 *
60 * In order to be able to have time accounting of the various power states
61 * (as used by programs such as PowerTOP to show the effectiveness of runtime
62 * PM), we need to track the time spent in each state.
63 * update_pm_runtime_accounting must be called each time before the
64 * runtime_status field is updated, to account the time in the old state
65 * correctly.
66 */
67void update_pm_runtime_accounting(struct device *dev)
68{
69 unsigned long now = jiffies;
def0c0a3 70 unsigned long delta;
4769373c
AS
71
72 delta = now - dev->power.accounting_timestamp;
73
4769373c
AS
74 dev->power.accounting_timestamp = now;
75
76 if (dev->power.disable_depth > 0)
77 return;
78
79 if (dev->power.runtime_status == RPM_SUSPENDED)
80 dev->power.suspended_jiffies += delta;
81 else
82 dev->power.active_jiffies += delta;
83}
84
85static void __update_runtime_status(struct device *dev, enum rpm_status status)
86{
87 update_pm_runtime_accounting(dev);
88 dev->power.runtime_status = status;
89}
90
5e928f77
RW
91/**
92 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
93 * @dev: Device to handle.
94 */
95static void pm_runtime_deactivate_timer(struct device *dev)
96{
97 if (dev->power.timer_expires > 0) {
8234f673 98 hrtimer_cancel(&dev->power.suspend_timer);
5e928f77
RW
99 dev->power.timer_expires = 0;
100 }
101}
102
103/**
104 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
105 * @dev: Device to handle.
106 */
107static void pm_runtime_cancel_pending(struct device *dev)
108{
109 pm_runtime_deactivate_timer(dev);
110 /*
111 * In case there's a request pending, make sure its work function will
112 * return without doing anything.
113 */
114 dev->power.request = RPM_REQ_NONE;
115}
116
15bcb91d
AS
117/*
118 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
119 * @dev: Device to handle.
120 *
121 * Compute the autosuspend-delay expiration time based on the device's
122 * power.last_busy time. If the delay has already expired or is disabled
123 * (negative) or the power.use_autosuspend flag isn't set, return 0.
1f7b7081 124 * Otherwise return the expiration time in nanoseconds (adjusted to be nonzero).
15bcb91d
AS
125 *
126 * This function may be called either with or without dev->power.lock held.
127 * Either way it can be racy, since power.last_busy may be updated at any time.
128 */
8234f673 129u64 pm_runtime_autosuspend_expiration(struct device *dev)
15bcb91d
AS
130{
131 int autosuspend_delay;
8234f673
VG
132 u64 last_busy, expires = 0;
133 u64 now = ktime_to_ns(ktime_get());
15bcb91d
AS
134
135 if (!dev->power.use_autosuspend)
136 goto out;
137
6aa7de05 138 autosuspend_delay = READ_ONCE(dev->power.autosuspend_delay);
15bcb91d
AS
139 if (autosuspend_delay < 0)
140 goto out;
141
6aa7de05 142 last_busy = READ_ONCE(dev->power.last_busy);
15bcb91d 143
ca27e4cd 144 expires = last_busy + (u64)autosuspend_delay * NSEC_PER_MSEC;
8234f673 145 if (expires <= now)
15bcb91d
AS
146 expires = 0; /* Already expired. */
147
148 out:
149 return expires;
150}
151EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
152
e823407f
ML
153static int dev_memalloc_noio(struct device *dev, void *data)
154{
155 return dev->power.memalloc_noio;
156}
157
158/*
159 * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
160 * @dev: Device to handle.
161 * @enable: True for setting the flag and False for clearing the flag.
162 *
163 * Set the flag for all devices in the path from the device to the
164 * root device in the device tree if @enable is true, otherwise clear
165 * the flag for devices in the path whose siblings don't set the flag.
166 *
167 * The function should only be called by block device, or network
168 * device driver for solving the deadlock problem during runtime
169 * resume/suspend:
170 *
171 * If memory allocation with GFP_KERNEL is called inside runtime
172 * resume/suspend callback of any one of its ancestors(or the
173 * block device itself), the deadlock may be triggered inside the
174 * memory allocation since it might not complete until the block
175 * device becomes active and the involed page I/O finishes. The
176 * situation is pointed out first by Alan Stern. Network device
177 * are involved in iSCSI kind of situation.
178 *
179 * The lock of dev_hotplug_mutex is held in the function for handling
180 * hotplug race because pm_runtime_set_memalloc_noio() may be called
181 * in async probe().
182 *
183 * The function should be called between device_add() and device_del()
184 * on the affected device(block/network device).
185 */
186void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
187{
188 static DEFINE_MUTEX(dev_hotplug_mutex);
189
190 mutex_lock(&dev_hotplug_mutex);
191 for (;;) {
192 bool enabled;
193
194 /* hold power lock since bitfield is not SMP-safe. */
195 spin_lock_irq(&dev->power.lock);
196 enabled = dev->power.memalloc_noio;
197 dev->power.memalloc_noio = enable;
198 spin_unlock_irq(&dev->power.lock);
199
200 /*
201 * not need to enable ancestors any more if the device
202 * has been enabled.
203 */
204 if (enabled && enable)
205 break;
206
207 dev = dev->parent;
208
209 /*
210 * clear flag of the parent device only if all the
211 * children don't set the flag because ancestor's
212 * flag was set by any one of the descendants.
213 */
214 if (!dev || (!enable &&
215 device_for_each_child(dev, NULL,
216 dev_memalloc_noio)))
217 break;
218 }
219 mutex_unlock(&dev_hotplug_mutex);
220}
221EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
222
5e928f77 223/**
1bfee5bc
AS
224 * rpm_check_suspend_allowed - Test whether a device may be suspended.
225 * @dev: Device to test.
5e928f77 226 */
1bfee5bc 227static int rpm_check_suspend_allowed(struct device *dev)
5e928f77
RW
228{
229 int retval = 0;
230
5e928f77
RW
231 if (dev->power.runtime_error)
232 retval = -EINVAL;
632e270e
RW
233 else if (dev->power.disable_depth > 0)
234 retval = -EACCES;
235 else if (atomic_read(&dev->power.usage_count) > 0)
5e928f77 236 retval = -EAGAIN;
62006c17
UH
237 else if (!dev->power.ignore_children &&
238 atomic_read(&dev->power.child_count))
5e928f77 239 retval = -EBUSY;
1bfee5bc
AS
240
241 /* Pending resume requests take precedence over suspends. */
242 else if ((dev->power.deferred_resume
78ca7c37 243 && dev->power.runtime_status == RPM_SUSPENDING)
1bfee5bc
AS
244 || (dev->power.request_pending
245 && dev->power.request == RPM_REQ_RESUME))
246 retval = -EAGAIN;
0759e80b 247 else if (__dev_pm_qos_read_value(dev) == 0)
55d7ec45 248 retval = -EPERM;
1bfee5bc
AS
249 else if (dev->power.runtime_status == RPM_SUSPENDED)
250 retval = 1;
251
252 return retval;
253}
254
21d5c57b
RW
255static int rpm_get_suppliers(struct device *dev)
256{
257 struct device_link *link;
258
259 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node) {
260 int retval;
261
262 if (!(link->flags & DL_FLAG_PM_RUNTIME))
263 continue;
264
265 if (READ_ONCE(link->status) == DL_STATE_SUPPLIER_UNBIND ||
266 link->rpm_active)
267 continue;
268
269 retval = pm_runtime_get_sync(link->supplier);
31eb7431
RW
270 /* Ignore suppliers with disabled runtime PM. */
271 if (retval < 0 && retval != -EACCES) {
21d5c57b
RW
272 pm_runtime_put_noidle(link->supplier);
273 return retval;
274 }
275 link->rpm_active = true;
276 }
277 return 0;
278}
279
280static void rpm_put_suppliers(struct device *dev)
281{
282 struct device_link *link;
283
284 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
285 if (link->rpm_active &&
286 READ_ONCE(link->status) != DL_STATE_SUPPLIER_UNBIND) {
287 pm_runtime_put(link->supplier);
288 link->rpm_active = false;
289 }
290}
291
ad3c36a5
RW
292/**
293 * __rpm_callback - Run a given runtime PM callback for a given device.
294 * @cb: Runtime PM callback to run.
295 * @dev: Device to run the callback for.
296 */
297static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
298 __releases(&dev->power.lock) __acquires(&dev->power.lock)
299{
21d5c57b 300 int retval, idx;
baa8809f 301 bool use_links = dev->power.links_count > 0;
ad3c36a5 302
21d5c57b 303 if (dev->power.irq_safe) {
ad3c36a5 304 spin_unlock(&dev->power.lock);
21d5c57b 305 } else {
ad3c36a5
RW
306 spin_unlock_irq(&dev->power.lock);
307
21d5c57b
RW
308 /*
309 * Resume suppliers if necessary.
310 *
311 * The device's runtime PM status cannot change until this
312 * routine returns, so it is safe to read the status outside of
313 * the lock.
314 */
baa8809f 315 if (use_links && dev->power.runtime_status == RPM_RESUMING) {
21d5c57b
RW
316 idx = device_links_read_lock();
317
318 retval = rpm_get_suppliers(dev);
319 if (retval)
320 goto fail;
321
322 device_links_read_unlock(idx);
323 }
324 }
325
ad3c36a5
RW
326 retval = cb(dev);
327
21d5c57b 328 if (dev->power.irq_safe) {
ad3c36a5 329 spin_lock(&dev->power.lock);
21d5c57b
RW
330 } else {
331 /*
332 * If the device is suspending and the callback has returned
333 * success, drop the usage counters of the suppliers that have
334 * been reference counted on its resume.
335 *
336 * Do that if resume fails too.
337 */
baa8809f
RW
338 if (use_links
339 && ((dev->power.runtime_status == RPM_SUSPENDING && !retval)
340 || (dev->power.runtime_status == RPM_RESUMING && retval))) {
21d5c57b
RW
341 idx = device_links_read_lock();
342
343 fail:
344 rpm_put_suppliers(dev);
345
346 device_links_read_unlock(idx);
347 }
348
ad3c36a5 349 spin_lock_irq(&dev->power.lock);
21d5c57b 350 }
ad3c36a5
RW
351
352 return retval;
353}
354
1bfee5bc 355/**
140a6c94 356 * rpm_idle - Notify device bus type if the device can be suspended.
1bfee5bc
AS
357 * @dev: Device to notify the bus type about.
358 * @rpmflags: Flag bits.
359 *
62052ab1 360 * Check if the device's runtime PM status allows it to be suspended. If
1bfee5bc
AS
361 * another idle notification has been started earlier, return immediately. If
362 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
d66e6db2
UH
363 * run the ->runtime_idle() callback directly. If the ->runtime_idle callback
364 * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag.
1bfee5bc
AS
365 *
366 * This function must be called under dev->power.lock with interrupts disabled.
367 */
140a6c94 368static int rpm_idle(struct device *dev, int rpmflags)
1bfee5bc 369{
71c63122 370 int (*callback)(struct device *);
1bfee5bc
AS
371 int retval;
372
d7737ce9 373 trace_rpm_idle_rcuidle(dev, rpmflags);
1bfee5bc
AS
374 retval = rpm_check_suspend_allowed(dev);
375 if (retval < 0)
376 ; /* Conditions are wrong. */
377
378 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
379 else if (dev->power.runtime_status != RPM_ACTIVE)
380 retval = -EAGAIN;
381
382 /*
383 * Any pending request other than an idle notification takes
384 * precedence over us, except that the timer may be running.
385 */
386 else if (dev->power.request_pending &&
387 dev->power.request > RPM_REQ_IDLE)
388 retval = -EAGAIN;
389
390 /* Act as though RPM_NOWAIT is always set. */
391 else if (dev->power.idle_notification)
392 retval = -EINPROGRESS;
5e928f77
RW
393 if (retval)
394 goto out;
395
1bfee5bc
AS
396 /* Pending requests need to be canceled. */
397 dev->power.request = RPM_REQ_NONE;
398
45f0a85c 399 if (dev->power.no_callbacks)
7490e442 400 goto out;
7490e442 401
1bfee5bc
AS
402 /* Carry out an asynchronous or a synchronous idle notification. */
403 if (rpmflags & RPM_ASYNC) {
404 dev->power.request = RPM_REQ_IDLE;
405 if (!dev->power.request_pending) {
406 dev->power.request_pending = true;
407 queue_work(pm_wq, &dev->power.work);
5e928f77 408 }
d7737ce9 409 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, 0);
45f0a85c 410 return 0;
5e928f77
RW
411 }
412
413 dev->power.idle_notification = true;
414
dbcd2d72 415 callback = RPM_GET_CALLBACK(dev, runtime_idle);
35cd133c 416
ad3c36a5 417 if (callback)
45f0a85c 418 retval = __rpm_callback(callback, dev);
5e928f77
RW
419
420 dev->power.idle_notification = false;
421 wake_up_all(&dev->power.wait_queue);
422
423 out:
d7737ce9 424 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
d66e6db2 425 return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
5e928f77
RW
426}
427
71c63122
RW
428/**
429 * rpm_callback - Run a given runtime PM callback for a given device.
430 * @cb: Runtime PM callback to run.
431 * @dev: Device to run the callback for.
432 */
433static int rpm_callback(int (*cb)(struct device *), struct device *dev)
71c63122
RW
434{
435 int retval;
436
437 if (!cb)
438 return -ENOSYS;
439
db88175f
ML
440 if (dev->power.memalloc_noio) {
441 unsigned int noio_flag;
442
443 /*
444 * Deadlock might be caused if memory allocation with
445 * GFP_KERNEL happens inside runtime_suspend and
446 * runtime_resume callbacks of one block device's
447 * ancestor or the block device itself. Network
448 * device might be thought as part of iSCSI block
449 * device, so network device and its ancestor should
450 * be marked as memalloc_noio too.
451 */
452 noio_flag = memalloc_noio_save();
453 retval = __rpm_callback(cb, dev);
454 memalloc_noio_restore(noio_flag);
455 } else {
456 retval = __rpm_callback(cb, dev);
457 }
71c63122 458
71c63122 459 dev->power.runtime_error = retval;
632e270e 460 return retval != -EACCES ? retval : -EIO;
71c63122
RW
461}
462
5e928f77 463/**
62052ab1 464 * rpm_suspend - Carry out runtime suspend of given device.
5e928f77 465 * @dev: Device to suspend.
3f9af051 466 * @rpmflags: Flag bits.
5e928f77 467 *
47d8f0ba
ML
468 * Check if the device's runtime PM status allows it to be suspended.
469 * Cancel a pending idle notification, autosuspend or suspend. If
470 * another suspend has been started earlier, either return immediately
471 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
472 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
857b36c7
ML
473 * otherwise run the ->runtime_suspend() callback directly. When
474 * ->runtime_suspend succeeded, if a deferred resume was requested while
475 * the callback was running then carry it out, otherwise send an idle
476 * notification for its parent (if the suspend succeeded and both
477 * ignore_children of parent->power and irq_safe of dev->power are not set).
886486b7
AS
478 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
479 * flag is set and the next autosuspend-delay expiration time is in the
480 * future, schedule another autosuspend attempt.
5e928f77
RW
481 *
482 * This function must be called under dev->power.lock with interrupts disabled.
483 */
140a6c94 484static int rpm_suspend(struct device *dev, int rpmflags)
5e928f77
RW
485 __releases(&dev->power.lock) __acquires(&dev->power.lock)
486{
71c63122 487 int (*callback)(struct device *);
5e928f77 488 struct device *parent = NULL;
1bfee5bc 489 int retval;
5e928f77 490
77893577 491 trace_rpm_suspend_rcuidle(dev, rpmflags);
5e928f77
RW
492
493 repeat:
1bfee5bc 494 retval = rpm_check_suspend_allowed(dev);
5e928f77 495
1bfee5bc
AS
496 if (retval < 0)
497 ; /* Conditions are wrong. */
498
499 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
500 else if (dev->power.runtime_status == RPM_RESUMING &&
501 !(rpmflags & RPM_ASYNC))
5e928f77 502 retval = -EAGAIN;
1bfee5bc 503 if (retval)
5e928f77 504 goto out;
5e928f77 505
15bcb91d
AS
506 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
507 if ((rpmflags & RPM_AUTO)
508 && dev->power.runtime_status != RPM_SUSPENDING) {
8234f673 509 u64 expires = pm_runtime_autosuspend_expiration(dev);
15bcb91d
AS
510
511 if (expires != 0) {
512 /* Pending requests need to be canceled. */
513 dev->power.request = RPM_REQ_NONE;
514
515 /*
516 * Optimization: If the timer is already running and is
517 * set to expire at or before the autosuspend delay,
518 * avoid the overhead of resetting it. Just let it
519 * expire; pm_suspend_timer_fn() will take care of the
520 * rest.
521 */
8234f673
VG
522 if (!(dev->power.timer_expires &&
523 dev->power.timer_expires <= expires)) {
524 /*
525 * We add a slack of 25% to gather wakeups
526 * without sacrificing the granularity.
527 */
ca27e4cd 528 u64 slack = (u64)READ_ONCE(dev->power.autosuspend_delay) *
8234f673
VG
529 (NSEC_PER_MSEC >> 2);
530
15bcb91d 531 dev->power.timer_expires = expires;
8234f673
VG
532 hrtimer_start_range_ns(&dev->power.suspend_timer,
533 ns_to_ktime(expires),
534 slack,
535 HRTIMER_MODE_ABS);
15bcb91d
AS
536 }
537 dev->power.timer_autosuspends = 1;
538 goto out;
539 }
540 }
541
5e928f77
RW
542 /* Other scheduled or pending requests need to be canceled. */
543 pm_runtime_cancel_pending(dev);
544
5e928f77
RW
545 if (dev->power.runtime_status == RPM_SUSPENDING) {
546 DEFINE_WAIT(wait);
547
1bfee5bc 548 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
5e928f77
RW
549 retval = -EINPROGRESS;
550 goto out;
551 }
552
ad3c36a5
RW
553 if (dev->power.irq_safe) {
554 spin_unlock(&dev->power.lock);
555
556 cpu_relax();
557
558 spin_lock(&dev->power.lock);
559 goto repeat;
560 }
561
5e928f77
RW
562 /* Wait for the other suspend running in parallel with us. */
563 for (;;) {
564 prepare_to_wait(&dev->power.wait_queue, &wait,
565 TASK_UNINTERRUPTIBLE);
566 if (dev->power.runtime_status != RPM_SUSPENDING)
567 break;
568
569 spin_unlock_irq(&dev->power.lock);
570
571 schedule();
572
573 spin_lock_irq(&dev->power.lock);
574 }
575 finish_wait(&dev->power.wait_queue, &wait);
576 goto repeat;
577 }
578
7490e442
AS
579 if (dev->power.no_callbacks)
580 goto no_callback; /* Assume success. */
581
1bfee5bc
AS
582 /* Carry out an asynchronous or a synchronous suspend. */
583 if (rpmflags & RPM_ASYNC) {
15bcb91d
AS
584 dev->power.request = (rpmflags & RPM_AUTO) ?
585 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
1bfee5bc
AS
586 if (!dev->power.request_pending) {
587 dev->power.request_pending = true;
588 queue_work(pm_wq, &dev->power.work);
589 }
590 goto out;
591 }
592
8d4b9d1b 593 __update_runtime_status(dev, RPM_SUSPENDING);
5e928f77 594
dbcd2d72 595 callback = RPM_GET_CALLBACK(dev, runtime_suspend);
35cd133c 596
bed57030 597 dev_pm_enable_wake_irq_check(dev, true);
71c63122 598 retval = rpm_callback(callback, dev);
00dc9ad1
RW
599 if (retval)
600 goto fail;
886486b7 601
7490e442 602 no_callback:
857b36c7
ML
603 __update_runtime_status(dev, RPM_SUSPENDED);
604 pm_runtime_deactivate_timer(dev);
5e928f77 605
857b36c7
ML
606 if (dev->parent) {
607 parent = dev->parent;
608 atomic_add_unless(&parent->power.child_count, -1, 0);
5e928f77
RW
609 }
610 wake_up_all(&dev->power.wait_queue);
611
612 if (dev->power.deferred_resume) {
58a34de7 613 dev->power.deferred_resume = false;
140a6c94 614 rpm_resume(dev, 0);
5e928f77
RW
615 retval = -EAGAIN;
616 goto out;
617 }
618
c3810c88 619 /* Maybe the parent is now able to suspend. */
c7b61de5 620 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
c3810c88 621 spin_unlock(&dev->power.lock);
5e928f77 622
c3810c88
AS
623 spin_lock(&parent->power.lock);
624 rpm_idle(parent, RPM_ASYNC);
625 spin_unlock(&parent->power.lock);
5e928f77 626
c3810c88 627 spin_lock(&dev->power.lock);
5e928f77
RW
628 }
629
630 out:
77893577 631 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
5e928f77
RW
632
633 return retval;
00dc9ad1
RW
634
635 fail:
bed57030 636 dev_pm_disable_wake_irq_check(dev);
00dc9ad1 637 __update_runtime_status(dev, RPM_ACTIVE);
00dc9ad1 638 dev->power.deferred_resume = false;
f2791d73
AS
639 wake_up_all(&dev->power.wait_queue);
640
00dc9ad1
RW
641 if (retval == -EAGAIN || retval == -EBUSY) {
642 dev->power.runtime_error = 0;
643
644 /*
645 * If the callback routine failed an autosuspend, and
646 * if the last_busy time has been updated so that there
647 * is a new autosuspend expiration time, automatically
648 * reschedule another autosuspend.
649 */
650 if ((rpmflags & RPM_AUTO) &&
651 pm_runtime_autosuspend_expiration(dev) != 0)
652 goto repeat;
653 } else {
654 pm_runtime_cancel_pending(dev);
655 }
00dc9ad1 656 goto out;
5e928f77
RW
657}
658
659/**
62052ab1 660 * rpm_resume - Carry out runtime resume of given device.
5e928f77 661 * @dev: Device to resume.
3f9af051 662 * @rpmflags: Flag bits.
5e928f77 663 *
62052ab1 664 * Check if the device's runtime PM status allows it to be resumed. Cancel
1bfee5bc 665 * any scheduled or pending requests. If another resume has been started
25985edc 666 * earlier, either return immediately or wait for it to finish, depending on the
1bfee5bc
AS
667 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
668 * parallel with this function, either tell the other process to resume after
669 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
670 * flag is set then queue a resume request; otherwise run the
671 * ->runtime_resume() callback directly. Queue an idle notification for the
672 * device if the resume succeeded.
5e928f77
RW
673 *
674 * This function must be called under dev->power.lock with interrupts disabled.
675 */
140a6c94 676static int rpm_resume(struct device *dev, int rpmflags)
5e928f77
RW
677 __releases(&dev->power.lock) __acquires(&dev->power.lock)
678{
71c63122 679 int (*callback)(struct device *);
5e928f77
RW
680 struct device *parent = NULL;
681 int retval = 0;
682
d44c950e 683 trace_rpm_resume_rcuidle(dev, rpmflags);
5e928f77
RW
684
685 repeat:
1bfee5bc 686 if (dev->power.runtime_error)
5e928f77 687 retval = -EINVAL;
6f3c77b0
KH
688 else if (dev->power.disable_depth == 1 && dev->power.is_suspended
689 && dev->power.runtime_status == RPM_ACTIVE)
690 retval = 1;
1bfee5bc 691 else if (dev->power.disable_depth > 0)
632e270e 692 retval = -EACCES;
1bfee5bc 693 if (retval)
5e928f77 694 goto out;
5e928f77 695
15bcb91d
AS
696 /*
697 * Other scheduled or pending requests need to be canceled. Small
698 * optimization: If an autosuspend timer is running, leave it running
699 * rather than cancelling it now only to restart it again in the near
700 * future.
701 */
702 dev->power.request = RPM_REQ_NONE;
703 if (!dev->power.timer_autosuspends)
704 pm_runtime_deactivate_timer(dev);
5e928f77 705
1bfee5bc 706 if (dev->power.runtime_status == RPM_ACTIVE) {
5e928f77 707 retval = 1;
5e928f77 708 goto out;
1bfee5bc 709 }
5e928f77
RW
710
711 if (dev->power.runtime_status == RPM_RESUMING
712 || dev->power.runtime_status == RPM_SUSPENDING) {
713 DEFINE_WAIT(wait);
714
1bfee5bc 715 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
5e928f77
RW
716 if (dev->power.runtime_status == RPM_SUSPENDING)
717 dev->power.deferred_resume = true;
1bfee5bc
AS
718 else
719 retval = -EINPROGRESS;
5e928f77
RW
720 goto out;
721 }
722
ad3c36a5
RW
723 if (dev->power.irq_safe) {
724 spin_unlock(&dev->power.lock);
725
726 cpu_relax();
727
728 spin_lock(&dev->power.lock);
729 goto repeat;
730 }
731
5e928f77
RW
732 /* Wait for the operation carried out in parallel with us. */
733 for (;;) {
734 prepare_to_wait(&dev->power.wait_queue, &wait,
735 TASK_UNINTERRUPTIBLE);
736 if (dev->power.runtime_status != RPM_RESUMING
737 && dev->power.runtime_status != RPM_SUSPENDING)
738 break;
739
740 spin_unlock_irq(&dev->power.lock);
741
742 schedule();
743
744 spin_lock_irq(&dev->power.lock);
745 }
746 finish_wait(&dev->power.wait_queue, &wait);
747 goto repeat;
748 }
749
7490e442
AS
750 /*
751 * See if we can skip waking up the parent. This is safe only if
752 * power.no_callbacks is set, because otherwise we don't know whether
753 * the resume will actually succeed.
754 */
755 if (dev->power.no_callbacks && !parent && dev->parent) {
d63be5f9 756 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
7490e442
AS
757 if (dev->parent->power.disable_depth > 0
758 || dev->parent->power.ignore_children
759 || dev->parent->power.runtime_status == RPM_ACTIVE) {
760 atomic_inc(&dev->parent->power.child_count);
761 spin_unlock(&dev->parent->power.lock);
7f321c26 762 retval = 1;
7490e442
AS
763 goto no_callback; /* Assume success. */
764 }
765 spin_unlock(&dev->parent->power.lock);
766 }
767
1bfee5bc
AS
768 /* Carry out an asynchronous or a synchronous resume. */
769 if (rpmflags & RPM_ASYNC) {
770 dev->power.request = RPM_REQ_RESUME;
771 if (!dev->power.request_pending) {
772 dev->power.request_pending = true;
773 queue_work(pm_wq, &dev->power.work);
774 }
775 retval = 0;
776 goto out;
777 }
778
5e928f77
RW
779 if (!parent && dev->parent) {
780 /*
c7b61de5
AS
781 * Increment the parent's usage counter and resume it if
782 * necessary. Not needed if dev is irq-safe; then the
783 * parent is permanently resumed.
5e928f77
RW
784 */
785 parent = dev->parent;
c7b61de5
AS
786 if (dev->power.irq_safe)
787 goto skip_parent;
862f89b3 788 spin_unlock(&dev->power.lock);
5e928f77
RW
789
790 pm_runtime_get_noresume(parent);
791
862f89b3 792 spin_lock(&parent->power.lock);
5e928f77 793 /*
216ef0b6
UH
794 * Resume the parent if it has runtime PM enabled and not been
795 * set to ignore its children.
5e928f77
RW
796 */
797 if (!parent->power.disable_depth
798 && !parent->power.ignore_children) {
140a6c94 799 rpm_resume(parent, 0);
5e928f77
RW
800 if (parent->power.runtime_status != RPM_ACTIVE)
801 retval = -EBUSY;
802 }
862f89b3 803 spin_unlock(&parent->power.lock);
5e928f77 804
862f89b3 805 spin_lock(&dev->power.lock);
5e928f77
RW
806 if (retval)
807 goto out;
808 goto repeat;
809 }
c7b61de5 810 skip_parent:
5e928f77 811
7490e442
AS
812 if (dev->power.no_callbacks)
813 goto no_callback; /* Assume success. */
814
8d4b9d1b 815 __update_runtime_status(dev, RPM_RESUMING);
5e928f77 816
dbcd2d72 817 callback = RPM_GET_CALLBACK(dev, runtime_resume);
35cd133c 818
bed57030 819 dev_pm_disable_wake_irq_check(dev);
71c63122 820 retval = rpm_callback(callback, dev);
5e928f77 821 if (retval) {
8d4b9d1b 822 __update_runtime_status(dev, RPM_SUSPENDED);
5e928f77 823 pm_runtime_cancel_pending(dev);
bed57030 824 dev_pm_enable_wake_irq_check(dev, false);
5e928f77 825 } else {
7490e442 826 no_callback:
8d4b9d1b 827 __update_runtime_status(dev, RPM_ACTIVE);
56f487c7 828 pm_runtime_mark_last_busy(dev);
5e928f77
RW
829 if (parent)
830 atomic_inc(&parent->power.child_count);
831 }
832 wake_up_all(&dev->power.wait_queue);
833
7f321c26 834 if (retval >= 0)
140a6c94 835 rpm_idle(dev, RPM_ASYNC);
5e928f77
RW
836
837 out:
c7b61de5 838 if (parent && !dev->power.irq_safe) {
5e928f77
RW
839 spin_unlock_irq(&dev->power.lock);
840
841 pm_runtime_put(parent);
842
843 spin_lock_irq(&dev->power.lock);
844 }
845
d44c950e 846 trace_rpm_return_int_rcuidle(dev, _THIS_IP_, retval);
5e928f77
RW
847
848 return retval;
849}
850
5e928f77 851/**
62052ab1 852 * pm_runtime_work - Universal runtime PM work function.
5e928f77
RW
853 * @work: Work structure used for scheduling the execution of this function.
854 *
855 * Use @work to get the device object the work is to be done for, determine what
62052ab1 856 * is to be done and execute the appropriate runtime PM function.
5e928f77
RW
857 */
858static void pm_runtime_work(struct work_struct *work)
859{
860 struct device *dev = container_of(work, struct device, power.work);
861 enum rpm_request req;
862
863 spin_lock_irq(&dev->power.lock);
864
865 if (!dev->power.request_pending)
866 goto out;
867
868 req = dev->power.request;
869 dev->power.request = RPM_REQ_NONE;
870 dev->power.request_pending = false;
871
872 switch (req) {
873 case RPM_REQ_NONE:
874 break;
875 case RPM_REQ_IDLE:
140a6c94 876 rpm_idle(dev, RPM_NOWAIT);
5e928f77
RW
877 break;
878 case RPM_REQ_SUSPEND:
140a6c94 879 rpm_suspend(dev, RPM_NOWAIT);
5e928f77 880 break;
15bcb91d
AS
881 case RPM_REQ_AUTOSUSPEND:
882 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
883 break;
5e928f77 884 case RPM_REQ_RESUME:
140a6c94 885 rpm_resume(dev, RPM_NOWAIT);
5e928f77
RW
886 break;
887 }
888
889 out:
890 spin_unlock_irq(&dev->power.lock);
891}
892
5e928f77
RW
893/**
894 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
895 * @data: Device pointer passed by pm_schedule_suspend().
896 *
1bfee5bc 897 * Check if the time is right and queue a suspend request.
5e928f77 898 */
8234f673 899static enum hrtimer_restart pm_suspend_timer_fn(struct hrtimer *timer)
5e928f77 900{
8234f673 901 struct device *dev = container_of(timer, struct device, power.suspend_timer);
5e928f77 902 unsigned long flags;
8234f673 903 u64 expires;
5e928f77
RW
904
905 spin_lock_irqsave(&dev->power.lock, flags);
906
907 expires = dev->power.timer_expires;
1f7b7081
LM
908 /*
909 * If 'expires' is after the current time, we've been called
910 * too early.
911 */
8234f673 912 if (expires > 0 && expires < ktime_to_ns(ktime_get())) {
5e928f77 913 dev->power.timer_expires = 0;
15bcb91d
AS
914 rpm_suspend(dev, dev->power.timer_autosuspends ?
915 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
5e928f77
RW
916 }
917
918 spin_unlock_irqrestore(&dev->power.lock, flags);
8234f673
VG
919
920 return HRTIMER_NORESTART;
5e928f77
RW
921}
922
923/**
924 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
925 * @dev: Device to suspend.
926 * @delay: Time to wait before submitting a suspend request, in milliseconds.
927 */
928int pm_schedule_suspend(struct device *dev, unsigned int delay)
929{
930 unsigned long flags;
8234f673 931 ktime_t expires;
1bfee5bc 932 int retval;
5e928f77
RW
933
934 spin_lock_irqsave(&dev->power.lock, flags);
935
5e928f77 936 if (!delay) {
140a6c94 937 retval = rpm_suspend(dev, RPM_ASYNC);
5e928f77
RW
938 goto out;
939 }
940
1bfee5bc 941 retval = rpm_check_suspend_allowed(dev);
5e928f77
RW
942 if (retval)
943 goto out;
944
1bfee5bc
AS
945 /* Other scheduled or pending requests need to be canceled. */
946 pm_runtime_cancel_pending(dev);
947
8234f673
VG
948 expires = ktime_add(ktime_get(), ms_to_ktime(delay));
949 dev->power.timer_expires = ktime_to_ns(expires);
15bcb91d 950 dev->power.timer_autosuspends = 0;
8234f673 951 hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS);
5e928f77
RW
952
953 out:
954 spin_unlock_irqrestore(&dev->power.lock, flags);
955
956 return retval;
957}
958EXPORT_SYMBOL_GPL(pm_schedule_suspend);
959
5e928f77 960/**
62052ab1 961 * __pm_runtime_idle - Entry point for runtime idle operations.
140a6c94
AS
962 * @dev: Device to send idle notification for.
963 * @rpmflags: Flag bits.
964 *
965 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
966 * return immediately if it is larger than zero. Then carry out an idle
967 * notification, either synchronous or asynchronous.
968 *
311aab73
CC
969 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
970 * or if pm_runtime_irq_safe() has been called.
5e928f77 971 */
140a6c94 972int __pm_runtime_idle(struct device *dev, int rpmflags)
5e928f77
RW
973{
974 unsigned long flags;
975 int retval;
976
140a6c94
AS
977 if (rpmflags & RPM_GET_PUT) {
978 if (!atomic_dec_and_test(&dev->power.usage_count))
979 return 0;
980 }
981
a9306a63
RW
982 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
983
5e928f77 984 spin_lock_irqsave(&dev->power.lock, flags);
140a6c94 985 retval = rpm_idle(dev, rpmflags);
5e928f77
RW
986 spin_unlock_irqrestore(&dev->power.lock, flags);
987
988 return retval;
989}
140a6c94 990EXPORT_SYMBOL_GPL(__pm_runtime_idle);
5e928f77
RW
991
992/**
62052ab1 993 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
140a6c94 994 * @dev: Device to suspend.
3f9af051 995 * @rpmflags: Flag bits.
5e928f77 996 *
15bcb91d
AS
997 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
998 * return immediately if it is larger than zero. Then carry out a suspend,
999 * either synchronous or asynchronous.
140a6c94 1000 *
311aab73
CC
1001 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1002 * or if pm_runtime_irq_safe() has been called.
5e928f77 1003 */
140a6c94 1004int __pm_runtime_suspend(struct device *dev, int rpmflags)
5e928f77 1005{
140a6c94 1006 unsigned long flags;
1d531c14 1007 int retval;
5e928f77 1008
15bcb91d
AS
1009 if (rpmflags & RPM_GET_PUT) {
1010 if (!atomic_dec_and_test(&dev->power.usage_count))
1011 return 0;
1012 }
1013
a9306a63
RW
1014 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
1015
140a6c94
AS
1016 spin_lock_irqsave(&dev->power.lock, flags);
1017 retval = rpm_suspend(dev, rpmflags);
1018 spin_unlock_irqrestore(&dev->power.lock, flags);
5e928f77
RW
1019
1020 return retval;
1021}
140a6c94 1022EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
5e928f77
RW
1023
1024/**
62052ab1 1025 * __pm_runtime_resume - Entry point for runtime resume operations.
140a6c94 1026 * @dev: Device to resume.
3f9af051 1027 * @rpmflags: Flag bits.
5e928f77 1028 *
140a6c94
AS
1029 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
1030 * carry out a resume, either synchronous or asynchronous.
1031 *
311aab73
CC
1032 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
1033 * or if pm_runtime_irq_safe() has been called.
5e928f77 1034 */
140a6c94 1035int __pm_runtime_resume(struct device *dev, int rpmflags)
5e928f77 1036{
140a6c94
AS
1037 unsigned long flags;
1038 int retval;
5e928f77 1039
a9306a63
RW
1040 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe &&
1041 dev->power.runtime_status != RPM_ACTIVE);
311aab73 1042
140a6c94
AS
1043 if (rpmflags & RPM_GET_PUT)
1044 atomic_inc(&dev->power.usage_count);
1045
1046 spin_lock_irqsave(&dev->power.lock, flags);
1047 retval = rpm_resume(dev, rpmflags);
1048 spin_unlock_irqrestore(&dev->power.lock, flags);
5e928f77
RW
1049
1050 return retval;
1051}
140a6c94 1052EXPORT_SYMBOL_GPL(__pm_runtime_resume);
5e928f77 1053
a436b6a1
RW
1054/**
1055 * pm_runtime_get_if_in_use - Conditionally bump up the device's usage counter.
1056 * @dev: Device to handle.
1057 *
1058 * Return -EINVAL if runtime PM is disabled for the device.
1059 *
1060 * If that's not the case and if the device's runtime PM status is RPM_ACTIVE
1061 * and the runtime PM usage counter is nonzero, increment the counter and
1062 * return 1. Otherwise return 0 without changing the counter.
1063 */
1064int pm_runtime_get_if_in_use(struct device *dev)
1065{
1066 unsigned long flags;
1067 int retval;
1068
1069 spin_lock_irqsave(&dev->power.lock, flags);
1070 retval = dev->power.disable_depth > 0 ? -EINVAL :
1071 dev->power.runtime_status == RPM_ACTIVE
1072 && atomic_inc_not_zero(&dev->power.usage_count);
1073 spin_unlock_irqrestore(&dev->power.lock, flags);
1074 return retval;
1075}
1076EXPORT_SYMBOL_GPL(pm_runtime_get_if_in_use);
1077
5e928f77 1078/**
62052ab1 1079 * __pm_runtime_set_status - Set runtime PM status of a device.
5e928f77 1080 * @dev: Device to handle.
62052ab1 1081 * @status: New runtime PM status of the device.
5e928f77 1082 *
62052ab1 1083 * If runtime PM of the device is disabled or its power.runtime_error field is
5e928f77
RW
1084 * different from zero, the status may be changed either to RPM_ACTIVE, or to
1085 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
1086 * However, if the device has a parent and the parent is not active, and the
1087 * parent's power.ignore_children flag is unset, the device's status cannot be
1088 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
1089 *
1090 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
1091 * and the device parent's counter of unsuspended children is modified to
1092 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
1093 * notification request for the parent is submitted.
1094 */
1095int __pm_runtime_set_status(struct device *dev, unsigned int status)
1096{
1097 struct device *parent = dev->parent;
1098 unsigned long flags;
1099 bool notify_parent = false;
1100 int error = 0;
1101
1102 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
1103 return -EINVAL;
1104
1105 spin_lock_irqsave(&dev->power.lock, flags);
1106
1107 if (!dev->power.runtime_error && !dev->power.disable_depth) {
1108 error = -EAGAIN;
1109 goto out;
1110 }
1111
f8817f61 1112 if (dev->power.runtime_status == status || !parent)
5e928f77
RW
1113 goto out_set;
1114
1115 if (status == RPM_SUSPENDED) {
f8817f61
RW
1116 atomic_add_unless(&parent->power.child_count, -1, 0);
1117 notify_parent = !parent->power.ignore_children;
1118 } else {
bab636b9 1119 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
5e928f77
RW
1120
1121 /*
1122 * It is invalid to put an active child under a parent that is
62052ab1 1123 * not active, has runtime PM enabled and the
5e928f77
RW
1124 * 'power.ignore_children' flag unset.
1125 */
1126 if (!parent->power.disable_depth
1127 && !parent->power.ignore_children
71723f95
LW
1128 && parent->power.runtime_status != RPM_ACTIVE) {
1129 dev_err(dev, "runtime PM trying to activate child device %s but parent (%s) is not active\n",
1130 dev_name(dev),
1131 dev_name(parent));
5e928f77 1132 error = -EBUSY;
71723f95 1133 } else if (dev->power.runtime_status == RPM_SUSPENDED) {
965c4ac0 1134 atomic_inc(&parent->power.child_count);
71723f95 1135 }
5e928f77 1136
862f89b3 1137 spin_unlock(&parent->power.lock);
5e928f77
RW
1138
1139 if (error)
1140 goto out;
1141 }
1142
1143 out_set:
8d4b9d1b 1144 __update_runtime_status(dev, status);
5e928f77
RW
1145 dev->power.runtime_error = 0;
1146 out:
1147 spin_unlock_irqrestore(&dev->power.lock, flags);
1148
1149 if (notify_parent)
1150 pm_request_idle(parent);
1151
1152 return error;
1153}
1154EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1155
1156/**
1157 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1158 * @dev: Device to handle.
1159 *
1160 * Flush all pending requests for the device from pm_wq and wait for all
62052ab1 1161 * runtime PM operations involving the device in progress to complete.
5e928f77
RW
1162 *
1163 * Should be called under dev->power.lock with interrupts disabled.
1164 */
1165static void __pm_runtime_barrier(struct device *dev)
1166{
1167 pm_runtime_deactivate_timer(dev);
1168
1169 if (dev->power.request_pending) {
1170 dev->power.request = RPM_REQ_NONE;
1171 spin_unlock_irq(&dev->power.lock);
1172
1173 cancel_work_sync(&dev->power.work);
1174
1175 spin_lock_irq(&dev->power.lock);
1176 dev->power.request_pending = false;
1177 }
1178
1179 if (dev->power.runtime_status == RPM_SUSPENDING
1180 || dev->power.runtime_status == RPM_RESUMING
1181 || dev->power.idle_notification) {
1182 DEFINE_WAIT(wait);
1183
1184 /* Suspend, wake-up or idle notification in progress. */
1185 for (;;) {
1186 prepare_to_wait(&dev->power.wait_queue, &wait,
1187 TASK_UNINTERRUPTIBLE);
1188 if (dev->power.runtime_status != RPM_SUSPENDING
1189 && dev->power.runtime_status != RPM_RESUMING
1190 && !dev->power.idle_notification)
1191 break;
1192 spin_unlock_irq(&dev->power.lock);
1193
1194 schedule();
1195
1196 spin_lock_irq(&dev->power.lock);
1197 }
1198 finish_wait(&dev->power.wait_queue, &wait);
1199 }
1200}
1201
1202/**
1203 * pm_runtime_barrier - Flush pending requests and wait for completions.
1204 * @dev: Device to handle.
1205 *
1206 * Prevent the device from being suspended by incrementing its usage counter and
1207 * if there's a pending resume request for the device, wake the device up.
1208 * Next, make sure that all pending requests for the device have been flushed
62052ab1 1209 * from pm_wq and wait for all runtime PM operations involving the device in
5e928f77
RW
1210 * progress to complete.
1211 *
1212 * Return value:
1213 * 1, if there was a resume request pending and the device had to be woken up,
1214 * 0, otherwise
1215 */
1216int pm_runtime_barrier(struct device *dev)
1217{
1218 int retval = 0;
1219
1220 pm_runtime_get_noresume(dev);
1221 spin_lock_irq(&dev->power.lock);
1222
1223 if (dev->power.request_pending
1224 && dev->power.request == RPM_REQ_RESUME) {
140a6c94 1225 rpm_resume(dev, 0);
5e928f77
RW
1226 retval = 1;
1227 }
1228
1229 __pm_runtime_barrier(dev);
1230
1231 spin_unlock_irq(&dev->power.lock);
1232 pm_runtime_put_noidle(dev);
1233
1234 return retval;
1235}
1236EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1237
1238/**
62052ab1 1239 * __pm_runtime_disable - Disable runtime PM of a device.
5e928f77
RW
1240 * @dev: Device to handle.
1241 * @check_resume: If set, check if there's a resume request for the device.
1242 *
7b60894f 1243 * Increment power.disable_depth for the device and if it was zero previously,
62052ab1 1244 * cancel all pending runtime PM requests for the device and wait for all
5e928f77 1245 * operations in progress to complete. The device can be either active or
62052ab1 1246 * suspended after its runtime PM has been disabled.
5e928f77
RW
1247 *
1248 * If @check_resume is set and there's a resume request pending when
1249 * __pm_runtime_disable() is called and power.disable_depth is zero, the
62052ab1 1250 * function will wake up the device before disabling its runtime PM.
5e928f77
RW
1251 */
1252void __pm_runtime_disable(struct device *dev, bool check_resume)
1253{
1254 spin_lock_irq(&dev->power.lock);
1255
1256 if (dev->power.disable_depth > 0) {
1257 dev->power.disable_depth++;
1258 goto out;
1259 }
1260
1261 /*
1262 * Wake up the device if there's a resume request pending, because that
62052ab1 1263 * means there probably is some I/O to process and disabling runtime PM
5e928f77
RW
1264 * shouldn't prevent the device from processing the I/O.
1265 */
1266 if (check_resume && dev->power.request_pending
1267 && dev->power.request == RPM_REQ_RESUME) {
1268 /*
1269 * Prevent suspends and idle notifications from being carried
1270 * out after we have woken up the device.
1271 */
1272 pm_runtime_get_noresume(dev);
1273
140a6c94 1274 rpm_resume(dev, 0);
5e928f77
RW
1275
1276 pm_runtime_put_noidle(dev);
1277 }
1278
1279 if (!dev->power.disable_depth++)
1280 __pm_runtime_barrier(dev);
1281
1282 out:
1283 spin_unlock_irq(&dev->power.lock);
1284}
1285EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1286
1287/**
62052ab1 1288 * pm_runtime_enable - Enable runtime PM of a device.
5e928f77
RW
1289 * @dev: Device to handle.
1290 */
1291void pm_runtime_enable(struct device *dev)
1292{
1293 unsigned long flags;
1294
1295 spin_lock_irqsave(&dev->power.lock, flags);
1296
1297 if (dev->power.disable_depth > 0)
1298 dev->power.disable_depth--;
1299 else
1300 dev_warn(dev, "Unbalanced %s!\n", __func__);
1301
f8817f61
RW
1302 WARN(!dev->power.disable_depth &&
1303 dev->power.runtime_status == RPM_SUSPENDED &&
1304 !dev->power.ignore_children &&
1305 atomic_read(&dev->power.child_count) > 0,
1306 "Enabling runtime PM for inactive device (%s) with active children\n",
1307 dev_name(dev));
1308
5e928f77
RW
1309 spin_unlock_irqrestore(&dev->power.lock, flags);
1310}
1311EXPORT_SYMBOL_GPL(pm_runtime_enable);
1312
53823639 1313/**
62052ab1 1314 * pm_runtime_forbid - Block runtime PM of a device.
53823639
RW
1315 * @dev: Device to handle.
1316 *
1317 * Increase the device's usage count and clear its power.runtime_auto flag,
1318 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1319 * for it.
1320 */
1321void pm_runtime_forbid(struct device *dev)
1322{
1323 spin_lock_irq(&dev->power.lock);
1324 if (!dev->power.runtime_auto)
1325 goto out;
1326
1327 dev->power.runtime_auto = false;
1328 atomic_inc(&dev->power.usage_count);
140a6c94 1329 rpm_resume(dev, 0);
53823639
RW
1330
1331 out:
1332 spin_unlock_irq(&dev->power.lock);
1333}
1334EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1335
1336/**
62052ab1 1337 * pm_runtime_allow - Unblock runtime PM of a device.
53823639
RW
1338 * @dev: Device to handle.
1339 *
1340 * Decrease the device's usage count and set its power.runtime_auto flag.
1341 */
1342void pm_runtime_allow(struct device *dev)
1343{
1344 spin_lock_irq(&dev->power.lock);
1345 if (dev->power.runtime_auto)
1346 goto out;
1347
1348 dev->power.runtime_auto = true;
1349 if (atomic_dec_and_test(&dev->power.usage_count))
fe7450b0 1350 rpm_idle(dev, RPM_AUTO | RPM_ASYNC);
53823639
RW
1351
1352 out:
1353 spin_unlock_irq(&dev->power.lock);
1354}
1355EXPORT_SYMBOL_GPL(pm_runtime_allow);
1356
7490e442 1357/**
62052ab1 1358 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
7490e442
AS
1359 * @dev: Device to handle.
1360 *
1361 * Set the power.no_callbacks flag, which tells the PM core that this
62052ab1
RW
1362 * device is power-managed through its parent and has no runtime PM
1363 * callbacks of its own. The runtime sysfs attributes will be removed.
7490e442
AS
1364 */
1365void pm_runtime_no_callbacks(struct device *dev)
1366{
1367 spin_lock_irq(&dev->power.lock);
1368 dev->power.no_callbacks = 1;
1369 spin_unlock_irq(&dev->power.lock);
1370 if (device_is_registered(dev))
1371 rpm_sysfs_remove(dev);
1372}
1373EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1374
c7b61de5
AS
1375/**
1376 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1377 * @dev: Device to handle
1378 *
1379 * Set the power.irq_safe flag, which tells the PM core that the
1380 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1381 * always be invoked with the spinlock held and interrupts disabled. It also
1382 * causes the parent's usage counter to be permanently incremented, preventing
1383 * the parent from runtime suspending -- otherwise an irq-safe child might have
1384 * to wait for a non-irq-safe parent.
1385 */
1386void pm_runtime_irq_safe(struct device *dev)
1387{
1388 if (dev->parent)
1389 pm_runtime_get_sync(dev->parent);
1390 spin_lock_irq(&dev->power.lock);
1391 dev->power.irq_safe = 1;
1392 spin_unlock_irq(&dev->power.lock);
1393}
1394EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1395
15bcb91d
AS
1396/**
1397 * update_autosuspend - Handle a change to a device's autosuspend settings.
1398 * @dev: Device to handle.
1399 * @old_delay: The former autosuspend_delay value.
1400 * @old_use: The former use_autosuspend value.
1401 *
1402 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1403 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1404 *
1405 * This function must be called under dev->power.lock with interrupts disabled.
1406 */
1407static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1408{
1409 int delay = dev->power.autosuspend_delay;
1410
1411 /* Should runtime suspend be prevented now? */
1412 if (dev->power.use_autosuspend && delay < 0) {
1413
1414 /* If it used to be allowed then prevent it. */
1415 if (!old_use || old_delay >= 0) {
1416 atomic_inc(&dev->power.usage_count);
1417 rpm_resume(dev, 0);
1418 }
1419 }
1420
1421 /* Runtime suspend should be allowed now. */
1422 else {
1423
1424 /* If it used to be prevented then allow it. */
1425 if (old_use && old_delay < 0)
1426 atomic_dec(&dev->power.usage_count);
1427
1428 /* Maybe we can autosuspend now. */
1429 rpm_idle(dev, RPM_AUTO);
1430 }
1431}
1432
1433/**
1434 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1435 * @dev: Device to handle.
1436 * @delay: Value of the new delay in milliseconds.
1437 *
1438 * Set the device's power.autosuspend_delay value. If it changes to negative
62052ab1
RW
1439 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1440 * changes the other way, allow runtime suspends.
15bcb91d
AS
1441 */
1442void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1443{
1444 int old_delay, old_use;
1445
1446 spin_lock_irq(&dev->power.lock);
1447 old_delay = dev->power.autosuspend_delay;
1448 old_use = dev->power.use_autosuspend;
1449 dev->power.autosuspend_delay = delay;
1450 update_autosuspend(dev, old_delay, old_use);
1451 spin_unlock_irq(&dev->power.lock);
1452}
1453EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1454
1455/**
1456 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1457 * @dev: Device to handle.
1458 * @use: New value for use_autosuspend.
1459 *
62052ab1 1460 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
15bcb91d
AS
1461 * suspends as needed.
1462 */
1463void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1464{
1465 int old_delay, old_use;
1466
1467 spin_lock_irq(&dev->power.lock);
1468 old_delay = dev->power.autosuspend_delay;
1469 old_use = dev->power.use_autosuspend;
1470 dev->power.use_autosuspend = use;
1471 update_autosuspend(dev, old_delay, old_use);
1472 spin_unlock_irq(&dev->power.lock);
1473}
1474EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1475
5e928f77 1476/**
62052ab1 1477 * pm_runtime_init - Initialize runtime PM fields in given device object.
5e928f77
RW
1478 * @dev: Device object to initialize.
1479 */
1480void pm_runtime_init(struct device *dev)
1481{
5e928f77
RW
1482 dev->power.runtime_status = RPM_SUSPENDED;
1483 dev->power.idle_notification = false;
1484
1485 dev->power.disable_depth = 1;
1486 atomic_set(&dev->power.usage_count, 0);
1487
1488 dev->power.runtime_error = 0;
1489
1490 atomic_set(&dev->power.child_count, 0);
1491 pm_suspend_ignore_children(dev, false);
53823639 1492 dev->power.runtime_auto = true;
5e928f77
RW
1493
1494 dev->power.request_pending = false;
1495 dev->power.request = RPM_REQ_NONE;
1496 dev->power.deferred_resume = false;
8d4b9d1b 1497 dev->power.accounting_timestamp = jiffies;
5e928f77
RW
1498 INIT_WORK(&dev->power.work, pm_runtime_work);
1499
1500 dev->power.timer_expires = 0;
8234f673
VG
1501 hrtimer_init(&dev->power.suspend_timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
1502 dev->power.suspend_timer.function = pm_suspend_timer_fn;
5e928f77
RW
1503
1504 init_waitqueue_head(&dev->power.wait_queue);
1505}
1506
5de85b9d
UH
1507/**
1508 * pm_runtime_reinit - Re-initialize runtime PM fields in given device object.
1509 * @dev: Device object to re-initialize.
1510 */
1511void pm_runtime_reinit(struct device *dev)
1512{
1513 if (!pm_runtime_enabled(dev)) {
1514 if (dev->power.runtime_status == RPM_ACTIVE)
1515 pm_runtime_set_suspended(dev);
1516 if (dev->power.irq_safe) {
1517 spin_lock_irq(&dev->power.lock);
1518 dev->power.irq_safe = 0;
1519 spin_unlock_irq(&dev->power.lock);
1520 if (dev->parent)
1521 pm_runtime_put(dev->parent);
1522 }
1523 }
1524}
1525
5e928f77
RW
1526/**
1527 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1528 * @dev: Device object being removed from device hierarchy.
1529 */
1530void pm_runtime_remove(struct device *dev)
1531{
1532 __pm_runtime_disable(dev, false);
5de85b9d 1533 pm_runtime_reinit(dev);
5e928f77 1534}
37f20416 1535
21d5c57b
RW
1536/**
1537 * pm_runtime_clean_up_links - Prepare links to consumers for driver removal.
1538 * @dev: Device whose driver is going to be removed.
1539 *
1540 * Check links from this device to any consumers and if any of them have active
1541 * runtime PM references to the device, drop the usage counter of the device
1542 * (once per link).
1543 *
1544 * Links with the DL_FLAG_STATELESS flag set are ignored.
1545 *
1546 * Since the device is guaranteed to be runtime-active at the point this is
1547 * called, nothing else needs to be done here.
1548 *
1549 * Moreover, this is called after device_links_busy() has returned 'false', so
1550 * the status of each link is guaranteed to be DL_STATE_SUPPLIER_UNBIND and
1551 * therefore rpm_active can't be manipulated concurrently.
1552 */
1553void pm_runtime_clean_up_links(struct device *dev)
1554{
1555 struct device_link *link;
1556 int idx;
1557
1558 idx = device_links_read_lock();
1559
1560 list_for_each_entry_rcu(link, &dev->links.consumers, s_node) {
1561 if (link->flags & DL_FLAG_STATELESS)
1562 continue;
1563
1564 if (link->rpm_active) {
1565 pm_runtime_put_noidle(dev);
1566 link->rpm_active = false;
1567 }
1568 }
1569
1570 device_links_read_unlock(idx);
1571}
1572
1573/**
b06c0b2f 1574 * pm_runtime_get_suppliers - Resume and reference-count supplier devices.
21d5c57b
RW
1575 * @dev: Consumer device.
1576 */
b06c0b2f 1577void pm_runtime_get_suppliers(struct device *dev)
21d5c57b 1578{
b06c0b2f 1579 struct device_link *link;
21d5c57b
RW
1580 int idx;
1581
1582 idx = device_links_read_lock();
1583
b06c0b2f
RW
1584 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
1585 if (link->flags & DL_FLAG_PM_RUNTIME)
1586 pm_runtime_get_sync(link->supplier);
1587
1588 device_links_read_unlock(idx);
1589}
1590
1591/**
1592 * pm_runtime_put_suppliers - Drop references to supplier devices.
1593 * @dev: Consumer device.
1594 */
1595void pm_runtime_put_suppliers(struct device *dev)
1596{
1597 struct device_link *link;
1598 int idx;
1599
1600 idx = device_links_read_lock();
1601
1602 list_for_each_entry_rcu(link, &dev->links.suppliers, c_node)
1603 if (link->flags & DL_FLAG_PM_RUNTIME)
1604 pm_runtime_put(link->supplier);
21d5c57b
RW
1605
1606 device_links_read_unlock(idx);
1607}
1608
baa8809f
RW
1609void pm_runtime_new_link(struct device *dev)
1610{
1611 spin_lock_irq(&dev->power.lock);
1612 dev->power.links_count++;
1613 spin_unlock_irq(&dev->power.lock);
1614}
1615
1616void pm_runtime_drop_link(struct device *dev)
1617{
a0504aec
UH
1618 rpm_put_suppliers(dev);
1619
baa8809f
RW
1620 spin_lock_irq(&dev->power.lock);
1621 WARN_ON(dev->power.links_count == 0);
1622 dev->power.links_count--;
1623 spin_unlock_irq(&dev->power.lock);
1624}
1625
4918e1f8
RW
1626static bool pm_runtime_need_not_resume(struct device *dev)
1627{
1628 return atomic_read(&dev->power.usage_count) <= 1 &&
1f5c6855
RW
1629 (atomic_read(&dev->power.child_count) == 0 ||
1630 dev->power.ignore_children);
4918e1f8
RW
1631}
1632
37f20416
UH
1633/**
1634 * pm_runtime_force_suspend - Force a device into suspend state if needed.
1635 * @dev: Device to suspend.
1636 *
1637 * Disable runtime PM so we safely can check the device's runtime PM status and
4918e1f8
RW
1638 * if it is active, invoke its ->runtime_suspend callback to suspend it and
1639 * change its runtime PM status field to RPM_SUSPENDED. Also, if the device's
1640 * usage and children counters don't indicate that the device was in use before
1641 * the system-wide transition under way, decrement its parent's children counter
1642 * (if there is a parent). Keep runtime PM disabled to preserve the state
1643 * unless we encounter errors.
37f20416
UH
1644 *
1645 * Typically this function may be invoked from a system suspend callback to make
4918e1f8
RW
1646 * sure the device is put into low power state and it should only be used during
1647 * system-wide PM transitions to sleep states. It assumes that the analogous
1648 * pm_runtime_force_resume() will be used to resume the device.
37f20416
UH
1649 */
1650int pm_runtime_force_suspend(struct device *dev)
1651{
1652 int (*callback)(struct device *);
617fcb67 1653 int ret;
37f20416
UH
1654
1655 pm_runtime_disable(dev);
37f20416
UH
1656 if (pm_runtime_status_suspended(dev))
1657 return 0;
1658
dbcd2d72 1659 callback = RPM_GET_CALLBACK(dev, runtime_suspend);
37f20416 1660
617fcb67 1661 ret = callback ? callback(dev) : 0;
37f20416
UH
1662 if (ret)
1663 goto err;
1664
1d9174fb 1665 /*
4918e1f8
RW
1666 * If the device can stay in suspend after the system-wide transition
1667 * to the working state that will follow, drop the children counter of
1668 * its parent, but set its status to RPM_SUSPENDED anyway in case this
1669 * function will be called again for it in the meantime.
1d9174fb 1670 */
4918e1f8
RW
1671 if (pm_runtime_need_not_resume(dev))
1672 pm_runtime_set_suspended(dev);
1673 else
1674 __update_runtime_status(dev, RPM_SUSPENDED);
1d9174fb 1675
37f20416 1676 return 0;
4918e1f8 1677
37f20416
UH
1678err:
1679 pm_runtime_enable(dev);
1680 return ret;
1681}
1682EXPORT_SYMBOL_GPL(pm_runtime_force_suspend);
1683
1684/**
1d9174fb 1685 * pm_runtime_force_resume - Force a device into resume state if needed.
37f20416
UH
1686 * @dev: Device to resume.
1687 *
1688 * Prior invoking this function we expect the user to have brought the device
1689 * into low power state by a call to pm_runtime_force_suspend(). Here we reverse
4918e1f8
RW
1690 * those actions and bring the device into full power, if it is expected to be
1691 * used on system resume. In the other case, we defer the resume to be managed
1692 * via runtime PM.
37f20416 1693 *
1d9174fb 1694 * Typically this function may be invoked from a system resume callback.
37f20416
UH
1695 */
1696int pm_runtime_force_resume(struct device *dev)
1697{
1698 int (*callback)(struct device *);
1699 int ret = 0;
1700
4918e1f8 1701 if (!pm_runtime_status_suspended(dev) || pm_runtime_need_not_resume(dev))
9f5b5274
UH
1702 goto out;
1703
1d9174fb 1704 /*
4918e1f8
RW
1705 * The value of the parent's children counter is correct already, so
1706 * just update the status of the device.
1707 */
1708 __update_runtime_status(dev, RPM_ACTIVE);
1d9174fb 1709
4918e1f8 1710 callback = RPM_GET_CALLBACK(dev, runtime_resume);
37f20416 1711
617fcb67 1712 ret = callback ? callback(dev) : 0;
0ae3aeef
UH
1713 if (ret) {
1714 pm_runtime_set_suspended(dev);
1715 goto out;
1716 }
1717
37f20416
UH
1718 pm_runtime_mark_last_busy(dev);
1719out:
1720 pm_runtime_enable(dev);
1721 return ret;
1722}
1723EXPORT_SYMBOL_GPL(pm_runtime_force_resume);