blob: ac495b1357faf84e68594674e860daac94d693af [file] [log] [blame]
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001/*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02002 * drivers/base/power/runtime.c - Helper functions for device runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02003 *
4 * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
Alan Stern1bfee5b2010-09-25 23:35:00 +02005 * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02006 *
7 * This file is released under the GPLv2.
8 */
9
10#include <linux/sched.h>
Paul Gortmaker1b6bc322011-05-27 07:12:15 -040011#include <linux/export.h>
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020012#include <linux/pm_runtime.h>
Ming Leic3dc2f12011-09-27 22:54:41 +020013#include <trace/events/rpm.h>
Alan Stern7490e442010-09-25 23:35:15 +020014#include "power.h"
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020015
Ulf Hansson5f59df72014-03-01 11:56:04 +010016#define RPM_GET_CALLBACK(dev, cb) \
17({ \
18 int (*__rpm_cb)(struct device *__d); \
19 \
20 if (dev->pm_domain) \
21 __rpm_cb = dev->pm_domain->ops.cb; \
22 else if (dev->type && dev->type->pm) \
23 __rpm_cb = dev->type->pm->cb; \
24 else if (dev->class && dev->class->pm) \
25 __rpm_cb = dev->class->pm->cb; \
26 else if (dev->bus && dev->bus->pm) \
27 __rpm_cb = dev->bus->pm->cb; \
28 else \
29 __rpm_cb = NULL; \
30 \
31 if (!__rpm_cb && dev->driver && dev->driver->pm) \
32 __rpm_cb = dev->driver->pm->cb; \
33 \
34 __rpm_cb; \
35})
36
37static int (*rpm_get_suspend_cb(struct device *dev))(struct device *)
38{
39 return RPM_GET_CALLBACK(dev, runtime_suspend);
40}
41
42static int (*rpm_get_resume_cb(struct device *dev))(struct device *)
43{
44 return RPM_GET_CALLBACK(dev, runtime_resume);
45}
46
47static int (*rpm_get_idle_cb(struct device *dev))(struct device *)
48{
49 return RPM_GET_CALLBACK(dev, runtime_idle);
50}
51
Alan Stern140a6c92010-09-25 23:35:07 +020052static int rpm_resume(struct device *dev, int rpmflags);
Alan Stern7490e442010-09-25 23:35:15 +020053static int rpm_suspend(struct device *dev, int rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020054
55/**
Alan Stern47693732010-09-25 23:34:46 +020056 * update_pm_runtime_accounting - Update the time accounting of power states
57 * @dev: Device to update the accounting for
58 *
59 * In order to be able to have time accounting of the various power states
60 * (as used by programs such as PowerTOP to show the effectiveness of runtime
61 * PM), we need to track the time spent in each state.
62 * update_pm_runtime_accounting must be called each time before the
63 * runtime_status field is updated, to account the time in the old state
64 * correctly.
65 */
66void update_pm_runtime_accounting(struct device *dev)
67{
68 unsigned long now = jiffies;
venu byravarasudef0c0a32011-11-03 10:12:14 +010069 unsigned long delta;
Alan Stern47693732010-09-25 23:34:46 +020070
71 delta = now - dev->power.accounting_timestamp;
72
Alan Stern47693732010-09-25 23:34:46 +020073 dev->power.accounting_timestamp = now;
74
75 if (dev->power.disable_depth > 0)
76 return;
77
78 if (dev->power.runtime_status == RPM_SUSPENDED)
79 dev->power.suspended_jiffies += delta;
80 else
81 dev->power.active_jiffies += delta;
82}
83
84static void __update_runtime_status(struct device *dev, enum rpm_status status)
85{
86 update_pm_runtime_accounting(dev);
87 dev->power.runtime_status = status;
88}
89
90/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020091 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
92 * @dev: Device to handle.
93 */
94static void pm_runtime_deactivate_timer(struct device *dev)
95{
96 if (dev->power.timer_expires > 0) {
97 del_timer(&dev->power.suspend_timer);
98 dev->power.timer_expires = 0;
99 }
100}
101
102/**
103 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
104 * @dev: Device to handle.
105 */
106static void pm_runtime_cancel_pending(struct device *dev)
107{
108 pm_runtime_deactivate_timer(dev);
109 /*
110 * In case there's a request pending, make sure its work function will
111 * return without doing anything.
112 */
113 dev->power.request = RPM_REQ_NONE;
114}
115
Alan Stern15bcb91d2010-09-25 23:35:21 +0200116/*
117 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
118 * @dev: Device to handle.
119 *
120 * Compute the autosuspend-delay expiration time based on the device's
121 * power.last_busy time. If the delay has already expired or is disabled
122 * (negative) or the power.use_autosuspend flag isn't set, return 0.
123 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
124 *
125 * This function may be called either with or without dev->power.lock held.
126 * Either way it can be racy, since power.last_busy may be updated at any time.
127 */
128unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
129{
130 int autosuspend_delay;
131 long elapsed;
132 unsigned long last_busy;
133 unsigned long expires = 0;
134
135 if (!dev->power.use_autosuspend)
136 goto out;
137
138 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
139 if (autosuspend_delay < 0)
140 goto out;
141
142 last_busy = ACCESS_ONCE(dev->power.last_busy);
143 elapsed = jiffies - last_busy;
144 if (elapsed < 0)
145 goto out; /* jiffies has wrapped around. */
146
147 /*
148 * If the autosuspend_delay is >= 1 second, align the timer by rounding
149 * up to the nearest second.
150 */
151 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
152 if (autosuspend_delay >= 1000)
153 expires = round_jiffies(expires);
154 expires += !expires;
155 if (elapsed >= expires - last_busy)
156 expires = 0; /* Already expired. */
157
158 out:
159 return expires;
160}
161EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
162
Ming Leie8234072013-02-22 16:34:11 -0800163static int dev_memalloc_noio(struct device *dev, void *data)
164{
165 return dev->power.memalloc_noio;
166}
167
168/*
169 * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
170 * @dev: Device to handle.
171 * @enable: True for setting the flag and False for clearing the flag.
172 *
173 * Set the flag for all devices in the path from the device to the
174 * root device in the device tree if @enable is true, otherwise clear
175 * the flag for devices in the path whose siblings don't set the flag.
176 *
177 * The function should only be called by block device, or network
178 * device driver for solving the deadlock problem during runtime
179 * resume/suspend:
180 *
181 * If memory allocation with GFP_KERNEL is called inside runtime
182 * resume/suspend callback of any one of its ancestors(or the
183 * block device itself), the deadlock may be triggered inside the
184 * memory allocation since it might not complete until the block
185 * device becomes active and the involed page I/O finishes. The
186 * situation is pointed out first by Alan Stern. Network device
187 * are involved in iSCSI kind of situation.
188 *
189 * The lock of dev_hotplug_mutex is held in the function for handling
190 * hotplug race because pm_runtime_set_memalloc_noio() may be called
191 * in async probe().
192 *
193 * The function should be called between device_add() and device_del()
194 * on the affected device(block/network device).
195 */
196void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
197{
198 static DEFINE_MUTEX(dev_hotplug_mutex);
199
200 mutex_lock(&dev_hotplug_mutex);
201 for (;;) {
202 bool enabled;
203
204 /* hold power lock since bitfield is not SMP-safe. */
205 spin_lock_irq(&dev->power.lock);
206 enabled = dev->power.memalloc_noio;
207 dev->power.memalloc_noio = enable;
208 spin_unlock_irq(&dev->power.lock);
209
210 /*
211 * not need to enable ancestors any more if the device
212 * has been enabled.
213 */
214 if (enabled && enable)
215 break;
216
217 dev = dev->parent;
218
219 /*
220 * clear flag of the parent device only if all the
221 * children don't set the flag because ancestor's
222 * flag was set by any one of the descendants.
223 */
224 if (!dev || (!enable &&
225 device_for_each_child(dev, NULL,
226 dev_memalloc_noio)))
227 break;
228 }
229 mutex_unlock(&dev_hotplug_mutex);
230}
231EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
232
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200233/**
Alan Stern1bfee5b2010-09-25 23:35:00 +0200234 * rpm_check_suspend_allowed - Test whether a device may be suspended.
235 * @dev: Device to test.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200236 */
Alan Stern1bfee5b2010-09-25 23:35:00 +0200237static int rpm_check_suspend_allowed(struct device *dev)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200238{
239 int retval = 0;
240
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200241 if (dev->power.runtime_error)
242 retval = -EINVAL;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200243 else if (dev->power.disable_depth > 0)
244 retval = -EACCES;
245 else if (atomic_read(&dev->power.usage_count) > 0)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200246 retval = -EAGAIN;
247 else if (!pm_children_suspended(dev))
248 retval = -EBUSY;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200249
250 /* Pending resume requests take precedence over suspends. */
251 else if ((dev->power.deferred_resume
Kevin Winchester78ca7c32010-10-29 15:29:55 +0200252 && dev->power.runtime_status == RPM_SUSPENDING)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200253 || (dev->power.request_pending
254 && dev->power.request == RPM_REQ_RESUME))
255 retval = -EAGAIN;
Rafael J. Wysocki55d7ec42012-08-15 21:32:04 +0200256 else if (__dev_pm_qos_read_value(dev) < 0)
257 retval = -EPERM;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200258 else if (dev->power.runtime_status == RPM_SUSPENDED)
259 retval = 1;
260
261 return retval;
262}
263
Alan Stern1bfee5b2010-09-25 23:35:00 +0200264/**
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200265 * __rpm_callback - Run a given runtime PM callback for a given device.
266 * @cb: Runtime PM callback to run.
267 * @dev: Device to run the callback for.
268 */
269static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
270 __releases(&dev->power.lock) __acquires(&dev->power.lock)
271{
272 int retval;
273
274 if (dev->power.irq_safe)
275 spin_unlock(&dev->power.lock);
276 else
277 spin_unlock_irq(&dev->power.lock);
278
279 retval = cb(dev);
280
281 if (dev->power.irq_safe)
282 spin_lock(&dev->power.lock);
283 else
284 spin_lock_irq(&dev->power.lock);
285
286 return retval;
287}
288
289/**
Alan Stern140a6c92010-09-25 23:35:07 +0200290 * rpm_idle - Notify device bus type if the device can be suspended.
Alan Stern1bfee5b2010-09-25 23:35:00 +0200291 * @dev: Device to notify the bus type about.
292 * @rpmflags: Flag bits.
293 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200294 * Check if the device's runtime PM status allows it to be suspended. If
Alan Stern1bfee5b2010-09-25 23:35:00 +0200295 * another idle notification has been started earlier, return immediately. If
296 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
Ulf Hanssond66e6db2013-10-15 22:25:08 +0200297 * run the ->runtime_idle() callback directly. If the ->runtime_idle callback
298 * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag.
Alan Stern1bfee5b2010-09-25 23:35:00 +0200299 *
300 * This function must be called under dev->power.lock with interrupts disabled.
301 */
Alan Stern140a6c92010-09-25 23:35:07 +0200302static int rpm_idle(struct device *dev, int rpmflags)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200303{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200304 int (*callback)(struct device *);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200305 int retval;
306
Ming Leic3dc2f12011-09-27 22:54:41 +0200307 trace_rpm_idle(dev, rpmflags);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200308 retval = rpm_check_suspend_allowed(dev);
309 if (retval < 0)
310 ; /* Conditions are wrong. */
311
312 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
313 else if (dev->power.runtime_status != RPM_ACTIVE)
314 retval = -EAGAIN;
315
316 /*
317 * Any pending request other than an idle notification takes
318 * precedence over us, except that the timer may be running.
319 */
320 else if (dev->power.request_pending &&
321 dev->power.request > RPM_REQ_IDLE)
322 retval = -EAGAIN;
323
324 /* Act as though RPM_NOWAIT is always set. */
325 else if (dev->power.idle_notification)
326 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200327 if (retval)
328 goto out;
329
Alan Stern1bfee5b2010-09-25 23:35:00 +0200330 /* Pending requests need to be canceled. */
331 dev->power.request = RPM_REQ_NONE;
332
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200333 if (dev->power.no_callbacks)
Alan Stern7490e442010-09-25 23:35:15 +0200334 goto out;
Alan Stern7490e442010-09-25 23:35:15 +0200335
Alan Stern1bfee5b2010-09-25 23:35:00 +0200336 /* Carry out an asynchronous or a synchronous idle notification. */
337 if (rpmflags & RPM_ASYNC) {
338 dev->power.request = RPM_REQ_IDLE;
339 if (!dev->power.request_pending) {
340 dev->power.request_pending = true;
341 queue_work(pm_wq, &dev->power.work);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200342 }
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200343 trace_rpm_return_int(dev, _THIS_IP_, 0);
344 return 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200345 }
346
347 dev->power.idle_notification = true;
348
Ulf Hansson5f59df72014-03-01 11:56:04 +0100349 callback = rpm_get_idle_cb(dev);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100350
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200351 if (callback)
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200352 retval = __rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200353
354 dev->power.idle_notification = false;
355 wake_up_all(&dev->power.wait_queue);
356
357 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200358 trace_rpm_return_int(dev, _THIS_IP_, retval);
Ulf Hanssond66e6db2013-10-15 22:25:08 +0200359 return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200360}
361
362/**
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200363 * rpm_callback - Run a given runtime PM callback for a given device.
364 * @cb: Runtime PM callback to run.
365 * @dev: Device to run the callback for.
366 */
367static int rpm_callback(int (*cb)(struct device *), struct device *dev)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200368{
369 int retval;
370
371 if (!cb)
372 return -ENOSYS;
373
Ming Leidb881752013-02-22 16:34:19 -0800374 if (dev->power.memalloc_noio) {
375 unsigned int noio_flag;
376
377 /*
378 * Deadlock might be caused if memory allocation with
379 * GFP_KERNEL happens inside runtime_suspend and
380 * runtime_resume callbacks of one block device's
381 * ancestor or the block device itself. Network
382 * device might be thought as part of iSCSI block
383 * device, so network device and its ancestor should
384 * be marked as memalloc_noio too.
385 */
386 noio_flag = memalloc_noio_save();
387 retval = __rpm_callback(cb, dev);
388 memalloc_noio_restore(noio_flag);
389 } else {
390 retval = __rpm_callback(cb, dev);
391 }
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200392
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200393 dev->power.runtime_error = retval;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200394 return retval != -EACCES ? retval : -EIO;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200395}
396
397/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200398 * rpm_suspend - Carry out runtime suspend of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200399 * @dev: Device to suspend.
Alan Stern3f9af0512010-09-25 23:34:54 +0200400 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200401 *
Ming Lei47d8f0b2011-10-12 11:53:32 +0800402 * Check if the device's runtime PM status allows it to be suspended.
403 * Cancel a pending idle notification, autosuspend or suspend. If
404 * another suspend has been started earlier, either return immediately
405 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
406 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
Ming Lei857b36c2011-10-12 22:59:33 +0200407 * otherwise run the ->runtime_suspend() callback directly. When
408 * ->runtime_suspend succeeded, if a deferred resume was requested while
409 * the callback was running then carry it out, otherwise send an idle
410 * notification for its parent (if the suspend succeeded and both
411 * ignore_children of parent->power and irq_safe of dev->power are not set).
Alan Stern886486b2011-11-03 23:39:18 +0100412 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
413 * flag is set and the next autosuspend-delay expiration time is in the
414 * future, schedule another autosuspend attempt.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200415 *
416 * This function must be called under dev->power.lock with interrupts disabled.
417 */
Alan Stern140a6c92010-09-25 23:35:07 +0200418static int rpm_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200419 __releases(&dev->power.lock) __acquires(&dev->power.lock)
420{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200421 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200422 struct device *parent = NULL;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200423 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200424
Ming Leic3dc2f12011-09-27 22:54:41 +0200425 trace_rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200426
427 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200428 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200429
Alan Stern1bfee5b2010-09-25 23:35:00 +0200430 if (retval < 0)
431 ; /* Conditions are wrong. */
432
433 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
434 else if (dev->power.runtime_status == RPM_RESUMING &&
435 !(rpmflags & RPM_ASYNC))
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200436 retval = -EAGAIN;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200437 if (retval)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200438 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200439
Alan Stern15bcb91d2010-09-25 23:35:21 +0200440 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
441 if ((rpmflags & RPM_AUTO)
442 && dev->power.runtime_status != RPM_SUSPENDING) {
443 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
444
445 if (expires != 0) {
446 /* Pending requests need to be canceled. */
447 dev->power.request = RPM_REQ_NONE;
448
449 /*
450 * Optimization: If the timer is already running and is
451 * set to expire at or before the autosuspend delay,
452 * avoid the overhead of resetting it. Just let it
453 * expire; pm_suspend_timer_fn() will take care of the
454 * rest.
455 */
456 if (!(dev->power.timer_expires && time_before_eq(
457 dev->power.timer_expires, expires))) {
458 dev->power.timer_expires = expires;
459 mod_timer(&dev->power.suspend_timer, expires);
460 }
461 dev->power.timer_autosuspends = 1;
462 goto out;
463 }
464 }
465
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200466 /* Other scheduled or pending requests need to be canceled. */
467 pm_runtime_cancel_pending(dev);
468
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200469 if (dev->power.runtime_status == RPM_SUSPENDING) {
470 DEFINE_WAIT(wait);
471
Alan Stern1bfee5b2010-09-25 23:35:00 +0200472 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200473 retval = -EINPROGRESS;
474 goto out;
475 }
476
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200477 if (dev->power.irq_safe) {
478 spin_unlock(&dev->power.lock);
479
480 cpu_relax();
481
482 spin_lock(&dev->power.lock);
483 goto repeat;
484 }
485
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200486 /* Wait for the other suspend running in parallel with us. */
487 for (;;) {
488 prepare_to_wait(&dev->power.wait_queue, &wait,
489 TASK_UNINTERRUPTIBLE);
490 if (dev->power.runtime_status != RPM_SUSPENDING)
491 break;
492
493 spin_unlock_irq(&dev->power.lock);
494
495 schedule();
496
497 spin_lock_irq(&dev->power.lock);
498 }
499 finish_wait(&dev->power.wait_queue, &wait);
500 goto repeat;
501 }
502
Alan Stern7490e442010-09-25 23:35:15 +0200503 if (dev->power.no_callbacks)
504 goto no_callback; /* Assume success. */
505
Alan Stern1bfee5b2010-09-25 23:35:00 +0200506 /* Carry out an asynchronous or a synchronous suspend. */
507 if (rpmflags & RPM_ASYNC) {
Alan Stern15bcb91d2010-09-25 23:35:21 +0200508 dev->power.request = (rpmflags & RPM_AUTO) ?
509 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200510 if (!dev->power.request_pending) {
511 dev->power.request_pending = true;
512 queue_work(pm_wq, &dev->power.work);
513 }
514 goto out;
515 }
516
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200517 __update_runtime_status(dev, RPM_SUSPENDING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200518
Ulf Hansson5f59df72014-03-01 11:56:04 +0100519 callback = rpm_get_suspend_cb(dev);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100520
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200521 retval = rpm_callback(callback, dev);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100522 if (retval)
523 goto fail;
Alan Stern886486b2011-11-03 23:39:18 +0100524
Alan Stern7490e442010-09-25 23:35:15 +0200525 no_callback:
Ming Lei857b36c2011-10-12 22:59:33 +0200526 __update_runtime_status(dev, RPM_SUSPENDED);
527 pm_runtime_deactivate_timer(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200528
Ming Lei857b36c2011-10-12 22:59:33 +0200529 if (dev->parent) {
530 parent = dev->parent;
531 atomic_add_unless(&parent->power.child_count, -1, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200532 }
533 wake_up_all(&dev->power.wait_queue);
534
535 if (dev->power.deferred_resume) {
Rafael J. Wysocki58a34de2012-08-15 21:31:55 +0200536 dev->power.deferred_resume = false;
Alan Stern140a6c92010-09-25 23:35:07 +0200537 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200538 retval = -EAGAIN;
539 goto out;
540 }
541
Alan Sternc3810c82011-01-25 20:50:07 +0100542 /* Maybe the parent is now able to suspend. */
Alan Sternc7b61de2010-12-01 00:14:42 +0100543 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
Alan Sternc3810c82011-01-25 20:50:07 +0100544 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200545
Alan Sternc3810c82011-01-25 20:50:07 +0100546 spin_lock(&parent->power.lock);
547 rpm_idle(parent, RPM_ASYNC);
548 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200549
Alan Sternc3810c82011-01-25 20:50:07 +0100550 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200551 }
552
553 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200554 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200555
556 return retval;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100557
558 fail:
559 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100560 dev->power.deferred_resume = false;
Alan Sternf2791d72012-03-26 22:46:52 +0200561 wake_up_all(&dev->power.wait_queue);
562
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100563 if (retval == -EAGAIN || retval == -EBUSY) {
564 dev->power.runtime_error = 0;
565
566 /*
567 * If the callback routine failed an autosuspend, and
568 * if the last_busy time has been updated so that there
569 * is a new autosuspend expiration time, automatically
570 * reschedule another autosuspend.
571 */
572 if ((rpmflags & RPM_AUTO) &&
573 pm_runtime_autosuspend_expiration(dev) != 0)
574 goto repeat;
575 } else {
576 pm_runtime_cancel_pending(dev);
577 }
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100578 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200579}
580
581/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200582 * rpm_resume - Carry out runtime resume of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200583 * @dev: Device to resume.
Alan Stern3f9af0512010-09-25 23:34:54 +0200584 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200585 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200586 * Check if the device's runtime PM status allows it to be resumed. Cancel
Alan Stern1bfee5b2010-09-25 23:35:00 +0200587 * any scheduled or pending requests. If another resume has been started
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300588 * earlier, either return immediately or wait for it to finish, depending on the
Alan Stern1bfee5b2010-09-25 23:35:00 +0200589 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
590 * parallel with this function, either tell the other process to resume after
591 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
592 * flag is set then queue a resume request; otherwise run the
593 * ->runtime_resume() callback directly. Queue an idle notification for the
594 * device if the resume succeeded.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200595 *
596 * This function must be called under dev->power.lock with interrupts disabled.
597 */
Alan Stern140a6c92010-09-25 23:35:07 +0200598static int rpm_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200599 __releases(&dev->power.lock) __acquires(&dev->power.lock)
600{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200601 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200602 struct device *parent = NULL;
603 int retval = 0;
604
Ming Leic3dc2f12011-09-27 22:54:41 +0200605 trace_rpm_resume(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200606
607 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200608 if (dev->power.runtime_error)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200609 retval = -EINVAL;
Kevin Hilman6f3c77b2012-09-21 22:47:34 +0000610 else if (dev->power.disable_depth == 1 && dev->power.is_suspended
611 && dev->power.runtime_status == RPM_ACTIVE)
612 retval = 1;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200613 else if (dev->power.disable_depth > 0)
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200614 retval = -EACCES;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200615 if (retval)
616 goto out;
617
Alan Stern15bcb91d2010-09-25 23:35:21 +0200618 /*
619 * Other scheduled or pending requests need to be canceled. Small
620 * optimization: If an autosuspend timer is running, leave it running
621 * rather than cancelling it now only to restart it again in the near
622 * future.
623 */
624 dev->power.request = RPM_REQ_NONE;
625 if (!dev->power.timer_autosuspends)
626 pm_runtime_deactivate_timer(dev);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200627
628 if (dev->power.runtime_status == RPM_ACTIVE) {
629 retval = 1;
630 goto out;
631 }
632
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200633 if (dev->power.runtime_status == RPM_RESUMING
634 || dev->power.runtime_status == RPM_SUSPENDING) {
635 DEFINE_WAIT(wait);
636
Alan Stern1bfee5b2010-09-25 23:35:00 +0200637 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200638 if (dev->power.runtime_status == RPM_SUSPENDING)
639 dev->power.deferred_resume = true;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200640 else
641 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200642 goto out;
643 }
644
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200645 if (dev->power.irq_safe) {
646 spin_unlock(&dev->power.lock);
647
648 cpu_relax();
649
650 spin_lock(&dev->power.lock);
651 goto repeat;
652 }
653
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200654 /* Wait for the operation carried out in parallel with us. */
655 for (;;) {
656 prepare_to_wait(&dev->power.wait_queue, &wait,
657 TASK_UNINTERRUPTIBLE);
658 if (dev->power.runtime_status != RPM_RESUMING
659 && dev->power.runtime_status != RPM_SUSPENDING)
660 break;
661
662 spin_unlock_irq(&dev->power.lock);
663
664 schedule();
665
666 spin_lock_irq(&dev->power.lock);
667 }
668 finish_wait(&dev->power.wait_queue, &wait);
669 goto repeat;
670 }
671
Alan Stern7490e442010-09-25 23:35:15 +0200672 /*
673 * See if we can skip waking up the parent. This is safe only if
674 * power.no_callbacks is set, because otherwise we don't know whether
675 * the resume will actually succeed.
676 */
677 if (dev->power.no_callbacks && !parent && dev->parent) {
Ming Leid63be5f2010-10-22 23:48:14 +0200678 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
Alan Stern7490e442010-09-25 23:35:15 +0200679 if (dev->parent->power.disable_depth > 0
680 || dev->parent->power.ignore_children
681 || dev->parent->power.runtime_status == RPM_ACTIVE) {
682 atomic_inc(&dev->parent->power.child_count);
683 spin_unlock(&dev->parent->power.lock);
Rafael J. Wysocki7f321c22012-08-15 21:31:45 +0200684 retval = 1;
Alan Stern7490e442010-09-25 23:35:15 +0200685 goto no_callback; /* Assume success. */
686 }
687 spin_unlock(&dev->parent->power.lock);
688 }
689
Alan Stern1bfee5b2010-09-25 23:35:00 +0200690 /* Carry out an asynchronous or a synchronous resume. */
691 if (rpmflags & RPM_ASYNC) {
692 dev->power.request = RPM_REQ_RESUME;
693 if (!dev->power.request_pending) {
694 dev->power.request_pending = true;
695 queue_work(pm_wq, &dev->power.work);
696 }
697 retval = 0;
698 goto out;
699 }
700
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200701 if (!parent && dev->parent) {
702 /*
Alan Sternc7b61de2010-12-01 00:14:42 +0100703 * Increment the parent's usage counter and resume it if
704 * necessary. Not needed if dev is irq-safe; then the
705 * parent is permanently resumed.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200706 */
707 parent = dev->parent;
Alan Sternc7b61de2010-12-01 00:14:42 +0100708 if (dev->power.irq_safe)
709 goto skip_parent;
Alan Stern862f89b2009-11-25 01:06:37 +0100710 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200711
712 pm_runtime_get_noresume(parent);
713
Alan Stern862f89b2009-11-25 01:06:37 +0100714 spin_lock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200715 /*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200716 * We can resume if the parent's runtime PM is disabled or it
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200717 * is set to ignore children.
718 */
719 if (!parent->power.disable_depth
720 && !parent->power.ignore_children) {
Alan Stern140a6c92010-09-25 23:35:07 +0200721 rpm_resume(parent, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200722 if (parent->power.runtime_status != RPM_ACTIVE)
723 retval = -EBUSY;
724 }
Alan Stern862f89b2009-11-25 01:06:37 +0100725 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200726
Alan Stern862f89b2009-11-25 01:06:37 +0100727 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200728 if (retval)
729 goto out;
730 goto repeat;
731 }
Alan Sternc7b61de2010-12-01 00:14:42 +0100732 skip_parent:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200733
Alan Stern7490e442010-09-25 23:35:15 +0200734 if (dev->power.no_callbacks)
735 goto no_callback; /* Assume success. */
736
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200737 __update_runtime_status(dev, RPM_RESUMING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200738
Ulf Hansson5f59df72014-03-01 11:56:04 +0100739 callback = rpm_get_resume_cb(dev);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100740
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200741 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200742 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200743 __update_runtime_status(dev, RPM_SUSPENDED);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200744 pm_runtime_cancel_pending(dev);
745 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200746 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200747 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200748 if (parent)
749 atomic_inc(&parent->power.child_count);
750 }
751 wake_up_all(&dev->power.wait_queue);
752
Rafael J. Wysocki7f321c22012-08-15 21:31:45 +0200753 if (retval >= 0)
Alan Stern140a6c92010-09-25 23:35:07 +0200754 rpm_idle(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200755
756 out:
Alan Sternc7b61de2010-12-01 00:14:42 +0100757 if (parent && !dev->power.irq_safe) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200758 spin_unlock_irq(&dev->power.lock);
759
760 pm_runtime_put(parent);
761
762 spin_lock_irq(&dev->power.lock);
763 }
764
Ming Leic3dc2f12011-09-27 22:54:41 +0200765 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200766
767 return retval;
768}
769
770/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200771 * pm_runtime_work - Universal runtime PM work function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200772 * @work: Work structure used for scheduling the execution of this function.
773 *
774 * Use @work to get the device object the work is to be done for, determine what
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200775 * is to be done and execute the appropriate runtime PM function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200776 */
777static void pm_runtime_work(struct work_struct *work)
778{
779 struct device *dev = container_of(work, struct device, power.work);
780 enum rpm_request req;
781
782 spin_lock_irq(&dev->power.lock);
783
784 if (!dev->power.request_pending)
785 goto out;
786
787 req = dev->power.request;
788 dev->power.request = RPM_REQ_NONE;
789 dev->power.request_pending = false;
790
791 switch (req) {
792 case RPM_REQ_NONE:
793 break;
794 case RPM_REQ_IDLE:
Alan Stern140a6c92010-09-25 23:35:07 +0200795 rpm_idle(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200796 break;
797 case RPM_REQ_SUSPEND:
Alan Stern140a6c92010-09-25 23:35:07 +0200798 rpm_suspend(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200799 break;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200800 case RPM_REQ_AUTOSUSPEND:
801 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
802 break;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200803 case RPM_REQ_RESUME:
Alan Stern140a6c92010-09-25 23:35:07 +0200804 rpm_resume(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200805 break;
806 }
807
808 out:
809 spin_unlock_irq(&dev->power.lock);
810}
811
812/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200813 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
814 * @data: Device pointer passed by pm_schedule_suspend().
815 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200816 * Check if the time is right and queue a suspend request.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200817 */
818static void pm_suspend_timer_fn(unsigned long data)
819{
820 struct device *dev = (struct device *)data;
821 unsigned long flags;
822 unsigned long expires;
823
824 spin_lock_irqsave(&dev->power.lock, flags);
825
826 expires = dev->power.timer_expires;
827 /* If 'expire' is after 'jiffies' we've been called too early. */
828 if (expires > 0 && !time_after(expires, jiffies)) {
829 dev->power.timer_expires = 0;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200830 rpm_suspend(dev, dev->power.timer_autosuspends ?
831 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200832 }
833
834 spin_unlock_irqrestore(&dev->power.lock, flags);
835}
836
837/**
838 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
839 * @dev: Device to suspend.
840 * @delay: Time to wait before submitting a suspend request, in milliseconds.
841 */
842int pm_schedule_suspend(struct device *dev, unsigned int delay)
843{
844 unsigned long flags;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200845 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200846
847 spin_lock_irqsave(&dev->power.lock, flags);
848
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200849 if (!delay) {
Alan Stern140a6c92010-09-25 23:35:07 +0200850 retval = rpm_suspend(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200851 goto out;
852 }
853
Alan Stern1bfee5b2010-09-25 23:35:00 +0200854 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200855 if (retval)
856 goto out;
857
Alan Stern1bfee5b2010-09-25 23:35:00 +0200858 /* Other scheduled or pending requests need to be canceled. */
859 pm_runtime_cancel_pending(dev);
860
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200861 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200862 dev->power.timer_expires += !dev->power.timer_expires;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200863 dev->power.timer_autosuspends = 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200864 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
865
866 out:
867 spin_unlock_irqrestore(&dev->power.lock, flags);
868
869 return retval;
870}
871EXPORT_SYMBOL_GPL(pm_schedule_suspend);
872
873/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200874 * __pm_runtime_idle - Entry point for runtime idle operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200875 * @dev: Device to send idle notification for.
876 * @rpmflags: Flag bits.
877 *
878 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
879 * return immediately if it is larger than zero. Then carry out an idle
880 * notification, either synchronous or asynchronous.
881 *
Colin Cross311aab72011-08-08 23:39:36 +0200882 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
883 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200884 */
Alan Stern140a6c92010-09-25 23:35:07 +0200885int __pm_runtime_idle(struct device *dev, int rpmflags)
886{
887 unsigned long flags;
888 int retval;
889
Colin Cross311aab72011-08-08 23:39:36 +0200890 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
891
Alan Stern140a6c92010-09-25 23:35:07 +0200892 if (rpmflags & RPM_GET_PUT) {
893 if (!atomic_dec_and_test(&dev->power.usage_count))
894 return 0;
895 }
896
897 spin_lock_irqsave(&dev->power.lock, flags);
898 retval = rpm_idle(dev, rpmflags);
899 spin_unlock_irqrestore(&dev->power.lock, flags);
900
901 return retval;
902}
903EXPORT_SYMBOL_GPL(__pm_runtime_idle);
904
905/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200906 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200907 * @dev: Device to suspend.
908 * @rpmflags: Flag bits.
909 *
Alan Stern15bcb91d2010-09-25 23:35:21 +0200910 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
911 * return immediately if it is larger than zero. Then carry out a suspend,
912 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200913 *
Colin Cross311aab72011-08-08 23:39:36 +0200914 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
915 * or if pm_runtime_irq_safe() has been called.
Alan Stern140a6c92010-09-25 23:35:07 +0200916 */
917int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200918{
919 unsigned long flags;
920 int retval;
921
Colin Cross311aab72011-08-08 23:39:36 +0200922 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
923
Alan Stern15bcb91d2010-09-25 23:35:21 +0200924 if (rpmflags & RPM_GET_PUT) {
925 if (!atomic_dec_and_test(&dev->power.usage_count))
926 return 0;
927 }
928
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200929 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200930 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200931 spin_unlock_irqrestore(&dev->power.lock, flags);
932
933 return retval;
934}
Alan Stern140a6c92010-09-25 23:35:07 +0200935EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200936
937/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200938 * __pm_runtime_resume - Entry point for runtime resume operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200939 * @dev: Device to resume.
Alan Stern3f9af0512010-09-25 23:34:54 +0200940 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200941 *
Alan Stern140a6c92010-09-25 23:35:07 +0200942 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
943 * carry out a resume, either synchronous or asynchronous.
944 *
Colin Cross311aab72011-08-08 23:39:36 +0200945 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
946 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200947 */
Alan Stern140a6c92010-09-25 23:35:07 +0200948int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200949{
Alan Stern140a6c92010-09-25 23:35:07 +0200950 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100951 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200952
Colin Cross311aab72011-08-08 23:39:36 +0200953 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
954
Alan Stern140a6c92010-09-25 23:35:07 +0200955 if (rpmflags & RPM_GET_PUT)
956 atomic_inc(&dev->power.usage_count);
957
958 spin_lock_irqsave(&dev->power.lock, flags);
959 retval = rpm_resume(dev, rpmflags);
960 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200961
962 return retval;
963}
Alan Stern140a6c92010-09-25 23:35:07 +0200964EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200965
966/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200967 * __pm_runtime_set_status - Set runtime PM status of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200968 * @dev: Device to handle.
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200969 * @status: New runtime PM status of the device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200970 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200971 * If runtime PM of the device is disabled or its power.runtime_error field is
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200972 * different from zero, the status may be changed either to RPM_ACTIVE, or to
973 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
974 * However, if the device has a parent and the parent is not active, and the
975 * parent's power.ignore_children flag is unset, the device's status cannot be
976 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
977 *
978 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
979 * and the device parent's counter of unsuspended children is modified to
980 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
981 * notification request for the parent is submitted.
982 */
983int __pm_runtime_set_status(struct device *dev, unsigned int status)
984{
985 struct device *parent = dev->parent;
986 unsigned long flags;
987 bool notify_parent = false;
988 int error = 0;
989
990 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
991 return -EINVAL;
992
993 spin_lock_irqsave(&dev->power.lock, flags);
994
995 if (!dev->power.runtime_error && !dev->power.disable_depth) {
996 error = -EAGAIN;
997 goto out;
998 }
999
1000 if (dev->power.runtime_status == status)
1001 goto out_set;
1002
1003 if (status == RPM_SUSPENDED) {
1004 /* It always is possible to set the status to 'suspended'. */
1005 if (parent) {
1006 atomic_add_unless(&parent->power.child_count, -1, 0);
1007 notify_parent = !parent->power.ignore_children;
1008 }
1009 goto out_set;
1010 }
1011
1012 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +01001013 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001014
1015 /*
1016 * It is invalid to put an active child under a parent that is
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001017 * not active, has runtime PM enabled and the
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001018 * 'power.ignore_children' flag unset.
1019 */
1020 if (!parent->power.disable_depth
1021 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +01001022 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001023 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +01001024 else if (dev->power.runtime_status == RPM_SUSPENDED)
1025 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001026
Alan Stern862f89b2009-11-25 01:06:37 +01001027 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001028
1029 if (error)
1030 goto out;
1031 }
1032
1033 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001034 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001035 dev->power.runtime_error = 0;
1036 out:
1037 spin_unlock_irqrestore(&dev->power.lock, flags);
1038
1039 if (notify_parent)
1040 pm_request_idle(parent);
1041
1042 return error;
1043}
1044EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1045
1046/**
1047 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1048 * @dev: Device to handle.
1049 *
1050 * Flush all pending requests for the device from pm_wq and wait for all
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001051 * runtime PM operations involving the device in progress to complete.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001052 *
1053 * Should be called under dev->power.lock with interrupts disabled.
1054 */
1055static void __pm_runtime_barrier(struct device *dev)
1056{
1057 pm_runtime_deactivate_timer(dev);
1058
1059 if (dev->power.request_pending) {
1060 dev->power.request = RPM_REQ_NONE;
1061 spin_unlock_irq(&dev->power.lock);
1062
1063 cancel_work_sync(&dev->power.work);
1064
1065 spin_lock_irq(&dev->power.lock);
1066 dev->power.request_pending = false;
1067 }
1068
1069 if (dev->power.runtime_status == RPM_SUSPENDING
1070 || dev->power.runtime_status == RPM_RESUMING
1071 || dev->power.idle_notification) {
1072 DEFINE_WAIT(wait);
1073
1074 /* Suspend, wake-up or idle notification in progress. */
1075 for (;;) {
1076 prepare_to_wait(&dev->power.wait_queue, &wait,
1077 TASK_UNINTERRUPTIBLE);
1078 if (dev->power.runtime_status != RPM_SUSPENDING
1079 && dev->power.runtime_status != RPM_RESUMING
1080 && !dev->power.idle_notification)
1081 break;
1082 spin_unlock_irq(&dev->power.lock);
1083
1084 schedule();
1085
1086 spin_lock_irq(&dev->power.lock);
1087 }
1088 finish_wait(&dev->power.wait_queue, &wait);
1089 }
1090}
1091
1092/**
1093 * pm_runtime_barrier - Flush pending requests and wait for completions.
1094 * @dev: Device to handle.
1095 *
1096 * Prevent the device from being suspended by incrementing its usage counter and
1097 * if there's a pending resume request for the device, wake the device up.
1098 * Next, make sure that all pending requests for the device have been flushed
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001099 * from pm_wq and wait for all runtime PM operations involving the device in
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001100 * progress to complete.
1101 *
1102 * Return value:
1103 * 1, if there was a resume request pending and the device had to be woken up,
1104 * 0, otherwise
1105 */
1106int pm_runtime_barrier(struct device *dev)
1107{
1108 int retval = 0;
1109
1110 pm_runtime_get_noresume(dev);
1111 spin_lock_irq(&dev->power.lock);
1112
1113 if (dev->power.request_pending
1114 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +02001115 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001116 retval = 1;
1117 }
1118
1119 __pm_runtime_barrier(dev);
1120
1121 spin_unlock_irq(&dev->power.lock);
1122 pm_runtime_put_noidle(dev);
1123
1124 return retval;
1125}
1126EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1127
1128/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001129 * __pm_runtime_disable - Disable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001130 * @dev: Device to handle.
1131 * @check_resume: If set, check if there's a resume request for the device.
1132 *
1133 * Increment power.disable_depth for the device and if was zero previously,
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001134 * cancel all pending runtime PM requests for the device and wait for all
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001135 * operations in progress to complete. The device can be either active or
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001136 * suspended after its runtime PM has been disabled.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001137 *
1138 * If @check_resume is set and there's a resume request pending when
1139 * __pm_runtime_disable() is called and power.disable_depth is zero, the
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001140 * function will wake up the device before disabling its runtime PM.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001141 */
1142void __pm_runtime_disable(struct device *dev, bool check_resume)
1143{
1144 spin_lock_irq(&dev->power.lock);
1145
1146 if (dev->power.disable_depth > 0) {
1147 dev->power.disable_depth++;
1148 goto out;
1149 }
1150
1151 /*
1152 * Wake up the device if there's a resume request pending, because that
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001153 * means there probably is some I/O to process and disabling runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001154 * shouldn't prevent the device from processing the I/O.
1155 */
1156 if (check_resume && dev->power.request_pending
1157 && dev->power.request == RPM_REQ_RESUME) {
1158 /*
1159 * Prevent suspends and idle notifications from being carried
1160 * out after we have woken up the device.
1161 */
1162 pm_runtime_get_noresume(dev);
1163
Alan Stern140a6c92010-09-25 23:35:07 +02001164 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001165
1166 pm_runtime_put_noidle(dev);
1167 }
1168
1169 if (!dev->power.disable_depth++)
1170 __pm_runtime_barrier(dev);
1171
1172 out:
1173 spin_unlock_irq(&dev->power.lock);
1174}
1175EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1176
1177/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001178 * pm_runtime_enable - Enable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001179 * @dev: Device to handle.
1180 */
1181void pm_runtime_enable(struct device *dev)
1182{
1183 unsigned long flags;
1184
1185 spin_lock_irqsave(&dev->power.lock, flags);
1186
1187 if (dev->power.disable_depth > 0)
1188 dev->power.disable_depth--;
1189 else
1190 dev_warn(dev, "Unbalanced %s!\n", __func__);
1191
1192 spin_unlock_irqrestore(&dev->power.lock, flags);
1193}
1194EXPORT_SYMBOL_GPL(pm_runtime_enable);
1195
1196/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001197 * pm_runtime_forbid - Block runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001198 * @dev: Device to handle.
1199 *
1200 * Increase the device's usage count and clear its power.runtime_auto flag,
1201 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1202 * for it.
1203 */
1204void pm_runtime_forbid(struct device *dev)
1205{
1206 spin_lock_irq(&dev->power.lock);
1207 if (!dev->power.runtime_auto)
1208 goto out;
1209
1210 dev->power.runtime_auto = false;
1211 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001212 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001213
1214 out:
1215 spin_unlock_irq(&dev->power.lock);
1216}
1217EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1218
1219/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001220 * pm_runtime_allow - Unblock runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001221 * @dev: Device to handle.
1222 *
1223 * Decrease the device's usage count and set its power.runtime_auto flag.
1224 */
1225void pm_runtime_allow(struct device *dev)
1226{
1227 spin_lock_irq(&dev->power.lock);
1228 if (dev->power.runtime_auto)
1229 goto out;
1230
1231 dev->power.runtime_auto = true;
1232 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb91d2010-09-25 23:35:21 +02001233 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001234
1235 out:
1236 spin_unlock_irq(&dev->power.lock);
1237}
1238EXPORT_SYMBOL_GPL(pm_runtime_allow);
1239
1240/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001241 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
Alan Stern7490e442010-09-25 23:35:15 +02001242 * @dev: Device to handle.
1243 *
1244 * Set the power.no_callbacks flag, which tells the PM core that this
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001245 * device is power-managed through its parent and has no runtime PM
1246 * callbacks of its own. The runtime sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001247 */
1248void pm_runtime_no_callbacks(struct device *dev)
1249{
1250 spin_lock_irq(&dev->power.lock);
1251 dev->power.no_callbacks = 1;
1252 spin_unlock_irq(&dev->power.lock);
1253 if (device_is_registered(dev))
1254 rpm_sysfs_remove(dev);
1255}
1256EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1257
1258/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001259 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1260 * @dev: Device to handle
1261 *
1262 * Set the power.irq_safe flag, which tells the PM core that the
1263 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1264 * always be invoked with the spinlock held and interrupts disabled. It also
1265 * causes the parent's usage counter to be permanently incremented, preventing
1266 * the parent from runtime suspending -- otherwise an irq-safe child might have
1267 * to wait for a non-irq-safe parent.
1268 */
1269void pm_runtime_irq_safe(struct device *dev)
1270{
1271 if (dev->parent)
1272 pm_runtime_get_sync(dev->parent);
1273 spin_lock_irq(&dev->power.lock);
1274 dev->power.irq_safe = 1;
1275 spin_unlock_irq(&dev->power.lock);
1276}
1277EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1278
1279/**
Alan Stern15bcb91d2010-09-25 23:35:21 +02001280 * update_autosuspend - Handle a change to a device's autosuspend settings.
1281 * @dev: Device to handle.
1282 * @old_delay: The former autosuspend_delay value.
1283 * @old_use: The former use_autosuspend value.
1284 *
1285 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1286 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1287 *
1288 * This function must be called under dev->power.lock with interrupts disabled.
1289 */
1290static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1291{
1292 int delay = dev->power.autosuspend_delay;
1293
1294 /* Should runtime suspend be prevented now? */
1295 if (dev->power.use_autosuspend && delay < 0) {
1296
1297 /* If it used to be allowed then prevent it. */
1298 if (!old_use || old_delay >= 0) {
1299 atomic_inc(&dev->power.usage_count);
1300 rpm_resume(dev, 0);
1301 }
1302 }
1303
1304 /* Runtime suspend should be allowed now. */
1305 else {
1306
1307 /* If it used to be prevented then allow it. */
1308 if (old_use && old_delay < 0)
1309 atomic_dec(&dev->power.usage_count);
1310
1311 /* Maybe we can autosuspend now. */
1312 rpm_idle(dev, RPM_AUTO);
1313 }
1314}
1315
1316/**
1317 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1318 * @dev: Device to handle.
1319 * @delay: Value of the new delay in milliseconds.
1320 *
1321 * Set the device's power.autosuspend_delay value. If it changes to negative
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001322 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1323 * changes the other way, allow runtime suspends.
Alan Stern15bcb91d2010-09-25 23:35:21 +02001324 */
1325void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1326{
1327 int old_delay, old_use;
1328
1329 spin_lock_irq(&dev->power.lock);
1330 old_delay = dev->power.autosuspend_delay;
1331 old_use = dev->power.use_autosuspend;
1332 dev->power.autosuspend_delay = delay;
1333 update_autosuspend(dev, old_delay, old_use);
1334 spin_unlock_irq(&dev->power.lock);
1335}
1336EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1337
1338/**
1339 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1340 * @dev: Device to handle.
1341 * @use: New value for use_autosuspend.
1342 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001343 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
Alan Stern15bcb91d2010-09-25 23:35:21 +02001344 * suspends as needed.
1345 */
1346void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1347{
1348 int old_delay, old_use;
1349
1350 spin_lock_irq(&dev->power.lock);
1351 old_delay = dev->power.autosuspend_delay;
1352 old_use = dev->power.use_autosuspend;
1353 dev->power.use_autosuspend = use;
1354 update_autosuspend(dev, old_delay, old_use);
1355 spin_unlock_irq(&dev->power.lock);
1356}
1357EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1358
1359/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001360 * pm_runtime_init - Initialize runtime PM fields in given device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001361 * @dev: Device object to initialize.
1362 */
1363void pm_runtime_init(struct device *dev)
1364{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001365 dev->power.runtime_status = RPM_SUSPENDED;
1366 dev->power.idle_notification = false;
1367
1368 dev->power.disable_depth = 1;
1369 atomic_set(&dev->power.usage_count, 0);
1370
1371 dev->power.runtime_error = 0;
1372
1373 atomic_set(&dev->power.child_count, 0);
1374 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001375 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001376
1377 dev->power.request_pending = false;
1378 dev->power.request = RPM_REQ_NONE;
1379 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001380 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001381 INIT_WORK(&dev->power.work, pm_runtime_work);
1382
1383 dev->power.timer_expires = 0;
1384 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1385 (unsigned long)dev);
1386
1387 init_waitqueue_head(&dev->power.wait_queue);
1388}
1389
1390/**
1391 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1392 * @dev: Device object being removed from device hierarchy.
1393 */
1394void pm_runtime_remove(struct device *dev)
1395{
1396 __pm_runtime_disable(dev, false);
1397
1398 /* Change the status back to 'suspended' to match the initial status. */
1399 if (dev->power.runtime_status == RPM_ACTIVE)
1400 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001401 if (dev->power.irq_safe && dev->parent)
Ulf Hanssondb28dfa2013-04-12 09:41:30 +00001402 pm_runtime_put(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001403}