xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision c88a1f8b96e7384627b918dfabbfc0c615a4a914)
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);
43*c88a1f8bSLukasz Majewski static LIST_HEAD(cpufreq_policy_list);
44bb176f7dSViresh Kumar 
45084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU
46084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */
47e77b89f1SDmitry Monakhov static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
48084f3493SThomas Renninger #endif
491da177e4SLinus Torvalds 
505a01f2e8SVenkatesh Pallipadi /*
515a01f2e8SVenkatesh Pallipadi  * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
525a01f2e8SVenkatesh Pallipadi  * all cpufreq/hotplug/workqueue/etc related lock issues.
535a01f2e8SVenkatesh Pallipadi  *
545a01f2e8SVenkatesh Pallipadi  * The rules for this semaphore:
555a01f2e8SVenkatesh Pallipadi  * - Any routine that wants to read from the policy structure will
565a01f2e8SVenkatesh Pallipadi  *   do a down_read on this semaphore.
575a01f2e8SVenkatesh Pallipadi  * - Any routine that will write to the policy structure and/or may take away
585a01f2e8SVenkatesh Pallipadi  *   the policy altogether (eg. CPU hotplug), will hold this lock in write
595a01f2e8SVenkatesh Pallipadi  *   mode before doing so.
605a01f2e8SVenkatesh Pallipadi  *
615a01f2e8SVenkatesh Pallipadi  * Additional rules:
625a01f2e8SVenkatesh Pallipadi  * - Governor routines that can be called in cpufreq hotplug path should not
635a01f2e8SVenkatesh Pallipadi  *   take this sem as top level hotplug notifier handler takes this.
64395913d0SMathieu Desnoyers  * - Lock should not be held across
65395913d0SMathieu Desnoyers  *     __cpufreq_governor(data, CPUFREQ_GOV_STOP);
665a01f2e8SVenkatesh Pallipadi  */
67f1625066STejun Heo static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
685a01f2e8SVenkatesh Pallipadi static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
695a01f2e8SVenkatesh Pallipadi 
705a01f2e8SVenkatesh Pallipadi #define lock_policy_rwsem(mode, cpu)					\
71fa1d8af4SViresh Kumar static int lock_policy_rwsem_##mode(int cpu)				\
725a01f2e8SVenkatesh Pallipadi {									\
73f1625066STejun Heo 	int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);		\
745a01f2e8SVenkatesh Pallipadi 	BUG_ON(policy_cpu == -1);					\
755a01f2e8SVenkatesh Pallipadi 	down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));		\
765a01f2e8SVenkatesh Pallipadi 									\
775a01f2e8SVenkatesh Pallipadi 	return 0;							\
785a01f2e8SVenkatesh Pallipadi }
795a01f2e8SVenkatesh Pallipadi 
805a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(read, cpu);
815a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(write, cpu);
825a01f2e8SVenkatesh Pallipadi 
83fa1d8af4SViresh Kumar #define unlock_policy_rwsem(mode, cpu)					\
84fa1d8af4SViresh Kumar static void unlock_policy_rwsem_##mode(int cpu)				\
85fa1d8af4SViresh Kumar {									\
86fa1d8af4SViresh Kumar 	int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);		\
87fa1d8af4SViresh Kumar 	BUG_ON(policy_cpu == -1);					\
88fa1d8af4SViresh Kumar 	up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));		\
895a01f2e8SVenkatesh Pallipadi }
905a01f2e8SVenkatesh Pallipadi 
91fa1d8af4SViresh Kumar unlock_policy_rwsem(read, cpu);
92fa1d8af4SViresh Kumar unlock_policy_rwsem(write, cpu);
935a01f2e8SVenkatesh Pallipadi 
941da177e4SLinus Torvalds /* internal prototypes */
9529464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy,
9629464f28SDave Jones 		unsigned int event);
975a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu);
9865f27f38SDavid Howells static void handle_update(struct work_struct *work);
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds /**
1011da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
1021da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
1031da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
1041da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
1051da177e4SLinus Torvalds  * The mutex locks both lists.
1061da177e4SLinus Torvalds  */
107e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
108b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list;
1091da177e4SLinus Torvalds 
11074212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called;
111b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void)
112b4dfdbb3SAlan Stern {
113b4dfdbb3SAlan Stern 	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
11474212ca4SCesar Eduardo Barros 	init_cpufreq_transition_notifier_list_called = true;
115b4dfdbb3SAlan Stern 	return 0;
116b4dfdbb3SAlan Stern }
117b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list);
1181da177e4SLinus Torvalds 
119a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
120da584455SViresh Kumar static int cpufreq_disabled(void)
121a7b422cdSKonrad Rzeszutek Wilk {
122a7b422cdSKonrad Rzeszutek Wilk 	return off;
123a7b422cdSKonrad Rzeszutek Wilk }
124a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
125a7b422cdSKonrad Rzeszutek Wilk {
126a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
127a7b422cdSKonrad Rzeszutek Wilk }
1281da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list);
1293fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
1301da177e4SLinus Torvalds 
1314d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1324d5dcc42SViresh Kumar {
1331c3d85ddSRafael J. Wysocki 	return cpufreq_driver->have_governor_per_policy;
1344d5dcc42SViresh Kumar }
1353f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1364d5dcc42SViresh Kumar 
137944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
138944e9a03SViresh Kumar {
139944e9a03SViresh Kumar 	if (have_governor_per_policy())
140944e9a03SViresh Kumar 		return &policy->kobj;
141944e9a03SViresh Kumar 	else
142944e9a03SViresh Kumar 		return cpufreq_global_kobject;
143944e9a03SViresh Kumar }
144944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
145944e9a03SViresh Kumar 
14672a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
14772a4ce34SViresh Kumar {
14872a4ce34SViresh Kumar 	u64 idle_time;
14972a4ce34SViresh Kumar 	u64 cur_wall_time;
15072a4ce34SViresh Kumar 	u64 busy_time;
15172a4ce34SViresh Kumar 
15272a4ce34SViresh Kumar 	cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
15372a4ce34SViresh Kumar 
15472a4ce34SViresh Kumar 	busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
15572a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
15672a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
15772a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
15872a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
15972a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
16072a4ce34SViresh Kumar 
16172a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
16272a4ce34SViresh Kumar 	if (wall)
16372a4ce34SViresh Kumar 		*wall = cputime_to_usecs(cur_wall_time);
16472a4ce34SViresh Kumar 
16572a4ce34SViresh Kumar 	return cputime_to_usecs(idle_time);
16672a4ce34SViresh Kumar }
16772a4ce34SViresh Kumar 
16872a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
16972a4ce34SViresh Kumar {
17072a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
17172a4ce34SViresh Kumar 
17272a4ce34SViresh Kumar 	if (idle_time == -1ULL)
17372a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
17472a4ce34SViresh Kumar 	else if (!io_busy)
17572a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
17672a4ce34SViresh Kumar 
17772a4ce34SViresh Kumar 	return idle_time;
17872a4ce34SViresh Kumar }
17972a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
18072a4ce34SViresh Kumar 
181a9144436SStephen Boyd static struct cpufreq_policy *__cpufreq_cpu_get(unsigned int cpu, bool sysfs)
1821da177e4SLinus Torvalds {
1833a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
1841da177e4SLinus Torvalds 	unsigned long flags;
1851da177e4SLinus Torvalds 
1867a6aedfaSMike Travis 	if (cpu >= nr_cpu_ids)
1871da177e4SLinus Torvalds 		goto err_out;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	/* get the cpufreq driver */
1900d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1911da177e4SLinus Torvalds 
1921c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver)
1931c3d85ddSRafael J. Wysocki 		goto err_out_unlock;
1941c3d85ddSRafael J. Wysocki 
1951c3d85ddSRafael J. Wysocki 	if (!try_module_get(cpufreq_driver->owner))
1961c3d85ddSRafael J. Wysocki 		goto err_out_unlock;
1971c3d85ddSRafael J. Wysocki 
1981da177e4SLinus Torvalds 	/* get the CPU */
1993a3e9e06SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
2001da177e4SLinus Torvalds 
2013a3e9e06SViresh Kumar 	if (!policy)
2021da177e4SLinus Torvalds 		goto err_out_put_module;
2031da177e4SLinus Torvalds 
2043a3e9e06SViresh Kumar 	if (!sysfs && !kobject_get(&policy->kobj))
2051da177e4SLinus Torvalds 		goto err_out_put_module;
2061da177e4SLinus Torvalds 
2070d1857a1SNathan Zimmer 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2083a3e9e06SViresh Kumar 	return policy;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds err_out_put_module:
2111c3d85ddSRafael J. Wysocki 	module_put(cpufreq_driver->owner);
2125800043bSNathan Zimmer err_out_unlock:
2131c3d85ddSRafael J. Wysocki 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2141da177e4SLinus Torvalds err_out:
2151da177e4SLinus Torvalds 	return NULL;
2161da177e4SLinus Torvalds }
217a9144436SStephen Boyd 
218a9144436SStephen Boyd struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
219a9144436SStephen Boyd {
220d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
221d5aaffa9SDirk Brandewie 		return NULL;
222d5aaffa9SDirk Brandewie 
223a9144436SStephen Boyd 	return __cpufreq_cpu_get(cpu, false);
224a9144436SStephen Boyd }
2251da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2261da177e4SLinus Torvalds 
227a9144436SStephen Boyd static struct cpufreq_policy *cpufreq_cpu_get_sysfs(unsigned int cpu)
2281da177e4SLinus Torvalds {
229a9144436SStephen Boyd 	return __cpufreq_cpu_get(cpu, true);
230a9144436SStephen Boyd }
231a9144436SStephen Boyd 
2323a3e9e06SViresh Kumar static void __cpufreq_cpu_put(struct cpufreq_policy *policy, bool sysfs)
233a9144436SStephen Boyd {
234a9144436SStephen Boyd 	if (!sysfs)
2353a3e9e06SViresh Kumar 		kobject_put(&policy->kobj);
2361c3d85ddSRafael J. Wysocki 	module_put(cpufreq_driver->owner);
2371da177e4SLinus Torvalds }
238a9144436SStephen Boyd 
2393a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
240a9144436SStephen Boyd {
241d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
242d5aaffa9SDirk Brandewie 		return;
243d5aaffa9SDirk Brandewie 
2443a3e9e06SViresh Kumar 	__cpufreq_cpu_put(policy, false);
245a9144436SStephen Boyd }
2461da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2471da177e4SLinus Torvalds 
2483a3e9e06SViresh Kumar static void cpufreq_cpu_put_sysfs(struct cpufreq_policy *policy)
249a9144436SStephen Boyd {
2503a3e9e06SViresh Kumar 	__cpufreq_cpu_put(policy, true);
251a9144436SStephen Boyd }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds /*********************************************************************
2541da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2551da177e4SLinus Torvalds  *********************************************************************/
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds /**
2581da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
2591da177e4SLinus Torvalds  *
2601da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
2611da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
2621da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
2631da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
2641da177e4SLinus Torvalds  */
2651da177e4SLinus Torvalds #ifndef CONFIG_SMP
2661da177e4SLinus Torvalds static unsigned long l_p_j_ref;
2671da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq;
2681da177e4SLinus Torvalds 
269858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
2721da177e4SLinus Torvalds 		return;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
2751da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
2761da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
2772d06d8c4SDominik Brodowski 		pr_debug("saving %lu as reference value for loops_per_jiffy; "
278e08f5f5bSGautham R Shenoy 			"freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
2791da177e4SLinus Torvalds 	}
280d08de0c1SAfzal Mohammed 	if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
28142d4dc3fSBenjamin Herrenschmidt 	    (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
282e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
283e08f5f5bSGautham R Shenoy 								ci->new);
2842d06d8c4SDominik Brodowski 		pr_debug("scaling loops_per_jiffy to %lu "
285e08f5f5bSGautham R Shenoy 			"for frequency %u kHz\n", loops_per_jiffy, ci->new);
2861da177e4SLinus Torvalds 	}
2871da177e4SLinus Torvalds }
2881da177e4SLinus Torvalds #else
289e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
290e08f5f5bSGautham R Shenoy {
291e08f5f5bSGautham R Shenoy 	return;
292e08f5f5bSGautham R Shenoy }
2931da177e4SLinus Torvalds #endif
2941da177e4SLinus Torvalds 
2950956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
296b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
2991da177e4SLinus Torvalds 
300d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
301d5aaffa9SDirk Brandewie 		return;
302d5aaffa9SDirk Brandewie 
3031c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
3042d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
305e4472cb3SDave Jones 		state, freqs->new);
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	switch (state) {
308e4472cb3SDave Jones 
3091da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
310266c13d7SViresh Kumar 		if (WARN(policy->transition_ongoing ==
311266c13d7SViresh Kumar 					cpumask_weight(policy->cpus),
3127c30ed53SViresh Kumar 				"In middle of another frequency transition\n"))
3137c30ed53SViresh Kumar 			return;
3147c30ed53SViresh Kumar 
315266c13d7SViresh Kumar 		policy->transition_ongoing++;
3167c30ed53SViresh Kumar 
317e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
318e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
319e4472cb3SDave Jones 		 * "old frequency".
3201da177e4SLinus Torvalds 		 */
3211c3d85ddSRafael J. Wysocki 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
322e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
323e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
3242d06d8c4SDominik Brodowski 				pr_debug("Warning: CPU frequency is"
325e4472cb3SDave Jones 					" %u, cpufreq assumed %u kHz.\n",
326e4472cb3SDave Jones 					freqs->old, policy->cur);
327e4472cb3SDave Jones 				freqs->old = policy->cur;
3281da177e4SLinus Torvalds 			}
3291da177e4SLinus Torvalds 		}
330b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
331e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
3321da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3331da177e4SLinus Torvalds 		break;
334e4472cb3SDave Jones 
3351da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3367c30ed53SViresh Kumar 		if (WARN(!policy->transition_ongoing,
3377c30ed53SViresh Kumar 				"No frequency transition in progress\n"))
3387c30ed53SViresh Kumar 			return;
3397c30ed53SViresh Kumar 
340266c13d7SViresh Kumar 		policy->transition_ongoing--;
3417c30ed53SViresh Kumar 
3421da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
3432d06d8c4SDominik Brodowski 		pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
3446f4f2723SThomas Renninger 			(unsigned long)freqs->cpu);
34525e41933SThomas Renninger 		trace_cpu_frequency(freqs->new, freqs->cpu);
346b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
347e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
348e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
349e4472cb3SDave Jones 			policy->cur = freqs->new;
3501da177e4SLinus Torvalds 		break;
3511da177e4SLinus Torvalds 	}
3521da177e4SLinus Torvalds }
353bb176f7dSViresh Kumar 
354b43a7ffbSViresh Kumar /**
355b43a7ffbSViresh Kumar  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
356b43a7ffbSViresh Kumar  * on frequency transition.
357b43a7ffbSViresh Kumar  *
358b43a7ffbSViresh Kumar  * This function calls the transition notifiers and the "adjust_jiffies"
359b43a7ffbSViresh Kumar  * function. It is called twice on all CPU frequency changes that have
360b43a7ffbSViresh Kumar  * external effects.
361b43a7ffbSViresh Kumar  */
362b43a7ffbSViresh Kumar void cpufreq_notify_transition(struct cpufreq_policy *policy,
363b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
364b43a7ffbSViresh Kumar {
365b43a7ffbSViresh Kumar 	for_each_cpu(freqs->cpu, policy->cpus)
366b43a7ffbSViresh Kumar 		__cpufreq_notify_transition(policy, freqs, state);
367b43a7ffbSViresh Kumar }
3681da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 
3711da177e4SLinus Torvalds /*********************************************************************
3721da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
3731da177e4SLinus Torvalds  *********************************************************************/
3741da177e4SLinus Torvalds 
3753bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor)
3763bcb09a3SJeremy Fitzhardinge {
3773bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
3783bcb09a3SJeremy Fitzhardinge 
3793bcb09a3SJeremy Fitzhardinge 	list_for_each_entry(t, &cpufreq_governor_list, governor_list)
3803bcb09a3SJeremy Fitzhardinge 		if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
3813bcb09a3SJeremy Fitzhardinge 			return t;
3823bcb09a3SJeremy Fitzhardinge 
3833bcb09a3SJeremy Fitzhardinge 	return NULL;
3843bcb09a3SJeremy Fitzhardinge }
3853bcb09a3SJeremy Fitzhardinge 
3861da177e4SLinus Torvalds /**
3871da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
3881da177e4SLinus Torvalds  */
3891da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
3901da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
3911da177e4SLinus Torvalds {
3923bcb09a3SJeremy Fitzhardinge 	int err = -EINVAL;
3933bcb09a3SJeremy Fitzhardinge 
3941c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver)
3953bcb09a3SJeremy Fitzhardinge 		goto out;
3963bcb09a3SJeremy Fitzhardinge 
3971c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
3981da177e4SLinus Torvalds 		if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
3991da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
4003bcb09a3SJeremy Fitzhardinge 			err = 0;
401e08f5f5bSGautham R Shenoy 		} else if (!strnicmp(str_governor, "powersave",
402e08f5f5bSGautham R Shenoy 						CPUFREQ_NAME_LEN)) {
4031da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
4043bcb09a3SJeremy Fitzhardinge 			err = 0;
4051da177e4SLinus Torvalds 		}
4061c3d85ddSRafael J. Wysocki 	} else if (cpufreq_driver->target) {
4071da177e4SLinus Torvalds 		struct cpufreq_governor *t;
4083bcb09a3SJeremy Fitzhardinge 
4093fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
4103bcb09a3SJeremy Fitzhardinge 
4113bcb09a3SJeremy Fitzhardinge 		t = __find_governor(str_governor);
4123bcb09a3SJeremy Fitzhardinge 
413ea714970SJeremy Fitzhardinge 		if (t == NULL) {
414ea714970SJeremy Fitzhardinge 			int ret;
415ea714970SJeremy Fitzhardinge 
416ea714970SJeremy Fitzhardinge 			mutex_unlock(&cpufreq_governor_mutex);
4171a8e1463SKees Cook 			ret = request_module("cpufreq_%s", str_governor);
418ea714970SJeremy Fitzhardinge 			mutex_lock(&cpufreq_governor_mutex);
419ea714970SJeremy Fitzhardinge 
420ea714970SJeremy Fitzhardinge 			if (ret == 0)
421ea714970SJeremy Fitzhardinge 				t = __find_governor(str_governor);
422ea714970SJeremy Fitzhardinge 		}
423ea714970SJeremy Fitzhardinge 
4243bcb09a3SJeremy Fitzhardinge 		if (t != NULL) {
4251da177e4SLinus Torvalds 			*governor = t;
4263bcb09a3SJeremy Fitzhardinge 			err = 0;
4271da177e4SLinus Torvalds 		}
4283bcb09a3SJeremy Fitzhardinge 
4293bcb09a3SJeremy Fitzhardinge 		mutex_unlock(&cpufreq_governor_mutex);
4301da177e4SLinus Torvalds 	}
4311da177e4SLinus Torvalds out:
4323bcb09a3SJeremy Fitzhardinge 	return err;
4331da177e4SLinus Torvalds }
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds /**
436e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
437e08f5f5bSGautham R Shenoy  * print out cpufreq information
4381da177e4SLinus Torvalds  *
4391da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
4401da177e4SLinus Torvalds  * "unsigned int".
4411da177e4SLinus Torvalds  */
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds #define show_one(file_name, object)			\
4441da177e4SLinus Torvalds static ssize_t show_##file_name				\
4451da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
4461da177e4SLinus Torvalds {							\
4471da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
4511da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
452ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
4531da177e4SLinus Torvalds show_one(scaling_min_freq, min);
4541da177e4SLinus Torvalds show_one(scaling_max_freq, max);
4551da177e4SLinus Torvalds show_one(scaling_cur_freq, cur);
4561da177e4SLinus Torvalds 
4573a3e9e06SViresh Kumar static int __cpufreq_set_policy(struct cpufreq_policy *policy,
4583a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy);
4597970e08bSThomas Renninger 
4601da177e4SLinus Torvalds /**
4611da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
4621da177e4SLinus Torvalds  */
4631da177e4SLinus Torvalds #define store_one(file_name, object)			\
4641da177e4SLinus Torvalds static ssize_t store_##file_name					\
4651da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
4661da177e4SLinus Torvalds {									\
467f55c9c26SJingoo Han 	unsigned int ret;						\
4681da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
4691da177e4SLinus Torvalds 									\
4701da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
4711da177e4SLinus Torvalds 	if (ret)							\
4721da177e4SLinus Torvalds 		return -EINVAL;						\
4731da177e4SLinus Torvalds 									\
4741da177e4SLinus Torvalds 	ret = sscanf(buf, "%u", &new_policy.object);			\
4751da177e4SLinus Torvalds 	if (ret != 1)							\
4761da177e4SLinus Torvalds 		return -EINVAL;						\
4771da177e4SLinus Torvalds 									\
4787970e08bSThomas Renninger 	ret = __cpufreq_set_policy(policy, &new_policy);		\
4797970e08bSThomas Renninger 	policy->user_policy.object = policy->object;			\
4801da177e4SLinus Torvalds 									\
4811da177e4SLinus Torvalds 	return ret ? ret : count;					\
4821da177e4SLinus Torvalds }
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds store_one(scaling_min_freq, min);
4851da177e4SLinus Torvalds store_one(scaling_max_freq, max);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /**
4881da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
4891da177e4SLinus Torvalds  */
490e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
491e08f5f5bSGautham R Shenoy 					char *buf)
4921da177e4SLinus Torvalds {
4935a01f2e8SVenkatesh Pallipadi 	unsigned int cur_freq = __cpufreq_get(policy->cpu);
4941da177e4SLinus Torvalds 	if (!cur_freq)
4951da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
4961da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds /**
5001da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
5011da177e4SLinus Torvalds  */
502905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
5051da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
5061da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
5071da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
5081da177e4SLinus Torvalds 	else if (policy->governor)
5094b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
51029464f28SDave Jones 				policy->governor->name);
5111da177e4SLinus Torvalds 	return -EINVAL;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds /**
5151da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
5161da177e4SLinus Torvalds  */
5171da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
5181da177e4SLinus Torvalds 					const char *buf, size_t count)
5191da177e4SLinus Torvalds {
520f55c9c26SJingoo Han 	unsigned int ret;
5211da177e4SLinus Torvalds 	char	str_governor[16];
5221da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);
5251da177e4SLinus Torvalds 	if (ret)
5261da177e4SLinus Torvalds 		return ret;
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
5291da177e4SLinus Torvalds 	if (ret != 1)
5301da177e4SLinus Torvalds 		return -EINVAL;
5311da177e4SLinus Torvalds 
532e08f5f5bSGautham R Shenoy 	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
533e08f5f5bSGautham R Shenoy 						&new_policy.governor))
5341da177e4SLinus Torvalds 		return -EINVAL;
5351da177e4SLinus Torvalds 
536bb176f7dSViresh Kumar 	/*
537bb176f7dSViresh Kumar 	 * Do not use cpufreq_set_policy here or the user_policy.max
538bb176f7dSViresh Kumar 	 * will be wrongly overridden
539bb176f7dSViresh Kumar 	 */
5407970e08bSThomas Renninger 	ret = __cpufreq_set_policy(policy, &new_policy);
5417970e08bSThomas Renninger 
5427970e08bSThomas Renninger 	policy->user_policy.policy = policy->policy;
5437970e08bSThomas Renninger 	policy->user_policy.governor = policy->governor;
5447970e08bSThomas Renninger 
545e08f5f5bSGautham R Shenoy 	if (ret)
546e08f5f5bSGautham R Shenoy 		return ret;
547e08f5f5bSGautham R Shenoy 	else
548e08f5f5bSGautham R Shenoy 		return count;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds /**
5521da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
5531da177e4SLinus Torvalds  */
5541da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
5551da177e4SLinus Torvalds {
5561c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds /**
5601da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
5611da177e4SLinus Torvalds  */
5621da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
5631da177e4SLinus Torvalds 						char *buf)
5641da177e4SLinus Torvalds {
5651da177e4SLinus Torvalds 	ssize_t i = 0;
5661da177e4SLinus Torvalds 	struct cpufreq_governor *t;
5671da177e4SLinus Torvalds 
5681c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->target) {
5691da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
5701da177e4SLinus Torvalds 		goto out;
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
57429464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
57529464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
5761da177e4SLinus Torvalds 			goto out;
5774b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
5781da177e4SLinus Torvalds 	}
5791da177e4SLinus Torvalds out:
5801da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
5811da177e4SLinus Torvalds 	return i;
5821da177e4SLinus Torvalds }
583e8628dd0SDarrick J. Wong 
584f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
5851da177e4SLinus Torvalds {
5861da177e4SLinus Torvalds 	ssize_t i = 0;
5871da177e4SLinus Torvalds 	unsigned int cpu;
5881da177e4SLinus Torvalds 
589835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
5901da177e4SLinus Torvalds 		if (i)
5911da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
5921da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
5931da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
5941da177e4SLinus Torvalds 			break;
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
5971da177e4SLinus Torvalds 	return i;
5981da177e4SLinus Torvalds }
599f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
6001da177e4SLinus Torvalds 
601e8628dd0SDarrick J. Wong /**
602e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
603e8628dd0SDarrick J. Wong  * hw coordination is in use
604e8628dd0SDarrick J. Wong  */
605e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
606e8628dd0SDarrick J. Wong {
607f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
608e8628dd0SDarrick J. Wong }
609e8628dd0SDarrick J. Wong 
610e8628dd0SDarrick J. Wong /**
611e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
612e8628dd0SDarrick J. Wong  */
613e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
614e8628dd0SDarrick J. Wong {
615f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
616e8628dd0SDarrick J. Wong }
617e8628dd0SDarrick J. Wong 
6189e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
6199e76988eSVenki Pallipadi 					const char *buf, size_t count)
6209e76988eSVenki Pallipadi {
6219e76988eSVenki Pallipadi 	unsigned int freq = 0;
6229e76988eSVenki Pallipadi 	unsigned int ret;
6239e76988eSVenki Pallipadi 
624879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
6259e76988eSVenki Pallipadi 		return -EINVAL;
6269e76988eSVenki Pallipadi 
6279e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
6289e76988eSVenki Pallipadi 	if (ret != 1)
6299e76988eSVenki Pallipadi 		return -EINVAL;
6309e76988eSVenki Pallipadi 
6319e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
6329e76988eSVenki Pallipadi 
6339e76988eSVenki Pallipadi 	return count;
6349e76988eSVenki Pallipadi }
6359e76988eSVenki Pallipadi 
6369e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
6379e76988eSVenki Pallipadi {
638879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
6399e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
6409e76988eSVenki Pallipadi 
6419e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
6429e76988eSVenki Pallipadi }
6431da177e4SLinus Torvalds 
644e2f74f35SThomas Renninger /**
6458bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
646e2f74f35SThomas Renninger  */
647e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
648e2f74f35SThomas Renninger {
649e2f74f35SThomas Renninger 	unsigned int limit;
650e2f74f35SThomas Renninger 	int ret;
6511c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
6521c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
653e2f74f35SThomas Renninger 		if (!ret)
654e2f74f35SThomas Renninger 			return sprintf(buf, "%u\n", limit);
655e2f74f35SThomas Renninger 	}
656e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
657e2f74f35SThomas Renninger }
658e2f74f35SThomas Renninger 
6596dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
6606dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
6616dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
6626dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
6636dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
6646dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
6656dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
6666dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
6676dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
6686dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
6696dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
6706dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
6716dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
6726dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds static struct attribute *default_attrs[] = {
6751da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
6761da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
677ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
6781da177e4SLinus Torvalds 	&scaling_min_freq.attr,
6791da177e4SLinus Torvalds 	&scaling_max_freq.attr,
6801da177e4SLinus Torvalds 	&affected_cpus.attr,
681e8628dd0SDarrick J. Wong 	&related_cpus.attr,
6821da177e4SLinus Torvalds 	&scaling_governor.attr,
6831da177e4SLinus Torvalds 	&scaling_driver.attr,
6841da177e4SLinus Torvalds 	&scaling_available_governors.attr,
6859e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
6861da177e4SLinus Torvalds 	NULL
6871da177e4SLinus Torvalds };
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
6901da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
6951da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
6960db4a8a9SDave Jones 	ssize_t ret = -EINVAL;
697a9144436SStephen Boyd 	policy = cpufreq_cpu_get_sysfs(policy->cpu);
6981da177e4SLinus Torvalds 	if (!policy)
6990db4a8a9SDave Jones 		goto no_policy;
7005a01f2e8SVenkatesh Pallipadi 
7015a01f2e8SVenkatesh Pallipadi 	if (lock_policy_rwsem_read(policy->cpu) < 0)
7020db4a8a9SDave Jones 		goto fail;
7035a01f2e8SVenkatesh Pallipadi 
704e08f5f5bSGautham R Shenoy 	if (fattr->show)
705e08f5f5bSGautham R Shenoy 		ret = fattr->show(policy, buf);
706e08f5f5bSGautham R Shenoy 	else
707e08f5f5bSGautham R Shenoy 		ret = -EIO;
708e08f5f5bSGautham R Shenoy 
7095a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_read(policy->cpu);
7100db4a8a9SDave Jones fail:
711a9144436SStephen Boyd 	cpufreq_cpu_put_sysfs(policy);
7120db4a8a9SDave Jones no_policy:
7131da177e4SLinus Torvalds 	return ret;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
7171da177e4SLinus Torvalds 		     const char *buf, size_t count)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7201da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
721a07530b4SDave Jones 	ssize_t ret = -EINVAL;
722a9144436SStephen Boyd 	policy = cpufreq_cpu_get_sysfs(policy->cpu);
7231da177e4SLinus Torvalds 	if (!policy)
724a07530b4SDave Jones 		goto no_policy;
7255a01f2e8SVenkatesh Pallipadi 
7265a01f2e8SVenkatesh Pallipadi 	if (lock_policy_rwsem_write(policy->cpu) < 0)
727a07530b4SDave Jones 		goto fail;
7285a01f2e8SVenkatesh Pallipadi 
729e08f5f5bSGautham R Shenoy 	if (fattr->store)
730e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
731e08f5f5bSGautham R Shenoy 	else
732e08f5f5bSGautham R Shenoy 		ret = -EIO;
733e08f5f5bSGautham R Shenoy 
7345a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(policy->cpu);
735a07530b4SDave Jones fail:
736a9144436SStephen Boyd 	cpufreq_cpu_put_sysfs(policy);
737a07530b4SDave Jones no_policy:
7381da177e4SLinus Torvalds 	return ret;
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
7421da177e4SLinus Torvalds {
7431da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7442d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
7451da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
7461da177e4SLinus Torvalds }
7471da177e4SLinus Torvalds 
74852cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
7491da177e4SLinus Torvalds 	.show	= show,
7501da177e4SLinus Torvalds 	.store	= store,
7511da177e4SLinus Torvalds };
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
7541da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
7551da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
7561da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
7571da177e4SLinus Torvalds };
7581da177e4SLinus Torvalds 
7592361be23SViresh Kumar struct kobject *cpufreq_global_kobject;
7602361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject);
7612361be23SViresh Kumar 
7622361be23SViresh Kumar static int cpufreq_global_kobject_usage;
7632361be23SViresh Kumar 
7642361be23SViresh Kumar int cpufreq_get_global_kobject(void)
7652361be23SViresh Kumar {
7662361be23SViresh Kumar 	if (!cpufreq_global_kobject_usage++)
7672361be23SViresh Kumar 		return kobject_add(cpufreq_global_kobject,
7682361be23SViresh Kumar 				&cpu_subsys.dev_root->kobj, "%s", "cpufreq");
7692361be23SViresh Kumar 
7702361be23SViresh Kumar 	return 0;
7712361be23SViresh Kumar }
7722361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject);
7732361be23SViresh Kumar 
7742361be23SViresh Kumar void cpufreq_put_global_kobject(void)
7752361be23SViresh Kumar {
7762361be23SViresh Kumar 	if (!--cpufreq_global_kobject_usage)
7772361be23SViresh Kumar 		kobject_del(cpufreq_global_kobject);
7782361be23SViresh Kumar }
7792361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject);
7802361be23SViresh Kumar 
7812361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr)
7822361be23SViresh Kumar {
7832361be23SViresh Kumar 	int ret = cpufreq_get_global_kobject();
7842361be23SViresh Kumar 
7852361be23SViresh Kumar 	if (!ret) {
7862361be23SViresh Kumar 		ret = sysfs_create_file(cpufreq_global_kobject, attr);
7872361be23SViresh Kumar 		if (ret)
7882361be23SViresh Kumar 			cpufreq_put_global_kobject();
7892361be23SViresh Kumar 	}
7902361be23SViresh Kumar 
7912361be23SViresh Kumar 	return ret;
7922361be23SViresh Kumar }
7932361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file);
7942361be23SViresh Kumar 
7952361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr)
7962361be23SViresh Kumar {
7972361be23SViresh Kumar 	sysfs_remove_file(cpufreq_global_kobject, attr);
7982361be23SViresh Kumar 	cpufreq_put_global_kobject();
7992361be23SViresh Kumar }
8002361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
8012361be23SViresh Kumar 
80219d6f7ecSDave Jones /* symlink affected CPUs */
803308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
80419d6f7ecSDave Jones {
80519d6f7ecSDave Jones 	unsigned int j;
80619d6f7ecSDave Jones 	int ret = 0;
80719d6f7ecSDave Jones 
80819d6f7ecSDave Jones 	for_each_cpu(j, policy->cpus) {
8098a25a2fdSKay Sievers 		struct device *cpu_dev;
81019d6f7ecSDave Jones 
811308b60e7SViresh Kumar 		if (j == policy->cpu)
81219d6f7ecSDave Jones 			continue;
81319d6f7ecSDave Jones 
814e8fdde10SViresh Kumar 		pr_debug("Adding link for CPU: %u\n", j);
8158a25a2fdSKay Sievers 		cpu_dev = get_cpu_device(j);
8168a25a2fdSKay Sievers 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
81719d6f7ecSDave Jones 					"cpufreq");
81871c3461eSRafael J. Wysocki 		if (ret)
81971c3461eSRafael J. Wysocki 			break;
82019d6f7ecSDave Jones 	}
82119d6f7ecSDave Jones 	return ret;
82219d6f7ecSDave Jones }
82319d6f7ecSDave Jones 
824308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
8258a25a2fdSKay Sievers 				     struct device *dev)
826909a694eSDave Jones {
827909a694eSDave Jones 	struct freq_attr **drv_attr;
828909a694eSDave Jones 	int ret = 0;
829909a694eSDave Jones 
830909a694eSDave Jones 	/* prepare interface data */
831909a694eSDave Jones 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
8328a25a2fdSKay Sievers 				   &dev->kobj, "cpufreq");
833909a694eSDave Jones 	if (ret)
834909a694eSDave Jones 		return ret;
835909a694eSDave Jones 
836909a694eSDave Jones 	/* set up files for this cpu device */
8371c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
838909a694eSDave Jones 	while ((drv_attr) && (*drv_attr)) {
839909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
840909a694eSDave Jones 		if (ret)
8411c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
842909a694eSDave Jones 		drv_attr++;
843909a694eSDave Jones 	}
8441c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
845909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
846909a694eSDave Jones 		if (ret)
8471c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
848909a694eSDave Jones 	}
8491c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target) {
850909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
851909a694eSDave Jones 		if (ret)
8521c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
853909a694eSDave Jones 	}
8541c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
855e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
856e2f74f35SThomas Renninger 		if (ret)
8571c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
858e2f74f35SThomas Renninger 	}
859909a694eSDave Jones 
860308b60e7SViresh Kumar 	ret = cpufreq_add_dev_symlink(policy);
861ecf7e461SDave Jones 	if (ret)
862ecf7e461SDave Jones 		goto err_out_kobj_put;
863ecf7e461SDave Jones 
864e18f1682SSrivatsa S. Bhat 	return ret;
865e18f1682SSrivatsa S. Bhat 
866e18f1682SSrivatsa S. Bhat err_out_kobj_put:
867e18f1682SSrivatsa S. Bhat 	kobject_put(&policy->kobj);
868e18f1682SSrivatsa S. Bhat 	wait_for_completion(&policy->kobj_unregister);
869e18f1682SSrivatsa S. Bhat 	return ret;
870e18f1682SSrivatsa S. Bhat }
871e18f1682SSrivatsa S. Bhat 
872e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy)
873e18f1682SSrivatsa S. Bhat {
874e18f1682SSrivatsa S. Bhat 	struct cpufreq_policy new_policy;
875e18f1682SSrivatsa S. Bhat 	int ret = 0;
876e18f1682SSrivatsa S. Bhat 
877d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
878ecf7e461SDave Jones 	/* assure that the starting sequence is run in __cpufreq_set_policy */
879ecf7e461SDave Jones 	policy->governor = NULL;
880ecf7e461SDave Jones 
881ecf7e461SDave Jones 	/* set default policy */
882ecf7e461SDave Jones 	ret = __cpufreq_set_policy(policy, &new_policy);
883ecf7e461SDave Jones 	policy->user_policy.policy = policy->policy;
884ecf7e461SDave Jones 	policy->user_policy.governor = policy->governor;
885ecf7e461SDave Jones 
886ecf7e461SDave Jones 	if (ret) {
8872d06d8c4SDominik Brodowski 		pr_debug("setting policy failed\n");
8881c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
8891c3d85ddSRafael J. Wysocki 			cpufreq_driver->exit(policy);
890ecf7e461SDave Jones 	}
891909a694eSDave Jones }
892909a694eSDave Jones 
893fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
894d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
895d8d3b471SViresh Kumar 				  unsigned int cpu, struct device *dev,
896d8d3b471SViresh Kumar 				  bool frozen)
897fcf80582SViresh Kumar {
8981c3d85ddSRafael J. Wysocki 	int ret = 0, has_target = !!cpufreq_driver->target;
899fcf80582SViresh Kumar 	unsigned long flags;
900fcf80582SViresh Kumar 
901820c6ca2SViresh Kumar 	if (has_target)
902fcf80582SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
903fcf80582SViresh Kumar 
904d8d3b471SViresh Kumar 	lock_policy_rwsem_write(policy->cpu);
9052eaa3e2dSViresh Kumar 
9060d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
9072eaa3e2dSViresh Kumar 
908fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
9092eaa3e2dSViresh Kumar 	per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
910fcf80582SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = policy;
9110d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
912fcf80582SViresh Kumar 
913d8d3b471SViresh Kumar 	unlock_policy_rwsem_write(policy->cpu);
9142eaa3e2dSViresh Kumar 
915820c6ca2SViresh Kumar 	if (has_target) {
916fcf80582SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_START);
917fcf80582SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
918820c6ca2SViresh Kumar 	}
919fcf80582SViresh Kumar 
920a82fab29SSrivatsa S. Bhat 	/* Don't touch sysfs links during light-weight init */
92171c3461eSRafael J. Wysocki 	if (!frozen)
922a82fab29SSrivatsa S. Bhat 		ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
923a82fab29SSrivatsa S. Bhat 
924a82fab29SSrivatsa S. Bhat 	return ret;
925fcf80582SViresh Kumar }
926fcf80582SViresh Kumar #endif
9271da177e4SLinus Torvalds 
9288414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
9298414809cSSrivatsa S. Bhat {
9308414809cSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
9318414809cSSrivatsa S. Bhat 	unsigned long flags;
9328414809cSSrivatsa S. Bhat 
9338414809cSSrivatsa S. Bhat 	write_lock_irqsave(&cpufreq_driver_lock, flags);
9348414809cSSrivatsa S. Bhat 
9358414809cSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
9368414809cSSrivatsa S. Bhat 
9378414809cSSrivatsa S. Bhat 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
9388414809cSSrivatsa S. Bhat 
9398414809cSSrivatsa S. Bhat 	return policy;
9408414809cSSrivatsa S. Bhat }
9418414809cSSrivatsa S. Bhat 
942e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void)
943e9698cc5SSrivatsa S. Bhat {
944e9698cc5SSrivatsa S. Bhat 	struct cpufreq_policy *policy;
945e9698cc5SSrivatsa S. Bhat 
946e9698cc5SSrivatsa S. Bhat 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
947e9698cc5SSrivatsa S. Bhat 	if (!policy)
948e9698cc5SSrivatsa S. Bhat 		return NULL;
949e9698cc5SSrivatsa S. Bhat 
950e9698cc5SSrivatsa S. Bhat 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
951e9698cc5SSrivatsa S. Bhat 		goto err_free_policy;
952e9698cc5SSrivatsa S. Bhat 
953e9698cc5SSrivatsa S. Bhat 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
954e9698cc5SSrivatsa S. Bhat 		goto err_free_cpumask;
955e9698cc5SSrivatsa S. Bhat 
956*c88a1f8bSLukasz Majewski 	INIT_LIST_HEAD(&policy->policy_list);
957e9698cc5SSrivatsa S. Bhat 	return policy;
958e9698cc5SSrivatsa S. Bhat 
959e9698cc5SSrivatsa S. Bhat err_free_cpumask:
960e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
961e9698cc5SSrivatsa S. Bhat err_free_policy:
962e9698cc5SSrivatsa S. Bhat 	kfree(policy);
963e9698cc5SSrivatsa S. Bhat 
964e9698cc5SSrivatsa S. Bhat 	return NULL;
965e9698cc5SSrivatsa S. Bhat }
966e9698cc5SSrivatsa S. Bhat 
967e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy)
968e9698cc5SSrivatsa S. Bhat {
969*c88a1f8bSLukasz Majewski 	unsigned long flags;
970*c88a1f8bSLukasz Majewski 
971*c88a1f8bSLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
972*c88a1f8bSLukasz Majewski 	list_del(&policy->policy_list);
973*c88a1f8bSLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
974*c88a1f8bSLukasz Majewski 
975e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
976e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
977e9698cc5SSrivatsa S. Bhat 	kfree(policy);
978e9698cc5SSrivatsa S. Bhat }
979e9698cc5SSrivatsa S. Bhat 
980a82fab29SSrivatsa S. Bhat static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
981a82fab29SSrivatsa S. Bhat 			     bool frozen)
9821da177e4SLinus Torvalds {
983fcf80582SViresh Kumar 	unsigned int j, cpu = dev->id;
98465922465SViresh Kumar 	int ret = -ENOMEM;
9851da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
9861da177e4SLinus Torvalds 	unsigned long flags;
98790e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
988fcf80582SViresh Kumar 	struct cpufreq_governor *gov;
98990e41bacSPrarit Bhargava 	int sibling;
99090e41bacSPrarit Bhargava #endif
9911da177e4SLinus Torvalds 
992c32b6b8eSAshok Raj 	if (cpu_is_offline(cpu))
993c32b6b8eSAshok Raj 		return 0;
994c32b6b8eSAshok Raj 
9952d06d8c4SDominik Brodowski 	pr_debug("adding CPU %u\n", cpu);
9961da177e4SLinus Torvalds 
9971da177e4SLinus Torvalds #ifdef CONFIG_SMP
9981da177e4SLinus Torvalds 	/* check whether a different CPU already registered this
9991da177e4SLinus Torvalds 	 * CPU because it is in the same boat. */
10001da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(cpu);
10011da177e4SLinus Torvalds 	if (unlikely(policy)) {
10028ff69732SDave Jones 		cpufreq_cpu_put(policy);
10031da177e4SLinus Torvalds 		return 0;
10041da177e4SLinus Torvalds 	}
1005fcf80582SViresh Kumar 
1006fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
1007fcf80582SViresh Kumar 	/* Check if this cpu was hot-unplugged earlier and has siblings */
10080d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1009fcf80582SViresh Kumar 	for_each_online_cpu(sibling) {
1010fcf80582SViresh Kumar 		struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
10112eaa3e2dSViresh Kumar 		if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
10120d1857a1SNathan Zimmer 			read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1013d8d3b471SViresh Kumar 			return cpufreq_add_policy_cpu(cp, cpu, dev, frozen);
1014fcf80582SViresh Kumar 		}
10152eaa3e2dSViresh Kumar 	}
10160d1857a1SNathan Zimmer 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1017fcf80582SViresh Kumar #endif
10181da177e4SLinus Torvalds #endif
10191da177e4SLinus Torvalds 
10201c3d85ddSRafael J. Wysocki 	if (!try_module_get(cpufreq_driver->owner)) {
10211da177e4SLinus Torvalds 		ret = -EINVAL;
10221da177e4SLinus Torvalds 		goto module_out;
10231da177e4SLinus Torvalds 	}
10241da177e4SLinus Torvalds 
10258414809cSSrivatsa S. Bhat 	if (frozen)
10268414809cSSrivatsa S. Bhat 		/* Restore the saved policy when doing light-weight init */
10278414809cSSrivatsa S. Bhat 		policy = cpufreq_policy_restore(cpu);
10288414809cSSrivatsa S. Bhat 	else
1029e9698cc5SSrivatsa S. Bhat 		policy = cpufreq_policy_alloc();
10308414809cSSrivatsa S. Bhat 
1031059019a3SDave Jones 	if (!policy)
10321da177e4SLinus Torvalds 		goto nomem_out;
1033059019a3SDave Jones 
10341da177e4SLinus Torvalds 	policy->cpu = cpu;
103565922465SViresh Kumar 	policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1036835481d9SRusty Russell 	cpumask_copy(policy->cpus, cpumask_of(cpu));
10371da177e4SLinus Torvalds 
10385a01f2e8SVenkatesh Pallipadi 	/* Initially set CPU itself as the policy_cpu */
1039f1625066STejun Heo 	per_cpu(cpufreq_policy_cpu, cpu) = cpu;
10405a01f2e8SVenkatesh Pallipadi 
10411da177e4SLinus Torvalds 	init_completion(&policy->kobj_unregister);
104265f27f38SDavid Howells 	INIT_WORK(&policy->update, handle_update);
10431da177e4SLinus Torvalds 
10441da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
10451da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
10461da177e4SLinus Torvalds 	 */
10471c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->init(policy);
10481da177e4SLinus Torvalds 	if (ret) {
10492d06d8c4SDominik Brodowski 		pr_debug("initialization failed\n");
10502eaa3e2dSViresh Kumar 		goto err_set_policy_cpu;
10511da177e4SLinus Torvalds 	}
1052643ae6e8SViresh Kumar 
1053fcf80582SViresh Kumar 	/* related cpus should atleast have policy->cpus */
1054fcf80582SViresh Kumar 	cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1055fcf80582SViresh Kumar 
1056643ae6e8SViresh Kumar 	/*
1057643ae6e8SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
1058643ae6e8SViresh Kumar 	 * managing offline cpus here.
1059643ae6e8SViresh Kumar 	 */
1060643ae6e8SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1061643ae6e8SViresh Kumar 
1062187d9f4eSMike Chan 	policy->user_policy.min = policy->min;
1063187d9f4eSMike Chan 	policy->user_policy.max = policy->max;
10641da177e4SLinus Torvalds 
1065a1531acdSThomas Renninger 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1066a1531acdSThomas Renninger 				     CPUFREQ_START, policy);
1067a1531acdSThomas Renninger 
1068fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
1069fcf80582SViresh Kumar 	gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1070fcf80582SViresh Kumar 	if (gov) {
1071fcf80582SViresh Kumar 		policy->governor = gov;
1072fcf80582SViresh Kumar 		pr_debug("Restoring governor %s for cpu %d\n",
1073fcf80582SViresh Kumar 		       policy->governor->name, cpu);
10744bfa042cSThomas Renninger 	}
1075fcf80582SViresh Kumar #endif
10761da177e4SLinus Torvalds 
1077e18f1682SSrivatsa S. Bhat 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1078e18f1682SSrivatsa S. Bhat 	for_each_cpu(j, policy->cpus) {
1079e18f1682SSrivatsa S. Bhat 		per_cpu(cpufreq_cpu_data, j) = policy;
1080e18f1682SSrivatsa S. Bhat 		per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
1081e18f1682SSrivatsa S. Bhat 	}
1082e18f1682SSrivatsa S. Bhat 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1083e18f1682SSrivatsa S. Bhat 
1084a82fab29SSrivatsa S. Bhat 	if (!frozen) {
1085308b60e7SViresh Kumar 		ret = cpufreq_add_dev_interface(policy, dev);
108619d6f7ecSDave Jones 		if (ret)
10870142f9dcSAhmed S. Darwish 			goto err_out_unregister;
1088*c88a1f8bSLukasz Majewski 
1089*c88a1f8bSLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
1090*c88a1f8bSLukasz Majewski 		list_add(&policy->policy_list, &cpufreq_policy_list);
1091*c88a1f8bSLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1092a82fab29SSrivatsa S. Bhat 	}
10938ff69732SDave Jones 
1094e18f1682SSrivatsa S. Bhat 	cpufreq_init_policy(policy);
1095e18f1682SSrivatsa S. Bhat 
1096038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
10971c3d85ddSRafael J. Wysocki 	module_put(cpufreq_driver->owner);
10982d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	return 0;
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds err_out_unregister:
11030d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1104e18f1682SSrivatsa S. Bhat 	for_each_cpu(j, policy->cpus) {
11057a6aedfaSMike Travis 		per_cpu(cpufreq_cpu_data, j) = NULL;
1106e18f1682SSrivatsa S. Bhat 		if (j != cpu)
1107e18f1682SSrivatsa S. Bhat 			per_cpu(cpufreq_policy_cpu, j) = -1;
1108e18f1682SSrivatsa S. Bhat 	}
11090d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
11101da177e4SLinus Torvalds 
11112eaa3e2dSViresh Kumar err_set_policy_cpu:
11122eaa3e2dSViresh Kumar 	per_cpu(cpufreq_policy_cpu, cpu) = -1;
1113e9698cc5SSrivatsa S. Bhat 	cpufreq_policy_free(policy);
11141da177e4SLinus Torvalds nomem_out:
11151c3d85ddSRafael J. Wysocki 	module_put(cpufreq_driver->owner);
11161da177e4SLinus Torvalds module_out:
11171da177e4SLinus Torvalds 	return ret;
11181da177e4SLinus Torvalds }
11191da177e4SLinus Torvalds 
1120a82fab29SSrivatsa S. Bhat /**
1121a82fab29SSrivatsa S. Bhat  * cpufreq_add_dev - add a CPU device
1122a82fab29SSrivatsa S. Bhat  *
1123a82fab29SSrivatsa S. Bhat  * Adds the cpufreq interface for a CPU device.
1124a82fab29SSrivatsa S. Bhat  *
1125a82fab29SSrivatsa S. Bhat  * The Oracle says: try running cpufreq registration/unregistration concurrently
1126a82fab29SSrivatsa S. Bhat  * with with cpu hotplugging and all hell will break loose. Tried to clean this
1127a82fab29SSrivatsa S. Bhat  * mess up, but more thorough testing is needed. - Mathieu
1128a82fab29SSrivatsa S. Bhat  */
1129a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1130a82fab29SSrivatsa S. Bhat {
1131a82fab29SSrivatsa S. Bhat 	return __cpufreq_add_dev(dev, sif, false);
1132a82fab29SSrivatsa S. Bhat }
1133a82fab29SSrivatsa S. Bhat 
1134b8eed8afSViresh Kumar static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1135b8eed8afSViresh Kumar {
1136b8eed8afSViresh Kumar 	int j;
1137b8eed8afSViresh Kumar 
1138b8eed8afSViresh Kumar 	policy->last_cpu = policy->cpu;
1139b8eed8afSViresh Kumar 	policy->cpu = cpu;
1140b8eed8afSViresh Kumar 
11413361b7b1SViresh Kumar 	for_each_cpu(j, policy->cpus)
1142b8eed8afSViresh Kumar 		per_cpu(cpufreq_policy_cpu, j) = cpu;
1143b8eed8afSViresh Kumar 
1144b8eed8afSViresh Kumar #ifdef CONFIG_CPU_FREQ_TABLE
1145b8eed8afSViresh Kumar 	cpufreq_frequency_table_update_policy_cpu(policy);
1146b8eed8afSViresh Kumar #endif
1147b8eed8afSViresh Kumar 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1148b8eed8afSViresh Kumar 			CPUFREQ_UPDATE_POLICY_CPU, policy);
1149b8eed8afSViresh Kumar }
11501da177e4SLinus Torvalds 
11513a3e9e06SViresh Kumar static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
1152a82fab29SSrivatsa S. Bhat 					   unsigned int old_cpu, bool frozen)
1153f9ba680dSSrivatsa S. Bhat {
1154f9ba680dSSrivatsa S. Bhat 	struct device *cpu_dev;
1155f9ba680dSSrivatsa S. Bhat 	unsigned long flags;
1156f9ba680dSSrivatsa S. Bhat 	int ret;
1157f9ba680dSSrivatsa S. Bhat 
1158f9ba680dSSrivatsa S. Bhat 	/* first sibling now owns the new sysfs dir */
11593a3e9e06SViresh Kumar 	cpu_dev = get_cpu_device(cpumask_first(policy->cpus));
1160a82fab29SSrivatsa S. Bhat 
1161a82fab29SSrivatsa S. Bhat 	/* Don't touch sysfs files during light-weight tear-down */
1162a82fab29SSrivatsa S. Bhat 	if (frozen)
1163a82fab29SSrivatsa S. Bhat 		return cpu_dev->id;
1164a82fab29SSrivatsa S. Bhat 
1165f9ba680dSSrivatsa S. Bhat 	sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
11663a3e9e06SViresh Kumar 	ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1167f9ba680dSSrivatsa S. Bhat 	if (ret) {
1168f9ba680dSSrivatsa S. Bhat 		pr_err("%s: Failed to move kobj: %d", __func__, ret);
1169f9ba680dSSrivatsa S. Bhat 
1170f9ba680dSSrivatsa S. Bhat 		WARN_ON(lock_policy_rwsem_write(old_cpu));
11713a3e9e06SViresh Kumar 		cpumask_set_cpu(old_cpu, policy->cpus);
1172f9ba680dSSrivatsa S. Bhat 
1173f9ba680dSSrivatsa S. Bhat 		write_lock_irqsave(&cpufreq_driver_lock, flags);
11743a3e9e06SViresh Kumar 		per_cpu(cpufreq_cpu_data, old_cpu) = policy;
1175f9ba680dSSrivatsa S. Bhat 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1176f9ba680dSSrivatsa S. Bhat 
1177f9ba680dSSrivatsa S. Bhat 		unlock_policy_rwsem_write(old_cpu);
1178f9ba680dSSrivatsa S. Bhat 
11793a3e9e06SViresh Kumar 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1180f9ba680dSSrivatsa S. Bhat 					"cpufreq");
1181f9ba680dSSrivatsa S. Bhat 
1182f9ba680dSSrivatsa S. Bhat 		return -EINVAL;
1183f9ba680dSSrivatsa S. Bhat 	}
1184f9ba680dSSrivatsa S. Bhat 
1185f9ba680dSSrivatsa S. Bhat 	return cpu_dev->id;
1186f9ba680dSSrivatsa S. Bhat }
1187f9ba680dSSrivatsa S. Bhat 
11881da177e4SLinus Torvalds /**
11895a01f2e8SVenkatesh Pallipadi  * __cpufreq_remove_dev - remove a CPU device
11901da177e4SLinus Torvalds  *
11911da177e4SLinus Torvalds  * Removes the cpufreq interface for a CPU device.
11925a01f2e8SVenkatesh Pallipadi  * Caller should already have policy_rwsem in write mode for this CPU.
11935a01f2e8SVenkatesh Pallipadi  * This routine frees the rwsem before returning.
11941da177e4SLinus Torvalds  */
1195bb176f7dSViresh Kumar static int __cpufreq_remove_dev(struct device *dev,
1196a82fab29SSrivatsa S. Bhat 				struct subsys_interface *sif, bool frozen)
11971da177e4SLinus Torvalds {
1198f9ba680dSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
1199f9ba680dSSrivatsa S. Bhat 	int new_cpu;
12001da177e4SLinus Torvalds 	unsigned long flags;
12013a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
1202499bca9bSAmerigo Wang 	struct kobject *kobj;
1203499bca9bSAmerigo Wang 	struct completion *cmp;
12041da177e4SLinus Torvalds 
1205b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
12061da177e4SLinus Torvalds 
12070d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
12081da177e4SLinus Torvalds 
12093a3e9e06SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
12107a6aedfaSMike Travis 	per_cpu(cpufreq_cpu_data, cpu) = NULL;
12111da177e4SLinus Torvalds 
12128414809cSSrivatsa S. Bhat 	/* Save the policy somewhere when doing a light-weight tear-down */
12138414809cSSrivatsa S. Bhat 	if (frozen)
12143a3e9e06SViresh Kumar 		per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
12158414809cSSrivatsa S. Bhat 
12160d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
12171da177e4SLinus Torvalds 
12183a3e9e06SViresh Kumar 	if (!policy) {
1219b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
12201da177e4SLinus Torvalds 		return -EINVAL;
12211da177e4SLinus Torvalds 	}
12221da177e4SLinus Torvalds 
12231c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
12243a3e9e06SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
12255a01f2e8SVenkatesh Pallipadi 
12261da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
12271c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->setpolicy)
1228fa69e33fSDirk Brandewie 		strncpy(per_cpu(cpufreq_cpu_governor, cpu),
12293a3e9e06SViresh Kumar 			policy->governor->name, CPUFREQ_NAME_LEN);
12301da177e4SLinus Torvalds #endif
12311da177e4SLinus Torvalds 
12322eaa3e2dSViresh Kumar 	WARN_ON(lock_policy_rwsem_write(cpu));
12333a3e9e06SViresh Kumar 	cpus = cpumask_weight(policy->cpus);
1234e4969ebaSViresh Kumar 
1235e4969ebaSViresh Kumar 	if (cpus > 1)
12363a3e9e06SViresh Kumar 		cpumask_clear_cpu(cpu, policy->cpus);
12372eaa3e2dSViresh Kumar 	unlock_policy_rwsem_write(cpu);
12381da177e4SLinus Torvalds 
12393a3e9e06SViresh Kumar 	if (cpu != policy->cpu && !frozen) {
124073bf0fc2SViresh Kumar 		sysfs_remove_link(&dev->kobj, "cpufreq");
124173bf0fc2SViresh Kumar 	} else if (cpus > 1) {
12422eaa3e2dSViresh Kumar 
12433a3e9e06SViresh Kumar 		new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
1244f9ba680dSSrivatsa S. Bhat 		if (new_cpu >= 0) {
12452eaa3e2dSViresh Kumar 			WARN_ON(lock_policy_rwsem_write(cpu));
12463a3e9e06SViresh Kumar 			update_policy_cpu(policy, new_cpu);
12472eaa3e2dSViresh Kumar 			unlock_policy_rwsem_write(cpu);
1248a82fab29SSrivatsa S. Bhat 
1249a82fab29SSrivatsa S. Bhat 			if (!frozen) {
1250f9ba680dSSrivatsa S. Bhat 				pr_debug("%s: policy Kobject moved to cpu: %d "
1251f9ba680dSSrivatsa S. Bhat 					 "from: %d\n",__func__, new_cpu, cpu);
12521da177e4SLinus Torvalds 			}
12531da177e4SLinus Torvalds 		}
1254a82fab29SSrivatsa S. Bhat 	}
1255b8eed8afSViresh Kumar 
1256b8eed8afSViresh Kumar 	/* If cpu is last user of policy, free policy */
1257b8eed8afSViresh Kumar 	if (cpus == 1) {
12582a998599SRafael J. Wysocki 		if (cpufreq_driver->target)
12593a3e9e06SViresh Kumar 			__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
12602a998599SRafael J. Wysocki 
12618414809cSSrivatsa S. Bhat 		if (!frozen) {
12622eaa3e2dSViresh Kumar 			lock_policy_rwsem_read(cpu);
12633a3e9e06SViresh Kumar 			kobj = &policy->kobj;
12643a3e9e06SViresh Kumar 			cmp = &policy->kobj_unregister;
12652eaa3e2dSViresh Kumar 			unlock_policy_rwsem_read(cpu);
1266499bca9bSAmerigo Wang 			kobject_put(kobj);
12671da177e4SLinus Torvalds 
12688414809cSSrivatsa S. Bhat 			/*
12698414809cSSrivatsa S. Bhat 			 * We need to make sure that the underlying kobj is
12708414809cSSrivatsa S. Bhat 			 * actually not referenced anymore by anybody before we
12718414809cSSrivatsa S. Bhat 			 * proceed with unloading.
12721da177e4SLinus Torvalds 			 */
12732d06d8c4SDominik Brodowski 			pr_debug("waiting for dropping of refcount\n");
1274499bca9bSAmerigo Wang 			wait_for_completion(cmp);
12752d06d8c4SDominik Brodowski 			pr_debug("wait complete\n");
12768414809cSSrivatsa S. Bhat 		}
12771da177e4SLinus Torvalds 
12788414809cSSrivatsa S. Bhat 		/*
12798414809cSSrivatsa S. Bhat 		 * Perform the ->exit() even during light-weight tear-down,
12808414809cSSrivatsa S. Bhat 		 * since this is a core component, and is essential for the
12818414809cSSrivatsa S. Bhat 		 * subsequent light-weight ->init() to succeed.
12828414809cSSrivatsa S. Bhat 		 */
12831c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
12843a3e9e06SViresh Kumar 			cpufreq_driver->exit(policy);
128527ecddc2SJacob Shin 
12868414809cSSrivatsa S. Bhat 		if (!frozen)
12873a3e9e06SViresh Kumar 			cpufreq_policy_free(policy);
12882a998599SRafael J. Wysocki 	} else {
12892a998599SRafael J. Wysocki 		if (cpufreq_driver->target) {
12903a3e9e06SViresh Kumar 			__cpufreq_governor(policy, CPUFREQ_GOV_START);
12913a3e9e06SViresh Kumar 			__cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1292b8eed8afSViresh Kumar 		}
12932a998599SRafael J. Wysocki 	}
12941da177e4SLinus Torvalds 
12952eaa3e2dSViresh Kumar 	per_cpu(cpufreq_policy_cpu, cpu) = -1;
12961da177e4SLinus Torvalds 	return 0;
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds 
12998a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
13005a01f2e8SVenkatesh Pallipadi {
13018a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
13025a01f2e8SVenkatesh Pallipadi 	int retval;
1303ec28297aSVenki Pallipadi 
1304ec28297aSVenki Pallipadi 	if (cpu_is_offline(cpu))
1305ec28297aSVenki Pallipadi 		return 0;
1306ec28297aSVenki Pallipadi 
1307a82fab29SSrivatsa S. Bhat 	retval = __cpufreq_remove_dev(dev, sif, false);
13085a01f2e8SVenkatesh Pallipadi 	return retval;
13095a01f2e8SVenkatesh Pallipadi }
13105a01f2e8SVenkatesh Pallipadi 
131165f27f38SDavid Howells static void handle_update(struct work_struct *work)
13121da177e4SLinus Torvalds {
131365f27f38SDavid Howells 	struct cpufreq_policy *policy =
131465f27f38SDavid Howells 		container_of(work, struct cpufreq_policy, update);
131565f27f38SDavid Howells 	unsigned int cpu = policy->cpu;
13162d06d8c4SDominik Brodowski 	pr_debug("handle_update for cpu %u called\n", cpu);
13171da177e4SLinus Torvalds 	cpufreq_update_policy(cpu);
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds /**
1321bb176f7dSViresh Kumar  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1322bb176f7dSViresh Kumar  *	in deep trouble.
13231da177e4SLinus Torvalds  *	@cpu: cpu number
13241da177e4SLinus Torvalds  *	@old_freq: CPU frequency the kernel thinks the CPU runs at
13251da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
13261da177e4SLinus Torvalds  *
132729464f28SDave Jones  *	We adjust to current frequency first, and need to clean up later.
132829464f28SDave Jones  *	So either call to cpufreq_update_policy() or schedule handle_update()).
13291da177e4SLinus Torvalds  */
1330e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1331e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
13321da177e4SLinus Torvalds {
1333b43a7ffbSViresh Kumar 	struct cpufreq_policy *policy;
13341da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1335b43a7ffbSViresh Kumar 	unsigned long flags;
1336b43a7ffbSViresh Kumar 
13372d06d8c4SDominik Brodowski 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
13381da177e4SLinus Torvalds 	       "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	freqs.old = old_freq;
13411da177e4SLinus Torvalds 	freqs.new = new_freq;
1342b43a7ffbSViresh Kumar 
1343b43a7ffbSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1344b43a7ffbSViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
1345b43a7ffbSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1346b43a7ffbSViresh Kumar 
1347b43a7ffbSViresh Kumar 	cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1348b43a7ffbSViresh Kumar 	cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
13491da177e4SLinus Torvalds }
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds /**
13524ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
135395235ca2SVenkatesh Pallipadi  * @cpu: CPU number
135495235ca2SVenkatesh Pallipadi  *
135595235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
135695235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
135795235ca2SVenkatesh Pallipadi  */
135895235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
135995235ca2SVenkatesh Pallipadi {
13609e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1361e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
136295235ca2SVenkatesh Pallipadi 
13631c3d85ddSRafael J. Wysocki 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
13641c3d85ddSRafael J. Wysocki 		return cpufreq_driver->get(cpu);
13659e21ba8bSDirk Brandewie 
13669e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
136795235ca2SVenkatesh Pallipadi 	if (policy) {
1368e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
136995235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
137095235ca2SVenkatesh Pallipadi 	}
137195235ca2SVenkatesh Pallipadi 
13724d34a67dSDave Jones 	return ret_freq;
137395235ca2SVenkatesh Pallipadi }
137495235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
137595235ca2SVenkatesh Pallipadi 
13763d737108SJesse Barnes /**
13773d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
13783d737108SJesse Barnes  * @cpu: CPU number
13793d737108SJesse Barnes  *
13803d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
13813d737108SJesse Barnes  */
13823d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
13833d737108SJesse Barnes {
13843d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
13853d737108SJesse Barnes 	unsigned int ret_freq = 0;
13863d737108SJesse Barnes 
13873d737108SJesse Barnes 	if (policy) {
13883d737108SJesse Barnes 		ret_freq = policy->max;
13893d737108SJesse Barnes 		cpufreq_cpu_put(policy);
13903d737108SJesse Barnes 	}
13913d737108SJesse Barnes 
13923d737108SJesse Barnes 	return ret_freq;
13933d737108SJesse Barnes }
13943d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
13953d737108SJesse Barnes 
13965a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu)
13971da177e4SLinus Torvalds {
13987a6aedfaSMike Travis 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1399e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
14001da177e4SLinus Torvalds 
14011c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->get)
14024d34a67dSDave Jones 		return ret_freq;
14031da177e4SLinus Torvalds 
14041c3d85ddSRafael J. Wysocki 	ret_freq = cpufreq_driver->get(cpu);
14051da177e4SLinus Torvalds 
1406e08f5f5bSGautham R Shenoy 	if (ret_freq && policy->cur &&
14071c3d85ddSRafael J. Wysocki 		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1408e08f5f5bSGautham R Shenoy 		/* verify no discrepancy between actual and
1409e08f5f5bSGautham R Shenoy 					saved value exists */
1410e08f5f5bSGautham R Shenoy 		if (unlikely(ret_freq != policy->cur)) {
1411e08f5f5bSGautham R Shenoy 			cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
14121da177e4SLinus Torvalds 			schedule_work(&policy->update);
14131da177e4SLinus Torvalds 		}
14141da177e4SLinus Torvalds 	}
14151da177e4SLinus Torvalds 
14164d34a67dSDave Jones 	return ret_freq;
14175a01f2e8SVenkatesh Pallipadi }
14181da177e4SLinus Torvalds 
14195a01f2e8SVenkatesh Pallipadi /**
14205a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
14215a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
14225a01f2e8SVenkatesh Pallipadi  *
14235a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
14245a01f2e8SVenkatesh Pallipadi  */
14255a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
14265a01f2e8SVenkatesh Pallipadi {
14275a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
14285a01f2e8SVenkatesh Pallipadi 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
14295a01f2e8SVenkatesh Pallipadi 
14305a01f2e8SVenkatesh Pallipadi 	if (!policy)
14315a01f2e8SVenkatesh Pallipadi 		goto out;
14325a01f2e8SVenkatesh Pallipadi 
14335a01f2e8SVenkatesh Pallipadi 	if (unlikely(lock_policy_rwsem_read(cpu)))
14345a01f2e8SVenkatesh Pallipadi 		goto out_policy;
14355a01f2e8SVenkatesh Pallipadi 
14365a01f2e8SVenkatesh Pallipadi 	ret_freq = __cpufreq_get(cpu);
14375a01f2e8SVenkatesh Pallipadi 
14385a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_read(cpu);
14395a01f2e8SVenkatesh Pallipadi 
14405a01f2e8SVenkatesh Pallipadi out_policy:
14411da177e4SLinus Torvalds 	cpufreq_cpu_put(policy);
14425a01f2e8SVenkatesh Pallipadi out:
14434d34a67dSDave Jones 	return ret_freq;
14441da177e4SLinus Torvalds }
14451da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
14461da177e4SLinus Torvalds 
14478a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
14488a25a2fdSKay Sievers 	.name		= "cpufreq",
14498a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
14508a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
14518a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1452e00e56dfSRafael J. Wysocki };
1453e00e56dfSRafael J. Wysocki 
14541da177e4SLinus Torvalds /**
1455e00e56dfSRafael J. Wysocki  * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1456e00e56dfSRafael J. Wysocki  *
1457e00e56dfSRafael J. Wysocki  * This function is only executed for the boot processor.  The other CPUs
1458e00e56dfSRafael J. Wysocki  * have been put offline by means of CPU hotplug.
145942d4dc3fSBenjamin Herrenschmidt  */
1460e00e56dfSRafael J. Wysocki static int cpufreq_bp_suspend(void)
146142d4dc3fSBenjamin Herrenschmidt {
1462e08f5f5bSGautham R Shenoy 	int ret = 0;
14634bc5d341SDave Jones 
1464e00e56dfSRafael J. Wysocki 	int cpu = smp_processor_id();
14653a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
146642d4dc3fSBenjamin Herrenschmidt 
14672d06d8c4SDominik Brodowski 	pr_debug("suspending cpu %u\n", cpu);
146842d4dc3fSBenjamin Herrenschmidt 
1469e00e56dfSRafael J. Wysocki 	/* If there's no policy for the boot CPU, we have nothing to do. */
14703a3e9e06SViresh Kumar 	policy = cpufreq_cpu_get(cpu);
14713a3e9e06SViresh Kumar 	if (!policy)
1472e00e56dfSRafael J. Wysocki 		return 0;
147342d4dc3fSBenjamin Herrenschmidt 
14741c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->suspend) {
14753a3e9e06SViresh Kumar 		ret = cpufreq_driver->suspend(policy);
1476ce6c3997SDominik Brodowski 		if (ret)
147742d4dc3fSBenjamin Herrenschmidt 			printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
14783a3e9e06SViresh Kumar 					"step on CPU %u\n", policy->cpu);
147942d4dc3fSBenjamin Herrenschmidt 	}
148042d4dc3fSBenjamin Herrenschmidt 
14813a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
1482c9060494SDave Jones 	return ret;
148342d4dc3fSBenjamin Herrenschmidt }
148442d4dc3fSBenjamin Herrenschmidt 
148542d4dc3fSBenjamin Herrenschmidt /**
1486e00e56dfSRafael J. Wysocki  * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
14871da177e4SLinus Torvalds  *
14881da177e4SLinus Torvalds  *	1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1489ce6c3997SDominik Brodowski  *	2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1490ce6c3997SDominik Brodowski  *	    restored. It will verify that the current freq is in sync with
1491ce6c3997SDominik Brodowski  *	    what we believe it to be. This is a bit later than when it
1492ce6c3997SDominik Brodowski  *	    should be, but nonethteless it's better than calling
1493ce6c3997SDominik Brodowski  *	    cpufreq_driver->get() here which might re-enable interrupts...
1494e00e56dfSRafael J. Wysocki  *
1495e00e56dfSRafael J. Wysocki  * This function is only executed for the boot CPU.  The other CPUs have not
1496e00e56dfSRafael J. Wysocki  * been turned on yet.
14971da177e4SLinus Torvalds  */
1498e00e56dfSRafael J. Wysocki static void cpufreq_bp_resume(void)
14991da177e4SLinus Torvalds {
1500e08f5f5bSGautham R Shenoy 	int ret = 0;
15014bc5d341SDave Jones 
1502e00e56dfSRafael J. Wysocki 	int cpu = smp_processor_id();
15033a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
15041da177e4SLinus Torvalds 
15052d06d8c4SDominik Brodowski 	pr_debug("resuming cpu %u\n", cpu);
15061da177e4SLinus Torvalds 
1507e00e56dfSRafael J. Wysocki 	/* If there's no policy for the boot CPU, we have nothing to do. */
15083a3e9e06SViresh Kumar 	policy = cpufreq_cpu_get(cpu);
15093a3e9e06SViresh Kumar 	if (!policy)
1510e00e56dfSRafael J. Wysocki 		return;
15111da177e4SLinus Torvalds 
15121c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->resume) {
15133a3e9e06SViresh Kumar 		ret = cpufreq_driver->resume(policy);
15141da177e4SLinus Torvalds 		if (ret) {
15151da177e4SLinus Torvalds 			printk(KERN_ERR "cpufreq: resume failed in ->resume "
15163a3e9e06SViresh Kumar 					"step on CPU %u\n", policy->cpu);
1517c9060494SDave Jones 			goto fail;
15181da177e4SLinus Torvalds 		}
15191da177e4SLinus Torvalds 	}
15201da177e4SLinus Torvalds 
15213a3e9e06SViresh Kumar 	schedule_work(&policy->update);
1522ce6c3997SDominik Brodowski 
1523c9060494SDave Jones fail:
15243a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
15251da177e4SLinus Torvalds }
15261da177e4SLinus Torvalds 
1527e00e56dfSRafael J. Wysocki static struct syscore_ops cpufreq_syscore_ops = {
1528e00e56dfSRafael J. Wysocki 	.suspend	= cpufreq_bp_suspend,
1529e00e56dfSRafael J. Wysocki 	.resume		= cpufreq_bp_resume,
15301da177e4SLinus Torvalds };
15311da177e4SLinus Torvalds 
15329d95046eSBorislav Petkov /**
15339d95046eSBorislav Petkov  *	cpufreq_get_current_driver - return current driver's name
15349d95046eSBorislav Petkov  *
15359d95046eSBorislav Petkov  *	Return the name string of the currently loaded cpufreq driver
15369d95046eSBorislav Petkov  *	or NULL, if none.
15379d95046eSBorislav Petkov  */
15389d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
15399d95046eSBorislav Petkov {
15401c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
15411c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
15421c3d85ddSRafael J. Wysocki 
15431c3d85ddSRafael J. Wysocki 	return NULL;
15449d95046eSBorislav Petkov }
15459d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds /*********************************************************************
15481da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
15491da177e4SLinus Torvalds  *********************************************************************/
15501da177e4SLinus Torvalds 
15511da177e4SLinus Torvalds /**
15521da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
15531da177e4SLinus Torvalds  *	@nb: notifier function to register
15541da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
15551da177e4SLinus Torvalds  *
15561da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
15571da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
15581da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
15591da177e4SLinus Torvalds  *      changes in cpufreq policy.
15601da177e4SLinus Torvalds  *
15611da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1562e041c683SAlan Stern  *	blocking_notifier_chain_register.
15631da177e4SLinus Torvalds  */
15641da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
15651da177e4SLinus Torvalds {
15661da177e4SLinus Torvalds 	int ret;
15671da177e4SLinus Torvalds 
1568d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1569d5aaffa9SDirk Brandewie 		return -EINVAL;
1570d5aaffa9SDirk Brandewie 
157174212ca4SCesar Eduardo Barros 	WARN_ON(!init_cpufreq_transition_notifier_list_called);
157274212ca4SCesar Eduardo Barros 
15731da177e4SLinus Torvalds 	switch (list) {
15741da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1575b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1576e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
15771da177e4SLinus Torvalds 		break;
15781da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1579e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1580e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
15811da177e4SLinus Torvalds 		break;
15821da177e4SLinus Torvalds 	default:
15831da177e4SLinus Torvalds 		ret = -EINVAL;
15841da177e4SLinus Torvalds 	}
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	return ret;
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
15891da177e4SLinus Torvalds 
15901da177e4SLinus Torvalds /**
15911da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
15921da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
15931da177e4SLinus Torvalds  *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
15941da177e4SLinus Torvalds  *
15951da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
15961da177e4SLinus Torvalds  *
15971da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1598e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
15991da177e4SLinus Torvalds  */
16001da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
16011da177e4SLinus Torvalds {
16021da177e4SLinus Torvalds 	int ret;
16031da177e4SLinus Torvalds 
1604d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1605d5aaffa9SDirk Brandewie 		return -EINVAL;
1606d5aaffa9SDirk Brandewie 
16071da177e4SLinus Torvalds 	switch (list) {
16081da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1609b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
1610e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
16111da177e4SLinus Torvalds 		break;
16121da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1613e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1614e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
16151da177e4SLinus Torvalds 		break;
16161da177e4SLinus Torvalds 	default:
16171da177e4SLinus Torvalds 		ret = -EINVAL;
16181da177e4SLinus Torvalds 	}
16191da177e4SLinus Torvalds 
16201da177e4SLinus Torvalds 	return ret;
16211da177e4SLinus Torvalds }
16221da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds 
16251da177e4SLinus Torvalds /*********************************************************************
16261da177e4SLinus Torvalds  *                              GOVERNORS                            *
16271da177e4SLinus Torvalds  *********************************************************************/
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
16301da177e4SLinus Torvalds 			    unsigned int target_freq,
16311da177e4SLinus Torvalds 			    unsigned int relation)
16321da177e4SLinus Torvalds {
16331da177e4SLinus Torvalds 	int retval = -EINVAL;
16347249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
1635c32b6b8eSAshok Raj 
1636a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1637a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
16387c30ed53SViresh Kumar 	if (policy->transition_ongoing)
16397c30ed53SViresh Kumar 		return -EBUSY;
1640a7b422cdSKonrad Rzeszutek Wilk 
16417249924eSViresh Kumar 	/* Make sure that target_freq is within supported range */
16427249924eSViresh Kumar 	if (target_freq > policy->max)
16437249924eSViresh Kumar 		target_freq = policy->max;
16447249924eSViresh Kumar 	if (target_freq < policy->min)
16457249924eSViresh Kumar 		target_freq = policy->min;
16467249924eSViresh Kumar 
16477249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
16487249924eSViresh Kumar 			policy->cpu, target_freq, relation, old_target_freq);
16495a1c0228SViresh Kumar 
16505a1c0228SViresh Kumar 	if (target_freq == policy->cur)
16515a1c0228SViresh Kumar 		return 0;
16525a1c0228SViresh Kumar 
16531c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
16541c3d85ddSRafael J. Wysocki 		retval = cpufreq_driver->target(policy, target_freq, relation);
165590d45d17SAshok Raj 
16561da177e4SLinus Torvalds 	return retval;
16571da177e4SLinus Torvalds }
16581da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
16591da177e4SLinus Torvalds 
16601da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
16611da177e4SLinus Torvalds 			  unsigned int target_freq,
16621da177e4SLinus Torvalds 			  unsigned int relation)
16631da177e4SLinus Torvalds {
1664f1829e4aSJulia Lawall 	int ret = -EINVAL;
16651da177e4SLinus Torvalds 
16665a01f2e8SVenkatesh Pallipadi 	if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1667f1829e4aSJulia Lawall 		goto fail;
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
16701da177e4SLinus Torvalds 
16715a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(policy->cpu);
16721da177e4SLinus Torvalds 
1673f1829e4aSJulia Lawall fail:
16741da177e4SLinus Torvalds 	return ret;
16751da177e4SLinus Torvalds }
16761da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
16771da177e4SLinus Torvalds 
1678153d7f3fSArjan van de Ven /*
1679153d7f3fSArjan van de Ven  * when "event" is CPUFREQ_GOV_LIMITS
1680153d7f3fSArjan van de Ven  */
16811da177e4SLinus Torvalds 
1682e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy,
1683e08f5f5bSGautham R Shenoy 					unsigned int event)
16841da177e4SLinus Torvalds {
1685cc993cabSDave Jones 	int ret;
16866afde10cSThomas Renninger 
16876afde10cSThomas Renninger 	/* Only must be defined when default governor is known to have latency
16886afde10cSThomas Renninger 	   restrictions, like e.g. conservative or ondemand.
16896afde10cSThomas Renninger 	   That this is the case is already ensured in Kconfig
16906afde10cSThomas Renninger 	*/
16916afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
16926afde10cSThomas Renninger 	struct cpufreq_governor *gov = &cpufreq_gov_performance;
16936afde10cSThomas Renninger #else
16946afde10cSThomas Renninger 	struct cpufreq_governor *gov = NULL;
16956afde10cSThomas Renninger #endif
16961c256245SThomas Renninger 
16971c256245SThomas Renninger 	if (policy->governor->max_transition_latency &&
16981c256245SThomas Renninger 	    policy->cpuinfo.transition_latency >
16991c256245SThomas Renninger 	    policy->governor->max_transition_latency) {
17006afde10cSThomas Renninger 		if (!gov)
17016afde10cSThomas Renninger 			return -EINVAL;
17026afde10cSThomas Renninger 		else {
17031c256245SThomas Renninger 			printk(KERN_WARNING "%s governor failed, too long"
17041c256245SThomas Renninger 			       " transition latency of HW, fallback"
17051c256245SThomas Renninger 			       " to %s governor\n",
17061c256245SThomas Renninger 			       policy->governor->name,
17071c256245SThomas Renninger 			       gov->name);
17081c256245SThomas Renninger 			policy->governor = gov;
17091c256245SThomas Renninger 		}
17106afde10cSThomas Renninger 	}
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	if (!try_module_get(policy->governor->owner))
17131da177e4SLinus Torvalds 		return -EINVAL;
17141da177e4SLinus Torvalds 
17152d06d8c4SDominik Brodowski 	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1716e08f5f5bSGautham R Shenoy 						policy->cpu, event);
171795731ebbSXiaoguang Chen 
171895731ebbSXiaoguang Chen 	mutex_lock(&cpufreq_governor_lock);
171995731ebbSXiaoguang Chen 	if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
172095731ebbSXiaoguang Chen 	    (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
172195731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
172295731ebbSXiaoguang Chen 		return -EBUSY;
172395731ebbSXiaoguang Chen 	}
172495731ebbSXiaoguang Chen 
172595731ebbSXiaoguang Chen 	if (event == CPUFREQ_GOV_STOP)
172695731ebbSXiaoguang Chen 		policy->governor_enabled = false;
172795731ebbSXiaoguang Chen 	else if (event == CPUFREQ_GOV_START)
172895731ebbSXiaoguang Chen 		policy->governor_enabled = true;
172995731ebbSXiaoguang Chen 
173095731ebbSXiaoguang Chen 	mutex_unlock(&cpufreq_governor_lock);
173195731ebbSXiaoguang Chen 
17321da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
17331da177e4SLinus Torvalds 
17344d5dcc42SViresh Kumar 	if (!ret) {
17354d5dcc42SViresh Kumar 		if (event == CPUFREQ_GOV_POLICY_INIT)
17368e53695fSViresh Kumar 			policy->governor->initialized++;
17374d5dcc42SViresh Kumar 		else if (event == CPUFREQ_GOV_POLICY_EXIT)
17388e53695fSViresh Kumar 			policy->governor->initialized--;
173995731ebbSXiaoguang Chen 	} else {
174095731ebbSXiaoguang Chen 		/* Restore original values */
174195731ebbSXiaoguang Chen 		mutex_lock(&cpufreq_governor_lock);
174295731ebbSXiaoguang Chen 		if (event == CPUFREQ_GOV_STOP)
174395731ebbSXiaoguang Chen 			policy->governor_enabled = true;
174495731ebbSXiaoguang Chen 		else if (event == CPUFREQ_GOV_START)
174595731ebbSXiaoguang Chen 			policy->governor_enabled = false;
174695731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
17474d5dcc42SViresh Kumar 	}
1748b394058fSViresh Kumar 
1749e08f5f5bSGautham R Shenoy 	/* we keep one module reference alive for
1750e08f5f5bSGautham R Shenoy 			each CPU governed by this CPU */
17511da177e4SLinus Torvalds 	if ((event != CPUFREQ_GOV_START) || ret)
17521da177e4SLinus Torvalds 		module_put(policy->governor->owner);
17531da177e4SLinus Torvalds 	if ((event == CPUFREQ_GOV_STOP) && !ret)
17541da177e4SLinus Torvalds 		module_put(policy->governor->owner);
17551da177e4SLinus Torvalds 
17561da177e4SLinus Torvalds 	return ret;
17571da177e4SLinus Torvalds }
17581da177e4SLinus Torvalds 
17591da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
17601da177e4SLinus Torvalds {
17613bcb09a3SJeremy Fitzhardinge 	int err;
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds 	if (!governor)
17641da177e4SLinus Torvalds 		return -EINVAL;
17651da177e4SLinus Torvalds 
1766a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1767a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1768a7b422cdSKonrad Rzeszutek Wilk 
17693fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
17701da177e4SLinus Torvalds 
1771b394058fSViresh Kumar 	governor->initialized = 0;
17723bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
17733bcb09a3SJeremy Fitzhardinge 	if (__find_governor(governor->name) == NULL) {
17743bcb09a3SJeremy Fitzhardinge 		err = 0;
17751da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
17763bcb09a3SJeremy Fitzhardinge 	}
17771da177e4SLinus Torvalds 
17783fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
17793bcb09a3SJeremy Fitzhardinge 	return err;
17801da177e4SLinus Torvalds }
17811da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
17841da177e4SLinus Torvalds {
178590e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
178690e41bacSPrarit Bhargava 	int cpu;
178790e41bacSPrarit Bhargava #endif
178890e41bacSPrarit Bhargava 
17891da177e4SLinus Torvalds 	if (!governor)
17901da177e4SLinus Torvalds 		return;
17911da177e4SLinus Torvalds 
1792a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1793a7b422cdSKonrad Rzeszutek Wilk 		return;
1794a7b422cdSKonrad Rzeszutek Wilk 
179590e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
179690e41bacSPrarit Bhargava 	for_each_present_cpu(cpu) {
179790e41bacSPrarit Bhargava 		if (cpu_online(cpu))
179890e41bacSPrarit Bhargava 			continue;
179990e41bacSPrarit Bhargava 		if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
180090e41bacSPrarit Bhargava 			strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
180190e41bacSPrarit Bhargava 	}
180290e41bacSPrarit Bhargava #endif
180390e41bacSPrarit Bhargava 
18043fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
18051da177e4SLinus Torvalds 	list_del(&governor->governor_list);
18063fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
18071da177e4SLinus Torvalds 	return;
18081da177e4SLinus Torvalds }
18091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds /*********************************************************************
18131da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
18141da177e4SLinus Torvalds  *********************************************************************/
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds /**
18171da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
181829464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
181929464f28SDave Jones  *	is written
18201da177e4SLinus Torvalds  *
18211da177e4SLinus Torvalds  * Reads the current cpufreq policy.
18221da177e4SLinus Torvalds  */
18231da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
18241da177e4SLinus Torvalds {
18251da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
18261da177e4SLinus Torvalds 	if (!policy)
18271da177e4SLinus Torvalds 		return -EINVAL;
18281da177e4SLinus Torvalds 
18291da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
18301da177e4SLinus Torvalds 	if (!cpu_policy)
18311da177e4SLinus Torvalds 		return -EINVAL;
18321da177e4SLinus Torvalds 
1833d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
18361da177e4SLinus Torvalds 	return 0;
18371da177e4SLinus Torvalds }
18381da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
18391da177e4SLinus Torvalds 
1840153d7f3fSArjan van de Ven /*
1841e08f5f5bSGautham R Shenoy  * data   : current policy.
1842e08f5f5bSGautham R Shenoy  * policy : policy to be set.
1843153d7f3fSArjan van de Ven  */
18443a3e9e06SViresh Kumar static int __cpufreq_set_policy(struct cpufreq_policy *policy,
18453a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy)
18461da177e4SLinus Torvalds {
18477bd353a9SViresh Kumar 	int ret = 0, failed = 1;
18481da177e4SLinus Torvalds 
18493a3e9e06SViresh Kumar 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
18503a3e9e06SViresh Kumar 		new_policy->min, new_policy->max);
18511da177e4SLinus Torvalds 
1852d5b73cd8SViresh Kumar 	memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
18531da177e4SLinus Torvalds 
18543a3e9e06SViresh Kumar 	if (new_policy->min > policy->max || new_policy->max < policy->min) {
18559c9a43edSMattia Dongili 		ret = -EINVAL;
18569c9a43edSMattia Dongili 		goto error_out;
18579c9a43edSMattia Dongili 	}
18589c9a43edSMattia Dongili 
18591da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
18603a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
18611da177e4SLinus Torvalds 	if (ret)
18621da177e4SLinus Torvalds 		goto error_out;
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
1865e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
18663a3e9e06SViresh Kumar 			CPUFREQ_ADJUST, new_policy);
18671da177e4SLinus Torvalds 
18681da177e4SLinus Torvalds 	/* adjust if necessary - hardware incompatibility*/
1869e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
18703a3e9e06SViresh Kumar 			CPUFREQ_INCOMPATIBLE, new_policy);
18711da177e4SLinus Torvalds 
1872bb176f7dSViresh Kumar 	/*
1873bb176f7dSViresh Kumar 	 * verify the cpu speed can be set within this limit, which might be
1874bb176f7dSViresh Kumar 	 * different to the first one
1875bb176f7dSViresh Kumar 	 */
18763a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
1877e041c683SAlan Stern 	if (ret)
18781da177e4SLinus Torvalds 		goto error_out;
18791da177e4SLinus Torvalds 
18801da177e4SLinus Torvalds 	/* notification of the new policy */
1881e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
18823a3e9e06SViresh Kumar 			CPUFREQ_NOTIFY, new_policy);
18831da177e4SLinus Torvalds 
18843a3e9e06SViresh Kumar 	policy->min = new_policy->min;
18853a3e9e06SViresh Kumar 	policy->max = new_policy->max;
18861da177e4SLinus Torvalds 
18872d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
18883a3e9e06SViresh Kumar 					policy->min, policy->max);
18891da177e4SLinus Torvalds 
18901c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
18913a3e9e06SViresh Kumar 		policy->policy = new_policy->policy;
18922d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
18933a3e9e06SViresh Kumar 		ret = cpufreq_driver->setpolicy(new_policy);
18941da177e4SLinus Torvalds 	} else {
18953a3e9e06SViresh Kumar 		if (new_policy->governor != policy->governor) {
18961da177e4SLinus Torvalds 			/* save old, working values */
18973a3e9e06SViresh Kumar 			struct cpufreq_governor *old_gov = policy->governor;
18981da177e4SLinus Torvalds 
18992d06d8c4SDominik Brodowski 			pr_debug("governor switch\n");
19001da177e4SLinus Torvalds 
19011da177e4SLinus Torvalds 			/* end old governor */
19023a3e9e06SViresh Kumar 			if (policy->governor) {
19033a3e9e06SViresh Kumar 				__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
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);
19087bd353a9SViresh Kumar 			}
19091da177e4SLinus Torvalds 
19101da177e4SLinus Torvalds 			/* start new governor */
19113a3e9e06SViresh Kumar 			policy->governor = new_policy->governor;
19123a3e9e06SViresh Kumar 			if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
19133a3e9e06SViresh Kumar 				if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
19147bd353a9SViresh Kumar 					failed = 0;
1915955ef483SViresh Kumar 				} else {
19163a3e9e06SViresh Kumar 					unlock_policy_rwsem_write(new_policy->cpu);
19173a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
19187bd353a9SViresh Kumar 							CPUFREQ_GOV_POLICY_EXIT);
19193a3e9e06SViresh Kumar 					lock_policy_rwsem_write(new_policy->cpu);
1920955ef483SViresh Kumar 				}
19217bd353a9SViresh Kumar 			}
19227bd353a9SViresh Kumar 
19237bd353a9SViresh Kumar 			if (failed) {
19241da177e4SLinus Torvalds 				/* new governor failed, so re-start old one */
19252d06d8c4SDominik Brodowski 				pr_debug("starting governor %s failed\n",
19263a3e9e06SViresh Kumar 							policy->governor->name);
19271da177e4SLinus Torvalds 				if (old_gov) {
19283a3e9e06SViresh Kumar 					policy->governor = old_gov;
19293a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
19307bd353a9SViresh Kumar 							CPUFREQ_GOV_POLICY_INIT);
19313a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
1932e08f5f5bSGautham R Shenoy 							   CPUFREQ_GOV_START);
19331da177e4SLinus Torvalds 				}
19341da177e4SLinus Torvalds 				ret = -EINVAL;
19351da177e4SLinus Torvalds 				goto error_out;
19361da177e4SLinus Torvalds 			}
19371da177e4SLinus Torvalds 			/* might be a policy change, too, so fall through */
19381da177e4SLinus Torvalds 		}
19392d06d8c4SDominik Brodowski 		pr_debug("governor: change or update limits\n");
19403a3e9e06SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
19411da177e4SLinus Torvalds 	}
19421da177e4SLinus Torvalds 
19431da177e4SLinus Torvalds error_out:
19441da177e4SLinus Torvalds 	return ret;
19451da177e4SLinus Torvalds }
19461da177e4SLinus Torvalds 
19471da177e4SLinus Torvalds /**
19481da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
19491da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
19501da177e4SLinus Torvalds  *
195125985edcSLucas De Marchi  *	Useful for policy notifiers which have different necessities
19521da177e4SLinus Torvalds  *	at different times.
19531da177e4SLinus Torvalds  */
19541da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
19551da177e4SLinus Torvalds {
19563a3e9e06SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
19573a3e9e06SViresh Kumar 	struct cpufreq_policy new_policy;
1958f1829e4aSJulia Lawall 	int ret;
19591da177e4SLinus Torvalds 
19603a3e9e06SViresh Kumar 	if (!policy) {
1961f1829e4aSJulia Lawall 		ret = -ENODEV;
1962f1829e4aSJulia Lawall 		goto no_policy;
1963f1829e4aSJulia Lawall 	}
19641da177e4SLinus Torvalds 
1965f1829e4aSJulia Lawall 	if (unlikely(lock_policy_rwsem_write(cpu))) {
1966f1829e4aSJulia Lawall 		ret = -EINVAL;
1967f1829e4aSJulia Lawall 		goto fail;
1968f1829e4aSJulia Lawall 	}
19691da177e4SLinus Torvalds 
19702d06d8c4SDominik Brodowski 	pr_debug("updating policy for CPU %u\n", cpu);
1971d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
19723a3e9e06SViresh Kumar 	new_policy.min = policy->user_policy.min;
19733a3e9e06SViresh Kumar 	new_policy.max = policy->user_policy.max;
19743a3e9e06SViresh Kumar 	new_policy.policy = policy->user_policy.policy;
19753a3e9e06SViresh Kumar 	new_policy.governor = policy->user_policy.governor;
19761da177e4SLinus Torvalds 
1977bb176f7dSViresh Kumar 	/*
1978bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
1979bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
1980bb176f7dSViresh Kumar 	 */
19811c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
19823a3e9e06SViresh Kumar 		new_policy.cur = cpufreq_driver->get(cpu);
19833a3e9e06SViresh Kumar 		if (!policy->cur) {
19842d06d8c4SDominik Brodowski 			pr_debug("Driver did not initialize current freq");
19853a3e9e06SViresh Kumar 			policy->cur = new_policy.cur;
1986a85f7bd3SThomas Renninger 		} else {
19873a3e9e06SViresh Kumar 			if (policy->cur != new_policy.cur && cpufreq_driver->target)
19883a3e9e06SViresh Kumar 				cpufreq_out_of_sync(cpu, policy->cur,
19893a3e9e06SViresh Kumar 								new_policy.cur);
19900961dd0dSThomas Renninger 		}
1991a85f7bd3SThomas Renninger 	}
19920961dd0dSThomas Renninger 
19933a3e9e06SViresh Kumar 	ret = __cpufreq_set_policy(policy, &new_policy);
19941da177e4SLinus Torvalds 
19955a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(cpu);
19965a01f2e8SVenkatesh Pallipadi 
1997f1829e4aSJulia Lawall fail:
19983a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
1999f1829e4aSJulia Lawall no_policy:
20001da177e4SLinus Torvalds 	return ret;
20011da177e4SLinus Torvalds }
20021da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
20031da177e4SLinus Torvalds 
20042760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb,
2005c32b6b8eSAshok Raj 					unsigned long action, void *hcpu)
2006c32b6b8eSAshok Raj {
2007c32b6b8eSAshok Raj 	unsigned int cpu = (unsigned long)hcpu;
20088a25a2fdSKay Sievers 	struct device *dev;
20095302c3fbSSrivatsa S. Bhat 	bool frozen = false;
2010c32b6b8eSAshok Raj 
20118a25a2fdSKay Sievers 	dev = get_cpu_device(cpu);
20128a25a2fdSKay Sievers 	if (dev) {
20135302c3fbSSrivatsa S. Bhat 
20145302c3fbSSrivatsa S. Bhat 		if (action & CPU_TASKS_FROZEN)
20155302c3fbSSrivatsa S. Bhat 			frozen = true;
20165302c3fbSSrivatsa S. Bhat 
20175302c3fbSSrivatsa S. Bhat 		switch (action & ~CPU_TASKS_FROZEN) {
2018c32b6b8eSAshok Raj 		case CPU_ONLINE:
20195302c3fbSSrivatsa S. Bhat 			__cpufreq_add_dev(dev, NULL, frozen);
202023d32899SSrivatsa S. Bhat 			cpufreq_update_policy(cpu);
2021c32b6b8eSAshok Raj 			break;
20225302c3fbSSrivatsa S. Bhat 
2023c32b6b8eSAshok Raj 		case CPU_DOWN_PREPARE:
20245302c3fbSSrivatsa S. Bhat 			__cpufreq_remove_dev(dev, NULL, frozen);
2025c32b6b8eSAshok Raj 			break;
20265302c3fbSSrivatsa S. Bhat 
20275a01f2e8SVenkatesh Pallipadi 		case CPU_DOWN_FAILED:
20285302c3fbSSrivatsa S. Bhat 			__cpufreq_add_dev(dev, NULL, frozen);
2029c32b6b8eSAshok Raj 			break;
2030c32b6b8eSAshok Raj 		}
2031c32b6b8eSAshok Raj 	}
2032c32b6b8eSAshok Raj 	return NOTIFY_OK;
2033c32b6b8eSAshok Raj }
2034c32b6b8eSAshok Raj 
20359c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = {
2036c32b6b8eSAshok Raj 	.notifier_call = cpufreq_cpu_callback,
2037c32b6b8eSAshok Raj };
20381da177e4SLinus Torvalds 
20391da177e4SLinus Torvalds /*********************************************************************
20401da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
20411da177e4SLinus Torvalds  *********************************************************************/
20421da177e4SLinus Torvalds 
20431da177e4SLinus Torvalds /**
20441da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
20451da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
20461da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
20471da177e4SLinus Torvalds  *
20481da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
20491da177e4SLinus Torvalds  * returns zero on success, -EBUSY when another driver got here first
20501da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
20511da177e4SLinus Torvalds  *
20521da177e4SLinus Torvalds  */
2053221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
20541da177e4SLinus Torvalds {
20551da177e4SLinus Torvalds 	unsigned long flags;
20561da177e4SLinus Torvalds 	int ret;
20571da177e4SLinus Torvalds 
2058a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2059a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2060a7b422cdSKonrad Rzeszutek Wilk 
20611da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
20621da177e4SLinus Torvalds 	    ((!driver_data->setpolicy) && (!driver_data->target)))
20631da177e4SLinus Torvalds 		return -EINVAL;
20641da177e4SLinus Torvalds 
20652d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
20661da177e4SLinus Torvalds 
20671da177e4SLinus Torvalds 	if (driver_data->setpolicy)
20681da177e4SLinus Torvalds 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
20691da177e4SLinus Torvalds 
20700d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
20711c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
20720d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
20731da177e4SLinus Torvalds 		return -EBUSY;
20741da177e4SLinus Torvalds 	}
20751c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
20760d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
20771da177e4SLinus Torvalds 
20788a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
20798f5bc2abSJiri Slaby 	if (ret)
20808f5bc2abSJiri Slaby 		goto err_null_driver;
20811da177e4SLinus Torvalds 
20821c3d85ddSRafael J. Wysocki 	if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
20831da177e4SLinus Torvalds 		int i;
20841da177e4SLinus Torvalds 		ret = -ENODEV;
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds 		/* check for at least one working CPU */
20877a6aedfaSMike Travis 		for (i = 0; i < nr_cpu_ids; i++)
20887a6aedfaSMike Travis 			if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
20891da177e4SLinus Torvalds 				ret = 0;
20907a6aedfaSMike Travis 				break;
20917a6aedfaSMike Travis 			}
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
20941da177e4SLinus Torvalds 		if (ret) {
20952d06d8c4SDominik Brodowski 			pr_debug("no CPU initialized for driver %s\n",
2096e08f5f5bSGautham R Shenoy 							driver_data->name);
20978a25a2fdSKay Sievers 			goto err_if_unreg;
20981da177e4SLinus Torvalds 		}
20991da177e4SLinus Torvalds 	}
21001da177e4SLinus Torvalds 
210165edc68cSChandra Seetharaman 	register_hotcpu_notifier(&cpufreq_cpu_notifier);
21022d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
21031da177e4SLinus Torvalds 
21048f5bc2abSJiri Slaby 	return 0;
21058a25a2fdSKay Sievers err_if_unreg:
21068a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
21078f5bc2abSJiri Slaby err_null_driver:
21080d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
21091c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
21100d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
21114d34a67dSDave Jones 	return ret;
21121da177e4SLinus Torvalds }
21131da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds /**
21161da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
21171da177e4SLinus Torvalds  *
21181da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
21191da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
21201da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
21211da177e4SLinus Torvalds  * currently not initialised.
21221da177e4SLinus Torvalds  */
2123221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
21241da177e4SLinus Torvalds {
21251da177e4SLinus Torvalds 	unsigned long flags;
21261da177e4SLinus Torvalds 
21271c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
21281da177e4SLinus Torvalds 		return -EINVAL;
21291da177e4SLinus Torvalds 
21302d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
21311da177e4SLinus Torvalds 
21328a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
213365edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
21341da177e4SLinus Torvalds 
21350d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
21361c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
21370d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
21381da177e4SLinus Torvalds 
21391da177e4SLinus Torvalds 	return 0;
21401da177e4SLinus Torvalds }
21411da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
21425a01f2e8SVenkatesh Pallipadi 
21435a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
21445a01f2e8SVenkatesh Pallipadi {
21455a01f2e8SVenkatesh Pallipadi 	int cpu;
21465a01f2e8SVenkatesh Pallipadi 
2147a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2148a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2149a7b422cdSKonrad Rzeszutek Wilk 
21505a01f2e8SVenkatesh Pallipadi 	for_each_possible_cpu(cpu) {
2151f1625066STejun Heo 		per_cpu(cpufreq_policy_cpu, cpu) = -1;
21525a01f2e8SVenkatesh Pallipadi 		init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
21535a01f2e8SVenkatesh Pallipadi 	}
21548aa84ad8SThomas Renninger 
21552361be23SViresh Kumar 	cpufreq_global_kobject = kobject_create();
21568aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
2157e00e56dfSRafael J. Wysocki 	register_syscore_ops(&cpufreq_syscore_ops);
21588aa84ad8SThomas Renninger 
21595a01f2e8SVenkatesh Pallipadi 	return 0;
21605a01f2e8SVenkatesh Pallipadi }
21615a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2162