Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-2.6
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / cpufreq / cpufreq.c
blob9b6ae7dc8b8a3a736666568fad41c3b8b567210d
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
9 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
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.
18 #include <linux/config.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/notifier.h>
23 #include <linux/cpufreq.h>
24 #include <linux/delay.h>
25 #include <linux/interrupt.h>
26 #include <linux/spinlock.h>
27 #include <linux/device.h>
28 #include <linux/slab.h>
29 #include <linux/cpu.h>
30 #include <linux/completion.h>
31 #include <linux/mutex.h>
33 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
35 /**
36 * The "cpufreq driver" - the arch- or hardware-dependend low
37 * level driver of CPUFreq support, and its spinlock. This lock
38 * also protects the cpufreq_cpu_data array.
40 static struct cpufreq_driver *cpufreq_driver;
41 static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
42 static DEFINE_SPINLOCK(cpufreq_driver_lock);
44 /* internal prototypes */
45 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
46 static void handle_update(void *data);
48 /**
49 * Two notifier lists: the "policy" list is involved in the
50 * validation process for a new CPU frequency policy; the
51 * "transition" list for kernel code that needs to handle
52 * changes to devices when the CPU clock speed changes.
53 * The mutex locks both lists.
55 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
56 static BLOCKING_NOTIFIER_HEAD(cpufreq_transition_notifier_list);
59 static LIST_HEAD(cpufreq_governor_list);
60 static DEFINE_MUTEX (cpufreq_governor_mutex);
62 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
64 struct cpufreq_policy *data;
65 unsigned long flags;
67 if (cpu >= NR_CPUS)
68 goto err_out;
70 /* get the cpufreq driver */
71 spin_lock_irqsave(&cpufreq_driver_lock, flags);
73 if (!cpufreq_driver)
74 goto err_out_unlock;
76 if (!try_module_get(cpufreq_driver->owner))
77 goto err_out_unlock;
80 /* get the CPU */
81 data = cpufreq_cpu_data[cpu];
83 if (!data)
84 goto err_out_put_module;
86 if (!kobject_get(&data->kobj))
87 goto err_out_put_module;
89 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
90 return data;
92 err_out_put_module:
93 module_put(cpufreq_driver->owner);
94 err_out_unlock:
95 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
96 err_out:
97 return NULL;
99 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 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 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 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
233 * on frequency transition.
235 * This function calls the transition notifiers and the "adjust_jiffies"
236 * function. It is called twice on all CPU frequency changes that have
237 * external effects.
239 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
241 struct cpufreq_policy *policy;
243 BUG_ON(irqs_disabled());
245 freqs->flags = cpufreq_driver->flags;
246 dprintk("notification %u of frequency transition to %u kHz\n",
247 state, freqs->new);
249 policy = cpufreq_cpu_data[freqs->cpu];
250 switch (state) {
252 case CPUFREQ_PRECHANGE:
253 /* detect if the driver reported a value as "old frequency"
254 * which is not equal to what the cpufreq core thinks is
255 * "old frequency".
257 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
258 if ((policy) && (policy->cpu == freqs->cpu) &&
259 (policy->cur) && (policy->cur != freqs->old)) {
260 dprintk(KERN_WARNING "Warning: CPU frequency is"
261 " %u, cpufreq assumed %u kHz.\n",
262 freqs->old, policy->cur);
263 freqs->old = policy->cur;
266 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
267 CPUFREQ_PRECHANGE, freqs);
268 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
269 break;
271 case CPUFREQ_POSTCHANGE:
272 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
273 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
274 CPUFREQ_POSTCHANGE, freqs);
275 if (likely(policy) && likely(policy->cpu == freqs->cpu))
276 policy->cur = freqs->new;
277 break;
280 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
284 /*********************************************************************
285 * SYSFS INTERFACE *
286 *********************************************************************/
289 * cpufreq_parse_governor - parse a governor string
291 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
292 struct cpufreq_governor **governor)
294 if (!cpufreq_driver)
295 return -EINVAL;
296 if (cpufreq_driver->setpolicy) {
297 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
298 *policy = CPUFREQ_POLICY_PERFORMANCE;
299 return 0;
300 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
301 *policy = CPUFREQ_POLICY_POWERSAVE;
302 return 0;
304 return -EINVAL;
305 } else {
306 struct cpufreq_governor *t;
307 mutex_lock(&cpufreq_governor_mutex);
308 if (!cpufreq_driver || !cpufreq_driver->target)
309 goto out;
310 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
311 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
312 *governor = t;
313 mutex_unlock(&cpufreq_governor_mutex);
314 return 0;
317 out:
318 mutex_unlock(&cpufreq_governor_mutex);
320 return -EINVAL;
322 EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
325 /* drivers/base/cpu.c */
326 extern struct sysdev_class cpu_sysdev_class;
330 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
332 * Write out information from cpufreq_driver->policy[cpu]; object must be
333 * "unsigned int".
336 #define show_one(file_name, object) \
337 static ssize_t show_##file_name \
338 (struct cpufreq_policy * policy, char *buf) \
340 return sprintf (buf, "%u\n", policy->object); \
343 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
344 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
345 show_one(scaling_min_freq, min);
346 show_one(scaling_max_freq, max);
347 show_one(scaling_cur_freq, cur);
350 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
352 #define store_one(file_name, object) \
353 static ssize_t store_##file_name \
354 (struct cpufreq_policy * policy, const char *buf, size_t count) \
356 unsigned int ret = -EINVAL; \
357 struct cpufreq_policy new_policy; \
359 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
360 if (ret) \
361 return -EINVAL; \
363 ret = sscanf (buf, "%u", &new_policy.object); \
364 if (ret != 1) \
365 return -EINVAL; \
367 ret = cpufreq_set_policy(&new_policy); \
369 return ret ? ret : count; \
372 store_one(scaling_min_freq,min);
373 store_one(scaling_max_freq,max);
376 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
378 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
380 unsigned int cur_freq = cpufreq_get(policy->cpu);
381 if (!cur_freq)
382 return sprintf(buf, "<unknown>");
383 return sprintf(buf, "%u\n", cur_freq);
388 * show_scaling_governor - show the current policy for the specified CPU
390 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
392 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
393 return sprintf(buf, "powersave\n");
394 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
395 return sprintf(buf, "performance\n");
396 else if (policy->governor)
397 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
398 return -EINVAL;
403 * store_scaling_governor - store policy for the specified CPU
405 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
406 const char *buf, size_t count)
408 unsigned int ret = -EINVAL;
409 char str_governor[16];
410 struct cpufreq_policy new_policy;
412 ret = cpufreq_get_policy(&new_policy, policy->cpu);
413 if (ret)
414 return ret;
416 ret = sscanf (buf, "%15s", str_governor);
417 if (ret != 1)
418 return -EINVAL;
420 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
421 return -EINVAL;
423 ret = cpufreq_set_policy(&new_policy);
424 return ret ? ret : count;
428 * show_scaling_driver - show the cpufreq driver currently loaded
430 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
432 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
436 * show_scaling_available_governors - show the available CPUfreq governors
438 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
439 char *buf)
441 ssize_t i = 0;
442 struct cpufreq_governor *t;
444 if (!cpufreq_driver->target) {
445 i += sprintf(buf, "performance powersave");
446 goto out;
449 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
450 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
451 goto out;
452 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
454 out:
455 i += sprintf(&buf[i], "\n");
456 return i;
459 * show_affected_cpus - show the CPUs affected by each transition
461 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
463 ssize_t i = 0;
464 unsigned int cpu;
466 for_each_cpu_mask(cpu, policy->cpus) {
467 if (i)
468 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
469 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
470 if (i >= (PAGE_SIZE - 5))
471 break;
473 i += sprintf(&buf[i], "\n");
474 return i;
478 #define define_one_ro(_name) \
479 static struct freq_attr _name = \
480 __ATTR(_name, 0444, show_##_name, NULL)
482 #define define_one_ro0400(_name) \
483 static struct freq_attr _name = \
484 __ATTR(_name, 0400, show_##_name, NULL)
486 #define define_one_rw(_name) \
487 static struct freq_attr _name = \
488 __ATTR(_name, 0644, show_##_name, store_##_name)
490 define_one_ro0400(cpuinfo_cur_freq);
491 define_one_ro(cpuinfo_min_freq);
492 define_one_ro(cpuinfo_max_freq);
493 define_one_ro(scaling_available_governors);
494 define_one_ro(scaling_driver);
495 define_one_ro(scaling_cur_freq);
496 define_one_ro(affected_cpus);
497 define_one_rw(scaling_min_freq);
498 define_one_rw(scaling_max_freq);
499 define_one_rw(scaling_governor);
501 static struct attribute * default_attrs[] = {
502 &cpuinfo_min_freq.attr,
503 &cpuinfo_max_freq.attr,
504 &scaling_min_freq.attr,
505 &scaling_max_freq.attr,
506 &affected_cpus.attr,
507 &scaling_governor.attr,
508 &scaling_driver.attr,
509 &scaling_available_governors.attr,
510 NULL
513 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
514 #define to_attr(a) container_of(a,struct freq_attr,attr)
516 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
518 struct cpufreq_policy * policy = to_policy(kobj);
519 struct freq_attr * fattr = to_attr(attr);
520 ssize_t ret;
521 policy = cpufreq_cpu_get(policy->cpu);
522 if (!policy)
523 return -EINVAL;
524 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
525 cpufreq_cpu_put(policy);
526 return ret;
529 static ssize_t store(struct kobject * kobj, struct attribute * attr,
530 const char * buf, size_t count)
532 struct cpufreq_policy * policy = to_policy(kobj);
533 struct freq_attr * fattr = to_attr(attr);
534 ssize_t ret;
535 policy = cpufreq_cpu_get(policy->cpu);
536 if (!policy)
537 return -EINVAL;
538 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
539 cpufreq_cpu_put(policy);
540 return ret;
543 static void cpufreq_sysfs_release(struct kobject * kobj)
545 struct cpufreq_policy * policy = to_policy(kobj);
546 dprintk("last reference is dropped\n");
547 complete(&policy->kobj_unregister);
550 static struct sysfs_ops sysfs_ops = {
551 .show = show,
552 .store = store,
555 static struct kobj_type ktype_cpufreq = {
556 .sysfs_ops = &sysfs_ops,
557 .default_attrs = default_attrs,
558 .release = cpufreq_sysfs_release,
563 * cpufreq_add_dev - add a CPU device
565 * Adds the cpufreq interface for a CPU device.
567 static int cpufreq_add_dev (struct sys_device * sys_dev)
569 unsigned int cpu = sys_dev->id;
570 int ret = 0;
571 struct cpufreq_policy new_policy;
572 struct cpufreq_policy *policy;
573 struct freq_attr **drv_attr;
574 struct sys_device *cpu_sys_dev;
575 unsigned long flags;
576 unsigned int j;
577 #ifdef CONFIG_SMP
578 struct cpufreq_policy *managed_policy;
579 #endif
581 if (cpu_is_offline(cpu))
582 return 0;
584 cpufreq_debug_disable_ratelimit();
585 dprintk("adding CPU %u\n", cpu);
587 #ifdef CONFIG_SMP
588 /* check whether a different CPU already registered this
589 * CPU because it is in the same boat. */
590 policy = cpufreq_cpu_get(cpu);
591 if (unlikely(policy)) {
592 cpufreq_cpu_put(policy);
593 cpufreq_debug_enable_ratelimit();
594 return 0;
596 #endif
598 if (!try_module_get(cpufreq_driver->owner)) {
599 ret = -EINVAL;
600 goto module_out;
603 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
604 if (!policy) {
605 ret = -ENOMEM;
606 goto nomem_out;
609 policy->cpu = cpu;
610 policy->cpus = cpumask_of_cpu(cpu);
612 mutex_init(&policy->lock);
613 mutex_lock(&policy->lock);
614 init_completion(&policy->kobj_unregister);
615 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
617 /* call driver. From then on the cpufreq must be able
618 * to accept all calls to ->verify and ->setpolicy for this CPU
620 ret = cpufreq_driver->init(policy);
621 if (ret) {
622 dprintk("initialization failed\n");
623 mutex_unlock(&policy->lock);
624 goto err_out;
627 #ifdef CONFIG_SMP
628 for_each_cpu_mask(j, policy->cpus) {
629 if (cpu == j)
630 continue;
632 /* check for existing affected CPUs. They may not be aware
633 * of it due to CPU Hotplug.
635 managed_policy = cpufreq_cpu_get(j);
636 if (unlikely(managed_policy)) {
637 spin_lock_irqsave(&cpufreq_driver_lock, flags);
638 managed_policy->cpus = policy->cpus;
639 cpufreq_cpu_data[cpu] = managed_policy;
640 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
642 dprintk("CPU already managed, adding link\n");
643 sysfs_create_link(&sys_dev->kobj,
644 &managed_policy->kobj, "cpufreq");
646 cpufreq_debug_enable_ratelimit();
647 mutex_unlock(&policy->lock);
648 ret = 0;
649 goto err_out_driver_exit; /* call driver->exit() */
652 #endif
653 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
655 /* prepare interface data */
656 policy->kobj.parent = &sys_dev->kobj;
657 policy->kobj.ktype = &ktype_cpufreq;
658 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
660 ret = kobject_register(&policy->kobj);
661 if (ret) {
662 mutex_unlock(&policy->lock);
663 goto err_out_driver_exit;
665 /* set up files for this cpu device */
666 drv_attr = cpufreq_driver->attr;
667 while ((drv_attr) && (*drv_attr)) {
668 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
669 drv_attr++;
671 if (cpufreq_driver->get)
672 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
673 if (cpufreq_driver->target)
674 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
676 spin_lock_irqsave(&cpufreq_driver_lock, flags);
677 for_each_cpu_mask(j, policy->cpus)
678 cpufreq_cpu_data[j] = policy;
679 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
681 /* symlink affected CPUs */
682 for_each_cpu_mask(j, policy->cpus) {
683 if (j == cpu)
684 continue;
685 if (!cpu_online(j))
686 continue;
688 dprintk("CPU already managed, adding link\n");
689 cpufreq_cpu_get(cpu);
690 cpu_sys_dev = get_cpu_sysdev(j);
691 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
692 "cpufreq");
695 policy->governor = NULL; /* to assure that the starting sequence is
696 * run in cpufreq_set_policy */
697 mutex_unlock(&policy->lock);
699 /* set default policy */
701 ret = cpufreq_set_policy(&new_policy);
702 if (ret) {
703 dprintk("setting policy failed\n");
704 goto err_out_unregister;
707 module_put(cpufreq_driver->owner);
708 dprintk("initialization complete\n");
709 cpufreq_debug_enable_ratelimit();
711 return 0;
714 err_out_unregister:
715 spin_lock_irqsave(&cpufreq_driver_lock, flags);
716 for_each_cpu_mask(j, policy->cpus)
717 cpufreq_cpu_data[j] = NULL;
718 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
720 kobject_unregister(&policy->kobj);
721 wait_for_completion(&policy->kobj_unregister);
723 err_out_driver_exit:
724 if (cpufreq_driver->exit)
725 cpufreq_driver->exit(policy);
727 err_out:
728 kfree(policy);
730 nomem_out:
731 module_put(cpufreq_driver->owner);
732 module_out:
733 cpufreq_debug_enable_ratelimit();
734 return ret;
739 * cpufreq_remove_dev - remove a CPU device
741 * Removes the cpufreq interface for a CPU device.
743 static int cpufreq_remove_dev (struct sys_device * sys_dev)
745 unsigned int cpu = sys_dev->id;
746 unsigned long flags;
747 struct cpufreq_policy *data;
748 #ifdef CONFIG_SMP
749 struct sys_device *cpu_sys_dev;
750 unsigned int j;
751 #endif
753 cpufreq_debug_disable_ratelimit();
754 dprintk("unregistering CPU %u\n", cpu);
756 spin_lock_irqsave(&cpufreq_driver_lock, flags);
757 data = cpufreq_cpu_data[cpu];
759 if (!data) {
760 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
761 cpufreq_debug_enable_ratelimit();
762 return -EINVAL;
764 cpufreq_cpu_data[cpu] = NULL;
767 #ifdef CONFIG_SMP
768 /* if this isn't the CPU which is the parent of the kobj, we
769 * only need to unlink, put and exit
771 if (unlikely(cpu != data->cpu)) {
772 dprintk("removing link\n");
773 cpu_clear(cpu, data->cpus);
774 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
775 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
776 cpufreq_cpu_put(data);
777 cpufreq_debug_enable_ratelimit();
778 return 0;
780 #endif
783 if (!kobject_get(&data->kobj)) {
784 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
785 cpufreq_debug_enable_ratelimit();
786 return -EFAULT;
789 #ifdef CONFIG_SMP
790 /* if we have other CPUs still registered, we need to unlink them,
791 * or else wait_for_completion below will lock up. Clean the
792 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
793 * links afterwards.
795 if (unlikely(cpus_weight(data->cpus) > 1)) {
796 for_each_cpu_mask(j, data->cpus) {
797 if (j == cpu)
798 continue;
799 cpufreq_cpu_data[j] = NULL;
803 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
805 if (unlikely(cpus_weight(data->cpus) > 1)) {
806 for_each_cpu_mask(j, data->cpus) {
807 if (j == cpu)
808 continue;
809 dprintk("removing link for cpu %u\n", j);
810 cpu_sys_dev = get_cpu_sysdev(j);
811 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
812 cpufreq_cpu_put(data);
815 #else
816 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
817 #endif
819 mutex_lock(&data->lock);
820 if (cpufreq_driver->target)
821 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
822 mutex_unlock(&data->lock);
824 kobject_unregister(&data->kobj);
826 kobject_put(&data->kobj);
828 /* we need to make sure that the underlying kobj is actually
829 * not referenced anymore by anybody before we proceed with
830 * unloading.
832 dprintk("waiting for dropping of refcount\n");
833 wait_for_completion(&data->kobj_unregister);
834 dprintk("wait complete\n");
836 if (cpufreq_driver->exit)
837 cpufreq_driver->exit(data);
839 kfree(data);
841 cpufreq_debug_enable_ratelimit();
842 return 0;
846 static void handle_update(void *data)
848 unsigned int cpu = (unsigned int)(long)data;
849 dprintk("handle_update for cpu %u called\n", cpu);
850 cpufreq_update_policy(cpu);
854 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
855 * @cpu: cpu number
856 * @old_freq: CPU frequency the kernel thinks the CPU runs at
857 * @new_freq: CPU frequency the CPU actually runs at
859 * We adjust to current frequency first, and need to clean up later. So either call
860 * to cpufreq_update_policy() or schedule handle_update()).
862 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
864 struct cpufreq_freqs freqs;
866 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
867 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
869 freqs.cpu = cpu;
870 freqs.old = old_freq;
871 freqs.new = new_freq;
872 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
873 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
878 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
879 * @cpu: CPU number
881 * This is the last known freq, without actually getting it from the driver.
882 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
884 unsigned int cpufreq_quick_get(unsigned int cpu)
886 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
887 unsigned int ret = 0;
889 if (policy) {
890 mutex_lock(&policy->lock);
891 ret = policy->cur;
892 mutex_unlock(&policy->lock);
893 cpufreq_cpu_put(policy);
896 return (ret);
898 EXPORT_SYMBOL(cpufreq_quick_get);
902 * cpufreq_get - get the current CPU frequency (in kHz)
903 * @cpu: CPU number
905 * Get the CPU current (static) CPU frequency
907 unsigned int cpufreq_get(unsigned int cpu)
909 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
910 unsigned int ret = 0;
912 if (!policy)
913 return 0;
915 if (!cpufreq_driver->get)
916 goto out;
918 mutex_lock(&policy->lock);
920 ret = cpufreq_driver->get(cpu);
922 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
923 /* verify no discrepancy between actual and saved value exists */
924 if (unlikely(ret != policy->cur)) {
925 cpufreq_out_of_sync(cpu, policy->cur, ret);
926 schedule_work(&policy->update);
930 mutex_unlock(&policy->lock);
932 out:
933 cpufreq_cpu_put(policy);
935 return (ret);
937 EXPORT_SYMBOL(cpufreq_get);
941 * cpufreq_suspend - let the low level driver prepare for suspend
944 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
946 int cpu = sysdev->id;
947 unsigned int ret = 0;
948 unsigned int cur_freq = 0;
949 struct cpufreq_policy *cpu_policy;
951 dprintk("resuming cpu %u\n", cpu);
953 if (!cpu_online(cpu))
954 return 0;
956 /* we may be lax here as interrupts are off. Nonetheless
957 * we need to grab the correct cpu policy, as to check
958 * whether we really run on this CPU.
961 cpu_policy = cpufreq_cpu_get(cpu);
962 if (!cpu_policy)
963 return -EINVAL;
965 /* only handle each CPU group once */
966 if (unlikely(cpu_policy->cpu != cpu)) {
967 cpufreq_cpu_put(cpu_policy);
968 return 0;
971 if (cpufreq_driver->suspend) {
972 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
973 if (ret) {
974 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
975 "step on CPU %u\n", cpu_policy->cpu);
976 cpufreq_cpu_put(cpu_policy);
977 return ret;
982 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
983 goto out;
985 if (cpufreq_driver->get)
986 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
988 if (!cur_freq || !cpu_policy->cur) {
989 printk(KERN_ERR "cpufreq: suspend failed to assert current "
990 "frequency is what timing core thinks it is.\n");
991 goto out;
994 if (unlikely(cur_freq != cpu_policy->cur)) {
995 struct cpufreq_freqs freqs;
997 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
998 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
999 "cpufreq assumed %u kHz.\n",
1000 cur_freq, cpu_policy->cur);
1002 freqs.cpu = cpu;
1003 freqs.old = cpu_policy->cur;
1004 freqs.new = cur_freq;
1006 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
1007 CPUFREQ_SUSPENDCHANGE, &freqs);
1008 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1010 cpu_policy->cur = cur_freq;
1013 out:
1014 cpufreq_cpu_put(cpu_policy);
1015 return 0;
1019 * cpufreq_resume - restore proper CPU frequency handling after resume
1021 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1022 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
1023 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1024 * restored.
1026 static int cpufreq_resume(struct sys_device * sysdev)
1028 int cpu = sysdev->id;
1029 unsigned int ret = 0;
1030 struct cpufreq_policy *cpu_policy;
1032 dprintk("resuming cpu %u\n", cpu);
1034 if (!cpu_online(cpu))
1035 return 0;
1037 /* we may be lax here as interrupts are off. Nonetheless
1038 * we need to grab the correct cpu policy, as to check
1039 * whether we really run on this CPU.
1042 cpu_policy = cpufreq_cpu_get(cpu);
1043 if (!cpu_policy)
1044 return -EINVAL;
1046 /* only handle each CPU group once */
1047 if (unlikely(cpu_policy->cpu != cpu)) {
1048 cpufreq_cpu_put(cpu_policy);
1049 return 0;
1052 if (cpufreq_driver->resume) {
1053 ret = cpufreq_driver->resume(cpu_policy);
1054 if (ret) {
1055 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1056 "step on CPU %u\n", cpu_policy->cpu);
1057 cpufreq_cpu_put(cpu_policy);
1058 return ret;
1062 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1063 unsigned int cur_freq = 0;
1065 if (cpufreq_driver->get)
1066 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1068 if (!cur_freq || !cpu_policy->cur) {
1069 printk(KERN_ERR "cpufreq: resume failed to assert "
1070 "current frequency is what timing core "
1071 "thinks it is.\n");
1072 goto out;
1075 if (unlikely(cur_freq != cpu_policy->cur)) {
1076 struct cpufreq_freqs freqs;
1078 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1079 dprintk(KERN_WARNING "Warning: CPU frequency"
1080 "is %u, cpufreq assumed %u kHz.\n",
1081 cur_freq, cpu_policy->cur);
1083 freqs.cpu = cpu;
1084 freqs.old = cpu_policy->cur;
1085 freqs.new = cur_freq;
1087 blocking_notifier_call_chain(
1088 &cpufreq_transition_notifier_list,
1089 CPUFREQ_RESUMECHANGE, &freqs);
1090 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1092 cpu_policy->cur = cur_freq;
1096 out:
1097 schedule_work(&cpu_policy->update);
1098 cpufreq_cpu_put(cpu_policy);
1099 return ret;
1102 static struct sysdev_driver cpufreq_sysdev_driver = {
1103 .add = cpufreq_add_dev,
1104 .remove = cpufreq_remove_dev,
1105 .suspend = cpufreq_suspend,
1106 .resume = cpufreq_resume,
1110 /*********************************************************************
1111 * NOTIFIER LISTS INTERFACE *
1112 *********************************************************************/
1115 * cpufreq_register_notifier - register a driver with cpufreq
1116 * @nb: notifier function to register
1117 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1119 * Add a driver to one of two lists: either a list of drivers that
1120 * are notified about clock rate changes (once before and once after
1121 * the transition), or a list of drivers that are notified about
1122 * changes in cpufreq policy.
1124 * This function may sleep, and has the same return conditions as
1125 * blocking_notifier_chain_register.
1127 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1129 int ret;
1131 switch (list) {
1132 case CPUFREQ_TRANSITION_NOTIFIER:
1133 ret = blocking_notifier_chain_register(
1134 &cpufreq_transition_notifier_list, nb);
1135 break;
1136 case CPUFREQ_POLICY_NOTIFIER:
1137 ret = blocking_notifier_chain_register(
1138 &cpufreq_policy_notifier_list, nb);
1139 break;
1140 default:
1141 ret = -EINVAL;
1144 return ret;
1146 EXPORT_SYMBOL(cpufreq_register_notifier);
1150 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1151 * @nb: notifier block to be unregistered
1152 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1154 * Remove a driver from the CPU frequency notifier list.
1156 * This function may sleep, and has the same return conditions as
1157 * blocking_notifier_chain_unregister.
1159 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1161 int ret;
1163 switch (list) {
1164 case CPUFREQ_TRANSITION_NOTIFIER:
1165 ret = blocking_notifier_chain_unregister(
1166 &cpufreq_transition_notifier_list, nb);
1167 break;
1168 case CPUFREQ_POLICY_NOTIFIER:
1169 ret = blocking_notifier_chain_unregister(
1170 &cpufreq_policy_notifier_list, nb);
1171 break;
1172 default:
1173 ret = -EINVAL;
1176 return ret;
1178 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1181 /*********************************************************************
1182 * GOVERNORS *
1183 *********************************************************************/
1186 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1187 unsigned int target_freq,
1188 unsigned int relation)
1190 int retval = -EINVAL;
1192 lock_cpu_hotplug();
1193 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1194 target_freq, relation);
1195 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1196 retval = cpufreq_driver->target(policy, target_freq, relation);
1198 unlock_cpu_hotplug();
1200 return retval;
1202 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1204 int cpufreq_driver_target(struct cpufreq_policy *policy,
1205 unsigned int target_freq,
1206 unsigned int relation)
1208 int ret;
1210 policy = cpufreq_cpu_get(policy->cpu);
1211 if (!policy)
1212 return -EINVAL;
1214 mutex_lock(&policy->lock);
1216 ret = __cpufreq_driver_target(policy, target_freq, relation);
1218 mutex_unlock(&policy->lock);
1220 cpufreq_cpu_put(policy);
1221 return ret;
1223 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1226 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1228 int ret;
1230 if (!try_module_get(policy->governor->owner))
1231 return -EINVAL;
1233 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1234 ret = policy->governor->governor(policy, event);
1236 /* we keep one module reference alive for each CPU governed by this CPU */
1237 if ((event != CPUFREQ_GOV_START) || ret)
1238 module_put(policy->governor->owner);
1239 if ((event == CPUFREQ_GOV_STOP) && !ret)
1240 module_put(policy->governor->owner);
1242 return ret;
1246 int cpufreq_governor(unsigned int cpu, unsigned int event)
1248 int ret = 0;
1249 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1251 if (!policy)
1252 return -EINVAL;
1254 mutex_lock(&policy->lock);
1255 ret = __cpufreq_governor(policy, event);
1256 mutex_unlock(&policy->lock);
1258 cpufreq_cpu_put(policy);
1259 return ret;
1261 EXPORT_SYMBOL_GPL(cpufreq_governor);
1264 int cpufreq_register_governor(struct cpufreq_governor *governor)
1266 struct cpufreq_governor *t;
1268 if (!governor)
1269 return -EINVAL;
1271 mutex_lock(&cpufreq_governor_mutex);
1273 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1274 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1275 mutex_unlock(&cpufreq_governor_mutex);
1276 return -EBUSY;
1279 list_add(&governor->governor_list, &cpufreq_governor_list);
1281 mutex_unlock(&cpufreq_governor_mutex);
1282 return 0;
1284 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1287 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1289 if (!governor)
1290 return;
1292 mutex_lock(&cpufreq_governor_mutex);
1293 list_del(&governor->governor_list);
1294 mutex_unlock(&cpufreq_governor_mutex);
1295 return;
1297 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1301 /*********************************************************************
1302 * POLICY INTERFACE *
1303 *********************************************************************/
1306 * cpufreq_get_policy - get the current cpufreq_policy
1307 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1309 * Reads the current cpufreq policy.
1311 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1313 struct cpufreq_policy *cpu_policy;
1314 if (!policy)
1315 return -EINVAL;
1317 cpu_policy = cpufreq_cpu_get(cpu);
1318 if (!cpu_policy)
1319 return -EINVAL;
1321 mutex_lock(&cpu_policy->lock);
1322 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1323 mutex_unlock(&cpu_policy->lock);
1325 cpufreq_cpu_put(cpu_policy);
1326 return 0;
1328 EXPORT_SYMBOL(cpufreq_get_policy);
1331 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1333 int ret = 0;
1335 cpufreq_debug_disable_ratelimit();
1336 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1337 policy->min, policy->max);
1339 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
1341 /* verify the cpu speed can be set within this limit */
1342 ret = cpufreq_driver->verify(policy);
1343 if (ret)
1344 goto error_out;
1346 /* adjust if necessary - all reasons */
1347 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1348 CPUFREQ_ADJUST, policy);
1350 /* adjust if necessary - hardware incompatibility*/
1351 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1352 CPUFREQ_INCOMPATIBLE, policy);
1354 /* verify the cpu speed can be set within this limit,
1355 which might be different to the first one */
1356 ret = cpufreq_driver->verify(policy);
1357 if (ret)
1358 goto error_out;
1360 /* notification of the new policy */
1361 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1362 CPUFREQ_NOTIFY, policy);
1364 data->min = policy->min;
1365 data->max = policy->max;
1367 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1369 if (cpufreq_driver->setpolicy) {
1370 data->policy = policy->policy;
1371 dprintk("setting range\n");
1372 ret = cpufreq_driver->setpolicy(policy);
1373 } else {
1374 if (policy->governor != data->governor) {
1375 /* save old, working values */
1376 struct cpufreq_governor *old_gov = data->governor;
1378 dprintk("governor switch\n");
1380 /* end old governor */
1381 if (data->governor)
1382 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1384 /* start new governor */
1385 data->governor = policy->governor;
1386 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1387 /* new governor failed, so re-start old one */
1388 dprintk("starting governor %s failed\n", data->governor->name);
1389 if (old_gov) {
1390 data->governor = old_gov;
1391 __cpufreq_governor(data, CPUFREQ_GOV_START);
1393 ret = -EINVAL;
1394 goto error_out;
1396 /* might be a policy change, too, so fall through */
1398 dprintk("governor: change or update limits\n");
1399 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1402 error_out:
1403 cpufreq_debug_enable_ratelimit();
1404 return ret;
1408 * cpufreq_set_policy - set a new CPUFreq policy
1409 * @policy: policy to be set.
1411 * Sets a new CPU frequency and voltage scaling policy.
1413 int cpufreq_set_policy(struct cpufreq_policy *policy)
1415 int ret = 0;
1416 struct cpufreq_policy *data;
1418 if (!policy)
1419 return -EINVAL;
1421 data = cpufreq_cpu_get(policy->cpu);
1422 if (!data)
1423 return -EINVAL;
1425 /* lock this CPU */
1426 mutex_lock(&data->lock);
1428 ret = __cpufreq_set_policy(data, policy);
1429 data->user_policy.min = data->min;
1430 data->user_policy.max = data->max;
1431 data->user_policy.policy = data->policy;
1432 data->user_policy.governor = data->governor;
1434 mutex_unlock(&data->lock);
1435 cpufreq_cpu_put(data);
1437 return ret;
1439 EXPORT_SYMBOL(cpufreq_set_policy);
1443 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1444 * @cpu: CPU which shall be re-evaluated
1446 * Usefull for policy notifiers which have different necessities
1447 * at different times.
1449 int cpufreq_update_policy(unsigned int cpu)
1451 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1452 struct cpufreq_policy policy;
1453 int ret = 0;
1455 if (!data)
1456 return -ENODEV;
1458 mutex_lock(&data->lock);
1460 dprintk("updating policy for CPU %u\n", cpu);
1461 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1462 policy.min = data->user_policy.min;
1463 policy.max = data->user_policy.max;
1464 policy.policy = data->user_policy.policy;
1465 policy.governor = data->user_policy.governor;
1467 /* BIOS might change freq behind our back
1468 -> ask driver for current freq and notify governors about a change */
1469 if (cpufreq_driver->get) {
1470 policy.cur = cpufreq_driver->get(cpu);
1471 if (!data->cur) {
1472 dprintk("Driver did not initialize current freq");
1473 data->cur = policy.cur;
1474 } else {
1475 if (data->cur != policy.cur)
1476 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1480 ret = __cpufreq_set_policy(data, &policy);
1482 mutex_unlock(&data->lock);
1484 cpufreq_cpu_put(data);
1485 return ret;
1487 EXPORT_SYMBOL(cpufreq_update_policy);
1489 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1490 unsigned long action, void *hcpu)
1492 unsigned int cpu = (unsigned long)hcpu;
1493 struct cpufreq_policy *policy;
1494 struct sys_device *sys_dev;
1496 sys_dev = get_cpu_sysdev(cpu);
1498 if (sys_dev) {
1499 switch (action) {
1500 case CPU_ONLINE:
1501 cpufreq_add_dev(sys_dev);
1502 break;
1503 case CPU_DOWN_PREPARE:
1505 * We attempt to put this cpu in lowest frequency
1506 * possible before going down. This will permit
1507 * hardware-managed P-State to switch other related
1508 * threads to min or higher speeds if possible.
1510 policy = cpufreq_cpu_data[cpu];
1511 if (policy) {
1512 cpufreq_driver_target(policy, policy->min,
1513 CPUFREQ_RELATION_H);
1515 break;
1516 case CPU_DEAD:
1517 cpufreq_remove_dev(sys_dev);
1518 break;
1521 return NOTIFY_OK;
1524 static struct notifier_block cpufreq_cpu_notifier =
1526 .notifier_call = cpufreq_cpu_callback,
1529 /*********************************************************************
1530 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1531 *********************************************************************/
1534 * cpufreq_register_driver - register a CPU Frequency driver
1535 * @driver_data: A struct cpufreq_driver containing the values#
1536 * submitted by the CPU Frequency driver.
1538 * Registers a CPU Frequency driver to this core code. This code
1539 * returns zero on success, -EBUSY when another driver got here first
1540 * (and isn't unregistered in the meantime).
1543 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1545 unsigned long flags;
1546 int ret;
1548 if (!driver_data || !driver_data->verify || !driver_data->init ||
1549 ((!driver_data->setpolicy) && (!driver_data->target)))
1550 return -EINVAL;
1552 dprintk("trying to register driver %s\n", driver_data->name);
1554 if (driver_data->setpolicy)
1555 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1557 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1558 if (cpufreq_driver) {
1559 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1560 return -EBUSY;
1562 cpufreq_driver = driver_data;
1563 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1565 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1567 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1568 int i;
1569 ret = -ENODEV;
1571 /* check for at least one working CPU */
1572 for (i=0; i<NR_CPUS; i++)
1573 if (cpufreq_cpu_data[i])
1574 ret = 0;
1576 /* if all ->init() calls failed, unregister */
1577 if (ret) {
1578 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1579 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1581 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1582 cpufreq_driver = NULL;
1583 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1587 if (!ret) {
1588 register_cpu_notifier(&cpufreq_cpu_notifier);
1589 dprintk("driver %s up and running\n", driver_data->name);
1590 cpufreq_debug_enable_ratelimit();
1593 return (ret);
1595 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1599 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1601 * Unregister the current CPUFreq driver. Only call this if you have
1602 * the right to do so, i.e. if you have succeeded in initialising before!
1603 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1604 * currently not initialised.
1606 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1608 unsigned long flags;
1610 cpufreq_debug_disable_ratelimit();
1612 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1613 cpufreq_debug_enable_ratelimit();
1614 return -EINVAL;
1617 dprintk("unregistering driver %s\n", driver->name);
1619 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1620 unregister_cpu_notifier(&cpufreq_cpu_notifier);
1622 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1623 cpufreq_driver = NULL;
1624 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1626 return 0;
1628 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);