Merge master.kernel.org:/pub/scm/linux/kernel/git/herbert/crypto-2.6
[linux-2.6/suspend2-2.6.18.git] / drivers / cpufreq / cpufreq.c
blob8d328186f774b81cfdd1eabf5c01d244f50115be
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/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>
32 #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
34 /**
35 * The "cpufreq driver" - the arch- or hardware-dependend low
36 * level driver of CPUFreq support, and its spinlock. This lock
37 * also protects the cpufreq_cpu_data array.
39 static struct cpufreq_driver *cpufreq_driver;
40 static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
41 static DEFINE_SPINLOCK(cpufreq_driver_lock);
43 /* internal prototypes */
44 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
45 static void handle_update(void *data);
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.
54 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
55 static BLOCKING_NOTIFIER_HEAD(cpufreq_transition_notifier_list);
58 static LIST_HEAD(cpufreq_governor_list);
59 static DEFINE_MUTEX (cpufreq_governor_mutex);
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;
88 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
89 return data;
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;
98 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
101 void cpufreq_cpu_put(struct cpufreq_policy *data)
103 kobject_put(&data->kobj);
104 module_put(cpufreq_driver->owner);
106 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
109 /*********************************************************************
110 * UNIFIED DEBUG HELPERS *
111 *********************************************************************/
112 #ifdef CONFIG_CPU_FREQ_DEBUG
114 /* what part(s) of the CPUfreq subsystem are debugged? */
115 static unsigned int debug;
117 /* is the debug output ratelimit'ed using printk_ratelimit? User can
118 * set or modify this value.
120 static unsigned int debug_ratelimit = 1;
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
126 static unsigned int disable_ratelimit = 1;
127 static DEFINE_SPINLOCK(disable_ratelimit_lock);
129 static void cpufreq_debug_enable_ratelimit(void)
131 unsigned long flags;
133 spin_lock_irqsave(&disable_ratelimit_lock, flags);
134 if (disable_ratelimit)
135 disable_ratelimit--;
136 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
139 static void cpufreq_debug_disable_ratelimit(void)
141 unsigned long flags;
143 spin_lock_irqsave(&disable_ratelimit_lock, flags);
144 disable_ratelimit++;
145 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
148 void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
150 char s[256];
151 va_list args;
152 unsigned int len;
153 unsigned long flags;
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;
162 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
164 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
166 va_start(args, fmt);
167 len += vsnprintf(&s[len], (256 - len), fmt, args);
168 va_end(args);
170 printk(s);
172 WARN_ON(len < 5);
175 EXPORT_SYMBOL(cpufreq_debug_printk);
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.");
181 module_param(debug_ratelimit, uint, 0644);
182 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
184 #else /* !CONFIG_CPU_FREQ_DEBUG */
186 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
187 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
189 #endif /* CONFIG_CPU_FREQ_DEBUG */
192 /*********************************************************************
193 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
194 *********************************************************************/
197 * adjust_jiffies - adjust the system "loops_per_jiffy"
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.
204 #ifndef CONFIG_SMP
205 static unsigned long l_p_j_ref;
206 static unsigned int l_p_j_ref_freq;
208 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
210 if (ci->flags & CPUFREQ_CONST_LOOPS)
211 return;
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);
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);
225 #else
226 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
227 #endif
231 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
232 * on frequency transition.
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.
238 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
240 struct cpufreq_policy *policy;
242 BUG_ON(irqs_disabled());
244 freqs->flags = cpufreq_driver->flags;
245 dprintk("notification %u of frequency transition to %u kHz\n",
246 state, freqs->new);
248 policy = cpufreq_cpu_data[freqs->cpu];
249 switch (state) {
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".
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;
265 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
266 CPUFREQ_PRECHANGE, freqs);
267 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
268 break;
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;
279 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
283 /*********************************************************************
284 * SYSFS INTERFACE *
285 *********************************************************************/
288 * cpufreq_parse_governor - parse a governor string
290 static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
291 struct cpufreq_governor **governor)
293 if (!cpufreq_driver)
294 return -EINVAL;
295 if (cpufreq_driver->setpolicy) {
296 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
297 *policy = CPUFREQ_POLICY_PERFORMANCE;
298 return 0;
299 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
300 *policy = CPUFREQ_POLICY_POWERSAVE;
301 return 0;
303 return -EINVAL;
304 } else {
305 struct cpufreq_governor *t;
306 mutex_lock(&cpufreq_governor_mutex);
307 if (!cpufreq_driver || !cpufreq_driver->target)
308 goto out;
309 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
310 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
311 *governor = t;
312 mutex_unlock(&cpufreq_governor_mutex);
313 return 0;
316 out:
317 mutex_unlock(&cpufreq_governor_mutex);
319 return -EINVAL;
323 /* drivers/base/cpu.c */
324 extern struct sysdev_class cpu_sysdev_class;
328 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
330 * Write out information from cpufreq_driver->policy[cpu]; object must be
331 * "unsigned int".
334 #define show_one(file_name, object) \
335 static ssize_t show_##file_name \
336 (struct cpufreq_policy * policy, char *buf) \
338 return sprintf (buf, "%u\n", policy->object); \
341 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
342 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
343 show_one(scaling_min_freq, min);
344 show_one(scaling_max_freq, max);
345 show_one(scaling_cur_freq, cur);
347 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
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 mutex_lock(&policy->lock); \
368 ret = __cpufreq_set_policy(policy, &new_policy); \
369 policy->user_policy.object = policy->object; \
370 mutex_unlock(&policy->lock); \
372 return ret ? ret : count; \
375 store_one(scaling_min_freq,min);
376 store_one(scaling_max_freq,max);
379 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
381 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
383 unsigned int cur_freq = cpufreq_get(policy->cpu);
384 if (!cur_freq)
385 return sprintf(buf, "<unknown>");
386 return sprintf(buf, "%u\n", cur_freq);
391 * show_scaling_governor - show the current policy for the specified CPU
393 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
395 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
396 return sprintf(buf, "powersave\n");
397 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
398 return sprintf(buf, "performance\n");
399 else if (policy->governor)
400 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
401 return -EINVAL;
406 * store_scaling_governor - store policy for the specified CPU
408 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
409 const char *buf, size_t count)
411 unsigned int ret = -EINVAL;
412 char str_governor[16];
413 struct cpufreq_policy new_policy;
415 ret = cpufreq_get_policy(&new_policy, policy->cpu);
416 if (ret)
417 return ret;
419 ret = sscanf (buf, "%15s", str_governor);
420 if (ret != 1)
421 return -EINVAL;
423 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
424 return -EINVAL;
426 lock_cpu_hotplug();
428 /* Do not use cpufreq_set_policy here or the user_policy.max
429 will be wrongly overridden */
430 mutex_lock(&policy->lock);
431 ret = __cpufreq_set_policy(policy, &new_policy);
433 policy->user_policy.policy = policy->policy;
434 policy->user_policy.governor = policy->governor;
435 mutex_unlock(&policy->lock);
437 unlock_cpu_hotplug();
439 return ret ? ret : count;
443 * show_scaling_driver - show the cpufreq driver currently loaded
445 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
447 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
451 * show_scaling_available_governors - show the available CPUfreq governors
453 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
454 char *buf)
456 ssize_t i = 0;
457 struct cpufreq_governor *t;
459 if (!cpufreq_driver->target) {
460 i += sprintf(buf, "performance powersave");
461 goto out;
464 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
465 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
466 goto out;
467 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
469 out:
470 i += sprintf(&buf[i], "\n");
471 return i;
474 * show_affected_cpus - show the CPUs affected by each transition
476 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
478 ssize_t i = 0;
479 unsigned int cpu;
481 for_each_cpu_mask(cpu, policy->cpus) {
482 if (i)
483 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
484 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
485 if (i >= (PAGE_SIZE - 5))
486 break;
488 i += sprintf(&buf[i], "\n");
489 return i;
493 #define define_one_ro(_name) \
494 static struct freq_attr _name = \
495 __ATTR(_name, 0444, show_##_name, NULL)
497 #define define_one_ro0400(_name) \
498 static struct freq_attr _name = \
499 __ATTR(_name, 0400, show_##_name, NULL)
501 #define define_one_rw(_name) \
502 static struct freq_attr _name = \
503 __ATTR(_name, 0644, show_##_name, store_##_name)
505 define_one_ro0400(cpuinfo_cur_freq);
506 define_one_ro(cpuinfo_min_freq);
507 define_one_ro(cpuinfo_max_freq);
508 define_one_ro(scaling_available_governors);
509 define_one_ro(scaling_driver);
510 define_one_ro(scaling_cur_freq);
511 define_one_ro(affected_cpus);
512 define_one_rw(scaling_min_freq);
513 define_one_rw(scaling_max_freq);
514 define_one_rw(scaling_governor);
516 static struct attribute * default_attrs[] = {
517 &cpuinfo_min_freq.attr,
518 &cpuinfo_max_freq.attr,
519 &scaling_min_freq.attr,
520 &scaling_max_freq.attr,
521 &affected_cpus.attr,
522 &scaling_governor.attr,
523 &scaling_driver.attr,
524 &scaling_available_governors.attr,
525 NULL
528 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
529 #define to_attr(a) container_of(a,struct freq_attr,attr)
531 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
533 struct cpufreq_policy * policy = to_policy(kobj);
534 struct freq_attr * fattr = to_attr(attr);
535 ssize_t ret;
536 policy = cpufreq_cpu_get(policy->cpu);
537 if (!policy)
538 return -EINVAL;
539 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
540 cpufreq_cpu_put(policy);
541 return ret;
544 static ssize_t store(struct kobject * kobj, struct attribute * attr,
545 const char * buf, size_t count)
547 struct cpufreq_policy * policy = to_policy(kobj);
548 struct freq_attr * fattr = to_attr(attr);
549 ssize_t ret;
550 policy = cpufreq_cpu_get(policy->cpu);
551 if (!policy)
552 return -EINVAL;
553 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
554 cpufreq_cpu_put(policy);
555 return ret;
558 static void cpufreq_sysfs_release(struct kobject * kobj)
560 struct cpufreq_policy * policy = to_policy(kobj);
561 dprintk("last reference is dropped\n");
562 complete(&policy->kobj_unregister);
565 static struct sysfs_ops sysfs_ops = {
566 .show = show,
567 .store = store,
570 static struct kobj_type ktype_cpufreq = {
571 .sysfs_ops = &sysfs_ops,
572 .default_attrs = default_attrs,
573 .release = cpufreq_sysfs_release,
578 * cpufreq_add_dev - add a CPU device
580 * Adds the cpufreq interface for a CPU device.
582 static int cpufreq_add_dev (struct sys_device * sys_dev)
584 unsigned int cpu = sys_dev->id;
585 int ret = 0;
586 struct cpufreq_policy new_policy;
587 struct cpufreq_policy *policy;
588 struct freq_attr **drv_attr;
589 struct sys_device *cpu_sys_dev;
590 unsigned long flags;
591 unsigned int j;
592 #ifdef CONFIG_SMP
593 struct cpufreq_policy *managed_policy;
594 #endif
596 if (cpu_is_offline(cpu))
597 return 0;
599 cpufreq_debug_disable_ratelimit();
600 dprintk("adding CPU %u\n", cpu);
602 #ifdef CONFIG_SMP
603 /* check whether a different CPU already registered this
604 * CPU because it is in the same boat. */
605 policy = cpufreq_cpu_get(cpu);
606 if (unlikely(policy)) {
607 cpufreq_cpu_put(policy);
608 cpufreq_debug_enable_ratelimit();
609 return 0;
611 #endif
613 if (!try_module_get(cpufreq_driver->owner)) {
614 ret = -EINVAL;
615 goto module_out;
618 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
619 if (!policy) {
620 ret = -ENOMEM;
621 goto nomem_out;
624 policy->cpu = cpu;
625 policy->cpus = cpumask_of_cpu(cpu);
627 mutex_init(&policy->lock);
628 mutex_lock(&policy->lock);
629 init_completion(&policy->kobj_unregister);
630 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
632 /* call driver. From then on the cpufreq must be able
633 * to accept all calls to ->verify and ->setpolicy for this CPU
635 ret = cpufreq_driver->init(policy);
636 if (ret) {
637 dprintk("initialization failed\n");
638 mutex_unlock(&policy->lock);
639 goto err_out;
642 #ifdef CONFIG_SMP
643 for_each_cpu_mask(j, policy->cpus) {
644 if (cpu == j)
645 continue;
647 /* check for existing affected CPUs. They may not be aware
648 * of it due to CPU Hotplug.
650 managed_policy = cpufreq_cpu_get(j);
651 if (unlikely(managed_policy)) {
652 spin_lock_irqsave(&cpufreq_driver_lock, flags);
653 managed_policy->cpus = policy->cpus;
654 cpufreq_cpu_data[cpu] = managed_policy;
655 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
657 dprintk("CPU already managed, adding link\n");
658 sysfs_create_link(&sys_dev->kobj,
659 &managed_policy->kobj, "cpufreq");
661 cpufreq_debug_enable_ratelimit();
662 mutex_unlock(&policy->lock);
663 ret = 0;
664 goto err_out_driver_exit; /* call driver->exit() */
667 #endif
668 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
670 /* prepare interface data */
671 policy->kobj.parent = &sys_dev->kobj;
672 policy->kobj.ktype = &ktype_cpufreq;
673 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
675 ret = kobject_register(&policy->kobj);
676 if (ret) {
677 mutex_unlock(&policy->lock);
678 goto err_out_driver_exit;
680 /* set up files for this cpu device */
681 drv_attr = cpufreq_driver->attr;
682 while ((drv_attr) && (*drv_attr)) {
683 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
684 drv_attr++;
686 if (cpufreq_driver->get)
687 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
688 if (cpufreq_driver->target)
689 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
691 spin_lock_irqsave(&cpufreq_driver_lock, flags);
692 for_each_cpu_mask(j, policy->cpus)
693 cpufreq_cpu_data[j] = policy;
694 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
696 /* symlink affected CPUs */
697 for_each_cpu_mask(j, policy->cpus) {
698 if (j == cpu)
699 continue;
700 if (!cpu_online(j))
701 continue;
703 dprintk("CPU %u already managed, adding link\n", j);
704 cpufreq_cpu_get(cpu);
705 cpu_sys_dev = get_cpu_sysdev(j);
706 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
707 "cpufreq");
710 policy->governor = NULL; /* to assure that the starting sequence is
711 * run in cpufreq_set_policy */
712 mutex_unlock(&policy->lock);
714 /* set default policy */
715 ret = cpufreq_set_policy(&new_policy);
716 if (ret) {
717 dprintk("setting policy failed\n");
718 goto err_out_unregister;
721 module_put(cpufreq_driver->owner);
722 dprintk("initialization complete\n");
723 cpufreq_debug_enable_ratelimit();
725 return 0;
728 err_out_unregister:
729 spin_lock_irqsave(&cpufreq_driver_lock, flags);
730 for_each_cpu_mask(j, policy->cpus)
731 cpufreq_cpu_data[j] = NULL;
732 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
734 kobject_unregister(&policy->kobj);
735 wait_for_completion(&policy->kobj_unregister);
737 err_out_driver_exit:
738 if (cpufreq_driver->exit)
739 cpufreq_driver->exit(policy);
741 err_out:
742 kfree(policy);
744 nomem_out:
745 module_put(cpufreq_driver->owner);
746 module_out:
747 cpufreq_debug_enable_ratelimit();
748 return ret;
753 * cpufreq_remove_dev - remove a CPU device
755 * Removes the cpufreq interface for a CPU device.
757 static int cpufreq_remove_dev (struct sys_device * sys_dev)
759 unsigned int cpu = sys_dev->id;
760 unsigned long flags;
761 struct cpufreq_policy *data;
762 #ifdef CONFIG_SMP
763 struct sys_device *cpu_sys_dev;
764 unsigned int j;
765 #endif
767 cpufreq_debug_disable_ratelimit();
768 dprintk("unregistering CPU %u\n", cpu);
770 spin_lock_irqsave(&cpufreq_driver_lock, flags);
771 data = cpufreq_cpu_data[cpu];
773 if (!data) {
774 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
775 cpufreq_debug_enable_ratelimit();
776 return -EINVAL;
778 cpufreq_cpu_data[cpu] = NULL;
781 #ifdef CONFIG_SMP
782 /* if this isn't the CPU which is the parent of the kobj, we
783 * only need to unlink, put and exit
785 if (unlikely(cpu != data->cpu)) {
786 dprintk("removing link\n");
787 cpu_clear(cpu, data->cpus);
788 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
789 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
790 cpufreq_cpu_put(data);
791 cpufreq_debug_enable_ratelimit();
792 return 0;
794 #endif
797 if (!kobject_get(&data->kobj)) {
798 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
799 cpufreq_debug_enable_ratelimit();
800 return -EFAULT;
803 #ifdef CONFIG_SMP
804 /* if we have other CPUs still registered, we need to unlink them,
805 * or else wait_for_completion below will lock up. Clean the
806 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
807 * links afterwards.
809 if (unlikely(cpus_weight(data->cpus) > 1)) {
810 for_each_cpu_mask(j, data->cpus) {
811 if (j == cpu)
812 continue;
813 cpufreq_cpu_data[j] = NULL;
817 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
819 if (unlikely(cpus_weight(data->cpus) > 1)) {
820 for_each_cpu_mask(j, data->cpus) {
821 if (j == cpu)
822 continue;
823 dprintk("removing link for cpu %u\n", j);
824 cpu_sys_dev = get_cpu_sysdev(j);
825 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
826 cpufreq_cpu_put(data);
829 #else
830 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
831 #endif
833 mutex_lock(&data->lock);
834 if (cpufreq_driver->target)
835 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
836 mutex_unlock(&data->lock);
838 kobject_unregister(&data->kobj);
840 kobject_put(&data->kobj);
842 /* we need to make sure that the underlying kobj is actually
843 * not referenced anymore by anybody before we proceed with
844 * unloading.
846 dprintk("waiting for dropping of refcount\n");
847 wait_for_completion(&data->kobj_unregister);
848 dprintk("wait complete\n");
850 if (cpufreq_driver->exit)
851 cpufreq_driver->exit(data);
853 kfree(data);
855 cpufreq_debug_enable_ratelimit();
856 return 0;
860 static void handle_update(void *data)
862 unsigned int cpu = (unsigned int)(long)data;
863 dprintk("handle_update for cpu %u called\n", cpu);
864 cpufreq_update_policy(cpu);
868 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
869 * @cpu: cpu number
870 * @old_freq: CPU frequency the kernel thinks the CPU runs at
871 * @new_freq: CPU frequency the CPU actually runs at
873 * We adjust to current frequency first, and need to clean up later. So either call
874 * to cpufreq_update_policy() or schedule handle_update()).
876 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
878 struct cpufreq_freqs freqs;
880 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
881 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
883 freqs.cpu = cpu;
884 freqs.old = old_freq;
885 freqs.new = new_freq;
886 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
887 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
892 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
893 * @cpu: CPU number
895 * This is the last known freq, without actually getting it from the driver.
896 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
898 unsigned int cpufreq_quick_get(unsigned int cpu)
900 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
901 unsigned int ret = 0;
903 if (policy) {
904 mutex_lock(&policy->lock);
905 ret = policy->cur;
906 mutex_unlock(&policy->lock);
907 cpufreq_cpu_put(policy);
910 return (ret);
912 EXPORT_SYMBOL(cpufreq_quick_get);
916 * cpufreq_get - get the current CPU frequency (in kHz)
917 * @cpu: CPU number
919 * Get the CPU current (static) CPU frequency
921 unsigned int cpufreq_get(unsigned int cpu)
923 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
924 unsigned int ret = 0;
926 if (!policy)
927 return 0;
929 if (!cpufreq_driver->get)
930 goto out;
932 mutex_lock(&policy->lock);
934 ret = cpufreq_driver->get(cpu);
936 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
937 /* verify no discrepancy between actual and saved value exists */
938 if (unlikely(ret != policy->cur)) {
939 cpufreq_out_of_sync(cpu, policy->cur, ret);
940 schedule_work(&policy->update);
944 mutex_unlock(&policy->lock);
946 out:
947 cpufreq_cpu_put(policy);
949 return (ret);
951 EXPORT_SYMBOL(cpufreq_get);
955 * cpufreq_suspend - let the low level driver prepare for suspend
958 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
960 int cpu = sysdev->id;
961 unsigned int ret = 0;
962 unsigned int cur_freq = 0;
963 struct cpufreq_policy *cpu_policy;
965 dprintk("resuming cpu %u\n", cpu);
967 if (!cpu_online(cpu))
968 return 0;
970 /* we may be lax here as interrupts are off. Nonetheless
971 * we need to grab the correct cpu policy, as to check
972 * whether we really run on this CPU.
975 cpu_policy = cpufreq_cpu_get(cpu);
976 if (!cpu_policy)
977 return -EINVAL;
979 /* only handle each CPU group once */
980 if (unlikely(cpu_policy->cpu != cpu)) {
981 cpufreq_cpu_put(cpu_policy);
982 return 0;
985 if (cpufreq_driver->suspend) {
986 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
987 if (ret) {
988 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
989 "step on CPU %u\n", cpu_policy->cpu);
990 cpufreq_cpu_put(cpu_policy);
991 return ret;
996 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
997 goto out;
999 if (cpufreq_driver->get)
1000 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1002 if (!cur_freq || !cpu_policy->cur) {
1003 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1004 "frequency is what timing core thinks it is.\n");
1005 goto out;
1008 if (unlikely(cur_freq != cpu_policy->cur)) {
1009 struct cpufreq_freqs freqs;
1011 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1012 dprintk("Warning: CPU frequency is %u, "
1013 "cpufreq assumed %u kHz.\n",
1014 cur_freq, cpu_policy->cur);
1016 freqs.cpu = cpu;
1017 freqs.old = cpu_policy->cur;
1018 freqs.new = cur_freq;
1020 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
1021 CPUFREQ_SUSPENDCHANGE, &freqs);
1022 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1024 cpu_policy->cur = cur_freq;
1027 out:
1028 cpufreq_cpu_put(cpu_policy);
1029 return 0;
1033 * cpufreq_resume - restore proper CPU frequency handling after resume
1035 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1036 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
1037 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1038 * restored.
1040 static int cpufreq_resume(struct sys_device * sysdev)
1042 int cpu = sysdev->id;
1043 unsigned int ret = 0;
1044 struct cpufreq_policy *cpu_policy;
1046 dprintk("resuming cpu %u\n", cpu);
1048 if (!cpu_online(cpu))
1049 return 0;
1051 /* we may be lax here as interrupts are off. Nonetheless
1052 * we need to grab the correct cpu policy, as to check
1053 * whether we really run on this CPU.
1056 cpu_policy = cpufreq_cpu_get(cpu);
1057 if (!cpu_policy)
1058 return -EINVAL;
1060 /* only handle each CPU group once */
1061 if (unlikely(cpu_policy->cpu != cpu)) {
1062 cpufreq_cpu_put(cpu_policy);
1063 return 0;
1066 if (cpufreq_driver->resume) {
1067 ret = cpufreq_driver->resume(cpu_policy);
1068 if (ret) {
1069 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1070 "step on CPU %u\n", cpu_policy->cpu);
1071 cpufreq_cpu_put(cpu_policy);
1072 return ret;
1076 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1077 unsigned int cur_freq = 0;
1079 if (cpufreq_driver->get)
1080 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1082 if (!cur_freq || !cpu_policy->cur) {
1083 printk(KERN_ERR "cpufreq: resume failed to assert "
1084 "current frequency is what timing core "
1085 "thinks it is.\n");
1086 goto out;
1089 if (unlikely(cur_freq != cpu_policy->cur)) {
1090 struct cpufreq_freqs freqs;
1092 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1093 dprintk("Warning: CPU frequency"
1094 "is %u, cpufreq assumed %u kHz.\n",
1095 cur_freq, cpu_policy->cur);
1097 freqs.cpu = cpu;
1098 freqs.old = cpu_policy->cur;
1099 freqs.new = cur_freq;
1101 blocking_notifier_call_chain(
1102 &cpufreq_transition_notifier_list,
1103 CPUFREQ_RESUMECHANGE, &freqs);
1104 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1106 cpu_policy->cur = cur_freq;
1110 out:
1111 schedule_work(&cpu_policy->update);
1112 cpufreq_cpu_put(cpu_policy);
1113 return ret;
1116 static struct sysdev_driver cpufreq_sysdev_driver = {
1117 .add = cpufreq_add_dev,
1118 .remove = cpufreq_remove_dev,
1119 .suspend = cpufreq_suspend,
1120 .resume = cpufreq_resume,
1124 /*********************************************************************
1125 * NOTIFIER LISTS INTERFACE *
1126 *********************************************************************/
1129 * cpufreq_register_notifier - register a driver with cpufreq
1130 * @nb: notifier function to register
1131 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1133 * Add a driver to one of two lists: either a list of drivers that
1134 * are notified about clock rate changes (once before and once after
1135 * the transition), or a list of drivers that are notified about
1136 * changes in cpufreq policy.
1138 * This function may sleep, and has the same return conditions as
1139 * blocking_notifier_chain_register.
1141 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1143 int ret;
1145 switch (list) {
1146 case CPUFREQ_TRANSITION_NOTIFIER:
1147 ret = blocking_notifier_chain_register(
1148 &cpufreq_transition_notifier_list, nb);
1149 break;
1150 case CPUFREQ_POLICY_NOTIFIER:
1151 ret = blocking_notifier_chain_register(
1152 &cpufreq_policy_notifier_list, nb);
1153 break;
1154 default:
1155 ret = -EINVAL;
1158 return ret;
1160 EXPORT_SYMBOL(cpufreq_register_notifier);
1164 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1165 * @nb: notifier block to be unregistered
1166 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1168 * Remove a driver from the CPU frequency notifier list.
1170 * This function may sleep, and has the same return conditions as
1171 * blocking_notifier_chain_unregister.
1173 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1175 int ret;
1177 switch (list) {
1178 case CPUFREQ_TRANSITION_NOTIFIER:
1179 ret = blocking_notifier_chain_unregister(
1180 &cpufreq_transition_notifier_list, nb);
1181 break;
1182 case CPUFREQ_POLICY_NOTIFIER:
1183 ret = blocking_notifier_chain_unregister(
1184 &cpufreq_policy_notifier_list, nb);
1185 break;
1186 default:
1187 ret = -EINVAL;
1190 return ret;
1192 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1195 /*********************************************************************
1196 * GOVERNORS *
1197 *********************************************************************/
1200 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1201 unsigned int target_freq,
1202 unsigned int relation)
1204 int retval = -EINVAL;
1206 lock_cpu_hotplug();
1207 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1208 target_freq, relation);
1209 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1210 retval = cpufreq_driver->target(policy, target_freq, relation);
1212 unlock_cpu_hotplug();
1214 return retval;
1216 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1218 int cpufreq_driver_target(struct cpufreq_policy *policy,
1219 unsigned int target_freq,
1220 unsigned int relation)
1222 int ret;
1224 policy = cpufreq_cpu_get(policy->cpu);
1225 if (!policy)
1226 return -EINVAL;
1228 mutex_lock(&policy->lock);
1230 ret = __cpufreq_driver_target(policy, target_freq, relation);
1232 mutex_unlock(&policy->lock);
1234 cpufreq_cpu_put(policy);
1235 return ret;
1237 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1240 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1242 int ret;
1244 if (!try_module_get(policy->governor->owner))
1245 return -EINVAL;
1247 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1248 ret = policy->governor->governor(policy, event);
1250 /* we keep one module reference alive for each CPU governed by this CPU */
1251 if ((event != CPUFREQ_GOV_START) || ret)
1252 module_put(policy->governor->owner);
1253 if ((event == CPUFREQ_GOV_STOP) && !ret)
1254 module_put(policy->governor->owner);
1256 return ret;
1260 int cpufreq_governor(unsigned int cpu, unsigned int event)
1262 int ret = 0;
1263 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1265 if (!policy)
1266 return -EINVAL;
1268 mutex_lock(&policy->lock);
1269 ret = __cpufreq_governor(policy, event);
1270 mutex_unlock(&policy->lock);
1272 cpufreq_cpu_put(policy);
1273 return ret;
1275 EXPORT_SYMBOL_GPL(cpufreq_governor);
1278 int cpufreq_register_governor(struct cpufreq_governor *governor)
1280 struct cpufreq_governor *t;
1282 if (!governor)
1283 return -EINVAL;
1285 mutex_lock(&cpufreq_governor_mutex);
1287 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1288 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1289 mutex_unlock(&cpufreq_governor_mutex);
1290 return -EBUSY;
1293 list_add(&governor->governor_list, &cpufreq_governor_list);
1295 mutex_unlock(&cpufreq_governor_mutex);
1296 return 0;
1298 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1301 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1303 if (!governor)
1304 return;
1306 mutex_lock(&cpufreq_governor_mutex);
1307 list_del(&governor->governor_list);
1308 mutex_unlock(&cpufreq_governor_mutex);
1309 return;
1311 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1315 /*********************************************************************
1316 * POLICY INTERFACE *
1317 *********************************************************************/
1320 * cpufreq_get_policy - get the current cpufreq_policy
1321 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1323 * Reads the current cpufreq policy.
1325 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1327 struct cpufreq_policy *cpu_policy;
1328 if (!policy)
1329 return -EINVAL;
1331 cpu_policy = cpufreq_cpu_get(cpu);
1332 if (!cpu_policy)
1333 return -EINVAL;
1335 mutex_lock(&cpu_policy->lock);
1336 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1337 mutex_unlock(&cpu_policy->lock);
1339 cpufreq_cpu_put(cpu_policy);
1340 return 0;
1342 EXPORT_SYMBOL(cpufreq_get_policy);
1345 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1347 int ret = 0;
1349 cpufreq_debug_disable_ratelimit();
1350 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1351 policy->min, policy->max);
1353 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
1355 /* verify the cpu speed can be set within this limit */
1356 ret = cpufreq_driver->verify(policy);
1357 if (ret)
1358 goto error_out;
1360 /* adjust if necessary - all reasons */
1361 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1362 CPUFREQ_ADJUST, policy);
1364 /* adjust if necessary - hardware incompatibility*/
1365 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1366 CPUFREQ_INCOMPATIBLE, policy);
1368 /* verify the cpu speed can be set within this limit,
1369 which might be different to the first one */
1370 ret = cpufreq_driver->verify(policy);
1371 if (ret)
1372 goto error_out;
1374 /* notification of the new policy */
1375 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1376 CPUFREQ_NOTIFY, policy);
1378 data->min = policy->min;
1379 data->max = policy->max;
1381 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1383 if (cpufreq_driver->setpolicy) {
1384 data->policy = policy->policy;
1385 dprintk("setting range\n");
1386 ret = cpufreq_driver->setpolicy(policy);
1387 } else {
1388 if (policy->governor != data->governor) {
1389 /* save old, working values */
1390 struct cpufreq_governor *old_gov = data->governor;
1392 dprintk("governor switch\n");
1394 /* end old governor */
1395 if (data->governor)
1396 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1398 /* start new governor */
1399 data->governor = policy->governor;
1400 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1401 /* new governor failed, so re-start old one */
1402 dprintk("starting governor %s failed\n", data->governor->name);
1403 if (old_gov) {
1404 data->governor = old_gov;
1405 __cpufreq_governor(data, CPUFREQ_GOV_START);
1407 ret = -EINVAL;
1408 goto error_out;
1410 /* might be a policy change, too, so fall through */
1412 dprintk("governor: change or update limits\n");
1413 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1416 error_out:
1417 cpufreq_debug_enable_ratelimit();
1418 return ret;
1422 * cpufreq_set_policy - set a new CPUFreq policy
1423 * @policy: policy to be set.
1425 * Sets a new CPU frequency and voltage scaling policy.
1427 int cpufreq_set_policy(struct cpufreq_policy *policy)
1429 int ret = 0;
1430 struct cpufreq_policy *data;
1432 if (!policy)
1433 return -EINVAL;
1435 data = cpufreq_cpu_get(policy->cpu);
1436 if (!data)
1437 return -EINVAL;
1439 /* lock this CPU */
1440 mutex_lock(&data->lock);
1442 ret = __cpufreq_set_policy(data, policy);
1443 data->user_policy.min = data->min;
1444 data->user_policy.max = data->max;
1445 data->user_policy.policy = data->policy;
1446 data->user_policy.governor = data->governor;
1448 mutex_unlock(&data->lock);
1449 cpufreq_cpu_put(data);
1451 return ret;
1453 EXPORT_SYMBOL(cpufreq_set_policy);
1457 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1458 * @cpu: CPU which shall be re-evaluated
1460 * Usefull for policy notifiers which have different necessities
1461 * at different times.
1463 int cpufreq_update_policy(unsigned int cpu)
1465 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1466 struct cpufreq_policy policy;
1467 int ret = 0;
1469 if (!data)
1470 return -ENODEV;
1472 mutex_lock(&data->lock);
1474 dprintk("updating policy for CPU %u\n", cpu);
1475 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1476 policy.min = data->user_policy.min;
1477 policy.max = data->user_policy.max;
1478 policy.policy = data->user_policy.policy;
1479 policy.governor = data->user_policy.governor;
1481 /* BIOS might change freq behind our back
1482 -> ask driver for current freq and notify governors about a change */
1483 if (cpufreq_driver->get) {
1484 policy.cur = cpufreq_driver->get(cpu);
1485 if (!data->cur) {
1486 dprintk("Driver did not initialize current freq");
1487 data->cur = policy.cur;
1488 } else {
1489 if (data->cur != policy.cur)
1490 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1494 ret = __cpufreq_set_policy(data, &policy);
1496 mutex_unlock(&data->lock);
1498 cpufreq_cpu_put(data);
1499 return ret;
1501 EXPORT_SYMBOL(cpufreq_update_policy);
1503 #ifdef CONFIG_HOTPLUG_CPU
1504 static int cpufreq_cpu_callback(struct notifier_block *nfb,
1505 unsigned long action, void *hcpu)
1507 unsigned int cpu = (unsigned long)hcpu;
1508 struct cpufreq_policy *policy;
1509 struct sys_device *sys_dev;
1511 sys_dev = get_cpu_sysdev(cpu);
1513 if (sys_dev) {
1514 switch (action) {
1515 case CPU_ONLINE:
1516 cpufreq_add_dev(sys_dev);
1517 break;
1518 case CPU_DOWN_PREPARE:
1520 * We attempt to put this cpu in lowest frequency
1521 * possible before going down. This will permit
1522 * hardware-managed P-State to switch other related
1523 * threads to min or higher speeds if possible.
1525 policy = cpufreq_cpu_data[cpu];
1526 if (policy) {
1527 cpufreq_driver_target(policy, policy->min,
1528 CPUFREQ_RELATION_H);
1530 break;
1531 case CPU_DEAD:
1532 cpufreq_remove_dev(sys_dev);
1533 break;
1536 return NOTIFY_OK;
1539 static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
1541 .notifier_call = cpufreq_cpu_callback,
1543 #endif /* CONFIG_HOTPLUG_CPU */
1545 /*********************************************************************
1546 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1547 *********************************************************************/
1550 * cpufreq_register_driver - register a CPU Frequency driver
1551 * @driver_data: A struct cpufreq_driver containing the values#
1552 * submitted by the CPU Frequency driver.
1554 * Registers a CPU Frequency driver to this core code. This code
1555 * returns zero on success, -EBUSY when another driver got here first
1556 * (and isn't unregistered in the meantime).
1559 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1561 unsigned long flags;
1562 int ret;
1564 if (!driver_data || !driver_data->verify || !driver_data->init ||
1565 ((!driver_data->setpolicy) && (!driver_data->target)))
1566 return -EINVAL;
1568 dprintk("trying to register driver %s\n", driver_data->name);
1570 if (driver_data->setpolicy)
1571 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1573 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1574 if (cpufreq_driver) {
1575 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1576 return -EBUSY;
1578 cpufreq_driver = driver_data;
1579 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1581 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1583 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1584 int i;
1585 ret = -ENODEV;
1587 /* check for at least one working CPU */
1588 for (i=0; i<NR_CPUS; i++)
1589 if (cpufreq_cpu_data[i])
1590 ret = 0;
1592 /* if all ->init() calls failed, unregister */
1593 if (ret) {
1594 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1595 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1597 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1598 cpufreq_driver = NULL;
1599 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1603 if (!ret) {
1604 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1605 dprintk("driver %s up and running\n", driver_data->name);
1606 cpufreq_debug_enable_ratelimit();
1609 return (ret);
1611 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1615 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1617 * Unregister the current CPUFreq driver. Only call this if you have
1618 * the right to do so, i.e. if you have succeeded in initialising before!
1619 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1620 * currently not initialised.
1622 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1624 unsigned long flags;
1626 cpufreq_debug_disable_ratelimit();
1628 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1629 cpufreq_debug_enable_ratelimit();
1630 return -EINVAL;
1633 dprintk("unregistering driver %s\n", driver->name);
1635 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1636 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1638 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1639 cpufreq_driver = NULL;
1640 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1642 return 0;
1644 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);