blob: 4ffe4a2add76631a05e9221819307abe2b4ce08f [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
Andrzej Hajdadbcd2d72014-10-17 12:58:02 +020016typedef int (*pm_callback_t)(struct device *);
Ulf Hansson5f59df72014-03-01 11:56:04 +010017
Andrzej Hajdadbcd2d72014-10-17 12:58:02 +020018static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset)
Ulf Hansson5f59df72014-03-01 11:56:04 +010019{
Andrzej Hajdadbcd2d72014-10-17 12:58:02 +020020 pm_callback_t cb;
21 const struct dev_pm_ops *ops;
22
23 if (dev->pm_domain)
24 ops = &dev->pm_domain->ops;
25 else if (dev->type && dev->type->pm)
26 ops = dev->type->pm;
27 else if (dev->class && dev->class->pm)
28 ops = dev->class->pm;
29 else if (dev->bus && dev->bus->pm)
30 ops = dev->bus->pm;
31 else
32 ops = NULL;
33
34 if (ops)
35 cb = *(pm_callback_t *)((void *)ops + cb_offset);
36 else
37 cb = NULL;
38
39 if (!cb && dev->driver && dev->driver->pm)
40 cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset);
41
42 return cb;
Ulf Hansson5f59df72014-03-01 11:56:04 +010043}
44
Andrzej Hajdadbcd2d72014-10-17 12:58:02 +020045#define RPM_GET_CALLBACK(dev, callback) \
46 __rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback))
Ulf Hansson5f59df72014-03-01 11:56:04 +010047
Alan Stern140a6c92010-09-25 23:35:07 +020048static int rpm_resume(struct device *dev, int rpmflags);
Alan Stern7490e442010-09-25 23:35:15 +020049static int rpm_suspend(struct device *dev, int rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020050
51/**
Alan Stern47693732010-09-25 23:34:46 +020052 * update_pm_runtime_accounting - Update the time accounting of power states
53 * @dev: Device to update the accounting for
54 *
55 * In order to be able to have time accounting of the various power states
56 * (as used by programs such as PowerTOP to show the effectiveness of runtime
57 * PM), we need to track the time spent in each state.
58 * update_pm_runtime_accounting must be called each time before the
59 * runtime_status field is updated, to account the time in the old state
60 * correctly.
61 */
62void update_pm_runtime_accounting(struct device *dev)
63{
64 unsigned long now = jiffies;
venu byravarasudef0c0a32011-11-03 10:12:14 +010065 unsigned long delta;
Alan Stern47693732010-09-25 23:34:46 +020066
67 delta = now - dev->power.accounting_timestamp;
68
Alan Stern47693732010-09-25 23:34:46 +020069 dev->power.accounting_timestamp = now;
70
71 if (dev->power.disable_depth > 0)
72 return;
73
74 if (dev->power.runtime_status == RPM_SUSPENDED)
75 dev->power.suspended_jiffies += delta;
76 else
77 dev->power.active_jiffies += delta;
78}
79
80static void __update_runtime_status(struct device *dev, enum rpm_status status)
81{
82 update_pm_runtime_accounting(dev);
83 dev->power.runtime_status = status;
84}
85
86/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +020087 * pm_runtime_deactivate_timer - Deactivate given device's suspend timer.
88 * @dev: Device to handle.
89 */
90static void pm_runtime_deactivate_timer(struct device *dev)
91{
92 if (dev->power.timer_expires > 0) {
93 del_timer(&dev->power.suspend_timer);
94 dev->power.timer_expires = 0;
95 }
96}
97
98/**
99 * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests.
100 * @dev: Device to handle.
101 */
102static void pm_runtime_cancel_pending(struct device *dev)
103{
104 pm_runtime_deactivate_timer(dev);
105 /*
106 * In case there's a request pending, make sure its work function will
107 * return without doing anything.
108 */
109 dev->power.request = RPM_REQ_NONE;
110}
111
Alan Stern15bcb91d2010-09-25 23:35:21 +0200112/*
113 * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time.
114 * @dev: Device to handle.
115 *
116 * Compute the autosuspend-delay expiration time based on the device's
117 * power.last_busy time. If the delay has already expired or is disabled
118 * (negative) or the power.use_autosuspend flag isn't set, return 0.
119 * Otherwise return the expiration time in jiffies (adjusted to be nonzero).
120 *
121 * This function may be called either with or without dev->power.lock held.
122 * Either way it can be racy, since power.last_busy may be updated at any time.
123 */
124unsigned long pm_runtime_autosuspend_expiration(struct device *dev)
125{
126 int autosuspend_delay;
127 long elapsed;
128 unsigned long last_busy;
129 unsigned long expires = 0;
130
131 if (!dev->power.use_autosuspend)
132 goto out;
133
134 autosuspend_delay = ACCESS_ONCE(dev->power.autosuspend_delay);
135 if (autosuspend_delay < 0)
136 goto out;
137
138 last_busy = ACCESS_ONCE(dev->power.last_busy);
139 elapsed = jiffies - last_busy;
140 if (elapsed < 0)
141 goto out; /* jiffies has wrapped around. */
142
143 /*
144 * If the autosuspend_delay is >= 1 second, align the timer by rounding
145 * up to the nearest second.
146 */
147 expires = last_busy + msecs_to_jiffies(autosuspend_delay);
148 if (autosuspend_delay >= 1000)
149 expires = round_jiffies(expires);
150 expires += !expires;
151 if (elapsed >= expires - last_busy)
152 expires = 0; /* Already expired. */
153
154 out:
155 return expires;
156}
157EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration);
158
Ming Leie8234072013-02-22 16:34:11 -0800159static int dev_memalloc_noio(struct device *dev, void *data)
160{
161 return dev->power.memalloc_noio;
162}
163
164/*
165 * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag.
166 * @dev: Device to handle.
167 * @enable: True for setting the flag and False for clearing the flag.
168 *
169 * Set the flag for all devices in the path from the device to the
170 * root device in the device tree if @enable is true, otherwise clear
171 * the flag for devices in the path whose siblings don't set the flag.
172 *
173 * The function should only be called by block device, or network
174 * device driver for solving the deadlock problem during runtime
175 * resume/suspend:
176 *
177 * If memory allocation with GFP_KERNEL is called inside runtime
178 * resume/suspend callback of any one of its ancestors(or the
179 * block device itself), the deadlock may be triggered inside the
180 * memory allocation since it might not complete until the block
181 * device becomes active and the involed page I/O finishes. The
182 * situation is pointed out first by Alan Stern. Network device
183 * are involved in iSCSI kind of situation.
184 *
185 * The lock of dev_hotplug_mutex is held in the function for handling
186 * hotplug race because pm_runtime_set_memalloc_noio() may be called
187 * in async probe().
188 *
189 * The function should be called between device_add() and device_del()
190 * on the affected device(block/network device).
191 */
192void pm_runtime_set_memalloc_noio(struct device *dev, bool enable)
193{
194 static DEFINE_MUTEX(dev_hotplug_mutex);
195
196 mutex_lock(&dev_hotplug_mutex);
197 for (;;) {
198 bool enabled;
199
200 /* hold power lock since bitfield is not SMP-safe. */
201 spin_lock_irq(&dev->power.lock);
202 enabled = dev->power.memalloc_noio;
203 dev->power.memalloc_noio = enable;
204 spin_unlock_irq(&dev->power.lock);
205
206 /*
207 * not need to enable ancestors any more if the device
208 * has been enabled.
209 */
210 if (enabled && enable)
211 break;
212
213 dev = dev->parent;
214
215 /*
216 * clear flag of the parent device only if all the
217 * children don't set the flag because ancestor's
218 * flag was set by any one of the descendants.
219 */
220 if (!dev || (!enable &&
221 device_for_each_child(dev, NULL,
222 dev_memalloc_noio)))
223 break;
224 }
225 mutex_unlock(&dev_hotplug_mutex);
226}
227EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio);
228
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200229/**
Alan Stern1bfee5b2010-09-25 23:35:00 +0200230 * rpm_check_suspend_allowed - Test whether a device may be suspended.
231 * @dev: Device to test.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200232 */
Alan Stern1bfee5b2010-09-25 23:35:00 +0200233static int rpm_check_suspend_allowed(struct device *dev)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200234{
235 int retval = 0;
236
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200237 if (dev->power.runtime_error)
238 retval = -EINVAL;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200239 else if (dev->power.disable_depth > 0)
240 retval = -EACCES;
241 else if (atomic_read(&dev->power.usage_count) > 0)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200242 retval = -EAGAIN;
243 else if (!pm_children_suspended(dev))
244 retval = -EBUSY;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200245
246 /* Pending resume requests take precedence over suspends. */
247 else if ((dev->power.deferred_resume
Kevin Winchester78ca7c32010-10-29 15:29:55 +0200248 && dev->power.runtime_status == RPM_SUSPENDING)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200249 || (dev->power.request_pending
250 && dev->power.request == RPM_REQ_RESUME))
251 retval = -EAGAIN;
Rafael J. Wysocki55d7ec42012-08-15 21:32:04 +0200252 else if (__dev_pm_qos_read_value(dev) < 0)
253 retval = -EPERM;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200254 else if (dev->power.runtime_status == RPM_SUSPENDED)
255 retval = 1;
256
257 return retval;
258}
259
Alan Stern1bfee5b2010-09-25 23:35:00 +0200260/**
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200261 * __rpm_callback - Run a given runtime PM callback for a given device.
262 * @cb: Runtime PM callback to run.
263 * @dev: Device to run the callback for.
264 */
265static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
266 __releases(&dev->power.lock) __acquires(&dev->power.lock)
267{
268 int retval;
269
270 if (dev->power.irq_safe)
271 spin_unlock(&dev->power.lock);
272 else
273 spin_unlock_irq(&dev->power.lock);
274
275 retval = cb(dev);
276
277 if (dev->power.irq_safe)
278 spin_lock(&dev->power.lock);
279 else
280 spin_lock_irq(&dev->power.lock);
281
282 return retval;
283}
284
285/**
Alan Stern140a6c92010-09-25 23:35:07 +0200286 * rpm_idle - Notify device bus type if the device can be suspended.
Alan Stern1bfee5b2010-09-25 23:35:00 +0200287 * @dev: Device to notify the bus type about.
288 * @rpmflags: Flag bits.
289 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200290 * Check if the device's runtime PM status allows it to be suspended. If
Alan Stern1bfee5b2010-09-25 23:35:00 +0200291 * another idle notification has been started earlier, return immediately. If
292 * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
Ulf Hanssond66e6db2013-10-15 22:25:08 +0200293 * run the ->runtime_idle() callback directly. If the ->runtime_idle callback
294 * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag.
Alan Stern1bfee5b2010-09-25 23:35:00 +0200295 *
296 * This function must be called under dev->power.lock with interrupts disabled.
297 */
Alan Stern140a6c92010-09-25 23:35:07 +0200298static int rpm_idle(struct device *dev, int rpmflags)
Alan Stern1bfee5b2010-09-25 23:35:00 +0200299{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200300 int (*callback)(struct device *);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200301 int retval;
302
Ming Leic3dc2f12011-09-27 22:54:41 +0200303 trace_rpm_idle(dev, rpmflags);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200304 retval = rpm_check_suspend_allowed(dev);
305 if (retval < 0)
306 ; /* Conditions are wrong. */
307
308 /* Idle notifications are allowed only in the RPM_ACTIVE state. */
309 else if (dev->power.runtime_status != RPM_ACTIVE)
310 retval = -EAGAIN;
311
312 /*
313 * Any pending request other than an idle notification takes
314 * precedence over us, except that the timer may be running.
315 */
316 else if (dev->power.request_pending &&
317 dev->power.request > RPM_REQ_IDLE)
318 retval = -EAGAIN;
319
320 /* Act as though RPM_NOWAIT is always set. */
321 else if (dev->power.idle_notification)
322 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200323 if (retval)
324 goto out;
325
Alan Stern1bfee5b2010-09-25 23:35:00 +0200326 /* Pending requests need to be canceled. */
327 dev->power.request = RPM_REQ_NONE;
328
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200329 if (dev->power.no_callbacks)
Alan Stern7490e442010-09-25 23:35:15 +0200330 goto out;
Alan Stern7490e442010-09-25 23:35:15 +0200331
Alan Stern1bfee5b2010-09-25 23:35:00 +0200332 /* Carry out an asynchronous or a synchronous idle notification. */
333 if (rpmflags & RPM_ASYNC) {
334 dev->power.request = RPM_REQ_IDLE;
335 if (!dev->power.request_pending) {
336 dev->power.request_pending = true;
337 queue_work(pm_wq, &dev->power.work);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200338 }
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200339 trace_rpm_return_int(dev, _THIS_IP_, 0);
340 return 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200341 }
342
343 dev->power.idle_notification = true;
344
Andrzej Hajdadbcd2d72014-10-17 12:58:02 +0200345 callback = RPM_GET_CALLBACK(dev, runtime_idle);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100346
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200347 if (callback)
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +0200348 retval = __rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200349
350 dev->power.idle_notification = false;
351 wake_up_all(&dev->power.wait_queue);
352
353 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200354 trace_rpm_return_int(dev, _THIS_IP_, retval);
Ulf Hanssond66e6db2013-10-15 22:25:08 +0200355 return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200356}
357
358/**
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200359 * rpm_callback - Run a given runtime PM callback for a given device.
360 * @cb: Runtime PM callback to run.
361 * @dev: Device to run the callback for.
362 */
363static int rpm_callback(int (*cb)(struct device *), struct device *dev)
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200364{
365 int retval;
366
367 if (!cb)
368 return -ENOSYS;
369
Ming Leidb881752013-02-22 16:34:19 -0800370 if (dev->power.memalloc_noio) {
371 unsigned int noio_flag;
372
373 /*
374 * Deadlock might be caused if memory allocation with
375 * GFP_KERNEL happens inside runtime_suspend and
376 * runtime_resume callbacks of one block device's
377 * ancestor or the block device itself. Network
378 * device might be thought as part of iSCSI block
379 * device, so network device and its ancestor should
380 * be marked as memalloc_noio too.
381 */
382 noio_flag = memalloc_noio_save();
383 retval = __rpm_callback(cb, dev);
384 memalloc_noio_restore(noio_flag);
385 } else {
386 retval = __rpm_callback(cb, dev);
387 }
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200388
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200389 dev->power.runtime_error = retval;
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200390 return retval != -EACCES ? retval : -EIO;
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200391}
392
393/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200394 * rpm_suspend - Carry out runtime suspend of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200395 * @dev: Device to suspend.
Alan Stern3f9af052010-09-25 23:34:54 +0200396 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200397 *
Ming Lei47d8f0b2011-10-12 11:53:32 +0800398 * Check if the device's runtime PM status allows it to be suspended.
399 * Cancel a pending idle notification, autosuspend or suspend. If
400 * another suspend has been started earlier, either return immediately
401 * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
402 * flags. If the RPM_ASYNC flag is set then queue a suspend request;
Ming Lei857b36c2011-10-12 22:59:33 +0200403 * otherwise run the ->runtime_suspend() callback directly. When
404 * ->runtime_suspend succeeded, if a deferred resume was requested while
405 * the callback was running then carry it out, otherwise send an idle
406 * notification for its parent (if the suspend succeeded and both
407 * ignore_children of parent->power and irq_safe of dev->power are not set).
Alan Stern886486b2011-11-03 23:39:18 +0100408 * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
409 * flag is set and the next autosuspend-delay expiration time is in the
410 * future, schedule another autosuspend attempt.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200411 *
412 * This function must be called under dev->power.lock with interrupts disabled.
413 */
Alan Stern140a6c92010-09-25 23:35:07 +0200414static int rpm_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200415 __releases(&dev->power.lock) __acquires(&dev->power.lock)
416{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200417 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200418 struct device *parent = NULL;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200419 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200420
Ming Leic3dc2f12011-09-27 22:54:41 +0200421 trace_rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200422
423 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200424 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200425
Alan Stern1bfee5b2010-09-25 23:35:00 +0200426 if (retval < 0)
427 ; /* Conditions are wrong. */
428
429 /* Synchronous suspends are not allowed in the RPM_RESUMING state. */
430 else if (dev->power.runtime_status == RPM_RESUMING &&
431 !(rpmflags & RPM_ASYNC))
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200432 retval = -EAGAIN;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200433 if (retval)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200434 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200435
Alan Stern15bcb91d2010-09-25 23:35:21 +0200436 /* If the autosuspend_delay time hasn't expired yet, reschedule. */
437 if ((rpmflags & RPM_AUTO)
438 && dev->power.runtime_status != RPM_SUSPENDING) {
439 unsigned long expires = pm_runtime_autosuspend_expiration(dev);
440
441 if (expires != 0) {
442 /* Pending requests need to be canceled. */
443 dev->power.request = RPM_REQ_NONE;
444
445 /*
446 * Optimization: If the timer is already running and is
447 * set to expire at or before the autosuspend delay,
448 * avoid the overhead of resetting it. Just let it
449 * expire; pm_suspend_timer_fn() will take care of the
450 * rest.
451 */
452 if (!(dev->power.timer_expires && time_before_eq(
453 dev->power.timer_expires, expires))) {
454 dev->power.timer_expires = expires;
455 mod_timer(&dev->power.suspend_timer, expires);
456 }
457 dev->power.timer_autosuspends = 1;
458 goto out;
459 }
460 }
461
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200462 /* Other scheduled or pending requests need to be canceled. */
463 pm_runtime_cancel_pending(dev);
464
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200465 if (dev->power.runtime_status == RPM_SUSPENDING) {
466 DEFINE_WAIT(wait);
467
Alan Stern1bfee5b2010-09-25 23:35:00 +0200468 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200469 retval = -EINPROGRESS;
470 goto out;
471 }
472
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200473 if (dev->power.irq_safe) {
474 spin_unlock(&dev->power.lock);
475
476 cpu_relax();
477
478 spin_lock(&dev->power.lock);
479 goto repeat;
480 }
481
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200482 /* Wait for the other suspend running in parallel with us. */
483 for (;;) {
484 prepare_to_wait(&dev->power.wait_queue, &wait,
485 TASK_UNINTERRUPTIBLE);
486 if (dev->power.runtime_status != RPM_SUSPENDING)
487 break;
488
489 spin_unlock_irq(&dev->power.lock);
490
491 schedule();
492
493 spin_lock_irq(&dev->power.lock);
494 }
495 finish_wait(&dev->power.wait_queue, &wait);
496 goto repeat;
497 }
498
Alan Stern7490e442010-09-25 23:35:15 +0200499 if (dev->power.no_callbacks)
500 goto no_callback; /* Assume success. */
501
Alan Stern1bfee5b2010-09-25 23:35:00 +0200502 /* Carry out an asynchronous or a synchronous suspend. */
503 if (rpmflags & RPM_ASYNC) {
Alan Stern15bcb91d2010-09-25 23:35:21 +0200504 dev->power.request = (rpmflags & RPM_AUTO) ?
505 RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200506 if (!dev->power.request_pending) {
507 dev->power.request_pending = true;
508 queue_work(pm_wq, &dev->power.work);
509 }
510 goto out;
511 }
512
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200513 __update_runtime_status(dev, RPM_SUSPENDING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200514
Andrzej Hajdadbcd2d72014-10-17 12:58:02 +0200515 callback = RPM_GET_CALLBACK(dev, runtime_suspend);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100516
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200517 retval = rpm_callback(callback, dev);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100518 if (retval)
519 goto fail;
Alan Stern886486b2011-11-03 23:39:18 +0100520
Alan Stern7490e442010-09-25 23:35:15 +0200521 no_callback:
Ming Lei857b36c2011-10-12 22:59:33 +0200522 __update_runtime_status(dev, RPM_SUSPENDED);
523 pm_runtime_deactivate_timer(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200524
Ming Lei857b36c2011-10-12 22:59:33 +0200525 if (dev->parent) {
526 parent = dev->parent;
527 atomic_add_unless(&parent->power.child_count, -1, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200528 }
529 wake_up_all(&dev->power.wait_queue);
530
531 if (dev->power.deferred_resume) {
Rafael J. Wysocki58a34de2012-08-15 21:31:55 +0200532 dev->power.deferred_resume = false;
Alan Stern140a6c92010-09-25 23:35:07 +0200533 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200534 retval = -EAGAIN;
535 goto out;
536 }
537
Alan Sternc3810c82011-01-25 20:50:07 +0100538 /* Maybe the parent is now able to suspend. */
Alan Sternc7b61de2010-12-01 00:14:42 +0100539 if (parent && !parent->power.ignore_children && !dev->power.irq_safe) {
Alan Sternc3810c82011-01-25 20:50:07 +0100540 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200541
Alan Sternc3810c82011-01-25 20:50:07 +0100542 spin_lock(&parent->power.lock);
543 rpm_idle(parent, RPM_ASYNC);
544 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200545
Alan Sternc3810c82011-01-25 20:50:07 +0100546 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200547 }
548
549 out:
Ming Leic3dc2f12011-09-27 22:54:41 +0200550 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200551
552 return retval;
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100553
554 fail:
555 __update_runtime_status(dev, RPM_ACTIVE);
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100556 dev->power.deferred_resume = false;
Alan Sternf2791d72012-03-26 22:46:52 +0200557 wake_up_all(&dev->power.wait_queue);
558
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100559 if (retval == -EAGAIN || retval == -EBUSY) {
560 dev->power.runtime_error = 0;
561
562 /*
563 * If the callback routine failed an autosuspend, and
564 * if the last_busy time has been updated so that there
565 * is a new autosuspend expiration time, automatically
566 * reschedule another autosuspend.
567 */
568 if ((rpmflags & RPM_AUTO) &&
569 pm_runtime_autosuspend_expiration(dev) != 0)
570 goto repeat;
571 } else {
572 pm_runtime_cancel_pending(dev);
573 }
Rafael J. Wysocki00dc9ad2011-12-01 00:01:31 +0100574 goto out;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200575}
576
577/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200578 * rpm_resume - Carry out runtime resume of given device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200579 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200580 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200581 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200582 * Check if the device's runtime PM status allows it to be resumed. Cancel
Alan Stern1bfee5b2010-09-25 23:35:00 +0200583 * any scheduled or pending requests. If another resume has been started
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300584 * earlier, either return immediately or wait for it to finish, depending on the
Alan Stern1bfee5b2010-09-25 23:35:00 +0200585 * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
586 * parallel with this function, either tell the other process to resume after
587 * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC
588 * flag is set then queue a resume request; otherwise run the
589 * ->runtime_resume() callback directly. Queue an idle notification for the
590 * device if the resume succeeded.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200591 *
592 * This function must be called under dev->power.lock with interrupts disabled.
593 */
Alan Stern140a6c92010-09-25 23:35:07 +0200594static int rpm_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200595 __releases(&dev->power.lock) __acquires(&dev->power.lock)
596{
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200597 int (*callback)(struct device *);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200598 struct device *parent = NULL;
599 int retval = 0;
600
Ming Leic3dc2f12011-09-27 22:54:41 +0200601 trace_rpm_resume(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200602
603 repeat:
Alan Stern1bfee5b2010-09-25 23:35:00 +0200604 if (dev->power.runtime_error)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200605 retval = -EINVAL;
Kevin Hilman6f3c77b2012-09-21 22:47:34 +0000606 else if (dev->power.disable_depth == 1 && dev->power.is_suspended
607 && dev->power.runtime_status == RPM_ACTIVE)
608 retval = 1;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200609 else if (dev->power.disable_depth > 0)
Rafael J. Wysocki632e2702011-07-01 22:29:15 +0200610 retval = -EACCES;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200611 if (retval)
612 goto out;
613
Alan Stern15bcb91d2010-09-25 23:35:21 +0200614 /*
615 * Other scheduled or pending requests need to be canceled. Small
616 * optimization: If an autosuspend timer is running, leave it running
617 * rather than cancelling it now only to restart it again in the near
618 * future.
619 */
620 dev->power.request = RPM_REQ_NONE;
621 if (!dev->power.timer_autosuspends)
622 pm_runtime_deactivate_timer(dev);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200623
624 if (dev->power.runtime_status == RPM_ACTIVE) {
625 retval = 1;
626 goto out;
627 }
628
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200629 if (dev->power.runtime_status == RPM_RESUMING
630 || dev->power.runtime_status == RPM_SUSPENDING) {
631 DEFINE_WAIT(wait);
632
Alan Stern1bfee5b2010-09-25 23:35:00 +0200633 if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200634 if (dev->power.runtime_status == RPM_SUSPENDING)
635 dev->power.deferred_resume = true;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200636 else
637 retval = -EINPROGRESS;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200638 goto out;
639 }
640
Rafael J. Wysockiad3c36a2011-09-27 21:54:52 +0200641 if (dev->power.irq_safe) {
642 spin_unlock(&dev->power.lock);
643
644 cpu_relax();
645
646 spin_lock(&dev->power.lock);
647 goto repeat;
648 }
649
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200650 /* Wait for the operation carried out in parallel with us. */
651 for (;;) {
652 prepare_to_wait(&dev->power.wait_queue, &wait,
653 TASK_UNINTERRUPTIBLE);
654 if (dev->power.runtime_status != RPM_RESUMING
655 && dev->power.runtime_status != RPM_SUSPENDING)
656 break;
657
658 spin_unlock_irq(&dev->power.lock);
659
660 schedule();
661
662 spin_lock_irq(&dev->power.lock);
663 }
664 finish_wait(&dev->power.wait_queue, &wait);
665 goto repeat;
666 }
667
Alan Stern7490e442010-09-25 23:35:15 +0200668 /*
669 * See if we can skip waking up the parent. This is safe only if
670 * power.no_callbacks is set, because otherwise we don't know whether
671 * the resume will actually succeed.
672 */
673 if (dev->power.no_callbacks && !parent && dev->parent) {
Ming Leid63be5f2010-10-22 23:48:14 +0200674 spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING);
Alan Stern7490e442010-09-25 23:35:15 +0200675 if (dev->parent->power.disable_depth > 0
676 || dev->parent->power.ignore_children
677 || dev->parent->power.runtime_status == RPM_ACTIVE) {
678 atomic_inc(&dev->parent->power.child_count);
679 spin_unlock(&dev->parent->power.lock);
Rafael J. Wysocki7f321c22012-08-15 21:31:45 +0200680 retval = 1;
Alan Stern7490e442010-09-25 23:35:15 +0200681 goto no_callback; /* Assume success. */
682 }
683 spin_unlock(&dev->parent->power.lock);
684 }
685
Alan Stern1bfee5b2010-09-25 23:35:00 +0200686 /* Carry out an asynchronous or a synchronous resume. */
687 if (rpmflags & RPM_ASYNC) {
688 dev->power.request = RPM_REQ_RESUME;
689 if (!dev->power.request_pending) {
690 dev->power.request_pending = true;
691 queue_work(pm_wq, &dev->power.work);
692 }
693 retval = 0;
694 goto out;
695 }
696
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200697 if (!parent && dev->parent) {
698 /*
Alan Sternc7b61de2010-12-01 00:14:42 +0100699 * Increment the parent's usage counter and resume it if
700 * necessary. Not needed if dev is irq-safe; then the
701 * parent is permanently resumed.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200702 */
703 parent = dev->parent;
Alan Sternc7b61de2010-12-01 00:14:42 +0100704 if (dev->power.irq_safe)
705 goto skip_parent;
Alan Stern862f89b2009-11-25 01:06:37 +0100706 spin_unlock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200707
708 pm_runtime_get_noresume(parent);
709
Alan Stern862f89b2009-11-25 01:06:37 +0100710 spin_lock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200711 /*
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200712 * We can resume if the parent's runtime PM is disabled or it
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200713 * is set to ignore children.
714 */
715 if (!parent->power.disable_depth
716 && !parent->power.ignore_children) {
Alan Stern140a6c92010-09-25 23:35:07 +0200717 rpm_resume(parent, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200718 if (parent->power.runtime_status != RPM_ACTIVE)
719 retval = -EBUSY;
720 }
Alan Stern862f89b2009-11-25 01:06:37 +0100721 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200722
Alan Stern862f89b2009-11-25 01:06:37 +0100723 spin_lock(&dev->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200724 if (retval)
725 goto out;
726 goto repeat;
727 }
Alan Sternc7b61de2010-12-01 00:14:42 +0100728 skip_parent:
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200729
Alan Stern7490e442010-09-25 23:35:15 +0200730 if (dev->power.no_callbacks)
731 goto no_callback; /* Assume success. */
732
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200733 __update_runtime_status(dev, RPM_RESUMING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200734
Andrzej Hajdadbcd2d72014-10-17 12:58:02 +0200735 callback = RPM_GET_CALLBACK(dev, runtime_resume);
Rafael J. Wysocki35cd1332011-12-18 00:34:13 +0100736
Rafael J. Wysocki71c63122010-10-04 22:08:01 +0200737 retval = rpm_callback(callback, dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200738 if (retval) {
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200739 __update_runtime_status(dev, RPM_SUSPENDED);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200740 pm_runtime_cancel_pending(dev);
741 } else {
Alan Stern7490e442010-09-25 23:35:15 +0200742 no_callback:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +0200743 __update_runtime_status(dev, RPM_ACTIVE);
Tony Lindgren56f487c2015-05-13 16:36:32 -0700744 pm_runtime_mark_last_busy(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200745 if (parent)
746 atomic_inc(&parent->power.child_count);
747 }
748 wake_up_all(&dev->power.wait_queue);
749
Rafael J. Wysocki7f321c22012-08-15 21:31:45 +0200750 if (retval >= 0)
Alan Stern140a6c92010-09-25 23:35:07 +0200751 rpm_idle(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200752
753 out:
Alan Sternc7b61de2010-12-01 00:14:42 +0100754 if (parent && !dev->power.irq_safe) {
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200755 spin_unlock_irq(&dev->power.lock);
756
757 pm_runtime_put(parent);
758
759 spin_lock_irq(&dev->power.lock);
760 }
761
Ming Leic3dc2f12011-09-27 22:54:41 +0200762 trace_rpm_return_int(dev, _THIS_IP_, retval);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200763
764 return retval;
765}
766
767/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200768 * pm_runtime_work - Universal runtime PM work function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200769 * @work: Work structure used for scheduling the execution of this function.
770 *
771 * Use @work to get the device object the work is to be done for, determine what
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200772 * is to be done and execute the appropriate runtime PM function.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200773 */
774static void pm_runtime_work(struct work_struct *work)
775{
776 struct device *dev = container_of(work, struct device, power.work);
777 enum rpm_request req;
778
779 spin_lock_irq(&dev->power.lock);
780
781 if (!dev->power.request_pending)
782 goto out;
783
784 req = dev->power.request;
785 dev->power.request = RPM_REQ_NONE;
786 dev->power.request_pending = false;
787
788 switch (req) {
789 case RPM_REQ_NONE:
790 break;
791 case RPM_REQ_IDLE:
Alan Stern140a6c92010-09-25 23:35:07 +0200792 rpm_idle(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200793 break;
794 case RPM_REQ_SUSPEND:
Alan Stern140a6c92010-09-25 23:35:07 +0200795 rpm_suspend(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200796 break;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200797 case RPM_REQ_AUTOSUSPEND:
798 rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO);
799 break;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200800 case RPM_REQ_RESUME:
Alan Stern140a6c92010-09-25 23:35:07 +0200801 rpm_resume(dev, RPM_NOWAIT);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200802 break;
803 }
804
805 out:
806 spin_unlock_irq(&dev->power.lock);
807}
808
809/**
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200810 * pm_suspend_timer_fn - Timer function for pm_schedule_suspend().
811 * @data: Device pointer passed by pm_schedule_suspend().
812 *
Alan Stern1bfee5b2010-09-25 23:35:00 +0200813 * Check if the time is right and queue a suspend request.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200814 */
815static void pm_suspend_timer_fn(unsigned long data)
816{
817 struct device *dev = (struct device *)data;
818 unsigned long flags;
819 unsigned long expires;
820
821 spin_lock_irqsave(&dev->power.lock, flags);
822
823 expires = dev->power.timer_expires;
824 /* If 'expire' is after 'jiffies' we've been called too early. */
825 if (expires > 0 && !time_after(expires, jiffies)) {
826 dev->power.timer_expires = 0;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200827 rpm_suspend(dev, dev->power.timer_autosuspends ?
828 (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200829 }
830
831 spin_unlock_irqrestore(&dev->power.lock, flags);
832}
833
834/**
835 * pm_schedule_suspend - Set up a timer to submit a suspend request in future.
836 * @dev: Device to suspend.
837 * @delay: Time to wait before submitting a suspend request, in milliseconds.
838 */
839int pm_schedule_suspend(struct device *dev, unsigned int delay)
840{
841 unsigned long flags;
Alan Stern1bfee5b2010-09-25 23:35:00 +0200842 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200843
844 spin_lock_irqsave(&dev->power.lock, flags);
845
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200846 if (!delay) {
Alan Stern140a6c92010-09-25 23:35:07 +0200847 retval = rpm_suspend(dev, RPM_ASYNC);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200848 goto out;
849 }
850
Alan Stern1bfee5b2010-09-25 23:35:00 +0200851 retval = rpm_check_suspend_allowed(dev);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200852 if (retval)
853 goto out;
854
Alan Stern1bfee5b2010-09-25 23:35:00 +0200855 /* Other scheduled or pending requests need to be canceled. */
856 pm_runtime_cancel_pending(dev);
857
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200858 dev->power.timer_expires = jiffies + msecs_to_jiffies(delay);
Alan Stern1bfee5b2010-09-25 23:35:00 +0200859 dev->power.timer_expires += !dev->power.timer_expires;
Alan Stern15bcb91d2010-09-25 23:35:21 +0200860 dev->power.timer_autosuspends = 0;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200861 mod_timer(&dev->power.suspend_timer, dev->power.timer_expires);
862
863 out:
864 spin_unlock_irqrestore(&dev->power.lock, flags);
865
866 return retval;
867}
868EXPORT_SYMBOL_GPL(pm_schedule_suspend);
869
870/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200871 * __pm_runtime_idle - Entry point for runtime idle operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200872 * @dev: Device to send idle notification for.
873 * @rpmflags: Flag bits.
874 *
875 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
876 * return immediately if it is larger than zero. Then carry out an idle
877 * notification, either synchronous or asynchronous.
878 *
Colin Cross311aab72011-08-08 23:39:36 +0200879 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
880 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200881 */
Alan Stern140a6c92010-09-25 23:35:07 +0200882int __pm_runtime_idle(struct device *dev, int rpmflags)
883{
884 unsigned long flags;
885 int retval;
886
Colin Cross311aab72011-08-08 23:39:36 +0200887 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
888
Alan Stern140a6c92010-09-25 23:35:07 +0200889 if (rpmflags & RPM_GET_PUT) {
890 if (!atomic_dec_and_test(&dev->power.usage_count))
891 return 0;
892 }
893
894 spin_lock_irqsave(&dev->power.lock, flags);
895 retval = rpm_idle(dev, rpmflags);
896 spin_unlock_irqrestore(&dev->power.lock, flags);
897
898 return retval;
899}
900EXPORT_SYMBOL_GPL(__pm_runtime_idle);
901
902/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200903 * __pm_runtime_suspend - Entry point for runtime put/suspend operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200904 * @dev: Device to suspend.
905 * @rpmflags: Flag bits.
906 *
Alan Stern15bcb91d2010-09-25 23:35:21 +0200907 * If the RPM_GET_PUT flag is set, decrement the device's usage count and
908 * return immediately if it is larger than zero. Then carry out a suspend,
909 * either synchronous or asynchronous.
Alan Stern140a6c92010-09-25 23:35:07 +0200910 *
Colin Cross311aab72011-08-08 23:39:36 +0200911 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
912 * or if pm_runtime_irq_safe() has been called.
Alan Stern140a6c92010-09-25 23:35:07 +0200913 */
914int __pm_runtime_suspend(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200915{
916 unsigned long flags;
917 int retval;
918
Colin Cross311aab72011-08-08 23:39:36 +0200919 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
920
Alan Stern15bcb91d2010-09-25 23:35:21 +0200921 if (rpmflags & RPM_GET_PUT) {
922 if (!atomic_dec_and_test(&dev->power.usage_count))
923 return 0;
924 }
925
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200926 spin_lock_irqsave(&dev->power.lock, flags);
Alan Stern140a6c92010-09-25 23:35:07 +0200927 retval = rpm_suspend(dev, rpmflags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200928 spin_unlock_irqrestore(&dev->power.lock, flags);
929
930 return retval;
931}
Alan Stern140a6c92010-09-25 23:35:07 +0200932EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200933
934/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200935 * __pm_runtime_resume - Entry point for runtime resume operations.
Alan Stern140a6c92010-09-25 23:35:07 +0200936 * @dev: Device to resume.
Alan Stern3f9af052010-09-25 23:34:54 +0200937 * @rpmflags: Flag bits.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200938 *
Alan Stern140a6c92010-09-25 23:35:07 +0200939 * If the RPM_GET_PUT flag is set, increment the device's usage count. Then
940 * carry out a resume, either synchronous or asynchronous.
941 *
Colin Cross311aab72011-08-08 23:39:36 +0200942 * This routine may be called in atomic context if the RPM_ASYNC flag is set,
943 * or if pm_runtime_irq_safe() has been called.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200944 */
Alan Stern140a6c92010-09-25 23:35:07 +0200945int __pm_runtime_resume(struct device *dev, int rpmflags)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200946{
Alan Stern140a6c92010-09-25 23:35:07 +0200947 unsigned long flags;
Alan Stern1d531c12009-12-13 20:28:30 +0100948 int retval;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200949
Colin Cross311aab72011-08-08 23:39:36 +0200950 might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
951
Alan Stern140a6c92010-09-25 23:35:07 +0200952 if (rpmflags & RPM_GET_PUT)
953 atomic_inc(&dev->power.usage_count);
954
955 spin_lock_irqsave(&dev->power.lock, flags);
956 retval = rpm_resume(dev, rpmflags);
957 spin_unlock_irqrestore(&dev->power.lock, flags);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200958
959 return retval;
960}
Alan Stern140a6c92010-09-25 23:35:07 +0200961EXPORT_SYMBOL_GPL(__pm_runtime_resume);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200962
963/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200964 * __pm_runtime_set_status - Set runtime PM status of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200965 * @dev: Device to handle.
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200966 * @status: New runtime PM status of the device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200967 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +0200968 * If runtime PM of the device is disabled or its power.runtime_error field is
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +0200969 * different from zero, the status may be changed either to RPM_ACTIVE, or to
970 * RPM_SUSPENDED, as long as that reflects the actual state of the device.
971 * However, if the device has a parent and the parent is not active, and the
972 * parent's power.ignore_children flag is unset, the device's status cannot be
973 * set to RPM_ACTIVE, so -EBUSY is returned in that case.
974 *
975 * If successful, __pm_runtime_set_status() clears the power.runtime_error field
976 * and the device parent's counter of unsuspended children is modified to
977 * reflect the new status. If the new status is RPM_SUSPENDED, an idle
978 * notification request for the parent is submitted.
979 */
980int __pm_runtime_set_status(struct device *dev, unsigned int status)
981{
982 struct device *parent = dev->parent;
983 unsigned long flags;
984 bool notify_parent = false;
985 int error = 0;
986
987 if (status != RPM_ACTIVE && status != RPM_SUSPENDED)
988 return -EINVAL;
989
990 spin_lock_irqsave(&dev->power.lock, flags);
991
992 if (!dev->power.runtime_error && !dev->power.disable_depth) {
993 error = -EAGAIN;
994 goto out;
995 }
996
997 if (dev->power.runtime_status == status)
998 goto out_set;
999
1000 if (status == RPM_SUSPENDED) {
1001 /* It always is possible to set the status to 'suspended'. */
1002 if (parent) {
1003 atomic_add_unless(&parent->power.child_count, -1, 0);
1004 notify_parent = !parent->power.ignore_children;
1005 }
1006 goto out_set;
1007 }
1008
1009 if (parent) {
Rafael J. Wysockibab636b2009-12-03 20:21:21 +01001010 spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001011
1012 /*
1013 * It is invalid to put an active child under a parent that is
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001014 * not active, has runtime PM enabled and the
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001015 * 'power.ignore_children' flag unset.
1016 */
1017 if (!parent->power.disable_depth
1018 && !parent->power.ignore_children
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +01001019 && parent->power.runtime_status != RPM_ACTIVE)
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001020 error = -EBUSY;
Rafael J. Wysocki965c4ac2009-12-03 21:04:41 +01001021 else if (dev->power.runtime_status == RPM_SUSPENDED)
1022 atomic_inc(&parent->power.child_count);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001023
Alan Stern862f89b2009-11-25 01:06:37 +01001024 spin_unlock(&parent->power.lock);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001025
1026 if (error)
1027 goto out;
1028 }
1029
1030 out_set:
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001031 __update_runtime_status(dev, status);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001032 dev->power.runtime_error = 0;
1033 out:
1034 spin_unlock_irqrestore(&dev->power.lock, flags);
1035
1036 if (notify_parent)
1037 pm_request_idle(parent);
1038
1039 return error;
1040}
1041EXPORT_SYMBOL_GPL(__pm_runtime_set_status);
1042
1043/**
1044 * __pm_runtime_barrier - Cancel pending requests and wait for completions.
1045 * @dev: Device to handle.
1046 *
1047 * Flush all pending requests for the device from pm_wq and wait for all
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001048 * runtime PM operations involving the device in progress to complete.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001049 *
1050 * Should be called under dev->power.lock with interrupts disabled.
1051 */
1052static void __pm_runtime_barrier(struct device *dev)
1053{
1054 pm_runtime_deactivate_timer(dev);
1055
1056 if (dev->power.request_pending) {
1057 dev->power.request = RPM_REQ_NONE;
1058 spin_unlock_irq(&dev->power.lock);
1059
1060 cancel_work_sync(&dev->power.work);
1061
1062 spin_lock_irq(&dev->power.lock);
1063 dev->power.request_pending = false;
1064 }
1065
1066 if (dev->power.runtime_status == RPM_SUSPENDING
1067 || dev->power.runtime_status == RPM_RESUMING
1068 || dev->power.idle_notification) {
1069 DEFINE_WAIT(wait);
1070
1071 /* Suspend, wake-up or idle notification in progress. */
1072 for (;;) {
1073 prepare_to_wait(&dev->power.wait_queue, &wait,
1074 TASK_UNINTERRUPTIBLE);
1075 if (dev->power.runtime_status != RPM_SUSPENDING
1076 && dev->power.runtime_status != RPM_RESUMING
1077 && !dev->power.idle_notification)
1078 break;
1079 spin_unlock_irq(&dev->power.lock);
1080
1081 schedule();
1082
1083 spin_lock_irq(&dev->power.lock);
1084 }
1085 finish_wait(&dev->power.wait_queue, &wait);
1086 }
1087}
1088
1089/**
1090 * pm_runtime_barrier - Flush pending requests and wait for completions.
1091 * @dev: Device to handle.
1092 *
1093 * Prevent the device from being suspended by incrementing its usage counter and
1094 * if there's a pending resume request for the device, wake the device up.
1095 * Next, make sure that all pending requests for the device have been flushed
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001096 * from pm_wq and wait for all runtime PM operations involving the device in
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001097 * progress to complete.
1098 *
1099 * Return value:
1100 * 1, if there was a resume request pending and the device had to be woken up,
1101 * 0, otherwise
1102 */
1103int pm_runtime_barrier(struct device *dev)
1104{
1105 int retval = 0;
1106
1107 pm_runtime_get_noresume(dev);
1108 spin_lock_irq(&dev->power.lock);
1109
1110 if (dev->power.request_pending
1111 && dev->power.request == RPM_REQ_RESUME) {
Alan Stern140a6c92010-09-25 23:35:07 +02001112 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001113 retval = 1;
1114 }
1115
1116 __pm_runtime_barrier(dev);
1117
1118 spin_unlock_irq(&dev->power.lock);
1119 pm_runtime_put_noidle(dev);
1120
1121 return retval;
1122}
1123EXPORT_SYMBOL_GPL(pm_runtime_barrier);
1124
1125/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001126 * __pm_runtime_disable - Disable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001127 * @dev: Device to handle.
1128 * @check_resume: If set, check if there's a resume request for the device.
1129 *
Geert Uytterhoeven7b608942014-03-11 11:23:40 +01001130 * Increment power.disable_depth for the device and if it was zero previously,
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001131 * cancel all pending runtime PM requests for the device and wait for all
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001132 * operations in progress to complete. The device can be either active or
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001133 * suspended after its runtime PM has been disabled.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001134 *
1135 * If @check_resume is set and there's a resume request pending when
1136 * __pm_runtime_disable() is called and power.disable_depth is zero, the
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001137 * function will wake up the device before disabling its runtime PM.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001138 */
1139void __pm_runtime_disable(struct device *dev, bool check_resume)
1140{
1141 spin_lock_irq(&dev->power.lock);
1142
1143 if (dev->power.disable_depth > 0) {
1144 dev->power.disable_depth++;
1145 goto out;
1146 }
1147
1148 /*
1149 * Wake up the device if there's a resume request pending, because that
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001150 * means there probably is some I/O to process and disabling runtime PM
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001151 * shouldn't prevent the device from processing the I/O.
1152 */
1153 if (check_resume && dev->power.request_pending
1154 && dev->power.request == RPM_REQ_RESUME) {
1155 /*
1156 * Prevent suspends and idle notifications from being carried
1157 * out after we have woken up the device.
1158 */
1159 pm_runtime_get_noresume(dev);
1160
Alan Stern140a6c92010-09-25 23:35:07 +02001161 rpm_resume(dev, 0);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001162
1163 pm_runtime_put_noidle(dev);
1164 }
1165
1166 if (!dev->power.disable_depth++)
1167 __pm_runtime_barrier(dev);
1168
1169 out:
1170 spin_unlock_irq(&dev->power.lock);
1171}
1172EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1173
1174/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001175 * pm_runtime_enable - Enable runtime PM of a device.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001176 * @dev: Device to handle.
1177 */
1178void pm_runtime_enable(struct device *dev)
1179{
1180 unsigned long flags;
1181
1182 spin_lock_irqsave(&dev->power.lock, flags);
1183
1184 if (dev->power.disable_depth > 0)
1185 dev->power.disable_depth--;
1186 else
1187 dev_warn(dev, "Unbalanced %s!\n", __func__);
1188
1189 spin_unlock_irqrestore(&dev->power.lock, flags);
1190}
1191EXPORT_SYMBOL_GPL(pm_runtime_enable);
1192
1193/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001194 * pm_runtime_forbid - Block runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001195 * @dev: Device to handle.
1196 *
1197 * Increase the device's usage count and clear its power.runtime_auto flag,
1198 * so that it cannot be suspended at run time until pm_runtime_allow() is called
1199 * for it.
1200 */
1201void pm_runtime_forbid(struct device *dev)
1202{
1203 spin_lock_irq(&dev->power.lock);
1204 if (!dev->power.runtime_auto)
1205 goto out;
1206
1207 dev->power.runtime_auto = false;
1208 atomic_inc(&dev->power.usage_count);
Alan Stern140a6c92010-09-25 23:35:07 +02001209 rpm_resume(dev, 0);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001210
1211 out:
1212 spin_unlock_irq(&dev->power.lock);
1213}
1214EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1215
1216/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001217 * pm_runtime_allow - Unblock runtime PM of a device.
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001218 * @dev: Device to handle.
1219 *
1220 * Decrease the device's usage count and set its power.runtime_auto flag.
1221 */
1222void pm_runtime_allow(struct device *dev)
1223{
1224 spin_lock_irq(&dev->power.lock);
1225 if (dev->power.runtime_auto)
1226 goto out;
1227
1228 dev->power.runtime_auto = true;
1229 if (atomic_dec_and_test(&dev->power.usage_count))
Alan Stern15bcb91d2010-09-25 23:35:21 +02001230 rpm_idle(dev, RPM_AUTO);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001231
1232 out:
1233 spin_unlock_irq(&dev->power.lock);
1234}
1235EXPORT_SYMBOL_GPL(pm_runtime_allow);
1236
1237/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001238 * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
Alan Stern7490e442010-09-25 23:35:15 +02001239 * @dev: Device to handle.
1240 *
1241 * Set the power.no_callbacks flag, which tells the PM core that this
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001242 * device is power-managed through its parent and has no runtime PM
1243 * callbacks of its own. The runtime sysfs attributes will be removed.
Alan Stern7490e442010-09-25 23:35:15 +02001244 */
1245void pm_runtime_no_callbacks(struct device *dev)
1246{
1247 spin_lock_irq(&dev->power.lock);
1248 dev->power.no_callbacks = 1;
1249 spin_unlock_irq(&dev->power.lock);
1250 if (device_is_registered(dev))
1251 rpm_sysfs_remove(dev);
1252}
1253EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks);
1254
1255/**
Alan Sternc7b61de2010-12-01 00:14:42 +01001256 * pm_runtime_irq_safe - Leave interrupts disabled during callbacks.
1257 * @dev: Device to handle
1258 *
1259 * Set the power.irq_safe flag, which tells the PM core that the
1260 * ->runtime_suspend() and ->runtime_resume() callbacks for this device should
1261 * always be invoked with the spinlock held and interrupts disabled. It also
1262 * causes the parent's usage counter to be permanently incremented, preventing
1263 * the parent from runtime suspending -- otherwise an irq-safe child might have
1264 * to wait for a non-irq-safe parent.
1265 */
1266void pm_runtime_irq_safe(struct device *dev)
1267{
1268 if (dev->parent)
1269 pm_runtime_get_sync(dev->parent);
1270 spin_lock_irq(&dev->power.lock);
1271 dev->power.irq_safe = 1;
1272 spin_unlock_irq(&dev->power.lock);
1273}
1274EXPORT_SYMBOL_GPL(pm_runtime_irq_safe);
1275
1276/**
Alan Stern15bcb91d2010-09-25 23:35:21 +02001277 * update_autosuspend - Handle a change to a device's autosuspend settings.
1278 * @dev: Device to handle.
1279 * @old_delay: The former autosuspend_delay value.
1280 * @old_use: The former use_autosuspend value.
1281 *
1282 * Prevent runtime suspend if the new delay is negative and use_autosuspend is
1283 * set; otherwise allow it. Send an idle notification if suspends are allowed.
1284 *
1285 * This function must be called under dev->power.lock with interrupts disabled.
1286 */
1287static void update_autosuspend(struct device *dev, int old_delay, int old_use)
1288{
1289 int delay = dev->power.autosuspend_delay;
1290
1291 /* Should runtime suspend be prevented now? */
1292 if (dev->power.use_autosuspend && delay < 0) {
1293
1294 /* If it used to be allowed then prevent it. */
1295 if (!old_use || old_delay >= 0) {
1296 atomic_inc(&dev->power.usage_count);
1297 rpm_resume(dev, 0);
1298 }
1299 }
1300
1301 /* Runtime suspend should be allowed now. */
1302 else {
1303
1304 /* If it used to be prevented then allow it. */
1305 if (old_use && old_delay < 0)
1306 atomic_dec(&dev->power.usage_count);
1307
1308 /* Maybe we can autosuspend now. */
1309 rpm_idle(dev, RPM_AUTO);
1310 }
1311}
1312
1313/**
1314 * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value.
1315 * @dev: Device to handle.
1316 * @delay: Value of the new delay in milliseconds.
1317 *
1318 * Set the device's power.autosuspend_delay value. If it changes to negative
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001319 * and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1320 * changes the other way, allow runtime suspends.
Alan Stern15bcb91d2010-09-25 23:35:21 +02001321 */
1322void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1323{
1324 int old_delay, old_use;
1325
1326 spin_lock_irq(&dev->power.lock);
1327 old_delay = dev->power.autosuspend_delay;
1328 old_use = dev->power.use_autosuspend;
1329 dev->power.autosuspend_delay = delay;
1330 update_autosuspend(dev, old_delay, old_use);
1331 spin_unlock_irq(&dev->power.lock);
1332}
1333EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay);
1334
1335/**
1336 * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag.
1337 * @dev: Device to handle.
1338 * @use: New value for use_autosuspend.
1339 *
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001340 * Set the device's power.use_autosuspend flag, and allow or prevent runtime
Alan Stern15bcb91d2010-09-25 23:35:21 +02001341 * suspends as needed.
1342 */
1343void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1344{
1345 int old_delay, old_use;
1346
1347 spin_lock_irq(&dev->power.lock);
1348 old_delay = dev->power.autosuspend_delay;
1349 old_use = dev->power.use_autosuspend;
1350 dev->power.use_autosuspend = use;
1351 update_autosuspend(dev, old_delay, old_use);
1352 spin_unlock_irq(&dev->power.lock);
1353}
1354EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1355
1356/**
Rafael J. Wysocki62052ab2011-07-06 10:52:13 +02001357 * pm_runtime_init - Initialize runtime PM fields in given device object.
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001358 * @dev: Device object to initialize.
1359 */
1360void pm_runtime_init(struct device *dev)
1361{
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001362 dev->power.runtime_status = RPM_SUSPENDED;
1363 dev->power.idle_notification = false;
1364
1365 dev->power.disable_depth = 1;
1366 atomic_set(&dev->power.usage_count, 0);
1367
1368 dev->power.runtime_error = 0;
1369
1370 atomic_set(&dev->power.child_count, 0);
1371 pm_suspend_ignore_children(dev, false);
Rafael J. Wysocki53823632010-01-23 22:02:51 +01001372 dev->power.runtime_auto = true;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001373
1374 dev->power.request_pending = false;
1375 dev->power.request = RPM_REQ_NONE;
1376 dev->power.deferred_resume = false;
Arjan van de Ven8d4b9d12010-07-19 02:01:06 +02001377 dev->power.accounting_timestamp = jiffies;
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001378 INIT_WORK(&dev->power.work, pm_runtime_work);
1379
1380 dev->power.timer_expires = 0;
1381 setup_timer(&dev->power.suspend_timer, pm_suspend_timer_fn,
1382 (unsigned long)dev);
1383
1384 init_waitqueue_head(&dev->power.wait_queue);
1385}
1386
1387/**
1388 * pm_runtime_remove - Prepare for removing a device from device hierarchy.
1389 * @dev: Device object being removed from device hierarchy.
1390 */
1391void pm_runtime_remove(struct device *dev)
1392{
1393 __pm_runtime_disable(dev, false);
1394
1395 /* Change the status back to 'suspended' to match the initial status. */
1396 if (dev->power.runtime_status == RPM_ACTIVE)
1397 pm_runtime_set_suspended(dev);
Alan Sternc7b61de2010-12-01 00:14:42 +01001398 if (dev->power.irq_safe && dev->parent)
Ulf Hanssondb28dfa2013-04-12 09:41:30 +00001399 pm_runtime_put(dev->parent);
Rafael J. Wysocki5e928f72009-08-18 23:38:32 +02001400}
Ulf Hansson37f20412014-03-01 11:56:05 +01001401
1402/**
1403 * pm_runtime_force_suspend - Force a device into suspend state if needed.
1404 * @dev: Device to suspend.
1405 *
1406 * Disable runtime PM so we safely can check the device's runtime PM status and
1407 * if it is active, invoke it's .runtime_suspend callback to bring it into
1408 * suspend state. Keep runtime PM disabled to preserve the state unless we
1409 * encounter errors.
1410 *
1411 * Typically this function may be invoked from a system suspend callback to make
1412 * sure the device is put into low power state.
1413 */
1414int pm_runtime_force_suspend(struct device *dev)
1415{
1416 int (*callback)(struct device *);
1417 int ret = 0;
1418
1419 pm_runtime_disable(dev);
Ulf Hansson37f20412014-03-01 11:56:05 +01001420 if (pm_runtime_status_suspended(dev))
1421 return 0;
1422
Andrzej Hajdadbcd2d72014-10-17 12:58:02 +02001423 callback = RPM_GET_CALLBACK(dev, runtime_suspend);
Ulf Hansson37f20412014-03-01 11:56:05 +01001424
1425 if (!callback) {
1426 ret = -ENOSYS;
1427 goto err;
1428 }
1429
1430 ret = callback(dev);
1431 if (ret)
1432 goto err;
1433
1434 pm_runtime_set_suspended(dev);
1435 return 0;
1436err:
1437 pm_runtime_enable(dev);
1438 return ret;
1439}
1440EXPORT_SYMBOL_GPL(pm_runtime_force_suspend);
1441
1442/**
1443 * pm_runtime_force_resume - Force a device into resume state.
1444 * @dev: Device to resume.
1445 *
1446 * Prior invoking this function we expect the user to have brought the device
1447 * into low power state by a call to pm_runtime_force_suspend(). Here we reverse
1448 * those actions and brings the device into full power. We update the runtime PM
1449 * status and re-enables runtime PM.
1450 *
1451 * Typically this function may be invoked from a system resume callback to make
1452 * sure the device is put into full power state.
1453 */
1454int pm_runtime_force_resume(struct device *dev)
1455{
1456 int (*callback)(struct device *);
1457 int ret = 0;
1458
Andrzej Hajdadbcd2d72014-10-17 12:58:02 +02001459 callback = RPM_GET_CALLBACK(dev, runtime_resume);
Ulf Hansson37f20412014-03-01 11:56:05 +01001460
1461 if (!callback) {
1462 ret = -ENOSYS;
1463 goto out;
1464 }
1465
1466 ret = callback(dev);
1467 if (ret)
1468 goto out;
1469
1470 pm_runtime_set_active(dev);
1471 pm_runtime_mark_last_busy(dev);
1472out:
1473 pm_runtime_enable(dev);
1474 return ret;
1475}
1476EXPORT_SYMBOL_GPL(pm_runtime_force_resume);