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 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 32 | #include <linux/pm_qos_params.h> |
| 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> |
| 43 | |
| 44 | #include <linux/uaccess.h> |
| 45 | |
| 46 | /* |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 47 | * locking rule: all changes to requests or notifiers lists |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 48 | * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock |
| 49 | * held, taken with _irqsave. One lock to rule them all |
| 50 | */ |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 51 | enum pm_qos_type { |
| 52 | PM_QOS_MAX, /* return the largest value */ |
| 53 | PM_QOS_MIN /* return the smallest value */ |
| 54 | }; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 55 | |
| 56 | struct pm_qos_object { |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 57 | struct plist_head requests; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 58 | struct blocking_notifier_head *notifiers; |
| 59 | struct miscdevice pm_qos_power_miscdev; |
| 60 | char *name; |
| 61 | s32 default_value; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 62 | enum pm_qos_type type; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 63 | }; |
| 64 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 65 | static DEFINE_SPINLOCK(pm_qos_lock); |
| 66 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 67 | static struct pm_qos_object null_pm_qos; |
| 68 | static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier); |
| 69 | static struct pm_qos_object cpu_dma_pm_qos = { |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 70 | .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests, pm_qos_lock), |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 71 | .notifiers = &cpu_dma_lat_notifier, |
| 72 | .name = "cpu_dma_latency", |
| 73 | .default_value = 2000 * USEC_PER_SEC, |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 74 | .type = PM_QOS_MIN, |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static BLOCKING_NOTIFIER_HEAD(network_lat_notifier); |
| 78 | static struct pm_qos_object network_lat_pm_qos = { |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 79 | .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests, pm_qos_lock), |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 80 | .notifiers = &network_lat_notifier, |
| 81 | .name = "network_latency", |
| 82 | .default_value = 2000 * USEC_PER_SEC, |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 83 | .type = PM_QOS_MIN |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | |
| 87 | static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier); |
| 88 | static struct pm_qos_object network_throughput_pm_qos = { |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 89 | .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests, pm_qos_lock), |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 90 | .notifiers = &network_throughput_notifier, |
| 91 | .name = "network_throughput", |
| 92 | .default_value = 0, |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 93 | .type = PM_QOS_MAX, |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | |
| 97 | static struct pm_qos_object *pm_qos_array[] = { |
| 98 | &null_pm_qos, |
| 99 | &cpu_dma_pm_qos, |
| 100 | &network_lat_pm_qos, |
| 101 | &network_throughput_pm_qos |
| 102 | }; |
| 103 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 104 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, |
| 105 | size_t count, loff_t *f_pos); |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 106 | static ssize_t pm_qos_power_read(struct file *filp, char __user *buf, |
| 107 | size_t count, loff_t *f_pos); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 108 | static int pm_qos_power_open(struct inode *inode, struct file *filp); |
| 109 | static int pm_qos_power_release(struct inode *inode, struct file *filp); |
| 110 | |
| 111 | static const struct file_operations pm_qos_power_fops = { |
| 112 | .write = pm_qos_power_write, |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 113 | .read = pm_qos_power_read, |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 114 | .open = pm_qos_power_open, |
| 115 | .release = pm_qos_power_release, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 116 | .llseek = noop_llseek, |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 119 | /* unlocked internal variant */ |
| 120 | static inline int pm_qos_get_value(struct pm_qos_object *o) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 121 | { |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 122 | if (plist_head_empty(&o->requests)) |
| 123 | return o->default_value; |
| 124 | |
| 125 | switch (o->type) { |
| 126 | case PM_QOS_MIN: |
Colin Cross | 00fafcd | 2010-11-15 22:45:22 +0100 | [diff] [blame] | 127 | return plist_first(&o->requests)->prio; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 128 | |
| 129 | case PM_QOS_MAX: |
Colin Cross | 00fafcd | 2010-11-15 22:45:22 +0100 | [diff] [blame] | 130 | return plist_last(&o->requests)->prio; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 131 | |
| 132 | default: |
| 133 | /* runtime check for not using enum */ |
| 134 | BUG(); |
| 135 | } |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 136 | } |
| 137 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 138 | static void update_target(struct pm_qos_object *o, struct plist_node *node, |
| 139 | int del, int value) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 140 | { |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 141 | unsigned long flags; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 142 | int prev_value, curr_value; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 143 | |
| 144 | spin_lock_irqsave(&pm_qos_lock, flags); |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 145 | prev_value = pm_qos_get_value(o); |
| 146 | /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */ |
| 147 | if (value != PM_QOS_DEFAULT_VALUE) { |
| 148 | /* |
| 149 | * to change the list, we atomically remove, reinit |
| 150 | * with new value and add, then see if the extremal |
| 151 | * changed |
| 152 | */ |
| 153 | plist_del(node, &o->requests); |
| 154 | plist_node_init(node, value); |
| 155 | plist_add(node, &o->requests); |
| 156 | } else if (del) { |
| 157 | plist_del(node, &o->requests); |
| 158 | } else { |
| 159 | plist_add(node, &o->requests); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 160 | } |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 161 | curr_value = pm_qos_get_value(o); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 162 | spin_unlock_irqrestore(&pm_qos_lock, flags); |
| 163 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 164 | if (prev_value != curr_value) |
| 165 | blocking_notifier_call_chain(o->notifiers, |
| 166 | (unsigned long)curr_value, |
| 167 | NULL); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static int register_pm_qos_misc(struct pm_qos_object *qos) |
| 171 | { |
| 172 | qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR; |
| 173 | qos->pm_qos_power_miscdev.name = qos->name; |
| 174 | qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops; |
| 175 | |
| 176 | return misc_register(&qos->pm_qos_power_miscdev); |
| 177 | } |
| 178 | |
| 179 | static int find_pm_qos_object_by_minor(int minor) |
| 180 | { |
| 181 | int pm_qos_class; |
| 182 | |
| 183 | for (pm_qos_class = 0; |
| 184 | pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) { |
| 185 | if (minor == |
| 186 | pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor) |
| 187 | return pm_qos_class; |
| 188 | } |
| 189 | return -1; |
| 190 | } |
| 191 | |
| 192 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 193 | * pm_qos_request - returns current system wide qos expectation |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 194 | * @pm_qos_class: identification of which qos value is requested |
| 195 | * |
| 196 | * This function returns the current target value in an atomic manner. |
| 197 | */ |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 198 | int pm_qos_request(int pm_qos_class) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 199 | { |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 200 | unsigned long flags; |
| 201 | int value; |
| 202 | |
| 203 | spin_lock_irqsave(&pm_qos_lock, flags); |
| 204 | value = pm_qos_get_value(pm_qos_array[pm_qos_class]); |
| 205 | spin_unlock_irqrestore(&pm_qos_lock, flags); |
| 206 | |
| 207 | return value; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 208 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 209 | EXPORT_SYMBOL_GPL(pm_qos_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 210 | |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 211 | int pm_qos_request_active(struct pm_qos_request_list *req) |
| 212 | { |
| 213 | return req->pm_qos_class != 0; |
| 214 | } |
| 215 | EXPORT_SYMBOL_GPL(pm_qos_request_active); |
| 216 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 217 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 218 | * pm_qos_add_request - inserts new qos request into the list |
Saravana Kannan | 25cc69e | 2010-08-26 20:18:43 +0200 | [diff] [blame] | 219 | * @dep: pointer to a preallocated handle |
| 220 | * @pm_qos_class: identifies which list of qos request to use |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 221 | * @value: defines the qos request |
| 222 | * |
| 223 | * 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] | 224 | * performance characteristics. It recomputes the aggregate QoS expectations |
Saravana Kannan | 25cc69e | 2010-08-26 20:18:43 +0200 | [diff] [blame] | 225 | * for the pm_qos_class of parameters and initializes the pm_qos_request_list |
| 226 | * handle. Caller needs to save this handle for later use in updates and |
| 227 | * removal. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 228 | */ |
Saravana Kannan | 25cc69e | 2010-08-26 20:18:43 +0200 | [diff] [blame] | 229 | |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 230 | void pm_qos_add_request(struct pm_qos_request_list *dep, |
| 231 | int pm_qos_class, s32 value) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 232 | { |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 233 | struct pm_qos_object *o = pm_qos_array[pm_qos_class]; |
| 234 | int new_value; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 235 | |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 236 | if (pm_qos_request_active(dep)) { |
| 237 | WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n"); |
| 238 | return; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 239 | } |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 240 | if (value == PM_QOS_DEFAULT_VALUE) |
| 241 | new_value = o->default_value; |
| 242 | else |
| 243 | new_value = value; |
| 244 | plist_node_init(&dep->list, new_value); |
| 245 | dep->pm_qos_class = pm_qos_class; |
| 246 | update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 247 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 248 | EXPORT_SYMBOL_GPL(pm_qos_add_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 249 | |
| 250 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 251 | * pm_qos_update_request - modifies an existing qos request |
| 252 | * @pm_qos_req : handle to list element holding a pm_qos request to use |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 253 | * @value: defines the qos request |
| 254 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 255 | * 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] | 256 | * with updating the target pm_qos_class value. |
| 257 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 258 | * Attempts are made to make this code callable on hot code paths. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 259 | */ |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 260 | void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req, |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 261 | s32 new_value) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 262 | { |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 263 | s32 temp; |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 264 | struct pm_qos_object *o; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 265 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 266 | if (!pm_qos_req) /*guard against callers passing in null */ |
| 267 | return; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 268 | |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 269 | if (!pm_qos_request_active(pm_qos_req)) { |
| 270 | WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n"); |
| 271 | return; |
| 272 | } |
| 273 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 274 | o = pm_qos_array[pm_qos_req->pm_qos_class]; |
| 275 | |
| 276 | if (new_value == PM_QOS_DEFAULT_VALUE) |
| 277 | temp = o->default_value; |
| 278 | else |
| 279 | temp = new_value; |
| 280 | |
| 281 | if (temp != pm_qos_req->list.prio) |
| 282 | update_target(o, &pm_qos_req->list, 0, temp); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 283 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 284 | EXPORT_SYMBOL_GPL(pm_qos_update_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 285 | |
| 286 | /** |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 287 | * pm_qos_remove_request - modifies an existing qos request |
| 288 | * @pm_qos_req: handle to request list element |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 289 | * |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 290 | * Will remove pm qos request from the list of requests and |
| 291 | * recompute the current target value for the pm_qos_class. Call this |
| 292 | * on slow code paths. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 293 | */ |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 294 | void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 295 | { |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 296 | struct pm_qos_object *o; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 297 | |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 298 | if (pm_qos_req == NULL) |
| 299 | return; |
| 300 | /* silent return to keep pcm code cleaner */ |
| 301 | |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 302 | if (!pm_qos_request_active(pm_qos_req)) { |
| 303 | WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n"); |
| 304 | return; |
| 305 | } |
| 306 | |
James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 307 | o = pm_qos_array[pm_qos_req->pm_qos_class]; |
| 308 | update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE); |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 309 | memset(pm_qos_req, 0, sizeof(*pm_qos_req)); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 310 | } |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 311 | EXPORT_SYMBOL_GPL(pm_qos_remove_request); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 312 | |
| 313 | /** |
| 314 | * pm_qos_add_notifier - sets notification entry for changes to target value |
| 315 | * @pm_qos_class: identifies which qos target changes should be notified. |
| 316 | * @notifier: notifier block managed by caller. |
| 317 | * |
| 318 | * will register the notifier into a notification chain that gets called |
Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 319 | * upon changes to the pm_qos_class target value. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 320 | */ |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 321 | 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] | 322 | { |
| 323 | int retval; |
| 324 | |
| 325 | retval = blocking_notifier_chain_register( |
| 326 | pm_qos_array[pm_qos_class]->notifiers, notifier); |
| 327 | |
| 328 | return retval; |
| 329 | } |
| 330 | EXPORT_SYMBOL_GPL(pm_qos_add_notifier); |
| 331 | |
| 332 | /** |
| 333 | * pm_qos_remove_notifier - deletes notification entry from chain. |
| 334 | * @pm_qos_class: identifies which qos target changes are notified. |
| 335 | * @notifier: notifier block to be removed. |
| 336 | * |
| 337 | * will remove the notifier from the notification chain that gets called |
Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 338 | * upon changes to the pm_qos_class target value. |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 339 | */ |
| 340 | int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier) |
| 341 | { |
| 342 | int retval; |
| 343 | |
| 344 | retval = blocking_notifier_chain_unregister( |
| 345 | pm_qos_array[pm_qos_class]->notifiers, notifier); |
| 346 | |
| 347 | return retval; |
| 348 | } |
| 349 | EXPORT_SYMBOL_GPL(pm_qos_remove_notifier); |
| 350 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 351 | static int pm_qos_power_open(struct inode *inode, struct file *filp) |
| 352 | { |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 353 | long pm_qos_class; |
| 354 | |
| 355 | pm_qos_class = find_pm_qos_object_by_minor(iminor(inode)); |
| 356 | if (pm_qos_class >= 0) { |
David Alan Gilbert | bac1e74 | 2010-08-24 20:22:18 +0200 | [diff] [blame] | 357 | struct pm_qos_request_list *req = kzalloc(sizeof(*req), GFP_KERNEL); |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 358 | if (!req) |
| 359 | return -ENOMEM; |
| 360 | |
| 361 | pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE); |
| 362 | filp->private_data = req; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 363 | |
| 364 | if (filp->private_data) |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 365 | return 0; |
| 366 | } |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 367 | return -EPERM; |
| 368 | } |
| 369 | |
| 370 | static int pm_qos_power_release(struct inode *inode, struct file *filp) |
| 371 | { |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 372 | struct pm_qos_request_list *req; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 373 | |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 374 | req = filp->private_data; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 375 | pm_qos_remove_request(req); |
James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 376 | kfree(req); |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 377 | |
| 378 | return 0; |
| 379 | } |
| 380 | |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 381 | |
Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 382 | static ssize_t pm_qos_power_read(struct file *filp, char __user *buf, |
| 383 | size_t count, loff_t *f_pos) |
| 384 | { |
| 385 | s32 value; |
| 386 | unsigned long flags; |
| 387 | struct pm_qos_object *o; |
| 388 | struct pm_qos_request_list *pm_qos_req = filp->private_data;; |
| 389 | |
| 390 | if (!pm_qos_req) |
| 391 | return -EINVAL; |
| 392 | if (!pm_qos_request_active(pm_qos_req)) |
| 393 | return -EINVAL; |
| 394 | |
| 395 | o = pm_qos_array[pm_qos_req->pm_qos_class]; |
| 396 | spin_lock_irqsave(&pm_qos_lock, flags); |
| 397 | value = pm_qos_get_value(o); |
| 398 | spin_unlock_irqrestore(&pm_qos_lock, flags); |
| 399 | |
| 400 | return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32)); |
| 401 | } |
| 402 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 403 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, |
| 404 | size_t count, loff_t *f_pos) |
| 405 | { |
| 406 | s32 value; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 407 | int x; |
| 408 | char ascii_value[11]; |
| 409 | struct pm_qos_request_list *pm_qos_req; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 410 | |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 411 | if (count == sizeof(s32)) { |
| 412 | if (copy_from_user(&value, buf, sizeof(s32))) |
| 413 | return -EFAULT; |
| 414 | } else if (count == 11) { /* len('0x12345678/0') */ |
| 415 | if (copy_from_user(ascii_value, buf, 11)) |
| 416 | return -EFAULT; |
mark gross | 0109c2c | 2010-09-09 23:20:09 +0200 | [diff] [blame] | 417 | if (strlen(ascii_value) != 10) |
| 418 | return -EINVAL; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 419 | x = sscanf(ascii_value, "%x", &value); |
| 420 | if (x != 1) |
| 421 | return -EINVAL; |
mark gross | 0109c2c | 2010-09-09 23:20:09 +0200 | [diff] [blame] | 422 | pr_debug("%s, %d, 0x%x\n", ascii_value, x, value); |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 423 | } else |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 424 | return -EINVAL; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 425 | |
Joe Perches | 99a5179 | 2010-09-04 18:52:50 -0700 | [diff] [blame] | 426 | pm_qos_req = filp->private_data; |
Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 427 | pm_qos_update_request(pm_qos_req, value); |
| 428 | |
| 429 | return count; |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | |
| 433 | static int __init pm_qos_power_init(void) |
| 434 | { |
| 435 | int ret = 0; |
| 436 | |
| 437 | ret = register_pm_qos_misc(&cpu_dma_pm_qos); |
| 438 | if (ret < 0) { |
| 439 | printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n"); |
| 440 | return ret; |
| 441 | } |
| 442 | ret = register_pm_qos_misc(&network_lat_pm_qos); |
| 443 | if (ret < 0) { |
| 444 | printk(KERN_ERR "pm_qos_param: network_latency setup failed\n"); |
| 445 | return ret; |
| 446 | } |
| 447 | ret = register_pm_qos_misc(&network_throughput_pm_qos); |
| 448 | if (ret < 0) |
| 449 | printk(KERN_ERR |
| 450 | "pm_qos_param: network_throughput setup failed\n"); |
| 451 | |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | late_initcall(pm_qos_power_init); |