2 * linux/drivers/cpufreq/cpufreq.c
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/notifier.h>
18 #include <linux/cpufreq.h>
19 #include <linux/delay.h>
20 #include <linux/interrupt.h>
21 #include <linux/spinlock.h>
22 #include <linux/device.h>
23 #include <linux/slab.h>
24 #include <linux/cpu.h>
25 #include <linux/completion.h>
28 * The "cpufreq driver" - the arch- or hardware-dependend low
29 * level driver of CPUFreq support, and its spinlock. This lock
30 * also protects the cpufreq_cpu_data array.
32 static struct cpufreq_driver *cpufreq_driver;
33 static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
34 static spinlock_t cpufreq_driver_lock = SPIN_LOCK_UNLOCKED;
36 /* internal prototypes */
37 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
38 static void handle_update(void *data);
39 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci);
42 * Two notifier lists: the "policy" list is involved in the
43 * validation process for a new CPU frequency policy; the
44 * "transition" list for kernel code that needs to handle
45 * changes to devices when the CPU clock speed changes.
46 * The mutex locks both lists.
48 static struct notifier_block *cpufreq_policy_notifier_list;
49 static struct notifier_block *cpufreq_transition_notifier_list;
50 static DECLARE_RWSEM (cpufreq_notifier_rwsem);
53 static LIST_HEAD(cpufreq_governor_list);
54 static DECLARE_MUTEX (cpufreq_governor_sem);
56 static struct cpufreq_policy * cpufreq_cpu_get(unsigned int cpu)
58 struct cpufreq_policy *data;
64 /* get the cpufreq driver */
65 spin_lock_irqsave(&cpufreq_driver_lock, flags);
70 if (!try_module_get(cpufreq_driver->owner))
75 data = cpufreq_cpu_data[cpu];
78 goto err_out_put_module;
80 if (!kobject_get(&data->kobj))
81 goto err_out_put_module;
84 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
89 module_put(cpufreq_driver->owner);
91 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
96 static void cpufreq_cpu_put(struct cpufreq_policy *data)
98 kobject_put(&data->kobj);
99 module_put(cpufreq_driver->owner);
102 /*********************************************************************
103 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
104 *********************************************************************/
107 * adjust_jiffies - adjust the system "loops_per_jiffy"
109 * This function alters the system "loops_per_jiffy" for the clock
110 * speed change. Note that loops_per_jiffy cannot be updated on SMP
111 * systems as each CPU might be scaled differently. So, use the arch
112 * per-CPU loops_per_jiffy value wherever possible.
115 static unsigned long l_p_j_ref;
116 static unsigned int l_p_j_ref_freq;
118 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
120 if (ci->flags & CPUFREQ_CONST_LOOPS)
123 if (!l_p_j_ref_freq) {
124 l_p_j_ref = loops_per_jiffy;
125 l_p_j_ref_freq = ci->old;
127 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
128 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
129 (val == CPUFREQ_RESUMECHANGE))
130 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
133 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
138 * cpufreq_notify_transition - call notifier chain and adjust_jiffies on frequency transition
140 * This function calls the transition notifiers and the "adjust_jiffies" function. It is called
141 * twice on all CPU frequency changes that have external effects.
143 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
145 BUG_ON(irqs_disabled());
147 freqs->flags = cpufreq_driver->flags;
149 down_read(&cpufreq_notifier_rwsem);
151 case CPUFREQ_PRECHANGE:
152 /* detect if the driver reported a value as "old frequency" which
153 * is not equal to what the cpufreq core thinks is "old frequency".
155 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
156 if ((likely(cpufreq_cpu_data[freqs->cpu])) &&
157 (likely(cpufreq_cpu_data[freqs->cpu]->cur)) &&
158 (unlikely(freqs->old != cpufreq_cpu_data[freqs->cpu]->cur)))
160 printk(KERN_WARNING "Warning: CPU frequency is %u, "
161 "cpufreq assumed %u kHz.\n", freqs->old, cpufreq_cpu_data[freqs->cpu]->cur);
162 freqs->old = cpufreq_cpu_data[freqs->cpu]->cur;
165 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_PRECHANGE, freqs);
166 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
168 case CPUFREQ_POSTCHANGE:
169 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
170 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_POSTCHANGE, freqs);
171 if (likely(cpufreq_cpu_data[freqs->cpu]))
172 cpufreq_cpu_data[freqs->cpu]->cur = freqs->new;
175 up_read(&cpufreq_notifier_rwsem);
177 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
181 /*********************************************************************
183 *********************************************************************/
186 * cpufreq_parse_governor - parse a governor string
188 int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
189 struct cpufreq_governor **governor)
193 if (cpufreq_driver->setpolicy) {
194 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
195 *policy = CPUFREQ_POLICY_PERFORMANCE;
197 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
198 *policy = CPUFREQ_POLICY_POWERSAVE;
203 struct cpufreq_governor *t;
204 down(&cpufreq_governor_sem);
205 if (!cpufreq_driver || !cpufreq_driver->target)
207 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
208 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
210 up(&cpufreq_governor_sem);
215 up(&cpufreq_governor_sem);
219 EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
222 /* drivers/base/cpu.c */
223 extern struct sysdev_class cpu_sysdev_class;
227 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
229 * Write out information from cpufreq_driver->policy[cpu]; object must be
233 #define show_one(file_name, object) \
234 static ssize_t show_##file_name \
235 (struct cpufreq_policy * policy, char *buf) \
237 return sprintf (buf, "%u\n", policy->object); \
240 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
241 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
242 show_one(scaling_min_freq, min);
243 show_one(scaling_max_freq, max);
244 show_one(scaling_cur_freq, cur);
247 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
249 #define store_one(file_name, object) \
250 static ssize_t store_##file_name \
251 (struct cpufreq_policy * policy, const char *buf, size_t count) \
253 unsigned int ret = -EINVAL; \
254 struct cpufreq_policy new_policy; \
256 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
260 ret = sscanf (buf, "%u", &new_policy.object); \
264 ret = cpufreq_set_policy(&new_policy); \
266 return ret ? ret : count; \
269 store_one(scaling_min_freq,min);
270 store_one(scaling_max_freq,max);
273 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
275 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
277 unsigned int cur_freq = cpufreq_get(policy->cpu);
279 return sprintf(buf, "<unknown>");
280 return sprintf(buf, "%u\n", cur_freq);
285 * show_scaling_governor - show the current policy for the specified CPU
287 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
289 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
290 return sprintf(buf, "powersave\n");
291 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
292 return sprintf(buf, "performance\n");
293 else if (policy->governor)
294 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
300 * store_scaling_governor - store policy for the specified CPU
302 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
303 const char *buf, size_t count)
305 unsigned int ret = -EINVAL;
306 char str_governor[16];
307 struct cpufreq_policy new_policy;
309 ret = cpufreq_get_policy(&new_policy, policy->cpu);
313 ret = sscanf (buf, "%15s", str_governor);
317 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
320 ret = cpufreq_set_policy(&new_policy);
322 return ret ? ret : count;
326 * show_scaling_driver - show the cpufreq driver currently loaded
328 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
330 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
334 * show_scaling_available_governors - show the available CPUfreq governors
336 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
340 struct cpufreq_governor *t;
342 if (!cpufreq_driver->target) {
343 i += sprintf(buf, "performance powersave");
347 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
348 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
350 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
353 i += sprintf(&buf[i], "\n");
358 #define define_one_ro(_name) \
359 struct freq_attr _name = { \
360 .attr = { .name = __stringify(_name), .mode = 0444 }, \
361 .show = show_##_name, \
364 #define define_one_ro0400(_name) \
365 struct freq_attr _name = { \
366 .attr = { .name = __stringify(_name), .mode = 0400 }, \
367 .show = show_##_name, \
370 #define define_one_rw(_name) \
371 struct freq_attr _name = { \
372 .attr = { .name = __stringify(_name), .mode = 0644 }, \
373 .show = show_##_name, \
374 .store = store_##_name, \
377 define_one_ro0400(cpuinfo_cur_freq);
378 define_one_ro(cpuinfo_min_freq);
379 define_one_ro(cpuinfo_max_freq);
380 define_one_ro(scaling_available_governors);
381 define_one_ro(scaling_driver);
382 define_one_ro(scaling_cur_freq);
383 define_one_rw(scaling_min_freq);
384 define_one_rw(scaling_max_freq);
385 define_one_rw(scaling_governor);
387 static struct attribute * default_attrs[] = {
388 &cpuinfo_min_freq.attr,
389 &cpuinfo_max_freq.attr,
390 &scaling_min_freq.attr,
391 &scaling_max_freq.attr,
392 &scaling_governor.attr,
393 &scaling_driver.attr,
394 &scaling_available_governors.attr,
398 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
399 #define to_attr(a) container_of(a,struct freq_attr,attr)
401 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
403 struct cpufreq_policy * policy = to_policy(kobj);
404 struct freq_attr * fattr = to_attr(attr);
406 policy = cpufreq_cpu_get(policy->cpu);
409 ret = fattr->show ? fattr->show(policy,buf) : 0;
410 cpufreq_cpu_put(policy);
414 static ssize_t store(struct kobject * kobj, struct attribute * attr,
415 const char * buf, size_t count)
417 struct cpufreq_policy * policy = to_policy(kobj);
418 struct freq_attr * fattr = to_attr(attr);
420 policy = cpufreq_cpu_get(policy->cpu);
423 ret = fattr->store ? fattr->store(policy,buf,count) : 0;
424 cpufreq_cpu_put(policy);
428 static void cpufreq_sysfs_release(struct kobject * kobj)
430 struct cpufreq_policy * policy = to_policy(kobj);
431 complete(&policy->kobj_unregister);
434 static struct sysfs_ops sysfs_ops = {
439 static struct kobj_type ktype_cpufreq = {
440 .sysfs_ops = &sysfs_ops,
441 .default_attrs = default_attrs,
442 .release = cpufreq_sysfs_release,
447 * cpufreq_add_dev - add a CPU device
449 * Adds the cpufreq interface for a CPU device.
451 static int cpufreq_add_dev (struct sys_device * sys_dev)
453 unsigned int cpu = sys_dev->id;
455 struct cpufreq_policy new_policy;
456 struct cpufreq_policy *policy;
457 struct freq_attr **drv_attr;
460 if (!try_module_get(cpufreq_driver->owner))
463 policy = kmalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
468 memset(policy, 0, sizeof(struct cpufreq_policy));
471 init_MUTEX_LOCKED(&policy->lock);
472 init_completion(&policy->kobj_unregister);
473 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
475 /* call driver. From then on the cpufreq must be able
476 * to accept all calls to ->verify and ->setpolicy for this CPU
478 ret = cpufreq_driver->init(policy);
482 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
484 /* prepare interface data */
485 policy->kobj.parent = &sys_dev->kobj;
486 policy->kobj.ktype = &ktype_cpufreq;
487 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
489 ret = kobject_register(&policy->kobj);
493 /* set up files for this cpu device */
494 drv_attr = cpufreq_driver->attr;
495 while ((drv_attr) && (*drv_attr)) {
496 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
499 if (cpufreq_driver->get)
500 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
501 if (cpufreq_driver->target)
502 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
504 spin_lock_irqsave(&cpufreq_driver_lock, flags);
505 cpufreq_cpu_data[cpu] = policy;
506 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
507 policy->governor = NULL; /* to assure that the starting sequence is
508 * run in cpufreq_set_policy */
511 /* set default policy */
513 ret = cpufreq_set_policy(&new_policy);
515 goto err_out_unregister;
517 module_put(cpufreq_driver->owner);
522 spin_lock_irqsave(&cpufreq_driver_lock, flags);
523 cpufreq_cpu_data[cpu] = NULL;
524 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
526 kobject_unregister(&policy->kobj);
527 wait_for_completion(&policy->kobj_unregister);
533 module_put(cpufreq_driver->owner);
539 * cpufreq_remove_dev - remove a CPU device
541 * Removes the cpufreq interface for a CPU device.
543 static int cpufreq_remove_dev (struct sys_device * sys_dev)
545 unsigned int cpu = sys_dev->id;
547 struct cpufreq_policy *data;
549 spin_lock_irqsave(&cpufreq_driver_lock, flags);
550 data = cpufreq_cpu_data[cpu];
553 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
556 cpufreq_cpu_data[cpu] = NULL;
557 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
559 if (!kobject_get(&data->kobj))
562 if (cpufreq_driver->target)
563 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
565 kobject_unregister(&data->kobj);
567 kobject_put(&data->kobj);
569 /* we need to make sure that the underlying kobj is actually
570 * not referenced anymore by anybody before we proceed with
573 wait_for_completion(&data->kobj_unregister);
575 if (cpufreq_driver->exit)
576 cpufreq_driver->exit(data);
584 static void handle_update(void *data)
586 unsigned int cpu = (unsigned int)(long)data;
587 cpufreq_update_policy(cpu);
591 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
593 * @old_freq: CPU frequency the kernel thinks the CPU runs at
594 * @new_freq: CPU frequency the CPU actually runs at
596 * We adjust to current frequency first, and need to clean up later. So either call
597 * to cpufreq_update_policy() or schedule handle_update()).
599 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
601 struct cpufreq_freqs freqs;
603 printk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
604 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
607 freqs.old = old_freq;
608 freqs.new = new_freq;
609 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
610 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
615 * cpufreq_get - get the current CPU frequency (in kHz)
618 * Get the CPU current (static) CPU frequency
620 unsigned int cpufreq_get(unsigned int cpu)
622 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
623 unsigned int ret = 0;
628 if (!cpufreq_driver->get)
633 ret = cpufreq_driver->get(cpu);
635 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS))
637 /* verify no discrepancy between actual and saved value exists */
638 if (unlikely(ret != policy->cur)) {
639 cpufreq_out_of_sync(cpu, policy->cur, ret);
640 schedule_work(&policy->update);
647 cpufreq_cpu_put(policy);
651 EXPORT_SYMBOL(cpufreq_get);
655 * cpufreq_resume - restore proper CPU frequency handling after resume
657 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
658 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
659 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are restored.
661 static int cpufreq_resume(struct sys_device * sysdev)
663 int cpu = sysdev->id;
664 unsigned int ret = 0;
665 struct cpufreq_policy *cpu_policy;
667 if (!cpu_online(cpu))
670 /* we may be lax here as interrupts are off. Nonetheless
671 * we need to grab the correct cpu policy, as to check
672 * whether we really run on this CPU.
675 cpu_policy = cpufreq_cpu_get(cpu);
679 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
680 unsigned int cur_freq = 0;
682 if (cpufreq_driver->get)
683 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
685 if (!cur_freq || !cpu_policy->cur) {
686 printk(KERN_ERR "cpufreq: resume failed to assert current frequency is what timing core thinks it is.\n");
690 if (unlikely(cur_freq != cpu_policy->cur)) {
691 struct cpufreq_freqs freqs;
693 printk(KERN_WARNING "Warning: CPU frequency is %u, "
694 "cpufreq assumed %u kHz.\n", cur_freq, cpu_policy->cur);
697 freqs.old = cpu_policy->cur;
698 freqs.new = cur_freq;
700 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_RESUMECHANGE, &freqs);
701 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
703 cpu_policy->cur = cur_freq;
708 schedule_work(&cpu_policy->update);
709 cpufreq_cpu_put(cpu_policy);
713 static struct sysdev_driver cpufreq_sysdev_driver = {
714 .add = cpufreq_add_dev,
715 .remove = cpufreq_remove_dev,
716 .resume = cpufreq_resume,
720 /*********************************************************************
721 * NOTIFIER LISTS INTERFACE *
722 *********************************************************************/
725 * cpufreq_register_notifier - register a driver with cpufreq
726 * @nb: notifier function to register
727 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
729 * Add a driver to one of two lists: either a list of drivers that
730 * are notified about clock rate changes (once before and once after
731 * the transition), or a list of drivers that are notified about
732 * changes in cpufreq policy.
734 * This function may sleep, and has the same return conditions as
735 * notifier_chain_register.
737 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
741 down_write(&cpufreq_notifier_rwsem);
743 case CPUFREQ_TRANSITION_NOTIFIER:
744 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
746 case CPUFREQ_POLICY_NOTIFIER:
747 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
752 up_write(&cpufreq_notifier_rwsem);
756 EXPORT_SYMBOL(cpufreq_register_notifier);
760 * cpufreq_unregister_notifier - unregister a driver with cpufreq
761 * @nb: notifier block to be unregistered
762 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
764 * Remove a driver from the CPU frequency notifier list.
766 * This function may sleep, and has the same return conditions as
767 * notifier_chain_unregister.
769 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
773 down_write(&cpufreq_notifier_rwsem);
775 case CPUFREQ_TRANSITION_NOTIFIER:
776 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
778 case CPUFREQ_POLICY_NOTIFIER:
779 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
784 up_write(&cpufreq_notifier_rwsem);
788 EXPORT_SYMBOL(cpufreq_unregister_notifier);
791 /*********************************************************************
793 *********************************************************************/
796 int __cpufreq_driver_target(struct cpufreq_policy *policy,
797 unsigned int target_freq,
798 unsigned int relation)
800 int retval = -EINVAL;
802 if (cpu_online(policy->cpu))
803 retval = cpufreq_driver->target(policy, target_freq, relation);
804 unlock_cpu_hotplug();
807 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
810 int cpufreq_driver_target(struct cpufreq_policy *policy,
811 unsigned int target_freq,
812 unsigned int relation)
816 policy = cpufreq_cpu_get(policy->cpu);
822 ret = __cpufreq_driver_target(policy, target_freq, relation);
826 cpufreq_cpu_put(policy);
830 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
833 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
837 if (!try_module_get(policy->governor->owner))
840 ret = policy->governor->governor(policy, event);
842 /* we keep one module reference alive for each CPU governed by this CPU */
843 if ((event != CPUFREQ_GOV_START) || ret)
844 module_put(policy->governor->owner);
845 if ((event == CPUFREQ_GOV_STOP) && !ret)
846 module_put(policy->governor->owner);
852 int cpufreq_governor(unsigned int cpu, unsigned int event)
855 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
861 ret = __cpufreq_governor(policy, event);
864 cpufreq_cpu_put(policy);
868 EXPORT_SYMBOL_GPL(cpufreq_governor);
871 int cpufreq_register_governor(struct cpufreq_governor *governor)
873 struct cpufreq_governor *t;
878 down(&cpufreq_governor_sem);
880 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
881 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
882 up(&cpufreq_governor_sem);
886 list_add(&governor->governor_list, &cpufreq_governor_list);
888 up(&cpufreq_governor_sem);
892 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
895 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
900 down(&cpufreq_governor_sem);
901 list_del(&governor->governor_list);
902 up(&cpufreq_governor_sem);
905 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
909 /*********************************************************************
911 *********************************************************************/
914 * cpufreq_get_policy - get the current cpufreq_policy
915 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
917 * Reads the current cpufreq policy.
919 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
921 struct cpufreq_policy *cpu_policy;
925 cpu_policy = cpufreq_cpu_get(cpu);
929 down(&cpu_policy->lock);
930 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
931 up(&cpu_policy->lock);
933 cpufreq_cpu_put(cpu_policy);
937 EXPORT_SYMBOL(cpufreq_get_policy);
940 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
944 memcpy(&policy->cpuinfo,
946 sizeof(struct cpufreq_cpuinfo));
948 /* verify the cpu speed can be set within this limit */
949 ret = cpufreq_driver->verify(policy);
953 down_read(&cpufreq_notifier_rwsem);
955 /* adjust if necessary - all reasons */
956 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
959 /* adjust if necessary - hardware incompatibility*/
960 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
963 /* verify the cpu speed can be set within this limit,
964 which might be different to the first one */
965 ret = cpufreq_driver->verify(policy);
967 up_read(&cpufreq_notifier_rwsem);
971 /* notification of the new policy */
972 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
975 up_read(&cpufreq_notifier_rwsem);
977 data->min = policy->min;
978 data->max = policy->max;
980 if (cpufreq_driver->setpolicy) {
981 data->policy = policy->policy;
982 ret = cpufreq_driver->setpolicy(policy);
984 if (policy->governor != data->governor) {
985 /* save old, working values */
986 struct cpufreq_governor *old_gov = data->governor;
988 /* end old governor */
990 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
992 /* start new governor */
993 data->governor = policy->governor;
994 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
995 /* new governor failed, so re-start old one */
997 data->governor = old_gov;
998 __cpufreq_governor(data, CPUFREQ_GOV_START);
1003 /* might be a policy change, too, so fall through */
1005 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1013 * cpufreq_set_policy - set a new CPUFreq policy
1014 * @policy: policy to be set.
1016 * Sets a new CPU frequency and voltage scaling policy.
1018 int cpufreq_set_policy(struct cpufreq_policy *policy)
1021 struct cpufreq_policy *data;
1026 data = cpufreq_cpu_get(policy->cpu);
1033 ret = __cpufreq_set_policy(data, policy);
1034 data->user_policy.min = data->min;
1035 data->user_policy.max = data->max;
1036 data->user_policy.policy = data->policy;
1037 data->user_policy.governor = data->governor;
1040 cpufreq_cpu_put(data);
1044 EXPORT_SYMBOL(cpufreq_set_policy);
1048 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1049 * @cpu: CPU which shall be re-evaluated
1051 * Usefull for policy notifiers which have different necessities
1052 * at different times.
1054 int cpufreq_update_policy(unsigned int cpu)
1056 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1057 struct cpufreq_policy policy;
1067 sizeof(struct cpufreq_policy));
1068 policy.min = data->user_policy.min;
1069 policy.max = data->user_policy.max;
1070 policy.policy = data->user_policy.policy;
1071 policy.governor = data->user_policy.governor;
1073 ret = __cpufreq_set_policy(data, &policy);
1077 cpufreq_cpu_put(data);
1080 EXPORT_SYMBOL(cpufreq_update_policy);
1083 /*********************************************************************
1084 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1085 *********************************************************************/
1088 * cpufreq_register_driver - register a CPU Frequency driver
1089 * @driver_data: A struct cpufreq_driver containing the values#
1090 * submitted by the CPU Frequency driver.
1092 * Registers a CPU Frequency driver to this core code. This code
1093 * returns zero on success, -EBUSY when another driver got here first
1094 * (and isn't unregistered in the meantime).
1097 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1099 unsigned long flags;
1102 if (!driver_data || !driver_data->verify || !driver_data->init ||
1103 ((!driver_data->setpolicy) && (!driver_data->target)))
1106 if (driver_data->setpolicy)
1107 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1109 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1110 if (cpufreq_driver) {
1111 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1114 cpufreq_driver = driver_data;
1115 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1117 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1119 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1123 /* check for at least one working CPU */
1124 for (i=0; i<NR_CPUS; i++)
1125 if (cpufreq_cpu_data[i])
1128 /* if all ->init() calls failed, unregister */
1130 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1132 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1133 cpufreq_driver = NULL;
1134 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1140 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1144 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1146 * Unregister the current CPUFreq driver. Only call this if you have
1147 * the right to do so, i.e. if you have succeeded in initialising before!
1148 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1149 * currently not initialised.
1151 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1153 unsigned long flags;
1155 if (!cpufreq_driver || (driver != cpufreq_driver))
1158 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1160 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1161 cpufreq_driver = NULL;
1162 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1166 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);