Linux 2.6.17.7
[linux/fpc-iii.git] / drivers / cpufreq / cpufreq.c
blob29b2fa5534ae3221017a49517a5017e56c328b4b
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;
324 /* drivers/base/cpu.c */
325 extern struct sysdev_class cpu_sysdev_class;
329 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
331 * Write out information from cpufreq_driver->policy[cpu]; object must be
332 * "unsigned int".
335 #define show_one(file_name, object) \
336 static ssize_t show_##file_name \
337 (struct cpufreq_policy * policy, char *buf) \
339 return sprintf (buf, "%u\n", policy->object); \
342 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
343 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
344 show_one(scaling_min_freq, min);
345 show_one(scaling_max_freq, max);
346 show_one(scaling_cur_freq, cur);
348 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
351 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
353 #define store_one(file_name, object) \
354 static ssize_t store_##file_name \
355 (struct cpufreq_policy * policy, const char *buf, size_t count) \
357 unsigned int ret = -EINVAL; \
358 struct cpufreq_policy new_policy; \
360 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
361 if (ret) \
362 return -EINVAL; \
364 ret = sscanf (buf, "%u", &new_policy.object); \
365 if (ret != 1) \
366 return -EINVAL; \
368 mutex_lock(&policy->lock); \
369 ret = __cpufreq_set_policy(policy, &new_policy); \
370 policy->user_policy.object = policy->object; \
371 mutex_unlock(&policy->lock); \
373 return ret ? ret : count; \
376 store_one(scaling_min_freq,min);
377 store_one(scaling_max_freq,max);
380 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
382 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
384 unsigned int cur_freq = cpufreq_get(policy->cpu);
385 if (!cur_freq)
386 return sprintf(buf, "<unknown>");
387 return sprintf(buf, "%u\n", cur_freq);
392 * show_scaling_governor - show the current policy for the specified CPU
394 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
396 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
397 return sprintf(buf, "powersave\n");
398 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
399 return sprintf(buf, "performance\n");
400 else if (policy->governor)
401 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
402 return -EINVAL;
407 * store_scaling_governor - store policy for the specified CPU
409 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
410 const char *buf, size_t count)
412 unsigned int ret = -EINVAL;
413 char str_governor[16];
414 struct cpufreq_policy new_policy;
416 ret = cpufreq_get_policy(&new_policy, policy->cpu);
417 if (ret)
418 return ret;
420 ret = sscanf (buf, "%15s", str_governor);
421 if (ret != 1)
422 return -EINVAL;
424 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
425 return -EINVAL;
427 /* Do not use cpufreq_set_policy here or the user_policy.max
428 will be wrongly overridden */
429 mutex_lock(&policy->lock);
430 ret = __cpufreq_set_policy(policy, &new_policy);
432 policy->user_policy.policy = policy->policy;
433 policy->user_policy.governor = policy->governor;
434 mutex_unlock(&policy->lock);
436 return ret ? ret : count;
440 * show_scaling_driver - show the cpufreq driver currently loaded
442 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
444 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
448 * show_scaling_available_governors - show the available CPUfreq governors
450 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
451 char *buf)
453 ssize_t i = 0;
454 struct cpufreq_governor *t;
456 if (!cpufreq_driver->target) {
457 i += sprintf(buf, "performance powersave");
458 goto out;
461 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
462 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
463 goto out;
464 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
466 out:
467 i += sprintf(&buf[i], "\n");
468 return i;
471 * show_affected_cpus - show the CPUs affected by each transition
473 static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
475 ssize_t i = 0;
476 unsigned int cpu;
478 for_each_cpu_mask(cpu, policy->cpus) {
479 if (i)
480 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
481 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
482 if (i >= (PAGE_SIZE - 5))
483 break;
485 i += sprintf(&buf[i], "\n");
486 return i;
490 #define define_one_ro(_name) \
491 static struct freq_attr _name = \
492 __ATTR(_name, 0444, show_##_name, NULL)
494 #define define_one_ro0400(_name) \
495 static struct freq_attr _name = \
496 __ATTR(_name, 0400, show_##_name, NULL)
498 #define define_one_rw(_name) \
499 static struct freq_attr _name = \
500 __ATTR(_name, 0644, show_##_name, store_##_name)
502 define_one_ro0400(cpuinfo_cur_freq);
503 define_one_ro(cpuinfo_min_freq);
504 define_one_ro(cpuinfo_max_freq);
505 define_one_ro(scaling_available_governors);
506 define_one_ro(scaling_driver);
507 define_one_ro(scaling_cur_freq);
508 define_one_ro(affected_cpus);
509 define_one_rw(scaling_min_freq);
510 define_one_rw(scaling_max_freq);
511 define_one_rw(scaling_governor);
513 static struct attribute * default_attrs[] = {
514 &cpuinfo_min_freq.attr,
515 &cpuinfo_max_freq.attr,
516 &scaling_min_freq.attr,
517 &scaling_max_freq.attr,
518 &affected_cpus.attr,
519 &scaling_governor.attr,
520 &scaling_driver.attr,
521 &scaling_available_governors.attr,
522 NULL
525 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
526 #define to_attr(a) container_of(a,struct freq_attr,attr)
528 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
530 struct cpufreq_policy * policy = to_policy(kobj);
531 struct freq_attr * fattr = to_attr(attr);
532 ssize_t ret;
533 policy = cpufreq_cpu_get(policy->cpu);
534 if (!policy)
535 return -EINVAL;
536 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
537 cpufreq_cpu_put(policy);
538 return ret;
541 static ssize_t store(struct kobject * kobj, struct attribute * attr,
542 const char * buf, size_t count)
544 struct cpufreq_policy * policy = to_policy(kobj);
545 struct freq_attr * fattr = to_attr(attr);
546 ssize_t ret;
547 policy = cpufreq_cpu_get(policy->cpu);
548 if (!policy)
549 return -EINVAL;
550 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
551 cpufreq_cpu_put(policy);
552 return ret;
555 static void cpufreq_sysfs_release(struct kobject * kobj)
557 struct cpufreq_policy * policy = to_policy(kobj);
558 dprintk("last reference is dropped\n");
559 complete(&policy->kobj_unregister);
562 static struct sysfs_ops sysfs_ops = {
563 .show = show,
564 .store = store,
567 static struct kobj_type ktype_cpufreq = {
568 .sysfs_ops = &sysfs_ops,
569 .default_attrs = default_attrs,
570 .release = cpufreq_sysfs_release,
575 * cpufreq_add_dev - add a CPU device
577 * Adds the cpufreq interface for a CPU device.
579 static int cpufreq_add_dev (struct sys_device * sys_dev)
581 unsigned int cpu = sys_dev->id;
582 int ret = 0;
583 struct cpufreq_policy new_policy;
584 struct cpufreq_policy *policy;
585 struct freq_attr **drv_attr;
586 struct sys_device *cpu_sys_dev;
587 unsigned long flags;
588 unsigned int j;
589 #ifdef CONFIG_SMP
590 struct cpufreq_policy *managed_policy;
591 #endif
593 if (cpu_is_offline(cpu))
594 return 0;
596 cpufreq_debug_disable_ratelimit();
597 dprintk("adding CPU %u\n", cpu);
599 #ifdef CONFIG_SMP
600 /* check whether a different CPU already registered this
601 * CPU because it is in the same boat. */
602 policy = cpufreq_cpu_get(cpu);
603 if (unlikely(policy)) {
604 cpufreq_cpu_put(policy);
605 cpufreq_debug_enable_ratelimit();
606 return 0;
608 #endif
610 if (!try_module_get(cpufreq_driver->owner)) {
611 ret = -EINVAL;
612 goto module_out;
615 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
616 if (!policy) {
617 ret = -ENOMEM;
618 goto nomem_out;
621 policy->cpu = cpu;
622 policy->cpus = cpumask_of_cpu(cpu);
624 mutex_init(&policy->lock);
625 mutex_lock(&policy->lock);
626 init_completion(&policy->kobj_unregister);
627 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
629 /* call driver. From then on the cpufreq must be able
630 * to accept all calls to ->verify and ->setpolicy for this CPU
632 ret = cpufreq_driver->init(policy);
633 if (ret) {
634 dprintk("initialization failed\n");
635 mutex_unlock(&policy->lock);
636 goto err_out;
639 #ifdef CONFIG_SMP
640 for_each_cpu_mask(j, policy->cpus) {
641 if (cpu == j)
642 continue;
644 /* check for existing affected CPUs. They may not be aware
645 * of it due to CPU Hotplug.
647 managed_policy = cpufreq_cpu_get(j);
648 if (unlikely(managed_policy)) {
649 spin_lock_irqsave(&cpufreq_driver_lock, flags);
650 managed_policy->cpus = policy->cpus;
651 cpufreq_cpu_data[cpu] = managed_policy;
652 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
654 dprintk("CPU already managed, adding link\n");
655 sysfs_create_link(&sys_dev->kobj,
656 &managed_policy->kobj, "cpufreq");
658 cpufreq_debug_enable_ratelimit();
659 mutex_unlock(&policy->lock);
660 ret = 0;
661 goto err_out_driver_exit; /* call driver->exit() */
664 #endif
665 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
667 /* prepare interface data */
668 policy->kobj.parent = &sys_dev->kobj;
669 policy->kobj.ktype = &ktype_cpufreq;
670 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
672 ret = kobject_register(&policy->kobj);
673 if (ret) {
674 mutex_unlock(&policy->lock);
675 goto err_out_driver_exit;
677 /* set up files for this cpu device */
678 drv_attr = cpufreq_driver->attr;
679 while ((drv_attr) && (*drv_attr)) {
680 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
681 drv_attr++;
683 if (cpufreq_driver->get)
684 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
685 if (cpufreq_driver->target)
686 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
688 spin_lock_irqsave(&cpufreq_driver_lock, flags);
689 for_each_cpu_mask(j, policy->cpus)
690 cpufreq_cpu_data[j] = policy;
691 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
693 /* symlink affected CPUs */
694 for_each_cpu_mask(j, policy->cpus) {
695 if (j == cpu)
696 continue;
697 if (!cpu_online(j))
698 continue;
700 dprintk("CPU %u already managed, adding link\n", j);
701 cpufreq_cpu_get(cpu);
702 cpu_sys_dev = get_cpu_sysdev(j);
703 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
704 "cpufreq");
707 policy->governor = NULL; /* to assure that the starting sequence is
708 * run in cpufreq_set_policy */
709 mutex_unlock(&policy->lock);
711 /* set default policy */
712 ret = cpufreq_set_policy(&new_policy);
713 if (ret) {
714 dprintk("setting policy failed\n");
715 goto err_out_unregister;
718 module_put(cpufreq_driver->owner);
719 dprintk("initialization complete\n");
720 cpufreq_debug_enable_ratelimit();
722 return 0;
725 err_out_unregister:
726 spin_lock_irqsave(&cpufreq_driver_lock, flags);
727 for_each_cpu_mask(j, policy->cpus)
728 cpufreq_cpu_data[j] = NULL;
729 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
731 kobject_unregister(&policy->kobj);
732 wait_for_completion(&policy->kobj_unregister);
734 err_out_driver_exit:
735 if (cpufreq_driver->exit)
736 cpufreq_driver->exit(policy);
738 err_out:
739 kfree(policy);
741 nomem_out:
742 module_put(cpufreq_driver->owner);
743 module_out:
744 cpufreq_debug_enable_ratelimit();
745 return ret;
750 * cpufreq_remove_dev - remove a CPU device
752 * Removes the cpufreq interface for a CPU device.
754 static int cpufreq_remove_dev (struct sys_device * sys_dev)
756 unsigned int cpu = sys_dev->id;
757 unsigned long flags;
758 struct cpufreq_policy *data;
759 #ifdef CONFIG_SMP
760 struct sys_device *cpu_sys_dev;
761 unsigned int j;
762 #endif
764 cpufreq_debug_disable_ratelimit();
765 dprintk("unregistering CPU %u\n", cpu);
767 spin_lock_irqsave(&cpufreq_driver_lock, flags);
768 data = cpufreq_cpu_data[cpu];
770 if (!data) {
771 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
772 cpufreq_debug_enable_ratelimit();
773 return -EINVAL;
775 cpufreq_cpu_data[cpu] = NULL;
778 #ifdef CONFIG_SMP
779 /* if this isn't the CPU which is the parent of the kobj, we
780 * only need to unlink, put and exit
782 if (unlikely(cpu != data->cpu)) {
783 dprintk("removing link\n");
784 cpu_clear(cpu, data->cpus);
785 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
786 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
787 cpufreq_cpu_put(data);
788 cpufreq_debug_enable_ratelimit();
789 return 0;
791 #endif
794 if (!kobject_get(&data->kobj)) {
795 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
796 cpufreq_debug_enable_ratelimit();
797 return -EFAULT;
800 #ifdef CONFIG_SMP
801 /* if we have other CPUs still registered, we need to unlink them,
802 * or else wait_for_completion below will lock up. Clean the
803 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
804 * links afterwards.
806 if (unlikely(cpus_weight(data->cpus) > 1)) {
807 for_each_cpu_mask(j, data->cpus) {
808 if (j == cpu)
809 continue;
810 cpufreq_cpu_data[j] = NULL;
814 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
816 if (unlikely(cpus_weight(data->cpus) > 1)) {
817 for_each_cpu_mask(j, data->cpus) {
818 if (j == cpu)
819 continue;
820 dprintk("removing link for cpu %u\n", j);
821 cpu_sys_dev = get_cpu_sysdev(j);
822 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
823 cpufreq_cpu_put(data);
826 #else
827 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
828 #endif
830 mutex_lock(&data->lock);
831 if (cpufreq_driver->target)
832 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
833 mutex_unlock(&data->lock);
835 kobject_unregister(&data->kobj);
837 kobject_put(&data->kobj);
839 /* we need to make sure that the underlying kobj is actually
840 * not referenced anymore by anybody before we proceed with
841 * unloading.
843 dprintk("waiting for dropping of refcount\n");
844 wait_for_completion(&data->kobj_unregister);
845 dprintk("wait complete\n");
847 if (cpufreq_driver->exit)
848 cpufreq_driver->exit(data);
850 kfree(data);
852 cpufreq_debug_enable_ratelimit();
853 return 0;
857 static void handle_update(void *data)
859 unsigned int cpu = (unsigned int)(long)data;
860 dprintk("handle_update for cpu %u called\n", cpu);
861 cpufreq_update_policy(cpu);
865 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
866 * @cpu: cpu number
867 * @old_freq: CPU frequency the kernel thinks the CPU runs at
868 * @new_freq: CPU frequency the CPU actually runs at
870 * We adjust to current frequency first, and need to clean up later. So either call
871 * to cpufreq_update_policy() or schedule handle_update()).
873 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
875 struct cpufreq_freqs freqs;
877 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
878 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
880 freqs.cpu = cpu;
881 freqs.old = old_freq;
882 freqs.new = new_freq;
883 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
884 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
889 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
890 * @cpu: CPU number
892 * This is the last known freq, without actually getting it from the driver.
893 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
895 unsigned int cpufreq_quick_get(unsigned int cpu)
897 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
898 unsigned int ret = 0;
900 if (policy) {
901 mutex_lock(&policy->lock);
902 ret = policy->cur;
903 mutex_unlock(&policy->lock);
904 cpufreq_cpu_put(policy);
907 return (ret);
909 EXPORT_SYMBOL(cpufreq_quick_get);
913 * cpufreq_get - get the current CPU frequency (in kHz)
914 * @cpu: CPU number
916 * Get the CPU current (static) CPU frequency
918 unsigned int cpufreq_get(unsigned int cpu)
920 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
921 unsigned int ret = 0;
923 if (!policy)
924 return 0;
926 if (!cpufreq_driver->get)
927 goto out;
929 mutex_lock(&policy->lock);
931 ret = cpufreq_driver->get(cpu);
933 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
934 /* verify no discrepancy between actual and saved value exists */
935 if (unlikely(ret != policy->cur)) {
936 cpufreq_out_of_sync(cpu, policy->cur, ret);
937 schedule_work(&policy->update);
941 mutex_unlock(&policy->lock);
943 out:
944 cpufreq_cpu_put(policy);
946 return (ret);
948 EXPORT_SYMBOL(cpufreq_get);
952 * cpufreq_suspend - let the low level driver prepare for suspend
955 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
957 int cpu = sysdev->id;
958 unsigned int ret = 0;
959 unsigned int cur_freq = 0;
960 struct cpufreq_policy *cpu_policy;
962 dprintk("resuming cpu %u\n", cpu);
964 if (!cpu_online(cpu))
965 return 0;
967 /* we may be lax here as interrupts are off. Nonetheless
968 * we need to grab the correct cpu policy, as to check
969 * whether we really run on this CPU.
972 cpu_policy = cpufreq_cpu_get(cpu);
973 if (!cpu_policy)
974 return -EINVAL;
976 /* only handle each CPU group once */
977 if (unlikely(cpu_policy->cpu != cpu)) {
978 cpufreq_cpu_put(cpu_policy);
979 return 0;
982 if (cpufreq_driver->suspend) {
983 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
984 if (ret) {
985 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
986 "step on CPU %u\n", cpu_policy->cpu);
987 cpufreq_cpu_put(cpu_policy);
988 return ret;
993 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
994 goto out;
996 if (cpufreq_driver->get)
997 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
999 if (!cur_freq || !cpu_policy->cur) {
1000 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1001 "frequency is what timing core thinks it is.\n");
1002 goto out;
1005 if (unlikely(cur_freq != cpu_policy->cur)) {
1006 struct cpufreq_freqs freqs;
1008 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1009 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
1010 "cpufreq assumed %u kHz.\n",
1011 cur_freq, cpu_policy->cur);
1013 freqs.cpu = cpu;
1014 freqs.old = cpu_policy->cur;
1015 freqs.new = cur_freq;
1017 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
1018 CPUFREQ_SUSPENDCHANGE, &freqs);
1019 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1021 cpu_policy->cur = cur_freq;
1024 out:
1025 cpufreq_cpu_put(cpu_policy);
1026 return 0;
1030 * cpufreq_resume - restore proper CPU frequency handling after resume
1032 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1033 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
1034 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1035 * restored.
1037 static int cpufreq_resume(struct sys_device * sysdev)
1039 int cpu = sysdev->id;
1040 unsigned int ret = 0;
1041 struct cpufreq_policy *cpu_policy;
1043 dprintk("resuming cpu %u\n", cpu);
1045 if (!cpu_online(cpu))
1046 return 0;
1048 /* we may be lax here as interrupts are off. Nonetheless
1049 * we need to grab the correct cpu policy, as to check
1050 * whether we really run on this CPU.
1053 cpu_policy = cpufreq_cpu_get(cpu);
1054 if (!cpu_policy)
1055 return -EINVAL;
1057 /* only handle each CPU group once */
1058 if (unlikely(cpu_policy->cpu != cpu)) {
1059 cpufreq_cpu_put(cpu_policy);
1060 return 0;
1063 if (cpufreq_driver->resume) {
1064 ret = cpufreq_driver->resume(cpu_policy);
1065 if (ret) {
1066 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1067 "step on CPU %u\n", cpu_policy->cpu);
1068 cpufreq_cpu_put(cpu_policy);
1069 return ret;
1073 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1074 unsigned int cur_freq = 0;
1076 if (cpufreq_driver->get)
1077 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1079 if (!cur_freq || !cpu_policy->cur) {
1080 printk(KERN_ERR "cpufreq: resume failed to assert "
1081 "current frequency is what timing core "
1082 "thinks it is.\n");
1083 goto out;
1086 if (unlikely(cur_freq != cpu_policy->cur)) {
1087 struct cpufreq_freqs freqs;
1089 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1090 dprintk(KERN_WARNING "Warning: CPU frequency"
1091 "is %u, cpufreq assumed %u kHz.\n",
1092 cur_freq, cpu_policy->cur);
1094 freqs.cpu = cpu;
1095 freqs.old = cpu_policy->cur;
1096 freqs.new = cur_freq;
1098 blocking_notifier_call_chain(
1099 &cpufreq_transition_notifier_list,
1100 CPUFREQ_RESUMECHANGE, &freqs);
1101 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1103 cpu_policy->cur = cur_freq;
1107 out:
1108 schedule_work(&cpu_policy->update);
1109 cpufreq_cpu_put(cpu_policy);
1110 return ret;
1113 static struct sysdev_driver cpufreq_sysdev_driver = {
1114 .add = cpufreq_add_dev,
1115 .remove = cpufreq_remove_dev,
1116 .suspend = cpufreq_suspend,
1117 .resume = cpufreq_resume,
1121 /*********************************************************************
1122 * NOTIFIER LISTS INTERFACE *
1123 *********************************************************************/
1126 * cpufreq_register_notifier - register a driver with cpufreq
1127 * @nb: notifier function to register
1128 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1130 * Add a driver to one of two lists: either a list of drivers that
1131 * are notified about clock rate changes (once before and once after
1132 * the transition), or a list of drivers that are notified about
1133 * changes in cpufreq policy.
1135 * This function may sleep, and has the same return conditions as
1136 * blocking_notifier_chain_register.
1138 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1140 int ret;
1142 switch (list) {
1143 case CPUFREQ_TRANSITION_NOTIFIER:
1144 ret = blocking_notifier_chain_register(
1145 &cpufreq_transition_notifier_list, nb);
1146 break;
1147 case CPUFREQ_POLICY_NOTIFIER:
1148 ret = blocking_notifier_chain_register(
1149 &cpufreq_policy_notifier_list, nb);
1150 break;
1151 default:
1152 ret = -EINVAL;
1155 return ret;
1157 EXPORT_SYMBOL(cpufreq_register_notifier);
1161 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1162 * @nb: notifier block to be unregistered
1163 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1165 * Remove a driver from the CPU frequency notifier list.
1167 * This function may sleep, and has the same return conditions as
1168 * blocking_notifier_chain_unregister.
1170 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1172 int ret;
1174 switch (list) {
1175 case CPUFREQ_TRANSITION_NOTIFIER:
1176 ret = blocking_notifier_chain_unregister(
1177 &cpufreq_transition_notifier_list, nb);
1178 break;
1179 case CPUFREQ_POLICY_NOTIFIER:
1180 ret = blocking_notifier_chain_unregister(
1181 &cpufreq_policy_notifier_list, nb);
1182 break;
1183 default:
1184 ret = -EINVAL;
1187 return ret;
1189 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1192 /*********************************************************************
1193 * GOVERNORS *
1194 *********************************************************************/
1197 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1198 unsigned int target_freq,
1199 unsigned int relation)
1201 int retval = -EINVAL;
1203 lock_cpu_hotplug();
1204 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1205 target_freq, relation);
1206 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1207 retval = cpufreq_driver->target(policy, target_freq, relation);
1209 unlock_cpu_hotplug();
1211 return retval;
1213 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1215 int cpufreq_driver_target(struct cpufreq_policy *policy,
1216 unsigned int target_freq,
1217 unsigned int relation)
1219 int ret;
1221 policy = cpufreq_cpu_get(policy->cpu);
1222 if (!policy)
1223 return -EINVAL;
1225 mutex_lock(&policy->lock);
1227 ret = __cpufreq_driver_target(policy, target_freq, relation);
1229 mutex_unlock(&policy->lock);
1231 cpufreq_cpu_put(policy);
1232 return ret;
1234 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1237 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1239 int ret;
1241 if (!try_module_get(policy->governor->owner))
1242 return -EINVAL;
1244 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1245 ret = policy->governor->governor(policy, event);
1247 /* we keep one module reference alive for each CPU governed by this CPU */
1248 if ((event != CPUFREQ_GOV_START) || ret)
1249 module_put(policy->governor->owner);
1250 if ((event == CPUFREQ_GOV_STOP) && !ret)
1251 module_put(policy->governor->owner);
1253 return ret;
1257 int cpufreq_governor(unsigned int cpu, unsigned int event)
1259 int ret = 0;
1260 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1262 if (!policy)
1263 return -EINVAL;
1265 mutex_lock(&policy->lock);
1266 ret = __cpufreq_governor(policy, event);
1267 mutex_unlock(&policy->lock);
1269 cpufreq_cpu_put(policy);
1270 return ret;
1272 EXPORT_SYMBOL_GPL(cpufreq_governor);
1275 int cpufreq_register_governor(struct cpufreq_governor *governor)
1277 struct cpufreq_governor *t;
1279 if (!governor)
1280 return -EINVAL;
1282 mutex_lock(&cpufreq_governor_mutex);
1284 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1285 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1286 mutex_unlock(&cpufreq_governor_mutex);
1287 return -EBUSY;
1290 list_add(&governor->governor_list, &cpufreq_governor_list);
1292 mutex_unlock(&cpufreq_governor_mutex);
1293 return 0;
1295 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1298 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1300 if (!governor)
1301 return;
1303 mutex_lock(&cpufreq_governor_mutex);
1304 list_del(&governor->governor_list);
1305 mutex_unlock(&cpufreq_governor_mutex);
1306 return;
1308 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1312 /*********************************************************************
1313 * POLICY INTERFACE *
1314 *********************************************************************/
1317 * cpufreq_get_policy - get the current cpufreq_policy
1318 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1320 * Reads the current cpufreq policy.
1322 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1324 struct cpufreq_policy *cpu_policy;
1325 if (!policy)
1326 return -EINVAL;
1328 cpu_policy = cpufreq_cpu_get(cpu);
1329 if (!cpu_policy)
1330 return -EINVAL;
1332 mutex_lock(&cpu_policy->lock);
1333 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1334 mutex_unlock(&cpu_policy->lock);
1336 cpufreq_cpu_put(cpu_policy);
1337 return 0;
1339 EXPORT_SYMBOL(cpufreq_get_policy);
1342 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1344 int ret = 0;
1346 cpufreq_debug_disable_ratelimit();
1347 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1348 policy->min, policy->max);
1350 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
1352 /* verify the cpu speed can be set within this limit */
1353 ret = cpufreq_driver->verify(policy);
1354 if (ret)
1355 goto error_out;
1357 /* adjust if necessary - all reasons */
1358 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1359 CPUFREQ_ADJUST, policy);
1361 /* adjust if necessary - hardware incompatibility*/
1362 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1363 CPUFREQ_INCOMPATIBLE, policy);
1365 /* verify the cpu speed can be set within this limit,
1366 which might be different to the first one */
1367 ret = cpufreq_driver->verify(policy);
1368 if (ret)
1369 goto error_out;
1371 /* notification of the new policy */
1372 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1373 CPUFREQ_NOTIFY, policy);
1375 data->min = policy->min;
1376 data->max = policy->max;
1378 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1380 if (cpufreq_driver->setpolicy) {
1381 data->policy = policy->policy;
1382 dprintk("setting range\n");
1383 ret = cpufreq_driver->setpolicy(policy);
1384 } else {
1385 if (policy->governor != data->governor) {
1386 /* save old, working values */
1387 struct cpufreq_governor *old_gov = data->governor;
1389 dprintk("governor switch\n");
1391 /* end old governor */
1392 if (data->governor)
1393 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1395 /* start new governor */
1396 data->governor = policy->governor;
1397 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1398 /* new governor failed, so re-start old one */
1399 dprintk("starting governor %s failed\n", data->governor->name);
1400 if (old_gov) {
1401 data->governor = old_gov;
1402 __cpufreq_governor(data, CPUFREQ_GOV_START);
1404 ret = -EINVAL;
1405 goto error_out;
1407 /* might be a policy change, too, so fall through */
1409 dprintk("governor: change or update limits\n");
1410 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1413 error_out:
1414 cpufreq_debug_enable_ratelimit();
1415 return ret;
1419 * cpufreq_set_policy - set a new CPUFreq policy
1420 * @policy: policy to be set.
1422 * Sets a new CPU frequency and voltage scaling policy.
1424 int cpufreq_set_policy(struct cpufreq_policy *policy)
1426 int ret = 0;
1427 struct cpufreq_policy *data;
1429 if (!policy)
1430 return -EINVAL;
1432 data = cpufreq_cpu_get(policy->cpu);
1433 if (!data)
1434 return -EINVAL;
1436 /* lock this CPU */
1437 mutex_lock(&data->lock);
1439 ret = __cpufreq_set_policy(data, policy);
1440 data->user_policy.min = data->min;
1441 data->user_policy.max = data->max;
1442 data->user_policy.policy = data->policy;
1443 data->user_policy.governor = data->governor;
1445 mutex_unlock(&data->lock);
1446 cpufreq_cpu_put(data);
1448 return ret;
1450 EXPORT_SYMBOL(cpufreq_set_policy);
1454 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1455 * @cpu: CPU which shall be re-evaluated
1457 * Usefull for policy notifiers which have different necessities
1458 * at different times.
1460 int cpufreq_update_policy(unsigned int cpu)
1462 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1463 struct cpufreq_policy policy;
1464 int ret = 0;
1466 if (!data)
1467 return -ENODEV;
1469 mutex_lock(&data->lock);
1471 dprintk("updating policy for CPU %u\n", cpu);
1472 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1473 policy.min = data->user_policy.min;
1474 policy.max = data->user_policy.max;
1475 policy.policy = data->user_policy.policy;
1476 policy.governor = data->user_policy.governor;
1478 /* BIOS might change freq behind our back
1479 -> ask driver for current freq and notify governors about a change */
1480 if (cpufreq_driver->get) {
1481 policy.cur = cpufreq_driver->get(cpu);
1482 if (!data->cur) {
1483 dprintk("Driver did not initialize current freq");
1484 data->cur = policy.cur;
1485 } else {
1486 if (data->cur != policy.cur)
1487 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1491 ret = __cpufreq_set_policy(data, &policy);
1493 mutex_unlock(&data->lock);
1495 cpufreq_cpu_put(data);
1496 return ret;
1498 EXPORT_SYMBOL(cpufreq_update_policy);
1500 static int cpufreq_cpu_callback(struct notifier_block *nfb,
1501 unsigned long action, void *hcpu)
1503 unsigned int cpu = (unsigned long)hcpu;
1504 struct cpufreq_policy *policy;
1505 struct sys_device *sys_dev;
1507 sys_dev = get_cpu_sysdev(cpu);
1509 if (sys_dev) {
1510 switch (action) {
1511 case CPU_ONLINE:
1512 cpufreq_add_dev(sys_dev);
1513 break;
1514 case CPU_DOWN_PREPARE:
1516 * We attempt to put this cpu in lowest frequency
1517 * possible before going down. This will permit
1518 * hardware-managed P-State to switch other related
1519 * threads to min or higher speeds if possible.
1521 policy = cpufreq_cpu_data[cpu];
1522 if (policy) {
1523 cpufreq_driver_target(policy, policy->min,
1524 CPUFREQ_RELATION_H);
1526 break;
1527 case CPU_DEAD:
1528 cpufreq_remove_dev(sys_dev);
1529 break;
1532 return NOTIFY_OK;
1535 static struct notifier_block cpufreq_cpu_notifier =
1537 .notifier_call = cpufreq_cpu_callback,
1540 /*********************************************************************
1541 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1542 *********************************************************************/
1545 * cpufreq_register_driver - register a CPU Frequency driver
1546 * @driver_data: A struct cpufreq_driver containing the values#
1547 * submitted by the CPU Frequency driver.
1549 * Registers a CPU Frequency driver to this core code. This code
1550 * returns zero on success, -EBUSY when another driver got here first
1551 * (and isn't unregistered in the meantime).
1554 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1556 unsigned long flags;
1557 int ret;
1559 if (!driver_data || !driver_data->verify || !driver_data->init ||
1560 ((!driver_data->setpolicy) && (!driver_data->target)))
1561 return -EINVAL;
1563 dprintk("trying to register driver %s\n", driver_data->name);
1565 if (driver_data->setpolicy)
1566 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1568 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1569 if (cpufreq_driver) {
1570 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1571 return -EBUSY;
1573 cpufreq_driver = driver_data;
1574 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1576 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1578 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1579 int i;
1580 ret = -ENODEV;
1582 /* check for at least one working CPU */
1583 for (i=0; i<NR_CPUS; i++)
1584 if (cpufreq_cpu_data[i])
1585 ret = 0;
1587 /* if all ->init() calls failed, unregister */
1588 if (ret) {
1589 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1590 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1592 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1593 cpufreq_driver = NULL;
1594 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1598 if (!ret) {
1599 register_cpu_notifier(&cpufreq_cpu_notifier);
1600 dprintk("driver %s up and running\n", driver_data->name);
1601 cpufreq_debug_enable_ratelimit();
1604 return (ret);
1606 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1610 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1612 * Unregister the current CPUFreq driver. Only call this if you have
1613 * the right to do so, i.e. if you have succeeded in initialising before!
1614 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1615 * currently not initialised.
1617 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1619 unsigned long flags;
1621 cpufreq_debug_disable_ratelimit();
1623 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1624 cpufreq_debug_enable_ratelimit();
1625 return -EINVAL;
1628 dprintk("unregistering driver %s\n", driver->name);
1630 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1631 unregister_cpu_notifier(&cpufreq_cpu_notifier);
1633 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1634 cpufreq_driver = NULL;
1635 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1637 return 0;
1639 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);