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