Commit | Line | Data |
---|---|---|
5de363b6 | 1 | // SPDX-License-Identifier: GPL-2.0 |
5e928f77 | 2 | /* |
62052ab1 | 3 | * drivers/base/power/runtime.c - Helper functions for device runtime PM |
5e928f77 RW |
4 | * |
5 | * Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc. | |
1bfee5bc | 6 | * Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu> |
5e928f77 | 7 | */ |
5b3cc15a | 8 | #include <linux/sched/mm.h> |
8234f673 VG |
9 | #include <linux/ktime.h> |
10 | #include <linux/hrtimer.h> | |
1b6bc32f | 11 | #include <linux/export.h> |
5e928f77 | 12 | #include <linux/pm_runtime.h> |
4990d4fe | 13 | #include <linux/pm_wakeirq.h> |
ed509c7e | 14 | #include <linux/rculist.h> |
c3dc2f14 | 15 | #include <trace/events/rpm.h> |
21d5c57b RW |
16 | |
17 | #include "../base.h" | |
7490e442 | 18 | #include "power.h" |
5e928f77 | 19 | |
dbcd2d72 | 20 | typedef int (*pm_callback_t)(struct device *); |
5f59df79 | 21 | |
dbcd2d72 | 22 | static pm_callback_t __rpm_get_callback(struct device *dev, size_t cb_offset) |
5f59df79 | 23 | { |
dbcd2d72 AH |
24 | pm_callback_t cb; |
25 | const struct dev_pm_ops *ops; | |
26 | ||
27 | if (dev->pm_domain) | |
28 | ops = &dev->pm_domain->ops; | |
29 | else if (dev->type && dev->type->pm) | |
30 | ops = dev->type->pm; | |
31 | else if (dev->class && dev->class->pm) | |
32 | ops = dev->class->pm; | |
33 | else if (dev->bus && dev->bus->pm) | |
34 | ops = dev->bus->pm; | |
35 | else | |
36 | ops = NULL; | |
37 | ||
38 | if (ops) | |
39 | cb = *(pm_callback_t *)((void *)ops + cb_offset); | |
40 | else | |
41 | cb = NULL; | |
42 | ||
43 | if (!cb && dev->driver && dev->driver->pm) | |
44 | cb = *(pm_callback_t *)((void *)dev->driver->pm + cb_offset); | |
45 | ||
46 | return cb; | |
5f59df79 UH |
47 | } |
48 | ||
dbcd2d72 AH |
49 | #define RPM_GET_CALLBACK(dev, callback) \ |
50 | __rpm_get_callback(dev, offsetof(struct dev_pm_ops, callback)) | |
51 | ||
140a6c94 | 52 | static int rpm_resume(struct device *dev, int rpmflags); |
7490e442 | 53 | static int rpm_suspend(struct device *dev, int rpmflags); |
5e928f77 | 54 | |
4769373c AS |
55 | /** |
56 | * update_pm_runtime_accounting - Update the time accounting of power states | |
57 | * @dev: Device to update the accounting for | |
58 | * | |
59 | * In order to be able to have time accounting of the various power states | |
60 | * (as used by programs such as PowerTOP to show the effectiveness of runtime | |
61 | * PM), we need to track the time spent in each state. | |
62 | * update_pm_runtime_accounting must be called each time before the | |
63 | * runtime_status field is updated, to account the time in the old state | |
64 | * correctly. | |
65 | */ | |
0996584b | 66 | static void update_pm_runtime_accounting(struct device *dev) |
4769373c | 67 | { |
fed7e88c | 68 | u64 now, last, delta; |
4769373c | 69 | |
fed7e88c VG |
70 | if (dev->power.disable_depth > 0) |
71 | return; | |
72 | ||
73 | last = dev->power.accounting_timestamp; | |
4769373c | 74 | |
fed7e88c | 75 | now = ktime_get_mono_fast_ns(); |
4769373c AS |
76 | dev->power.accounting_timestamp = now; |
77 | ||
c155f649 VG |
78 | /* |
79 | * Because ktime_get_mono_fast_ns() is not monotonic during | |
80 | * timekeeping updates, ensure that 'now' is after the last saved | |
81 | * timesptamp. | |
82 | */ | |
83 | if (now < last) | |
4769373c AS |
84 | return; |
85 | ||
c155f649 VG |
86 | delta = now - last; |
87 | ||
4769373c | 88 | if (dev->power.runtime_status == RPM_SUSPENDED) |
a08c2a5a | 89 | dev->power.suspended_time += delta; |
4769373c | 90 | else |
a08c2a5a | 91 | dev->power.active_time += delta; |
4769373c AS |
92 | } |
93 | ||
94 | static void __update_runtime_status(struct device *dev, enum rpm_status status) | |
95 | { | |
96 | update_pm_runtime_accounting(dev); | |
015abee4 | 97 | trace_rpm_status(dev, status); |
4769373c AS |
98 | dev->power.runtime_status = status; |
99 | } | |
100 | ||
fdc56c07 | 101 | static u64 rpm_get_accounted_time(struct device *dev, bool suspended) |
8a62ffe2 | 102 | { |
a08c2a5a TG |
103 | u64 time; |
104 | unsigned long flags; | |
8a62ffe2 VG |
105 | |
106 | spin_lock_irqsave(&dev->power.lock, flags); | |
107 | ||
108 | update_pm_runtime_accounting(dev); | |
fdc56c07 | 109 | time = suspended ? dev->power.suspended_time : dev->power.active_time; |
8a62ffe2 VG |
110 | |
111 | spin_unlock_irqrestore(&dev->power.lock, flags); | |
112 | ||
a08c2a5a | 113 | return time; |
8a62ffe2 | 114 | } |
fdc56c07 UH |
115 | |
116 | u64 pm_runtime_active_time(struct device *dev) | |
117 | { | |
118 | return rpm_get_accounted_time(dev, false); | |
119 | } | |
120 | ||
121 | u64 pm_runtime_suspended_time(struct device *dev) | |
122 | { | |
123 | return rpm_get_accounted_time(dev, true); | |
124 | } | |
8a62ffe2 VG |
125 | EXPORT_SYMBOL_GPL(pm_runtime_suspended_time); |
126 | ||
5e928f77 RW |
127 | /** |
128 | * pm_runtime_deactivate_timer - Deactivate given device's suspend timer. | |
129 | * @dev: Device to handle. | |
130 | */ | |
131 | static void pm_runtime_deactivate_timer(struct device *dev) | |
132 | { | |
133 | if (dev->power.timer_expires > 0) { | |
74fb4486 | 134 | hrtimer_try_to_cancel(&dev->power.suspend_timer); |
5e928f77 RW |
135 | dev->power.timer_expires = 0; |
136 | } | |
137 | } | |
138 | ||
139 | /** | |
140 | * pm_runtime_cancel_pending - Deactivate suspend timer and cancel requests. | |
141 | * @dev: Device to handle. | |
142 | */ | |
143 | static void pm_runtime_cancel_pending(struct device *dev) | |
144 | { | |
145 | pm_runtime_deactivate_timer(dev); | |
146 | /* | |
147 | * In case there's a request pending, make sure its work function will | |
148 | * return without doing anything. | |
149 | */ | |
150 | dev->power.request = RPM_REQ_NONE; | |
151 | } | |
152 | ||
15bcb91d AS |
153 | /* |
154 | * pm_runtime_autosuspend_expiration - Get a device's autosuspend-delay expiration time. | |
155 | * @dev: Device to handle. | |
156 | * | |
157 | * Compute the autosuspend-delay expiration time based on the device's | |
158 | * power.last_busy time. If the delay has already expired or is disabled | |
159 | * (negative) or the power.use_autosuspend flag isn't set, return 0. | |
1f7b7081 | 160 | * Otherwise return the expiration time in nanoseconds (adjusted to be nonzero). |
15bcb91d AS |
161 | * |
162 | * This function may be called either with or without dev->power.lock held. | |
163 | * Either way it can be racy, since power.last_busy may be updated at any time. | |
164 | */ | |
8234f673 | 165 | u64 pm_runtime_autosuspend_expiration(struct device *dev) |
15bcb91d AS |
166 | { |
167 | int autosuspend_delay; | |
f800ea32 | 168 | u64 expires; |
15bcb91d AS |
169 | |
170 | if (!dev->power.use_autosuspend) | |
f800ea32 | 171 | return 0; |
15bcb91d | 172 | |
6aa7de05 | 173 | autosuspend_delay = READ_ONCE(dev->power.autosuspend_delay); |
15bcb91d | 174 | if (autosuspend_delay < 0) |
f800ea32 | 175 | return 0; |
15bcb91d | 176 | |
f800ea32 LM |
177 | expires = READ_ONCE(dev->power.last_busy); |
178 | expires += (u64)autosuspend_delay * NSEC_PER_MSEC; | |
179 | if (expires > ktime_get_mono_fast_ns()) | |
180 | return expires; /* Expires in the future */ | |
15bcb91d | 181 | |
f800ea32 | 182 | return 0; |
15bcb91d AS |
183 | } |
184 | EXPORT_SYMBOL_GPL(pm_runtime_autosuspend_expiration); | |
185 | ||
e823407f ML |
186 | static int dev_memalloc_noio(struct device *dev, void *data) |
187 | { | |
188 | return dev->power.memalloc_noio; | |
189 | } | |
190 | ||
191 | /* | |
192 | * pm_runtime_set_memalloc_noio - Set a device's memalloc_noio flag. | |
193 | * @dev: Device to handle. | |
194 | * @enable: True for setting the flag and False for clearing the flag. | |
195 | * | |
196 | * Set the flag for all devices in the path from the device to the | |
197 | * root device in the device tree if @enable is true, otherwise clear | |
198 | * the flag for devices in the path whose siblings don't set the flag. | |
199 | * | |
200 | * The function should only be called by block device, or network | |
201 | * device driver for solving the deadlock problem during runtime | |
202 | * resume/suspend: | |
203 | * | |
204 | * If memory allocation with GFP_KERNEL is called inside runtime | |
205 | * resume/suspend callback of any one of its ancestors(or the | |
206 | * block device itself), the deadlock may be triggered inside the | |
207 | * memory allocation since it might not complete until the block | |
208 | * device becomes active and the involed page I/O finishes. The | |
209 | * situation is pointed out first by Alan Stern. Network device | |
210 | * are involved in iSCSI kind of situation. | |
211 | * | |
212 | * The lock of dev_hotplug_mutex is held in the function for handling | |
213 | * hotplug race because pm_runtime_set_memalloc_noio() may be called | |
214 | * in async probe(). | |
215 | * | |
216 | * The function should be called between device_add() and device_del() | |
217 | * on the affected device(block/network device). | |
218 | */ | |
219 | void pm_runtime_set_memalloc_noio(struct device *dev, bool enable) | |
220 | { | |
221 | static DEFINE_MUTEX(dev_hotplug_mutex); | |
222 | ||
223 | mutex_lock(&dev_hotplug_mutex); | |
224 | for (;;) { | |
225 | bool enabled; | |
226 | ||
227 | /* hold power lock since bitfield is not SMP-safe. */ | |
228 | spin_lock_irq(&dev->power.lock); | |
229 | enabled = dev->power.memalloc_noio; | |
230 | dev->power.memalloc_noio = enable; | |
231 | spin_unlock_irq(&dev->power.lock); | |
232 | ||
233 | /* | |
234 | * not need to enable ancestors any more if the device | |
235 | * has been enabled. | |
236 | */ | |
237 | if (enabled && enable) | |
238 | break; | |
239 | ||
240 | dev = dev->parent; | |
241 | ||
242 | /* | |
243 | * clear flag of the parent device only if all the | |
244 | * children don't set the flag because ancestor's | |
245 | * flag was set by any one of the descendants. | |
246 | */ | |
247 | if (!dev || (!enable && | |
dbfa4478 | 248 | device_for_each_child(dev, NULL, dev_memalloc_noio))) |
e823407f ML |
249 | break; |
250 | } | |
251 | mutex_unlock(&dev_hotplug_mutex); | |
252 | } | |
253 | EXPORT_SYMBOL_GPL(pm_runtime_set_memalloc_noio); | |
254 | ||
5e928f77 | 255 | /** |
1bfee5bc AS |
256 | * rpm_check_suspend_allowed - Test whether a device may be suspended. |
257 | * @dev: Device to test. | |
5e928f77 | 258 | */ |
1bfee5bc | 259 | static int rpm_check_suspend_allowed(struct device *dev) |
5e928f77 RW |
260 | { |
261 | int retval = 0; | |
262 | ||
5e928f77 RW |
263 | if (dev->power.runtime_error) |
264 | retval = -EINVAL; | |
632e270e RW |
265 | else if (dev->power.disable_depth > 0) |
266 | retval = -EACCES; | |
82586a72 | 267 | else if (atomic_read(&dev->power.usage_count)) |
5e928f77 | 268 | retval = -EAGAIN; |
dbfa4478 | 269 | else if (!dev->power.ignore_children && atomic_read(&dev->power.child_count)) |
5e928f77 | 270 | retval = -EBUSY; |
1bfee5bc AS |
271 | |
272 | /* Pending resume requests take precedence over suspends. */ | |
dbfa4478 RW |
273 | else if ((dev->power.deferred_resume && |
274 | dev->power.runtime_status == RPM_SUSPENDING) || | |
275 | (dev->power.request_pending && dev->power.request == RPM_REQ_RESUME)) | |
1bfee5bc | 276 | retval = -EAGAIN; |
8262331e | 277 | else if (__dev_pm_qos_resume_latency(dev) == 0) |
55d7ec45 | 278 | retval = -EPERM; |
1bfee5bc AS |
279 | else if (dev->power.runtime_status == RPM_SUSPENDED) |
280 | retval = 1; | |
281 | ||
282 | return retval; | |
283 | } | |
284 | ||
21d5c57b RW |
285 | static int rpm_get_suppliers(struct device *dev) |
286 | { | |
287 | struct device_link *link; | |
288 | ||
c2fa1e1b JFG |
289 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, |
290 | device_links_read_lock_held()) { | |
21d5c57b RW |
291 | int retval; |
292 | ||
d12544fb | 293 | if (!(link->flags & DL_FLAG_PM_RUNTIME)) |
21d5c57b RW |
294 | continue; |
295 | ||
296 | retval = pm_runtime_get_sync(link->supplier); | |
31eb7431 RW |
297 | /* Ignore suppliers with disabled runtime PM. */ |
298 | if (retval < 0 && retval != -EACCES) { | |
21d5c57b RW |
299 | pm_runtime_put_noidle(link->supplier); |
300 | return retval; | |
301 | } | |
e2f3cd83 | 302 | refcount_inc(&link->rpm_active); |
21d5c57b RW |
303 | } |
304 | return 0; | |
305 | } | |
306 | ||
d1579e61 RW |
307 | /** |
308 | * pm_runtime_release_supplier - Drop references to device link's supplier. | |
309 | * @link: Target device link. | |
d1579e61 | 310 | * |
07358194 | 311 | * Drop all runtime PM references associated with @link to its supplier device. |
d1579e61 | 312 | */ |
07358194 | 313 | void pm_runtime_release_supplier(struct device_link *link) |
d1579e61 RW |
314 | { |
315 | struct device *supplier = link->supplier; | |
316 | ||
317 | /* | |
318 | * The additional power.usage_count check is a safety net in case | |
319 | * the rpm_active refcount becomes saturated, in which case | |
320 | * refcount_dec_not_one() would return true forever, but it is not | |
321 | * strictly necessary. | |
322 | */ | |
323 | while (refcount_dec_not_one(&link->rpm_active) && | |
324 | atomic_read(&supplier->power.usage_count) > 0) | |
325 | pm_runtime_put_noidle(supplier); | |
d1579e61 RW |
326 | } |
327 | ||
5244f5e2 | 328 | static void __rpm_put_suppliers(struct device *dev, bool try_to_suspend) |
21d5c57b RW |
329 | { |
330 | struct device_link *link; | |
331 | ||
c2fa1e1b | 332 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, |
07358194 RW |
333 | device_links_read_lock_held()) { |
334 | pm_runtime_release_supplier(link); | |
335 | if (try_to_suspend) | |
336 | pm_request_idle(link->supplier); | |
337 | } | |
21d5c57b RW |
338 | } |
339 | ||
5244f5e2 RW |
340 | static void rpm_put_suppliers(struct device *dev) |
341 | { | |
342 | __rpm_put_suppliers(dev, true); | |
343 | } | |
344 | ||
345 | static void rpm_suspend_suppliers(struct device *dev) | |
346 | { | |
347 | struct device_link *link; | |
348 | int idx = device_links_read_lock(); | |
349 | ||
350 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, | |
351 | device_links_read_lock_held()) | |
352 | pm_request_idle(link->supplier); | |
353 | ||
354 | device_links_read_unlock(idx); | |
355 | } | |
356 | ||
ad3c36a5 RW |
357 | /** |
358 | * __rpm_callback - Run a given runtime PM callback for a given device. | |
359 | * @cb: Runtime PM callback to run. | |
360 | * @dev: Device to run the callback for. | |
361 | */ | |
362 | static int __rpm_callback(int (*cb)(struct device *), struct device *dev) | |
363 | __releases(&dev->power.lock) __acquires(&dev->power.lock) | |
364 | { | |
63d00be6 | 365 | int retval = 0, idx; |
0cab893f | 366 | bool use_links = dev->power.links_count > 0; |
ad3c36a5 | 367 | |
21d5c57b | 368 | if (dev->power.irq_safe) { |
ad3c36a5 | 369 | spin_unlock(&dev->power.lock); |
21d5c57b | 370 | } else { |
ad3c36a5 RW |
371 | spin_unlock_irq(&dev->power.lock); |
372 | ||
0cab893f RW |
373 | /* |
374 | * Resume suppliers if necessary. | |
375 | * | |
376 | * The device's runtime PM status cannot change until this | |
377 | * routine returns, so it is safe to read the status outside of | |
378 | * the lock. | |
379 | */ | |
380 | if (use_links && dev->power.runtime_status == RPM_RESUMING) { | |
21d5c57b RW |
381 | idx = device_links_read_lock(); |
382 | ||
383 | retval = rpm_get_suppliers(dev); | |
5244f5e2 RW |
384 | if (retval) { |
385 | rpm_put_suppliers(dev); | |
21d5c57b | 386 | goto fail; |
5244f5e2 | 387 | } |
21d5c57b RW |
388 | |
389 | device_links_read_unlock(idx); | |
390 | } | |
391 | } | |
392 | ||
63d00be6 UH |
393 | if (cb) |
394 | retval = cb(dev); | |
ad3c36a5 | 395 | |
21d5c57b | 396 | if (dev->power.irq_safe) { |
ad3c36a5 | 397 | spin_lock(&dev->power.lock); |
0cab893f RW |
398 | } else { |
399 | /* | |
400 | * If the device is suspending and the callback has returned | |
401 | * success, drop the usage counters of the suppliers that have | |
402 | * been reference counted on its resume. | |
403 | * | |
404 | * Do that if resume fails too. | |
405 | */ | |
dbfa4478 RW |
406 | if (use_links && |
407 | ((dev->power.runtime_status == RPM_SUSPENDING && !retval) || | |
408 | (dev->power.runtime_status == RPM_RESUMING && retval))) { | |
0cab893f | 409 | idx = device_links_read_lock(); |
44cc89f7 | 410 | |
5244f5e2 | 411 | __rpm_put_suppliers(dev, false); |
44cc89f7 | 412 | |
5244f5e2 | 413 | fail: |
0cab893f RW |
414 | device_links_read_unlock(idx); |
415 | } | |
21d5c57b | 416 | |
ad3c36a5 | 417 | spin_lock_irq(&dev->power.lock); |
21d5c57b | 418 | } |
ad3c36a5 RW |
419 | |
420 | return retval; | |
421 | } | |
422 | ||
0307f4e8 RW |
423 | /** |
424 | * rpm_callback - Run a given runtime PM callback for a given device. | |
425 | * @cb: Runtime PM callback to run. | |
426 | * @dev: Device to run the callback for. | |
427 | */ | |
428 | static int rpm_callback(int (*cb)(struct device *), struct device *dev) | |
429 | { | |
430 | int retval; | |
431 | ||
432 | if (dev->power.memalloc_noio) { | |
433 | unsigned int noio_flag; | |
434 | ||
435 | /* | |
436 | * Deadlock might be caused if memory allocation with | |
437 | * GFP_KERNEL happens inside runtime_suspend and | |
438 | * runtime_resume callbacks of one block device's | |
439 | * ancestor or the block device itself. Network | |
440 | * device might be thought as part of iSCSI block | |
441 | * device, so network device and its ancestor should | |
442 | * be marked as memalloc_noio too. | |
443 | */ | |
444 | noio_flag = memalloc_noio_save(); | |
445 | retval = __rpm_callback(cb, dev); | |
446 | memalloc_noio_restore(noio_flag); | |
447 | } else { | |
448 | retval = __rpm_callback(cb, dev); | |
449 | } | |
450 | ||
72263869 RW |
451 | /* |
452 | * Since -EACCES means that runtime PM is disabled for the given device, | |
453 | * it should not be returned by runtime PM callbacks. If it is returned | |
454 | * nevertheless, assume it to be a transient error and convert it to | |
455 | * -EAGAIN. | |
456 | */ | |
457 | if (retval == -EACCES) | |
458 | retval = -EAGAIN; | |
459 | ||
460 | if (retval != -EAGAIN && retval != -EBUSY) | |
461 | dev->power.runtime_error = retval; | |
462 | ||
463 | return retval; | |
0307f4e8 RW |
464 | } |
465 | ||
1bfee5bc | 466 | /** |
140a6c94 | 467 | * rpm_idle - Notify device bus type if the device can be suspended. |
1bfee5bc AS |
468 | * @dev: Device to notify the bus type about. |
469 | * @rpmflags: Flag bits. | |
470 | * | |
62052ab1 | 471 | * Check if the device's runtime PM status allows it to be suspended. If |
1bfee5bc AS |
472 | * another idle notification has been started earlier, return immediately. If |
473 | * the RPM_ASYNC flag is set then queue an idle-notification request; otherwise | |
d66e6db2 UH |
474 | * run the ->runtime_idle() callback directly. If the ->runtime_idle callback |
475 | * doesn't exist or if it returns 0, call rpm_suspend with the RPM_AUTO flag. | |
1bfee5bc AS |
476 | * |
477 | * This function must be called under dev->power.lock with interrupts disabled. | |
478 | */ | |
140a6c94 | 479 | static int rpm_idle(struct device *dev, int rpmflags) |
1bfee5bc | 480 | { |
71c63122 | 481 | int (*callback)(struct device *); |
1bfee5bc AS |
482 | int retval; |
483 | ||
db8f5086 | 484 | trace_rpm_idle(dev, rpmflags); |
1bfee5bc AS |
485 | retval = rpm_check_suspend_allowed(dev); |
486 | if (retval < 0) | |
487 | ; /* Conditions are wrong. */ | |
488 | ||
489 | /* Idle notifications are allowed only in the RPM_ACTIVE state. */ | |
490 | else if (dev->power.runtime_status != RPM_ACTIVE) | |
491 | retval = -EAGAIN; | |
492 | ||
493 | /* | |
494 | * Any pending request other than an idle notification takes | |
495 | * precedence over us, except that the timer may be running. | |
496 | */ | |
497 | else if (dev->power.request_pending && | |
498 | dev->power.request > RPM_REQ_IDLE) | |
499 | retval = -EAGAIN; | |
500 | ||
501 | /* Act as though RPM_NOWAIT is always set. */ | |
502 | else if (dev->power.idle_notification) | |
503 | retval = -EINPROGRESS; | |
dbfa4478 | 504 | |
5e928f77 RW |
505 | if (retval) |
506 | goto out; | |
507 | ||
1bfee5bc AS |
508 | /* Pending requests need to be canceled. */ |
509 | dev->power.request = RPM_REQ_NONE; | |
510 | ||
5a2bd1b1 UH |
511 | callback = RPM_GET_CALLBACK(dev, runtime_idle); |
512 | ||
513 | /* If no callback assume success. */ | |
514 | if (!callback || dev->power.no_callbacks) | |
7490e442 | 515 | goto out; |
7490e442 | 516 | |
1bfee5bc AS |
517 | /* Carry out an asynchronous or a synchronous idle notification. */ |
518 | if (rpmflags & RPM_ASYNC) { | |
519 | dev->power.request = RPM_REQ_IDLE; | |
520 | if (!dev->power.request_pending) { | |
521 | dev->power.request_pending = true; | |
522 | queue_work(pm_wq, &dev->power.work); | |
5e928f77 | 523 | } |
db8f5086 | 524 | trace_rpm_return_int(dev, _THIS_IP_, 0); |
45f0a85c | 525 | return 0; |
5e928f77 RW |
526 | } |
527 | ||
528 | dev->power.idle_notification = true; | |
529 | ||
bc80c2e4 RW |
530 | if (dev->power.irq_safe) |
531 | spin_unlock(&dev->power.lock); | |
532 | else | |
533 | spin_unlock_irq(&dev->power.lock); | |
534 | ||
535 | retval = callback(dev); | |
536 | ||
537 | if (dev->power.irq_safe) | |
538 | spin_lock(&dev->power.lock); | |
539 | else | |
540 | spin_lock_irq(&dev->power.lock); | |
5e928f77 RW |
541 | |
542 | dev->power.idle_notification = false; | |
543 | wake_up_all(&dev->power.wait_queue); | |
544 | ||
545 | out: | |
db8f5086 | 546 | trace_rpm_return_int(dev, _THIS_IP_, retval); |
d66e6db2 | 547 | return retval ? retval : rpm_suspend(dev, rpmflags | RPM_AUTO); |
5e928f77 RW |
548 | } |
549 | ||
550 | /** | |
62052ab1 | 551 | * rpm_suspend - Carry out runtime suspend of given device. |
5e928f77 | 552 | * @dev: Device to suspend. |
3f9af051 | 553 | * @rpmflags: Flag bits. |
5e928f77 | 554 | * |
47d8f0ba ML |
555 | * Check if the device's runtime PM status allows it to be suspended. |
556 | * Cancel a pending idle notification, autosuspend or suspend. If | |
557 | * another suspend has been started earlier, either return immediately | |
558 | * or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC | |
559 | * flags. If the RPM_ASYNC flag is set then queue a suspend request; | |
857b36c7 ML |
560 | * otherwise run the ->runtime_suspend() callback directly. When |
561 | * ->runtime_suspend succeeded, if a deferred resume was requested while | |
562 | * the callback was running then carry it out, otherwise send an idle | |
563 | * notification for its parent (if the suspend succeeded and both | |
564 | * ignore_children of parent->power and irq_safe of dev->power are not set). | |
886486b7 AS |
565 | * If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO |
566 | * flag is set and the next autosuspend-delay expiration time is in the | |
567 | * future, schedule another autosuspend attempt. | |
5e928f77 RW |
568 | * |
569 | * This function must be called under dev->power.lock with interrupts disabled. | |
570 | */ | |
140a6c94 | 571 | static int rpm_suspend(struct device *dev, int rpmflags) |
5e928f77 RW |
572 | __releases(&dev->power.lock) __acquires(&dev->power.lock) |
573 | { | |
71c63122 | 574 | int (*callback)(struct device *); |
5e928f77 | 575 | struct device *parent = NULL; |
1bfee5bc | 576 | int retval; |
5e928f77 | 577 | |
db8f5086 | 578 | trace_rpm_suspend(dev, rpmflags); |
5e928f77 RW |
579 | |
580 | repeat: | |
1bfee5bc | 581 | retval = rpm_check_suspend_allowed(dev); |
1bfee5bc | 582 | if (retval < 0) |
3618bbaa | 583 | goto out; /* Conditions are wrong. */ |
1bfee5bc AS |
584 | |
585 | /* Synchronous suspends are not allowed in the RPM_RESUMING state. */ | |
3618bbaa | 586 | if (dev->power.runtime_status == RPM_RESUMING && !(rpmflags & RPM_ASYNC)) |
5e928f77 | 587 | retval = -EAGAIN; |
dbfa4478 | 588 | |
1bfee5bc | 589 | if (retval) |
5e928f77 | 590 | goto out; |
5e928f77 | 591 | |
15bcb91d | 592 | /* If the autosuspend_delay time hasn't expired yet, reschedule. */ |
dbfa4478 | 593 | if ((rpmflags & RPM_AUTO) && dev->power.runtime_status != RPM_SUSPENDING) { |
8234f673 | 594 | u64 expires = pm_runtime_autosuspend_expiration(dev); |
15bcb91d AS |
595 | |
596 | if (expires != 0) { | |
597 | /* Pending requests need to be canceled. */ | |
598 | dev->power.request = RPM_REQ_NONE; | |
599 | ||
600 | /* | |
601 | * Optimization: If the timer is already running and is | |
602 | * set to expire at or before the autosuspend delay, | |
603 | * avoid the overhead of resetting it. Just let it | |
604 | * expire; pm_suspend_timer_fn() will take care of the | |
605 | * rest. | |
606 | */ | |
8234f673 | 607 | if (!(dev->power.timer_expires && |
dbfa4478 | 608 | dev->power.timer_expires <= expires)) { |
8234f673 VG |
609 | /* |
610 | * We add a slack of 25% to gather wakeups | |
611 | * without sacrificing the granularity. | |
612 | */ | |
ca27e4cd | 613 | u64 slack = (u64)READ_ONCE(dev->power.autosuspend_delay) * |
8234f673 VG |
614 | (NSEC_PER_MSEC >> 2); |
615 | ||
15bcb91d | 616 | dev->power.timer_expires = expires; |
8234f673 | 617 | hrtimer_start_range_ns(&dev->power.suspend_timer, |
dbfa4478 RW |
618 | ns_to_ktime(expires), |
619 | slack, | |
620 | HRTIMER_MODE_ABS); | |
15bcb91d AS |
621 | } |
622 | dev->power.timer_autosuspends = 1; | |
623 | goto out; | |
624 | } | |
625 | } | |
626 | ||
5e928f77 RW |
627 | /* Other scheduled or pending requests need to be canceled. */ |
628 | pm_runtime_cancel_pending(dev); | |
629 | ||
5e928f77 RW |
630 | if (dev->power.runtime_status == RPM_SUSPENDING) { |
631 | DEFINE_WAIT(wait); | |
632 | ||
1bfee5bc | 633 | if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) { |
5e928f77 RW |
634 | retval = -EINPROGRESS; |
635 | goto out; | |
636 | } | |
637 | ||
ad3c36a5 RW |
638 | if (dev->power.irq_safe) { |
639 | spin_unlock(&dev->power.lock); | |
640 | ||
641 | cpu_relax(); | |
642 | ||
643 | spin_lock(&dev->power.lock); | |
644 | goto repeat; | |
645 | } | |
646 | ||
5e928f77 RW |
647 | /* Wait for the other suspend running in parallel with us. */ |
648 | for (;;) { | |
649 | prepare_to_wait(&dev->power.wait_queue, &wait, | |
650 | TASK_UNINTERRUPTIBLE); | |
651 | if (dev->power.runtime_status != RPM_SUSPENDING) | |
652 | break; | |
653 | ||
654 | spin_unlock_irq(&dev->power.lock); | |
655 | ||
656 | schedule(); | |
657 | ||
658 | spin_lock_irq(&dev->power.lock); | |
659 | } | |
660 | finish_wait(&dev->power.wait_queue, &wait); | |
661 | goto repeat; | |
662 | } | |
663 | ||
7490e442 AS |
664 | if (dev->power.no_callbacks) |
665 | goto no_callback; /* Assume success. */ | |
666 | ||
1bfee5bc AS |
667 | /* Carry out an asynchronous or a synchronous suspend. */ |
668 | if (rpmflags & RPM_ASYNC) { | |
15bcb91d AS |
669 | dev->power.request = (rpmflags & RPM_AUTO) ? |
670 | RPM_REQ_AUTOSUSPEND : RPM_REQ_SUSPEND; | |
1bfee5bc AS |
671 | if (!dev->power.request_pending) { |
672 | dev->power.request_pending = true; | |
673 | queue_work(pm_wq, &dev->power.work); | |
674 | } | |
675 | goto out; | |
676 | } | |
677 | ||
8d4b9d1b | 678 | __update_runtime_status(dev, RPM_SUSPENDING); |
5e928f77 | 679 | |
dbcd2d72 | 680 | callback = RPM_GET_CALLBACK(dev, runtime_suspend); |
35cd133c | 681 | |
bed57030 | 682 | dev_pm_enable_wake_irq_check(dev, true); |
71c63122 | 683 | retval = rpm_callback(callback, dev); |
00dc9ad1 RW |
684 | if (retval) |
685 | goto fail; | |
886486b7 | 686 | |
25971410 CY |
687 | dev_pm_enable_wake_irq_complete(dev); |
688 | ||
7490e442 | 689 | no_callback: |
857b36c7 ML |
690 | __update_runtime_status(dev, RPM_SUSPENDED); |
691 | pm_runtime_deactivate_timer(dev); | |
5e928f77 | 692 | |
857b36c7 ML |
693 | if (dev->parent) { |
694 | parent = dev->parent; | |
695 | atomic_add_unless(&parent->power.child_count, -1, 0); | |
5e928f77 RW |
696 | } |
697 | wake_up_all(&dev->power.wait_queue); | |
698 | ||
699 | if (dev->power.deferred_resume) { | |
58a34de7 | 700 | dev->power.deferred_resume = false; |
140a6c94 | 701 | rpm_resume(dev, 0); |
5e928f77 RW |
702 | retval = -EAGAIN; |
703 | goto out; | |
704 | } | |
705 | ||
5244f5e2 RW |
706 | if (dev->power.irq_safe) |
707 | goto out; | |
708 | ||
c3810c88 | 709 | /* Maybe the parent is now able to suspend. */ |
5244f5e2 | 710 | if (parent && !parent->power.ignore_children) { |
c3810c88 | 711 | spin_unlock(&dev->power.lock); |
5e928f77 | 712 | |
c3810c88 AS |
713 | spin_lock(&parent->power.lock); |
714 | rpm_idle(parent, RPM_ASYNC); | |
715 | spin_unlock(&parent->power.lock); | |
5e928f77 | 716 | |
c3810c88 | 717 | spin_lock(&dev->power.lock); |
5e928f77 | 718 | } |
5244f5e2 RW |
719 | /* Maybe the suppliers are now able to suspend. */ |
720 | if (dev->power.links_count > 0) { | |
721 | spin_unlock_irq(&dev->power.lock); | |
722 | ||
723 | rpm_suspend_suppliers(dev); | |
724 | ||
725 | spin_lock_irq(&dev->power.lock); | |
726 | } | |
5e928f77 RW |
727 | |
728 | out: | |
db8f5086 | 729 | trace_rpm_return_int(dev, _THIS_IP_, retval); |
5e928f77 RW |
730 | |
731 | return retval; | |
00dc9ad1 RW |
732 | |
733 | fail: | |
25971410 | 734 | dev_pm_disable_wake_irq_check(dev, true); |
00dc9ad1 | 735 | __update_runtime_status(dev, RPM_ACTIVE); |
00dc9ad1 | 736 | dev->power.deferred_resume = false; |
f2791d73 AS |
737 | wake_up_all(&dev->power.wait_queue); |
738 | ||
72263869 RW |
739 | /* |
740 | * On transient errors, if the callback routine failed an autosuspend, | |
741 | * and if the last_busy time has been updated so that there is a new | |
742 | * autosuspend expiration time, automatically reschedule another | |
743 | * autosuspend. | |
744 | */ | |
745 | if (!dev->power.runtime_error && (rpmflags & RPM_AUTO) && | |
746 | pm_runtime_autosuspend_expiration(dev) != 0) | |
747 | goto repeat; | |
748 | ||
749 | pm_runtime_cancel_pending(dev); | |
00dc9ad1 | 750 | |
00dc9ad1 | 751 | goto out; |
5e928f77 RW |
752 | } |
753 | ||
754 | /** | |
62052ab1 | 755 | * rpm_resume - Carry out runtime resume of given device. |
5e928f77 | 756 | * @dev: Device to resume. |
3f9af051 | 757 | * @rpmflags: Flag bits. |
5e928f77 | 758 | * |
62052ab1 | 759 | * Check if the device's runtime PM status allows it to be resumed. Cancel |
1bfee5bc | 760 | * any scheduled or pending requests. If another resume has been started |
25985edc | 761 | * earlier, either return immediately or wait for it to finish, depending on the |
1bfee5bc AS |
762 | * RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in |
763 | * parallel with this function, either tell the other process to resume after | |
764 | * suspending (deferred_resume) or wait for it to finish. If the RPM_ASYNC | |
765 | * flag is set then queue a resume request; otherwise run the | |
766 | * ->runtime_resume() callback directly. Queue an idle notification for the | |
767 | * device if the resume succeeded. | |
5e928f77 RW |
768 | * |
769 | * This function must be called under dev->power.lock with interrupts disabled. | |
770 | */ | |
140a6c94 | 771 | static int rpm_resume(struct device *dev, int rpmflags) |
5e928f77 RW |
772 | __releases(&dev->power.lock) __acquires(&dev->power.lock) |
773 | { | |
71c63122 | 774 | int (*callback)(struct device *); |
5e928f77 RW |
775 | struct device *parent = NULL; |
776 | int retval = 0; | |
777 | ||
db8f5086 | 778 | trace_rpm_resume(dev, rpmflags); |
5e928f77 RW |
779 | |
780 | repeat: | |
c24efa67 | 781 | if (dev->power.runtime_error) { |
5e928f77 | 782 | retval = -EINVAL; |
c24efa67 RW |
783 | } else if (dev->power.disable_depth > 0) { |
784 | if (dev->power.runtime_status == RPM_ACTIVE && | |
785 | dev->power.last_status == RPM_ACTIVE) | |
786 | retval = 1; | |
787 | else | |
788 | retval = -EACCES; | |
789 | } | |
1bfee5bc | 790 | if (retval) |
5e928f77 | 791 | goto out; |
5e928f77 | 792 | |
15bcb91d AS |
793 | /* |
794 | * Other scheduled or pending requests need to be canceled. Small | |
795 | * optimization: If an autosuspend timer is running, leave it running | |
796 | * rather than cancelling it now only to restart it again in the near | |
797 | * future. | |
798 | */ | |
799 | dev->power.request = RPM_REQ_NONE; | |
800 | if (!dev->power.timer_autosuspends) | |
801 | pm_runtime_deactivate_timer(dev); | |
5e928f77 | 802 | |
1bfee5bc | 803 | if (dev->power.runtime_status == RPM_ACTIVE) { |
5e928f77 | 804 | retval = 1; |
5e928f77 | 805 | goto out; |
1bfee5bc | 806 | } |
5e928f77 | 807 | |
dbfa4478 RW |
808 | if (dev->power.runtime_status == RPM_RESUMING || |
809 | dev->power.runtime_status == RPM_SUSPENDING) { | |
5e928f77 RW |
810 | DEFINE_WAIT(wait); |
811 | ||
1bfee5bc | 812 | if (rpmflags & (RPM_ASYNC | RPM_NOWAIT)) { |
e66332a4 | 813 | if (dev->power.runtime_status == RPM_SUSPENDING) { |
5e928f77 | 814 | dev->power.deferred_resume = true; |
e66332a4 RW |
815 | if (rpmflags & RPM_NOWAIT) |
816 | retval = -EINPROGRESS; | |
817 | } else { | |
1bfee5bc | 818 | retval = -EINPROGRESS; |
e66332a4 | 819 | } |
5e928f77 RW |
820 | goto out; |
821 | } | |
822 | ||
ad3c36a5 RW |
823 | if (dev->power.irq_safe) { |
824 | spin_unlock(&dev->power.lock); | |
825 | ||
826 | cpu_relax(); | |
827 | ||
828 | spin_lock(&dev->power.lock); | |
829 | goto repeat; | |
830 | } | |
831 | ||
5e928f77 RW |
832 | /* Wait for the operation carried out in parallel with us. */ |
833 | for (;;) { | |
834 | prepare_to_wait(&dev->power.wait_queue, &wait, | |
835 | TASK_UNINTERRUPTIBLE); | |
dbfa4478 RW |
836 | if (dev->power.runtime_status != RPM_RESUMING && |
837 | dev->power.runtime_status != RPM_SUSPENDING) | |
5e928f77 RW |
838 | break; |
839 | ||
840 | spin_unlock_irq(&dev->power.lock); | |
841 | ||
842 | schedule(); | |
843 | ||
844 | spin_lock_irq(&dev->power.lock); | |
845 | } | |
846 | finish_wait(&dev->power.wait_queue, &wait); | |
847 | goto repeat; | |
848 | } | |
849 | ||
7490e442 AS |
850 | /* |
851 | * See if we can skip waking up the parent. This is safe only if | |
852 | * power.no_callbacks is set, because otherwise we don't know whether | |
853 | * the resume will actually succeed. | |
854 | */ | |
855 | if (dev->power.no_callbacks && !parent && dev->parent) { | |
d63be5f9 | 856 | spin_lock_nested(&dev->parent->power.lock, SINGLE_DEPTH_NESTING); |
dbfa4478 RW |
857 | if (dev->parent->power.disable_depth > 0 || |
858 | dev->parent->power.ignore_children || | |
859 | dev->parent->power.runtime_status == RPM_ACTIVE) { | |
7490e442 AS |
860 | atomic_inc(&dev->parent->power.child_count); |
861 | spin_unlock(&dev->parent->power.lock); | |
7f321c26 | 862 | retval = 1; |
7490e442 AS |
863 | goto no_callback; /* Assume success. */ |
864 | } | |
865 | spin_unlock(&dev->parent->power.lock); | |
866 | } | |
867 | ||
1bfee5bc AS |
868 | /* Carry out an asynchronous or a synchronous resume. */ |
869 | if (rpmflags & RPM_ASYNC) { | |
870 | dev->power.request = RPM_REQ_RESUME; | |
871 | if (!dev->power.request_pending) { | |
872 | dev->power.request_pending = true; | |
873 | queue_work(pm_wq, &dev->power.work); | |
874 | } | |
875 | retval = 0; | |
876 | goto out; | |
877 | } | |
878 | ||
5e928f77 RW |
879 | if (!parent && dev->parent) { |
880 | /* | |
c7b61de5 AS |
881 | * Increment the parent's usage counter and resume it if |
882 | * necessary. Not needed if dev is irq-safe; then the | |
883 | * parent is permanently resumed. | |
5e928f77 RW |
884 | */ |
885 | parent = dev->parent; | |
c7b61de5 AS |
886 | if (dev->power.irq_safe) |
887 | goto skip_parent; | |
dbfa4478 | 888 | |
862f89b3 | 889 | spin_unlock(&dev->power.lock); |
5e928f77 RW |
890 | |
891 | pm_runtime_get_noresume(parent); | |
892 | ||
862f89b3 | 893 | spin_lock(&parent->power.lock); |
5e928f77 | 894 | /* |
216ef0b6 UH |
895 | * Resume the parent if it has runtime PM enabled and not been |
896 | * set to ignore its children. | |
5e928f77 | 897 | */ |
dbfa4478 RW |
898 | if (!parent->power.disable_depth && |
899 | !parent->power.ignore_children) { | |
140a6c94 | 900 | rpm_resume(parent, 0); |
5e928f77 RW |
901 | if (parent->power.runtime_status != RPM_ACTIVE) |
902 | retval = -EBUSY; | |
903 | } | |
862f89b3 | 904 | spin_unlock(&parent->power.lock); |
5e928f77 | 905 | |
862f89b3 | 906 | spin_lock(&dev->power.lock); |
5e928f77 RW |
907 | if (retval) |
908 | goto out; | |
dbfa4478 | 909 | |
5e928f77 RW |
910 | goto repeat; |
911 | } | |
c7b61de5 | 912 | skip_parent: |
5e928f77 | 913 | |
7490e442 AS |
914 | if (dev->power.no_callbacks) |
915 | goto no_callback; /* Assume success. */ | |
916 | ||
8d4b9d1b | 917 | __update_runtime_status(dev, RPM_RESUMING); |
5e928f77 | 918 | |
dbcd2d72 | 919 | callback = RPM_GET_CALLBACK(dev, runtime_resume); |
35cd133c | 920 | |
25971410 | 921 | dev_pm_disable_wake_irq_check(dev, false); |
71c63122 | 922 | retval = rpm_callback(callback, dev); |
5e928f77 | 923 | if (retval) { |
8d4b9d1b | 924 | __update_runtime_status(dev, RPM_SUSPENDED); |
5e928f77 | 925 | pm_runtime_cancel_pending(dev); |
bed57030 | 926 | dev_pm_enable_wake_irq_check(dev, false); |
5e928f77 | 927 | } else { |
7490e442 | 928 | no_callback: |
8d4b9d1b | 929 | __update_runtime_status(dev, RPM_ACTIVE); |
56f487c7 | 930 | pm_runtime_mark_last_busy(dev); |
5e928f77 RW |
931 | if (parent) |
932 | atomic_inc(&parent->power.child_count); | |
933 | } | |
934 | wake_up_all(&dev->power.wait_queue); | |
935 | ||
7f321c26 | 936 | if (retval >= 0) |
140a6c94 | 937 | rpm_idle(dev, RPM_ASYNC); |
5e928f77 RW |
938 | |
939 | out: | |
c7b61de5 | 940 | if (parent && !dev->power.irq_safe) { |
5e928f77 RW |
941 | spin_unlock_irq(&dev->power.lock); |
942 | ||
943 | pm_runtime_put(parent); | |
944 | ||
945 | spin_lock_irq(&dev->power.lock); | |
946 | } | |
947 | ||
db8f5086 | 948 | trace_rpm_return_int(dev, _THIS_IP_, retval); |
5e928f77 RW |
949 | |
950 | return retval; | |
951 | } | |
952 | ||
5e928f77 | 953 | /** |
62052ab1 | 954 | * pm_runtime_work - Universal runtime PM work function. |
5e928f77 RW |
955 | * @work: Work structure used for scheduling the execution of this function. |
956 | * | |
957 | * Use @work to get the device object the work is to be done for, determine what | |
62052ab1 | 958 | * is to be done and execute the appropriate runtime PM function. |
5e928f77 RW |
959 | */ |
960 | static void pm_runtime_work(struct work_struct *work) | |
961 | { | |
962 | struct device *dev = container_of(work, struct device, power.work); | |
963 | enum rpm_request req; | |
964 | ||
965 | spin_lock_irq(&dev->power.lock); | |
966 | ||
967 | if (!dev->power.request_pending) | |
968 | goto out; | |
969 | ||
970 | req = dev->power.request; | |
971 | dev->power.request = RPM_REQ_NONE; | |
972 | dev->power.request_pending = false; | |
973 | ||
974 | switch (req) { | |
975 | case RPM_REQ_NONE: | |
976 | break; | |
977 | case RPM_REQ_IDLE: | |
140a6c94 | 978 | rpm_idle(dev, RPM_NOWAIT); |
5e928f77 RW |
979 | break; |
980 | case RPM_REQ_SUSPEND: | |
140a6c94 | 981 | rpm_suspend(dev, RPM_NOWAIT); |
5e928f77 | 982 | break; |
15bcb91d AS |
983 | case RPM_REQ_AUTOSUSPEND: |
984 | rpm_suspend(dev, RPM_NOWAIT | RPM_AUTO); | |
985 | break; | |
5e928f77 | 986 | case RPM_REQ_RESUME: |
140a6c94 | 987 | rpm_resume(dev, RPM_NOWAIT); |
5e928f77 RW |
988 | break; |
989 | } | |
990 | ||
991 | out: | |
992 | spin_unlock_irq(&dev->power.lock); | |
993 | } | |
994 | ||
5e928f77 RW |
995 | /** |
996 | * pm_suspend_timer_fn - Timer function for pm_schedule_suspend(). | |
12c0632b | 997 | * @timer: hrtimer used by pm_schedule_suspend(). |
5e928f77 | 998 | * |
1bfee5bc | 999 | * Check if the time is right and queue a suspend request. |
5e928f77 | 1000 | */ |
8234f673 | 1001 | static enum hrtimer_restart pm_suspend_timer_fn(struct hrtimer *timer) |
5e928f77 | 1002 | { |
8234f673 | 1003 | struct device *dev = container_of(timer, struct device, power.suspend_timer); |
5e928f77 | 1004 | unsigned long flags; |
8234f673 | 1005 | u64 expires; |
5e928f77 RW |
1006 | |
1007 | spin_lock_irqsave(&dev->power.lock, flags); | |
1008 | ||
1009 | expires = dev->power.timer_expires; | |
1f7b7081 LM |
1010 | /* |
1011 | * If 'expires' is after the current time, we've been called | |
1012 | * too early. | |
1013 | */ | |
40d3b40d | 1014 | if (expires > 0 && expires <= ktime_get_mono_fast_ns()) { |
5e928f77 | 1015 | dev->power.timer_expires = 0; |
15bcb91d AS |
1016 | rpm_suspend(dev, dev->power.timer_autosuspends ? |
1017 | (RPM_ASYNC | RPM_AUTO) : RPM_ASYNC); | |
5e928f77 RW |
1018 | } |
1019 | ||
1020 | spin_unlock_irqrestore(&dev->power.lock, flags); | |
8234f673 VG |
1021 | |
1022 | return HRTIMER_NORESTART; | |
5e928f77 RW |
1023 | } |
1024 | ||
1025 | /** | |
1026 | * pm_schedule_suspend - Set up a timer to submit a suspend request in future. | |
1027 | * @dev: Device to suspend. | |
1028 | * @delay: Time to wait before submitting a suspend request, in milliseconds. | |
1029 | */ | |
1030 | int pm_schedule_suspend(struct device *dev, unsigned int delay) | |
1031 | { | |
1032 | unsigned long flags; | |
15efb47d | 1033 | u64 expires; |
1bfee5bc | 1034 | int retval; |
5e928f77 RW |
1035 | |
1036 | spin_lock_irqsave(&dev->power.lock, flags); | |
1037 | ||
5e928f77 | 1038 | if (!delay) { |
140a6c94 | 1039 | retval = rpm_suspend(dev, RPM_ASYNC); |
5e928f77 RW |
1040 | goto out; |
1041 | } | |
1042 | ||
1bfee5bc | 1043 | retval = rpm_check_suspend_allowed(dev); |
5e928f77 RW |
1044 | if (retval) |
1045 | goto out; | |
1046 | ||
1bfee5bc AS |
1047 | /* Other scheduled or pending requests need to be canceled. */ |
1048 | pm_runtime_cancel_pending(dev); | |
1049 | ||
15efb47d VG |
1050 | expires = ktime_get_mono_fast_ns() + (u64)delay * NSEC_PER_MSEC; |
1051 | dev->power.timer_expires = expires; | |
15bcb91d | 1052 | dev->power.timer_autosuspends = 0; |
8234f673 | 1053 | hrtimer_start(&dev->power.suspend_timer, expires, HRTIMER_MODE_ABS); |
5e928f77 RW |
1054 | |
1055 | out: | |
1056 | spin_unlock_irqrestore(&dev->power.lock, flags); | |
1057 | ||
1058 | return retval; | |
1059 | } | |
1060 | EXPORT_SYMBOL_GPL(pm_schedule_suspend); | |
1061 | ||
82586a72 RW |
1062 | static int rpm_drop_usage_count(struct device *dev) |
1063 | { | |
1064 | int ret; | |
1065 | ||
1066 | ret = atomic_sub_return(1, &dev->power.usage_count); | |
1067 | if (ret >= 0) | |
1068 | return ret; | |
1069 | ||
1070 | /* | |
1071 | * Because rpm_resume() does not check the usage counter, it will resume | |
1072 | * the device even if the usage counter is 0 or negative, so it is | |
1073 | * sufficient to increment the usage counter here to reverse the change | |
1074 | * made above. | |
1075 | */ | |
1076 | atomic_inc(&dev->power.usage_count); | |
1077 | dev_warn(dev, "Runtime PM usage count underflow!\n"); | |
1078 | return -EINVAL; | |
1079 | } | |
1080 | ||
5e928f77 | 1081 | /** |
62052ab1 | 1082 | * __pm_runtime_idle - Entry point for runtime idle operations. |
140a6c94 AS |
1083 | * @dev: Device to send idle notification for. |
1084 | * @rpmflags: Flag bits. | |
1085 | * | |
1086 | * If the RPM_GET_PUT flag is set, decrement the device's usage count and | |
82586a72 RW |
1087 | * return immediately if it is larger than zero (if it becomes negative, log a |
1088 | * warning, increment it, and return an error). Then carry out an idle | |
140a6c94 AS |
1089 | * notification, either synchronous or asynchronous. |
1090 | * | |
311aab73 CC |
1091 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
1092 | * or if pm_runtime_irq_safe() has been called. | |
5e928f77 | 1093 | */ |
140a6c94 | 1094 | int __pm_runtime_idle(struct device *dev, int rpmflags) |
5e928f77 RW |
1095 | { |
1096 | unsigned long flags; | |
1097 | int retval; | |
1098 | ||
140a6c94 | 1099 | if (rpmflags & RPM_GET_PUT) { |
82586a72 RW |
1100 | retval = rpm_drop_usage_count(dev); |
1101 | if (retval < 0) { | |
1102 | return retval; | |
1103 | } else if (retval > 0) { | |
db8f5086 | 1104 | trace_rpm_usage(dev, rpmflags); |
140a6c94 | 1105 | return 0; |
d2292906 | 1106 | } |
140a6c94 AS |
1107 | } |
1108 | ||
a9306a63 RW |
1109 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); |
1110 | ||
5e928f77 | 1111 | spin_lock_irqsave(&dev->power.lock, flags); |
140a6c94 | 1112 | retval = rpm_idle(dev, rpmflags); |
5e928f77 RW |
1113 | spin_unlock_irqrestore(&dev->power.lock, flags); |
1114 | ||
1115 | return retval; | |
1116 | } | |
140a6c94 | 1117 | EXPORT_SYMBOL_GPL(__pm_runtime_idle); |
5e928f77 RW |
1118 | |
1119 | /** | |
62052ab1 | 1120 | * __pm_runtime_suspend - Entry point for runtime put/suspend operations. |
140a6c94 | 1121 | * @dev: Device to suspend. |
3f9af051 | 1122 | * @rpmflags: Flag bits. |
5e928f77 | 1123 | * |
15bcb91d | 1124 | * If the RPM_GET_PUT flag is set, decrement the device's usage count and |
82586a72 RW |
1125 | * return immediately if it is larger than zero (if it becomes negative, log a |
1126 | * warning, increment it, and return an error). Then carry out a suspend, | |
15bcb91d | 1127 | * either synchronous or asynchronous. |
140a6c94 | 1128 | * |
311aab73 CC |
1129 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
1130 | * or if pm_runtime_irq_safe() has been called. | |
5e928f77 | 1131 | */ |
140a6c94 | 1132 | int __pm_runtime_suspend(struct device *dev, int rpmflags) |
5e928f77 | 1133 | { |
140a6c94 | 1134 | unsigned long flags; |
1d531c14 | 1135 | int retval; |
5e928f77 | 1136 | |
15bcb91d | 1137 | if (rpmflags & RPM_GET_PUT) { |
82586a72 RW |
1138 | retval = rpm_drop_usage_count(dev); |
1139 | if (retval < 0) { | |
1140 | return retval; | |
1141 | } else if (retval > 0) { | |
db8f5086 | 1142 | trace_rpm_usage(dev, rpmflags); |
15bcb91d | 1143 | return 0; |
d2292906 | 1144 | } |
15bcb91d AS |
1145 | } |
1146 | ||
a9306a63 RW |
1147 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe); |
1148 | ||
140a6c94 AS |
1149 | spin_lock_irqsave(&dev->power.lock, flags); |
1150 | retval = rpm_suspend(dev, rpmflags); | |
1151 | spin_unlock_irqrestore(&dev->power.lock, flags); | |
5e928f77 RW |
1152 | |
1153 | return retval; | |
1154 | } | |
140a6c94 | 1155 | EXPORT_SYMBOL_GPL(__pm_runtime_suspend); |
5e928f77 RW |
1156 | |
1157 | /** | |
62052ab1 | 1158 | * __pm_runtime_resume - Entry point for runtime resume operations. |
140a6c94 | 1159 | * @dev: Device to resume. |
3f9af051 | 1160 | * @rpmflags: Flag bits. |
5e928f77 | 1161 | * |
140a6c94 AS |
1162 | * If the RPM_GET_PUT flag is set, increment the device's usage count. Then |
1163 | * carry out a resume, either synchronous or asynchronous. | |
1164 | * | |
311aab73 CC |
1165 | * This routine may be called in atomic context if the RPM_ASYNC flag is set, |
1166 | * or if pm_runtime_irq_safe() has been called. | |
5e928f77 | 1167 | */ |
140a6c94 | 1168 | int __pm_runtime_resume(struct device *dev, int rpmflags) |
5e928f77 | 1169 | { |
140a6c94 AS |
1170 | unsigned long flags; |
1171 | int retval; | |
5e928f77 | 1172 | |
a9306a63 RW |
1173 | might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe && |
1174 | dev->power.runtime_status != RPM_ACTIVE); | |
311aab73 | 1175 | |
140a6c94 AS |
1176 | if (rpmflags & RPM_GET_PUT) |
1177 | atomic_inc(&dev->power.usage_count); | |
1178 | ||
1179 | spin_lock_irqsave(&dev->power.lock, flags); | |
1180 | retval = rpm_resume(dev, rpmflags); | |
1181 | spin_unlock_irqrestore(&dev->power.lock, flags); | |
5e928f77 RW |
1182 | |
1183 | return retval; | |
1184 | } | |
140a6c94 | 1185 | EXPORT_SYMBOL_GPL(__pm_runtime_resume); |
5e928f77 | 1186 | |
a436b6a1 | 1187 | /** |
c0ef3df8 | 1188 | * pm_runtime_get_conditional - Conditionally bump up device usage counter. |
a436b6a1 | 1189 | * @dev: Device to handle. |
0abf803e | 1190 | * @ign_usage_count: Whether or not to look at the current usage counter value. |
a436b6a1 | 1191 | * |
0abf803e | 1192 | * Return -EINVAL if runtime PM is disabled for @dev. |
a436b6a1 | 1193 | * |
0abf803e RW |
1194 | * Otherwise, if the runtime PM status of @dev is %RPM_ACTIVE and either |
1195 | * @ign_usage_count is %true or the runtime PM usage counter of @dev is not | |
1196 | * zero, increment the usage counter of @dev and return 1. Otherwise, return 0 | |
1197 | * without changing the usage counter. | |
c111566b | 1198 | * |
0abf803e RW |
1199 | * If @ign_usage_count is %true, this function can be used to prevent suspending |
1200 | * the device when its runtime PM status is %RPM_ACTIVE. | |
c111566b | 1201 | * |
0abf803e RW |
1202 | * If @ign_usage_count is %false, this function can be used to prevent |
1203 | * suspending the device when both its runtime PM status is %RPM_ACTIVE and its | |
1204 | * runtime PM usage counter is not zero. | |
c111566b | 1205 | * |
10aa694e | 1206 | * The caller is responsible for decrementing the runtime PM usage counter of |
0abf803e | 1207 | * @dev after this function has returned a positive value for it. |
a436b6a1 | 1208 | */ |
c0ef3df8 | 1209 | static int pm_runtime_get_conditional(struct device *dev, bool ign_usage_count) |
a436b6a1 RW |
1210 | { |
1211 | unsigned long flags; | |
1212 | int retval; | |
1213 | ||
1214 | spin_lock_irqsave(&dev->power.lock, flags); | |
c111566b SA |
1215 | if (dev->power.disable_depth > 0) { |
1216 | retval = -EINVAL; | |
1217 | } else if (dev->power.runtime_status != RPM_ACTIVE) { | |
1218 | retval = 0; | |
1219 | } else if (ign_usage_count) { | |
1220 | retval = 1; | |
1221 | atomic_inc(&dev->power.usage_count); | |
1222 | } else { | |
1223 | retval = atomic_inc_not_zero(&dev->power.usage_count); | |
1224 | } | |
db8f5086 | 1225 | trace_rpm_usage(dev, 0); |
a436b6a1 | 1226 | spin_unlock_irqrestore(&dev->power.lock, flags); |
c111566b | 1227 | |
a436b6a1 RW |
1228 | return retval; |
1229 | } | |
c0ef3df8 SA |
1230 | |
1231 | /** | |
1232 | * pm_runtime_get_if_active - Bump up runtime PM usage counter if the device is | |
1233 | * in active state | |
1234 | * @dev: Target device. | |
1235 | * | |
1236 | * Increment the runtime PM usage counter of @dev if its runtime PM status is | |
1237 | * %RPM_ACTIVE, in which case it returns 1. If the device is in a different | |
1238 | * state, 0 is returned. -EINVAL is returned if runtime PM is disabled for the | |
1239 | * device, in which case also the usage_count will remain unmodified. | |
1240 | */ | |
1241 | int pm_runtime_get_if_active(struct device *dev) | |
1242 | { | |
1243 | return pm_runtime_get_conditional(dev, true); | |
1244 | } | |
c111566b | 1245 | EXPORT_SYMBOL_GPL(pm_runtime_get_if_active); |
a436b6a1 | 1246 | |
c0ef3df8 SA |
1247 | /** |
1248 | * pm_runtime_get_if_in_use - Conditionally bump up runtime PM usage counter. | |
1249 | * @dev: Target device. | |
1250 | * | |
1251 | * Increment the runtime PM usage counter of @dev if its runtime PM status is | |
1252 | * %RPM_ACTIVE and its runtime PM usage counter is greater than 0, in which case | |
1253 | * it returns 1. If the device is in a different state or its usage_count is 0, | |
1254 | * 0 is returned. -EINVAL is returned if runtime PM is disabled for the device, | |
1255 | * in which case also the usage_count will remain unmodified. | |
1256 | */ | |
1257 | int pm_runtime_get_if_in_use(struct device *dev) | |
1258 | { | |
1259 | return pm_runtime_get_conditional(dev, false); | |
1260 | } | |
1261 | EXPORT_SYMBOL_GPL(pm_runtime_get_if_in_use); | |
1262 | ||
5e928f77 | 1263 | /** |
62052ab1 | 1264 | * __pm_runtime_set_status - Set runtime PM status of a device. |
5e928f77 | 1265 | * @dev: Device to handle. |
62052ab1 | 1266 | * @status: New runtime PM status of the device. |
5e928f77 | 1267 | * |
62052ab1 | 1268 | * If runtime PM of the device is disabled or its power.runtime_error field is |
5e928f77 RW |
1269 | * different from zero, the status may be changed either to RPM_ACTIVE, or to |
1270 | * RPM_SUSPENDED, as long as that reflects the actual state of the device. | |
1271 | * However, if the device has a parent and the parent is not active, and the | |
1272 | * parent's power.ignore_children flag is unset, the device's status cannot be | |
1273 | * set to RPM_ACTIVE, so -EBUSY is returned in that case. | |
1274 | * | |
1275 | * If successful, __pm_runtime_set_status() clears the power.runtime_error field | |
1276 | * and the device parent's counter of unsuspended children is modified to | |
1277 | * reflect the new status. If the new status is RPM_SUSPENDED, an idle | |
1278 | * notification request for the parent is submitted. | |
4080ab08 RW |
1279 | * |
1280 | * If @dev has any suppliers (as reflected by device links to them), and @status | |
1281 | * is RPM_ACTIVE, they will be activated upfront and if the activation of one | |
1282 | * of them fails, the status of @dev will be changed to RPM_SUSPENDED (instead | |
1283 | * of the @status value) and the suppliers will be deacticated on exit. The | |
1284 | * error returned by the failing supplier activation will be returned in that | |
1285 | * case. | |
5e928f77 RW |
1286 | */ |
1287 | int __pm_runtime_set_status(struct device *dev, unsigned int status) | |
1288 | { | |
1289 | struct device *parent = dev->parent; | |
5e928f77 | 1290 | bool notify_parent = false; |
13966517 | 1291 | unsigned long flags; |
5e928f77 RW |
1292 | int error = 0; |
1293 | ||
1294 | if (status != RPM_ACTIVE && status != RPM_SUSPENDED) | |
1295 | return -EINVAL; | |
1296 | ||
13966517 | 1297 | spin_lock_irqsave(&dev->power.lock, flags); |
5e928f77 | 1298 | |
c1567f81 RW |
1299 | /* |
1300 | * Prevent PM-runtime from being enabled for the device or return an | |
1301 | * error if it is enabled already and working. | |
1302 | */ | |
1303 | if (dev->power.runtime_error || dev->power.disable_depth) | |
1304 | dev->power.disable_depth++; | |
1305 | else | |
5e928f77 | 1306 | error = -EAGAIN; |
c1567f81 | 1307 | |
13966517 | 1308 | spin_unlock_irqrestore(&dev->power.lock, flags); |
c1567f81 RW |
1309 | |
1310 | if (error) | |
1311 | return error; | |
1312 | ||
4080ab08 RW |
1313 | /* |
1314 | * If the new status is RPM_ACTIVE, the suppliers can be activated | |
1315 | * upfront regardless of the current status, because next time | |
1316 | * rpm_put_suppliers() runs, the rpm_active refcounts of the links | |
1317 | * involved will be dropped down to one anyway. | |
1318 | */ | |
1319 | if (status == RPM_ACTIVE) { | |
1320 | int idx = device_links_read_lock(); | |
1321 | ||
1322 | error = rpm_get_suppliers(dev); | |
1323 | if (error) | |
1324 | status = RPM_SUSPENDED; | |
1325 | ||
1326 | device_links_read_unlock(idx); | |
5e928f77 RW |
1327 | } |
1328 | ||
13966517 | 1329 | spin_lock_irqsave(&dev->power.lock, flags); |
5e928f77 | 1330 | |
f8817f61 | 1331 | if (dev->power.runtime_status == status || !parent) |
5e928f77 RW |
1332 | goto out_set; |
1333 | ||
1334 | if (status == RPM_SUSPENDED) { | |
f8817f61 RW |
1335 | atomic_add_unless(&parent->power.child_count, -1, 0); |
1336 | notify_parent = !parent->power.ignore_children; | |
1337 | } else { | |
bab636b9 | 1338 | spin_lock_nested(&parent->power.lock, SINGLE_DEPTH_NESTING); |
5e928f77 RW |
1339 | |
1340 | /* | |
1341 | * It is invalid to put an active child under a parent that is | |
62052ab1 | 1342 | * not active, has runtime PM enabled and the |
5e928f77 RW |
1343 | * 'power.ignore_children' flag unset. |
1344 | */ | |
dbfa4478 RW |
1345 | if (!parent->power.disable_depth && |
1346 | !parent->power.ignore_children && | |
1347 | parent->power.runtime_status != RPM_ACTIVE) { | |
71723f95 LW |
1348 | dev_err(dev, "runtime PM trying to activate child device %s but parent (%s) is not active\n", |
1349 | dev_name(dev), | |
1350 | dev_name(parent)); | |
5e928f77 | 1351 | error = -EBUSY; |
71723f95 | 1352 | } else if (dev->power.runtime_status == RPM_SUSPENDED) { |
965c4ac0 | 1353 | atomic_inc(&parent->power.child_count); |
71723f95 | 1354 | } |
5e928f77 | 1355 | |
862f89b3 | 1356 | spin_unlock(&parent->power.lock); |
5e928f77 | 1357 | |
4080ab08 RW |
1358 | if (error) { |
1359 | status = RPM_SUSPENDED; | |
5e928f77 | 1360 | goto out; |
4080ab08 | 1361 | } |
5e928f77 RW |
1362 | } |
1363 | ||
1364 | out_set: | |
8d4b9d1b | 1365 | __update_runtime_status(dev, status); |
4080ab08 RW |
1366 | if (!error) |
1367 | dev->power.runtime_error = 0; | |
1368 | ||
5e928f77 | 1369 | out: |
13966517 | 1370 | spin_unlock_irqrestore(&dev->power.lock, flags); |
5e928f77 RW |
1371 | |
1372 | if (notify_parent) | |
1373 | pm_request_idle(parent); | |
1374 | ||
4080ab08 RW |
1375 | if (status == RPM_SUSPENDED) { |
1376 | int idx = device_links_read_lock(); | |
1377 | ||
1378 | rpm_put_suppliers(dev); | |
1379 | ||
1380 | device_links_read_unlock(idx); | |
1381 | } | |
1382 | ||
c1567f81 RW |
1383 | pm_runtime_enable(dev); |
1384 | ||
5e928f77 RW |
1385 | return error; |
1386 | } | |
1387 | EXPORT_SYMBOL_GPL(__pm_runtime_set_status); | |
1388 | ||
1389 | /** | |
1390 | * __pm_runtime_barrier - Cancel pending requests and wait for completions. | |
1391 | * @dev: Device to handle. | |
1392 | * | |
1393 | * Flush all pending requests for the device from pm_wq and wait for all | |
62052ab1 | 1394 | * runtime PM operations involving the device in progress to complete. |
5e928f77 RW |
1395 | * |
1396 | * Should be called under dev->power.lock with interrupts disabled. | |
1397 | */ | |
1398 | static void __pm_runtime_barrier(struct device *dev) | |
1399 | { | |
1400 | pm_runtime_deactivate_timer(dev); | |
1401 | ||
1402 | if (dev->power.request_pending) { | |
1403 | dev->power.request = RPM_REQ_NONE; | |
1404 | spin_unlock_irq(&dev->power.lock); | |
1405 | ||
1406 | cancel_work_sync(&dev->power.work); | |
1407 | ||
1408 | spin_lock_irq(&dev->power.lock); | |
1409 | dev->power.request_pending = false; | |
1410 | } | |
1411 | ||
dbfa4478 RW |
1412 | if (dev->power.runtime_status == RPM_SUSPENDING || |
1413 | dev->power.runtime_status == RPM_RESUMING || | |
1414 | dev->power.idle_notification) { | |
5e928f77 RW |
1415 | DEFINE_WAIT(wait); |
1416 | ||
1417 | /* Suspend, wake-up or idle notification in progress. */ | |
1418 | for (;;) { | |
1419 | prepare_to_wait(&dev->power.wait_queue, &wait, | |
1420 | TASK_UNINTERRUPTIBLE); | |
1421 | if (dev->power.runtime_status != RPM_SUSPENDING | |
1422 | && dev->power.runtime_status != RPM_RESUMING | |
1423 | && !dev->power.idle_notification) | |
1424 | break; | |
1425 | spin_unlock_irq(&dev->power.lock); | |
1426 | ||
1427 | schedule(); | |
1428 | ||
1429 | spin_lock_irq(&dev->power.lock); | |
1430 | } | |
1431 | finish_wait(&dev->power.wait_queue, &wait); | |
1432 | } | |
1433 | } | |
1434 | ||
1435 | /** | |
1436 | * pm_runtime_barrier - Flush pending requests and wait for completions. | |
1437 | * @dev: Device to handle. | |
1438 | * | |
1439 | * Prevent the device from being suspended by incrementing its usage counter and | |
1440 | * if there's a pending resume request for the device, wake the device up. | |
1441 | * Next, make sure that all pending requests for the device have been flushed | |
62052ab1 | 1442 | * from pm_wq and wait for all runtime PM operations involving the device in |
5e928f77 RW |
1443 | * progress to complete. |
1444 | * | |
1445 | * Return value: | |
1446 | * 1, if there was a resume request pending and the device had to be woken up, | |
1447 | * 0, otherwise | |
1448 | */ | |
1449 | int pm_runtime_barrier(struct device *dev) | |
1450 | { | |
1451 | int retval = 0; | |
1452 | ||
1453 | pm_runtime_get_noresume(dev); | |
1454 | spin_lock_irq(&dev->power.lock); | |
1455 | ||
1456 | if (dev->power.request_pending | |
1457 | && dev->power.request == RPM_REQ_RESUME) { | |
140a6c94 | 1458 | rpm_resume(dev, 0); |
5e928f77 RW |
1459 | retval = 1; |
1460 | } | |
1461 | ||
1462 | __pm_runtime_barrier(dev); | |
1463 | ||
1464 | spin_unlock_irq(&dev->power.lock); | |
1465 | pm_runtime_put_noidle(dev); | |
1466 | ||
1467 | return retval; | |
1468 | } | |
1469 | EXPORT_SYMBOL_GPL(pm_runtime_barrier); | |
1470 | ||
520a552f | 1471 | bool pm_runtime_block_if_disabled(struct device *dev) |
3e5eee14 | 1472 | { |
520a552f RW |
1473 | bool ret; |
1474 | ||
3e5eee14 RW |
1475 | spin_lock_irq(&dev->power.lock); |
1476 | ||
a84c2a88 RW |
1477 | ret = !pm_runtime_enabled(dev); |
1478 | if (ret && dev->power.last_status == RPM_INVALID) | |
3e5eee14 RW |
1479 | dev->power.last_status = RPM_BLOCKED; |
1480 | ||
1481 | spin_unlock_irq(&dev->power.lock); | |
520a552f RW |
1482 | |
1483 | return ret; | |
3e5eee14 RW |
1484 | } |
1485 | ||
1486 | void pm_runtime_unblock(struct device *dev) | |
1487 | { | |
1488 | spin_lock_irq(&dev->power.lock); | |
1489 | ||
1490 | if (dev->power.last_status == RPM_BLOCKED) | |
1491 | dev->power.last_status = RPM_INVALID; | |
1492 | ||
1493 | spin_unlock_irq(&dev->power.lock); | |
1494 | } | |
1495 | ||
5e928f77 RW |
1496 | void __pm_runtime_disable(struct device *dev, bool check_resume) |
1497 | { | |
1498 | spin_lock_irq(&dev->power.lock); | |
1499 | ||
1500 | if (dev->power.disable_depth > 0) { | |
1501 | dev->power.disable_depth++; | |
1502 | goto out; | |
1503 | } | |
1504 | ||
1505 | /* | |
1506 | * Wake up the device if there's a resume request pending, because that | |
62052ab1 | 1507 | * means there probably is some I/O to process and disabling runtime PM |
5e928f77 RW |
1508 | * shouldn't prevent the device from processing the I/O. |
1509 | */ | |
dbfa4478 RW |
1510 | if (check_resume && dev->power.request_pending && |
1511 | dev->power.request == RPM_REQ_RESUME) { | |
5e928f77 RW |
1512 | /* |
1513 | * Prevent suspends and idle notifications from being carried | |
1514 | * out after we have woken up the device. | |
1515 | */ | |
1516 | pm_runtime_get_noresume(dev); | |
1517 | ||
140a6c94 | 1518 | rpm_resume(dev, 0); |
5e928f77 RW |
1519 | |
1520 | pm_runtime_put_noidle(dev); | |
1521 | } | |
1522 | ||
fed7e88c VG |
1523 | /* Update time accounting before disabling PM-runtime. */ |
1524 | update_pm_runtime_accounting(dev); | |
1525 | ||
c24efa67 | 1526 | if (!dev->power.disable_depth++) { |
5e928f77 | 1527 | __pm_runtime_barrier(dev); |
c24efa67 RW |
1528 | dev->power.last_status = dev->power.runtime_status; |
1529 | } | |
5e928f77 RW |
1530 | |
1531 | out: | |
1532 | spin_unlock_irq(&dev->power.lock); | |
1533 | } | |
1534 | EXPORT_SYMBOL_GPL(__pm_runtime_disable); | |
1535 | ||
1536 | /** | |
62052ab1 | 1537 | * pm_runtime_enable - Enable runtime PM of a device. |
5e928f77 RW |
1538 | * @dev: Device to handle. |
1539 | */ | |
1540 | void pm_runtime_enable(struct device *dev) | |
1541 | { | |
1542 | unsigned long flags; | |
1543 | ||
1544 | spin_lock_irqsave(&dev->power.lock, flags); | |
1545 | ||
c24efa67 | 1546 | if (!dev->power.disable_depth) { |
5e928f77 | 1547 | dev_warn(dev, "Unbalanced %s!\n", __func__); |
c24efa67 | 1548 | goto out; |
58456488 | 1549 | } |
5e928f77 | 1550 | |
c24efa67 RW |
1551 | if (--dev->power.disable_depth > 0) |
1552 | goto out; | |
1553 | ||
3e5eee14 RW |
1554 | if (dev->power.last_status == RPM_BLOCKED) { |
1555 | dev_warn(dev, "Attempt to enable runtime PM when it is blocked\n"); | |
1556 | dump_stack(); | |
1557 | } | |
c24efa67 RW |
1558 | dev->power.last_status = RPM_INVALID; |
1559 | dev->power.accounting_timestamp = ktime_get_mono_fast_ns(); | |
1560 | ||
1561 | if (dev->power.runtime_status == RPM_SUSPENDED && | |
1562 | !dev->power.ignore_children && | |
1563 | atomic_read(&dev->power.child_count) > 0) | |
1564 | dev_warn(dev, "Enabling runtime PM for inactive device with active children\n"); | |
f8817f61 | 1565 | |
c24efa67 | 1566 | out: |
5e928f77 RW |
1567 | spin_unlock_irqrestore(&dev->power.lock, flags); |
1568 | } | |
1569 | EXPORT_SYMBOL_GPL(pm_runtime_enable); | |
1570 | ||
73db799b BC |
1571 | static void pm_runtime_set_suspended_action(void *data) |
1572 | { | |
1573 | pm_runtime_set_suspended(data); | |
1574 | } | |
1575 | ||
1576 | /** | |
1577 | * devm_pm_runtime_set_active_enabled - set_active version of devm_pm_runtime_enable. | |
1578 | * | |
1579 | * @dev: Device to handle. | |
1580 | */ | |
1581 | int devm_pm_runtime_set_active_enabled(struct device *dev) | |
1582 | { | |
1583 | int err; | |
1584 | ||
1585 | err = pm_runtime_set_active(dev); | |
1586 | if (err) | |
1587 | return err; | |
1588 | ||
1589 | err = devm_add_action_or_reset(dev, pm_runtime_set_suspended_action, dev); | |
1590 | if (err) | |
1591 | return err; | |
1592 | ||
1593 | return devm_pm_runtime_enable(dev); | |
1594 | } | |
1595 | EXPORT_SYMBOL_GPL(devm_pm_runtime_set_active_enabled); | |
1596 | ||
b3636a3a DB |
1597 | static void pm_runtime_disable_action(void *data) |
1598 | { | |
b4060db9 | 1599 | pm_runtime_dont_use_autosuspend(data); |
b3636a3a DB |
1600 | pm_runtime_disable(data); |
1601 | } | |
1602 | ||
1603 | /** | |
1604 | * devm_pm_runtime_enable - devres-enabled version of pm_runtime_enable. | |
b4060db9 DA |
1605 | * |
1606 | * NOTE: this will also handle calling pm_runtime_dont_use_autosuspend() for | |
1607 | * you at driver exit time if needed. | |
1608 | * | |
b3636a3a DB |
1609 | * @dev: Device to handle. |
1610 | */ | |
1611 | int devm_pm_runtime_enable(struct device *dev) | |
1612 | { | |
1613 | pm_runtime_enable(dev); | |
1614 | ||
1615 | return devm_add_action_or_reset(dev, pm_runtime_disable_action, dev); | |
1616 | } | |
1617 | EXPORT_SYMBOL_GPL(devm_pm_runtime_enable); | |
1618 | ||
73db799b BC |
1619 | static void pm_runtime_put_noidle_action(void *data) |
1620 | { | |
1621 | pm_runtime_put_noidle(data); | |
1622 | } | |
1623 | ||
1624 | /** | |
1625 | * devm_pm_runtime_get_noresume - devres-enabled version of pm_runtime_get_noresume. | |
1626 | * | |
1627 | * @dev: Device to handle. | |
1628 | */ | |
1629 | int devm_pm_runtime_get_noresume(struct device *dev) | |
1630 | { | |
1631 | pm_runtime_get_noresume(dev); | |
1632 | ||
1633 | return devm_add_action_or_reset(dev, pm_runtime_put_noidle_action, dev); | |
1634 | } | |
1635 | EXPORT_SYMBOL_GPL(devm_pm_runtime_get_noresume); | |
1636 | ||
53823639 | 1637 | /** |
62052ab1 | 1638 | * pm_runtime_forbid - Block runtime PM of a device. |
53823639 RW |
1639 | * @dev: Device to handle. |
1640 | * | |
1641 | * Increase the device's usage count and clear its power.runtime_auto flag, | |
1642 | * so that it cannot be suspended at run time until pm_runtime_allow() is called | |
1643 | * for it. | |
1644 | */ | |
1645 | void pm_runtime_forbid(struct device *dev) | |
1646 | { | |
1647 | spin_lock_irq(&dev->power.lock); | |
1648 | if (!dev->power.runtime_auto) | |
1649 | goto out; | |
1650 | ||
1651 | dev->power.runtime_auto = false; | |
1652 | atomic_inc(&dev->power.usage_count); | |
140a6c94 | 1653 | rpm_resume(dev, 0); |
53823639 RW |
1654 | |
1655 | out: | |
1656 | spin_unlock_irq(&dev->power.lock); | |
1657 | } | |
1658 | EXPORT_SYMBOL_GPL(pm_runtime_forbid); | |
1659 | ||
1660 | /** | |
62052ab1 | 1661 | * pm_runtime_allow - Unblock runtime PM of a device. |
53823639 RW |
1662 | * @dev: Device to handle. |
1663 | * | |
1664 | * Decrease the device's usage count and set its power.runtime_auto flag. | |
1665 | */ | |
1666 | void pm_runtime_allow(struct device *dev) | |
1667 | { | |
82586a72 RW |
1668 | int ret; |
1669 | ||
53823639 RW |
1670 | spin_lock_irq(&dev->power.lock); |
1671 | if (dev->power.runtime_auto) | |
1672 | goto out; | |
1673 | ||
1674 | dev->power.runtime_auto = true; | |
82586a72 RW |
1675 | ret = rpm_drop_usage_count(dev); |
1676 | if (ret == 0) | |
fe7450b0 | 1677 | rpm_idle(dev, RPM_AUTO | RPM_ASYNC); |
82586a72 | 1678 | else if (ret > 0) |
db8f5086 | 1679 | trace_rpm_usage(dev, RPM_AUTO | RPM_ASYNC); |
53823639 RW |
1680 | |
1681 | out: | |
1682 | spin_unlock_irq(&dev->power.lock); | |
1683 | } | |
1684 | EXPORT_SYMBOL_GPL(pm_runtime_allow); | |
1685 | ||
7490e442 | 1686 | /** |
62052ab1 | 1687 | * pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device. |
7490e442 AS |
1688 | * @dev: Device to handle. |
1689 | * | |
1690 | * Set the power.no_callbacks flag, which tells the PM core that this | |
62052ab1 RW |
1691 | * device is power-managed through its parent and has no runtime PM |
1692 | * callbacks of its own. The runtime sysfs attributes will be removed. | |
7490e442 AS |
1693 | */ |
1694 | void pm_runtime_no_callbacks(struct device *dev) | |
1695 | { | |
1696 | spin_lock_irq(&dev->power.lock); | |
1697 | dev->power.no_callbacks = 1; | |
1698 | spin_unlock_irq(&dev->power.lock); | |
1699 | if (device_is_registered(dev)) | |
1700 | rpm_sysfs_remove(dev); | |
1701 | } | |
1702 | EXPORT_SYMBOL_GPL(pm_runtime_no_callbacks); | |
1703 | ||
c7b61de5 AS |
1704 | /** |
1705 | * pm_runtime_irq_safe - Leave interrupts disabled during callbacks. | |
1706 | * @dev: Device to handle | |
1707 | * | |
1708 | * Set the power.irq_safe flag, which tells the PM core that the | |
1709 | * ->runtime_suspend() and ->runtime_resume() callbacks for this device should | |
1710 | * always be invoked with the spinlock held and interrupts disabled. It also | |
1711 | * causes the parent's usage counter to be permanently incremented, preventing | |
1712 | * the parent from runtime suspending -- otherwise an irq-safe child might have | |
1713 | * to wait for a non-irq-safe parent. | |
1714 | */ | |
1715 | void pm_runtime_irq_safe(struct device *dev) | |
1716 | { | |
1717 | if (dev->parent) | |
1718 | pm_runtime_get_sync(dev->parent); | |
dbfa4478 | 1719 | |
c7b61de5 AS |
1720 | spin_lock_irq(&dev->power.lock); |
1721 | dev->power.irq_safe = 1; | |
1722 | spin_unlock_irq(&dev->power.lock); | |
1723 | } | |
1724 | EXPORT_SYMBOL_GPL(pm_runtime_irq_safe); | |
1725 | ||
15bcb91d AS |
1726 | /** |
1727 | * update_autosuspend - Handle a change to a device's autosuspend settings. | |
1728 | * @dev: Device to handle. | |
1729 | * @old_delay: The former autosuspend_delay value. | |
1730 | * @old_use: The former use_autosuspend value. | |
1731 | * | |
1732 | * Prevent runtime suspend if the new delay is negative and use_autosuspend is | |
1733 | * set; otherwise allow it. Send an idle notification if suspends are allowed. | |
1734 | * | |
1735 | * This function must be called under dev->power.lock with interrupts disabled. | |
1736 | */ | |
1737 | static void update_autosuspend(struct device *dev, int old_delay, int old_use) | |
1738 | { | |
1739 | int delay = dev->power.autosuspend_delay; | |
1740 | ||
1741 | /* Should runtime suspend be prevented now? */ | |
1742 | if (dev->power.use_autosuspend && delay < 0) { | |
1743 | ||
1744 | /* If it used to be allowed then prevent it. */ | |
1745 | if (!old_use || old_delay >= 0) { | |
1746 | atomic_inc(&dev->power.usage_count); | |
1747 | rpm_resume(dev, 0); | |
d2292906 | 1748 | } else { |
db8f5086 | 1749 | trace_rpm_usage(dev, 0); |
15bcb91d AS |
1750 | } |
1751 | } | |
1752 | ||
1753 | /* Runtime suspend should be allowed now. */ | |
1754 | else { | |
1755 | ||
1756 | /* If it used to be prevented then allow it. */ | |
1757 | if (old_use && old_delay < 0) | |
1758 | atomic_dec(&dev->power.usage_count); | |
1759 | ||
1760 | /* Maybe we can autosuspend now. */ | |
1761 | rpm_idle(dev, RPM_AUTO); | |
1762 | } | |
1763 | } | |
1764 | ||
1765 | /** | |
1766 | * pm_runtime_set_autosuspend_delay - Set a device's autosuspend_delay value. | |
1767 | * @dev: Device to handle. | |
1768 | * @delay: Value of the new delay in milliseconds. | |
1769 | * | |
1770 | * Set the device's power.autosuspend_delay value. If it changes to negative | |
62052ab1 RW |
1771 | * and the power.use_autosuspend flag is set, prevent runtime suspends. If it |
1772 | * changes the other way, allow runtime suspends. | |
15bcb91d AS |
1773 | */ |
1774 | void pm_runtime_set_autosuspend_delay(struct device *dev, int delay) | |
1775 | { | |
1776 | int old_delay, old_use; | |
1777 | ||
1778 | spin_lock_irq(&dev->power.lock); | |
1779 | old_delay = dev->power.autosuspend_delay; | |
1780 | old_use = dev->power.use_autosuspend; | |
1781 | dev->power.autosuspend_delay = delay; | |
1782 | update_autosuspend(dev, old_delay, old_use); | |
1783 | spin_unlock_irq(&dev->power.lock); | |
1784 | } | |
1785 | EXPORT_SYMBOL_GPL(pm_runtime_set_autosuspend_delay); | |
1786 | ||
1787 | /** | |
1788 | * __pm_runtime_use_autosuspend - Set a device's use_autosuspend flag. | |
1789 | * @dev: Device to handle. | |
1790 | * @use: New value for use_autosuspend. | |
1791 | * | |
62052ab1 | 1792 | * Set the device's power.use_autosuspend flag, and allow or prevent runtime |
15bcb91d AS |
1793 | * suspends as needed. |
1794 | */ | |
1795 | void __pm_runtime_use_autosuspend(struct device *dev, bool use) | |
1796 | { | |
1797 | int old_delay, old_use; | |
1798 | ||
1799 | spin_lock_irq(&dev->power.lock); | |
1800 | old_delay = dev->power.autosuspend_delay; | |
1801 | old_use = dev->power.use_autosuspend; | |
1802 | dev->power.use_autosuspend = use; | |
1803 | update_autosuspend(dev, old_delay, old_use); | |
1804 | spin_unlock_irq(&dev->power.lock); | |
1805 | } | |
1806 | EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend); | |
1807 | ||
5e928f77 | 1808 | /** |
62052ab1 | 1809 | * pm_runtime_init - Initialize runtime PM fields in given device object. |
5e928f77 RW |
1810 | * @dev: Device object to initialize. |
1811 | */ | |
1812 | void pm_runtime_init(struct device *dev) | |
1813 | { | |
5e928f77 | 1814 | dev->power.runtime_status = RPM_SUSPENDED; |
c24efa67 | 1815 | dev->power.last_status = RPM_INVALID; |
5e928f77 RW |
1816 | dev->power.idle_notification = false; |
1817 | ||
1818 | dev->power.disable_depth = 1; | |
1819 | atomic_set(&dev->power.usage_count, 0); | |
1820 | ||
1821 | dev->power.runtime_error = 0; | |
1822 | ||
1823 | atomic_set(&dev->power.child_count, 0); | |
1824 | pm_suspend_ignore_children(dev, false); | |
53823639 | 1825 | dev->power.runtime_auto = true; |
5e928f77 RW |
1826 | |
1827 | dev->power.request_pending = false; | |
1828 | dev->power.request = RPM_REQ_NONE; | |
1829 | dev->power.deferred_resume = false; | |
c745253e | 1830 | dev->power.needs_force_resume = 0; |
5e928f77 RW |
1831 | INIT_WORK(&dev->power.work, pm_runtime_work); |
1832 | ||
1833 | dev->power.timer_expires = 0; | |
efad91a9 NC |
1834 | hrtimer_setup(&dev->power.suspend_timer, pm_suspend_timer_fn, CLOCK_MONOTONIC, |
1835 | HRTIMER_MODE_ABS); | |
5e928f77 RW |
1836 | |
1837 | init_waitqueue_head(&dev->power.wait_queue); | |
1838 | } | |
1839 | ||
5de85b9d UH |
1840 | /** |
1841 | * pm_runtime_reinit - Re-initialize runtime PM fields in given device object. | |
1842 | * @dev: Device object to re-initialize. | |
1843 | */ | |
1844 | void pm_runtime_reinit(struct device *dev) | |
1845 | { | |
1846 | if (!pm_runtime_enabled(dev)) { | |
1847 | if (dev->power.runtime_status == RPM_ACTIVE) | |
1848 | pm_runtime_set_suspended(dev); | |
1849 | if (dev->power.irq_safe) { | |
1850 | spin_lock_irq(&dev->power.lock); | |
1851 | dev->power.irq_safe = 0; | |
1852 | spin_unlock_irq(&dev->power.lock); | |
1853 | if (dev->parent) | |
1854 | pm_runtime_put(dev->parent); | |
1855 | } | |
1856 | } | |
1857 | } | |
1858 | ||
5e928f77 RW |
1859 | /** |
1860 | * pm_runtime_remove - Prepare for removing a device from device hierarchy. | |
1861 | * @dev: Device object being removed from device hierarchy. | |
1862 | */ | |
1863 | void pm_runtime_remove(struct device *dev) | |
1864 | { | |
1865 | __pm_runtime_disable(dev, false); | |
5de85b9d | 1866 | pm_runtime_reinit(dev); |
5e928f77 | 1867 | } |
37f20416 | 1868 | |
21d5c57b | 1869 | /** |
b06c0b2f | 1870 | * pm_runtime_get_suppliers - Resume and reference-count supplier devices. |
21d5c57b RW |
1871 | * @dev: Consumer device. |
1872 | */ | |
b06c0b2f | 1873 | void pm_runtime_get_suppliers(struct device *dev) |
21d5c57b | 1874 | { |
b06c0b2f | 1875 | struct device_link *link; |
21d5c57b RW |
1876 | int idx; |
1877 | ||
1878 | idx = device_links_read_lock(); | |
1879 | ||
c2fa1e1b JFG |
1880 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, |
1881 | device_links_read_lock_held()) | |
4c06c4e6 | 1882 | if (link->flags & DL_FLAG_PM_RUNTIME) { |
36003d4c | 1883 | link->supplier_preactivated = true; |
b06c0b2f | 1884 | pm_runtime_get_sync(link->supplier); |
4c06c4e6 | 1885 | } |
b06c0b2f RW |
1886 | |
1887 | device_links_read_unlock(idx); | |
1888 | } | |
1889 | ||
1890 | /** | |
1891 | * pm_runtime_put_suppliers - Drop references to supplier devices. | |
1892 | * @dev: Consumer device. | |
1893 | */ | |
1894 | void pm_runtime_put_suppliers(struct device *dev) | |
1895 | { | |
1896 | struct device_link *link; | |
1897 | int idx; | |
1898 | ||
1899 | idx = device_links_read_lock(); | |
1900 | ||
c2fa1e1b JFG |
1901 | list_for_each_entry_rcu(link, &dev->links.suppliers, c_node, |
1902 | device_links_read_lock_held()) | |
36003d4c RW |
1903 | if (link->supplier_preactivated) { |
1904 | link->supplier_preactivated = false; | |
88737106 | 1905 | pm_runtime_put(link->supplier); |
36003d4c | 1906 | } |
21d5c57b RW |
1907 | |
1908 | device_links_read_unlock(idx); | |
1909 | } | |
1910 | ||
baa8809f RW |
1911 | void pm_runtime_new_link(struct device *dev) |
1912 | { | |
1913 | spin_lock_irq(&dev->power.lock); | |
1914 | dev->power.links_count++; | |
1915 | spin_unlock_irq(&dev->power.lock); | |
1916 | } | |
1917 | ||
e0e398e2 | 1918 | static void pm_runtime_drop_link_count(struct device *dev) |
baa8809f RW |
1919 | { |
1920 | spin_lock_irq(&dev->power.lock); | |
1921 | WARN_ON(dev->power.links_count == 0); | |
1922 | dev->power.links_count--; | |
1923 | spin_unlock_irq(&dev->power.lock); | |
e0e398e2 RW |
1924 | } |
1925 | ||
1926 | /** | |
1927 | * pm_runtime_drop_link - Prepare for device link removal. | |
1928 | * @link: Device link going away. | |
1929 | * | |
1930 | * Drop the link count of the consumer end of @link and decrement the supplier | |
1931 | * device's runtime PM usage counter as many times as needed to drop all of the | |
1932 | * PM runtime reference to it from the consumer. | |
1933 | */ | |
1934 | void pm_runtime_drop_link(struct device_link *link) | |
1935 | { | |
1936 | if (!(link->flags & DL_FLAG_PM_RUNTIME)) | |
1937 | return; | |
1938 | ||
1939 | pm_runtime_drop_link_count(link->consumer); | |
07358194 RW |
1940 | pm_runtime_release_supplier(link); |
1941 | pm_request_idle(link->supplier); | |
baa8809f RW |
1942 | } |
1943 | ||
eeb87d17 | 1944 | bool pm_runtime_need_not_resume(struct device *dev) |
4918e1f8 RW |
1945 | { |
1946 | return atomic_read(&dev->power.usage_count) <= 1 && | |
1f5c6855 RW |
1947 | (atomic_read(&dev->power.child_count) == 0 || |
1948 | dev->power.ignore_children); | |
4918e1f8 RW |
1949 | } |
1950 | ||
37f20416 UH |
1951 | /** |
1952 | * pm_runtime_force_suspend - Force a device into suspend state if needed. | |
1953 | * @dev: Device to suspend. | |
1954 | * | |
1955 | * Disable runtime PM so we safely can check the device's runtime PM status and | |
4918e1f8 RW |
1956 | * if it is active, invoke its ->runtime_suspend callback to suspend it and |
1957 | * change its runtime PM status field to RPM_SUSPENDED. Also, if the device's | |
1958 | * usage and children counters don't indicate that the device was in use before | |
1959 | * the system-wide transition under way, decrement its parent's children counter | |
1960 | * (if there is a parent). Keep runtime PM disabled to preserve the state | |
1961 | * unless we encounter errors. | |
37f20416 UH |
1962 | * |
1963 | * Typically this function may be invoked from a system suspend callback to make | |
4918e1f8 RW |
1964 | * sure the device is put into low power state and it should only be used during |
1965 | * system-wide PM transitions to sleep states. It assumes that the analogous | |
1966 | * pm_runtime_force_resume() will be used to resume the device. | |
450316dc RF |
1967 | * |
1968 | * Do not use with DPM_FLAG_SMART_SUSPEND as this can lead to an inconsistent | |
1969 | * state where this function has called the ->runtime_suspend callback but the | |
1970 | * PM core marks the driver as runtime active. | |
37f20416 UH |
1971 | */ |
1972 | int pm_runtime_force_suspend(struct device *dev) | |
1973 | { | |
1974 | int (*callback)(struct device *); | |
617fcb67 | 1975 | int ret; |
37f20416 UH |
1976 | |
1977 | pm_runtime_disable(dev); | |
37f20416 UH |
1978 | if (pm_runtime_status_suspended(dev)) |
1979 | return 0; | |
1980 | ||
dbcd2d72 | 1981 | callback = RPM_GET_CALLBACK(dev, runtime_suspend); |
37f20416 | 1982 | |
c46a0d5a | 1983 | dev_pm_enable_wake_irq_check(dev, true); |
617fcb67 | 1984 | ret = callback ? callback(dev) : 0; |
37f20416 UH |
1985 | if (ret) |
1986 | goto err; | |
1987 | ||
c46a0d5a UH |
1988 | dev_pm_enable_wake_irq_complete(dev); |
1989 | ||
1d9174fb | 1990 | /* |
4918e1f8 RW |
1991 | * If the device can stay in suspend after the system-wide transition |
1992 | * to the working state that will follow, drop the children counter of | |
1993 | * its parent, but set its status to RPM_SUSPENDED anyway in case this | |
1994 | * function will be called again for it in the meantime. | |
1d9174fb | 1995 | */ |
c745253e | 1996 | if (pm_runtime_need_not_resume(dev)) { |
4918e1f8 | 1997 | pm_runtime_set_suspended(dev); |
c745253e | 1998 | } else { |
4918e1f8 | 1999 | __update_runtime_status(dev, RPM_SUSPENDED); |
c745253e TL |
2000 | dev->power.needs_force_resume = 1; |
2001 | } | |
1d9174fb | 2002 | |
37f20416 | 2003 | return 0; |
4918e1f8 | 2004 | |
37f20416 | 2005 | err: |
c46a0d5a | 2006 | dev_pm_disable_wake_irq_check(dev, true); |
37f20416 UH |
2007 | pm_runtime_enable(dev); |
2008 | return ret; | |
2009 | } | |
2010 | EXPORT_SYMBOL_GPL(pm_runtime_force_suspend); | |
2011 | ||
2012 | /** | |
1d9174fb | 2013 | * pm_runtime_force_resume - Force a device into resume state if needed. |
37f20416 UH |
2014 | * @dev: Device to resume. |
2015 | * | |
2016 | * Prior invoking this function we expect the user to have brought the device | |
2017 | * into low power state by a call to pm_runtime_force_suspend(). Here we reverse | |
4918e1f8 RW |
2018 | * those actions and bring the device into full power, if it is expected to be |
2019 | * used on system resume. In the other case, we defer the resume to be managed | |
2020 | * via runtime PM. | |
37f20416 | 2021 | * |
1d9174fb | 2022 | * Typically this function may be invoked from a system resume callback. |
37f20416 UH |
2023 | */ |
2024 | int pm_runtime_force_resume(struct device *dev) | |
2025 | { | |
2026 | int (*callback)(struct device *); | |
2027 | int ret = 0; | |
2028 | ||
d2677d57 | 2029 | if (!dev->power.needs_force_resume) |
9f5b5274 UH |
2030 | goto out; |
2031 | ||
1d9174fb | 2032 | /* |
4918e1f8 RW |
2033 | * The value of the parent's children counter is correct already, so |
2034 | * just update the status of the device. | |
2035 | */ | |
2036 | __update_runtime_status(dev, RPM_ACTIVE); | |
1d9174fb | 2037 | |
4918e1f8 | 2038 | callback = RPM_GET_CALLBACK(dev, runtime_resume); |
37f20416 | 2039 | |
c46a0d5a | 2040 | dev_pm_disable_wake_irq_check(dev, false); |
617fcb67 | 2041 | ret = callback ? callback(dev) : 0; |
0ae3aeef UH |
2042 | if (ret) { |
2043 | pm_runtime_set_suspended(dev); | |
c46a0d5a | 2044 | dev_pm_enable_wake_irq_check(dev, false); |
0ae3aeef UH |
2045 | goto out; |
2046 | } | |
2047 | ||
37f20416 UH |
2048 | pm_runtime_mark_last_busy(dev); |
2049 | out: | |
c745253e | 2050 | dev->power.needs_force_resume = 0; |
37f20416 UH |
2051 | pm_runtime_enable(dev); |
2052 | return ret; | |
2053 | } | |
2054 | EXPORT_SYMBOL_GPL(pm_runtime_force_resume); |