xref: /OK3568_Linux_fs/kernel/Documentation/power/pm_qos_interface.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun===============================
2*4882a593SmuzhiyunPM Quality Of Service Interface
3*4882a593Smuzhiyun===============================
4*4882a593Smuzhiyun
5*4882a593SmuzhiyunThis interface provides a kernel and user mode interface for registering
6*4882a593Smuzhiyunperformance expectations by drivers, subsystems and user space applications on
7*4882a593Smuzhiyunone of the parameters.
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunTwo different PM QoS frameworks are available:
10*4882a593Smuzhiyun * CPU latency QoS.
11*4882a593Smuzhiyun * The per-device PM QoS framework provides the API to manage the
12*4882a593Smuzhiyun   per-device latency constraints and PM QoS flags.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunThe latency unit used in the PM QoS framework is the microsecond (usec).
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun1. PM QoS framework
18*4882a593Smuzhiyun===================
19*4882a593Smuzhiyun
20*4882a593SmuzhiyunA global list of CPU latency QoS requests is maintained along with an aggregated
21*4882a593Smuzhiyun(effective) target value.  The aggregated target value is updated with changes
22*4882a593Smuzhiyunto the request list or elements of the list.  For CPU latency QoS, the
23*4882a593Smuzhiyunaggregated target value is simply the min of the request values held in the list
24*4882a593Smuzhiyunelements.
25*4882a593Smuzhiyun
26*4882a593SmuzhiyunNote: the aggregated target value is implemented as an atomic variable so that
27*4882a593Smuzhiyunreading the aggregated value does not require any locking mechanism.
28*4882a593Smuzhiyun
29*4882a593SmuzhiyunFrom kernel space the use of this interface is simple:
30*4882a593Smuzhiyun
31*4882a593Smuzhiyunvoid cpu_latency_qos_add_request(handle, target_value):
32*4882a593Smuzhiyun  Will insert an element into the CPU latency QoS list with the target value.
33*4882a593Smuzhiyun  Upon change to this list the new target is recomputed and any registered
34*4882a593Smuzhiyun  notifiers are called only if the target value is now different.
35*4882a593Smuzhiyun  Clients of PM QoS need to save the returned handle for future use in other
36*4882a593Smuzhiyun  PM QoS API functions.
37*4882a593Smuzhiyun
38*4882a593Smuzhiyunvoid cpu_latency_qos_update_request(handle, new_target_value):
39*4882a593Smuzhiyun  Will update the list element pointed to by the handle with the new target
40*4882a593Smuzhiyun  value and recompute the new aggregated target, calling the notification tree
41*4882a593Smuzhiyun  if the target is changed.
42*4882a593Smuzhiyun
43*4882a593Smuzhiyunvoid cpu_latency_qos_remove_request(handle):
44*4882a593Smuzhiyun  Will remove the element.  After removal it will update the aggregate target
45*4882a593Smuzhiyun  and call the notification tree if the target was changed as a result of
46*4882a593Smuzhiyun  removing the request.
47*4882a593Smuzhiyun
48*4882a593Smuzhiyunint cpu_latency_qos_limit():
49*4882a593Smuzhiyun  Returns the aggregated value for the CPU latency QoS.
50*4882a593Smuzhiyun
51*4882a593Smuzhiyunint cpu_latency_qos_request_active(handle):
52*4882a593Smuzhiyun  Returns if the request is still active, i.e. it has not been removed from the
53*4882a593Smuzhiyun  CPU latency QoS list.
54*4882a593Smuzhiyun
55*4882a593Smuzhiyunint cpu_latency_qos_add_notifier(notifier):
56*4882a593Smuzhiyun  Adds a notification callback function to the CPU latency QoS. The callback is
57*4882a593Smuzhiyun  called when the aggregated value for the CPU latency QoS is changed.
58*4882a593Smuzhiyun
59*4882a593Smuzhiyunint cpu_latency_qos_remove_notifier(notifier):
60*4882a593Smuzhiyun  Removes the notification callback function from the CPU latency QoS.
61*4882a593Smuzhiyun
62*4882a593Smuzhiyun
63*4882a593SmuzhiyunFrom user space:
64*4882a593Smuzhiyun
65*4882a593SmuzhiyunThe infrastructure exposes one device node, /dev/cpu_dma_latency, for the CPU
66*4882a593Smuzhiyunlatency QoS.
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunOnly processes can register a PM QoS request.  To provide for automatic
69*4882a593Smuzhiyuncleanup of a process, the interface requires the process to register its
70*4882a593Smuzhiyunparameter requests as follows.
71*4882a593Smuzhiyun
72*4882a593SmuzhiyunTo register the default PM QoS target for the CPU latency QoS, the process must
73*4882a593Smuzhiyunopen /dev/cpu_dma_latency.
74*4882a593Smuzhiyun
75*4882a593SmuzhiyunAs long as the device node is held open that process has a registered
76*4882a593Smuzhiyunrequest on the parameter.
77*4882a593Smuzhiyun
78*4882a593SmuzhiyunTo change the requested target value, the process needs to write an s32 value to
79*4882a593Smuzhiyunthe open device node.  Alternatively, it can write a hex string for the value
80*4882a593Smuzhiyunusing the 10 char long format e.g. "0x12345678".  This translates to a
81*4882a593Smuzhiyuncpu_latency_qos_update_request() call.
82*4882a593Smuzhiyun
83*4882a593SmuzhiyunTo remove the user mode request for a target value simply close the device
84*4882a593Smuzhiyunnode.
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun2. PM QoS per-device latency and flags framework
88*4882a593Smuzhiyun================================================
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunFor each device, there are three lists of PM QoS requests. Two of them are
91*4882a593Smuzhiyunmaintained along with the aggregated targets of resume latency and active
92*4882a593Smuzhiyunstate latency tolerance (in microseconds) and the third one is for PM QoS flags.
93*4882a593SmuzhiyunValues are updated in response to changes of the request list.
94*4882a593Smuzhiyun
95*4882a593SmuzhiyunThe target values of resume latency and active state latency tolerance are
96*4882a593Smuzhiyunsimply the minimum of the request values held in the parameter list elements.
97*4882a593SmuzhiyunThe PM QoS flags aggregate value is a gather (bitwise OR) of all list elements'
98*4882a593Smuzhiyunvalues.  One device PM QoS flag is defined currently: PM_QOS_FLAG_NO_POWER_OFF.
99*4882a593Smuzhiyun
100*4882a593SmuzhiyunNote: The aggregated target values are implemented in such a way that reading
101*4882a593Smuzhiyunthe aggregated value does not require any locking mechanism.
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun
104*4882a593SmuzhiyunFrom kernel mode the use of this interface is the following:
105*4882a593Smuzhiyun
106*4882a593Smuzhiyunint dev_pm_qos_add_request(device, handle, type, value):
107*4882a593Smuzhiyun  Will insert an element into the list for that identified device with the
108*4882a593Smuzhiyun  target value.  Upon change to this list the new target is recomputed and any
109*4882a593Smuzhiyun  registered notifiers are called only if the target value is now different.
110*4882a593Smuzhiyun  Clients of dev_pm_qos need to save the handle for future use in other
111*4882a593Smuzhiyun  dev_pm_qos API functions.
112*4882a593Smuzhiyun
113*4882a593Smuzhiyunint dev_pm_qos_update_request(handle, new_value):
114*4882a593Smuzhiyun  Will update the list element pointed to by the handle with the new target
115*4882a593Smuzhiyun  value and recompute the new aggregated target, calling the notification
116*4882a593Smuzhiyun  trees if the target is changed.
117*4882a593Smuzhiyun
118*4882a593Smuzhiyunint dev_pm_qos_remove_request(handle):
119*4882a593Smuzhiyun  Will remove the element.  After removal it will update the aggregate target
120*4882a593Smuzhiyun  and call the notification trees if the target was changed as a result of
121*4882a593Smuzhiyun  removing the request.
122*4882a593Smuzhiyun
123*4882a593Smuzhiyuns32 dev_pm_qos_read_value(device, type):
124*4882a593Smuzhiyun  Returns the aggregated value for a given device's constraints list.
125*4882a593Smuzhiyun
126*4882a593Smuzhiyunenum pm_qos_flags_status dev_pm_qos_flags(device, mask)
127*4882a593Smuzhiyun  Check PM QoS flags of the given device against the given mask of flags.
128*4882a593Smuzhiyun  The meaning of the return values is as follows:
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun	PM_QOS_FLAGS_ALL:
131*4882a593Smuzhiyun		All flags from the mask are set
132*4882a593Smuzhiyun	PM_QOS_FLAGS_SOME:
133*4882a593Smuzhiyun		Some flags from the mask are set
134*4882a593Smuzhiyun	PM_QOS_FLAGS_NONE:
135*4882a593Smuzhiyun		No flags from the mask are set
136*4882a593Smuzhiyun	PM_QOS_FLAGS_UNDEFINED:
137*4882a593Smuzhiyun		The device's PM QoS structure has not been initialized
138*4882a593Smuzhiyun		or the list of requests is empty.
139*4882a593Smuzhiyun
140*4882a593Smuzhiyunint dev_pm_qos_add_ancestor_request(dev, handle, type, value)
141*4882a593Smuzhiyun  Add a PM QoS request for the first direct ancestor of the given device whose
142*4882a593Smuzhiyun  power.ignore_children flag is unset (for DEV_PM_QOS_RESUME_LATENCY requests)
143*4882a593Smuzhiyun  or whose power.set_latency_tolerance callback pointer is not NULL (for
144*4882a593Smuzhiyun  DEV_PM_QOS_LATENCY_TOLERANCE requests).
145*4882a593Smuzhiyun
146*4882a593Smuzhiyunint dev_pm_qos_expose_latency_limit(device, value)
147*4882a593Smuzhiyun  Add a request to the device's PM QoS list of resume latency constraints and
148*4882a593Smuzhiyun  create a sysfs attribute pm_qos_resume_latency_us under the device's power
149*4882a593Smuzhiyun  directory allowing user space to manipulate that request.
150*4882a593Smuzhiyun
151*4882a593Smuzhiyunvoid dev_pm_qos_hide_latency_limit(device)
152*4882a593Smuzhiyun  Drop the request added by dev_pm_qos_expose_latency_limit() from the device's
153*4882a593Smuzhiyun  PM QoS list of resume latency constraints and remove sysfs attribute
154*4882a593Smuzhiyun  pm_qos_resume_latency_us from the device's power directory.
155*4882a593Smuzhiyun
156*4882a593Smuzhiyunint dev_pm_qos_expose_flags(device, value)
157*4882a593Smuzhiyun  Add a request to the device's PM QoS list of flags and create sysfs attribute
158*4882a593Smuzhiyun  pm_qos_no_power_off under the device's power directory allowing user space to
159*4882a593Smuzhiyun  change the value of the PM_QOS_FLAG_NO_POWER_OFF flag.
160*4882a593Smuzhiyun
161*4882a593Smuzhiyunvoid dev_pm_qos_hide_flags(device)
162*4882a593Smuzhiyun  Drop the request added by dev_pm_qos_expose_flags() from the device's PM QoS
163*4882a593Smuzhiyun  list of flags and remove sysfs attribute pm_qos_no_power_off from the device's
164*4882a593Smuzhiyun  power directory.
165*4882a593Smuzhiyun
166*4882a593SmuzhiyunNotification mechanisms:
167*4882a593Smuzhiyun
168*4882a593SmuzhiyunThe per-device PM QoS framework has a per-device notification tree.
169*4882a593Smuzhiyun
170*4882a593Smuzhiyunint dev_pm_qos_add_notifier(device, notifier, type):
171*4882a593Smuzhiyun  Adds a notification callback function for the device for a particular request
172*4882a593Smuzhiyun  type.
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun  The callback is called when the aggregated value of the device constraints
175*4882a593Smuzhiyun  list is changed.
176*4882a593Smuzhiyun
177*4882a593Smuzhiyunint dev_pm_qos_remove_notifier(device, notifier, type):
178*4882a593Smuzhiyun  Removes the notification callback function for the device.
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun
181*4882a593SmuzhiyunActive state latency tolerance
182*4882a593Smuzhiyun^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
183*4882a593Smuzhiyun
184*4882a593SmuzhiyunThis device PM QoS type is used to support systems in which hardware may switch
185*4882a593Smuzhiyunto energy-saving operation modes on the fly.  In those systems, if the operation
186*4882a593Smuzhiyunmode chosen by the hardware attempts to save energy in an overly aggressive way,
187*4882a593Smuzhiyunit may cause excess latencies to be visible to software, causing it to miss
188*4882a593Smuzhiyuncertain protocol requirements or target frame or sample rates etc.
189*4882a593Smuzhiyun
190*4882a593SmuzhiyunIf there is a latency tolerance control mechanism for a given device available
191*4882a593Smuzhiyunto software, the .set_latency_tolerance callback in that device's dev_pm_info
192*4882a593Smuzhiyunstructure should be populated.  The routine pointed to by it is should implement
193*4882a593Smuzhiyunwhatever is necessary to transfer the effective requirement value to the
194*4882a593Smuzhiyunhardware.
195*4882a593Smuzhiyun
196*4882a593SmuzhiyunWhenever the effective latency tolerance changes for the device, its
197*4882a593Smuzhiyun.set_latency_tolerance() callback will be executed and the effective value will
198*4882a593Smuzhiyunbe passed to it.  If that value is negative, which means that the list of
199*4882a593Smuzhiyunlatency tolerance requirements for the device is empty, the callback is expected
200*4882a593Smuzhiyunto switch the underlying hardware latency tolerance control mechanism to an
201*4882a593Smuzhiyunautonomous mode if available.  If that value is PM_QOS_LATENCY_ANY, in turn, and
202*4882a593Smuzhiyunthe hardware supports a special "no requirement" setting, the callback is
203*4882a593Smuzhiyunexpected to use it.  That allows software to prevent the hardware from
204*4882a593Smuzhiyunautomatically updating the device's latency tolerance in response to its power
205*4882a593Smuzhiyunstate changes (e.g. during transitions from D3cold to D0), which generally may
206*4882a593Smuzhiyunbe done in the autonomous latency tolerance control mode.
207*4882a593Smuzhiyun
208*4882a593SmuzhiyunIf .set_latency_tolerance() is present for the device, sysfs attribute
209*4882a593Smuzhiyunpm_qos_latency_tolerance_us will be present in the devivce's power directory.
210*4882a593SmuzhiyunThen, user space can use that attribute to specify its latency tolerance
211*4882a593Smuzhiyunrequirement for the device, if any.  Writing "any" to it means "no requirement,
212*4882a593Smuzhiyunbut do not let the hardware control latency tolerance" and writing "auto" to it
213*4882a593Smuzhiyunallows the hardware to be switched to the autonomous mode if there are no other
214*4882a593Smuzhiyunrequirements from the kernel side in the device's list.
215*4882a593Smuzhiyun
216*4882a593SmuzhiyunKernel code can use the functions described above along with the
217*4882a593SmuzhiyunDEV_PM_QOS_LATENCY_TOLERANCE device PM QoS type to add, remove and update
218*4882a593Smuzhiyunlatency tolerance requirements for devices.
219