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