xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision 5ae4a4b45d4396aa7f7c008c4ae9eca981d43f8c)
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>
316f4f2723SThomas Renninger #include <trace/events/power.h>
326f4f2723SThomas Renninger 
33b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list);
34f963735aSViresh Kumar 
35f963735aSViresh Kumar /* Macros to iterate over CPU policies */
36f963735aSViresh Kumar #define for_each_suitable_policy(__policy, __active)			 \
37fd7dc7e6SEric Biggers 	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
38fd7dc7e6SEric Biggers 		if ((__active) == !policy_is_inactive(__policy))
39f963735aSViresh Kumar 
40f963735aSViresh Kumar #define for_each_active_policy(__policy)		\
41f963735aSViresh Kumar 	for_each_suitable_policy(__policy, true)
42f963735aSViresh Kumar #define for_each_inactive_policy(__policy)		\
43f963735aSViresh Kumar 	for_each_suitable_policy(__policy, false)
44f963735aSViresh Kumar 
45b4f0676fSViresh Kumar #define for_each_policy(__policy)			\
46b4f0676fSViresh Kumar 	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
47b4f0676fSViresh Kumar 
48f7b27061SViresh Kumar /* Iterate over governors */
49f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list);
50f7b27061SViresh Kumar #define for_each_governor(__governor)				\
51f7b27061SViresh Kumar 	list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
52f7b27061SViresh Kumar 
538412b456SQuentin Perret static char default_governor[CPUFREQ_NAME_LEN];
548412b456SQuentin Perret 
55a9909c21SLee Jones /*
56cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
571da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
581da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
591da177e4SLinus Torvalds  */
601c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
617a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
62bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
63bb176f7dSViresh Kumar 
64874f6353SIonela Voinescu static DEFINE_STATIC_KEY_FALSE(cpufreq_freq_invariance);
65874f6353SIonela Voinescu bool cpufreq_supports_freq_invariance(void)
66874f6353SIonela Voinescu {
67874f6353SIonela Voinescu 	return static_branch_likely(&cpufreq_freq_invariance);
68874f6353SIonela Voinescu }
69874f6353SIonela Voinescu 
702f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */
712f0aea93SViresh Kumar static bool cpufreq_suspended;
721da177e4SLinus Torvalds 
739c0ebcf7SViresh Kumar static inline bool has_target(void)
749c0ebcf7SViresh Kumar {
759c0ebcf7SViresh Kumar 	return cpufreq_driver->target_index || cpufreq_driver->target;
769c0ebcf7SViresh Kumar }
779c0ebcf7SViresh Kumar 
781da177e4SLinus Torvalds /* internal prototypes */
79d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
80a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy);
81a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy);
82a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy);
831e4f63aeSRafael J. Wysocki static int cpufreq_set_policy(struct cpufreq_policy *policy,
841e4f63aeSRafael J. Wysocki 			      struct cpufreq_governor *new_gov,
851e4f63aeSRafael J. Wysocki 			      unsigned int new_pol);
8645482c70SRafael J. Wysocki 
87a9909c21SLee Jones /*
881da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
891da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
901da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
911da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
921da177e4SLinus Torvalds  * The mutex locks both lists.
931da177e4SLinus Torvalds  */
94e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
95cc85de36SSebastian Andrzej Siewior SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
961da177e4SLinus Torvalds 
97a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
98da584455SViresh Kumar static int cpufreq_disabled(void)
99a7b422cdSKonrad Rzeszutek Wilk {
100a7b422cdSKonrad Rzeszutek Wilk 	return off;
101a7b422cdSKonrad Rzeszutek Wilk }
102a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
103a7b422cdSKonrad Rzeszutek Wilk {
104a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
105a7b422cdSKonrad Rzeszutek Wilk }
1063fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
1071da177e4SLinus Torvalds 
1084d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1094d5dcc42SViresh Kumar {
1100b981e70SViresh Kumar 	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
1114d5dcc42SViresh Kumar }
1123f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1134d5dcc42SViresh Kumar 
114183edb20SYangtao Li static struct kobject *cpufreq_global_kobject;
115183edb20SYangtao Li 
116944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
117944e9a03SViresh Kumar {
118944e9a03SViresh Kumar 	if (have_governor_per_policy())
119944e9a03SViresh Kumar 		return &policy->kobj;
120944e9a03SViresh Kumar 	else
121944e9a03SViresh Kumar 		return cpufreq_global_kobject;
122944e9a03SViresh Kumar }
123944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
124944e9a03SViresh Kumar 
12572a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
12672a4ce34SViresh Kumar {
1275720821bSFrederic Weisbecker 	struct kernel_cpustat kcpustat;
12872a4ce34SViresh Kumar 	u64 cur_wall_time;
1295720821bSFrederic Weisbecker 	u64 idle_time;
13072a4ce34SViresh Kumar 	u64 busy_time;
13172a4ce34SViresh Kumar 
1327fb1327eSFrederic Weisbecker 	cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
13372a4ce34SViresh Kumar 
1345720821bSFrederic Weisbecker 	kcpustat_cpu_fetch(&kcpustat, cpu);
1355720821bSFrederic Weisbecker 
1365720821bSFrederic Weisbecker 	busy_time = kcpustat.cpustat[CPUTIME_USER];
1375720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_SYSTEM];
1385720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_IRQ];
1395720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ];
1405720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_STEAL];
1415720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_NICE];
14272a4ce34SViresh Kumar 
14372a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
14472a4ce34SViresh Kumar 	if (wall)
1457fb1327eSFrederic Weisbecker 		*wall = div_u64(cur_wall_time, NSEC_PER_USEC);
14672a4ce34SViresh Kumar 
1477fb1327eSFrederic Weisbecker 	return div_u64(idle_time, NSEC_PER_USEC);
14872a4ce34SViresh Kumar }
14972a4ce34SViresh Kumar 
15072a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
15172a4ce34SViresh Kumar {
15272a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
15372a4ce34SViresh Kumar 
15472a4ce34SViresh Kumar 	if (idle_time == -1ULL)
15572a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
15672a4ce34SViresh Kumar 	else if (!io_busy)
15772a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
15872a4ce34SViresh Kumar 
15972a4ce34SViresh Kumar 	return idle_time;
16072a4ce34SViresh Kumar }
16172a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
16272a4ce34SViresh Kumar 
16370e9e778SViresh Kumar /*
16470e9e778SViresh Kumar  * This is a generic cpufreq init() routine which can be used by cpufreq
16570e9e778SViresh Kumar  * drivers of SMP systems. It will do following:
16670e9e778SViresh Kumar  * - validate & show freq table passed
16770e9e778SViresh Kumar  * - set policies transition latency
16870e9e778SViresh Kumar  * - policy->cpus with all possible CPUs
16970e9e778SViresh Kumar  */
170c4dcc8a1SViresh Kumar void cpufreq_generic_init(struct cpufreq_policy *policy,
17170e9e778SViresh Kumar 		struct cpufreq_frequency_table *table,
17270e9e778SViresh Kumar 		unsigned int transition_latency)
17370e9e778SViresh Kumar {
17492c99d15SViresh Kumar 	policy->freq_table = table;
17570e9e778SViresh Kumar 	policy->cpuinfo.transition_latency = transition_latency;
17670e9e778SViresh Kumar 
17770e9e778SViresh Kumar 	/*
17858405af6SShailendra Verma 	 * The driver only supports the SMP configuration where all processors
17970e9e778SViresh Kumar 	 * share the clock and voltage and clock.
18070e9e778SViresh Kumar 	 */
18170e9e778SViresh Kumar 	cpumask_setall(policy->cpus);
18270e9e778SViresh Kumar }
18370e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init);
18470e9e778SViresh Kumar 
1851f0bd44eSRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
186652ed95dSViresh Kumar {
187652ed95dSViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
188652ed95dSViresh Kumar 
189988bed09SViresh Kumar 	return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
190988bed09SViresh Kumar }
1911f0bd44eSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
192988bed09SViresh Kumar 
193988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu)
194988bed09SViresh Kumar {
195988bed09SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
196988bed09SViresh Kumar 
197652ed95dSViresh Kumar 	if (!policy || IS_ERR(policy->clk)) {
198e837f9b5SJoe Perches 		pr_err("%s: No %s associated to cpu: %d\n",
199e837f9b5SJoe Perches 		       __func__, policy ? "clk" : "policy", cpu);
200652ed95dSViresh Kumar 		return 0;
201652ed95dSViresh Kumar 	}
202652ed95dSViresh Kumar 
203652ed95dSViresh Kumar 	return clk_get_rate(policy->clk) / 1000;
204652ed95dSViresh Kumar }
205652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get);
206652ed95dSViresh Kumar 
20750e9c852SViresh Kumar /**
2085d094feaSRafael J. Wysocki  * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
2095d094feaSRafael J. Wysocki  * @cpu: CPU to find the policy for.
21050e9c852SViresh Kumar  *
2115d094feaSRafael J. Wysocki  * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
2125d094feaSRafael J. Wysocki  * the kobject reference counter of that policy.  Return a valid policy on
2135d094feaSRafael J. Wysocki  * success or NULL on failure.
21450e9c852SViresh Kumar  *
2155d094feaSRafael J. Wysocki  * The policy returned by this function has to be released with the help of
2165d094feaSRafael J. Wysocki  * cpufreq_cpu_put() to balance its kobject reference counter properly.
21750e9c852SViresh Kumar  */
2186eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
2191da177e4SLinus Torvalds {
2206eed9404SViresh Kumar 	struct cpufreq_policy *policy = NULL;
2211da177e4SLinus Torvalds 	unsigned long flags;
2221da177e4SLinus Torvalds 
2231b947c90SViresh Kumar 	if (WARN_ON(cpu >= nr_cpu_ids))
2246eed9404SViresh Kumar 		return NULL;
2256eed9404SViresh Kumar 
2261da177e4SLinus Torvalds 	/* get the cpufreq driver */
2270d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
2281da177e4SLinus Torvalds 
2296eed9404SViresh Kumar 	if (cpufreq_driver) {
2301da177e4SLinus Torvalds 		/* get the CPU */
231988bed09SViresh Kumar 		policy = cpufreq_cpu_get_raw(cpu);
2326eed9404SViresh Kumar 		if (policy)
2336eed9404SViresh Kumar 			kobject_get(&policy->kobj);
2346eed9404SViresh Kumar 	}
2356eed9404SViresh Kumar 
2366eed9404SViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2371da177e4SLinus Torvalds 
2383a3e9e06SViresh Kumar 	return policy;
239a9144436SStephen Boyd }
2401da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2411da177e4SLinus Torvalds 
24250e9c852SViresh Kumar /**
2435d094feaSRafael J. Wysocki  * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
2445d094feaSRafael J. Wysocki  * @policy: cpufreq policy returned by cpufreq_cpu_get().
24550e9c852SViresh Kumar  */
2463a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
247a9144436SStephen Boyd {
2486eed9404SViresh Kumar 	kobject_put(&policy->kobj);
249a9144436SStephen Boyd }
2501da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2511da177e4SLinus Torvalds 
252540a3758SRafael J. Wysocki /**
253540a3758SRafael J. Wysocki  * cpufreq_cpu_release - Unlock a policy and decrement its usage counter.
254540a3758SRafael J. Wysocki  * @policy: cpufreq policy returned by cpufreq_cpu_acquire().
255540a3758SRafael J. Wysocki  */
2569083e498SRafael J. Wysocki void cpufreq_cpu_release(struct cpufreq_policy *policy)
257540a3758SRafael J. Wysocki {
258540a3758SRafael J. Wysocki 	if (WARN_ON(!policy))
259540a3758SRafael J. Wysocki 		return;
260540a3758SRafael J. Wysocki 
261540a3758SRafael J. Wysocki 	lockdep_assert_held(&policy->rwsem);
262540a3758SRafael J. Wysocki 
263540a3758SRafael J. Wysocki 	up_write(&policy->rwsem);
264540a3758SRafael J. Wysocki 
265540a3758SRafael J. Wysocki 	cpufreq_cpu_put(policy);
266540a3758SRafael J. Wysocki }
267540a3758SRafael J. Wysocki 
268540a3758SRafael J. Wysocki /**
269540a3758SRafael J. Wysocki  * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it.
270540a3758SRafael J. Wysocki  * @cpu: CPU to find the policy for.
271540a3758SRafael J. Wysocki  *
272540a3758SRafael J. Wysocki  * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and
273540a3758SRafael J. Wysocki  * if the policy returned by it is not NULL, acquire its rwsem for writing.
274540a3758SRafael J. Wysocki  * Return the policy if it is active or release it and return NULL otherwise.
275540a3758SRafael J. Wysocki  *
276540a3758SRafael J. Wysocki  * The policy returned by this function has to be released with the help of
277540a3758SRafael J. Wysocki  * cpufreq_cpu_release() in order to release its rwsem and balance its usage
278540a3758SRafael J. Wysocki  * counter properly.
279540a3758SRafael J. Wysocki  */
2809083e498SRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu)
281540a3758SRafael J. Wysocki {
282540a3758SRafael J. Wysocki 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
283540a3758SRafael J. Wysocki 
284540a3758SRafael J. Wysocki 	if (!policy)
285540a3758SRafael J. Wysocki 		return NULL;
286540a3758SRafael J. Wysocki 
287540a3758SRafael J. Wysocki 	down_write(&policy->rwsem);
288540a3758SRafael J. Wysocki 
289540a3758SRafael J. Wysocki 	if (policy_is_inactive(policy)) {
290540a3758SRafael J. Wysocki 		cpufreq_cpu_release(policy);
291540a3758SRafael J. Wysocki 		return NULL;
292540a3758SRafael J. Wysocki 	}
293540a3758SRafael J. Wysocki 
294540a3758SRafael J. Wysocki 	return policy;
295540a3758SRafael J. Wysocki }
296540a3758SRafael J. Wysocki 
2971da177e4SLinus Torvalds /*********************************************************************
2981da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2991da177e4SLinus Torvalds  *********************************************************************/
3001da177e4SLinus Torvalds 
301ec06e586SRafael J. Wysocki /**
302ec06e586SRafael J. Wysocki  * adjust_jiffies - Adjust the system "loops_per_jiffy".
303ec06e586SRafael J. Wysocki  * @val: CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
304ec06e586SRafael J. Wysocki  * @ci: Frequency change information.
3051da177e4SLinus Torvalds  *
3061da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
3071da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
3081da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
3091da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
3101da177e4SLinus Torvalds  */
31139c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
31239c132eeSViresh Kumar {
3131da177e4SLinus Torvalds #ifndef CONFIG_SMP
3141da177e4SLinus Torvalds 	static unsigned long l_p_j_ref;
3151da177e4SLinus Torvalds 	static unsigned int l_p_j_ref_freq;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
3181da177e4SLinus Torvalds 		return;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
3211da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
3221da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
323e837f9b5SJoe Perches 		pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
324e837f9b5SJoe Perches 			 l_p_j_ref, l_p_j_ref_freq);
3251da177e4SLinus Torvalds 	}
3260b443eadSViresh Kumar 	if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
327e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
328e08f5f5bSGautham R Shenoy 								ci->new);
329e837f9b5SJoe Perches 		pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
330e837f9b5SJoe Perches 			 loops_per_jiffy, ci->new);
3311da177e4SLinus Torvalds 	}
3321da177e4SLinus Torvalds #endif
33339c132eeSViresh Kumar }
3341da177e4SLinus Torvalds 
33520b5324dSViresh Kumar /**
336ec06e586SRafael J. Wysocki  * cpufreq_notify_transition - Notify frequency transition and adjust jiffies.
33720b5324dSViresh Kumar  * @policy: cpufreq policy to enable fast frequency switching for.
33820b5324dSViresh Kumar  * @freqs: contain details of the frequency update.
33920b5324dSViresh Kumar  * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
34020b5324dSViresh Kumar  *
341ec06e586SRafael J. Wysocki  * This function calls the transition notifiers and adjust_jiffies().
342ec06e586SRafael J. Wysocki  *
343ec06e586SRafael J. Wysocki  * It is called twice on all CPU frequency changes that have external effects.
34420b5324dSViresh Kumar  */
34520b5324dSViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy,
34620b5324dSViresh Kumar 				      struct cpufreq_freqs *freqs,
34720b5324dSViresh Kumar 				      unsigned int state)
3481da177e4SLinus Torvalds {
349df24014aSViresh Kumar 	int cpu;
350df24014aSViresh Kumar 
3511da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
3521da177e4SLinus Torvalds 
353d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
354d5aaffa9SDirk Brandewie 		return;
355d5aaffa9SDirk Brandewie 
356df24014aSViresh Kumar 	freqs->policy = policy;
3571c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
3582d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
359e4472cb3SDave Jones 		 state, freqs->new);
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	switch (state) {
3621da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
36320b5324dSViresh Kumar 		/*
36420b5324dSViresh Kumar 		 * Detect if the driver reported a value as "old frequency"
365e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
366e4472cb3SDave Jones 		 * "old frequency".
3671da177e4SLinus Torvalds 		 */
36898015228SViresh Kumar 		if (policy->cur && policy->cur != freqs->old) {
369e837f9b5SJoe Perches 			pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
370e4472cb3SDave Jones 				 freqs->old, policy->cur);
371e4472cb3SDave Jones 			freqs->old = policy->cur;
3721da177e4SLinus Torvalds 		}
37320b5324dSViresh Kumar 
374b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
375e4472cb3SDave Jones 					 CPUFREQ_PRECHANGE, freqs);
37620b5324dSViresh Kumar 
3771da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3781da177e4SLinus Torvalds 		break;
379e4472cb3SDave Jones 
3801da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3811da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
38220b5324dSViresh Kumar 		pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
38320b5324dSViresh Kumar 			 cpumask_pr_args(policy->cpus));
38420b5324dSViresh Kumar 
385df24014aSViresh Kumar 		for_each_cpu(cpu, policy->cpus)
386df24014aSViresh Kumar 			trace_cpu_frequency(freqs->new, cpu);
387df24014aSViresh Kumar 
388b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
389e4472cb3SDave Jones 					 CPUFREQ_POSTCHANGE, freqs);
390bb176f7dSViresh Kumar 
39120b5324dSViresh Kumar 		cpufreq_stats_record_transition(policy, freqs->new);
39220b5324dSViresh Kumar 		policy->cur = freqs->new;
39320b5324dSViresh Kumar 	}
394b43a7ffbSViresh Kumar }
3951da177e4SLinus Torvalds 
396f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */
397236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
398f7ba3b41SViresh Kumar 		struct cpufreq_freqs *freqs, int transition_failed)
399f7ba3b41SViresh Kumar {
400f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
401f7ba3b41SViresh Kumar 	if (!transition_failed)
402f7ba3b41SViresh Kumar 		return;
403f7ba3b41SViresh Kumar 
404f7ba3b41SViresh Kumar 	swap(freqs->old, freqs->new);
405f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
406f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
407f7ba3b41SViresh Kumar }
408f7ba3b41SViresh Kumar 
40912478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
41012478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs)
41112478cf0SSrivatsa S. Bhat {
412ca654dc3SSrivatsa S. Bhat 
413ca654dc3SSrivatsa S. Bhat 	/*
414ca654dc3SSrivatsa S. Bhat 	 * Catch double invocations of _begin() which lead to self-deadlock.
415ca654dc3SSrivatsa S. Bhat 	 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
416ca654dc3SSrivatsa S. Bhat 	 * doesn't invoke _begin() on their behalf, and hence the chances of
417ca654dc3SSrivatsa S. Bhat 	 * double invocations are very low. Moreover, there are scenarios
418ca654dc3SSrivatsa S. Bhat 	 * where these checks can emit false-positive warnings in these
419ca654dc3SSrivatsa S. Bhat 	 * drivers; so we avoid that by skipping them altogether.
420ca654dc3SSrivatsa S. Bhat 	 */
421ca654dc3SSrivatsa S. Bhat 	WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
422ca654dc3SSrivatsa S. Bhat 				&& current == policy->transition_task);
423ca654dc3SSrivatsa S. Bhat 
42412478cf0SSrivatsa S. Bhat wait:
42512478cf0SSrivatsa S. Bhat 	wait_event(policy->transition_wait, !policy->transition_ongoing);
42612478cf0SSrivatsa S. Bhat 
42712478cf0SSrivatsa S. Bhat 	spin_lock(&policy->transition_lock);
42812478cf0SSrivatsa S. Bhat 
42912478cf0SSrivatsa S. Bhat 	if (unlikely(policy->transition_ongoing)) {
43012478cf0SSrivatsa S. Bhat 		spin_unlock(&policy->transition_lock);
43112478cf0SSrivatsa S. Bhat 		goto wait;
43212478cf0SSrivatsa S. Bhat 	}
43312478cf0SSrivatsa S. Bhat 
43412478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = true;
435ca654dc3SSrivatsa S. Bhat 	policy->transition_task = current;
43612478cf0SSrivatsa S. Bhat 
43712478cf0SSrivatsa S. Bhat 	spin_unlock(&policy->transition_lock);
43812478cf0SSrivatsa S. Bhat 
43912478cf0SSrivatsa S. Bhat 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
44012478cf0SSrivatsa S. Bhat }
44112478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
44212478cf0SSrivatsa S. Bhat 
44312478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
44412478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs, int transition_failed)
44512478cf0SSrivatsa S. Bhat {
4460e7ea2f3SIgor Stoppa 	if (WARN_ON(!policy->transition_ongoing))
44712478cf0SSrivatsa S. Bhat 		return;
44812478cf0SSrivatsa S. Bhat 
44912478cf0SSrivatsa S. Bhat 	cpufreq_notify_post_transition(policy, freqs, transition_failed);
45012478cf0SSrivatsa S. Bhat 
4511a0419b0SIonela Voinescu 	arch_set_freq_scale(policy->related_cpus,
4521a0419b0SIonela Voinescu 			    policy->cur,
4531a0419b0SIonela Voinescu 			    policy->cpuinfo.max_freq);
4541a0419b0SIonela Voinescu 
45512478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = false;
456ca654dc3SSrivatsa S. Bhat 	policy->transition_task = NULL;
45712478cf0SSrivatsa S. Bhat 
45812478cf0SSrivatsa S. Bhat 	wake_up(&policy->transition_wait);
45912478cf0SSrivatsa S. Bhat }
46012478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
46112478cf0SSrivatsa S. Bhat 
462b7898fdaSRafael J. Wysocki /*
463b7898fdaSRafael J. Wysocki  * Fast frequency switching status count.  Positive means "enabled", negative
464b7898fdaSRafael J. Wysocki  * means "disabled" and 0 means "not decided yet".
465b7898fdaSRafael J. Wysocki  */
466b7898fdaSRafael J. Wysocki static int cpufreq_fast_switch_count;
467b7898fdaSRafael J. Wysocki static DEFINE_MUTEX(cpufreq_fast_switch_lock);
468b7898fdaSRafael J. Wysocki 
469b7898fdaSRafael J. Wysocki static void cpufreq_list_transition_notifiers(void)
470b7898fdaSRafael J. Wysocki {
471b7898fdaSRafael J. Wysocki 	struct notifier_block *nb;
472b7898fdaSRafael J. Wysocki 
473b7898fdaSRafael J. Wysocki 	pr_info("Registered transition notifiers:\n");
474b7898fdaSRafael J. Wysocki 
475b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_transition_notifier_list.mutex);
476b7898fdaSRafael J. Wysocki 
477b7898fdaSRafael J. Wysocki 	for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
478d75f773cSSakari Ailus 		pr_info("%pS\n", nb->notifier_call);
479b7898fdaSRafael J. Wysocki 
480b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_transition_notifier_list.mutex);
481b7898fdaSRafael J. Wysocki }
482b7898fdaSRafael J. Wysocki 
483b7898fdaSRafael J. Wysocki /**
484b7898fdaSRafael J. Wysocki  * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
485b7898fdaSRafael J. Wysocki  * @policy: cpufreq policy to enable fast frequency switching for.
486b7898fdaSRafael J. Wysocki  *
487b7898fdaSRafael J. Wysocki  * Try to enable fast frequency switching for @policy.
488b7898fdaSRafael J. Wysocki  *
489b7898fdaSRafael J. Wysocki  * The attempt will fail if there is at least one transition notifier registered
490b7898fdaSRafael J. Wysocki  * at this point, as fast frequency switching is quite fundamentally at odds
491b7898fdaSRafael J. Wysocki  * with transition notifiers.  Thus if successful, it will make registration of
492b7898fdaSRafael J. Wysocki  * transition notifiers fail going forward.
493b7898fdaSRafael J. Wysocki  */
494b7898fdaSRafael J. Wysocki void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
495b7898fdaSRafael J. Wysocki {
496b7898fdaSRafael J. Wysocki 	lockdep_assert_held(&policy->rwsem);
497b7898fdaSRafael J. Wysocki 
498b7898fdaSRafael J. Wysocki 	if (!policy->fast_switch_possible)
499b7898fdaSRafael J. Wysocki 		return;
500b7898fdaSRafael J. Wysocki 
501b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_fast_switch_lock);
502b7898fdaSRafael J. Wysocki 	if (cpufreq_fast_switch_count >= 0) {
503b7898fdaSRafael J. Wysocki 		cpufreq_fast_switch_count++;
504b7898fdaSRafael J. Wysocki 		policy->fast_switch_enabled = true;
505b7898fdaSRafael J. Wysocki 	} else {
506b7898fdaSRafael J. Wysocki 		pr_warn("CPU%u: Fast frequency switching not enabled\n",
507b7898fdaSRafael J. Wysocki 			policy->cpu);
508b7898fdaSRafael J. Wysocki 		cpufreq_list_transition_notifiers();
509b7898fdaSRafael J. Wysocki 	}
510b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_fast_switch_lock);
511b7898fdaSRafael J. Wysocki }
512b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
513b7898fdaSRafael J. Wysocki 
5146c9d9c81SRafael J. Wysocki /**
5156c9d9c81SRafael J. Wysocki  * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
5166c9d9c81SRafael J. Wysocki  * @policy: cpufreq policy to disable fast frequency switching for.
5176c9d9c81SRafael J. Wysocki  */
5186c9d9c81SRafael J. Wysocki void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
519b7898fdaSRafael J. Wysocki {
520b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_fast_switch_lock);
521b7898fdaSRafael J. Wysocki 	if (policy->fast_switch_enabled) {
522b7898fdaSRafael J. Wysocki 		policy->fast_switch_enabled = false;
523b7898fdaSRafael J. Wysocki 		if (!WARN_ON(cpufreq_fast_switch_count <= 0))
524b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count--;
525b7898fdaSRafael J. Wysocki 	}
526b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_fast_switch_lock);
527b7898fdaSRafael J. Wysocki }
5286c9d9c81SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
5291da177e4SLinus Torvalds 
530e3c06236SSteve Muckle /**
531e3c06236SSteve Muckle  * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
532e3c06236SSteve Muckle  * one.
533a9909c21SLee Jones  * @policy: associated policy to interrogate
534e3c06236SSteve Muckle  * @target_freq: target frequency to resolve.
535e3c06236SSteve Muckle  *
536e3c06236SSteve Muckle  * The target to driver frequency mapping is cached in the policy.
537e3c06236SSteve Muckle  *
538e3c06236SSteve Muckle  * Return: Lowest driver-supported frequency greater than or equal to the
539e3c06236SSteve Muckle  * given target_freq, subject to policy (min/max) and driver limitations.
540e3c06236SSteve Muckle  */
541e3c06236SSteve Muckle unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
542e3c06236SSteve Muckle 					 unsigned int target_freq)
543e3c06236SSteve Muckle {
544e3c06236SSteve Muckle 	target_freq = clamp_val(target_freq, policy->min, policy->max);
545e3c06236SSteve Muckle 	policy->cached_target_freq = target_freq;
546abe8bd02SViresh Kumar 
547abe8bd02SViresh Kumar 	if (cpufreq_driver->target_index) {
548292072c3SViresh Kumar 		unsigned int idx;
549abe8bd02SViresh Kumar 
550abe8bd02SViresh Kumar 		idx = cpufreq_frequency_table_target(policy, target_freq,
551abe8bd02SViresh Kumar 						     CPUFREQ_RELATION_L);
552abe8bd02SViresh Kumar 		policy->cached_resolved_idx = idx;
553abe8bd02SViresh Kumar 		return policy->freq_table[idx].frequency;
554abe8bd02SViresh Kumar 	}
555abe8bd02SViresh Kumar 
556e3c06236SSteve Muckle 	if (cpufreq_driver->resolve_freq)
557e3c06236SSteve Muckle 		return cpufreq_driver->resolve_freq(policy, target_freq);
558abe8bd02SViresh Kumar 
559abe8bd02SViresh Kumar 	return target_freq;
560e3c06236SSteve Muckle }
561ae2c1ca6SSteve Muckle EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
562e3c06236SSteve Muckle 
563aa7519afSViresh Kumar unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
564aa7519afSViresh Kumar {
565aa7519afSViresh Kumar 	unsigned int latency;
566aa7519afSViresh Kumar 
567aa7519afSViresh Kumar 	if (policy->transition_delay_us)
568aa7519afSViresh Kumar 		return policy->transition_delay_us;
569aa7519afSViresh Kumar 
570aa7519afSViresh Kumar 	latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
571e948bc8fSViresh Kumar 	if (latency) {
572e948bc8fSViresh Kumar 		/*
573e948bc8fSViresh Kumar 		 * For platforms that can change the frequency very fast (< 10
574e948bc8fSViresh Kumar 		 * us), the above formula gives a decent transition delay. But
575e948bc8fSViresh Kumar 		 * for platforms where transition_latency is in milliseconds, it
576e948bc8fSViresh Kumar 		 * ends up giving unrealistic values.
577e948bc8fSViresh Kumar 		 *
578e948bc8fSViresh Kumar 		 * Cap the default transition delay to 10 ms, which seems to be
579e948bc8fSViresh Kumar 		 * a reasonable amount of time after which we should reevaluate
580e948bc8fSViresh Kumar 		 * the frequency.
581e948bc8fSViresh Kumar 		 */
582e948bc8fSViresh Kumar 		return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
583e948bc8fSViresh Kumar 	}
584aa7519afSViresh Kumar 
585aa7519afSViresh Kumar 	return LATENCY_MULTIPLIER;
586aa7519afSViresh Kumar }
587aa7519afSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
588aa7519afSViresh Kumar 
5891da177e4SLinus Torvalds /*********************************************************************
5901da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
5911da177e4SLinus Torvalds  *********************************************************************/
5928a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj,
593625c85a6SViresh Kumar 			  struct kobj_attribute *attr, char *buf)
5946f19efc0SLukasz Majewski {
5956f19efc0SLukasz Majewski 	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
5966f19efc0SLukasz Majewski }
5976f19efc0SLukasz Majewski 
598625c85a6SViresh Kumar static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
5996f19efc0SLukasz Majewski 			   const char *buf, size_t count)
6006f19efc0SLukasz Majewski {
6016f19efc0SLukasz Majewski 	int ret, enable;
6026f19efc0SLukasz Majewski 
6036f19efc0SLukasz Majewski 	ret = sscanf(buf, "%d", &enable);
6046f19efc0SLukasz Majewski 	if (ret != 1 || enable < 0 || enable > 1)
6056f19efc0SLukasz Majewski 		return -EINVAL;
6066f19efc0SLukasz Majewski 
6076f19efc0SLukasz Majewski 	if (cpufreq_boost_trigger_state(enable)) {
608e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST!\n",
609e837f9b5SJoe Perches 		       __func__, enable ? "enable" : "disable");
6106f19efc0SLukasz Majewski 		return -EINVAL;
6116f19efc0SLukasz Majewski 	}
6126f19efc0SLukasz Majewski 
613e837f9b5SJoe Perches 	pr_debug("%s: cpufreq BOOST %s\n",
614e837f9b5SJoe Perches 		 __func__, enable ? "enabled" : "disabled");
6156f19efc0SLukasz Majewski 
6166f19efc0SLukasz Majewski 	return count;
6176f19efc0SLukasz Majewski }
6186f19efc0SLukasz Majewski define_one_global_rw(boost);
6191da177e4SLinus Torvalds 
62042f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor)
6213bcb09a3SJeremy Fitzhardinge {
6223bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
6233bcb09a3SJeremy Fitzhardinge 
624f7b27061SViresh Kumar 	for_each_governor(t)
6257c4f4539SRasmus Villemoes 		if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
6263bcb09a3SJeremy Fitzhardinge 			return t;
6273bcb09a3SJeremy Fitzhardinge 
6283bcb09a3SJeremy Fitzhardinge 	return NULL;
6293bcb09a3SJeremy Fitzhardinge }
6303bcb09a3SJeremy Fitzhardinge 
6318cc46ae5SViresh Kumar static struct cpufreq_governor *get_governor(const char *str_governor)
6328cc46ae5SViresh Kumar {
6338cc46ae5SViresh Kumar 	struct cpufreq_governor *t;
6348cc46ae5SViresh Kumar 
6358cc46ae5SViresh Kumar 	mutex_lock(&cpufreq_governor_mutex);
6368cc46ae5SViresh Kumar 	t = find_governor(str_governor);
6378cc46ae5SViresh Kumar 	if (!t)
6388cc46ae5SViresh Kumar 		goto unlock;
6398cc46ae5SViresh Kumar 
6408cc46ae5SViresh Kumar 	if (!try_module_get(t->owner))
6418cc46ae5SViresh Kumar 		t = NULL;
6428cc46ae5SViresh Kumar 
6438cc46ae5SViresh Kumar unlock:
6448cc46ae5SViresh Kumar 	mutex_unlock(&cpufreq_governor_mutex);
6458cc46ae5SViresh Kumar 
6468cc46ae5SViresh Kumar 	return t;
6478cc46ae5SViresh Kumar }
6488cc46ae5SViresh Kumar 
6491e4f63aeSRafael J. Wysocki static unsigned int cpufreq_parse_policy(char *str_governor)
6501da177e4SLinus Torvalds {
6511e4f63aeSRafael J. Wysocki 	if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN))
6521e4f63aeSRafael J. Wysocki 		return CPUFREQ_POLICY_PERFORMANCE;
6531e4f63aeSRafael J. Wysocki 
6541e4f63aeSRafael J. Wysocki 	if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN))
6551e4f63aeSRafael J. Wysocki 		return CPUFREQ_POLICY_POWERSAVE;
6561e4f63aeSRafael J. Wysocki 
6571e4f63aeSRafael J. Wysocki 	return CPUFREQ_POLICY_UNKNOWN;
658ab05d97aSYue Hu }
659ab05d97aSYue Hu 
660ab05d97aSYue Hu /**
6615ddc6d4eSViresh Kumar  * cpufreq_parse_governor - parse a governor string only for has_target()
6621e4f63aeSRafael J. Wysocki  * @str_governor: Governor name.
663ab05d97aSYue Hu  */
6641e4f63aeSRafael J. Wysocki static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor)
665ab05d97aSYue Hu {
6661da177e4SLinus Torvalds 	struct cpufreq_governor *t;
6673bcb09a3SJeremy Fitzhardinge 
6688cc46ae5SViresh Kumar 	t = get_governor(str_governor);
6698cc46ae5SViresh Kumar 	if (t)
6708cc46ae5SViresh Kumar 		return t;
6713bcb09a3SJeremy Fitzhardinge 
6728cc46ae5SViresh Kumar 	if (request_module("cpufreq_%s", str_governor))
6731e4f63aeSRafael J. Wysocki 		return NULL;
674045149e6SRafael J. Wysocki 
6758cc46ae5SViresh Kumar 	return get_governor(str_governor);
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
678a9909c21SLee Jones /*
679e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
680e08f5f5bSGautham R Shenoy  * print out cpufreq information
6811da177e4SLinus Torvalds  *
6821da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
6831da177e4SLinus Torvalds  * "unsigned int".
6841da177e4SLinus Torvalds  */
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds #define show_one(file_name, object)			\
6871da177e4SLinus Torvalds static ssize_t show_##file_name				\
6881da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
6891da177e4SLinus Torvalds {							\
6901da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
6941da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
695ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
6961da177e4SLinus Torvalds show_one(scaling_min_freq, min);
6971da177e4SLinus Torvalds show_one(scaling_max_freq, max);
698c034b02eSDirk Brandewie 
699f8475cefSLen Brown __weak unsigned int arch_freq_get_on_cpu(int cpu)
700f8475cefSLen Brown {
701f8475cefSLen Brown 	return 0;
702f8475cefSLen Brown }
703f8475cefSLen Brown 
70409347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
705c034b02eSDirk Brandewie {
706c034b02eSDirk Brandewie 	ssize_t ret;
707f8475cefSLen Brown 	unsigned int freq;
708c034b02eSDirk Brandewie 
709f8475cefSLen Brown 	freq = arch_freq_get_on_cpu(policy->cpu);
710f8475cefSLen Brown 	if (freq)
711f8475cefSLen Brown 		ret = sprintf(buf, "%u\n", freq);
712681fe684SViresh Kumar 	else if (cpufreq_driver->setpolicy && cpufreq_driver->get)
713c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
714c034b02eSDirk Brandewie 	else
715c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", policy->cur);
716c034b02eSDirk Brandewie 	return ret;
717c034b02eSDirk Brandewie }
7181da177e4SLinus Torvalds 
719a9909c21SLee Jones /*
7201da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
7211da177e4SLinus Torvalds  */
7221da177e4SLinus Torvalds #define store_one(file_name, object)			\
7231da177e4SLinus Torvalds static ssize_t store_##file_name					\
7241da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
7251da177e4SLinus Torvalds {									\
72618c49926SViresh Kumar 	unsigned long val;						\
72718c49926SViresh Kumar 	int ret;							\
7281da177e4SLinus Torvalds 									\
72918c49926SViresh Kumar 	ret = sscanf(buf, "%lu", &val);					\
7301da177e4SLinus Torvalds 	if (ret != 1)							\
7311da177e4SLinus Torvalds 		return -EINVAL;						\
7321da177e4SLinus Torvalds 									\
7333000ce3cSRafael J. Wysocki 	ret = freq_qos_update_request(policy->object##_freq_req, val);\
73418c49926SViresh Kumar 	return ret >= 0 ? count : ret;					\
7351da177e4SLinus Torvalds }
7361da177e4SLinus Torvalds 
7371da177e4SLinus Torvalds store_one(scaling_min_freq, min);
7381da177e4SLinus Torvalds store_one(scaling_max_freq, max);
7391da177e4SLinus Torvalds 
740a9909c21SLee Jones /*
7411da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
7421da177e4SLinus Torvalds  */
743e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
744e08f5f5bSGautham R Shenoy 					char *buf)
7451da177e4SLinus Torvalds {
746d92d50a4SViresh Kumar 	unsigned int cur_freq = __cpufreq_get(policy);
7479b4f603eSRafael J. Wysocki 
7489b4f603eSRafael J. Wysocki 	if (cur_freq)
7491da177e4SLinus Torvalds 		return sprintf(buf, "%u\n", cur_freq);
7509b4f603eSRafael J. Wysocki 
7519b4f603eSRafael J. Wysocki 	return sprintf(buf, "<unknown>\n");
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
754a9909c21SLee Jones /*
7551da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
7561da177e4SLinus Torvalds  */
757905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
7601da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
7611da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
7621da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
7631da177e4SLinus Torvalds 	else if (policy->governor)
7644b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
76529464f28SDave Jones 				policy->governor->name);
7661da177e4SLinus Torvalds 	return -EINVAL;
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
769a9909c21SLee Jones /*
7701da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
7711da177e4SLinus Torvalds  */
7721da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
7731da177e4SLinus Torvalds 					const char *buf, size_t count)
7741da177e4SLinus Torvalds {
7751da177e4SLinus Torvalds 	char str_governor[16];
7761e4f63aeSRafael J. Wysocki 	int ret;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
7791da177e4SLinus Torvalds 	if (ret != 1)
7801da177e4SLinus Torvalds 		return -EINVAL;
7811da177e4SLinus Torvalds 
782ab05d97aSYue Hu 	if (cpufreq_driver->setpolicy) {
7831e4f63aeSRafael J. Wysocki 		unsigned int new_pol;
7841e4f63aeSRafael J. Wysocki 
7851e4f63aeSRafael J. Wysocki 		new_pol = cpufreq_parse_policy(str_governor);
7861e4f63aeSRafael J. Wysocki 		if (!new_pol)
787ab05d97aSYue Hu 			return -EINVAL;
7881e4f63aeSRafael J. Wysocki 
7891e4f63aeSRafael J. Wysocki 		ret = cpufreq_set_policy(policy, NULL, new_pol);
790ab05d97aSYue Hu 	} else {
7911e4f63aeSRafael J. Wysocki 		struct cpufreq_governor *new_gov;
7921e4f63aeSRafael J. Wysocki 
7931e4f63aeSRafael J. Wysocki 		new_gov = cpufreq_parse_governor(str_governor);
7941e4f63aeSRafael J. Wysocki 		if (!new_gov)
7951da177e4SLinus Torvalds 			return -EINVAL;
7961e4f63aeSRafael J. Wysocki 
7971e4f63aeSRafael J. Wysocki 		ret = cpufreq_set_policy(policy, new_gov,
7981e4f63aeSRafael J. Wysocki 					 CPUFREQ_POLICY_UNKNOWN);
7991e4f63aeSRafael J. Wysocki 
8001e4f63aeSRafael J. Wysocki 		module_put(new_gov->owner);
801ab05d97aSYue Hu 	}
8021da177e4SLinus Torvalds 
80388dc4384SViresh Kumar 	return ret ? ret : count;
8041da177e4SLinus Torvalds }
8051da177e4SLinus Torvalds 
806a9909c21SLee Jones /*
8071da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
8081da177e4SLinus Torvalds  */
8091da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
8101da177e4SLinus Torvalds {
8111c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
8121da177e4SLinus Torvalds }
8131da177e4SLinus Torvalds 
814a9909c21SLee Jones /*
8151da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
8161da177e4SLinus Torvalds  */
8171da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
8181da177e4SLinus Torvalds 						char *buf)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds 	ssize_t i = 0;
8211da177e4SLinus Torvalds 	struct cpufreq_governor *t;
8221da177e4SLinus Torvalds 
8239c0ebcf7SViresh Kumar 	if (!has_target()) {
8241da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
8251da177e4SLinus Torvalds 		goto out;
8261da177e4SLinus Torvalds 	}
8271da177e4SLinus Torvalds 
8288cc46ae5SViresh Kumar 	mutex_lock(&cpufreq_governor_mutex);
829f7b27061SViresh Kumar 	for_each_governor(t) {
83029464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
83129464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
8328cc46ae5SViresh Kumar 			break;
8334b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
8341da177e4SLinus Torvalds 	}
8358cc46ae5SViresh Kumar 	mutex_unlock(&cpufreq_governor_mutex);
8361da177e4SLinus Torvalds out:
8371da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
8381da177e4SLinus Torvalds 	return i;
8391da177e4SLinus Torvalds }
840e8628dd0SDarrick J. Wong 
841f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
8421da177e4SLinus Torvalds {
8431da177e4SLinus Torvalds 	ssize_t i = 0;
8441da177e4SLinus Torvalds 	unsigned int cpu;
8451da177e4SLinus Torvalds 
846835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
8471da177e4SLinus Torvalds 		if (i)
8481da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
8491da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
8501da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
8511da177e4SLinus Torvalds 			break;
8521da177e4SLinus Torvalds 	}
8531da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
8541da177e4SLinus Torvalds 	return i;
8551da177e4SLinus Torvalds }
856f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
8571da177e4SLinus Torvalds 
858a9909c21SLee Jones /*
859e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
860e8628dd0SDarrick J. Wong  * hw coordination is in use
861e8628dd0SDarrick J. Wong  */
862e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
863e8628dd0SDarrick J. Wong {
864f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
865e8628dd0SDarrick J. Wong }
866e8628dd0SDarrick J. Wong 
867a9909c21SLee Jones /*
868e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
869e8628dd0SDarrick J. Wong  */
870e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
871e8628dd0SDarrick J. Wong {
872f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
873e8628dd0SDarrick J. Wong }
874e8628dd0SDarrick J. Wong 
8759e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
8769e76988eSVenki Pallipadi 					const char *buf, size_t count)
8779e76988eSVenki Pallipadi {
8789e76988eSVenki Pallipadi 	unsigned int freq = 0;
8799e76988eSVenki Pallipadi 	unsigned int ret;
8809e76988eSVenki Pallipadi 
881879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
8829e76988eSVenki Pallipadi 		return -EINVAL;
8839e76988eSVenki Pallipadi 
8849e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
8859e76988eSVenki Pallipadi 	if (ret != 1)
8869e76988eSVenki Pallipadi 		return -EINVAL;
8879e76988eSVenki Pallipadi 
8889e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
8899e76988eSVenki Pallipadi 
8909e76988eSVenki Pallipadi 	return count;
8919e76988eSVenki Pallipadi }
8929e76988eSVenki Pallipadi 
8939e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
8949e76988eSVenki Pallipadi {
895879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
8969e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
8979e76988eSVenki Pallipadi 
8989e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
8999e76988eSVenki Pallipadi }
9001da177e4SLinus Torvalds 
901a9909c21SLee Jones /*
9028bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
903e2f74f35SThomas Renninger  */
904e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
905e2f74f35SThomas Renninger {
906e2f74f35SThomas Renninger 	unsigned int limit;
907e2f74f35SThomas Renninger 	int ret;
9081c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
909e2f74f35SThomas Renninger 	if (!ret)
910e2f74f35SThomas Renninger 		return sprintf(buf, "%u\n", limit);
911e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
912e2f74f35SThomas Renninger }
913e2f74f35SThomas Renninger 
9146dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
9156dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
9166dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
9176dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
9186dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
9196dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
9206dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
9216dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
9226dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
9236dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
9246dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
9256dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
9266dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
9276dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds static struct attribute *default_attrs[] = {
9301da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
9311da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
932ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
9331da177e4SLinus Torvalds 	&scaling_min_freq.attr,
9341da177e4SLinus Torvalds 	&scaling_max_freq.attr,
9351da177e4SLinus Torvalds 	&affected_cpus.attr,
936e8628dd0SDarrick J. Wong 	&related_cpus.attr,
9371da177e4SLinus Torvalds 	&scaling_governor.attr,
9381da177e4SLinus Torvalds 	&scaling_driver.attr,
9391da177e4SLinus Torvalds 	&scaling_available_governors.attr,
9409e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
9411da177e4SLinus Torvalds 	NULL
9421da177e4SLinus Torvalds };
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
9451da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
9481da177e4SLinus Torvalds {
9491da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
9501da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
9511b750e3bSViresh Kumar 	ssize_t ret;
9526eed9404SViresh Kumar 
953e6e8df07SKai Shen 	if (!fattr->show)
954e6e8df07SKai Shen 		return -EIO;
955e6e8df07SKai Shen 
956ad7722daSviresh kumar 	down_read(&policy->rwsem);
957e08f5f5bSGautham R Shenoy 	ret = fattr->show(policy, buf);
958ad7722daSviresh kumar 	up_read(&policy->rwsem);
9591b750e3bSViresh Kumar 
9601da177e4SLinus Torvalds 	return ret;
9611da177e4SLinus Torvalds }
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
9641da177e4SLinus Torvalds 		     const char *buf, size_t count)
9651da177e4SLinus Torvalds {
9661da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
9671da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
968a07530b4SDave Jones 	ssize_t ret = -EINVAL;
9696eed9404SViresh Kumar 
970e6e8df07SKai Shen 	if (!fattr->store)
971e6e8df07SKai Shen 		return -EIO;
972e6e8df07SKai Shen 
9739b3d9bb3SWaiman Long 	/*
9749b3d9bb3SWaiman Long 	 * cpus_read_trylock() is used here to work around a circular lock
9759b3d9bb3SWaiman Long 	 * dependency problem with respect to the cpufreq_register_driver().
9769b3d9bb3SWaiman Long 	 */
9779b3d9bb3SWaiman Long 	if (!cpus_read_trylock())
9789b3d9bb3SWaiman Long 		return -EBUSY;
9794f750c93SSrivatsa S. Bhat 
9806541aef0SRafael J. Wysocki 	if (cpu_online(policy->cpu)) {
981ad7722daSviresh kumar 		down_write(&policy->rwsem);
982e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
983ad7722daSviresh kumar 		up_write(&policy->rwsem);
9846541aef0SRafael J. Wysocki 	}
9856541aef0SRafael J. Wysocki 
986a92551e4SSebastian Andrzej Siewior 	cpus_read_unlock();
9874f750c93SSrivatsa S. Bhat 
9881da177e4SLinus Torvalds 	return ret;
9891da177e4SLinus Torvalds }
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
9921da177e4SLinus Torvalds {
9931da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
9942d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
9951da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
99852cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
9991da177e4SLinus Torvalds 	.show	= show,
10001da177e4SLinus Torvalds 	.store	= store,
10011da177e4SLinus Torvalds };
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
10041da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
10051da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
10061da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
10071da177e4SLinus Torvalds };
10081da177e4SLinus Torvalds 
10092f0ba790SRafael J. Wysocki static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
101087549141SViresh Kumar {
10112f0ba790SRafael J. Wysocki 	struct device *dev = get_cpu_device(cpu);
10122f0ba790SRafael J. Wysocki 
101367d874c3SViresh Kumar 	if (unlikely(!dev))
10142f0ba790SRafael J. Wysocki 		return;
10152f0ba790SRafael J. Wysocki 
10162f0ba790SRafael J. Wysocki 	if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
10172f0ba790SRafael J. Wysocki 		return;
10182f0ba790SRafael J. Wysocki 
101926619804SViresh Kumar 	dev_dbg(dev, "%s: Adding symlink\n", __func__);
10202f0ba790SRafael J. Wysocki 	if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
10212f0ba790SRafael J. Wysocki 		dev_err(dev, "cpufreq symlink creation failed\n");
102287549141SViresh Kumar }
102387549141SViresh Kumar 
102426619804SViresh Kumar static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
102526619804SViresh Kumar 				   struct device *dev)
102687549141SViresh Kumar {
102726619804SViresh Kumar 	dev_dbg(dev, "%s: Removing symlink\n", __func__);
102826619804SViresh Kumar 	sysfs_remove_link(&dev->kobj, "cpufreq");
102987549141SViresh Kumar }
103087549141SViresh Kumar 
1031d9612a49SRafael J. Wysocki static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
1032909a694eSDave Jones {
1033909a694eSDave Jones 	struct freq_attr **drv_attr;
1034909a694eSDave Jones 	int ret = 0;
1035909a694eSDave Jones 
1036909a694eSDave Jones 	/* set up files for this cpu device */
10371c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
1038f13f1184SViresh Kumar 	while (drv_attr && *drv_attr) {
1039909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
1040909a694eSDave Jones 		if (ret)
10416d4e81edSTomeu Vizoso 			return ret;
1042909a694eSDave Jones 		drv_attr++;
1043909a694eSDave Jones 	}
10441c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
1045909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
1046909a694eSDave Jones 		if (ret)
10476d4e81edSTomeu Vizoso 			return ret;
1048909a694eSDave Jones 	}
1049c034b02eSDirk Brandewie 
1050909a694eSDave Jones 	ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
1051909a694eSDave Jones 	if (ret)
10526d4e81edSTomeu Vizoso 		return ret;
1053c034b02eSDirk Brandewie 
10541c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
1055e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1056e2f74f35SThomas Renninger 		if (ret)
10576d4e81edSTomeu Vizoso 			return ret;
1058e2f74f35SThomas Renninger 	}
1059909a694eSDave Jones 
106026619804SViresh Kumar 	return 0;
1061e18f1682SSrivatsa S. Bhat }
1062e18f1682SSrivatsa S. Bhat 
10637f0fa40fSViresh Kumar static int cpufreq_init_policy(struct cpufreq_policy *policy)
1064e18f1682SSrivatsa S. Bhat {
10651e4f63aeSRafael J. Wysocki 	struct cpufreq_governor *gov = NULL;
10661e4f63aeSRafael J. Wysocki 	unsigned int pol = CPUFREQ_POLICY_UNKNOWN;
10678cc46ae5SViresh Kumar 	int ret;
1068ab05d97aSYue Hu 
1069ab05d97aSYue Hu 	if (has_target()) {
10701e4f63aeSRafael J. Wysocki 		/* Update policy governor to the one used before hotplug. */
10718cc46ae5SViresh Kumar 		gov = get_governor(policy->last_governor);
1072de1df26bSRafael J. Wysocki 		if (gov) {
10736e2c89d1Sviresh kumar 			pr_debug("Restoring governor %s for cpu %d\n",
10748412b456SQuentin Perret 				 gov->name, policy->cpu);
10751e4f63aeSRafael J. Wysocki 		} else {
10768412b456SQuentin Perret 			gov = get_governor(default_governor);
1077de1df26bSRafael J. Wysocki 		}
10788412b456SQuentin Perret 
10798412b456SQuentin Perret 		if (!gov) {
10808412b456SQuentin Perret 			gov = cpufreq_default_governor();
10818412b456SQuentin Perret 			__module_get(gov->owner);
10828412b456SQuentin Perret 		}
10838412b456SQuentin Perret 
1084ab05d97aSYue Hu 	} else {
10858412b456SQuentin Perret 
108669030dd1SSrinivas Pandruvada 		/* Use the default policy if there is no last_policy. */
1087ab05d97aSYue Hu 		if (policy->last_policy) {
10881e4f63aeSRafael J. Wysocki 			pol = policy->last_policy;
10898412b456SQuentin Perret 		} else {
10908412b456SQuentin Perret 			pol = cpufreq_parse_policy(default_governor);
1091f5739cb0SRafael J. Wysocki 			/*
10928412b456SQuentin Perret 			 * In case the default governor is neither "performance"
1093f5739cb0SRafael J. Wysocki 			 * nor "powersave", fall back to the initial policy
1094f5739cb0SRafael J. Wysocki 			 * value set by the driver.
1095f5739cb0SRafael J. Wysocki 			 */
1096f5739cb0SRafael J. Wysocki 			if (pol == CPUFREQ_POLICY_UNKNOWN)
1097f5739cb0SRafael J. Wysocki 				pol = policy->policy;
109869030dd1SSrinivas Pandruvada 		}
1099f5739cb0SRafael J. Wysocki 		if (pol != CPUFREQ_POLICY_PERFORMANCE &&
1100f5739cb0SRafael J. Wysocki 		    pol != CPUFREQ_POLICY_POWERSAVE)
1101f5739cb0SRafael J. Wysocki 			return -ENODATA;
1102ab05d97aSYue Hu 	}
1103ab05d97aSYue Hu 
11048cc46ae5SViresh Kumar 	ret = cpufreq_set_policy(policy, gov, pol);
11058cc46ae5SViresh Kumar 	if (gov)
11068cc46ae5SViresh Kumar 		module_put(gov->owner);
11078cc46ae5SViresh Kumar 
11088cc46ae5SViresh Kumar 	return ret;
1109909a694eSDave Jones }
1110909a694eSDave Jones 
1111d9612a49SRafael J. Wysocki static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1112fcf80582SViresh Kumar {
11139c0ebcf7SViresh Kumar 	int ret = 0;
1114fcf80582SViresh Kumar 
1115bb29ae15SViresh Kumar 	/* Has this CPU been taken care of already? */
1116bb29ae15SViresh Kumar 	if (cpumask_test_cpu(cpu, policy->cpus))
1117bb29ae15SViresh Kumar 		return 0;
1118bb29ae15SViresh Kumar 
111949f18560SViresh Kumar 	down_write(&policy->rwsem);
112045482c70SRafael J. Wysocki 	if (has_target())
112145482c70SRafael J. Wysocki 		cpufreq_stop_governor(policy);
1122fcf80582SViresh Kumar 
1123fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
11242eaa3e2dSViresh Kumar 
11259c0ebcf7SViresh Kumar 	if (has_target()) {
11260a300767SRafael J. Wysocki 		ret = cpufreq_start_governor(policy);
112749f18560SViresh Kumar 		if (ret)
11283de9bdebSViresh Kumar 			pr_err("%s: Failed to start governor\n", __func__);
1129820c6ca2SViresh Kumar 	}
113049f18560SViresh Kumar 	up_write(&policy->rwsem);
1131fcf80582SViresh Kumar 	return ret;
1132fcf80582SViresh Kumar }
1133fcf80582SViresh Kumar 
1134c57b25bdSViresh Kumar void refresh_frequency_limits(struct cpufreq_policy *policy)
113570a59fdeSViresh Kumar {
113667d874c3SViresh Kumar 	if (!policy_is_inactive(policy)) {
113770a59fdeSViresh Kumar 		pr_debug("updating policy for CPU %u\n", policy->cpu);
113870a59fdeSViresh Kumar 
11391e4f63aeSRafael J. Wysocki 		cpufreq_set_policy(policy, policy->governor, policy->policy);
114070a59fdeSViresh Kumar 	}
114167d874c3SViresh Kumar }
1142c57b25bdSViresh Kumar EXPORT_SYMBOL(refresh_frequency_limits);
114370a59fdeSViresh Kumar 
114411eb69b9SViresh Kumar static void handle_update(struct work_struct *work)
114511eb69b9SViresh Kumar {
114611eb69b9SViresh Kumar 	struct cpufreq_policy *policy =
114711eb69b9SViresh Kumar 		container_of(work, struct cpufreq_policy, update);
114870a59fdeSViresh Kumar 
114970a59fdeSViresh Kumar 	pr_debug("handle_update for cpu %u called\n", policy->cpu);
115067d874c3SViresh Kumar 	down_write(&policy->rwsem);
115170a59fdeSViresh Kumar 	refresh_frequency_limits(policy);
115267d874c3SViresh Kumar 	up_write(&policy->rwsem);
1153fcf80582SViresh Kumar }
11541da177e4SLinus Torvalds 
115567d874c3SViresh Kumar static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq,
115667d874c3SViresh Kumar 				void *data)
11578414809cSSrivatsa S. Bhat {
115867d874c3SViresh Kumar 	struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min);
1159e9698cc5SSrivatsa S. Bhat 
116067d874c3SViresh Kumar 	schedule_work(&policy->update);
116167d874c3SViresh Kumar 	return 0;
1162edd4a893SViresh Kumar }
1163edd4a893SViresh Kumar 
116467d874c3SViresh Kumar static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq,
116567d874c3SViresh Kumar 				void *data)
116667d874c3SViresh Kumar {
116767d874c3SViresh Kumar 	struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max);
1168ad7722daSviresh kumar 
116967d874c3SViresh Kumar 	schedule_work(&policy->update);
117067d874c3SViresh Kumar 	return 0;
1171e9698cc5SSrivatsa S. Bhat }
1172e9698cc5SSrivatsa S. Bhat 
1173f9f41e3eSViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
117442f921a6SViresh Kumar {
117542f921a6SViresh Kumar 	struct kobject *kobj;
117642f921a6SViresh Kumar 	struct completion *cmp;
117742f921a6SViresh Kumar 
117887549141SViresh Kumar 	down_write(&policy->rwsem);
11791aefc75bSRafael J. Wysocki 	cpufreq_stats_free_table(policy);
118042f921a6SViresh Kumar 	kobj = &policy->kobj;
118142f921a6SViresh Kumar 	cmp = &policy->kobj_unregister;
118287549141SViresh Kumar 	up_write(&policy->rwsem);
118342f921a6SViresh Kumar 	kobject_put(kobj);
118442f921a6SViresh Kumar 
118542f921a6SViresh Kumar 	/*
118642f921a6SViresh Kumar 	 * We need to make sure that the underlying kobj is
118742f921a6SViresh Kumar 	 * actually not referenced anymore by anybody before we
118842f921a6SViresh Kumar 	 * proceed with unloading.
118942f921a6SViresh Kumar 	 */
119042f921a6SViresh Kumar 	pr_debug("waiting for dropping of refcount\n");
119142f921a6SViresh Kumar 	wait_for_completion(cmp);
119242f921a6SViresh Kumar 	pr_debug("wait complete\n");
119342f921a6SViresh Kumar }
119442f921a6SViresh Kumar 
119567d874c3SViresh Kumar static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
119667d874c3SViresh Kumar {
119767d874c3SViresh Kumar 	struct cpufreq_policy *policy;
119867d874c3SViresh Kumar 	struct device *dev = get_cpu_device(cpu);
119967d874c3SViresh Kumar 	int ret;
120067d874c3SViresh Kumar 
120167d874c3SViresh Kumar 	if (!dev)
120267d874c3SViresh Kumar 		return NULL;
120367d874c3SViresh Kumar 
120467d874c3SViresh Kumar 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
120567d874c3SViresh Kumar 	if (!policy)
120667d874c3SViresh Kumar 		return NULL;
120767d874c3SViresh Kumar 
120867d874c3SViresh Kumar 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
120967d874c3SViresh Kumar 		goto err_free_policy;
121067d874c3SViresh Kumar 
121167d874c3SViresh Kumar 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
121267d874c3SViresh Kumar 		goto err_free_cpumask;
121367d874c3SViresh Kumar 
121467d874c3SViresh Kumar 	if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
121567d874c3SViresh Kumar 		goto err_free_rcpumask;
121667d874c3SViresh Kumar 
121767d874c3SViresh Kumar 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
121867d874c3SViresh Kumar 				   cpufreq_global_kobject, "policy%u", cpu);
121967d874c3SViresh Kumar 	if (ret) {
122067d874c3SViresh Kumar 		dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret);
122167d874c3SViresh Kumar 		/*
122267d874c3SViresh Kumar 		 * The entire policy object will be freed below, but the extra
122367d874c3SViresh Kumar 		 * memory allocated for the kobject name needs to be freed by
122467d874c3SViresh Kumar 		 * releasing the kobject.
122567d874c3SViresh Kumar 		 */
122667d874c3SViresh Kumar 		kobject_put(&policy->kobj);
122767d874c3SViresh Kumar 		goto err_free_real_cpus;
122867d874c3SViresh Kumar 	}
122967d874c3SViresh Kumar 
12303000ce3cSRafael J. Wysocki 	freq_constraints_init(&policy->constraints);
12313000ce3cSRafael J. Wysocki 
123267d874c3SViresh Kumar 	policy->nb_min.notifier_call = cpufreq_notifier_min;
123367d874c3SViresh Kumar 	policy->nb_max.notifier_call = cpufreq_notifier_max;
123467d874c3SViresh Kumar 
12353000ce3cSRafael J. Wysocki 	ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN,
12363000ce3cSRafael J. Wysocki 				    &policy->nb_min);
123767d874c3SViresh Kumar 	if (ret) {
123867d874c3SViresh Kumar 		dev_err(dev, "Failed to register MIN QoS notifier: %d (%*pbl)\n",
123967d874c3SViresh Kumar 			ret, cpumask_pr_args(policy->cpus));
124067d874c3SViresh Kumar 		goto err_kobj_remove;
124167d874c3SViresh Kumar 	}
124267d874c3SViresh Kumar 
12433000ce3cSRafael J. Wysocki 	ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX,
12443000ce3cSRafael J. Wysocki 				    &policy->nb_max);
124567d874c3SViresh Kumar 	if (ret) {
124667d874c3SViresh Kumar 		dev_err(dev, "Failed to register MAX QoS notifier: %d (%*pbl)\n",
124767d874c3SViresh Kumar 			ret, cpumask_pr_args(policy->cpus));
124867d874c3SViresh Kumar 		goto err_min_qos_notifier;
124967d874c3SViresh Kumar 	}
125067d874c3SViresh Kumar 
125167d874c3SViresh Kumar 	INIT_LIST_HEAD(&policy->policy_list);
125267d874c3SViresh Kumar 	init_rwsem(&policy->rwsem);
125367d874c3SViresh Kumar 	spin_lock_init(&policy->transition_lock);
125467d874c3SViresh Kumar 	init_waitqueue_head(&policy->transition_wait);
125567d874c3SViresh Kumar 	init_completion(&policy->kobj_unregister);
125667d874c3SViresh Kumar 	INIT_WORK(&policy->update, handle_update);
125767d874c3SViresh Kumar 
125867d874c3SViresh Kumar 	policy->cpu = cpu;
125967d874c3SViresh Kumar 	return policy;
126067d874c3SViresh Kumar 
126167d874c3SViresh Kumar err_min_qos_notifier:
12623000ce3cSRafael J. Wysocki 	freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
12633000ce3cSRafael J. Wysocki 				 &policy->nb_min);
126467d874c3SViresh Kumar err_kobj_remove:
126567d874c3SViresh Kumar 	cpufreq_policy_put_kobj(policy);
126667d874c3SViresh Kumar err_free_real_cpus:
126767d874c3SViresh Kumar 	free_cpumask_var(policy->real_cpus);
126867d874c3SViresh Kumar err_free_rcpumask:
126967d874c3SViresh Kumar 	free_cpumask_var(policy->related_cpus);
127067d874c3SViresh Kumar err_free_cpumask:
127167d874c3SViresh Kumar 	free_cpumask_var(policy->cpus);
127267d874c3SViresh Kumar err_free_policy:
127367d874c3SViresh Kumar 	kfree(policy);
127467d874c3SViresh Kumar 
127567d874c3SViresh Kumar 	return NULL;
127667d874c3SViresh Kumar }
127767d874c3SViresh Kumar 
1278f9f41e3eSViresh Kumar static void cpufreq_policy_free(struct cpufreq_policy *policy)
1279e9698cc5SSrivatsa S. Bhat {
1280988bed09SViresh Kumar 	unsigned long flags;
1281988bed09SViresh Kumar 	int cpu;
1282988bed09SViresh Kumar 
1283988bed09SViresh Kumar 	/* Remove policy from list */
1284988bed09SViresh Kumar 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1285988bed09SViresh Kumar 	list_del(&policy->policy_list);
1286988bed09SViresh Kumar 
1287988bed09SViresh Kumar 	for_each_cpu(cpu, policy->related_cpus)
1288988bed09SViresh Kumar 		per_cpu(cpufreq_cpu_data, cpu) = NULL;
1289988bed09SViresh Kumar 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1290988bed09SViresh Kumar 
12913000ce3cSRafael J. Wysocki 	freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX,
12923000ce3cSRafael J. Wysocki 				 &policy->nb_max);
12933000ce3cSRafael J. Wysocki 	freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
12943000ce3cSRafael J. Wysocki 				 &policy->nb_min);
12956a149036SViresh Kumar 
12966941051dSSudeep Holla 	/* Cancel any pending policy->update work before freeing the policy. */
12976941051dSSudeep Holla 	cancel_work_sync(&policy->update);
12986a149036SViresh Kumar 
12996a149036SViresh Kumar 	if (policy->max_freq_req) {
13006a149036SViresh Kumar 		/*
13016a149036SViresh Kumar 		 * CPUFREQ_CREATE_POLICY notification is sent only after
13026a149036SViresh Kumar 		 * successfully adding max_freq_req request.
13036a149036SViresh Kumar 		 */
13046a149036SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
13056a149036SViresh Kumar 					     CPUFREQ_REMOVE_POLICY, policy);
13063000ce3cSRafael J. Wysocki 		freq_qos_remove_request(policy->max_freq_req);
13076a149036SViresh Kumar 	}
13086a149036SViresh Kumar 
13093000ce3cSRafael J. Wysocki 	freq_qos_remove_request(policy->min_freq_req);
131018c49926SViresh Kumar 	kfree(policy->min_freq_req);
131167d874c3SViresh Kumar 
1312f9f41e3eSViresh Kumar 	cpufreq_policy_put_kobj(policy);
1313559ed407SRafael J. Wysocki 	free_cpumask_var(policy->real_cpus);
1314e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
1315e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1316e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1317e9698cc5SSrivatsa S. Bhat }
1318e9698cc5SSrivatsa S. Bhat 
13190b275352SRafael J. Wysocki static int cpufreq_online(unsigned int cpu)
13201da177e4SLinus Torvalds {
13217f0c020aSViresh Kumar 	struct cpufreq_policy *policy;
1322194d99c7SRafael J. Wysocki 	bool new_policy;
13231da177e4SLinus Torvalds 	unsigned long flags;
13240b275352SRafael J. Wysocki 	unsigned int j;
13250b275352SRafael J. Wysocki 	int ret;
1326c32b6b8eSAshok Raj 
13270b275352SRafael J. Wysocki 	pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
13286eed9404SViresh Kumar 
1329bb29ae15SViresh Kumar 	/* Check if this CPU already has a policy to manage it */
13309104bb26SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
133111ce707eSRafael J. Wysocki 	if (policy) {
13329104bb26SViresh Kumar 		WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
133311ce707eSRafael J. Wysocki 		if (!policy_is_inactive(policy))
1334d9612a49SRafael J. Wysocki 			return cpufreq_add_policy_cpu(policy, cpu);
13351da177e4SLinus Torvalds 
133611ce707eSRafael J. Wysocki 		/* This is the only online CPU for the policy.  Start over. */
1337194d99c7SRafael J. Wysocki 		new_policy = false;
133811ce707eSRafael J. Wysocki 		down_write(&policy->rwsem);
133911ce707eSRafael J. Wysocki 		policy->cpu = cpu;
134011ce707eSRafael J. Wysocki 		policy->governor = NULL;
134111ce707eSRafael J. Wysocki 		up_write(&policy->rwsem);
134211ce707eSRafael J. Wysocki 	} else {
1343194d99c7SRafael J. Wysocki 		new_policy = true;
1344a34e63b1SRafael J. Wysocki 		policy = cpufreq_policy_alloc(cpu);
1345059019a3SDave Jones 		if (!policy)
1346d4d854d6SRafael J. Wysocki 			return -ENOMEM;
134772368d12SRafael J. Wysocki 	}
13480d66b91eSSrivatsa S. Bhat 
134991a12e91SViresh Kumar 	if (!new_policy && cpufreq_driver->online) {
135091a12e91SViresh Kumar 		ret = cpufreq_driver->online(policy);
135191a12e91SViresh Kumar 		if (ret) {
135291a12e91SViresh Kumar 			pr_debug("%s: %d: initialization failed\n", __func__,
135391a12e91SViresh Kumar 				 __LINE__);
135491a12e91SViresh Kumar 			goto out_exit_policy;
135591a12e91SViresh Kumar 		}
135691a12e91SViresh Kumar 
135791a12e91SViresh Kumar 		/* Recover policy->cpus using related_cpus */
135891a12e91SViresh Kumar 		cpumask_copy(policy->cpus, policy->related_cpus);
135991a12e91SViresh Kumar 	} else {
1360835481d9SRusty Russell 		cpumask_copy(policy->cpus, cpumask_of(cpu));
13611da177e4SLinus Torvalds 
136291a12e91SViresh Kumar 		/*
136391a12e91SViresh Kumar 		 * Call driver. From then on the cpufreq must be able
136491a12e91SViresh Kumar 		 * to accept all calls to ->verify and ->setpolicy for this CPU.
13651da177e4SLinus Torvalds 		 */
13661c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->init(policy);
13671da177e4SLinus Torvalds 		if (ret) {
136891a12e91SViresh Kumar 			pr_debug("%s: %d: initialization failed\n", __func__,
136991a12e91SViresh Kumar 				 __LINE__);
13708101f997SViresh Kumar 			goto out_free_policy;
13711da177e4SLinus Torvalds 		}
1372643ae6e8SViresh Kumar 
1373d417e069SViresh Kumar 		ret = cpufreq_table_validate_and_sort(policy);
1374d417e069SViresh Kumar 		if (ret)
1375d417e069SViresh Kumar 			goto out_exit_policy;
1376d417e069SViresh Kumar 
13774d1f3a5bSRafael J. Wysocki 		/* related_cpus should at least include policy->cpus. */
13780998a03aSViresh Kumar 		cpumask_copy(policy->related_cpus, policy->cpus);
13794d1f3a5bSRafael J. Wysocki 	}
1380559ed407SRafael J. Wysocki 
138191a12e91SViresh Kumar 	down_write(&policy->rwsem);
13825a7e56a5SViresh Kumar 	/*
13835a7e56a5SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
13845a7e56a5SViresh Kumar 	 * managing offline cpus here.
13855a7e56a5SViresh Kumar 	 */
13865a7e56a5SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
13875a7e56a5SViresh Kumar 
1388194d99c7SRafael J. Wysocki 	if (new_policy) {
13892f0ba790SRafael J. Wysocki 		for_each_cpu(j, policy->related_cpus) {
1390652ed95dSViresh Kumar 			per_cpu(cpufreq_cpu_data, j) = policy;
13912f0ba790SRafael J. Wysocki 			add_cpu_dev_symlink(policy, j);
13922f0ba790SRafael J. Wysocki 		}
139318c49926SViresh Kumar 
139418c49926SViresh Kumar 		policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req),
139518c49926SViresh Kumar 					       GFP_KERNEL);
1396b96f0384SWang ShaoBo 		if (!policy->min_freq_req) {
1397b96f0384SWang ShaoBo 			ret = -ENOMEM;
139818c49926SViresh Kumar 			goto out_destroy_policy;
1399b96f0384SWang ShaoBo 		}
140018c49926SViresh Kumar 
14013000ce3cSRafael J. Wysocki 		ret = freq_qos_add_request(&policy->constraints,
14023000ce3cSRafael J. Wysocki 					   policy->min_freq_req, FREQ_QOS_MIN,
140318c49926SViresh Kumar 					   policy->min);
140418c49926SViresh Kumar 		if (ret < 0) {
140518c49926SViresh Kumar 			/*
14063000ce3cSRafael J. Wysocki 			 * So we don't call freq_qos_remove_request() for an
140718c49926SViresh Kumar 			 * uninitialized request.
140818c49926SViresh Kumar 			 */
140918c49926SViresh Kumar 			kfree(policy->min_freq_req);
141018c49926SViresh Kumar 			policy->min_freq_req = NULL;
141118c49926SViresh Kumar 			goto out_destroy_policy;
141218c49926SViresh Kumar 		}
141318c49926SViresh Kumar 
141418c49926SViresh Kumar 		/*
141518c49926SViresh Kumar 		 * This must be initialized right here to avoid calling
14163000ce3cSRafael J. Wysocki 		 * freq_qos_remove_request() on uninitialized request in case
141718c49926SViresh Kumar 		 * of errors.
141818c49926SViresh Kumar 		 */
141918c49926SViresh Kumar 		policy->max_freq_req = policy->min_freq_req + 1;
142018c49926SViresh Kumar 
14213000ce3cSRafael J. Wysocki 		ret = freq_qos_add_request(&policy->constraints,
14223000ce3cSRafael J. Wysocki 					   policy->max_freq_req, FREQ_QOS_MAX,
142318c49926SViresh Kumar 					   policy->max);
142418c49926SViresh Kumar 		if (ret < 0) {
142518c49926SViresh Kumar 			policy->max_freq_req = NULL;
142618c49926SViresh Kumar 			goto out_destroy_policy;
142718c49926SViresh Kumar 		}
14286a149036SViresh Kumar 
14296a149036SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
14306a149036SViresh Kumar 				CPUFREQ_CREATE_POLICY, policy);
1431988bed09SViresh Kumar 	}
1432652ed95dSViresh Kumar 
14335ddc6d4eSViresh Kumar 	if (cpufreq_driver->get && has_target()) {
1434da60ce9fSViresh Kumar 		policy->cur = cpufreq_driver->get(policy->cpu);
1435da60ce9fSViresh Kumar 		if (!policy->cur) {
1436b96f0384SWang ShaoBo 			ret = -EIO;
1437da60ce9fSViresh Kumar 			pr_err("%s: ->get() failed\n", __func__);
1438d417e069SViresh Kumar 			goto out_destroy_policy;
1439da60ce9fSViresh Kumar 		}
1440da60ce9fSViresh Kumar 	}
1441da60ce9fSViresh Kumar 
1442d3916691SViresh Kumar 	/*
1443d3916691SViresh Kumar 	 * Sometimes boot loaders set CPU frequency to a value outside of
1444d3916691SViresh Kumar 	 * frequency table present with cpufreq core. In such cases CPU might be
1445d3916691SViresh Kumar 	 * unstable if it has to run on that frequency for long duration of time
1446d3916691SViresh Kumar 	 * and so its better to set it to a frequency which is specified in
1447d3916691SViresh Kumar 	 * freq-table. This also makes cpufreq stats inconsistent as
1448d3916691SViresh Kumar 	 * cpufreq-stats would fail to register because current frequency of CPU
1449d3916691SViresh Kumar 	 * isn't found in freq-table.
1450d3916691SViresh Kumar 	 *
1451d3916691SViresh Kumar 	 * Because we don't want this change to effect boot process badly, we go
1452d3916691SViresh Kumar 	 * for the next freq which is >= policy->cur ('cur' must be set by now,
1453d3916691SViresh Kumar 	 * otherwise we will end up setting freq to lowest of the table as 'cur'
1454d3916691SViresh Kumar 	 * is initialized to zero).
1455d3916691SViresh Kumar 	 *
1456d3916691SViresh Kumar 	 * We are passing target-freq as "policy->cur - 1" otherwise
1457d3916691SViresh Kumar 	 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1458d3916691SViresh Kumar 	 * equal to target-freq.
1459d3916691SViresh Kumar 	 */
1460d3916691SViresh Kumar 	if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1461d3916691SViresh Kumar 	    && has_target()) {
146297148d0aSViresh Kumar 		unsigned int old_freq = policy->cur;
146397148d0aSViresh Kumar 
1464d3916691SViresh Kumar 		/* Are we running at unknown frequency ? */
146597148d0aSViresh Kumar 		ret = cpufreq_frequency_table_get_index(policy, old_freq);
1466d3916691SViresh Kumar 		if (ret == -EINVAL) {
146797148d0aSViresh Kumar 			ret = __cpufreq_driver_target(policy, old_freq - 1,
1468d3916691SViresh Kumar 						      CPUFREQ_RELATION_L);
1469d3916691SViresh Kumar 
1470d3916691SViresh Kumar 			/*
1471d3916691SViresh Kumar 			 * Reaching here after boot in a few seconds may not
1472d3916691SViresh Kumar 			 * mean that system will remain stable at "unknown"
1473d3916691SViresh Kumar 			 * frequency for longer duration. Hence, a BUG_ON().
1474d3916691SViresh Kumar 			 */
1475d3916691SViresh Kumar 			BUG_ON(ret);
147697148d0aSViresh Kumar 			pr_info("%s: CPU%d: Running at unlisted initial frequency: %u KHz, changing to: %u KHz\n",
147797148d0aSViresh Kumar 				__func__, policy->cpu, old_freq, policy->cur);
1478d3916691SViresh Kumar 		}
1479d3916691SViresh Kumar 	}
1480d3916691SViresh Kumar 
1481194d99c7SRafael J. Wysocki 	if (new_policy) {
1482d9612a49SRafael J. Wysocki 		ret = cpufreq_add_dev_interface(policy);
148319d6f7ecSDave Jones 		if (ret)
1484d417e069SViresh Kumar 			goto out_destroy_policy;
14851aefc75bSRafael J. Wysocki 
14861aefc75bSRafael J. Wysocki 		cpufreq_stats_create_table(policy);
1487c88a1f8bSLukasz Majewski 
1488c88a1f8bSLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
1489c88a1f8bSLukasz Majewski 		list_add(&policy->policy_list, &cpufreq_policy_list);
1490c88a1f8bSLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1491988bed09SViresh Kumar 	}
14928ff69732SDave Jones 
14937f0fa40fSViresh Kumar 	ret = cpufreq_init_policy(policy);
14947f0fa40fSViresh Kumar 	if (ret) {
14957f0fa40fSViresh Kumar 		pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
14967f0fa40fSViresh Kumar 		       __func__, cpu, ret);
1497d417e069SViresh Kumar 		goto out_destroy_policy;
149808fd8c1cSViresh Kumar 	}
1499e18f1682SSrivatsa S. Bhat 
15004e97b631SViresh Kumar 	up_write(&policy->rwsem);
150108fd8c1cSViresh Kumar 
1502038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
15037c45cf31SViresh Kumar 
15047c45cf31SViresh Kumar 	/* Callback for handling stuff after policy is ready */
15057c45cf31SViresh Kumar 	if (cpufreq_driver->ready)
15067c45cf31SViresh Kumar 		cpufreq_driver->ready(policy);
15077c45cf31SViresh Kumar 
1508bcc61569SDaniel Lezcano 	if (cpufreq_thermal_control_enabled(cpufreq_driver))
15095c238a8bSAmit Kucheria 		policy->cdev = of_cpufreq_cooling_register(policy);
15105c238a8bSAmit Kucheria 
15112d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 	return 0;
15141da177e4SLinus Torvalds 
1515d417e069SViresh Kumar out_destroy_policy:
1516b24b6478SViresh Kumar 	for_each_cpu(j, policy->real_cpus)
1517b24b6478SViresh Kumar 		remove_cpu_dev_symlink(policy, get_cpu_device(j));
1518b24b6478SViresh Kumar 
15197106e02bSPrarit Bhargava 	up_write(&policy->rwsem);
15207106e02bSPrarit Bhargava 
1521d417e069SViresh Kumar out_exit_policy:
1522da60ce9fSViresh Kumar 	if (cpufreq_driver->exit)
1523da60ce9fSViresh Kumar 		cpufreq_driver->exit(policy);
15242f0ba790SRafael J. Wysocki 
15258101f997SViresh Kumar out_free_policy:
1526f9f41e3eSViresh Kumar 	cpufreq_policy_free(policy);
15271da177e4SLinus Torvalds 	return ret;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds 
15300b275352SRafael J. Wysocki /**
15310b275352SRafael J. Wysocki  * cpufreq_add_dev - the cpufreq interface for a CPU device.
15320b275352SRafael J. Wysocki  * @dev: CPU device.
15330b275352SRafael J. Wysocki  * @sif: Subsystem interface structure pointer (not used)
15340b275352SRafael J. Wysocki  */
15350b275352SRafael J. Wysocki static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
15360b275352SRafael J. Wysocki {
1537a794d613SRafael J. Wysocki 	struct cpufreq_policy *policy;
15380b275352SRafael J. Wysocki 	unsigned cpu = dev->id;
153926619804SViresh Kumar 	int ret;
15400b275352SRafael J. Wysocki 
15410b275352SRafael J. Wysocki 	dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
15420b275352SRafael J. Wysocki 
154326619804SViresh Kumar 	if (cpu_online(cpu)) {
154426619804SViresh Kumar 		ret = cpufreq_online(cpu);
154526619804SViresh Kumar 		if (ret)
154626619804SViresh Kumar 			return ret;
154726619804SViresh Kumar 	}
1548a794d613SRafael J. Wysocki 
154926619804SViresh Kumar 	/* Create sysfs link on CPU registration */
1550a794d613SRafael J. Wysocki 	policy = per_cpu(cpufreq_cpu_data, cpu);
15512f0ba790SRafael J. Wysocki 	if (policy)
15522f0ba790SRafael J. Wysocki 		add_cpu_dev_symlink(policy, cpu);
15532f0ba790SRafael J. Wysocki 
1554a794d613SRafael J. Wysocki 	return 0;
15551da177e4SLinus Torvalds }
15561da177e4SLinus Torvalds 
155727622b06SSebastian Andrzej Siewior static int cpufreq_offline(unsigned int cpu)
15581da177e4SLinus Torvalds {
15593a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
156069cee714SViresh Kumar 	int ret;
15611da177e4SLinus Torvalds 
1562b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
15631da177e4SLinus Torvalds 
1564988bed09SViresh Kumar 	policy = cpufreq_cpu_get_raw(cpu);
15653a3e9e06SViresh Kumar 	if (!policy) {
1566b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
156727622b06SSebastian Andrzej Siewior 		return 0;
15681da177e4SLinus Torvalds 	}
15691da177e4SLinus Torvalds 
157049f18560SViresh Kumar 	down_write(&policy->rwsem);
157145482c70SRafael J. Wysocki 	if (has_target())
157245482c70SRafael J. Wysocki 		cpufreq_stop_governor(policy);
15731da177e4SLinus Torvalds 
15749591becbSViresh Kumar 	cpumask_clear_cpu(cpu, policy->cpus);
15754573237bSViresh Kumar 
15769591becbSViresh Kumar 	if (policy_is_inactive(policy)) {
15779591becbSViresh Kumar 		if (has_target())
15784573237bSViresh Kumar 			strncpy(policy->last_governor, policy->governor->name,
15794573237bSViresh Kumar 				CPUFREQ_NAME_LEN);
158069030dd1SSrinivas Pandruvada 		else
158169030dd1SSrinivas Pandruvada 			policy->last_policy = policy->policy;
15829591becbSViresh Kumar 	} else if (cpu == policy->cpu) {
15839591becbSViresh Kumar 		/* Nominate new CPU */
15849591becbSViresh Kumar 		policy->cpu = cpumask_any(policy->cpus);
15859591becbSViresh Kumar 	}
15861da177e4SLinus Torvalds 
15879591becbSViresh Kumar 	/* Start governor again for active policy */
15889591becbSViresh Kumar 	if (!policy_is_inactive(policy)) {
15899591becbSViresh Kumar 		if (has_target()) {
15900a300767SRafael J. Wysocki 			ret = cpufreq_start_governor(policy);
15919591becbSViresh Kumar 			if (ret)
15929591becbSViresh Kumar 				pr_err("%s: Failed to start governor\n", __func__);
15939591becbSViresh Kumar 		}
159469cee714SViresh Kumar 
159549f18560SViresh Kumar 		goto unlock;
159669cee714SViresh Kumar 	}
159769cee714SViresh Kumar 
1598bcc61569SDaniel Lezcano 	if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
15995c238a8bSAmit Kucheria 		cpufreq_cooling_unregister(policy->cdev);
16005c238a8bSAmit Kucheria 		policy->cdev = NULL;
16015c238a8bSAmit Kucheria 	}
16025c238a8bSAmit Kucheria 
160369cee714SViresh Kumar 	if (cpufreq_driver->stop_cpu)
1604367dc4aaSDirk Brandewie 		cpufreq_driver->stop_cpu(policy);
160587549141SViresh Kumar 
160636be3418SRafael J. Wysocki 	if (has_target())
160736be3418SRafael J. Wysocki 		cpufreq_exit_governor(policy);
16082a998599SRafael J. Wysocki 
16098414809cSSrivatsa S. Bhat 	/*
161091a12e91SViresh Kumar 	 * Perform the ->offline() during light-weight tear-down, as
161191a12e91SViresh Kumar 	 * that allows fast recovery when the CPU comes back.
16128414809cSSrivatsa S. Bhat 	 */
161391a12e91SViresh Kumar 	if (cpufreq_driver->offline) {
161491a12e91SViresh Kumar 		cpufreq_driver->offline(policy);
161591a12e91SViresh Kumar 	} else if (cpufreq_driver->exit) {
16163a3e9e06SViresh Kumar 		cpufreq_driver->exit(policy);
161755582bccSSrinivas Pandruvada 		policy->freq_table = NULL;
161855582bccSSrinivas Pandruvada 	}
161949f18560SViresh Kumar 
162049f18560SViresh Kumar unlock:
162149f18560SViresh Kumar 	up_write(&policy->rwsem);
162227622b06SSebastian Andrzej Siewior 	return 0;
16231da177e4SLinus Torvalds }
16241da177e4SLinus Torvalds 
1625a9909c21SLee Jones /*
162627a862e9SViresh Kumar  * cpufreq_remove_dev - remove a CPU device
1627cedb70afSSrivatsa S. Bhat  *
1628cedb70afSSrivatsa S. Bhat  * Removes the cpufreq interface for a CPU device.
1629cedb70afSSrivatsa S. Bhat  */
163071db87baSViresh Kumar static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
16315a01f2e8SVenkatesh Pallipadi {
16328a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
163387549141SViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
163487549141SViresh Kumar 
163587549141SViresh Kumar 	if (!policy)
163671db87baSViresh Kumar 		return;
1637ec28297aSVenki Pallipadi 
163869cee714SViresh Kumar 	if (cpu_online(cpu))
163969cee714SViresh Kumar 		cpufreq_offline(cpu);
164087549141SViresh Kumar 
1641559ed407SRafael J. Wysocki 	cpumask_clear_cpu(cpu, policy->real_cpus);
164226619804SViresh Kumar 	remove_cpu_dev_symlink(policy, dev);
1643f344dae0SViresh Kumar 
164491a12e91SViresh Kumar 	if (cpumask_empty(policy->real_cpus)) {
164591a12e91SViresh Kumar 		/* We did light-weight exit earlier, do full tear down now */
164691a12e91SViresh Kumar 		if (cpufreq_driver->offline)
164791a12e91SViresh Kumar 			cpufreq_driver->exit(policy);
164891a12e91SViresh Kumar 
1649f9f41e3eSViresh Kumar 		cpufreq_policy_free(policy);
16505a01f2e8SVenkatesh Pallipadi 	}
165191a12e91SViresh Kumar }
16525a01f2e8SVenkatesh Pallipadi 
16531da177e4SLinus Torvalds /**
1654ec06e586SRafael J. Wysocki  * cpufreq_out_of_sync - Fix up actual and saved CPU frequency difference.
1655ec06e586SRafael J. Wysocki  * @policy: Policy managing CPUs.
1656ec06e586SRafael J. Wysocki  * @new_freq: New CPU frequency.
16571da177e4SLinus Torvalds  *
1658ec06e586SRafael J. Wysocki  * Adjust to the current frequency first and clean up later by either calling
1659ec06e586SRafael J. Wysocki  * cpufreq_update_policy(), or scheduling handle_update().
16601da177e4SLinus Torvalds  */
1661a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1662e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
16631da177e4SLinus Torvalds {
16641da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1665b43a7ffbSViresh Kumar 
1666e837f9b5SJoe Perches 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1667a1e1dc41SViresh Kumar 		 policy->cur, new_freq);
16681da177e4SLinus Torvalds 
1669a1e1dc41SViresh Kumar 	freqs.old = policy->cur;
16701da177e4SLinus Torvalds 	freqs.new = new_freq;
1671b43a7ffbSViresh Kumar 
16728fec051eSViresh Kumar 	cpufreq_freq_transition_begin(policy, &freqs);
16738fec051eSViresh Kumar 	cpufreq_freq_transition_end(policy, &freqs, 0);
16741da177e4SLinus Torvalds }
16751da177e4SLinus Torvalds 
16765980752eSViresh Kumar static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update)
16775980752eSViresh Kumar {
16785980752eSViresh Kumar 	unsigned int new_freq;
16795980752eSViresh Kumar 
16805980752eSViresh Kumar 	new_freq = cpufreq_driver->get(policy->cpu);
16815980752eSViresh Kumar 	if (!new_freq)
16825980752eSViresh Kumar 		return 0;
16835980752eSViresh Kumar 
16845980752eSViresh Kumar 	/*
16855980752eSViresh Kumar 	 * If fast frequency switching is used with the given policy, the check
16865980752eSViresh Kumar 	 * against policy->cur is pointless, so skip it in that case.
16875980752eSViresh Kumar 	 */
16885980752eSViresh Kumar 	if (policy->fast_switch_enabled || !has_target())
16895980752eSViresh Kumar 		return new_freq;
16905980752eSViresh Kumar 
16915980752eSViresh Kumar 	if (policy->cur != new_freq) {
16925980752eSViresh Kumar 		cpufreq_out_of_sync(policy, new_freq);
16935980752eSViresh Kumar 		if (update)
16945980752eSViresh Kumar 			schedule_work(&policy->update);
16955980752eSViresh Kumar 	}
16965980752eSViresh Kumar 
16975980752eSViresh Kumar 	return new_freq;
16985980752eSViresh Kumar }
16995980752eSViresh Kumar 
17001da177e4SLinus Torvalds /**
17014ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
170295235ca2SVenkatesh Pallipadi  * @cpu: CPU number
170395235ca2SVenkatesh Pallipadi  *
170495235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
170595235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
170695235ca2SVenkatesh Pallipadi  */
170795235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
170895235ca2SVenkatesh Pallipadi {
17099e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1710e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
1711c75361c0SRichard Cochran 	unsigned long flags;
171295235ca2SVenkatesh Pallipadi 
1713c75361c0SRichard Cochran 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1714c75361c0SRichard Cochran 
1715c75361c0SRichard Cochran 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1716c75361c0SRichard Cochran 		ret_freq = cpufreq_driver->get(cpu);
1717c75361c0SRichard Cochran 		read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1718c75361c0SRichard Cochran 		return ret_freq;
1719c75361c0SRichard Cochran 	}
1720c75361c0SRichard Cochran 
1721c75361c0SRichard Cochran 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
17229e21ba8bSDirk Brandewie 
17239e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
172495235ca2SVenkatesh Pallipadi 	if (policy) {
1725e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
172695235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
172795235ca2SVenkatesh Pallipadi 	}
172895235ca2SVenkatesh Pallipadi 
17294d34a67dSDave Jones 	return ret_freq;
173095235ca2SVenkatesh Pallipadi }
173195235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
173295235ca2SVenkatesh Pallipadi 
17333d737108SJesse Barnes /**
17343d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
17353d737108SJesse Barnes  * @cpu: CPU number
17363d737108SJesse Barnes  *
17373d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
17383d737108SJesse Barnes  */
17393d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
17403d737108SJesse Barnes {
17413d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
17423d737108SJesse Barnes 	unsigned int ret_freq = 0;
17433d737108SJesse Barnes 
17443d737108SJesse Barnes 	if (policy) {
17453d737108SJesse Barnes 		ret_freq = policy->max;
17463d737108SJesse Barnes 		cpufreq_cpu_put(policy);
17473d737108SJesse Barnes 	}
17483d737108SJesse Barnes 
17493d737108SJesse Barnes 	return ret_freq;
17503d737108SJesse Barnes }
17513d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
17523d737108SJesse Barnes 
1753bbce8eaaSIonela Voinescu /**
1754bbce8eaaSIonela Voinescu  * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU
1755bbce8eaaSIonela Voinescu  * @cpu: CPU number
1756bbce8eaaSIonela Voinescu  *
1757bbce8eaaSIonela Voinescu  * The default return value is the max_freq field of cpuinfo.
1758bbce8eaaSIonela Voinescu  */
1759bbce8eaaSIonela Voinescu __weak unsigned int cpufreq_get_hw_max_freq(unsigned int cpu)
1760bbce8eaaSIonela Voinescu {
1761bbce8eaaSIonela Voinescu 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1762bbce8eaaSIonela Voinescu 	unsigned int ret_freq = 0;
1763bbce8eaaSIonela Voinescu 
1764bbce8eaaSIonela Voinescu 	if (policy) {
1765bbce8eaaSIonela Voinescu 		ret_freq = policy->cpuinfo.max_freq;
1766bbce8eaaSIonela Voinescu 		cpufreq_cpu_put(policy);
1767bbce8eaaSIonela Voinescu 	}
1768bbce8eaaSIonela Voinescu 
1769bbce8eaaSIonela Voinescu 	return ret_freq;
1770bbce8eaaSIonela Voinescu }
1771bbce8eaaSIonela Voinescu EXPORT_SYMBOL(cpufreq_get_hw_max_freq);
1772bbce8eaaSIonela Voinescu 
1773d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
17741da177e4SLinus Torvalds {
17754db7c34cSYue Hu 	if (unlikely(policy_is_inactive(policy)))
17765980752eSViresh Kumar 		return 0;
17771da177e4SLinus Torvalds 
17785980752eSViresh Kumar 	return cpufreq_verify_current_freq(policy, true);
17795a01f2e8SVenkatesh Pallipadi }
17801da177e4SLinus Torvalds 
17815a01f2e8SVenkatesh Pallipadi /**
17825a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
17835a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
17845a01f2e8SVenkatesh Pallipadi  *
17855a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
17865a01f2e8SVenkatesh Pallipadi  */
17875a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
17885a01f2e8SVenkatesh Pallipadi {
1789999976e0SAaron Plattner 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
17905a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
17915a01f2e8SVenkatesh Pallipadi 
1792999976e0SAaron Plattner 	if (policy) {
1793ad7722daSviresh kumar 		down_read(&policy->rwsem);
17944db7c34cSYue Hu 		if (cpufreq_driver->get)
1795d92d50a4SViresh Kumar 			ret_freq = __cpufreq_get(policy);
1796ad7722daSviresh kumar 		up_read(&policy->rwsem);
1797999976e0SAaron Plattner 
1798999976e0SAaron Plattner 		cpufreq_cpu_put(policy);
1799999976e0SAaron Plattner 	}
18006eed9404SViresh Kumar 
18014d34a67dSDave Jones 	return ret_freq;
18021da177e4SLinus Torvalds }
18031da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
18041da177e4SLinus Torvalds 
18058a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
18068a25a2fdSKay Sievers 	.name		= "cpufreq",
18078a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
18088a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
18098a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1810e00e56dfSRafael J. Wysocki };
1811e00e56dfSRafael J. Wysocki 
1812e28867eaSViresh Kumar /*
1813e28867eaSViresh Kumar  * In case platform wants some specific frequency to be configured
1814e28867eaSViresh Kumar  * during suspend..
181542d4dc3fSBenjamin Herrenschmidt  */
1816e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy)
181742d4dc3fSBenjamin Herrenschmidt {
1818e28867eaSViresh Kumar 	int ret;
18194bc5d341SDave Jones 
1820e28867eaSViresh Kumar 	if (!policy->suspend_freq) {
1821201f3716SBartlomiej Zolnierkiewicz 		pr_debug("%s: suspend_freq not defined\n", __func__);
1822201f3716SBartlomiej Zolnierkiewicz 		return 0;
182342d4dc3fSBenjamin Herrenschmidt 	}
182442d4dc3fSBenjamin Herrenschmidt 
1825e28867eaSViresh Kumar 	pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1826e28867eaSViresh Kumar 			policy->suspend_freq);
1827e28867eaSViresh Kumar 
1828e28867eaSViresh Kumar 	ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1829e28867eaSViresh Kumar 			CPUFREQ_RELATION_H);
1830e28867eaSViresh Kumar 	if (ret)
1831e28867eaSViresh Kumar 		pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1832e28867eaSViresh Kumar 				__func__, policy->suspend_freq, ret);
1833e28867eaSViresh Kumar 
1834c9060494SDave Jones 	return ret;
183542d4dc3fSBenjamin Herrenschmidt }
1836e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend);
183742d4dc3fSBenjamin Herrenschmidt 
183842d4dc3fSBenjamin Herrenschmidt /**
1839ec06e586SRafael J. Wysocki  * cpufreq_suspend() - Suspend CPUFreq governors.
18401da177e4SLinus Torvalds  *
18412f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycles for suspending governors
18422f0aea93SViresh Kumar  * as some platforms can't change frequency after this point in suspend cycle.
18432f0aea93SViresh Kumar  * Because some of the devices (like: i2c, regulators, etc) they use for
18442f0aea93SViresh Kumar  * changing frequency are suspended quickly after this point.
18451da177e4SLinus Torvalds  */
18462f0aea93SViresh Kumar void cpufreq_suspend(void)
18471da177e4SLinus Torvalds {
18483a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
18491da177e4SLinus Torvalds 
18502f0aea93SViresh Kumar 	if (!cpufreq_driver)
1851e00e56dfSRafael J. Wysocki 		return;
18521da177e4SLinus Torvalds 
1853ba41e1bcSRafael J. Wysocki 	if (!has_target() && !cpufreq_driver->suspend)
1854b1b12babSViresh Kumar 		goto suspend;
18551da177e4SLinus Torvalds 
18562f0aea93SViresh Kumar 	pr_debug("%s: Suspending Governors\n", __func__);
18572f0aea93SViresh Kumar 
1858f963735aSViresh Kumar 	for_each_active_policy(policy) {
1859ba41e1bcSRafael J. Wysocki 		if (has_target()) {
186049f18560SViresh Kumar 			down_write(&policy->rwsem);
186145482c70SRafael J. Wysocki 			cpufreq_stop_governor(policy);
186249f18560SViresh Kumar 			up_write(&policy->rwsem);
1863ba41e1bcSRafael J. Wysocki 		}
1864ba41e1bcSRafael J. Wysocki 
1865ba41e1bcSRafael J. Wysocki 		if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
1866e9a7cc1dSFlorian Fainelli 			pr_err("%s: Failed to suspend driver: %s\n", __func__,
1867e9a7cc1dSFlorian Fainelli 				cpufreq_driver->name);
18681da177e4SLinus Torvalds 	}
1869b1b12babSViresh Kumar 
1870b1b12babSViresh Kumar suspend:
1871b1b12babSViresh Kumar 	cpufreq_suspended = true;
18721da177e4SLinus Torvalds }
18731da177e4SLinus Torvalds 
18741da177e4SLinus Torvalds /**
1875ec06e586SRafael J. Wysocki  * cpufreq_resume() - Resume CPUFreq governors.
18761da177e4SLinus Torvalds  *
18772f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycle for resuming governors that
18782f0aea93SViresh Kumar  * are suspended with cpufreq_suspend().
18791da177e4SLinus Torvalds  */
18802f0aea93SViresh Kumar void cpufreq_resume(void)
18811da177e4SLinus Torvalds {
18821da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
188349f18560SViresh Kumar 	int ret;
18841da177e4SLinus Torvalds 
18852f0aea93SViresh Kumar 	if (!cpufreq_driver)
18861da177e4SLinus Torvalds 		return;
18871da177e4SLinus Torvalds 
1888703cbaa6SBo Yan 	if (unlikely(!cpufreq_suspended))
1889703cbaa6SBo Yan 		return;
1890703cbaa6SBo Yan 
18918e30444eSLan Tianyu 	cpufreq_suspended = false;
18928e30444eSLan Tianyu 
1893ba41e1bcSRafael J. Wysocki 	if (!has_target() && !cpufreq_driver->resume)
18942f0aea93SViresh Kumar 		return;
18951da177e4SLinus Torvalds 
18962f0aea93SViresh Kumar 	pr_debug("%s: Resuming Governors\n", __func__);
18972f0aea93SViresh Kumar 
1898f963735aSViresh Kumar 	for_each_active_policy(policy) {
189949f18560SViresh Kumar 		if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
19000c5aa405SViresh Kumar 			pr_err("%s: Failed to resume driver: %p\n", __func__,
19010c5aa405SViresh Kumar 				policy);
1902ba41e1bcSRafael J. Wysocki 		} else if (has_target()) {
190349f18560SViresh Kumar 			down_write(&policy->rwsem);
19040a300767SRafael J. Wysocki 			ret = cpufreq_start_governor(policy);
190549f18560SViresh Kumar 			up_write(&policy->rwsem);
190649f18560SViresh Kumar 
190749f18560SViresh Kumar 			if (ret)
19082f0aea93SViresh Kumar 				pr_err("%s: Failed to start governor for policy: %p\n",
19092f0aea93SViresh Kumar 				       __func__, policy);
1910c75de0acSViresh Kumar 		}
191149f18560SViresh Kumar 	}
19121da177e4SLinus Torvalds }
19131da177e4SLinus Torvalds 
19149d95046eSBorislav Petkov /**
1915a62f68f5SRafael J. Wysocki  * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones.
1916a62f68f5SRafael J. Wysocki  * @flags: Flags to test against the current cpufreq driver's flags.
1917a62f68f5SRafael J. Wysocki  *
1918a62f68f5SRafael J. Wysocki  * Assumes that the driver is there, so callers must ensure that this is the
1919a62f68f5SRafael J. Wysocki  * case.
1920a62f68f5SRafael J. Wysocki  */
1921a62f68f5SRafael J. Wysocki bool cpufreq_driver_test_flags(u16 flags)
1922a62f68f5SRafael J. Wysocki {
1923a62f68f5SRafael J. Wysocki 	return !!(cpufreq_driver->flags & flags);
1924a62f68f5SRafael J. Wysocki }
1925a62f68f5SRafael J. Wysocki 
1926a62f68f5SRafael J. Wysocki /**
1927ec06e586SRafael J. Wysocki  * cpufreq_get_current_driver - Return the current driver's name.
19289d95046eSBorislav Petkov  *
1929ec06e586SRafael J. Wysocki  * Return the name string of the currently registered cpufreq driver or NULL if
1930ec06e586SRafael J. Wysocki  * none.
19319d95046eSBorislav Petkov  */
19329d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
19339d95046eSBorislav Petkov {
19341c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
19351c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
19361c3d85ddSRafael J. Wysocki 
19371c3d85ddSRafael J. Wysocki 	return NULL;
19389d95046eSBorislav Petkov }
19399d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
19401da177e4SLinus Torvalds 
194151315cdfSThomas Petazzoni /**
1942ec06e586SRafael J. Wysocki  * cpufreq_get_driver_data - Return current driver data.
194351315cdfSThomas Petazzoni  *
1944ec06e586SRafael J. Wysocki  * Return the private data of the currently registered cpufreq driver, or NULL
1945ec06e586SRafael J. Wysocki  * if no cpufreq driver has been registered.
194651315cdfSThomas Petazzoni  */
194751315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void)
194851315cdfSThomas Petazzoni {
194951315cdfSThomas Petazzoni 	if (cpufreq_driver)
195051315cdfSThomas Petazzoni 		return cpufreq_driver->driver_data;
195151315cdfSThomas Petazzoni 
195251315cdfSThomas Petazzoni 	return NULL;
195351315cdfSThomas Petazzoni }
195451315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
195551315cdfSThomas Petazzoni 
19561da177e4SLinus Torvalds /*********************************************************************
19571da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
19581da177e4SLinus Torvalds  *********************************************************************/
19591da177e4SLinus Torvalds 
19601da177e4SLinus Torvalds /**
1961ec06e586SRafael J. Wysocki  * cpufreq_register_notifier - Register a notifier with cpufreq.
1962ec06e586SRafael J. Wysocki  * @nb: notifier function to register.
1963ec06e586SRafael J. Wysocki  * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER.
19641da177e4SLinus Torvalds  *
1965ec06e586SRafael J. Wysocki  * Add a notifier to one of two lists: either a list of notifiers that run on
1966ec06e586SRafael J. Wysocki  * clock rate changes (once before and once after every transition), or a list
1967ec06e586SRafael J. Wysocki  * of notifiers that ron on cpufreq policy changes.
19681da177e4SLinus Torvalds  *
1969ec06e586SRafael J. Wysocki  * This function may sleep and it has the same return values as
1970ec06e586SRafael J. Wysocki  * blocking_notifier_chain_register().
19711da177e4SLinus Torvalds  */
19721da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
19731da177e4SLinus Torvalds {
19741da177e4SLinus Torvalds 	int ret;
19751da177e4SLinus Torvalds 
1976d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1977d5aaffa9SDirk Brandewie 		return -EINVAL;
1978d5aaffa9SDirk Brandewie 
19791da177e4SLinus Torvalds 	switch (list) {
19801da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1981b7898fdaSRafael J. Wysocki 		mutex_lock(&cpufreq_fast_switch_lock);
1982b7898fdaSRafael J. Wysocki 
1983b7898fdaSRafael J. Wysocki 		if (cpufreq_fast_switch_count > 0) {
1984b7898fdaSRafael J. Wysocki 			mutex_unlock(&cpufreq_fast_switch_lock);
1985b7898fdaSRafael J. Wysocki 			return -EBUSY;
1986b7898fdaSRafael J. Wysocki 		}
1987b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1988e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
1989b7898fdaSRafael J. Wysocki 		if (!ret)
1990b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count--;
1991b7898fdaSRafael J. Wysocki 
1992b7898fdaSRafael J. Wysocki 		mutex_unlock(&cpufreq_fast_switch_lock);
19931da177e4SLinus Torvalds 		break;
19941da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1995e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1996e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
19971da177e4SLinus Torvalds 		break;
19981da177e4SLinus Torvalds 	default:
19991da177e4SLinus Torvalds 		ret = -EINVAL;
20001da177e4SLinus Torvalds 	}
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	return ret;
20031da177e4SLinus Torvalds }
20041da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds /**
2007ec06e586SRafael J. Wysocki  * cpufreq_unregister_notifier - Unregister a notifier from cpufreq.
2008ec06e586SRafael J. Wysocki  * @nb: notifier block to be unregistered.
2009ec06e586SRafael J. Wysocki  * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER.
20101da177e4SLinus Torvalds  *
2011ec06e586SRafael J. Wysocki  * Remove a notifier from one of the cpufreq notifier lists.
20121da177e4SLinus Torvalds  *
2013ec06e586SRafael J. Wysocki  * This function may sleep and it has the same return values as
2014ec06e586SRafael J. Wysocki  * blocking_notifier_chain_unregister().
20151da177e4SLinus Torvalds  */
20161da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
20171da177e4SLinus Torvalds {
20181da177e4SLinus Torvalds 	int ret;
20191da177e4SLinus Torvalds 
2020d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
2021d5aaffa9SDirk Brandewie 		return -EINVAL;
2022d5aaffa9SDirk Brandewie 
20231da177e4SLinus Torvalds 	switch (list) {
20241da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
2025b7898fdaSRafael J. Wysocki 		mutex_lock(&cpufreq_fast_switch_lock);
2026b7898fdaSRafael J. Wysocki 
2027b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
2028e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
2029b7898fdaSRafael J. Wysocki 		if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
2030b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count++;
2031b7898fdaSRafael J. Wysocki 
2032b7898fdaSRafael J. Wysocki 		mutex_unlock(&cpufreq_fast_switch_lock);
20331da177e4SLinus Torvalds 		break;
20341da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
2035e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
2036e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
20371da177e4SLinus Torvalds 		break;
20381da177e4SLinus Torvalds 	default:
20391da177e4SLinus Torvalds 		ret = -EINVAL;
20401da177e4SLinus Torvalds 	}
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds 	return ret;
20431da177e4SLinus Torvalds }
20441da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 
20471da177e4SLinus Torvalds /*********************************************************************
20481da177e4SLinus Torvalds  *                              GOVERNORS                            *
20491da177e4SLinus Torvalds  *********************************************************************/
20501da177e4SLinus Torvalds 
2051b7898fdaSRafael J. Wysocki /**
2052b7898fdaSRafael J. Wysocki  * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
2053b7898fdaSRafael J. Wysocki  * @policy: cpufreq policy to switch the frequency for.
2054b7898fdaSRafael J. Wysocki  * @target_freq: New frequency to set (may be approximate).
2055b7898fdaSRafael J. Wysocki  *
2056b7898fdaSRafael J. Wysocki  * Carry out a fast frequency switch without sleeping.
2057b7898fdaSRafael J. Wysocki  *
2058b7898fdaSRafael J. Wysocki  * The driver's ->fast_switch() callback invoked by this function must be
2059b7898fdaSRafael J. Wysocki  * suitable for being called from within RCU-sched read-side critical sections
2060b7898fdaSRafael J. Wysocki  * and it is expected to select the minimum available frequency greater than or
2061b7898fdaSRafael J. Wysocki  * equal to @target_freq (CPUFREQ_RELATION_L).
2062b7898fdaSRafael J. Wysocki  *
2063b7898fdaSRafael J. Wysocki  * This function must not be called if policy->fast_switch_enabled is unset.
2064b7898fdaSRafael J. Wysocki  *
2065b7898fdaSRafael J. Wysocki  * Governors calling this function must guarantee that it will never be invoked
2066b7898fdaSRafael J. Wysocki  * twice in parallel for the same policy and that it will never be called in
2067b7898fdaSRafael J. Wysocki  * parallel with either ->target() or ->target_index() for the same policy.
2068b7898fdaSRafael J. Wysocki  *
2069209887e6SViresh Kumar  * Returns the actual frequency set for the CPU.
2070209887e6SViresh Kumar  *
2071209887e6SViresh Kumar  * If 0 is returned by the driver's ->fast_switch() callback to indicate an
2072209887e6SViresh Kumar  * error condition, the hardware configuration must be preserved.
2073b7898fdaSRafael J. Wysocki  */
2074b7898fdaSRafael J. Wysocki unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
2075b7898fdaSRafael J. Wysocki 					unsigned int target_freq)
2076b7898fdaSRafael J. Wysocki {
20771a0419b0SIonela Voinescu 	unsigned int freq;
207808d8c65eSViresh Kumar 	int cpu;
2079b7898fdaSRafael J. Wysocki 
20801a0419b0SIonela Voinescu 	target_freq = clamp_val(target_freq, policy->min, policy->max);
20811a0419b0SIonela Voinescu 	freq = cpufreq_driver->fast_switch(policy, target_freq);
20821a0419b0SIonela Voinescu 
208396f60cddSViresh Kumar 	if (!freq)
208496f60cddSViresh Kumar 		return 0;
208596f60cddSViresh Kumar 
208608d8c65eSViresh Kumar 	policy->cur = freq;
20871a0419b0SIonela Voinescu 	arch_set_freq_scale(policy->related_cpus, freq,
20881a0419b0SIonela Voinescu 			    policy->cpuinfo.max_freq);
208996f60cddSViresh Kumar 	cpufreq_stats_record_transition(policy, freq);
20901a0419b0SIonela Voinescu 
209108d8c65eSViresh Kumar 	if (trace_cpu_frequency_enabled()) {
209208d8c65eSViresh Kumar 		for_each_cpu(cpu, policy->cpus)
209308d8c65eSViresh Kumar 			trace_cpu_frequency(freq, cpu);
209408d8c65eSViresh Kumar 	}
209508d8c65eSViresh Kumar 
20961a0419b0SIonela Voinescu 	return freq;
2097b7898fdaSRafael J. Wysocki }
2098b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
2099b7898fdaSRafael J. Wysocki 
2100ee2cc427SRafael J. Wysocki /**
2101ee2cc427SRafael J. Wysocki  * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go.
2102ee2cc427SRafael J. Wysocki  * @cpu: Target CPU.
2103ee2cc427SRafael J. Wysocki  * @min_perf: Minimum (required) performance level (units of @capacity).
2104ee2cc427SRafael J. Wysocki  * @target_perf: Terget (desired) performance level (units of @capacity).
2105ee2cc427SRafael J. Wysocki  * @capacity: Capacity of the target CPU.
2106ee2cc427SRafael J. Wysocki  *
2107ee2cc427SRafael J. Wysocki  * Carry out a fast performance level switch of @cpu without sleeping.
2108ee2cc427SRafael J. Wysocki  *
2109ee2cc427SRafael J. Wysocki  * The driver's ->adjust_perf() callback invoked by this function must be
2110ee2cc427SRafael J. Wysocki  * suitable for being called from within RCU-sched read-side critical sections
2111ee2cc427SRafael J. Wysocki  * and it is expected to select a suitable performance level equal to or above
2112ee2cc427SRafael J. Wysocki  * @min_perf and preferably equal to or below @target_perf.
2113ee2cc427SRafael J. Wysocki  *
2114ee2cc427SRafael J. Wysocki  * This function must not be called if policy->fast_switch_enabled is unset.
2115ee2cc427SRafael J. Wysocki  *
2116ee2cc427SRafael J. Wysocki  * Governors calling this function must guarantee that it will never be invoked
2117ee2cc427SRafael J. Wysocki  * twice in parallel for the same CPU and that it will never be called in
2118ee2cc427SRafael J. Wysocki  * parallel with either ->target() or ->target_index() or ->fast_switch() for
2119ee2cc427SRafael J. Wysocki  * the same CPU.
2120ee2cc427SRafael J. Wysocki  */
2121ee2cc427SRafael J. Wysocki void cpufreq_driver_adjust_perf(unsigned int cpu,
2122ee2cc427SRafael J. Wysocki 				 unsigned long min_perf,
2123ee2cc427SRafael J. Wysocki 				 unsigned long target_perf,
2124ee2cc427SRafael J. Wysocki 				 unsigned long capacity)
2125ee2cc427SRafael J. Wysocki {
2126ee2cc427SRafael J. Wysocki 	cpufreq_driver->adjust_perf(cpu, min_perf, target_perf, capacity);
2127ee2cc427SRafael J. Wysocki }
2128ee2cc427SRafael J. Wysocki 
2129ee2cc427SRafael J. Wysocki /**
2130ee2cc427SRafael J. Wysocki  * cpufreq_driver_has_adjust_perf - Check "direct fast switch" callback.
2131ee2cc427SRafael J. Wysocki  *
2132ee2cc427SRafael J. Wysocki  * Return 'true' if the ->adjust_perf callback is present for the
2133ee2cc427SRafael J. Wysocki  * current driver or 'false' otherwise.
2134ee2cc427SRafael J. Wysocki  */
2135ee2cc427SRafael J. Wysocki bool cpufreq_driver_has_adjust_perf(void)
2136ee2cc427SRafael J. Wysocki {
2137ee2cc427SRafael J. Wysocki 	return !!cpufreq_driver->adjust_perf;
2138ee2cc427SRafael J. Wysocki }
2139ee2cc427SRafael J. Wysocki 
21401c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */
21411c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy,
21421c03a2d0SViresh Kumar 				 struct cpufreq_freqs *freqs, int index)
21431c03a2d0SViresh Kumar {
21441c03a2d0SViresh Kumar 	int ret;
21451c03a2d0SViresh Kumar 
21461c03a2d0SViresh Kumar 	freqs->new = cpufreq_driver->get_intermediate(policy, index);
21471c03a2d0SViresh Kumar 
21481c03a2d0SViresh Kumar 	/* We don't need to switch to intermediate freq */
21491c03a2d0SViresh Kumar 	if (!freqs->new)
21501c03a2d0SViresh Kumar 		return 0;
21511c03a2d0SViresh Kumar 
21521c03a2d0SViresh Kumar 	pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
21531c03a2d0SViresh Kumar 		 __func__, policy->cpu, freqs->old, freqs->new);
21541c03a2d0SViresh Kumar 
21551c03a2d0SViresh Kumar 	cpufreq_freq_transition_begin(policy, freqs);
21561c03a2d0SViresh Kumar 	ret = cpufreq_driver->target_intermediate(policy, index);
21571c03a2d0SViresh Kumar 	cpufreq_freq_transition_end(policy, freqs, ret);
21581c03a2d0SViresh Kumar 
21591c03a2d0SViresh Kumar 	if (ret)
21601c03a2d0SViresh Kumar 		pr_err("%s: Failed to change to intermediate frequency: %d\n",
21611c03a2d0SViresh Kumar 		       __func__, ret);
21621c03a2d0SViresh Kumar 
21631c03a2d0SViresh Kumar 	return ret;
21641c03a2d0SViresh Kumar }
21651c03a2d0SViresh Kumar 
216623727845SViresh Kumar static int __target_index(struct cpufreq_policy *policy, int index)
21678d65775dSViresh Kumar {
21681c03a2d0SViresh Kumar 	struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
216956a7ff75SRafael J. Wysocki 	unsigned int restore_freq, intermediate_freq = 0;
217023727845SViresh Kumar 	unsigned int newfreq = policy->freq_table[index].frequency;
21718d65775dSViresh Kumar 	int retval = -EINVAL;
21728d65775dSViresh Kumar 	bool notify;
21738d65775dSViresh Kumar 
217423727845SViresh Kumar 	if (newfreq == policy->cur)
217523727845SViresh Kumar 		return 0;
217623727845SViresh Kumar 
217756a7ff75SRafael J. Wysocki 	/* Save last value to restore later on errors */
217856a7ff75SRafael J. Wysocki 	restore_freq = policy->cur;
217956a7ff75SRafael J. Wysocki 
21808d65775dSViresh Kumar 	notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
21818d65775dSViresh Kumar 	if (notify) {
21821c03a2d0SViresh Kumar 		/* Handle switching to intermediate frequency */
21831c03a2d0SViresh Kumar 		if (cpufreq_driver->get_intermediate) {
21841c03a2d0SViresh Kumar 			retval = __target_intermediate(policy, &freqs, index);
21851c03a2d0SViresh Kumar 			if (retval)
21861c03a2d0SViresh Kumar 				return retval;
21878d65775dSViresh Kumar 
21881c03a2d0SViresh Kumar 			intermediate_freq = freqs.new;
21891c03a2d0SViresh Kumar 			/* Set old freq to intermediate */
21901c03a2d0SViresh Kumar 			if (intermediate_freq)
21911c03a2d0SViresh Kumar 				freqs.old = freqs.new;
21921c03a2d0SViresh Kumar 		}
21931c03a2d0SViresh Kumar 
219423727845SViresh Kumar 		freqs.new = newfreq;
21958d65775dSViresh Kumar 		pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
21968d65775dSViresh Kumar 			 __func__, policy->cpu, freqs.old, freqs.new);
21978d65775dSViresh Kumar 
21988d65775dSViresh Kumar 		cpufreq_freq_transition_begin(policy, &freqs);
21998d65775dSViresh Kumar 	}
22008d65775dSViresh Kumar 
22018d65775dSViresh Kumar 	retval = cpufreq_driver->target_index(policy, index);
22028d65775dSViresh Kumar 	if (retval)
22038d65775dSViresh Kumar 		pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
22048d65775dSViresh Kumar 		       retval);
22058d65775dSViresh Kumar 
22061c03a2d0SViresh Kumar 	if (notify) {
22078d65775dSViresh Kumar 		cpufreq_freq_transition_end(policy, &freqs, retval);
22088d65775dSViresh Kumar 
22091c03a2d0SViresh Kumar 		/*
22101c03a2d0SViresh Kumar 		 * Failed after setting to intermediate freq? Driver should have
22111c03a2d0SViresh Kumar 		 * reverted back to initial frequency and so should we. Check
22121c03a2d0SViresh Kumar 		 * here for intermediate_freq instead of get_intermediate, in
221358405af6SShailendra Verma 		 * case we haven't switched to intermediate freq at all.
22141c03a2d0SViresh Kumar 		 */
22151c03a2d0SViresh Kumar 		if (unlikely(retval && intermediate_freq)) {
22161c03a2d0SViresh Kumar 			freqs.old = intermediate_freq;
221756a7ff75SRafael J. Wysocki 			freqs.new = restore_freq;
22181c03a2d0SViresh Kumar 			cpufreq_freq_transition_begin(policy, &freqs);
22191c03a2d0SViresh Kumar 			cpufreq_freq_transition_end(policy, &freqs, 0);
22201c03a2d0SViresh Kumar 		}
22211c03a2d0SViresh Kumar 	}
22221c03a2d0SViresh Kumar 
22238d65775dSViresh Kumar 	return retval;
22248d65775dSViresh Kumar }
22258d65775dSViresh Kumar 
22261da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
22271da177e4SLinus Torvalds 			    unsigned int target_freq,
22281da177e4SLinus Torvalds 			    unsigned int relation)
22291da177e4SLinus Torvalds {
22307249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
2231d218ed77SViresh Kumar 	int index;
2232c32b6b8eSAshok Raj 
2233a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2234a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2235a7b422cdSKonrad Rzeszutek Wilk 
22367249924eSViresh Kumar 	/* Make sure that target_freq is within supported range */
2237910c6e88SViresh Kumar 	target_freq = clamp_val(target_freq, policy->min, policy->max);
22387249924eSViresh Kumar 
22397249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
22407249924eSViresh Kumar 		 policy->cpu, target_freq, relation, old_target_freq);
22415a1c0228SViresh Kumar 
22429c0ebcf7SViresh Kumar 	/*
22439c0ebcf7SViresh Kumar 	 * This might look like a redundant call as we are checking it again
22449c0ebcf7SViresh Kumar 	 * after finding index. But it is left intentionally for cases where
22459c0ebcf7SViresh Kumar 	 * exactly same freq is called again and so we can save on few function
22469c0ebcf7SViresh Kumar 	 * calls.
22479c0ebcf7SViresh Kumar 	 */
22481c534352SRafael J. Wysocki 	if (target_freq == policy->cur &&
22491c534352SRafael J. Wysocki 	    !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS))
22505a1c0228SViresh Kumar 		return 0;
22515a1c0228SViresh Kumar 
22521c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
22536019d23aSRafael J. Wysocki 		return cpufreq_driver->target(policy, target_freq, relation);
22546019d23aSRafael J. Wysocki 
22556019d23aSRafael J. Wysocki 	if (!cpufreq_driver->target_index)
22566019d23aSRafael J. Wysocki 		return -EINVAL;
225790d45d17SAshok Raj 
2258d218ed77SViresh Kumar 	index = cpufreq_frequency_table_target(policy, target_freq, relation);
22599c0ebcf7SViresh Kumar 
226023727845SViresh Kumar 	return __target_index(policy, index);
22616019d23aSRafael J. Wysocki }
22621da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
22631da177e4SLinus Torvalds 
22641da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
22651da177e4SLinus Torvalds 			  unsigned int target_freq,
22661da177e4SLinus Torvalds 			  unsigned int relation)
22671da177e4SLinus Torvalds {
226862c23a89SColin Ian King 	int ret;
22691da177e4SLinus Torvalds 
2270ad7722daSviresh kumar 	down_write(&policy->rwsem);
22711da177e4SLinus Torvalds 
22721da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
22731da177e4SLinus Torvalds 
2274ad7722daSviresh kumar 	up_write(&policy->rwsem);
22751da177e4SLinus Torvalds 
22761da177e4SLinus Torvalds 	return ret;
22771da177e4SLinus Torvalds }
22781da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
22791da177e4SLinus Torvalds 
2280de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2281de1df26bSRafael J. Wysocki {
2282de1df26bSRafael J. Wysocki 	return NULL;
2283de1df26bSRafael J. Wysocki }
2284de1df26bSRafael J. Wysocki 
2285a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy)
22861da177e4SLinus Torvalds {
2287cc993cabSDave Jones 	int ret;
22886afde10cSThomas Renninger 
22892f0aea93SViresh Kumar 	/* Don't start any governor operations if we are entering suspend */
22902f0aea93SViresh Kumar 	if (cpufreq_suspended)
22912f0aea93SViresh Kumar 		return 0;
2292cb57720bSEthan Zhao 	/*
2293cb57720bSEthan Zhao 	 * Governor might not be initiated here if ACPI _PPC changed
2294cb57720bSEthan Zhao 	 * notification happened, so check it.
2295cb57720bSEthan Zhao 	 */
2296cb57720bSEthan Zhao 	if (!policy->governor)
2297cb57720bSEthan Zhao 		return -EINVAL;
22982f0aea93SViresh Kumar 
2299ed4676e2SViresh Kumar 	/* Platform doesn't want dynamic frequency switching ? */
23009a2a9ebcSRafael J. Wysocki 	if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING &&
2301fc4c709fSViresh Kumar 	    cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
2302de1df26bSRafael J. Wysocki 		struct cpufreq_governor *gov = cpufreq_fallback_governor();
2303de1df26bSRafael J. Wysocki 
2304de1df26bSRafael J. Wysocki 		if (gov) {
2305fe829ed8SViresh Kumar 			pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
2306e837f9b5SJoe Perches 				policy->governor->name, gov->name);
23071c256245SThomas Renninger 			policy->governor = gov;
2308de1df26bSRafael J. Wysocki 		} else {
2309de1df26bSRafael J. Wysocki 			return -EINVAL;
23101c256245SThomas Renninger 		}
23116afde10cSThomas Renninger 	}
23121da177e4SLinus Torvalds 
23131da177e4SLinus Torvalds 	if (!try_module_get(policy->governor->owner))
23141da177e4SLinus Torvalds 		return -EINVAL;
23151da177e4SLinus Torvalds 
2316a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
231795731ebbSXiaoguang Chen 
2318e788892bSRafael J. Wysocki 	if (policy->governor->init) {
2319e788892bSRafael J. Wysocki 		ret = policy->governor->init(policy);
2320a92604b4SRafael J. Wysocki 		if (ret) {
23211da177e4SLinus Torvalds 			module_put(policy->governor->owner);
2322a92604b4SRafael J. Wysocki 			return ret;
23239e8c0a89SRafael J. Wysocki 		}
232436be3418SRafael J. Wysocki 	}
23251da177e4SLinus Torvalds 
2326ea9364bbSRafael J. Wysocki 	policy->strict_target = !!(policy->governor->flags & CPUFREQ_GOV_STRICT_TARGET);
2327ea9364bbSRafael J. Wysocki 
2328a92604b4SRafael J. Wysocki 	return 0;
2329a92604b4SRafael J. Wysocki }
2330a92604b4SRafael J. Wysocki 
2331a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2332a92604b4SRafael J. Wysocki {
2333a92604b4SRafael J. Wysocki 	if (cpufreq_suspended || !policy->governor)
2334a92604b4SRafael J. Wysocki 		return;
2335a92604b4SRafael J. Wysocki 
2336a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2337a92604b4SRafael J. Wysocki 
2338e788892bSRafael J. Wysocki 	if (policy->governor->exit)
2339e788892bSRafael J. Wysocki 		policy->governor->exit(policy);
2340a92604b4SRafael J. Wysocki 
23411da177e4SLinus Torvalds 	module_put(policy->governor->owner);
23421da177e4SLinus Torvalds }
23431da177e4SLinus Torvalds 
2344f6ebbcf0SRafael J. Wysocki int cpufreq_start_governor(struct cpufreq_policy *policy)
23450a300767SRafael J. Wysocki {
23460a300767SRafael J. Wysocki 	int ret;
23470a300767SRafael J. Wysocki 
2348a92604b4SRafael J. Wysocki 	if (cpufreq_suspended)
2349a92604b4SRafael J. Wysocki 		return 0;
2350a92604b4SRafael J. Wysocki 
2351a92604b4SRafael J. Wysocki 	if (!policy->governor)
2352a92604b4SRafael J. Wysocki 		return -EINVAL;
2353a92604b4SRafael J. Wysocki 
2354a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2355a92604b4SRafael J. Wysocki 
2356407d0fffSViresh Kumar 	if (cpufreq_driver->get)
23575980752eSViresh Kumar 		cpufreq_verify_current_freq(policy, false);
23583bbf8fe3SRafael J. Wysocki 
2359e788892bSRafael J. Wysocki 	if (policy->governor->start) {
2360e788892bSRafael J. Wysocki 		ret = policy->governor->start(policy);
2361d6ff44d6SRafael J. Wysocki 		if (ret)
2362d6ff44d6SRafael J. Wysocki 			return ret;
2363e788892bSRafael J. Wysocki 	}
2364d6ff44d6SRafael J. Wysocki 
2365e788892bSRafael J. Wysocki 	if (policy->governor->limits)
2366e788892bSRafael J. Wysocki 		policy->governor->limits(policy);
2367e788892bSRafael J. Wysocki 
2368d6ff44d6SRafael J. Wysocki 	return 0;
23690a300767SRafael J. Wysocki }
23700a300767SRafael J. Wysocki 
2371f6ebbcf0SRafael J. Wysocki void cpufreq_stop_governor(struct cpufreq_policy *policy)
2372a92604b4SRafael J. Wysocki {
2373a92604b4SRafael J. Wysocki 	if (cpufreq_suspended || !policy->governor)
2374a92604b4SRafael J. Wysocki 		return;
2375a92604b4SRafael J. Wysocki 
2376a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2377a92604b4SRafael J. Wysocki 
2378e788892bSRafael J. Wysocki 	if (policy->governor->stop)
2379e788892bSRafael J. Wysocki 		policy->governor->stop(policy);
2380a92604b4SRafael J. Wysocki }
2381a92604b4SRafael J. Wysocki 
2382a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2383a92604b4SRafael J. Wysocki {
2384a92604b4SRafael J. Wysocki 	if (cpufreq_suspended || !policy->governor)
2385a92604b4SRafael J. Wysocki 		return;
2386a92604b4SRafael J. Wysocki 
2387a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2388a92604b4SRafael J. Wysocki 
2389e788892bSRafael J. Wysocki 	if (policy->governor->limits)
2390e788892bSRafael J. Wysocki 		policy->governor->limits(policy);
23911da177e4SLinus Torvalds }
23921da177e4SLinus Torvalds 
23931da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
23941da177e4SLinus Torvalds {
23953bcb09a3SJeremy Fitzhardinge 	int err;
23961da177e4SLinus Torvalds 
23971da177e4SLinus Torvalds 	if (!governor)
23981da177e4SLinus Torvalds 		return -EINVAL;
23991da177e4SLinus Torvalds 
2400a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2401a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2402a7b422cdSKonrad Rzeszutek Wilk 
24033fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
24041da177e4SLinus Torvalds 
24053bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
240642f91fa1SViresh Kumar 	if (!find_governor(governor->name)) {
24073bcb09a3SJeremy Fitzhardinge 		err = 0;
24081da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
24093bcb09a3SJeremy Fitzhardinge 	}
24101da177e4SLinus Torvalds 
24113fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
24123bcb09a3SJeremy Fitzhardinge 	return err;
24131da177e4SLinus Torvalds }
24141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
24151da177e4SLinus Torvalds 
24161da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
24171da177e4SLinus Torvalds {
24184573237bSViresh Kumar 	struct cpufreq_policy *policy;
24194573237bSViresh Kumar 	unsigned long flags;
242090e41bacSPrarit Bhargava 
24211da177e4SLinus Torvalds 	if (!governor)
24221da177e4SLinus Torvalds 		return;
24231da177e4SLinus Torvalds 
2424a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2425a7b422cdSKonrad Rzeszutek Wilk 		return;
2426a7b422cdSKonrad Rzeszutek Wilk 
24274573237bSViresh Kumar 	/* clear last_governor for all inactive policies */
24284573237bSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
24294573237bSViresh Kumar 	for_each_inactive_policy(policy) {
243018bf3a12SViresh Kumar 		if (!strcmp(policy->last_governor, governor->name)) {
243118bf3a12SViresh Kumar 			policy->governor = NULL;
24324573237bSViresh Kumar 			strcpy(policy->last_governor, "\0");
243390e41bacSPrarit Bhargava 		}
243418bf3a12SViresh Kumar 	}
24354573237bSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
243690e41bacSPrarit Bhargava 
24373fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
24381da177e4SLinus Torvalds 	list_del(&governor->governor_list);
24393fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
24401da177e4SLinus Torvalds }
24411da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
24421da177e4SLinus Torvalds 
24431da177e4SLinus Torvalds 
24441da177e4SLinus Torvalds /*********************************************************************
24451da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
24461da177e4SLinus Torvalds  *********************************************************************/
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds /**
24491da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
245029464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
245129464f28SDave Jones  *	is written
2452a9909c21SLee Jones  * @cpu: CPU to find the policy for
24531da177e4SLinus Torvalds  *
24541da177e4SLinus Torvalds  * Reads the current cpufreq policy.
24551da177e4SLinus Torvalds  */
24561da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
24571da177e4SLinus Torvalds {
24581da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
24591da177e4SLinus Torvalds 	if (!policy)
24601da177e4SLinus Torvalds 		return -EINVAL;
24611da177e4SLinus Torvalds 
24621da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
24631da177e4SLinus Torvalds 	if (!cpu_policy)
24641da177e4SLinus Torvalds 		return -EINVAL;
24651da177e4SLinus Torvalds 
2466d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
24671da177e4SLinus Torvalds 
24681da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
24691da177e4SLinus Torvalds 	return 0;
24701da177e4SLinus Torvalds }
24711da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
24721da177e4SLinus Torvalds 
2473a0dbb819SRafael J. Wysocki /**
2474a0dbb819SRafael J. Wysocki  * cpufreq_set_policy - Modify cpufreq policy parameters.
2475a0dbb819SRafael J. Wysocki  * @policy: Policy object to modify.
24761e4f63aeSRafael J. Wysocki  * @new_gov: Policy governor pointer.
24771e4f63aeSRafael J. Wysocki  * @new_pol: Policy value (for drivers with built-in governors).
2478a0dbb819SRafael J. Wysocki  *
24791e4f63aeSRafael J. Wysocki  * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency
24801e4f63aeSRafael J. Wysocki  * limits to be set for the policy, update @policy with the verified limits
24811e4f63aeSRafael J. Wysocki  * values and either invoke the driver's ->setpolicy() callback (if present) or
24821e4f63aeSRafael J. Wysocki  * carry out a governor update for @policy.  That is, run the current governor's
24831e4f63aeSRafael J. Wysocki  * ->limits() callback (if @new_gov points to the same object as the one in
24841e4f63aeSRafael J. Wysocki  * @policy) or replace the governor for @policy with @new_gov.
2485a0dbb819SRafael J. Wysocki  *
2486a0dbb819SRafael J. Wysocki  * The cpuinfo part of @policy is not updated by this function.
2487153d7f3fSArjan van de Ven  */
24881e4f63aeSRafael J. Wysocki static int cpufreq_set_policy(struct cpufreq_policy *policy,
24891e4f63aeSRafael J. Wysocki 			      struct cpufreq_governor *new_gov,
24901e4f63aeSRafael J. Wysocki 			      unsigned int new_pol)
24911da177e4SLinus Torvalds {
24921e4f63aeSRafael J. Wysocki 	struct cpufreq_policy_data new_data;
2493d9a789c7SRafael J. Wysocki 	struct cpufreq_governor *old_gov;
2494d9a789c7SRafael J. Wysocki 	int ret;
24951da177e4SLinus Torvalds 
24961e4f63aeSRafael J. Wysocki 	memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
24971e4f63aeSRafael J. Wysocki 	new_data.freq_table = policy->freq_table;
24981e4f63aeSRafael J. Wysocki 	new_data.cpu = policy->cpu;
2499fba9573bSPan Xinhui 	/*
250067d874c3SViresh Kumar 	 * PM QoS framework collects all the requests from users and provide us
250167d874c3SViresh Kumar 	 * the final aggregated value here.
2502fba9573bSPan Xinhui 	 */
25031e4f63aeSRafael J. Wysocki 	new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
25041e4f63aeSRafael J. Wysocki 	new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
25051e4f63aeSRafael J. Wysocki 
25061e4f63aeSRafael J. Wysocki 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
25071e4f63aeSRafael J. Wysocki 		 new_data.cpu, new_data.min, new_data.max);
25089c9a43edSMattia Dongili 
2509737ffb27SViresh Kumar 	/*
2510737ffb27SViresh Kumar 	 * Verify that the CPU speed can be set within these limits and make sure
2511737ffb27SViresh Kumar 	 * that min <= max.
2512737ffb27SViresh Kumar 	 */
25131e4f63aeSRafael J. Wysocki 	ret = cpufreq_driver->verify(&new_data);
25141da177e4SLinus Torvalds 	if (ret)
2515d9a789c7SRafael J. Wysocki 		return ret;
25161da177e4SLinus Torvalds 
25171e4f63aeSRafael J. Wysocki 	policy->min = new_data.min;
25181e4f63aeSRafael J. Wysocki 	policy->max = new_data.max;
2519601b2185SRuchi Kandoi 	trace_cpu_frequency_limits(policy);
25201da177e4SLinus Torvalds 
2521e3c06236SSteve Muckle 	policy->cached_target_freq = UINT_MAX;
2522e3c06236SSteve Muckle 
25232d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
25243a3e9e06SViresh Kumar 		 policy->min, policy->max);
25251da177e4SLinus Torvalds 
25261c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
25271e4f63aeSRafael J. Wysocki 		policy->policy = new_pol;
25282d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
2529167a38dcSRafael J. Wysocki 		return cpufreq_driver->setpolicy(policy);
2530d9a789c7SRafael J. Wysocki 	}
2531d9a789c7SRafael J. Wysocki 
25321e4f63aeSRafael J. Wysocki 	if (new_gov == policy->governor) {
25332bb4059eSRafael J. Wysocki 		pr_debug("governor limits update\n");
2534a92604b4SRafael J. Wysocki 		cpufreq_governor_limits(policy);
2535d6ff44d6SRafael J. Wysocki 		return 0;
25360a300767SRafael J. Wysocki 	}
25371da177e4SLinus Torvalds 
25382d06d8c4SDominik Brodowski 	pr_debug("governor switch\n");
25391da177e4SLinus Torvalds 
2540d9a789c7SRafael J. Wysocki 	/* save old, working values */
2541d9a789c7SRafael J. Wysocki 	old_gov = policy->governor;
25421da177e4SLinus Torvalds 	/* end old governor */
2543d9a789c7SRafael J. Wysocki 	if (old_gov) {
254445482c70SRafael J. Wysocki 		cpufreq_stop_governor(policy);
254536be3418SRafael J. Wysocki 		cpufreq_exit_governor(policy);
25467bd353a9SViresh Kumar 	}
25471da177e4SLinus Torvalds 
25481da177e4SLinus Torvalds 	/* start new governor */
25491e4f63aeSRafael J. Wysocki 	policy->governor = new_gov;
2550a92604b4SRafael J. Wysocki 	ret = cpufreq_init_governor(policy);
25514bc384aeSViresh Kumar 	if (!ret) {
25520a300767SRafael J. Wysocki 		ret = cpufreq_start_governor(policy);
25530a300767SRafael J. Wysocki 		if (!ret) {
25542bb4059eSRafael J. Wysocki 			pr_debug("governor change\n");
2555531b5c9fSQuentin Perret 			sched_cpufreq_governor_change(policy, old_gov);
25560a300767SRafael J. Wysocki 			return 0;
25570a300767SRafael J. Wysocki 		}
2558b7898fdaSRafael J. Wysocki 		cpufreq_exit_governor(policy);
2559955ef483SViresh Kumar 	}
25607bd353a9SViresh Kumar 
25611da177e4SLinus Torvalds 	/* new governor failed, so re-start old one */
2562d9a789c7SRafael J. Wysocki 	pr_debug("starting governor %s failed\n", policy->governor->name);
25631da177e4SLinus Torvalds 	if (old_gov) {
25643a3e9e06SViresh Kumar 		policy->governor = old_gov;
2565a92604b4SRafael J. Wysocki 		if (cpufreq_init_governor(policy))
25664bc384aeSViresh Kumar 			policy->governor = NULL;
25674bc384aeSViresh Kumar 		else
25680a300767SRafael J. Wysocki 			cpufreq_start_governor(policy);
25691da177e4SLinus Torvalds 	}
25701da177e4SLinus Torvalds 
25714bc384aeSViresh Kumar 	return ret;
25721da177e4SLinus Torvalds }
25731da177e4SLinus Torvalds 
25741da177e4SLinus Torvalds /**
2575a0dbb819SRafael J. Wysocki  * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2576a0dbb819SRafael J. Wysocki  * @cpu: CPU to re-evaluate the policy for.
25771da177e4SLinus Torvalds  *
2578a0dbb819SRafael J. Wysocki  * Update the current frequency for the cpufreq policy of @cpu and use
257918c49926SViresh Kumar  * cpufreq_set_policy() to re-apply the min and max limits, which triggers the
258018c49926SViresh Kumar  * evaluation of policy notifiers and the cpufreq driver's ->verify() callback
258118c49926SViresh Kumar  * for the policy in question, among other things.
25821da177e4SLinus Torvalds  */
258330248fefSRafael J. Wysocki void cpufreq_update_policy(unsigned int cpu)
25841da177e4SLinus Torvalds {
2585540a3758SRafael J. Wysocki 	struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
25861da177e4SLinus Torvalds 
2587fefa8ff8SAaron Plattner 	if (!policy)
258830248fefSRafael J. Wysocki 		return;
25891da177e4SLinus Torvalds 
2590bb176f7dSViresh Kumar 	/*
2591bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
2592bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
2593bb176f7dSViresh Kumar 	 */
25945ddc6d4eSViresh Kumar 	if (cpufreq_driver->get && has_target() &&
25955980752eSViresh Kumar 	    (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
2596742c87bfSRafael J. Wysocki 		goto unlock;
259730248fefSRafael J. Wysocki 
259870a59fdeSViresh Kumar 	refresh_frequency_limits(policy);
25991da177e4SLinus Torvalds 
2600fefa8ff8SAaron Plattner unlock:
2601540a3758SRafael J. Wysocki 	cpufreq_cpu_release(policy);
26021da177e4SLinus Torvalds }
26031da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
26041da177e4SLinus Torvalds 
26055a25e3f7SRafael J. Wysocki /**
26065a25e3f7SRafael J. Wysocki  * cpufreq_update_limits - Update policy limits for a given CPU.
26075a25e3f7SRafael J. Wysocki  * @cpu: CPU to update the policy limits for.
26085a25e3f7SRafael J. Wysocki  *
26095a25e3f7SRafael J. Wysocki  * Invoke the driver's ->update_limits callback if present or call
26105a25e3f7SRafael J. Wysocki  * cpufreq_update_policy() for @cpu.
26115a25e3f7SRafael J. Wysocki  */
26125a25e3f7SRafael J. Wysocki void cpufreq_update_limits(unsigned int cpu)
26135a25e3f7SRafael J. Wysocki {
26145a25e3f7SRafael J. Wysocki 	if (cpufreq_driver->update_limits)
26155a25e3f7SRafael J. Wysocki 		cpufreq_driver->update_limits(cpu);
26165a25e3f7SRafael J. Wysocki 	else
26175a25e3f7SRafael J. Wysocki 		cpufreq_update_policy(cpu);
26185a25e3f7SRafael J. Wysocki }
26195a25e3f7SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_update_limits);
26205a25e3f7SRafael J. Wysocki 
26211da177e4SLinus Torvalds /*********************************************************************
26226f19efc0SLukasz Majewski  *               BOOST						     *
26236f19efc0SLukasz Majewski  *********************************************************************/
2624cf6fada7SXiongfeng Wang static int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state)
26256f19efc0SLukasz Majewski {
2626552abb88SRafael J. Wysocki 	int ret;
2627552abb88SRafael J. Wysocki 
2628f8bfc116SViresh Kumar 	if (!policy->freq_table)
2629552abb88SRafael J. Wysocki 		return -ENXIO;
2630f8bfc116SViresh Kumar 
2631cf6fada7SXiongfeng Wang 	ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table);
26326f19efc0SLukasz Majewski 	if (ret) {
2633cf6fada7SXiongfeng Wang 		pr_err("%s: Policy frequency update failed\n", __func__);
2634552abb88SRafael J. Wysocki 		return ret;
26356f19efc0SLukasz Majewski 	}
263649f18560SViresh Kumar 
26373000ce3cSRafael J. Wysocki 	ret = freq_qos_update_request(policy->max_freq_req, policy->max);
2638e61a4125SViresh Kumar 	if (ret < 0)
2639552abb88SRafael J. Wysocki 		return ret;
26406f19efc0SLukasz Majewski 
2641552abb88SRafael J. Wysocki 	return 0;
26426f19efc0SLukasz Majewski }
26436f19efc0SLukasz Majewski 
26446f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state)
26456f19efc0SLukasz Majewski {
2646cf6fada7SXiongfeng Wang 	struct cpufreq_policy *policy;
26476f19efc0SLukasz Majewski 	unsigned long flags;
26486f19efc0SLukasz Majewski 	int ret = 0;
26496f19efc0SLukasz Majewski 
26506f19efc0SLukasz Majewski 	if (cpufreq_driver->boost_enabled == state)
26516f19efc0SLukasz Majewski 		return 0;
26526f19efc0SLukasz Majewski 
26536f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
26546f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = state;
26556f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
26566f19efc0SLukasz Majewski 
2657cf6fada7SXiongfeng Wang 	get_online_cpus();
2658cf6fada7SXiongfeng Wang 	for_each_active_policy(policy) {
2659cf6fada7SXiongfeng Wang 		ret = cpufreq_driver->set_boost(policy, state);
2660cf6fada7SXiongfeng Wang 		if (ret)
2661cf6fada7SXiongfeng Wang 			goto err_reset_state;
2662cf6fada7SXiongfeng Wang 	}
2663cf6fada7SXiongfeng Wang 	put_online_cpus();
2664cf6fada7SXiongfeng Wang 
2665cf6fada7SXiongfeng Wang 	return 0;
2666cf6fada7SXiongfeng Wang 
2667cf6fada7SXiongfeng Wang err_reset_state:
2668cf6fada7SXiongfeng Wang 	put_online_cpus();
2669cf6fada7SXiongfeng Wang 
26706f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
26716f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = !state;
26726f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
26736f19efc0SLukasz Majewski 
2674e837f9b5SJoe Perches 	pr_err("%s: Cannot %s BOOST\n",
2675e837f9b5SJoe Perches 	       __func__, state ? "enable" : "disable");
26766f19efc0SLukasz Majewski 
26776f19efc0SLukasz Majewski 	return ret;
26786f19efc0SLukasz Majewski }
26796f19efc0SLukasz Majewski 
268041669da0SRafael J. Wysocki static bool cpufreq_boost_supported(void)
26816f19efc0SLukasz Majewski {
268289f98d7eSYue Hu 	return cpufreq_driver->set_boost;
26836f19efc0SLukasz Majewski }
26846f19efc0SLukasz Majewski 
268544139ed4SViresh Kumar static int create_boost_sysfs_file(void)
268644139ed4SViresh Kumar {
268744139ed4SViresh Kumar 	int ret;
268844139ed4SViresh Kumar 
2689c82bd444SViresh Kumar 	ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
269044139ed4SViresh Kumar 	if (ret)
269144139ed4SViresh Kumar 		pr_err("%s: cannot register global BOOST sysfs file\n",
269244139ed4SViresh Kumar 		       __func__);
269344139ed4SViresh Kumar 
269444139ed4SViresh Kumar 	return ret;
269544139ed4SViresh Kumar }
269644139ed4SViresh Kumar 
269744139ed4SViresh Kumar static void remove_boost_sysfs_file(void)
269844139ed4SViresh Kumar {
269944139ed4SViresh Kumar 	if (cpufreq_boost_supported())
2700c82bd444SViresh Kumar 		sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
270144139ed4SViresh Kumar }
270244139ed4SViresh Kumar 
270344139ed4SViresh Kumar int cpufreq_enable_boost_support(void)
270444139ed4SViresh Kumar {
270544139ed4SViresh Kumar 	if (!cpufreq_driver)
270644139ed4SViresh Kumar 		return -EINVAL;
270744139ed4SViresh Kumar 
270844139ed4SViresh Kumar 	if (cpufreq_boost_supported())
270944139ed4SViresh Kumar 		return 0;
271044139ed4SViresh Kumar 
27117a6c79f2SRafael J. Wysocki 	cpufreq_driver->set_boost = cpufreq_boost_set_sw;
271244139ed4SViresh Kumar 
271344139ed4SViresh Kumar 	/* This will get removed on driver unregister */
271444139ed4SViresh Kumar 	return create_boost_sysfs_file();
271544139ed4SViresh Kumar }
271644139ed4SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
271744139ed4SViresh Kumar 
27186f19efc0SLukasz Majewski int cpufreq_boost_enabled(void)
27196f19efc0SLukasz Majewski {
27206f19efc0SLukasz Majewski 	return cpufreq_driver->boost_enabled;
27216f19efc0SLukasz Majewski }
27226f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
27236f19efc0SLukasz Majewski 
27246f19efc0SLukasz Majewski /*********************************************************************
27251da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
27261da177e4SLinus Torvalds  *********************************************************************/
272727622b06SSebastian Andrzej Siewior static enum cpuhp_state hp_online;
27281da177e4SLinus Torvalds 
2729c4a3fa26SChen Yu static int cpuhp_cpufreq_online(unsigned int cpu)
2730c4a3fa26SChen Yu {
2731c4a3fa26SChen Yu 	cpufreq_online(cpu);
2732c4a3fa26SChen Yu 
2733c4a3fa26SChen Yu 	return 0;
2734c4a3fa26SChen Yu }
2735c4a3fa26SChen Yu 
2736c4a3fa26SChen Yu static int cpuhp_cpufreq_offline(unsigned int cpu)
2737c4a3fa26SChen Yu {
2738c4a3fa26SChen Yu 	cpufreq_offline(cpu);
2739c4a3fa26SChen Yu 
2740c4a3fa26SChen Yu 	return 0;
2741c4a3fa26SChen Yu }
2742c4a3fa26SChen Yu 
27431da177e4SLinus Torvalds /**
27441da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
27451da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
27461da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
27471da177e4SLinus Torvalds  *
27481da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
274963af4055SEric Biggers  * returns zero on success, -EEXIST when another driver got here first
27501da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
27511da177e4SLinus Torvalds  *
27521da177e4SLinus Torvalds  */
2753221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
27541da177e4SLinus Torvalds {
27551da177e4SLinus Torvalds 	unsigned long flags;
27561da177e4SLinus Torvalds 	int ret;
27571da177e4SLinus Torvalds 
2758a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2759a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2760a7b422cdSKonrad Rzeszutek Wilk 
276146770be0SViresh Kumar 	/*
276246770be0SViresh Kumar 	 * The cpufreq core depends heavily on the availability of device
276346770be0SViresh Kumar 	 * structure, make sure they are available before proceeding further.
276446770be0SViresh Kumar 	 */
276546770be0SViresh Kumar 	if (!get_cpu_device(0))
276646770be0SViresh Kumar 		return -EPROBE_DEFER;
276746770be0SViresh Kumar 
27681da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
27699c0ebcf7SViresh Kumar 	    !(driver_data->setpolicy || driver_data->target_index ||
27709832235fSRafael J. Wysocki 		    driver_data->target) ||
27719832235fSRafael J. Wysocki 	     (driver_data->setpolicy && (driver_data->target_index ||
27721c03a2d0SViresh Kumar 		    driver_data->target)) ||
2773a9a22b57SViresh Kumar 	     (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
277491a12e91SViresh Kumar 	     (!driver_data->online != !driver_data->offline))
27751da177e4SLinus Torvalds 		return -EINVAL;
27761da177e4SLinus Torvalds 
27772d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
27781da177e4SLinus Torvalds 
2779fdd320daSRafael J. Wysocki 	/* Protect against concurrent CPU online/offline. */
2780a92551e4SSebastian Andrzej Siewior 	cpus_read_lock();
2781fdd320daSRafael J. Wysocki 
27820d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
27831c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
27840d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2785fdd320daSRafael J. Wysocki 		ret = -EEXIST;
2786fdd320daSRafael J. Wysocki 		goto out;
27871da177e4SLinus Torvalds 	}
27881c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
27890d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
27901da177e4SLinus Torvalds 
2791874f6353SIonela Voinescu 	/*
2792874f6353SIonela Voinescu 	 * Mark support for the scheduler's frequency invariance engine for
2793874f6353SIonela Voinescu 	 * drivers that implement target(), target_index() or fast_switch().
2794874f6353SIonela Voinescu 	 */
2795874f6353SIonela Voinescu 	if (!cpufreq_driver->setpolicy) {
2796874f6353SIonela Voinescu 		static_branch_enable_cpuslocked(&cpufreq_freq_invariance);
2797874f6353SIonela Voinescu 		pr_debug("supports frequency invariance");
2798874f6353SIonela Voinescu 	}
2799874f6353SIonela Voinescu 
2800bc68b7dfSViresh Kumar 	if (driver_data->setpolicy)
2801bc68b7dfSViresh Kumar 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
2802bc68b7dfSViresh Kumar 
28037a6c79f2SRafael J. Wysocki 	if (cpufreq_boost_supported()) {
280444139ed4SViresh Kumar 		ret = create_boost_sysfs_file();
280544139ed4SViresh Kumar 		if (ret)
28066f19efc0SLukasz Majewski 			goto err_null_driver;
28077a6c79f2SRafael J. Wysocki 	}
28086f19efc0SLukasz Majewski 
28098a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
28108f5bc2abSJiri Slaby 	if (ret)
28116f19efc0SLukasz Majewski 		goto err_boost_unreg;
28121da177e4SLinus Torvalds 
2813*5ae4a4b4SViresh Kumar 	if (unlikely(list_empty(&cpufreq_policy_list))) {
28141da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
28156c770036SDavid Arcari 		ret = -ENODEV;
2816ce1bcfe9SViresh Kumar 		pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2817e08f5f5bSGautham R Shenoy 			 driver_data->name);
28188a25a2fdSKay Sievers 		goto err_if_unreg;
28191da177e4SLinus Torvalds 	}
28201da177e4SLinus Torvalds 
2821a92551e4SSebastian Andrzej Siewior 	ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2822a92551e4SSebastian Andrzej Siewior 						   "cpufreq:online",
2823c4a3fa26SChen Yu 						   cpuhp_cpufreq_online,
2824c4a3fa26SChen Yu 						   cpuhp_cpufreq_offline);
282527622b06SSebastian Andrzej Siewior 	if (ret < 0)
282627622b06SSebastian Andrzej Siewior 		goto err_if_unreg;
282727622b06SSebastian Andrzej Siewior 	hp_online = ret;
28285372e054SSebastian Andrzej Siewior 	ret = 0;
282927622b06SSebastian Andrzej Siewior 
28302d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
28313834abb4SPankaj Gupta 	goto out;
2832fdd320daSRafael J. Wysocki 
28338a25a2fdSKay Sievers err_if_unreg:
28348a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
28356f19efc0SLukasz Majewski err_boost_unreg:
283644139ed4SViresh Kumar 	remove_boost_sysfs_file();
28378f5bc2abSJiri Slaby err_null_driver:
28380d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
28391c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
28400d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
28413834abb4SPankaj Gupta out:
2842a92551e4SSebastian Andrzej Siewior 	cpus_read_unlock();
28433834abb4SPankaj Gupta 	return ret;
28441da177e4SLinus Torvalds }
28451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
28461da177e4SLinus Torvalds 
2847a9909c21SLee Jones /*
28481da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
28491da177e4SLinus Torvalds  *
28501da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
28511da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
28521da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
28531da177e4SLinus Torvalds  * currently not initialised.
28541da177e4SLinus Torvalds  */
2855221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
28561da177e4SLinus Torvalds {
28571da177e4SLinus Torvalds 	unsigned long flags;
28581da177e4SLinus Torvalds 
28591c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
28601da177e4SLinus Torvalds 		return -EINVAL;
28611da177e4SLinus Torvalds 
28622d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
28631da177e4SLinus Torvalds 
2864454d3a25SSebastian Andrzej Siewior 	/* Protect against concurrent cpu hotplug */
2865a92551e4SSebastian Andrzej Siewior 	cpus_read_lock();
28668a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
286744139ed4SViresh Kumar 	remove_boost_sysfs_file();
2868874f6353SIonela Voinescu 	static_branch_disable_cpuslocked(&cpufreq_freq_invariance);
2869a92551e4SSebastian Andrzej Siewior 	cpuhp_remove_state_nocalls_cpuslocked(hp_online);
28701da177e4SLinus Torvalds 
28710d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
28726eed9404SViresh Kumar 
28731c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
28746eed9404SViresh Kumar 
28750d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2876a92551e4SSebastian Andrzej Siewior 	cpus_read_unlock();
28771da177e4SLinus Torvalds 
28781da177e4SLinus Torvalds 	return 0;
28791da177e4SLinus Torvalds }
28801da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
28815a01f2e8SVenkatesh Pallipadi 
28825a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
28835a01f2e8SVenkatesh Pallipadi {
28848412b456SQuentin Perret 	struct cpufreq_governor *gov = cpufreq_default_governor();
28858412b456SQuentin Perret 
2886a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2887a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2888a7b422cdSKonrad Rzeszutek Wilk 
28898eec1020SViresh Kumar 	cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
28908aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
28918aa84ad8SThomas Renninger 
28928412b456SQuentin Perret 	if (!strlen(default_governor))
28938412b456SQuentin Perret 		strncpy(default_governor, gov->name, CPUFREQ_NAME_LEN);
28948412b456SQuentin Perret 
28955a01f2e8SVenkatesh Pallipadi 	return 0;
28965a01f2e8SVenkatesh Pallipadi }
2897d82f2692SLen Brown module_param(off, int, 0444);
28988412b456SQuentin Perret module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444);
28995a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2900