| 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 | * | 
|  | 5 | * Dependents on a QoS value : register requirements | 
|  | 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 | * | 
|  | 17 | * There are lists of pm_qos_objects each one wrapping requirements, notifiers | 
|  | 18 | * | 
|  | 19 | * User mode requirements on a QOS parameter register themselves to the | 
|  | 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 | 
|  | 23 | * requirement is removed and a new qos target is computed.  This way when the | 
|  | 24 | * requirement that the application has is cleaned up when closes the file | 
|  | 25 | * pointer or exits the pm_qos_object will get an opportunity to clean up. | 
|  | 26 | * | 
|  | 27 | * mark gross mgross@linux.intel.com | 
|  | 28 | */ | 
|  | 29 |  | 
|  | 30 | #include <linux/pm_qos_params.h> | 
|  | 31 | #include <linux/sched.h> | 
| Arnd Bergmann | db26e64 | 2008-05-20 19:16:33 +0200 | [diff] [blame] | 32 | #include <linux/smp_lock.h> | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 33 | #include <linux/spinlock.h> | 
|  | 34 | #include <linux/slab.h> | 
|  | 35 | #include <linux/time.h> | 
|  | 36 | #include <linux/fs.h> | 
|  | 37 | #include <linux/device.h> | 
|  | 38 | #include <linux/miscdevice.h> | 
|  | 39 | #include <linux/string.h> | 
|  | 40 | #include <linux/platform_device.h> | 
|  | 41 | #include <linux/init.h> | 
|  | 42 |  | 
|  | 43 | #include <linux/uaccess.h> | 
|  | 44 |  | 
|  | 45 | /* | 
|  | 46 | * locking rule: all changes to target_value or requirements or notifiers lists | 
|  | 47 | * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock | 
|  | 48 | * held, taken with _irqsave.  One lock to rule them all | 
|  | 49 | */ | 
|  | 50 | struct requirement_list { | 
|  | 51 | struct list_head list; | 
|  | 52 | union { | 
|  | 53 | s32 value; | 
|  | 54 | s32 usec; | 
|  | 55 | s32 kbps; | 
|  | 56 | }; | 
|  | 57 | char *name; | 
|  | 58 | }; | 
|  | 59 |  | 
|  | 60 | static s32 max_compare(s32 v1, s32 v2); | 
|  | 61 | static s32 min_compare(s32 v1, s32 v2); | 
|  | 62 |  | 
|  | 63 | struct pm_qos_object { | 
|  | 64 | struct requirement_list requirements; | 
|  | 65 | struct blocking_notifier_head *notifiers; | 
|  | 66 | struct miscdevice pm_qos_power_miscdev; | 
|  | 67 | char *name; | 
|  | 68 | s32 default_value; | 
|  | 69 | s32 target_value; | 
|  | 70 | s32 (*comparitor)(s32, s32); | 
|  | 71 | }; | 
|  | 72 |  | 
|  | 73 | static struct pm_qos_object null_pm_qos; | 
|  | 74 | static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier); | 
|  | 75 | static struct pm_qos_object cpu_dma_pm_qos = { | 
|  | 76 | .requirements = {LIST_HEAD_INIT(cpu_dma_pm_qos.requirements.list)}, | 
|  | 77 | .notifiers = &cpu_dma_lat_notifier, | 
|  | 78 | .name = "cpu_dma_latency", | 
|  | 79 | .default_value = 2000 * USEC_PER_SEC, | 
|  | 80 | .target_value = 2000 * USEC_PER_SEC, | 
|  | 81 | .comparitor = min_compare | 
|  | 82 | }; | 
|  | 83 |  | 
|  | 84 | static BLOCKING_NOTIFIER_HEAD(network_lat_notifier); | 
|  | 85 | static struct pm_qos_object network_lat_pm_qos = { | 
|  | 86 | .requirements = {LIST_HEAD_INIT(network_lat_pm_qos.requirements.list)}, | 
|  | 87 | .notifiers = &network_lat_notifier, | 
|  | 88 | .name = "network_latency", | 
|  | 89 | .default_value = 2000 * USEC_PER_SEC, | 
|  | 90 | .target_value = 2000 * USEC_PER_SEC, | 
|  | 91 | .comparitor = min_compare | 
|  | 92 | }; | 
|  | 93 |  | 
|  | 94 |  | 
|  | 95 | static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier); | 
|  | 96 | static struct pm_qos_object network_throughput_pm_qos = { | 
|  | 97 | .requirements = | 
|  | 98 | {LIST_HEAD_INIT(network_throughput_pm_qos.requirements.list)}, | 
|  | 99 | .notifiers = &network_throughput_notifier, | 
|  | 100 | .name = "network_throughput", | 
|  | 101 | .default_value = 0, | 
|  | 102 | .target_value = 0, | 
|  | 103 | .comparitor = max_compare | 
|  | 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 |  | 
|  | 114 | static DEFINE_SPINLOCK(pm_qos_lock); | 
|  | 115 |  | 
|  | 116 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, | 
|  | 117 | size_t count, loff_t *f_pos); | 
|  | 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, | 
|  | 123 | .open = pm_qos_power_open, | 
|  | 124 | .release = pm_qos_power_release, | 
|  | 125 | }; | 
|  | 126 |  | 
|  | 127 | /* static helper functions */ | 
|  | 128 | static s32 max_compare(s32 v1, s32 v2) | 
|  | 129 | { | 
|  | 130 | return max(v1, v2); | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | static s32 min_compare(s32 v1, s32 v2) | 
|  | 134 | { | 
|  | 135 | return min(v1, v2); | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 |  | 
|  | 139 | static void update_target(int target) | 
|  | 140 | { | 
|  | 141 | s32 extreme_value; | 
|  | 142 | struct requirement_list *node; | 
|  | 143 | unsigned long flags; | 
|  | 144 | int call_notifier = 0; | 
|  | 145 |  | 
|  | 146 | spin_lock_irqsave(&pm_qos_lock, flags); | 
|  | 147 | extreme_value = pm_qos_array[target]->default_value; | 
|  | 148 | list_for_each_entry(node, | 
|  | 149 | &pm_qos_array[target]->requirements.list, list) { | 
|  | 150 | extreme_value = pm_qos_array[target]->comparitor( | 
|  | 151 | extreme_value, node->value); | 
|  | 152 | } | 
|  | 153 | if (pm_qos_array[target]->target_value != extreme_value) { | 
|  | 154 | call_notifier = 1; | 
|  | 155 | pm_qos_array[target]->target_value = extreme_value; | 
|  | 156 | pr_debug(KERN_ERR "new target for qos %d is %d\n", target, | 
|  | 157 | pm_qos_array[target]->target_value); | 
|  | 158 | } | 
|  | 159 | spin_unlock_irqrestore(&pm_qos_lock, flags); | 
|  | 160 |  | 
|  | 161 | if (call_notifier) | 
|  | 162 | blocking_notifier_call_chain(pm_qos_array[target]->notifiers, | 
|  | 163 | (unsigned long) extreme_value, NULL); | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | static int register_pm_qos_misc(struct pm_qos_object *qos) | 
|  | 167 | { | 
|  | 168 | qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR; | 
|  | 169 | qos->pm_qos_power_miscdev.name = qos->name; | 
|  | 170 | qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops; | 
|  | 171 |  | 
|  | 172 | return misc_register(&qos->pm_qos_power_miscdev); | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | static int find_pm_qos_object_by_minor(int minor) | 
|  | 176 | { | 
|  | 177 | int pm_qos_class; | 
|  | 178 |  | 
|  | 179 | for (pm_qos_class = 0; | 
|  | 180 | pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) { | 
|  | 181 | if (minor == | 
|  | 182 | pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor) | 
|  | 183 | return pm_qos_class; | 
|  | 184 | } | 
|  | 185 | return -1; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | /** | 
|  | 189 | * pm_qos_requirement - returns current system wide qos expectation | 
|  | 190 | * @pm_qos_class: identification of which qos value is requested | 
|  | 191 | * | 
|  | 192 | * This function returns the current target value in an atomic manner. | 
|  | 193 | */ | 
|  | 194 | int pm_qos_requirement(int pm_qos_class) | 
|  | 195 | { | 
|  | 196 | int ret_val; | 
|  | 197 | unsigned long flags; | 
|  | 198 |  | 
|  | 199 | spin_lock_irqsave(&pm_qos_lock, flags); | 
|  | 200 | ret_val = pm_qos_array[pm_qos_class]->target_value; | 
|  | 201 | spin_unlock_irqrestore(&pm_qos_lock, flags); | 
|  | 202 |  | 
|  | 203 | return ret_val; | 
|  | 204 | } | 
|  | 205 | EXPORT_SYMBOL_GPL(pm_qos_requirement); | 
|  | 206 |  | 
|  | 207 | /** | 
|  | 208 | * pm_qos_add_requirement - inserts new qos request into the list | 
|  | 209 | * @pm_qos_class: identifies which list of qos request to us | 
|  | 210 | * @name: identifies the request | 
|  | 211 | * @value: defines the qos request | 
|  | 212 | * | 
|  | 213 | * This function inserts a new entry in the pm_qos_class list of requested qos | 
|  | 214 | * performance charactoistics.  It recomputes the agregate QoS expectations for | 
|  | 215 | * the pm_qos_class of parrameters. | 
|  | 216 | */ | 
|  | 217 | int pm_qos_add_requirement(int pm_qos_class, char *name, s32 value) | 
|  | 218 | { | 
|  | 219 | struct requirement_list *dep; | 
|  | 220 | unsigned long flags; | 
|  | 221 |  | 
|  | 222 | dep = kzalloc(sizeof(struct requirement_list), GFP_KERNEL); | 
|  | 223 | if (dep) { | 
|  | 224 | if (value == PM_QOS_DEFAULT_VALUE) | 
|  | 225 | dep->value = pm_qos_array[pm_qos_class]->default_value; | 
|  | 226 | else | 
|  | 227 | dep->value = value; | 
|  | 228 | dep->name = kstrdup(name, GFP_KERNEL); | 
|  | 229 | if (!dep->name) | 
|  | 230 | goto cleanup; | 
|  | 231 |  | 
|  | 232 | spin_lock_irqsave(&pm_qos_lock, flags); | 
|  | 233 | list_add(&dep->list, | 
|  | 234 | &pm_qos_array[pm_qos_class]->requirements.list); | 
|  | 235 | spin_unlock_irqrestore(&pm_qos_lock, flags); | 
|  | 236 | update_target(pm_qos_class); | 
|  | 237 |  | 
|  | 238 | return 0; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | cleanup: | 
|  | 242 | kfree(dep); | 
|  | 243 | return -ENOMEM; | 
|  | 244 | } | 
|  | 245 | EXPORT_SYMBOL_GPL(pm_qos_add_requirement); | 
|  | 246 |  | 
|  | 247 | /** | 
|  | 248 | * pm_qos_update_requirement - modifies an existing qos request | 
|  | 249 | * @pm_qos_class: identifies which list of qos request to us | 
|  | 250 | * @name: identifies the request | 
|  | 251 | * @value: defines the qos request | 
|  | 252 | * | 
|  | 253 | * Updates an existing qos requierement for the pm_qos_class of parameters along | 
|  | 254 | * with updating the target pm_qos_class value. | 
|  | 255 | * | 
|  | 256 | * If the named request isn't in the lest then no change is made. | 
|  | 257 | */ | 
|  | 258 | int pm_qos_update_requirement(int pm_qos_class, char *name, s32 new_value) | 
|  | 259 | { | 
|  | 260 | unsigned long flags; | 
|  | 261 | struct requirement_list *node; | 
|  | 262 | int pending_update = 0; | 
|  | 263 |  | 
|  | 264 | spin_lock_irqsave(&pm_qos_lock, flags); | 
|  | 265 | list_for_each_entry(node, | 
|  | 266 | &pm_qos_array[pm_qos_class]->requirements.list, list) { | 
|  | 267 | if (strcmp(node->name, name) == 0) { | 
|  | 268 | if (new_value == PM_QOS_DEFAULT_VALUE) | 
|  | 269 | node->value = | 
|  | 270 | pm_qos_array[pm_qos_class]->default_value; | 
|  | 271 | else | 
|  | 272 | node->value = new_value; | 
|  | 273 | pending_update = 1; | 
|  | 274 | break; | 
|  | 275 | } | 
|  | 276 | } | 
|  | 277 | spin_unlock_irqrestore(&pm_qos_lock, flags); | 
|  | 278 | if (pending_update) | 
|  | 279 | update_target(pm_qos_class); | 
|  | 280 |  | 
|  | 281 | return 0; | 
|  | 282 | } | 
|  | 283 | EXPORT_SYMBOL_GPL(pm_qos_update_requirement); | 
|  | 284 |  | 
|  | 285 | /** | 
|  | 286 | * pm_qos_remove_requirement - modifies an existing qos request | 
|  | 287 | * @pm_qos_class: identifies which list of qos request to us | 
|  | 288 | * @name: identifies the request | 
|  | 289 | * | 
|  | 290 | * Will remove named qos request from pm_qos_class list of parrameters and | 
|  | 291 | * recompute the current target value for the pm_qos_class. | 
|  | 292 | */ | 
|  | 293 | void pm_qos_remove_requirement(int pm_qos_class, char *name) | 
|  | 294 | { | 
|  | 295 | unsigned long flags; | 
|  | 296 | struct requirement_list *node; | 
|  | 297 | int pending_update = 0; | 
|  | 298 |  | 
|  | 299 | spin_lock_irqsave(&pm_qos_lock, flags); | 
|  | 300 | list_for_each_entry(node, | 
|  | 301 | &pm_qos_array[pm_qos_class]->requirements.list, list) { | 
|  | 302 | if (strcmp(node->name, name) == 0) { | 
|  | 303 | kfree(node->name); | 
|  | 304 | list_del(&node->list); | 
|  | 305 | kfree(node); | 
|  | 306 | pending_update = 1; | 
|  | 307 | break; | 
|  | 308 | } | 
|  | 309 | } | 
|  | 310 | spin_unlock_irqrestore(&pm_qos_lock, flags); | 
|  | 311 | if (pending_update) | 
|  | 312 | update_target(pm_qos_class); | 
|  | 313 | } | 
|  | 314 | EXPORT_SYMBOL_GPL(pm_qos_remove_requirement); | 
|  | 315 |  | 
|  | 316 | /** | 
|  | 317 | * pm_qos_add_notifier - sets notification entry for changes to target value | 
|  | 318 | * @pm_qos_class: identifies which qos target changes should be notified. | 
|  | 319 | * @notifier: notifier block managed by caller. | 
|  | 320 | * | 
|  | 321 | * will register the notifier into a notification chain that gets called | 
|  | 322 | * uppon changes to the pm_qos_class target value. | 
|  | 323 | */ | 
|  | 324 | int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier) | 
|  | 325 | { | 
|  | 326 | int retval; | 
|  | 327 |  | 
|  | 328 | retval = blocking_notifier_chain_register( | 
|  | 329 | pm_qos_array[pm_qos_class]->notifiers, notifier); | 
|  | 330 |  | 
|  | 331 | return retval; | 
|  | 332 | } | 
|  | 333 | EXPORT_SYMBOL_GPL(pm_qos_add_notifier); | 
|  | 334 |  | 
|  | 335 | /** | 
|  | 336 | * pm_qos_remove_notifier - deletes notification entry from chain. | 
|  | 337 | * @pm_qos_class: identifies which qos target changes are notified. | 
|  | 338 | * @notifier: notifier block to be removed. | 
|  | 339 | * | 
|  | 340 | * will remove the notifier from the notification chain that gets called | 
|  | 341 | * uppon changes to the pm_qos_class target value. | 
|  | 342 | */ | 
|  | 343 | int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier) | 
|  | 344 | { | 
|  | 345 | int retval; | 
|  | 346 |  | 
|  | 347 | retval = blocking_notifier_chain_unregister( | 
|  | 348 | pm_qos_array[pm_qos_class]->notifiers, notifier); | 
|  | 349 |  | 
|  | 350 | return retval; | 
|  | 351 | } | 
|  | 352 | EXPORT_SYMBOL_GPL(pm_qos_remove_notifier); | 
|  | 353 |  | 
|  | 354 | #define PID_NAME_LEN sizeof("process_1234567890") | 
|  | 355 | static char name[PID_NAME_LEN]; | 
|  | 356 |  | 
|  | 357 | static int pm_qos_power_open(struct inode *inode, struct file *filp) | 
|  | 358 | { | 
|  | 359 | int ret; | 
|  | 360 | long pm_qos_class; | 
|  | 361 |  | 
| Arnd Bergmann | db26e64 | 2008-05-20 19:16:33 +0200 | [diff] [blame] | 362 | lock_kernel(); | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 363 | pm_qos_class = find_pm_qos_object_by_minor(iminor(inode)); | 
|  | 364 | if (pm_qos_class >= 0) { | 
|  | 365 | filp->private_data = (void *)pm_qos_class; | 
|  | 366 | sprintf(name, "process_%d", current->pid); | 
|  | 367 | ret = pm_qos_add_requirement(pm_qos_class, name, | 
|  | 368 | PM_QOS_DEFAULT_VALUE); | 
| Arnd Bergmann | db26e64 | 2008-05-20 19:16:33 +0200 | [diff] [blame] | 369 | if (ret >= 0) { | 
|  | 370 | unlock_kernel(); | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 371 | return 0; | 
| Arnd Bergmann | db26e64 | 2008-05-20 19:16:33 +0200 | [diff] [blame] | 372 | } | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 373 | } | 
| Arnd Bergmann | db26e64 | 2008-05-20 19:16:33 +0200 | [diff] [blame] | 374 | unlock_kernel(); | 
| Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 375 |  | 
|  | 376 | return -EPERM; | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | static int pm_qos_power_release(struct inode *inode, struct file *filp) | 
|  | 380 | { | 
|  | 381 | int pm_qos_class; | 
|  | 382 |  | 
|  | 383 | pm_qos_class = (long)filp->private_data; | 
|  | 384 | sprintf(name, "process_%d", current->pid); | 
|  | 385 | pm_qos_remove_requirement(pm_qos_class, name); | 
|  | 386 |  | 
|  | 387 | return 0; | 
|  | 388 | } | 
|  | 389 |  | 
|  | 390 | static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf, | 
|  | 391 | size_t count, loff_t *f_pos) | 
|  | 392 | { | 
|  | 393 | s32 value; | 
|  | 394 | int pm_qos_class; | 
|  | 395 |  | 
|  | 396 | pm_qos_class = (long)filp->private_data; | 
|  | 397 | if (count != sizeof(s32)) | 
|  | 398 | return -EINVAL; | 
|  | 399 | if (copy_from_user(&value, buf, sizeof(s32))) | 
|  | 400 | return -EFAULT; | 
|  | 401 | sprintf(name, "process_%d", current->pid); | 
|  | 402 | pm_qos_update_requirement(pm_qos_class, name, value); | 
|  | 403 |  | 
|  | 404 | return  sizeof(s32); | 
|  | 405 | } | 
|  | 406 |  | 
|  | 407 |  | 
|  | 408 | static int __init pm_qos_power_init(void) | 
|  | 409 | { | 
|  | 410 | int ret = 0; | 
|  | 411 |  | 
|  | 412 | ret = register_pm_qos_misc(&cpu_dma_pm_qos); | 
|  | 413 | if (ret < 0) { | 
|  | 414 | printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n"); | 
|  | 415 | return ret; | 
|  | 416 | } | 
|  | 417 | ret = register_pm_qos_misc(&network_lat_pm_qos); | 
|  | 418 | if (ret < 0) { | 
|  | 419 | printk(KERN_ERR "pm_qos_param: network_latency setup failed\n"); | 
|  | 420 | return ret; | 
|  | 421 | } | 
|  | 422 | ret = register_pm_qos_misc(&network_throughput_pm_qos); | 
|  | 423 | if (ret < 0) | 
|  | 424 | printk(KERN_ERR | 
|  | 425 | "pm_qos_param: network_throughput setup failed\n"); | 
|  | 426 |  | 
|  | 427 | return ret; | 
|  | 428 | } | 
|  | 429 |  | 
|  | 430 | late_initcall(pm_qos_power_init); |