2 * linux/drivers/cpufreq/cpufreq.c
4 * Copyright (C) 2001 Russell King
8 * Added handling for CPU hotplug
10 * Fix handling for CPU hotplug -- affected CPUs
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/notifier.h>
22 #include <linux/cpufreq.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/spinlock.h>
26 #include <linux/device.h>
27 #include <linux/slab.h>
28 #include <linux/cpu.h>
29 #include <linux/completion.h>
30 #include <linux/mutex.h>
31 #include <linux/syscore_ops.h>
33 #include <trace/events/power.h>
36 * The "cpufreq driver" - the arch- or hardware-dependent 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 DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
42 #ifdef CONFIG_HOTPLUG_CPU
43 /* This one keeps track of the previously set governor of a removed CPU */
44 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
46 static DEFINE_SPINLOCK(cpufreq_driver_lock);
49 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
50 * all cpufreq/hotplug/workqueue/etc related lock issues.
52 * The rules for this semaphore:
53 * - Any routine that wants to read from the policy structure will
54 * do a down_read on this semaphore.
55 * - Any routine that will write to the policy structure and/or may take away
56 * the policy altogether (eg. CPU hotplug), will hold this lock in write
57 * mode before doing so.
60 * - All holders of the lock should check to make sure that the CPU they
61 * are concerned with are online after they get the lock.
62 * - Governor routines that can be called in cpufreq hotplug path should not
63 * take this sem as top level hotplug notifier handler takes this.
64 * - Lock should not be held across
65 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
67 static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
68 static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
70 #define lock_policy_rwsem(mode, cpu) \
71 static int lock_policy_rwsem_##mode \
74 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
75 BUG_ON(policy_cpu == -1); \
76 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
77 if (unlikely(!cpu_online(cpu))) { \
78 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
85 lock_policy_rwsem(read, cpu);
87 lock_policy_rwsem(write, cpu);
89 static void unlock_policy_rwsem_read(int cpu)
91 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
92 BUG_ON(policy_cpu == -1);
93 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
96 static void unlock_policy_rwsem_write(int cpu)
98 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
99 BUG_ON(policy_cpu == -1);
100 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
104 /* internal prototypes */
105 static int __cpufreq_governor(struct cpufreq_policy *policy,
107 static unsigned int __cpufreq_get(unsigned int cpu);
108 static void handle_update(struct work_struct *work);
111 * Two notifier lists: the "policy" list is involved in the
112 * validation process for a new CPU frequency policy; the
113 * "transition" list for kernel code that needs to handle
114 * changes to devices when the CPU clock speed changes.
115 * The mutex locks both lists.
117 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
118 static struct srcu_notifier_head cpufreq_transition_notifier_list;
120 static bool init_cpufreq_transition_notifier_list_called;
121 static int __init init_cpufreq_transition_notifier_list(void)
123 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
124 init_cpufreq_transition_notifier_list_called = true;
127 pure_initcall(init_cpufreq_transition_notifier_list);
129 static LIST_HEAD(cpufreq_governor_list);
130 static DEFINE_MUTEX(cpufreq_governor_mutex);
132 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
134 struct cpufreq_policy *data;
137 if (cpu >= nr_cpu_ids)
140 /* get the cpufreq driver */
141 spin_lock_irqsave(&cpufreq_driver_lock, flags);
146 if (!try_module_get(cpufreq_driver->owner))
151 data = per_cpu(cpufreq_cpu_data, cpu);
154 goto err_out_put_module;
156 if (!kobject_get(&data->kobj))
157 goto err_out_put_module;
159 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
163 module_put(cpufreq_driver->owner);
165 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
169 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
172 void cpufreq_cpu_put(struct cpufreq_policy *data)
174 kobject_put(&data->kobj);
175 module_put(cpufreq_driver->owner);
177 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
180 /*********************************************************************
181 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
182 *********************************************************************/
185 * adjust_jiffies - adjust the system "loops_per_jiffy"
187 * This function alters the system "loops_per_jiffy" for the clock
188 * speed change. Note that loops_per_jiffy cannot be updated on SMP
189 * systems as each CPU might be scaled differently. So, use the arch
190 * per-CPU loops_per_jiffy value wherever possible.
193 static unsigned long l_p_j_ref;
194 static unsigned int l_p_j_ref_freq;
196 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
198 if (ci->flags & CPUFREQ_CONST_LOOPS)
201 if (!l_p_j_ref_freq) {
202 l_p_j_ref = loops_per_jiffy;
203 l_p_j_ref_freq = ci->old;
204 pr_debug("saving %lu as reference value for loops_per_jiffy; "
205 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
207 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
208 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
209 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
210 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
212 pr_debug("scaling loops_per_jiffy to %lu "
213 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
217 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
225 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
226 * on frequency transition.
228 * This function calls the transition notifiers and the "adjust_jiffies"
229 * function. It is called twice on all CPU frequency changes that have
232 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
234 struct cpufreq_policy *policy;
236 BUG_ON(irqs_disabled());
238 freqs->flags = cpufreq_driver->flags;
239 pr_debug("notification %u of frequency transition to %u kHz\n",
242 policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
245 case CPUFREQ_PRECHANGE:
246 /* detect if the driver reported a value as "old frequency"
247 * which is not equal to what the cpufreq core thinks is
250 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
251 if ((policy) && (policy->cpu == freqs->cpu) &&
252 (policy->cur) && (policy->cur != freqs->old)) {
253 pr_debug("Warning: CPU frequency is"
254 " %u, cpufreq assumed %u kHz.\n",
255 freqs->old, policy->cur);
256 freqs->old = policy->cur;
259 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
260 CPUFREQ_PRECHANGE, freqs);
261 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
264 case CPUFREQ_POSTCHANGE:
265 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
266 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
267 (unsigned long)freqs->cpu);
268 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu);
269 trace_cpu_frequency(freqs->new, freqs->cpu);
270 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
271 CPUFREQ_POSTCHANGE, freqs);
272 if (likely(policy) && likely(policy->cpu == freqs->cpu))
273 policy->cur = freqs->new;
277 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
281 /*********************************************************************
283 *********************************************************************/
285 static struct cpufreq_governor *__find_governor(const char *str_governor)
287 struct cpufreq_governor *t;
289 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
290 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
297 * cpufreq_parse_governor - parse a governor string
299 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
300 struct cpufreq_governor **governor)
307 if (cpufreq_driver->setpolicy) {
308 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
309 *policy = CPUFREQ_POLICY_PERFORMANCE;
311 } else if (!strnicmp(str_governor, "powersave",
313 *policy = CPUFREQ_POLICY_POWERSAVE;
316 } else if (cpufreq_driver->target) {
317 struct cpufreq_governor *t;
319 mutex_lock(&cpufreq_governor_mutex);
321 t = __find_governor(str_governor);
324 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
330 mutex_unlock(&cpufreq_governor_mutex);
331 ret = request_module("%s", name);
332 mutex_lock(&cpufreq_governor_mutex);
335 t = __find_governor(str_governor);
346 mutex_unlock(&cpufreq_governor_mutex);
354 * cpufreq_per_cpu_attr_read() / show_##file_name() -
355 * print out cpufreq information
357 * Write out information from cpufreq_driver->policy[cpu]; object must be
361 #define show_one(file_name, object) \
362 static ssize_t show_##file_name \
363 (struct cpufreq_policy *policy, char *buf) \
365 return sprintf(buf, "%u\n", policy->object); \
368 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
369 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
370 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
371 show_one(scaling_min_freq, min);
372 show_one(scaling_max_freq, max);
373 show_one(scaling_cur_freq, cur);
375 static int __cpufreq_set_policy(struct cpufreq_policy *data,
376 struct cpufreq_policy *policy);
379 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
381 #define store_one(file_name, object) \
382 static ssize_t store_##file_name \
383 (struct cpufreq_policy *policy, const char *buf, size_t count) \
385 unsigned int ret = -EINVAL; \
386 struct cpufreq_policy new_policy; \
388 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
392 ret = sscanf(buf, "%u", &new_policy.object); \
396 ret = __cpufreq_set_policy(policy, &new_policy); \
397 policy->user_policy.object = policy->object; \
399 return ret ? ret : count; \
402 store_one(scaling_min_freq, min);
403 store_one(scaling_max_freq, max);
406 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
408 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
411 unsigned int cur_freq = __cpufreq_get(policy->cpu);
413 return sprintf(buf, "<unknown>");
414 return sprintf(buf, "%u\n", cur_freq);
419 * show_scaling_governor - show the current policy for the specified CPU
421 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
423 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
424 return sprintf(buf, "powersave\n");
425 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
426 return sprintf(buf, "performance\n");
427 else if (policy->governor)
428 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
429 policy->governor->name);
435 * store_scaling_governor - store policy for the specified CPU
437 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
438 const char *buf, size_t count)
440 unsigned int ret = -EINVAL;
441 char str_governor[16];
442 struct cpufreq_policy new_policy;
444 ret = cpufreq_get_policy(&new_policy, policy->cpu);
448 ret = sscanf(buf, "%15s", str_governor);
452 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
453 &new_policy.governor))
456 /* Do not use cpufreq_set_policy here or the user_policy.max
457 will be wrongly overridden */
458 ret = __cpufreq_set_policy(policy, &new_policy);
460 policy->user_policy.policy = policy->policy;
461 policy->user_policy.governor = policy->governor;
470 * show_scaling_driver - show the cpufreq driver currently loaded
472 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
474 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
478 * show_scaling_available_governors - show the available CPUfreq governors
480 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
484 struct cpufreq_governor *t;
486 if (!cpufreq_driver->target) {
487 i += sprintf(buf, "performance powersave");
491 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
492 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
493 - (CPUFREQ_NAME_LEN + 2)))
495 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
498 i += sprintf(&buf[i], "\n");
502 static ssize_t show_cpus(const struct cpumask *mask, char *buf)
507 for_each_cpu(cpu, mask) {
509 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
510 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
511 if (i >= (PAGE_SIZE - 5))
514 i += sprintf(&buf[i], "\n");
519 * show_related_cpus - show the CPUs affected by each transition even if
520 * hw coordination is in use
522 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
524 if (cpumask_empty(policy->related_cpus))
525 return show_cpus(policy->cpus, buf);
526 return show_cpus(policy->related_cpus, buf);
530 * show_affected_cpus - show the CPUs affected by each transition
532 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
534 return show_cpus(policy->cpus, buf);
537 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
538 const char *buf, size_t count)
540 unsigned int freq = 0;
543 if (!policy->governor || !policy->governor->store_setspeed)
546 ret = sscanf(buf, "%u", &freq);
550 policy->governor->store_setspeed(policy, freq);
555 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
557 if (!policy->governor || !policy->governor->show_setspeed)
558 return sprintf(buf, "<unsupported>\n");
560 return policy->governor->show_setspeed(policy, buf);
564 * show_scaling_driver - show the current cpufreq HW/BIOS limitation
566 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
570 if (cpufreq_driver->bios_limit) {
571 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
573 return sprintf(buf, "%u\n", limit);
575 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
578 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
579 cpufreq_freq_attr_ro(cpuinfo_min_freq);
580 cpufreq_freq_attr_ro(cpuinfo_max_freq);
581 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
582 cpufreq_freq_attr_ro(scaling_available_governors);
583 cpufreq_freq_attr_ro(scaling_driver);
584 cpufreq_freq_attr_ro(scaling_cur_freq);
585 cpufreq_freq_attr_ro(bios_limit);
586 cpufreq_freq_attr_ro(related_cpus);
587 cpufreq_freq_attr_ro(affected_cpus);
588 cpufreq_freq_attr_rw(scaling_min_freq);
589 cpufreq_freq_attr_rw(scaling_max_freq);
590 cpufreq_freq_attr_rw(scaling_governor);
591 cpufreq_freq_attr_rw(scaling_setspeed);
593 static struct attribute *default_attrs[] = {
594 &cpuinfo_min_freq.attr,
595 &cpuinfo_max_freq.attr,
596 &cpuinfo_transition_latency.attr,
597 &scaling_min_freq.attr,
598 &scaling_max_freq.attr,
601 &scaling_governor.attr,
602 &scaling_driver.attr,
603 &scaling_available_governors.attr,
604 &scaling_setspeed.attr,
608 struct kobject *cpufreq_global_kobject;
609 EXPORT_SYMBOL(cpufreq_global_kobject);
611 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
612 #define to_attr(a) container_of(a, struct freq_attr, attr)
614 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
616 struct cpufreq_policy *policy = to_policy(kobj);
617 struct freq_attr *fattr = to_attr(attr);
618 ssize_t ret = -EINVAL;
619 policy = cpufreq_cpu_get(policy->cpu);
623 if (lock_policy_rwsem_read(policy->cpu) < 0)
627 ret = fattr->show(policy, buf);
631 unlock_policy_rwsem_read(policy->cpu);
633 cpufreq_cpu_put(policy);
638 static ssize_t store(struct kobject *kobj, struct attribute *attr,
639 const char *buf, size_t count)
641 struct cpufreq_policy *policy = to_policy(kobj);
642 struct freq_attr *fattr = to_attr(attr);
643 ssize_t ret = -EINVAL;
644 policy = cpufreq_cpu_get(policy->cpu);
648 if (lock_policy_rwsem_write(policy->cpu) < 0)
652 ret = fattr->store(policy, buf, count);
656 unlock_policy_rwsem_write(policy->cpu);
658 cpufreq_cpu_put(policy);
663 static void cpufreq_sysfs_release(struct kobject *kobj)
665 struct cpufreq_policy *policy = to_policy(kobj);
666 pr_debug("last reference is dropped\n");
667 complete(&policy->kobj_unregister);
670 static const struct sysfs_ops sysfs_ops = {
675 static struct kobj_type ktype_cpufreq = {
676 .sysfs_ops = &sysfs_ops,
677 .default_attrs = default_attrs,
678 .release = cpufreq_sysfs_release,
685 * Positive: When we have a managed CPU and the sysfs got symlinked
687 static int cpufreq_add_dev_policy(unsigned int cpu,
688 struct cpufreq_policy *policy,
689 struct sys_device *sys_dev)
695 #ifdef CONFIG_HOTPLUG_CPU
696 struct cpufreq_governor *gov;
698 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
700 policy->governor = gov;
701 pr_debug("Restoring governor %s for cpu %d\n",
702 policy->governor->name, cpu);
706 for_each_cpu(j, policy->cpus) {
707 struct cpufreq_policy *managed_policy;
712 /* Check for existing affected CPUs.
713 * They may not be aware of it due to CPU Hotplug.
714 * cpufreq_cpu_put is called when the device is removed
715 * in __cpufreq_remove_dev()
717 managed_policy = cpufreq_cpu_get(j);
718 if (unlikely(managed_policy)) {
720 /* Set proper policy_cpu */
721 unlock_policy_rwsem_write(cpu);
722 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
724 if (lock_policy_rwsem_write(cpu) < 0) {
725 /* Should not go through policy unlock path */
726 if (cpufreq_driver->exit)
727 cpufreq_driver->exit(policy);
728 cpufreq_cpu_put(managed_policy);
732 spin_lock_irqsave(&cpufreq_driver_lock, flags);
733 cpumask_copy(managed_policy->cpus, policy->cpus);
734 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
735 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
737 pr_debug("CPU already managed, adding link\n");
738 ret = sysfs_create_link(&sys_dev->kobj,
739 &managed_policy->kobj,
742 cpufreq_cpu_put(managed_policy);
744 * Success. We only needed to be added to the mask.
745 * Call driver->exit() because only the cpu parent of
746 * the kobj needed to call init().
748 if (cpufreq_driver->exit)
749 cpufreq_driver->exit(policy);
762 /* symlink affected CPUs */
763 static int cpufreq_add_dev_symlink(unsigned int cpu,
764 struct cpufreq_policy *policy)
769 for_each_cpu(j, policy->cpus) {
770 struct cpufreq_policy *managed_policy;
771 struct sys_device *cpu_sys_dev;
778 pr_debug("CPU %u already managed, adding link\n", j);
779 managed_policy = cpufreq_cpu_get(cpu);
780 cpu_sys_dev = get_cpu_sysdev(j);
781 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
784 cpufreq_cpu_put(managed_policy);
791 static int cpufreq_add_dev_interface(unsigned int cpu,
792 struct cpufreq_policy *policy,
793 struct sys_device *sys_dev)
795 struct cpufreq_policy new_policy;
796 struct freq_attr **drv_attr;
801 /* prepare interface data */
802 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
803 &sys_dev->kobj, "cpufreq");
807 /* set up files for this cpu device */
808 drv_attr = cpufreq_driver->attr;
809 while ((drv_attr) && (*drv_attr)) {
810 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
812 goto err_out_kobj_put;
815 if (cpufreq_driver->get) {
816 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
818 goto err_out_kobj_put;
820 if (cpufreq_driver->target) {
821 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
823 goto err_out_kobj_put;
825 if (cpufreq_driver->bios_limit) {
826 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
828 goto err_out_kobj_put;
831 spin_lock_irqsave(&cpufreq_driver_lock, flags);
832 for_each_cpu(j, policy->cpus) {
835 per_cpu(cpufreq_cpu_data, j) = policy;
836 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
838 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
840 ret = cpufreq_add_dev_symlink(cpu, policy);
842 goto err_out_kobj_put;
844 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
845 /* assure that the starting sequence is run in __cpufreq_set_policy */
846 policy->governor = NULL;
848 /* set default policy */
849 ret = __cpufreq_set_policy(policy, &new_policy);
850 policy->user_policy.policy = policy->policy;
851 policy->user_policy.governor = policy->governor;
854 pr_debug("setting policy failed\n");
855 if (cpufreq_driver->exit)
856 cpufreq_driver->exit(policy);
861 kobject_put(&policy->kobj);
862 wait_for_completion(&policy->kobj_unregister);
868 * cpufreq_add_dev - add a CPU device
870 * Adds the cpufreq interface for a CPU device.
872 * The Oracle says: try running cpufreq registration/unregistration concurrently
873 * with with cpu hotplugging and all hell will break loose. Tried to clean this
874 * mess up, but more thorough testing is needed. - Mathieu
876 static int cpufreq_add_dev(struct sys_device *sys_dev)
878 unsigned int cpu = sys_dev->id;
879 int ret = 0, found = 0;
880 struct cpufreq_policy *policy;
883 #ifdef CONFIG_HOTPLUG_CPU
887 if (cpu_is_offline(cpu))
890 pr_debug("adding CPU %u\n", cpu);
893 /* check whether a different CPU already registered this
894 * CPU because it is in the same boat. */
895 policy = cpufreq_cpu_get(cpu);
896 if (unlikely(policy)) {
897 cpufreq_cpu_put(policy);
902 if (!try_module_get(cpufreq_driver->owner)) {
908 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
912 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
913 goto err_free_policy;
915 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
916 goto err_free_cpumask;
919 cpumask_copy(policy->cpus, cpumask_of(cpu));
921 /* Initially set CPU itself as the policy_cpu */
922 per_cpu(cpufreq_policy_cpu, cpu) = cpu;
923 ret = (lock_policy_rwsem_write(cpu) < 0);
926 init_completion(&policy->kobj_unregister);
927 INIT_WORK(&policy->update, handle_update);
929 /* Set governor before ->init, so that driver could check it */
930 #ifdef CONFIG_HOTPLUG_CPU
931 for_each_online_cpu(sibling) {
932 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
933 if (cp && cp->governor &&
934 (cpumask_test_cpu(cpu, cp->related_cpus))) {
935 policy->governor = cp->governor;
942 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
943 /* call driver. From then on the cpufreq must be able
944 * to accept all calls to ->verify and ->setpolicy for this CPU
946 ret = cpufreq_driver->init(policy);
948 pr_debug("initialization failed\n");
949 goto err_unlock_policy;
951 policy->user_policy.min = policy->min;
952 policy->user_policy.max = policy->max;
954 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
955 CPUFREQ_START, policy);
957 ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
960 /* This is a managed cpu, symlink created,
963 goto err_unlock_policy;
966 ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
968 goto err_out_unregister;
970 unlock_policy_rwsem_write(cpu);
972 kobject_uevent(&policy->kobj, KOBJ_ADD);
973 module_put(cpufreq_driver->owner);
974 pr_debug("initialization complete\n");
980 spin_lock_irqsave(&cpufreq_driver_lock, flags);
981 for_each_cpu(j, policy->cpus)
982 per_cpu(cpufreq_cpu_data, j) = NULL;
983 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
985 kobject_put(&policy->kobj);
986 wait_for_completion(&policy->kobj_unregister);
989 unlock_policy_rwsem_write(cpu);
990 free_cpumask_var(policy->related_cpus);
992 free_cpumask_var(policy->cpus);
996 module_put(cpufreq_driver->owner);
1003 * __cpufreq_remove_dev - remove a CPU device
1005 * Removes the cpufreq interface for a CPU device.
1006 * Caller should already have policy_rwsem in write mode for this CPU.
1007 * This routine frees the rwsem before returning.
1009 static int __cpufreq_remove_dev(struct sys_device *sys_dev)
1011 unsigned int cpu = sys_dev->id;
1012 unsigned long flags;
1013 struct cpufreq_policy *data;
1014 struct kobject *kobj;
1015 struct completion *cmp;
1017 struct sys_device *cpu_sys_dev;
1021 pr_debug("unregistering CPU %u\n", cpu);
1023 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1024 data = per_cpu(cpufreq_cpu_data, cpu);
1027 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1028 unlock_policy_rwsem_write(cpu);
1031 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1035 /* if this isn't the CPU which is the parent of the kobj, we
1036 * only need to unlink, put and exit
1038 if (unlikely(cpu != data->cpu)) {
1039 pr_debug("removing link\n");
1040 cpumask_clear_cpu(cpu, data->cpus);
1041 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1042 kobj = &sys_dev->kobj;
1043 cpufreq_cpu_put(data);
1044 unlock_policy_rwsem_write(cpu);
1045 sysfs_remove_link(kobj, "cpufreq");
1052 #ifdef CONFIG_HOTPLUG_CPU
1053 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1057 /* if we have other CPUs still registered, we need to unlink them,
1058 * or else wait_for_completion below will lock up. Clean the
1059 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1060 * the sysfs links afterwards.
1062 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1063 for_each_cpu(j, data->cpus) {
1066 per_cpu(cpufreq_cpu_data, j) = NULL;
1070 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1072 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1073 for_each_cpu(j, data->cpus) {
1076 pr_debug("removing link for cpu %u\n", j);
1077 #ifdef CONFIG_HOTPLUG_CPU
1078 strncpy(per_cpu(cpufreq_cpu_governor, j),
1079 data->governor->name, CPUFREQ_NAME_LEN);
1081 cpu_sys_dev = get_cpu_sysdev(j);
1082 kobj = &cpu_sys_dev->kobj;
1083 unlock_policy_rwsem_write(cpu);
1084 sysfs_remove_link(kobj, "cpufreq");
1085 lock_policy_rwsem_write(cpu);
1086 cpufreq_cpu_put(data);
1090 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1093 if (cpufreq_driver->target)
1094 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1097 cmp = &data->kobj_unregister;
1098 unlock_policy_rwsem_write(cpu);
1101 /* we need to make sure that the underlying kobj is actually
1102 * not referenced anymore by anybody before we proceed with
1105 pr_debug("waiting for dropping of refcount\n");
1106 wait_for_completion(cmp);
1107 pr_debug("wait complete\n");
1109 lock_policy_rwsem_write(cpu);
1110 if (cpufreq_driver->exit)
1111 cpufreq_driver->exit(data);
1112 unlock_policy_rwsem_write(cpu);
1114 #ifdef CONFIG_HOTPLUG_CPU
1115 /* when the CPU which is the parent of the kobj is hotplugged
1116 * offline, check for siblings, and create cpufreq sysfs interface
1119 if (unlikely(cpumask_weight(data->cpus) > 1)) {
1120 /* first sibling now owns the new sysfs dir */
1121 cpumask_clear_cpu(cpu, data->cpus);
1122 cpufreq_add_dev(get_cpu_sysdev(cpumask_first(data->cpus)));
1124 /* finally remove our own symlink */
1125 lock_policy_rwsem_write(cpu);
1126 __cpufreq_remove_dev(sys_dev);
1130 free_cpumask_var(data->related_cpus);
1131 free_cpumask_var(data->cpus);
1138 static int cpufreq_remove_dev(struct sys_device *sys_dev)
1140 unsigned int cpu = sys_dev->id;
1143 if (cpu_is_offline(cpu))
1146 if (unlikely(lock_policy_rwsem_write(cpu)))
1149 retval = __cpufreq_remove_dev(sys_dev);
1154 static void handle_update(struct work_struct *work)
1156 struct cpufreq_policy *policy =
1157 container_of(work, struct cpufreq_policy, update);
1158 unsigned int cpu = policy->cpu;
1159 pr_debug("handle_update for cpu %u called\n", cpu);
1160 cpufreq_update_policy(cpu);
1164 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1166 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1167 * @new_freq: CPU frequency the CPU actually runs at
1169 * We adjust to current frequency first, and need to clean up later.
1170 * So either call to cpufreq_update_policy() or schedule handle_update()).
1172 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1173 unsigned int new_freq)
1175 struct cpufreq_freqs freqs;
1177 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1178 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1181 freqs.old = old_freq;
1182 freqs.new = new_freq;
1183 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1184 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1189 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1192 * This is the last known freq, without actually getting it from the driver.
1193 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1195 unsigned int cpufreq_quick_get(unsigned int cpu)
1197 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1198 unsigned int ret_freq = 0;
1201 ret_freq = policy->cur;
1202 cpufreq_cpu_put(policy);
1207 EXPORT_SYMBOL(cpufreq_quick_get);
1210 static unsigned int __cpufreq_get(unsigned int cpu)
1212 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1213 unsigned int ret_freq = 0;
1215 if (!cpufreq_driver->get)
1218 ret_freq = cpufreq_driver->get(cpu);
1220 if (ret_freq && policy->cur &&
1221 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1222 /* verify no discrepancy between actual and
1223 saved value exists */
1224 if (unlikely(ret_freq != policy->cur)) {
1225 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1226 schedule_work(&policy->update);
1234 * cpufreq_get - get the current CPU frequency (in kHz)
1237 * Get the CPU current (static) CPU frequency
1239 unsigned int cpufreq_get(unsigned int cpu)
1241 unsigned int ret_freq = 0;
1242 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1247 if (unlikely(lock_policy_rwsem_read(cpu)))
1250 ret_freq = __cpufreq_get(cpu);
1252 unlock_policy_rwsem_read(cpu);
1255 cpufreq_cpu_put(policy);
1259 EXPORT_SYMBOL(cpufreq_get);
1261 static struct sysdev_driver cpufreq_sysdev_driver = {
1262 .add = cpufreq_add_dev,
1263 .remove = cpufreq_remove_dev,
1268 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1270 * This function is only executed for the boot processor. The other CPUs
1271 * have been put offline by means of CPU hotplug.
1273 static int cpufreq_bp_suspend(void)
1277 int cpu = smp_processor_id();
1278 struct cpufreq_policy *cpu_policy;
1280 pr_debug("suspending cpu %u\n", cpu);
1282 /* If there's no policy for the boot CPU, we have nothing to do. */
1283 cpu_policy = cpufreq_cpu_get(cpu);
1287 if (cpufreq_driver->suspend) {
1288 ret = cpufreq_driver->suspend(cpu_policy);
1290 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1291 "step on CPU %u\n", cpu_policy->cpu);
1294 cpufreq_cpu_put(cpu_policy);
1299 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1301 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1302 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1303 * restored. It will verify that the current freq is in sync with
1304 * what we believe it to be. This is a bit later than when it
1305 * should be, but nonethteless it's better than calling
1306 * cpufreq_driver->get() here which might re-enable interrupts...
1308 * This function is only executed for the boot CPU. The other CPUs have not
1309 * been turned on yet.
1311 static void cpufreq_bp_resume(void)
1315 int cpu = smp_processor_id();
1316 struct cpufreq_policy *cpu_policy;
1318 pr_debug("resuming cpu %u\n", cpu);
1320 /* If there's no policy for the boot CPU, we have nothing to do. */
1321 cpu_policy = cpufreq_cpu_get(cpu);
1325 if (cpufreq_driver->resume) {
1326 ret = cpufreq_driver->resume(cpu_policy);
1328 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1329 "step on CPU %u\n", cpu_policy->cpu);
1334 schedule_work(&cpu_policy->update);
1337 cpufreq_cpu_put(cpu_policy);
1340 static struct syscore_ops cpufreq_syscore_ops = {
1341 .suspend = cpufreq_bp_suspend,
1342 .resume = cpufreq_bp_resume,
1346 /*********************************************************************
1347 * NOTIFIER LISTS INTERFACE *
1348 *********************************************************************/
1351 * cpufreq_register_notifier - register a driver with cpufreq
1352 * @nb: notifier function to register
1353 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1355 * Add a driver to one of two lists: either a list of drivers that
1356 * are notified about clock rate changes (once before and once after
1357 * the transition), or a list of drivers that are notified about
1358 * changes in cpufreq policy.
1360 * This function may sleep, and has the same return conditions as
1361 * blocking_notifier_chain_register.
1363 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1367 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1370 case CPUFREQ_TRANSITION_NOTIFIER:
1371 ret = srcu_notifier_chain_register(
1372 &cpufreq_transition_notifier_list, nb);
1374 case CPUFREQ_POLICY_NOTIFIER:
1375 ret = blocking_notifier_chain_register(
1376 &cpufreq_policy_notifier_list, nb);
1384 EXPORT_SYMBOL(cpufreq_register_notifier);
1388 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1389 * @nb: notifier block to be unregistered
1390 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1392 * Remove a driver from the CPU frequency notifier list.
1394 * This function may sleep, and has the same return conditions as
1395 * blocking_notifier_chain_unregister.
1397 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1402 case CPUFREQ_TRANSITION_NOTIFIER:
1403 ret = srcu_notifier_chain_unregister(
1404 &cpufreq_transition_notifier_list, nb);
1406 case CPUFREQ_POLICY_NOTIFIER:
1407 ret = blocking_notifier_chain_unregister(
1408 &cpufreq_policy_notifier_list, nb);
1416 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1419 /*********************************************************************
1421 *********************************************************************/
1424 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1425 unsigned int target_freq,
1426 unsigned int relation)
1428 int retval = -EINVAL;
1430 pr_debug("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1431 target_freq, relation);
1432 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1433 retval = cpufreq_driver->target(policy, target_freq, relation);
1437 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1439 int cpufreq_driver_target(struct cpufreq_policy *policy,
1440 unsigned int target_freq,
1441 unsigned int relation)
1445 policy = cpufreq_cpu_get(policy->cpu);
1449 if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1452 ret = __cpufreq_driver_target(policy, target_freq, relation);
1454 unlock_policy_rwsem_write(policy->cpu);
1457 cpufreq_cpu_put(policy);
1461 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1463 int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
1467 policy = cpufreq_cpu_get(policy->cpu);
1471 if (cpu_online(cpu) && cpufreq_driver->getavg)
1472 ret = cpufreq_driver->getavg(policy, cpu);
1474 cpufreq_cpu_put(policy);
1477 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
1480 * when "event" is CPUFREQ_GOV_LIMITS
1483 static int __cpufreq_governor(struct cpufreq_policy *policy,
1488 /* Only must be defined when default governor is known to have latency
1489 restrictions, like e.g. conservative or ondemand.
1490 That this is the case is already ensured in Kconfig
1492 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1493 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1495 struct cpufreq_governor *gov = NULL;
1498 if (policy->governor->max_transition_latency &&
1499 policy->cpuinfo.transition_latency >
1500 policy->governor->max_transition_latency) {
1504 printk(KERN_WARNING "%s governor failed, too long"
1505 " transition latency of HW, fallback"
1506 " to %s governor\n",
1507 policy->governor->name,
1509 policy->governor = gov;
1513 if (!try_module_get(policy->governor->owner))
1516 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1517 policy->cpu, event);
1518 ret = policy->governor->governor(policy, event);
1520 /* we keep one module reference alive for
1521 each CPU governed by this CPU */
1522 if ((event != CPUFREQ_GOV_START) || ret)
1523 module_put(policy->governor->owner);
1524 if ((event == CPUFREQ_GOV_STOP) && !ret)
1525 module_put(policy->governor->owner);
1531 int cpufreq_register_governor(struct cpufreq_governor *governor)
1538 mutex_lock(&cpufreq_governor_mutex);
1541 if (__find_governor(governor->name) == NULL) {
1543 list_add(&governor->governor_list, &cpufreq_governor_list);
1546 mutex_unlock(&cpufreq_governor_mutex);
1549 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1552 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1554 #ifdef CONFIG_HOTPLUG_CPU
1561 #ifdef CONFIG_HOTPLUG_CPU
1562 for_each_present_cpu(cpu) {
1563 if (cpu_online(cpu))
1565 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1566 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1570 mutex_lock(&cpufreq_governor_mutex);
1571 list_del(&governor->governor_list);
1572 mutex_unlock(&cpufreq_governor_mutex);
1575 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1579 /*********************************************************************
1580 * POLICY INTERFACE *
1581 *********************************************************************/
1584 * cpufreq_get_policy - get the current cpufreq_policy
1585 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1588 * Reads the current cpufreq policy.
1590 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1592 struct cpufreq_policy *cpu_policy;
1596 cpu_policy = cpufreq_cpu_get(cpu);
1600 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1602 cpufreq_cpu_put(cpu_policy);
1605 EXPORT_SYMBOL(cpufreq_get_policy);
1609 * data : current policy.
1610 * policy : policy to be set.
1612 static int __cpufreq_set_policy(struct cpufreq_policy *data,
1613 struct cpufreq_policy *policy)
1617 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1618 policy->min, policy->max);
1620 memcpy(&policy->cpuinfo, &data->cpuinfo,
1621 sizeof(struct cpufreq_cpuinfo));
1623 if (policy->min > data->max || policy->max < data->min) {
1628 /* verify the cpu speed can be set within this limit */
1629 ret = cpufreq_driver->verify(policy);
1633 /* adjust if necessary - all reasons */
1634 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1635 CPUFREQ_ADJUST, policy);
1637 /* adjust if necessary - hardware incompatibility*/
1638 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1639 CPUFREQ_INCOMPATIBLE, policy);
1641 /* verify the cpu speed can be set within this limit,
1642 which might be different to the first one */
1643 ret = cpufreq_driver->verify(policy);
1647 /* notification of the new policy */
1648 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1649 CPUFREQ_NOTIFY, policy);
1651 data->min = policy->min;
1652 data->max = policy->max;
1654 pr_debug("new min and max freqs are %u - %u kHz\n",
1655 data->min, data->max);
1657 if (cpufreq_driver->setpolicy) {
1658 data->policy = policy->policy;
1659 pr_debug("setting range\n");
1660 ret = cpufreq_driver->setpolicy(policy);
1662 if (policy->governor != data->governor) {
1663 /* save old, working values */
1664 struct cpufreq_governor *old_gov = data->governor;
1666 pr_debug("governor switch\n");
1668 /* end old governor */
1670 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1672 /* start new governor */
1673 data->governor = policy->governor;
1674 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1675 /* new governor failed, so re-start old one */
1676 pr_debug("starting governor %s failed\n",
1677 data->governor->name);
1679 data->governor = old_gov;
1680 __cpufreq_governor(data,
1686 /* might be a policy change, too, so fall through */
1688 pr_debug("governor: change or update limits\n");
1689 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1697 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1698 * @cpu: CPU which shall be re-evaluated
1700 * Useful for policy notifiers which have different necessities
1701 * at different times.
1703 int cpufreq_update_policy(unsigned int cpu)
1705 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1706 struct cpufreq_policy policy;
1714 if (unlikely(lock_policy_rwsem_write(cpu))) {
1719 pr_debug("updating policy for CPU %u\n", cpu);
1720 memcpy(&policy, data, sizeof(struct cpufreq_policy));
1721 policy.min = data->user_policy.min;
1722 policy.max = data->user_policy.max;
1723 policy.policy = data->user_policy.policy;
1724 policy.governor = data->user_policy.governor;
1726 /* BIOS might change freq behind our back
1727 -> ask driver for current freq and notify governors about a change */
1728 if (cpufreq_driver->get) {
1729 policy.cur = cpufreq_driver->get(cpu);
1731 pr_debug("Driver did not initialize current freq");
1732 data->cur = policy.cur;
1734 if (data->cur != policy.cur)
1735 cpufreq_out_of_sync(cpu, data->cur,
1740 ret = __cpufreq_set_policy(data, &policy);
1742 unlock_policy_rwsem_write(cpu);
1745 cpufreq_cpu_put(data);
1749 EXPORT_SYMBOL(cpufreq_update_policy);
1751 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1752 unsigned long action, void *hcpu)
1754 unsigned int cpu = (unsigned long)hcpu;
1755 struct sys_device *sys_dev;
1757 sys_dev = get_cpu_sysdev(cpu);
1761 case CPU_ONLINE_FROZEN:
1762 cpufreq_add_dev(sys_dev);
1764 case CPU_DOWN_PREPARE:
1765 case CPU_DOWN_PREPARE_FROZEN:
1766 if (unlikely(lock_policy_rwsem_write(cpu)))
1769 __cpufreq_remove_dev(sys_dev);
1771 case CPU_DOWN_FAILED:
1772 case CPU_DOWN_FAILED_FROZEN:
1773 cpufreq_add_dev(sys_dev);
1780 static struct notifier_block __refdata cpufreq_cpu_notifier = {
1781 .notifier_call = cpufreq_cpu_callback,
1784 /*********************************************************************
1785 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1786 *********************************************************************/
1789 * cpufreq_register_driver - register a CPU Frequency driver
1790 * @driver_data: A struct cpufreq_driver containing the values#
1791 * submitted by the CPU Frequency driver.
1793 * Registers a CPU Frequency driver to this core code. This code
1794 * returns zero on success, -EBUSY when another driver got here first
1795 * (and isn't unregistered in the meantime).
1798 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1800 unsigned long flags;
1803 if (!driver_data || !driver_data->verify || !driver_data->init ||
1804 ((!driver_data->setpolicy) && (!driver_data->target)))
1807 pr_debug("trying to register driver %s\n", driver_data->name);
1809 if (driver_data->setpolicy)
1810 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1812 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1813 if (cpufreq_driver) {
1814 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1817 cpufreq_driver = driver_data;
1818 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1820 ret = sysdev_driver_register(&cpu_sysdev_class,
1821 &cpufreq_sysdev_driver);
1823 goto err_null_driver;
1825 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1829 /* check for at least one working CPU */
1830 for (i = 0; i < nr_cpu_ids; i++)
1831 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
1836 /* if all ->init() calls failed, unregister */
1838 pr_debug("no CPU initialized for driver %s\n",
1840 goto err_sysdev_unreg;
1844 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1845 pr_debug("driver %s up and running\n", driver_data->name);
1849 sysdev_driver_unregister(&cpu_sysdev_class,
1850 &cpufreq_sysdev_driver);
1852 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1853 cpufreq_driver = NULL;
1854 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1857 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1861 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1863 * Unregister the current CPUFreq driver. Only call this if you have
1864 * the right to do so, i.e. if you have succeeded in initialising before!
1865 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1866 * currently not initialised.
1868 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1870 unsigned long flags;
1872 if (!cpufreq_driver || (driver != cpufreq_driver))
1875 pr_debug("unregistering driver %s\n", driver->name);
1877 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1878 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1880 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1881 cpufreq_driver = NULL;
1882 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1886 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
1888 static int __init cpufreq_core_init(void)
1892 for_each_possible_cpu(cpu) {
1893 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1894 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1897 cpufreq_global_kobject = kobject_create_and_add("cpufreq",
1898 &cpu_sysdev_class.kset.kobj);
1899 BUG_ON(!cpufreq_global_kobject);
1900 register_syscore_ops(&cpufreq_syscore_ops);
1904 core_initcall(cpufreq_core_init);