2
* drivers/base/power/runtime.c - Helper functions for device run-time PM
2
* drivers/base/power/runtime.c - Helper functions for device runtime PM
4
4
* Copyright (c) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
5
5
* Copyright (C) 2010 Alan Stern <stern@rowland.harvard.edu>
28
30
void update_pm_runtime_accounting(struct device *dev)
30
32
unsigned long now = jiffies;
33
35
delta = now - dev->power.accounting_timestamp;
38
37
dev->power.accounting_timestamp = now;
40
39
if (dev->power.disable_depth > 0)
136
135
if (dev->power.runtime_error)
137
136
retval = -EINVAL;
138
else if (atomic_read(&dev->power.usage_count) > 0
139
|| dev->power.disable_depth > 0)
137
else if (dev->power.disable_depth > 0)
139
else if (atomic_read(&dev->power.usage_count) > 0)
140
140
retval = -EAGAIN;
141
141
else if (!pm_children_suspended(dev))
157
* __rpm_callback - Run a given runtime PM callback for a given device.
158
* @cb: Runtime PM callback to run.
159
* @dev: Device to run the callback for.
161
static int __rpm_callback(int (*cb)(struct device *), struct device *dev)
162
__releases(&dev->power.lock) __acquires(&dev->power.lock)
166
if (dev->power.irq_safe)
167
spin_unlock(&dev->power.lock);
169
spin_unlock_irq(&dev->power.lock);
173
if (dev->power.irq_safe)
174
spin_lock(&dev->power.lock);
176
spin_lock_irq(&dev->power.lock);
157
182
* rpm_idle - Notify device bus type if the device can be suspended.
158
183
* @dev: Device to notify the bus type about.
159
184
* @rpmflags: Flag bits.
161
* Check if the device's run-time PM status allows it to be suspended. If
186
* Check if the device's runtime PM status allows it to be suspended. If
162
187
* another idle notification has been started earlier, return immediately. If
163
188
* the RPM_ASYNC flag is set then queue an idle-notification request; otherwise
164
189
* run the ->runtime_idle() callback directly.
214
240
dev->power.idle_notification = true;
217
callback = dev->pwr_domain->ops.runtime_idle;
243
callback = dev->pm_domain->ops.runtime_idle;
218
244
else if (dev->type && dev->type->pm)
219
245
callback = dev->type->pm->runtime_idle;
220
246
else if (dev->class && dev->class->pm)
228
spin_unlock_irq(&dev->power.lock);
232
spin_lock_irq(&dev->power.lock);
254
__rpm_callback(callback, dev);
235
256
dev->power.idle_notification = false;
236
257
wake_up_all(&dev->power.wait_queue);
260
trace_rpm_return_int(dev, _THIS_IP_, retval);
245
267
* @dev: Device to run the callback for.
247
269
static int rpm_callback(int (*cb)(struct device *), struct device *dev)
248
__releases(&dev->power.lock) __acquires(&dev->power.lock)
255
if (dev->power.irq_safe) {
258
spin_unlock_irq(&dev->power.lock);
262
spin_lock_irq(&dev->power.lock);
276
retval = __rpm_callback(cb, dev);
264
278
dev->power.runtime_error = retval;
279
return retval != -EACCES ? retval : -EIO;
269
* rpm_suspend - Carry out run-time suspend of given device.
283
* rpm_suspend - Carry out runtime suspend of given device.
270
284
* @dev: Device to suspend.
271
285
* @rpmflags: Flag bits.
273
* Check if the device's run-time PM status allows it to be suspended. If
274
* another suspend has been started earlier, either return immediately or wait
275
* for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC flags. Cancel a
276
* pending idle notification. If the RPM_ASYNC flag is set then queue a
277
* suspend request; otherwise run the ->runtime_suspend() callback directly.
278
* If a deferred resume was requested while the callback was running then carry
279
* it out; otherwise send an idle notification for the device (if the suspend
280
* failed) or for its parent (if the suspend succeeded).
287
* Check if the device's runtime PM status allows it to be suspended.
288
* Cancel a pending idle notification, autosuspend or suspend. If
289
* another suspend has been started earlier, either return immediately
290
* or wait for it to finish, depending on the RPM_NOWAIT and RPM_ASYNC
291
* flags. If the RPM_ASYNC flag is set then queue a suspend request;
292
* otherwise run the ->runtime_suspend() callback directly. When
293
* ->runtime_suspend succeeded, if a deferred resume was requested while
294
* the callback was running then carry it out, otherwise send an idle
295
* notification for its parent (if the suspend succeeded and both
296
* ignore_children of parent->power and irq_safe of dev->power are not set).
281
297
* If ->runtime_suspend failed with -EAGAIN or -EBUSY, and if the RPM_AUTO
282
298
* flag is set and the next autosuspend-delay expiration time is in the
283
299
* future, schedule another autosuspend attempt.
291
307
struct device *parent = NULL;
294
dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
310
trace_rpm_suspend(dev, rpmflags);
297
313
retval = rpm_check_suspend_allowed(dev);
362
if (dev->power.irq_safe) {
363
spin_unlock(&dev->power.lock);
367
spin_lock(&dev->power.lock);
346
371
/* Wait for the other suspend running in parallel with us. */
348
373
prepare_to_wait(&dev->power.wait_queue, &wait,
378
403
__update_runtime_status(dev, RPM_SUSPENDING);
381
callback = dev->pwr_domain->ops.runtime_suspend;
406
callback = dev->pm_domain->ops.runtime_suspend;
382
407
else if (dev->type && dev->type->pm)
383
408
callback = dev->type->pm->runtime_suspend;
384
409
else if (dev->class && dev->class->pm)
391
416
retval = rpm_callback(callback, dev);
393
418
__update_runtime_status(dev, RPM_ACTIVE);
394
dev->power.deferred_resume = 0;
419
dev->power.deferred_resume = false;
395
420
if (retval == -EAGAIN || retval == -EBUSY) {
396
421
dev->power.runtime_error = 0;
408
433
pm_runtime_cancel_pending(dev);
435
wake_up_all(&dev->power.wait_queue);
412
__update_runtime_status(dev, RPM_SUSPENDED);
413
pm_runtime_deactivate_timer(dev);
439
__update_runtime_status(dev, RPM_SUSPENDED);
440
pm_runtime_deactivate_timer(dev);
416
parent = dev->parent;
417
atomic_add_unless(&parent->power.child_count, -1, 0);
443
parent = dev->parent;
444
atomic_add_unless(&parent->power.child_count, -1, 0);
420
446
wake_up_all(&dev->power.wait_queue);
440
dev_dbg(dev, "%s returns %d\n", __func__, retval);
466
trace_rpm_return_int(dev, _THIS_IP_, retval);
446
* rpm_resume - Carry out run-time resume of given device.
472
* rpm_resume - Carry out runtime resume of given device.
447
473
* @dev: Device to resume.
448
474
* @rpmflags: Flag bits.
450
* Check if the device's run-time PM status allows it to be resumed. Cancel
476
* Check if the device's runtime PM status allows it to be resumed. Cancel
451
477
* any scheduled or pending requests. If another resume has been started
452
478
* earlier, either return immediately or wait for it to finish, depending on the
453
479
* RPM_NOWAIT and RPM_ASYNC flags. Similarly, if there's a suspend running in
466
492
struct device *parent = NULL;
469
dev_dbg(dev, "%s flags 0x%x\n", __func__, rpmflags);
495
trace_rpm_resume(dev, rpmflags);
472
498
if (dev->power.runtime_error)
473
499
retval = -EINVAL;
474
500
else if (dev->power.disable_depth > 0)
532
if (dev->power.irq_safe) {
533
spin_unlock(&dev->power.lock);
537
spin_lock(&dev->power.lock);
506
541
/* Wait for the operation carried out in parallel with us. */
508
543
prepare_to_wait(&dev->power.wait_queue, &wait,
565
600
spin_lock(&parent->power.lock);
567
* We can resume if the parent's run-time PM is disabled or it
602
* We can resume if the parent's runtime PM is disabled or it
568
603
* is set to ignore children.
570
605
if (!parent->power.disable_depth
588
623
__update_runtime_status(dev, RPM_RESUMING);
591
callback = dev->pwr_domain->ops.runtime_resume;
626
callback = dev->pm_domain->ops.runtime_resume;
592
627
else if (dev->type && dev->type->pm)
593
628
callback = dev->type->pm->runtime_resume;
594
629
else if (dev->class && dev->class->pm)
622
657
spin_lock_irq(&dev->power.lock);
625
dev_dbg(dev, "%s returns %d\n", __func__, retval);
660
trace_rpm_return_int(dev, _THIS_IP_, retval);
631
* pm_runtime_work - Universal run-time PM work function.
666
* pm_runtime_work - Universal runtime PM work function.
632
667
* @work: Work structure used for scheduling the execution of this function.
634
669
* Use @work to get the device object the work is to be done for, determine what
635
* is to be done and execute the appropriate run-time PM function.
670
* is to be done and execute the appropriate runtime PM function.
637
672
static void pm_runtime_work(struct work_struct *work)
739
774
* return immediately if it is larger than zero. Then carry out an idle
740
775
* notification, either synchronous or asynchronous.
742
* This routine may be called in atomic context if the RPM_ASYNC flag is set.
777
* This routine may be called in atomic context if the RPM_ASYNC flag is set,
778
* or if pm_runtime_irq_safe() has been called.
744
780
int __pm_runtime_idle(struct device *dev, int rpmflags)
746
782
unsigned long flags;
785
might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
749
787
if (rpmflags & RPM_GET_PUT) {
750
788
if (!atomic_dec_and_test(&dev->power.usage_count))
768
806
* return immediately if it is larger than zero. Then carry out a suspend,
769
807
* either synchronous or asynchronous.
771
* This routine may be called in atomic context if the RPM_ASYNC flag is set.
809
* This routine may be called in atomic context if the RPM_ASYNC flag is set,
810
* or if pm_runtime_irq_safe() has been called.
773
812
int __pm_runtime_suspend(struct device *dev, int rpmflags)
775
814
unsigned long flags;
817
might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
778
819
if (rpmflags & RPM_GET_PUT) {
779
820
if (!atomic_dec_and_test(&dev->power.usage_count))
789
830
EXPORT_SYMBOL_GPL(__pm_runtime_suspend);
792
* __pm_runtime_resume - Entry point for run-time resume operations.
833
* __pm_runtime_resume - Entry point for runtime resume operations.
793
834
* @dev: Device to resume.
794
835
* @rpmflags: Flag bits.
796
837
* If the RPM_GET_PUT flag is set, increment the device's usage count. Then
797
838
* carry out a resume, either synchronous or asynchronous.
799
* This routine may be called in atomic context if the RPM_ASYNC flag is set.
840
* This routine may be called in atomic context if the RPM_ASYNC flag is set,
841
* or if pm_runtime_irq_safe() has been called.
801
843
int __pm_runtime_resume(struct device *dev, int rpmflags)
803
845
unsigned long flags;
848
might_sleep_if(!(rpmflags & RPM_ASYNC) && !dev->power.irq_safe);
806
850
if (rpmflags & RPM_GET_PUT)
807
851
atomic_inc(&dev->power.usage_count);
815
859
EXPORT_SYMBOL_GPL(__pm_runtime_resume);
818
* __pm_runtime_set_status - Set run-time PM status of a device.
862
* __pm_runtime_set_status - Set runtime PM status of a device.
819
863
* @dev: Device to handle.
820
* @status: New run-time PM status of the device.
864
* @status: New runtime PM status of the device.
822
* If run-time PM of the device is disabled or its power.runtime_error field is
866
* If runtime PM of the device is disabled or its power.runtime_error field is
823
867
* different from zero, the status may be changed either to RPM_ACTIVE, or to
824
868
* RPM_SUSPENDED, as long as that reflects the actual state of the device.
825
869
* However, if the device has a parent and the parent is not active, and the
867
911
* It is invalid to put an active child under a parent that is
868
* not active, has run-time PM enabled and the
912
* not active, has runtime PM enabled and the
869
913
* 'power.ignore_children' flag unset.
871
915
if (!parent->power.disable_depth
899
943
* @dev: Device to handle.
901
945
* Flush all pending requests for the device from pm_wq and wait for all
902
* run-time PM operations involving the device in progress to complete.
946
* runtime PM operations involving the device in progress to complete.
904
948
* Should be called under dev->power.lock with interrupts disabled.
947
991
* Prevent the device from being suspended by incrementing its usage counter and
948
992
* if there's a pending resume request for the device, wake the device up.
949
993
* Next, make sure that all pending requests for the device have been flushed
950
* from pm_wq and wait for all run-time PM operations involving the device in
994
* from pm_wq and wait for all runtime PM operations involving the device in
951
995
* progress to complete.
977
1021
EXPORT_SYMBOL_GPL(pm_runtime_barrier);
980
* __pm_runtime_disable - Disable run-time PM of a device.
1024
* __pm_runtime_disable - Disable runtime PM of a device.
981
1025
* @dev: Device to handle.
982
1026
* @check_resume: If set, check if there's a resume request for the device.
984
1028
* Increment power.disable_depth for the device and if was zero previously,
985
* cancel all pending run-time PM requests for the device and wait for all
1029
* cancel all pending runtime PM requests for the device and wait for all
986
1030
* operations in progress to complete. The device can be either active or
987
* suspended after its run-time PM has been disabled.
1031
* suspended after its runtime PM has been disabled.
989
1033
* If @check_resume is set and there's a resume request pending when
990
1034
* __pm_runtime_disable() is called and power.disable_depth is zero, the
991
* function will wake up the device before disabling its run-time PM.
1035
* function will wake up the device before disabling its runtime PM.
993
1037
void __pm_runtime_disable(struct device *dev, bool check_resume)
1003
1047
* Wake up the device if there's a resume request pending, because that
1004
* means there probably is some I/O to process and disabling run-time PM
1048
* means there probably is some I/O to process and disabling runtime PM
1005
1049
* shouldn't prevent the device from processing the I/O.
1007
1051
if (check_resume && dev->power.request_pending
1026
1070
EXPORT_SYMBOL_GPL(__pm_runtime_disable);
1029
* pm_runtime_enable - Enable run-time PM of a device.
1073
* pm_runtime_enable - Enable runtime PM of a device.
1030
1074
* @dev: Device to handle.
1032
1076
void pm_runtime_enable(struct device *dev)
1045
1089
EXPORT_SYMBOL_GPL(pm_runtime_enable);
1048
* pm_runtime_forbid - Block run-time PM of a device.
1092
* pm_runtime_forbid - Block runtime PM of a device.
1049
1093
* @dev: Device to handle.
1051
1095
* Increase the device's usage count and clear its power.runtime_auto flag,
1068
1112
EXPORT_SYMBOL_GPL(pm_runtime_forbid);
1071
* pm_runtime_allow - Unblock run-time PM of a device.
1115
* pm_runtime_allow - Unblock runtime PM of a device.
1072
1116
* @dev: Device to handle.
1074
1118
* Decrease the device's usage count and set its power.runtime_auto flag.
1089
1133
EXPORT_SYMBOL_GPL(pm_runtime_allow);
1092
* pm_runtime_no_callbacks - Ignore run-time PM callbacks for a device.
1136
* pm_runtime_no_callbacks - Ignore runtime PM callbacks for a device.
1093
1137
* @dev: Device to handle.
1095
1139
* Set the power.no_callbacks flag, which tells the PM core that this
1096
* device is power-managed through its parent and has no run-time PM
1097
* callbacks of its own. The run-time sysfs attributes will be removed.
1140
* device is power-managed through its parent and has no runtime PM
1141
* callbacks of its own. The runtime sysfs attributes will be removed.
1099
1143
void pm_runtime_no_callbacks(struct device *dev)
1170
1214
* @delay: Value of the new delay in milliseconds.
1172
1216
* Set the device's power.autosuspend_delay value. If it changes to negative
1173
* and the power.use_autosuspend flag is set, prevent run-time suspends. If it
1174
* changes the other way, allow run-time suspends.
1217
* and the power.use_autosuspend flag is set, prevent runtime suspends. If it
1218
* changes the other way, allow runtime suspends.
1176
1220
void pm_runtime_set_autosuspend_delay(struct device *dev, int delay)
1191
1235
* @dev: Device to handle.
1192
1236
* @use: New value for use_autosuspend.
1194
* Set the device's power.use_autosuspend flag, and allow or prevent run-time
1238
* Set the device's power.use_autosuspend flag, and allow or prevent runtime
1195
1239
* suspends as needed.
1197
1241
void __pm_runtime_use_autosuspend(struct device *dev, bool use)
1208
1252
EXPORT_SYMBOL_GPL(__pm_runtime_use_autosuspend);
1211
* pm_runtime_init - Initialize run-time PM fields in given device object.
1255
* pm_runtime_init - Initialize runtime PM fields in given device object.
1212
1256
* @dev: Device object to initialize.
1214
1258
void pm_runtime_init(struct device *dev)