| 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> | 
| 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 | /* | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 48 | * locking rule: all changes to requests 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 { | 
| James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 63 | struct plist_head requests; | 
| 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 = { | 
| Dima Zavin | 0226f8a | 2011-07-07 17:27:59 -0700 | [diff] [blame] | 77 | .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests), | 
| 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 = { | 
| Dima Zavin | 0226f8a | 2011-07-07 17:27:59 -0700 | [diff] [blame] | 87 | .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests), | 
| 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 = { | 
| Dima Zavin | 0226f8a | 2011-07-07 17:27:59 -0700 | [diff] [blame] | 98 | .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests), | 
| 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 | { | 
| James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 132 | if (plist_head_empty(&o->requests)) | 
|  | 133 | return o->default_value; | 
|  | 134 |  | 
|  | 135 | switch (o->type) { | 
|  | 136 | case PM_QOS_MIN: | 
| Colin Cross | 00fafcd | 2010-11-15 22:45:22 +0100 | [diff] [blame] | 137 | return plist_first(&o->requests)->prio; | 
| James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 138 |  | 
|  | 139 | case PM_QOS_MAX: | 
| Colin Cross | 00fafcd | 2010-11-15 22:45:22 +0100 | [diff] [blame] | 140 | return plist_last(&o->requests)->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 | */ | 
|  | 173 | plist_del(node, &o->requests); | 
|  | 174 | plist_node_init(node, value); | 
|  | 175 | plist_add(node, &o->requests); | 
|  | 176 | } else if (del) { | 
|  | 177 | plist_del(node, &o->requests); | 
|  | 178 | } else { | 
|  | 179 | plist_add(node, &o->requests); | 
| 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 |  | 
|  | 191 | static int register_pm_qos_misc(struct pm_qos_object *qos) | 
|  | 192 | { | 
|  | 193 | qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR; | 
|  | 194 | qos->pm_qos_power_miscdev.name = qos->name; | 
|  | 195 | qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops; | 
|  | 196 |  | 
|  | 197 | return misc_register(&qos->pm_qos_power_miscdev); | 
|  | 198 | } | 
|  | 199 |  | 
|  | 200 | static int find_pm_qos_object_by_minor(int minor) | 
|  | 201 | { | 
|  | 202 | int pm_qos_class; | 
|  | 203 |  | 
|  | 204 | for (pm_qos_class = 0; | 
|  | 205 | pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) { | 
|  | 206 | if (minor == | 
|  | 207 | pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor) | 
|  | 208 | return pm_qos_class; | 
|  | 209 | } | 
|  | 210 | return -1; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | /** | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 214 | * pm_qos_request - returns current system wide qos expectation | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 215 | * @pm_qos_class: identification of which qos value is requested | 
|  | 216 | * | 
| Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 217 | * This function returns the current target value. | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 218 | */ | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 219 | int pm_qos_request(int pm_qos_class) | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 220 | { | 
| Tim Chen | 333c5ae | 2011-02-11 12:49:04 -0800 | [diff] [blame] | 221 | return pm_qos_read_value(pm_qos_array[pm_qos_class]); | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 222 | } | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 223 | EXPORT_SYMBOL_GPL(pm_qos_request); | 
| 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 | int pm_qos_request_active(struct pm_qos_request_list *req) | 
|  | 226 | { | 
|  | 227 | return req->pm_qos_class != 0; | 
|  | 228 | } | 
|  | 229 | EXPORT_SYMBOL_GPL(pm_qos_request_active); | 
|  | 230 |  | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 231 | /** | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 232 | * pm_qos_add_request - inserts new qos request into the list | 
| Saravana Kannan | 25cc69e | 2010-08-26 20:18:43 +0200 | [diff] [blame] | 233 | * @dep: pointer to a preallocated handle | 
|  | 234 | * @pm_qos_class: identifies which list of qos request to use | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 235 | * @value: defines the qos request | 
|  | 236 | * | 
|  | 237 | * 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] | 238 | * performance characteristics.  It recomputes the aggregate QoS expectations | 
| Saravana Kannan | 25cc69e | 2010-08-26 20:18:43 +0200 | [diff] [blame] | 239 | * for the pm_qos_class of parameters and initializes the pm_qos_request_list | 
|  | 240 | * handle.  Caller needs to save this handle for later use in updates and | 
|  | 241 | * removal. | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 242 | */ | 
| Saravana Kannan | 25cc69e | 2010-08-26 20:18:43 +0200 | [diff] [blame] | 243 |  | 
| James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 244 | void pm_qos_add_request(struct pm_qos_request_list *dep, | 
|  | 245 | int pm_qos_class, s32 value) | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 246 | { | 
| James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 247 | struct pm_qos_object *o =  pm_qos_array[pm_qos_class]; | 
|  | 248 | int new_value; | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 249 |  | 
| James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 250 | if (pm_qos_request_active(dep)) { | 
|  | 251 | WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n"); | 
|  | 252 | return; | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 253 | } | 
| James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 254 | if (value == PM_QOS_DEFAULT_VALUE) | 
|  | 255 | new_value = o->default_value; | 
|  | 256 | else | 
|  | 257 | new_value = value; | 
|  | 258 | plist_node_init(&dep->list, new_value); | 
|  | 259 | dep->pm_qos_class = pm_qos_class; | 
|  | 260 | update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE); | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 261 | } | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 262 | EXPORT_SYMBOL_GPL(pm_qos_add_request); | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 263 |  | 
|  | 264 | /** | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 265 | * pm_qos_update_request - modifies an existing qos request | 
|  | 266 | * @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] | 267 | * @value: defines the qos request | 
|  | 268 | * | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 269 | * 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] | 270 | * with updating the target pm_qos_class value. | 
|  | 271 | * | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 272 | * Attempts are made to make this code callable on hot code paths. | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 273 | */ | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 274 | 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] | 275 | s32 new_value) | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 276 | { | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 277 | s32 temp; | 
| James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 278 | struct pm_qos_object *o; | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 279 |  | 
| James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 280 | if (!pm_qos_req) /*guard against callers passing in null */ | 
|  | 281 | return; | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 282 |  | 
| James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 283 | if (!pm_qos_request_active(pm_qos_req)) { | 
|  | 284 | WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n"); | 
|  | 285 | return; | 
|  | 286 | } | 
|  | 287 |  | 
| James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 288 | o = pm_qos_array[pm_qos_req->pm_qos_class]; | 
|  | 289 |  | 
|  | 290 | if (new_value == PM_QOS_DEFAULT_VALUE) | 
|  | 291 | temp = o->default_value; | 
|  | 292 | else | 
|  | 293 | temp = new_value; | 
|  | 294 |  | 
|  | 295 | if (temp != pm_qos_req->list.prio) | 
|  | 296 | update_target(o, &pm_qos_req->list, 0, temp); | 
| 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 | EXPORT_SYMBOL_GPL(pm_qos_update_request); | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 299 |  | 
|  | 300 | /** | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 301 | * pm_qos_remove_request - modifies an existing qos request | 
|  | 302 | * @pm_qos_req: handle to request list element | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 303 | * | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 304 | * Will remove pm qos request from the list of requests and | 
|  | 305 | * recompute the current target value for the pm_qos_class.  Call this | 
|  | 306 | * on slow code paths. | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 307 | */ | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 308 | 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] | 309 | { | 
| James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 310 | struct pm_qos_object *o; | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 311 |  | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 312 | if (pm_qos_req == NULL) | 
|  | 313 | return; | 
|  | 314 | /* silent return to keep pcm code cleaner */ | 
|  | 315 |  | 
| James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 316 | if (!pm_qos_request_active(pm_qos_req)) { | 
|  | 317 | WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n"); | 
|  | 318 | return; | 
|  | 319 | } | 
|  | 320 |  | 
| James Bottomley | 5f27984 | 2010-07-19 02:00:18 +0200 | [diff] [blame] | 321 | o = pm_qos_array[pm_qos_req->pm_qos_class]; | 
|  | 322 | update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE); | 
| James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 323 | memset(pm_qos_req, 0, sizeof(*pm_qos_req)); | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 324 | } | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 325 | EXPORT_SYMBOL_GPL(pm_qos_remove_request); | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 326 |  | 
|  | 327 | /** | 
|  | 328 | * pm_qos_add_notifier - sets notification entry for changes to target value | 
|  | 329 | * @pm_qos_class: identifies which qos target changes should be notified. | 
|  | 330 | * @notifier: notifier block managed by caller. | 
|  | 331 | * | 
|  | 332 | * will register the notifier into a notification chain that gets called | 
| Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 333 | * upon changes to the pm_qos_class target value. | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 334 | */ | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 335 | 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] | 336 | { | 
|  | 337 | int retval; | 
|  | 338 |  | 
|  | 339 | retval = blocking_notifier_chain_register( | 
|  | 340 | pm_qos_array[pm_qos_class]->notifiers, notifier); | 
|  | 341 |  | 
|  | 342 | return retval; | 
|  | 343 | } | 
|  | 344 | EXPORT_SYMBOL_GPL(pm_qos_add_notifier); | 
|  | 345 |  | 
|  | 346 | /** | 
|  | 347 | * pm_qos_remove_notifier - deletes notification entry from chain. | 
|  | 348 | * @pm_qos_class: identifies which qos target changes are notified. | 
|  | 349 | * @notifier: notifier block to be removed. | 
|  | 350 | * | 
|  | 351 | * will remove the notifier from the notification chain that gets called | 
| Richard Hughes | bf1db69 | 2008-08-05 13:01:35 -0700 | [diff] [blame] | 352 | * upon changes to the pm_qos_class target value. | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 353 | */ | 
|  | 354 | int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier) | 
|  | 355 | { | 
|  | 356 | int retval; | 
|  | 357 |  | 
|  | 358 | retval = blocking_notifier_chain_unregister( | 
|  | 359 | pm_qos_array[pm_qos_class]->notifiers, notifier); | 
|  | 360 |  | 
|  | 361 | return retval; | 
|  | 362 | } | 
|  | 363 | EXPORT_SYMBOL_GPL(pm_qos_remove_notifier); | 
|  | 364 |  | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 365 | static int pm_qos_power_open(struct inode *inode, struct file *filp) | 
|  | 366 | { | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 367 | long pm_qos_class; | 
|  | 368 |  | 
|  | 369 | pm_qos_class = find_pm_qos_object_by_minor(iminor(inode)); | 
|  | 370 | if (pm_qos_class >= 0) { | 
| David Alan Gilbert | bac1e74 | 2010-08-24 20:22:18 +0200 | [diff] [blame] | 371 | struct pm_qos_request_list *req = kzalloc(sizeof(*req), GFP_KERNEL); | 
| James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 372 | if (!req) | 
|  | 373 | return -ENOMEM; | 
|  | 374 |  | 
|  | 375 | pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE); | 
|  | 376 | filp->private_data = req; | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 377 |  | 
|  | 378 | if (filp->private_data) | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 379 | return 0; | 
|  | 380 | } | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 381 | return -EPERM; | 
|  | 382 | } | 
|  | 383 |  | 
|  | 384 | static int pm_qos_power_release(struct inode *inode, struct file *filp) | 
|  | 385 | { | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 386 | struct pm_qos_request_list *req; | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 387 |  | 
| James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 388 | req = filp->private_data; | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 389 | pm_qos_remove_request(req); | 
| James Bottomley | 82f6825 | 2010-07-05 22:53:06 +0200 | [diff] [blame] | 390 | kfree(req); | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 391 |  | 
|  | 392 | return 0; | 
|  | 393 | } | 
|  | 394 |  | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 395 |  | 
| Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 396 | static ssize_t pm_qos_power_read(struct file *filp, char __user *buf, | 
|  | 397 | size_t count, loff_t *f_pos) | 
|  | 398 | { | 
|  | 399 | s32 value; | 
|  | 400 | unsigned long flags; | 
|  | 401 | struct pm_qos_object *o; | 
| Justin P. Mattock | 6eab04a | 2011-04-08 19:49:08 -0700 | [diff] [blame] | 402 | struct pm_qos_request_list *pm_qos_req = filp->private_data; | 
| Thomas Renninger | f9b9e80 | 2011-02-28 22:06:34 +0100 | [diff] [blame] | 403 |  | 
|  | 404 | if (!pm_qos_req) | 
|  | 405 | return -EINVAL; | 
|  | 406 | if (!pm_qos_request_active(pm_qos_req)) | 
|  | 407 | return -EINVAL; | 
|  | 408 |  | 
|  | 409 | o = pm_qos_array[pm_qos_req->pm_qos_class]; | 
|  | 410 | spin_lock_irqsave(&pm_qos_lock, flags); | 
|  | 411 | value = pm_qos_get_value(o); | 
|  | 412 | spin_unlock_irqrestore(&pm_qos_lock, flags); | 
|  | 413 |  | 
|  | 414 | return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32)); | 
|  | 415 | } | 
|  | 416 |  | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 417 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, | 
|  | 418 | size_t count, loff_t *f_pos) | 
|  | 419 | { | 
|  | 420 | s32 value; | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 421 | struct pm_qos_request_list *pm_qos_req; | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 422 |  | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 423 | if (count == sizeof(s32)) { | 
|  | 424 | if (copy_from_user(&value, buf, sizeof(s32))) | 
|  | 425 | return -EFAULT; | 
| Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 426 | } else if (count <= 11) { /* ASCII perhaps? */ | 
|  | 427 | char ascii_value[11]; | 
|  | 428 | unsigned long int ulval; | 
|  | 429 | int ret; | 
|  | 430 |  | 
|  | 431 | if (copy_from_user(ascii_value, buf, count)) | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 432 | return -EFAULT; | 
| Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 433 |  | 
|  | 434 | if (count > 10) { | 
|  | 435 | if (ascii_value[10] == '\n') | 
|  | 436 | ascii_value[10] = '\0'; | 
|  | 437 | else | 
|  | 438 | return -EINVAL; | 
|  | 439 | } else { | 
|  | 440 | ascii_value[count] = '\0'; | 
|  | 441 | } | 
|  | 442 | ret = strict_strtoul(ascii_value, 16, &ulval); | 
|  | 443 | if (ret) { | 
|  | 444 | pr_debug("%s, 0x%lx, 0x%x\n", ascii_value, ulval, ret); | 
| mark gross | 0109c2c | 2010-09-09 23:20:09 +0200 | [diff] [blame] | 445 | return -EINVAL; | 
| Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 446 | } | 
|  | 447 | value = (s32)lower_32_bits(ulval); | 
|  | 448 | } else { | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 449 | return -EINVAL; | 
| Rafael J. Wysocki | 0775a60 | 2011-05-27 00:05:23 +0200 | [diff] [blame] | 450 | } | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 451 |  | 
| Joe Perches | 99a5179 | 2010-09-04 18:52:50 -0700 | [diff] [blame] | 452 | pm_qos_req = filp->private_data; | 
| Mark Gross | ed77134 | 2010-05-06 01:59:26 +0200 | [diff] [blame] | 453 | pm_qos_update_request(pm_qos_req, value); | 
|  | 454 |  | 
|  | 455 | return count; | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 456 | } | 
|  | 457 |  | 
|  | 458 |  | 
|  | 459 | static int __init pm_qos_power_init(void) | 
|  | 460 | { | 
|  | 461 | int ret = 0; | 
|  | 462 |  | 
|  | 463 | ret = register_pm_qos_misc(&cpu_dma_pm_qos); | 
|  | 464 | if (ret < 0) { | 
|  | 465 | printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n"); | 
|  | 466 | return ret; | 
|  | 467 | } | 
|  | 468 | ret = register_pm_qos_misc(&network_lat_pm_qos); | 
|  | 469 | if (ret < 0) { | 
|  | 470 | printk(KERN_ERR "pm_qos_param: network_latency setup failed\n"); | 
|  | 471 | return ret; | 
|  | 472 | } | 
|  | 473 | ret = register_pm_qos_misc(&network_throughput_pm_qos); | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 474 | if (ret < 0) { | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 475 | printk(KERN_ERR | 
|  | 476 | "pm_qos_param: network_throughput setup failed\n"); | 
| Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 477 | return 0; | 
|  | 478 | } | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 479 |  | 
|  | 480 | return ret; | 
|  | 481 | } | 
|  | 482 |  | 
|  | 483 | late_initcall(pm_qos_power_init); |