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