This commit was manufactured by cvs2svn to create tag
[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         return cpufreq_driver->target(policy, target_freq, relation);
726 }
727 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
728
729
730 int cpufreq_driver_target(struct cpufreq_policy *policy,
731                           unsigned int target_freq,
732                           unsigned int relation)
733 {
734         unsigned int ret;
735
736         policy = cpufreq_cpu_get(policy->cpu);
737         if (!policy)
738                 return -EINVAL;
739
740         down(&policy->lock);
741
742         ret = __cpufreq_driver_target(policy, target_freq, relation);
743
744         up(&policy->lock);
745
746         cpufreq_cpu_put(policy);
747
748         return ret;
749 }
750 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
751
752
753 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
754 {
755         int ret = -EINVAL;
756
757         if (!try_module_get(policy->governor->owner))
758                 return -EINVAL;
759
760         ret = policy->governor->governor(policy, event);
761
762         /* we keep one module reference alive for each CPU governed by this CPU */
763         if ((event != CPUFREQ_GOV_START) || ret)
764                 module_put(policy->governor->owner);
765         if ((event == CPUFREQ_GOV_STOP) && !ret)
766                 module_put(policy->governor->owner);
767
768         return ret;
769 }
770
771
772 int cpufreq_governor(unsigned int cpu, unsigned int event)
773 {
774         int ret = 0;
775         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
776
777         if (!policy)
778                 return -EINVAL;
779
780         down(&policy->lock);
781         ret = __cpufreq_governor(policy, event);
782         up(&policy->lock);
783
784         cpufreq_cpu_put(policy);
785
786         return ret;
787 }
788 EXPORT_SYMBOL_GPL(cpufreq_governor);
789
790
791 int cpufreq_register_governor(struct cpufreq_governor *governor)
792 {
793         struct cpufreq_governor *t;
794
795         if (!governor)
796                 return -EINVAL;
797
798         down(&cpufreq_governor_sem);
799         
800         list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
801                 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
802                         up(&cpufreq_governor_sem);
803                         return -EBUSY;
804                 }
805         }
806         list_add(&governor->governor_list, &cpufreq_governor_list);
807
808         up(&cpufreq_governor_sem);
809
810         return 0;
811 }
812 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
813
814
815 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
816 {
817         if (!governor)
818                 return;
819
820         down(&cpufreq_governor_sem);
821         list_del(&governor->governor_list);
822         up(&cpufreq_governor_sem);
823         return;
824 }
825 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
826
827
828
829 /*********************************************************************
830  *                          POLICY INTERFACE                         *
831  *********************************************************************/
832
833 /**
834  * cpufreq_get_policy - get the current cpufreq_policy
835  * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
836  *
837  * Reads the current cpufreq policy.
838  */
839 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
840 {
841         struct cpufreq_policy *cpu_policy;
842         if (!policy)
843                 return -EINVAL;
844
845         cpu_policy = cpufreq_cpu_get(cpu);
846         if (!cpu_policy)
847                 return -EINVAL;
848
849         down(&cpu_policy->lock);
850         memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
851         up(&cpu_policy->lock);
852
853         cpufreq_cpu_put(cpu_policy);
854
855         return 0;
856 }
857 EXPORT_SYMBOL(cpufreq_get_policy);
858
859
860 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
861 {
862         int ret = 0;
863
864         memcpy(&policy->cpuinfo, 
865                &data->cpuinfo, 
866                sizeof(struct cpufreq_cpuinfo));
867
868         /* verify the cpu speed can be set within this limit */
869         ret = cpufreq_driver->verify(policy);
870         if (ret)
871                 goto error_out;
872
873         down_read(&cpufreq_notifier_rwsem);
874
875         /* adjust if necessary - all reasons */
876         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
877                             policy);
878
879         /* adjust if necessary - hardware incompatibility*/
880         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
881                             policy);
882
883         /* verify the cpu speed can be set within this limit,
884            which might be different to the first one */
885         ret = cpufreq_driver->verify(policy);
886         if (ret) {
887                 up_read(&cpufreq_notifier_rwsem);
888                 goto error_out;
889         }
890
891         /* notification of the new policy */
892         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
893                             policy);
894
895         up_read(&cpufreq_notifier_rwsem);
896
897         data->min    = policy->min;
898         data->max    = policy->max;
899
900         if (cpufreq_driver->setpolicy) {
901                 data->policy = policy->policy;
902                 ret = cpufreq_driver->setpolicy(policy);
903         } else {
904                 if (policy->governor != data->governor) {
905                         /* save old, working values */
906                         struct cpufreq_governor *old_gov = data->governor;
907
908                         /* end old governor */
909                         if (data->governor)
910                                 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
911
912                         /* start new governor */
913                         data->governor = policy->governor;
914                         if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
915                                 /* new governor failed, so re-start old one */
916                                 if (old_gov) {
917                                         data->governor = old_gov;
918                                         __cpufreq_governor(data, CPUFREQ_GOV_START);
919                                 }
920                                 ret = -EINVAL;
921                                 goto error_out;
922                         }
923                         /* might be a policy change, too, so fall through */
924                 }
925                 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
926         }
927
928  error_out:
929         return ret;
930 }
931
932 /**
933  *      cpufreq_set_policy - set a new CPUFreq policy
934  *      @policy: policy to be set.
935  *
936  *      Sets a new CPU frequency and voltage scaling policy.
937  */
938 int cpufreq_set_policy(struct cpufreq_policy *policy)
939 {
940         int ret = 0;
941         struct cpufreq_policy *data;
942
943         if (!policy)
944                 return -EINVAL;
945
946         data = cpufreq_cpu_get(policy->cpu);
947         if (!data)
948                 return -EINVAL;
949
950         /* lock this CPU */
951         down(&data->lock);
952
953         ret = __cpufreq_set_policy(data, policy);
954         data->user_policy.min = data->min;
955         data->user_policy.max = data->max;
956         data->user_policy.policy = data->policy;
957         data->user_policy.governor = data->governor;
958
959         up(&data->lock);
960         cpufreq_cpu_put(data);
961
962         return ret;
963 }
964 EXPORT_SYMBOL(cpufreq_set_policy);
965
966
967 /**
968  *      cpufreq_update_policy - re-evaluate an existing cpufreq policy
969  *      @cpu: CPU which shall be re-evaluated
970  *
971  *      Usefull for policy notifiers which have different necessities
972  *      at different times.
973  */
974 int cpufreq_update_policy(unsigned int cpu)
975 {
976         struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
977         struct cpufreq_policy policy;
978         int ret = 0;
979
980         if (!data)
981                 return -ENODEV;
982
983         down(&data->lock);
984
985         memcpy(&policy, 
986                data,
987                sizeof(struct cpufreq_policy));
988         policy.min = data->user_policy.min;
989         policy.max = data->user_policy.max;
990         policy.policy = data->user_policy.policy;
991         policy.governor = data->user_policy.governor;
992
993         ret = __cpufreq_set_policy(data, &policy);
994
995         up(&data->lock);
996
997         cpufreq_cpu_put(data);
998         return ret;
999 }
1000 EXPORT_SYMBOL(cpufreq_update_policy);
1001
1002
1003 /*********************************************************************
1004  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
1005  *********************************************************************/
1006
1007 /**
1008  * adjust_jiffies - adjust the system "loops_per_jiffy"
1009  *
1010  * This function alters the system "loops_per_jiffy" for the clock
1011  * speed change. Note that loops_per_jiffy cannot be updated on SMP
1012  * systems as each CPU might be scaled differently. So, use the arch 
1013  * per-CPU loops_per_jiffy value wherever possible.
1014  */
1015 #ifndef CONFIG_SMP
1016 static unsigned long l_p_j_ref;
1017 static unsigned int  l_p_j_ref_freq;
1018
1019 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
1020 {
1021         if (ci->flags & CPUFREQ_CONST_LOOPS)
1022                 return;
1023
1024         if (!l_p_j_ref_freq) {
1025                 l_p_j_ref = loops_per_jiffy;
1026                 l_p_j_ref_freq = ci->old;
1027         }
1028         if ((val == CPUFREQ_PRECHANGE  && ci->old < ci->new) ||
1029             (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
1030             (val == CPUFREQ_RESUMECHANGE))
1031                 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
1032 }
1033 #else
1034 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
1035 #endif
1036
1037
1038 /**
1039  * cpufreq_notify_transition - call notifier chain and adjust_jiffies on frequency transition
1040  *
1041  * This function calls the transition notifiers and the "adjust_jiffies" function. It is called
1042  * twice on all CPU frequency changes that have external effects. 
1043  */
1044 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
1045 {
1046         BUG_ON(irqs_disabled());
1047
1048         freqs->flags = cpufreq_driver->flags;
1049
1050         down_read(&cpufreq_notifier_rwsem);
1051         switch (state) {
1052         case CPUFREQ_PRECHANGE:
1053                 /* detect if the driver reported a value as "old frequency" which
1054                  * is not equal to what the cpufreq core thinks is "old frequency".
1055                  */
1056                 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1057                         if ((likely(cpufreq_cpu_data[freqs->cpu]->cur)) &&
1058                             (unlikely(freqs->old != cpufreq_cpu_data[freqs->cpu]->cur)))
1059                         {
1060                                 if (cpufreq_driver->flags & CPUFREQ_PANIC_OUTOFSYNC)
1061                                         panic("CPU Frequency is out of sync.");
1062
1063                                 printk(KERN_WARNING "Warning: CPU frequency out of sync: "
1064                                        "cpufreq and timing core thinks of %u, is %u kHz.\n", 
1065                                        cpufreq_cpu_data[freqs->cpu]->cur, freqs->old);
1066                                 freqs->old = cpufreq_cpu_data[freqs->cpu]->cur;
1067                         }
1068                 }
1069                 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_PRECHANGE, freqs);
1070                 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
1071                 break;
1072         case CPUFREQ_POSTCHANGE:
1073                 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
1074                 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_POSTCHANGE, freqs);
1075                 cpufreq_cpu_data[freqs->cpu]->cur = freqs->new;
1076                 break;
1077         }
1078         up_read(&cpufreq_notifier_rwsem);
1079 }
1080 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
1081
1082
1083
1084 /*********************************************************************
1085  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
1086  *********************************************************************/
1087
1088 /**
1089  * cpufreq_register_driver - register a CPU Frequency driver
1090  * @driver_data: A struct cpufreq_driver containing the values#
1091  * submitted by the CPU Frequency driver.
1092  *
1093  *   Registers a CPU Frequency driver to this core code. This code 
1094  * returns zero on success, -EBUSY when another driver got here first
1095  * (and isn't unregistered in the meantime). 
1096  *
1097  */
1098 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1099 {
1100         unsigned long flags;
1101         int ret;
1102
1103         if (!driver_data || !driver_data->verify || !driver_data->init ||
1104             ((!driver_data->setpolicy) && (!driver_data->target)))
1105                 return -EINVAL;
1106
1107         if (driver_data->setpolicy)
1108                 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1109
1110         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1111         if (cpufreq_driver) {
1112                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1113                 return -EBUSY;
1114         }
1115         cpufreq_driver = driver_data;
1116         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1117
1118         ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1119
1120         if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1121                 int i;
1122                 ret = -ENODEV;
1123
1124                 /* check for at least one working CPU */
1125                 for (i=0; i<NR_CPUS; i++)
1126                         if (cpufreq_cpu_data[i])
1127                                 ret = 0;
1128
1129                 /* if all ->init() calls failed, unregister */
1130                 if (ret) {
1131                         sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1132
1133                         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1134                         cpufreq_driver = NULL;
1135                         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1136                 }
1137         }
1138
1139         return (ret);
1140 }
1141 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1142
1143
1144 /**
1145  * cpufreq_unregister_driver - unregister the current CPUFreq driver
1146  *
1147  *    Unregister the current CPUFreq driver. Only call this if you have 
1148  * the right to do so, i.e. if you have succeeded in initialising before!
1149  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1150  * currently not initialised.
1151  */
1152 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1153 {
1154         unsigned long flags;
1155
1156         if (!cpufreq_driver || (driver != cpufreq_driver))
1157                 return -EINVAL;
1158
1159         sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1160
1161         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1162         cpufreq_driver = NULL;
1163         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1164
1165         return 0;
1166 }
1167 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);