xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision d5b73cd870e2b049ef566aec2791dbf5fd26a7ec)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/drivers/cpufreq/cpufreq.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2001 Russell King
51da177e4SLinus Torvalds  *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6bb176f7dSViresh Kumar  *            (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
71da177e4SLinus Torvalds  *
8c32b6b8eSAshok Raj  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9c32b6b8eSAshok Raj  *	Added handling for CPU hotplug
108ff69732SDave Jones  *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
118ff69732SDave Jones  *	Fix handling for CPU hotplug -- affected CPUs
12c32b6b8eSAshok Raj  *
131da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
141da177e4SLinus Torvalds  * it under the terms of the GNU General Public License version 2 as
151da177e4SLinus Torvalds  * published by the Free Software Foundation.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
18db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19db701151SViresh Kumar 
205ff0a268SViresh Kumar #include <linux/cpu.h>
211da177e4SLinus Torvalds #include <linux/cpufreq.h>
221da177e4SLinus Torvalds #include <linux/delay.h>
231da177e4SLinus Torvalds #include <linux/device.h>
245ff0a268SViresh Kumar #include <linux/init.h>
255ff0a268SViresh Kumar #include <linux/kernel_stat.h>
265ff0a268SViresh Kumar #include <linux/module.h>
273fc54d37Sakpm@osdl.org #include <linux/mutex.h>
285ff0a268SViresh Kumar #include <linux/slab.h>
29e00e56dfSRafael J. Wysocki #include <linux/syscore_ops.h>
305ff0a268SViresh Kumar #include <linux/tick.h>
316f4f2723SThomas Renninger #include <trace/events/power.h>
326f4f2723SThomas Renninger 
331da177e4SLinus Torvalds /**
34cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
351da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
361da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
371da177e4SLinus Torvalds  */
381c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
397a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
408414809cSSrivatsa S. Bhat static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
41bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
42bb176f7dSViresh Kumar static DEFINE_MUTEX(cpufreq_governor_lock);
43bb176f7dSViresh Kumar 
44084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU
45084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */
46e77b89f1SDmitry Monakhov static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
47084f3493SThomas Renninger #endif
481da177e4SLinus Torvalds 
495a01f2e8SVenkatesh Pallipadi /*
505a01f2e8SVenkatesh Pallipadi  * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
515a01f2e8SVenkatesh Pallipadi  * all cpufreq/hotplug/workqueue/etc related lock issues.
525a01f2e8SVenkatesh Pallipadi  *
535a01f2e8SVenkatesh Pallipadi  * The rules for this semaphore:
545a01f2e8SVenkatesh Pallipadi  * - Any routine that wants to read from the policy structure will
555a01f2e8SVenkatesh Pallipadi  *   do a down_read on this semaphore.
565a01f2e8SVenkatesh Pallipadi  * - Any routine that will write to the policy structure and/or may take away
575a01f2e8SVenkatesh Pallipadi  *   the policy altogether (eg. CPU hotplug), will hold this lock in write
585a01f2e8SVenkatesh Pallipadi  *   mode before doing so.
595a01f2e8SVenkatesh Pallipadi  *
605a01f2e8SVenkatesh Pallipadi  * Additional rules:
615a01f2e8SVenkatesh Pallipadi  * - Governor routines that can be called in cpufreq hotplug path should not
625a01f2e8SVenkatesh Pallipadi  *   take this sem as top level hotplug notifier handler takes this.
63395913d0SMathieu Desnoyers  * - Lock should not be held across
64395913d0SMathieu Desnoyers  *     __cpufreq_governor(data, CPUFREQ_GOV_STOP);
655a01f2e8SVenkatesh Pallipadi  */
66f1625066STejun Heo static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
675a01f2e8SVenkatesh Pallipadi static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
685a01f2e8SVenkatesh Pallipadi 
695a01f2e8SVenkatesh Pallipadi #define lock_policy_rwsem(mode, cpu)					\
70fa1d8af4SViresh Kumar static int lock_policy_rwsem_##mode(int cpu)				\
715a01f2e8SVenkatesh Pallipadi {									\
72f1625066STejun Heo 	int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);		\
735a01f2e8SVenkatesh Pallipadi 	BUG_ON(policy_cpu == -1);					\
745a01f2e8SVenkatesh Pallipadi 	down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));		\
755a01f2e8SVenkatesh Pallipadi 									\
765a01f2e8SVenkatesh Pallipadi 	return 0;							\
775a01f2e8SVenkatesh Pallipadi }
785a01f2e8SVenkatesh Pallipadi 
795a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(read, cpu);
805a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(write, cpu);
815a01f2e8SVenkatesh Pallipadi 
82fa1d8af4SViresh Kumar #define unlock_policy_rwsem(mode, cpu)					\
83fa1d8af4SViresh Kumar static void unlock_policy_rwsem_##mode(int cpu)				\
84fa1d8af4SViresh Kumar {									\
85fa1d8af4SViresh Kumar 	int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);		\
86fa1d8af4SViresh Kumar 	BUG_ON(policy_cpu == -1);					\
87fa1d8af4SViresh Kumar 	up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));		\
885a01f2e8SVenkatesh Pallipadi }
895a01f2e8SVenkatesh Pallipadi 
90fa1d8af4SViresh Kumar unlock_policy_rwsem(read, cpu);
91fa1d8af4SViresh Kumar unlock_policy_rwsem(write, cpu);
925a01f2e8SVenkatesh Pallipadi 
931da177e4SLinus Torvalds /* internal prototypes */
9429464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy,
9529464f28SDave Jones 		unsigned int event);
965a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu);
9765f27f38SDavid Howells static void handle_update(struct work_struct *work);
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /**
1001da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
1011da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
1021da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
1031da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
1041da177e4SLinus Torvalds  * The mutex locks both lists.
1051da177e4SLinus Torvalds  */
106e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
107b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list;
1081da177e4SLinus Torvalds 
10974212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called;
110b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void)
111b4dfdbb3SAlan Stern {
112b4dfdbb3SAlan Stern 	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
11374212ca4SCesar Eduardo Barros 	init_cpufreq_transition_notifier_list_called = true;
114b4dfdbb3SAlan Stern 	return 0;
115b4dfdbb3SAlan Stern }
116b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list);
1171da177e4SLinus Torvalds 
118a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
119da584455SViresh Kumar static int cpufreq_disabled(void)
120a7b422cdSKonrad Rzeszutek Wilk {
121a7b422cdSKonrad Rzeszutek Wilk 	return off;
122a7b422cdSKonrad Rzeszutek Wilk }
123a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
124a7b422cdSKonrad Rzeszutek Wilk {
125a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
126a7b422cdSKonrad Rzeszutek Wilk }
1271da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list);
1283fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
1291da177e4SLinus Torvalds 
1304d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1314d5dcc42SViresh Kumar {
1321c3d85ddSRafael J. Wysocki 	return cpufreq_driver->have_governor_per_policy;
1334d5dcc42SViresh Kumar }
1343f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1354d5dcc42SViresh Kumar 
136944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
137944e9a03SViresh Kumar {
138944e9a03SViresh Kumar 	if (have_governor_per_policy())
139944e9a03SViresh Kumar 		return &policy->kobj;
140944e9a03SViresh Kumar 	else
141944e9a03SViresh Kumar 		return cpufreq_global_kobject;
142944e9a03SViresh Kumar }
143944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
144944e9a03SViresh Kumar 
14572a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
14672a4ce34SViresh Kumar {
14772a4ce34SViresh Kumar 	u64 idle_time;
14872a4ce34SViresh Kumar 	u64 cur_wall_time;
14972a4ce34SViresh Kumar 	u64 busy_time;
15072a4ce34SViresh Kumar 
15172a4ce34SViresh Kumar 	cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
15272a4ce34SViresh Kumar 
15372a4ce34SViresh Kumar 	busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
15472a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
15572a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
15672a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
15772a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
15872a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
15972a4ce34SViresh Kumar 
16072a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
16172a4ce34SViresh Kumar 	if (wall)
16272a4ce34SViresh Kumar 		*wall = cputime_to_usecs(cur_wall_time);
16372a4ce34SViresh Kumar 
16472a4ce34SViresh Kumar 	return cputime_to_usecs(idle_time);
16572a4ce34SViresh Kumar }
16672a4ce34SViresh Kumar 
16772a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
16872a4ce34SViresh Kumar {
16972a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
17072a4ce34SViresh Kumar 
17172a4ce34SViresh Kumar 	if (idle_time == -1ULL)
17272a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
17372a4ce34SViresh Kumar 	else if (!io_busy)
17472a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
17572a4ce34SViresh Kumar 
17672a4ce34SViresh Kumar 	return idle_time;
17772a4ce34SViresh Kumar }
17872a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
17972a4ce34SViresh Kumar 
180a9144436SStephen Boyd static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
1811da177e4SLinus Torvalds {
1823a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
1831da177e4SLinus Torvalds 	unsigned long flags;
1841da177e4SLinus Torvalds 
1857a6aedfaSMike Travis 	if (cpu >= nr_cpu_ids)
1861da177e4SLinus Torvalds 		goto err_out;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	/* get the cpufreq driver */
1890d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1901da177e4SLinus Torvalds 
1911c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver)
1921c3d85ddSRafael J. Wysocki 		goto err_out_unlock;
1931c3d85ddSRafael J. Wysocki 
1941c3d85ddSRafael J. Wysocki 	if (!try_module_get(cpufreq_driver->owner))
1951c3d85ddSRafael J. Wysocki 		goto err_out_unlock;
1961c3d85ddSRafael J. Wysocki 
1971da177e4SLinus Torvalds 	/* get the CPU */
1983a3e9e06SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
1991da177e4SLinus Torvalds 
2003a3e9e06SViresh Kumar 	if (!policy)
2011da177e4SLinus Torvalds 		goto err_out_put_module;
2021da177e4SLinus Torvalds 
2033a3e9e06SViresh Kumar 	if (!sysfs && !kobject_get(&policy->kobj))
2041da177e4SLinus Torvalds 		goto err_out_put_module;
2051da177e4SLinus Torvalds 
2060d1857a1SNathan Zimmer 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2073a3e9e06SViresh Kumar 	return policy;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds err_out_put_module:
2101c3d85ddSRafael J. Wysocki 	module_put(cpufreq_driver->owner);
2115800043bSNathan Zimmer err_out_unlock:
2121c3d85ddSRafael J. Wysocki 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2131da177e4SLinus Torvalds err_out:
2141da177e4SLinus Torvalds 	return NULL;
2151da177e4SLinus Torvalds }
216a9144436SStephen Boyd 
217a9144436SStephen Boyd struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
218a9144436SStephen Boyd {
219d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
220d5aaffa9SDirk Brandewie 		return NULL;
221d5aaffa9SDirk Brandewie 
222a9144436SStephen Boyd 	return __cpufreq_cpu_get(cpu, false);
223a9144436SStephen Boyd }
2241da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2251da177e4SLinus Torvalds 
226a9144436SStephen Boyd static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
2271da177e4SLinus Torvalds {
228a9144436SStephen Boyd 	return __cpufreq_cpu_get(cpu, true);
229a9144436SStephen Boyd }
230a9144436SStephen Boyd 
2313a3e9e06SViresh Kumar static void __cpufreq_cpu_put(struct cpufreq_policy *policy, bool sysfs)
232a9144436SStephen Boyd {
233a9144436SStephen Boyd 	if (!sysfs)
2343a3e9e06SViresh Kumar 		kobject_put(&policy->kobj);
2351c3d85ddSRafael J. Wysocki 	module_put(cpufreq_driver->owner);
2361da177e4SLinus Torvalds }
237a9144436SStephen Boyd 
2383a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
239a9144436SStephen Boyd {
240d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
241d5aaffa9SDirk Brandewie 		return;
242d5aaffa9SDirk Brandewie 
2433a3e9e06SViresh Kumar 	__cpufreq_cpu_put(policy, false);
244a9144436SStephen Boyd }
2451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2461da177e4SLinus Torvalds 
2473a3e9e06SViresh Kumar static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *policy)
248a9144436SStephen Boyd {
2493a3e9e06SViresh Kumar 	__cpufreq_cpu_put(policy, true);
250a9144436SStephen Boyd }
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds /*********************************************************************
2531da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2541da177e4SLinus Torvalds  *********************************************************************/
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds /**
2571da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
2581da177e4SLinus Torvalds  *
2591da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
2601da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
2611da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
2621da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
2631da177e4SLinus Torvalds  */
2641da177e4SLinus Torvalds #ifndef CONFIG_SMP
2651da177e4SLinus Torvalds static unsigned long l_p_j_ref;
2661da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq;
2671da177e4SLinus Torvalds 
268858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
2711da177e4SLinus Torvalds 		return;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
2741da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
2751da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
2762d06d8c4SDominik Brodowski 		pr_debug("saving %lu as reference value for loops_per_jiffy; "
277e08f5f5bSGautham R Shenoy 			"freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
2781da177e4SLinus Torvalds 	}
279d08de0c1SAfzal Mohammed 	if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
28042d4dc3fSBenjamin Herrenschmidt 	    (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
281e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
282e08f5f5bSGautham R Shenoy 								ci->new);
2832d06d8c4SDominik Brodowski 		pr_debug("scaling loops_per_jiffy to %lu "
284e08f5f5bSGautham R Shenoy 			"for frequency %u kHz\n", loops_per_jiffy, ci->new);
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds #else
288e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
289e08f5f5bSGautham R Shenoy {
290e08f5f5bSGautham R Shenoy 	return;
291e08f5f5bSGautham R Shenoy }
2921da177e4SLinus Torvalds #endif
2931da177e4SLinus Torvalds 
2940956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
295b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
2981da177e4SLinus Torvalds 
299d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
300d5aaffa9SDirk Brandewie 		return;
301d5aaffa9SDirk Brandewie 
3021c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
3032d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
304e4472cb3SDave Jones 		state, freqs->new);
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds 	switch (state) {
307e4472cb3SDave Jones 
3081da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
309266c13d7SViresh Kumar 		if (WARN(policy->transition_ongoing ==
310266c13d7SViresh Kumar 					cpumask_weight(policy->cpus),
3117c30ed53SViresh Kumar 				"In middle of another frequency transition\n"))
3127c30ed53SViresh Kumar 			return;
3137c30ed53SViresh Kumar 
314266c13d7SViresh Kumar 		policy->transition_ongoing++;
3157c30ed53SViresh Kumar 
316e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
317e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
318e4472cb3SDave Jones 		 * "old frequency".
3191da177e4SLinus Torvalds 		 */
3201c3d85ddSRafael J. Wysocki 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
321e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
322e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
3232d06d8c4SDominik Brodowski 				pr_debug("Warning: CPU frequency is"
324e4472cb3SDave Jones 					" %u, cpufreq assumed %u kHz.\n",
325e4472cb3SDave Jones 					freqs->old, policy->cur);
326e4472cb3SDave Jones 				freqs->old = policy->cur;
3271da177e4SLinus Torvalds 			}
3281da177e4SLinus Torvalds 		}
329b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
330e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
3311da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3321da177e4SLinus Torvalds 		break;
333e4472cb3SDave Jones 
3341da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3357c30ed53SViresh Kumar 		if (WARN(!policy->transition_ongoing,
3367c30ed53SViresh Kumar 				"No frequency transition in progress\n"))
3377c30ed53SViresh Kumar 			return;
3387c30ed53SViresh Kumar 
339266c13d7SViresh Kumar 		policy->transition_ongoing--;
3407c30ed53SViresh Kumar 
3411da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
3422d06d8c4SDominik Brodowski 		pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
3436f4f2723SThomas Renninger 			(unsigned long)freqs->cpu);
34425e41933SThomas Renninger 		trace_cpu_frequency(freqs->new, freqs->cpu);
345b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
346e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
347e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
348e4472cb3SDave Jones 			policy->cur = freqs->new;
3491da177e4SLinus Torvalds 		break;
3501da177e4SLinus Torvalds 	}
3511da177e4SLinus Torvalds }
352bb176f7dSViresh Kumar 
353b43a7ffbSViresh Kumar /**
354b43a7ffbSViresh Kumar  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
355b43a7ffbSViresh Kumar  * on frequency transition.
356b43a7ffbSViresh Kumar  *
357b43a7ffbSViresh Kumar  * This function calls the transition notifiers and the "adjust_jiffies"
358b43a7ffbSViresh Kumar  * function. It is called twice on all CPU frequency changes that have
359b43a7ffbSViresh Kumar  * external effects.
360b43a7ffbSViresh Kumar  */
361b43a7ffbSViresh Kumar void cpufreq_notify_transition(struct cpufreq_policy *policy,
362b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
363b43a7ffbSViresh Kumar {
364b43a7ffbSViresh Kumar 	for_each_cpu(freqs->cpu, policy->cpus)
365b43a7ffbSViresh Kumar 		__cpufreq_notify_transition(policy, freqs, state);
366b43a7ffbSViresh Kumar }
3671da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds /*********************************************************************
3711da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
3721da177e4SLinus Torvalds  *********************************************************************/
3731da177e4SLinus Torvalds 
3743bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor)
3753bcb09a3SJeremy Fitzhardinge {
3763bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
3773bcb09a3SJeremy Fitzhardinge 
3783bcb09a3SJeremy Fitzhardinge 	list_for_each_entry(t, &cpufreq_governor_list, governor_list)
3793bcb09a3SJeremy Fitzhardinge 		if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
3803bcb09a3SJeremy Fitzhardinge 			return t;
3813bcb09a3SJeremy Fitzhardinge 
3823bcb09a3SJeremy Fitzhardinge 	return NULL;
3833bcb09a3SJeremy Fitzhardinge }
3843bcb09a3SJeremy Fitzhardinge 
3851da177e4SLinus Torvalds /**
3861da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
3871da177e4SLinus Torvalds  */
3881da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
3891da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
3901da177e4SLinus Torvalds {
3913bcb09a3SJeremy Fitzhardinge 	int err = -EINVAL;
3923bcb09a3SJeremy Fitzhardinge 
3931c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver)
3943bcb09a3SJeremy Fitzhardinge 		goto out;
3953bcb09a3SJeremy Fitzhardinge 
3961c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
3971da177e4SLinus Torvalds 		if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
3981da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
3993bcb09a3SJeremy Fitzhardinge 			err = 0;
400e08f5f5bSGautham R Shenoy 		} else if (!strnicmp(str_governor, "powersave",
401e08f5f5bSGautham R Shenoy 						CPUFREQ_NAME_LEN)) {
4021da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
4033bcb09a3SJeremy Fitzhardinge 			err = 0;
4041da177e4SLinus Torvalds 		}
4051c3d85ddSRafael J. Wysocki 	} else if (cpufreq_driver->target) {
4061da177e4SLinus Torvalds 		struct cpufreq_governor *t;
4073bcb09a3SJeremy Fitzhardinge 
4083fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
4093bcb09a3SJeremy Fitzhardinge 
4103bcb09a3SJeremy Fitzhardinge 		t = __find_governor(str_governor);
4113bcb09a3SJeremy Fitzhardinge 
412ea714970SJeremy Fitzhardinge 		if (t == NULL) {
413ea714970SJeremy Fitzhardinge 			int ret;
414ea714970SJeremy Fitzhardinge 
415ea714970SJeremy Fitzhardinge 			mutex_unlock(&cpufreq_governor_mutex);
4161a8e1463SKees Cook 			ret = request_module("cpufreq_%s", str_governor);
417ea714970SJeremy Fitzhardinge 			mutex_lock(&cpufreq_governor_mutex);
418ea714970SJeremy Fitzhardinge 
419ea714970SJeremy Fitzhardinge 			if (ret == 0)
420ea714970SJeremy Fitzhardinge 				t = __find_governor(str_governor);
421ea714970SJeremy Fitzhardinge 		}
422ea714970SJeremy Fitzhardinge 
4233bcb09a3SJeremy Fitzhardinge 		if (t != NULL) {
4241da177e4SLinus Torvalds 			*governor = t;
4253bcb09a3SJeremy Fitzhardinge 			err = 0;
4261da177e4SLinus Torvalds 		}
4273bcb09a3SJeremy Fitzhardinge 
4283bcb09a3SJeremy Fitzhardinge 		mutex_unlock(&cpufreq_governor_mutex);
4291da177e4SLinus Torvalds 	}
4301da177e4SLinus Torvalds out:
4313bcb09a3SJeremy Fitzhardinge 	return err;
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds /**
435e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
436e08f5f5bSGautham R Shenoy  * print out cpufreq information
4371da177e4SLinus Torvalds  *
4381da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
4391da177e4SLinus Torvalds  * "unsigned int".
4401da177e4SLinus Torvalds  */
4411da177e4SLinus Torvalds 
4421da177e4SLinus Torvalds #define show_one(file_name, object)			\
4431da177e4SLinus Torvalds static ssize_t show_##file_name				\
4441da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
4451da177e4SLinus Torvalds {							\
4461da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
4501da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
451ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
4521da177e4SLinus Torvalds show_one(scaling_min_freq, min);
4531da177e4SLinus Torvalds show_one(scaling_max_freq, max);
4541da177e4SLinus Torvalds show_one(scaling_cur_freq, cur);
4551da177e4SLinus Torvalds 
4563a3e9e06SViresh Kumar static int __cpufreq_set_policy(struct cpufreq_policy *policy,
4573a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy);
4587970e08bSThomas Renninger 
4591da177e4SLinus Torvalds /**
4601da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
4611da177e4SLinus Torvalds  */
4621da177e4SLinus Torvalds #define store_one(file_name, object)			\
4631da177e4SLinus Torvalds static ssize_t store_##file_name					\
4641da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
4651da177e4SLinus Torvalds {									\
466f55c9c26SJingoo Han 	unsigned int ret;						\
4671da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
4681da177e4SLinus Torvalds 									\
4691da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
4701da177e4SLinus Torvalds 	if (ret)							\
4711da177e4SLinus Torvalds 		return -EINVAL;						\
4721da177e4SLinus Torvalds 									\
4731da177e4SLinus Torvalds 	ret = sscanf(buf, "%u", &new_policy.object);			\
4741da177e4SLinus Torvalds 	if (ret != 1)							\
4751da177e4SLinus Torvalds 		return -EINVAL;						\
4761da177e4SLinus Torvalds 									\
4777970e08bSThomas Renninger 	ret = __cpufreq_set_policy(policy, &new_policy);		\
4787970e08bSThomas Renninger 	policy->user_policy.object = policy->object;			\
4791da177e4SLinus Torvalds 									\
4801da177e4SLinus Torvalds 	return ret ? ret : count;					\
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds store_one(scaling_min_freq, min);
4841da177e4SLinus Torvalds store_one(scaling_max_freq, max);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /**
4871da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
4881da177e4SLinus Torvalds  */
489e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
490e08f5f5bSGautham R Shenoy 					char *buf)
4911da177e4SLinus Torvalds {
4925a01f2e8SVenkatesh Pallipadi 	unsigned int cur_freq = __cpufreq_get(policy->cpu);
4931da177e4SLinus Torvalds 	if (!cur_freq)
4941da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
4951da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds /**
4991da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
5001da177e4SLinus Torvalds  */
501905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
5021da177e4SLinus Torvalds {
5031da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
5041da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
5051da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
5061da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
5071da177e4SLinus Torvalds 	else if (policy->governor)
5084b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
50929464f28SDave Jones 				policy->governor->name);
5101da177e4SLinus Torvalds 	return -EINVAL;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds /**
5141da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
5151da177e4SLinus Torvalds  */
5161da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
5171da177e4SLinus Torvalds 					const char *buf, size_t count)
5181da177e4SLinus Torvalds {
519f55c9c26SJingoo Han 	unsigned int ret;
5201da177e4SLinus Torvalds 	char	str_governor[16];
5211da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);
5241da177e4SLinus Torvalds 	if (ret)
5251da177e4SLinus Torvalds 		return ret;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
5281da177e4SLinus Torvalds 	if (ret != 1)
5291da177e4SLinus Torvalds 		return -EINVAL;
5301da177e4SLinus Torvalds 
531e08f5f5bSGautham R Shenoy 	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
532e08f5f5bSGautham R Shenoy 						&new_policy.governor))
5331da177e4SLinus Torvalds 		return -EINVAL;
5341da177e4SLinus Torvalds 
535bb176f7dSViresh Kumar 	/*
536bb176f7dSViresh Kumar 	 * Do not use cpufreq_set_policy here or the user_policy.max
537bb176f7dSViresh Kumar 	 * will be wrongly overridden
538bb176f7dSViresh Kumar 	 */
5397970e08bSThomas Renninger 	ret = __cpufreq_set_policy(policy, &new_policy);
5407970e08bSThomas Renninger 
5417970e08bSThomas Renninger 	policy->user_policy.policy = policy->policy;
5427970e08bSThomas Renninger 	policy->user_policy.governor = policy->governor;
5437970e08bSThomas Renninger 
544e08f5f5bSGautham R Shenoy 	if (ret)
545e08f5f5bSGautham R Shenoy 		return ret;
546e08f5f5bSGautham R Shenoy 	else
547e08f5f5bSGautham R Shenoy 		return count;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds /**
5511da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
5521da177e4SLinus Torvalds  */
5531da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
5541da177e4SLinus Torvalds {
5551c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds /**
5591da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
5601da177e4SLinus Torvalds  */
5611da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
5621da177e4SLinus Torvalds 						char *buf)
5631da177e4SLinus Torvalds {
5641da177e4SLinus Torvalds 	ssize_t i = 0;
5651da177e4SLinus Torvalds 	struct cpufreq_governor *t;
5661da177e4SLinus Torvalds 
5671c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->target) {
5681da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
5691da177e4SLinus Torvalds 		goto out;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
57329464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
57429464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
5751da177e4SLinus Torvalds 			goto out;
5764b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
5771da177e4SLinus Torvalds 	}
5781da177e4SLinus Torvalds out:
5791da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
5801da177e4SLinus Torvalds 	return i;
5811da177e4SLinus Torvalds }
582e8628dd0SDarrick J. Wong 
583f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
5841da177e4SLinus Torvalds {
5851da177e4SLinus Torvalds 	ssize_t i = 0;
5861da177e4SLinus Torvalds 	unsigned int cpu;
5871da177e4SLinus Torvalds 
588835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
5891da177e4SLinus Torvalds 		if (i)
5901da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
5911da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
5921da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
5931da177e4SLinus Torvalds 			break;
5941da177e4SLinus Torvalds 	}
5951da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
5961da177e4SLinus Torvalds 	return i;
5971da177e4SLinus Torvalds }
598f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
5991da177e4SLinus Torvalds 
600e8628dd0SDarrick J. Wong /**
601e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
602e8628dd0SDarrick J. Wong  * hw coordination is in use
603e8628dd0SDarrick J. Wong  */
604e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
605e8628dd0SDarrick J. Wong {
606f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
607e8628dd0SDarrick J. Wong }
608e8628dd0SDarrick J. Wong 
609e8628dd0SDarrick J. Wong /**
610e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
611e8628dd0SDarrick J. Wong  */
612e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
613e8628dd0SDarrick J. Wong {
614f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
615e8628dd0SDarrick J. Wong }
616e8628dd0SDarrick J. Wong 
6179e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
6189e76988eSVenki Pallipadi 					const char *buf, size_t count)
6199e76988eSVenki Pallipadi {
6209e76988eSVenki Pallipadi 	unsigned int freq = 0;
6219e76988eSVenki Pallipadi 	unsigned int ret;
6229e76988eSVenki Pallipadi 
623879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
6249e76988eSVenki Pallipadi 		return -EINVAL;
6259e76988eSVenki Pallipadi 
6269e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
6279e76988eSVenki Pallipadi 	if (ret != 1)
6289e76988eSVenki Pallipadi 		return -EINVAL;
6299e76988eSVenki Pallipadi 
6309e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
6319e76988eSVenki Pallipadi 
6329e76988eSVenki Pallipadi 	return count;
6339e76988eSVenki Pallipadi }
6349e76988eSVenki Pallipadi 
6359e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
6369e76988eSVenki Pallipadi {
637879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
6389e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
6399e76988eSVenki Pallipadi 
6409e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
6419e76988eSVenki Pallipadi }
6421da177e4SLinus Torvalds 
643e2f74f35SThomas Renninger /**
6448bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
645e2f74f35SThomas Renninger  */
646e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
647e2f74f35SThomas Renninger {
648e2f74f35SThomas Renninger 	unsigned int limit;
649e2f74f35SThomas Renninger 	int ret;
6501c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
6511c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
652e2f74f35SThomas Renninger 		if (!ret)
653e2f74f35SThomas Renninger 			return sprintf(buf, "%u\n", limit);
654e2f74f35SThomas Renninger 	}
655e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
656e2f74f35SThomas Renninger }
657e2f74f35SThomas Renninger 
6586dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
6596dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
6606dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
6616dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
6626dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
6636dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
6646dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
6656dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
6666dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
6676dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
6686dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
6696dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
6706dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
6716dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds static struct attribute *default_attrs[] = {
6741da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
6751da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
676ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
6771da177e4SLinus Torvalds 	&scaling_min_freq.attr,
6781da177e4SLinus Torvalds 	&scaling_max_freq.attr,
6791da177e4SLinus Torvalds 	&affected_cpus.attr,
680e8628dd0SDarrick J. Wong 	&related_cpus.attr,
6811da177e4SLinus Torvalds 	&scaling_governor.attr,
6821da177e4SLinus Torvalds 	&scaling_driver.attr,
6831da177e4SLinus Torvalds 	&scaling_available_governors.attr,
6849e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
6851da177e4SLinus Torvalds 	NULL
6861da177e4SLinus Torvalds };
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
6891da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
6921da177e4SLinus Torvalds {
6931da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
6941da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
6950db4a8a9SDave Jones 	ssize_t ret = -EINVAL;
696a9144436SStephen Boyd 	policy = cpufreq_cpu_get_sysfs(policy->cpu);
6971da177e4SLinus Torvalds 	if (!policy)
6980db4a8a9SDave Jones 		goto no_policy;
6995a01f2e8SVenkatesh Pallipadi 
7005a01f2e8SVenkatesh Pallipadi 	if (lock_policy_rwsem_read(policy->cpu) < 0)
7010db4a8a9SDave Jones 		goto fail;
7025a01f2e8SVenkatesh Pallipadi 
703e08f5f5bSGautham R Shenoy 	if (fattr->show)
704e08f5f5bSGautham R Shenoy 		ret = fattr->show(policy, buf);
705e08f5f5bSGautham R Shenoy 	else
706e08f5f5bSGautham R Shenoy 		ret = -EIO;
707e08f5f5bSGautham R Shenoy 
7085a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_read(policy->cpu);
7090db4a8a9SDave Jones fail:
710a9144436SStephen Boyd 	cpufreq_cpu_put_sysfs(policy);
7110db4a8a9SDave Jones no_policy:
7121da177e4SLinus Torvalds 	return ret;
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
7161da177e4SLinus Torvalds 		     const char *buf, size_t count)
7171da177e4SLinus Torvalds {
7181da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7191da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
720a07530b4SDave Jones 	ssize_t ret = -EINVAL;
721a9144436SStephen Boyd 	policy = cpufreq_cpu_get_sysfs(policy->cpu);
7221da177e4SLinus Torvalds 	if (!policy)
723a07530b4SDave Jones 		goto no_policy;
7245a01f2e8SVenkatesh Pallipadi 
7255a01f2e8SVenkatesh Pallipadi 	if (lock_policy_rwsem_write(policy->cpu) < 0)
726a07530b4SDave Jones 		goto fail;
7275a01f2e8SVenkatesh Pallipadi 
728e08f5f5bSGautham R Shenoy 	if (fattr->store)
729e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
730e08f5f5bSGautham R Shenoy 	else
731e08f5f5bSGautham R Shenoy 		ret = -EIO;
732e08f5f5bSGautham R Shenoy 
7335a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(policy->cpu);
734a07530b4SDave Jones fail:
735a9144436SStephen Boyd 	cpufreq_cpu_put_sysfs(policy);
736a07530b4SDave Jones no_policy:
7371da177e4SLinus Torvalds 	return ret;
7381da177e4SLinus Torvalds }
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
7411da177e4SLinus Torvalds {
7421da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7432d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
7441da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
7451da177e4SLinus Torvalds }
7461da177e4SLinus Torvalds 
74752cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
7481da177e4SLinus Torvalds 	.show	= show,
7491da177e4SLinus Torvalds 	.store	= store,
7501da177e4SLinus Torvalds };
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
7531da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
7541da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
7551da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
7561da177e4SLinus Torvalds };
7571da177e4SLinus Torvalds 
7582361be23SViresh Kumar struct kobject *cpufreq_global_kobject;
7592361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject);
7602361be23SViresh Kumar 
7612361be23SViresh Kumar static int cpufreq_global_kobject_usage;
7622361be23SViresh Kumar 
7632361be23SViresh Kumar int cpufreq_get_global_kobject(void)
7642361be23SViresh Kumar {
7652361be23SViresh Kumar 	if (!cpufreq_global_kobject_usage++)
7662361be23SViresh Kumar 		return kobject_add(cpufreq_global_kobject,
7672361be23SViresh Kumar 				&cpu_subsys.dev_root->kobj, "%s", "cpufreq");
7682361be23SViresh Kumar 
7692361be23SViresh Kumar 	return 0;
7702361be23SViresh Kumar }
7712361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject);
7722361be23SViresh Kumar 
7732361be23SViresh Kumar void cpufreq_put_global_kobject(void)
7742361be23SViresh Kumar {
7752361be23SViresh Kumar 	if (!--cpufreq_global_kobject_usage)
7762361be23SViresh Kumar 		kobject_del(cpufreq_global_kobject);
7772361be23SViresh Kumar }
7782361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject);
7792361be23SViresh Kumar 
7802361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr)
7812361be23SViresh Kumar {
7822361be23SViresh Kumar 	int ret = cpufreq_get_global_kobject();
7832361be23SViresh Kumar 
7842361be23SViresh Kumar 	if (!ret) {
7852361be23SViresh Kumar 		ret = sysfs_create_file(cpufreq_global_kobject, attr);
7862361be23SViresh Kumar 		if (ret)
7872361be23SViresh Kumar 			cpufreq_put_global_kobject();
7882361be23SViresh Kumar 	}
7892361be23SViresh Kumar 
7902361be23SViresh Kumar 	return ret;
7912361be23SViresh Kumar }
7922361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file);
7932361be23SViresh Kumar 
7942361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr)
7952361be23SViresh Kumar {
7962361be23SViresh Kumar 	sysfs_remove_file(cpufreq_global_kobject, attr);
7972361be23SViresh Kumar 	cpufreq_put_global_kobject();
7982361be23SViresh Kumar }
7992361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
8002361be23SViresh Kumar 
80119d6f7ecSDave Jones /* symlink affected CPUs */
802308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
80319d6f7ecSDave Jones {
80419d6f7ecSDave Jones 	unsigned int j;
80519d6f7ecSDave Jones 	int ret = 0;
80619d6f7ecSDave Jones 
80719d6f7ecSDave Jones 	for_each_cpu(j, policy->cpus) {
8088a25a2fdSKay Sievers 		struct device *cpu_dev;
80919d6f7ecSDave Jones 
810308b60e7SViresh Kumar 		if (j == policy->cpu)
81119d6f7ecSDave Jones 			continue;
81219d6f7ecSDave Jones 
813e8fdde10SViresh Kumar 		pr_debug("Adding link for CPU: %u\n", j);
8148a25a2fdSKay Sievers 		cpu_dev = get_cpu_device(j);
8158a25a2fdSKay Sievers 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
81619d6f7ecSDave Jones 					"cpufreq");
81771c3461eSRafael J. Wysocki 		if (ret)
81871c3461eSRafael J. Wysocki 			break;
81919d6f7ecSDave Jones 	}
82019d6f7ecSDave Jones 	return ret;
82119d6f7ecSDave Jones }
82219d6f7ecSDave Jones 
823308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
8248a25a2fdSKay Sievers 				     struct device *dev)
825909a694eSDave Jones {
826909a694eSDave Jones 	struct freq_attr **drv_attr;
827909a694eSDave Jones 	int ret = 0;
828909a694eSDave Jones 
829909a694eSDave Jones 	/* prepare interface data */
830909a694eSDave Jones 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
8318a25a2fdSKay Sievers 				   &dev->kobj, "cpufreq");
832909a694eSDave Jones 	if (ret)
833909a694eSDave Jones 		return ret;
834909a694eSDave Jones 
835909a694eSDave Jones 	/* set up files for this cpu device */
8361c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
837909a694eSDave Jones 	while ((drv_attr) && (*drv_attr)) {
838909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
839909a694eSDave Jones 		if (ret)
8401c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
841909a694eSDave Jones 		drv_attr++;
842909a694eSDave Jones 	}
8431c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
844909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
845909a694eSDave Jones 		if (ret)
8461c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
847909a694eSDave Jones 	}
8481c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target) {
849909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
850909a694eSDave Jones 		if (ret)
8511c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
852909a694eSDave Jones 	}
8531c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
854e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
855e2f74f35SThomas Renninger 		if (ret)
8561c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
857e2f74f35SThomas Renninger 	}
858909a694eSDave Jones 
859308b60e7SViresh Kumar 	ret = cpufreq_add_dev_symlink(policy);
860ecf7e461SDave Jones 	if (ret)
861ecf7e461SDave Jones 		goto err_out_kobj_put;
862ecf7e461SDave Jones 
863e18f1682SSrivatsa S. Bhat 	return ret;
864e18f1682SSrivatsa S. Bhat 
865e18f1682SSrivatsa S. Bhat err_out_kobj_put:
866e18f1682SSrivatsa S. Bhat 	kobject_put(&policy->kobj);
867e18f1682SSrivatsa S. Bhat 	wait_for_completion(&policy->kobj_unregister);
868e18f1682SSrivatsa S. Bhat 	return ret;
869e18f1682SSrivatsa S. Bhat }
870e18f1682SSrivatsa S. Bhat 
871e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy)
872e18f1682SSrivatsa S. Bhat {
873e18f1682SSrivatsa S. Bhat 	struct cpufreq_policy new_policy;
874e18f1682SSrivatsa S. Bhat 	int ret = 0;
875e18f1682SSrivatsa S. Bhat 
876*d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
877ecf7e461SDave Jones 	/* assure that the starting sequence is run in __cpufreq_set_policy */
878ecf7e461SDave Jones 	policy->governor = NULL;
879ecf7e461SDave Jones 
880ecf7e461SDave Jones 	/* set default policy */
881ecf7e461SDave Jones 	ret = __cpufreq_set_policy(policy, &new_policy);
882ecf7e461SDave Jones 	policy->user_policy.policy = policy->policy;
883ecf7e461SDave Jones 	policy->user_policy.governor = policy->governor;
884ecf7e461SDave Jones 
885ecf7e461SDave Jones 	if (ret) {
8862d06d8c4SDominik Brodowski 		pr_debug("setting policy failed\n");
8871c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
8881c3d85ddSRafael J. Wysocki 			cpufreq_driver->exit(policy);
889ecf7e461SDave Jones 	}
890909a694eSDave Jones }
891909a694eSDave Jones 
892fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
893d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
894d8d3b471SViresh Kumar 				  unsigned int cpu, struct device *dev,
895d8d3b471SViresh Kumar 				  bool frozen)
896fcf80582SViresh Kumar {
8971c3d85ddSRafael J. Wysocki 	int ret = 0, has_target = !!cpufreq_driver->target;
898fcf80582SViresh Kumar 	unsigned long flags;
899fcf80582SViresh Kumar 
900820c6ca2SViresh Kumar 	if (has_target)
901fcf80582SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
902fcf80582SViresh Kumar 
903d8d3b471SViresh Kumar 	lock_policy_rwsem_write(policy->cpu);
9042eaa3e2dSViresh Kumar 
9050d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
9062eaa3e2dSViresh Kumar 
907fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
9082eaa3e2dSViresh Kumar 	per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
909fcf80582SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = policy;
9100d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
911fcf80582SViresh Kumar 
912d8d3b471SViresh Kumar 	unlock_policy_rwsem_write(policy->cpu);
9132eaa3e2dSViresh Kumar 
914820c6ca2SViresh Kumar 	if (has_target) {
915fcf80582SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_START);
916fcf80582SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
917820c6ca2SViresh Kumar 	}
918fcf80582SViresh Kumar 
919a82fab29SSrivatsa S. Bhat 	/* Don't touch sysfs links during light-weight init */
92071c3461eSRafael J. Wysocki 	if (!frozen)
921a82fab29SSrivatsa S. Bhat 		ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
922a82fab29SSrivatsa S. Bhat 
923a82fab29SSrivatsa S. Bhat 	return ret;
924fcf80582SViresh Kumar }
925fcf80582SViresh Kumar #endif
9261da177e4SLinus Torvalds 
9278414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
9288414809cSSrivatsa S. Bhat {
9298414809cSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
9308414809cSSrivatsa S. Bhat 	unsigned long flags;
9318414809cSSrivatsa S. Bhat 
9328414809cSSrivatsa S. Bhat 	write_lock_irqsave(&cpufreq_driver_lock, flags);
9338414809cSSrivatsa S. Bhat 
9348414809cSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
9358414809cSSrivatsa S. Bhat 
9368414809cSSrivatsa S. Bhat 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
9378414809cSSrivatsa S. Bhat 
9388414809cSSrivatsa S. Bhat 	return policy;
9398414809cSSrivatsa S. Bhat }
9408414809cSSrivatsa S. Bhat 
941e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void)
942e9698cc5SSrivatsa S. Bhat {
943e9698cc5SSrivatsa S. Bhat 	struct cpufreq_policy *policy;
944e9698cc5SSrivatsa S. Bhat 
945e9698cc5SSrivatsa S. Bhat 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
946e9698cc5SSrivatsa S. Bhat 	if (!policy)
947e9698cc5SSrivatsa S. Bhat 		return NULL;
948e9698cc5SSrivatsa S. Bhat 
949e9698cc5SSrivatsa S. Bhat 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
950e9698cc5SSrivatsa S. Bhat 		goto err_free_policy;
951e9698cc5SSrivatsa S. Bhat 
952e9698cc5SSrivatsa S. Bhat 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
953e9698cc5SSrivatsa S. Bhat 		goto err_free_cpumask;
954e9698cc5SSrivatsa S. Bhat 
955e9698cc5SSrivatsa S. Bhat 	return policy;
956e9698cc5SSrivatsa S. Bhat 
957e9698cc5SSrivatsa S. Bhat err_free_cpumask:
958e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
959e9698cc5SSrivatsa S. Bhat err_free_policy:
960e9698cc5SSrivatsa S. Bhat 	kfree(policy);
961e9698cc5SSrivatsa S. Bhat 
962e9698cc5SSrivatsa S. Bhat 	return NULL;
963e9698cc5SSrivatsa S. Bhat }
964e9698cc5SSrivatsa S. Bhat 
965e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy)
966e9698cc5SSrivatsa S. Bhat {
967e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
968e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
969e9698cc5SSrivatsa S. Bhat 	kfree(policy);
970e9698cc5SSrivatsa S. Bhat }
971e9698cc5SSrivatsa S. Bhat 
972a82fab29SSrivatsa S. Bhat static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
973a82fab29SSrivatsa S. Bhat 			     bool frozen)
9741da177e4SLinus Torvalds {
975fcf80582SViresh Kumar 	unsigned int j, cpu = dev->id;
97665922465SViresh Kumar 	int ret = -ENOMEM;
9771da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
9781da177e4SLinus Torvalds 	unsigned long flags;
97990e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
980fcf80582SViresh Kumar 	struct cpufreq_governor *gov;
98190e41bacSPrarit Bhargava 	int sibling;
98290e41bacSPrarit Bhargava #endif
9831da177e4SLinus Torvalds 
984c32b6b8eSAshok Raj 	if (cpu_is_offline(cpu))
985c32b6b8eSAshok Raj 		return 0;
986c32b6b8eSAshok Raj 
9872d06d8c4SDominik Brodowski 	pr_debug("adding CPU %u\n", cpu);
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds #ifdef CONFIG_SMP
9901da177e4SLinus Torvalds 	/* check whether a different CPU already registered this
9911da177e4SLinus Torvalds 	 * CPU because it is in the same boat. */
9921da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(cpu);
9931da177e4SLinus Torvalds 	if (unlikely(policy)) {
9948ff69732SDave Jones 		cpufreq_cpu_put(policy);
9951da177e4SLinus Torvalds 		return 0;
9961da177e4SLinus Torvalds 	}
997fcf80582SViresh Kumar 
998fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
999fcf80582SViresh Kumar 	/* Check if this cpu was hot-unplugged earlier and has siblings */
10000d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1001fcf80582SViresh Kumar 	for_each_online_cpu(sibling) {
1002fcf80582SViresh Kumar 		struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
10032eaa3e2dSViresh Kumar 		if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
10040d1857a1SNathan Zimmer 			read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1005d8d3b471SViresh Kumar 			return cpufreq_add_policy_cpu(cp, cpu, dev, frozen);
1006fcf80582SViresh Kumar 		}
10072eaa3e2dSViresh Kumar 	}
10080d1857a1SNathan Zimmer 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1009fcf80582SViresh Kumar #endif
10101da177e4SLinus Torvalds #endif
10111da177e4SLinus Torvalds 
10121c3d85ddSRafael J. Wysocki 	if (!try_module_get(cpufreq_driver->owner)) {
10131da177e4SLinus Torvalds 		ret = -EINVAL;
10141da177e4SLinus Torvalds 		goto module_out;
10151da177e4SLinus Torvalds 	}
10161da177e4SLinus Torvalds 
10178414809cSSrivatsa S. Bhat 	if (frozen)
10188414809cSSrivatsa S. Bhat 		/* Restore the saved policy when doing light-weight init */
10198414809cSSrivatsa S. Bhat 		policy = cpufreq_policy_restore(cpu);
10208414809cSSrivatsa S. Bhat 	else
1021e9698cc5SSrivatsa S. Bhat 		policy = cpufreq_policy_alloc();
10228414809cSSrivatsa S. Bhat 
1023059019a3SDave Jones 	if (!policy)
10241da177e4SLinus Torvalds 		goto nomem_out;
1025059019a3SDave Jones 
10261da177e4SLinus Torvalds 	policy->cpu = cpu;
102765922465SViresh Kumar 	policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1028835481d9SRusty Russell 	cpumask_copy(policy->cpus, cpumask_of(cpu));
10291da177e4SLinus Torvalds 
10305a01f2e8SVenkatesh Pallipadi 	/* Initially set CPU itself as the policy_cpu */
1031f1625066STejun Heo 	per_cpu(cpufreq_policy_cpu, cpu) = cpu;
10325a01f2e8SVenkatesh Pallipadi 
10331da177e4SLinus Torvalds 	init_completion(&policy->kobj_unregister);
103465f27f38SDavid Howells 	INIT_WORK(&policy->update, handle_update);
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
10371da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
10381da177e4SLinus Torvalds 	 */
10391c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->init(policy);
10401da177e4SLinus Torvalds 	if (ret) {
10412d06d8c4SDominik Brodowski 		pr_debug("initialization failed\n");
10422eaa3e2dSViresh Kumar 		goto err_set_policy_cpu;
10431da177e4SLinus Torvalds 	}
1044643ae6e8SViresh Kumar 
1045fcf80582SViresh Kumar 	/* related cpus should atleast have policy->cpus */
1046fcf80582SViresh Kumar 	cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1047fcf80582SViresh Kumar 
1048643ae6e8SViresh Kumar 	/*
1049643ae6e8SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
1050643ae6e8SViresh Kumar 	 * managing offline cpus here.
1051643ae6e8SViresh Kumar 	 */
1052643ae6e8SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1053643ae6e8SViresh Kumar 
1054187d9f4eSMike Chan 	policy->user_policy.min = policy->min;
1055187d9f4eSMike Chan 	policy->user_policy.max = policy->max;
10561da177e4SLinus Torvalds 
1057a1531acdSThomas Renninger 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1058a1531acdSThomas Renninger 				     CPUFREQ_START, policy);
1059a1531acdSThomas Renninger 
1060fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
1061fcf80582SViresh Kumar 	gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1062fcf80582SViresh Kumar 	if (gov) {
1063fcf80582SViresh Kumar 		policy->governor = gov;
1064fcf80582SViresh Kumar 		pr_debug("Restoring governor %s for cpu %d\n",
1065fcf80582SViresh Kumar 		       policy->governor->name, cpu);
10664bfa042cSThomas Renninger 	}
1067fcf80582SViresh Kumar #endif
10681da177e4SLinus Torvalds 
1069e18f1682SSrivatsa S. Bhat 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1070e18f1682SSrivatsa S. Bhat 	for_each_cpu(j, policy->cpus) {
1071e18f1682SSrivatsa S. Bhat 		per_cpu(cpufreq_cpu_data, j) = policy;
1072e18f1682SSrivatsa S. Bhat 		per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
1073e18f1682SSrivatsa S. Bhat 	}
1074e18f1682SSrivatsa S. Bhat 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1075e18f1682SSrivatsa S. Bhat 
1076a82fab29SSrivatsa S. Bhat 	if (!frozen) {
1077308b60e7SViresh Kumar 		ret = cpufreq_add_dev_interface(policy, dev);
107819d6f7ecSDave Jones 		if (ret)
10790142f9dcSAhmed S. Darwish 			goto err_out_unregister;
1080a82fab29SSrivatsa S. Bhat 	}
10818ff69732SDave Jones 
1082e18f1682SSrivatsa S. Bhat 	cpufreq_init_policy(policy);
1083e18f1682SSrivatsa S. Bhat 
1084038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
10851c3d85ddSRafael J. Wysocki 	module_put(cpufreq_driver->owner);
10862d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
10871da177e4SLinus Torvalds 
10881da177e4SLinus Torvalds 	return 0;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds err_out_unregister:
10910d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1092e18f1682SSrivatsa S. Bhat 	for_each_cpu(j, policy->cpus) {
10937a6aedfaSMike Travis 		per_cpu(cpufreq_cpu_data, j) = NULL;
1094e18f1682SSrivatsa S. Bhat 		if (j != cpu)
1095e18f1682SSrivatsa S. Bhat 			per_cpu(cpufreq_policy_cpu, j) = -1;
1096e18f1682SSrivatsa S. Bhat 	}
10970d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
10981da177e4SLinus Torvalds 
10992eaa3e2dSViresh Kumar err_set_policy_cpu:
11002eaa3e2dSViresh Kumar 	per_cpu(cpufreq_policy_cpu, cpu) = -1;
1101e9698cc5SSrivatsa S. Bhat 	cpufreq_policy_free(policy);
11021da177e4SLinus Torvalds nomem_out:
11031c3d85ddSRafael J. Wysocki 	module_put(cpufreq_driver->owner);
11041da177e4SLinus Torvalds module_out:
11051da177e4SLinus Torvalds 	return ret;
11061da177e4SLinus Torvalds }
11071da177e4SLinus Torvalds 
1108a82fab29SSrivatsa S. Bhat /**
1109a82fab29SSrivatsa S. Bhat  * cpufreq_add_dev - add a CPU device
1110a82fab29SSrivatsa S. Bhat  *
1111a82fab29SSrivatsa S. Bhat  * Adds the cpufreq interface for a CPU device.
1112a82fab29SSrivatsa S. Bhat  *
1113a82fab29SSrivatsa S. Bhat  * The Oracle says: try running cpufreq registration/unregistration concurrently
1114a82fab29SSrivatsa S. Bhat  * with with cpu hotplugging and all hell will break loose. Tried to clean this
1115a82fab29SSrivatsa S. Bhat  * mess up, but more thorough testing is needed. - Mathieu
1116a82fab29SSrivatsa S. Bhat  */
1117a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1118a82fab29SSrivatsa S. Bhat {
1119a82fab29SSrivatsa S. Bhat 	return __cpufreq_add_dev(dev, sif, false);
1120a82fab29SSrivatsa S. Bhat }
1121a82fab29SSrivatsa S. Bhat 
1122b8eed8afSViresh Kumar static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1123b8eed8afSViresh Kumar {
1124b8eed8afSViresh Kumar 	int j;
1125b8eed8afSViresh Kumar 
1126b8eed8afSViresh Kumar 	policy->last_cpu = policy->cpu;
1127b8eed8afSViresh Kumar 	policy->cpu = cpu;
1128b8eed8afSViresh Kumar 
11293361b7b1SViresh Kumar 	for_each_cpu(j, policy->cpus)
1130b8eed8afSViresh Kumar 		per_cpu(cpufreq_policy_cpu, j) = cpu;
1131b8eed8afSViresh Kumar 
1132b8eed8afSViresh Kumar #ifdef CONFIG_CPU_FREQ_TABLE
1133b8eed8afSViresh Kumar 	cpufreq_frequency_table_update_policy_cpu(policy);
1134b8eed8afSViresh Kumar #endif
1135b8eed8afSViresh Kumar 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1136b8eed8afSViresh Kumar 			CPUFREQ_UPDATE_POLICY_CPU, policy);
1137b8eed8afSViresh Kumar }
11381da177e4SLinus Torvalds 
11393a3e9e06SViresh Kumar static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
1140a82fab29SSrivatsa S. Bhat 					   unsigned int old_cpu, bool frozen)
1141f9ba680dSSrivatsa S. Bhat {
1142f9ba680dSSrivatsa S. Bhat 	struct device *cpu_dev;
1143f9ba680dSSrivatsa S. Bhat 	unsigned long flags;
1144f9ba680dSSrivatsa S. Bhat 	int ret;
1145f9ba680dSSrivatsa S. Bhat 
1146f9ba680dSSrivatsa S. Bhat 	/* first sibling now owns the new sysfs dir */
11473a3e9e06SViresh Kumar 	cpu_dev = get_cpu_device(cpumask_first(policy->cpus));
1148a82fab29SSrivatsa S. Bhat 
1149a82fab29SSrivatsa S. Bhat 	/* Don't touch sysfs files during light-weight tear-down */
1150a82fab29SSrivatsa S. Bhat 	if (frozen)
1151a82fab29SSrivatsa S. Bhat 		return cpu_dev->id;
1152a82fab29SSrivatsa S. Bhat 
1153f9ba680dSSrivatsa S. Bhat 	sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
11543a3e9e06SViresh Kumar 	ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1155f9ba680dSSrivatsa S. Bhat 	if (ret) {
1156f9ba680dSSrivatsa S. Bhat 		pr_err("%s: Failed to move kobj: %d", __func__, ret);
1157f9ba680dSSrivatsa S. Bhat 
1158f9ba680dSSrivatsa S. Bhat 		WARN_ON(lock_policy_rwsem_write(old_cpu));
11593a3e9e06SViresh Kumar 		cpumask_set_cpu(old_cpu, policy->cpus);
1160f9ba680dSSrivatsa S. Bhat 
1161f9ba680dSSrivatsa S. Bhat 		write_lock_irqsave(&cpufreq_driver_lock, flags);
11623a3e9e06SViresh Kumar 		per_cpu(cpufreq_cpu_data, old_cpu) = policy;
1163f9ba680dSSrivatsa S. Bhat 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1164f9ba680dSSrivatsa S. Bhat 
1165f9ba680dSSrivatsa S. Bhat 		unlock_policy_rwsem_write(old_cpu);
1166f9ba680dSSrivatsa S. Bhat 
11673a3e9e06SViresh Kumar 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1168f9ba680dSSrivatsa S. Bhat 					"cpufreq");
1169f9ba680dSSrivatsa S. Bhat 
1170f9ba680dSSrivatsa S. Bhat 		return -EINVAL;
1171f9ba680dSSrivatsa S. Bhat 	}
1172f9ba680dSSrivatsa S. Bhat 
1173f9ba680dSSrivatsa S. Bhat 	return cpu_dev->id;
1174f9ba680dSSrivatsa S. Bhat }
1175f9ba680dSSrivatsa S. Bhat 
11761da177e4SLinus Torvalds /**
11775a01f2e8SVenkatesh Pallipadi  * __cpufreq_remove_dev - remove a CPU device
11781da177e4SLinus Torvalds  *
11791da177e4SLinus Torvalds  * Removes the cpufreq interface for a CPU device.
11805a01f2e8SVenkatesh Pallipadi  * Caller should already have policy_rwsem in write mode for this CPU.
11815a01f2e8SVenkatesh Pallipadi  * This routine frees the rwsem before returning.
11821da177e4SLinus Torvalds  */
1183bb176f7dSViresh Kumar static int __cpufreq_remove_dev(struct device *dev,
1184a82fab29SSrivatsa S. Bhat 				struct subsys_interface *sif, bool frozen)
11851da177e4SLinus Torvalds {
1186f9ba680dSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
1187f9ba680dSSrivatsa S. Bhat 	int new_cpu;
11881da177e4SLinus Torvalds 	unsigned long flags;
11893a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
1190499bca9bSAmerigo Wang 	struct kobject *kobj;
1191499bca9bSAmerigo Wang 	struct completion *cmp;
11921da177e4SLinus Torvalds 
1193b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
11941da177e4SLinus Torvalds 
11950d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
11961da177e4SLinus Torvalds 
11973a3e9e06SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
11987a6aedfaSMike Travis 	per_cpu(cpufreq_cpu_data, cpu) = NULL;
11991da177e4SLinus Torvalds 
12008414809cSSrivatsa S. Bhat 	/* Save the policy somewhere when doing a light-weight tear-down */
12018414809cSSrivatsa S. Bhat 	if (frozen)
12023a3e9e06SViresh Kumar 		per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
12038414809cSSrivatsa S. Bhat 
12040d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
12051da177e4SLinus Torvalds 
12063a3e9e06SViresh Kumar 	if (!policy) {
1207b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
12081da177e4SLinus Torvalds 		return -EINVAL;
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 
12111c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
12123a3e9e06SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
12135a01f2e8SVenkatesh Pallipadi 
12141da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
12151c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->setpolicy)
1216fa69e33fSDirk Brandewie 		strncpy(per_cpu(cpufreq_cpu_governor, cpu),
12173a3e9e06SViresh Kumar 			policy->governor->name, CPUFREQ_NAME_LEN);
12181da177e4SLinus Torvalds #endif
12191da177e4SLinus Torvalds 
12202eaa3e2dSViresh Kumar 	WARN_ON(lock_policy_rwsem_write(cpu));
12213a3e9e06SViresh Kumar 	cpus = cpumask_weight(policy->cpus);
1222e4969ebaSViresh Kumar 
1223e4969ebaSViresh Kumar 	if (cpus > 1)
12243a3e9e06SViresh Kumar 		cpumask_clear_cpu(cpu, policy->cpus);
12252eaa3e2dSViresh Kumar 	unlock_policy_rwsem_write(cpu);
12261da177e4SLinus Torvalds 
12273a3e9e06SViresh Kumar 	if (cpu != policy->cpu && !frozen) {
122873bf0fc2SViresh Kumar 		sysfs_remove_link(&dev->kobj, "cpufreq");
122973bf0fc2SViresh Kumar 	} else if (cpus > 1) {
12302eaa3e2dSViresh Kumar 
12313a3e9e06SViresh Kumar 		new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
1232f9ba680dSSrivatsa S. Bhat 		if (new_cpu >= 0) {
12332eaa3e2dSViresh Kumar 			WARN_ON(lock_policy_rwsem_write(cpu));
12343a3e9e06SViresh Kumar 			update_policy_cpu(policy, new_cpu);
12352eaa3e2dSViresh Kumar 			unlock_policy_rwsem_write(cpu);
1236a82fab29SSrivatsa S. Bhat 
1237a82fab29SSrivatsa S. Bhat 			if (!frozen) {
1238f9ba680dSSrivatsa S. Bhat 				pr_debug("%s: policy Kobject moved to cpu: %d "
1239f9ba680dSSrivatsa S. Bhat 					 "from: %d\n",__func__, new_cpu, cpu);
12401da177e4SLinus Torvalds 			}
12411da177e4SLinus Torvalds 		}
1242a82fab29SSrivatsa S. Bhat 	}
1243b8eed8afSViresh Kumar 
1244b8eed8afSViresh Kumar 	/* If cpu is last user of policy, free policy */
1245b8eed8afSViresh Kumar 	if (cpus == 1) {
12462a998599SRafael J. Wysocki 		if (cpufreq_driver->target)
12473a3e9e06SViresh Kumar 			__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
12482a998599SRafael J. Wysocki 
12498414809cSSrivatsa S. Bhat 		if (!frozen) {
12502eaa3e2dSViresh Kumar 			lock_policy_rwsem_read(cpu);
12513a3e9e06SViresh Kumar 			kobj = &policy->kobj;
12523a3e9e06SViresh Kumar 			cmp = &policy->kobj_unregister;
12532eaa3e2dSViresh Kumar 			unlock_policy_rwsem_read(cpu);
1254499bca9bSAmerigo Wang 			kobject_put(kobj);
12551da177e4SLinus Torvalds 
12568414809cSSrivatsa S. Bhat 			/*
12578414809cSSrivatsa S. Bhat 			 * We need to make sure that the underlying kobj is
12588414809cSSrivatsa S. Bhat 			 * actually not referenced anymore by anybody before we
12598414809cSSrivatsa S. Bhat 			 * proceed with unloading.
12601da177e4SLinus Torvalds 			 */
12612d06d8c4SDominik Brodowski 			pr_debug("waiting for dropping of refcount\n");
1262499bca9bSAmerigo Wang 			wait_for_completion(cmp);
12632d06d8c4SDominik Brodowski 			pr_debug("wait complete\n");
12648414809cSSrivatsa S. Bhat 		}
12651da177e4SLinus Torvalds 
12668414809cSSrivatsa S. Bhat 		/*
12678414809cSSrivatsa S. Bhat 		 * Perform the ->exit() even during light-weight tear-down,
12688414809cSSrivatsa S. Bhat 		 * since this is a core component, and is essential for the
12698414809cSSrivatsa S. Bhat 		 * subsequent light-weight ->init() to succeed.
12708414809cSSrivatsa S. Bhat 		 */
12711c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
12723a3e9e06SViresh Kumar 			cpufreq_driver->exit(policy);
127327ecddc2SJacob Shin 
12748414809cSSrivatsa S. Bhat 		if (!frozen)
12753a3e9e06SViresh Kumar 			cpufreq_policy_free(policy);
12762a998599SRafael J. Wysocki 	} else {
12772a998599SRafael J. Wysocki 		if (cpufreq_driver->target) {
12783a3e9e06SViresh Kumar 			__cpufreq_governor(policy, CPUFREQ_GOV_START);
12793a3e9e06SViresh Kumar 			__cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1280b8eed8afSViresh Kumar 		}
12812a998599SRafael J. Wysocki 	}
12821da177e4SLinus Torvalds 
12832eaa3e2dSViresh Kumar 	per_cpu(cpufreq_policy_cpu, cpu) = -1;
12841da177e4SLinus Torvalds 	return 0;
12851da177e4SLinus Torvalds }
12861da177e4SLinus Torvalds 
12878a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
12885a01f2e8SVenkatesh Pallipadi {
12898a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
12905a01f2e8SVenkatesh Pallipadi 	int retval;
1291ec28297aSVenki Pallipadi 
1292ec28297aSVenki Pallipadi 	if (cpu_is_offline(cpu))
1293ec28297aSVenki Pallipadi 		return 0;
1294ec28297aSVenki Pallipadi 
1295a82fab29SSrivatsa S. Bhat 	retval = __cpufreq_remove_dev(dev, sif, false);
12965a01f2e8SVenkatesh Pallipadi 	return retval;
12975a01f2e8SVenkatesh Pallipadi }
12985a01f2e8SVenkatesh Pallipadi 
129965f27f38SDavid Howells static void handle_update(struct work_struct *work)
13001da177e4SLinus Torvalds {
130165f27f38SDavid Howells 	struct cpufreq_policy *policy =
130265f27f38SDavid Howells 		container_of(work, struct cpufreq_policy, update);
130365f27f38SDavid Howells 	unsigned int cpu = policy->cpu;
13042d06d8c4SDominik Brodowski 	pr_debug("handle_update for cpu %u called\n", cpu);
13051da177e4SLinus Torvalds 	cpufreq_update_policy(cpu);
13061da177e4SLinus Torvalds }
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds /**
1309bb176f7dSViresh Kumar  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1310bb176f7dSViresh Kumar  *	in deep trouble.
13111da177e4SLinus Torvalds  *	@cpu: cpu number
13121da177e4SLinus Torvalds  *	@old_freq: CPU frequency the kernel thinks the CPU runs at
13131da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
13141da177e4SLinus Torvalds  *
131529464f28SDave Jones  *	We adjust to current frequency first, and need to clean up later.
131629464f28SDave Jones  *	So either call to cpufreq_update_policy() or schedule handle_update()).
13171da177e4SLinus Torvalds  */
1318e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1319e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
13201da177e4SLinus Torvalds {
1321b43a7ffbSViresh Kumar 	struct cpufreq_policy *policy;
13221da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1323b43a7ffbSViresh Kumar 	unsigned long flags;
1324b43a7ffbSViresh Kumar 
13252d06d8c4SDominik Brodowski 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
13261da177e4SLinus Torvalds 	       "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
13271da177e4SLinus Torvalds 
13281da177e4SLinus Torvalds 	freqs.old = old_freq;
13291da177e4SLinus Torvalds 	freqs.new = new_freq;
1330b43a7ffbSViresh Kumar 
1331b43a7ffbSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1332b43a7ffbSViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
1333b43a7ffbSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1334b43a7ffbSViresh Kumar 
1335b43a7ffbSViresh Kumar 	cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1336b43a7ffbSViresh Kumar 	cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
13371da177e4SLinus Torvalds }
13381da177e4SLinus Torvalds 
13391da177e4SLinus Torvalds /**
13404ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
134195235ca2SVenkatesh Pallipadi  * @cpu: CPU number
134295235ca2SVenkatesh Pallipadi  *
134395235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
134495235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
134595235ca2SVenkatesh Pallipadi  */
134695235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
134795235ca2SVenkatesh Pallipadi {
13489e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1349e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
135095235ca2SVenkatesh Pallipadi 
13511c3d85ddSRafael J. Wysocki 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
13521c3d85ddSRafael J. Wysocki 		return cpufreq_driver->get(cpu);
13539e21ba8bSDirk Brandewie 
13549e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
135595235ca2SVenkatesh Pallipadi 	if (policy) {
1356e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
135795235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
135895235ca2SVenkatesh Pallipadi 	}
135995235ca2SVenkatesh Pallipadi 
13604d34a67dSDave Jones 	return ret_freq;
136195235ca2SVenkatesh Pallipadi }
136295235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
136395235ca2SVenkatesh Pallipadi 
13643d737108SJesse Barnes /**
13653d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
13663d737108SJesse Barnes  * @cpu: CPU number
13673d737108SJesse Barnes  *
13683d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
13693d737108SJesse Barnes  */
13703d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
13713d737108SJesse Barnes {
13723d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
13733d737108SJesse Barnes 	unsigned int ret_freq = 0;
13743d737108SJesse Barnes 
13753d737108SJesse Barnes 	if (policy) {
13763d737108SJesse Barnes 		ret_freq = policy->max;
13773d737108SJesse Barnes 		cpufreq_cpu_put(policy);
13783d737108SJesse Barnes 	}
13793d737108SJesse Barnes 
13803d737108SJesse Barnes 	return ret_freq;
13813d737108SJesse Barnes }
13823d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
13833d737108SJesse Barnes 
13845a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu)
13851da177e4SLinus Torvalds {
13867a6aedfaSMike Travis 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1387e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
13881da177e4SLinus Torvalds 
13891c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->get)
13904d34a67dSDave Jones 		return ret_freq;
13911da177e4SLinus Torvalds 
13921c3d85ddSRafael J. Wysocki 	ret_freq = cpufreq_driver->get(cpu);
13931da177e4SLinus Torvalds 
1394e08f5f5bSGautham R Shenoy 	if (ret_freq && policy->cur &&
13951c3d85ddSRafael J. Wysocki 		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1396e08f5f5bSGautham R Shenoy 		/* verify no discrepancy between actual and
1397e08f5f5bSGautham R Shenoy 					saved value exists */
1398e08f5f5bSGautham R Shenoy 		if (unlikely(ret_freq != policy->cur)) {
1399e08f5f5bSGautham R Shenoy 			cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
14001da177e4SLinus Torvalds 			schedule_work(&policy->update);
14011da177e4SLinus Torvalds 		}
14021da177e4SLinus Torvalds 	}
14031da177e4SLinus Torvalds 
14044d34a67dSDave Jones 	return ret_freq;
14055a01f2e8SVenkatesh Pallipadi }
14061da177e4SLinus Torvalds 
14075a01f2e8SVenkatesh Pallipadi /**
14085a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
14095a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
14105a01f2e8SVenkatesh Pallipadi  *
14115a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
14125a01f2e8SVenkatesh Pallipadi  */
14135a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
14145a01f2e8SVenkatesh Pallipadi {
14155a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
14165a01f2e8SVenkatesh Pallipadi 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
14175a01f2e8SVenkatesh Pallipadi 
14185a01f2e8SVenkatesh Pallipadi 	if (!policy)
14195a01f2e8SVenkatesh Pallipadi 		goto out;
14205a01f2e8SVenkatesh Pallipadi 
14215a01f2e8SVenkatesh Pallipadi 	if (unlikely(lock_policy_rwsem_read(cpu)))
14225a01f2e8SVenkatesh Pallipadi 		goto out_policy;
14235a01f2e8SVenkatesh Pallipadi 
14245a01f2e8SVenkatesh Pallipadi 	ret_freq = __cpufreq_get(cpu);
14255a01f2e8SVenkatesh Pallipadi 
14265a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_read(cpu);
14275a01f2e8SVenkatesh Pallipadi 
14285a01f2e8SVenkatesh Pallipadi out_policy:
14291da177e4SLinus Torvalds 	cpufreq_cpu_put(policy);
14305a01f2e8SVenkatesh Pallipadi out:
14314d34a67dSDave Jones 	return ret_freq;
14321da177e4SLinus Torvalds }
14331da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
14341da177e4SLinus Torvalds 
14358a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
14368a25a2fdSKay Sievers 	.name		= "cpufreq",
14378a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
14388a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
14398a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1440e00e56dfSRafael J. Wysocki };
1441e00e56dfSRafael J. Wysocki 
14421da177e4SLinus Torvalds /**
1443e00e56dfSRafael J. Wysocki  * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1444e00e56dfSRafael J. Wysocki  *
1445e00e56dfSRafael J. Wysocki  * This function is only executed for the boot processor.  The other CPUs
1446e00e56dfSRafael J. Wysocki  * have been put offline by means of CPU hotplug.
144742d4dc3fSBenjamin Herrenschmidt  */
1448e00e56dfSRafael J. Wysocki static int cpufreq_bp_suspend(void)
144942d4dc3fSBenjamin Herrenschmidt {
1450e08f5f5bSGautham R Shenoy 	int ret = 0;
14514bc5d341SDave Jones 
1452e00e56dfSRafael J. Wysocki 	int cpu = smp_processor_id();
14533a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
145442d4dc3fSBenjamin Herrenschmidt 
14552d06d8c4SDominik Brodowski 	pr_debug("suspending cpu %u\n", cpu);
145642d4dc3fSBenjamin Herrenschmidt 
1457e00e56dfSRafael J. Wysocki 	/* If there's no policy for the boot CPU, we have nothing to do. */
14583a3e9e06SViresh Kumar 	policy = cpufreq_cpu_get(cpu);
14593a3e9e06SViresh Kumar 	if (!policy)
1460e00e56dfSRafael J. Wysocki 		return 0;
146142d4dc3fSBenjamin Herrenschmidt 
14621c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->suspend) {
14633a3e9e06SViresh Kumar 		ret = cpufreq_driver->suspend(policy);
1464ce6c3997SDominik Brodowski 		if (ret)
146542d4dc3fSBenjamin Herrenschmidt 			printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
14663a3e9e06SViresh Kumar 					"step on CPU %u\n", policy->cpu);
146742d4dc3fSBenjamin Herrenschmidt 	}
146842d4dc3fSBenjamin Herrenschmidt 
14693a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
1470c9060494SDave Jones 	return ret;
147142d4dc3fSBenjamin Herrenschmidt }
147242d4dc3fSBenjamin Herrenschmidt 
147342d4dc3fSBenjamin Herrenschmidt /**
1474e00e56dfSRafael J. Wysocki  * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
14751da177e4SLinus Torvalds  *
14761da177e4SLinus Torvalds  *	1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1477ce6c3997SDominik Brodowski  *	2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1478ce6c3997SDominik Brodowski  *	    restored. It will verify that the current freq is in sync with
1479ce6c3997SDominik Brodowski  *	    what we believe it to be. This is a bit later than when it
1480ce6c3997SDominik Brodowski  *	    should be, but nonethteless it's better than calling
1481ce6c3997SDominik Brodowski  *	    cpufreq_driver->get() here which might re-enable interrupts...
1482e00e56dfSRafael J. Wysocki  *
1483e00e56dfSRafael J. Wysocki  * This function is only executed for the boot CPU.  The other CPUs have not
1484e00e56dfSRafael J. Wysocki  * been turned on yet.
14851da177e4SLinus Torvalds  */
1486e00e56dfSRafael J. Wysocki static void cpufreq_bp_resume(void)
14871da177e4SLinus Torvalds {
1488e08f5f5bSGautham R Shenoy 	int ret = 0;
14894bc5d341SDave Jones 
1490e00e56dfSRafael J. Wysocki 	int cpu = smp_processor_id();
14913a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
14921da177e4SLinus Torvalds 
14932d06d8c4SDominik Brodowski 	pr_debug("resuming cpu %u\n", cpu);
14941da177e4SLinus Torvalds 
1495e00e56dfSRafael J. Wysocki 	/* If there's no policy for the boot CPU, we have nothing to do. */
14963a3e9e06SViresh Kumar 	policy = cpufreq_cpu_get(cpu);
14973a3e9e06SViresh Kumar 	if (!policy)
1498e00e56dfSRafael J. Wysocki 		return;
14991da177e4SLinus Torvalds 
15001c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->resume) {
15013a3e9e06SViresh Kumar 		ret = cpufreq_driver->resume(policy);
15021da177e4SLinus Torvalds 		if (ret) {
15031da177e4SLinus Torvalds 			printk(KERN_ERR "cpufreq: resume failed in ->resume "
15043a3e9e06SViresh Kumar 					"step on CPU %u\n", policy->cpu);
1505c9060494SDave Jones 			goto fail;
15061da177e4SLinus Torvalds 		}
15071da177e4SLinus Torvalds 	}
15081da177e4SLinus Torvalds 
15093a3e9e06SViresh Kumar 	schedule_work(&policy->update);
1510ce6c3997SDominik Brodowski 
1511c9060494SDave Jones fail:
15123a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
15131da177e4SLinus Torvalds }
15141da177e4SLinus Torvalds 
1515e00e56dfSRafael J. Wysocki static struct syscore_ops cpufreq_syscore_ops = {
1516e00e56dfSRafael J. Wysocki 	.suspend	= cpufreq_bp_suspend,
1517e00e56dfSRafael J. Wysocki 	.resume		= cpufreq_bp_resume,
15181da177e4SLinus Torvalds };
15191da177e4SLinus Torvalds 
15209d95046eSBorislav Petkov /**
15219d95046eSBorislav Petkov  *	cpufreq_get_current_driver - return current driver's name
15229d95046eSBorislav Petkov  *
15239d95046eSBorislav Petkov  *	Return the name string of the currently loaded cpufreq driver
15249d95046eSBorislav Petkov  *	or NULL, if none.
15259d95046eSBorislav Petkov  */
15269d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
15279d95046eSBorislav Petkov {
15281c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
15291c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
15301c3d85ddSRafael J. Wysocki 
15311c3d85ddSRafael J. Wysocki 	return NULL;
15329d95046eSBorislav Petkov }
15339d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds /*********************************************************************
15361da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
15371da177e4SLinus Torvalds  *********************************************************************/
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds /**
15401da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
15411da177e4SLinus Torvalds  *	@nb: notifier function to register
15421da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
15431da177e4SLinus Torvalds  *
15441da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
15451da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
15461da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
15471da177e4SLinus Torvalds  *      changes in cpufreq policy.
15481da177e4SLinus Torvalds  *
15491da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1550e041c683SAlan Stern  *	blocking_notifier_chain_register.
15511da177e4SLinus Torvalds  */
15521da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
15531da177e4SLinus Torvalds {
15541da177e4SLinus Torvalds 	int ret;
15551da177e4SLinus Torvalds 
1556d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1557d5aaffa9SDirk Brandewie 		return -EINVAL;
1558d5aaffa9SDirk Brandewie 
155974212ca4SCesar Eduardo Barros 	WARN_ON(!init_cpufreq_transition_notifier_list_called);
156074212ca4SCesar Eduardo Barros 
15611da177e4SLinus Torvalds 	switch (list) {
15621da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1563b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1564e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
15651da177e4SLinus Torvalds 		break;
15661da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1567e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1568e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
15691da177e4SLinus Torvalds 		break;
15701da177e4SLinus Torvalds 	default:
15711da177e4SLinus Torvalds 		ret = -EINVAL;
15721da177e4SLinus Torvalds 	}
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	return ret;
15751da177e4SLinus Torvalds }
15761da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
15771da177e4SLinus Torvalds 
15781da177e4SLinus Torvalds /**
15791da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
15801da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
15811da177e4SLinus Torvalds  *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
15821da177e4SLinus Torvalds  *
15831da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
15841da177e4SLinus Torvalds  *
15851da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1586e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
15871da177e4SLinus Torvalds  */
15881da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
15891da177e4SLinus Torvalds {
15901da177e4SLinus Torvalds 	int ret;
15911da177e4SLinus Torvalds 
1592d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1593d5aaffa9SDirk Brandewie 		return -EINVAL;
1594d5aaffa9SDirk Brandewie 
15951da177e4SLinus Torvalds 	switch (list) {
15961da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1597b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
1598e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
15991da177e4SLinus Torvalds 		break;
16001da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1601e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1602e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
16031da177e4SLinus Torvalds 		break;
16041da177e4SLinus Torvalds 	default:
16051da177e4SLinus Torvalds 		ret = -EINVAL;
16061da177e4SLinus Torvalds 	}
16071da177e4SLinus Torvalds 
16081da177e4SLinus Torvalds 	return ret;
16091da177e4SLinus Torvalds }
16101da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds /*********************************************************************
16141da177e4SLinus Torvalds  *                              GOVERNORS                            *
16151da177e4SLinus Torvalds  *********************************************************************/
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
16181da177e4SLinus Torvalds 			    unsigned int target_freq,
16191da177e4SLinus Torvalds 			    unsigned int relation)
16201da177e4SLinus Torvalds {
16211da177e4SLinus Torvalds 	int retval = -EINVAL;
16227249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
1623c32b6b8eSAshok Raj 
1624a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1625a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
16267c30ed53SViresh Kumar 	if (policy->transition_ongoing)
16277c30ed53SViresh Kumar 		return -EBUSY;
1628a7b422cdSKonrad Rzeszutek Wilk 
16297249924eSViresh Kumar 	/* Make sure that target_freq is within supported range */
16307249924eSViresh Kumar 	if (target_freq > policy->max)
16317249924eSViresh Kumar 		target_freq = policy->max;
16327249924eSViresh Kumar 	if (target_freq < policy->min)
16337249924eSViresh Kumar 		target_freq = policy->min;
16347249924eSViresh Kumar 
16357249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
16367249924eSViresh Kumar 			policy->cpu, target_freq, relation, old_target_freq);
16375a1c0228SViresh Kumar 
16385a1c0228SViresh Kumar 	if (target_freq == policy->cur)
16395a1c0228SViresh Kumar 		return 0;
16405a1c0228SViresh Kumar 
16411c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
16421c3d85ddSRafael J. Wysocki 		retval = cpufreq_driver->target(policy, target_freq, relation);
164390d45d17SAshok Raj 
16441da177e4SLinus Torvalds 	return retval;
16451da177e4SLinus Torvalds }
16461da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
16471da177e4SLinus Torvalds 
16481da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
16491da177e4SLinus Torvalds 			  unsigned int target_freq,
16501da177e4SLinus Torvalds 			  unsigned int relation)
16511da177e4SLinus Torvalds {
1652f1829e4aSJulia Lawall 	int ret = -EINVAL;
16531da177e4SLinus Torvalds 
16545a01f2e8SVenkatesh Pallipadi 	if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1655f1829e4aSJulia Lawall 		goto fail;
16561da177e4SLinus Torvalds 
16571da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
16581da177e4SLinus Torvalds 
16595a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(policy->cpu);
16601da177e4SLinus Torvalds 
1661f1829e4aSJulia Lawall fail:
16621da177e4SLinus Torvalds 	return ret;
16631da177e4SLinus Torvalds }
16641da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
16651da177e4SLinus Torvalds 
1666153d7f3fSArjan van de Ven /*
1667153d7f3fSArjan van de Ven  * when "event" is CPUFREQ_GOV_LIMITS
1668153d7f3fSArjan van de Ven  */
16691da177e4SLinus Torvalds 
1670e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy,
1671e08f5f5bSGautham R Shenoy 					unsigned int event)
16721da177e4SLinus Torvalds {
1673cc993cabSDave Jones 	int ret;
16746afde10cSThomas Renninger 
16756afde10cSThomas Renninger 	/* Only must be defined when default governor is known to have latency
16766afde10cSThomas Renninger 	   restrictions, like e.g. conservative or ondemand.
16776afde10cSThomas Renninger 	   That this is the case is already ensured in Kconfig
16786afde10cSThomas Renninger 	*/
16796afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
16806afde10cSThomas Renninger 	struct cpufreq_governor *gov = &cpufreq_gov_performance;
16816afde10cSThomas Renninger #else
16826afde10cSThomas Renninger 	struct cpufreq_governor *gov = NULL;
16836afde10cSThomas Renninger #endif
16841c256245SThomas Renninger 
16851c256245SThomas Renninger 	if (policy->governor->max_transition_latency &&
16861c256245SThomas Renninger 	    policy->cpuinfo.transition_latency >
16871c256245SThomas Renninger 	    policy->governor->max_transition_latency) {
16886afde10cSThomas Renninger 		if (!gov)
16896afde10cSThomas Renninger 			return -EINVAL;
16906afde10cSThomas Renninger 		else {
16911c256245SThomas Renninger 			printk(KERN_WARNING "%s governor failed, too long"
16921c256245SThomas Renninger 			       " transition latency of HW, fallback"
16931c256245SThomas Renninger 			       " to %s governor\n",
16941c256245SThomas Renninger 			       policy->governor->name,
16951c256245SThomas Renninger 			       gov->name);
16961c256245SThomas Renninger 			policy->governor = gov;
16971c256245SThomas Renninger 		}
16986afde10cSThomas Renninger 	}
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds 	if (!try_module_get(policy->governor->owner))
17011da177e4SLinus Torvalds 		return -EINVAL;
17021da177e4SLinus Torvalds 
17032d06d8c4SDominik Brodowski 	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1704e08f5f5bSGautham R Shenoy 						policy->cpu, event);
170595731ebbSXiaoguang Chen 
170695731ebbSXiaoguang Chen 	mutex_lock(&cpufreq_governor_lock);
170795731ebbSXiaoguang Chen 	if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
170895731ebbSXiaoguang Chen 	    (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
170995731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
171095731ebbSXiaoguang Chen 		return -EBUSY;
171195731ebbSXiaoguang Chen 	}
171295731ebbSXiaoguang Chen 
171395731ebbSXiaoguang Chen 	if (event == CPUFREQ_GOV_STOP)
171495731ebbSXiaoguang Chen 		policy->governor_enabled = false;
171595731ebbSXiaoguang Chen 	else if (event == CPUFREQ_GOV_START)
171695731ebbSXiaoguang Chen 		policy->governor_enabled = true;
171795731ebbSXiaoguang Chen 
171895731ebbSXiaoguang Chen 	mutex_unlock(&cpufreq_governor_lock);
171995731ebbSXiaoguang Chen 
17201da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
17211da177e4SLinus Torvalds 
17224d5dcc42SViresh Kumar 	if (!ret) {
17234d5dcc42SViresh Kumar 		if (event == CPUFREQ_GOV_POLICY_INIT)
17248e53695fSViresh Kumar 			policy->governor->initialized++;
17254d5dcc42SViresh Kumar 		else if (event == CPUFREQ_GOV_POLICY_EXIT)
17268e53695fSViresh Kumar 			policy->governor->initialized--;
172795731ebbSXiaoguang Chen 	} else {
172895731ebbSXiaoguang Chen 		/* Restore original values */
172995731ebbSXiaoguang Chen 		mutex_lock(&cpufreq_governor_lock);
173095731ebbSXiaoguang Chen 		if (event == CPUFREQ_GOV_STOP)
173195731ebbSXiaoguang Chen 			policy->governor_enabled = true;
173295731ebbSXiaoguang Chen 		else if (event == CPUFREQ_GOV_START)
173395731ebbSXiaoguang Chen 			policy->governor_enabled = false;
173495731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
17354d5dcc42SViresh Kumar 	}
1736b394058fSViresh Kumar 
1737e08f5f5bSGautham R Shenoy 	/* we keep one module reference alive for
1738e08f5f5bSGautham R Shenoy 			each CPU governed by this CPU */
17391da177e4SLinus Torvalds 	if ((event != CPUFREQ_GOV_START) || ret)
17401da177e4SLinus Torvalds 		module_put(policy->governor->owner);
17411da177e4SLinus Torvalds 	if ((event == CPUFREQ_GOV_STOP) && !ret)
17421da177e4SLinus Torvalds 		module_put(policy->governor->owner);
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds 	return ret;
17451da177e4SLinus Torvalds }
17461da177e4SLinus Torvalds 
17471da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
17481da177e4SLinus Torvalds {
17493bcb09a3SJeremy Fitzhardinge 	int err;
17501da177e4SLinus Torvalds 
17511da177e4SLinus Torvalds 	if (!governor)
17521da177e4SLinus Torvalds 		return -EINVAL;
17531da177e4SLinus Torvalds 
1754a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1755a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1756a7b422cdSKonrad Rzeszutek Wilk 
17573fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
17581da177e4SLinus Torvalds 
1759b394058fSViresh Kumar 	governor->initialized = 0;
17603bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
17613bcb09a3SJeremy Fitzhardinge 	if (__find_governor(governor->name) == NULL) {
17623bcb09a3SJeremy Fitzhardinge 		err = 0;
17631da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
17643bcb09a3SJeremy Fitzhardinge 	}
17651da177e4SLinus Torvalds 
17663fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
17673bcb09a3SJeremy Fitzhardinge 	return err;
17681da177e4SLinus Torvalds }
17691da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
17701da177e4SLinus Torvalds 
17711da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
17721da177e4SLinus Torvalds {
177390e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
177490e41bacSPrarit Bhargava 	int cpu;
177590e41bacSPrarit Bhargava #endif
177690e41bacSPrarit Bhargava 
17771da177e4SLinus Torvalds 	if (!governor)
17781da177e4SLinus Torvalds 		return;
17791da177e4SLinus Torvalds 
1780a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1781a7b422cdSKonrad Rzeszutek Wilk 		return;
1782a7b422cdSKonrad Rzeszutek Wilk 
178390e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
178490e41bacSPrarit Bhargava 	for_each_present_cpu(cpu) {
178590e41bacSPrarit Bhargava 		if (cpu_online(cpu))
178690e41bacSPrarit Bhargava 			continue;
178790e41bacSPrarit Bhargava 		if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
178890e41bacSPrarit Bhargava 			strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
178990e41bacSPrarit Bhargava 	}
179090e41bacSPrarit Bhargava #endif
179190e41bacSPrarit Bhargava 
17923fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
17931da177e4SLinus Torvalds 	list_del(&governor->governor_list);
17943fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
17951da177e4SLinus Torvalds 	return;
17961da177e4SLinus Torvalds }
17971da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 
18001da177e4SLinus Torvalds /*********************************************************************
18011da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
18021da177e4SLinus Torvalds  *********************************************************************/
18031da177e4SLinus Torvalds 
18041da177e4SLinus Torvalds /**
18051da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
180629464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
180729464f28SDave Jones  *	is written
18081da177e4SLinus Torvalds  *
18091da177e4SLinus Torvalds  * Reads the current cpufreq policy.
18101da177e4SLinus Torvalds  */
18111da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
18121da177e4SLinus Torvalds {
18131da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
18141da177e4SLinus Torvalds 	if (!policy)
18151da177e4SLinus Torvalds 		return -EINVAL;
18161da177e4SLinus Torvalds 
18171da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
18181da177e4SLinus Torvalds 	if (!cpu_policy)
18191da177e4SLinus Torvalds 		return -EINVAL;
18201da177e4SLinus Torvalds 
1821*d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
18221da177e4SLinus Torvalds 
18231da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
18241da177e4SLinus Torvalds 	return 0;
18251da177e4SLinus Torvalds }
18261da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
18271da177e4SLinus Torvalds 
1828153d7f3fSArjan van de Ven /*
1829e08f5f5bSGautham R Shenoy  * data   : current policy.
1830e08f5f5bSGautham R Shenoy  * policy : policy to be set.
1831153d7f3fSArjan van de Ven  */
18323a3e9e06SViresh Kumar static int __cpufreq_set_policy(struct cpufreq_policy *policy,
18333a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy)
18341da177e4SLinus Torvalds {
18357bd353a9SViresh Kumar 	int ret = 0, failed = 1;
18361da177e4SLinus Torvalds 
18373a3e9e06SViresh Kumar 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
18383a3e9e06SViresh Kumar 		new_policy->min, new_policy->max);
18391da177e4SLinus Torvalds 
1840*d5b73cd8SViresh Kumar 	memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
18411da177e4SLinus Torvalds 
18423a3e9e06SViresh Kumar 	if (new_policy->min > policy->max || new_policy->max < policy->min) {
18439c9a43edSMattia Dongili 		ret = -EINVAL;
18449c9a43edSMattia Dongili 		goto error_out;
18459c9a43edSMattia Dongili 	}
18469c9a43edSMattia Dongili 
18471da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
18483a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
18491da177e4SLinus Torvalds 	if (ret)
18501da177e4SLinus Torvalds 		goto error_out;
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
1853e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
18543a3e9e06SViresh Kumar 			CPUFREQ_ADJUST, new_policy);
18551da177e4SLinus Torvalds 
18561da177e4SLinus Torvalds 	/* adjust if necessary - hardware incompatibility*/
1857e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
18583a3e9e06SViresh Kumar 			CPUFREQ_INCOMPATIBLE, new_policy);
18591da177e4SLinus Torvalds 
1860bb176f7dSViresh Kumar 	/*
1861bb176f7dSViresh Kumar 	 * verify the cpu speed can be set within this limit, which might be
1862bb176f7dSViresh Kumar 	 * different to the first one
1863bb176f7dSViresh Kumar 	 */
18643a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
1865e041c683SAlan Stern 	if (ret)
18661da177e4SLinus Torvalds 		goto error_out;
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	/* notification of the new policy */
1869e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
18703a3e9e06SViresh Kumar 			CPUFREQ_NOTIFY, new_policy);
18711da177e4SLinus Torvalds 
18723a3e9e06SViresh Kumar 	policy->min = new_policy->min;
18733a3e9e06SViresh Kumar 	policy->max = new_policy->max;
18741da177e4SLinus Torvalds 
18752d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
18763a3e9e06SViresh Kumar 					policy->min, policy->max);
18771da177e4SLinus Torvalds 
18781c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
18793a3e9e06SViresh Kumar 		policy->policy = new_policy->policy;
18802d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
18813a3e9e06SViresh Kumar 		ret = cpufreq_driver->setpolicy(new_policy);
18821da177e4SLinus Torvalds 	} else {
18833a3e9e06SViresh Kumar 		if (new_policy->governor != policy->governor) {
18841da177e4SLinus Torvalds 			/* save old, working values */
18853a3e9e06SViresh Kumar 			struct cpufreq_governor *old_gov = policy->governor;
18861da177e4SLinus Torvalds 
18872d06d8c4SDominik Brodowski 			pr_debug("governor switch\n");
18881da177e4SLinus Torvalds 
18891da177e4SLinus Torvalds 			/* end old governor */
18903a3e9e06SViresh Kumar 			if (policy->governor) {
18913a3e9e06SViresh Kumar 				__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
18923a3e9e06SViresh Kumar 				unlock_policy_rwsem_write(new_policy->cpu);
18933a3e9e06SViresh Kumar 				__cpufreq_governor(policy,
18947bd353a9SViresh Kumar 						CPUFREQ_GOV_POLICY_EXIT);
18953a3e9e06SViresh Kumar 				lock_policy_rwsem_write(new_policy->cpu);
18967bd353a9SViresh Kumar 			}
18971da177e4SLinus Torvalds 
18981da177e4SLinus Torvalds 			/* start new governor */
18993a3e9e06SViresh Kumar 			policy->governor = new_policy->governor;
19003a3e9e06SViresh Kumar 			if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
19013a3e9e06SViresh Kumar 				if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
19027bd353a9SViresh Kumar 					failed = 0;
1903955ef483SViresh Kumar 				} else {
19043a3e9e06SViresh Kumar 					unlock_policy_rwsem_write(new_policy->cpu);
19053a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
19067bd353a9SViresh Kumar 							CPUFREQ_GOV_POLICY_EXIT);
19073a3e9e06SViresh Kumar 					lock_policy_rwsem_write(new_policy->cpu);
1908955ef483SViresh Kumar 				}
19097bd353a9SViresh Kumar 			}
19107bd353a9SViresh Kumar 
19117bd353a9SViresh Kumar 			if (failed) {
19121da177e4SLinus Torvalds 				/* new governor failed, so re-start old one */
19132d06d8c4SDominik Brodowski 				pr_debug("starting governor %s failed\n",
19143a3e9e06SViresh Kumar 							policy->governor->name);
19151da177e4SLinus Torvalds 				if (old_gov) {
19163a3e9e06SViresh Kumar 					policy->governor = old_gov;
19173a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
19187bd353a9SViresh Kumar 							CPUFREQ_GOV_POLICY_INIT);
19193a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
1920e08f5f5bSGautham R Shenoy 							   CPUFREQ_GOV_START);
19211da177e4SLinus Torvalds 				}
19221da177e4SLinus Torvalds 				ret = -EINVAL;
19231da177e4SLinus Torvalds 				goto error_out;
19241da177e4SLinus Torvalds 			}
19251da177e4SLinus Torvalds 			/* might be a policy change, too, so fall through */
19261da177e4SLinus Torvalds 		}
19272d06d8c4SDominik Brodowski 		pr_debug("governor: change or update limits\n");
19283a3e9e06SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
19291da177e4SLinus Torvalds 	}
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds error_out:
19321da177e4SLinus Torvalds 	return ret;
19331da177e4SLinus Torvalds }
19341da177e4SLinus Torvalds 
19351da177e4SLinus Torvalds /**
19361da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
19371da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
19381da177e4SLinus Torvalds  *
193925985edcSLucas De Marchi  *	Useful for policy notifiers which have different necessities
19401da177e4SLinus Torvalds  *	at different times.
19411da177e4SLinus Torvalds  */
19421da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
19431da177e4SLinus Torvalds {
19443a3e9e06SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
19453a3e9e06SViresh Kumar 	struct cpufreq_policy new_policy;
1946f1829e4aSJulia Lawall 	int ret;
19471da177e4SLinus Torvalds 
19483a3e9e06SViresh Kumar 	if (!policy) {
1949f1829e4aSJulia Lawall 		ret = -ENODEV;
1950f1829e4aSJulia Lawall 		goto no_policy;
1951f1829e4aSJulia Lawall 	}
19521da177e4SLinus Torvalds 
1953f1829e4aSJulia Lawall 	if (unlikely(lock_policy_rwsem_write(cpu))) {
1954f1829e4aSJulia Lawall 		ret = -EINVAL;
1955f1829e4aSJulia Lawall 		goto fail;
1956f1829e4aSJulia Lawall 	}
19571da177e4SLinus Torvalds 
19582d06d8c4SDominik Brodowski 	pr_debug("updating policy for CPU %u\n", cpu);
1959*d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
19603a3e9e06SViresh Kumar 	new_policy.min = policy->user_policy.min;
19613a3e9e06SViresh Kumar 	new_policy.max = policy->user_policy.max;
19623a3e9e06SViresh Kumar 	new_policy.policy = policy->user_policy.policy;
19633a3e9e06SViresh Kumar 	new_policy.governor = policy->user_policy.governor;
19641da177e4SLinus Torvalds 
1965bb176f7dSViresh Kumar 	/*
1966bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
1967bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
1968bb176f7dSViresh Kumar 	 */
19691c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
19703a3e9e06SViresh Kumar 		new_policy.cur = cpufreq_driver->get(cpu);
19713a3e9e06SViresh Kumar 		if (!policy->cur) {
19722d06d8c4SDominik Brodowski 			pr_debug("Driver did not initialize current freq");
19733a3e9e06SViresh Kumar 			policy->cur = new_policy.cur;
1974a85f7bd3SThomas Renninger 		} else {
19753a3e9e06SViresh Kumar 			if (policy->cur != new_policy.cur && cpufreq_driver->target)
19763a3e9e06SViresh Kumar 				cpufreq_out_of_sync(cpu, policy->cur,
19773a3e9e06SViresh Kumar 								new_policy.cur);
19780961dd0dSThomas Renninger 		}
1979a85f7bd3SThomas Renninger 	}
19800961dd0dSThomas Renninger 
19813a3e9e06SViresh Kumar 	ret = __cpufreq_set_policy(policy, &new_policy);
19821da177e4SLinus Torvalds 
19835a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(cpu);
19845a01f2e8SVenkatesh Pallipadi 
1985f1829e4aSJulia Lawall fail:
19863a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
1987f1829e4aSJulia Lawall no_policy:
19881da177e4SLinus Torvalds 	return ret;
19891da177e4SLinus Torvalds }
19901da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
19911da177e4SLinus Torvalds 
19922760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb,
1993c32b6b8eSAshok Raj 					unsigned long action, void *hcpu)
1994c32b6b8eSAshok Raj {
1995c32b6b8eSAshok Raj 	unsigned int cpu = (unsigned long)hcpu;
19968a25a2fdSKay Sievers 	struct device *dev;
19975302c3fbSSrivatsa S. Bhat 	bool frozen = false;
1998c32b6b8eSAshok Raj 
19998a25a2fdSKay Sievers 	dev = get_cpu_device(cpu);
20008a25a2fdSKay Sievers 	if (dev) {
20015302c3fbSSrivatsa S. Bhat 
20025302c3fbSSrivatsa S. Bhat 		if (action & CPU_TASKS_FROZEN)
20035302c3fbSSrivatsa S. Bhat 			frozen = true;
20045302c3fbSSrivatsa S. Bhat 
20055302c3fbSSrivatsa S. Bhat 		switch (action & ~CPU_TASKS_FROZEN) {
2006c32b6b8eSAshok Raj 		case CPU_ONLINE:
20075302c3fbSSrivatsa S. Bhat 			__cpufreq_add_dev(dev, NULL, frozen);
200823d32899SSrivatsa S. Bhat 			cpufreq_update_policy(cpu);
2009c32b6b8eSAshok Raj 			break;
20105302c3fbSSrivatsa S. Bhat 
2011c32b6b8eSAshok Raj 		case CPU_DOWN_PREPARE:
20125302c3fbSSrivatsa S. Bhat 			__cpufreq_remove_dev(dev, NULL, frozen);
2013c32b6b8eSAshok Raj 			break;
20145302c3fbSSrivatsa S. Bhat 
20155a01f2e8SVenkatesh Pallipadi 		case CPU_DOWN_FAILED:
20165302c3fbSSrivatsa S. Bhat 			__cpufreq_add_dev(dev, NULL, frozen);
2017c32b6b8eSAshok Raj 			break;
2018c32b6b8eSAshok Raj 		}
2019c32b6b8eSAshok Raj 	}
2020c32b6b8eSAshok Raj 	return NOTIFY_OK;
2021c32b6b8eSAshok Raj }
2022c32b6b8eSAshok Raj 
20239c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = {
2024c32b6b8eSAshok Raj 	.notifier_call = cpufreq_cpu_callback,
2025c32b6b8eSAshok Raj };
20261da177e4SLinus Torvalds 
20271da177e4SLinus Torvalds /*********************************************************************
20281da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
20291da177e4SLinus Torvalds  *********************************************************************/
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds /**
20321da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
20331da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
20341da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
20351da177e4SLinus Torvalds  *
20361da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
20371da177e4SLinus Torvalds  * returns zero on success, -EBUSY when another driver got here first
20381da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
20391da177e4SLinus Torvalds  *
20401da177e4SLinus Torvalds  */
2041221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
20421da177e4SLinus Torvalds {
20431da177e4SLinus Torvalds 	unsigned long flags;
20441da177e4SLinus Torvalds 	int ret;
20451da177e4SLinus Torvalds 
2046a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2047a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2048a7b422cdSKonrad Rzeszutek Wilk 
20491da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
20501da177e4SLinus Torvalds 	    ((!driver_data->setpolicy) && (!driver_data->target)))
20511da177e4SLinus Torvalds 		return -EINVAL;
20521da177e4SLinus Torvalds 
20532d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
20541da177e4SLinus Torvalds 
20551da177e4SLinus Torvalds 	if (driver_data->setpolicy)
20561da177e4SLinus Torvalds 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
20571da177e4SLinus Torvalds 
20580d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
20591c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
20600d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
20611da177e4SLinus Torvalds 		return -EBUSY;
20621da177e4SLinus Torvalds 	}
20631c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
20640d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
20651da177e4SLinus Torvalds 
20668a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
20678f5bc2abSJiri Slaby 	if (ret)
20688f5bc2abSJiri Slaby 		goto err_null_driver;
20691da177e4SLinus Torvalds 
20701c3d85ddSRafael J. Wysocki 	if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
20711da177e4SLinus Torvalds 		int i;
20721da177e4SLinus Torvalds 		ret = -ENODEV;
20731da177e4SLinus Torvalds 
20741da177e4SLinus Torvalds 		/* check for at least one working CPU */
20757a6aedfaSMike Travis 		for (i = 0; i < nr_cpu_ids; i++)
20767a6aedfaSMike Travis 			if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
20771da177e4SLinus Torvalds 				ret = 0;
20787a6aedfaSMike Travis 				break;
20797a6aedfaSMike Travis 			}
20801da177e4SLinus Torvalds 
20811da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
20821da177e4SLinus Torvalds 		if (ret) {
20832d06d8c4SDominik Brodowski 			pr_debug("no CPU initialized for driver %s\n",
2084e08f5f5bSGautham R Shenoy 							driver_data->name);
20858a25a2fdSKay Sievers 			goto err_if_unreg;
20861da177e4SLinus Torvalds 		}
20871da177e4SLinus Torvalds 	}
20881da177e4SLinus Torvalds 
208965edc68cSChandra Seetharaman 	register_hotcpu_notifier(&cpufreq_cpu_notifier);
20902d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
20911da177e4SLinus Torvalds 
20928f5bc2abSJiri Slaby 	return 0;
20938a25a2fdSKay Sievers err_if_unreg:
20948a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
20958f5bc2abSJiri Slaby err_null_driver:
20960d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
20971c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
20980d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
20994d34a67dSDave Jones 	return ret;
21001da177e4SLinus Torvalds }
21011da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
21021da177e4SLinus Torvalds 
21031da177e4SLinus Torvalds /**
21041da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
21051da177e4SLinus Torvalds  *
21061da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
21071da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
21081da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
21091da177e4SLinus Torvalds  * currently not initialised.
21101da177e4SLinus Torvalds  */
2111221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
21121da177e4SLinus Torvalds {
21131da177e4SLinus Torvalds 	unsigned long flags;
21141da177e4SLinus Torvalds 
21151c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
21161da177e4SLinus Torvalds 		return -EINVAL;
21171da177e4SLinus Torvalds 
21182d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
21191da177e4SLinus Torvalds 
21208a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
212165edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
21221da177e4SLinus Torvalds 
21230d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
21241c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
21250d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
21261da177e4SLinus Torvalds 
21271da177e4SLinus Torvalds 	return 0;
21281da177e4SLinus Torvalds }
21291da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
21305a01f2e8SVenkatesh Pallipadi 
21315a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
21325a01f2e8SVenkatesh Pallipadi {
21335a01f2e8SVenkatesh Pallipadi 	int cpu;
21345a01f2e8SVenkatesh Pallipadi 
2135a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2136a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2137a7b422cdSKonrad Rzeszutek Wilk 
21385a01f2e8SVenkatesh Pallipadi 	for_each_possible_cpu(cpu) {
2139f1625066STejun Heo 		per_cpu(cpufreq_policy_cpu, cpu) = -1;
21405a01f2e8SVenkatesh Pallipadi 		init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
21415a01f2e8SVenkatesh Pallipadi 	}
21428aa84ad8SThomas Renninger 
21432361be23SViresh Kumar 	cpufreq_global_kobject = kobject_create();
21448aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
2145e00e56dfSRafael J. Wysocki 	register_syscore_ops(&cpufreq_syscore_ops);
21468aa84ad8SThomas Renninger 
21475a01f2e8SVenkatesh Pallipadi 	return 0;
21485a01f2e8SVenkatesh Pallipadi }
21495a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2150