Runtime Power Management Framework for I/O Devices¶
2009-2011 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
2010 Alan Stern <stern@rowland.harvard.edu>
2014 Intel Corp., Rafael J. Wysocki <rafael.j.wysocki@intel.com>
1. Introduction¶
Support for runtime power management (runtime PM) of I/O devices is providedat the power management core (PM core) level by means of:
The power management workqueue pm_wq in which bus types and device drivers canput their PM-related work items. It is strongly recommended that pm_wq beused for queuing all work items related to runtime PM, because this allowsthem to be synchronized with system-wide power transitions (suspend to RAM,hibernation and resume from system sleep states). pm_wq is declared ininclude/linux/pm_runtime.h and defined in kernel/power/main.c.
A number of runtime PM fields in the ‘power’ member of ‘
structdevice’ (whichis of the type ‘structdev_pm_info’, defined in include/linux/pm.h) that canbe used for synchronizing runtime PM operations with one another.Three device runtime PM callbacks in ‘
structdev_pm_ops’ (defined ininclude/linux/pm.h).A set of helper functions defined in drivers/base/power/runtime.c that can beused for carrying out runtime PM operations in such a way that thesynchronization between them is taken care of by the PM core. Bus types anddevice drivers are encouraged to use these functions.
The runtime PM callbacks present in ‘structdev_pm_ops’, the device runtime PMfields of ‘structdev_pm_info’ and the core helper functions provided forruntime PM are described below.
2. Device Runtime PM Callbacks¶
There are three device runtime PM callbacks defined in ‘structdev_pm_ops’:
struct dev_pm_ops { ... int (*runtime_suspend)(struct device *dev); int (*runtime_resume)(struct device *dev); int (*runtime_idle)(struct device *dev); ...};The ->runtime_suspend(), ->runtime_resume() and ->runtime_idle() callbacksare executed by the PM core for the device’s subsystem that may be either ofthe following:
PM domain of the device, if the device’s PM domain object, dev->pm_domain,is present.
Device type of the device, if both dev->type and dev->type->pm are present.
Device class of the device, if both dev->class and dev->class->pm arepresent.
Bus type of the device, if both dev->bus and dev->bus->pm are present.
If the subsystem chosen by applying the above rules doesn’t provide the relevantcallback, the PM core will invoke the corresponding driver callback stored indev->driver->pm directly (if present).
The PM core always checks which callback to use in the order given above, so thepriority order of callbacks from high to low is: PM domain, device type, classand bus type. Moreover, the high-priority one will always take precedence overa low-priority one. The PM domain, bus type, device type and class callbacksare referred to as subsystem-level callbacks in what follows.
By default, the callbacks are always invoked in process context with interruptsenabled. However, thepm_runtime_irq_safe() helper function can be used to tellthe PM core that it is safe to run the ->runtime_suspend(), ->runtime_resume()and ->runtime_idle() callbacks for the given device in atomic context withinterrupts disabled. This implies that the callback routines in question mustnot block or sleep, but it also means that the synchronous helper functionslisted at the end of Section 4 may be used for that device within an interrupthandler or generally in an atomic context.
The subsystem-level suspend callback, if present, is _entirely_ _responsible_for handling the suspend of the device as appropriate, which may, but need notinclude executing the device driver’s own ->runtime_suspend() callback (from thePM core’s point of view it is not necessary to implement a ->runtime_suspend()callback in a device driver as long as the subsystem-level suspend callbackknows what to do to handle the device).
Once the subsystem-level suspend callback (or the driver suspend callback,if invoked directly) has completed successfully for the given device, the PMcore regards the device as suspended, which need not mean that it has beenput into a low power state. It is supposed to mean, however, that thedevice will not process data and will not communicate with the CPU(s) andRAM until the appropriate resume callback is executed for it. The runtimePM status of a device after successful execution of the suspend callback is‘suspended’.
If the suspend callback returns -EBUSY or -EAGAIN, the device’s runtime PMstatus remains ‘active’, which means that the device _must_ be fullyoperational afterwards.
If the suspend callback returns an error code different from -EBUSY and-EAGAIN, the PM core regards this as a fatal error and will refuse to runthe helper functions described in Section 4 for the device until its statusis directly set to either ‘active’, or ‘suspended’ (the PM core providesspecial helper functions for this purpose).
In particular, if the driver requires remote wakeup capability (i.e. hardwaremechanism allowing the device to request a change of its power state, such asPCI PME) for proper functioning anddevice_can_wakeup() returns ‘false’ for thedevice, then ->runtime_suspend() should return -EBUSY. On the other hand, ifdevice_can_wakeup() returns ‘true’ for the device and the device is put into alow-power state during the execution of the suspend callback, it is expectedthat remote wakeup will be enabled for the device. Generally, remote wakeupshould be enabled for all input devices put into low-power states at run time.
The subsystem-level resume callback, if present, isentirely responsible forhandling the resume of the device as appropriate, which may, but need notinclude executing the device driver’s own ->runtime_resume() callback (from thePM core’s point of view it is not necessary to implement a ->runtime_resume()callback in a device driver as long as the subsystem-level resume callback knowswhat to do to handle the device).
Once the subsystem-level resume callback (or the driver resume callback, ifinvoked directly) has completed successfully, the PM core regards the deviceas fully operational, which means that the device _must_ be able to completeI/O operations as needed. The runtime PM status of the device is then‘active’.
If the resume callback returns an error code, the PM core regards this as afatal error and will refuse to run the helper functions described in Section4 for the device, until its status is directly set to either ‘active’, or‘suspended’ (by means of special helper functions provided by the PM corefor this purpose).
The idle callback (a subsystem-level one, if present, or the driver one) isexecuted by the PM core whenever the device appears to be idle, which isindicated to the PM core by two counters, the device’s usage counter and thecounter of ‘active’ children of the device.
If any of these counters is decreased using a helper function provided bythe PM core and it turns out to be equal to zero, the other counter ischecked. If that counter also is equal to zero, the PM core executes theidle callback with the device as its argument.
The action performed by the idle callback is totally dependent on the subsystem(or driver) in question, but the expected and recommended action is to checkif the device can be suspended (i.e. if all of the conditions necessary forsuspending the device are satisfied) and to queue up a suspend request for thedevice in that case. If there is no idle callback, or if the callback returns0, then the PM core will attempt to carry out a runtime suspend of the device,also respecting devices configured for autosuspend. In essence this means acall topm_runtime_autosuspend(). To prevent this (for example, if the callbackroutine has started a delayed suspend), the routine must return a non-zerovalue. Negative error return codes are ignored by the PM core.
The helper functions provided by the PM core, described in Section 4, guaranteethat the following constraints are met with respect to runtime PM callbacks forone device:
The callbacks are mutually exclusive (e.g. it is forbidden to execute->
runtime_suspend()in parallel with ->runtime_resume()or with anotherinstance of ->runtime_suspend()for the same device) with the exception that->runtime_suspend()or ->runtime_resume()can be executed in parallel with->runtime_idle()(although ->runtime_idle()will not be started while anyof the other callbacks is being executed for the same device).->
runtime_idle()and ->runtime_suspend()can only be executed for ‘active’devices (i.e. the PM core will only execute ->runtime_idle()or->runtime_suspend()for the devices the runtime PM status of which is‘active’).->
runtime_idle()and ->runtime_suspend()can only be executed for a devicethe usage counter of which is equal to zero _and_ either the counter of‘active’ children of which is equal to zero, or the ‘power.ignore_children’flag of which is set.->
runtime_resume()can only be executed for ‘suspended’ devices (i.e. thePM core will only execute ->runtime_resume()for the devices the runtimePM status of which is ‘suspended’).
Additionally, the helper functions provided by the PM core obey the followingrules:
If ->
runtime_suspend()is about to be executed or there’s a pending requestto execute it, ->runtime_idle()will not be executed for the same device.A request to execute or to schedule the execution of ->
runtime_suspend()will cancel any pending requests to execute ->runtime_idle()for the samedevice.If ->
runtime_resume()is about to be executed or there’s a pending requestto execute it, the other callbacks will not be executed for the same device.A request to execute ->
runtime_resume()will cancel any pending orscheduled requests to execute the other callbacks for the same device,except for scheduled autosuspends.
3. Runtime PM Device Fields¶
The following device runtime PM fields are present in ‘structdev_pm_info’, asdefined in include/linux/pm.h:
- struct timer_list suspend_timer;
timer used for scheduling (delayed) suspend and autosuspend requests
- unsigned long timer_expires;
timer expiration time, in jiffies (if this is different from zero, thetimer is running and will expire at that time, otherwise the timer is notrunning)
- struct work_struct work;
work structure used for queuing up requests (i.e. work items in pm_wq)
- wait_queue_head_t wait_queue;
wait queue used if any of the helper functions needs to wait for anotherone to complete
- spinlock_t lock;
lock used for synchronization
- atomic_t usage_count;
the usage counter of the device
- atomic_t child_count;
the count of ‘active’ children of the device
- unsigned int ignore_children;
if set, the value of child_count is ignored (but still updated)
- unsigned int disable_depth;
used for disabling the helper functions (they work normally if this isequal to zero); the initial value of it is 1 (i.e. runtime PM isinitially disabled for all devices)
- int runtime_error;
if set, there was a fatal error (one of the callbacks returned error codeas described in Section 2), so the helper functions will not work untilthis flag is cleared; this is the error code returned by the failingcallback
- unsigned int idle_notification;
if set, ->
runtime_idle()is being executed- unsigned int request_pending;
if set, there’s a pending request (i.e. a work item queued up into pm_wq)
- enum rpm_request request;
type of request that’s pending (valid if request_pending is set)
- unsigned int deferred_resume;
set if ->
runtime_resume()is about to be run while ->runtime_suspend()isbeing executed for that device and it is not practical to wait for thesuspend to complete; means “start a resume as soon as you’ve suspended”- enum rpm_status runtime_status;
the runtime PM status of the device; this field’s initial value isRPM_SUSPENDED, which means that each device is initially regarded by thePM core as ‘suspended’, regardless of its real hardware status
- enum rpm_status last_status;
the last runtime PM status of the device captured before disabling runtimePM for it (invalid initially and when disable_depth is 0)
- unsigned int runtime_auto;
if set, indicates that the user space has allowed the device driver topower manage the device at run time via the /sys/devices/.../power/controlinterface; it may only be modified with the help of the
pm_runtime_allow()andpm_runtime_forbid()helper functions- unsigned int no_callbacks;
indicates that the device does not use the runtime PM callbacks (seeSection 8); it may be modified only by the
pm_runtime_no_callbacks()helper function- unsigned int irq_safe;
indicates that the ->
runtime_suspend()and ->runtime_resume()callbackswill be invoked with the spinlock held and interrupts disabled- unsigned int use_autosuspend;
indicates that the device’s driver supports delayed autosuspend (seeSection 9); it may be modified only by thepm_runtime{_dont}
_use_autosuspend()helper functions- unsigned int timer_autosuspends;
indicates that the PM core should attempt to carry out an autosuspendwhen the timer expires rather than a normal suspend
- int autosuspend_delay;
the delay time (in milliseconds) to be used for autosuspend
- unsigned long last_busy;
the time (in jiffies) when the
pm_runtime_mark_last_busy()helperfunction was last called for this device; used in calculating inactivityperiods for autosuspend
All of the above fields are members of the ‘power’ member of ‘structdevice’.
4. Runtime PM Device Helper Functions¶
The following runtime PM helper functions are defined indrivers/base/power/runtime.c and include/linux/pm_runtime.h:
- void pm_runtime_init(struct device *dev);
initialize the device runtime PM fields in ‘
structdev_pm_info’- void pm_runtime_remove(struct device *dev);
make sure that the runtime PM of the device will be disabled afterremoving the device from device hierarchy
- int pm_runtime_idle(struct device *dev);
execute the subsystem-level idle callback for the device; returns anerror code on failure, where -EINPROGRESS means that ->
runtime_idle()isalready being executed; if there is no callback or the callback returns 0then run pm_runtime_autosuspend(dev) and return its result- int pm_runtime_suspend(struct device *dev);
execute the subsystem-level suspend callback for the device; returns 0 onsuccess, 1 if the device’s runtime PM status was already ‘suspended’, orerror code on failure, where -EAGAIN or -EBUSY means it is safe to attemptto suspend the device again in future and -EACCES means that‘power.disable_depth’ is different from 0
- int pm_runtime_autosuspend(struct device *dev);
same as
pm_runtime_suspend()except that a call topm_runtime_mark_last_busy()is made and an autosuspend is scheduled forthe appropriate time and 0 is returned- int pm_runtime_resume(struct device *dev);
execute the subsystem-level resume callback for the device; returns 0 onsuccess, 1 if the device’s runtime PM status is already ‘active’ (also if‘power.disable_depth’ is nonzero, but the status was ‘active’ when it waschanging from 0 to 1) or error code on failure, where -EAGAIN means it maybe safe to attempt to resume the device again in future, but‘power.runtime_error’ should be checked additionally, and -EACCES meansthat the callback could not be run, because ‘power.disable_depth’ wasdifferent from 0
- int pm_runtime_resume_and_get(struct device *dev);
run pm_runtime_resume(dev) and if successful, increment the device’susage counter; returns 0 on success (whether or not the device’sruntime PM status was already ‘active’) or the error code from
pm_runtime_resume()on failure.- int pm_request_idle(struct device *dev);
submit a request to execute the subsystem-level idle callback for thedevice (the request is represented by a work item in pm_wq); returns 0 onsuccess or error code if the request has not been queued up
- int pm_request_autosuspend(struct device *dev);
Call
pm_runtime_mark_last_busy()and schedule the execution of thesubsystem-level suspend callback for the device when the autosuspend delayexpires- int pm_schedule_suspend(struct device *dev, unsigned int delay);
schedule the execution of the subsystem-level suspend callback for thedevice in future, where ‘delay’ is the time to wait before queuing up asuspend work item in pm_wq, in milliseconds (if ‘delay’ is zero, the workitem is queued up immediately); returns 0 on success, 1 if the device’s PMruntime status was already ‘suspended’, or error code if the requesthasn’t been scheduled (or queued up if ‘delay’ is 0); if the execution of->
runtime_suspend()is already scheduled and not yet expired, the newvalue of ‘delay’ will be used as the time to wait- int pm_request_resume(struct device *dev);
submit a request to execute the subsystem-level resume callback for thedevice (the request is represented by a work item in pm_wq); returns 0 onsuccess, 1 if the device’s runtime PM status was already ‘active’, orerror code if the request hasn’t been queued up
- void pm_runtime_get_noresume(struct device *dev);
increment the device’s usage counter
- int pm_runtime_get(struct device *dev);
increment the device’s usage counter, run pm_request_resume(dev) andreturn its result
- int pm_runtime_get_sync(struct device *dev);
increment the device’s usage counter, run pm_runtime_resume(dev) andreturn its result;note that it does not drop the device’s usage counter on errors, soconsider using
pm_runtime_resume_and_get()instead of it, especiallyif its return value is checked by the caller, as this is likely toresult in cleaner code.- int pm_runtime_get_if_in_use(struct device *dev);
return -EINVAL if ‘power.disable_depth’ is nonzero; otherwise, if theruntime PM status is RPM_ACTIVE and the runtime PM usage counter isnonzero, increment the counter and return 1; otherwise return 0 withoutchanging the counter
- int pm_runtime_get_if_active(struct device *dev);
return -EINVAL if ‘power.disable_depth’ is nonzero; otherwise, if theruntime PM status is RPM_ACTIVE, increment the counter andreturn 1; otherwise return 0 without changing the counter
- void pm_runtime_put_noidle(struct device *dev);
decrement the device’s usage counter
- int pm_runtime_put(struct device *dev);
decrement the device’s usage counter; if the result is 0 then runpm_request_idle(dev) and return its result
- int pm_runtime_put_autosuspend(struct device *dev);
set the power.last_busy field to the current time and decrement thedevice’s usage counter; if the result is 0 then runpm_request_autosuspend(dev) and return its result
- int __pm_runtime_put_autosuspend(struct device *dev);
decrement the device’s usage counter; if the result is 0 then runpm_request_autosuspend(dev) and return its result
- int pm_runtime_put_sync(struct device *dev);
decrement the device’s usage counter; if the result is 0 then runpm_runtime_idle(dev) and return its result
- int pm_runtime_put_sync_suspend(struct device *dev);
decrement the device’s usage counter; if the result is 0 then runpm_runtime_suspend(dev) and return its result
- int pm_runtime_put_sync_autosuspend(struct device *dev);
set the power.last_busy field to the current time and decrement thedevice’s usage counter; if the result is 0 then runpm_runtime_autosuspend(dev) and return its result
- void pm_runtime_enable(struct device *dev);
decrement the device’s ‘power.disable_depth’ field; if that field is equalto zero, the runtime PM helper functions can execute subsystem-levelcallbacks described in Section 2 for the device
- int pm_runtime_disable(struct device *dev);
increment the device’s ‘power.disable_depth’ field (if the value of thatfield was previously zero, this prevents subsystem-level runtime PMcallbacks from being run for the device), make sure that all of thepending runtime PM operations on the device are either completed orcanceled; returns 1 if there was a resume request pending and it wasnecessary to execute the subsystem-level resume callback for the deviceto satisfy that request, otherwise 0 is returned
- void pm_runtime_barrier(struct device *dev);
check if there’s a resume request pending for the device and resume it(synchronously) in that case, cancel any other pending runtime PM requestsregarding it and wait for all runtime PM operations on it in progress tocomplete
- void pm_suspend_ignore_children(struct device *dev, bool enable);
set/unset the power.ignore_children flag of the device
- int pm_runtime_set_active(struct device *dev);
clear the device’s ‘power.runtime_error’ flag, set the device’s runtimePM status to ‘active’ and update its parent’s counter of ‘active’children as appropriate (it is only valid to use this function if‘power.runtime_error’ is set or ‘power.disable_depth’ is greater thanzero); it will fail and return error code if the device has a parentwhich is not active and the ‘power.ignore_children’ flag of which is unset
- void pm_runtime_set_suspended(struct device *dev);
clear the device’s ‘power.runtime_error’ flag, set the device’s runtimePM status to ‘suspended’ and update its parent’s counter of ‘active’children as appropriate (it is only valid to use this function if‘power.runtime_error’ is set or ‘power.disable_depth’ is greater thanzero)
- bool pm_runtime_active(struct device *dev);
return true if the device’s runtime PM status is ‘active’ or its‘power.disable_depth’ field is not equal to zero, or false otherwise
- bool pm_runtime_suspended(struct device *dev);
return true if the device’s runtime PM status is ‘suspended’ and its‘power.disable_depth’ field is equal to zero, or false otherwise
- bool pm_runtime_status_suspended(struct device *dev);
return true if the device’s runtime PM status is ‘suspended’
- void pm_runtime_no_callbacks(struct device *dev);
set the power.no_callbacks flag for the device and remove the runtimePM attributes from /sys/devices/.../power (or prevent them from beingadded when the device is registered)
- void pm_runtime_irq_safe(struct device *dev);
set the power.irq_safe flag for the device, causing the runtime-PMcallbacks to be invoked with interrupts off
- bool pm_runtime_is_irq_safe(struct device *dev);
return true if power.irq_safe flag was set for the device, causingthe runtime-PM callbacks to be invoked with interrupts off
- void pm_runtime_mark_last_busy(struct device *dev);
set the power.last_busy field to the current time
- void pm_runtime_use_autosuspend(struct device *dev);
set the power.use_autosuspend flag, enabling autosuspend delays; callpm_runtime_get_sync if the flag was previously cleared andpower.autosuspend_delay is negative
- void pm_runtime_dont_use_autosuspend(struct device *dev);
clear the power.use_autosuspend flag, disabling autosuspend delays;decrement the device’s usage counter if the flag was previously set andpower.autosuspend_delay is negative; call pm_runtime_idle
- void pm_runtime_set_autosuspend_delay(struct device *dev, int delay);
set the power.autosuspend_delay value to ‘delay’ (expressed inmilliseconds); if ‘delay’ is negative then runtime suspends areprevented; if power.use_autosuspend is set, pm_runtime_get_sync may becalled or the device’s usage counter may be decremented andpm_runtime_idle called depending on if power.autosuspend_delay ischanged to or from a negative value; if power.use_autosuspend is clear,pm_runtime_idle is called
- unsigned long pm_runtime_autosuspend_expiration(struct device *dev);
calculate the time when the current autosuspend delay period will expire,based on power.last_busy and power.autosuspend_delay; if the delay timeis 1000 ms or larger then the expiration time is rounded up to thenearest second; returns 0 if the delay period has already expired orpower.use_autosuspend isn’t set, otherwise returns the expiration timein jiffies
It is safe to execute the following helper functions from interrupt context:
pm_request_idle()pm_request_autosuspend()pm_schedule_suspend()pm_request_resume()pm_runtime_get_noresume()pm_runtime_get()pm_runtime_put_noidle()pm_runtime_put()pm_runtime_put_autosuspend()__pm_runtime_put_autosuspend()pm_runtime_enable()pm_suspend_ignore_children()pm_runtime_set_active()pm_runtime_set_suspended()pm_runtime_suspended()pm_runtime_mark_last_busy()pm_runtime_autosuspend_expiration()
Ifpm_runtime_irq_safe() has been called for a device then the following helperfunctions may also be used in interrupt context:
pm_runtime_idle()pm_runtime_suspend()pm_runtime_autosuspend()pm_runtime_resume()pm_runtime_get_sync()pm_runtime_put_sync()pm_runtime_put_sync_suspend()pm_runtime_put_sync_autosuspend()
5. Runtime PM Initialization, Device Probing and Removal¶
Initially, the runtime PM is disabled for all devices, which means that themajority of the runtime PM helper functions described in Section 4 will return-EAGAIN untilpm_runtime_enable() is called for the device.
In addition to that, the initial runtime PM status of all devices is‘suspended’, but it need not reflect the actual physical state of the device.Thus, if the device is initially active (i.e. it is able to process I/O), itsruntime PM status must be changed to ‘active’, with the help ofpm_runtime_set_active(), beforepm_runtime_enable() is called for the device.
However, if the device has a parent and the parent’s runtime PM is enabled,callingpm_runtime_set_active() for the device will affect the parent, unlessthe parent’s ‘power.ignore_children’ flag is set. Namely, in that case theparent won’t be able to suspend at run time, using the PM core’s helperfunctions, as long as the child’s status is ‘active’, even if the child’sruntime PM is still disabled (i.e.pm_runtime_enable() hasn’t been called forthe child yet orpm_runtime_disable() has been called for it). For this reason,oncepm_runtime_set_active() has been called for the device,pm_runtime_enable()should be called for it too as soon as reasonably possible or its runtime PMstatus should be changed back to ‘suspended’ with the help ofpm_runtime_set_suspended().
If the default initial runtime PM status of the device (i.e. ‘suspended’)reflects the actual state of the device, its bus type’s or its driver’s->probe() callback will likely need to wake it up using one of the PM core’shelper functions described in Section 4. In that case,pm_runtime_resume()should be used. Of course, for this purpose the device’s runtime PM has to beenabled earlier by callingpm_runtime_enable().
Note, if the device may execute pm_runtime calls during the probe (such asif it is registered with a subsystem that may call back in) then thepm_runtime_get_sync() call paired with apm_runtime_put() call will beappropriate to ensure that the device is not put back to sleep during theprobe. This can happen with systems such as the network device layer.
It may be desirable to suspend the device once ->probe() has finished.Therefore the driver core uses the asynchronouspm_request_idle() to submit arequest to execute the subsystem-level idle callback for the device at thattime. A driver that makes use of the runtime autosuspend feature may want toupdate the last busy mark before returning from ->probe().
Moreover, the driver core prevents runtime PM callbacks from racing with the busnotifier callback in__device_release_driver(), which is necessary because thenotifier is used by some subsystems to carry out operations affecting theruntime PM functionality. It does so by callingpm_runtime_get_sync() beforedriver_sysfs_remove() and the BUS_NOTIFY_UNBIND_DRIVER notifications. Thisresumes the device if it’s in the suspended state and prevents it frombeing suspended again while those routines are being executed.
To allow bus types and drivers to put devices into the suspended state bycallingpm_runtime_suspend() from their ->remove() routines, the driver coreexecutespm_runtime_put_sync() after running the BUS_NOTIFY_UNBIND_DRIVERnotifications in__device_release_driver(). This requires bus types anddrivers to make their ->remove() callbacks avoid races with runtime PM directly,but it also allows more flexibility in the handling of devices during theremoval of their drivers.
Drivers in ->remove() callback should undo the runtime PM changes donein ->probe(). Usually this means callingpm_runtime_disable(),pm_runtime_dont_use_autosuspend() etc.
The user space can effectively disallow the driver of the device to power manageit at run time by changing the value of its /sys/devices/.../power/controlattribute to “on”, which causespm_runtime_forbid() to be called. In principle,this mechanism may also be used by the driver to effectively turn off theruntime power management of the device until the user space turns it on.Namely, during the initialization the driver can make sure that the runtime PMstatus of the device is ‘active’ and callpm_runtime_forbid(). It should benoted, however, that if the user space has already intentionally changed thevalue of /sys/devices/.../power/control to “auto” to allow the driver to powermanage the device at run time, the driver may confuse it by usingpm_runtime_forbid() this way.
6. Runtime PM and System Sleep¶
Runtime PM and system sleep (i.e., system suspend and hibernation, also knownas suspend-to-RAM and suspend-to-disk) interact with each other in a couple ofways. If a device is active when a system sleep starts, everything isstraightforward. But what should happen if the device is already suspended?
The device may have different wake-up settings for runtime PM and system sleep.For example, remote wake-up may be enabled for runtime suspend but disallowedfor system sleep (device_may_wakeup(dev) returns ‘false’). When this happens,the subsystem-level system suspend callback is responsible for changing thedevice’s wake-up setting (it may leave that to the device driver’s systemsuspend routine). It may be necessary to resume the device and suspend it againin order to do so. The same is true if the driver uses different power levelsor other settings for runtime suspend and system sleep.
During system resume, the simplest approach is to bring all devices back to fullpower, even if they had been suspended before the system suspend began. Thereare several reasons for this, including:
The device might need to switch power levels, wake-up settings, etc.
Remote wake-up events might have been lost by the firmware.
The device’s children may need the device to be at full power in orderto resume themselves.
The driver’s idea of the device state may not agree with the device’sphysical state. This can happen during resume from hibernation.
The device might need to be reset.
Even though the device was suspended, if its usage counter was > 0 then mostlikely it would need a runtime resume in the near future anyway.
If the device had been suspended before the system suspend began and it’sbrought back to full power during resume, then its runtime PM status will haveto be updated to reflect the actual post-system sleep status. The way to dothis is:
pm_runtime_disable(dev);
pm_runtime_set_active(dev);
pm_runtime_enable(dev);
The PM core always increments the runtime usage counter before calling the->suspend() callback and decrements it after calling the ->resume() callback.Hence disabling runtime PM temporarily like this will not cause any runtimesuspend attempts to be permanently lost. If the usage count goes to zerofollowing the return of the ->resume() callback, the ->runtime_idle() callbackwill be invoked as usual.
On some systems, however, system sleep is not entered through a global firmwareor hardware operation. Instead, all hardware components are put into low-powerstates directly by the kernel in a coordinated way. Then, the system sleepstate effectively follows from the states the hardware components end up inand the system is woken up from that state by a hardware interrupt or a similarmechanism entirely under the kernel’s control. As a result, the kernel nevergives control away and the states of all devices during resume are preciselyknown to it. If that is the case and none of the situations listed above takesplace (in particular, if the system is not waking up from hibernation), it maybe more efficient to leave the devices that had been suspended before the systemsuspend began in the suspended state.
To this end, the PM core provides a mechanism allowing some coordination betweendifferent levels of device hierarchy. Namely, if a system suspend .prepare()callback returns a positive number for a device, that indicates to the PM corethat the device appears to be runtime-suspended and its state is fine, so itmay be left in runtime suspend provided that all of its descendants are alsoleft in runtime suspend. If that happens, the PM core will not execute anysystem suspend and resume callbacks for all of those devices, except for the.complete() callback, which is then entirely responsible for handling the deviceas appropriate. This only applies to system suspend transitions that are notrelated to hibernation (seeDevice Power Management Basics for moreinformation).
The PM core does its best to reduce the probability of race conditions betweenthe runtime PM and system suspend/resume (and hibernation) callbacks by carryingout the following operations:
During system suspend
pm_runtime_get_noresume()is called for every deviceright before executing the subsystem-level .prepare()callback for it andpm_runtime_barrier()is called for every device right before executing thesubsystem-level .suspend()callback for it. In addition to that the PM corecalls__pm_runtime_disable()with ‘false’ as the second argument for everydevice right before executing the subsystem-level .suspend_late()callbackfor it.During system resume
pm_runtime_enable()andpm_runtime_put()are called forevery device right after executing the subsystem-level .resume_early()callback and right after executing the subsystem-level .complete()callbackfor it, respectively.
7. Generic subsystem callbacks¶
Subsystems may wish to conserve code space by using the set of generic powermanagement callbacks provided by the PM core, defined indriver/base/power/generic_ops.c:
- int pm_generic_runtime_suspend(struct device *dev);
invoke the ->
runtime_suspend()callback provided by the driver of thisdevice and return its result, or return 0 if not defined- int pm_generic_runtime_resume(struct device *dev);
invoke the ->
runtime_resume()callback provided by the driver of thisdevice and return its result, or return 0 if not defined- int pm_generic_suspend(struct device *dev);
if the device has not been suspended at run time, invoke the ->
suspend()callback provided by its driver and return its result, or return 0 if notdefined- int pm_generic_suspend_noirq(struct device *dev);
if pm_runtime_suspended(dev) returns “false”, invoke the ->
suspend_noirq()callback provided by the device’s driver and return its result, or return0 if not defined- int pm_generic_resume(struct device *dev);
invoke the ->
resume()callback provided by the driver of this device and,if successful, change the device’s runtime PM status to ‘active’- int pm_generic_resume_noirq(struct device *dev);
invoke the ->
resume_noirq()callback provided by the driver of this device- int pm_generic_freeze(struct device *dev);
if the device has not been suspended at run time, invoke the ->
freeze()callback provided by its driver and return its result, or return 0 if notdefined- int pm_generic_freeze_noirq(struct device *dev);
if pm_runtime_suspended(dev) returns “false”, invoke the ->
freeze_noirq()callback provided by the device’s driver and return its result, or return0 if not defined- int pm_generic_thaw(struct device *dev);
if the device has not been suspended at run time, invoke the ->
thaw()callback provided by its driver and return its result, or return 0 if notdefined- int pm_generic_thaw_noirq(struct device *dev);
if pm_runtime_suspended(dev) returns “false”, invoke the ->
thaw_noirq()callback provided by the device’s driver and return its result, or return0 if not defined- int pm_generic_poweroff(struct device *dev);
if the device has not been suspended at run time, invoke the ->
poweroff()callback provided by its driver and return its result, or return 0 if notdefined- int pm_generic_poweroff_noirq(struct device *dev);
if pm_runtime_suspended(dev) returns “false”, run the ->
poweroff_noirq()callback provided by the device’s driver and return its result, or return0 if not defined- int pm_generic_restore(struct device *dev);
invoke the ->
restore()callback provided by the driver of this device and,if successful, change the device’s runtime PM status to ‘active’- int pm_generic_restore_noirq(struct device *dev);
invoke the ->
restore_noirq()callback provided by the device’s driver
These functions are the defaults used by the PM core if a subsystem doesn’tprovide its own callbacks for ->runtime_idle(), ->runtime_suspend(),->runtime_resume(), ->suspend(), ->suspend_noirq(), ->resume(),->resume_noirq(), ->freeze(), ->freeze_noirq(), ->thaw(), ->thaw_noirq(),->poweroff(), ->poweroff_noirq(), ->restore(), ->restore_noirq() in thesubsystem-level dev_pm_ops structure.
Device drivers that wish to use the same function as a system suspend, freeze,poweroff and runtime suspend callback, and similarly for system resume, thaw,restore, and runtime resume, can achieve similar behaviour with the help of theDEFINE_RUNTIME_DEV_PM_OPS() defined in include/linux/pm_runtime.h (possibly setting itslast argument to NULL).
8. “No-Callback” Devices¶
Some “devices” are only logical sub-devices of their parent and cannot bepower-managed on their own. (The prototype example is a USB interface. EntireUSB devices can go into low-power mode or send wake-up requests, but neither ispossible for individual interfaces.) The drivers for these devices have noneed of runtime PM callbacks; if the callbacks did exist, ->runtime_suspend()and ->runtime_resume() would always return 0 without doing anything else and->runtime_idle() would always callpm_runtime_suspend().
Subsystems can tell the PM core about these devices by callingpm_runtime_no_callbacks(). This should be done after the device structure isinitialized and before it is registered (although after device registration isalso okay). The routine will set the device’s power.no_callbacks flag andprevent the non-debugging runtime PM sysfs attributes from being created.
When power.no_callbacks is set, the PM core will not invoke the->runtime_idle(), ->runtime_suspend(), or ->runtime_resume() callbacks.Instead it will assume that suspends and resumes always succeed and that idledevices should be suspended.
As a consequence, the PM core will never directly inform the device’s subsystemor driver about runtime power changes. Instead, the driver for the device’sparent must take responsibility for telling the device’s driver when theparent’s power state changes.
Note that, in some cases it may not be desirable for subsystems/drivers to callpm_runtime_no_callbacks() for their devices. This could be because a subset ofthe runtime PM callbacks needs to be implemented, a platform dependent PMdomain could get attached to the device or that the device is power managedthrough a supplier device link. For these reasons and to avoid boilerplate codein subsystems/drivers, the PM core allows runtime PM callbacks to beunassigned. More precisely, if a callback pointer is NULL, the PM core will actas though there was a callback and it returned 0.
9. Autosuspend, or automatically-delayed suspends¶
Changing a device’s power state isn’t free; it requires both time and energy.A device should be put in a low-power state only when there’s some reason tothink it will remain in that state for a substantial time. A common heuristicsays that a device which hasn’t been used for a while is liable to remainunused; following this advice, drivers should not allow devices to be suspendedat runtime until they have been inactive for some minimum period. Even whenthe heuristic ends up being non-optimal, it will still prevent devices from“bouncing” too rapidly between low-power and full-power states.
The term “autosuspend” is an historical remnant. It doesn’t mean that thedevice is automatically suspended (the subsystem or driver still has to callthe appropriate PM routines); rather it means that runtime suspends willautomatically be delayed until the desired period of inactivity has elapsed.
Inactivity is determined based on the power.last_busy field. The desired lengthof the inactivity period is a matter of policy. Subsystems can set this lengthinitially by callingpm_runtime_set_autosuspend_delay(), but after deviceregistration the length should be controlled by user space, using the/sys/devices/.../power/autosuspend_delay_ms attribute.
In order to use autosuspend, subsystems or drivers must callpm_runtime_use_autosuspend() (preferably before registering the device), andthereafter they should use the various*_autosuspend() helper functionsinstead of the non-autosuspend counterparts:
Instead of: pm_runtime_suspend use: pm_runtime_autosuspend;Instead of: pm_schedule_suspend use: pm_request_autosuspend;Instead of: pm_runtime_put use: pm_runtime_put_autosuspend;Instead of: pm_runtime_put_sync use: pm_runtime_put_sync_autosuspend.
Drivers may also continue to use the non-autosuspend helper functions; theywill behave normally, which means sometimes taking the autosuspend delay intoaccount (see pm_runtime_idle). The autosuspend variants of the functions alsocallpm_runtime_mark_last_busy().
Under some circumstances a driver or subsystem may want to prevent a devicefrom autosuspending immediately, even though the usage counter is zero and theautosuspend delay time has expired. If the ->runtime_suspend() callbackreturns -EAGAIN or -EBUSY, and if the next autosuspend delay expiration time isin the future (as it normally would be if the callback invokedpm_runtime_mark_last_busy()), the PM core will automatically reschedule theautosuspend. The ->runtime_suspend() callback can’t do this reschedulingitself because no suspend requests of any kind are accepted while the device issuspending (i.e., while the callback is running).
The implementation is well suited for asynchronous use in interrupt contexts.However such use inevitably involves races, because the PM core can’tsynchronize ->runtime_suspend() callbacks with the arrival of I/O requests.This synchronization must be handled by the driver, using its private lock.Here is a schematic pseudo-code example:
foo_read_or_write(struct foo_priv *foo, void *data){ lock(&foo->private_lock); add_request_to_io_queue(foo, data); if (foo->num_pending_requests++ == 0) pm_runtime_get(&foo->dev); if (!foo->is_suspended) foo_process_next_request(foo); unlock(&foo->private_lock);}foo_io_completion(struct foo_priv *foo, void *req){ lock(&foo->private_lock); if (--foo->num_pending_requests == 0) pm_runtime_put_autosuspend(&foo->dev); else foo_process_next_request(foo); unlock(&foo->private_lock); /* Send req result back to the user ... */}int foo_runtime_suspend(struct device *dev){ struct foo_priv foo = container_of(dev, ...); int ret = 0; lock(&foo->private_lock); if (foo->num_pending_requests > 0) { ret = -EBUSY; } else { /* ... suspend the device ... */ foo->is_suspended = 1; } unlock(&foo->private_lock); return ret;}int foo_runtime_resume(struct device *dev){ struct foo_priv foo = container_of(dev, ...); lock(&foo->private_lock); /* ... resume the device ... */ foo->is_suspended = 0; pm_runtime_mark_last_busy(&foo->dev); if (foo->num_pending_requests > 0) foo_process_next_request(foo); unlock(&foo->private_lock); return 0;}The important point is that afterfoo_io_completion() asks for an autosuspend,thefoo_runtime_suspend() callback may race withfoo_read_or_write().Thereforefoo_runtime_suspend() has to check whether there are any pending I/Orequests (while holding the private lock) before allowing the suspend toproceed.
In addition, the power.autosuspend_delay field can be changed by user space atany time. If a driver cares about this, it can callpm_runtime_autosuspend_expiration() from within the ->runtime_suspend()callback while holding its private lock. If the function returns a nonzerovalue then the delay has not yet expired and the callback should return-EAGAIN.