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