[PATCH] mm: dma32 zone statistics
[linux-2.6.22.y-op.git] / drivers / cpufreq / cpufreq.c
bloba9163d02983af68f069887a520330e84470cc58b
1 /*
2 * linux/drivers/cpufreq/cpufreq.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
8 * Added handling for CPU hotplug
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.
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>
30 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
32 /**
33 * The "cpufreq driver" - the arch- or hardware-dependend low
34 * level driver of CPUFreq support, and its spinlock. This lock
35 * also protects the cpufreq_cpu_data array.
37 static struct cpufreq_driver *cpufreq_driver;
38 static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
39 static DEFINE_SPINLOCK(cpufreq_driver_lock);
41 /* internal prototypes */
42 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
43 static void handle_update(void *data);
44 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci);
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.
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);
58 static LIST_HEAD(cpufreq_governor_list);
59 static DECLARE_MUTEX (cpufreq_governor_sem);
61 struct cpufreq_policy * cpufreq_cpu_get(unsigned int cpu)
63 struct cpufreq_policy *data;
64 unsigned long flags;
66 if (cpu >= NR_CPUS)
67 goto err_out;
69 /* get the cpufreq driver */
70 spin_lock_irqsave(&cpufreq_driver_lock, flags);
72 if (!cpufreq_driver)
73 goto err_out_unlock;
75 if (!try_module_get(cpufreq_driver->owner))
76 goto err_out_unlock;
79 /* get the CPU */
80 data = cpufreq_cpu_data[cpu];
82 if (!data)
83 goto err_out_put_module;
85 if (!kobject_get(&data->kobj))
86 goto err_out_put_module;
89 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
91 return data;
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;
100 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
102 void cpufreq_cpu_put(struct cpufreq_policy *data)
104 kobject_put(&data->kobj);
105 module_put(cpufreq_driver->owner);
107 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
110 /*********************************************************************
111 * UNIFIED DEBUG HELPERS *
112 *********************************************************************/
113 #ifdef CONFIG_CPU_FREQ_DEBUG
115 /* what part(s) of the CPUfreq subsystem are debugged? */
116 static unsigned int debug;
118 /* is the debug output ratelimit'ed using printk_ratelimit? User can
119 * set or modify this value.
121 static unsigned int debug_ratelimit = 1;
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
127 static unsigned int disable_ratelimit = 1;
128 static DEFINE_SPINLOCK(disable_ratelimit_lock);
130 static inline void cpufreq_debug_enable_ratelimit(void)
132 unsigned long flags;
134 spin_lock_irqsave(&disable_ratelimit_lock, flags);
135 if (disable_ratelimit)
136 disable_ratelimit--;
137 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
140 static inline void cpufreq_debug_disable_ratelimit(void)
142 unsigned long flags;
144 spin_lock_irqsave(&disable_ratelimit_lock, flags);
145 disable_ratelimit++;
146 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
149 void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
151 char s[256];
152 va_list args;
153 unsigned int len;
154 unsigned long flags;
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;
163 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
165 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
167 va_start(args, fmt);
168 len += vsnprintf(&s[len], (256 - len), fmt, args);
169 va_end(args);
171 printk(s);
173 WARN_ON(len < 5);
176 EXPORT_SYMBOL(cpufreq_debug_printk);
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.");
182 module_param(debug_ratelimit, uint, 0644);
183 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
185 #else /* !CONFIG_CPU_FREQ_DEBUG */
187 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
188 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
190 #endif /* CONFIG_CPU_FREQ_DEBUG */
193 /*********************************************************************
194 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
195 *********************************************************************/
198 * adjust_jiffies - adjust the system "loops_per_jiffy"
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.
205 #ifndef CONFIG_SMP
206 static unsigned long l_p_j_ref;
207 static unsigned int l_p_j_ref_freq;
209 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
211 if (ci->flags & CPUFREQ_CONST_LOOPS)
212 return;
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);
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);
226 #else
227 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
228 #endif
232 * cpufreq_notify_transition - call notifier chain and adjust_jiffies on frequency transition
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.
237 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
239 BUG_ON(irqs_disabled());
241 freqs->flags = cpufreq_driver->flags;
242 dprintk("notification %u of frequency transition to %u kHz\n", state, freqs->new);
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".
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)))
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;
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;
272 up_read(&cpufreq_notifier_rwsem);
274 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
278 /*********************************************************************
279 * SYSFS INTERFACE *
280 *********************************************************************/
283 * cpufreq_parse_governor - parse a governor string
285 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
286 struct cpufreq_governor **governor)
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;
298 return -EINVAL;
299 } else {
300 struct cpufreq_governor *t;
301 down(&cpufreq_governor_sem);
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 up(&cpufreq_governor_sem);
308 return 0;
311 out:
312 up(&cpufreq_governor_sem);
314 return -EINVAL;
316 EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
319 /* drivers/base/cpu.c */
320 extern struct sysdev_class cpu_sysdev_class;
324 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
326 * Write out information from cpufreq_driver->policy[cpu]; object must be
327 * "unsigned int".
330 #define show_one(file_name, object) \
331 static ssize_t show_##file_name \
332 (struct cpufreq_policy * policy, char *buf) \
334 return sprintf (buf, "%u\n", policy->object); \
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);
344 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
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) \
350 unsigned int ret = -EINVAL; \
351 struct cpufreq_policy new_policy; \
353 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
354 if (ret) \
355 return -EINVAL; \
357 ret = sscanf (buf, "%u", &new_policy.object); \
358 if (ret != 1) \
359 return -EINVAL; \
361 ret = cpufreq_set_policy(&new_policy); \
363 return ret ? ret : count; \
366 store_one(scaling_min_freq,min);
367 store_one(scaling_max_freq,max);
370 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
372 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
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);
382 * show_scaling_governor - show the current policy for the specified CPU
384 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
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;
397 * store_scaling_governor - store policy for the specified CPU
399 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
400 const char *buf, size_t count)
402 unsigned int ret = -EINVAL;
403 char str_governor[16];
404 struct cpufreq_policy new_policy;
406 ret = cpufreq_get_policy(&new_policy, policy->cpu);
407 if (ret)
408 return ret;
410 ret = sscanf (buf, "%15s", str_governor);
411 if (ret != 1)
412 return -EINVAL;
414 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
415 return -EINVAL;
417 ret = cpufreq_set_policy(&new_policy);
419 return ret ? ret : count;
423 * show_scaling_driver - show the cpufreq driver currently loaded
425 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
427 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
431 * show_scaling_available_governors - show the available CPUfreq governors
433 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
434 char *buf)
436 ssize_t i = 0;
437 struct cpufreq_governor *t;
439 if (!cpufreq_driver->target) {
440 i += sprintf(buf, "performance powersave");
441 goto out;
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);
449 out:
450 i += sprintf(&buf[i], "\n");
451 return i;
454 * show_affected_cpus - show the CPUs affected by each transition
456 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
458 ssize_t i = 0;
459 unsigned int cpu;
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;
468 i += sprintf(&buf[i], "\n");
469 return i;
473 #define define_one_ro(_name) \
474 static struct freq_attr _name = \
475 __ATTR(_name, 0444, show_##_name, NULL)
477 #define define_one_ro0400(_name) \
478 static struct freq_attr _name = \
479 __ATTR(_name, 0400, show_##_name, NULL)
481 #define define_one_rw(_name) \
482 static struct freq_attr _name = \
483 __ATTR(_name, 0644, show_##_name, store_##_name)
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);
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
508 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
509 #define to_attr(a) container_of(a,struct freq_attr,attr)
511 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
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;
524 static ssize_t store(struct kobject * kobj, struct attribute * attr,
525 const char * buf, size_t count)
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;
538 static void cpufreq_sysfs_release(struct kobject * kobj)
540 struct cpufreq_policy * policy = to_policy(kobj);
541 dprintk("last reference is dropped\n");
542 complete(&policy->kobj_unregister);
545 static struct sysfs_ops sysfs_ops = {
546 .show = show,
547 .store = store,
550 static struct kobj_type ktype_cpufreq = {
551 .sysfs_ops = &sysfs_ops,
552 .default_attrs = default_attrs,
553 .release = cpufreq_sysfs_release,
558 * cpufreq_add_dev - add a CPU device
560 * Adds the cpufreq interface for a CPU device.
562 static int cpufreq_add_dev (struct sys_device * sys_dev)
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;
572 if (cpu_is_offline(cpu))
573 return 0;
575 cpufreq_debug_disable_ratelimit();
576 dprintk("adding CPU %u\n", cpu);
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;
588 #endif
590 if (!try_module_get(cpufreq_driver->owner)) {
591 ret = -EINVAL;
592 goto module_out;
595 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
596 if (!policy) {
597 ret = -ENOMEM;
598 goto nomem_out;
601 policy->cpu = cpu;
602 policy->cpus = cpumask_of_cpu(cpu);
604 init_MUTEX_LOCKED(&policy->lock);
605 init_completion(&policy->kobj_unregister);
606 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
608 /* call driver. From then on the cpufreq must be able
609 * to accept all calls to ->verify and ->setpolicy for this CPU
611 ret = cpufreq_driver->init(policy);
612 if (ret) {
613 dprintk("initialization failed\n");
614 goto err_out;
617 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
619 /* prepare interface data */
620 policy->kobj.parent = &sys_dev->kobj;
621 policy->kobj.ktype = &ktype_cpufreq;
622 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
624 ret = kobject_register(&policy->kobj);
625 if (ret)
626 goto err_out_driver_exit;
628 /* set up files for this cpu device */
629 drv_attr = cpufreq_driver->attr;
630 while ((drv_attr) && (*drv_attr)) {
631 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
632 drv_attr++;
634 if (cpufreq_driver->get)
635 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
636 if (cpufreq_driver->target)
637 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
639 spin_lock_irqsave(&cpufreq_driver_lock, flags);
640 for_each_cpu_mask(j, policy->cpus)
641 cpufreq_cpu_data[j] = policy;
642 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
643 policy->governor = NULL; /* to assure that the starting sequence is
644 * run in cpufreq_set_policy */
645 up(&policy->lock);
647 /* set default policy */
649 ret = cpufreq_set_policy(&new_policy);
650 if (ret) {
651 dprintk("setting policy failed\n");
652 goto err_out_unregister;
655 module_put(cpufreq_driver->owner);
656 dprintk("initialization complete\n");
657 cpufreq_debug_enable_ratelimit();
659 return 0;
662 err_out_unregister:
663 spin_lock_irqsave(&cpufreq_driver_lock, flags);
664 for_each_cpu_mask(j, policy->cpus)
665 cpufreq_cpu_data[j] = NULL;
666 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
668 kobject_unregister(&policy->kobj);
669 wait_for_completion(&policy->kobj_unregister);
671 err_out_driver_exit:
672 if (cpufreq_driver->exit)
673 cpufreq_driver->exit(policy);
675 err_out:
676 kfree(policy);
678 nomem_out:
679 module_put(cpufreq_driver->owner);
680 module_out:
681 cpufreq_debug_enable_ratelimit();
682 return ret;
687 * cpufreq_remove_dev - remove a CPU device
689 * Removes the cpufreq interface for a CPU device.
691 static int cpufreq_remove_dev (struct sys_device * sys_dev)
693 unsigned int cpu = sys_dev->id;
694 unsigned long flags;
695 struct cpufreq_policy *data;
696 #ifdef CONFIG_SMP
697 struct sys_device *cpu_sys_dev;
698 unsigned int j;
699 #endif
701 cpufreq_debug_disable_ratelimit();
702 dprintk("unregistering CPU %u\n", cpu);
704 spin_lock_irqsave(&cpufreq_driver_lock, flags);
705 data = cpufreq_cpu_data[cpu];
707 if (!data) {
708 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
709 cpufreq_debug_enable_ratelimit();
710 return -EINVAL;
712 cpufreq_cpu_data[cpu] = NULL;
715 #ifdef CONFIG_SMP
716 /* if this isn't the CPU which is the parent of the kobj, we
717 * only need to unlink, put and exit
719 if (unlikely(cpu != data->cpu)) {
720 dprintk("removing link\n");
721 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
722 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
723 cpufreq_cpu_put(data);
724 cpufreq_debug_enable_ratelimit();
725 return 0;
727 #endif
730 if (!kobject_get(&data->kobj)) {
731 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
732 cpufreq_debug_enable_ratelimit();
733 return -EFAULT;
736 #ifdef CONFIG_SMP
737 /* if we have other CPUs still registered, we need to unlink them,
738 * or else wait_for_completion below will lock up. Clean the
739 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
740 * links afterwards.
742 if (unlikely(cpus_weight(data->cpus) > 1)) {
743 for_each_cpu_mask(j, data->cpus) {
744 if (j == cpu)
745 continue;
746 cpufreq_cpu_data[j] = NULL;
750 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
752 if (unlikely(cpus_weight(data->cpus) > 1)) {
753 for_each_cpu_mask(j, data->cpus) {
754 if (j == cpu)
755 continue;
756 dprintk("removing link for cpu %u\n", j);
757 cpu_sys_dev = get_cpu_sysdev(j);
758 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
759 cpufreq_cpu_put(data);
762 #else
763 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
764 #endif
766 down(&data->lock);
767 if (cpufreq_driver->target)
768 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
769 up(&data->lock);
771 kobject_unregister(&data->kobj);
773 kobject_put(&data->kobj);
775 /* we need to make sure that the underlying kobj is actually
776 * not referenced anymore by anybody before we proceed with
777 * unloading.
779 dprintk("waiting for dropping of refcount\n");
780 wait_for_completion(&data->kobj_unregister);
781 dprintk("wait complete\n");
783 if (cpufreq_driver->exit)
784 cpufreq_driver->exit(data);
786 kfree(data);
788 cpufreq_debug_enable_ratelimit();
790 return 0;
794 static void handle_update(void *data)
796 unsigned int cpu = (unsigned int)(long)data;
797 dprintk("handle_update for cpu %u called\n", cpu);
798 cpufreq_update_policy(cpu);
802 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
803 * @cpu: cpu number
804 * @old_freq: CPU frequency the kernel thinks the CPU runs at
805 * @new_freq: CPU frequency the CPU actually runs at
807 * We adjust to current frequency first, and need to clean up later. So either call
808 * to cpufreq_update_policy() or schedule handle_update()).
810 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
812 struct cpufreq_freqs freqs;
814 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
815 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
817 freqs.cpu = cpu;
818 freqs.old = old_freq;
819 freqs.new = new_freq;
820 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
821 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
825 /**
826 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
827 * @cpu: CPU number
829 * This is the last known freq, without actually getting it from the driver.
830 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
832 unsigned int cpufreq_quick_get(unsigned int cpu)
834 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
835 unsigned int ret = 0;
837 if (policy) {
838 down(&policy->lock);
839 ret = policy->cur;
840 up(&policy->lock);
841 cpufreq_cpu_put(policy);
844 return (ret);
846 EXPORT_SYMBOL(cpufreq_quick_get);
849 /**
850 * cpufreq_get - get the current CPU frequency (in kHz)
851 * @cpu: CPU number
853 * Get the CPU current (static) CPU frequency
855 unsigned int cpufreq_get(unsigned int cpu)
857 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
858 unsigned int ret = 0;
860 if (!policy)
861 return 0;
863 if (!cpufreq_driver->get)
864 goto out;
866 down(&policy->lock);
868 ret = cpufreq_driver->get(cpu);
870 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS))
872 /* verify no discrepancy between actual and saved value exists */
873 if (unlikely(ret != policy->cur)) {
874 cpufreq_out_of_sync(cpu, policy->cur, ret);
875 schedule_work(&policy->update);
879 up(&policy->lock);
881 out:
882 cpufreq_cpu_put(policy);
884 return (ret);
886 EXPORT_SYMBOL(cpufreq_get);
890 * cpufreq_suspend - let the low level driver prepare for suspend
893 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
895 int cpu = sysdev->id;
896 unsigned int ret = 0;
897 unsigned int cur_freq = 0;
898 struct cpufreq_policy *cpu_policy;
900 dprintk("resuming cpu %u\n", cpu);
902 if (!cpu_online(cpu))
903 return 0;
905 /* we may be lax here as interrupts are off. Nonetheless
906 * we need to grab the correct cpu policy, as to check
907 * whether we really run on this CPU.
910 cpu_policy = cpufreq_cpu_get(cpu);
911 if (!cpu_policy)
912 return -EINVAL;
914 /* only handle each CPU group once */
915 if (unlikely(cpu_policy->cpu != cpu)) {
916 cpufreq_cpu_put(cpu_policy);
917 return 0;
920 if (cpufreq_driver->suspend) {
921 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
922 if (ret) {
923 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
924 "step on CPU %u\n", cpu_policy->cpu);
925 cpufreq_cpu_put(cpu_policy);
926 return ret;
931 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
932 goto out;
934 if (cpufreq_driver->get)
935 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
937 if (!cur_freq || !cpu_policy->cur) {
938 printk(KERN_ERR "cpufreq: suspend failed to assert current "
939 "frequency is what timing core thinks it is.\n");
940 goto out;
943 if (unlikely(cur_freq != cpu_policy->cur)) {
944 struct cpufreq_freqs freqs;
946 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
947 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
948 "cpufreq assumed %u kHz.\n",
949 cur_freq, cpu_policy->cur);
951 freqs.cpu = cpu;
952 freqs.old = cpu_policy->cur;
953 freqs.new = cur_freq;
955 notifier_call_chain(&cpufreq_transition_notifier_list,
956 CPUFREQ_SUSPENDCHANGE, &freqs);
957 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
959 cpu_policy->cur = cur_freq;
962 out:
963 cpufreq_cpu_put(cpu_policy);
964 return 0;
968 * cpufreq_resume - restore proper CPU frequency handling after resume
970 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
971 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
972 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
973 * restored.
975 static int cpufreq_resume(struct sys_device * sysdev)
977 int cpu = sysdev->id;
978 unsigned int ret = 0;
979 struct cpufreq_policy *cpu_policy;
981 dprintk("resuming cpu %u\n", cpu);
983 if (!cpu_online(cpu))
984 return 0;
986 /* we may be lax here as interrupts are off. Nonetheless
987 * we need to grab the correct cpu policy, as to check
988 * whether we really run on this CPU.
991 cpu_policy = cpufreq_cpu_get(cpu);
992 if (!cpu_policy)
993 return -EINVAL;
995 /* only handle each CPU group once */
996 if (unlikely(cpu_policy->cpu != cpu)) {
997 cpufreq_cpu_put(cpu_policy);
998 return 0;
1001 if (cpufreq_driver->resume) {
1002 ret = cpufreq_driver->resume(cpu_policy);
1003 if (ret) {
1004 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1005 "step on CPU %u\n", cpu_policy->cpu);
1006 cpufreq_cpu_put(cpu_policy);
1007 return ret;
1011 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1012 unsigned int cur_freq = 0;
1014 if (cpufreq_driver->get)
1015 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1017 if (!cur_freq || !cpu_policy->cur) {
1018 printk(KERN_ERR "cpufreq: resume failed to assert "
1019 "current frequency is what timing core "
1020 "thinks it is.\n");
1021 goto out;
1024 if (unlikely(cur_freq != cpu_policy->cur)) {
1025 struct cpufreq_freqs freqs;
1027 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1028 dprintk(KERN_WARNING "Warning: CPU frequency"
1029 "is %u, cpufreq assumed %u kHz.\n",
1030 cur_freq, cpu_policy->cur);
1032 freqs.cpu = cpu;
1033 freqs.old = cpu_policy->cur;
1034 freqs.new = cur_freq;
1036 notifier_call_chain(&cpufreq_transition_notifier_list,
1037 CPUFREQ_RESUMECHANGE, &freqs);
1038 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1040 cpu_policy->cur = cur_freq;
1044 out:
1045 schedule_work(&cpu_policy->update);
1046 cpufreq_cpu_put(cpu_policy);
1047 return ret;
1050 static struct sysdev_driver cpufreq_sysdev_driver = {
1051 .add = cpufreq_add_dev,
1052 .remove = cpufreq_remove_dev,
1053 .suspend = cpufreq_suspend,
1054 .resume = cpufreq_resume,
1058 /*********************************************************************
1059 * NOTIFIER LISTS INTERFACE *
1060 *********************************************************************/
1063 * cpufreq_register_notifier - register a driver with cpufreq
1064 * @nb: notifier function to register
1065 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1067 * Add a driver to one of two lists: either a list of drivers that
1068 * are notified about clock rate changes (once before and once after
1069 * the transition), or a list of drivers that are notified about
1070 * changes in cpufreq policy.
1072 * This function may sleep, and has the same return conditions as
1073 * notifier_chain_register.
1075 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1077 int ret;
1079 down_write(&cpufreq_notifier_rwsem);
1080 switch (list) {
1081 case CPUFREQ_TRANSITION_NOTIFIER:
1082 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
1083 break;
1084 case CPUFREQ_POLICY_NOTIFIER:
1085 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
1086 break;
1087 default:
1088 ret = -EINVAL;
1090 up_write(&cpufreq_notifier_rwsem);
1092 return ret;
1094 EXPORT_SYMBOL(cpufreq_register_notifier);
1098 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1099 * @nb: notifier block to be unregistered
1100 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1102 * Remove a driver from the CPU frequency notifier list.
1104 * This function may sleep, and has the same return conditions as
1105 * notifier_chain_unregister.
1107 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1109 int ret;
1111 down_write(&cpufreq_notifier_rwsem);
1112 switch (list) {
1113 case CPUFREQ_TRANSITION_NOTIFIER:
1114 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
1115 break;
1116 case CPUFREQ_POLICY_NOTIFIER:
1117 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
1118 break;
1119 default:
1120 ret = -EINVAL;
1122 up_write(&cpufreq_notifier_rwsem);
1124 return ret;
1126 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1129 /*********************************************************************
1130 * GOVERNORS *
1131 *********************************************************************/
1134 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1135 unsigned int target_freq,
1136 unsigned int relation)
1138 int retval = -EINVAL;
1140 lock_cpu_hotplug();
1141 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1142 target_freq, relation);
1143 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1144 retval = cpufreq_driver->target(policy, target_freq, relation);
1146 unlock_cpu_hotplug();
1148 return retval;
1150 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1152 int cpufreq_driver_target(struct cpufreq_policy *policy,
1153 unsigned int target_freq,
1154 unsigned int relation)
1156 int ret;
1158 policy = cpufreq_cpu_get(policy->cpu);
1159 if (!policy)
1160 return -EINVAL;
1162 down(&policy->lock);
1164 ret = __cpufreq_driver_target(policy, target_freq, relation);
1166 up(&policy->lock);
1168 cpufreq_cpu_put(policy);
1170 return ret;
1172 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1175 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1177 int ret;
1179 if (!try_module_get(policy->governor->owner))
1180 return -EINVAL;
1182 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1183 ret = policy->governor->governor(policy, event);
1185 /* we keep one module reference alive for each CPU governed by this CPU */
1186 if ((event != CPUFREQ_GOV_START) || ret)
1187 module_put(policy->governor->owner);
1188 if ((event == CPUFREQ_GOV_STOP) && !ret)
1189 module_put(policy->governor->owner);
1191 return ret;
1195 int cpufreq_governor(unsigned int cpu, unsigned int event)
1197 int ret = 0;
1198 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1200 if (!policy)
1201 return -EINVAL;
1203 down(&policy->lock);
1204 ret = __cpufreq_governor(policy, event);
1205 up(&policy->lock);
1207 cpufreq_cpu_put(policy);
1209 return ret;
1211 EXPORT_SYMBOL_GPL(cpufreq_governor);
1214 int cpufreq_register_governor(struct cpufreq_governor *governor)
1216 struct cpufreq_governor *t;
1218 if (!governor)
1219 return -EINVAL;
1221 down(&cpufreq_governor_sem);
1223 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1224 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1225 up(&cpufreq_governor_sem);
1226 return -EBUSY;
1229 list_add(&governor->governor_list, &cpufreq_governor_list);
1231 up(&cpufreq_governor_sem);
1233 return 0;
1235 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1238 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1240 if (!governor)
1241 return;
1243 down(&cpufreq_governor_sem);
1244 list_del(&governor->governor_list);
1245 up(&cpufreq_governor_sem);
1246 return;
1248 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1252 /*********************************************************************
1253 * POLICY INTERFACE *
1254 *********************************************************************/
1257 * cpufreq_get_policy - get the current cpufreq_policy
1258 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1260 * Reads the current cpufreq policy.
1262 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1264 struct cpufreq_policy *cpu_policy;
1265 if (!policy)
1266 return -EINVAL;
1268 cpu_policy = cpufreq_cpu_get(cpu);
1269 if (!cpu_policy)
1270 return -EINVAL;
1272 down(&cpu_policy->lock);
1273 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1274 up(&cpu_policy->lock);
1276 cpufreq_cpu_put(cpu_policy);
1278 return 0;
1280 EXPORT_SYMBOL(cpufreq_get_policy);
1283 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1285 int ret = 0;
1287 cpufreq_debug_disable_ratelimit();
1288 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1289 policy->min, policy->max);
1291 memcpy(&policy->cpuinfo,
1292 &data->cpuinfo,
1293 sizeof(struct cpufreq_cpuinfo));
1295 /* verify the cpu speed can be set within this limit */
1296 ret = cpufreq_driver->verify(policy);
1297 if (ret)
1298 goto error_out;
1300 down_read(&cpufreq_notifier_rwsem);
1302 /* adjust if necessary - all reasons */
1303 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
1304 policy);
1306 /* adjust if necessary - hardware incompatibility*/
1307 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
1308 policy);
1310 /* verify the cpu speed can be set within this limit,
1311 which might be different to the first one */
1312 ret = cpufreq_driver->verify(policy);
1313 if (ret) {
1314 up_read(&cpufreq_notifier_rwsem);
1315 goto error_out;
1318 /* notification of the new policy */
1319 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
1320 policy);
1322 up_read(&cpufreq_notifier_rwsem);
1324 data->min = policy->min;
1325 data->max = policy->max;
1327 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1329 if (cpufreq_driver->setpolicy) {
1330 data->policy = policy->policy;
1331 dprintk("setting range\n");
1332 ret = cpufreq_driver->setpolicy(policy);
1333 } else {
1334 if (policy->governor != data->governor) {
1335 /* save old, working values */
1336 struct cpufreq_governor *old_gov = data->governor;
1338 dprintk("governor switch\n");
1340 /* end old governor */
1341 if (data->governor)
1342 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1344 /* start new governor */
1345 data->governor = policy->governor;
1346 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1347 /* new governor failed, so re-start old one */
1348 dprintk("starting governor %s failed\n", data->governor->name);
1349 if (old_gov) {
1350 data->governor = old_gov;
1351 __cpufreq_governor(data, CPUFREQ_GOV_START);
1353 ret = -EINVAL;
1354 goto error_out;
1356 /* might be a policy change, too, so fall through */
1358 dprintk("governor: change or update limits\n");
1359 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1362 error_out:
1363 cpufreq_debug_enable_ratelimit();
1364 return ret;
1368 * cpufreq_set_policy - set a new CPUFreq policy
1369 * @policy: policy to be set.
1371 * Sets a new CPU frequency and voltage scaling policy.
1373 int cpufreq_set_policy(struct cpufreq_policy *policy)
1375 int ret = 0;
1376 struct cpufreq_policy *data;
1378 if (!policy)
1379 return -EINVAL;
1381 data = cpufreq_cpu_get(policy->cpu);
1382 if (!data)
1383 return -EINVAL;
1385 /* lock this CPU */
1386 down(&data->lock);
1388 ret = __cpufreq_set_policy(data, policy);
1389 data->user_policy.min = data->min;
1390 data->user_policy.max = data->max;
1391 data->user_policy.policy = data->policy;
1392 data->user_policy.governor = data->governor;
1394 up(&data->lock);
1395 cpufreq_cpu_put(data);
1397 return ret;
1399 EXPORT_SYMBOL(cpufreq_set_policy);
1403 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1404 * @cpu: CPU which shall be re-evaluated
1406 * Usefull for policy notifiers which have different necessities
1407 * at different times.
1409 int cpufreq_update_policy(unsigned int cpu)
1411 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1412 struct cpufreq_policy policy;
1413 int ret = 0;
1415 if (!data)
1416 return -ENODEV;
1418 down(&data->lock);
1420 dprintk("updating policy for CPU %u\n", cpu);
1421 memcpy(&policy,
1422 data,
1423 sizeof(struct cpufreq_policy));
1424 policy.min = data->user_policy.min;
1425 policy.max = data->user_policy.max;
1426 policy.policy = data->user_policy.policy;
1427 policy.governor = data->user_policy.governor;
1429 ret = __cpufreq_set_policy(data, &policy);
1431 up(&data->lock);
1433 cpufreq_cpu_put(data);
1434 return ret;
1436 EXPORT_SYMBOL(cpufreq_update_policy);
1438 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1439 unsigned long action, void *hcpu)
1441 unsigned int cpu = (unsigned long)hcpu;
1442 struct cpufreq_policy *policy;
1443 struct sys_device *sys_dev;
1445 sys_dev = get_cpu_sysdev(cpu);
1447 if (sys_dev) {
1448 switch (action) {
1449 case CPU_ONLINE:
1450 cpufreq_add_dev(sys_dev);
1451 break;
1452 case CPU_DOWN_PREPARE:
1454 * We attempt to put this cpu in lowest frequency
1455 * possible before going down. This will permit
1456 * hardware-managed P-State to switch other related
1457 * threads to min or higher speeds if possible.
1459 policy = cpufreq_cpu_data[cpu];
1460 if (policy) {
1461 cpufreq_driver_target(policy, policy->min,
1462 CPUFREQ_RELATION_H);
1464 break;
1465 case CPU_DEAD:
1466 cpufreq_remove_dev(sys_dev);
1467 break;
1470 return NOTIFY_OK;
1473 static struct notifier_block cpufreq_cpu_notifier =
1475 .notifier_call = cpufreq_cpu_callback,
1478 /*********************************************************************
1479 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1480 *********************************************************************/
1483 * cpufreq_register_driver - register a CPU Frequency driver
1484 * @driver_data: A struct cpufreq_driver containing the values#
1485 * submitted by the CPU Frequency driver.
1487 * Registers a CPU Frequency driver to this core code. This code
1488 * returns zero on success, -EBUSY when another driver got here first
1489 * (and isn't unregistered in the meantime).
1492 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1494 unsigned long flags;
1495 int ret;
1497 if (!driver_data || !driver_data->verify || !driver_data->init ||
1498 ((!driver_data->setpolicy) && (!driver_data->target)))
1499 return -EINVAL;
1501 dprintk("trying to register driver %s\n", driver_data->name);
1503 if (driver_data->setpolicy)
1504 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1506 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1507 if (cpufreq_driver) {
1508 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1509 return -EBUSY;
1511 cpufreq_driver = driver_data;
1512 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1514 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1516 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1517 int i;
1518 ret = -ENODEV;
1520 /* check for at least one working CPU */
1521 for (i=0; i<NR_CPUS; i++)
1522 if (cpufreq_cpu_data[i])
1523 ret = 0;
1525 /* if all ->init() calls failed, unregister */
1526 if (ret) {
1527 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1528 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1530 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1531 cpufreq_driver = NULL;
1532 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1536 if (!ret) {
1537 register_cpu_notifier(&cpufreq_cpu_notifier);
1538 dprintk("driver %s up and running\n", driver_data->name);
1539 cpufreq_debug_enable_ratelimit();
1542 return (ret);
1544 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1548 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1550 * Unregister the current CPUFreq driver. Only call this if you have
1551 * the right to do so, i.e. if you have succeeded in initialising before!
1552 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1553 * currently not initialised.
1555 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1557 unsigned long flags;
1559 cpufreq_debug_disable_ratelimit();
1561 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1562 cpufreq_debug_enable_ratelimit();
1563 return -EINVAL;
1566 dprintk("unregistering driver %s\n", driver->name);
1568 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1569 unregister_cpu_notifier(&cpufreq_cpu_notifier);
1571 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1572 cpufreq_driver = NULL;
1573 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1575 return 0;
1577 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);