xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision 6e4f6b5eac461867471b3f368699097b31843d23)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/drivers/cpufreq/cpufreq.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2001 Russell King
61da177e4SLinus Torvalds  *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7bb176f7dSViresh Kumar  *            (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
81da177e4SLinus Torvalds  *
9c32b6b8eSAshok Raj  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
10c32b6b8eSAshok Raj  *	Added handling for CPU hotplug
118ff69732SDave Jones  *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
128ff69732SDave Jones  *	Fix handling for CPU hotplug -- affected CPUs
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
15db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16db701151SViresh Kumar 
175ff0a268SViresh Kumar #include <linux/cpu.h>
181da177e4SLinus Torvalds #include <linux/cpufreq.h>
195c238a8bSAmit Kucheria #include <linux/cpu_cooling.h>
201da177e4SLinus Torvalds #include <linux/delay.h>
211da177e4SLinus Torvalds #include <linux/device.h>
225ff0a268SViresh Kumar #include <linux/init.h>
235ff0a268SViresh Kumar #include <linux/kernel_stat.h>
245ff0a268SViresh Kumar #include <linux/module.h>
253fc54d37Sakpm@osdl.org #include <linux/mutex.h>
2667d874c3SViresh Kumar #include <linux/pm_qos.h>
275ff0a268SViresh Kumar #include <linux/slab.h>
282f0aea93SViresh Kumar #include <linux/suspend.h>
2990de2a4aSDoug Anderson #include <linux/syscore_ops.h>
305ff0a268SViresh Kumar #include <linux/tick.h>
31f55ae08cSViresh Kumar #include <linux/units.h>
326f4f2723SThomas Renninger #include <trace/events/power.h>
336f4f2723SThomas Renninger 
34b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list);
35f963735aSViresh Kumar 
36f963735aSViresh Kumar /* Macros to iterate over CPU policies */
37f963735aSViresh Kumar #define for_each_suitable_policy(__policy, __active)			 \
38fd7dc7e6SEric Biggers 	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
39fd7dc7e6SEric Biggers 		if ((__active) == !policy_is_inactive(__policy))
40f963735aSViresh Kumar 
41f963735aSViresh Kumar #define for_each_active_policy(__policy)		\
42f963735aSViresh Kumar 	for_each_suitable_policy(__policy, true)
43f963735aSViresh Kumar #define for_each_inactive_policy(__policy)		\
44f963735aSViresh Kumar 	for_each_suitable_policy(__policy, false)
45f963735aSViresh Kumar 
46f7b27061SViresh Kumar /* Iterate over governors */
47f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list);
48f7b27061SViresh Kumar #define for_each_governor(__governor)				\
49f7b27061SViresh Kumar 	list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
50f7b27061SViresh Kumar 
518412b456SQuentin Perret static char default_governor[CPUFREQ_NAME_LEN];
528412b456SQuentin Perret 
53a9909c21SLee Jones /*
54cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
551da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
561da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
571da177e4SLinus Torvalds  */
581c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
597a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
60bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
61bb176f7dSViresh Kumar 
62874f6353SIonela Voinescu static DEFINE_STATIC_KEY_FALSE(cpufreq_freq_invariance);
cpufreq_supports_freq_invariance(void)63874f6353SIonela Voinescu bool cpufreq_supports_freq_invariance(void)
64874f6353SIonela Voinescu {
65874f6353SIonela Voinescu 	return static_branch_likely(&cpufreq_freq_invariance);
66874f6353SIonela Voinescu }
67874f6353SIonela Voinescu 
682f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */
692f0aea93SViresh Kumar static bool cpufreq_suspended;
701da177e4SLinus Torvalds 
has_target(void)719c0ebcf7SViresh Kumar static inline bool has_target(void)
729c0ebcf7SViresh Kumar {
739c0ebcf7SViresh Kumar 	return cpufreq_driver->target_index || cpufreq_driver->target;
749c0ebcf7SViresh Kumar }
759c0ebcf7SViresh Kumar 
has_target_index(void)76a038895eSViresh Kumar bool has_target_index(void)
77a038895eSViresh Kumar {
78a038895eSViresh Kumar 	return !!cpufreq_driver->target_index;
79a038895eSViresh Kumar }
80a038895eSViresh Kumar 
811da177e4SLinus Torvalds /* internal prototypes */
82d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
83a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy);
84a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy);
85a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy);
861e4f63aeSRafael J. Wysocki static int cpufreq_set_policy(struct cpufreq_policy *policy,
871e4f63aeSRafael J. Wysocki 			      struct cpufreq_governor *new_gov,
881e4f63aeSRafael J. Wysocki 			      unsigned int new_pol);
89218a06a7SJie Zhan static bool cpufreq_boost_supported(void);
9045482c70SRafael J. Wysocki 
91a9909c21SLee Jones /*
921da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
931da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
941da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
951da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
961da177e4SLinus Torvalds  * The mutex locks both lists.
971da177e4SLinus Torvalds  */
98e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
99cc85de36SSebastian Andrzej Siewior SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
1001da177e4SLinus Torvalds 
101a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
cpufreq_disabled(void)102da584455SViresh Kumar static int cpufreq_disabled(void)
103a7b422cdSKonrad Rzeszutek Wilk {
104a7b422cdSKonrad Rzeszutek Wilk 	return off;
105a7b422cdSKonrad Rzeszutek Wilk }
disable_cpufreq(void)106a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
107a7b422cdSKonrad Rzeszutek Wilk {
108a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
109a7b422cdSKonrad Rzeszutek Wilk }
1103fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
1111da177e4SLinus Torvalds 
have_governor_per_policy(void)1124d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1134d5dcc42SViresh Kumar {
1140b981e70SViresh Kumar 	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
1154d5dcc42SViresh Kumar }
1163f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1174d5dcc42SViresh Kumar 
118183edb20SYangtao Li static struct kobject *cpufreq_global_kobject;
119183edb20SYangtao Li 
get_governor_parent_kobj(struct cpufreq_policy * policy)120944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
121944e9a03SViresh Kumar {
122944e9a03SViresh Kumar 	if (have_governor_per_policy())
123944e9a03SViresh Kumar 		return &policy->kobj;
124944e9a03SViresh Kumar 	else
125944e9a03SViresh Kumar 		return cpufreq_global_kobject;
126944e9a03SViresh Kumar }
127944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
128944e9a03SViresh Kumar 
get_cpu_idle_time_jiffy(unsigned int cpu,u64 * wall)12972a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
13072a4ce34SViresh Kumar {
1315720821bSFrederic Weisbecker 	struct kernel_cpustat kcpustat;
13272a4ce34SViresh Kumar 	u64 cur_wall_time;
1335720821bSFrederic Weisbecker 	u64 idle_time;
13472a4ce34SViresh Kumar 	u64 busy_time;
13572a4ce34SViresh Kumar 
1367fb1327eSFrederic Weisbecker 	cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
13772a4ce34SViresh Kumar 
1385720821bSFrederic Weisbecker 	kcpustat_cpu_fetch(&kcpustat, cpu);
1395720821bSFrederic Weisbecker 
1405720821bSFrederic Weisbecker 	busy_time = kcpustat.cpustat[CPUTIME_USER];
1415720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_SYSTEM];
1425720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_IRQ];
1435720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ];
1445720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_STEAL];
1455720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_NICE];
14672a4ce34SViresh Kumar 
14772a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
14872a4ce34SViresh Kumar 	if (wall)
1497fb1327eSFrederic Weisbecker 		*wall = div_u64(cur_wall_time, NSEC_PER_USEC);
15072a4ce34SViresh Kumar 
1517fb1327eSFrederic Weisbecker 	return div_u64(idle_time, NSEC_PER_USEC);
15272a4ce34SViresh Kumar }
15372a4ce34SViresh Kumar 
get_cpu_idle_time(unsigned int cpu,u64 * wall,int io_busy)15472a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
15572a4ce34SViresh Kumar {
15672a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
15772a4ce34SViresh Kumar 
15872a4ce34SViresh Kumar 	if (idle_time == -1ULL)
15972a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
16072a4ce34SViresh Kumar 	else if (!io_busy)
16172a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
16272a4ce34SViresh Kumar 
16372a4ce34SViresh Kumar 	return idle_time;
16472a4ce34SViresh Kumar }
16572a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
16672a4ce34SViresh Kumar 
16770e9e778SViresh Kumar /*
16870e9e778SViresh Kumar  * This is a generic cpufreq init() routine which can be used by cpufreq
16970e9e778SViresh Kumar  * drivers of SMP systems. It will do following:
17070e9e778SViresh Kumar  * - validate & show freq table passed
17170e9e778SViresh Kumar  * - set policies transition latency
17270e9e778SViresh Kumar  * - policy->cpus with all possible CPUs
17370e9e778SViresh Kumar  */
cpufreq_generic_init(struct cpufreq_policy * policy,struct cpufreq_frequency_table * table,unsigned int transition_latency)174c4dcc8a1SViresh Kumar void cpufreq_generic_init(struct cpufreq_policy *policy,
17570e9e778SViresh Kumar 		struct cpufreq_frequency_table *table,
17670e9e778SViresh Kumar 		unsigned int transition_latency)
17770e9e778SViresh Kumar {
17892c99d15SViresh Kumar 	policy->freq_table = table;
17970e9e778SViresh Kumar 	policy->cpuinfo.transition_latency = transition_latency;
18070e9e778SViresh Kumar 
18170e9e778SViresh Kumar 	/*
18258405af6SShailendra Verma 	 * The driver only supports the SMP configuration where all processors
18370e9e778SViresh Kumar 	 * share the clock and voltage and clock.
18470e9e778SViresh Kumar 	 */
18570e9e778SViresh Kumar 	cpumask_setall(policy->cpus);
18670e9e778SViresh Kumar }
18770e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init);
18870e9e778SViresh Kumar 
cpufreq_cpu_get_raw(unsigned int cpu)1891f0bd44eSRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
190652ed95dSViresh Kumar {
191652ed95dSViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
192652ed95dSViresh Kumar 
193988bed09SViresh Kumar 	return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
194988bed09SViresh Kumar }
1951f0bd44eSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
196988bed09SViresh Kumar 
cpufreq_generic_get(unsigned int cpu)197988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu)
198988bed09SViresh Kumar {
199988bed09SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
200988bed09SViresh Kumar 
201652ed95dSViresh Kumar 	if (!policy || IS_ERR(policy->clk)) {
202e837f9b5SJoe Perches 		pr_err("%s: No %s associated to cpu: %d\n",
203e837f9b5SJoe Perches 		       __func__, policy ? "clk" : "policy", cpu);
204652ed95dSViresh Kumar 		return 0;
205652ed95dSViresh Kumar 	}
206652ed95dSViresh Kumar 
207652ed95dSViresh Kumar 	return clk_get_rate(policy->clk) / 1000;
208652ed95dSViresh Kumar }
209652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get);
210652ed95dSViresh Kumar 
21150e9c852SViresh Kumar /**
2125d094feaSRafael J. Wysocki  * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
2135d094feaSRafael J. Wysocki  * @cpu: CPU to find the policy for.
21450e9c852SViresh Kumar  *
2155d094feaSRafael J. Wysocki  * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
2165d094feaSRafael J. Wysocki  * the kobject reference counter of that policy.  Return a valid policy on
2175d094feaSRafael J. Wysocki  * success or NULL on failure.
21850e9c852SViresh Kumar  *
2195d094feaSRafael J. Wysocki  * The policy returned by this function has to be released with the help of
2205d094feaSRafael J. Wysocki  * cpufreq_cpu_put() to balance its kobject reference counter properly.
22150e9c852SViresh Kumar  */
cpufreq_cpu_get(unsigned int cpu)2226eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
2231da177e4SLinus Torvalds {
2246eed9404SViresh Kumar 	struct cpufreq_policy *policy = NULL;
2251da177e4SLinus Torvalds 	unsigned long flags;
2261da177e4SLinus Torvalds 
2271b947c90SViresh Kumar 	if (WARN_ON(cpu >= nr_cpu_ids))
2286eed9404SViresh Kumar 		return NULL;
2296eed9404SViresh Kumar 
2301da177e4SLinus Torvalds 	/* get the cpufreq driver */
2310d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
2321da177e4SLinus Torvalds 
2336eed9404SViresh Kumar 	if (cpufreq_driver) {
2341da177e4SLinus Torvalds 		/* get the CPU */
235988bed09SViresh Kumar 		policy = cpufreq_cpu_get_raw(cpu);
2366eed9404SViresh Kumar 		if (policy)
2376eed9404SViresh Kumar 			kobject_get(&policy->kobj);
2386eed9404SViresh Kumar 	}
2396eed9404SViresh Kumar 
2406eed9404SViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2411da177e4SLinus Torvalds 
2423a3e9e06SViresh Kumar 	return policy;
243a9144436SStephen Boyd }
2441da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2451da177e4SLinus Torvalds 
24650e9c852SViresh Kumar /**
2475d094feaSRafael J. Wysocki  * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
2485d094feaSRafael J. Wysocki  * @policy: cpufreq policy returned by cpufreq_cpu_get().
24950e9c852SViresh Kumar  */
cpufreq_cpu_put(struct cpufreq_policy * policy)2503a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
251a9144436SStephen Boyd {
2526eed9404SViresh Kumar 	kobject_put(&policy->kobj);
253a9144436SStephen Boyd }
2541da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2551da177e4SLinus Torvalds 
256540a3758SRafael J. Wysocki /**
257540a3758SRafael J. Wysocki  * cpufreq_cpu_release - Unlock a policy and decrement its usage counter.
258540a3758SRafael J. Wysocki  * @policy: cpufreq policy returned by cpufreq_cpu_acquire().
259540a3758SRafael J. Wysocki  */
cpufreq_cpu_release(struct cpufreq_policy * policy)2609083e498SRafael J. Wysocki void cpufreq_cpu_release(struct cpufreq_policy *policy)
261540a3758SRafael J. Wysocki {
262540a3758SRafael J. Wysocki 	if (WARN_ON(!policy))
263540a3758SRafael J. Wysocki 		return;
264540a3758SRafael J. Wysocki 
265540a3758SRafael J. Wysocki 	lockdep_assert_held(&policy->rwsem);
266540a3758SRafael J. Wysocki 
267540a3758SRafael J. Wysocki 	up_write(&policy->rwsem);
268540a3758SRafael J. Wysocki 
269540a3758SRafael J. Wysocki 	cpufreq_cpu_put(policy);
270540a3758SRafael J. Wysocki }
271540a3758SRafael J. Wysocki 
272540a3758SRafael J. Wysocki /**
273540a3758SRafael J. Wysocki  * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it.
274540a3758SRafael J. Wysocki  * @cpu: CPU to find the policy for.
275540a3758SRafael J. Wysocki  *
276540a3758SRafael J. Wysocki  * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and
277540a3758SRafael J. Wysocki  * if the policy returned by it is not NULL, acquire its rwsem for writing.
278540a3758SRafael J. Wysocki  * Return the policy if it is active or release it and return NULL otherwise.
279540a3758SRafael J. Wysocki  *
280540a3758SRafael J. Wysocki  * The policy returned by this function has to be released with the help of
281540a3758SRafael J. Wysocki  * cpufreq_cpu_release() in order to release its rwsem and balance its usage
282540a3758SRafael J. Wysocki  * counter properly.
283540a3758SRafael J. Wysocki  */
cpufreq_cpu_acquire(unsigned int cpu)2849083e498SRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu)
285540a3758SRafael J. Wysocki {
286540a3758SRafael J. Wysocki 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
287540a3758SRafael J. Wysocki 
288540a3758SRafael J. Wysocki 	if (!policy)
289540a3758SRafael J. Wysocki 		return NULL;
290540a3758SRafael J. Wysocki 
291540a3758SRafael J. Wysocki 	down_write(&policy->rwsem);
292540a3758SRafael J. Wysocki 
293540a3758SRafael J. Wysocki 	if (policy_is_inactive(policy)) {
294540a3758SRafael J. Wysocki 		cpufreq_cpu_release(policy);
295540a3758SRafael J. Wysocki 		return NULL;
296540a3758SRafael J. Wysocki 	}
297540a3758SRafael J. Wysocki 
298540a3758SRafael J. Wysocki 	return policy;
299540a3758SRafael J. Wysocki }
300540a3758SRafael J. Wysocki 
3011da177e4SLinus Torvalds /*********************************************************************
3021da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
3031da177e4SLinus Torvalds  *********************************************************************/
3041da177e4SLinus Torvalds 
305ec06e586SRafael J. Wysocki /**
306ec06e586SRafael J. Wysocki  * adjust_jiffies - Adjust the system "loops_per_jiffy".
307ec06e586SRafael J. Wysocki  * @val: CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
308ec06e586SRafael J. Wysocki  * @ci: Frequency change information.
3091da177e4SLinus Torvalds  *
3101da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
3111da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
3121da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
3131da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
3141da177e4SLinus Torvalds  */
adjust_jiffies(unsigned long val,struct cpufreq_freqs * ci)31539c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
31639c132eeSViresh Kumar {
3171da177e4SLinus Torvalds #ifndef CONFIG_SMP
3181da177e4SLinus Torvalds 	static unsigned long l_p_j_ref;
3191da177e4SLinus Torvalds 	static unsigned int l_p_j_ref_freq;
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
3221da177e4SLinus Torvalds 		return;
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
3251da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
3261da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
327e837f9b5SJoe Perches 		pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
328e837f9b5SJoe Perches 			 l_p_j_ref, l_p_j_ref_freq);
3291da177e4SLinus Torvalds 	}
3300b443eadSViresh Kumar 	if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
331e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
332e08f5f5bSGautham R Shenoy 								ci->new);
333e837f9b5SJoe Perches 		pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
334e837f9b5SJoe Perches 			 loops_per_jiffy, ci->new);
3351da177e4SLinus Torvalds 	}
3361da177e4SLinus Torvalds #endif
33739c132eeSViresh Kumar }
3381da177e4SLinus Torvalds 
33920b5324dSViresh Kumar /**
340ec06e586SRafael J. Wysocki  * cpufreq_notify_transition - Notify frequency transition and adjust jiffies.
34120b5324dSViresh Kumar  * @policy: cpufreq policy to enable fast frequency switching for.
34220b5324dSViresh Kumar  * @freqs: contain details of the frequency update.
34320b5324dSViresh Kumar  * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
34420b5324dSViresh Kumar  *
345ec06e586SRafael J. Wysocki  * This function calls the transition notifiers and adjust_jiffies().
346ec06e586SRafael J. Wysocki  *
347ec06e586SRafael J. Wysocki  * It is called twice on all CPU frequency changes that have external effects.
34820b5324dSViresh Kumar  */
cpufreq_notify_transition(struct cpufreq_policy * policy,struct cpufreq_freqs * freqs,unsigned int state)34920b5324dSViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy,
35020b5324dSViresh Kumar 				      struct cpufreq_freqs *freqs,
35120b5324dSViresh Kumar 				      unsigned int state)
3521da177e4SLinus Torvalds {
353df24014aSViresh Kumar 	int cpu;
354df24014aSViresh Kumar 
3551da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
3561da177e4SLinus Torvalds 
357d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
358d5aaffa9SDirk Brandewie 		return;
359d5aaffa9SDirk Brandewie 
360df24014aSViresh Kumar 	freqs->policy = policy;
3611c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
3622d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
363e4472cb3SDave Jones 		 state, freqs->new);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	switch (state) {
3661da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
36720b5324dSViresh Kumar 		/*
36820b5324dSViresh Kumar 		 * Detect if the driver reported a value as "old frequency"
369e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
370e4472cb3SDave Jones 		 * "old frequency".
3711da177e4SLinus Torvalds 		 */
37298015228SViresh Kumar 		if (policy->cur && policy->cur != freqs->old) {
373e837f9b5SJoe Perches 			pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
374e4472cb3SDave Jones 				 freqs->old, policy->cur);
375e4472cb3SDave Jones 			freqs->old = policy->cur;
3761da177e4SLinus Torvalds 		}
37720b5324dSViresh Kumar 
378b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
379e4472cb3SDave Jones 					 CPUFREQ_PRECHANGE, freqs);
38020b5324dSViresh Kumar 
3811da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3821da177e4SLinus Torvalds 		break;
383e4472cb3SDave Jones 
3841da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3851da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
38620b5324dSViresh Kumar 		pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
38720b5324dSViresh Kumar 			 cpumask_pr_args(policy->cpus));
38820b5324dSViresh Kumar 
389df24014aSViresh Kumar 		for_each_cpu(cpu, policy->cpus)
390df24014aSViresh Kumar 			trace_cpu_frequency(freqs->new, cpu);
391df24014aSViresh Kumar 
392b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
393e4472cb3SDave Jones 					 CPUFREQ_POSTCHANGE, freqs);
394bb176f7dSViresh Kumar 
39520b5324dSViresh Kumar 		cpufreq_stats_record_transition(policy, freqs->new);
39620b5324dSViresh Kumar 		policy->cur = freqs->new;
39720b5324dSViresh Kumar 	}
398b43a7ffbSViresh Kumar }
3991da177e4SLinus Torvalds 
400f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */
cpufreq_notify_post_transition(struct cpufreq_policy * policy,struct cpufreq_freqs * freqs,int transition_failed)401236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
402f7ba3b41SViresh Kumar 		struct cpufreq_freqs *freqs, int transition_failed)
403f7ba3b41SViresh Kumar {
404f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
405f7ba3b41SViresh Kumar 	if (!transition_failed)
406f7ba3b41SViresh Kumar 		return;
407f7ba3b41SViresh Kumar 
408f7ba3b41SViresh Kumar 	swap(freqs->old, freqs->new);
409f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
410f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
411f7ba3b41SViresh Kumar }
412f7ba3b41SViresh Kumar 
cpufreq_freq_transition_begin(struct cpufreq_policy * policy,struct cpufreq_freqs * freqs)41312478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
41412478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs)
41512478cf0SSrivatsa S. Bhat {
416ca654dc3SSrivatsa S. Bhat 
417ca654dc3SSrivatsa S. Bhat 	/*
418ca654dc3SSrivatsa S. Bhat 	 * Catch double invocations of _begin() which lead to self-deadlock.
419ca654dc3SSrivatsa S. Bhat 	 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
420ca654dc3SSrivatsa S. Bhat 	 * doesn't invoke _begin() on their behalf, and hence the chances of
421ca654dc3SSrivatsa S. Bhat 	 * double invocations are very low. Moreover, there are scenarios
422ca654dc3SSrivatsa S. Bhat 	 * where these checks can emit false-positive warnings in these
423ca654dc3SSrivatsa S. Bhat 	 * drivers; so we avoid that by skipping them altogether.
424ca654dc3SSrivatsa S. Bhat 	 */
425ca654dc3SSrivatsa S. Bhat 	WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
426ca654dc3SSrivatsa S. Bhat 				&& current == policy->transition_task);
427ca654dc3SSrivatsa S. Bhat 
42812478cf0SSrivatsa S. Bhat wait:
42912478cf0SSrivatsa S. Bhat 	wait_event(policy->transition_wait, !policy->transition_ongoing);
43012478cf0SSrivatsa S. Bhat 
43112478cf0SSrivatsa S. Bhat 	spin_lock(&policy->transition_lock);
43212478cf0SSrivatsa S. Bhat 
43312478cf0SSrivatsa S. Bhat 	if (unlikely(policy->transition_ongoing)) {
43412478cf0SSrivatsa S. Bhat 		spin_unlock(&policy->transition_lock);
43512478cf0SSrivatsa S. Bhat 		goto wait;
43612478cf0SSrivatsa S. Bhat 	}
43712478cf0SSrivatsa S. Bhat 
43812478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = true;
439ca654dc3SSrivatsa S. Bhat 	policy->transition_task = current;
44012478cf0SSrivatsa S. Bhat 
44112478cf0SSrivatsa S. Bhat 	spin_unlock(&policy->transition_lock);
44212478cf0SSrivatsa S. Bhat 
44312478cf0SSrivatsa S. Bhat 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
44412478cf0SSrivatsa S. Bhat }
44512478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
44612478cf0SSrivatsa S. Bhat 
cpufreq_freq_transition_end(struct cpufreq_policy * policy,struct cpufreq_freqs * freqs,int transition_failed)44712478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
44812478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs, int transition_failed)
44912478cf0SSrivatsa S. Bhat {
4500e7ea2f3SIgor Stoppa 	if (WARN_ON(!policy->transition_ongoing))
45112478cf0SSrivatsa S. Bhat 		return;
45212478cf0SSrivatsa S. Bhat 
45312478cf0SSrivatsa S. Bhat 	cpufreq_notify_post_transition(policy, freqs, transition_failed);
45412478cf0SSrivatsa S. Bhat 
4551a0419b0SIonela Voinescu 	arch_set_freq_scale(policy->related_cpus,
4561a0419b0SIonela Voinescu 			    policy->cur,
4571a0419b0SIonela Voinescu 			    policy->cpuinfo.max_freq);
4581a0419b0SIonela Voinescu 
45961bfbf79SLiao Chang 	spin_lock(&policy->transition_lock);
46012478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = false;
461ca654dc3SSrivatsa S. Bhat 	policy->transition_task = NULL;
46261bfbf79SLiao Chang 	spin_unlock(&policy->transition_lock);
46312478cf0SSrivatsa S. Bhat 
46412478cf0SSrivatsa S. Bhat 	wake_up(&policy->transition_wait);
46512478cf0SSrivatsa S. Bhat }
46612478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
46712478cf0SSrivatsa S. Bhat 
468b7898fdaSRafael J. Wysocki /*
469b7898fdaSRafael J. Wysocki  * Fast frequency switching status count.  Positive means "enabled", negative
470b7898fdaSRafael J. Wysocki  * means "disabled" and 0 means "not decided yet".
471b7898fdaSRafael J. Wysocki  */
472b7898fdaSRafael J. Wysocki static int cpufreq_fast_switch_count;
473b7898fdaSRafael J. Wysocki static DEFINE_MUTEX(cpufreq_fast_switch_lock);
474b7898fdaSRafael J. Wysocki 
cpufreq_list_transition_notifiers(void)475b7898fdaSRafael J. Wysocki static void cpufreq_list_transition_notifiers(void)
476b7898fdaSRafael J. Wysocki {
477b7898fdaSRafael J. Wysocki 	struct notifier_block *nb;
478b7898fdaSRafael J. Wysocki 
479b7898fdaSRafael J. Wysocki 	pr_info("Registered transition notifiers:\n");
480b7898fdaSRafael J. Wysocki 
481b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_transition_notifier_list.mutex);
482b7898fdaSRafael J. Wysocki 
483b7898fdaSRafael J. Wysocki 	for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
484d75f773cSSakari Ailus 		pr_info("%pS\n", nb->notifier_call);
485b7898fdaSRafael J. Wysocki 
486b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_transition_notifier_list.mutex);
487b7898fdaSRafael J. Wysocki }
488b7898fdaSRafael J. Wysocki 
489b7898fdaSRafael J. Wysocki /**
490b7898fdaSRafael J. Wysocki  * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
491b7898fdaSRafael J. Wysocki  * @policy: cpufreq policy to enable fast frequency switching for.
492b7898fdaSRafael J. Wysocki  *
493b7898fdaSRafael J. Wysocki  * Try to enable fast frequency switching for @policy.
494b7898fdaSRafael J. Wysocki  *
495b7898fdaSRafael J. Wysocki  * The attempt will fail if there is at least one transition notifier registered
496b7898fdaSRafael J. Wysocki  * at this point, as fast frequency switching is quite fundamentally at odds
497b7898fdaSRafael J. Wysocki  * with transition notifiers.  Thus if successful, it will make registration of
498b7898fdaSRafael J. Wysocki  * transition notifiers fail going forward.
499b7898fdaSRafael J. Wysocki  */
cpufreq_enable_fast_switch(struct cpufreq_policy * policy)500b7898fdaSRafael J. Wysocki void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
501b7898fdaSRafael J. Wysocki {
502b7898fdaSRafael J. Wysocki 	lockdep_assert_held(&policy->rwsem);
503b7898fdaSRafael J. Wysocki 
504b7898fdaSRafael J. Wysocki 	if (!policy->fast_switch_possible)
505b7898fdaSRafael J. Wysocki 		return;
506b7898fdaSRafael J. Wysocki 
507b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_fast_switch_lock);
508b7898fdaSRafael J. Wysocki 	if (cpufreq_fast_switch_count >= 0) {
509b7898fdaSRafael J. Wysocki 		cpufreq_fast_switch_count++;
510b7898fdaSRafael J. Wysocki 		policy->fast_switch_enabled = true;
511b7898fdaSRafael J. Wysocki 	} else {
512b7898fdaSRafael J. Wysocki 		pr_warn("CPU%u: Fast frequency switching not enabled\n",
513b7898fdaSRafael J. Wysocki 			policy->cpu);
514b7898fdaSRafael J. Wysocki 		cpufreq_list_transition_notifiers();
515b7898fdaSRafael J. Wysocki 	}
516b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_fast_switch_lock);
517b7898fdaSRafael J. Wysocki }
518b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
519b7898fdaSRafael J. Wysocki 
5206c9d9c81SRafael J. Wysocki /**
5216c9d9c81SRafael J. Wysocki  * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
5226c9d9c81SRafael J. Wysocki  * @policy: cpufreq policy to disable fast frequency switching for.
5236c9d9c81SRafael J. Wysocki  */
cpufreq_disable_fast_switch(struct cpufreq_policy * policy)5246c9d9c81SRafael J. Wysocki void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
525b7898fdaSRafael J. Wysocki {
526b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_fast_switch_lock);
527b7898fdaSRafael J. Wysocki 	if (policy->fast_switch_enabled) {
528b7898fdaSRafael J. Wysocki 		policy->fast_switch_enabled = false;
529b7898fdaSRafael J. Wysocki 		if (!WARN_ON(cpufreq_fast_switch_count <= 0))
530b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count--;
531b7898fdaSRafael J. Wysocki 	}
532b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_fast_switch_lock);
533b7898fdaSRafael J. Wysocki }
5346c9d9c81SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
5351da177e4SLinus Torvalds 
__resolve_freq(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int relation)536f9ccdec2SViresh Kumar static unsigned int __resolve_freq(struct cpufreq_policy *policy,
537f9ccdec2SViresh Kumar 		unsigned int target_freq, unsigned int relation)
538f9ccdec2SViresh Kumar {
539f9ccdec2SViresh Kumar 	unsigned int idx;
540f9ccdec2SViresh Kumar 
541b3beca76SViresh Kumar 	target_freq = clamp_val(target_freq, policy->min, policy->max);
542f9ccdec2SViresh Kumar 
5436ca7076fSLukasz Luba 	if (!policy->freq_table)
544f9ccdec2SViresh Kumar 		return target_freq;
545b3beca76SViresh Kumar 
546b3beca76SViresh Kumar 	idx = cpufreq_frequency_table_target(policy, target_freq, relation);
547b3beca76SViresh Kumar 	policy->cached_resolved_idx = idx;
548b3beca76SViresh Kumar 	policy->cached_target_freq = target_freq;
549b3beca76SViresh Kumar 	return policy->freq_table[idx].frequency;
550f9ccdec2SViresh Kumar }
551f9ccdec2SViresh Kumar 
552e3c06236SSteve Muckle /**
553e3c06236SSteve Muckle  * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
554e3c06236SSteve Muckle  * one.
555a9909c21SLee Jones  * @policy: associated policy to interrogate
556e3c06236SSteve Muckle  * @target_freq: target frequency to resolve.
557e3c06236SSteve Muckle  *
558e3c06236SSteve Muckle  * The target to driver frequency mapping is cached in the policy.
559e3c06236SSteve Muckle  *
560e3c06236SSteve Muckle  * Return: Lowest driver-supported frequency greater than or equal to the
561e3c06236SSteve Muckle  * given target_freq, subject to policy (min/max) and driver limitations.
562e3c06236SSteve Muckle  */
cpufreq_driver_resolve_freq(struct cpufreq_policy * policy,unsigned int target_freq)563e3c06236SSteve Muckle unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
564e3c06236SSteve Muckle 					 unsigned int target_freq)
565e3c06236SSteve Muckle {
566b894d20eSVincent Donnefort 	return __resolve_freq(policy, target_freq, CPUFREQ_RELATION_LE);
567e3c06236SSteve Muckle }
568ae2c1ca6SSteve Muckle EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
569e3c06236SSteve Muckle 
cpufreq_policy_transition_delay_us(struct cpufreq_policy * policy)570aa7519afSViresh Kumar unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
571aa7519afSViresh Kumar {
572aa7519afSViresh Kumar 	unsigned int latency;
573aa7519afSViresh Kumar 
574aa7519afSViresh Kumar 	if (policy->transition_delay_us)
575aa7519afSViresh Kumar 		return policy->transition_delay_us;
576aa7519afSViresh Kumar 
577aa7519afSViresh Kumar 	latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
578e948bc8fSViresh Kumar 	if (latency) {
579e948bc8fSViresh Kumar 		/*
580e948bc8fSViresh Kumar 		 * For platforms that can change the frequency very fast (< 10
581e948bc8fSViresh Kumar 		 * us), the above formula gives a decent transition delay. But
582e948bc8fSViresh Kumar 		 * for platforms where transition_latency is in milliseconds, it
583e948bc8fSViresh Kumar 		 * ends up giving unrealistic values.
584e948bc8fSViresh Kumar 		 *
585e948bc8fSViresh Kumar 		 * Cap the default transition delay to 10 ms, which seems to be
586e948bc8fSViresh Kumar 		 * a reasonable amount of time after which we should reevaluate
587e948bc8fSViresh Kumar 		 * the frequency.
588e948bc8fSViresh Kumar 		 */
589e948bc8fSViresh Kumar 		return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
590e948bc8fSViresh Kumar 	}
591aa7519afSViresh Kumar 
592aa7519afSViresh Kumar 	return LATENCY_MULTIPLIER;
593aa7519afSViresh Kumar }
594aa7519afSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
595aa7519afSViresh Kumar 
5961da177e4SLinus Torvalds /*********************************************************************
5971da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
5981da177e4SLinus Torvalds  *********************************************************************/
show_boost(struct kobject * kobj,struct kobj_attribute * attr,char * buf)5998a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj,
600625c85a6SViresh Kumar 			  struct kobj_attribute *attr, char *buf)
6016f19efc0SLukasz Majewski {
6026f19efc0SLukasz Majewski 	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
6036f19efc0SLukasz Majewski }
6046f19efc0SLukasz Majewski 
store_boost(struct kobject * kobj,struct kobj_attribute * attr,const char * buf,size_t count)605625c85a6SViresh Kumar static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
6066f19efc0SLukasz Majewski 			   const char *buf, size_t count)
6076f19efc0SLukasz Majewski {
6086f19efc0SLukasz Majewski 	int ret, enable;
6096f19efc0SLukasz Majewski 
6106f19efc0SLukasz Majewski 	ret = sscanf(buf, "%d", &enable);
6116f19efc0SLukasz Majewski 	if (ret != 1 || enable < 0 || enable > 1)
6126f19efc0SLukasz Majewski 		return -EINVAL;
6136f19efc0SLukasz Majewski 
6146f19efc0SLukasz Majewski 	if (cpufreq_boost_trigger_state(enable)) {
615e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST!\n",
616e837f9b5SJoe Perches 		       __func__, enable ? "enable" : "disable");
6176f19efc0SLukasz Majewski 		return -EINVAL;
6186f19efc0SLukasz Majewski 	}
6196f19efc0SLukasz Majewski 
620e837f9b5SJoe Perches 	pr_debug("%s: cpufreq BOOST %s\n",
621e837f9b5SJoe Perches 		 __func__, enable ? "enabled" : "disabled");
6226f19efc0SLukasz Majewski 
6236f19efc0SLukasz Majewski 	return count;
6246f19efc0SLukasz Majewski }
6256f19efc0SLukasz Majewski define_one_global_rw(boost);
6261da177e4SLinus Torvalds 
show_local_boost(struct cpufreq_policy * policy,char * buf)627218a06a7SJie Zhan static ssize_t show_local_boost(struct cpufreq_policy *policy, char *buf)
628218a06a7SJie Zhan {
629218a06a7SJie Zhan 	return sysfs_emit(buf, "%d\n", policy->boost_enabled);
630218a06a7SJie Zhan }
631218a06a7SJie Zhan 
store_local_boost(struct cpufreq_policy * policy,const char * buf,size_t count)632218a06a7SJie Zhan static ssize_t store_local_boost(struct cpufreq_policy *policy,
633218a06a7SJie Zhan 				 const char *buf, size_t count)
634218a06a7SJie Zhan {
635218a06a7SJie Zhan 	int ret, enable;
636218a06a7SJie Zhan 
637218a06a7SJie Zhan 	ret = kstrtoint(buf, 10, &enable);
638218a06a7SJie Zhan 	if (ret || enable < 0 || enable > 1)
639218a06a7SJie Zhan 		return -EINVAL;
640218a06a7SJie Zhan 
641218a06a7SJie Zhan 	if (!cpufreq_driver->boost_enabled)
642218a06a7SJie Zhan 		return -EINVAL;
643218a06a7SJie Zhan 
644218a06a7SJie Zhan 	if (policy->boost_enabled == enable)
645218a06a7SJie Zhan 		return count;
646218a06a7SJie Zhan 
6479d47d2e7SSibi Sankar 	policy->boost_enabled = enable;
6489d47d2e7SSibi Sankar 
649218a06a7SJie Zhan 	cpus_read_lock();
650218a06a7SJie Zhan 	ret = cpufreq_driver->set_boost(policy, enable);
651218a06a7SJie Zhan 	cpus_read_unlock();
652218a06a7SJie Zhan 
6539d47d2e7SSibi Sankar 	if (ret) {
6549d47d2e7SSibi Sankar 		policy->boost_enabled = !policy->boost_enabled;
655218a06a7SJie Zhan 		return ret;
6569d47d2e7SSibi Sankar 	}
657218a06a7SJie Zhan 
658218a06a7SJie Zhan 	return count;
659218a06a7SJie Zhan }
660218a06a7SJie Zhan 
661218a06a7SJie Zhan static struct freq_attr local_boost = __ATTR(boost, 0644, show_local_boost, store_local_boost);
662218a06a7SJie Zhan 
find_governor(const char * str_governor)66342f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor)
6643bcb09a3SJeremy Fitzhardinge {
6653bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
6663bcb09a3SJeremy Fitzhardinge 
667f7b27061SViresh Kumar 	for_each_governor(t)
6687c4f4539SRasmus Villemoes 		if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
6693bcb09a3SJeremy Fitzhardinge 			return t;
6703bcb09a3SJeremy Fitzhardinge 
6713bcb09a3SJeremy Fitzhardinge 	return NULL;
6723bcb09a3SJeremy Fitzhardinge }
6733bcb09a3SJeremy Fitzhardinge 
get_governor(const char * str_governor)6748cc46ae5SViresh Kumar static struct cpufreq_governor *get_governor(const char *str_governor)
6758cc46ae5SViresh Kumar {
6768cc46ae5SViresh Kumar 	struct cpufreq_governor *t;
6778cc46ae5SViresh Kumar 
6788cc46ae5SViresh Kumar 	mutex_lock(&cpufreq_governor_mutex);
6798cc46ae5SViresh Kumar 	t = find_governor(str_governor);
6808cc46ae5SViresh Kumar 	if (!t)
6818cc46ae5SViresh Kumar 		goto unlock;
6828cc46ae5SViresh Kumar 
6838cc46ae5SViresh Kumar 	if (!try_module_get(t->owner))
6848cc46ae5SViresh Kumar 		t = NULL;
6858cc46ae5SViresh Kumar 
6868cc46ae5SViresh Kumar unlock:
6878cc46ae5SViresh Kumar 	mutex_unlock(&cpufreq_governor_mutex);
6888cc46ae5SViresh Kumar 
6898cc46ae5SViresh Kumar 	return t;
6908cc46ae5SViresh Kumar }
6918cc46ae5SViresh Kumar 
cpufreq_parse_policy(char * str_governor)6921e4f63aeSRafael J. Wysocki static unsigned int cpufreq_parse_policy(char *str_governor)
6931da177e4SLinus Torvalds {
6941e4f63aeSRafael J. Wysocki 	if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN))
6951e4f63aeSRafael J. Wysocki 		return CPUFREQ_POLICY_PERFORMANCE;
6961e4f63aeSRafael J. Wysocki 
6971e4f63aeSRafael J. Wysocki 	if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN))
6981e4f63aeSRafael J. Wysocki 		return CPUFREQ_POLICY_POWERSAVE;
6991e4f63aeSRafael J. Wysocki 
7001e4f63aeSRafael J. Wysocki 	return CPUFREQ_POLICY_UNKNOWN;
701ab05d97aSYue Hu }
702ab05d97aSYue Hu 
703ab05d97aSYue Hu /**
7045ddc6d4eSViresh Kumar  * cpufreq_parse_governor - parse a governor string only for has_target()
7051e4f63aeSRafael J. Wysocki  * @str_governor: Governor name.
706ab05d97aSYue Hu  */
cpufreq_parse_governor(char * str_governor)7071e4f63aeSRafael J. Wysocki static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor)
708ab05d97aSYue Hu {
7091da177e4SLinus Torvalds 	struct cpufreq_governor *t;
7103bcb09a3SJeremy Fitzhardinge 
7118cc46ae5SViresh Kumar 	t = get_governor(str_governor);
7128cc46ae5SViresh Kumar 	if (t)
7138cc46ae5SViresh Kumar 		return t;
7143bcb09a3SJeremy Fitzhardinge 
7158cc46ae5SViresh Kumar 	if (request_module("cpufreq_%s", str_governor))
7161e4f63aeSRafael J. Wysocki 		return NULL;
717045149e6SRafael J. Wysocki 
7188cc46ae5SViresh Kumar 	return get_governor(str_governor);
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds 
721a9909c21SLee Jones /*
722e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
723e08f5f5bSGautham R Shenoy  * print out cpufreq information
7241da177e4SLinus Torvalds  *
7251da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
7261da177e4SLinus Torvalds  * "unsigned int".
7271da177e4SLinus Torvalds  */
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds #define show_one(file_name, object)			\
7301da177e4SLinus Torvalds static ssize_t show_##file_name				\
7311da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
7321da177e4SLinus Torvalds {							\
7331da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
7341da177e4SLinus Torvalds }
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
7371da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
738ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
7391da177e4SLinus Torvalds show_one(scaling_min_freq, min);
7401da177e4SLinus Torvalds show_one(scaling_max_freq, max);
741c034b02eSDirk Brandewie 
arch_freq_get_on_cpu(int cpu)742f8475cefSLen Brown __weak unsigned int arch_freq_get_on_cpu(int cpu)
743f8475cefSLen Brown {
744f8475cefSLen Brown 	return 0;
745f8475cefSLen Brown }
746f8475cefSLen Brown 
show_scaling_cur_freq(struct cpufreq_policy * policy,char * buf)74709347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
748c034b02eSDirk Brandewie {
749c034b02eSDirk Brandewie 	ssize_t ret;
750f8475cefSLen Brown 	unsigned int freq;
751c034b02eSDirk Brandewie 
752f8475cefSLen Brown 	freq = arch_freq_get_on_cpu(policy->cpu);
753f8475cefSLen Brown 	if (freq)
754f8475cefSLen Brown 		ret = sprintf(buf, "%u\n", freq);
755681fe684SViresh Kumar 	else if (cpufreq_driver->setpolicy && cpufreq_driver->get)
756c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
757c034b02eSDirk Brandewie 	else
758c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", policy->cur);
759c034b02eSDirk Brandewie 	return ret;
760c034b02eSDirk Brandewie }
7611da177e4SLinus Torvalds 
762a9909c21SLee Jones /*
7631da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
7641da177e4SLinus Torvalds  */
7651da177e4SLinus Torvalds #define store_one(file_name, object)			\
7661da177e4SLinus Torvalds static ssize_t store_##file_name					\
7671da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
7681da177e4SLinus Torvalds {									\
76918c49926SViresh Kumar 	unsigned long val;						\
77018c49926SViresh Kumar 	int ret;							\
7711da177e4SLinus Torvalds 									\
772877d5cd2Sqinyu 	ret = kstrtoul(buf, 0, &val);					\
773877d5cd2Sqinyu 	if (ret)							\
774877d5cd2Sqinyu 		return ret;						\
7751da177e4SLinus Torvalds 									\
7763000ce3cSRafael J. Wysocki 	ret = freq_qos_update_request(policy->object##_freq_req, val);\
77718c49926SViresh Kumar 	return ret >= 0 ? count : ret;					\
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds store_one(scaling_min_freq, min);
7811da177e4SLinus Torvalds store_one(scaling_max_freq, max);
7821da177e4SLinus Torvalds 
783a9909c21SLee Jones /*
7841da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
7851da177e4SLinus Torvalds  */
show_cpuinfo_cur_freq(struct cpufreq_policy * policy,char * buf)786e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
787e08f5f5bSGautham R Shenoy 					char *buf)
7881da177e4SLinus Torvalds {
789d92d50a4SViresh Kumar 	unsigned int cur_freq = __cpufreq_get(policy);
7909b4f603eSRafael J. Wysocki 
7919b4f603eSRafael J. Wysocki 	if (cur_freq)
7921da177e4SLinus Torvalds 		return sprintf(buf, "%u\n", cur_freq);
7939b4f603eSRafael J. Wysocki 
7949b4f603eSRafael J. Wysocki 	return sprintf(buf, "<unknown>\n");
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
797a9909c21SLee Jones /*
7981da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
7991da177e4SLinus Torvalds  */
show_scaling_governor(struct cpufreq_policy * policy,char * buf)800905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
8011da177e4SLinus Torvalds {
8021da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
8031da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
8041da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
8051da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
8061da177e4SLinus Torvalds 	else if (policy->governor)
8074b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
80829464f28SDave Jones 				policy->governor->name);
8091da177e4SLinus Torvalds 	return -EINVAL;
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
812a9909c21SLee Jones /*
8131da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
8141da177e4SLinus Torvalds  */
store_scaling_governor(struct cpufreq_policy * policy,const char * buf,size_t count)8151da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
8161da177e4SLinus Torvalds 					const char *buf, size_t count)
8171da177e4SLinus Torvalds {
8181da177e4SLinus Torvalds 	char str_governor[16];
8191e4f63aeSRafael J. Wysocki 	int ret;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
8221da177e4SLinus Torvalds 	if (ret != 1)
8231da177e4SLinus Torvalds 		return -EINVAL;
8241da177e4SLinus Torvalds 
825ab05d97aSYue Hu 	if (cpufreq_driver->setpolicy) {
8261e4f63aeSRafael J. Wysocki 		unsigned int new_pol;
8271e4f63aeSRafael J. Wysocki 
8281e4f63aeSRafael J. Wysocki 		new_pol = cpufreq_parse_policy(str_governor);
8291e4f63aeSRafael J. Wysocki 		if (!new_pol)
830ab05d97aSYue Hu 			return -EINVAL;
8311e4f63aeSRafael J. Wysocki 
8321e4f63aeSRafael J. Wysocki 		ret = cpufreq_set_policy(policy, NULL, new_pol);
833ab05d97aSYue Hu 	} else {
8341e4f63aeSRafael J. Wysocki 		struct cpufreq_governor *new_gov;
8351e4f63aeSRafael J. Wysocki 
8361e4f63aeSRafael J. Wysocki 		new_gov = cpufreq_parse_governor(str_governor);
8371e4f63aeSRafael J. Wysocki 		if (!new_gov)
8381da177e4SLinus Torvalds 			return -EINVAL;
8391e4f63aeSRafael J. Wysocki 
8401e4f63aeSRafael J. Wysocki 		ret = cpufreq_set_policy(policy, new_gov,
8411e4f63aeSRafael J. Wysocki 					 CPUFREQ_POLICY_UNKNOWN);
8421e4f63aeSRafael J. Wysocki 
8431e4f63aeSRafael J. Wysocki 		module_put(new_gov->owner);
844ab05d97aSYue Hu 	}
8451da177e4SLinus Torvalds 
84688dc4384SViresh Kumar 	return ret ? ret : count;
8471da177e4SLinus Torvalds }
8481da177e4SLinus Torvalds 
849a9909c21SLee Jones /*
8501da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
8511da177e4SLinus Torvalds  */
show_scaling_driver(struct cpufreq_policy * policy,char * buf)8521da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
8531da177e4SLinus Torvalds {
8541c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
8551da177e4SLinus Torvalds }
8561da177e4SLinus Torvalds 
857a9909c21SLee Jones /*
8581da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
8591da177e4SLinus Torvalds  */
show_scaling_available_governors(struct cpufreq_policy * policy,char * buf)8601da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
8611da177e4SLinus Torvalds 						char *buf)
8621da177e4SLinus Torvalds {
8631da177e4SLinus Torvalds 	ssize_t i = 0;
8641da177e4SLinus Torvalds 	struct cpufreq_governor *t;
8651da177e4SLinus Torvalds 
8669c0ebcf7SViresh Kumar 	if (!has_target()) {
8671da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
8681da177e4SLinus Torvalds 		goto out;
8691da177e4SLinus Torvalds 	}
8701da177e4SLinus Torvalds 
8718cc46ae5SViresh Kumar 	mutex_lock(&cpufreq_governor_mutex);
872f7b27061SViresh Kumar 	for_each_governor(t) {
87329464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
87429464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
8758cc46ae5SViresh Kumar 			break;
8764b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
8771da177e4SLinus Torvalds 	}
8788cc46ae5SViresh Kumar 	mutex_unlock(&cpufreq_governor_mutex);
8791da177e4SLinus Torvalds out:
8801da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
8811da177e4SLinus Torvalds 	return i;
8821da177e4SLinus Torvalds }
883e8628dd0SDarrick J. Wong 
cpufreq_show_cpus(const struct cpumask * mask,char * buf)884f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
8851da177e4SLinus Torvalds {
8861da177e4SLinus Torvalds 	ssize_t i = 0;
8871da177e4SLinus Torvalds 	unsigned int cpu;
8881da177e4SLinus Torvalds 
889835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
8901da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u ", cpu);
8911da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
8921da177e4SLinus Torvalds 			break;
8931da177e4SLinus Torvalds 	}
894336e5128SViresh Kumar 
895336e5128SViresh Kumar 	/* Remove the extra space at the end */
896336e5128SViresh Kumar 	i--;
897336e5128SViresh Kumar 
8981da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
8991da177e4SLinus Torvalds 	return i;
9001da177e4SLinus Torvalds }
901f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
9021da177e4SLinus Torvalds 
903a9909c21SLee Jones /*
904e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
905e8628dd0SDarrick J. Wong  * hw coordination is in use
906e8628dd0SDarrick J. Wong  */
show_related_cpus(struct cpufreq_policy * policy,char * buf)907e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
908e8628dd0SDarrick J. Wong {
909f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
910e8628dd0SDarrick J. Wong }
911e8628dd0SDarrick J. Wong 
912a9909c21SLee Jones /*
913e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
914e8628dd0SDarrick J. Wong  */
show_affected_cpus(struct cpufreq_policy * policy,char * buf)915e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
916e8628dd0SDarrick J. Wong {
917f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
918e8628dd0SDarrick J. Wong }
919e8628dd0SDarrick J. Wong 
store_scaling_setspeed(struct cpufreq_policy * policy,const char * buf,size_t count)9209e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
9219e76988eSVenki Pallipadi 					const char *buf, size_t count)
9229e76988eSVenki Pallipadi {
9239e76988eSVenki Pallipadi 	unsigned int freq = 0;
9249e76988eSVenki Pallipadi 	unsigned int ret;
9259e76988eSVenki Pallipadi 
926879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
9279e76988eSVenki Pallipadi 		return -EINVAL;
9289e76988eSVenki Pallipadi 
9299e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
9309e76988eSVenki Pallipadi 	if (ret != 1)
9319e76988eSVenki Pallipadi 		return -EINVAL;
9329e76988eSVenki Pallipadi 
9339e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
9349e76988eSVenki Pallipadi 
9359e76988eSVenki Pallipadi 	return count;
9369e76988eSVenki Pallipadi }
9379e76988eSVenki Pallipadi 
show_scaling_setspeed(struct cpufreq_policy * policy,char * buf)9389e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
9399e76988eSVenki Pallipadi {
940879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
9419e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
9429e76988eSVenki Pallipadi 
9439e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
9449e76988eSVenki Pallipadi }
9451da177e4SLinus Torvalds 
946a9909c21SLee Jones /*
9478bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
948e2f74f35SThomas Renninger  */
show_bios_limit(struct cpufreq_policy * policy,char * buf)949e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
950e2f74f35SThomas Renninger {
951e2f74f35SThomas Renninger 	unsigned int limit;
952e2f74f35SThomas Renninger 	int ret;
9531c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
954e2f74f35SThomas Renninger 	if (!ret)
955e2f74f35SThomas Renninger 		return sprintf(buf, "%u\n", limit);
956e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
957e2f74f35SThomas Renninger }
958e2f74f35SThomas Renninger 
9596dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
9606dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
9616dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
9626dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
9636dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
9646dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
9656dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
9666dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
9676dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
9686dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
9696dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
9706dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
9716dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
9726dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
9731da177e4SLinus Torvalds 
974fe262d5cSGreg Kroah-Hartman static struct attribute *cpufreq_attrs[] = {
9751da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
9761da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
977ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
9781da177e4SLinus Torvalds 	&scaling_min_freq.attr,
9791da177e4SLinus Torvalds 	&scaling_max_freq.attr,
9801da177e4SLinus Torvalds 	&affected_cpus.attr,
981e8628dd0SDarrick J. Wong 	&related_cpus.attr,
9821da177e4SLinus Torvalds 	&scaling_governor.attr,
9831da177e4SLinus Torvalds 	&scaling_driver.attr,
9841da177e4SLinus Torvalds 	&scaling_available_governors.attr,
9859e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
9861da177e4SLinus Torvalds 	NULL
9871da177e4SLinus Torvalds };
988fe262d5cSGreg Kroah-Hartman ATTRIBUTE_GROUPS(cpufreq);
9891da177e4SLinus Torvalds 
9901da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
9911da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
9921da177e4SLinus Torvalds 
show(struct kobject * kobj,struct attribute * attr,char * buf)9931da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
9941da177e4SLinus Torvalds {
9951da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
9961da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
997d4627a28SSchspa Shi 	ssize_t ret = -EBUSY;
9986eed9404SViresh Kumar 
999e6e8df07SKai Shen 	if (!fattr->show)
1000e6e8df07SKai Shen 		return -EIO;
1001e6e8df07SKai Shen 
1002ad7722daSviresh kumar 	down_read(&policy->rwsem);
1003d4627a28SSchspa Shi 	if (likely(!policy_is_inactive(policy)))
1004e08f5f5bSGautham R Shenoy 		ret = fattr->show(policy, buf);
1005ad7722daSviresh kumar 	up_read(&policy->rwsem);
10061b750e3bSViresh Kumar 
10071da177e4SLinus Torvalds 	return ret;
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds 
store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t count)10101da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
10111da177e4SLinus Torvalds 		     const char *buf, size_t count)
10121da177e4SLinus Torvalds {
10131da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
10141da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
1015d4627a28SSchspa Shi 	ssize_t ret = -EBUSY;
10166eed9404SViresh Kumar 
1017e6e8df07SKai Shen 	if (!fattr->store)
1018e6e8df07SKai Shen 		return -EIO;
1019e6e8df07SKai Shen 
1020ad7722daSviresh kumar 	down_write(&policy->rwsem);
1021d4627a28SSchspa Shi 	if (likely(!policy_is_inactive(policy)))
1022e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
1023ad7722daSviresh kumar 	up_write(&policy->rwsem);
10244f750c93SSrivatsa S. Bhat 
10251da177e4SLinus Torvalds 	return ret;
10261da177e4SLinus Torvalds }
10271da177e4SLinus Torvalds 
cpufreq_sysfs_release(struct kobject * kobj)10281da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
10291da177e4SLinus Torvalds {
10301da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
10312d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
10321da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
10331da177e4SLinus Torvalds }
10341da177e4SLinus Torvalds 
103552cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
10361da177e4SLinus Torvalds 	.show	= show,
10371da177e4SLinus Torvalds 	.store	= store,
10381da177e4SLinus Torvalds };
10391da177e4SLinus Torvalds 
1040108fcad9SThomas Weißschuh static const struct kobj_type ktype_cpufreq = {
10411da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
1042fe262d5cSGreg Kroah-Hartman 	.default_groups	= cpufreq_groups,
10431da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
10441da177e4SLinus Torvalds };
10451da177e4SLinus Torvalds 
add_cpu_dev_symlink(struct cpufreq_policy * policy,unsigned int cpu,struct device * dev)10462c1b5a84SXiongfeng Wang static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu,
10472c1b5a84SXiongfeng Wang 				struct device *dev)
104887549141SViresh Kumar {
104967d874c3SViresh Kumar 	if (unlikely(!dev))
10502f0ba790SRafael J. Wysocki 		return;
10512f0ba790SRafael J. Wysocki 
10522f0ba790SRafael J. Wysocki 	if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
10532f0ba790SRafael J. Wysocki 		return;
10542f0ba790SRafael J. Wysocki 
105526619804SViresh Kumar 	dev_dbg(dev, "%s: Adding symlink\n", __func__);
10562f0ba790SRafael J. Wysocki 	if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
10572f0ba790SRafael J. Wysocki 		dev_err(dev, "cpufreq symlink creation failed\n");
105887549141SViresh Kumar }
105987549141SViresh Kumar 
remove_cpu_dev_symlink(struct cpufreq_policy * policy,int cpu,struct device * dev)10605c84c1b8SViresh Kumar static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu,
106126619804SViresh Kumar 				   struct device *dev)
106287549141SViresh Kumar {
106326619804SViresh Kumar 	dev_dbg(dev, "%s: Removing symlink\n", __func__);
106426619804SViresh Kumar 	sysfs_remove_link(&dev->kobj, "cpufreq");
10655c84c1b8SViresh Kumar 	cpumask_clear_cpu(cpu, policy->real_cpus);
106687549141SViresh Kumar }
106787549141SViresh Kumar 
cpufreq_add_dev_interface(struct cpufreq_policy * policy)1068d9612a49SRafael J. Wysocki static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
1069909a694eSDave Jones {
1070909a694eSDave Jones 	struct freq_attr **drv_attr;
1071909a694eSDave Jones 	int ret = 0;
1072909a694eSDave Jones 
1073909a694eSDave Jones 	/* set up files for this cpu device */
10741c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
1075f13f1184SViresh Kumar 	while (drv_attr && *drv_attr) {
1076909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1077909a694eSDave Jones 		if (ret)
10786d4e81edSTomeu Vizoso 			return ret;
1079909a694eSDave Jones 		drv_attr++;
1080909a694eSDave Jones 	}
10811c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
1082909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1083909a694eSDave Jones 		if (ret)
10846d4e81edSTomeu Vizoso 			return ret;
1085909a694eSDave Jones 	}
1086c034b02eSDirk Brandewie 
1087909a694eSDave Jones 	ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1088909a694eSDave Jones 	if (ret)
10896d4e81edSTomeu Vizoso 		return ret;
1090c034b02eSDirk Brandewie 
10911c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
1092e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1093e2f74f35SThomas Renninger 		if (ret)
10946d4e81edSTomeu Vizoso 			return ret;
1095e2f74f35SThomas Renninger 	}
1096909a694eSDave Jones 
1097218a06a7SJie Zhan 	if (cpufreq_boost_supported()) {
1098218a06a7SJie Zhan 		ret = sysfs_create_file(&policy->kobj, &local_boost.attr);
1099218a06a7SJie Zhan 		if (ret)
1100218a06a7SJie Zhan 			return ret;
1101218a06a7SJie Zhan 	}
1102218a06a7SJie Zhan 
110326619804SViresh Kumar 	return 0;
1104e18f1682SSrivatsa S. Bhat }
1105e18f1682SSrivatsa S. Bhat 
cpufreq_init_policy(struct cpufreq_policy * policy)11067f0fa40fSViresh Kumar static int cpufreq_init_policy(struct cpufreq_policy *policy)
1107e18f1682SSrivatsa S. Bhat {
11081e4f63aeSRafael J. Wysocki 	struct cpufreq_governor *gov = NULL;
11091e4f63aeSRafael J. Wysocki 	unsigned int pol = CPUFREQ_POLICY_UNKNOWN;
11108cc46ae5SViresh Kumar 	int ret;
1111ab05d97aSYue Hu 
1112ab05d97aSYue Hu 	if (has_target()) {
11131e4f63aeSRafael J. Wysocki 		/* Update policy governor to the one used before hotplug. */
11148cc46ae5SViresh Kumar 		gov = get_governor(policy->last_governor);
1115de1df26bSRafael J. Wysocki 		if (gov) {
11166e2c89d1Sviresh kumar 			pr_debug("Restoring governor %s for cpu %d\n",
11178412b456SQuentin Perret 				 gov->name, policy->cpu);
11181e4f63aeSRafael J. Wysocki 		} else {
11198412b456SQuentin Perret 			gov = get_governor(default_governor);
1120de1df26bSRafael J. Wysocki 		}
11218412b456SQuentin Perret 
11228412b456SQuentin Perret 		if (!gov) {
11238412b456SQuentin Perret 			gov = cpufreq_default_governor();
11248412b456SQuentin Perret 			__module_get(gov->owner);
11258412b456SQuentin Perret 		}
11268412b456SQuentin Perret 
1127ab05d97aSYue Hu 	} else {
11288412b456SQuentin Perret 
112969030dd1SSrinivas Pandruvada 		/* Use the default policy if there is no last_policy. */
1130ab05d97aSYue Hu 		if (policy->last_policy) {
11311e4f63aeSRafael J. Wysocki 			pol = policy->last_policy;
11328412b456SQuentin Perret 		} else {
11338412b456SQuentin Perret 			pol = cpufreq_parse_policy(default_governor);
1134f5739cb0SRafael J. Wysocki 			/*
11358412b456SQuentin Perret 			 * In case the default governor is neither "performance"
1136f5739cb0SRafael J. Wysocki 			 * nor "powersave", fall back to the initial policy
1137f5739cb0SRafael J. Wysocki 			 * value set by the driver.
1138f5739cb0SRafael J. Wysocki 			 */
1139f5739cb0SRafael J. Wysocki 			if (pol == CPUFREQ_POLICY_UNKNOWN)
1140f5739cb0SRafael J. Wysocki 				pol = policy->policy;
114169030dd1SSrinivas Pandruvada 		}
1142f5739cb0SRafael J. Wysocki 		if (pol != CPUFREQ_POLICY_PERFORMANCE &&
1143f5739cb0SRafael J. Wysocki 		    pol != CPUFREQ_POLICY_POWERSAVE)
1144f5739cb0SRafael J. Wysocki 			return -ENODATA;
1145ab05d97aSYue Hu 	}
1146ab05d97aSYue Hu 
11478cc46ae5SViresh Kumar 	ret = cpufreq_set_policy(policy, gov, pol);
11488cc46ae5SViresh Kumar 	if (gov)
11498cc46ae5SViresh Kumar 		module_put(gov->owner);
11508cc46ae5SViresh Kumar 
11518cc46ae5SViresh Kumar 	return ret;
1152909a694eSDave Jones }
1153909a694eSDave Jones 
cpufreq_add_policy_cpu(struct cpufreq_policy * policy,unsigned int cpu)1154d9612a49SRafael J. Wysocki static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1155fcf80582SViresh Kumar {
11569c0ebcf7SViresh Kumar 	int ret = 0;
1157fcf80582SViresh Kumar 
1158bb29ae15SViresh Kumar 	/* Has this CPU been taken care of already? */
1159bb29ae15SViresh Kumar 	if (cpumask_test_cpu(cpu, policy->cpus))
1160bb29ae15SViresh Kumar 		return 0;
1161bb29ae15SViresh Kumar 
116249f18560SViresh Kumar 	down_write(&policy->rwsem);
116345482c70SRafael J. Wysocki 	if (has_target())
116445482c70SRafael J. Wysocki 		cpufreq_stop_governor(policy);
1165fcf80582SViresh Kumar 
1166fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
11672eaa3e2dSViresh Kumar 
11689c0ebcf7SViresh Kumar 	if (has_target()) {
11690a300767SRafael J. Wysocki 		ret = cpufreq_start_governor(policy);
117049f18560SViresh Kumar 		if (ret)
11713de9bdebSViresh Kumar 			pr_err("%s: Failed to start governor\n", __func__);
1172820c6ca2SViresh Kumar 	}
117349f18560SViresh Kumar 	up_write(&policy->rwsem);
1174fcf80582SViresh Kumar 	return ret;
1175fcf80582SViresh Kumar }
1176fcf80582SViresh Kumar 
refresh_frequency_limits(struct cpufreq_policy * policy)1177c57b25bdSViresh Kumar void refresh_frequency_limits(struct cpufreq_policy *policy)
117870a59fdeSViresh Kumar {
117967d874c3SViresh Kumar 	if (!policy_is_inactive(policy)) {
118070a59fdeSViresh Kumar 		pr_debug("updating policy for CPU %u\n", policy->cpu);
118170a59fdeSViresh Kumar 
11821e4f63aeSRafael J. Wysocki 		cpufreq_set_policy(policy, policy->governor, policy->policy);
118370a59fdeSViresh Kumar 	}
118467d874c3SViresh Kumar }
1185c57b25bdSViresh Kumar EXPORT_SYMBOL(refresh_frequency_limits);
118670a59fdeSViresh Kumar 
handle_update(struct work_struct * work)118711eb69b9SViresh Kumar static void handle_update(struct work_struct *work)
118811eb69b9SViresh Kumar {
118911eb69b9SViresh Kumar 	struct cpufreq_policy *policy =
119011eb69b9SViresh Kumar 		container_of(work, struct cpufreq_policy, update);
119170a59fdeSViresh Kumar 
119270a59fdeSViresh Kumar 	pr_debug("handle_update for cpu %u called\n", policy->cpu);
119367d874c3SViresh Kumar 	down_write(&policy->rwsem);
119470a59fdeSViresh Kumar 	refresh_frequency_limits(policy);
119567d874c3SViresh Kumar 	up_write(&policy->rwsem);
1196fcf80582SViresh Kumar }
11971da177e4SLinus Torvalds 
cpufreq_notifier_min(struct notifier_block * nb,unsigned long freq,void * data)119867d874c3SViresh Kumar static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq,
119967d874c3SViresh Kumar 				void *data)
12008414809cSSrivatsa S. Bhat {
120167d874c3SViresh Kumar 	struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min);
1202e9698cc5SSrivatsa S. Bhat 
120367d874c3SViresh Kumar 	schedule_work(&policy->update);
120467d874c3SViresh Kumar 	return 0;
1205edd4a893SViresh Kumar }
1206edd4a893SViresh Kumar 
cpufreq_notifier_max(struct notifier_block * nb,unsigned long freq,void * data)120767d874c3SViresh Kumar static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq,
120867d874c3SViresh Kumar 				void *data)
120967d874c3SViresh Kumar {
121067d874c3SViresh Kumar 	struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max);
1211ad7722daSviresh kumar 
121267d874c3SViresh Kumar 	schedule_work(&policy->update);
121367d874c3SViresh Kumar 	return 0;
1214e9698cc5SSrivatsa S. Bhat }
1215e9698cc5SSrivatsa S. Bhat 
cpufreq_policy_put_kobj(struct cpufreq_policy * policy)1216f9f41e3eSViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
121742f921a6SViresh Kumar {
121842f921a6SViresh Kumar 	struct kobject *kobj;
121942f921a6SViresh Kumar 	struct completion *cmp;
122042f921a6SViresh Kumar 
122187549141SViresh Kumar 	down_write(&policy->rwsem);
12221aefc75bSRafael J. Wysocki 	cpufreq_stats_free_table(policy);
122342f921a6SViresh Kumar 	kobj = &policy->kobj;
122442f921a6SViresh Kumar 	cmp = &policy->kobj_unregister;
122587549141SViresh Kumar 	up_write(&policy->rwsem);
122642f921a6SViresh Kumar 	kobject_put(kobj);
122742f921a6SViresh Kumar 
122842f921a6SViresh Kumar 	/*
122942f921a6SViresh Kumar 	 * We need to make sure that the underlying kobj is
123042f921a6SViresh Kumar 	 * actually not referenced anymore by anybody before we
123142f921a6SViresh Kumar 	 * proceed with unloading.
123242f921a6SViresh Kumar 	 */
123342f921a6SViresh Kumar 	pr_debug("waiting for dropping of refcount\n");
123442f921a6SViresh Kumar 	wait_for_completion(cmp);
123542f921a6SViresh Kumar 	pr_debug("wait complete\n");
123642f921a6SViresh Kumar }
123742f921a6SViresh Kumar 
cpufreq_policy_alloc(unsigned int cpu)123867d874c3SViresh Kumar static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
123967d874c3SViresh Kumar {
124067d874c3SViresh Kumar 	struct cpufreq_policy *policy;
124167d874c3SViresh Kumar 	struct device *dev = get_cpu_device(cpu);
124267d874c3SViresh Kumar 	int ret;
124367d874c3SViresh Kumar 
124467d874c3SViresh Kumar 	if (!dev)
124567d874c3SViresh Kumar 		return NULL;
124667d874c3SViresh Kumar 
124767d874c3SViresh Kumar 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
124867d874c3SViresh Kumar 	if (!policy)
124967d874c3SViresh Kumar 		return NULL;
125067d874c3SViresh Kumar 
125167d874c3SViresh Kumar 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
125267d874c3SViresh Kumar 		goto err_free_policy;
125367d874c3SViresh Kumar 
125467d874c3SViresh Kumar 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
125567d874c3SViresh Kumar 		goto err_free_cpumask;
125667d874c3SViresh Kumar 
125767d874c3SViresh Kumar 	if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
125867d874c3SViresh Kumar 		goto err_free_rcpumask;
125967d874c3SViresh Kumar 
12605c510548SYongqiang Liu 	init_completion(&policy->kobj_unregister);
126167d874c3SViresh Kumar 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
126267d874c3SViresh Kumar 				   cpufreq_global_kobject, "policy%u", cpu);
126367d874c3SViresh Kumar 	if (ret) {
126467d874c3SViresh Kumar 		dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret);
126567d874c3SViresh Kumar 		/*
126667d874c3SViresh Kumar 		 * The entire policy object will be freed below, but the extra
126767d874c3SViresh Kumar 		 * memory allocated for the kobject name needs to be freed by
126867d874c3SViresh Kumar 		 * releasing the kobject.
126967d874c3SViresh Kumar 		 */
127067d874c3SViresh Kumar 		kobject_put(&policy->kobj);
127167d874c3SViresh Kumar 		goto err_free_real_cpus;
127267d874c3SViresh Kumar 	}
127367d874c3SViresh Kumar 
12743000ce3cSRafael J. Wysocki 	freq_constraints_init(&policy->constraints);
12753000ce3cSRafael J. Wysocki 
127667d874c3SViresh Kumar 	policy->nb_min.notifier_call = cpufreq_notifier_min;
127767d874c3SViresh Kumar 	policy->nb_max.notifier_call = cpufreq_notifier_max;
127867d874c3SViresh Kumar 
12793000ce3cSRafael J. Wysocki 	ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN,
12803000ce3cSRafael J. Wysocki 				    &policy->nb_min);
128167d874c3SViresh Kumar 	if (ret) {
1282ba6ea77dSLiao Chang 		dev_err(dev, "Failed to register MIN QoS notifier: %d (CPU%u)\n",
1283ba6ea77dSLiao Chang 			ret, cpu);
128467d874c3SViresh Kumar 		goto err_kobj_remove;
128567d874c3SViresh Kumar 	}
128667d874c3SViresh Kumar 
12873000ce3cSRafael J. Wysocki 	ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX,
12883000ce3cSRafael J. Wysocki 				    &policy->nb_max);
128967d874c3SViresh Kumar 	if (ret) {
1290ba6ea77dSLiao Chang 		dev_err(dev, "Failed to register MAX QoS notifier: %d (CPU%u)\n",
1291ba6ea77dSLiao Chang 			ret, cpu);
129267d874c3SViresh Kumar 		goto err_min_qos_notifier;
129367d874c3SViresh Kumar 	}
129467d874c3SViresh Kumar 
129567d874c3SViresh Kumar 	INIT_LIST_HEAD(&policy->policy_list);
129667d874c3SViresh Kumar 	init_rwsem(&policy->rwsem);
129767d874c3SViresh Kumar 	spin_lock_init(&policy->transition_lock);
129867d874c3SViresh Kumar 	init_waitqueue_head(&policy->transition_wait);
129967d874c3SViresh Kumar 	INIT_WORK(&policy->update, handle_update);
130067d874c3SViresh Kumar 
130167d874c3SViresh Kumar 	policy->cpu = cpu;
130267d874c3SViresh Kumar 	return policy;
130367d874c3SViresh Kumar 
130467d874c3SViresh Kumar err_min_qos_notifier:
13053000ce3cSRafael J. Wysocki 	freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
13063000ce3cSRafael J. Wysocki 				 &policy->nb_min);
130767d874c3SViresh Kumar err_kobj_remove:
130867d874c3SViresh Kumar 	cpufreq_policy_put_kobj(policy);
130967d874c3SViresh Kumar err_free_real_cpus:
131067d874c3SViresh Kumar 	free_cpumask_var(policy->real_cpus);
131167d874c3SViresh Kumar err_free_rcpumask:
131267d874c3SViresh Kumar 	free_cpumask_var(policy->related_cpus);
131367d874c3SViresh Kumar err_free_cpumask:
131467d874c3SViresh Kumar 	free_cpumask_var(policy->cpus);
131567d874c3SViresh Kumar err_free_policy:
131667d874c3SViresh Kumar 	kfree(policy);
131767d874c3SViresh Kumar 
131867d874c3SViresh Kumar 	return NULL;
131967d874c3SViresh Kumar }
132067d874c3SViresh Kumar 
cpufreq_policy_free(struct cpufreq_policy * policy)1321f9f41e3eSViresh Kumar static void cpufreq_policy_free(struct cpufreq_policy *policy)
1322e9698cc5SSrivatsa S. Bhat {
1323988bed09SViresh Kumar 	unsigned long flags;
1324988bed09SViresh Kumar 	int cpu;
1325988bed09SViresh Kumar 
1326a2f6a7acSViresh Kumar 	/*
1327a2f6a7acSViresh Kumar 	 * The callers must ensure the policy is inactive by now, to avoid any
1328a2f6a7acSViresh Kumar 	 * races with show()/store() callbacks.
1329a2f6a7acSViresh Kumar 	 */
1330a2f6a7acSViresh Kumar 	if (unlikely(!policy_is_inactive(policy)))
1331a2f6a7acSViresh Kumar 		pr_warn("%s: Freeing active policy\n", __func__);
1332a2f6a7acSViresh Kumar 
1333988bed09SViresh Kumar 	/* Remove policy from list */
1334988bed09SViresh Kumar 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1335988bed09SViresh Kumar 	list_del(&policy->policy_list);
1336988bed09SViresh Kumar 
1337988bed09SViresh Kumar 	for_each_cpu(cpu, policy->related_cpus)
1338988bed09SViresh Kumar 		per_cpu(cpufreq_cpu_data, cpu) = NULL;
1339988bed09SViresh Kumar 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1340988bed09SViresh Kumar 
13413000ce3cSRafael J. Wysocki 	freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX,
13423000ce3cSRafael J. Wysocki 				 &policy->nb_max);
13433000ce3cSRafael J. Wysocki 	freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
13443000ce3cSRafael J. Wysocki 				 &policy->nb_min);
13456a149036SViresh Kumar 
13466941051dSSudeep Holla 	/* Cancel any pending policy->update work before freeing the policy. */
13476941051dSSudeep Holla 	cancel_work_sync(&policy->update);
13486a149036SViresh Kumar 
13496a149036SViresh Kumar 	if (policy->max_freq_req) {
13506a149036SViresh Kumar 		/*
13511e81d3e0STang Yizhou 		 * Remove max_freq_req after sending CPUFREQ_REMOVE_POLICY
13521e81d3e0STang Yizhou 		 * notification, since CPUFREQ_CREATE_POLICY notification was
13531e81d3e0STang Yizhou 		 * sent after adding max_freq_req earlier.
13546a149036SViresh Kumar 		 */
13556a149036SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
13566a149036SViresh Kumar 					     CPUFREQ_REMOVE_POLICY, policy);
13573000ce3cSRafael J. Wysocki 		freq_qos_remove_request(policy->max_freq_req);
13586a149036SViresh Kumar 	}
13596a149036SViresh Kumar 
13603000ce3cSRafael J. Wysocki 	freq_qos_remove_request(policy->min_freq_req);
136118c49926SViresh Kumar 	kfree(policy->min_freq_req);
136267d874c3SViresh Kumar 
1363f9f41e3eSViresh Kumar 	cpufreq_policy_put_kobj(policy);
1364559ed407SRafael J. Wysocki 	free_cpumask_var(policy->real_cpus);
1365e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
1366e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1367e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1368e9698cc5SSrivatsa S. Bhat }
1369e9698cc5SSrivatsa S. Bhat 
cpufreq_online(unsigned int cpu)13700b275352SRafael J. Wysocki static int cpufreq_online(unsigned int cpu)
13711da177e4SLinus Torvalds {
13727f0c020aSViresh Kumar 	struct cpufreq_policy *policy;
1373194d99c7SRafael J. Wysocki 	bool new_policy;
13741da177e4SLinus Torvalds 	unsigned long flags;
13750b275352SRafael J. Wysocki 	unsigned int j;
13760b275352SRafael J. Wysocki 	int ret;
1377c32b6b8eSAshok Raj 
13780b275352SRafael J. Wysocki 	pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
13796eed9404SViresh Kumar 
1380bb29ae15SViresh Kumar 	/* Check if this CPU already has a policy to manage it */
13819104bb26SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
138211ce707eSRafael J. Wysocki 	if (policy) {
13839104bb26SViresh Kumar 		WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
138411ce707eSRafael J. Wysocki 		if (!policy_is_inactive(policy))
1385d9612a49SRafael J. Wysocki 			return cpufreq_add_policy_cpu(policy, cpu);
13861da177e4SLinus Torvalds 
138711ce707eSRafael J. Wysocki 		/* This is the only online CPU for the policy.  Start over. */
1388194d99c7SRafael J. Wysocki 		new_policy = false;
138911ce707eSRafael J. Wysocki 		down_write(&policy->rwsem);
139011ce707eSRafael J. Wysocki 		policy->cpu = cpu;
139111ce707eSRafael J. Wysocki 		policy->governor = NULL;
139211ce707eSRafael J. Wysocki 	} else {
1393194d99c7SRafael J. Wysocki 		new_policy = true;
1394a34e63b1SRafael J. Wysocki 		policy = cpufreq_policy_alloc(cpu);
1395059019a3SDave Jones 		if (!policy)
1396d4d854d6SRafael J. Wysocki 			return -ENOMEM;
1397514ff1bcSSchspa Shi 		down_write(&policy->rwsem);
139872368d12SRafael J. Wysocki 	}
13990d66b91eSSrivatsa S. Bhat 
140091a12e91SViresh Kumar 	if (!new_policy && cpufreq_driver->online) {
140168315f1aSPierre Gondois 		/* Recover policy->cpus using related_cpus */
140268315f1aSPierre Gondois 		cpumask_copy(policy->cpus, policy->related_cpus);
140368315f1aSPierre Gondois 
140491a12e91SViresh Kumar 		ret = cpufreq_driver->online(policy);
140591a12e91SViresh Kumar 		if (ret) {
140691a12e91SViresh Kumar 			pr_debug("%s: %d: initialization failed\n", __func__,
140791a12e91SViresh Kumar 				 __LINE__);
140891a12e91SViresh Kumar 			goto out_exit_policy;
140991a12e91SViresh Kumar 		}
141091a12e91SViresh Kumar 	} else {
1411835481d9SRusty Russell 		cpumask_copy(policy->cpus, cpumask_of(cpu));
14121da177e4SLinus Torvalds 
141391a12e91SViresh Kumar 		/*
141491a12e91SViresh Kumar 		 * Call driver. From then on the cpufreq must be able
141591a12e91SViresh Kumar 		 * to accept all calls to ->verify and ->setpolicy for this CPU.
14161da177e4SLinus Torvalds 		 */
14171c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->init(policy);
14181da177e4SLinus Torvalds 		if (ret) {
141991a12e91SViresh Kumar 			pr_debug("%s: %d: initialization failed\n", __func__,
142091a12e91SViresh Kumar 				 __LINE__);
14218101f997SViresh Kumar 			goto out_free_policy;
14221da177e4SLinus Torvalds 		}
1423643ae6e8SViresh Kumar 
14249d47d2e7SSibi Sankar 		/* Let the per-policy boost flag mirror the cpufreq_driver boost during init */
1425*e4081843SMario Limonciello 		if (cpufreq_boost_enabled() && policy_has_boost_freq(policy))
1426*e4081843SMario Limonciello 			policy->boost_enabled = true;
14279d47d2e7SSibi Sankar 
14283b718057SRafael J. Wysocki 		/*
14293b718057SRafael J. Wysocki 		 * The initialization has succeeded and the policy is online.
14303b718057SRafael J. Wysocki 		 * If there is a problem with its frequency table, take it
14313b718057SRafael J. Wysocki 		 * offline and drop it.
14323b718057SRafael J. Wysocki 		 */
1433d417e069SViresh Kumar 		ret = cpufreq_table_validate_and_sort(policy);
1434d417e069SViresh Kumar 		if (ret)
14353b718057SRafael J. Wysocki 			goto out_offline_policy;
1436d417e069SViresh Kumar 
14374d1f3a5bSRafael J. Wysocki 		/* related_cpus should at least include policy->cpus. */
14380998a03aSViresh Kumar 		cpumask_copy(policy->related_cpus, policy->cpus);
14394d1f3a5bSRafael J. Wysocki 	}
1440559ed407SRafael J. Wysocki 
14415a7e56a5SViresh Kumar 	/*
14425a7e56a5SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
14435a7e56a5SViresh Kumar 	 * managing offline cpus here.
14445a7e56a5SViresh Kumar 	 */
14455a7e56a5SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
14465a7e56a5SViresh Kumar 
1447194d99c7SRafael J. Wysocki 	if (new_policy) {
14482f0ba790SRafael J. Wysocki 		for_each_cpu(j, policy->related_cpus) {
1449652ed95dSViresh Kumar 			per_cpu(cpufreq_cpu_data, j) = policy;
14502c1b5a84SXiongfeng Wang 			add_cpu_dev_symlink(policy, j, get_cpu_device(j));
14512f0ba790SRafael J. Wysocki 		}
145218c49926SViresh Kumar 
145318c49926SViresh Kumar 		policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req),
145418c49926SViresh Kumar 					       GFP_KERNEL);
1455b96f0384SWang ShaoBo 		if (!policy->min_freq_req) {
1456b96f0384SWang ShaoBo 			ret = -ENOMEM;
145718c49926SViresh Kumar 			goto out_destroy_policy;
1458b96f0384SWang ShaoBo 		}
145918c49926SViresh Kumar 
14603000ce3cSRafael J. Wysocki 		ret = freq_qos_add_request(&policy->constraints,
14613000ce3cSRafael J. Wysocki 					   policy->min_freq_req, FREQ_QOS_MIN,
1462521223d8SRafael J. Wysocki 					   FREQ_QOS_MIN_DEFAULT_VALUE);
146318c49926SViresh Kumar 		if (ret < 0) {
146418c49926SViresh Kumar 			/*
14653000ce3cSRafael J. Wysocki 			 * So we don't call freq_qos_remove_request() for an
146618c49926SViresh Kumar 			 * uninitialized request.
146718c49926SViresh Kumar 			 */
146818c49926SViresh Kumar 			kfree(policy->min_freq_req);
146918c49926SViresh Kumar 			policy->min_freq_req = NULL;
147018c49926SViresh Kumar 			goto out_destroy_policy;
147118c49926SViresh Kumar 		}
147218c49926SViresh Kumar 
147318c49926SViresh Kumar 		/*
147418c49926SViresh Kumar 		 * This must be initialized right here to avoid calling
14753000ce3cSRafael J. Wysocki 		 * freq_qos_remove_request() on uninitialized request in case
147618c49926SViresh Kumar 		 * of errors.
147718c49926SViresh Kumar 		 */
147818c49926SViresh Kumar 		policy->max_freq_req = policy->min_freq_req + 1;
147918c49926SViresh Kumar 
14803000ce3cSRafael J. Wysocki 		ret = freq_qos_add_request(&policy->constraints,
14813000ce3cSRafael J. Wysocki 					   policy->max_freq_req, FREQ_QOS_MAX,
1482521223d8SRafael J. Wysocki 					   FREQ_QOS_MAX_DEFAULT_VALUE);
148318c49926SViresh Kumar 		if (ret < 0) {
148418c49926SViresh Kumar 			policy->max_freq_req = NULL;
148518c49926SViresh Kumar 			goto out_destroy_policy;
148618c49926SViresh Kumar 		}
14876a149036SViresh Kumar 
14886a149036SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
14896a149036SViresh Kumar 				CPUFREQ_CREATE_POLICY, policy);
1490988bed09SViresh Kumar 	}
1491652ed95dSViresh Kumar 
14925ddc6d4eSViresh Kumar 	if (cpufreq_driver->get && has_target()) {
1493da60ce9fSViresh Kumar 		policy->cur = cpufreq_driver->get(policy->cpu);
1494da60ce9fSViresh Kumar 		if (!policy->cur) {
1495b96f0384SWang ShaoBo 			ret = -EIO;
1496da60ce9fSViresh Kumar 			pr_err("%s: ->get() failed\n", __func__);
1497d417e069SViresh Kumar 			goto out_destroy_policy;
1498da60ce9fSViresh Kumar 		}
1499da60ce9fSViresh Kumar 	}
1500da60ce9fSViresh Kumar 
1501d3916691SViresh Kumar 	/*
1502d3916691SViresh Kumar 	 * Sometimes boot loaders set CPU frequency to a value outside of
1503d3916691SViresh Kumar 	 * frequency table present with cpufreq core. In such cases CPU might be
1504d3916691SViresh Kumar 	 * unstable if it has to run on that frequency for long duration of time
1505d3916691SViresh Kumar 	 * and so its better to set it to a frequency which is specified in
1506d3916691SViresh Kumar 	 * freq-table. This also makes cpufreq stats inconsistent as
1507d3916691SViresh Kumar 	 * cpufreq-stats would fail to register because current frequency of CPU
1508d3916691SViresh Kumar 	 * isn't found in freq-table.
1509d3916691SViresh Kumar 	 *
1510d3916691SViresh Kumar 	 * Because we don't want this change to effect boot process badly, we go
1511d3916691SViresh Kumar 	 * for the next freq which is >= policy->cur ('cur' must be set by now,
1512d3916691SViresh Kumar 	 * otherwise we will end up setting freq to lowest of the table as 'cur'
1513d3916691SViresh Kumar 	 * is initialized to zero).
1514d3916691SViresh Kumar 	 *
1515d3916691SViresh Kumar 	 * We are passing target-freq as "policy->cur - 1" otherwise
1516d3916691SViresh Kumar 	 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1517d3916691SViresh Kumar 	 * equal to target-freq.
1518d3916691SViresh Kumar 	 */
1519d3916691SViresh Kumar 	if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1520d3916691SViresh Kumar 	    && has_target()) {
152197148d0aSViresh Kumar 		unsigned int old_freq = policy->cur;
152297148d0aSViresh Kumar 
1523d3916691SViresh Kumar 		/* Are we running at unknown frequency ? */
152497148d0aSViresh Kumar 		ret = cpufreq_frequency_table_get_index(policy, old_freq);
1525d3916691SViresh Kumar 		if (ret == -EINVAL) {
152697148d0aSViresh Kumar 			ret = __cpufreq_driver_target(policy, old_freq - 1,
1527d3916691SViresh Kumar 						      CPUFREQ_RELATION_L);
1528d3916691SViresh Kumar 
1529d3916691SViresh Kumar 			/*
1530d3916691SViresh Kumar 			 * Reaching here after boot in a few seconds may not
1531d3916691SViresh Kumar 			 * mean that system will remain stable at "unknown"
1532d3916691SViresh Kumar 			 * frequency for longer duration. Hence, a BUG_ON().
1533d3916691SViresh Kumar 			 */
1534d3916691SViresh Kumar 			BUG_ON(ret);
153597148d0aSViresh Kumar 			pr_info("%s: CPU%d: Running at unlisted initial frequency: %u KHz, changing to: %u KHz\n",
153697148d0aSViresh Kumar 				__func__, policy->cpu, old_freq, policy->cur);
1537d3916691SViresh Kumar 		}
1538d3916691SViresh Kumar 	}
1539d3916691SViresh Kumar 
1540194d99c7SRafael J. Wysocki 	if (new_policy) {
1541d9612a49SRafael J. Wysocki 		ret = cpufreq_add_dev_interface(policy);
154219d6f7ecSDave Jones 		if (ret)
1543d417e069SViresh Kumar 			goto out_destroy_policy;
15441aefc75bSRafael J. Wysocki 
15451aefc75bSRafael J. Wysocki 		cpufreq_stats_create_table(policy);
1546c88a1f8bSLukasz Majewski 
1547c88a1f8bSLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
1548c88a1f8bSLukasz Majewski 		list_add(&policy->policy_list, &cpufreq_policy_list);
1549c88a1f8bSLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1550c17495b0SViresh Kumar 
1551c17495b0SViresh Kumar 		/*
1552c17495b0SViresh Kumar 		 * Register with the energy model before
1553c17495b0SViresh Kumar 		 * sched_cpufreq_governor_change() is called, which will result
1554c17495b0SViresh Kumar 		 * in rebuilding of the sched domains, which should only be done
1555c17495b0SViresh Kumar 		 * once the energy model is properly initialized for the policy
1556c17495b0SViresh Kumar 		 * first.
1557c17495b0SViresh Kumar 		 *
1558c17495b0SViresh Kumar 		 * Also, this should be called before the policy is registered
1559c17495b0SViresh Kumar 		 * with cooling framework.
1560c17495b0SViresh Kumar 		 */
1561c17495b0SViresh Kumar 		if (cpufreq_driver->register_em)
1562c17495b0SViresh Kumar 			cpufreq_driver->register_em(policy);
1563988bed09SViresh Kumar 	}
15648ff69732SDave Jones 
15657f0fa40fSViresh Kumar 	ret = cpufreq_init_policy(policy);
15667f0fa40fSViresh Kumar 	if (ret) {
15677f0fa40fSViresh Kumar 		pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
15687f0fa40fSViresh Kumar 		       __func__, cpu, ret);
1569d417e069SViresh Kumar 		goto out_destroy_policy;
157008fd8c1cSViresh Kumar 	}
1571e18f1682SSrivatsa S. Bhat 
15724e97b631SViresh Kumar 	up_write(&policy->rwsem);
157308fd8c1cSViresh Kumar 
1574038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
15757c45cf31SViresh Kumar 
15764f774c4aSBjorn Andersson 	/* Callback for handling stuff after policy is ready */
15774f774c4aSBjorn Andersson 	if (cpufreq_driver->ready)
15784f774c4aSBjorn Andersson 		cpufreq_driver->ready(policy);
15794f774c4aSBjorn Andersson 
15803ba4acebSViresh Kumar 	/* Register cpufreq cooling only for a new policy */
15813ba4acebSViresh Kumar 	if (new_policy && cpufreq_thermal_control_enabled(cpufreq_driver))
15825c238a8bSAmit Kucheria 		policy->cdev = of_cpufreq_cooling_register(policy);
15835c238a8bSAmit Kucheria 
15842d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
15851da177e4SLinus Torvalds 
15861da177e4SLinus Torvalds 	return 0;
15871da177e4SLinus Torvalds 
1588d417e069SViresh Kumar out_destroy_policy:
1589b24b6478SViresh Kumar 	for_each_cpu(j, policy->real_cpus)
15905c84c1b8SViresh Kumar 		remove_cpu_dev_symlink(policy, j, get_cpu_device(j));
1591b24b6478SViresh Kumar 
15923b718057SRafael J. Wysocki out_offline_policy:
15933b718057SRafael J. Wysocki 	if (cpufreq_driver->offline)
15943b718057SRafael J. Wysocki 		cpufreq_driver->offline(policy);
15953b718057SRafael J. Wysocki 
1596d417e069SViresh Kumar out_exit_policy:
1597da60ce9fSViresh Kumar 	if (cpufreq_driver->exit)
1598da60ce9fSViresh Kumar 		cpufreq_driver->exit(policy);
15992f0ba790SRafael J. Wysocki 
16008101f997SViresh Kumar out_free_policy:
1601a2f6a7acSViresh Kumar 	cpumask_clear(policy->cpus);
1602514ff1bcSSchspa Shi 	up_write(&policy->rwsem);
1603514ff1bcSSchspa Shi 
1604f9f41e3eSViresh Kumar 	cpufreq_policy_free(policy);
16051da177e4SLinus Torvalds 	return ret;
16061da177e4SLinus Torvalds }
16071da177e4SLinus Torvalds 
16080b275352SRafael J. Wysocki /**
16090b275352SRafael J. Wysocki  * cpufreq_add_dev - the cpufreq interface for a CPU device.
16100b275352SRafael J. Wysocki  * @dev: CPU device.
16110b275352SRafael J. Wysocki  * @sif: Subsystem interface structure pointer (not used)
16120b275352SRafael J. Wysocki  */
cpufreq_add_dev(struct device * dev,struct subsys_interface * sif)16130b275352SRafael J. Wysocki static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
16140b275352SRafael J. Wysocki {
1615a794d613SRafael J. Wysocki 	struct cpufreq_policy *policy;
16160b275352SRafael J. Wysocki 	unsigned cpu = dev->id;
161726619804SViresh Kumar 	int ret;
16180b275352SRafael J. Wysocki 
16190b275352SRafael J. Wysocki 	dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
16200b275352SRafael J. Wysocki 
162126619804SViresh Kumar 	if (cpu_online(cpu)) {
162226619804SViresh Kumar 		ret = cpufreq_online(cpu);
162326619804SViresh Kumar 		if (ret)
162426619804SViresh Kumar 			return ret;
162526619804SViresh Kumar 	}
1626a794d613SRafael J. Wysocki 
162726619804SViresh Kumar 	/* Create sysfs link on CPU registration */
1628a794d613SRafael J. Wysocki 	policy = per_cpu(cpufreq_cpu_data, cpu);
16292f0ba790SRafael J. Wysocki 	if (policy)
16302c1b5a84SXiongfeng Wang 		add_cpu_dev_symlink(policy, cpu, dev);
16312f0ba790SRafael J. Wysocki 
1632a794d613SRafael J. Wysocki 	return 0;
16331da177e4SLinus Torvalds }
16341da177e4SLinus Torvalds 
__cpufreq_offline(unsigned int cpu,struct cpufreq_policy * policy)1635fddd8f86SRafael J. Wysocki static void __cpufreq_offline(unsigned int cpu, struct cpufreq_policy *policy)
16361da177e4SLinus Torvalds {
163769cee714SViresh Kumar 	int ret;
16381da177e4SLinus Torvalds 
163945482c70SRafael J. Wysocki 	if (has_target())
164045482c70SRafael J. Wysocki 		cpufreq_stop_governor(policy);
1641084f3493SThomas Renninger 
16429591becbSViresh Kumar 	cpumask_clear_cpu(cpu, policy->cpus);
16434573237bSViresh Kumar 
16449591becbSViresh Kumar 	if (!policy_is_inactive(policy)) {
1645e1e962c5SRafael J. Wysocki 		/* Nominate a new CPU if necessary. */
1646e1e962c5SRafael J. Wysocki 		if (cpu == policy->cpu)
1647e1e962c5SRafael J. Wysocki 			policy->cpu = cpumask_any(policy->cpus);
1648e1e962c5SRafael J. Wysocki 
1649e1e962c5SRafael J. Wysocki 		/* Start the governor again for the active policy. */
16509591becbSViresh Kumar 		if (has_target()) {
16510a300767SRafael J. Wysocki 			ret = cpufreq_start_governor(policy);
16529591becbSViresh Kumar 			if (ret)
16539591becbSViresh Kumar 				pr_err("%s: Failed to start governor\n", __func__);
16549591becbSViresh Kumar 		}
165569cee714SViresh Kumar 
1656fddd8f86SRafael J. Wysocki 		return;
165769cee714SViresh Kumar 	}
165869cee714SViresh Kumar 
1659e1e962c5SRafael J. Wysocki 	if (has_target())
1660e1e962c5SRafael J. Wysocki 		strncpy(policy->last_governor, policy->governor->name,
1661e1e962c5SRafael J. Wysocki 			CPUFREQ_NAME_LEN);
1662e1e962c5SRafael J. Wysocki 	else
1663e1e962c5SRafael J. Wysocki 		policy->last_policy = policy->policy;
1664e1e962c5SRafael J. Wysocki 
166536be3418SRafael J. Wysocki 	if (has_target())
166636be3418SRafael J. Wysocki 		cpufreq_exit_governor(policy);
16672a998599SRafael J. Wysocki 
16688414809cSSrivatsa S. Bhat 	/*
166991a12e91SViresh Kumar 	 * Perform the ->offline() during light-weight tear-down, as
167091a12e91SViresh Kumar 	 * that allows fast recovery when the CPU comes back.
16718414809cSSrivatsa S. Bhat 	 */
167291a12e91SViresh Kumar 	if (cpufreq_driver->offline) {
167391a12e91SViresh Kumar 		cpufreq_driver->offline(policy);
16743e99f060SViresh Kumar 		return;
167555582bccSSrinivas Pandruvada 	}
16763e99f060SViresh Kumar 
16773e99f060SViresh Kumar 	if (cpufreq_driver->exit)
16783e99f060SViresh Kumar 		cpufreq_driver->exit(policy);
16793e99f060SViresh Kumar 
16803e99f060SViresh Kumar 	policy->freq_table = NULL;
1681fddd8f86SRafael J. Wysocki }
168249f18560SViresh Kumar 
cpufreq_offline(unsigned int cpu)1683fddd8f86SRafael J. Wysocki static int cpufreq_offline(unsigned int cpu)
1684fddd8f86SRafael J. Wysocki {
1685fddd8f86SRafael J. Wysocki 	struct cpufreq_policy *policy;
1686fddd8f86SRafael J. Wysocki 
1687fddd8f86SRafael J. Wysocki 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1688fddd8f86SRafael J. Wysocki 
1689fddd8f86SRafael J. Wysocki 	policy = cpufreq_cpu_get_raw(cpu);
1690fddd8f86SRafael J. Wysocki 	if (!policy) {
1691fddd8f86SRafael J. Wysocki 		pr_debug("%s: No cpu_data found\n", __func__);
1692fddd8f86SRafael J. Wysocki 		return 0;
1693fddd8f86SRafael J. Wysocki 	}
1694fddd8f86SRafael J. Wysocki 
1695fddd8f86SRafael J. Wysocki 	down_write(&policy->rwsem);
1696fddd8f86SRafael J. Wysocki 
1697fddd8f86SRafael J. Wysocki 	__cpufreq_offline(cpu, policy);
1698fddd8f86SRafael J. Wysocki 
169949f18560SViresh Kumar 	up_write(&policy->rwsem);
170027622b06SSebastian Andrzej Siewior 	return 0;
17011da177e4SLinus Torvalds }
17021da177e4SLinus Torvalds 
1703a9909c21SLee Jones /*
170427a862e9SViresh Kumar  * cpufreq_remove_dev - remove a CPU device
1705cedb70afSSrivatsa S. Bhat  *
1706cedb70afSSrivatsa S. Bhat  * Removes the cpufreq interface for a CPU device.
1707cedb70afSSrivatsa S. Bhat  */
cpufreq_remove_dev(struct device * dev,struct subsys_interface * sif)170871db87baSViresh Kumar static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
17095a01f2e8SVenkatesh Pallipadi {
17108a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
171187549141SViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
171287549141SViresh Kumar 
171387549141SViresh Kumar 	if (!policy)
171471db87baSViresh Kumar 		return;
1715ec28297aSVenki Pallipadi 
1716f339f354SRafael J. Wysocki 	down_write(&policy->rwsem);
1717f339f354SRafael J. Wysocki 
171869cee714SViresh Kumar 	if (cpu_online(cpu))
1719f339f354SRafael J. Wysocki 		__cpufreq_offline(cpu, policy);
172087549141SViresh Kumar 
17215c84c1b8SViresh Kumar 	remove_cpu_dev_symlink(policy, cpu, dev);
1722f344dae0SViresh Kumar 
1723f339f354SRafael J. Wysocki 	if (!cpumask_empty(policy->real_cpus)) {
1724f339f354SRafael J. Wysocki 		up_write(&policy->rwsem);
1725f339f354SRafael J. Wysocki 		return;
1726f339f354SRafael J. Wysocki 	}
1727f339f354SRafael J. Wysocki 
17283ba4acebSViresh Kumar 	/*
17293ba4acebSViresh Kumar 	 * Unregister cpufreq cooling once all the CPUs of the policy are
17303ba4acebSViresh Kumar 	 * removed.
17313ba4acebSViresh Kumar 	 */
17323ba4acebSViresh Kumar 	if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
17333ba4acebSViresh Kumar 		cpufreq_cooling_unregister(policy->cdev);
17343ba4acebSViresh Kumar 		policy->cdev = NULL;
17353ba4acebSViresh Kumar 	}
17363ba4acebSViresh Kumar 
173791a12e91SViresh Kumar 	/* We did light-weight exit earlier, do full tear down now */
17383e99f060SViresh Kumar 	if (cpufreq_driver->offline && cpufreq_driver->exit)
173991a12e91SViresh Kumar 		cpufreq_driver->exit(policy);
174091a12e91SViresh Kumar 
1741f339f354SRafael J. Wysocki 	up_write(&policy->rwsem);
1742f339f354SRafael J. Wysocki 
1743f9f41e3eSViresh Kumar 	cpufreq_policy_free(policy);
17445a01f2e8SVenkatesh Pallipadi }
17455a01f2e8SVenkatesh Pallipadi 
17461da177e4SLinus Torvalds /**
1747ec06e586SRafael J. Wysocki  * cpufreq_out_of_sync - Fix up actual and saved CPU frequency difference.
1748ec06e586SRafael J. Wysocki  * @policy: Policy managing CPUs.
1749ec06e586SRafael J. Wysocki  * @new_freq: New CPU frequency.
17501da177e4SLinus Torvalds  *
1751ec06e586SRafael J. Wysocki  * Adjust to the current frequency first and clean up later by either calling
1752ec06e586SRafael J. Wysocki  * cpufreq_update_policy(), or scheduling handle_update().
17531da177e4SLinus Torvalds  */
cpufreq_out_of_sync(struct cpufreq_policy * policy,unsigned int new_freq)1754a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1755e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
17561da177e4SLinus Torvalds {
17571da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1758b43a7ffbSViresh Kumar 
1759e837f9b5SJoe Perches 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1760a1e1dc41SViresh Kumar 		 policy->cur, new_freq);
17611da177e4SLinus Torvalds 
1762a1e1dc41SViresh Kumar 	freqs.old = policy->cur;
17631da177e4SLinus Torvalds 	freqs.new = new_freq;
1764b43a7ffbSViresh Kumar 
17658fec051eSViresh Kumar 	cpufreq_freq_transition_begin(policy, &freqs);
17668fec051eSViresh Kumar 	cpufreq_freq_transition_end(policy, &freqs, 0);
17671da177e4SLinus Torvalds }
17681da177e4SLinus Torvalds 
cpufreq_verify_current_freq(struct cpufreq_policy * policy,bool update)17695980752eSViresh Kumar static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update)
17705980752eSViresh Kumar {
17715980752eSViresh Kumar 	unsigned int new_freq;
17725980752eSViresh Kumar 
17735980752eSViresh Kumar 	new_freq = cpufreq_driver->get(policy->cpu);
17745980752eSViresh Kumar 	if (!new_freq)
17755980752eSViresh Kumar 		return 0;
17765980752eSViresh Kumar 
17775980752eSViresh Kumar 	/*
17785980752eSViresh Kumar 	 * If fast frequency switching is used with the given policy, the check
17795980752eSViresh Kumar 	 * against policy->cur is pointless, so skip it in that case.
17805980752eSViresh Kumar 	 */
17815980752eSViresh Kumar 	if (policy->fast_switch_enabled || !has_target())
17825980752eSViresh Kumar 		return new_freq;
17835980752eSViresh Kumar 
17845980752eSViresh Kumar 	if (policy->cur != new_freq) {
1785f55ae08cSViresh Kumar 		/*
1786f55ae08cSViresh Kumar 		 * For some platforms, the frequency returned by hardware may be
1787f55ae08cSViresh Kumar 		 * slightly different from what is provided in the frequency
1788f55ae08cSViresh Kumar 		 * table, for example hardware may return 499 MHz instead of 500
1789f55ae08cSViresh Kumar 		 * MHz. In such cases it is better to avoid getting into
1790f55ae08cSViresh Kumar 		 * unnecessary frequency updates.
1791f55ae08cSViresh Kumar 		 */
179244295af5SSanjay Chandrashekara 		if (abs(policy->cur - new_freq) < KHZ_PER_MHZ)
1793f55ae08cSViresh Kumar 			return policy->cur;
1794f55ae08cSViresh Kumar 
17955980752eSViresh Kumar 		cpufreq_out_of_sync(policy, new_freq);
17965980752eSViresh Kumar 		if (update)
17975980752eSViresh Kumar 			schedule_work(&policy->update);
17985980752eSViresh Kumar 	}
17995980752eSViresh Kumar 
18005980752eSViresh Kumar 	return new_freq;
18015980752eSViresh Kumar }
18025980752eSViresh Kumar 
18031da177e4SLinus Torvalds /**
18044ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
180595235ca2SVenkatesh Pallipadi  * @cpu: CPU number
180695235ca2SVenkatesh Pallipadi  *
180795235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
180895235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
180995235ca2SVenkatesh Pallipadi  */
cpufreq_quick_get(unsigned int cpu)181095235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
181195235ca2SVenkatesh Pallipadi {
18129e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1813e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
1814c75361c0SRichard Cochran 	unsigned long flags;
181595235ca2SVenkatesh Pallipadi 
1816c75361c0SRichard Cochran 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1817c75361c0SRichard Cochran 
1818c75361c0SRichard Cochran 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1819c75361c0SRichard Cochran 		ret_freq = cpufreq_driver->get(cpu);
1820c75361c0SRichard Cochran 		read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1821c75361c0SRichard Cochran 		return ret_freq;
1822c75361c0SRichard Cochran 	}
1823c75361c0SRichard Cochran 
1824c75361c0SRichard Cochran 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
18259e21ba8bSDirk Brandewie 
18269e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
182795235ca2SVenkatesh Pallipadi 	if (policy) {
1828e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
182995235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
183095235ca2SVenkatesh Pallipadi 	}
183195235ca2SVenkatesh Pallipadi 
18324d34a67dSDave Jones 	return ret_freq;
183395235ca2SVenkatesh Pallipadi }
183495235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
183595235ca2SVenkatesh Pallipadi 
18363d737108SJesse Barnes /**
18373d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
18383d737108SJesse Barnes  * @cpu: CPU number
18393d737108SJesse Barnes  *
18403d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
18413d737108SJesse Barnes  */
cpufreq_quick_get_max(unsigned int cpu)18423d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
18433d737108SJesse Barnes {
18443d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
18453d737108SJesse Barnes 	unsigned int ret_freq = 0;
18463d737108SJesse Barnes 
18473d737108SJesse Barnes 	if (policy) {
18483d737108SJesse Barnes 		ret_freq = policy->max;
18493d737108SJesse Barnes 		cpufreq_cpu_put(policy);
18503d737108SJesse Barnes 	}
18513d737108SJesse Barnes 
18523d737108SJesse Barnes 	return ret_freq;
18533d737108SJesse Barnes }
18543d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
18553d737108SJesse Barnes 
1856bbce8eaaSIonela Voinescu /**
1857bbce8eaaSIonela Voinescu  * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU
1858bbce8eaaSIonela Voinescu  * @cpu: CPU number
1859bbce8eaaSIonela Voinescu  *
1860bbce8eaaSIonela Voinescu  * The default return value is the max_freq field of cpuinfo.
1861bbce8eaaSIonela Voinescu  */
cpufreq_get_hw_max_freq(unsigned int cpu)1862bbce8eaaSIonela Voinescu __weak unsigned int cpufreq_get_hw_max_freq(unsigned int cpu)
1863bbce8eaaSIonela Voinescu {
1864bbce8eaaSIonela Voinescu 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1865bbce8eaaSIonela Voinescu 	unsigned int ret_freq = 0;
1866bbce8eaaSIonela Voinescu 
1867bbce8eaaSIonela Voinescu 	if (policy) {
1868bbce8eaaSIonela Voinescu 		ret_freq = policy->cpuinfo.max_freq;
1869bbce8eaaSIonela Voinescu 		cpufreq_cpu_put(policy);
1870bbce8eaaSIonela Voinescu 	}
1871bbce8eaaSIonela Voinescu 
1872bbce8eaaSIonela Voinescu 	return ret_freq;
1873bbce8eaaSIonela Voinescu }
1874bbce8eaaSIonela Voinescu EXPORT_SYMBOL(cpufreq_get_hw_max_freq);
1875bbce8eaaSIonela Voinescu 
__cpufreq_get(struct cpufreq_policy * policy)1876d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
18771da177e4SLinus Torvalds {
18784db7c34cSYue Hu 	if (unlikely(policy_is_inactive(policy)))
18795980752eSViresh Kumar 		return 0;
18801da177e4SLinus Torvalds 
18815980752eSViresh Kumar 	return cpufreq_verify_current_freq(policy, true);
18825a01f2e8SVenkatesh Pallipadi }
18831da177e4SLinus Torvalds 
18845a01f2e8SVenkatesh Pallipadi /**
18855a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
18865a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
18875a01f2e8SVenkatesh Pallipadi  *
18885a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
18895a01f2e8SVenkatesh Pallipadi  */
cpufreq_get(unsigned int cpu)18905a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
18915a01f2e8SVenkatesh Pallipadi {
1892999976e0SAaron Plattner 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
18935a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
18945a01f2e8SVenkatesh Pallipadi 
1895999976e0SAaron Plattner 	if (policy) {
1896ad7722daSviresh kumar 		down_read(&policy->rwsem);
18974db7c34cSYue Hu 		if (cpufreq_driver->get)
1898d92d50a4SViresh Kumar 			ret_freq = __cpufreq_get(policy);
1899ad7722daSviresh kumar 		up_read(&policy->rwsem);
1900999976e0SAaron Plattner 
1901999976e0SAaron Plattner 		cpufreq_cpu_put(policy);
1902999976e0SAaron Plattner 	}
19036eed9404SViresh Kumar 
19044d34a67dSDave Jones 	return ret_freq;
19051da177e4SLinus Torvalds }
19061da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
19071da177e4SLinus Torvalds 
19088a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
19098a25a2fdSKay Sievers 	.name		= "cpufreq",
19108a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
19118a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
19128a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1913e00e56dfSRafael J. Wysocki };
1914e00e56dfSRafael J. Wysocki 
1915e28867eaSViresh Kumar /*
1916e28867eaSViresh Kumar  * In case platform wants some specific frequency to be configured
1917e28867eaSViresh Kumar  * during suspend..
191842d4dc3fSBenjamin Herrenschmidt  */
cpufreq_generic_suspend(struct cpufreq_policy * policy)1919e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy)
192042d4dc3fSBenjamin Herrenschmidt {
1921e28867eaSViresh Kumar 	int ret;
19224bc5d341SDave Jones 
1923e28867eaSViresh Kumar 	if (!policy->suspend_freq) {
1924201f3716SBartlomiej Zolnierkiewicz 		pr_debug("%s: suspend_freq not defined\n", __func__);
1925201f3716SBartlomiej Zolnierkiewicz 		return 0;
192642d4dc3fSBenjamin Herrenschmidt 	}
192742d4dc3fSBenjamin Herrenschmidt 
1928e28867eaSViresh Kumar 	pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1929e28867eaSViresh Kumar 			policy->suspend_freq);
1930e28867eaSViresh Kumar 
1931e28867eaSViresh Kumar 	ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1932e28867eaSViresh Kumar 			CPUFREQ_RELATION_H);
1933e28867eaSViresh Kumar 	if (ret)
1934e28867eaSViresh Kumar 		pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1935e28867eaSViresh Kumar 				__func__, policy->suspend_freq, ret);
1936e28867eaSViresh Kumar 
1937c9060494SDave Jones 	return ret;
193842d4dc3fSBenjamin Herrenschmidt }
1939e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend);
194042d4dc3fSBenjamin Herrenschmidt 
194142d4dc3fSBenjamin Herrenschmidt /**
1942ec06e586SRafael J. Wysocki  * cpufreq_suspend() - Suspend CPUFreq governors.
19431da177e4SLinus Torvalds  *
19442f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycles for suspending governors
19452f0aea93SViresh Kumar  * as some platforms can't change frequency after this point in suspend cycle.
19462f0aea93SViresh Kumar  * Because some of the devices (like: i2c, regulators, etc) they use for
19472f0aea93SViresh Kumar  * changing frequency are suspended quickly after this point.
19481da177e4SLinus Torvalds  */
cpufreq_suspend(void)19492f0aea93SViresh Kumar void cpufreq_suspend(void)
19501da177e4SLinus Torvalds {
19513a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
19521da177e4SLinus Torvalds 
19532f0aea93SViresh Kumar 	if (!cpufreq_driver)
1954e00e56dfSRafael J. Wysocki 		return;
19551da177e4SLinus Torvalds 
1956ba41e1bcSRafael J. Wysocki 	if (!has_target() && !cpufreq_driver->suspend)
1957b1b12babSViresh Kumar 		goto suspend;
19581da177e4SLinus Torvalds 
19592f0aea93SViresh Kumar 	pr_debug("%s: Suspending Governors\n", __func__);
19602f0aea93SViresh Kumar 
1961f963735aSViresh Kumar 	for_each_active_policy(policy) {
1962ba41e1bcSRafael J. Wysocki 		if (has_target()) {
196349f18560SViresh Kumar 			down_write(&policy->rwsem);
196445482c70SRafael J. Wysocki 			cpufreq_stop_governor(policy);
196549f18560SViresh Kumar 			up_write(&policy->rwsem);
1966ba41e1bcSRafael J. Wysocki 		}
1967ba41e1bcSRafael J. Wysocki 
1968ba41e1bcSRafael J. Wysocki 		if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
1969e9a7cc1dSFlorian Fainelli 			pr_err("%s: Failed to suspend driver: %s\n", __func__,
1970e9a7cc1dSFlorian Fainelli 				cpufreq_driver->name);
19711da177e4SLinus Torvalds 	}
1972b1b12babSViresh Kumar 
1973b1b12babSViresh Kumar suspend:
1974b1b12babSViresh Kumar 	cpufreq_suspended = true;
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds 
19771da177e4SLinus Torvalds /**
1978ec06e586SRafael J. Wysocki  * cpufreq_resume() - Resume CPUFreq governors.
19791da177e4SLinus Torvalds  *
19802f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycle for resuming governors that
19812f0aea93SViresh Kumar  * are suspended with cpufreq_suspend().
19821da177e4SLinus Torvalds  */
cpufreq_resume(void)19832f0aea93SViresh Kumar void cpufreq_resume(void)
19841da177e4SLinus Torvalds {
19851da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
198649f18560SViresh Kumar 	int ret;
19871da177e4SLinus Torvalds 
19882f0aea93SViresh Kumar 	if (!cpufreq_driver)
19891da177e4SLinus Torvalds 		return;
19901da177e4SLinus Torvalds 
1991703cbaa6SBo Yan 	if (unlikely(!cpufreq_suspended))
1992703cbaa6SBo Yan 		return;
1993703cbaa6SBo Yan 
19948e30444eSLan Tianyu 	cpufreq_suspended = false;
19958e30444eSLan Tianyu 
1996ba41e1bcSRafael J. Wysocki 	if (!has_target() && !cpufreq_driver->resume)
19972f0aea93SViresh Kumar 		return;
19981da177e4SLinus Torvalds 
19992f0aea93SViresh Kumar 	pr_debug("%s: Resuming Governors\n", __func__);
20002f0aea93SViresh Kumar 
2001f963735aSViresh Kumar 	for_each_active_policy(policy) {
200249f18560SViresh Kumar 		if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
20031f464cb4SLiao Chang 			pr_err("%s: Failed to resume driver: %s\n", __func__,
20041f464cb4SLiao Chang 				cpufreq_driver->name);
2005ba41e1bcSRafael J. Wysocki 		} else if (has_target()) {
200649f18560SViresh Kumar 			down_write(&policy->rwsem);
20070a300767SRafael J. Wysocki 			ret = cpufreq_start_governor(policy);
200849f18560SViresh Kumar 			up_write(&policy->rwsem);
200949f18560SViresh Kumar 
201049f18560SViresh Kumar 			if (ret)
20111f464cb4SLiao Chang 				pr_err("%s: Failed to start governor for CPU%u's policy\n",
20121f464cb4SLiao Chang 				       __func__, policy->cpu);
2013c75de0acSViresh Kumar 		}
201449f18560SViresh Kumar 	}
20151da177e4SLinus Torvalds }
20161da177e4SLinus Torvalds 
20179d95046eSBorislav Petkov /**
2018a62f68f5SRafael J. Wysocki  * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones.
2019a62f68f5SRafael J. Wysocki  * @flags: Flags to test against the current cpufreq driver's flags.
2020a62f68f5SRafael J. Wysocki  *
2021a62f68f5SRafael J. Wysocki  * Assumes that the driver is there, so callers must ensure that this is the
2022a62f68f5SRafael J. Wysocki  * case.
2023a62f68f5SRafael J. Wysocki  */
cpufreq_driver_test_flags(u16 flags)2024a62f68f5SRafael J. Wysocki bool cpufreq_driver_test_flags(u16 flags)
2025a62f68f5SRafael J. Wysocki {
2026a62f68f5SRafael J. Wysocki 	return !!(cpufreq_driver->flags & flags);
2027a62f68f5SRafael J. Wysocki }
2028a62f68f5SRafael J. Wysocki 
2029a62f68f5SRafael J. Wysocki /**
2030ec06e586SRafael J. Wysocki  * cpufreq_get_current_driver - Return the current driver's name.
20319d95046eSBorislav Petkov  *
2032ec06e586SRafael J. Wysocki  * Return the name string of the currently registered cpufreq driver or NULL if
2033ec06e586SRafael J. Wysocki  * none.
20349d95046eSBorislav Petkov  */
cpufreq_get_current_driver(void)20359d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
20369d95046eSBorislav Petkov {
20371c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
20381c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
20391c3d85ddSRafael J. Wysocki 
20401c3d85ddSRafael J. Wysocki 	return NULL;
20419d95046eSBorislav Petkov }
20429d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
20431da177e4SLinus Torvalds 
204451315cdfSThomas Petazzoni /**
2045ec06e586SRafael J. Wysocki  * cpufreq_get_driver_data - Return current driver data.
204651315cdfSThomas Petazzoni  *
2047ec06e586SRafael J. Wysocki  * Return the private data of the currently registered cpufreq driver, or NULL
2048ec06e586SRafael J. Wysocki  * if no cpufreq driver has been registered.
204951315cdfSThomas Petazzoni  */
cpufreq_get_driver_data(void)205051315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void)
205151315cdfSThomas Petazzoni {
205251315cdfSThomas Petazzoni 	if (cpufreq_driver)
205351315cdfSThomas Petazzoni 		return cpufreq_driver->driver_data;
205451315cdfSThomas Petazzoni 
205551315cdfSThomas Petazzoni 	return NULL;
205651315cdfSThomas Petazzoni }
205751315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
205851315cdfSThomas Petazzoni 
20591da177e4SLinus Torvalds /*********************************************************************
20601da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
20611da177e4SLinus Torvalds  *********************************************************************/
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds /**
2064ec06e586SRafael J. Wysocki  * cpufreq_register_notifier - Register a notifier with cpufreq.
2065ec06e586SRafael J. Wysocki  * @nb: notifier function to register.
2066ec06e586SRafael J. Wysocki  * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER.
20671da177e4SLinus Torvalds  *
2068ec06e586SRafael J. Wysocki  * Add a notifier to one of two lists: either a list of notifiers that run on
2069ec06e586SRafael J. Wysocki  * clock rate changes (once before and once after every transition), or a list
2070ec06e586SRafael J. Wysocki  * of notifiers that ron on cpufreq policy changes.
20711da177e4SLinus Torvalds  *
2072ec06e586SRafael J. Wysocki  * This function may sleep and it has the same return values as
2073ec06e586SRafael J. Wysocki  * blocking_notifier_chain_register().
20741da177e4SLinus Torvalds  */
cpufreq_register_notifier(struct notifier_block * nb,unsigned int list)20751da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
20761da177e4SLinus Torvalds {
20771da177e4SLinus Torvalds 	int ret;
20781da177e4SLinus Torvalds 
2079d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
2080d5aaffa9SDirk Brandewie 		return -EINVAL;
2081d5aaffa9SDirk Brandewie 
20821da177e4SLinus Torvalds 	switch (list) {
20831da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
2084b7898fdaSRafael J. Wysocki 		mutex_lock(&cpufreq_fast_switch_lock);
2085b7898fdaSRafael J. Wysocki 
2086b7898fdaSRafael J. Wysocki 		if (cpufreq_fast_switch_count > 0) {
2087b7898fdaSRafael J. Wysocki 			mutex_unlock(&cpufreq_fast_switch_lock);
2088b7898fdaSRafael J. Wysocki 			return -EBUSY;
2089b7898fdaSRafael J. Wysocki 		}
2090b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
2091e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
2092b7898fdaSRafael J. Wysocki 		if (!ret)
2093b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count--;
2094b7898fdaSRafael J. Wysocki 
2095b7898fdaSRafael J. Wysocki 		mutex_unlock(&cpufreq_fast_switch_lock);
20961da177e4SLinus Torvalds 		break;
20971da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
2098e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
2099e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
21001da177e4SLinus Torvalds 		break;
21011da177e4SLinus Torvalds 	default:
21021da177e4SLinus Torvalds 		ret = -EINVAL;
21031da177e4SLinus Torvalds 	}
21041da177e4SLinus Torvalds 
21051da177e4SLinus Torvalds 	return ret;
21061da177e4SLinus Torvalds }
21071da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds /**
2110ec06e586SRafael J. Wysocki  * cpufreq_unregister_notifier - Unregister a notifier from cpufreq.
2111ec06e586SRafael J. Wysocki  * @nb: notifier block to be unregistered.
2112ec06e586SRafael J. Wysocki  * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER.
21131da177e4SLinus Torvalds  *
2114ec06e586SRafael J. Wysocki  * Remove a notifier from one of the cpufreq notifier lists.
21151da177e4SLinus Torvalds  *
2116ec06e586SRafael J. Wysocki  * This function may sleep and it has the same return values as
2117ec06e586SRafael J. Wysocki  * blocking_notifier_chain_unregister().
21181da177e4SLinus Torvalds  */
cpufreq_unregister_notifier(struct notifier_block * nb,unsigned int list)21191da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
21201da177e4SLinus Torvalds {
21211da177e4SLinus Torvalds 	int ret;
21221da177e4SLinus Torvalds 
2123d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
2124d5aaffa9SDirk Brandewie 		return -EINVAL;
2125d5aaffa9SDirk Brandewie 
21261da177e4SLinus Torvalds 	switch (list) {
21271da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
2128b7898fdaSRafael J. Wysocki 		mutex_lock(&cpufreq_fast_switch_lock);
2129b7898fdaSRafael J. Wysocki 
2130b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
2131e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
2132b7898fdaSRafael J. Wysocki 		if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
2133b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count++;
2134b7898fdaSRafael J. Wysocki 
2135b7898fdaSRafael J. Wysocki 		mutex_unlock(&cpufreq_fast_switch_lock);
21361da177e4SLinus Torvalds 		break;
21371da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
2138e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
2139e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
21401da177e4SLinus Torvalds 		break;
21411da177e4SLinus Torvalds 	default:
21421da177e4SLinus Torvalds 		ret = -EINVAL;
21431da177e4SLinus Torvalds 	}
21441da177e4SLinus Torvalds 
21451da177e4SLinus Torvalds 	return ret;
21461da177e4SLinus Torvalds }
21471da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
21481da177e4SLinus Torvalds 
21491da177e4SLinus Torvalds 
21501da177e4SLinus Torvalds /*********************************************************************
21511da177e4SLinus Torvalds  *                              GOVERNORS                            *
21521da177e4SLinus Torvalds  *********************************************************************/
21531da177e4SLinus Torvalds 
2154b7898fdaSRafael J. Wysocki /**
2155b7898fdaSRafael J. Wysocki  * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
2156b7898fdaSRafael J. Wysocki  * @policy: cpufreq policy to switch the frequency for.
2157b7898fdaSRafael J. Wysocki  * @target_freq: New frequency to set (may be approximate).
2158b7898fdaSRafael J. Wysocki  *
2159b7898fdaSRafael J. Wysocki  * Carry out a fast frequency switch without sleeping.
2160b7898fdaSRafael J. Wysocki  *
2161b7898fdaSRafael J. Wysocki  * The driver's ->fast_switch() callback invoked by this function must be
2162b7898fdaSRafael J. Wysocki  * suitable for being called from within RCU-sched read-side critical sections
2163b7898fdaSRafael J. Wysocki  * and it is expected to select the minimum available frequency greater than or
2164b7898fdaSRafael J. Wysocki  * equal to @target_freq (CPUFREQ_RELATION_L).
2165b7898fdaSRafael J. Wysocki  *
2166b7898fdaSRafael J. Wysocki  * This function must not be called if policy->fast_switch_enabled is unset.
2167b7898fdaSRafael J. Wysocki  *
2168b7898fdaSRafael J. Wysocki  * Governors calling this function must guarantee that it will never be invoked
2169b7898fdaSRafael J. Wysocki  * twice in parallel for the same policy and that it will never be called in
2170b7898fdaSRafael J. Wysocki  * parallel with either ->target() or ->target_index() for the same policy.
2171b7898fdaSRafael J. Wysocki  *
2172209887e6SViresh Kumar  * Returns the actual frequency set for the CPU.
2173209887e6SViresh Kumar  *
2174209887e6SViresh Kumar  * If 0 is returned by the driver's ->fast_switch() callback to indicate an
2175209887e6SViresh Kumar  * error condition, the hardware configuration must be preserved.
2176b7898fdaSRafael J. Wysocki  */
cpufreq_driver_fast_switch(struct cpufreq_policy * policy,unsigned int target_freq)2177b7898fdaSRafael J. Wysocki unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
2178b7898fdaSRafael J. Wysocki 					unsigned int target_freq)
2179b7898fdaSRafael J. Wysocki {
21801a0419b0SIonela Voinescu 	unsigned int freq;
218108d8c65eSViresh Kumar 	int cpu;
2182b7898fdaSRafael J. Wysocki 
21831a0419b0SIonela Voinescu 	target_freq = clamp_val(target_freq, policy->min, policy->max);
21841a0419b0SIonela Voinescu 	freq = cpufreq_driver->fast_switch(policy, target_freq);
21851a0419b0SIonela Voinescu 
218696f60cddSViresh Kumar 	if (!freq)
218796f60cddSViresh Kumar 		return 0;
218896f60cddSViresh Kumar 
218908d8c65eSViresh Kumar 	policy->cur = freq;
21901a0419b0SIonela Voinescu 	arch_set_freq_scale(policy->related_cpus, freq,
21911a0419b0SIonela Voinescu 			    policy->cpuinfo.max_freq);
219296f60cddSViresh Kumar 	cpufreq_stats_record_transition(policy, freq);
21931a0419b0SIonela Voinescu 
219408d8c65eSViresh Kumar 	if (trace_cpu_frequency_enabled()) {
219508d8c65eSViresh Kumar 		for_each_cpu(cpu, policy->cpus)
219608d8c65eSViresh Kumar 			trace_cpu_frequency(freq, cpu);
219708d8c65eSViresh Kumar 	}
219808d8c65eSViresh Kumar 
21991a0419b0SIonela Voinescu 	return freq;
2200b7898fdaSRafael J. Wysocki }
2201b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
2202b7898fdaSRafael J. Wysocki 
2203ee2cc427SRafael J. Wysocki /**
2204ee2cc427SRafael J. Wysocki  * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go.
2205ee2cc427SRafael J. Wysocki  * @cpu: Target CPU.
2206ee2cc427SRafael J. Wysocki  * @min_perf: Minimum (required) performance level (units of @capacity).
22074e6df217SYue Hu  * @target_perf: Target (desired) performance level (units of @capacity).
2208ee2cc427SRafael J. Wysocki  * @capacity: Capacity of the target CPU.
2209ee2cc427SRafael J. Wysocki  *
2210ee2cc427SRafael J. Wysocki  * Carry out a fast performance level switch of @cpu without sleeping.
2211ee2cc427SRafael J. Wysocki  *
2212ee2cc427SRafael J. Wysocki  * The driver's ->adjust_perf() callback invoked by this function must be
2213ee2cc427SRafael J. Wysocki  * suitable for being called from within RCU-sched read-side critical sections
2214ee2cc427SRafael J. Wysocki  * and it is expected to select a suitable performance level equal to or above
2215ee2cc427SRafael J. Wysocki  * @min_perf and preferably equal to or below @target_perf.
2216ee2cc427SRafael J. Wysocki  *
2217ee2cc427SRafael J. Wysocki  * This function must not be called if policy->fast_switch_enabled is unset.
2218ee2cc427SRafael J. Wysocki  *
2219ee2cc427SRafael J. Wysocki  * Governors calling this function must guarantee that it will never be invoked
2220ee2cc427SRafael J. Wysocki  * twice in parallel for the same CPU and that it will never be called in
2221ee2cc427SRafael J. Wysocki  * parallel with either ->target() or ->target_index() or ->fast_switch() for
2222ee2cc427SRafael J. Wysocki  * the same CPU.
2223ee2cc427SRafael J. Wysocki  */
cpufreq_driver_adjust_perf(unsigned int cpu,unsigned long min_perf,unsigned long target_perf,unsigned long capacity)2224ee2cc427SRafael J. Wysocki void cpufreq_driver_adjust_perf(unsigned int cpu,
2225ee2cc427SRafael J. Wysocki 				 unsigned long min_perf,
2226ee2cc427SRafael J. Wysocki 				 unsigned long target_perf,
2227ee2cc427SRafael J. Wysocki 				 unsigned long capacity)
2228ee2cc427SRafael J. Wysocki {
2229ee2cc427SRafael J. Wysocki 	cpufreq_driver->adjust_perf(cpu, min_perf, target_perf, capacity);
2230ee2cc427SRafael J. Wysocki }
2231ee2cc427SRafael J. Wysocki 
2232ee2cc427SRafael J. Wysocki /**
2233ee2cc427SRafael J. Wysocki  * cpufreq_driver_has_adjust_perf - Check "direct fast switch" callback.
2234ee2cc427SRafael J. Wysocki  *
2235ee2cc427SRafael J. Wysocki  * Return 'true' if the ->adjust_perf callback is present for the
2236ee2cc427SRafael J. Wysocki  * current driver or 'false' otherwise.
2237ee2cc427SRafael J. Wysocki  */
cpufreq_driver_has_adjust_perf(void)2238ee2cc427SRafael J. Wysocki bool cpufreq_driver_has_adjust_perf(void)
2239ee2cc427SRafael J. Wysocki {
2240ee2cc427SRafael J. Wysocki 	return !!cpufreq_driver->adjust_perf;
2241ee2cc427SRafael J. Wysocki }
2242ee2cc427SRafael J. Wysocki 
22431c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */
__target_intermediate(struct cpufreq_policy * policy,struct cpufreq_freqs * freqs,int index)22441c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy,
22451c03a2d0SViresh Kumar 				 struct cpufreq_freqs *freqs, int index)
22461c03a2d0SViresh Kumar {
22471c03a2d0SViresh Kumar 	int ret;
22481c03a2d0SViresh Kumar 
22491c03a2d0SViresh Kumar 	freqs->new = cpufreq_driver->get_intermediate(policy, index);
22501c03a2d0SViresh Kumar 
22511c03a2d0SViresh Kumar 	/* We don't need to switch to intermediate freq */
22521c03a2d0SViresh Kumar 	if (!freqs->new)
22531c03a2d0SViresh Kumar 		return 0;
22541c03a2d0SViresh Kumar 
22551c03a2d0SViresh Kumar 	pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
22561c03a2d0SViresh Kumar 		 __func__, policy->cpu, freqs->old, freqs->new);
22571c03a2d0SViresh Kumar 
22581c03a2d0SViresh Kumar 	cpufreq_freq_transition_begin(policy, freqs);
22591c03a2d0SViresh Kumar 	ret = cpufreq_driver->target_intermediate(policy, index);
22601c03a2d0SViresh Kumar 	cpufreq_freq_transition_end(policy, freqs, ret);
22611c03a2d0SViresh Kumar 
22621c03a2d0SViresh Kumar 	if (ret)
22631c03a2d0SViresh Kumar 		pr_err("%s: Failed to change to intermediate frequency: %d\n",
22641c03a2d0SViresh Kumar 		       __func__, ret);
22651c03a2d0SViresh Kumar 
22661c03a2d0SViresh Kumar 	return ret;
22671c03a2d0SViresh Kumar }
22681c03a2d0SViresh Kumar 
__target_index(struct cpufreq_policy * policy,int index)226923727845SViresh Kumar static int __target_index(struct cpufreq_policy *policy, int index)
22708d65775dSViresh Kumar {
22711c03a2d0SViresh Kumar 	struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
227256a7ff75SRafael J. Wysocki 	unsigned int restore_freq, intermediate_freq = 0;
227323727845SViresh Kumar 	unsigned int newfreq = policy->freq_table[index].frequency;
22748d65775dSViresh Kumar 	int retval = -EINVAL;
22758d65775dSViresh Kumar 	bool notify;
22768d65775dSViresh Kumar 
227723727845SViresh Kumar 	if (newfreq == policy->cur)
227823727845SViresh Kumar 		return 0;
227923727845SViresh Kumar 
228056a7ff75SRafael J. Wysocki 	/* Save last value to restore later on errors */
228156a7ff75SRafael J. Wysocki 	restore_freq = policy->cur;
228256a7ff75SRafael J. Wysocki 
22838d65775dSViresh Kumar 	notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
22848d65775dSViresh Kumar 	if (notify) {
22851c03a2d0SViresh Kumar 		/* Handle switching to intermediate frequency */
22861c03a2d0SViresh Kumar 		if (cpufreq_driver->get_intermediate) {
22871c03a2d0SViresh Kumar 			retval = __target_intermediate(policy, &freqs, index);
22881c03a2d0SViresh Kumar 			if (retval)
22891c03a2d0SViresh Kumar 				return retval;
22908d65775dSViresh Kumar 
22911c03a2d0SViresh Kumar 			intermediate_freq = freqs.new;
22921c03a2d0SViresh Kumar 			/* Set old freq to intermediate */
22931c03a2d0SViresh Kumar 			if (intermediate_freq)
22941c03a2d0SViresh Kumar 				freqs.old = freqs.new;
22951c03a2d0SViresh Kumar 		}
22961c03a2d0SViresh Kumar 
229723727845SViresh Kumar 		freqs.new = newfreq;
22988d65775dSViresh Kumar 		pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
22998d65775dSViresh Kumar 			 __func__, policy->cpu, freqs.old, freqs.new);
23008d65775dSViresh Kumar 
23018d65775dSViresh Kumar 		cpufreq_freq_transition_begin(policy, &freqs);
23028d65775dSViresh Kumar 	}
23038d65775dSViresh Kumar 
23048d65775dSViresh Kumar 	retval = cpufreq_driver->target_index(policy, index);
23058d65775dSViresh Kumar 	if (retval)
23068d65775dSViresh Kumar 		pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
23078d65775dSViresh Kumar 		       retval);
23088d65775dSViresh Kumar 
23091c03a2d0SViresh Kumar 	if (notify) {
23108d65775dSViresh Kumar 		cpufreq_freq_transition_end(policy, &freqs, retval);
23118d65775dSViresh Kumar 
23121c03a2d0SViresh Kumar 		/*
23131c03a2d0SViresh Kumar 		 * Failed after setting to intermediate freq? Driver should have
23141c03a2d0SViresh Kumar 		 * reverted back to initial frequency and so should we. Check
23151c03a2d0SViresh Kumar 		 * here for intermediate_freq instead of get_intermediate, in
231658405af6SShailendra Verma 		 * case we haven't switched to intermediate freq at all.
23171c03a2d0SViresh Kumar 		 */
23181c03a2d0SViresh Kumar 		if (unlikely(retval && intermediate_freq)) {
23191c03a2d0SViresh Kumar 			freqs.old = intermediate_freq;
232056a7ff75SRafael J. Wysocki 			freqs.new = restore_freq;
23211c03a2d0SViresh Kumar 			cpufreq_freq_transition_begin(policy, &freqs);
23221c03a2d0SViresh Kumar 			cpufreq_freq_transition_end(policy, &freqs, 0);
23231c03a2d0SViresh Kumar 		}
23241c03a2d0SViresh Kumar 	}
23251c03a2d0SViresh Kumar 
23268d65775dSViresh Kumar 	return retval;
23278d65775dSViresh Kumar }
23288d65775dSViresh Kumar 
__cpufreq_driver_target(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int relation)23291da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
23301da177e4SLinus Torvalds 			    unsigned int target_freq,
23311da177e4SLinus Torvalds 			    unsigned int relation)
23321da177e4SLinus Torvalds {
23337249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
2334c32b6b8eSAshok Raj 
2335a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2336a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2337a7b422cdSKonrad Rzeszutek Wilk 
2338f9ccdec2SViresh Kumar 	target_freq = __resolve_freq(policy, target_freq, relation);
23397249924eSViresh Kumar 
23407249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
23417249924eSViresh Kumar 		 policy->cpu, target_freq, relation, old_target_freq);
23425a1c0228SViresh Kumar 
23439c0ebcf7SViresh Kumar 	/*
23449c0ebcf7SViresh Kumar 	 * This might look like a redundant call as we are checking it again
23459c0ebcf7SViresh Kumar 	 * after finding index. But it is left intentionally for cases where
23469c0ebcf7SViresh Kumar 	 * exactly same freq is called again and so we can save on few function
23479c0ebcf7SViresh Kumar 	 * calls.
23489c0ebcf7SViresh Kumar 	 */
23491c534352SRafael J. Wysocki 	if (target_freq == policy->cur &&
23501c534352SRafael J. Wysocki 	    !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS))
23515a1c0228SViresh Kumar 		return 0;
23525a1c0228SViresh Kumar 
23531f39fa0dSVincent Donnefort 	if (cpufreq_driver->target) {
23541f39fa0dSVincent Donnefort 		/*
23551f39fa0dSVincent Donnefort 		 * If the driver hasn't setup a single inefficient frequency,
23561f39fa0dSVincent Donnefort 		 * it's unlikely it knows how to decode CPUFREQ_RELATION_E.
23571f39fa0dSVincent Donnefort 		 */
23581f39fa0dSVincent Donnefort 		if (!policy->efficiencies_available)
23591f39fa0dSVincent Donnefort 			relation &= ~CPUFREQ_RELATION_E;
23601f39fa0dSVincent Donnefort 
23616019d23aSRafael J. Wysocki 		return cpufreq_driver->target(policy, target_freq, relation);
23621f39fa0dSVincent Donnefort 	}
23636019d23aSRafael J. Wysocki 
23646019d23aSRafael J. Wysocki 	if (!cpufreq_driver->target_index)
23656019d23aSRafael J. Wysocki 		return -EINVAL;
236690d45d17SAshok Raj 
2367f9ccdec2SViresh Kumar 	return __target_index(policy, policy->cached_resolved_idx);
23686019d23aSRafael J. Wysocki }
23691da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
23701da177e4SLinus Torvalds 
cpufreq_driver_target(struct cpufreq_policy * policy,unsigned int target_freq,unsigned int relation)23711da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
23721da177e4SLinus Torvalds 			  unsigned int target_freq,
23731da177e4SLinus Torvalds 			  unsigned int relation)
23741da177e4SLinus Torvalds {
237562c23a89SColin Ian King 	int ret;
23761da177e4SLinus Torvalds 
2377ad7722daSviresh kumar 	down_write(&policy->rwsem);
23781da177e4SLinus Torvalds 
23791da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
23801da177e4SLinus Torvalds 
2381ad7722daSviresh kumar 	up_write(&policy->rwsem);
23821da177e4SLinus Torvalds 
23831da177e4SLinus Torvalds 	return ret;
23841da177e4SLinus Torvalds }
23851da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
23861da177e4SLinus Torvalds 
cpufreq_fallback_governor(void)2387de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2388de1df26bSRafael J. Wysocki {
2389de1df26bSRafael J. Wysocki 	return NULL;
2390de1df26bSRafael J. Wysocki }
2391de1df26bSRafael J. Wysocki 
cpufreq_init_governor(struct cpufreq_policy * policy)2392a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy)
23931da177e4SLinus Torvalds {
2394cc993cabSDave Jones 	int ret;
23956afde10cSThomas Renninger 
23962f0aea93SViresh Kumar 	/* Don't start any governor operations if we are entering suspend */
23972f0aea93SViresh Kumar 	if (cpufreq_suspended)
23982f0aea93SViresh Kumar 		return 0;
2399cb57720bSEthan Zhao 	/*
2400cb57720bSEthan Zhao 	 * Governor might not be initiated here if ACPI _PPC changed
2401cb57720bSEthan Zhao 	 * notification happened, so check it.
2402cb57720bSEthan Zhao 	 */
2403cb57720bSEthan Zhao 	if (!policy->governor)
2404cb57720bSEthan Zhao 		return -EINVAL;
24052f0aea93SViresh Kumar 
2406ed4676e2SViresh Kumar 	/* Platform doesn't want dynamic frequency switching ? */
24079a2a9ebcSRafael J. Wysocki 	if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING &&
2408fc4c709fSViresh Kumar 	    cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
2409de1df26bSRafael J. Wysocki 		struct cpufreq_governor *gov = cpufreq_fallback_governor();
2410de1df26bSRafael J. Wysocki 
2411de1df26bSRafael J. Wysocki 		if (gov) {
2412fe829ed8SViresh Kumar 			pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
2413e837f9b5SJoe Perches 				policy->governor->name, gov->name);
24141c256245SThomas Renninger 			policy->governor = gov;
2415de1df26bSRafael J. Wysocki 		} else {
2416de1df26bSRafael J. Wysocki 			return -EINVAL;
24171c256245SThomas Renninger 		}
24186afde10cSThomas Renninger 	}
24191da177e4SLinus Torvalds 
24201da177e4SLinus Torvalds 	if (!try_module_get(policy->governor->owner))
24211da177e4SLinus Torvalds 		return -EINVAL;
24221da177e4SLinus Torvalds 
2423a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
242495731ebbSXiaoguang Chen 
2425e788892bSRafael J. Wysocki 	if (policy->governor->init) {
2426e788892bSRafael J. Wysocki 		ret = policy->governor->init(policy);
2427a92604b4SRafael J. Wysocki 		if (ret) {
24281da177e4SLinus Torvalds 			module_put(policy->governor->owner);
2429a92604b4SRafael J. Wysocki 			return ret;
24309e8c0a89SRafael J. Wysocki 		}
243136be3418SRafael J. Wysocki 	}
24321da177e4SLinus Torvalds 
2433ea9364bbSRafael J. Wysocki 	policy->strict_target = !!(policy->governor->flags & CPUFREQ_GOV_STRICT_TARGET);
2434ea9364bbSRafael J. Wysocki 
2435a92604b4SRafael J. Wysocki 	return 0;
2436a92604b4SRafael J. Wysocki }
2437a92604b4SRafael J. Wysocki 
cpufreq_exit_governor(struct cpufreq_policy * policy)2438a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2439a92604b4SRafael J. Wysocki {
2440a92604b4SRafael J. Wysocki 	if (cpufreq_suspended || !policy->governor)
2441a92604b4SRafael J. Wysocki 		return;
2442a92604b4SRafael J. Wysocki 
2443a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2444a92604b4SRafael J. Wysocki 
2445e788892bSRafael J. Wysocki 	if (policy->governor->exit)
2446e788892bSRafael J. Wysocki 		policy->governor->exit(policy);
2447a92604b4SRafael J. Wysocki 
24481da177e4SLinus Torvalds 	module_put(policy->governor->owner);
24491da177e4SLinus Torvalds }
24501da177e4SLinus Torvalds 
cpufreq_start_governor(struct cpufreq_policy * policy)2451f6ebbcf0SRafael J. Wysocki int cpufreq_start_governor(struct cpufreq_policy *policy)
24520a300767SRafael J. Wysocki {
24530a300767SRafael J. Wysocki 	int ret;
24540a300767SRafael J. Wysocki 
2455a92604b4SRafael J. Wysocki 	if (cpufreq_suspended)
2456a92604b4SRafael J. Wysocki 		return 0;
2457a92604b4SRafael J. Wysocki 
2458a92604b4SRafael J. Wysocki 	if (!policy->governor)
2459a92604b4SRafael J. Wysocki 		return -EINVAL;
2460a92604b4SRafael J. Wysocki 
2461a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2462a92604b4SRafael J. Wysocki 
2463407d0fffSViresh Kumar 	if (cpufreq_driver->get)
24645980752eSViresh Kumar 		cpufreq_verify_current_freq(policy, false);
24653bbf8fe3SRafael J. Wysocki 
2466e788892bSRafael J. Wysocki 	if (policy->governor->start) {
2467e788892bSRafael J. Wysocki 		ret = policy->governor->start(policy);
2468d6ff44d6SRafael J. Wysocki 		if (ret)
2469d6ff44d6SRafael J. Wysocki 			return ret;
2470e788892bSRafael J. Wysocki 	}
2471d6ff44d6SRafael J. Wysocki 
2472e788892bSRafael J. Wysocki 	if (policy->governor->limits)
2473e788892bSRafael J. Wysocki 		policy->governor->limits(policy);
2474e788892bSRafael J. Wysocki 
2475d6ff44d6SRafael J. Wysocki 	return 0;
24760a300767SRafael J. Wysocki }
24770a300767SRafael J. Wysocki 
cpufreq_stop_governor(struct cpufreq_policy * policy)2478f6ebbcf0SRafael J. Wysocki void cpufreq_stop_governor(struct cpufreq_policy *policy)
2479a92604b4SRafael J. Wysocki {
2480a92604b4SRafael J. Wysocki 	if (cpufreq_suspended || !policy->governor)
2481a92604b4SRafael J. Wysocki 		return;
2482a92604b4SRafael J. Wysocki 
2483a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2484a92604b4SRafael J. Wysocki 
2485e788892bSRafael J. Wysocki 	if (policy->governor->stop)
2486e788892bSRafael J. Wysocki 		policy->governor->stop(policy);
2487a92604b4SRafael J. Wysocki }
2488a92604b4SRafael J. Wysocki 
cpufreq_governor_limits(struct cpufreq_policy * policy)2489a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2490a92604b4SRafael J. Wysocki {
2491a92604b4SRafael J. Wysocki 	if (cpufreq_suspended || !policy->governor)
2492a92604b4SRafael J. Wysocki 		return;
2493a92604b4SRafael J. Wysocki 
2494a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2495a92604b4SRafael J. Wysocki 
2496e788892bSRafael J. Wysocki 	if (policy->governor->limits)
2497e788892bSRafael J. Wysocki 		policy->governor->limits(policy);
24981da177e4SLinus Torvalds }
24991da177e4SLinus Torvalds 
cpufreq_register_governor(struct cpufreq_governor * governor)25001da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
25011da177e4SLinus Torvalds {
25023bcb09a3SJeremy Fitzhardinge 	int err;
25031da177e4SLinus Torvalds 
25041da177e4SLinus Torvalds 	if (!governor)
25051da177e4SLinus Torvalds 		return -EINVAL;
25061da177e4SLinus Torvalds 
2507a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2508a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2509a7b422cdSKonrad Rzeszutek Wilk 
25103fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
25111da177e4SLinus Torvalds 
25123bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
251342f91fa1SViresh Kumar 	if (!find_governor(governor->name)) {
25143bcb09a3SJeremy Fitzhardinge 		err = 0;
25151da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
25163bcb09a3SJeremy Fitzhardinge 	}
25171da177e4SLinus Torvalds 
25183fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
25193bcb09a3SJeremy Fitzhardinge 	return err;
25201da177e4SLinus Torvalds }
25211da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
25221da177e4SLinus Torvalds 
cpufreq_unregister_governor(struct cpufreq_governor * governor)25231da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
25241da177e4SLinus Torvalds {
25254573237bSViresh Kumar 	struct cpufreq_policy *policy;
25264573237bSViresh Kumar 	unsigned long flags;
252790e41bacSPrarit Bhargava 
25281da177e4SLinus Torvalds 	if (!governor)
25291da177e4SLinus Torvalds 		return;
25301da177e4SLinus Torvalds 
2531a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2532a7b422cdSKonrad Rzeszutek Wilk 		return;
2533a7b422cdSKonrad Rzeszutek Wilk 
25344573237bSViresh Kumar 	/* clear last_governor for all inactive policies */
25354573237bSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
25364573237bSViresh Kumar 	for_each_inactive_policy(policy) {
253718bf3a12SViresh Kumar 		if (!strcmp(policy->last_governor, governor->name)) {
253818bf3a12SViresh Kumar 			policy->governor = NULL;
25394573237bSViresh Kumar 			strcpy(policy->last_governor, "\0");
254090e41bacSPrarit Bhargava 		}
254118bf3a12SViresh Kumar 	}
25424573237bSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
254390e41bacSPrarit Bhargava 
25443fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
25451da177e4SLinus Torvalds 	list_del(&governor->governor_list);
25463fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
25471da177e4SLinus Torvalds }
25481da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
25491da177e4SLinus Torvalds 
25501da177e4SLinus Torvalds 
25511da177e4SLinus Torvalds /*********************************************************************
25521da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
25531da177e4SLinus Torvalds  *********************************************************************/
25541da177e4SLinus Torvalds 
25551da177e4SLinus Torvalds /**
25561da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
255729464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
255829464f28SDave Jones  *	is written
2559a9909c21SLee Jones  * @cpu: CPU to find the policy for
25601da177e4SLinus Torvalds  *
25611da177e4SLinus Torvalds  * Reads the current cpufreq policy.
25621da177e4SLinus Torvalds  */
cpufreq_get_policy(struct cpufreq_policy * policy,unsigned int cpu)25631da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
25641da177e4SLinus Torvalds {
25651da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
25661da177e4SLinus Torvalds 	if (!policy)
25671da177e4SLinus Torvalds 		return -EINVAL;
25681da177e4SLinus Torvalds 
25691da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
25701da177e4SLinus Torvalds 	if (!cpu_policy)
25711da177e4SLinus Torvalds 		return -EINVAL;
25721da177e4SLinus Torvalds 
2573d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
25741da177e4SLinus Torvalds 
25751da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
25761da177e4SLinus Torvalds 	return 0;
25771da177e4SLinus Torvalds }
25781da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
25791da177e4SLinus Torvalds 
2580a0dbb819SRafael J. Wysocki /**
2581a0dbb819SRafael J. Wysocki  * cpufreq_set_policy - Modify cpufreq policy parameters.
2582a0dbb819SRafael J. Wysocki  * @policy: Policy object to modify.
25831e4f63aeSRafael J. Wysocki  * @new_gov: Policy governor pointer.
25841e4f63aeSRafael J. Wysocki  * @new_pol: Policy value (for drivers with built-in governors).
2585a0dbb819SRafael J. Wysocki  *
25861e4f63aeSRafael J. Wysocki  * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency
25871e4f63aeSRafael J. Wysocki  * limits to be set for the policy, update @policy with the verified limits
25881e4f63aeSRafael J. Wysocki  * values and either invoke the driver's ->setpolicy() callback (if present) or
25891e4f63aeSRafael J. Wysocki  * carry out a governor update for @policy.  That is, run the current governor's
25901e4f63aeSRafael J. Wysocki  * ->limits() callback (if @new_gov points to the same object as the one in
25911e4f63aeSRafael J. Wysocki  * @policy) or replace the governor for @policy with @new_gov.
2592a0dbb819SRafael J. Wysocki  *
2593a0dbb819SRafael J. Wysocki  * The cpuinfo part of @policy is not updated by this function.
2594153d7f3fSArjan van de Ven  */
cpufreq_set_policy(struct cpufreq_policy * policy,struct cpufreq_governor * new_gov,unsigned int new_pol)25951e4f63aeSRafael J. Wysocki static int cpufreq_set_policy(struct cpufreq_policy *policy,
25961e4f63aeSRafael J. Wysocki 			      struct cpufreq_governor *new_gov,
25971e4f63aeSRafael J. Wysocki 			      unsigned int new_pol)
25981da177e4SLinus Torvalds {
25991e4f63aeSRafael J. Wysocki 	struct cpufreq_policy_data new_data;
2600d9a789c7SRafael J. Wysocki 	struct cpufreq_governor *old_gov;
2601d9a789c7SRafael J. Wysocki 	int ret;
26021da177e4SLinus Torvalds 
26031e4f63aeSRafael J. Wysocki 	memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
26041e4f63aeSRafael J. Wysocki 	new_data.freq_table = policy->freq_table;
26051e4f63aeSRafael J. Wysocki 	new_data.cpu = policy->cpu;
2606fba9573bSPan Xinhui 	/*
260767d874c3SViresh Kumar 	 * PM QoS framework collects all the requests from users and provide us
260867d874c3SViresh Kumar 	 * the final aggregated value here.
2609fba9573bSPan Xinhui 	 */
26101e4f63aeSRafael J. Wysocki 	new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
26111e4f63aeSRafael J. Wysocki 	new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
26121e4f63aeSRafael J. Wysocki 
26131e4f63aeSRafael J. Wysocki 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
26141e4f63aeSRafael J. Wysocki 		 new_data.cpu, new_data.min, new_data.max);
26159c9a43edSMattia Dongili 
2616737ffb27SViresh Kumar 	/*
2617737ffb27SViresh Kumar 	 * Verify that the CPU speed can be set within these limits and make sure
2618737ffb27SViresh Kumar 	 * that min <= max.
2619737ffb27SViresh Kumar 	 */
26201e4f63aeSRafael J. Wysocki 	ret = cpufreq_driver->verify(&new_data);
26211da177e4SLinus Torvalds 	if (ret)
2622d9a789c7SRafael J. Wysocki 		return ret;
26231da177e4SLinus Torvalds 
262415171769SVincent Donnefort 	/*
262515171769SVincent Donnefort 	 * Resolve policy min/max to available frequencies. It ensures
262615171769SVincent Donnefort 	 * no frequency resolution will neither overshoot the requested maximum
262715171769SVincent Donnefort 	 * nor undershoot the requested minimum.
262815171769SVincent Donnefort 	 */
26291e4f63aeSRafael J. Wysocki 	policy->min = new_data.min;
26301e4f63aeSRafael J. Wysocki 	policy->max = new_data.max;
263115171769SVincent Donnefort 	policy->min = __resolve_freq(policy, policy->min, CPUFREQ_RELATION_L);
263215171769SVincent Donnefort 	policy->max = __resolve_freq(policy, policy->max, CPUFREQ_RELATION_H);
2633601b2185SRuchi Kandoi 	trace_cpu_frequency_limits(policy);
26341da177e4SLinus Torvalds 
2635e3c06236SSteve Muckle 	policy->cached_target_freq = UINT_MAX;
2636e3c06236SSteve Muckle 
26372d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
26383a3e9e06SViresh Kumar 		 policy->min, policy->max);
26391da177e4SLinus Torvalds 
26401c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
26411e4f63aeSRafael J. Wysocki 		policy->policy = new_pol;
26422d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
2643167a38dcSRafael J. Wysocki 		return cpufreq_driver->setpolicy(policy);
2644d9a789c7SRafael J. Wysocki 	}
2645d9a789c7SRafael J. Wysocki 
26461e4f63aeSRafael J. Wysocki 	if (new_gov == policy->governor) {
26472bb4059eSRafael J. Wysocki 		pr_debug("governor limits update\n");
2648a92604b4SRafael J. Wysocki 		cpufreq_governor_limits(policy);
2649d6ff44d6SRafael J. Wysocki 		return 0;
26500a300767SRafael J. Wysocki 	}
26511da177e4SLinus Torvalds 
26522d06d8c4SDominik Brodowski 	pr_debug("governor switch\n");
26531da177e4SLinus Torvalds 
2654d9a789c7SRafael J. Wysocki 	/* save old, working values */
2655d9a789c7SRafael J. Wysocki 	old_gov = policy->governor;
26561da177e4SLinus Torvalds 	/* end old governor */
2657d9a789c7SRafael J. Wysocki 	if (old_gov) {
265845482c70SRafael J. Wysocki 		cpufreq_stop_governor(policy);
265936be3418SRafael J. Wysocki 		cpufreq_exit_governor(policy);
26607bd353a9SViresh Kumar 	}
26611da177e4SLinus Torvalds 
26621da177e4SLinus Torvalds 	/* start new governor */
26631e4f63aeSRafael J. Wysocki 	policy->governor = new_gov;
2664a92604b4SRafael J. Wysocki 	ret = cpufreq_init_governor(policy);
26654bc384aeSViresh Kumar 	if (!ret) {
26660a300767SRafael J. Wysocki 		ret = cpufreq_start_governor(policy);
26670a300767SRafael J. Wysocki 		if (!ret) {
26682bb4059eSRafael J. Wysocki 			pr_debug("governor change\n");
2669531b5c9fSQuentin Perret 			sched_cpufreq_governor_change(policy, old_gov);
26700a300767SRafael J. Wysocki 			return 0;
26710a300767SRafael J. Wysocki 		}
2672b7898fdaSRafael J. Wysocki 		cpufreq_exit_governor(policy);
2673955ef483SViresh Kumar 	}
26747bd353a9SViresh Kumar 
26751da177e4SLinus Torvalds 	/* new governor failed, so re-start old one */
2676d9a789c7SRafael J. Wysocki 	pr_debug("starting governor %s failed\n", policy->governor->name);
26771da177e4SLinus Torvalds 	if (old_gov) {
26783a3e9e06SViresh Kumar 		policy->governor = old_gov;
2679a92604b4SRafael J. Wysocki 		if (cpufreq_init_governor(policy))
26804bc384aeSViresh Kumar 			policy->governor = NULL;
26814bc384aeSViresh Kumar 		else
26820a300767SRafael J. Wysocki 			cpufreq_start_governor(policy);
26831da177e4SLinus Torvalds 	}
26841da177e4SLinus Torvalds 
26854bc384aeSViresh Kumar 	return ret;
26861da177e4SLinus Torvalds }
26871da177e4SLinus Torvalds 
26881da177e4SLinus Torvalds /**
2689a0dbb819SRafael J. Wysocki  * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2690a0dbb819SRafael J. Wysocki  * @cpu: CPU to re-evaluate the policy for.
26911da177e4SLinus Torvalds  *
2692a0dbb819SRafael J. Wysocki  * Update the current frequency for the cpufreq policy of @cpu and use
269318c49926SViresh Kumar  * cpufreq_set_policy() to re-apply the min and max limits, which triggers the
269418c49926SViresh Kumar  * evaluation of policy notifiers and the cpufreq driver's ->verify() callback
269518c49926SViresh Kumar  * for the policy in question, among other things.
26961da177e4SLinus Torvalds  */
cpufreq_update_policy(unsigned int cpu)269730248fefSRafael J. Wysocki void cpufreq_update_policy(unsigned int cpu)
26981da177e4SLinus Torvalds {
2699540a3758SRafael J. Wysocki 	struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
27001da177e4SLinus Torvalds 
2701fefa8ff8SAaron Plattner 	if (!policy)
270230248fefSRafael J. Wysocki 		return;
27031da177e4SLinus Torvalds 
2704bb176f7dSViresh Kumar 	/*
2705bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
2706bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
2707bb176f7dSViresh Kumar 	 */
27085ddc6d4eSViresh Kumar 	if (cpufreq_driver->get && has_target() &&
27095980752eSViresh Kumar 	    (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
2710742c87bfSRafael J. Wysocki 		goto unlock;
271130248fefSRafael J. Wysocki 
271270a59fdeSViresh Kumar 	refresh_frequency_limits(policy);
27131da177e4SLinus Torvalds 
2714fefa8ff8SAaron Plattner unlock:
2715540a3758SRafael J. Wysocki 	cpufreq_cpu_release(policy);
27161da177e4SLinus Torvalds }
27171da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
27181da177e4SLinus Torvalds 
27195a25e3f7SRafael J. Wysocki /**
27205a25e3f7SRafael J. Wysocki  * cpufreq_update_limits - Update policy limits for a given CPU.
27215a25e3f7SRafael J. Wysocki  * @cpu: CPU to update the policy limits for.
27225a25e3f7SRafael J. Wysocki  *
27235a25e3f7SRafael J. Wysocki  * Invoke the driver's ->update_limits callback if present or call
27245a25e3f7SRafael J. Wysocki  * cpufreq_update_policy() for @cpu.
27255a25e3f7SRafael J. Wysocki  */
cpufreq_update_limits(unsigned int cpu)27265a25e3f7SRafael J. Wysocki void cpufreq_update_limits(unsigned int cpu)
27275a25e3f7SRafael J. Wysocki {
27285a25e3f7SRafael J. Wysocki 	if (cpufreq_driver->update_limits)
27295a25e3f7SRafael J. Wysocki 		cpufreq_driver->update_limits(cpu);
27305a25e3f7SRafael J. Wysocki 	else
27315a25e3f7SRafael J. Wysocki 		cpufreq_update_policy(cpu);
27325a25e3f7SRafael J. Wysocki }
27335a25e3f7SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_update_limits);
27345a25e3f7SRafael J. Wysocki 
27351da177e4SLinus Torvalds /*********************************************************************
27366f19efc0SLukasz Majewski  *               BOOST						     *
27376f19efc0SLukasz Majewski  *********************************************************************/
cpufreq_boost_set_sw(struct cpufreq_policy * policy,int state)2738cf6fada7SXiongfeng Wang static int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state)
27396f19efc0SLukasz Majewski {
2740552abb88SRafael J. Wysocki 	int ret;
2741552abb88SRafael J. Wysocki 
2742f8bfc116SViresh Kumar 	if (!policy->freq_table)
2743552abb88SRafael J. Wysocki 		return -ENXIO;
2744f8bfc116SViresh Kumar 
2745cf6fada7SXiongfeng Wang 	ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table);
27466f19efc0SLukasz Majewski 	if (ret) {
2747cf6fada7SXiongfeng Wang 		pr_err("%s: Policy frequency update failed\n", __func__);
2748552abb88SRafael J. Wysocki 		return ret;
27496f19efc0SLukasz Majewski 	}
275049f18560SViresh Kumar 
27513000ce3cSRafael J. Wysocki 	ret = freq_qos_update_request(policy->max_freq_req, policy->max);
2752e61a4125SViresh Kumar 	if (ret < 0)
2753552abb88SRafael J. Wysocki 		return ret;
27546f19efc0SLukasz Majewski 
2755552abb88SRafael J. Wysocki 	return 0;
27566f19efc0SLukasz Majewski }
27576f19efc0SLukasz Majewski 
cpufreq_boost_trigger_state(int state)27586f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state)
27596f19efc0SLukasz Majewski {
2760cf6fada7SXiongfeng Wang 	struct cpufreq_policy *policy;
27616f19efc0SLukasz Majewski 	unsigned long flags;
27626f19efc0SLukasz Majewski 	int ret = 0;
27636f19efc0SLukasz Majewski 
27646f19efc0SLukasz Majewski 	if (cpufreq_driver->boost_enabled == state)
27656f19efc0SLukasz Majewski 		return 0;
27666f19efc0SLukasz Majewski 
27676f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
27686f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = state;
27696f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
27706f19efc0SLukasz Majewski 
277109681a07SSebastian Andrzej Siewior 	cpus_read_lock();
2772cf6fada7SXiongfeng Wang 	for_each_active_policy(policy) {
2773218a06a7SJie Zhan 		policy->boost_enabled = state;
27749d47d2e7SSibi Sankar 		ret = cpufreq_driver->set_boost(policy, state);
27759d47d2e7SSibi Sankar 		if (ret) {
27769d47d2e7SSibi Sankar 			policy->boost_enabled = !policy->boost_enabled;
27779d47d2e7SSibi Sankar 			goto err_reset_state;
27789d47d2e7SSibi Sankar 		}
2779cf6fada7SXiongfeng Wang 	}
278009681a07SSebastian Andrzej Siewior 	cpus_read_unlock();
2781cf6fada7SXiongfeng Wang 
2782cf6fada7SXiongfeng Wang 	return 0;
2783cf6fada7SXiongfeng Wang 
2784cf6fada7SXiongfeng Wang err_reset_state:
278509681a07SSebastian Andrzej Siewior 	cpus_read_unlock();
2786cf6fada7SXiongfeng Wang 
27876f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
27886f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = !state;
27896f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
27906f19efc0SLukasz Majewski 
2791e837f9b5SJoe Perches 	pr_err("%s: Cannot %s BOOST\n",
2792e837f9b5SJoe Perches 	       __func__, state ? "enable" : "disable");
27936f19efc0SLukasz Majewski 
27946f19efc0SLukasz Majewski 	return ret;
27956f19efc0SLukasz Majewski }
27966f19efc0SLukasz Majewski 
cpufreq_boost_supported(void)279741669da0SRafael J. Wysocki static bool cpufreq_boost_supported(void)
27986f19efc0SLukasz Majewski {
279989f98d7eSYue Hu 	return cpufreq_driver->set_boost;
28006f19efc0SLukasz Majewski }
28016f19efc0SLukasz Majewski 
create_boost_sysfs_file(void)280244139ed4SViresh Kumar static int create_boost_sysfs_file(void)
280344139ed4SViresh Kumar {
280444139ed4SViresh Kumar 	int ret;
280544139ed4SViresh Kumar 
2806c82bd444SViresh Kumar 	ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
280744139ed4SViresh Kumar 	if (ret)
280844139ed4SViresh Kumar 		pr_err("%s: cannot register global BOOST sysfs file\n",
280944139ed4SViresh Kumar 		       __func__);
281044139ed4SViresh Kumar 
281144139ed4SViresh Kumar 	return ret;
281244139ed4SViresh Kumar }
281344139ed4SViresh Kumar 
remove_boost_sysfs_file(void)281444139ed4SViresh Kumar static void remove_boost_sysfs_file(void)
281544139ed4SViresh Kumar {
281644139ed4SViresh Kumar 	if (cpufreq_boost_supported())
2817c82bd444SViresh Kumar 		sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
281844139ed4SViresh Kumar }
281944139ed4SViresh Kumar 
cpufreq_enable_boost_support(void)282044139ed4SViresh Kumar int cpufreq_enable_boost_support(void)
282144139ed4SViresh Kumar {
282244139ed4SViresh Kumar 	if (!cpufreq_driver)
282344139ed4SViresh Kumar 		return -EINVAL;
282444139ed4SViresh Kumar 
282544139ed4SViresh Kumar 	if (cpufreq_boost_supported())
282644139ed4SViresh Kumar 		return 0;
282744139ed4SViresh Kumar 
28287a6c79f2SRafael J. Wysocki 	cpufreq_driver->set_boost = cpufreq_boost_set_sw;
282944139ed4SViresh Kumar 
283044139ed4SViresh Kumar 	/* This will get removed on driver unregister */
283144139ed4SViresh Kumar 	return create_boost_sysfs_file();
283244139ed4SViresh Kumar }
283344139ed4SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
283444139ed4SViresh Kumar 
cpufreq_boost_enabled(void)28356f19efc0SLukasz Majewski int cpufreq_boost_enabled(void)
28366f19efc0SLukasz Majewski {
28376f19efc0SLukasz Majewski 	return cpufreq_driver->boost_enabled;
28386f19efc0SLukasz Majewski }
28396f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
28406f19efc0SLukasz Majewski 
28416f19efc0SLukasz Majewski /*********************************************************************
28421da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
28431da177e4SLinus Torvalds  *********************************************************************/
284427622b06SSebastian Andrzej Siewior static enum cpuhp_state hp_online;
28451da177e4SLinus Torvalds 
cpuhp_cpufreq_online(unsigned int cpu)2846c4a3fa26SChen Yu static int cpuhp_cpufreq_online(unsigned int cpu)
2847c4a3fa26SChen Yu {
2848c4a3fa26SChen Yu 	cpufreq_online(cpu);
2849c4a3fa26SChen Yu 
2850c4a3fa26SChen Yu 	return 0;
2851c4a3fa26SChen Yu }
2852c4a3fa26SChen Yu 
cpuhp_cpufreq_offline(unsigned int cpu)2853c4a3fa26SChen Yu static int cpuhp_cpufreq_offline(unsigned int cpu)
2854c4a3fa26SChen Yu {
2855c4a3fa26SChen Yu 	cpufreq_offline(cpu);
2856c4a3fa26SChen Yu 
2857c4a3fa26SChen Yu 	return 0;
2858c4a3fa26SChen Yu }
2859c4a3fa26SChen Yu 
28601da177e4SLinus Torvalds /**
28611da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
28621da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
28631da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
28641da177e4SLinus Torvalds  *
28651da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
286663af4055SEric Biggers  * returns zero on success, -EEXIST when another driver got here first
28671da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
28681da177e4SLinus Torvalds  *
28691da177e4SLinus Torvalds  */
cpufreq_register_driver(struct cpufreq_driver * driver_data)2870221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
28711da177e4SLinus Torvalds {
28721da177e4SLinus Torvalds 	unsigned long flags;
28731da177e4SLinus Torvalds 	int ret;
28741da177e4SLinus Torvalds 
2875a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2876a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2877a7b422cdSKonrad Rzeszutek Wilk 
287846770be0SViresh Kumar 	/*
287946770be0SViresh Kumar 	 * The cpufreq core depends heavily on the availability of device
288046770be0SViresh Kumar 	 * structure, make sure they are available before proceeding further.
288146770be0SViresh Kumar 	 */
288246770be0SViresh Kumar 	if (!get_cpu_device(0))
288346770be0SViresh Kumar 		return -EPROBE_DEFER;
288446770be0SViresh Kumar 
28851da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
28869c0ebcf7SViresh Kumar 	    !(driver_data->setpolicy || driver_data->target_index ||
28879832235fSRafael J. Wysocki 		    driver_data->target) ||
28889832235fSRafael J. Wysocki 	     (driver_data->setpolicy && (driver_data->target_index ||
28891c03a2d0SViresh Kumar 		    driver_data->target)) ||
2890a9a22b57SViresh Kumar 	     (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
2891b4a11fa3SWyes Karny 	     (!driver_data->online != !driver_data->offline) ||
2892b4a11fa3SWyes Karny 		 (driver_data->adjust_perf && !driver_data->fast_switch))
28931da177e4SLinus Torvalds 		return -EINVAL;
28941da177e4SLinus Torvalds 
28952d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
28961da177e4SLinus Torvalds 
2897fdd320daSRafael J. Wysocki 	/* Protect against concurrent CPU online/offline. */
2898a92551e4SSebastian Andrzej Siewior 	cpus_read_lock();
2899fdd320daSRafael J. Wysocki 
29000d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
29011c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
29020d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2903fdd320daSRafael J. Wysocki 		ret = -EEXIST;
2904fdd320daSRafael J. Wysocki 		goto out;
29051da177e4SLinus Torvalds 	}
29061c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
29070d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
29081da177e4SLinus Torvalds 
2909874f6353SIonela Voinescu 	/*
2910874f6353SIonela Voinescu 	 * Mark support for the scheduler's frequency invariance engine for
2911874f6353SIonela Voinescu 	 * drivers that implement target(), target_index() or fast_switch().
2912874f6353SIonela Voinescu 	 */
2913874f6353SIonela Voinescu 	if (!cpufreq_driver->setpolicy) {
2914874f6353SIonela Voinescu 		static_branch_enable_cpuslocked(&cpufreq_freq_invariance);
2915874f6353SIonela Voinescu 		pr_debug("supports frequency invariance");
2916874f6353SIonela Voinescu 	}
2917874f6353SIonela Voinescu 
2918bc68b7dfSViresh Kumar 	if (driver_data->setpolicy)
2919bc68b7dfSViresh Kumar 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
2920bc68b7dfSViresh Kumar 
29217a6c79f2SRafael J. Wysocki 	if (cpufreq_boost_supported()) {
292244139ed4SViresh Kumar 		ret = create_boost_sysfs_file();
292344139ed4SViresh Kumar 		if (ret)
29246f19efc0SLukasz Majewski 			goto err_null_driver;
29257a6c79f2SRafael J. Wysocki 	}
29266f19efc0SLukasz Majewski 
29278a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
29288f5bc2abSJiri Slaby 	if (ret)
29296f19efc0SLukasz Majewski 		goto err_boost_unreg;
29301da177e4SLinus Torvalds 
29315ae4a4b4SViresh Kumar 	if (unlikely(list_empty(&cpufreq_policy_list))) {
29321da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
29336c770036SDavid Arcari 		ret = -ENODEV;
2934ce1bcfe9SViresh Kumar 		pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2935e08f5f5bSGautham R Shenoy 			 driver_data->name);
29368a25a2fdSKay Sievers 		goto err_if_unreg;
29371da177e4SLinus Torvalds 	}
29381da177e4SLinus Torvalds 
2939a92551e4SSebastian Andrzej Siewior 	ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2940a92551e4SSebastian Andrzej Siewior 						   "cpufreq:online",
2941c4a3fa26SChen Yu 						   cpuhp_cpufreq_online,
2942c4a3fa26SChen Yu 						   cpuhp_cpufreq_offline);
294327622b06SSebastian Andrzej Siewior 	if (ret < 0)
294427622b06SSebastian Andrzej Siewior 		goto err_if_unreg;
294527622b06SSebastian Andrzej Siewior 	hp_online = ret;
29465372e054SSebastian Andrzej Siewior 	ret = 0;
294727622b06SSebastian Andrzej Siewior 
29482d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
29493834abb4SPankaj Gupta 	goto out;
2950fdd320daSRafael J. Wysocki 
29518a25a2fdSKay Sievers err_if_unreg:
29528a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
29536f19efc0SLukasz Majewski err_boost_unreg:
295444139ed4SViresh Kumar 	remove_boost_sysfs_file();
29558f5bc2abSJiri Slaby err_null_driver:
29560d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
29571c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
29580d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
29593834abb4SPankaj Gupta out:
2960a92551e4SSebastian Andrzej Siewior 	cpus_read_unlock();
29613834abb4SPankaj Gupta 	return ret;
29621da177e4SLinus Torvalds }
29631da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
29641da177e4SLinus Torvalds 
2965a9909c21SLee Jones /*
29661da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
29671da177e4SLinus Torvalds  *
29681da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
29691da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
29701da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
29711da177e4SLinus Torvalds  * currently not initialised.
29721da177e4SLinus Torvalds  */
cpufreq_unregister_driver(struct cpufreq_driver * driver)2973dd329e1eSUwe Kleine-König void cpufreq_unregister_driver(struct cpufreq_driver *driver)
29741da177e4SLinus Torvalds {
29751da177e4SLinus Torvalds 	unsigned long flags;
29761da177e4SLinus Torvalds 
2977dd329e1eSUwe Kleine-König 	if (WARN_ON(!cpufreq_driver || (driver != cpufreq_driver)))
2978dd329e1eSUwe Kleine-König 		return;
29791da177e4SLinus Torvalds 
29802d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
29811da177e4SLinus Torvalds 
2982454d3a25SSebastian Andrzej Siewior 	/* Protect against concurrent cpu hotplug */
2983a92551e4SSebastian Andrzej Siewior 	cpus_read_lock();
29848a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
298544139ed4SViresh Kumar 	remove_boost_sysfs_file();
2986874f6353SIonela Voinescu 	static_branch_disable_cpuslocked(&cpufreq_freq_invariance);
2987a92551e4SSebastian Andrzej Siewior 	cpuhp_remove_state_nocalls_cpuslocked(hp_online);
29881da177e4SLinus Torvalds 
29890d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
29906eed9404SViresh Kumar 
29911c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
29926eed9404SViresh Kumar 
29930d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2994a92551e4SSebastian Andrzej Siewior 	cpus_read_unlock();
29951da177e4SLinus Torvalds }
29961da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
29975a01f2e8SVenkatesh Pallipadi 
cpufreq_core_init(void)29985a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
29995a01f2e8SVenkatesh Pallipadi {
30008412b456SQuentin Perret 	struct cpufreq_governor *gov = cpufreq_default_governor();
30012744a63cSGreg Kroah-Hartman 	struct device *dev_root;
30028412b456SQuentin Perret 
3003a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
3004a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
3005a7b422cdSKonrad Rzeszutek Wilk 
30062744a63cSGreg Kroah-Hartman 	dev_root = bus_get_dev_root(&cpu_subsys);
30072744a63cSGreg Kroah-Hartman 	if (dev_root) {
30082744a63cSGreg Kroah-Hartman 		cpufreq_global_kobject = kobject_create_and_add("cpufreq", &dev_root->kobj);
30092744a63cSGreg Kroah-Hartman 		put_device(dev_root);
30102744a63cSGreg Kroah-Hartman 	}
30118aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
30128aa84ad8SThomas Renninger 
30138412b456SQuentin Perret 	if (!strlen(default_governor))
30148412b456SQuentin Perret 		strncpy(default_governor, gov->name, CPUFREQ_NAME_LEN);
30158412b456SQuentin Perret 
30165a01f2e8SVenkatesh Pallipadi 	return 0;
30175a01f2e8SVenkatesh Pallipadi }
3018d82f2692SLen Brown module_param(off, int, 0444);
30198412b456SQuentin Perret module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444);
30205a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
3021