Merge git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / kernel / pm_qos_params.c
blob3db49b9ca374ded32bf224f266e904a8006e61a8
1 /*
2 * This module exposes the interface to kernel space for specifying
3 * QoS dependencies. It provides infrastructure for registration of:
5 * Dependents on a QoS value : register requirements
6 * Watchers of QoS value : get notified when target QoS value changes
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.
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)
17 * There are lists of pm_qos_objects each one wrapping requirements, notifiers
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.
27 * Mark Gross <mgross@linux.intel.com>
30 #include <linux/pm_qos_params.h>
31 #include <linux/sched.h>
32 #include <linux/spinlock.h>
33 #include <linux/slab.h>
34 #include <linux/time.h>
35 #include <linux/fs.h>
36 #include <linux/device.h>
37 #include <linux/miscdevice.h>
38 #include <linux/string.h>
39 #include <linux/platform_device.h>
40 #include <linux/init.h>
42 #include <linux/uaccess.h>
45 * locking rule: all changes to requirements or notifiers lists
46 * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
47 * held, taken with _irqsave. One lock to rule them all
49 struct requirement_list {
50 struct list_head list;
51 union {
52 s32 value;
53 s32 usec;
54 s32 kbps;
56 char *name;
59 static s32 max_compare(s32 v1, s32 v2);
60 static s32 min_compare(s32 v1, s32 v2);
62 struct pm_qos_object {
63 struct requirement_list requirements;
64 struct blocking_notifier_head *notifiers;
65 struct miscdevice pm_qos_power_miscdev;
66 char *name;
67 s32 default_value;
68 atomic_t target_value;
69 s32 (*comparitor)(s32, s32);
72 static struct pm_qos_object null_pm_qos;
73 static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
74 static struct pm_qos_object cpu_dma_pm_qos = {
75 .requirements = {LIST_HEAD_INIT(cpu_dma_pm_qos.requirements.list)},
76 .notifiers = &cpu_dma_lat_notifier,
77 .name = "cpu_dma_latency",
78 .default_value = 2000 * USEC_PER_SEC,
79 .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC),
80 .comparitor = min_compare
83 static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
84 static struct pm_qos_object network_lat_pm_qos = {
85 .requirements = {LIST_HEAD_INIT(network_lat_pm_qos.requirements.list)},
86 .notifiers = &network_lat_notifier,
87 .name = "network_latency",
88 .default_value = 2000 * USEC_PER_SEC,
89 .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC),
90 .comparitor = min_compare
94 static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
95 static struct pm_qos_object network_throughput_pm_qos = {
96 .requirements =
97 {LIST_HEAD_INIT(network_throughput_pm_qos.requirements.list)},
98 .notifiers = &network_throughput_notifier,
99 .name = "network_throughput",
100 .default_value = 0,
101 .target_value = ATOMIC_INIT(0),
102 .comparitor = max_compare
106 static struct pm_qos_object *pm_qos_array[] = {
107 &null_pm_qos,
108 &cpu_dma_pm_qos,
109 &network_lat_pm_qos,
110 &network_throughput_pm_qos
113 static DEFINE_SPINLOCK(pm_qos_lock);
115 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
116 size_t count, loff_t *f_pos);
117 static int pm_qos_power_open(struct inode *inode, struct file *filp);
118 static int pm_qos_power_release(struct inode *inode, struct file *filp);
120 static const struct file_operations pm_qos_power_fops = {
121 .write = pm_qos_power_write,
122 .open = pm_qos_power_open,
123 .release = pm_qos_power_release,
126 /* static helper functions */
127 static s32 max_compare(s32 v1, s32 v2)
129 return max(v1, v2);
132 static s32 min_compare(s32 v1, s32 v2)
134 return min(v1, v2);
138 static void update_target(int target)
140 s32 extreme_value;
141 struct requirement_list *node;
142 unsigned long flags;
143 int call_notifier = 0;
145 spin_lock_irqsave(&pm_qos_lock, flags);
146 extreme_value = pm_qos_array[target]->default_value;
147 list_for_each_entry(node,
148 &pm_qos_array[target]->requirements.list, list) {
149 extreme_value = pm_qos_array[target]->comparitor(
150 extreme_value, node->value);
152 if (atomic_read(&pm_qos_array[target]->target_value) != extreme_value) {
153 call_notifier = 1;
154 atomic_set(&pm_qos_array[target]->target_value, extreme_value);
155 pr_debug(KERN_ERR "new target for qos %d is %d\n", target,
156 atomic_read(&pm_qos_array[target]->target_value));
158 spin_unlock_irqrestore(&pm_qos_lock, flags);
160 if (call_notifier)
161 blocking_notifier_call_chain(pm_qos_array[target]->notifiers,
162 (unsigned long) extreme_value, NULL);
165 static int register_pm_qos_misc(struct pm_qos_object *qos)
167 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
168 qos->pm_qos_power_miscdev.name = qos->name;
169 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
171 return misc_register(&qos->pm_qos_power_miscdev);
174 static int find_pm_qos_object_by_minor(int minor)
176 int pm_qos_class;
178 for (pm_qos_class = 0;
179 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
180 if (minor ==
181 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
182 return pm_qos_class;
184 return -1;
188 * pm_qos_requirement - returns current system wide qos expectation
189 * @pm_qos_class: identification of which qos value is requested
191 * This function returns the current target value in an atomic manner.
193 int pm_qos_requirement(int pm_qos_class)
195 return atomic_read(&pm_qos_array[pm_qos_class]->target_value);
197 EXPORT_SYMBOL_GPL(pm_qos_requirement);
200 * pm_qos_add_requirement - inserts new qos request into the list
201 * @pm_qos_class: identifies which list of qos request to us
202 * @name: identifies the request
203 * @value: defines the qos request
205 * This function inserts a new entry in the pm_qos_class list of requested qos
206 * performance characteristics. It recomputes the aggregate QoS expectations
207 * for the pm_qos_class of parameters.
209 int pm_qos_add_requirement(int pm_qos_class, char *name, s32 value)
211 struct requirement_list *dep;
212 unsigned long flags;
214 dep = kzalloc(sizeof(struct requirement_list), GFP_KERNEL);
215 if (dep) {
216 if (value == PM_QOS_DEFAULT_VALUE)
217 dep->value = pm_qos_array[pm_qos_class]->default_value;
218 else
219 dep->value = value;
220 dep->name = kstrdup(name, GFP_KERNEL);
221 if (!dep->name)
222 goto cleanup;
224 spin_lock_irqsave(&pm_qos_lock, flags);
225 list_add(&dep->list,
226 &pm_qos_array[pm_qos_class]->requirements.list);
227 spin_unlock_irqrestore(&pm_qos_lock, flags);
228 update_target(pm_qos_class);
230 return 0;
233 cleanup:
234 kfree(dep);
235 return -ENOMEM;
237 EXPORT_SYMBOL_GPL(pm_qos_add_requirement);
240 * pm_qos_update_requirement - modifies an existing qos request
241 * @pm_qos_class: identifies which list of qos request to us
242 * @name: identifies the request
243 * @value: defines the qos request
245 * Updates an existing qos requirement for the pm_qos_class of parameters along
246 * with updating the target pm_qos_class value.
248 * If the named request isn't in the list then no change is made.
250 int pm_qos_update_requirement(int pm_qos_class, char *name, s32 new_value)
252 unsigned long flags;
253 struct requirement_list *node;
254 int pending_update = 0;
256 spin_lock_irqsave(&pm_qos_lock, flags);
257 list_for_each_entry(node,
258 &pm_qos_array[pm_qos_class]->requirements.list, list) {
259 if (strcmp(node->name, name) == 0) {
260 if (new_value == PM_QOS_DEFAULT_VALUE)
261 node->value =
262 pm_qos_array[pm_qos_class]->default_value;
263 else
264 node->value = new_value;
265 pending_update = 1;
266 break;
269 spin_unlock_irqrestore(&pm_qos_lock, flags);
270 if (pending_update)
271 update_target(pm_qos_class);
273 return 0;
275 EXPORT_SYMBOL_GPL(pm_qos_update_requirement);
278 * pm_qos_remove_requirement - modifies an existing qos request
279 * @pm_qos_class: identifies which list of qos request to us
280 * @name: identifies the request
282 * Will remove named qos request from pm_qos_class list of parameters and
283 * recompute the current target value for the pm_qos_class.
285 void pm_qos_remove_requirement(int pm_qos_class, char *name)
287 unsigned long flags;
288 struct requirement_list *node;
289 int pending_update = 0;
291 spin_lock_irqsave(&pm_qos_lock, flags);
292 list_for_each_entry(node,
293 &pm_qos_array[pm_qos_class]->requirements.list, list) {
294 if (strcmp(node->name, name) == 0) {
295 kfree(node->name);
296 list_del(&node->list);
297 kfree(node);
298 pending_update = 1;
299 break;
302 spin_unlock_irqrestore(&pm_qos_lock, flags);
303 if (pending_update)
304 update_target(pm_qos_class);
306 EXPORT_SYMBOL_GPL(pm_qos_remove_requirement);
309 * pm_qos_add_notifier - sets notification entry for changes to target value
310 * @pm_qos_class: identifies which qos target changes should be notified.
311 * @notifier: notifier block managed by caller.
313 * will register the notifier into a notification chain that gets called
314 * upon changes to the pm_qos_class target value.
316 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
318 int retval;
320 retval = blocking_notifier_chain_register(
321 pm_qos_array[pm_qos_class]->notifiers, notifier);
323 return retval;
325 EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
328 * pm_qos_remove_notifier - deletes notification entry from chain.
329 * @pm_qos_class: identifies which qos target changes are notified.
330 * @notifier: notifier block to be removed.
332 * will remove the notifier from the notification chain that gets called
333 * upon changes to the pm_qos_class target value.
335 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
337 int retval;
339 retval = blocking_notifier_chain_unregister(
340 pm_qos_array[pm_qos_class]->notifiers, notifier);
342 return retval;
344 EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
346 #define PID_NAME_LEN 32
348 static int pm_qos_power_open(struct inode *inode, struct file *filp)
350 int ret;
351 long pm_qos_class;
352 char name[PID_NAME_LEN];
354 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
355 if (pm_qos_class >= 0) {
356 filp->private_data = (void *)pm_qos_class;
357 snprintf(name, PID_NAME_LEN, "process_%d", current->pid);
358 ret = pm_qos_add_requirement(pm_qos_class, name,
359 PM_QOS_DEFAULT_VALUE);
360 if (ret >= 0)
361 return 0;
363 return -EPERM;
366 static int pm_qos_power_release(struct inode *inode, struct file *filp)
368 int pm_qos_class;
369 char name[PID_NAME_LEN];
371 pm_qos_class = (long)filp->private_data;
372 snprintf(name, PID_NAME_LEN, "process_%d", current->pid);
373 pm_qos_remove_requirement(pm_qos_class, name);
375 return 0;
378 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
379 size_t count, loff_t *f_pos)
381 s32 value;
382 int pm_qos_class;
383 char name[PID_NAME_LEN];
385 pm_qos_class = (long)filp->private_data;
386 if (count != sizeof(s32))
387 return -EINVAL;
388 if (copy_from_user(&value, buf, sizeof(s32)))
389 return -EFAULT;
390 snprintf(name, PID_NAME_LEN, "process_%d", current->pid);
391 pm_qos_update_requirement(pm_qos_class, name, value);
393 return sizeof(s32);
397 static int __init pm_qos_power_init(void)
399 int ret = 0;
401 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
402 if (ret < 0) {
403 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
404 return ret;
406 ret = register_pm_qos_misc(&network_lat_pm_qos);
407 if (ret < 0) {
408 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
409 return ret;
411 ret = register_pm_qos_misc(&network_throughput_pm_qos);
412 if (ret < 0)
413 printk(KERN_ERR
414 "pm_qos_param: network_throughput setup failed\n");
416 return ret;
419 late_initcall(pm_qos_power_init);