Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 2 | /* |
| 3 | * This module exposes the interface to kernel space for specifying |
| 4 | * QoS dependencies. It provides infrastructure for registration of: |
| 5 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 6 | * Dependents on a QoS value : register requests |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 7 | * Watchers of QoS value : get notified when target QoS value changes |
| 8 | * |
| 9 | * This QoS design is best effort based. Dependents register their QoS needs. |
| 10 | * Watchers register to keep track of the current QoS needs of the system. |
| 11 | * |
| 12 | * There are 3 basic classes of QoS parameter: latency, timeout, throughput |
| 13 | * each have defined units: |
| 14 | * latency: usec |
| 15 | * timeout: usec <-- currently not used. |
| 16 | * throughput: kbs (kilo byte / sec) |
| 17 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 18 | * There are lists of pm_qos_objects each one wrapping requests, notifiers |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 19 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 20 | * User mode requests on a QOS parameter register themselves to the |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 21 | * subsystem by opening the device node /dev/... and writing there request to |
| 22 | * the node. As long as the process holds a file handle open to the node the |
| 23 | * client continues to be accounted for. Upon file release the usermode |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 24 | * request is removed and a new qos target is computed. This way when the |
| 25 | * request that the application has is cleaned up when closes the file |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 26 | * pointer or exits the pm_qos_object will get an opportunity to clean up. |
| 27 | * |
Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 28 | * Mark Gross <mgross@linux.intel.com> |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 29 | */ |
| 30 | |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 31 | /*#define DEBUG*/ |
| 32 | |
Jean Pihet | e8db0be | 2011-08-25 15:35:03 +0200 | [diff] [blame] | 33 | #include <linux/pm_qos.h> |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 34 | #include <linux/sched.h> |
| 35 | #include <linux/spinlock.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/time.h> |
| 38 | #include <linux/fs.h> |
| 39 | #include <linux/device.h> |
| 40 | #include <linux/miscdevice.h> |
| 41 | #include <linux/string.h> |
| 42 | #include <linux/platform_device.h> |
| 43 | #include <linux/init.h> |
Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 44 | #include <linux/kernel.h> |
Nishanth Menon | f5f4eda | 2014-12-05 11:19:08 -0600 | [diff] [blame] | 45 | #include <linux/debugfs.h> |
| 46 | #include <linux/seq_file.h> |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 47 | |
| 48 | #include <linux/uaccess.h> |
Paul Gortmaker | 6e5fdee | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 49 | #include <linux/export.h> |
Sahara | 247e9ee | 2013-06-21 11:12:28 +0900 | [diff] [blame] | 50 | #include <trace/events/power.h> |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 51 | |
| 52 | /* |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 53 | * locking rule: all changes to constraints or notifiers lists |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 54 | * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock |
| 55 | * held, taken with _irqsave. One lock to rule them all |
| 56 | */ |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 57 | struct pm_qos_object { |
Jean Pihet | 4e1779b | 2011-08-25 15:35:27 +0200 | [diff] [blame] | 58 | struct pm_qos_constraints *constraints; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 59 | struct miscdevice pm_qos_power_miscdev; |
| 60 | char *name; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 63 | static DEFINE_SPINLOCK(pm_qos_lock); |
| 64 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 65 | static struct pm_qos_object null_pm_qos; |
Jean Pihet | 4e1779b | 2011-08-25 15:35:27 +0200 | [diff] [blame] | 66 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 67 | static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier); |
Jean Pihet | 4e1779b | 2011-08-25 15:35:27 +0200 | [diff] [blame] | 68 | static struct pm_qos_constraints cpu_dma_constraints = { |
| 69 | .list = PLIST_HEAD_INIT(cpu_dma_constraints.list), |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 70 | .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE, |
| 71 | .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE, |
Rafael J. Wysocki | 327adae | 2014-02-11 00:35:29 +0100 | [diff] [blame] | 72 | .no_constraint_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE, |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 73 | .type = PM_QOS_MIN, |
Jean Pihet | 4e1779b | 2011-08-25 15:35:27 +0200 | [diff] [blame] | 74 | .notifiers = &cpu_dma_lat_notifier, |
| 75 | }; |
| 76 | static struct pm_qos_object cpu_dma_pm_qos = { |
| 77 | .constraints = &cpu_dma_constraints, |
Dominik Brodowski | a6f05b9 | 2011-11-06 21:54:12 +0100 | [diff] [blame] | 78 | .name = "cpu_dma_latency", |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | static BLOCKING_NOTIFIER_HEAD(network_lat_notifier); |
Jean Pihet | 4e1779b | 2011-08-25 15:35:27 +0200 | [diff] [blame] | 82 | static struct pm_qos_constraints network_lat_constraints = { |
| 83 | .list = PLIST_HEAD_INIT(network_lat_constraints.list), |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 84 | .target_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE, |
| 85 | .default_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE, |
Rafael J. Wysocki | 327adae | 2014-02-11 00:35:29 +0100 | [diff] [blame] | 86 | .no_constraint_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE, |
Jean Pihet | 4e1779b | 2011-08-25 15:35:27 +0200 | [diff] [blame] | 87 | .type = PM_QOS_MIN, |
| 88 | .notifiers = &network_lat_notifier, |
| 89 | }; |
| 90 | static struct pm_qos_object network_lat_pm_qos = { |
| 91 | .constraints = &network_lat_constraints, |
| 92 | .name = "network_latency", |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | |
| 96 | static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier); |
Jean Pihet | 4e1779b | 2011-08-25 15:35:27 +0200 | [diff] [blame] | 97 | static struct pm_qos_constraints network_tput_constraints = { |
| 98 | .list = PLIST_HEAD_INIT(network_tput_constraints.list), |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 99 | .target_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE, |
| 100 | .default_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE, |
Rafael J. Wysocki | 327adae | 2014-02-11 00:35:29 +0100 | [diff] [blame] | 101 | .no_constraint_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE, |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 102 | .type = PM_QOS_MAX, |
Jean Pihet | 4e1779b | 2011-08-25 15:35:27 +0200 | [diff] [blame] | 103 | .notifiers = &network_throughput_notifier, |
| 104 | }; |
| 105 | static struct pm_qos_object network_throughput_pm_qos = { |
| 106 | .constraints = &network_tput_constraints, |
| 107 | .name = "network_throughput", |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | |
Tomeu Vizoso | 7990da7 | 2014-09-03 17:49:32 +0200 | [diff] [blame] | 111 | static BLOCKING_NOTIFIER_HEAD(memory_bandwidth_notifier); |
| 112 | static struct pm_qos_constraints memory_bw_constraints = { |
| 113 | .list = PLIST_HEAD_INIT(memory_bw_constraints.list), |
| 114 | .target_value = PM_QOS_MEMORY_BANDWIDTH_DEFAULT_VALUE, |
| 115 | .default_value = PM_QOS_MEMORY_BANDWIDTH_DEFAULT_VALUE, |
| 116 | .no_constraint_value = PM_QOS_MEMORY_BANDWIDTH_DEFAULT_VALUE, |
| 117 | .type = PM_QOS_SUM, |
| 118 | .notifiers = &memory_bandwidth_notifier, |
| 119 | }; |
| 120 | static struct pm_qos_object memory_bandwidth_pm_qos = { |
| 121 | .constraints = &memory_bw_constraints, |
| 122 | .name = "memory_bandwidth", |
| 123 | }; |
| 124 | |
| 125 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 126 | static struct pm_qos_object *pm_qos_array[] = { |
| 127 | &null_pm_qos, |
| 128 | &cpu_dma_pm_qos, |
| 129 | &network_lat_pm_qos, |
Tomeu Vizoso | 7990da7 | 2014-09-03 17:49:32 +0200 | [diff] [blame] | 130 | &network_throughput_pm_qos, |
| 131 | &memory_bandwidth_pm_qos, |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 134 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, |
| 135 | size_t count, loff_t *f_pos); |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 136 | static ssize_t pm_qos_power_read(struct file *filp, char __user *buf, |
| 137 | size_t count, loff_t *f_pos); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 138 | static int pm_qos_power_open(struct inode *inode, struct file *filp); |
| 139 | static int pm_qos_power_release(struct inode *inode, struct file *filp); |
| 140 | |
| 141 | static const struct file_operations pm_qos_power_fops = { |
| 142 | .write = pm_qos_power_write, |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 143 | .read = pm_qos_power_read, |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 144 | .open = pm_qos_power_open, |
| 145 | .release = pm_qos_power_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 146 | .llseek = noop_llseek, |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 147 | }; |
| 148 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 149 | /* unlocked internal variant */ |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 150 | static inline int pm_qos_get_value(struct pm_qos_constraints *c) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 151 | { |
Tomeu Vizoso | 7990da7 | 2014-09-03 17:49:32 +0200 | [diff] [blame] | 152 | struct plist_node *node; |
| 153 | int total_value = 0; |
| 154 | |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 155 | if (plist_head_empty(&c->list)) |
Rafael J. Wysocki | 327adae | 2014-02-11 00:35:29 +0100 | [diff] [blame] | 156 | return c->no_constraint_value; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 157 | |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 158 | switch (c->type) { |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 159 | case PM_QOS_MIN: |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 160 | return plist_first(&c->list)->prio; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 161 | |
| 162 | case PM_QOS_MAX: |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 163 | return plist_last(&c->list)->prio; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 164 | |
Tomeu Vizoso | 7990da7 | 2014-09-03 17:49:32 +0200 | [diff] [blame] | 165 | case PM_QOS_SUM: |
| 166 | plist_for_each(node, &c->list) |
| 167 | total_value += node->prio; |
| 168 | |
| 169 | return total_value; |
| 170 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 171 | default: |
| 172 | /* runtime check for not using enum */ |
| 173 | BUG(); |
Luis Gonzalez Fernandez | c6a57bf | 2012-09-07 21:35:21 +0200 | [diff] [blame] | 174 | return PM_QOS_DEFAULT_VALUE; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 175 | } |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Jean Pihet | b66213c | 2011-08-25 15:35:47 +0200 | [diff] [blame] | 178 | s32 pm_qos_read_value(struct pm_qos_constraints *c) |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 179 | { |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 180 | return c->target_value; |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 183 | static inline void pm_qos_set_value(struct pm_qos_constraints *c, s32 value) |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 184 | { |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 185 | c->target_value = value; |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Yangtao Li | 96c6935 | 2018-11-06 09:38:06 -0500 | [diff] [blame] | 188 | static int pm_qos_debug_show(struct seq_file *s, void *unused) |
Nishanth Menon | f5f4eda | 2014-12-05 11:19:08 -0600 | [diff] [blame] | 189 | { |
| 190 | struct pm_qos_object *qos = (struct pm_qos_object *)s->private; |
| 191 | struct pm_qos_constraints *c; |
| 192 | struct pm_qos_request *req; |
| 193 | char *type; |
| 194 | unsigned long flags; |
| 195 | int tot_reqs = 0; |
| 196 | int active_reqs = 0; |
| 197 | |
| 198 | if (IS_ERR_OR_NULL(qos)) { |
| 199 | pr_err("%s: bad qos param!\n", __func__); |
| 200 | return -EINVAL; |
| 201 | } |
| 202 | c = qos->constraints; |
| 203 | if (IS_ERR_OR_NULL(c)) { |
| 204 | pr_err("%s: Bad constraints on qos?\n", __func__); |
| 205 | return -EINVAL; |
| 206 | } |
| 207 | |
| 208 | /* Lock to ensure we have a snapshot */ |
| 209 | spin_lock_irqsave(&pm_qos_lock, flags); |
| 210 | if (plist_head_empty(&c->list)) { |
| 211 | seq_puts(s, "Empty!\n"); |
| 212 | goto out; |
| 213 | } |
| 214 | |
| 215 | switch (c->type) { |
| 216 | case PM_QOS_MIN: |
| 217 | type = "Minimum"; |
| 218 | break; |
| 219 | case PM_QOS_MAX: |
| 220 | type = "Maximum"; |
| 221 | break; |
| 222 | case PM_QOS_SUM: |
| 223 | type = "Sum"; |
| 224 | break; |
| 225 | default: |
| 226 | type = "Unknown"; |
| 227 | } |
| 228 | |
| 229 | plist_for_each_entry(req, &c->list, node) { |
| 230 | char *state = "Default"; |
| 231 | |
| 232 | if ((req->node).prio != c->default_value) { |
| 233 | active_reqs++; |
| 234 | state = "Active"; |
| 235 | } |
| 236 | tot_reqs++; |
| 237 | seq_printf(s, "%d: %d: %s\n", tot_reqs, |
| 238 | (req->node).prio, state); |
| 239 | } |
| 240 | |
| 241 | seq_printf(s, "Type=%s, Value=%d, Requests: active=%d / total=%d\n", |
| 242 | type, pm_qos_get_value(c), active_reqs, tot_reqs); |
| 243 | |
| 244 | out: |
| 245 | spin_unlock_irqrestore(&pm_qos_lock, flags); |
| 246 | return 0; |
| 247 | } |
| 248 | |
Yangtao Li | 96c6935 | 2018-11-06 09:38:06 -0500 | [diff] [blame] | 249 | DEFINE_SHOW_ATTRIBUTE(pm_qos_debug); |
Nishanth Menon | f5f4eda | 2014-12-05 11:19:08 -0600 | [diff] [blame] | 250 | |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 251 | /** |
| 252 | * pm_qos_update_target - manages the constraints list and calls the notifiers |
| 253 | * if needed |
| 254 | * @c: constraints data struct |
| 255 | * @node: request to add to the list, to update or to remove |
| 256 | * @action: action to take on the constraints list |
| 257 | * @value: value of the request to add or update |
| 258 | * |
| 259 | * This function returns 1 if the aggregated constraint value has changed, 0 |
| 260 | * otherwise. |
| 261 | */ |
| 262 | int pm_qos_update_target(struct pm_qos_constraints *c, struct plist_node *node, |
| 263 | enum pm_qos_req_action action, int value) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 264 | { |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 265 | unsigned long flags; |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 266 | int prev_value, curr_value, new_value; |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 267 | int ret; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 268 | |
| 269 | spin_lock_irqsave(&pm_qos_lock, flags); |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 270 | prev_value = pm_qos_get_value(c); |
| 271 | if (value == PM_QOS_DEFAULT_VALUE) |
| 272 | new_value = c->default_value; |
| 273 | else |
| 274 | new_value = value; |
| 275 | |
| 276 | switch (action) { |
| 277 | case PM_QOS_REMOVE_REQ: |
| 278 | plist_del(node, &c->list); |
| 279 | break; |
| 280 | case PM_QOS_UPDATE_REQ: |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 281 | /* |
| 282 | * to change the list, we atomically remove, reinit |
| 283 | * with new value and add, then see if the extremal |
| 284 | * changed |
| 285 | */ |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 286 | plist_del(node, &c->list); |
Gustavo A. R. Silva | fe43e2c | 2018-03-30 16:18:44 -0500 | [diff] [blame] | 287 | /* fall through */ |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 288 | case PM_QOS_ADD_REQ: |
| 289 | plist_node_init(node, new_value); |
| 290 | plist_add(node, &c->list); |
| 291 | break; |
| 292 | default: |
| 293 | /* no action */ |
| 294 | ; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 295 | } |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 296 | |
| 297 | curr_value = pm_qos_get_value(c); |
| 298 | pm_qos_set_value(c, curr_value); |
| 299 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 300 | spin_unlock_irqrestore(&pm_qos_lock, flags); |
| 301 | |
Sahara | 247e9ee | 2013-06-21 11:12:28 +0900 | [diff] [blame] | 302 | trace_pm_qos_update_target(action, prev_value, curr_value); |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 303 | if (prev_value != curr_value) { |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 304 | ret = 1; |
| 305 | if (c->notifiers) |
| 306 | blocking_notifier_call_chain(c->notifiers, |
| 307 | (unsigned long)curr_value, |
| 308 | NULL); |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 309 | } else { |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 310 | ret = 0; |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 311 | } |
Rafael J. Wysocki | 2d984ad | 2014-02-11 00:35:38 +0100 | [diff] [blame] | 312 | return ret; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 315 | /** |
Rafael J. Wysocki | 5efbe42 | 2012-10-23 01:07:46 +0200 | [diff] [blame] | 316 | * pm_qos_flags_remove_req - Remove device PM QoS flags request. |
| 317 | * @pqf: Device PM QoS flags set to remove the request from. |
| 318 | * @req: Request to remove from the set. |
| 319 | */ |
| 320 | static void pm_qos_flags_remove_req(struct pm_qos_flags *pqf, |
| 321 | struct pm_qos_flags_request *req) |
| 322 | { |
| 323 | s32 val = 0; |
| 324 | |
| 325 | list_del(&req->node); |
| 326 | list_for_each_entry(req, &pqf->list, node) |
| 327 | val |= req->flags; |
| 328 | |
| 329 | pqf->effective_flags = val; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * pm_qos_update_flags - Update a set of PM QoS flags. |
| 334 | * @pqf: Set of flags to update. |
| 335 | * @req: Request to add to the set, to modify, or to remove from the set. |
| 336 | * @action: Action to take on the set. |
| 337 | * @val: Value of the request to add or modify. |
| 338 | * |
| 339 | * Update the given set of PM QoS flags and call notifiers if the aggregate |
| 340 | * value has changed. Returns 1 if the aggregate constraint value has changed, |
| 341 | * 0 otherwise. |
| 342 | */ |
| 343 | bool pm_qos_update_flags(struct pm_qos_flags *pqf, |
| 344 | struct pm_qos_flags_request *req, |
| 345 | enum pm_qos_req_action action, s32 val) |
| 346 | { |
| 347 | unsigned long irqflags; |
| 348 | s32 prev_value, curr_value; |
| 349 | |
| 350 | spin_lock_irqsave(&pm_qos_lock, irqflags); |
| 351 | |
| 352 | prev_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags; |
| 353 | |
| 354 | switch (action) { |
| 355 | case PM_QOS_REMOVE_REQ: |
| 356 | pm_qos_flags_remove_req(pqf, req); |
| 357 | break; |
| 358 | case PM_QOS_UPDATE_REQ: |
| 359 | pm_qos_flags_remove_req(pqf, req); |
Gustavo A. R. Silva | fe43e2c | 2018-03-30 16:18:44 -0500 | [diff] [blame] | 360 | /* fall through */ |
Rafael J. Wysocki | 5efbe42 | 2012-10-23 01:07:46 +0200 | [diff] [blame] | 361 | case PM_QOS_ADD_REQ: |
| 362 | req->flags = val; |
| 363 | INIT_LIST_HEAD(&req->node); |
| 364 | list_add_tail(&req->node, &pqf->list); |
| 365 | pqf->effective_flags |= val; |
| 366 | break; |
| 367 | default: |
| 368 | /* no action */ |
| 369 | ; |
| 370 | } |
| 371 | |
| 372 | curr_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags; |
| 373 | |
| 374 | spin_unlock_irqrestore(&pm_qos_lock, irqflags); |
| 375 | |
Sahara | 247e9ee | 2013-06-21 11:12:28 +0900 | [diff] [blame] | 376 | trace_pm_qos_update_flags(action, prev_value, curr_value); |
Rafael J. Wysocki | 5efbe42 | 2012-10-23 01:07:46 +0200 | [diff] [blame] | 377 | return prev_value != curr_value; |
| 378 | } |
| 379 | |
| 380 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 381 | * pm_qos_request - returns current system wide qos expectation |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 382 | * @pm_qos_class: identification of which qos value is requested |
| 383 | * |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 384 | * This function returns the current target value. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 385 | */ |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 386 | int pm_qos_request(int pm_qos_class) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 387 | { |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 388 | return pm_qos_read_value(pm_qos_array[pm_qos_class]->constraints); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 389 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 390 | EXPORT_SYMBOL_GPL(pm_qos_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 391 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 392 | int pm_qos_request_active(struct pm_qos_request *req) |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 393 | { |
| 394 | return req->pm_qos_class != 0; |
| 395 | } |
| 396 | EXPORT_SYMBOL_GPL(pm_qos_request_active); |
| 397 | |
Stephen Boyd | 40fea92f | 2013-08-13 14:12:40 -0700 | [diff] [blame] | 398 | static void __pm_qos_update_request(struct pm_qos_request *req, |
| 399 | s32 new_value) |
| 400 | { |
| 401 | trace_pm_qos_update_request(req->pm_qos_class, new_value); |
| 402 | |
| 403 | if (new_value != req->node.prio) |
| 404 | pm_qos_update_target( |
| 405 | pm_qos_array[req->pm_qos_class]->constraints, |
| 406 | &req->node, PM_QOS_UPDATE_REQ, new_value); |
| 407 | } |
| 408 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 409 | /** |
MyungJoo Ham | c4772d1 | 2012-03-28 23:31:24 +0200 | [diff] [blame] | 410 | * pm_qos_work_fn - the timeout handler of pm_qos_update_request_timeout |
| 411 | * @work: work struct for the delayed work (timeout) |
| 412 | * |
| 413 | * This cancels the timeout request by falling back to the default at timeout. |
| 414 | */ |
| 415 | static void pm_qos_work_fn(struct work_struct *work) |
| 416 | { |
| 417 | struct pm_qos_request *req = container_of(to_delayed_work(work), |
| 418 | struct pm_qos_request, |
| 419 | work); |
| 420 | |
Stephen Boyd | 40fea92f | 2013-08-13 14:12:40 -0700 | [diff] [blame] | 421 | __pm_qos_update_request(req, PM_QOS_DEFAULT_VALUE); |
MyungJoo Ham | c4772d1 | 2012-03-28 23:31:24 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 425 | * pm_qos_add_request - inserts new qos request into the list |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 426 | * @req: pointer to a preallocated handle |
Saravana Kannan | 25cc69e | 2010-08-26 20:18:43 +0200 | [diff] [blame] | 427 | * @pm_qos_class: identifies which list of qos request to use |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 428 | * @value: defines the qos request |
| 429 | * |
| 430 | * This function inserts a new entry in the pm_qos_class list of requested qos |
Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 431 | * performance characteristics. It recomputes the aggregate QoS expectations |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 432 | * for the pm_qos_class of parameters and initializes the pm_qos_request |
Saravana Kannan | 25cc69e | 2010-08-26 20:18:43 +0200 | [diff] [blame] | 433 | * handle. Caller needs to save this handle for later use in updates and |
| 434 | * removal. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 435 | */ |
Saravana Kannan | 25cc69e | 2010-08-26 20:18:43 +0200 | [diff] [blame] | 436 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 437 | void pm_qos_add_request(struct pm_qos_request *req, |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 438 | int pm_qos_class, s32 value) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 439 | { |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 440 | if (!req) /*guard against callers passing in null */ |
| 441 | return; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 442 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 443 | if (pm_qos_request_active(req)) { |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 444 | WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n"); |
| 445 | return; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 446 | } |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 447 | req->pm_qos_class = pm_qos_class; |
MyungJoo Ham | c4772d1 | 2012-03-28 23:31:24 +0200 | [diff] [blame] | 448 | INIT_DELAYED_WORK(&req->work, pm_qos_work_fn); |
Sahara | ae8822b | 2013-06-21 11:12:29 +0900 | [diff] [blame] | 449 | trace_pm_qos_add_request(pm_qos_class, value); |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 450 | pm_qos_update_target(pm_qos_array[pm_qos_class]->constraints, |
| 451 | &req->node, PM_QOS_ADD_REQ, value); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 452 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 453 | EXPORT_SYMBOL_GPL(pm_qos_add_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 454 | |
| 455 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 456 | * pm_qos_update_request - modifies an existing qos request |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 457 | * @req : handle to list element holding a pm_qos request to use |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 458 | * @value: defines the qos request |
| 459 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 460 | * Updates an existing qos request for the pm_qos_class of parameters along |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 461 | * with updating the target pm_qos_class value. |
| 462 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 463 | * Attempts are made to make this code callable on hot code paths. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 464 | */ |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 465 | void pm_qos_update_request(struct pm_qos_request *req, |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 466 | s32 new_value) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 467 | { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 468 | if (!req) /*guard against callers passing in null */ |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 469 | return; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 470 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 471 | if (!pm_qos_request_active(req)) { |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 472 | WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n"); |
| 473 | return; |
| 474 | } |
| 475 | |
Tejun Heo | a81f80f | 2016-09-16 15:49:33 -0400 | [diff] [blame] | 476 | cancel_delayed_work_sync(&req->work); |
Stephen Boyd | 40fea92f | 2013-08-13 14:12:40 -0700 | [diff] [blame] | 477 | __pm_qos_update_request(req, new_value); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 478 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 479 | EXPORT_SYMBOL_GPL(pm_qos_update_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 480 | |
| 481 | /** |
MyungJoo Ham | c4772d1 | 2012-03-28 23:31:24 +0200 | [diff] [blame] | 482 | * pm_qos_update_request_timeout - modifies an existing qos request temporarily. |
| 483 | * @req : handle to list element holding a pm_qos request to use |
| 484 | * @new_value: defines the temporal qos request |
| 485 | * @timeout_us: the effective duration of this qos request in usecs. |
| 486 | * |
| 487 | * After timeout_us, this qos request is cancelled automatically. |
| 488 | */ |
| 489 | void pm_qos_update_request_timeout(struct pm_qos_request *req, s32 new_value, |
| 490 | unsigned long timeout_us) |
| 491 | { |
| 492 | if (!req) |
| 493 | return; |
| 494 | if (WARN(!pm_qos_request_active(req), |
| 495 | "%s called for unknown object.", __func__)) |
| 496 | return; |
| 497 | |
Tejun Heo | ed1ac6e | 2013-01-11 13:37:33 +0100 | [diff] [blame] | 498 | cancel_delayed_work_sync(&req->work); |
MyungJoo Ham | c4772d1 | 2012-03-28 23:31:24 +0200 | [diff] [blame] | 499 | |
Sahara | ae8822b | 2013-06-21 11:12:29 +0900 | [diff] [blame] | 500 | trace_pm_qos_update_request_timeout(req->pm_qos_class, |
| 501 | new_value, timeout_us); |
MyungJoo Ham | c4772d1 | 2012-03-28 23:31:24 +0200 | [diff] [blame] | 502 | if (new_value != req->node.prio) |
| 503 | pm_qos_update_target( |
| 504 | pm_qos_array[req->pm_qos_class]->constraints, |
| 505 | &req->node, PM_QOS_UPDATE_REQ, new_value); |
| 506 | |
| 507 | schedule_delayed_work(&req->work, usecs_to_jiffies(timeout_us)); |
| 508 | } |
| 509 | |
| 510 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 511 | * pm_qos_remove_request - modifies an existing qos request |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 512 | * @req: handle to request list element |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 513 | * |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 514 | * Will remove pm qos request from the list of constraints and |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 515 | * recompute the current target value for the pm_qos_class. Call this |
| 516 | * on slow code paths. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 517 | */ |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 518 | void pm_qos_remove_request(struct pm_qos_request *req) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 519 | { |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 520 | if (!req) /*guard against callers passing in null */ |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 521 | return; |
| 522 | /* silent return to keep pcm code cleaner */ |
| 523 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 524 | if (!pm_qos_request_active(req)) { |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 525 | WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n"); |
| 526 | return; |
| 527 | } |
| 528 | |
Tejun Heo | ed1ac6e | 2013-01-11 13:37:33 +0100 | [diff] [blame] | 529 | cancel_delayed_work_sync(&req->work); |
MyungJoo Ham | c4772d1 | 2012-03-28 23:31:24 +0200 | [diff] [blame] | 530 | |
Sahara | ae8822b | 2013-06-21 11:12:29 +0900 | [diff] [blame] | 531 | trace_pm_qos_remove_request(req->pm_qos_class, PM_QOS_DEFAULT_VALUE); |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 532 | pm_qos_update_target(pm_qos_array[req->pm_qos_class]->constraints, |
| 533 | &req->node, PM_QOS_REMOVE_REQ, |
| 534 | PM_QOS_DEFAULT_VALUE); |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 535 | memset(req, 0, sizeof(*req)); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 536 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 537 | EXPORT_SYMBOL_GPL(pm_qos_remove_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 538 | |
| 539 | /** |
| 540 | * pm_qos_add_notifier - sets notification entry for changes to target value |
| 541 | * @pm_qos_class: identifies which qos target changes should be notified. |
| 542 | * @notifier: notifier block managed by caller. |
| 543 | * |
| 544 | * will register the notifier into a notification chain that gets called |
Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 545 | * upon changes to the pm_qos_class target value. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 546 | */ |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 547 | int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 548 | { |
| 549 | int retval; |
| 550 | |
| 551 | retval = blocking_notifier_chain_register( |
Jean Pihet | 4e1779b | 2011-08-25 15:35:27 +0200 | [diff] [blame] | 552 | pm_qos_array[pm_qos_class]->constraints->notifiers, |
| 553 | notifier); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 554 | |
| 555 | return retval; |
| 556 | } |
| 557 | EXPORT_SYMBOL_GPL(pm_qos_add_notifier); |
| 558 | |
| 559 | /** |
| 560 | * pm_qos_remove_notifier - deletes notification entry from chain. |
| 561 | * @pm_qos_class: identifies which qos target changes are notified. |
| 562 | * @notifier: notifier block to be removed. |
| 563 | * |
| 564 | * will remove the notifier from the notification chain that gets called |
Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 565 | * upon changes to the pm_qos_class target value. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 566 | */ |
| 567 | int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier) |
| 568 | { |
| 569 | int retval; |
| 570 | |
| 571 | retval = blocking_notifier_chain_unregister( |
Jean Pihet | 4e1779b | 2011-08-25 15:35:27 +0200 | [diff] [blame] | 572 | pm_qos_array[pm_qos_class]->constraints->notifiers, |
| 573 | notifier); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 574 | |
| 575 | return retval; |
| 576 | } |
| 577 | EXPORT_SYMBOL_GPL(pm_qos_remove_notifier); |
| 578 | |
Jean Pihet | 4a31a33 | 2011-08-25 15:35:20 +0200 | [diff] [blame] | 579 | /* User space interface to PM QoS classes via misc devices */ |
Nishanth Menon | f5f4eda | 2014-12-05 11:19:08 -0600 | [diff] [blame] | 580 | static int register_pm_qos_misc(struct pm_qos_object *qos, struct dentry *d) |
Jean Pihet | 4a31a33 | 2011-08-25 15:35:20 +0200 | [diff] [blame] | 581 | { |
| 582 | qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR; |
| 583 | qos->pm_qos_power_miscdev.name = qos->name; |
| 584 | qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops; |
| 585 | |
Greg Kroah-Hartman | 659dc45 | 2019-01-22 16:21:47 +0100 | [diff] [blame] | 586 | debugfs_create_file(qos->name, S_IRUGO, d, (void *)qos, |
| 587 | &pm_qos_debug_fops); |
Nishanth Menon | f5f4eda | 2014-12-05 11:19:08 -0600 | [diff] [blame] | 588 | |
Jean Pihet | 4a31a33 | 2011-08-25 15:35:20 +0200 | [diff] [blame] | 589 | return misc_register(&qos->pm_qos_power_miscdev); |
| 590 | } |
| 591 | |
| 592 | static int find_pm_qos_object_by_minor(int minor) |
| 593 | { |
| 594 | int pm_qos_class; |
| 595 | |
Sahara | d24c2a4 | 2013-06-20 11:33:57 +0900 | [diff] [blame] | 596 | for (pm_qos_class = PM_QOS_CPU_DMA_LATENCY; |
Jean Pihet | 4a31a33 | 2011-08-25 15:35:20 +0200 | [diff] [blame] | 597 | pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) { |
| 598 | if (minor == |
| 599 | pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor) |
| 600 | return pm_qos_class; |
| 601 | } |
| 602 | return -1; |
| 603 | } |
| 604 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 605 | static int pm_qos_power_open(struct inode *inode, struct file *filp) |
| 606 | { |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 607 | long pm_qos_class; |
| 608 | |
| 609 | pm_qos_class = find_pm_qos_object_by_minor(iminor(inode)); |
Sahara | d24c2a4 | 2013-06-20 11:33:57 +0900 | [diff] [blame] | 610 | if (pm_qos_class >= PM_QOS_CPU_DMA_LATENCY) { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 611 | struct pm_qos_request *req = kzalloc(sizeof(*req), GFP_KERNEL); |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 612 | if (!req) |
| 613 | return -ENOMEM; |
| 614 | |
| 615 | pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE); |
| 616 | filp->private_data = req; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 617 | |
Guennadi Liakhovetski | 6513fd6 | 2011-11-03 10:12:36 +0100 | [diff] [blame] | 618 | return 0; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 619 | } |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 620 | return -EPERM; |
| 621 | } |
| 622 | |
| 623 | static int pm_qos_power_release(struct inode *inode, struct file *filp) |
| 624 | { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 625 | struct pm_qos_request *req; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 626 | |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 627 | req = filp->private_data; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 628 | pm_qos_remove_request(req); |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 629 | kfree(req); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 634 | |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 635 | static ssize_t pm_qos_power_read(struct file *filp, char __user *buf, |
| 636 | size_t count, loff_t *f_pos) |
| 637 | { |
| 638 | s32 value; |
| 639 | unsigned long flags; |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 640 | struct pm_qos_request *req = filp->private_data; |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 641 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 642 | if (!req) |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 643 | return -EINVAL; |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 644 | if (!pm_qos_request_active(req)) |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 645 | return -EINVAL; |
| 646 | |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 647 | spin_lock_irqsave(&pm_qos_lock, flags); |
Jean Pihet | abe98ec | 2011-08-25 15:35:34 +0200 | [diff] [blame] | 648 | value = pm_qos_get_value(pm_qos_array[req->pm_qos_class]->constraints); |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 649 | spin_unlock_irqrestore(&pm_qos_lock, flags); |
| 650 | |
| 651 | return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32)); |
| 652 | } |
| 653 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 654 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, |
| 655 | size_t count, loff_t *f_pos) |
| 656 | { |
| 657 | s32 value; |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 658 | struct pm_qos_request *req; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 659 | |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 660 | if (count == sizeof(s32)) { |
| 661 | if (copy_from_user(&value, buf, sizeof(s32))) |
| 662 | return -EFAULT; |
Andy Shevchenko | d4f7ecf | 2013-09-11 17:02:38 +0300 | [diff] [blame] | 663 | } else { |
Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 664 | int ret; |
| 665 | |
Andy Shevchenko | d4f7ecf | 2013-09-11 17:02:38 +0300 | [diff] [blame] | 666 | ret = kstrtos32_from_user(buf, count, 16, &value); |
| 667 | if (ret) |
| 668 | return ret; |
Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 669 | } |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 670 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 671 | req = filp->private_data; |
| 672 | pm_qos_update_request(req, value); |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 673 | |
| 674 | return count; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | |
| 678 | static int __init pm_qos_power_init(void) |
| 679 | { |
| 680 | int ret = 0; |
Alex Frid | d031e1d | 2012-01-29 20:39:25 +0100 | [diff] [blame] | 681 | int i; |
Nishanth Menon | f5f4eda | 2014-12-05 11:19:08 -0600 | [diff] [blame] | 682 | struct dentry *d; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 683 | |
Alex Frid | d031e1d | 2012-01-29 20:39:25 +0100 | [diff] [blame] | 684 | BUILD_BUG_ON(ARRAY_SIZE(pm_qos_array) != PM_QOS_NUM_CLASSES); |
| 685 | |
Nishanth Menon | f5f4eda | 2014-12-05 11:19:08 -0600 | [diff] [blame] | 686 | d = debugfs_create_dir("pm_qos", NULL); |
Nishanth Menon | f5f4eda | 2014-12-05 11:19:08 -0600 | [diff] [blame] | 687 | |
Sahara | d24c2a4 | 2013-06-20 11:33:57 +0900 | [diff] [blame] | 688 | for (i = PM_QOS_CPU_DMA_LATENCY; i < PM_QOS_NUM_CLASSES; i++) { |
Nishanth Menon | f5f4eda | 2014-12-05 11:19:08 -0600 | [diff] [blame] | 689 | ret = register_pm_qos_misc(pm_qos_array[i], d); |
Alex Frid | d031e1d | 2012-01-29 20:39:25 +0100 | [diff] [blame] | 690 | if (ret < 0) { |
Joe Perches | 64ec72a | 2017-09-27 22:01:34 -0700 | [diff] [blame] | 691 | pr_err("%s: %s setup failed\n", |
| 692 | __func__, pm_qos_array[i]->name); |
Alex Frid | d031e1d | 2012-01-29 20:39:25 +0100 | [diff] [blame] | 693 | return ret; |
| 694 | } |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 695 | } |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 696 | |
| 697 | return ret; |
| 698 | } |
| 699 | |
| 700 | late_initcall(pm_qos_power_init); |