1 #include <net/compat.h>
3 /* This is the backport of pm-qos params for kernels <= 2.6.25 */
4 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
7 * This module exposes the interface to kernel space for specifying
8 * QoS dependencies. It provides infrastructure for registration of:
10 * Dependents on a QoS value : register requirements
11 * Watchers of QoS value : get notified when target QoS value changes
13 * This QoS design is best effort based. Dependents register their QoS needs.
14 * Watchers register to keep track of the current QoS needs of the system.
16 * There are 3 basic classes of QoS parameter: latency, timeout, throughput
17 * each have defined units:
19 * timeout: usec <-- currently not used.
20 * throughput: kbs (kilo byte / sec)
22 * There are lists of pm_qos_objects each one wrapping requirements, notifiers
24 * User mode requirements on a QOS parameter register themselves to the
25 * subsystem by opening the device node /dev/... and writing there request to
26 * the node. As long as the process holds a file handle open to the node the
27 * client continues to be accounted for. Upon file release the usermode
28 * requirement is removed and a new qos target is computed. This way when the
29 * requirement that the application has is cleaned up when closes the file
30 * pointer or exits the pm_qos_object will get an opportunity to clean up.
32 * Mark Gross <mgross@linux.intel.com>
35 #include <linux/pm_qos_params.h>
36 #include <linux/sched.h>
37 #include <linux/spinlock.h>
38 #include <linux/slab.h>
39 #include <linux/time.h>
41 #include <linux/device.h>
42 #include <linux/miscdevice.h>
43 #include <linux/string.h>
44 #include <linux/platform_device.h>
45 #include <linux/init.h>
47 #include <linux/uaccess.h>
50 * locking rule: all changes to requirements or notifiers lists
51 * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
52 * held, taken with _irqsave. One lock to rule them all
54 struct requirement_list {
55 struct list_head list;
64 static s32 max_compare(s32 v1, s32 v2);
65 static s32 min_compare(s32 v1, s32 v2);
67 struct pm_qos_object {
68 struct requirement_list requirements;
69 struct blocking_notifier_head *notifiers;
70 struct miscdevice pm_qos_power_miscdev;
73 atomic_t target_value;
74 s32 (*comparitor)(s32, s32);
77 static struct pm_qos_object null_pm_qos;
78 static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
79 static struct pm_qos_object cpu_dma_pm_qos = {
80 .requirements = {LIST_HEAD_INIT(cpu_dma_pm_qos.requirements.list)},
81 .notifiers = &cpu_dma_lat_notifier,
82 .name = "cpu_dma_latency",
83 .default_value = 2000 * USEC_PER_SEC,
84 .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC),
85 .comparitor = min_compare
88 static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
89 static struct pm_qos_object network_lat_pm_qos = {
90 .requirements = {LIST_HEAD_INIT(network_lat_pm_qos.requirements.list)},
91 .notifiers = &network_lat_notifier,
92 .name = "network_latency",
93 .default_value = 2000 * USEC_PER_SEC,
94 .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC),
95 .comparitor = min_compare
99 static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
100 static struct pm_qos_object network_throughput_pm_qos = {
102 {LIST_HEAD_INIT(network_throughput_pm_qos.requirements.list)},
103 .notifiers = &network_throughput_notifier,
104 .name = "network_throughput",
106 .target_value = ATOMIC_INIT(0),
107 .comparitor = max_compare
110 static BLOCKING_NOTIFIER_HEAD(system_bus_freq_notifier);
111 static struct pm_qos_object system_bus_freq_pm_qos = {
113 {LIST_HEAD_INIT(system_bus_freq_pm_qos.requirements.list)},
114 .notifiers = &system_bus_freq_notifier,
115 .name = "system_bus_freq",
117 .target_value = ATOMIC_INIT(0),
118 .comparitor = max_compare
122 static struct pm_qos_object *pm_qos_array[PM_QOS_NUM_CLASSES] = {
123 [PM_QOS_RESERVED] = &null_pm_qos,
124 [PM_QOS_CPU_DMA_LATENCY] = &cpu_dma_pm_qos,
125 [PM_QOS_NETWORK_LATENCY] = &network_lat_pm_qos,
126 [PM_QOS_NETWORK_THROUGHPUT] = &network_throughput_pm_qos,
127 [PM_QOS_SYSTEM_BUS_FREQ] = &system_bus_freq_pm_qos,
130 static DEFINE_SPINLOCK(pm_qos_lock);
132 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
133 size_t count, loff_t *f_pos);
134 static int pm_qos_power_open(struct inode *inode, struct file *filp);
135 static int pm_qos_power_release(struct inode *inode, struct file *filp);
137 static const struct file_operations pm_qos_power_fops = {
138 .write = pm_qos_power_write,
139 .open = pm_qos_power_open,
140 .release = pm_qos_power_release,
143 /* static helper functions */
144 static s32 max_compare(s32 v1, s32 v2)
149 static s32 min_compare(s32 v1, s32 v2)
155 static void update_target(int target)
158 struct requirement_list *node;
160 int call_notifier = 0;
162 spin_lock_irqsave(&pm_qos_lock, flags);
163 extreme_value = pm_qos_array[target]->default_value;
164 list_for_each_entry(node,
165 &pm_qos_array[target]->requirements.list, list) {
166 extreme_value = pm_qos_array[target]->comparitor(
167 extreme_value, node->value);
169 if (atomic_read(&pm_qos_array[target]->target_value) != extreme_value) {
171 atomic_set(&pm_qos_array[target]->target_value, extreme_value);
172 pr_debug(KERN_ERR "new target for qos %d is %d\n", target,
173 atomic_read(&pm_qos_array[target]->target_value));
175 spin_unlock_irqrestore(&pm_qos_lock, flags);
178 blocking_notifier_call_chain(pm_qos_array[target]->notifiers,
179 (unsigned long) extreme_value, NULL);
182 static int register_pm_qos_misc(struct pm_qos_object *qos)
184 qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
185 qos->pm_qos_power_miscdev.name = qos->name;
186 qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
188 return misc_register(&qos->pm_qos_power_miscdev);
191 static int find_pm_qos_object_by_minor(int minor)
195 for (pm_qos_class = 0;
196 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
198 pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
205 * pm_qos_requirement - returns current system wide qos expectation
206 * @pm_qos_class: identification of which qos value is requested
208 * This function returns the current target value in an atomic manner.
210 int pm_qos_requirement(int pm_qos_class)
212 return atomic_read(&pm_qos_array[pm_qos_class]->target_value);
214 EXPORT_SYMBOL_GPL(pm_qos_requirement);
217 * pm_qos_add_requirement - inserts new qos request into the list
218 * @pm_qos_class: identifies which list of qos request to us
219 * @name: identifies the request
220 * @value: defines the qos request
222 * This function inserts a new entry in the pm_qos_class list of requested qos
223 * performance characteristics. It recomputes the aggregate QoS expectations
224 * for the pm_qos_class of parameters.
226 int pm_qos_add_requirement(int pm_qos_class, char *name, s32 value)
228 struct requirement_list *dep;
231 dep = kzalloc(sizeof(struct requirement_list), GFP_KERNEL);
233 if (value == PM_QOS_DEFAULT_VALUE)
234 dep->value = pm_qos_array[pm_qos_class]->default_value;
237 dep->name = kstrdup(name, GFP_KERNEL);
241 spin_lock_irqsave(&pm_qos_lock, flags);
243 &pm_qos_array[pm_qos_class]->requirements.list);
244 spin_unlock_irqrestore(&pm_qos_lock, flags);
245 update_target(pm_qos_class);
254 EXPORT_SYMBOL_GPL(pm_qos_add_requirement);
257 * pm_qos_update_requirement - modifies an existing qos request
258 * @pm_qos_class: identifies which list of qos request to us
259 * @name: identifies the request
260 * @value: defines the qos request
262 * Updates an existing qos requirement for the pm_qos_class of parameters along
263 * with updating the target pm_qos_class value.
265 * If the named request isn't in the list then no change is made.
267 int pm_qos_update_requirement(int pm_qos_class, char *name, s32 new_value)
270 struct requirement_list *node;
271 int pending_update = 0;
273 spin_lock_irqsave(&pm_qos_lock, flags);
274 list_for_each_entry(node,
275 &pm_qos_array[pm_qos_class]->requirements.list, list) {
276 if (strcmp(node->name, name) == 0) {
277 if (new_value == PM_QOS_DEFAULT_VALUE)
279 pm_qos_array[pm_qos_class]->default_value;
281 node->value = new_value;
286 spin_unlock_irqrestore(&pm_qos_lock, flags);
288 update_target(pm_qos_class);
292 EXPORT_SYMBOL_GPL(pm_qos_update_requirement);
295 * pm_qos_remove_requirement - modifies an existing qos request
296 * @pm_qos_class: identifies which list of qos request to us
297 * @name: identifies the request
299 * Will remove named qos request from pm_qos_class list of parameters and
300 * recompute the current target value for the pm_qos_class.
302 void pm_qos_remove_requirement(int pm_qos_class, char *name)
305 struct requirement_list *node;
306 int pending_update = 0;
308 spin_lock_irqsave(&pm_qos_lock, flags);
309 list_for_each_entry(node,
310 &pm_qos_array[pm_qos_class]->requirements.list, list) {
311 if (strcmp(node->name, name) == 0) {
313 list_del(&node->list);
319 spin_unlock_irqrestore(&pm_qos_lock, flags);
321 update_target(pm_qos_class);
323 EXPORT_SYMBOL_GPL(pm_qos_remove_requirement);
326 * pm_qos_add_notifier - sets notification entry for changes to target value
327 * @pm_qos_class: identifies which qos target changes should be notified.
328 * @notifier: notifier block managed by caller.
330 * will register the notifier into a notification chain that gets called
331 * upon changes to the pm_qos_class target value.
333 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
337 retval = blocking_notifier_chain_register(
338 pm_qos_array[pm_qos_class]->notifiers, notifier);
342 EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
345 * pm_qos_remove_notifier - deletes notification entry from chain.
346 * @pm_qos_class: identifies which qos target changes are notified.
347 * @notifier: notifier block to be removed.
349 * will remove the notifier from the notification chain that gets called
350 * upon changes to the pm_qos_class target value.
352 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
356 retval = blocking_notifier_chain_unregister(
357 pm_qos_array[pm_qos_class]->notifiers, notifier);
361 EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
363 #define PID_NAME_LEN 32
365 static int pm_qos_power_open(struct inode *inode, struct file *filp)
369 char name[PID_NAME_LEN];
371 pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
372 if (pm_qos_class >= 0) {
373 filp->private_data = (void *)pm_qos_class;
374 snprintf(name, PID_NAME_LEN, "process_%d", current->pid);
375 ret = pm_qos_add_requirement(pm_qos_class, name,
376 PM_QOS_DEFAULT_VALUE);
383 static int pm_qos_power_release(struct inode *inode, struct file *filp)
386 char name[PID_NAME_LEN];
388 pm_qos_class = (long)filp->private_data;
389 snprintf(name, PID_NAME_LEN, "process_%d", current->pid);
390 pm_qos_remove_requirement(pm_qos_class, name);
395 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
396 size_t count, loff_t *f_pos)
400 char name[PID_NAME_LEN];
402 pm_qos_class = (long)filp->private_data;
403 if (count != sizeof(s32))
405 if (copy_from_user(&value, buf, sizeof(s32)))
407 snprintf(name, PID_NAME_LEN, "process_%d", current->pid);
408 pm_qos_update_requirement(pm_qos_class, name, value);
415 * This initializes pm-qos for older kernels.
417 int compat_pm_qos_power_init(void)
421 ret = register_pm_qos_misc(&cpu_dma_pm_qos);
423 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
426 ret = register_pm_qos_misc(&network_lat_pm_qos);
428 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
431 ret = register_pm_qos_misc(&network_throughput_pm_qos);
434 "pm_qos_param: network_throughput setup failed\n");
437 ret = register_pm_qos_misc(&system_bus_freq_pm_qos);
440 "pm_qos_param: system_bus_freq setup failed\n");
445 int compat_pm_qos_power_deinit(void)
449 ret = misc_deregister(&cpu_dma_pm_qos.pm_qos_power_miscdev);
451 printk(KERN_ERR "pm_qos_param: cpu_dma_latency deinit failed\n");
455 ret = misc_deregister(&network_lat_pm_qos.pm_qos_power_miscdev);
457 printk(KERN_ERR "pm_qos_param: network_latency deinit failed\n");
461 ret = misc_deregister(&network_throughput_pm_qos.pm_qos_power_miscdev);
464 "pm_qos_param: network_throughput deinit failed\n");
468 ret = misc_deregister(&system_bus_freq_pm_qos.pm_qos_power_miscdev);
471 "pm_qos_param: system_bus_freq deinit failed\n");
477 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) */