wl1251: fix ELP_CTRL register accesses when using SDIO
[pandora-wifi.git] / compat / pm_qos_params.c
1 #include <net/compat.h>
2
3 /* This is the backport of pm-qos params for kernels <= 2.6.25 */
4 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
5
6 /*
7  * This module exposes the interface to kernel space for specifying
8  * QoS dependencies.  It provides infrastructure for registration of:
9  *
10  * Dependents on a QoS value : register requirements
11  * Watchers of QoS value : get notified when target QoS value changes
12  *
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.
15  *
16  * There are 3 basic classes of QoS parameter: latency, timeout, throughput
17  * each have defined units:
18  * latency: usec
19  * timeout: usec <-- currently not used.
20  * throughput: kbs (kilo byte / sec)
21  *
22  * There are lists of pm_qos_objects each one wrapping requirements, notifiers
23  *
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.
31  *
32  * Mark Gross <mgross@linux.intel.com>
33  */
34
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>
40 #include <linux/fs.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>
46
47 #include <linux/uaccess.h>
48
49 /*
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
53  */
54 struct requirement_list {
55         struct list_head list;
56         union {
57                 s32 value;
58                 s32 usec;
59                 s32 kbps;
60         };
61         char *name;
62 };
63
64 static s32 max_compare(s32 v1, s32 v2);
65 static s32 min_compare(s32 v1, s32 v2);
66
67 struct pm_qos_object {
68         struct requirement_list requirements;
69         struct blocking_notifier_head *notifiers;
70         struct miscdevice pm_qos_power_miscdev;
71         char *name;
72         s32 default_value;
73         atomic_t target_value;
74         s32 (*comparitor)(s32, s32);
75 };
76
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
86 };
87
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
96 };
97
98
99 static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
100 static struct pm_qos_object network_throughput_pm_qos = {
101         .requirements =
102                 {LIST_HEAD_INIT(network_throughput_pm_qos.requirements.list)},
103         .notifiers = &network_throughput_notifier,
104         .name = "network_throughput",
105         .default_value = 0,
106         .target_value = ATOMIC_INIT(0),
107         .comparitor = max_compare
108 };
109
110 static BLOCKING_NOTIFIER_HEAD(system_bus_freq_notifier);
111 static struct pm_qos_object system_bus_freq_pm_qos = {
112         .requirements =
113                 {LIST_HEAD_INIT(system_bus_freq_pm_qos.requirements.list)},
114         .notifiers = &system_bus_freq_notifier,
115         .name = "system_bus_freq",
116         .default_value = 0,
117         .target_value = ATOMIC_INIT(0),
118         .comparitor = max_compare
119 };
120
121
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,
128 };
129
130 static DEFINE_SPINLOCK(pm_qos_lock);
131
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);
136
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,
141 };
142
143 /* static helper functions */
144 static s32 max_compare(s32 v1, s32 v2)
145 {
146         return max(v1, v2);
147 }
148
149 static s32 min_compare(s32 v1, s32 v2)
150 {
151         return min(v1, v2);
152 }
153
154
155 static void update_target(int target)
156 {
157         s32 extreme_value;
158         struct requirement_list *node;
159         unsigned long flags;
160         int call_notifier = 0;
161
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);
168         }
169         if (atomic_read(&pm_qos_array[target]->target_value) != extreme_value) {
170                 call_notifier = 1;
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));
174         }
175         spin_unlock_irqrestore(&pm_qos_lock, flags);
176
177         if (call_notifier)
178                 blocking_notifier_call_chain(pm_qos_array[target]->notifiers,
179                         (unsigned long) extreme_value, NULL);
180 }
181
182 static int register_pm_qos_misc(struct pm_qos_object *qos)
183 {
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;
187
188         return misc_register(&qos->pm_qos_power_miscdev);
189 }
190
191 static int find_pm_qos_object_by_minor(int minor)
192 {
193         int pm_qos_class;
194
195         for (pm_qos_class = 0;
196                 pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
197                 if (minor ==
198                         pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
199                         return pm_qos_class;
200         }
201         return -1;
202 }
203
204 /**
205  * pm_qos_requirement - returns current system wide qos expectation
206  * @pm_qos_class: identification of which qos value is requested
207  *
208  * This function returns the current target value in an atomic manner.
209  */
210 int pm_qos_requirement(int pm_qos_class)
211 {
212         return atomic_read(&pm_qos_array[pm_qos_class]->target_value);
213 }
214 EXPORT_SYMBOL_GPL(pm_qos_requirement);
215
216 /**
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
221  *
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.
225  */
226 int pm_qos_add_requirement(int pm_qos_class, char *name, s32 value)
227 {
228         struct requirement_list *dep;
229         unsigned long flags;
230
231         dep = kzalloc(sizeof(struct requirement_list), GFP_KERNEL);
232         if (dep) {
233                 if (value == PM_QOS_DEFAULT_VALUE)
234                         dep->value = pm_qos_array[pm_qos_class]->default_value;
235                 else
236                         dep->value = value;
237                 dep->name = kstrdup(name, GFP_KERNEL);
238                 if (!dep->name)
239                         goto cleanup;
240
241                 spin_lock_irqsave(&pm_qos_lock, flags);
242                 list_add(&dep->list,
243                         &pm_qos_array[pm_qos_class]->requirements.list);
244                 spin_unlock_irqrestore(&pm_qos_lock, flags);
245                 update_target(pm_qos_class);
246
247                 return 0;
248         }
249
250 cleanup:
251         kfree(dep);
252         return -ENOMEM;
253 }
254 EXPORT_SYMBOL_GPL(pm_qos_add_requirement);
255
256 /**
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
261  *
262  * Updates an existing qos requirement for the pm_qos_class of parameters along
263  * with updating the target pm_qos_class value.
264  *
265  * If the named request isn't in the list then no change is made.
266  */
267 int pm_qos_update_requirement(int pm_qos_class, char *name, s32 new_value)
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                         if (new_value == PM_QOS_DEFAULT_VALUE)
278                                 node->value =
279                                 pm_qos_array[pm_qos_class]->default_value;
280                         else
281                                 node->value = new_value;
282                         pending_update = 1;
283                         break;
284                 }
285         }
286         spin_unlock_irqrestore(&pm_qos_lock, flags);
287         if (pending_update)
288                 update_target(pm_qos_class);
289
290         return 0;
291 }
292 EXPORT_SYMBOL_GPL(pm_qos_update_requirement);
293
294 /**
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
298  *
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.
301  */
302 void pm_qos_remove_requirement(int pm_qos_class, char *name)
303 {
304         unsigned long flags;
305         struct requirement_list *node;
306         int pending_update = 0;
307
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) {
312                         kfree(node->name);
313                         list_del(&node->list);
314                         kfree(node);
315                         pending_update = 1;
316                         break;
317                 }
318         }
319         spin_unlock_irqrestore(&pm_qos_lock, flags);
320         if (pending_update)
321                 update_target(pm_qos_class);
322 }
323 EXPORT_SYMBOL_GPL(pm_qos_remove_requirement);
324
325 /**
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.
329  *
330  * will register the notifier into a notification chain that gets called
331  * upon changes to the pm_qos_class target value.
332  */
333 int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
334 {
335         int retval;
336
337         retval = blocking_notifier_chain_register(
338                         pm_qos_array[pm_qos_class]->notifiers, notifier);
339
340         return retval;
341 }
342 EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
343
344 /**
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.
348  *
349  * will remove the notifier from the notification chain that gets called
350  * upon changes to the pm_qos_class target value.
351  */
352 int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
353 {
354         int retval;
355
356         retval = blocking_notifier_chain_unregister(
357                         pm_qos_array[pm_qos_class]->notifiers, notifier);
358
359         return retval;
360 }
361 EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
362
363 #define PID_NAME_LEN 32
364
365 static int pm_qos_power_open(struct inode *inode, struct file *filp)
366 {
367         int ret;
368         long pm_qos_class;
369         char name[PID_NAME_LEN];
370
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);
377                 if (ret >= 0)
378                         return 0;
379         }
380         return -EPERM;
381 }
382
383 static int pm_qos_power_release(struct inode *inode, struct file *filp)
384 {
385         int pm_qos_class;
386         char name[PID_NAME_LEN];
387
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);
391
392         return 0;
393 }
394
395 static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
396                 size_t count, loff_t *f_pos)
397 {
398         s32 value;
399         int pm_qos_class;
400         char name[PID_NAME_LEN];
401
402         pm_qos_class = (long)filp->private_data;
403         if (count != sizeof(s32))
404                 return -EINVAL;
405         if (copy_from_user(&value, buf, sizeof(s32)))
406                 return -EFAULT;
407         snprintf(name, PID_NAME_LEN, "process_%d", current->pid);
408         pm_qos_update_requirement(pm_qos_class, name, value);
409
410         return  sizeof(s32);
411 }
412
413
414 /*
415  * This initializes pm-qos for older kernels.
416  */
417 int compat_pm_qos_power_init(void)
418 {
419         int ret = 0;
420
421         ret = register_pm_qos_misc(&cpu_dma_pm_qos);
422         if (ret < 0) {
423                 printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
424                 return ret;
425         }
426         ret = register_pm_qos_misc(&network_lat_pm_qos);
427         if (ret < 0) {
428                 printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
429                 return ret;
430         }
431         ret = register_pm_qos_misc(&network_throughput_pm_qos);
432         if (ret < 0) {
433                 printk(KERN_ERR
434                         "pm_qos_param: network_throughput setup failed\n");
435                 return ret;
436         }
437         ret = register_pm_qos_misc(&system_bus_freq_pm_qos);
438         if (ret < 0)
439                 printk(KERN_ERR
440                         "pm_qos_param: system_bus_freq setup failed\n");
441
442         return ret;
443 }
444
445 int compat_pm_qos_power_deinit(void)
446 {
447         int ret = 0;
448
449         ret = misc_deregister(&cpu_dma_pm_qos.pm_qos_power_miscdev);
450         if (ret < 0) {
451                 printk(KERN_ERR "pm_qos_param: cpu_dma_latency deinit failed\n");
452                 return ret;
453         }
454
455         ret = misc_deregister(&network_lat_pm_qos.pm_qos_power_miscdev);
456         if (ret < 0) {
457                 printk(KERN_ERR "pm_qos_param: network_latency deinit failed\n");
458                 return ret;
459         }
460
461         ret = misc_deregister(&network_throughput_pm_qos.pm_qos_power_miscdev);
462         if (ret < 0) {
463                 printk(KERN_ERR
464                         "pm_qos_param: network_throughput deinit failed\n");
465                 return ret;
466         }
467
468         ret = misc_deregister(&system_bus_freq_pm_qos.pm_qos_power_miscdev);
469         if (ret < 0) {
470                 printk(KERN_ERR
471                         "pm_qos_param: system_bus_freq deinit failed\n");
472                 return ret;
473         }
474
475         return ret;
476 }
477 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) */