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 /*********************************************************************
104 *********************************************************************/
107 * cpufreq_parse_governor - parse a governor string
109 int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
110 struct cpufreq_governor **governor)
114 if (cpufreq_driver->setpolicy) {
115 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
116 *policy = CPUFREQ_POLICY_PERFORMANCE;
118 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
119 *policy = CPUFREQ_POLICY_POWERSAVE;
124 struct cpufreq_governor *t;
125 down(&cpufreq_governor_sem);
126 if (!cpufreq_driver || !cpufreq_driver->target)
128 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
129 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
131 up(&cpufreq_governor_sem);
136 up(&cpufreq_governor_sem);
140 EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
143 /* drivers/base/cpu.c */
144 extern struct sysdev_class cpu_sysdev_class;
148 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
150 * Write out information from cpufreq_driver->policy[cpu]; object must be
154 #define show_one(file_name, object) \
155 static ssize_t show_##file_name \
156 (struct cpufreq_policy * policy, char *buf) \
158 return sprintf (buf, "%u\n", policy->object); \
161 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
162 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
163 show_one(scaling_min_freq, min);
164 show_one(scaling_max_freq, max);
165 show_one(scaling_cur_freq, cur);
168 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
170 #define store_one(file_name, object) \
171 static ssize_t store_##file_name \
172 (struct cpufreq_policy * policy, const char *buf, size_t count) \
174 unsigned int ret = -EINVAL; \
175 struct cpufreq_policy new_policy; \
177 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
181 ret = sscanf (buf, "%u", &new_policy.object); \
185 ret = cpufreq_set_policy(&new_policy); \
187 return ret ? ret : count; \
190 store_one(scaling_min_freq,min);
191 store_one(scaling_max_freq,max);
194 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
196 static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
198 unsigned int cur_freq = cpufreq_get(policy->cpu);
200 return sprintf(buf, "<unknown>");
201 return sprintf(buf, "%u\n", cur_freq);
206 * show_scaling_governor - show the current policy for the specified CPU
208 static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
210 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
211 return sprintf(buf, "powersave\n");
212 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
213 return sprintf(buf, "performance\n");
214 else if (policy->governor)
215 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
221 * store_scaling_governor - store policy for the specified CPU
223 static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
224 const char *buf, size_t count)
226 unsigned int ret = -EINVAL;
227 char str_governor[16];
228 struct cpufreq_policy new_policy;
230 ret = cpufreq_get_policy(&new_policy, policy->cpu);
234 ret = sscanf (buf, "%15s", str_governor);
238 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
241 ret = cpufreq_set_policy(&new_policy);
243 return ret ? ret : count;
247 * show_scaling_driver - show the cpufreq driver currently loaded
249 static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
251 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
255 * show_scaling_available_governors - show the available CPUfreq governors
257 static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
261 struct cpufreq_governor *t;
263 if (!cpufreq_driver->target) {
264 i += sprintf(buf, "performance powersave");
268 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
269 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
271 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
274 i += sprintf(&buf[i], "\n");
279 #define define_one_ro(_name) \
280 struct freq_attr _name = { \
281 .attr = { .name = __stringify(_name), .mode = 0444 }, \
282 .show = show_##_name, \
285 #define define_one_ro0400(_name) \
286 struct freq_attr _name = { \
287 .attr = { .name = __stringify(_name), .mode = 0400 }, \
288 .show = show_##_name, \
291 #define define_one_rw(_name) \
292 struct freq_attr _name = { \
293 .attr = { .name = __stringify(_name), .mode = 0644 }, \
294 .show = show_##_name, \
295 .store = store_##_name, \
298 define_one_ro0400(cpuinfo_cur_freq);
299 define_one_ro(cpuinfo_min_freq);
300 define_one_ro(cpuinfo_max_freq);
301 define_one_ro(scaling_available_governors);
302 define_one_ro(scaling_driver);
303 define_one_ro(scaling_cur_freq);
304 define_one_rw(scaling_min_freq);
305 define_one_rw(scaling_max_freq);
306 define_one_rw(scaling_governor);
308 static struct attribute * default_attrs[] = {
309 &cpuinfo_min_freq.attr,
310 &cpuinfo_max_freq.attr,
311 &scaling_min_freq.attr,
312 &scaling_max_freq.attr,
313 &scaling_governor.attr,
314 &scaling_driver.attr,
315 &scaling_available_governors.attr,
319 #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
320 #define to_attr(a) container_of(a,struct freq_attr,attr)
322 static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
324 struct cpufreq_policy * policy = to_policy(kobj);
325 struct freq_attr * fattr = to_attr(attr);
327 policy = cpufreq_cpu_get(policy->cpu);
330 ret = fattr->show ? fattr->show(policy,buf) : 0;
331 cpufreq_cpu_put(policy);
335 static ssize_t store(struct kobject * kobj, struct attribute * attr,
336 const char * buf, size_t count)
338 struct cpufreq_policy * policy = to_policy(kobj);
339 struct freq_attr * fattr = to_attr(attr);
341 policy = cpufreq_cpu_get(policy->cpu);
344 ret = fattr->store ? fattr->store(policy,buf,count) : 0;
345 cpufreq_cpu_put(policy);
349 static void cpufreq_sysfs_release(struct kobject * kobj)
351 struct cpufreq_policy * policy = to_policy(kobj);
352 complete(&policy->kobj_unregister);
355 static struct sysfs_ops sysfs_ops = {
360 static struct kobj_type ktype_cpufreq = {
361 .sysfs_ops = &sysfs_ops,
362 .default_attrs = default_attrs,
363 .release = cpufreq_sysfs_release,
368 * cpufreq_add_dev - add a CPU device
370 * Adds the cpufreq interface for a CPU device.
372 static int cpufreq_add_dev (struct sys_device * sys_dev)
374 unsigned int cpu = sys_dev->id;
376 struct cpufreq_policy new_policy;
377 struct cpufreq_policy *policy;
378 struct freq_attr **drv_attr;
381 if (!try_module_get(cpufreq_driver->owner))
384 policy = kmalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
389 memset(policy, 0, sizeof(struct cpufreq_policy));
392 init_MUTEX_LOCKED(&policy->lock);
393 init_completion(&policy->kobj_unregister);
394 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
396 /* call driver. From then on the cpufreq must be able
397 * to accept all calls to ->verify and ->setpolicy for this CPU
399 ret = cpufreq_driver->init(policy);
403 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
405 /* prepare interface data */
406 policy->kobj.parent = &sys_dev->kobj;
407 policy->kobj.ktype = &ktype_cpufreq;
408 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
410 ret = kobject_register(&policy->kobj);
414 /* set up files for this cpu device */
415 drv_attr = cpufreq_driver->attr;
416 while ((drv_attr) && (*drv_attr)) {
417 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
420 if (cpufreq_driver->get)
421 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
422 if (cpufreq_driver->target)
423 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
425 spin_lock_irqsave(&cpufreq_driver_lock, flags);
426 cpufreq_cpu_data[cpu] = policy;
427 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
428 policy->governor = NULL; /* to assure that the starting sequence is
429 * run in cpufreq_set_policy */
432 /* set default policy */
434 ret = cpufreq_set_policy(&new_policy);
436 goto err_out_unregister;
438 module_put(cpufreq_driver->owner);
443 spin_lock_irqsave(&cpufreq_driver_lock, flags);
444 cpufreq_cpu_data[cpu] = NULL;
445 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
447 kobject_unregister(&policy->kobj);
448 wait_for_completion(&policy->kobj_unregister);
454 module_put(cpufreq_driver->owner);
460 * cpufreq_remove_dev - remove a CPU device
462 * Removes the cpufreq interface for a CPU device.
464 static int cpufreq_remove_dev (struct sys_device * sys_dev)
466 unsigned int cpu = sys_dev->id;
468 struct cpufreq_policy *data;
470 spin_lock_irqsave(&cpufreq_driver_lock, flags);
471 data = cpufreq_cpu_data[cpu];
474 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
477 cpufreq_cpu_data[cpu] = NULL;
478 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
480 if (!kobject_get(&data->kobj))
483 if (cpufreq_driver->target)
484 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
486 kobject_unregister(&data->kobj);
488 kobject_put(&data->kobj);
490 /* we need to make sure that the underlying kobj is actually
491 * not referenced anymore by anybody before we proceed with
494 wait_for_completion(&data->kobj_unregister);
496 if (cpufreq_driver->exit)
497 cpufreq_driver->exit(data);
505 static void handle_update(void *data)
507 unsigned int cpu = (unsigned int)(long)data;
508 cpufreq_update_policy(cpu);
512 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
514 * @old_freq: CPU frequency the kernel thinks the CPU runs at
515 * @new_freq: CPU frequency the CPU actually runs at
517 * We adjust to current frequency first, and need to clean up later. So either call
518 * to cpufreq_update_policy() or schedule handle_update()).
520 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
522 struct cpufreq_freqs freqs;
524 if (cpufreq_driver->flags & CPUFREQ_PANIC_OUTOFSYNC)
525 panic("CPU Frequency is out of sync.");
527 printk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
528 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
531 freqs.old = old_freq;
532 freqs.new = new_freq;
533 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
534 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
539 * cpufreq_get - get the current CPU frequency (in kHz)
542 * Get the CPU current (static) CPU frequency
544 unsigned int cpufreq_get(unsigned int cpu)
546 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
547 unsigned int ret = 0;
552 if (!cpufreq_driver->get)
557 ret = cpufreq_driver->get(cpu);
559 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS))
561 /* verify no discrepancy between actual and saved value exists */
562 if (unlikely(ret != policy->cur)) {
563 cpufreq_out_of_sync(cpu, policy->cur, ret);
564 schedule_work(&policy->update);
571 cpufreq_cpu_put(policy);
575 EXPORT_SYMBOL(cpufreq_get);
579 * cpufreq_resume - restore proper CPU frequency handling after resume
581 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
582 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
583 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are restored.
585 static int cpufreq_resume(struct sys_device * sysdev)
587 int cpu = sysdev->id;
588 unsigned int ret = 0;
589 struct cpufreq_policy *cpu_policy;
591 if (!cpu_online(cpu))
594 /* we may be lax here as interrupts are off. Nonetheless
595 * we need to grab the correct cpu policy, as to check
596 * whether we really run on this CPU.
599 cpu_policy = cpufreq_cpu_get(cpu);
603 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
604 unsigned int cur_freq = 0;
606 if (cpufreq_driver->get)
607 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
609 if (!cur_freq || !cpu_policy->cur) {
610 printk(KERN_ERR "cpufreq: resume failed to assert current frequency is what timing core thinks it is.\n");
614 if (unlikely(cur_freq != cpu_policy->cur)) {
615 struct cpufreq_freqs freqs;
617 if (cpufreq_driver->flags & CPUFREQ_PANIC_RESUME_OUTOFSYNC)
618 panic("CPU Frequency is out of sync.");
620 printk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing"
621 "core thinks of %u, is %u kHz.\n", cpu_policy->cur, cur_freq);
624 freqs.old = cpu_policy->cur;
625 freqs.new = cur_freq;
627 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_RESUMECHANGE, &freqs);
628 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
633 schedule_work(&cpu_policy->update);
634 cpufreq_cpu_put(cpu_policy);
638 static struct sysdev_driver cpufreq_sysdev_driver = {
639 .add = cpufreq_add_dev,
640 .remove = cpufreq_remove_dev,
641 .resume = cpufreq_resume,
645 /*********************************************************************
646 * NOTIFIER LISTS INTERFACE *
647 *********************************************************************/
650 * cpufreq_register_notifier - register a driver with cpufreq
651 * @nb: notifier function to register
652 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
654 * Add a driver to one of two lists: either a list of drivers that
655 * are notified about clock rate changes (once before and once after
656 * the transition), or a list of drivers that are notified about
657 * changes in cpufreq policy.
659 * This function may sleep, and has the same return conditions as
660 * notifier_chain_register.
662 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
666 down_write(&cpufreq_notifier_rwsem);
668 case CPUFREQ_TRANSITION_NOTIFIER:
669 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
671 case CPUFREQ_POLICY_NOTIFIER:
672 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
677 up_write(&cpufreq_notifier_rwsem);
681 EXPORT_SYMBOL(cpufreq_register_notifier);
685 * cpufreq_unregister_notifier - unregister a driver with cpufreq
686 * @nb: notifier block to be unregistered
687 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
689 * Remove a driver from the CPU frequency notifier list.
691 * This function may sleep, and has the same return conditions as
692 * notifier_chain_unregister.
694 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
698 down_write(&cpufreq_notifier_rwsem);
700 case CPUFREQ_TRANSITION_NOTIFIER:
701 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
703 case CPUFREQ_POLICY_NOTIFIER:
704 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
709 up_write(&cpufreq_notifier_rwsem);
713 EXPORT_SYMBOL(cpufreq_unregister_notifier);
716 /*********************************************************************
718 *********************************************************************/
721 int __cpufreq_driver_target(struct cpufreq_policy *policy,
722 unsigned int target_freq,
723 unsigned int relation)
725 int retval = -EINVAL;
727 if (cpu_online(policy->cpu))
728 retval = cpufreq_driver->target(policy, target_freq, relation);
729 unlock_cpu_hotplug();
732 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
735 int cpufreq_driver_target(struct cpufreq_policy *policy,
736 unsigned int target_freq,
737 unsigned int relation)
741 policy = cpufreq_cpu_get(policy->cpu);
747 ret = __cpufreq_driver_target(policy, target_freq, relation);
751 cpufreq_cpu_put(policy);
755 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
758 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
762 if (!try_module_get(policy->governor->owner))
765 ret = policy->governor->governor(policy, event);
767 /* we keep one module reference alive for each CPU governed by this CPU */
768 if ((event != CPUFREQ_GOV_START) || ret)
769 module_put(policy->governor->owner);
770 if ((event == CPUFREQ_GOV_STOP) && !ret)
771 module_put(policy->governor->owner);
777 int cpufreq_governor(unsigned int cpu, unsigned int event)
780 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
786 ret = __cpufreq_governor(policy, event);
789 cpufreq_cpu_put(policy);
793 EXPORT_SYMBOL_GPL(cpufreq_governor);
796 int cpufreq_register_governor(struct cpufreq_governor *governor)
798 struct cpufreq_governor *t;
803 down(&cpufreq_governor_sem);
805 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
806 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
807 up(&cpufreq_governor_sem);
811 list_add(&governor->governor_list, &cpufreq_governor_list);
813 up(&cpufreq_governor_sem);
817 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
820 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
825 down(&cpufreq_governor_sem);
826 list_del(&governor->governor_list);
827 up(&cpufreq_governor_sem);
830 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
834 /*********************************************************************
836 *********************************************************************/
839 * cpufreq_get_policy - get the current cpufreq_policy
840 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
842 * Reads the current cpufreq policy.
844 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
846 struct cpufreq_policy *cpu_policy;
850 cpu_policy = cpufreq_cpu_get(cpu);
854 down(&cpu_policy->lock);
855 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
856 up(&cpu_policy->lock);
858 cpufreq_cpu_put(cpu_policy);
862 EXPORT_SYMBOL(cpufreq_get_policy);
865 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
869 memcpy(&policy->cpuinfo,
871 sizeof(struct cpufreq_cpuinfo));
873 /* verify the cpu speed can be set within this limit */
874 ret = cpufreq_driver->verify(policy);
878 down_read(&cpufreq_notifier_rwsem);
880 /* adjust if necessary - all reasons */
881 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
884 /* adjust if necessary - hardware incompatibility*/
885 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
888 /* verify the cpu speed can be set within this limit,
889 which might be different to the first one */
890 ret = cpufreq_driver->verify(policy);
892 up_read(&cpufreq_notifier_rwsem);
896 /* notification of the new policy */
897 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
900 up_read(&cpufreq_notifier_rwsem);
902 data->min = policy->min;
903 data->max = policy->max;
905 if (cpufreq_driver->setpolicy) {
906 data->policy = policy->policy;
907 ret = cpufreq_driver->setpolicy(policy);
909 if (policy->governor != data->governor) {
910 /* save old, working values */
911 struct cpufreq_governor *old_gov = data->governor;
913 /* end old governor */
915 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
917 /* start new governor */
918 data->governor = policy->governor;
919 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
920 /* new governor failed, so re-start old one */
922 data->governor = old_gov;
923 __cpufreq_governor(data, CPUFREQ_GOV_START);
928 /* might be a policy change, too, so fall through */
930 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
938 * cpufreq_set_policy - set a new CPUFreq policy
939 * @policy: policy to be set.
941 * Sets a new CPU frequency and voltage scaling policy.
943 int cpufreq_set_policy(struct cpufreq_policy *policy)
946 struct cpufreq_policy *data;
951 data = cpufreq_cpu_get(policy->cpu);
958 ret = __cpufreq_set_policy(data, policy);
959 data->user_policy.min = data->min;
960 data->user_policy.max = data->max;
961 data->user_policy.policy = data->policy;
962 data->user_policy.governor = data->governor;
965 cpufreq_cpu_put(data);
969 EXPORT_SYMBOL(cpufreq_set_policy);
973 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
974 * @cpu: CPU which shall be re-evaluated
976 * Usefull for policy notifiers which have different necessities
977 * at different times.
979 int cpufreq_update_policy(unsigned int cpu)
981 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
982 struct cpufreq_policy policy;
992 sizeof(struct cpufreq_policy));
993 policy.min = data->user_policy.min;
994 policy.max = data->user_policy.max;
995 policy.policy = data->user_policy.policy;
996 policy.governor = data->user_policy.governor;
998 ret = __cpufreq_set_policy(data, &policy);
1002 cpufreq_cpu_put(data);
1005 EXPORT_SYMBOL(cpufreq_update_policy);
1008 /*********************************************************************
1009 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
1010 *********************************************************************/
1013 * adjust_jiffies - adjust the system "loops_per_jiffy"
1015 * This function alters the system "loops_per_jiffy" for the clock
1016 * speed change. Note that loops_per_jiffy cannot be updated on SMP
1017 * systems as each CPU might be scaled differently. So, use the arch
1018 * per-CPU loops_per_jiffy value wherever possible.
1021 static unsigned long l_p_j_ref;
1022 static unsigned int l_p_j_ref_freq;
1024 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
1026 if (ci->flags & CPUFREQ_CONST_LOOPS)
1029 if (!l_p_j_ref_freq) {
1030 l_p_j_ref = loops_per_jiffy;
1031 l_p_j_ref_freq = ci->old;
1033 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
1034 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
1035 (val == CPUFREQ_RESUMECHANGE))
1036 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
1039 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
1044 * cpufreq_notify_transition - call notifier chain and adjust_jiffies on frequency transition
1046 * This function calls the transition notifiers and the "adjust_jiffies" function. It is called
1047 * twice on all CPU frequency changes that have external effects.
1049 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
1051 BUG_ON(irqs_disabled());
1053 freqs->flags = cpufreq_driver->flags;
1055 down_read(&cpufreq_notifier_rwsem);
1057 case CPUFREQ_PRECHANGE:
1058 /* detect if the driver reported a value as "old frequency" which
1059 * is not equal to what the cpufreq core thinks is "old frequency".
1061 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1062 if ((likely(cpufreq_cpu_data[freqs->cpu]->cur)) &&
1063 (unlikely(freqs->old != cpufreq_cpu_data[freqs->cpu]->cur)))
1065 if (cpufreq_driver->flags & CPUFREQ_PANIC_OUTOFSYNC)
1066 panic("CPU Frequency is out of sync.");
1068 printk(KERN_WARNING "Warning: CPU frequency out of sync: "
1069 "cpufreq and timing core thinks of %u, is %u kHz.\n",
1070 cpufreq_cpu_data[freqs->cpu]->cur, freqs->old);
1071 freqs->old = cpufreq_cpu_data[freqs->cpu]->cur;
1074 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_PRECHANGE, freqs);
1075 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
1077 case CPUFREQ_POSTCHANGE:
1078 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
1079 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_POSTCHANGE, freqs);
1080 cpufreq_cpu_data[freqs->cpu]->cur = freqs->new;
1083 up_read(&cpufreq_notifier_rwsem);
1085 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
1089 /*********************************************************************
1090 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1091 *********************************************************************/
1094 * cpufreq_register_driver - register a CPU Frequency driver
1095 * @driver_data: A struct cpufreq_driver containing the values#
1096 * submitted by the CPU Frequency driver.
1098 * Registers a CPU Frequency driver to this core code. This code
1099 * returns zero on success, -EBUSY when another driver got here first
1100 * (and isn't unregistered in the meantime).
1103 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1105 unsigned long flags;
1108 if (!driver_data || !driver_data->verify || !driver_data->init ||
1109 ((!driver_data->setpolicy) && (!driver_data->target)))
1112 if (driver_data->setpolicy)
1113 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1115 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1116 if (cpufreq_driver) {
1117 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1120 cpufreq_driver = driver_data;
1121 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1123 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1125 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1129 /* check for at least one working CPU */
1130 for (i=0; i<NR_CPUS; i++)
1131 if (cpufreq_cpu_data[i])
1134 /* if all ->init() calls failed, unregister */
1136 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1138 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1139 cpufreq_driver = NULL;
1140 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1146 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1150 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1152 * Unregister the current CPUFreq driver. Only call this if you have
1153 * the right to do so, i.e. if you have succeeded in initialising before!
1154 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1155 * currently not initialised.
1157 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1159 unsigned long flags;
1161 if (!cpufreq_driver || (driver != cpufreq_driver))
1164 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1166 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1167 cpufreq_driver = NULL;
1168 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1172 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);