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