f2b73f078eef6103e36fce6c6ad9ed497c5c5597
[linux-flexiantxendom0-3.2.10.git] / drivers / cpufreq / cpufreq.c
1 /*
2  *  linux/drivers/cpufreq/cpufreq.c
3  *
4  *  Copyright (C) 2001 Russell King
5  *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  */
12
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/notifier.h>
18 #include <linux/cpufreq.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <linux/spinlock.h>
22 #include <linux/device.h>
23 #include <linux/slab.h>
24 #include <linux/cpu.h>
25 #include <linux/completion.h>
26
27 /**
28  * The "cpufreq driver" - the arch- or hardware-dependend low
29  * level driver of CPUFreq support, and its spinlock. This lock
30  * also protects the cpufreq_cpu_data array.
31  */
32 static struct cpufreq_driver    *cpufreq_driver;
33 static struct cpufreq_policy    *cpufreq_cpu_data[NR_CPUS];
34 static spinlock_t               cpufreq_driver_lock = SPIN_LOCK_UNLOCKED;
35
36 /* internal prototype */
37 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
38
39
40 /**
41  * Two notifier lists: the "policy" list is involved in the 
42  * validation process for a new CPU frequency policy; the 
43  * "transition" list for kernel code that needs to handle
44  * changes to devices when the CPU clock speed changes.
45  * The mutex locks both lists.
46  */
47 static struct notifier_block    *cpufreq_policy_notifier_list;
48 static struct notifier_block    *cpufreq_transition_notifier_list;
49 static DECLARE_RWSEM            (cpufreq_notifier_rwsem);
50
51
52 static LIST_HEAD(cpufreq_governor_list);
53 static DECLARE_MUTEX            (cpufreq_governor_sem);
54
55 static struct cpufreq_policy * cpufreq_cpu_get(unsigned int cpu)
56 {
57         struct cpufreq_policy *data;
58         unsigned long flags;
59
60         if (cpu >= NR_CPUS)
61                 goto err_out;
62
63         /* get the cpufreq driver */
64         spin_lock_irqsave(&cpufreq_driver_lock, flags);
65
66         if (!cpufreq_driver)
67                 goto err_out_unlock;
68
69         if (!try_module_get(cpufreq_driver->owner))
70                 goto err_out_unlock;
71
72
73         /* get the CPU */
74         data = cpufreq_cpu_data[cpu];
75
76         if (!data)
77                 goto err_out_put_module;
78
79         if (!kobject_get(&data->kobj))
80                 goto err_out_put_module;
81
82
83         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
84
85         return data;
86
87  err_out_put_module:
88         module_put(cpufreq_driver->owner);
89  err_out_unlock:
90         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
91  err_out:
92         return NULL;
93 }
94
95 static void cpufreq_cpu_put(struct cpufreq_policy *data)
96 {
97         kobject_put(&data->kobj);
98         module_put(cpufreq_driver->owner);
99 }
100
101 /*********************************************************************
102  *                          SYSFS INTERFACE                          *
103  *********************************************************************/
104
105 /**
106  * cpufreq_parse_governor - parse a governor string
107  */
108 int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
109                                 struct cpufreq_governor **governor)
110 {
111         if (!cpufreq_driver)
112                 return -EINVAL;
113         if (cpufreq_driver->setpolicy) {
114                 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
115                         *policy = CPUFREQ_POLICY_PERFORMANCE;
116                         return 0;
117                 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
118                         *policy = CPUFREQ_POLICY_POWERSAVE;
119                         return 0;
120                 }
121                 return -EINVAL;
122         } else {
123                 struct cpufreq_governor *t;
124                 down(&cpufreq_governor_sem);
125                 if (!cpufreq_driver || !cpufreq_driver->target)
126                         goto out;
127                 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
128                         if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
129                                 *governor = t;
130                                 up(&cpufreq_governor_sem);
131                                 return 0;
132                         }
133                 }
134         out:
135                 up(&cpufreq_governor_sem);
136         }
137         return -EINVAL;
138 }
139 EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
140
141
142 /* drivers/base/cpu.c */
143 extern struct sysdev_class cpu_sysdev_class;
144
145
146 /**
147  * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
148  *
149  * Write out information from cpufreq_driver->policy[cpu]; object must be
150  * "unsigned int".
151  */
152
153 #define show_one(file_name, object)                                     \
154 static ssize_t show_##file_name                                         \
155 (struct cpufreq_policy * policy, char *buf)                             \
156 {                                                                       \
157         return sprintf (buf, "%u\n", policy->object);                   \
158 }
159
160 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
161 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
162 show_one(scaling_min_freq, min);
163 show_one(scaling_max_freq, max);
164
165 /**
166  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
167  */
168 #define store_one(file_name, object)                    \
169 static ssize_t store_##file_name                                        \
170 (struct cpufreq_policy * policy, const char *buf, size_t count)         \
171 {                                                                       \
172         unsigned int ret = -EINVAL;                                     \
173         struct cpufreq_policy new_policy;                               \
174                                                                         \
175         ret = cpufreq_get_policy(&new_policy, policy->cpu);             \
176         if (ret)                                                        \
177                 return -EINVAL;                                         \
178                                                                         \
179         ret = sscanf (buf, "%u", &new_policy.object);                   \
180         if (ret != 1)                                                   \
181                 return -EINVAL;                                         \
182                                                                         \
183         ret = cpufreq_set_policy(&new_policy);                          \
184                                                                         \
185         return ret ? ret : count;                                       \
186 }
187
188 store_one(scaling_min_freq,min);
189 store_one(scaling_max_freq,max);
190
191 /**
192  * show_scaling_governor - show the current policy for the specified CPU
193  */
194 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
195 {
196         if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
197                 return sprintf(buf, "powersave\n");
198         else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
199                 return sprintf(buf, "performance\n");
200         else if (policy->governor)
201                 return snprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
202         return -EINVAL;
203 }
204
205
206 /**
207  * store_scaling_governor - store policy for the specified CPU
208  */
209 static ssize_t store_scaling_governor (struct cpufreq_policy * policy, 
210                                        const char *buf, size_t count) 
211 {
212         unsigned int ret = -EINVAL;
213         char    str_governor[16];
214         struct cpufreq_policy new_policy;
215
216         ret = cpufreq_get_policy(&new_policy, policy->cpu);
217         if (ret)
218                 return ret;
219
220         ret = sscanf (buf, "%15s", str_governor);
221         if (ret != 1)
222                 return -EINVAL;
223
224         if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
225                 return -EINVAL;
226
227         ret = cpufreq_set_policy(&new_policy);
228
229         return ret ? ret : count;
230 }
231
232 /**
233  * show_scaling_driver - show the cpufreq driver currently loaded
234  */
235 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
236 {
237         return snprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
238 }
239
240 /**
241  * show_scaling_available_governors - show the available CPUfreq governors
242  */
243 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
244                                 char *buf)
245 {
246         ssize_t i = 0;
247         struct cpufreq_governor *t;
248
249         if (!cpufreq_driver->target) {
250                 i += sprintf(buf, "performance powersave");
251                 goto out;
252         }
253
254         list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
255                 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
256                         goto out;
257                 i += snprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
258         }
259  out:
260         i += sprintf(&buf[i], "\n");
261         return i;
262 }
263
264
265 #define define_one_ro(_name) \
266 struct freq_attr _name = { \
267         .attr = { .name = __stringify(_name), .mode = 0444 }, \
268         .show = show_##_name, \
269 }
270
271 #define define_one_rw(_name) \
272 struct freq_attr _name = { \
273         .attr = { .name = __stringify(_name), .mode = 0644 }, \
274         .show = show_##_name, \
275         .store = store_##_name, \
276 }
277
278 define_one_ro(cpuinfo_min_freq);
279 define_one_ro(cpuinfo_max_freq);
280 define_one_ro(scaling_available_governors);
281 define_one_ro(scaling_driver);
282 define_one_rw(scaling_min_freq);
283 define_one_rw(scaling_max_freq);
284 define_one_rw(scaling_governor);
285
286 static struct attribute * default_attrs[] = {
287         &cpuinfo_min_freq.attr,
288         &cpuinfo_max_freq.attr,
289         &scaling_min_freq.attr,
290         &scaling_max_freq.attr,
291         &scaling_governor.attr,
292         &scaling_driver.attr,
293         &scaling_available_governors.attr,
294         NULL
295 };
296
297 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
298 #define to_attr(a) container_of(a,struct freq_attr,attr)
299
300 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
301 {
302         struct cpufreq_policy * policy = to_policy(kobj);
303         struct freq_attr * fattr = to_attr(attr);
304         ssize_t ret;
305         policy = cpufreq_cpu_get(policy->cpu);
306         if (!policy)
307                 return -EINVAL;
308         ret = fattr->show ? fattr->show(policy,buf) : 0;
309         cpufreq_cpu_put(policy);
310         return ret;
311 }
312
313 static ssize_t store(struct kobject * kobj, struct attribute * attr, 
314                      const char * buf, size_t count)
315 {
316         struct cpufreq_policy * policy = to_policy(kobj);
317         struct freq_attr * fattr = to_attr(attr);
318         ssize_t ret;
319         policy = cpufreq_cpu_get(policy->cpu);
320         if (!policy)
321                 return -EINVAL;
322         ret = fattr->store ? fattr->store(policy,buf,count) : 0;
323         cpufreq_cpu_put(policy);
324         return ret;
325 }
326
327 static void cpufreq_sysfs_release(struct kobject * kobj)
328 {
329         struct cpufreq_policy * policy = to_policy(kobj);
330         complete(&policy->kobj_unregister);
331 }
332
333 static struct sysfs_ops sysfs_ops = {
334         .show   = show,
335         .store  = store,
336 };
337
338 static struct kobj_type ktype_cpufreq = {
339         .sysfs_ops      = &sysfs_ops,
340         .default_attrs  = default_attrs,
341         .release        = cpufreq_sysfs_release,
342 };
343
344
345 /**
346  * cpufreq_add_dev - add a CPU device
347  *
348  * Adds the cpufreq interface for a CPU device. 
349  */
350 static int cpufreq_add_dev (struct sys_device * sys_dev)
351 {
352         unsigned int cpu = sys_dev->id;
353         int ret = 0;
354         struct cpufreq_policy new_policy;
355         struct cpufreq_policy *policy;
356         struct freq_attr **drv_attr;
357         unsigned long flags;
358
359         if (!try_module_get(cpufreq_driver->owner))
360                 return -EINVAL;
361
362         policy = kmalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
363         if (!policy)
364                 return -ENOMEM;
365         memset(policy, 0, sizeof(struct cpufreq_policy));
366
367         policy->cpu = cpu;
368         init_MUTEX_LOCKED(&policy->lock);
369         init_completion(&policy->kobj_unregister);
370
371         /* call driver. From then on the cpufreq must be able
372          * to accept all calls to ->verify and ->setpolicy for this CPU
373          */
374         ret = cpufreq_driver->init(policy);
375         if (ret)
376                 goto err_out;
377
378         memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
379
380         /* prepare interface data */
381         policy->kobj.parent = &sys_dev->kobj;
382         policy->kobj.ktype = &ktype_cpufreq;
383         strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
384
385         ret = kobject_register(&policy->kobj);
386         if (ret)
387                 goto err_out;
388
389         /* set up files for this cpu device */
390         drv_attr = cpufreq_driver->attr;
391         while ((drv_attr) && (*drv_attr)) {
392                 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
393                 drv_attr++;
394         }
395
396         spin_lock_irqsave(&cpufreq_driver_lock, flags);
397         cpufreq_cpu_data[cpu] = policy;
398         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
399         policy->governor = NULL; /* to assure that the starting sequence is
400                                   * run in cpufreq_set_policy */
401         up(&policy->lock);
402         
403         /* set default policy */
404         
405         ret = cpufreq_set_policy(&new_policy);
406         if (ret)
407                 goto err_out_unregister;
408
409         module_put(cpufreq_driver->owner);
410         return 0;
411
412
413  err_out_unregister:
414         spin_lock_irqsave(&cpufreq_driver_lock, flags);
415         cpufreq_cpu_data[cpu] = NULL;
416         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
417
418         kobject_unregister(&policy->kobj);
419         wait_for_completion(&policy->kobj_unregister);
420
421  err_out:
422         kfree(policy);
423         module_put(cpufreq_driver->owner);
424         return ret;
425 }
426
427
428 /**
429  * cpufreq_remove_dev - remove a CPU device
430  *
431  * Removes the cpufreq interface for a CPU device.
432  */
433 static int cpufreq_remove_dev (struct sys_device * sys_dev)
434 {
435         unsigned int cpu = sys_dev->id;
436         unsigned long flags;
437         struct cpufreq_policy *data;
438
439         spin_lock_irqsave(&cpufreq_driver_lock, flags);
440         data = cpufreq_cpu_data[cpu];
441
442         if (!data) {
443                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
444                 return -EINVAL;
445         }
446         cpufreq_cpu_data[cpu] = NULL;
447         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
448
449         if (!kobject_get(&data->kobj))
450                 return -EFAULT;
451
452         if (cpufreq_driver->target)
453                 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
454
455         kobject_unregister(&data->kobj);
456
457         kobject_put(&data->kobj);
458
459         /* we need to make sure that the underlying kobj is actually
460          * not referenced anymore by anybody before we proceed with 
461          * unloading.
462          */
463         wait_for_completion(&data->kobj_unregister);
464
465         if (cpufreq_driver->exit)
466                 cpufreq_driver->exit(data);
467
468         kfree(data);
469
470         return 0;
471 }
472
473 /**
474  *      cpufreq_resume - restore the CPU clock frequency after resume
475  *
476  *      Restore the CPU clock frequency so that our idea of the current
477  *      frequency reflects the actual hardware.
478  */
479 static int cpufreq_resume(struct sys_device * sysdev)
480 {
481         int cpu = sysdev->id;
482         unsigned int ret = 0;
483         struct cpufreq_policy *cpu_policy;
484
485         if (!cpu_online(cpu))
486                 return 0;
487
488         /* we may be lax here as interrupts are off. Nonetheless
489          * we need to grab the correct cpu policy, as to check
490          * whether we really run on this CPU.
491          */
492
493         cpu_policy = cpufreq_cpu_get(cpu);
494         if (!cpu_policy)
495                 return -EINVAL;
496
497         if (cpufreq_driver->resume)
498                 ret = cpufreq_driver->resume(cpu_policy);
499         if (ret) {
500                 printk(KERN_ERR "cpufreq: resume failed in ->resume step on CPU %u\n", cpu_policy->cpu);
501                 goto out;
502         }
503
504         if (cpufreq_driver->setpolicy)
505                 ret = cpufreq_driver->setpolicy(cpu_policy);
506         else
507         /* CPUFREQ_RELATION_H or CPUFREQ_RELATION_L have the same effect here, as cpu_policy->cur is known
508          * to be a valid and exact target frequency
509          */
510                 ret = cpufreq_driver->target(cpu_policy, cpu_policy->cur, CPUFREQ_RELATION_H);
511
512         if (ret) {
513                 printk(KERN_ERR "cpufreq: resume failed in ->setpolicy/target step on CPU %u\n", cpu_policy->cpu);
514                 goto out;
515         }
516
517  out:
518         cpufreq_cpu_put(cpu_policy);
519
520         return ret;
521 }
522
523 static struct sysdev_driver cpufreq_sysdev_driver = {
524         .add            = cpufreq_add_dev,
525         .remove         = cpufreq_remove_dev,
526         .resume         = cpufreq_resume,
527 };
528
529
530 /*********************************************************************
531  *                     NOTIFIER LISTS INTERFACE                      *
532  *********************************************************************/
533
534 /**
535  *      cpufreq_register_notifier - register a driver with cpufreq
536  *      @nb: notifier function to register
537  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
538  *
539  *      Add a driver to one of two lists: either a list of drivers that 
540  *      are notified about clock rate changes (once before and once after
541  *      the transition), or a list of drivers that are notified about
542  *      changes in cpufreq policy.
543  *
544  *      This function may sleep, and has the same return conditions as
545  *      notifier_chain_register.
546  */
547 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
548 {
549         int ret;
550
551         down_write(&cpufreq_notifier_rwsem);
552         switch (list) {
553         case CPUFREQ_TRANSITION_NOTIFIER:
554                 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
555                 break;
556         case CPUFREQ_POLICY_NOTIFIER:
557                 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
558                 break;
559         default:
560                 ret = -EINVAL;
561         }
562         up_write(&cpufreq_notifier_rwsem);
563
564         return ret;
565 }
566 EXPORT_SYMBOL(cpufreq_register_notifier);
567
568
569 /**
570  *      cpufreq_unregister_notifier - unregister a driver with cpufreq
571  *      @nb: notifier block to be unregistered
572  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
573  *
574  *      Remove a driver from the CPU frequency notifier list.
575  *
576  *      This function may sleep, and has the same return conditions as
577  *      notifier_chain_unregister.
578  */
579 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
580 {
581         int ret;
582
583         down_write(&cpufreq_notifier_rwsem);
584         switch (list) {
585         case CPUFREQ_TRANSITION_NOTIFIER:
586                 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
587                 break;
588         case CPUFREQ_POLICY_NOTIFIER:
589                 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
590                 break;
591         default:
592                 ret = -EINVAL;
593         }
594         up_write(&cpufreq_notifier_rwsem);
595
596         return ret;
597 }
598 EXPORT_SYMBOL(cpufreq_unregister_notifier);
599
600
601 /*********************************************************************
602  *                              GOVERNORS                            *
603  *********************************************************************/
604
605
606 int __cpufreq_driver_target(struct cpufreq_policy *policy,
607                             unsigned int target_freq,
608                             unsigned int relation)
609 {
610         return cpufreq_driver->target(policy, target_freq, relation);
611 }
612 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
613
614
615 int cpufreq_driver_target(struct cpufreq_policy *policy,
616                           unsigned int target_freq,
617                           unsigned int relation)
618 {
619         unsigned int ret;
620
621         policy = cpufreq_cpu_get(policy->cpu);
622         if (!policy)
623                 return -EINVAL;
624
625         down(&policy->lock);
626
627         ret = __cpufreq_driver_target(policy, target_freq, relation);
628
629         up(&policy->lock);
630
631         cpufreq_cpu_put(policy);
632
633         return ret;
634 }
635 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
636
637
638 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
639 {
640         int ret = -EINVAL;
641
642         if (!try_module_get(policy->governor->owner))
643                 return -EINVAL;
644
645         ret = policy->governor->governor(policy, event);
646
647         /* we keep one module reference alive for each CPU governed by this CPU */
648         if ((event != CPUFREQ_GOV_START) || ret)
649                 module_put(policy->governor->owner);
650         if ((event == CPUFREQ_GOV_STOP) && !ret)
651                 module_put(policy->governor->owner);
652
653         return ret;
654 }
655
656
657 int cpufreq_governor(unsigned int cpu, unsigned int event)
658 {
659         int ret = 0;
660         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
661
662         if (!policy)
663                 return -EINVAL;
664
665         down(&policy->lock);
666         ret = __cpufreq_governor(policy, event);
667         up(&policy->lock);
668
669         cpufreq_cpu_put(policy);
670
671         return ret;
672 }
673 EXPORT_SYMBOL_GPL(cpufreq_governor);
674
675
676 int cpufreq_register_governor(struct cpufreq_governor *governor)
677 {
678         struct cpufreq_governor *t;
679
680         if (!governor)
681                 return -EINVAL;
682
683         down(&cpufreq_governor_sem);
684         
685         list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
686                 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
687                         up(&cpufreq_governor_sem);
688                         return -EBUSY;
689                 }
690         }
691         list_add(&governor->governor_list, &cpufreq_governor_list);
692
693         up(&cpufreq_governor_sem);
694
695         return 0;
696 }
697 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
698
699
700 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
701 {
702         if (!governor)
703                 return;
704
705         down(&cpufreq_governor_sem);
706         list_del(&governor->governor_list);
707         up(&cpufreq_governor_sem);
708         return;
709 }
710 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
711
712
713
714 /*********************************************************************
715  *                          POLICY INTERFACE                         *
716  *********************************************************************/
717
718 /**
719  * cpufreq_get_policy - get the current cpufreq_policy
720  * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
721  *
722  * Reads the current cpufreq policy.
723  */
724 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
725 {
726         struct cpufreq_policy *cpu_policy;
727         if (!policy)
728                 return -EINVAL;
729
730         cpu_policy = cpufreq_cpu_get(cpu);
731         if (!cpu_policy)
732                 return -EINVAL;
733
734         down(&cpu_policy->lock);
735         memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
736         up(&cpu_policy->lock);
737
738         cpufreq_cpu_put(cpu_policy);
739
740         return 0;
741 }
742 EXPORT_SYMBOL(cpufreq_get_policy);
743
744
745 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
746 {
747         int ret = 0;
748
749         memcpy(&policy->cpuinfo, 
750                &data->cpuinfo, 
751                sizeof(struct cpufreq_cpuinfo));
752
753         /* verify the cpu speed can be set within this limit */
754         ret = cpufreq_driver->verify(policy);
755         if (ret)
756                 goto error_out;
757
758         down_read(&cpufreq_notifier_rwsem);
759
760         /* adjust if necessary - all reasons */
761         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
762                             policy);
763
764         /* adjust if necessary - hardware incompatibility*/
765         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
766                             policy);
767
768         /* verify the cpu speed can be set within this limit,
769            which might be different to the first one */
770         ret = cpufreq_driver->verify(policy);
771         if (ret) {
772                 up_read(&cpufreq_notifier_rwsem);
773                 goto error_out;
774         }
775
776         /* notification of the new policy */
777         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
778                             policy);
779
780         up_read(&cpufreq_notifier_rwsem);
781
782         data->min    = policy->min;
783         data->max    = policy->max;
784
785         if (cpufreq_driver->setpolicy) {
786                 data->policy = policy->policy;
787                 ret = cpufreq_driver->setpolicy(policy);
788         } else {
789                 if (policy->governor != data->governor) {
790                         /* save old, working values */
791                         struct cpufreq_governor *old_gov = data->governor;
792
793                         /* end old governor */
794                         if (data->governor)
795                                 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
796
797                         /* start new governor */
798                         data->governor = policy->governor;
799                         if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
800                                 /* new governor failed, so re-start old one */
801                                 if (old_gov) {
802                                         data->governor = old_gov;
803                                         __cpufreq_governor(data, CPUFREQ_GOV_START);
804                                 }
805                                 ret = -EINVAL;
806                                 goto error_out;
807                         }
808                         /* might be a policy change, too, so fall through */
809                 }
810                 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
811         }
812
813  error_out:
814         return ret;
815 }
816
817 /**
818  *      cpufreq_set_policy - set a new CPUFreq policy
819  *      @policy: policy to be set.
820  *
821  *      Sets a new CPU frequency and voltage scaling policy.
822  */
823 int cpufreq_set_policy(struct cpufreq_policy *policy)
824 {
825         int ret = 0;
826         struct cpufreq_policy *data;
827
828         if (!policy)
829                 return -EINVAL;
830
831         data = cpufreq_cpu_get(policy->cpu);
832         if (!data)
833                 return -EINVAL;
834
835         /* lock this CPU */
836         down(&data->lock);
837
838         ret = __cpufreq_set_policy(data, policy);
839         data->user_policy.min = data->min;
840         data->user_policy.max = data->max;
841         data->user_policy.policy = data->policy;
842         data->user_policy.governor = data->governor;
843
844         up(&data->lock);
845         cpufreq_cpu_put(data);
846
847         return ret;
848 }
849 EXPORT_SYMBOL(cpufreq_set_policy);
850
851
852 /**
853  *      cpufreq_update_policy - re-evaluate an existing cpufreq policy
854  *      @cpu: CPU which shall be re-evaluated
855  *
856  *      Usefull for policy notifiers which have different necessities
857  *      at different times.
858  */
859 int cpufreq_update_policy(unsigned int cpu)
860 {
861         struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
862         struct cpufreq_policy policy;
863         int ret = 0;
864
865         if (!data)
866                 return -ENODEV;
867
868         down(&data->lock);
869
870         memcpy(&policy, 
871                data,
872                sizeof(struct cpufreq_policy));
873         policy.min = data->user_policy.min;
874         policy.max = data->user_policy.max;
875         policy.policy = data->user_policy.policy;
876         policy.governor = data->user_policy.governor;
877
878         ret = __cpufreq_set_policy(data, &policy);
879
880         up(&data->lock);
881
882         cpufreq_cpu_put(data);
883         return ret;
884 }
885 EXPORT_SYMBOL(cpufreq_update_policy);
886
887
888 /*********************************************************************
889  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
890  *********************************************************************/
891
892 /**
893  * adjust_jiffies - adjust the system "loops_per_jiffy"
894  *
895  * This function alters the system "loops_per_jiffy" for the clock
896  * speed change. Note that loops_per_jiffy cannot be updated on SMP
897  * systems as each CPU might be scaled differently. So, use the arch 
898  * per-CPU loops_per_jiffy value wherever possible.
899  */
900 #ifndef CONFIG_SMP
901 static unsigned long l_p_j_ref;
902 static unsigned int  l_p_j_ref_freq;
903
904 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
905 {
906         if (!l_p_j_ref_freq) {
907                 l_p_j_ref = loops_per_jiffy;
908                 l_p_j_ref_freq = ci->old;
909         }
910         if ((val == CPUFREQ_PRECHANGE  && ci->old < ci->new) ||
911             (val == CPUFREQ_POSTCHANGE && ci->old > ci->new))
912                 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
913 }
914 #else
915 #define adjust_jiffies(x...) do {} while (0)
916 #endif
917
918
919 /**
920  * cpufreq_notify_transition - call notifier chain and adjust_jiffies on frequency transition
921  *
922  * This function calls the transition notifiers and the "adjust_jiffies" function. It is called
923  * twice on all CPU frequency changes that have external effects. 
924  */
925 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
926 {
927         if (irqs_disabled())
928                 return;   /* Only valid if we're in the resume process where
929                            * everyone knows what CPU frequency we are at */
930
931         down_read(&cpufreq_notifier_rwsem);
932         switch (state) {
933         case CPUFREQ_PRECHANGE:
934                 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_PRECHANGE, freqs);
935                 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
936                 break;
937         case CPUFREQ_POSTCHANGE:
938                 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
939                 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_POSTCHANGE, freqs);
940                 cpufreq_cpu_data[freqs->cpu]->cur = freqs->new;
941                 break;
942         }
943         up_read(&cpufreq_notifier_rwsem);
944 }
945 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
946
947
948
949 /*********************************************************************
950  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
951  *********************************************************************/
952
953 /**
954  * cpufreq_register_driver - register a CPU Frequency driver
955  * @driver_data: A struct cpufreq_driver containing the values#
956  * submitted by the CPU Frequency driver.
957  *
958  *   Registers a CPU Frequency driver to this core code. This code 
959  * returns zero on success, -EBUSY when another driver got here first
960  * (and isn't unregistered in the meantime). 
961  *
962  */
963 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
964 {
965         unsigned long flags;
966
967         if (!driver_data || !driver_data->verify || !driver_data->init ||
968             ((!driver_data->setpolicy) && (!driver_data->target)))
969                 return -EINVAL;
970
971         spin_lock_irqsave(&cpufreq_driver_lock, flags);
972         if (cpufreq_driver) {
973                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
974                 return -EBUSY;
975         }
976         cpufreq_driver = driver_data;
977         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
978
979         return sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
980 }
981 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
982
983
984 /**
985  * cpufreq_unregister_driver - unregister the current CPUFreq driver
986  *
987  *    Unregister the current CPUFreq driver. Only call this if you have 
988  * the right to do so, i.e. if you have succeeded in initialising before!
989  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
990  * currently not initialised.
991  */
992 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
993 {
994         unsigned long flags;
995
996         if (!cpufreq_driver || (driver != cpufreq_driver))
997                 return -EINVAL;
998
999         sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1000
1001         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1002         cpufreq_driver = NULL;
1003         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1004
1005         return 0;
1006 }
1007 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);