18c2ecf20Sopenharmony_ci===============================
28c2ecf20Sopenharmony_ciPM Quality Of Service Interface
38c2ecf20Sopenharmony_ci===============================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ciThis interface provides a kernel and user mode interface for registering
68c2ecf20Sopenharmony_ciperformance expectations by drivers, subsystems and user space applications on
78c2ecf20Sopenharmony_cione of the parameters.
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ciTwo different PM QoS frameworks are available:
108c2ecf20Sopenharmony_ci * CPU latency QoS.
118c2ecf20Sopenharmony_ci * The per-device PM QoS framework provides the API to manage the
128c2ecf20Sopenharmony_ci   per-device latency constraints and PM QoS flags.
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ciThe latency unit used in the PM QoS framework is the microsecond (usec).
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci1. PM QoS framework
188c2ecf20Sopenharmony_ci===================
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ciA global list of CPU latency QoS requests is maintained along with an aggregated
218c2ecf20Sopenharmony_ci(effective) target value.  The aggregated target value is updated with changes
228c2ecf20Sopenharmony_cito the request list or elements of the list.  For CPU latency QoS, the
238c2ecf20Sopenharmony_ciaggregated target value is simply the min of the request values held in the list
248c2ecf20Sopenharmony_cielements.
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ciNote: the aggregated target value is implemented as an atomic variable so that
278c2ecf20Sopenharmony_cireading the aggregated value does not require any locking mechanism.
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciFrom kernel space the use of this interface is simple:
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_civoid cpu_latency_qos_add_request(handle, target_value):
328c2ecf20Sopenharmony_ci  Will insert an element into the CPU latency QoS list with the target value.
338c2ecf20Sopenharmony_ci  Upon change to this list the new target is recomputed and any registered
348c2ecf20Sopenharmony_ci  notifiers are called only if the target value is now different.
358c2ecf20Sopenharmony_ci  Clients of PM QoS need to save the returned handle for future use in other
368c2ecf20Sopenharmony_ci  PM QoS API functions.
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_civoid cpu_latency_qos_update_request(handle, new_target_value):
398c2ecf20Sopenharmony_ci  Will update the list element pointed to by the handle with the new target
408c2ecf20Sopenharmony_ci  value and recompute the new aggregated target, calling the notification tree
418c2ecf20Sopenharmony_ci  if the target is changed.
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_civoid cpu_latency_qos_remove_request(handle):
448c2ecf20Sopenharmony_ci  Will remove the element.  After removal it will update the aggregate target
458c2ecf20Sopenharmony_ci  and call the notification tree if the target was changed as a result of
468c2ecf20Sopenharmony_ci  removing the request.
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ciint cpu_latency_qos_limit():
498c2ecf20Sopenharmony_ci  Returns the aggregated value for the CPU latency QoS.
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciint cpu_latency_qos_request_active(handle):
528c2ecf20Sopenharmony_ci  Returns if the request is still active, i.e. it has not been removed from the
538c2ecf20Sopenharmony_ci  CPU latency QoS list.
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ciint cpu_latency_qos_add_notifier(notifier):
568c2ecf20Sopenharmony_ci  Adds a notification callback function to the CPU latency QoS. The callback is
578c2ecf20Sopenharmony_ci  called when the aggregated value for the CPU latency QoS is changed.
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciint cpu_latency_qos_remove_notifier(notifier):
608c2ecf20Sopenharmony_ci  Removes the notification callback function from the CPU latency QoS.
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ciFrom user space:
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ciThe infrastructure exposes one device node, /dev/cpu_dma_latency, for the CPU
668c2ecf20Sopenharmony_cilatency QoS.
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciOnly processes can register a PM QoS request.  To provide for automatic
698c2ecf20Sopenharmony_cicleanup of a process, the interface requires the process to register its
708c2ecf20Sopenharmony_ciparameter requests as follows.
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ciTo register the default PM QoS target for the CPU latency QoS, the process must
738c2ecf20Sopenharmony_ciopen /dev/cpu_dma_latency.
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ciAs long as the device node is held open that process has a registered
768c2ecf20Sopenharmony_cirequest on the parameter.
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ciTo change the requested target value, the process needs to write an s32 value to
798c2ecf20Sopenharmony_cithe open device node.  Alternatively, it can write a hex string for the value
808c2ecf20Sopenharmony_ciusing the 10 char long format e.g. "0x12345678".  This translates to a
818c2ecf20Sopenharmony_cicpu_latency_qos_update_request() call.
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ciTo remove the user mode request for a target value simply close the device
848c2ecf20Sopenharmony_cinode.
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci2. PM QoS per-device latency and flags framework
888c2ecf20Sopenharmony_ci================================================
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciFor each device, there are three lists of PM QoS requests. Two of them are
918c2ecf20Sopenharmony_cimaintained along with the aggregated targets of resume latency and active
928c2ecf20Sopenharmony_cistate latency tolerance (in microseconds) and the third one is for PM QoS flags.
938c2ecf20Sopenharmony_ciValues are updated in response to changes of the request list.
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ciThe target values of resume latency and active state latency tolerance are
968c2ecf20Sopenharmony_cisimply the minimum of the request values held in the parameter list elements.
978c2ecf20Sopenharmony_ciThe PM QoS flags aggregate value is a gather (bitwise OR) of all list elements'
988c2ecf20Sopenharmony_civalues.  One device PM QoS flag is defined currently: PM_QOS_FLAG_NO_POWER_OFF.
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ciNote: The aggregated target values are implemented in such a way that reading
1018c2ecf20Sopenharmony_cithe aggregated value does not require any locking mechanism.
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ciFrom kernel mode the use of this interface is the following:
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ciint dev_pm_qos_add_request(device, handle, type, value):
1078c2ecf20Sopenharmony_ci  Will insert an element into the list for that identified device with the
1088c2ecf20Sopenharmony_ci  target value.  Upon change to this list the new target is recomputed and any
1098c2ecf20Sopenharmony_ci  registered notifiers are called only if the target value is now different.
1108c2ecf20Sopenharmony_ci  Clients of dev_pm_qos need to save the handle for future use in other
1118c2ecf20Sopenharmony_ci  dev_pm_qos API functions.
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ciint dev_pm_qos_update_request(handle, new_value):
1148c2ecf20Sopenharmony_ci  Will update the list element pointed to by the handle with the new target
1158c2ecf20Sopenharmony_ci  value and recompute the new aggregated target, calling the notification
1168c2ecf20Sopenharmony_ci  trees if the target is changed.
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ciint dev_pm_qos_remove_request(handle):
1198c2ecf20Sopenharmony_ci  Will remove the element.  After removal it will update the aggregate target
1208c2ecf20Sopenharmony_ci  and call the notification trees if the target was changed as a result of
1218c2ecf20Sopenharmony_ci  removing the request.
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cis32 dev_pm_qos_read_value(device, type):
1248c2ecf20Sopenharmony_ci  Returns the aggregated value for a given device's constraints list.
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cienum pm_qos_flags_status dev_pm_qos_flags(device, mask)
1278c2ecf20Sopenharmony_ci  Check PM QoS flags of the given device against the given mask of flags.
1288c2ecf20Sopenharmony_ci  The meaning of the return values is as follows:
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	PM_QOS_FLAGS_ALL:
1318c2ecf20Sopenharmony_ci		All flags from the mask are set
1328c2ecf20Sopenharmony_ci	PM_QOS_FLAGS_SOME:
1338c2ecf20Sopenharmony_ci		Some flags from the mask are set
1348c2ecf20Sopenharmony_ci	PM_QOS_FLAGS_NONE:
1358c2ecf20Sopenharmony_ci		No flags from the mask are set
1368c2ecf20Sopenharmony_ci	PM_QOS_FLAGS_UNDEFINED:
1378c2ecf20Sopenharmony_ci		The device's PM QoS structure has not been initialized
1388c2ecf20Sopenharmony_ci		or the list of requests is empty.
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ciint dev_pm_qos_add_ancestor_request(dev, handle, type, value)
1418c2ecf20Sopenharmony_ci  Add a PM QoS request for the first direct ancestor of the given device whose
1428c2ecf20Sopenharmony_ci  power.ignore_children flag is unset (for DEV_PM_QOS_RESUME_LATENCY requests)
1438c2ecf20Sopenharmony_ci  or whose power.set_latency_tolerance callback pointer is not NULL (for
1448c2ecf20Sopenharmony_ci  DEV_PM_QOS_LATENCY_TOLERANCE requests).
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ciint dev_pm_qos_expose_latency_limit(device, value)
1478c2ecf20Sopenharmony_ci  Add a request to the device's PM QoS list of resume latency constraints and
1488c2ecf20Sopenharmony_ci  create a sysfs attribute pm_qos_resume_latency_us under the device's power
1498c2ecf20Sopenharmony_ci  directory allowing user space to manipulate that request.
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_civoid dev_pm_qos_hide_latency_limit(device)
1528c2ecf20Sopenharmony_ci  Drop the request added by dev_pm_qos_expose_latency_limit() from the device's
1538c2ecf20Sopenharmony_ci  PM QoS list of resume latency constraints and remove sysfs attribute
1548c2ecf20Sopenharmony_ci  pm_qos_resume_latency_us from the device's power directory.
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ciint dev_pm_qos_expose_flags(device, value)
1578c2ecf20Sopenharmony_ci  Add a request to the device's PM QoS list of flags and create sysfs attribute
1588c2ecf20Sopenharmony_ci  pm_qos_no_power_off under the device's power directory allowing user space to
1598c2ecf20Sopenharmony_ci  change the value of the PM_QOS_FLAG_NO_POWER_OFF flag.
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_civoid dev_pm_qos_hide_flags(device)
1628c2ecf20Sopenharmony_ci  Drop the request added by dev_pm_qos_expose_flags() from the device's PM QoS
1638c2ecf20Sopenharmony_ci  list of flags and remove sysfs attribute pm_qos_no_power_off from the device's
1648c2ecf20Sopenharmony_ci  power directory.
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ciNotification mechanisms:
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ciThe per-device PM QoS framework has a per-device notification tree.
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ciint dev_pm_qos_add_notifier(device, notifier, type):
1718c2ecf20Sopenharmony_ci  Adds a notification callback function for the device for a particular request
1728c2ecf20Sopenharmony_ci  type.
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci  The callback is called when the aggregated value of the device constraints
1758c2ecf20Sopenharmony_ci  list is changed.
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ciint dev_pm_qos_remove_notifier(device, notifier, type):
1788c2ecf20Sopenharmony_ci  Removes the notification callback function for the device.
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ciActive state latency tolerance
1828c2ecf20Sopenharmony_ci^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ciThis device PM QoS type is used to support systems in which hardware may switch
1858c2ecf20Sopenharmony_cito energy-saving operation modes on the fly.  In those systems, if the operation
1868c2ecf20Sopenharmony_cimode chosen by the hardware attempts to save energy in an overly aggressive way,
1878c2ecf20Sopenharmony_ciit may cause excess latencies to be visible to software, causing it to miss
1888c2ecf20Sopenharmony_cicertain protocol requirements or target frame or sample rates etc.
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ciIf there is a latency tolerance control mechanism for a given device available
1918c2ecf20Sopenharmony_cito software, the .set_latency_tolerance callback in that device's dev_pm_info
1928c2ecf20Sopenharmony_cistructure should be populated.  The routine pointed to by it is should implement
1938c2ecf20Sopenharmony_ciwhatever is necessary to transfer the effective requirement value to the
1948c2ecf20Sopenharmony_cihardware.
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ciWhenever the effective latency tolerance changes for the device, its
1978c2ecf20Sopenharmony_ci.set_latency_tolerance() callback will be executed and the effective value will
1988c2ecf20Sopenharmony_cibe passed to it.  If that value is negative, which means that the list of
1998c2ecf20Sopenharmony_cilatency tolerance requirements for the device is empty, the callback is expected
2008c2ecf20Sopenharmony_cito switch the underlying hardware latency tolerance control mechanism to an
2018c2ecf20Sopenharmony_ciautonomous mode if available.  If that value is PM_QOS_LATENCY_ANY, in turn, and
2028c2ecf20Sopenharmony_cithe hardware supports a special "no requirement" setting, the callback is
2038c2ecf20Sopenharmony_ciexpected to use it.  That allows software to prevent the hardware from
2048c2ecf20Sopenharmony_ciautomatically updating the device's latency tolerance in response to its power
2058c2ecf20Sopenharmony_cistate changes (e.g. during transitions from D3cold to D0), which generally may
2068c2ecf20Sopenharmony_cibe done in the autonomous latency tolerance control mode.
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ciIf .set_latency_tolerance() is present for the device, sysfs attribute
2098c2ecf20Sopenharmony_cipm_qos_latency_tolerance_us will be present in the devivce's power directory.
2108c2ecf20Sopenharmony_ciThen, user space can use that attribute to specify its latency tolerance
2118c2ecf20Sopenharmony_cirequirement for the device, if any.  Writing "any" to it means "no requirement,
2128c2ecf20Sopenharmony_cibut do not let the hardware control latency tolerance" and writing "auto" to it
2138c2ecf20Sopenharmony_ciallows the hardware to be switched to the autonomous mode if there are no other
2148c2ecf20Sopenharmony_cirequirements from the kernel side in the device's list.
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ciKernel code can use the functions described above along with the
2178c2ecf20Sopenharmony_ciDEV_PM_QOS_LATENCY_TOLERANCE device PM QoS type to add, remove and update
2188c2ecf20Sopenharmony_cilatency tolerance requirements for devices.
219