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