]> Pileus Git - ~andy/linux/blob - drivers/cpufreq/cpufreq.c
[CPUFREQ] convert remaining cpufreq semaphore to a mutex
[~andy/linux] / 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  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
8  *                      Added handling for CPU hotplug
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/notifier.h>
21 #include <linux/cpufreq.h>
22 #include <linux/delay.h>
23 #include <linux/interrupt.h>
24 #include <linux/spinlock.h>
25 #include <linux/device.h>
26 #include <linux/slab.h>
27 #include <linux/cpu.h>
28 #include <linux/completion.h>
29 #include <linux/mutex.h>
30
31 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
32
33 /**
34  * The "cpufreq driver" - the arch- or hardware-dependend low
35  * level driver of CPUFreq support, and its spinlock. This lock
36  * also protects the cpufreq_cpu_data array.
37  */
38 static struct cpufreq_driver    *cpufreq_driver;
39 static struct cpufreq_policy    *cpufreq_cpu_data[NR_CPUS];
40 static DEFINE_SPINLOCK(cpufreq_driver_lock);
41
42 /* internal prototypes */
43 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
44 static void handle_update(void *data);
45
46 /**
47  * Two notifier lists: the "policy" list is involved in the 
48  * validation process for a new CPU frequency policy; the 
49  * "transition" list for kernel code that needs to handle
50  * changes to devices when the CPU clock speed changes.
51  * The mutex locks both lists.
52  */
53 static struct notifier_block    *cpufreq_policy_notifier_list;
54 static struct notifier_block    *cpufreq_transition_notifier_list;
55 static DECLARE_RWSEM            (cpufreq_notifier_rwsem);
56
57
58 static LIST_HEAD(cpufreq_governor_list);
59 static DEFINE_MUTEX             (cpufreq_governor_mutex);
60
61 struct cpufreq_policy * cpufreq_cpu_get(unsigned int cpu)
62 {
63         struct cpufreq_policy *data;
64         unsigned long flags;
65
66         if (cpu >= NR_CPUS)
67                 goto err_out;
68
69         /* get the cpufreq driver */
70         spin_lock_irqsave(&cpufreq_driver_lock, flags);
71
72         if (!cpufreq_driver)
73                 goto err_out_unlock;
74
75         if (!try_module_get(cpufreq_driver->owner))
76                 goto err_out_unlock;
77
78
79         /* get the CPU */
80         data = cpufreq_cpu_data[cpu];
81
82         if (!data)
83                 goto err_out_put_module;
84
85         if (!kobject_get(&data->kobj))
86                 goto err_out_put_module;
87
88
89         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
90
91         return data;
92
93  err_out_put_module:
94         module_put(cpufreq_driver->owner);
95  err_out_unlock:
96         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
97  err_out:
98         return NULL;
99 }
100 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
101
102 void cpufreq_cpu_put(struct cpufreq_policy *data)
103 {
104         kobject_put(&data->kobj);
105         module_put(cpufreq_driver->owner);
106 }
107 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
108
109
110 /*********************************************************************
111  *                     UNIFIED DEBUG HELPERS                         *
112  *********************************************************************/
113 #ifdef CONFIG_CPU_FREQ_DEBUG
114
115 /* what part(s) of the CPUfreq subsystem are debugged? */
116 static unsigned int debug;
117
118 /* is the debug output ratelimit'ed using printk_ratelimit? User can
119  * set or modify this value.
120  */
121 static unsigned int debug_ratelimit = 1;
122
123 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
124  * loading of a cpufreq driver, temporarily disabled when a new policy
125  * is set, and disabled upon cpufreq driver removal
126  */
127 static unsigned int disable_ratelimit = 1;
128 static DEFINE_SPINLOCK(disable_ratelimit_lock);
129
130 static void cpufreq_debug_enable_ratelimit(void)
131 {
132         unsigned long flags;
133
134         spin_lock_irqsave(&disable_ratelimit_lock, flags);
135         if (disable_ratelimit)
136                 disable_ratelimit--;
137         spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
138 }
139
140 static void cpufreq_debug_disable_ratelimit(void)
141 {
142         unsigned long flags;
143
144         spin_lock_irqsave(&disable_ratelimit_lock, flags);
145         disable_ratelimit++;
146         spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
147 }
148
149 void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
150 {
151         char s[256];
152         va_list args;
153         unsigned int len;
154         unsigned long flags;
155         
156         WARN_ON(!prefix);
157         if (type & debug) {
158                 spin_lock_irqsave(&disable_ratelimit_lock, flags);
159                 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
160                         spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
161                         return;
162                 }
163                 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
164
165                 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
166
167                 va_start(args, fmt);
168                 len += vsnprintf(&s[len], (256 - len), fmt, args);
169                 va_end(args);
170
171                 printk(s);
172
173                 WARN_ON(len < 5);
174         }
175 }
176 EXPORT_SYMBOL(cpufreq_debug_printk);
177
178
179 module_param(debug, uint, 0644);
180 MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
181
182 module_param(debug_ratelimit, uint, 0644);
183 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
184
185 #else /* !CONFIG_CPU_FREQ_DEBUG */
186
187 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
188 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
189
190 #endif /* CONFIG_CPU_FREQ_DEBUG */
191
192
193 /*********************************************************************
194  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
195  *********************************************************************/
196
197 /**
198  * adjust_jiffies - adjust the system "loops_per_jiffy"
199  *
200  * This function alters the system "loops_per_jiffy" for the clock
201  * speed change. Note that loops_per_jiffy cannot be updated on SMP
202  * systems as each CPU might be scaled differently. So, use the arch 
203  * per-CPU loops_per_jiffy value wherever possible.
204  */
205 #ifndef CONFIG_SMP
206 static unsigned long l_p_j_ref;
207 static unsigned int  l_p_j_ref_freq;
208
209 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
210 {
211         if (ci->flags & CPUFREQ_CONST_LOOPS)
212                 return;
213
214         if (!l_p_j_ref_freq) {
215                 l_p_j_ref = loops_per_jiffy;
216                 l_p_j_ref_freq = ci->old;
217                 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
218         }
219         if ((val == CPUFREQ_PRECHANGE  && ci->old < ci->new) ||
220             (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
221             (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
222                 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
223                 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
224         }
225 }
226 #else
227 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
228 #endif
229
230
231 /**
232  * cpufreq_notify_transition - call notifier chain and adjust_jiffies on frequency transition
233  *
234  * This function calls the transition notifiers and the "adjust_jiffies" function. It is called
235  * twice on all CPU frequency changes that have external effects. 
236  */
237 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
238 {
239         BUG_ON(irqs_disabled());
240
241         freqs->flags = cpufreq_driver->flags;
242         dprintk("notification %u of frequency transition to %u kHz\n", state, freqs->new);
243
244         down_read(&cpufreq_notifier_rwsem);
245         switch (state) {
246         case CPUFREQ_PRECHANGE:
247                 /* detect if the driver reported a value as "old frequency" which
248                  * is not equal to what the cpufreq core thinks is "old frequency".
249                  */
250                 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
251                         if ((likely(cpufreq_cpu_data[freqs->cpu])) &&
252                             (likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu)) &&
253                             (likely(cpufreq_cpu_data[freqs->cpu]->cur)) &&
254                             (unlikely(freqs->old != cpufreq_cpu_data[freqs->cpu]->cur)))
255                         {
256                                 dprintk(KERN_WARNING "Warning: CPU frequency is %u, "
257                                        "cpufreq assumed %u kHz.\n", freqs->old, cpufreq_cpu_data[freqs->cpu]->cur);
258                                 freqs->old = cpufreq_cpu_data[freqs->cpu]->cur;
259                         }
260                 }
261                 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_PRECHANGE, freqs);
262                 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
263                 break;
264         case CPUFREQ_POSTCHANGE:
265                 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
266                 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_POSTCHANGE, freqs);
267                 if ((likely(cpufreq_cpu_data[freqs->cpu])) && 
268                     (likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu)))
269                         cpufreq_cpu_data[freqs->cpu]->cur = freqs->new;
270                 break;
271         }
272         up_read(&cpufreq_notifier_rwsem);
273 }
274 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
275
276
277
278 /*********************************************************************
279  *                          SYSFS INTERFACE                          *
280  *********************************************************************/
281
282 /**
283  * cpufreq_parse_governor - parse a governor string
284  */
285 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
286                                 struct cpufreq_governor **governor)
287 {
288         if (!cpufreq_driver)
289                 return -EINVAL;
290         if (cpufreq_driver->setpolicy) {
291                 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
292                         *policy = CPUFREQ_POLICY_PERFORMANCE;
293                         return 0;
294                 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
295                         *policy = CPUFREQ_POLICY_POWERSAVE;
296                         return 0;
297                 }
298                 return -EINVAL;
299         } else {
300                 struct cpufreq_governor *t;
301                 mutex_lock(&cpufreq_governor_mutex);
302                 if (!cpufreq_driver || !cpufreq_driver->target)
303                         goto out;
304                 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
305                         if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
306                                 *governor = t;
307                                 mutex_unlock(&cpufreq_governor_mutex);
308                                 return 0;
309                         }
310                 }
311         out:
312                 mutex_unlock(&cpufreq_governor_mutex);
313         }
314         return -EINVAL;
315 }
316 EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
317
318
319 /* drivers/base/cpu.c */
320 extern struct sysdev_class cpu_sysdev_class;
321
322
323 /**
324  * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
325  *
326  * Write out information from cpufreq_driver->policy[cpu]; object must be
327  * "unsigned int".
328  */
329
330 #define show_one(file_name, object)                                     \
331 static ssize_t show_##file_name                                         \
332 (struct cpufreq_policy * policy, char *buf)                             \
333 {                                                                       \
334         return sprintf (buf, "%u\n", policy->object);                   \
335 }
336
337 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
338 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
339 show_one(scaling_min_freq, min);
340 show_one(scaling_max_freq, max);
341 show_one(scaling_cur_freq, cur);
342
343 /**
344  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
345  */
346 #define store_one(file_name, object)                    \
347 static ssize_t store_##file_name                                        \
348 (struct cpufreq_policy * policy, const char *buf, size_t count)         \
349 {                                                                       \
350         unsigned int ret = -EINVAL;                                     \
351         struct cpufreq_policy new_policy;                               \
352                                                                         \
353         ret = cpufreq_get_policy(&new_policy, policy->cpu);             \
354         if (ret)                                                        \
355                 return -EINVAL;                                         \
356                                                                         \
357         ret = sscanf (buf, "%u", &new_policy.object);                   \
358         if (ret != 1)                                                   \
359                 return -EINVAL;                                         \
360                                                                         \
361         ret = cpufreq_set_policy(&new_policy);                          \
362                                                                         \
363         return ret ? ret : count;                                       \
364 }
365
366 store_one(scaling_min_freq,min);
367 store_one(scaling_max_freq,max);
368
369 /**
370  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
371  */
372 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
373 {
374         unsigned int cur_freq = cpufreq_get(policy->cpu);
375         if (!cur_freq)
376                 return sprintf(buf, "<unknown>");
377         return sprintf(buf, "%u\n", cur_freq);
378 }
379
380
381 /**
382  * show_scaling_governor - show the current policy for the specified CPU
383  */
384 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
385 {
386         if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
387                 return sprintf(buf, "powersave\n");
388         else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
389                 return sprintf(buf, "performance\n");
390         else if (policy->governor)
391                 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
392         return -EINVAL;
393 }
394
395
396 /**
397  * store_scaling_governor - store policy for the specified CPU
398  */
399 static ssize_t store_scaling_governor (struct cpufreq_policy * policy, 
400                                        const char *buf, size_t count) 
401 {
402         unsigned int ret = -EINVAL;
403         char    str_governor[16];
404         struct cpufreq_policy new_policy;
405
406         ret = cpufreq_get_policy(&new_policy, policy->cpu);
407         if (ret)
408                 return ret;
409
410         ret = sscanf (buf, "%15s", str_governor);
411         if (ret != 1)
412                 return -EINVAL;
413
414         if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
415                 return -EINVAL;
416
417         ret = cpufreq_set_policy(&new_policy);
418
419         return ret ? ret : count;
420 }
421
422 /**
423  * show_scaling_driver - show the cpufreq driver currently loaded
424  */
425 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
426 {
427         return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
428 }
429
430 /**
431  * show_scaling_available_governors - show the available CPUfreq governors
432  */
433 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
434                                 char *buf)
435 {
436         ssize_t i = 0;
437         struct cpufreq_governor *t;
438
439         if (!cpufreq_driver->target) {
440                 i += sprintf(buf, "performance powersave");
441                 goto out;
442         }
443
444         list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
445                 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
446                         goto out;
447                 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
448         }
449  out:
450         i += sprintf(&buf[i], "\n");
451         return i;
452 }
453 /**
454  * show_affected_cpus - show the CPUs affected by each transition
455  */
456 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
457 {
458         ssize_t i = 0;
459         unsigned int cpu;
460
461         for_each_cpu_mask(cpu, policy->cpus) {
462                 if (i)
463                         i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
464                 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
465                 if (i >= (PAGE_SIZE - 5))
466                     break;
467         }
468         i += sprintf(&buf[i], "\n");
469         return i;
470 }
471
472
473 #define define_one_ro(_name) \
474 static struct freq_attr _name = \
475 __ATTR(_name, 0444, show_##_name, NULL)
476
477 #define define_one_ro0400(_name) \
478 static struct freq_attr _name = \
479 __ATTR(_name, 0400, show_##_name, NULL)
480
481 #define define_one_rw(_name) \
482 static struct freq_attr _name = \
483 __ATTR(_name, 0644, show_##_name, store_##_name)
484
485 define_one_ro0400(cpuinfo_cur_freq);
486 define_one_ro(cpuinfo_min_freq);
487 define_one_ro(cpuinfo_max_freq);
488 define_one_ro(scaling_available_governors);
489 define_one_ro(scaling_driver);
490 define_one_ro(scaling_cur_freq);
491 define_one_ro(affected_cpus);
492 define_one_rw(scaling_min_freq);
493 define_one_rw(scaling_max_freq);
494 define_one_rw(scaling_governor);
495
496 static struct attribute * default_attrs[] = {
497         &cpuinfo_min_freq.attr,
498         &cpuinfo_max_freq.attr,
499         &scaling_min_freq.attr,
500         &scaling_max_freq.attr,
501         &affected_cpus.attr,
502         &scaling_governor.attr,
503         &scaling_driver.attr,
504         &scaling_available_governors.attr,
505         NULL
506 };
507
508 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
509 #define to_attr(a) container_of(a,struct freq_attr,attr)
510
511 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
512 {
513         struct cpufreq_policy * policy = to_policy(kobj);
514         struct freq_attr * fattr = to_attr(attr);
515         ssize_t ret;
516         policy = cpufreq_cpu_get(policy->cpu);
517         if (!policy)
518                 return -EINVAL;
519         ret = fattr->show ? fattr->show(policy,buf) : -EIO;
520         cpufreq_cpu_put(policy);
521         return ret;
522 }
523
524 static ssize_t store(struct kobject * kobj, struct attribute * attr, 
525                      const char * buf, size_t count)
526 {
527         struct cpufreq_policy * policy = to_policy(kobj);
528         struct freq_attr * fattr = to_attr(attr);
529         ssize_t ret;
530         policy = cpufreq_cpu_get(policy->cpu);
531         if (!policy)
532                 return -EINVAL;
533         ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
534         cpufreq_cpu_put(policy);
535         return ret;
536 }
537
538 static void cpufreq_sysfs_release(struct kobject * kobj)
539 {
540         struct cpufreq_policy * policy = to_policy(kobj);
541         dprintk("last reference is dropped\n");
542         complete(&policy->kobj_unregister);
543 }
544
545 static struct sysfs_ops sysfs_ops = {
546         .show   = show,
547         .store  = store,
548 };
549
550 static struct kobj_type ktype_cpufreq = {
551         .sysfs_ops      = &sysfs_ops,
552         .default_attrs  = default_attrs,
553         .release        = cpufreq_sysfs_release,
554 };
555
556
557 /**
558  * cpufreq_add_dev - add a CPU device
559  *
560  * Adds the cpufreq interface for a CPU device. 
561  */
562 static int cpufreq_add_dev (struct sys_device * sys_dev)
563 {
564         unsigned int cpu = sys_dev->id;
565         int ret = 0;
566         struct cpufreq_policy new_policy;
567         struct cpufreq_policy *policy;
568         struct freq_attr **drv_attr;
569         unsigned long flags;
570         unsigned int j;
571
572         if (cpu_is_offline(cpu))
573                 return 0;
574
575         cpufreq_debug_disable_ratelimit();
576         dprintk("adding CPU %u\n", cpu);
577
578 #ifdef CONFIG_SMP
579         /* check whether a different CPU already registered this
580          * CPU because it is in the same boat. */
581         policy = cpufreq_cpu_get(cpu);
582         if (unlikely(policy)) {
583                 dprintk("CPU already managed, adding link\n");
584                 sysfs_create_link(&sys_dev->kobj, &policy->kobj, "cpufreq");
585                 cpufreq_debug_enable_ratelimit();
586                 return 0;
587         }
588 #endif
589
590         if (!try_module_get(cpufreq_driver->owner)) {
591                 ret = -EINVAL;
592                 goto module_out;
593         }
594
595         policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
596         if (!policy) {
597                 ret = -ENOMEM;
598                 goto nomem_out;
599         }
600
601         policy->cpu = cpu;
602         policy->cpus = cpumask_of_cpu(cpu);
603
604         mutex_init(&policy->lock);
605         mutex_lock(&policy->lock);
606         init_completion(&policy->kobj_unregister);
607         INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
608
609         /* call driver. From then on the cpufreq must be able
610          * to accept all calls to ->verify and ->setpolicy for this CPU
611          */
612         ret = cpufreq_driver->init(policy);
613         if (ret) {
614                 dprintk("initialization failed\n");
615                 goto err_out;
616         }
617
618         memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
619
620         /* prepare interface data */
621         policy->kobj.parent = &sys_dev->kobj;
622         policy->kobj.ktype = &ktype_cpufreq;
623         strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
624
625         ret = kobject_register(&policy->kobj);
626         if (ret)
627                 goto err_out_driver_exit;
628
629         /* set up files for this cpu device */
630         drv_attr = cpufreq_driver->attr;
631         while ((drv_attr) && (*drv_attr)) {
632                 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
633                 drv_attr++;
634         }
635         if (cpufreq_driver->get)
636                 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
637         if (cpufreq_driver->target)
638                 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
639
640         spin_lock_irqsave(&cpufreq_driver_lock, flags);
641         for_each_cpu_mask(j, policy->cpus)
642                 cpufreq_cpu_data[j] = policy;
643         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
644         policy->governor = NULL; /* to assure that the starting sequence is
645                                   * run in cpufreq_set_policy */
646         mutex_unlock(&policy->lock);
647         
648         /* set default policy */
649         
650         ret = cpufreq_set_policy(&new_policy);
651         if (ret) {
652                 dprintk("setting policy failed\n");
653                 goto err_out_unregister;
654         }
655
656         module_put(cpufreq_driver->owner);
657         dprintk("initialization complete\n");
658         cpufreq_debug_enable_ratelimit();
659         
660         return 0;
661
662
663 err_out_unregister:
664         spin_lock_irqsave(&cpufreq_driver_lock, flags);
665         for_each_cpu_mask(j, policy->cpus)
666                 cpufreq_cpu_data[j] = NULL;
667         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
668
669         kobject_unregister(&policy->kobj);
670         wait_for_completion(&policy->kobj_unregister);
671
672 err_out_driver_exit:
673         if (cpufreq_driver->exit)
674                 cpufreq_driver->exit(policy);
675
676 err_out:
677         kfree(policy);
678
679 nomem_out:
680         module_put(cpufreq_driver->owner);
681 module_out:
682         cpufreq_debug_enable_ratelimit();
683         return ret;
684 }
685
686
687 /**
688  * cpufreq_remove_dev - remove a CPU device
689  *
690  * Removes the cpufreq interface for a CPU device.
691  */
692 static int cpufreq_remove_dev (struct sys_device * sys_dev)
693 {
694         unsigned int cpu = sys_dev->id;
695         unsigned long flags;
696         struct cpufreq_policy *data;
697 #ifdef CONFIG_SMP
698         struct sys_device *cpu_sys_dev;
699         unsigned int j;
700 #endif
701
702         cpufreq_debug_disable_ratelimit();
703         dprintk("unregistering CPU %u\n", cpu);
704
705         spin_lock_irqsave(&cpufreq_driver_lock, flags);
706         data = cpufreq_cpu_data[cpu];
707
708         if (!data) {
709                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
710                 cpufreq_debug_enable_ratelimit();
711                 return -EINVAL;
712         }
713         cpufreq_cpu_data[cpu] = NULL;
714
715
716 #ifdef CONFIG_SMP
717         /* if this isn't the CPU which is the parent of the kobj, we
718          * only need to unlink, put and exit 
719          */
720         if (unlikely(cpu != data->cpu)) {
721                 dprintk("removing link\n");
722                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
723                 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
724                 cpufreq_cpu_put(data);
725                 cpufreq_debug_enable_ratelimit();
726                 return 0;
727         }
728 #endif
729
730
731         if (!kobject_get(&data->kobj)) {
732                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
733                 cpufreq_debug_enable_ratelimit();
734                 return -EFAULT;
735         }
736
737 #ifdef CONFIG_SMP
738         /* if we have other CPUs still registered, we need to unlink them,
739          * or else wait_for_completion below will lock up. Clean the
740          * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
741          * links afterwards.
742          */
743         if (unlikely(cpus_weight(data->cpus) > 1)) {
744                 for_each_cpu_mask(j, data->cpus) {
745                         if (j == cpu)
746                                 continue;
747                         cpufreq_cpu_data[j] = NULL;
748                 }
749         }
750
751         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
752
753         if (unlikely(cpus_weight(data->cpus) > 1)) {
754                 for_each_cpu_mask(j, data->cpus) {
755                         if (j == cpu)
756                                 continue;
757                         dprintk("removing link for cpu %u\n", j);
758                         cpu_sys_dev = get_cpu_sysdev(j);
759                         sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
760                         cpufreq_cpu_put(data);
761                 }
762         }
763 #else
764         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
765 #endif
766
767         mutex_lock(&data->lock);
768         if (cpufreq_driver->target)
769                 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
770         mutex_unlock(&data->lock);
771
772         kobject_unregister(&data->kobj);
773
774         kobject_put(&data->kobj);
775
776         /* we need to make sure that the underlying kobj is actually
777          * not referenced anymore by anybody before we proceed with 
778          * unloading.
779          */
780         dprintk("waiting for dropping of refcount\n");
781         wait_for_completion(&data->kobj_unregister);
782         dprintk("wait complete\n");
783
784         if (cpufreq_driver->exit)
785                 cpufreq_driver->exit(data);
786
787         kfree(data);
788
789         cpufreq_debug_enable_ratelimit();
790
791         return 0;
792 }
793
794
795 static void handle_update(void *data)
796 {
797         unsigned int cpu = (unsigned int)(long)data;
798         dprintk("handle_update for cpu %u called\n", cpu);
799         cpufreq_update_policy(cpu);
800 }
801
802 /**
803  *      cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
804  *      @cpu: cpu number
805  *      @old_freq: CPU frequency the kernel thinks the CPU runs at
806  *      @new_freq: CPU frequency the CPU actually runs at
807  *
808  *      We adjust to current frequency first, and need to clean up later. So either call
809  *      to cpufreq_update_policy() or schedule handle_update()).
810  */
811 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
812 {
813         struct cpufreq_freqs freqs;
814
815         dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
816                "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
817
818         freqs.cpu = cpu;
819         freqs.old = old_freq;
820         freqs.new = new_freq;
821         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
822         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
823 }
824
825
826 /** 
827  * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
828  * @cpu: CPU number
829  *
830  * This is the last known freq, without actually getting it from the driver.
831  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
832  */
833 unsigned int cpufreq_quick_get(unsigned int cpu)
834 {
835         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
836         unsigned int ret = 0;
837
838         if (policy) {
839                 mutex_lock(&policy->lock);
840                 ret = policy->cur;
841                 mutex_unlock(&policy->lock);
842                 cpufreq_cpu_put(policy);
843         }
844
845         return (ret);
846 }
847 EXPORT_SYMBOL(cpufreq_quick_get);
848
849
850 /** 
851  * cpufreq_get - get the current CPU frequency (in kHz)
852  * @cpu: CPU number
853  *
854  * Get the CPU current (static) CPU frequency
855  */
856 unsigned int cpufreq_get(unsigned int cpu)
857 {
858         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
859         unsigned int ret = 0;
860
861         if (!policy)
862                 return 0;
863
864         if (!cpufreq_driver->get)
865                 goto out;
866
867         mutex_lock(&policy->lock);
868
869         ret = cpufreq_driver->get(cpu);
870
871         if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) 
872         {
873                 /* verify no discrepancy between actual and saved value exists */
874                 if (unlikely(ret != policy->cur)) {
875                         cpufreq_out_of_sync(cpu, policy->cur, ret);
876                         schedule_work(&policy->update);
877                 }
878         }
879
880         mutex_unlock(&policy->lock);
881
882  out:
883         cpufreq_cpu_put(policy);
884
885         return (ret);
886 }
887 EXPORT_SYMBOL(cpufreq_get);
888
889
890 /**
891  *      cpufreq_suspend - let the low level driver prepare for suspend
892  */
893
894 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
895 {
896         int cpu = sysdev->id;
897         unsigned int ret = 0;
898         unsigned int cur_freq = 0;
899         struct cpufreq_policy *cpu_policy;
900
901         dprintk("resuming cpu %u\n", cpu);
902
903         if (!cpu_online(cpu))
904                 return 0;
905
906         /* we may be lax here as interrupts are off. Nonetheless
907          * we need to grab the correct cpu policy, as to check
908          * whether we really run on this CPU.
909          */
910
911         cpu_policy = cpufreq_cpu_get(cpu);
912         if (!cpu_policy)
913                 return -EINVAL;
914
915         /* only handle each CPU group once */
916         if (unlikely(cpu_policy->cpu != cpu)) {
917                 cpufreq_cpu_put(cpu_policy);
918                 return 0;
919         }
920
921         if (cpufreq_driver->suspend) {
922                 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
923                 if (ret) {
924                         printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
925                                         "step on CPU %u\n", cpu_policy->cpu);
926                         cpufreq_cpu_put(cpu_policy);
927                         return ret;
928                 }
929         }
930
931
932         if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
933                 goto out;
934
935         if (cpufreq_driver->get)
936                 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
937
938         if (!cur_freq || !cpu_policy->cur) {
939                 printk(KERN_ERR "cpufreq: suspend failed to assert current "
940                        "frequency is what timing core thinks it is.\n");
941                 goto out;
942         }
943
944         if (unlikely(cur_freq != cpu_policy->cur)) {
945                 struct cpufreq_freqs freqs;
946
947                 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
948                         dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
949                                "cpufreq assumed %u kHz.\n",
950                                cur_freq, cpu_policy->cur);
951
952                 freqs.cpu = cpu;
953                 freqs.old = cpu_policy->cur;
954                 freqs.new = cur_freq;
955
956                 notifier_call_chain(&cpufreq_transition_notifier_list,
957                                     CPUFREQ_SUSPENDCHANGE, &freqs);
958                 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
959
960                 cpu_policy->cur = cur_freq;
961         }
962
963  out:
964         cpufreq_cpu_put(cpu_policy);
965         return 0;
966 }
967
968 /**
969  *      cpufreq_resume -  restore proper CPU frequency handling after resume
970  *
971  *      1.) resume CPUfreq hardware support (cpufreq_driver->resume())
972  *      2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
973  *      3.) schedule call cpufreq_update_policy() ASAP as interrupts are
974  *          restored.
975  */
976 static int cpufreq_resume(struct sys_device * sysdev)
977 {
978         int cpu = sysdev->id;
979         unsigned int ret = 0;
980         struct cpufreq_policy *cpu_policy;
981
982         dprintk("resuming cpu %u\n", cpu);
983
984         if (!cpu_online(cpu))
985                 return 0;
986
987         /* we may be lax here as interrupts are off. Nonetheless
988          * we need to grab the correct cpu policy, as to check
989          * whether we really run on this CPU.
990          */
991
992         cpu_policy = cpufreq_cpu_get(cpu);
993         if (!cpu_policy)
994                 return -EINVAL;
995
996         /* only handle each CPU group once */
997         if (unlikely(cpu_policy->cpu != cpu)) {
998                 cpufreq_cpu_put(cpu_policy);
999                 return 0;
1000         }
1001
1002         if (cpufreq_driver->resume) {
1003                 ret = cpufreq_driver->resume(cpu_policy);
1004                 if (ret) {
1005                         printk(KERN_ERR "cpufreq: resume failed in ->resume "
1006                                         "step on CPU %u\n", cpu_policy->cpu);
1007                         cpufreq_cpu_put(cpu_policy);
1008                         return ret;
1009                 }
1010         }
1011
1012         if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1013                 unsigned int cur_freq = 0;
1014
1015                 if (cpufreq_driver->get)
1016                         cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1017
1018                 if (!cur_freq || !cpu_policy->cur) {
1019                         printk(KERN_ERR "cpufreq: resume failed to assert "
1020                                         "current frequency is what timing core "
1021                                         "thinks it is.\n");
1022                         goto out;
1023                 }
1024
1025                 if (unlikely(cur_freq != cpu_policy->cur)) {
1026                         struct cpufreq_freqs freqs;
1027
1028                         if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1029                                 dprintk(KERN_WARNING "Warning: CPU frequency"
1030                                        "is %u, cpufreq assumed %u kHz.\n",
1031                                        cur_freq, cpu_policy->cur);
1032
1033                         freqs.cpu = cpu;
1034                         freqs.old = cpu_policy->cur;
1035                         freqs.new = cur_freq;
1036
1037                         notifier_call_chain(&cpufreq_transition_notifier_list,
1038                                         CPUFREQ_RESUMECHANGE, &freqs);
1039                         adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1040
1041                         cpu_policy->cur = cur_freq;
1042                 }
1043         }
1044
1045 out:
1046         schedule_work(&cpu_policy->update);
1047         cpufreq_cpu_put(cpu_policy);
1048         return ret;
1049 }
1050
1051 static struct sysdev_driver cpufreq_sysdev_driver = {
1052         .add            = cpufreq_add_dev,
1053         .remove         = cpufreq_remove_dev,
1054         .suspend        = cpufreq_suspend,
1055         .resume         = cpufreq_resume,
1056 };
1057
1058
1059 /*********************************************************************
1060  *                     NOTIFIER LISTS INTERFACE                      *
1061  *********************************************************************/
1062
1063 /**
1064  *      cpufreq_register_notifier - register a driver with cpufreq
1065  *      @nb: notifier function to register
1066  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1067  *
1068  *      Add a driver to one of two lists: either a list of drivers that 
1069  *      are notified about clock rate changes (once before and once after
1070  *      the transition), or a list of drivers that are notified about
1071  *      changes in cpufreq policy.
1072  *
1073  *      This function may sleep, and has the same return conditions as
1074  *      notifier_chain_register.
1075  */
1076 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1077 {
1078         int ret;
1079
1080         down_write(&cpufreq_notifier_rwsem);
1081         switch (list) {
1082         case CPUFREQ_TRANSITION_NOTIFIER:
1083                 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
1084                 break;
1085         case CPUFREQ_POLICY_NOTIFIER:
1086                 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
1087                 break;
1088         default:
1089                 ret = -EINVAL;
1090         }
1091         up_write(&cpufreq_notifier_rwsem);
1092
1093         return ret;
1094 }
1095 EXPORT_SYMBOL(cpufreq_register_notifier);
1096
1097
1098 /**
1099  *      cpufreq_unregister_notifier - unregister a driver with cpufreq
1100  *      @nb: notifier block to be unregistered
1101  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1102  *
1103  *      Remove a driver from the CPU frequency notifier list.
1104  *
1105  *      This function may sleep, and has the same return conditions as
1106  *      notifier_chain_unregister.
1107  */
1108 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1109 {
1110         int ret;
1111
1112         down_write(&cpufreq_notifier_rwsem);
1113         switch (list) {
1114         case CPUFREQ_TRANSITION_NOTIFIER:
1115                 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
1116                 break;
1117         case CPUFREQ_POLICY_NOTIFIER:
1118                 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
1119                 break;
1120         default:
1121                 ret = -EINVAL;
1122         }
1123         up_write(&cpufreq_notifier_rwsem);
1124
1125         return ret;
1126 }
1127 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1128
1129
1130 /*********************************************************************
1131  *                              GOVERNORS                            *
1132  *********************************************************************/
1133
1134
1135 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1136                             unsigned int target_freq,
1137                             unsigned int relation)
1138 {
1139         int retval = -EINVAL;
1140
1141         lock_cpu_hotplug();
1142         dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1143                 target_freq, relation);
1144         if (cpu_online(policy->cpu) && cpufreq_driver->target)
1145                 retval = cpufreq_driver->target(policy, target_freq, relation);
1146
1147         unlock_cpu_hotplug();
1148
1149         return retval;
1150 }
1151 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1152
1153 int cpufreq_driver_target(struct cpufreq_policy *policy,
1154                           unsigned int target_freq,
1155                           unsigned int relation)
1156 {
1157         int ret;
1158
1159         policy = cpufreq_cpu_get(policy->cpu);
1160         if (!policy)
1161                 return -EINVAL;
1162
1163         mutex_lock(&policy->lock);
1164
1165         ret = __cpufreq_driver_target(policy, target_freq, relation);
1166
1167         mutex_unlock(&policy->lock);
1168
1169         cpufreq_cpu_put(policy);
1170
1171         return ret;
1172 }
1173 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1174
1175
1176 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1177 {
1178         int ret;
1179
1180         if (!try_module_get(policy->governor->owner))
1181                 return -EINVAL;
1182
1183         dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1184         ret = policy->governor->governor(policy, event);
1185
1186         /* we keep one module reference alive for each CPU governed by this CPU */
1187         if ((event != CPUFREQ_GOV_START) || ret)
1188                 module_put(policy->governor->owner);
1189         if ((event == CPUFREQ_GOV_STOP) && !ret)
1190                 module_put(policy->governor->owner);
1191
1192         return ret;
1193 }
1194
1195
1196 int cpufreq_governor(unsigned int cpu, unsigned int event)
1197 {
1198         int ret = 0;
1199         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1200
1201         if (!policy)
1202                 return -EINVAL;
1203
1204         mutex_lock(&policy->lock);
1205         ret = __cpufreq_governor(policy, event);
1206         mutex_unlock(&policy->lock);
1207
1208         cpufreq_cpu_put(policy);
1209
1210         return ret;
1211 }
1212 EXPORT_SYMBOL_GPL(cpufreq_governor);
1213
1214
1215 int cpufreq_register_governor(struct cpufreq_governor *governor)
1216 {
1217         struct cpufreq_governor *t;
1218
1219         if (!governor)
1220                 return -EINVAL;
1221
1222         mutex_lock(&cpufreq_governor_mutex);
1223         
1224         list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1225                 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1226                         mutex_unlock(&cpufreq_governor_mutex);
1227                         return -EBUSY;
1228                 }
1229         }
1230         list_add(&governor->governor_list, &cpufreq_governor_list);
1231
1232         mutex_unlock(&cpufreq_governor_mutex);
1233
1234         return 0;
1235 }
1236 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1237
1238
1239 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1240 {
1241         if (!governor)
1242                 return;
1243
1244         mutex_lock(&cpufreq_governor_mutex);
1245         list_del(&governor->governor_list);
1246         mutex_unlock(&cpufreq_governor_mutex);
1247         return;
1248 }
1249 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1250
1251
1252
1253 /*********************************************************************
1254  *                          POLICY INTERFACE                         *
1255  *********************************************************************/
1256
1257 /**
1258  * cpufreq_get_policy - get the current cpufreq_policy
1259  * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1260  *
1261  * Reads the current cpufreq policy.
1262  */
1263 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1264 {
1265         struct cpufreq_policy *cpu_policy;
1266         if (!policy)
1267                 return -EINVAL;
1268
1269         cpu_policy = cpufreq_cpu_get(cpu);
1270         if (!cpu_policy)
1271                 return -EINVAL;
1272
1273         mutex_lock(&cpu_policy->lock);
1274         memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1275         mutex_unlock(&cpu_policy->lock);
1276
1277         cpufreq_cpu_put(cpu_policy);
1278
1279         return 0;
1280 }
1281 EXPORT_SYMBOL(cpufreq_get_policy);
1282
1283
1284 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1285 {
1286         int ret = 0;
1287
1288         cpufreq_debug_disable_ratelimit();
1289         dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1290                 policy->min, policy->max);
1291
1292         memcpy(&policy->cpuinfo, 
1293                &data->cpuinfo, 
1294                sizeof(struct cpufreq_cpuinfo));
1295
1296         /* verify the cpu speed can be set within this limit */
1297         ret = cpufreq_driver->verify(policy);
1298         if (ret)
1299                 goto error_out;
1300
1301         down_read(&cpufreq_notifier_rwsem);
1302
1303         /* adjust if necessary - all reasons */
1304         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
1305                             policy);
1306
1307         /* adjust if necessary - hardware incompatibility*/
1308         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
1309                             policy);
1310
1311         /* verify the cpu speed can be set within this limit,
1312            which might be different to the first one */
1313         ret = cpufreq_driver->verify(policy);
1314         if (ret) {
1315                 up_read(&cpufreq_notifier_rwsem);
1316                 goto error_out;
1317         }
1318
1319         /* notification of the new policy */
1320         notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
1321                             policy);
1322
1323         up_read(&cpufreq_notifier_rwsem);
1324
1325         data->min    = policy->min;
1326         data->max    = policy->max;
1327
1328         dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1329
1330         if (cpufreq_driver->setpolicy) {
1331                 data->policy = policy->policy;
1332                 dprintk("setting range\n");
1333                 ret = cpufreq_driver->setpolicy(policy);
1334         } else {
1335                 if (policy->governor != data->governor) {
1336                         /* save old, working values */
1337                         struct cpufreq_governor *old_gov = data->governor;
1338
1339                         dprintk("governor switch\n");
1340
1341                         /* end old governor */
1342                         if (data->governor)
1343                                 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1344
1345                         /* start new governor */
1346                         data->governor = policy->governor;
1347                         if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1348                                 /* new governor failed, so re-start old one */
1349                                 dprintk("starting governor %s failed\n", data->governor->name);
1350                                 if (old_gov) {
1351                                         data->governor = old_gov;
1352                                         __cpufreq_governor(data, CPUFREQ_GOV_START);
1353                                 }
1354                                 ret = -EINVAL;
1355                                 goto error_out;
1356                         }
1357                         /* might be a policy change, too, so fall through */
1358                 }
1359                 dprintk("governor: change or update limits\n");
1360                 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1361         }
1362
1363  error_out:
1364         cpufreq_debug_enable_ratelimit();
1365         return ret;
1366 }
1367
1368 /**
1369  *      cpufreq_set_policy - set a new CPUFreq policy
1370  *      @policy: policy to be set.
1371  *
1372  *      Sets a new CPU frequency and voltage scaling policy.
1373  */
1374 int cpufreq_set_policy(struct cpufreq_policy *policy)
1375 {
1376         int ret = 0;
1377         struct cpufreq_policy *data;
1378
1379         if (!policy)
1380                 return -EINVAL;
1381
1382         data = cpufreq_cpu_get(policy->cpu);
1383         if (!data)
1384                 return -EINVAL;
1385
1386         /* lock this CPU */
1387         mutex_lock(&data->lock);
1388
1389         ret = __cpufreq_set_policy(data, policy);
1390         data->user_policy.min = data->min;
1391         data->user_policy.max = data->max;
1392         data->user_policy.policy = data->policy;
1393         data->user_policy.governor = data->governor;
1394
1395         mutex_unlock(&data->lock);
1396         cpufreq_cpu_put(data);
1397
1398         return ret;
1399 }
1400 EXPORT_SYMBOL(cpufreq_set_policy);
1401
1402
1403 /**
1404  *      cpufreq_update_policy - re-evaluate an existing cpufreq policy
1405  *      @cpu: CPU which shall be re-evaluated
1406  *
1407  *      Usefull for policy notifiers which have different necessities
1408  *      at different times.
1409  */
1410 int cpufreq_update_policy(unsigned int cpu)
1411 {
1412         struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1413         struct cpufreq_policy policy;
1414         int ret = 0;
1415
1416         if (!data)
1417                 return -ENODEV;
1418
1419         mutex_lock(&data->lock);
1420
1421         dprintk("updating policy for CPU %u\n", cpu);
1422         memcpy(&policy, 
1423                data,
1424                sizeof(struct cpufreq_policy));
1425         policy.min = data->user_policy.min;
1426         policy.max = data->user_policy.max;
1427         policy.policy = data->user_policy.policy;
1428         policy.governor = data->user_policy.governor;
1429
1430         ret = __cpufreq_set_policy(data, &policy);
1431
1432         mutex_unlock(&data->lock);
1433
1434         cpufreq_cpu_put(data);
1435         return ret;
1436 }
1437 EXPORT_SYMBOL(cpufreq_update_policy);
1438
1439 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1440                                         unsigned long action, void *hcpu)
1441 {
1442         unsigned int cpu = (unsigned long)hcpu;
1443         struct cpufreq_policy *policy;
1444         struct sys_device *sys_dev;
1445
1446         sys_dev = get_cpu_sysdev(cpu);
1447
1448         if (sys_dev) {
1449                 switch (action) {
1450                 case CPU_ONLINE:
1451                         cpufreq_add_dev(sys_dev);
1452                         break;
1453                 case CPU_DOWN_PREPARE:
1454                         /*
1455                          * We attempt to put this cpu in lowest frequency
1456                          * possible before going down. This will permit
1457                          * hardware-managed P-State to switch other related
1458                          * threads to min or higher speeds if possible.
1459                          */
1460                         policy = cpufreq_cpu_data[cpu];
1461                         if (policy) {
1462                                 cpufreq_driver_target(policy, policy->min,
1463                                                 CPUFREQ_RELATION_H);
1464                         }
1465                         break;
1466                 case CPU_DEAD:
1467                         cpufreq_remove_dev(sys_dev);
1468                         break;
1469                 }
1470         }
1471         return NOTIFY_OK;
1472 }
1473
1474 static struct notifier_block cpufreq_cpu_notifier =
1475 {
1476     .notifier_call = cpufreq_cpu_callback,
1477 };
1478
1479 /*********************************************************************
1480  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
1481  *********************************************************************/
1482
1483 /**
1484  * cpufreq_register_driver - register a CPU Frequency driver
1485  * @driver_data: A struct cpufreq_driver containing the values#
1486  * submitted by the CPU Frequency driver.
1487  *
1488  *   Registers a CPU Frequency driver to this core code. This code 
1489  * returns zero on success, -EBUSY when another driver got here first
1490  * (and isn't unregistered in the meantime). 
1491  *
1492  */
1493 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1494 {
1495         unsigned long flags;
1496         int ret;
1497
1498         if (!driver_data || !driver_data->verify || !driver_data->init ||
1499             ((!driver_data->setpolicy) && (!driver_data->target)))
1500                 return -EINVAL;
1501
1502         dprintk("trying to register driver %s\n", driver_data->name);
1503
1504         if (driver_data->setpolicy)
1505                 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1506
1507         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1508         if (cpufreq_driver) {
1509                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1510                 return -EBUSY;
1511         }
1512         cpufreq_driver = driver_data;
1513         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1514
1515         ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1516
1517         if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1518                 int i;
1519                 ret = -ENODEV;
1520
1521                 /* check for at least one working CPU */
1522                 for (i=0; i<NR_CPUS; i++)
1523                         if (cpufreq_cpu_data[i])
1524                                 ret = 0;
1525
1526                 /* if all ->init() calls failed, unregister */
1527                 if (ret) {
1528                         dprintk("no CPU initialized for driver %s\n", driver_data->name);
1529                         sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1530
1531                         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1532                         cpufreq_driver = NULL;
1533                         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1534                 }
1535         }
1536
1537         if (!ret) {
1538                 register_cpu_notifier(&cpufreq_cpu_notifier);
1539                 dprintk("driver %s up and running\n", driver_data->name);
1540                 cpufreq_debug_enable_ratelimit();
1541         }
1542
1543         return (ret);
1544 }
1545 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1546
1547
1548 /**
1549  * cpufreq_unregister_driver - unregister the current CPUFreq driver
1550  *
1551  *    Unregister the current CPUFreq driver. Only call this if you have 
1552  * the right to do so, i.e. if you have succeeded in initialising before!
1553  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1554  * currently not initialised.
1555  */
1556 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1557 {
1558         unsigned long flags;
1559
1560         cpufreq_debug_disable_ratelimit();
1561
1562         if (!cpufreq_driver || (driver != cpufreq_driver)) {
1563                 cpufreq_debug_enable_ratelimit();
1564                 return -EINVAL;
1565         }
1566
1567         dprintk("unregistering driver %s\n", driver->name);
1568
1569         sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1570         unregister_cpu_notifier(&cpufreq_cpu_notifier);
1571
1572         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1573         cpufreq_driver = NULL;
1574         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1575
1576         return 0;
1577 }
1578 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);