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