Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 1 | =============================== |
| 2 | PM Quality Of Service Interface |
| 3 | =============================== |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 4 | |
| 5 | This interface provides a kernel and user mode interface for registering |
| 6 | performance expectations by drivers, subsystems and user space applications on |
| 7 | one of the parameters. |
| 8 | |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 9 | Two different PM QoS frameworks are available: |
Randy Dunlap | 3c87402 | 2020-02-16 20:54:21 -0800 | [diff] [blame] | 10 | * CPU latency QoS. |
| 11 | * The per-device PM QoS framework provides the API to manage the |
Bjorn Helgaas | 1992b66 | 2019-11-19 08:09:23 -0600 | [diff] [blame] | 12 | per-device latency constraints and PM QoS flags. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 13 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 14 | The latency unit used in the PM QoS framework is the microsecond (usec). |
Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 15 | |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 16 | |
| 17 | 1. PM QoS framework |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 18 | =================== |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 19 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 20 | A global list of CPU latency QoS requests is maintained along with an aggregated |
| 21 | (effective) target value. The aggregated target value is updated with changes |
| 22 | to the request list or elements of the list. For CPU latency QoS, the |
| 23 | aggregated target value is simply the min of the request values held in the list |
| 24 | elements. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 25 | |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 26 | Note: the aggregated target value is implemented as an atomic variable so that |
| 27 | reading the aggregated value does not require any locking mechanism. |
| 28 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 29 | From kernel space the use of this interface is simple: |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 30 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 31 | void cpu_latency_qos_add_request(handle, target_value): |
| 32 | Will insert an element into the CPU latency QoS list with the target value. |
| 33 | Upon change to this list the new target is recomputed and any registered |
| 34 | notifiers are called only if the target value is now different. |
| 35 | Clients of PM QoS need to save the returned handle for future use in other |
| 36 | PM QoS API functions. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 37 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 38 | void cpu_latency_qos_update_request(handle, new_target_value): |
Bjorn Helgaas | 1992b66 | 2019-11-19 08:09:23 -0600 | [diff] [blame] | 39 | Will update the list element pointed to by the handle with the new target |
| 40 | value and recompute the new aggregated target, calling the notification tree |
| 41 | if the target is changed. |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 42 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 43 | void cpu_latency_qos_remove_request(handle): |
Bjorn Helgaas | 1992b66 | 2019-11-19 08:09:23 -0600 | [diff] [blame] | 44 | Will remove the element. After removal it will update the aggregate target |
| 45 | and call the notification tree if the target was changed as a result of |
| 46 | removing the request. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 47 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 48 | int cpu_latency_qos_limit(): |
| 49 | Returns the aggregated value for the CPU latency QoS. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 50 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 51 | int cpu_latency_qos_request_active(handle): |
| 52 | Returns if the request is still active, i.e. it has not been removed from the |
| 53 | CPU latency QoS list. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 54 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 55 | int cpu_latency_qos_add_notifier(notifier): |
| 56 | Adds a notification callback function to the CPU latency QoS. The callback is |
| 57 | called when the aggregated value for the CPU latency QoS is changed. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 58 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 59 | int cpu_latency_qos_remove_notifier(notifier): |
| 60 | Removes the notification callback function from the CPU latency QoS. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 61 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 62 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 63 | From user space: |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 64 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 65 | The infrastructure exposes one device node, /dev/cpu_dma_latency, for the CPU |
| 66 | latency QoS. |
| 67 | |
| 68 | Only processes can register a PM QoS request. To provide for automatic |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 69 | cleanup of a process, the interface requires the process to register its |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 70 | parameter requests as follows. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 71 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 72 | To register the default PM QoS target for the CPU latency QoS, the process must |
| 73 | open /dev/cpu_dma_latency. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 74 | |
| 75 | As long as the device node is held open that process has a registered |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 76 | request on the parameter. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 77 | |
Rafael J. Wysocki | b8e6e27 | 2020-02-12 00:36:35 +0100 | [diff] [blame] | 78 | To change the requested target value, the process needs to write an s32 value to |
| 79 | the open device node. Alternatively, it can write a hex string for the value |
| 80 | using the 10 char long format e.g. "0x12345678". This translates to a |
| 81 | cpu_latency_qos_update_request() call. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 82 | |
| 83 | To remove the user mode request for a target value simply close the device |
| 84 | node. |
| 85 | |
| 86 | |
lan,Tianyu | d30b82a | 2013-06-21 10:09:15 +0800 | [diff] [blame] | 87 | 2. PM QoS per-device latency and flags framework |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 88 | ================================================ |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 89 | |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 90 | For each device, there are three lists of PM QoS requests. Two of them are |
| 91 | maintained along with the aggregated targets of resume latency and active |
| 92 | state latency tolerance (in microseconds) and the third one is for PM QoS flags. |
| 93 | Values are updated in response to changes of the request list. |
lan,Tianyu | d30b82a | 2013-06-21 10:09:15 +0800 | [diff] [blame] | 94 | |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 95 | The target values of resume latency and active state latency tolerance are |
| 96 | simply the minimum of the request values held in the parameter list elements. |
| 97 | The PM QoS flags aggregate value is a gather (bitwise OR) of all list elements' |
Rafael J. Wysocki | 20f97ca | 2017-10-13 15:27:24 +0200 | [diff] [blame] | 98 | values. One device PM QoS flag is defined currently: PM_QOS_FLAG_NO_POWER_OFF. |
lan,Tianyu | d30b82a | 2013-06-21 10:09:15 +0800 | [diff] [blame] | 99 | |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 100 | Note: The aggregated target values are implemented in such a way that reading |
| 101 | the aggregated value does not require any locking mechanism. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 102 | |
| 103 | |
| 104 | From kernel mode the use of this interface is the following: |
| 105 | |
Rafael J. Wysocki | ae0fb4b | 2012-10-23 01:09:12 +0200 | [diff] [blame] | 106 | int dev_pm_qos_add_request(device, handle, type, value): |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 107 | Will insert an element into the list for that identified device with the |
| 108 | target value. Upon change to this list the new target is recomputed and any |
| 109 | registered notifiers are called only if the target value is now different. |
| 110 | Clients of dev_pm_qos need to save the handle for future use in other |
| 111 | dev_pm_qos API functions. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 112 | |
| 113 | int dev_pm_qos_update_request(handle, new_value): |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 114 | Will update the list element pointed to by the handle with the new target |
| 115 | value and recompute the new aggregated target, calling the notification |
| 116 | trees if the target is changed. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 117 | |
| 118 | int dev_pm_qos_remove_request(handle): |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 119 | Will remove the element. After removal it will update the aggregate target |
| 120 | and call the notification trees if the target was changed as a result of |
| 121 | removing the request. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 122 | |
Linus Torvalds | d0411ec | 2019-07-18 09:32:28 -0700 | [diff] [blame] | 123 | s32 dev_pm_qos_read_value(device, type): |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 124 | Returns the aggregated value for a given device's constraints list. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 125 | |
lan,Tianyu | d30b82a | 2013-06-21 10:09:15 +0800 | [diff] [blame] | 126 | enum pm_qos_flags_status dev_pm_qos_flags(device, mask) |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 127 | Check PM QoS flags of the given device against the given mask of flags. |
| 128 | The meaning of the return values is as follows: |
| 129 | |
| 130 | PM_QOS_FLAGS_ALL: |
| 131 | All flags from the mask are set |
| 132 | PM_QOS_FLAGS_SOME: |
| 133 | Some flags from the mask are set |
| 134 | PM_QOS_FLAGS_NONE: |
| 135 | No flags from the mask are set |
| 136 | PM_QOS_FLAGS_UNDEFINED: |
| 137 | The device's PM QoS structure has not been initialized |
| 138 | or the list of requests is empty. |
lan,Tianyu | d30b82a | 2013-06-21 10:09:15 +0800 | [diff] [blame] | 139 | |
Rafael J. Wysocki | 71d821f | 2014-02-11 00:36:00 +0100 | [diff] [blame] | 140 | int dev_pm_qos_add_ancestor_request(dev, handle, type, value) |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 141 | Add a PM QoS request for the first direct ancestor of the given device whose |
| 142 | power.ignore_children flag is unset (for DEV_PM_QOS_RESUME_LATENCY requests) |
| 143 | or whose power.set_latency_tolerance callback pointer is not NULL (for |
| 144 | DEV_PM_QOS_LATENCY_TOLERANCE requests). |
lan,Tianyu | d30b82a | 2013-06-21 10:09:15 +0800 | [diff] [blame] | 145 | |
| 146 | int dev_pm_qos_expose_latency_limit(device, value) |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 147 | Add a request to the device's PM QoS list of resume latency constraints and |
| 148 | create a sysfs attribute pm_qos_resume_latency_us under the device's power |
| 149 | directory allowing user space to manipulate that request. |
lan,Tianyu | d30b82a | 2013-06-21 10:09:15 +0800 | [diff] [blame] | 150 | |
| 151 | void dev_pm_qos_hide_latency_limit(device) |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 152 | Drop the request added by dev_pm_qos_expose_latency_limit() from the device's |
| 153 | PM QoS list of resume latency constraints and remove sysfs attribute |
| 154 | pm_qos_resume_latency_us from the device's power directory. |
lan,Tianyu | d30b82a | 2013-06-21 10:09:15 +0800 | [diff] [blame] | 155 | |
| 156 | int dev_pm_qos_expose_flags(device, value) |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 157 | Add a request to the device's PM QoS list of flags and create sysfs attribute |
| 158 | pm_qos_no_power_off under the device's power directory allowing user space to |
| 159 | change the value of the PM_QOS_FLAG_NO_POWER_OFF flag. |
lan,Tianyu | d30b82a | 2013-06-21 10:09:15 +0800 | [diff] [blame] | 160 | |
| 161 | void dev_pm_qos_hide_flags(device) |
Bjorn Helgaas | 1992b66 | 2019-11-19 08:09:23 -0600 | [diff] [blame] | 162 | Drop the request added by dev_pm_qos_expose_flags() from the device's PM QoS |
| 163 | list of flags and remove sysfs attribute pm_qos_no_power_off from the device's |
| 164 | power directory. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 165 | |
| 166 | Notification mechanisms: |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 167 | |
Viresh Kumar | d08d1b2 | 2017-02-22 13:58:52 +0530 | [diff] [blame] | 168 | The per-device PM QoS framework has a per-device notification tree. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 169 | |
Linus Torvalds | d0411ec | 2019-07-18 09:32:28 -0700 | [diff] [blame] | 170 | int dev_pm_qos_add_notifier(device, notifier, type): |
| 171 | Adds a notification callback function for the device for a particular request |
| 172 | type. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 173 | |
Bjorn Helgaas | 1992b66 | 2019-11-19 08:09:23 -0600 | [diff] [blame] | 174 | The callback is called when the aggregated value of the device constraints |
| 175 | list is changed. |
Linus Torvalds | d0411ec | 2019-07-18 09:32:28 -0700 | [diff] [blame] | 176 | |
| 177 | int dev_pm_qos_remove_notifier(device, notifier, type): |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 178 | Removes the notification callback function for the device. |
Jean Pihet | e3cba32 | 2011-10-04 21:54:45 +0200 | [diff] [blame] | 179 | |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 180 | |
| 181 | Active state latency tolerance |
Mauro Carvalho Chehab | 151f4e2 | 2019-06-13 07:10:36 -0300 | [diff] [blame] | 182 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 183 | |
| 184 | This device PM QoS type is used to support systems in which hardware may switch |
| 185 | to energy-saving operation modes on the fly. In those systems, if the operation |
| 186 | mode chosen by the hardware attempts to save energy in an overly aggressive way, |
| 187 | it may cause excess latencies to be visible to software, causing it to miss |
| 188 | certain protocol requirements or target frame or sample rates etc. |
| 189 | |
| 190 | If there is a latency tolerance control mechanism for a given device available |
| 191 | to software, the .set_latency_tolerance callback in that device's dev_pm_info |
| 192 | structure should be populated. The routine pointed to by it is should implement |
| 193 | whatever is necessary to transfer the effective requirement value to the |
| 194 | hardware. |
| 195 | |
| 196 | Whenever the effective latency tolerance changes for the device, its |
| 197 | .set_latency_tolerance() callback will be executed and the effective value will |
| 198 | be passed to it. If that value is negative, which means that the list of |
| 199 | latency tolerance requirements for the device is empty, the callback is expected |
| 200 | to switch the underlying hardware latency tolerance control mechanism to an |
| 201 | autonomous mode if available. If that value is PM_QOS_LATENCY_ANY, in turn, and |
| 202 | the hardware supports a special "no requirement" setting, the callback is |
| 203 | expected to use it. That allows software to prevent the hardware from |
| 204 | automatically updating the device's latency tolerance in response to its power |
| 205 | state changes (e.g. during transitions from D3cold to D0), which generally may |
| 206 | be done in the autonomous latency tolerance control mode. |
| 207 | |
| 208 | If .set_latency_tolerance() is present for the device, sysfs attribute |
| 209 | pm_qos_latency_tolerance_us will be present in the devivce's power directory. |
| 210 | Then, user space can use that attribute to specify its latency tolerance |
| 211 | requirement for the device, if any. Writing "any" to it means "no requirement, |
| 212 | but do not let the hardware control latency tolerance" and writing "auto" to it |
| 213 | allows the hardware to be switched to the autonomous mode if there are no other |
| 214 | requirements from the kernel side in the device's list. |
| 215 | |
| 216 | Kernel code can use the functions described above along with the |
| 217 | DEV_PM_QOS_LATENCY_TOLERANCE device PM QoS type to add, remove and update |
| 218 | latency tolerance requirements for devices. |