Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * This module exposes the interface to kernel space for specifying |
| 3 | * QoS dependencies. It provides infrastructure for registration of: |
| 4 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 5 | * Dependents on a QoS value : register requests |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 6 | * Watchers of QoS value : get notified when target QoS value changes |
| 7 | * |
| 8 | * This QoS design is best effort based. Dependents register their QoS needs. |
| 9 | * Watchers register to keep track of the current QoS needs of the system. |
| 10 | * |
| 11 | * There are 3 basic classes of QoS parameter: latency, timeout, throughput |
| 12 | * each have defined units: |
| 13 | * latency: usec |
| 14 | * timeout: usec <-- currently not used. |
| 15 | * throughput: kbs (kilo byte / sec) |
| 16 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 17 | * There are lists of pm_qos_objects each one wrapping requests, notifiers |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 18 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 19 | * User mode requests on a QOS parameter register themselves to the |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 20 | * subsystem by opening the device node /dev/... and writing there request to |
| 21 | * the node. As long as the process holds a file handle open to the node the |
| 22 | * client continues to be accounted for. Upon file release the usermode |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 23 | * request is removed and a new qos target is computed. This way when the |
| 24 | * request that the application has is cleaned up when closes the file |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 25 | * pointer or exits the pm_qos_object will get an opportunity to clean up. |
| 26 | * |
Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 27 | * Mark Gross <mgross@linux.intel.com> |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 28 | */ |
| 29 | |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 30 | /*#define DEBUG*/ |
| 31 | |
Jean Pihet | e8db0be | 2011-08-25 15:35:03 +0200 | [diff] [blame] | 32 | #include <linux/pm_qos.h> |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 33 | #include <linux/sched.h> |
| 34 | #include <linux/spinlock.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/time.h> |
| 37 | #include <linux/fs.h> |
| 38 | #include <linux/device.h> |
| 39 | #include <linux/miscdevice.h> |
| 40 | #include <linux/string.h> |
| 41 | #include <linux/platform_device.h> |
| 42 | #include <linux/init.h> |
Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 43 | #include <linux/kernel.h> |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 44 | |
| 45 | #include <linux/uaccess.h> |
| 46 | |
| 47 | /* |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 48 | * locking rule: all changes to constraints or notifiers lists |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 49 | * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock |
| 50 | * held, taken with _irqsave. One lock to rule them all |
| 51 | */ |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 52 | enum pm_qos_type { |
| 53 | PM_QOS_MAX, /* return the largest value */ |
| 54 | PM_QOS_MIN /* return the smallest value */ |
| 55 | }; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 56 | |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 57 | /* |
| 58 | * Note: The lockless read path depends on the CPU accessing |
| 59 | * target_value atomically. Atomic access is only guaranteed on all CPU |
| 60 | * types linux supports for 32 bit quantites |
| 61 | */ |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 62 | struct pm_qos_object { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 63 | struct plist_head constraints; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 64 | struct blocking_notifier_head *notifiers; |
| 65 | struct miscdevice pm_qos_power_miscdev; |
| 66 | char *name; |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 67 | s32 target_value; /* Do not change to 64 bit */ |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 68 | s32 default_value; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 69 | enum pm_qos_type type; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 72 | static DEFINE_SPINLOCK(pm_qos_lock); |
| 73 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 74 | static struct pm_qos_object null_pm_qos; |
| 75 | static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier); |
| 76 | static struct pm_qos_object cpu_dma_pm_qos = { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 77 | .constraints = PLIST_HEAD_INIT(cpu_dma_pm_qos.constraints), |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 78 | .notifiers = &cpu_dma_lat_notifier, |
| 79 | .name = "cpu_dma_latency", |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 80 | .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE, |
| 81 | .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE, |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 82 | .type = PM_QOS_MIN, |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | static BLOCKING_NOTIFIER_HEAD(network_lat_notifier); |
| 86 | static struct pm_qos_object network_lat_pm_qos = { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 87 | .constraints = PLIST_HEAD_INIT(network_lat_pm_qos.constraints), |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 88 | .notifiers = &network_lat_notifier, |
| 89 | .name = "network_latency", |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 90 | .target_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE, |
| 91 | .default_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE, |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 92 | .type = PM_QOS_MIN |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | |
| 96 | static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier); |
| 97 | static struct pm_qos_object network_throughput_pm_qos = { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 98 | .constraints = PLIST_HEAD_INIT(network_throughput_pm_qos.constraints), |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 99 | .notifiers = &network_throughput_notifier, |
| 100 | .name = "network_throughput", |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 101 | .target_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE, |
| 102 | .default_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE, |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 103 | .type = PM_QOS_MAX, |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | |
| 107 | static struct pm_qos_object *pm_qos_array[] = { |
| 108 | &null_pm_qos, |
| 109 | &cpu_dma_pm_qos, |
| 110 | &network_lat_pm_qos, |
| 111 | &network_throughput_pm_qos |
| 112 | }; |
| 113 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 114 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, |
| 115 | size_t count, loff_t *f_pos); |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 116 | static ssize_t pm_qos_power_read(struct file *filp, char __user *buf, |
| 117 | size_t count, loff_t *f_pos); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 118 | static int pm_qos_power_open(struct inode *inode, struct file *filp); |
| 119 | static int pm_qos_power_release(struct inode *inode, struct file *filp); |
| 120 | |
| 121 | static const struct file_operations pm_qos_power_fops = { |
| 122 | .write = pm_qos_power_write, |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 123 | .read = pm_qos_power_read, |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 124 | .open = pm_qos_power_open, |
| 125 | .release = pm_qos_power_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 126 | .llseek = noop_llseek, |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 127 | }; |
| 128 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 129 | /* unlocked internal variant */ |
| 130 | static inline int pm_qos_get_value(struct pm_qos_object *o) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 131 | { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 132 | if (plist_head_empty(&o->constraints)) |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 133 | return o->default_value; |
| 134 | |
| 135 | switch (o->type) { |
| 136 | case PM_QOS_MIN: |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 137 | return plist_first(&o->constraints)->prio; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 138 | |
| 139 | case PM_QOS_MAX: |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 140 | return plist_last(&o->constraints)->prio; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 141 | |
| 142 | default: |
| 143 | /* runtime check for not using enum */ |
| 144 | BUG(); |
| 145 | } |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 148 | static inline s32 pm_qos_read_value(struct pm_qos_object *o) |
| 149 | { |
| 150 | return o->target_value; |
| 151 | } |
| 152 | |
| 153 | static inline void pm_qos_set_value(struct pm_qos_object *o, s32 value) |
| 154 | { |
| 155 | o->target_value = value; |
| 156 | } |
| 157 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 158 | static void update_target(struct pm_qos_object *o, struct plist_node *node, |
| 159 | int del, int value) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 160 | { |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 161 | unsigned long flags; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 162 | int prev_value, curr_value; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 163 | |
| 164 | spin_lock_irqsave(&pm_qos_lock, flags); |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 165 | prev_value = pm_qos_get_value(o); |
| 166 | /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */ |
| 167 | if (value != PM_QOS_DEFAULT_VALUE) { |
| 168 | /* |
| 169 | * to change the list, we atomically remove, reinit |
| 170 | * with new value and add, then see if the extremal |
| 171 | * changed |
| 172 | */ |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 173 | plist_del(node, &o->constraints); |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 174 | plist_node_init(node, value); |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 175 | plist_add(node, &o->constraints); |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 176 | } else if (del) { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 177 | plist_del(node, &o->constraints); |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 178 | } else { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 179 | plist_add(node, &o->constraints); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 180 | } |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 181 | curr_value = pm_qos_get_value(o); |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 182 | pm_qos_set_value(o, curr_value); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 183 | spin_unlock_irqrestore(&pm_qos_lock, flags); |
| 184 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 185 | if (prev_value != curr_value) |
| 186 | blocking_notifier_call_chain(o->notifiers, |
| 187 | (unsigned long)curr_value, |
| 188 | NULL); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 191 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 192 | * pm_qos_request - returns current system wide qos expectation |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 193 | * @pm_qos_class: identification of which qos value is requested |
| 194 | * |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 195 | * This function returns the current target value. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 196 | */ |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 197 | int pm_qos_request(int pm_qos_class) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 198 | { |
Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 199 | return pm_qos_read_value(pm_qos_array[pm_qos_class]); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 200 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 201 | EXPORT_SYMBOL_GPL(pm_qos_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 202 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 203 | int pm_qos_request_active(struct pm_qos_request *req) |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 204 | { |
| 205 | return req->pm_qos_class != 0; |
| 206 | } |
| 207 | EXPORT_SYMBOL_GPL(pm_qos_request_active); |
| 208 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 209 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 210 | * pm_qos_add_request - inserts new qos request into the list |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 211 | * @req: pointer to a preallocated handle |
Saravana Kannan | 25cc69e | 2010-08-26 20:18:43 +0200 | [diff] [blame] | 212 | * @pm_qos_class: identifies which list of qos request to use |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 213 | * @value: defines the qos request |
| 214 | * |
| 215 | * 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] | 216 | * performance characteristics. It recomputes the aggregate QoS expectations |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 217 | * 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] | 218 | * handle. Caller needs to save this handle for later use in updates and |
| 219 | * removal. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 220 | */ |
Saravana Kannan | 25cc69e | 2010-08-26 20:18:43 +0200 | [diff] [blame] | 221 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 222 | void pm_qos_add_request(struct pm_qos_request *req, |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 223 | int pm_qos_class, s32 value) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 224 | { |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 225 | struct pm_qos_object *o = pm_qos_array[pm_qos_class]; |
| 226 | int new_value; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 227 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 228 | if (pm_qos_request_active(req)) { |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 229 | WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n"); |
| 230 | return; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 231 | } |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 232 | if (value == PM_QOS_DEFAULT_VALUE) |
| 233 | new_value = o->default_value; |
| 234 | else |
| 235 | new_value = value; |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 236 | plist_node_init(&req->node, new_value); |
| 237 | req->pm_qos_class = pm_qos_class; |
| 238 | update_target(o, &req->node, 0, PM_QOS_DEFAULT_VALUE); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 239 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 240 | EXPORT_SYMBOL_GPL(pm_qos_add_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 241 | |
| 242 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 243 | * pm_qos_update_request - modifies an existing qos request |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 244 | * @req : handle to list element holding a pm_qos request to use |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 245 | * @value: defines the qos request |
| 246 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 247 | * 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] | 248 | * with updating the target pm_qos_class value. |
| 249 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 250 | * Attempts are made to make this code callable on hot code paths. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 251 | */ |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 252 | void pm_qos_update_request(struct pm_qos_request *req, |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 253 | s32 new_value) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 254 | { |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 255 | s32 temp; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 256 | struct pm_qos_object *o; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 257 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 258 | if (!req) /*guard against callers passing in null */ |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 259 | return; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 260 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 261 | if (!pm_qos_request_active(req)) { |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 262 | WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n"); |
| 263 | return; |
| 264 | } |
| 265 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 266 | o = pm_qos_array[req->pm_qos_class]; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 267 | |
| 268 | if (new_value == PM_QOS_DEFAULT_VALUE) |
| 269 | temp = o->default_value; |
| 270 | else |
| 271 | temp = new_value; |
| 272 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 273 | if (temp != req->node.prio) |
| 274 | update_target(o, &req->node, 0, temp); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 275 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 276 | EXPORT_SYMBOL_GPL(pm_qos_update_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 277 | |
| 278 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 279 | * pm_qos_remove_request - modifies an existing qos request |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 280 | * @req: handle to request list element |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 281 | * |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 282 | * Will remove pm qos request from the list of constraints and |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 283 | * recompute the current target value for the pm_qos_class. Call this |
| 284 | * on slow code paths. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 285 | */ |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 286 | void pm_qos_remove_request(struct pm_qos_request *req) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 287 | { |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 288 | struct pm_qos_object *o; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 289 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 290 | if (req == NULL) |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 291 | return; |
| 292 | /* silent return to keep pcm code cleaner */ |
| 293 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 294 | if (!pm_qos_request_active(req)) { |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 295 | WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n"); |
| 296 | return; |
| 297 | } |
| 298 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 299 | o = pm_qos_array[req->pm_qos_class]; |
| 300 | update_target(o, &req->node, 1, PM_QOS_DEFAULT_VALUE); |
| 301 | memset(req, 0, sizeof(*req)); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 302 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 303 | EXPORT_SYMBOL_GPL(pm_qos_remove_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 304 | |
| 305 | /** |
| 306 | * pm_qos_add_notifier - sets notification entry for changes to target value |
| 307 | * @pm_qos_class: identifies which qos target changes should be notified. |
| 308 | * @notifier: notifier block managed by caller. |
| 309 | * |
| 310 | * will register the notifier into a notification chain that gets called |
Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 311 | * upon changes to the pm_qos_class target value. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 312 | */ |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 313 | 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] | 314 | { |
| 315 | int retval; |
| 316 | |
| 317 | retval = blocking_notifier_chain_register( |
| 318 | pm_qos_array[pm_qos_class]->notifiers, notifier); |
| 319 | |
| 320 | return retval; |
| 321 | } |
| 322 | EXPORT_SYMBOL_GPL(pm_qos_add_notifier); |
| 323 | |
| 324 | /** |
| 325 | * pm_qos_remove_notifier - deletes notification entry from chain. |
| 326 | * @pm_qos_class: identifies which qos target changes are notified. |
| 327 | * @notifier: notifier block to be removed. |
| 328 | * |
| 329 | * will remove the notifier from the notification chain that gets called |
Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 330 | * upon changes to the pm_qos_class target value. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 331 | */ |
| 332 | int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier) |
| 333 | { |
| 334 | int retval; |
| 335 | |
| 336 | retval = blocking_notifier_chain_unregister( |
| 337 | pm_qos_array[pm_qos_class]->notifiers, notifier); |
| 338 | |
| 339 | return retval; |
| 340 | } |
| 341 | EXPORT_SYMBOL_GPL(pm_qos_remove_notifier); |
| 342 | |
Jean Pihet | 4a31a33 | 2011-08-25 15:35:20 +0200 | [diff] [blame^] | 343 | /* User space interface to PM QoS classes via misc devices */ |
| 344 | static int register_pm_qos_misc(struct pm_qos_object *qos) |
| 345 | { |
| 346 | qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR; |
| 347 | qos->pm_qos_power_miscdev.name = qos->name; |
| 348 | qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops; |
| 349 | |
| 350 | return misc_register(&qos->pm_qos_power_miscdev); |
| 351 | } |
| 352 | |
| 353 | static int find_pm_qos_object_by_minor(int minor) |
| 354 | { |
| 355 | int pm_qos_class; |
| 356 | |
| 357 | for (pm_qos_class = 0; |
| 358 | pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) { |
| 359 | if (minor == |
| 360 | pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor) |
| 361 | return pm_qos_class; |
| 362 | } |
| 363 | return -1; |
| 364 | } |
| 365 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 366 | static int pm_qos_power_open(struct inode *inode, struct file *filp) |
| 367 | { |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 368 | long pm_qos_class; |
| 369 | |
| 370 | pm_qos_class = find_pm_qos_object_by_minor(iminor(inode)); |
| 371 | if (pm_qos_class >= 0) { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 372 | struct pm_qos_request *req = kzalloc(sizeof(*req), GFP_KERNEL); |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 373 | if (!req) |
| 374 | return -ENOMEM; |
| 375 | |
| 376 | pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE); |
| 377 | filp->private_data = req; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 378 | |
| 379 | if (filp->private_data) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 380 | return 0; |
| 381 | } |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 382 | return -EPERM; |
| 383 | } |
| 384 | |
| 385 | static int pm_qos_power_release(struct inode *inode, struct file *filp) |
| 386 | { |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 387 | struct pm_qos_request *req; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 388 | |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 389 | req = filp->private_data; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 390 | pm_qos_remove_request(req); |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 391 | kfree(req); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 392 | |
| 393 | return 0; |
| 394 | } |
| 395 | |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 396 | |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 397 | static ssize_t pm_qos_power_read(struct file *filp, char __user *buf, |
| 398 | size_t count, loff_t *f_pos) |
| 399 | { |
| 400 | s32 value; |
| 401 | unsigned long flags; |
| 402 | struct pm_qos_object *o; |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 403 | struct pm_qos_request *req = filp->private_data; |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 404 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 405 | if (!req) |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 406 | return -EINVAL; |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 407 | if (!pm_qos_request_active(req)) |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 408 | return -EINVAL; |
| 409 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 410 | o = pm_qos_array[req->pm_qos_class]; |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 411 | spin_lock_irqsave(&pm_qos_lock, flags); |
| 412 | value = pm_qos_get_value(o); |
| 413 | spin_unlock_irqrestore(&pm_qos_lock, flags); |
| 414 | |
| 415 | return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32)); |
| 416 | } |
| 417 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 418 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, |
| 419 | size_t count, loff_t *f_pos) |
| 420 | { |
| 421 | s32 value; |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 422 | struct pm_qos_request *req; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 423 | |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 424 | if (count == sizeof(s32)) { |
| 425 | if (copy_from_user(&value, buf, sizeof(s32))) |
| 426 | return -EFAULT; |
Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 427 | } else if (count <= 11) { /* ASCII perhaps? */ |
| 428 | char ascii_value[11]; |
| 429 | unsigned long int ulval; |
| 430 | int ret; |
| 431 | |
| 432 | if (copy_from_user(ascii_value, buf, count)) |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 433 | return -EFAULT; |
Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 434 | |
| 435 | if (count > 10) { |
| 436 | if (ascii_value[10] == '\n') |
| 437 | ascii_value[10] = '\0'; |
| 438 | else |
| 439 | return -EINVAL; |
| 440 | } else { |
| 441 | ascii_value[count] = '\0'; |
| 442 | } |
| 443 | ret = strict_strtoul(ascii_value, 16, &ulval); |
| 444 | if (ret) { |
| 445 | pr_debug("%s, 0x%lx, 0x%x\n", ascii_value, ulval, ret); |
mark gross | 0109c2c | 2010-09-09 23:20:09 +0200 | [diff] [blame] | 446 | return -EINVAL; |
Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 447 | } |
| 448 | value = (s32)lower_32_bits(ulval); |
| 449 | } else { |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 450 | return -EINVAL; |
Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 451 | } |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 452 | |
Jean Pihet | cc74998 | 2011-08-25 15:35:12 +0200 | [diff] [blame] | 453 | req = filp->private_data; |
| 454 | pm_qos_update_request(req, value); |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 455 | |
| 456 | return count; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
| 460 | static int __init pm_qos_power_init(void) |
| 461 | { |
| 462 | int ret = 0; |
| 463 | |
| 464 | ret = register_pm_qos_misc(&cpu_dma_pm_qos); |
| 465 | if (ret < 0) { |
| 466 | printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n"); |
| 467 | return ret; |
| 468 | } |
| 469 | ret = register_pm_qos_misc(&network_lat_pm_qos); |
| 470 | if (ret < 0) { |
| 471 | printk(KERN_ERR "pm_qos_param: network_latency setup failed\n"); |
| 472 | return ret; |
| 473 | } |
| 474 | ret = register_pm_qos_misc(&network_throughput_pm_qos); |
| 475 | if (ret < 0) |
| 476 | printk(KERN_ERR |
| 477 | "pm_qos_param: network_throughput setup failed\n"); |
| 478 | |
| 479 | return ret; |
| 480 | } |
| 481 | |
| 482 | late_initcall(pm_qos_power_init); |