blob: 268a35097578d94f78f4baef4d9cc8b3fa1c31cd [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
Alan Stern140a6c92010-09-25 23:35:07 +020016static int rpm_resume(struct device *dev, int rpmflags);
Alan Stern7490e442010-09-25 23:35:15 +020017static int rpm_suspend(struct device *dev, int rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020018
19/**
Alan Stern47693732010-09-25 23:34:46 +020020 * update_pm_runtime_accounting - Update the time accounting of power states
21 * @dev: Device to update the accounting for
22 *
23 * In order to be able to have time accounting of the various power states
24 * (as used by programs such as PowerTOP to show the effectiveness of runtime
25 * PM), we need to track the time spent in each state.
26 * update_pm_runtime_accounting must be called each time before the
27 * runtime_status field is updated, to account the time in the old state
28 * correctly.
29 */
30void update_pm_runtime_accounting(struct device *dev)
31{
32 unsigned long now = jiffies;
venu byravarasudef0c0a32011-11-03 10:12:14 +010033 unsigned long delta;
Alan Stern47693732010-09-25 23:34:46 +020034
35 delta = now - dev->power.accounting_timestamp;
36
Alan Stern47693732010-09-25 23:34:46 +020037 dev->power.accounting_timestamp = now;
38
39 if (dev->power.disable_depth > 0)
40 return;
41
42 if (dev->power.runtime_status == RPM_SUSPENDED)
43 dev->power.suspended_jiffies += delta;
44 else
45 dev->power.active_jiffies += delta;
46}
47
48static void __update_runtime_status(struct device *dev, enum rpm_status status)
49{
50 update_pm_runtime_accounting(dev);
51 dev->power.runtime_status = status;
52}
53
54/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020055 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
56 * @dev: Device to handle.
57 */
58static void pm_runtime_deactivate_timer(struct device *dev)
59{
60 if (dev->power.timer_expires > 0) {
61 del_timer(&dev->power.suspend_timer);
62 dev->power.timer_expires = 0;
63 }
64}
65
66/**
67 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
68 * @dev: Device to handle.
69 */
70static void pm_runtime_cancel_pending(struct device *dev)
71{
72 pm_runtime_deactivate_timer(dev);
73 /*
74 * In case there's a request pending, make sure its work function will
75 * return without doing anything.
76 */
77 dev->power.request = RPM_REQ_NONE;
78}
79
Alan Stern15bcb91d2010-09-25 23:35:21 +020080/*
81 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
82 * @dev: Device to handle.
83 *
84 * Compute the autosuspend-delay expiration time based on the device's
85 * power.last_busy time. If the delay has already expired or is disabled
86 * (negative) or the power.use_autosuspend flag isn't set, return 0.
87 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
88 *
89 * This function may be called either with or without dev->power.lock held.
90 * Either way it can be racy, since power.last_busy may be updated at any time.
91 */
92unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
93{
94 int autosuspend_delay;
95 long elapsed;
96 unsigned long last_busy;
97 unsigned long expires = 0;
98
99 if (!dev->power.use_autosuspend)
100 goto out;
101
102 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
103 if (autosuspend_delay < 0)
104 goto out;
105
106 last_busy = ACCESS_ONCE(dev->power.last_busy);
107 elapsed = jiffies - last_busy;
108 if (elapsed < 0)
109 goto out; /* jiffies has wrapped around. */
110
111 /*
112 * If the autosuspend_delay is >= 1 second, align the timer by rounding
113 * up to the nearest second.
114 */
115 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
116 if (autosuspend_delay >= 1000)
117 expires = round_jiffies(expires);
118 expires += !expires;
119 if (elapsed >= expires - last_busy)
120 expires = 0; /* Already expired. */
121
122 out:
123 return expires;
124}
125EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
126
Ming Leie8234072013-02-22 16:34:11 -0800127static int dev_memalloc_noio(struct device *dev, void *data)
128{
129 return dev->power.memalloc_noio;
130}
131
132/*
133 * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
134 * @dev: Device to handle.
135 * @enable: True for setting the flag and False for clearing the flag.
136 *
137 * Set the flag for all devices in the path from the device to the
138 * root device in the device tree if @enable is true, otherwise clear
139 * the flag for devices in the path whose siblings don't set the flag.
140 *
141 * The function should only be called by block device, or network
142 * device driver for solving the deadlock problem during runtime
143 * resume/suspend:
144 *
145 * If memory allocation with GFP_KERNEL is called inside runtime
146 * resume/suspend callback of any one of its ancestors(or the
147 * block device itself), the deadlock may be triggered inside the
148 * memory allocation since it might not complete until the block
149 * device becomes active and the involed page I/O finishes. The
150 * situation is pointed out first by Alan Stern. Network device
151 * are involved in iSCSI kind of situation.
152 *
153 * The lock of dev_hotplug_mutex is held in the function for handling
154 * hotplug race because pm_runtime_set_memalloc_noio() may be called
155 * in async probe().
156 *
157 * The function should be called between device_add() and device_del()
158 * on the affected device(block/network device).
159 */
160void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
161{
162 static DEFINE_MUTEX(dev_hotplug_mutex);
163
164 mutex_lock(&dev_hotplug_mutex);
165 for (;;) {
166 bool enabled;
167
168 /* hold power lock since bitfield is not SMP-safe. */
169 spin_lock_irq(&dev->power.lock);
170 enabled = dev->power.memalloc_noio;
171 dev->power.memalloc_noio = enable;
172 spin_unlock_irq(&dev->power.lock);
173
174 /*
175 * not need to enable ancestors any more if the device
176 * has been enabled.
177 */
178 if (enabled && enable)
179 break;
180
181 dev = dev->parent;
182
183 /*
184 * clear flag of the parent device only if all the
185 * children don't set the flag because ancestor's
186 * flag was set by any one of the descendants.
187 */
188 if (!dev || (!enable &&
189 device_for_each_child(dev, NULL,
190 dev_memalloc_noio)))
191 break;
192 }
193 mutex_unlock(&dev_hotplug_mutex);
194}
195EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
196
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200197/**
Alan Stern1bfee5b2010-09-25 23:35:00 +0200198 * rpm_check_suspend_allowed - Test whether a device may be suspended.
199 * @dev: Device to test.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200200 */
Alan Stern1bfee5b2010-09-25 23:35:00 +0200201static int rpm_check_suspend_allowed(struct device *dev)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200202{
203 int retval = 0;
204
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200205 if (dev->power.runtime_error)
206 retval = -EINVAL;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200207 else if (dev->power.disable_depth > 0)
208 retval = -EACCES;
209 else if (atomic_read(&dev->power.usage_count) > 0)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200210 retval = -EAGAIN;
211 else if (!pm_children_suspended(dev))
212 retval = -EBUSY;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200213
214 /* Pending resume requests take precedence over suspends. */
215 else if ((dev->power.deferred_resume
Kevin Winchester78ca7c32010-10-29 15:29:55 +0200216 && dev->power.runtime_status == RPM_SUSPENDING)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200217 || (dev->power.request_pending
218 && dev->power.request == RPM_REQ_RESUME))
219 retval = -EAGAIN;
Rafael J. Wysocki55d7ec42012-08-15 21:32:04 +0200220 else if (__dev_pm_qos_read_value(dev) < 0)
221 retval = -EPERM;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200222 else if (dev->power.runtime_status == RPM_SUSPENDED)
223 retval = 1;
224
225 return retval;
226}
227
Alan Stern1bfee5b2010-09-25 23:35:00 +0200228/**
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200229 * __rpm_callback - Run a given runtime PM callback for a given device.
230 * @cb: Runtime PM callback to run.
231 * @dev: Device to run the callback for.
232 */
233static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
234 __releases(&dev->power.lock) __acquires(&dev->power.lock)
235{
236 int retval;
237
238 if (dev->power.irq_safe)
239 spin_unlock(&dev->power.lock);
240 else
241 spin_unlock_irq(&dev->power.lock);
242
243 retval = cb(dev);
244
245 if (dev->power.irq_safe)
246 spin_lock(&dev->power.lock);
247 else
248 spin_lock_irq(&dev->power.lock);
249
250 return retval;
251}
252
253/**
Alan Stern140a6c92010-09-25 23:35:07 +0200254 * rpm_idle - Notify device bus type if the device can be suspended.
Alan Stern1bfee5b2010-09-25 23:35:00 +0200255 * @dev: Device to notify the bus type about.
256 * @rpmflags: Flag bits.
257 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200258 * Check if the device's runtime PM status allows it to be suspended. If
Alan Stern1bfee5b2010-09-25 23:35:00 +0200259 * another idle notification has been started earlier, return immediately. If
260 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
261 * run the ->runtime_idle() callback directly.
262 *
263 * This function must be called under dev->power.lock with interrupts disabled.
264 */
Alan Stern140a6c92010-09-25 23:35:07 +0200265static int rpm_idle(struct device *dev, int rpmflags)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200266{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200267 int (*callback)(struct device *);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200268 int retval;
269
Ming Leic3dc2f12011-09-27 22:54:41 +0200270 trace_rpm_idle(dev, rpmflags);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200271 retval = rpm_check_suspend_allowed(dev);
272 if (retval < 0)
273 ; /* Conditions are wrong. */
274
275 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
276 else if (dev->power.runtime_status != RPM_ACTIVE)
277 retval = -EAGAIN;
278
279 /*
280 * Any pending request other than an idle notification takes
281 * precedence over us, except that the timer may be running.
282 */
283 else if (dev->power.request_pending &&
284 dev->power.request > RPM_REQ_IDLE)
285 retval = -EAGAIN;
286
287 /* Act as though RPM_NOWAIT is always set. */
288 else if (dev->power.idle_notification)
289 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200290 if (retval)
291 goto out;
292
Alan Stern1bfee5b2010-09-25 23:35:00 +0200293 /* Pending requests need to be canceled. */
294 dev->power.request = RPM_REQ_NONE;
295
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200296 if (dev->power.no_callbacks)
Alan Stern7490e442010-09-25 23:35:15 +0200297 goto out;
Alan Stern7490e442010-09-25 23:35:15 +0200298
Alan Stern1bfee5b2010-09-25 23:35:00 +0200299 /* Carry out an asynchronous or a synchronous idle notification. */
300 if (rpmflags & RPM_ASYNC) {
301 dev->power.request = RPM_REQ_IDLE;
302 if (!dev->power.request_pending) {
303 dev->power.request_pending = true;
304 queue_work(pm_wq, &dev->power.work);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200305 }
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200306 trace_rpm_return_int(dev, _THIS_IP_, 0);
307 return 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200308 }
309
310 dev->power.idle_notification = true;
311
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200312 if (dev->pm_domain)
313 callback = dev->pm_domain->ops.runtime_idle;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200314 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200315 callback = dev->type->pm->runtime_idle;
316 else if (dev->class && dev->class->pm)
317 callback = dev->class->pm->runtime_idle;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100318 else if (dev->bus && dev->bus->pm)
319 callback = dev->bus->pm->runtime_idle;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200320 else
321 callback = NULL;
322
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100323 if (!callback && dev->driver && dev->driver->pm)
324 callback = dev->driver->pm->runtime_idle;
325
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200326 if (callback)
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200327 retval = __rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200328
329 dev->power.idle_notification = false;
330 wake_up_all(&dev->power.wait_queue);
331
332 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200333 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200334 return retval ? retval : rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200335}
336
337/**
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200338 * rpm_callback - Run a given runtime PM callback for a given device.
339 * @cb: Runtime PM callback to run.
340 * @dev: Device to run the callback for.
341 */
342static int rpm_callback(int (*cb)(struct device *), struct device *dev)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200343{
344 int retval;
345
346 if (!cb)
347 return -ENOSYS;
348
Ming Leidb881752013-02-22 16:34:19 -0800349 if (dev->power.memalloc_noio) {
350 unsigned int noio_flag;
351
352 /*
353 * Deadlock might be caused if memory allocation with
354 * GFP_KERNEL happens inside runtime_suspend and
355 * runtime_resume callbacks of one block device's
356 * ancestor or the block device itself. Network
357 * device might be thought as part of iSCSI block
358 * device, so network device and its ancestor should
359 * be marked as memalloc_noio too.
360 */
361 noio_flag = memalloc_noio_save();
362 retval = __rpm_callback(cb, dev);
363 memalloc_noio_restore(noio_flag);
364 } else {
365 retval = __rpm_callback(cb, dev);
366 }
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200367
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200368 dev->power.runtime_error = retval;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200369 return retval != -EACCES ? retval : -EIO;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200370}
371
372/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200373 * rpm_suspend - Carry out runtime suspend of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200374 * @dev: Device to suspend.
Alan Stern3f9af0512010-09-25 23:34:54 +0200375 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200376 *
Ming Lei47d8f0b2011-10-12 11:53:32 +0800377 * Check if the device's runtime PM status allows it to be suspended.
378 * Cancel a pending idle notification, autosuspend or suspend. If
379 * another suspend has been started earlier, either return immediately
380 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
381 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
Ming Lei857b36c2011-10-12 22:59:33 +0200382 * otherwise run the ->runtime_suspend() callback directly. When
383 * ->runtime_suspend succeeded, if a deferred resume was requested while
384 * the callback was running then carry it out, otherwise send an idle
385 * notification for its parent (if the suspend succeeded and both
386 * ignore_children of parent->power and irq_safe of dev->power are not set).
Alan Stern886486b2011-11-03 23:39:18 +0100387 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
388 * flag is set and the next autosuspend-delay expiration time is in the
389 * future, schedule another autosuspend attempt.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200390 *
391 * This function must be called under dev->power.lock with interrupts disabled.
392 */
Alan Stern140a6c92010-09-25 23:35:07 +0200393static int rpm_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200394 __releases(&dev->power.lock) __acquires(&dev->power.lock)
395{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200396 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200397 struct device *parent = NULL;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200398 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200399
Ming Leic3dc2f12011-09-27 22:54:41 +0200400 trace_rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200401
402 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200403 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200404
Alan Stern1bfee5b2010-09-25 23:35:00 +0200405 if (retval < 0)
406 ; /* Conditions are wrong. */
407
408 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
409 else if (dev->power.runtime_status == RPM_RESUMING &&
410 !(rpmflags & RPM_ASYNC))
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200411 retval = -EAGAIN;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200412 if (retval)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200413 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200414
Alan Stern15bcb91d2010-09-25 23:35:21 +0200415 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
416 if ((rpmflags & RPM_AUTO)
417 && dev->power.runtime_status != RPM_SUSPENDING) {
418 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
419
420 if (expires != 0) {
421 /* Pending requests need to be canceled. */
422 dev->power.request = RPM_REQ_NONE;
423
424 /*
425 * Optimization: If the timer is already running and is
426 * set to expire at or before the autosuspend delay,
427 * avoid the overhead of resetting it. Just let it
428 * expire; pm_suspend_timer_fn() will take care of the
429 * rest.
430 */
431 if (!(dev->power.timer_expires && time_before_eq(
432 dev->power.timer_expires, expires))) {
433 dev->power.timer_expires = expires;
434 mod_timer(&dev->power.suspend_timer, expires);
435 }
436 dev->power.timer_autosuspends = 1;
437 goto out;
438 }
439 }
440
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200441 /* Other scheduled or pending requests need to be canceled. */
442 pm_runtime_cancel_pending(dev);
443
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200444 if (dev->power.runtime_status == RPM_SUSPENDING) {
445 DEFINE_WAIT(wait);
446
Alan Stern1bfee5b2010-09-25 23:35:00 +0200447 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200448 retval = -EINPROGRESS;
449 goto out;
450 }
451
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200452 if (dev->power.irq_safe) {
453 spin_unlock(&dev->power.lock);
454
455 cpu_relax();
456
457 spin_lock(&dev->power.lock);
458 goto repeat;
459 }
460
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200461 /* Wait for the other suspend running in parallel with us. */
462 for (;;) {
463 prepare_to_wait(&dev->power.wait_queue, &wait,
464 TASK_UNINTERRUPTIBLE);
465 if (dev->power.runtime_status != RPM_SUSPENDING)
466 break;
467
468 spin_unlock_irq(&dev->power.lock);
469
470 schedule();
471
472 spin_lock_irq(&dev->power.lock);
473 }
474 finish_wait(&dev->power.wait_queue, &wait);
475 goto repeat;
476 }
477
Alan Stern7490e442010-09-25 23:35:15 +0200478 if (dev->power.no_callbacks)
479 goto no_callback; /* Assume success. */
480
Alan Stern1bfee5b2010-09-25 23:35:00 +0200481 /* Carry out an asynchronous or a synchronous suspend. */
482 if (rpmflags & RPM_ASYNC) {
Alan Stern15bcb91d2010-09-25 23:35:21 +0200483 dev->power.request = (rpmflags & RPM_AUTO) ?
484 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200485 if (!dev->power.request_pending) {
486 dev->power.request_pending = true;
487 queue_work(pm_wq, &dev->power.work);
488 }
489 goto out;
490 }
491
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200492 __update_runtime_status(dev, RPM_SUSPENDING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200493
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200494 if (dev->pm_domain)
495 callback = dev->pm_domain->ops.runtime_suspend;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200496 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200497 callback = dev->type->pm->runtime_suspend;
498 else if (dev->class && dev->class->pm)
499 callback = dev->class->pm->runtime_suspend;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100500 else if (dev->bus && dev->bus->pm)
501 callback = dev->bus->pm->runtime_suspend;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200502 else
503 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200504
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100505 if (!callback && dev->driver && dev->driver->pm)
506 callback = dev->driver->pm->runtime_suspend;
507
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200508 retval = rpm_callback(callback, dev);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100509 if (retval)
510 goto fail;
Alan Stern886486b2011-11-03 23:39:18 +0100511
Alan Stern7490e442010-09-25 23:35:15 +0200512 no_callback:
Ming Lei857b36c2011-10-12 22:59:33 +0200513 __update_runtime_status(dev, RPM_SUSPENDED);
514 pm_runtime_deactivate_timer(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200515
Ming Lei857b36c2011-10-12 22:59:33 +0200516 if (dev->parent) {
517 parent = dev->parent;
518 atomic_add_unless(&parent->power.child_count, -1, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200519 }
520 wake_up_all(&dev->power.wait_queue);
521
522 if (dev->power.deferred_resume) {
Rafael J. Wysocki58a34de2012-08-15 21:31:55 +0200523 dev->power.deferred_resume = false;
Alan Stern140a6c92010-09-25 23:35:07 +0200524 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200525 retval = -EAGAIN;
526 goto out;
527 }
528
Alan Sternc3810c82011-01-25 20:50:07 +0100529 /* Maybe the parent is now able to suspend. */
Alan Sternc7b61de2010-12-01 00:14:42 +0100530 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
Alan Sternc3810c82011-01-25 20:50:07 +0100531 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200532
Alan Sternc3810c82011-01-25 20:50:07 +0100533 spin_lock(&parent->power.lock);
534 rpm_idle(parent, RPM_ASYNC);
535 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200536
Alan Sternc3810c82011-01-25 20:50:07 +0100537 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200538 }
539
540 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200541 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200542
543 return retval;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100544
545 fail:
546 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100547 dev->power.deferred_resume = false;
Alan Sternf2791d72012-03-26 22:46:52 +0200548 wake_up_all(&dev->power.wait_queue);
549
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100550 if (retval == -EAGAIN || retval == -EBUSY) {
551 dev->power.runtime_error = 0;
552
553 /*
554 * If the callback routine failed an autosuspend, and
555 * if the last_busy time has been updated so that there
556 * is a new autosuspend expiration time, automatically
557 * reschedule another autosuspend.
558 */
559 if ((rpmflags & RPM_AUTO) &&
560 pm_runtime_autosuspend_expiration(dev) != 0)
561 goto repeat;
562 } else {
563 pm_runtime_cancel_pending(dev);
564 }
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100565 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200566}
567
568/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200569 * rpm_resume - Carry out runtime resume of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200570 * @dev: Device to resume.
Alan Stern3f9af0512010-09-25 23:34:54 +0200571 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200572 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200573 * Check if the device's runtime PM status allows it to be resumed. Cancel
Alan Stern1bfee5b2010-09-25 23:35:00 +0200574 * any scheduled or pending requests. If another resume has been started
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300575 * earlier, either return immediately or wait for it to finish, depending on the
Alan Stern1bfee5b2010-09-25 23:35:00 +0200576 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
577 * parallel with this function, either tell the other process to resume after
578 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
579 * flag is set then queue a resume request; otherwise run the
580 * ->runtime_resume() callback directly. Queue an idle notification for the
581 * device if the resume succeeded.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200582 *
583 * This function must be called under dev->power.lock with interrupts disabled.
584 */
Alan Stern140a6c92010-09-25 23:35:07 +0200585static int rpm_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200586 __releases(&dev->power.lock) __acquires(&dev->power.lock)
587{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200588 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200589 struct device *parent = NULL;
590 int retval = 0;
591
Ming Leic3dc2f12011-09-27 22:54:41 +0200592 trace_rpm_resume(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200593
594 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200595 if (dev->power.runtime_error)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200596 retval = -EINVAL;
Kevin Hilman6f3c77b2012-09-21 22:47:34 +0000597 else if (dev->power.disable_depth == 1 && dev->power.is_suspended
598 && dev->power.runtime_status == RPM_ACTIVE)
599 retval = 1;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200600 else if (dev->power.disable_depth > 0)
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200601 retval = -EACCES;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200602 if (retval)
603 goto out;
604
Alan Stern15bcb91d2010-09-25 23:35:21 +0200605 /*
606 * Other scheduled or pending requests need to be canceled. Small
607 * optimization: If an autosuspend timer is running, leave it running
608 * rather than cancelling it now only to restart it again in the near
609 * future.
610 */
611 dev->power.request = RPM_REQ_NONE;
612 if (!dev->power.timer_autosuspends)
613 pm_runtime_deactivate_timer(dev);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200614
615 if (dev->power.runtime_status == RPM_ACTIVE) {
616 retval = 1;
617 goto out;
618 }
619
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200620 if (dev->power.runtime_status == RPM_RESUMING
621 || dev->power.runtime_status == RPM_SUSPENDING) {
622 DEFINE_WAIT(wait);
623
Alan Stern1bfee5b2010-09-25 23:35:00 +0200624 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200625 if (dev->power.runtime_status == RPM_SUSPENDING)
626 dev->power.deferred_resume = true;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200627 else
628 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200629 goto out;
630 }
631
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200632 if (dev->power.irq_safe) {
633 spin_unlock(&dev->power.lock);
634
635 cpu_relax();
636
637 spin_lock(&dev->power.lock);
638 goto repeat;
639 }
640
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200641 /* Wait for the operation carried out in parallel with us. */
642 for (;;) {
643 prepare_to_wait(&dev->power.wait_queue, &wait,
644 TASK_UNINTERRUPTIBLE);
645 if (dev->power.runtime_status != RPM_RESUMING
646 && dev->power.runtime_status != RPM_SUSPENDING)
647 break;
648
649 spin_unlock_irq(&dev->power.lock);
650
651 schedule();
652
653 spin_lock_irq(&dev->power.lock);
654 }
655 finish_wait(&dev->power.wait_queue, &wait);
656 goto repeat;
657 }
658
Alan Stern7490e442010-09-25 23:35:15 +0200659 /*
660 * See if we can skip waking up the parent. This is safe only if
661 * power.no_callbacks is set, because otherwise we don't know whether
662 * the resume will actually succeed.
663 */
664 if (dev->power.no_callbacks && !parent && dev->parent) {
Ming Leid63be5f2010-10-22 23:48:14 +0200665 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
Alan Stern7490e442010-09-25 23:35:15 +0200666 if (dev->parent->power.disable_depth > 0
667 || dev->parent->power.ignore_children
668 || dev->parent->power.runtime_status == RPM_ACTIVE) {
669 atomic_inc(&dev->parent->power.child_count);
670 spin_unlock(&dev->parent->power.lock);
Rafael J. Wysocki7f321c22012-08-15 21:31:45 +0200671 retval = 1;
Alan Stern7490e442010-09-25 23:35:15 +0200672 goto no_callback; /* Assume success. */
673 }
674 spin_unlock(&dev->parent->power.lock);
675 }
676
Alan Stern1bfee5b2010-09-25 23:35:00 +0200677 /* Carry out an asynchronous or a synchronous resume. */
678 if (rpmflags & RPM_ASYNC) {
679 dev->power.request = RPM_REQ_RESUME;
680 if (!dev->power.request_pending) {
681 dev->power.request_pending = true;
682 queue_work(pm_wq, &dev->power.work);
683 }
684 retval = 0;
685 goto out;
686 }
687
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200688 if (!parent && dev->parent) {
689 /*
Alan Sternc7b61de2010-12-01 00:14:42 +0100690 * Increment the parent's usage counter and resume it if
691 * necessary. Not needed if dev is irq-safe; then the
692 * parent is permanently resumed.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200693 */
694 parent = dev->parent;
Alan Sternc7b61de2010-12-01 00:14:42 +0100695 if (dev->power.irq_safe)
696 goto skip_parent;
Alan Stern862f89b2009-11-25 01:06:37 +0100697 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200698
699 pm_runtime_get_noresume(parent);
700
Alan Stern862f89b2009-11-25 01:06:37 +0100701 spin_lock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200702 /*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200703 * We can resume if the parent's runtime PM is disabled or it
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200704 * is set to ignore children.
705 */
706 if (!parent->power.disable_depth
707 && !parent->power.ignore_children) {
Alan Stern140a6c92010-09-25 23:35:07 +0200708 rpm_resume(parent, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200709 if (parent->power.runtime_status != RPM_ACTIVE)
710 retval = -EBUSY;
711 }
Alan Stern862f89b2009-11-25 01:06:37 +0100712 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200713
Alan Stern862f89b2009-11-25 01:06:37 +0100714 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200715 if (retval)
716 goto out;
717 goto repeat;
718 }
Alan Sternc7b61de2010-12-01 00:14:42 +0100719 skip_parent:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200720
Alan Stern7490e442010-09-25 23:35:15 +0200721 if (dev->power.no_callbacks)
722 goto no_callback; /* Assume success. */
723
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200724 __update_runtime_status(dev, RPM_RESUMING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200725
Rafael J. Wysocki564b9052011-06-23 01:52:55 +0200726 if (dev->pm_domain)
727 callback = dev->pm_domain->ops.runtime_resume;
Rafael J. Wysocki4d27e9d2011-04-29 00:35:50 +0200728 else if (dev->type && dev->type->pm)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200729 callback = dev->type->pm->runtime_resume;
730 else if (dev->class && dev->class->pm)
731 callback = dev->class->pm->runtime_resume;
Rafael J. Wysocki9659cc02011-02-18 23:20:21 +0100732 else if (dev->bus && dev->bus->pm)
733 callback = dev->bus->pm->runtime_resume;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200734 else
735 callback = NULL;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200736
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100737 if (!callback && dev->driver && dev->driver->pm)
738 callback = dev->driver->pm->runtime_resume;
739
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200740 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200741 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200742 __update_runtime_status(dev, RPM_SUSPENDED);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200743 pm_runtime_cancel_pending(dev);
744 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200745 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200746 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200747 if (parent)
748 atomic_inc(&parent->power.child_count);
749 }
750 wake_up_all(&dev->power.wait_queue);
751
Rafael J. Wysocki7f321c22012-08-15 21:31:45 +0200752 if (retval >= 0)
Alan Stern140a6c92010-09-25 23:35:07 +0200753 rpm_idle(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200754
755 out:
Alan Sternc7b61de2010-12-01 00:14:42 +0100756 if (parent && !dev->power.irq_safe) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200757 spin_unlock_irq(&dev->power.lock);
758
759 pm_runtime_put(parent);
760
761 spin_lock_irq(&dev->power.lock);
762 }
763
Ming Leic3dc2f12011-09-27 22:54:41 +0200764 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200765
766 return retval;
767}
768
769/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200770 * pm_runtime_work - Universal runtime PM work function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200771 * @work: Work structure used for scheduling the execution of this function.
772 *
773 * Use @work to get the device object the work is to be done for, determine what
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200774 * is to be done and execute the appropriate runtime PM function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200775 */
776static void pm_runtime_work(struct work_struct *work)
777{
778 struct device *dev = container_of(work, struct device, power.work);
779 enum rpm_request req;
780
781 spin_lock_irq(&dev->power.lock);
782
783 if (!dev->power.request_pending)
784 goto out;
785
786 req = dev->power.request;
787 dev->power.request = RPM_REQ_NONE;
788 dev->power.request_pending = false;
789
790 switch (req) {
791 case RPM_REQ_NONE:
792 break;
793 case RPM_REQ_IDLE:
Alan Stern140a6c92010-09-25 23:35:07 +0200794 rpm_idle(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200795 break;
796 case RPM_REQ_SUSPEND:
Alan Stern140a6c92010-09-25 23:35:07 +0200797 rpm_suspend(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200798 break;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200799 case RPM_REQ_AUTOSUSPEND:
800 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
801 break;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200802 case RPM_REQ_RESUME:
Alan Stern140a6c92010-09-25 23:35:07 +0200803 rpm_resume(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200804 break;
805 }
806
807 out:
808 spin_unlock_irq(&dev->power.lock);
809}
810
811/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200812 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
813 * @data: Device pointer passed by pm_schedule_suspend().
814 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200815 * Check if the time is right and queue a suspend request.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200816 */
817static void pm_suspend_timer_fn(unsigned long data)
818{
819 struct device *dev = (struct device *)data;
820 unsigned long flags;
821 unsigned long expires;
822
823 spin_lock_irqsave(&dev->power.lock, flags);
824
825 expires = dev->power.timer_expires;
826 /* If 'expire' is after 'jiffies' we've been called too early. */
827 if (expires > 0 && !time_after(expires, jiffies)) {
828 dev->power.timer_expires = 0;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200829 rpm_suspend(dev, dev->power.timer_autosuspends ?
830 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200831 }
832
833 spin_unlock_irqrestore(&dev->power.lock, flags);
834}
835
836/**
837 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
838 * @dev: Device to suspend.
839 * @delay: Time to wait before submitting a suspend request, in milliseconds.
840 */
841int pm_schedule_suspend(struct device *dev, unsigned int delay)
842{
843 unsigned long flags;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200844 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200845
846 spin_lock_irqsave(&dev->power.lock, flags);
847
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200848 if (!delay) {
Alan Stern140a6c92010-09-25 23:35:07 +0200849 retval = rpm_suspend(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200850 goto out;
851 }
852
Alan Stern1bfee5b2010-09-25 23:35:00 +0200853 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200854 if (retval)
855 goto out;
856
Alan Stern1bfee5b2010-09-25 23:35:00 +0200857 /* Other scheduled or pending requests need to be canceled. */
858 pm_runtime_cancel_pending(dev);
859
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200860 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200861 dev->power.timer_expires += !dev->power.timer_expires;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200862 dev->power.timer_autosuspends = 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200863 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
864
865 out:
866 spin_unlock_irqrestore(&dev->power.lock, flags);
867
868 return retval;
869}
870EXPORT_SYMBOL_GPL(pm_schedule_suspend);
871
872/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200873 * __pm_runtime_idle - Entry point for runtime idle operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200874 * @dev: Device to send idle notification for.
875 * @rpmflags: Flag bits.
876 *
877 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
878 * return immediately if it is larger than zero. Then carry out an idle
879 * notification, either synchronous or asynchronous.
880 *
Colin Cross311aab72011-08-08 23:39:36 +0200881 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
882 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200883 */
Alan Stern140a6c92010-09-25 23:35:07 +0200884int __pm_runtime_idle(struct device *dev, int rpmflags)
885{
886 unsigned long flags;
887 int retval;
888
Colin Cross311aab72011-08-08 23:39:36 +0200889 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
890
Alan Stern140a6c92010-09-25 23:35:07 +0200891 if (rpmflags & RPM_GET_PUT) {
892 if (!atomic_dec_and_test(&dev->power.usage_count))
893 return 0;
894 }
895
896 spin_lock_irqsave(&dev->power.lock, flags);
897 retval = rpm_idle(dev, rpmflags);
898 spin_unlock_irqrestore(&dev->power.lock, flags);
899
900 return retval;
901}
902EXPORT_SYMBOL_GPL(__pm_runtime_idle);
903
904/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200905 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200906 * @dev: Device to suspend.
907 * @rpmflags: Flag bits.
908 *
Alan Stern15bcb91d2010-09-25 23:35:21 +0200909 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
910 * return immediately if it is larger than zero. Then carry out a suspend,
911 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200912 *
Colin Cross311aab72011-08-08 23:39:36 +0200913 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
914 * or if pm_runtime_irq_safe() has been called.
Alan Stern140a6c92010-09-25 23:35:07 +0200915 */
916int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200917{
918 unsigned long flags;
919 int retval;
920
Colin Cross311aab72011-08-08 23:39:36 +0200921 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
922
Alan Stern15bcb91d2010-09-25 23:35:21 +0200923 if (rpmflags & RPM_GET_PUT) {
924 if (!atomic_dec_and_test(&dev->power.usage_count))
925 return 0;
926 }
927
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200928 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200929 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200930 spin_unlock_irqrestore(&dev->power.lock, flags);
931
932 return retval;
933}
Alan Stern140a6c92010-09-25 23:35:07 +0200934EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200935
936/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200937 * __pm_runtime_resume - Entry point for runtime resume operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200938 * @dev: Device to resume.
Alan Stern3f9af0512010-09-25 23:34:54 +0200939 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200940 *
Alan Stern140a6c92010-09-25 23:35:07 +0200941 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
942 * carry out a resume, either synchronous or asynchronous.
943 *
Colin Cross311aab72011-08-08 23:39:36 +0200944 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
945 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200946 */
Alan Stern140a6c92010-09-25 23:35:07 +0200947int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200948{
Alan Stern140a6c92010-09-25 23:35:07 +0200949 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100950 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200951
Colin Cross311aab72011-08-08 23:39:36 +0200952 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
953
Alan Stern140a6c92010-09-25 23:35:07 +0200954 if (rpmflags & RPM_GET_PUT)
955 atomic_inc(&dev->power.usage_count);
956
957 spin_lock_irqsave(&dev->power.lock, flags);
958 retval = rpm_resume(dev, rpmflags);
959 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200960
961 return retval;
962}
Alan Stern140a6c92010-09-25 23:35:07 +0200963EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200964
965/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200966 * __pm_runtime_set_status - Set runtime PM status of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200967 * @dev: Device to handle.
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200968 * @status: New runtime PM status of the device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200969 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200970 * If runtime PM of the device is disabled or its power.runtime_error field is
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200971 * different from zero, the status may be changed either to RPM_ACTIVE, or to
972 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
973 * However, if the device has a parent and the parent is not active, and the
974 * parent's power.ignore_children flag is unset, the device's status cannot be
975 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
976 *
977 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
978 * and the device parent's counter of unsuspended children is modified to
979 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
980 * notification request for the parent is submitted.
981 */
982int __pm_runtime_set_status(struct device *dev, unsigned int status)
983{
984 struct device *parent = dev->parent;
985 unsigned long flags;
986 bool notify_parent = false;
987 int error = 0;
988
989 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
990 return -EINVAL;
991
992 spin_lock_irqsave(&dev->power.lock, flags);
993
994 if (!dev->power.runtime_error && !dev->power.disable_depth) {
995 error = -EAGAIN;
996 goto out;
997 }
998
999 if (dev->power.runtime_status == status)
1000 goto out_set;
1001
1002 if (status == RPM_SUSPENDED) {
1003 /* It always is possible to set the status to 'suspended'. */
1004 if (parent) {
1005 atomic_add_unless(&parent->power.child_count, -1, 0);
1006 notify_parent = !parent->power.ignore_children;
1007 }
1008 goto out_set;
1009 }
1010
1011 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +01001012 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001013
1014 /*
1015 * It is invalid to put an active child under a parent that is
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001016 * not active, has runtime PM enabled and the
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001017 * 'power.ignore_children' flag unset.
1018 */
1019 if (!parent->power.disable_depth
1020 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +01001021 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001022 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +01001023 else if (dev->power.runtime_status == RPM_SUSPENDED)
1024 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001025
Alan Stern862f89b2009-11-25 01:06:37 +01001026 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001027
1028 if (error)
1029 goto out;
1030 }
1031
1032 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001033 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001034 dev->power.runtime_error = 0;
1035 out:
1036 spin_unlock_irqrestore(&dev->power.lock, flags);
1037
1038 if (notify_parent)
1039 pm_request_idle(parent);
1040
1041 return error;
1042}
1043EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1044
1045/**
1046 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1047 * @dev: Device to handle.
1048 *
1049 * Flush all pending requests for the device from pm_wq and wait for all
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001050 * runtime PM operations involving the device in progress to complete.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001051 *
1052 * Should be called under dev->power.lock with interrupts disabled.
1053 */
1054static void __pm_runtime_barrier(struct device *dev)
1055{
1056 pm_runtime_deactivate_timer(dev);
1057
1058 if (dev->power.request_pending) {
1059 dev->power.request = RPM_REQ_NONE;
1060 spin_unlock_irq(&dev->power.lock);
1061
1062 cancel_work_sync(&dev->power.work);
1063
1064 spin_lock_irq(&dev->power.lock);
1065 dev->power.request_pending = false;
1066 }
1067
1068 if (dev->power.runtime_status == RPM_SUSPENDING
1069 || dev->power.runtime_status == RPM_RESUMING
1070 || dev->power.idle_notification) {
1071 DEFINE_WAIT(wait);
1072
1073 /* Suspend, wake-up or idle notification in progress. */
1074 for (;;) {
1075 prepare_to_wait(&dev->power.wait_queue, &wait,
1076 TASK_UNINTERRUPTIBLE);
1077 if (dev->power.runtime_status != RPM_SUSPENDING
1078 && dev->power.runtime_status != RPM_RESUMING
1079 && !dev->power.idle_notification)
1080 break;
1081 spin_unlock_irq(&dev->power.lock);
1082
1083 schedule();
1084
1085 spin_lock_irq(&dev->power.lock);
1086 }
1087 finish_wait(&dev->power.wait_queue, &wait);
1088 }
1089}
1090
1091/**
1092 * pm_runtime_barrier - Flush pending requests and wait for completions.
1093 * @dev: Device to handle.
1094 *
1095 * Prevent the device from being suspended by incrementing its usage counter and
1096 * if there's a pending resume request for the device, wake the device up.
1097 * Next, make sure that all pending requests for the device have been flushed
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001098 * from pm_wq and wait for all runtime PM operations involving the device in
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001099 * progress to complete.
1100 *
1101 * Return value:
1102 * 1, if there was a resume request pending and the device had to be woken up,
1103 * 0, otherwise
1104 */
1105int pm_runtime_barrier(struct device *dev)
1106{
1107 int retval = 0;
1108
1109 pm_runtime_get_noresume(dev);
1110 spin_lock_irq(&dev->power.lock);
1111
1112 if (dev->power.request_pending
1113 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +02001114 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001115 retval = 1;
1116 }
1117
1118 __pm_runtime_barrier(dev);
1119
1120 spin_unlock_irq(&dev->power.lock);
1121 pm_runtime_put_noidle(dev);
1122
1123 return retval;
1124}
1125EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1126
1127/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001128 * __pm_runtime_disable - Disable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001129 * @dev: Device to handle.
1130 * @check_resume: If set, check if there's a resume request for the device.
1131 *
1132 * Increment power.disable_depth for the device and if was zero previously,
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001133 * cancel all pending runtime PM requests for the device and wait for all
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001134 * operations in progress to complete. The device can be either active or
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001135 * suspended after its runtime PM has been disabled.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001136 *
1137 * If @check_resume is set and there's a resume request pending when
1138 * __pm_runtime_disable() is called and power.disable_depth is zero, the
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001139 * function will wake up the device before disabling its runtime PM.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001140 */
1141void __pm_runtime_disable(struct device *dev, bool check_resume)
1142{
1143 spin_lock_irq(&dev->power.lock);
1144
1145 if (dev->power.disable_depth > 0) {
1146 dev->power.disable_depth++;
1147 goto out;
1148 }
1149
1150 /*
1151 * Wake up the device if there's a resume request pending, because that
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001152 * means there probably is some I/O to process and disabling runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001153 * shouldn't prevent the device from processing the I/O.
1154 */
1155 if (check_resume && dev->power.request_pending
1156 && dev->power.request == RPM_REQ_RESUME) {
1157 /*
1158 * Prevent suspends and idle notifications from being carried
1159 * out after we have woken up the device.
1160 */
1161 pm_runtime_get_noresume(dev);
1162
Alan Stern140a6c92010-09-25 23:35:07 +02001163 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001164
1165 pm_runtime_put_noidle(dev);
1166 }
1167
1168 if (!dev->power.disable_depth++)
1169 __pm_runtime_barrier(dev);
1170
1171 out:
1172 spin_unlock_irq(&dev->power.lock);
1173}
1174EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1175
1176/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001177 * pm_runtime_enable - Enable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001178 * @dev: Device to handle.
1179 */
1180void pm_runtime_enable(struct device *dev)
1181{
1182 unsigned long flags;
1183
1184 spin_lock_irqsave(&dev->power.lock, flags);
1185
1186 if (dev->power.disable_depth > 0)
1187 dev->power.disable_depth--;
1188 else
1189 dev_warn(dev, "Unbalanced %s!\n", __func__);
1190
1191 spin_unlock_irqrestore(&dev->power.lock, flags);
1192}
1193EXPORT_SYMBOL_GPL(pm_runtime_enable);
1194
1195/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001196 * pm_runtime_forbid - Block runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001197 * @dev: Device to handle.
1198 *
1199 * Increase the device's usage count and clear its power.runtime_auto flag,
1200 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1201 * for it.
1202 */
1203void pm_runtime_forbid(struct device *dev)
1204{
1205 spin_lock_irq(&dev->power.lock);
1206 if (!dev->power.runtime_auto)
1207 goto out;
1208
1209 dev->power.runtime_auto = false;
1210 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001211 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001212
1213 out:
1214 spin_unlock_irq(&dev->power.lock);
1215}
1216EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1217
1218/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001219 * pm_runtime_allow - Unblock runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001220 * @dev: Device to handle.
1221 *
1222 * Decrease the device's usage count and set its power.runtime_auto flag.
1223 */
1224void pm_runtime_allow(struct device *dev)
1225{
1226 spin_lock_irq(&dev->power.lock);
1227 if (dev->power.runtime_auto)
1228 goto out;
1229
1230 dev->power.runtime_auto = true;
1231 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb91d2010-09-25 23:35:21 +02001232 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001233
1234 out:
1235 spin_unlock_irq(&dev->power.lock);
1236}
1237EXPORT_SYMBOL_GPL(pm_runtime_allow);
1238
1239/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001240 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
Alan Stern7490e442010-09-25 23:35:15 +02001241 * @dev: Device to handle.
1242 *
1243 * Set the power.no_callbacks flag, which tells the PM core that this
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001244 * device is power-managed through its parent and has no runtime PM
1245 * callbacks of its own. The runtime sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001246 */
1247void pm_runtime_no_callbacks(struct device *dev)
1248{
1249 spin_lock_irq(&dev->power.lock);
1250 dev->power.no_callbacks = 1;
1251 spin_unlock_irq(&dev->power.lock);
1252 if (device_is_registered(dev))
1253 rpm_sysfs_remove(dev);
1254}
1255EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1256
1257/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001258 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1259 * @dev: Device to handle
1260 *
1261 * Set the power.irq_safe flag, which tells the PM core that the
1262 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1263 * always be invoked with the spinlock held and interrupts disabled. It also
1264 * causes the parent's usage counter to be permanently incremented, preventing
1265 * the parent from runtime suspending -- otherwise an irq-safe child might have
1266 * to wait for a non-irq-safe parent.
1267 */
1268void pm_runtime_irq_safe(struct device *dev)
1269{
1270 if (dev->parent)
1271 pm_runtime_get_sync(dev->parent);
1272 spin_lock_irq(&dev->power.lock);
1273 dev->power.irq_safe = 1;
1274 spin_unlock_irq(&dev->power.lock);
1275}
1276EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1277
1278/**
Alan Stern15bcb91d2010-09-25 23:35:21 +02001279 * update_autosuspend - Handle a change to a device's autosuspend settings.
1280 * @dev: Device to handle.
1281 * @old_delay: The former autosuspend_delay value.
1282 * @old_use: The former use_autosuspend value.
1283 *
1284 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1285 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1286 *
1287 * This function must be called under dev->power.lock with interrupts disabled.
1288 */
1289static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1290{
1291 int delay = dev->power.autosuspend_delay;
1292
1293 /* Should runtime suspend be prevented now? */
1294 if (dev->power.use_autosuspend && delay < 0) {
1295
1296 /* If it used to be allowed then prevent it. */
1297 if (!old_use || old_delay >= 0) {
1298 atomic_inc(&dev->power.usage_count);
1299 rpm_resume(dev, 0);
1300 }
1301 }
1302
1303 /* Runtime suspend should be allowed now. */
1304 else {
1305
1306 /* If it used to be prevented then allow it. */
1307 if (old_use && old_delay < 0)
1308 atomic_dec(&dev->power.usage_count);
1309
1310 /* Maybe we can autosuspend now. */
1311 rpm_idle(dev, RPM_AUTO);
1312 }
1313}
1314
1315/**
1316 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1317 * @dev: Device to handle.
1318 * @delay: Value of the new delay in milliseconds.
1319 *
1320 * Set the device's power.autosuspend_delay value. If it changes to negative
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001321 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1322 * changes the other way, allow runtime suspends.
Alan Stern15bcb91d2010-09-25 23:35:21 +02001323 */
1324void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1325{
1326 int old_delay, old_use;
1327
1328 spin_lock_irq(&dev->power.lock);
1329 old_delay = dev->power.autosuspend_delay;
1330 old_use = dev->power.use_autosuspend;
1331 dev->power.autosuspend_delay = delay;
1332 update_autosuspend(dev, old_delay, old_use);
1333 spin_unlock_irq(&dev->power.lock);
1334}
1335EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1336
1337/**
1338 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1339 * @dev: Device to handle.
1340 * @use: New value for use_autosuspend.
1341 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001342 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
Alan Stern15bcb91d2010-09-25 23:35:21 +02001343 * suspends as needed.
1344 */
1345void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1346{
1347 int old_delay, old_use;
1348
1349 spin_lock_irq(&dev->power.lock);
1350 old_delay = dev->power.autosuspend_delay;
1351 old_use = dev->power.use_autosuspend;
1352 dev->power.use_autosuspend = use;
1353 update_autosuspend(dev, old_delay, old_use);
1354 spin_unlock_irq(&dev->power.lock);
1355}
1356EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1357
1358/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001359 * pm_runtime_init - Initialize runtime PM fields in given device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001360 * @dev: Device object to initialize.
1361 */
1362void pm_runtime_init(struct device *dev)
1363{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001364 dev->power.runtime_status = RPM_SUSPENDED;
1365 dev->power.idle_notification = false;
1366
1367 dev->power.disable_depth = 1;
1368 atomic_set(&dev->power.usage_count, 0);
1369
1370 dev->power.runtime_error = 0;
1371
1372 atomic_set(&dev->power.child_count, 0);
1373 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001374 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001375
1376 dev->power.request_pending = false;
1377 dev->power.request = RPM_REQ_NONE;
1378 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001379 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001380 INIT_WORK(&dev->power.work, pm_runtime_work);
1381
1382 dev->power.timer_expires = 0;
1383 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1384 (unsigned long)dev);
1385
1386 init_waitqueue_head(&dev->power.wait_queue);
1387}
1388
1389/**
1390 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1391 * @dev: Device object being removed from device hierarchy.
1392 */
1393void pm_runtime_remove(struct device *dev)
1394{
1395 __pm_runtime_disable(dev, false);
1396
1397 /* Change the status back to 'suspended' to match the initial status. */
1398 if (dev->power.runtime_status == RPM_ACTIVE)
1399 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001400 if (dev->power.irq_safe && dev->parent)
Ulf Hanssondb28dfa2013-04-12 09:41:30 +00001401 pm_runtime_put(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001402}