ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.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 scnprintf(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 scnprintf(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 += scnprintf(&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                 ret = -ENOMEM;
365                 goto nomem_out;
366         }
367         memset(policy, 0, sizeof(struct cpufreq_policy));
368
369         policy->cpu = cpu;
370         init_MUTEX_LOCKED(&policy->lock);
371         init_completion(&policy->kobj_unregister);
372
373         /* call driver. From then on the cpufreq must be able
374          * to accept all calls to ->verify and ->setpolicy for this CPU
375          */
376         ret = cpufreq_driver->init(policy);
377         if (ret)
378                 goto err_out;
379
380         memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
381
382         /* prepare interface data */
383         policy->kobj.parent = &sys_dev->kobj;
384         policy->kobj.ktype = &ktype_cpufreq;
385         strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
386
387         ret = kobject_register(&policy->kobj);
388         if (ret)
389                 goto err_out;
390
391         /* set up files for this cpu device */
392         drv_attr = cpufreq_driver->attr;
393         while ((drv_attr) && (*drv_attr)) {
394                 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
395                 drv_attr++;
396         }
397
398         spin_lock_irqsave(&cpufreq_driver_lock, flags);
399         cpufreq_cpu_data[cpu] = policy;
400         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
401         policy->governor = NULL; /* to assure that the starting sequence is
402                                   * run in cpufreq_set_policy */
403         up(&policy->lock);
404         
405         /* set default policy */
406         
407         ret = cpufreq_set_policy(&new_policy);
408         if (ret)
409                 goto err_out_unregister;
410
411         module_put(cpufreq_driver->owner);
412         return 0;
413
414
415 err_out_unregister:
416         spin_lock_irqsave(&cpufreq_driver_lock, flags);
417         cpufreq_cpu_data[cpu] = NULL;
418         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
419
420         kobject_unregister(&policy->kobj);
421         wait_for_completion(&policy->kobj_unregister);
422
423 err_out:
424         kfree(policy);
425
426 nomem_out:
427         module_put(cpufreq_driver->owner);
428         return ret;
429 }
430
431
432 /**
433  * cpufreq_remove_dev - remove a CPU device
434  *
435  * Removes the cpufreq interface for a CPU device.
436  */
437 static int cpufreq_remove_dev (struct sys_device * sys_dev)
438 {
439         unsigned int cpu = sys_dev->id;
440         unsigned long flags;
441         struct cpufreq_policy *data;
442
443         spin_lock_irqsave(&cpufreq_driver_lock, flags);
444         data = cpufreq_cpu_data[cpu];
445
446         if (!data) {
447                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
448                 return -EINVAL;
449         }
450         cpufreq_cpu_data[cpu] = NULL;
451         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
452
453         if (!kobject_get(&data->kobj))
454                 return -EFAULT;
455
456         if (cpufreq_driver->target)
457                 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
458
459         kobject_unregister(&data->kobj);
460
461         kobject_put(&data->kobj);
462
463         /* we need to make sure that the underlying kobj is actually
464          * not referenced anymore by anybody before we proceed with 
465          * unloading.
466          */
467         wait_for_completion(&data->kobj_unregister);
468
469         if (cpufreq_driver->exit)
470                 cpufreq_driver->exit(data);
471
472         kfree(data);
473
474         return 0;
475 }
476
477 /**
478  *      cpufreq_resume - restore the CPU clock frequency after resume
479  *
480  *      Restore the CPU clock frequency so that our idea of the current
481  *      frequency reflects the actual hardware.
482  */
483 static int cpufreq_resume(struct sys_device * sysdev)
484 {
485         int cpu = sysdev->id;
486         unsigned int ret = 0;
487         struct cpufreq_policy *cpu_policy;
488
489         if (!cpu_online(cpu))
490                 return 0;
491
492         /* we may be lax here as interrupts are off. Nonetheless
493          * we need to grab the correct cpu policy, as to check
494          * whether we really run on this CPU.
495          */
496
497         cpu_policy = cpufreq_cpu_get(cpu);
498         if (!cpu_policy)
499                 return -EINVAL;
500
501         if (cpufreq_driver->resume)
502                 ret = cpufreq_driver->resume(cpu_policy);
503         if (ret) {
504                 printk(KERN_ERR "cpufreq: resume failed in ->resume step on CPU %u\n", cpu_policy->cpu);
505                 goto out;
506         }
507
508         if (cpufreq_driver->setpolicy)
509                 ret = cpufreq_driver->setpolicy(cpu_policy);
510         else
511                 /* CPUFREQ_RELATION_H or CPUFREQ_RELATION_L have the same effect here, as cpu_policy->cur is known
512                  * to be a valid and exact target frequency
513                  */
514                 ret = cpufreq_driver->target(cpu_policy, cpu_policy->cur, CPUFREQ_RELATION_H);
515
516         if (ret)
517                 printk(KERN_ERR "cpufreq: resume failed in ->setpolicy/target step on CPU %u\n", cpu_policy->cpu);
518
519 out:
520         cpufreq_cpu_put(cpu_policy);
521         return ret;
522 }
523
524 static struct sysdev_driver cpufreq_sysdev_driver = {
525         .add            = cpufreq_add_dev,
526         .remove         = cpufreq_remove_dev,
527         .resume         = cpufreq_resume,
528 };
529
530
531 /*********************************************************************
532  *                     NOTIFIER LISTS INTERFACE                      *
533  *********************************************************************/
534
535 /**
536  *      cpufreq_register_notifier - register a driver with cpufreq
537  *      @nb: notifier function to register
538  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
539  *
540  *      Add a driver to one of two lists: either a list of drivers that 
541  *      are notified about clock rate changes (once before and once after
542  *      the transition), or a list of drivers that are notified about
543  *      changes in cpufreq policy.
544  *
545  *      This function may sleep, and has the same return conditions as
546  *      notifier_chain_register.
547  */
548 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
549 {
550         int ret;
551
552         down_write(&cpufreq_notifier_rwsem);
553         switch (list) {
554         case CPUFREQ_TRANSITION_NOTIFIER:
555                 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
556                 break;
557         case CPUFREQ_POLICY_NOTIFIER:
558                 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
559                 break;
560         default:
561                 ret = -EINVAL;
562         }
563         up_write(&cpufreq_notifier_rwsem);
564
565         return ret;
566 }
567 EXPORT_SYMBOL(cpufreq_register_notifier);
568
569
570 /**
571  *      cpufreq_unregister_notifier - unregister a driver with cpufreq
572  *      @nb: notifier block to be unregistered
573  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
574  *
575  *      Remove a driver from the CPU frequency notifier list.
576  *
577  *      This function may sleep, and has the same return conditions as
578  *      notifier_chain_unregister.
579  */
580 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
581 {
582         int ret;
583
584         down_write(&cpufreq_notifier_rwsem);
585         switch (list) {
586         case CPUFREQ_TRANSITION_NOTIFIER:
587                 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
588                 break;
589         case CPUFREQ_POLICY_NOTIFIER:
590                 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
591                 break;
592         default:
593                 ret = -EINVAL;
594         }
595         up_write(&cpufreq_notifier_rwsem);
596
597         return ret;
598 }
599 EXPORT_SYMBOL(cpufreq_unregister_notifier);
600
601
602 /*********************************************************************
603  *                              GOVERNORS                            *
604  *********************************************************************/
605
606
607 int __cpufreq_driver_target(struct cpufreq_policy *policy,
608                             unsigned int target_freq,
609                             unsigned int relation)
610 {
611         return cpufreq_driver->target(policy, target_freq, relation);
612 }
613 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
614
615
616 int cpufreq_driver_target(struct cpufreq_policy *policy,
617                           unsigned int target_freq,
618                           unsigned int relation)
619 {
620         unsigned int ret;
621
622         policy = cpufreq_cpu_get(policy->cpu);
623         if (!policy)
624                 return -EINVAL;
625
626         down(&policy->lock);
627
628         ret = __cpufreq_driver_target(policy, target_freq, relation);
629
630         up(&policy->lock);
631
632         cpufreq_cpu_put(policy);
633
634         return ret;
635 }
636 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
637
638
639 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
640 {
641         int ret = -EINVAL;
642
643         if (!try_module_get(policy->governor->owner))
644                 return -EINVAL;
645
646         ret = policy->governor->governor(policy, event);
647
648         /* we keep one module reference alive for each CPU governed by this CPU */
649         if ((event != CPUFREQ_GOV_START) || ret)
650                 module_put(policy->governor->owner);
651         if ((event == CPUFREQ_GOV_STOP) && !ret)
652                 module_put(policy->governor->owner);
653
654         return ret;
655 }
656
657
658 int cpufreq_governor(unsigned int cpu, unsigned int event)
659 {
660         int ret = 0;
661         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
662
663         if (!policy)
664                 return -EINVAL;
665
666         down(&policy->lock);
667         ret = __cpufreq_governor(policy, event);
668         up(&policy->lock);
669
670         cpufreq_cpu_put(policy);
671
672         return ret;
673 }
674 EXPORT_SYMBOL_GPL(cpufreq_governor);
675
676
677 int cpufreq_register_governor(struct cpufreq_governor *governor)
678 {
679         struct cpufreq_governor *t;
680
681         if (!governor)
682                 return -EINVAL;
683
684         down(&cpufreq_governor_sem);
685         
686         list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
687                 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
688                         up(&cpufreq_governor_sem);
689                         return -EBUSY;
690                 }
691         }
692         list_add(&governor->governor_list, &cpufreq_governor_list);
693
694         up(&cpufreq_governor_sem);
695
696         return 0;
697 }
698 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
699
700
701 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
702 {
703         if (!governor)
704                 return;
705
706         down(&cpufreq_governor_sem);
707         list_del(&governor->governor_list);
708         up(&cpufreq_governor_sem);
709         return;
710 }
711 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
712
713
714
715 /*********************************************************************
716  *                          POLICY INTERFACE                         *
717  *********************************************************************/
718
719 /**
720  * cpufreq_get_policy - get the current cpufreq_policy
721  * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
722  *
723  * Reads the current cpufreq policy.
724  */
725 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
726 {
727         struct cpufreq_policy *cpu_policy;
728         if (!policy)
729                 return -EINVAL;
730
731         cpu_policy = cpufreq_cpu_get(cpu);
732         if (!cpu_policy)
733                 return -EINVAL;
734
735         down(&cpu_policy->lock);
736         memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
737         up(&cpu_policy->lock);
738
739         cpufreq_cpu_put(cpu_policy);
740
741         return 0;
742 }
743 EXPORT_SYMBOL(cpufreq_get_policy);
744
745
746 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
747 {
748         int ret = 0;
749
750         memcpy(&policy->cpuinfo, 
751                &data->cpuinfo, 
752                sizeof(struct cpufreq_cpuinfo));
753
754         /* verify the cpu speed can be set within this limit */
755         ret = cpufreq_driver->verify(policy);
756         if (ret)
757                 goto error_out;
758
759         down_read(&cpufreq_notifier_rwsem);
760
761         /* adjust if necessary - all reasons */
762         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
763                             policy);
764
765         /* adjust if necessary - hardware incompatibility*/
766         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
767                             policy);
768
769         /* verify the cpu speed can be set within this limit,
770            which might be different to the first one */
771         ret = cpufreq_driver->verify(policy);
772         if (ret) {
773                 up_read(&cpufreq_notifier_rwsem);
774                 goto error_out;
775         }
776
777         /* notification of the new policy */
778         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
779                             policy);
780
781         up_read(&cpufreq_notifier_rwsem);
782
783         data->min    = policy->min;
784         data->max    = policy->max;
785
786         if (cpufreq_driver->setpolicy) {
787                 data->policy = policy->policy;
788                 ret = cpufreq_driver->setpolicy(policy);
789         } else {
790                 if (policy->governor != data->governor) {
791                         /* save old, working values */
792                         struct cpufreq_governor *old_gov = data->governor;
793
794                         /* end old governor */
795                         if (data->governor)
796                                 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
797
798                         /* start new governor */
799                         data->governor = policy->governor;
800                         if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
801                                 /* new governor failed, so re-start old one */
802                                 if (old_gov) {
803                                         data->governor = old_gov;
804                                         __cpufreq_governor(data, CPUFREQ_GOV_START);
805                                 }
806                                 ret = -EINVAL;
807                                 goto error_out;
808                         }
809                         /* might be a policy change, too, so fall through */
810                 }
811                 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
812         }
813
814  error_out:
815         return ret;
816 }
817
818 /**
819  *      cpufreq_set_policy - set a new CPUFreq policy
820  *      @policy: policy to be set.
821  *
822  *      Sets a new CPU frequency and voltage scaling policy.
823  */
824 int cpufreq_set_policy(struct cpufreq_policy *policy)
825 {
826         int ret = 0;
827         struct cpufreq_policy *data;
828
829         if (!policy)
830                 return -EINVAL;
831
832         data = cpufreq_cpu_get(policy->cpu);
833         if (!data)
834                 return -EINVAL;
835
836         /* lock this CPU */
837         down(&data->lock);
838
839         ret = __cpufreq_set_policy(data, policy);
840         data->user_policy.min = data->min;
841         data->user_policy.max = data->max;
842         data->user_policy.policy = data->policy;
843         data->user_policy.governor = data->governor;
844
845         up(&data->lock);
846         cpufreq_cpu_put(data);
847
848         return ret;
849 }
850 EXPORT_SYMBOL(cpufreq_set_policy);
851
852
853 /**
854  *      cpufreq_update_policy - re-evaluate an existing cpufreq policy
855  *      @cpu: CPU which shall be re-evaluated
856  *
857  *      Usefull for policy notifiers which have different necessities
858  *      at different times.
859  */
860 int cpufreq_update_policy(unsigned int cpu)
861 {
862         struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
863         struct cpufreq_policy policy;
864         int ret = 0;
865
866         if (!data)
867                 return -ENODEV;
868
869         down(&data->lock);
870
871         memcpy(&policy, 
872                data,
873                sizeof(struct cpufreq_policy));
874         policy.min = data->user_policy.min;
875         policy.max = data->user_policy.max;
876         policy.policy = data->user_policy.policy;
877         policy.governor = data->user_policy.governor;
878
879         ret = __cpufreq_set_policy(data, &policy);
880
881         up(&data->lock);
882
883         cpufreq_cpu_put(data);
884         return ret;
885 }
886 EXPORT_SYMBOL(cpufreq_update_policy);
887
888
889 /*********************************************************************
890  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
891  *********************************************************************/
892
893 /**
894  * adjust_jiffies - adjust the system "loops_per_jiffy"
895  *
896  * This function alters the system "loops_per_jiffy" for the clock
897  * speed change. Note that loops_per_jiffy cannot be updated on SMP
898  * systems as each CPU might be scaled differently. So, use the arch 
899  * per-CPU loops_per_jiffy value wherever possible.
900  */
901 #ifndef CONFIG_SMP
902 static unsigned long l_p_j_ref;
903 static unsigned int  l_p_j_ref_freq;
904
905 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
906 {
907         if (!l_p_j_ref_freq) {
908                 l_p_j_ref = loops_per_jiffy;
909                 l_p_j_ref_freq = ci->old;
910         }
911         if ((val == CPUFREQ_PRECHANGE  && ci->old < ci->new) ||
912             (val == CPUFREQ_POSTCHANGE && ci->old > ci->new))
913                 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
914 }
915 #else
916 #define adjust_jiffies(x...) do {} while (0)
917 #endif
918
919
920 /**
921  * cpufreq_notify_transition - call notifier chain and adjust_jiffies on frequency transition
922  *
923  * This function calls the transition notifiers and the "adjust_jiffies" function. It is called
924  * twice on all CPU frequency changes that have external effects. 
925  */
926 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
927 {
928         if (irqs_disabled())
929                 return;   /* Only valid if we're in the resume process where
930                            * everyone knows what CPU frequency we are at */
931
932         down_read(&cpufreq_notifier_rwsem);
933         switch (state) {
934         case CPUFREQ_PRECHANGE:
935                 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_PRECHANGE, freqs);
936                 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
937                 break;
938         case CPUFREQ_POSTCHANGE:
939                 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
940                 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_POSTCHANGE, freqs);
941                 cpufreq_cpu_data[freqs->cpu]->cur = freqs->new;
942                 break;
943         }
944         up_read(&cpufreq_notifier_rwsem);
945 }
946 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
947
948
949
950 /*********************************************************************
951  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
952  *********************************************************************/
953
954 /**
955  * cpufreq_register_driver - register a CPU Frequency driver
956  * @driver_data: A struct cpufreq_driver containing the values#
957  * submitted by the CPU Frequency driver.
958  *
959  *   Registers a CPU Frequency driver to this core code. This code 
960  * returns zero on success, -EBUSY when another driver got here first
961  * (and isn't unregistered in the meantime). 
962  *
963  */
964 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
965 {
966         unsigned long flags;
967         int ret;
968
969         if (!driver_data || !driver_data->verify || !driver_data->init ||
970             ((!driver_data->setpolicy) && (!driver_data->target)))
971                 return -EINVAL;
972
973         spin_lock_irqsave(&cpufreq_driver_lock, flags);
974         if (cpufreq_driver) {
975                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
976                 return -EBUSY;
977         }
978         cpufreq_driver = driver_data;
979         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
980
981         ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
982
983         if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
984                 int i;
985                 ret = -ENODEV;
986
987                 /* check for at least one working CPU */
988                 for (i=0; i<NR_CPUS; i++)
989                         if (cpufreq_cpu_data[i])
990                                 ret = 0;
991
992                 /* if all ->init() calls failed, unregister */
993                 if (ret) {
994                         sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
995
996                         spin_lock_irqsave(&cpufreq_driver_lock, flags);
997                         cpufreq_driver = NULL;
998                         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
999                 }
1000         }
1001
1002         return (ret);
1003 }
1004 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1005
1006
1007 /**
1008  * cpufreq_unregister_driver - unregister the current CPUFreq driver
1009  *
1010  *    Unregister the current CPUFreq driver. Only call this if you have 
1011  * the right to do so, i.e. if you have succeeded in initialising before!
1012  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1013  * currently not initialised.
1014  */
1015 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1016 {
1017         unsigned long flags;
1018
1019         if (!cpufreq_driver || (driver != cpufreq_driver))
1020                 return -EINVAL;
1021
1022         sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1023
1024         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1025         cpufreq_driver = NULL;
1026         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1027
1028         return 0;
1029 }
1030 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);