xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision 5c84c1b84bd80a735b2c7804e7be86d043fca595)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/drivers/cpufreq/cpufreq.c
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2001 Russell King
61da177e4SLinus Torvalds  *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7bb176f7dSViresh Kumar  *            (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
81da177e4SLinus Torvalds  *
9c32b6b8eSAshok Raj  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
10c32b6b8eSAshok Raj  *	Added handling for CPU hotplug
118ff69732SDave Jones  *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
128ff69732SDave Jones  *	Fix handling for CPU hotplug -- affected CPUs
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
15db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16db701151SViresh Kumar 
175ff0a268SViresh Kumar #include <linux/cpu.h>
181da177e4SLinus Torvalds #include <linux/cpufreq.h>
195c238a8bSAmit Kucheria #include <linux/cpu_cooling.h>
201da177e4SLinus Torvalds #include <linux/delay.h>
211da177e4SLinus Torvalds #include <linux/device.h>
225ff0a268SViresh Kumar #include <linux/init.h>
235ff0a268SViresh Kumar #include <linux/kernel_stat.h>
245ff0a268SViresh Kumar #include <linux/module.h>
253fc54d37Sakpm@osdl.org #include <linux/mutex.h>
2667d874c3SViresh Kumar #include <linux/pm_qos.h>
275ff0a268SViresh Kumar #include <linux/slab.h>
282f0aea93SViresh Kumar #include <linux/suspend.h>
2990de2a4aSDoug Anderson #include <linux/syscore_ops.h>
305ff0a268SViresh Kumar #include <linux/tick.h>
31f55ae08cSViresh Kumar #include <linux/units.h>
326f4f2723SThomas Renninger #include <trace/events/power.h>
336f4f2723SThomas Renninger 
34b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list);
35f963735aSViresh Kumar 
36f963735aSViresh Kumar /* Macros to iterate over CPU policies */
37f963735aSViresh Kumar #define for_each_suitable_policy(__policy, __active)			 \
38fd7dc7e6SEric Biggers 	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
39fd7dc7e6SEric Biggers 		if ((__active) == !policy_is_inactive(__policy))
40f963735aSViresh Kumar 
41f963735aSViresh Kumar #define for_each_active_policy(__policy)		\
42f963735aSViresh Kumar 	for_each_suitable_policy(__policy, true)
43f963735aSViresh Kumar #define for_each_inactive_policy(__policy)		\
44f963735aSViresh Kumar 	for_each_suitable_policy(__policy, false)
45f963735aSViresh Kumar 
46f7b27061SViresh Kumar /* Iterate over governors */
47f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list);
48f7b27061SViresh Kumar #define for_each_governor(__governor)				\
49f7b27061SViresh Kumar 	list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
50f7b27061SViresh Kumar 
518412b456SQuentin Perret static char default_governor[CPUFREQ_NAME_LEN];
528412b456SQuentin Perret 
53a9909c21SLee Jones /*
54cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
551da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
561da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
571da177e4SLinus Torvalds  */
581c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
597a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
60bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
61bb176f7dSViresh Kumar 
62874f6353SIonela Voinescu static DEFINE_STATIC_KEY_FALSE(cpufreq_freq_invariance);
63874f6353SIonela Voinescu bool cpufreq_supports_freq_invariance(void)
64874f6353SIonela Voinescu {
65874f6353SIonela Voinescu 	return static_branch_likely(&cpufreq_freq_invariance);
66874f6353SIonela Voinescu }
67874f6353SIonela Voinescu 
682f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */
692f0aea93SViresh Kumar static bool cpufreq_suspended;
701da177e4SLinus Torvalds 
719c0ebcf7SViresh Kumar static inline bool has_target(void)
729c0ebcf7SViresh Kumar {
739c0ebcf7SViresh Kumar 	return cpufreq_driver->target_index || cpufreq_driver->target;
749c0ebcf7SViresh Kumar }
759c0ebcf7SViresh Kumar 
761da177e4SLinus Torvalds /* internal prototypes */
77d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
78a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy);
79a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy);
80a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy);
811e4f63aeSRafael J. Wysocki static int cpufreq_set_policy(struct cpufreq_policy *policy,
821e4f63aeSRafael J. Wysocki 			      struct cpufreq_governor *new_gov,
831e4f63aeSRafael J. Wysocki 			      unsigned int new_pol);
8445482c70SRafael J. Wysocki 
85a9909c21SLee Jones /*
861da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
871da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
881da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
891da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
901da177e4SLinus Torvalds  * The mutex locks both lists.
911da177e4SLinus Torvalds  */
92e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
93cc85de36SSebastian Andrzej Siewior SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
941da177e4SLinus Torvalds 
95a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
96da584455SViresh Kumar static int cpufreq_disabled(void)
97a7b422cdSKonrad Rzeszutek Wilk {
98a7b422cdSKonrad Rzeszutek Wilk 	return off;
99a7b422cdSKonrad Rzeszutek Wilk }
100a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
101a7b422cdSKonrad Rzeszutek Wilk {
102a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
103a7b422cdSKonrad Rzeszutek Wilk }
1043fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
1051da177e4SLinus Torvalds 
1064d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1074d5dcc42SViresh Kumar {
1080b981e70SViresh Kumar 	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
1094d5dcc42SViresh Kumar }
1103f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1114d5dcc42SViresh Kumar 
112183edb20SYangtao Li static struct kobject *cpufreq_global_kobject;
113183edb20SYangtao Li 
114944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
115944e9a03SViresh Kumar {
116944e9a03SViresh Kumar 	if (have_governor_per_policy())
117944e9a03SViresh Kumar 		return &policy->kobj;
118944e9a03SViresh Kumar 	else
119944e9a03SViresh Kumar 		return cpufreq_global_kobject;
120944e9a03SViresh Kumar }
121944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
122944e9a03SViresh Kumar 
12372a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
12472a4ce34SViresh Kumar {
1255720821bSFrederic Weisbecker 	struct kernel_cpustat kcpustat;
12672a4ce34SViresh Kumar 	u64 cur_wall_time;
1275720821bSFrederic Weisbecker 	u64 idle_time;
12872a4ce34SViresh Kumar 	u64 busy_time;
12972a4ce34SViresh Kumar 
1307fb1327eSFrederic Weisbecker 	cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
13172a4ce34SViresh Kumar 
1325720821bSFrederic Weisbecker 	kcpustat_cpu_fetch(&kcpustat, cpu);
1335720821bSFrederic Weisbecker 
1345720821bSFrederic Weisbecker 	busy_time = kcpustat.cpustat[CPUTIME_USER];
1355720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_SYSTEM];
1365720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_IRQ];
1375720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ];
1385720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_STEAL];
1395720821bSFrederic Weisbecker 	busy_time += kcpustat.cpustat[CPUTIME_NICE];
14072a4ce34SViresh Kumar 
14172a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
14272a4ce34SViresh Kumar 	if (wall)
1437fb1327eSFrederic Weisbecker 		*wall = div_u64(cur_wall_time, NSEC_PER_USEC);
14472a4ce34SViresh Kumar 
1457fb1327eSFrederic Weisbecker 	return div_u64(idle_time, NSEC_PER_USEC);
14672a4ce34SViresh Kumar }
14772a4ce34SViresh Kumar 
14872a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
14972a4ce34SViresh Kumar {
15072a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
15172a4ce34SViresh Kumar 
15272a4ce34SViresh Kumar 	if (idle_time == -1ULL)
15372a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
15472a4ce34SViresh Kumar 	else if (!io_busy)
15572a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
15672a4ce34SViresh Kumar 
15772a4ce34SViresh Kumar 	return idle_time;
15872a4ce34SViresh Kumar }
15972a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
16072a4ce34SViresh Kumar 
16170e9e778SViresh Kumar /*
16270e9e778SViresh Kumar  * This is a generic cpufreq init() routine which can be used by cpufreq
16370e9e778SViresh Kumar  * drivers of SMP systems. It will do following:
16470e9e778SViresh Kumar  * - validate & show freq table passed
16570e9e778SViresh Kumar  * - set policies transition latency
16670e9e778SViresh Kumar  * - policy->cpus with all possible CPUs
16770e9e778SViresh Kumar  */
168c4dcc8a1SViresh Kumar void cpufreq_generic_init(struct cpufreq_policy *policy,
16970e9e778SViresh Kumar 		struct cpufreq_frequency_table *table,
17070e9e778SViresh Kumar 		unsigned int transition_latency)
17170e9e778SViresh Kumar {
17292c99d15SViresh Kumar 	policy->freq_table = table;
17370e9e778SViresh Kumar 	policy->cpuinfo.transition_latency = transition_latency;
17470e9e778SViresh Kumar 
17570e9e778SViresh Kumar 	/*
17658405af6SShailendra Verma 	 * The driver only supports the SMP configuration where all processors
17770e9e778SViresh Kumar 	 * share the clock and voltage and clock.
17870e9e778SViresh Kumar 	 */
17970e9e778SViresh Kumar 	cpumask_setall(policy->cpus);
18070e9e778SViresh Kumar }
18170e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init);
18270e9e778SViresh Kumar 
1831f0bd44eSRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
184652ed95dSViresh Kumar {
185652ed95dSViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
186652ed95dSViresh Kumar 
187988bed09SViresh Kumar 	return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
188988bed09SViresh Kumar }
1891f0bd44eSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
190988bed09SViresh Kumar 
191988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu)
192988bed09SViresh Kumar {
193988bed09SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
194988bed09SViresh Kumar 
195652ed95dSViresh Kumar 	if (!policy || IS_ERR(policy->clk)) {
196e837f9b5SJoe Perches 		pr_err("%s: No %s associated to cpu: %d\n",
197e837f9b5SJoe Perches 		       __func__, policy ? "clk" : "policy", cpu);
198652ed95dSViresh Kumar 		return 0;
199652ed95dSViresh Kumar 	}
200652ed95dSViresh Kumar 
201652ed95dSViresh Kumar 	return clk_get_rate(policy->clk) / 1000;
202652ed95dSViresh Kumar }
203652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get);
204652ed95dSViresh Kumar 
20550e9c852SViresh Kumar /**
2065d094feaSRafael J. Wysocki  * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
2075d094feaSRafael J. Wysocki  * @cpu: CPU to find the policy for.
20850e9c852SViresh Kumar  *
2095d094feaSRafael J. Wysocki  * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
2105d094feaSRafael J. Wysocki  * the kobject reference counter of that policy.  Return a valid policy on
2115d094feaSRafael J. Wysocki  * success or NULL on failure.
21250e9c852SViresh Kumar  *
2135d094feaSRafael J. Wysocki  * The policy returned by this function has to be released with the help of
2145d094feaSRafael J. Wysocki  * cpufreq_cpu_put() to balance its kobject reference counter properly.
21550e9c852SViresh Kumar  */
2166eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
2171da177e4SLinus Torvalds {
2186eed9404SViresh Kumar 	struct cpufreq_policy *policy = NULL;
2191da177e4SLinus Torvalds 	unsigned long flags;
2201da177e4SLinus Torvalds 
2211b947c90SViresh Kumar 	if (WARN_ON(cpu >= nr_cpu_ids))
2226eed9404SViresh Kumar 		return NULL;
2236eed9404SViresh Kumar 
2241da177e4SLinus Torvalds 	/* get the cpufreq driver */
2250d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
2261da177e4SLinus Torvalds 
2276eed9404SViresh Kumar 	if (cpufreq_driver) {
2281da177e4SLinus Torvalds 		/* get the CPU */
229988bed09SViresh Kumar 		policy = cpufreq_cpu_get_raw(cpu);
2306eed9404SViresh Kumar 		if (policy)
2316eed9404SViresh Kumar 			kobject_get(&policy->kobj);
2326eed9404SViresh Kumar 	}
2336eed9404SViresh Kumar 
2346eed9404SViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2351da177e4SLinus Torvalds 
2363a3e9e06SViresh Kumar 	return policy;
237a9144436SStephen Boyd }
2381da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2391da177e4SLinus Torvalds 
24050e9c852SViresh Kumar /**
2415d094feaSRafael J. Wysocki  * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
2425d094feaSRafael J. Wysocki  * @policy: cpufreq policy returned by cpufreq_cpu_get().
24350e9c852SViresh Kumar  */
2443a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
245a9144436SStephen Boyd {
2466eed9404SViresh Kumar 	kobject_put(&policy->kobj);
247a9144436SStephen Boyd }
2481da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2491da177e4SLinus Torvalds 
250540a3758SRafael J. Wysocki /**
251540a3758SRafael J. Wysocki  * cpufreq_cpu_release - Unlock a policy and decrement its usage counter.
252540a3758SRafael J. Wysocki  * @policy: cpufreq policy returned by cpufreq_cpu_acquire().
253540a3758SRafael J. Wysocki  */
2549083e498SRafael J. Wysocki void cpufreq_cpu_release(struct cpufreq_policy *policy)
255540a3758SRafael J. Wysocki {
256540a3758SRafael J. Wysocki 	if (WARN_ON(!policy))
257540a3758SRafael J. Wysocki 		return;
258540a3758SRafael J. Wysocki 
259540a3758SRafael J. Wysocki 	lockdep_assert_held(&policy->rwsem);
260540a3758SRafael J. Wysocki 
261540a3758SRafael J. Wysocki 	up_write(&policy->rwsem);
262540a3758SRafael J. Wysocki 
263540a3758SRafael J. Wysocki 	cpufreq_cpu_put(policy);
264540a3758SRafael J. Wysocki }
265540a3758SRafael J. Wysocki 
266540a3758SRafael J. Wysocki /**
267540a3758SRafael J. Wysocki  * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it.
268540a3758SRafael J. Wysocki  * @cpu: CPU to find the policy for.
269540a3758SRafael J. Wysocki  *
270540a3758SRafael J. Wysocki  * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and
271540a3758SRafael J. Wysocki  * if the policy returned by it is not NULL, acquire its rwsem for writing.
272540a3758SRafael J. Wysocki  * Return the policy if it is active or release it and return NULL otherwise.
273540a3758SRafael J. Wysocki  *
274540a3758SRafael J. Wysocki  * The policy returned by this function has to be released with the help of
275540a3758SRafael J. Wysocki  * cpufreq_cpu_release() in order to release its rwsem and balance its usage
276540a3758SRafael J. Wysocki  * counter properly.
277540a3758SRafael J. Wysocki  */
2789083e498SRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu)
279540a3758SRafael J. Wysocki {
280540a3758SRafael J. Wysocki 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
281540a3758SRafael J. Wysocki 
282540a3758SRafael J. Wysocki 	if (!policy)
283540a3758SRafael J. Wysocki 		return NULL;
284540a3758SRafael J. Wysocki 
285540a3758SRafael J. Wysocki 	down_write(&policy->rwsem);
286540a3758SRafael J. Wysocki 
287540a3758SRafael J. Wysocki 	if (policy_is_inactive(policy)) {
288540a3758SRafael J. Wysocki 		cpufreq_cpu_release(policy);
289540a3758SRafael J. Wysocki 		return NULL;
290540a3758SRafael J. Wysocki 	}
291540a3758SRafael J. Wysocki 
292540a3758SRafael J. Wysocki 	return policy;
293540a3758SRafael J. Wysocki }
294540a3758SRafael J. Wysocki 
2951da177e4SLinus Torvalds /*********************************************************************
2961da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2971da177e4SLinus Torvalds  *********************************************************************/
2981da177e4SLinus Torvalds 
299ec06e586SRafael J. Wysocki /**
300ec06e586SRafael J. Wysocki  * adjust_jiffies - Adjust the system "loops_per_jiffy".
301ec06e586SRafael J. Wysocki  * @val: CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
302ec06e586SRafael J. Wysocki  * @ci: Frequency change information.
3031da177e4SLinus Torvalds  *
3041da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
3051da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
3061da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
3071da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
3081da177e4SLinus Torvalds  */
30939c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
31039c132eeSViresh Kumar {
3111da177e4SLinus Torvalds #ifndef CONFIG_SMP
3121da177e4SLinus Torvalds 	static unsigned long l_p_j_ref;
3131da177e4SLinus Torvalds 	static unsigned int l_p_j_ref_freq;
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
3161da177e4SLinus Torvalds 		return;
3171da177e4SLinus Torvalds 
3181da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
3191da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
3201da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
321e837f9b5SJoe Perches 		pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
322e837f9b5SJoe Perches 			 l_p_j_ref, l_p_j_ref_freq);
3231da177e4SLinus Torvalds 	}
3240b443eadSViresh Kumar 	if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
325e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
326e08f5f5bSGautham R Shenoy 								ci->new);
327e837f9b5SJoe Perches 		pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
328e837f9b5SJoe Perches 			 loops_per_jiffy, ci->new);
3291da177e4SLinus Torvalds 	}
3301da177e4SLinus Torvalds #endif
33139c132eeSViresh Kumar }
3321da177e4SLinus Torvalds 
33320b5324dSViresh Kumar /**
334ec06e586SRafael J. Wysocki  * cpufreq_notify_transition - Notify frequency transition and adjust jiffies.
33520b5324dSViresh Kumar  * @policy: cpufreq policy to enable fast frequency switching for.
33620b5324dSViresh Kumar  * @freqs: contain details of the frequency update.
33720b5324dSViresh Kumar  * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
33820b5324dSViresh Kumar  *
339ec06e586SRafael J. Wysocki  * This function calls the transition notifiers and adjust_jiffies().
340ec06e586SRafael J. Wysocki  *
341ec06e586SRafael J. Wysocki  * It is called twice on all CPU frequency changes that have external effects.
34220b5324dSViresh Kumar  */
34320b5324dSViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy,
34420b5324dSViresh Kumar 				      struct cpufreq_freqs *freqs,
34520b5324dSViresh Kumar 				      unsigned int state)
3461da177e4SLinus Torvalds {
347df24014aSViresh Kumar 	int cpu;
348df24014aSViresh Kumar 
3491da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
3501da177e4SLinus Torvalds 
351d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
352d5aaffa9SDirk Brandewie 		return;
353d5aaffa9SDirk Brandewie 
354df24014aSViresh Kumar 	freqs->policy = policy;
3551c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
3562d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
357e4472cb3SDave Jones 		 state, freqs->new);
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	switch (state) {
3601da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
36120b5324dSViresh Kumar 		/*
36220b5324dSViresh Kumar 		 * Detect if the driver reported a value as "old frequency"
363e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
364e4472cb3SDave Jones 		 * "old frequency".
3651da177e4SLinus Torvalds 		 */
36698015228SViresh Kumar 		if (policy->cur && policy->cur != freqs->old) {
367e837f9b5SJoe Perches 			pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
368e4472cb3SDave Jones 				 freqs->old, policy->cur);
369e4472cb3SDave Jones 			freqs->old = policy->cur;
3701da177e4SLinus Torvalds 		}
37120b5324dSViresh Kumar 
372b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
373e4472cb3SDave Jones 					 CPUFREQ_PRECHANGE, freqs);
37420b5324dSViresh Kumar 
3751da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3761da177e4SLinus Torvalds 		break;
377e4472cb3SDave Jones 
3781da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3791da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
38020b5324dSViresh Kumar 		pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
38120b5324dSViresh Kumar 			 cpumask_pr_args(policy->cpus));
38220b5324dSViresh Kumar 
383df24014aSViresh Kumar 		for_each_cpu(cpu, policy->cpus)
384df24014aSViresh Kumar 			trace_cpu_frequency(freqs->new, cpu);
385df24014aSViresh Kumar 
386b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
387e4472cb3SDave Jones 					 CPUFREQ_POSTCHANGE, freqs);
388bb176f7dSViresh Kumar 
38920b5324dSViresh Kumar 		cpufreq_stats_record_transition(policy, freqs->new);
39020b5324dSViresh Kumar 		policy->cur = freqs->new;
39120b5324dSViresh Kumar 	}
392b43a7ffbSViresh Kumar }
3931da177e4SLinus Torvalds 
394f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */
395236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
396f7ba3b41SViresh Kumar 		struct cpufreq_freqs *freqs, int transition_failed)
397f7ba3b41SViresh Kumar {
398f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
399f7ba3b41SViresh Kumar 	if (!transition_failed)
400f7ba3b41SViresh Kumar 		return;
401f7ba3b41SViresh Kumar 
402f7ba3b41SViresh Kumar 	swap(freqs->old, freqs->new);
403f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
404f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
405f7ba3b41SViresh Kumar }
406f7ba3b41SViresh Kumar 
40712478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
40812478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs)
40912478cf0SSrivatsa S. Bhat {
410ca654dc3SSrivatsa S. Bhat 
411ca654dc3SSrivatsa S. Bhat 	/*
412ca654dc3SSrivatsa S. Bhat 	 * Catch double invocations of _begin() which lead to self-deadlock.
413ca654dc3SSrivatsa S. Bhat 	 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
414ca654dc3SSrivatsa S. Bhat 	 * doesn't invoke _begin() on their behalf, and hence the chances of
415ca654dc3SSrivatsa S. Bhat 	 * double invocations are very low. Moreover, there are scenarios
416ca654dc3SSrivatsa S. Bhat 	 * where these checks can emit false-positive warnings in these
417ca654dc3SSrivatsa S. Bhat 	 * drivers; so we avoid that by skipping them altogether.
418ca654dc3SSrivatsa S. Bhat 	 */
419ca654dc3SSrivatsa S. Bhat 	WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
420ca654dc3SSrivatsa S. Bhat 				&& current == policy->transition_task);
421ca654dc3SSrivatsa S. Bhat 
42212478cf0SSrivatsa S. Bhat wait:
42312478cf0SSrivatsa S. Bhat 	wait_event(policy->transition_wait, !policy->transition_ongoing);
42412478cf0SSrivatsa S. Bhat 
42512478cf0SSrivatsa S. Bhat 	spin_lock(&policy->transition_lock);
42612478cf0SSrivatsa S. Bhat 
42712478cf0SSrivatsa S. Bhat 	if (unlikely(policy->transition_ongoing)) {
42812478cf0SSrivatsa S. Bhat 		spin_unlock(&policy->transition_lock);
42912478cf0SSrivatsa S. Bhat 		goto wait;
43012478cf0SSrivatsa S. Bhat 	}
43112478cf0SSrivatsa S. Bhat 
43212478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = true;
433ca654dc3SSrivatsa S. Bhat 	policy->transition_task = current;
43412478cf0SSrivatsa S. Bhat 
43512478cf0SSrivatsa S. Bhat 	spin_unlock(&policy->transition_lock);
43612478cf0SSrivatsa S. Bhat 
43712478cf0SSrivatsa S. Bhat 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
43812478cf0SSrivatsa S. Bhat }
43912478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
44012478cf0SSrivatsa S. Bhat 
44112478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
44212478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs, int transition_failed)
44312478cf0SSrivatsa S. Bhat {
4440e7ea2f3SIgor Stoppa 	if (WARN_ON(!policy->transition_ongoing))
44512478cf0SSrivatsa S. Bhat 		return;
44612478cf0SSrivatsa S. Bhat 
44712478cf0SSrivatsa S. Bhat 	cpufreq_notify_post_transition(policy, freqs, transition_failed);
44812478cf0SSrivatsa S. Bhat 
4491a0419b0SIonela Voinescu 	arch_set_freq_scale(policy->related_cpus,
4501a0419b0SIonela Voinescu 			    policy->cur,
4511a0419b0SIonela Voinescu 			    policy->cpuinfo.max_freq);
4521a0419b0SIonela Voinescu 
45312478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = false;
454ca654dc3SSrivatsa S. Bhat 	policy->transition_task = NULL;
45512478cf0SSrivatsa S. Bhat 
45612478cf0SSrivatsa S. Bhat 	wake_up(&policy->transition_wait);
45712478cf0SSrivatsa S. Bhat }
45812478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
45912478cf0SSrivatsa S. Bhat 
460b7898fdaSRafael J. Wysocki /*
461b7898fdaSRafael J. Wysocki  * Fast frequency switching status count.  Positive means "enabled", negative
462b7898fdaSRafael J. Wysocki  * means "disabled" and 0 means "not decided yet".
463b7898fdaSRafael J. Wysocki  */
464b7898fdaSRafael J. Wysocki static int cpufreq_fast_switch_count;
465b7898fdaSRafael J. Wysocki static DEFINE_MUTEX(cpufreq_fast_switch_lock);
466b7898fdaSRafael J. Wysocki 
467b7898fdaSRafael J. Wysocki static void cpufreq_list_transition_notifiers(void)
468b7898fdaSRafael J. Wysocki {
469b7898fdaSRafael J. Wysocki 	struct notifier_block *nb;
470b7898fdaSRafael J. Wysocki 
471b7898fdaSRafael J. Wysocki 	pr_info("Registered transition notifiers:\n");
472b7898fdaSRafael J. Wysocki 
473b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_transition_notifier_list.mutex);
474b7898fdaSRafael J. Wysocki 
475b7898fdaSRafael J. Wysocki 	for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
476d75f773cSSakari Ailus 		pr_info("%pS\n", nb->notifier_call);
477b7898fdaSRafael J. Wysocki 
478b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_transition_notifier_list.mutex);
479b7898fdaSRafael J. Wysocki }
480b7898fdaSRafael J. Wysocki 
481b7898fdaSRafael J. Wysocki /**
482b7898fdaSRafael J. Wysocki  * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
483b7898fdaSRafael J. Wysocki  * @policy: cpufreq policy to enable fast frequency switching for.
484b7898fdaSRafael J. Wysocki  *
485b7898fdaSRafael J. Wysocki  * Try to enable fast frequency switching for @policy.
486b7898fdaSRafael J. Wysocki  *
487b7898fdaSRafael J. Wysocki  * The attempt will fail if there is at least one transition notifier registered
488b7898fdaSRafael J. Wysocki  * at this point, as fast frequency switching is quite fundamentally at odds
489b7898fdaSRafael J. Wysocki  * with transition notifiers.  Thus if successful, it will make registration of
490b7898fdaSRafael J. Wysocki  * transition notifiers fail going forward.
491b7898fdaSRafael J. Wysocki  */
492b7898fdaSRafael J. Wysocki void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
493b7898fdaSRafael J. Wysocki {
494b7898fdaSRafael J. Wysocki 	lockdep_assert_held(&policy->rwsem);
495b7898fdaSRafael J. Wysocki 
496b7898fdaSRafael J. Wysocki 	if (!policy->fast_switch_possible)
497b7898fdaSRafael J. Wysocki 		return;
498b7898fdaSRafael J. Wysocki 
499b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_fast_switch_lock);
500b7898fdaSRafael J. Wysocki 	if (cpufreq_fast_switch_count >= 0) {
501b7898fdaSRafael J. Wysocki 		cpufreq_fast_switch_count++;
502b7898fdaSRafael J. Wysocki 		policy->fast_switch_enabled = true;
503b7898fdaSRafael J. Wysocki 	} else {
504b7898fdaSRafael J. Wysocki 		pr_warn("CPU%u: Fast frequency switching not enabled\n",
505b7898fdaSRafael J. Wysocki 			policy->cpu);
506b7898fdaSRafael J. Wysocki 		cpufreq_list_transition_notifiers();
507b7898fdaSRafael J. Wysocki 	}
508b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_fast_switch_lock);
509b7898fdaSRafael J. Wysocki }
510b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
511b7898fdaSRafael J. Wysocki 
5126c9d9c81SRafael J. Wysocki /**
5136c9d9c81SRafael J. Wysocki  * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
5146c9d9c81SRafael J. Wysocki  * @policy: cpufreq policy to disable fast frequency switching for.
5156c9d9c81SRafael J. Wysocki  */
5166c9d9c81SRafael J. Wysocki void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
517b7898fdaSRafael J. Wysocki {
518b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_fast_switch_lock);
519b7898fdaSRafael J. Wysocki 	if (policy->fast_switch_enabled) {
520b7898fdaSRafael J. Wysocki 		policy->fast_switch_enabled = false;
521b7898fdaSRafael J. Wysocki 		if (!WARN_ON(cpufreq_fast_switch_count <= 0))
522b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count--;
523b7898fdaSRafael J. Wysocki 	}
524b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_fast_switch_lock);
525b7898fdaSRafael J. Wysocki }
5266c9d9c81SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
5271da177e4SLinus Torvalds 
528f9ccdec2SViresh Kumar static unsigned int __resolve_freq(struct cpufreq_policy *policy,
529f9ccdec2SViresh Kumar 		unsigned int target_freq, unsigned int relation)
530f9ccdec2SViresh Kumar {
531f9ccdec2SViresh Kumar 	unsigned int idx;
532f9ccdec2SViresh Kumar 
533b3beca76SViresh Kumar 	target_freq = clamp_val(target_freq, policy->min, policy->max);
534f9ccdec2SViresh Kumar 
535b3beca76SViresh Kumar 	if (!cpufreq_driver->target_index)
536f9ccdec2SViresh Kumar 		return target_freq;
537b3beca76SViresh Kumar 
538b3beca76SViresh Kumar 	idx = cpufreq_frequency_table_target(policy, target_freq, relation);
539b3beca76SViresh Kumar 	policy->cached_resolved_idx = idx;
540b3beca76SViresh Kumar 	policy->cached_target_freq = target_freq;
541b3beca76SViresh Kumar 	return policy->freq_table[idx].frequency;
542f9ccdec2SViresh Kumar }
543f9ccdec2SViresh Kumar 
544e3c06236SSteve Muckle /**
545e3c06236SSteve Muckle  * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
546e3c06236SSteve Muckle  * one.
547a9909c21SLee Jones  * @policy: associated policy to interrogate
548e3c06236SSteve Muckle  * @target_freq: target frequency to resolve.
549e3c06236SSteve Muckle  *
550e3c06236SSteve Muckle  * The target to driver frequency mapping is cached in the policy.
551e3c06236SSteve Muckle  *
552e3c06236SSteve Muckle  * Return: Lowest driver-supported frequency greater than or equal to the
553e3c06236SSteve Muckle  * given target_freq, subject to policy (min/max) and driver limitations.
554e3c06236SSteve Muckle  */
555e3c06236SSteve Muckle unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
556e3c06236SSteve Muckle 					 unsigned int target_freq)
557e3c06236SSteve Muckle {
558b894d20eSVincent Donnefort 	return __resolve_freq(policy, target_freq, CPUFREQ_RELATION_LE);
559e3c06236SSteve Muckle }
560ae2c1ca6SSteve Muckle EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
561e3c06236SSteve Muckle 
562aa7519afSViresh Kumar unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
563aa7519afSViresh Kumar {
564aa7519afSViresh Kumar 	unsigned int latency;
565aa7519afSViresh Kumar 
566aa7519afSViresh Kumar 	if (policy->transition_delay_us)
567aa7519afSViresh Kumar 		return policy->transition_delay_us;
568aa7519afSViresh Kumar 
569aa7519afSViresh Kumar 	latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
570e948bc8fSViresh Kumar 	if (latency) {
571e948bc8fSViresh Kumar 		/*
572e948bc8fSViresh Kumar 		 * For platforms that can change the frequency very fast (< 10
573e948bc8fSViresh Kumar 		 * us), the above formula gives a decent transition delay. But
574e948bc8fSViresh Kumar 		 * for platforms where transition_latency is in milliseconds, it
575e948bc8fSViresh Kumar 		 * ends up giving unrealistic values.
576e948bc8fSViresh Kumar 		 *
577e948bc8fSViresh Kumar 		 * Cap the default transition delay to 10 ms, which seems to be
578e948bc8fSViresh Kumar 		 * a reasonable amount of time after which we should reevaluate
579e948bc8fSViresh Kumar 		 * the frequency.
580e948bc8fSViresh Kumar 		 */
581e948bc8fSViresh Kumar 		return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
582e948bc8fSViresh Kumar 	}
583aa7519afSViresh Kumar 
584aa7519afSViresh Kumar 	return LATENCY_MULTIPLIER;
585aa7519afSViresh Kumar }
586aa7519afSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
587aa7519afSViresh Kumar 
5881da177e4SLinus Torvalds /*********************************************************************
5891da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
5901da177e4SLinus Torvalds  *********************************************************************/
5918a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj,
592625c85a6SViresh Kumar 			  struct kobj_attribute *attr, char *buf)
5936f19efc0SLukasz Majewski {
5946f19efc0SLukasz Majewski 	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
5956f19efc0SLukasz Majewski }
5966f19efc0SLukasz Majewski 
597625c85a6SViresh Kumar static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
5986f19efc0SLukasz Majewski 			   const char *buf, size_t count)
5996f19efc0SLukasz Majewski {
6006f19efc0SLukasz Majewski 	int ret, enable;
6016f19efc0SLukasz Majewski 
6026f19efc0SLukasz Majewski 	ret = sscanf(buf, "%d", &enable);
6036f19efc0SLukasz Majewski 	if (ret != 1 || enable < 0 || enable > 1)
6046f19efc0SLukasz Majewski 		return -EINVAL;
6056f19efc0SLukasz Majewski 
6066f19efc0SLukasz Majewski 	if (cpufreq_boost_trigger_state(enable)) {
607e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST!\n",
608e837f9b5SJoe Perches 		       __func__, enable ? "enable" : "disable");
6096f19efc0SLukasz Majewski 		return -EINVAL;
6106f19efc0SLukasz Majewski 	}
6116f19efc0SLukasz Majewski 
612e837f9b5SJoe Perches 	pr_debug("%s: cpufreq BOOST %s\n",
613e837f9b5SJoe Perches 		 __func__, enable ? "enabled" : "disabled");
6146f19efc0SLukasz Majewski 
6156f19efc0SLukasz Majewski 	return count;
6166f19efc0SLukasz Majewski }
6176f19efc0SLukasz Majewski define_one_global_rw(boost);
6181da177e4SLinus Torvalds 
61942f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor)
6203bcb09a3SJeremy Fitzhardinge {
6213bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
6223bcb09a3SJeremy Fitzhardinge 
623f7b27061SViresh Kumar 	for_each_governor(t)
6247c4f4539SRasmus Villemoes 		if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
6253bcb09a3SJeremy Fitzhardinge 			return t;
6263bcb09a3SJeremy Fitzhardinge 
6273bcb09a3SJeremy Fitzhardinge 	return NULL;
6283bcb09a3SJeremy Fitzhardinge }
6293bcb09a3SJeremy Fitzhardinge 
6308cc46ae5SViresh Kumar static struct cpufreq_governor *get_governor(const char *str_governor)
6318cc46ae5SViresh Kumar {
6328cc46ae5SViresh Kumar 	struct cpufreq_governor *t;
6338cc46ae5SViresh Kumar 
6348cc46ae5SViresh Kumar 	mutex_lock(&cpufreq_governor_mutex);
6358cc46ae5SViresh Kumar 	t = find_governor(str_governor);
6368cc46ae5SViresh Kumar 	if (!t)
6378cc46ae5SViresh Kumar 		goto unlock;
6388cc46ae5SViresh Kumar 
6398cc46ae5SViresh Kumar 	if (!try_module_get(t->owner))
6408cc46ae5SViresh Kumar 		t = NULL;
6418cc46ae5SViresh Kumar 
6428cc46ae5SViresh Kumar unlock:
6438cc46ae5SViresh Kumar 	mutex_unlock(&cpufreq_governor_mutex);
6448cc46ae5SViresh Kumar 
6458cc46ae5SViresh Kumar 	return t;
6468cc46ae5SViresh Kumar }
6478cc46ae5SViresh Kumar 
6481e4f63aeSRafael J. Wysocki static unsigned int cpufreq_parse_policy(char *str_governor)
6491da177e4SLinus Torvalds {
6501e4f63aeSRafael J. Wysocki 	if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN))
6511e4f63aeSRafael J. Wysocki 		return CPUFREQ_POLICY_PERFORMANCE;
6521e4f63aeSRafael J. Wysocki 
6531e4f63aeSRafael J. Wysocki 	if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN))
6541e4f63aeSRafael J. Wysocki 		return CPUFREQ_POLICY_POWERSAVE;
6551e4f63aeSRafael J. Wysocki 
6561e4f63aeSRafael J. Wysocki 	return CPUFREQ_POLICY_UNKNOWN;
657ab05d97aSYue Hu }
658ab05d97aSYue Hu 
659ab05d97aSYue Hu /**
6605ddc6d4eSViresh Kumar  * cpufreq_parse_governor - parse a governor string only for has_target()
6611e4f63aeSRafael J. Wysocki  * @str_governor: Governor name.
662ab05d97aSYue Hu  */
6631e4f63aeSRafael J. Wysocki static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor)
664ab05d97aSYue Hu {
6651da177e4SLinus Torvalds 	struct cpufreq_governor *t;
6663bcb09a3SJeremy Fitzhardinge 
6678cc46ae5SViresh Kumar 	t = get_governor(str_governor);
6688cc46ae5SViresh Kumar 	if (t)
6698cc46ae5SViresh Kumar 		return t;
6703bcb09a3SJeremy Fitzhardinge 
6718cc46ae5SViresh Kumar 	if (request_module("cpufreq_%s", str_governor))
6721e4f63aeSRafael J. Wysocki 		return NULL;
673045149e6SRafael J. Wysocki 
6748cc46ae5SViresh Kumar 	return get_governor(str_governor);
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
677a9909c21SLee Jones /*
678e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
679e08f5f5bSGautham R Shenoy  * print out cpufreq information
6801da177e4SLinus Torvalds  *
6811da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
6821da177e4SLinus Torvalds  * "unsigned int".
6831da177e4SLinus Torvalds  */
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds #define show_one(file_name, object)			\
6861da177e4SLinus Torvalds static ssize_t show_##file_name				\
6871da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
6881da177e4SLinus Torvalds {							\
6891da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
6931da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
694ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
6951da177e4SLinus Torvalds show_one(scaling_min_freq, min);
6961da177e4SLinus Torvalds show_one(scaling_max_freq, max);
697c034b02eSDirk Brandewie 
698f8475cefSLen Brown __weak unsigned int arch_freq_get_on_cpu(int cpu)
699f8475cefSLen Brown {
700f8475cefSLen Brown 	return 0;
701f8475cefSLen Brown }
702f8475cefSLen Brown 
70309347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
704c034b02eSDirk Brandewie {
705c034b02eSDirk Brandewie 	ssize_t ret;
706f8475cefSLen Brown 	unsigned int freq;
707c034b02eSDirk Brandewie 
708f8475cefSLen Brown 	freq = arch_freq_get_on_cpu(policy->cpu);
709f8475cefSLen Brown 	if (freq)
710f8475cefSLen Brown 		ret = sprintf(buf, "%u\n", freq);
711681fe684SViresh Kumar 	else if (cpufreq_driver->setpolicy && cpufreq_driver->get)
712c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
713c034b02eSDirk Brandewie 	else
714c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", policy->cur);
715c034b02eSDirk Brandewie 	return ret;
716c034b02eSDirk Brandewie }
7171da177e4SLinus Torvalds 
718a9909c21SLee Jones /*
7191da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
7201da177e4SLinus Torvalds  */
7211da177e4SLinus Torvalds #define store_one(file_name, object)			\
7221da177e4SLinus Torvalds static ssize_t store_##file_name					\
7231da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
7241da177e4SLinus Torvalds {									\
72518c49926SViresh Kumar 	unsigned long val;						\
72618c49926SViresh Kumar 	int ret;							\
7271da177e4SLinus Torvalds 									\
72818c49926SViresh Kumar 	ret = sscanf(buf, "%lu", &val);					\
7291da177e4SLinus Torvalds 	if (ret != 1)							\
7301da177e4SLinus Torvalds 		return -EINVAL;						\
7311da177e4SLinus Torvalds 									\
7323000ce3cSRafael J. Wysocki 	ret = freq_qos_update_request(policy->object##_freq_req, val);\
73318c49926SViresh Kumar 	return ret >= 0 ? count : ret;					\
7341da177e4SLinus Torvalds }
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds store_one(scaling_min_freq, min);
7371da177e4SLinus Torvalds store_one(scaling_max_freq, max);
7381da177e4SLinus Torvalds 
739a9909c21SLee Jones /*
7401da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
7411da177e4SLinus Torvalds  */
742e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
743e08f5f5bSGautham R Shenoy 					char *buf)
7441da177e4SLinus Torvalds {
745d92d50a4SViresh Kumar 	unsigned int cur_freq = __cpufreq_get(policy);
7469b4f603eSRafael J. Wysocki 
7479b4f603eSRafael J. Wysocki 	if (cur_freq)
7481da177e4SLinus Torvalds 		return sprintf(buf, "%u\n", cur_freq);
7499b4f603eSRafael J. Wysocki 
7509b4f603eSRafael J. Wysocki 	return sprintf(buf, "<unknown>\n");
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
753a9909c21SLee Jones /*
7541da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
7551da177e4SLinus Torvalds  */
756905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
7571da177e4SLinus Torvalds {
7581da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
7591da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
7601da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
7611da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
7621da177e4SLinus Torvalds 	else if (policy->governor)
7634b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
76429464f28SDave Jones 				policy->governor->name);
7651da177e4SLinus Torvalds 	return -EINVAL;
7661da177e4SLinus Torvalds }
7671da177e4SLinus Torvalds 
768a9909c21SLee Jones /*
7691da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
7701da177e4SLinus Torvalds  */
7711da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
7721da177e4SLinus Torvalds 					const char *buf, size_t count)
7731da177e4SLinus Torvalds {
7741da177e4SLinus Torvalds 	char str_governor[16];
7751e4f63aeSRafael J. Wysocki 	int ret;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
7781da177e4SLinus Torvalds 	if (ret != 1)
7791da177e4SLinus Torvalds 		return -EINVAL;
7801da177e4SLinus Torvalds 
781ab05d97aSYue Hu 	if (cpufreq_driver->setpolicy) {
7821e4f63aeSRafael J. Wysocki 		unsigned int new_pol;
7831e4f63aeSRafael J. Wysocki 
7841e4f63aeSRafael J. Wysocki 		new_pol = cpufreq_parse_policy(str_governor);
7851e4f63aeSRafael J. Wysocki 		if (!new_pol)
786ab05d97aSYue Hu 			return -EINVAL;
7871e4f63aeSRafael J. Wysocki 
7881e4f63aeSRafael J. Wysocki 		ret = cpufreq_set_policy(policy, NULL, new_pol);
789ab05d97aSYue Hu 	} else {
7901e4f63aeSRafael J. Wysocki 		struct cpufreq_governor *new_gov;
7911e4f63aeSRafael J. Wysocki 
7921e4f63aeSRafael J. Wysocki 		new_gov = cpufreq_parse_governor(str_governor);
7931e4f63aeSRafael J. Wysocki 		if (!new_gov)
7941da177e4SLinus Torvalds 			return -EINVAL;
7951e4f63aeSRafael J. Wysocki 
7961e4f63aeSRafael J. Wysocki 		ret = cpufreq_set_policy(policy, new_gov,
7971e4f63aeSRafael J. Wysocki 					 CPUFREQ_POLICY_UNKNOWN);
7981e4f63aeSRafael J. Wysocki 
7991e4f63aeSRafael J. Wysocki 		module_put(new_gov->owner);
800ab05d97aSYue Hu 	}
8011da177e4SLinus Torvalds 
80288dc4384SViresh Kumar 	return ret ? ret : count;
8031da177e4SLinus Torvalds }
8041da177e4SLinus Torvalds 
805a9909c21SLee Jones /*
8061da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
8071da177e4SLinus Torvalds  */
8081da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
8091da177e4SLinus Torvalds {
8101c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
8111da177e4SLinus Torvalds }
8121da177e4SLinus Torvalds 
813a9909c21SLee Jones /*
8141da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
8151da177e4SLinus Torvalds  */
8161da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
8171da177e4SLinus Torvalds 						char *buf)
8181da177e4SLinus Torvalds {
8191da177e4SLinus Torvalds 	ssize_t i = 0;
8201da177e4SLinus Torvalds 	struct cpufreq_governor *t;
8211da177e4SLinus Torvalds 
8229c0ebcf7SViresh Kumar 	if (!has_target()) {
8231da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
8241da177e4SLinus Torvalds 		goto out;
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds 
8278cc46ae5SViresh Kumar 	mutex_lock(&cpufreq_governor_mutex);
828f7b27061SViresh Kumar 	for_each_governor(t) {
82929464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
83029464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
8318cc46ae5SViresh Kumar 			break;
8324b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
8331da177e4SLinus Torvalds 	}
8348cc46ae5SViresh Kumar 	mutex_unlock(&cpufreq_governor_mutex);
8351da177e4SLinus Torvalds out:
8361da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
8371da177e4SLinus Torvalds 	return i;
8381da177e4SLinus Torvalds }
839e8628dd0SDarrick J. Wong 
840f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
8411da177e4SLinus Torvalds {
8421da177e4SLinus Torvalds 	ssize_t i = 0;
8431da177e4SLinus Torvalds 	unsigned int cpu;
8441da177e4SLinus Torvalds 
845835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
8461da177e4SLinus Torvalds 		if (i)
8471da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
8481da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
8491da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
8501da177e4SLinus Torvalds 			break;
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
8531da177e4SLinus Torvalds 	return i;
8541da177e4SLinus Torvalds }
855f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
8561da177e4SLinus Torvalds 
857a9909c21SLee Jones /*
858e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
859e8628dd0SDarrick J. Wong  * hw coordination is in use
860e8628dd0SDarrick J. Wong  */
861e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
862e8628dd0SDarrick J. Wong {
863f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
864e8628dd0SDarrick J. Wong }
865e8628dd0SDarrick J. Wong 
866a9909c21SLee Jones /*
867e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
868e8628dd0SDarrick J. Wong  */
869e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
870e8628dd0SDarrick J. Wong {
871f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
872e8628dd0SDarrick J. Wong }
873e8628dd0SDarrick J. Wong 
8749e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
8759e76988eSVenki Pallipadi 					const char *buf, size_t count)
8769e76988eSVenki Pallipadi {
8779e76988eSVenki Pallipadi 	unsigned int freq = 0;
8789e76988eSVenki Pallipadi 	unsigned int ret;
8799e76988eSVenki Pallipadi 
880879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
8819e76988eSVenki Pallipadi 		return -EINVAL;
8829e76988eSVenki Pallipadi 
8839e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
8849e76988eSVenki Pallipadi 	if (ret != 1)
8859e76988eSVenki Pallipadi 		return -EINVAL;
8869e76988eSVenki Pallipadi 
8879e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
8889e76988eSVenki Pallipadi 
8899e76988eSVenki Pallipadi 	return count;
8909e76988eSVenki Pallipadi }
8919e76988eSVenki Pallipadi 
8929e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
8939e76988eSVenki Pallipadi {
894879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
8959e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
8969e76988eSVenki Pallipadi 
8979e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
8989e76988eSVenki Pallipadi }
8991da177e4SLinus Torvalds 
900a9909c21SLee Jones /*
9018bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
902e2f74f35SThomas Renninger  */
903e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
904e2f74f35SThomas Renninger {
905e2f74f35SThomas Renninger 	unsigned int limit;
906e2f74f35SThomas Renninger 	int ret;
9071c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
908e2f74f35SThomas Renninger 	if (!ret)
909e2f74f35SThomas Renninger 		return sprintf(buf, "%u\n", limit);
910e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
911e2f74f35SThomas Renninger }
912e2f74f35SThomas Renninger 
9136dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
9146dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
9156dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
9166dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
9176dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
9186dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
9196dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
9206dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
9216dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
9226dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
9236dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
9246dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
9256dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
9266dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
9271da177e4SLinus Torvalds 
928fe262d5cSGreg Kroah-Hartman static struct attribute *cpufreq_attrs[] = {
9291da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
9301da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
931ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
9321da177e4SLinus Torvalds 	&scaling_min_freq.attr,
9331da177e4SLinus Torvalds 	&scaling_max_freq.attr,
9341da177e4SLinus Torvalds 	&affected_cpus.attr,
935e8628dd0SDarrick J. Wong 	&related_cpus.attr,
9361da177e4SLinus Torvalds 	&scaling_governor.attr,
9371da177e4SLinus Torvalds 	&scaling_driver.attr,
9381da177e4SLinus Torvalds 	&scaling_available_governors.attr,
9399e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
9401da177e4SLinus Torvalds 	NULL
9411da177e4SLinus Torvalds };
942fe262d5cSGreg Kroah-Hartman ATTRIBUTE_GROUPS(cpufreq);
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,
1005fe262d5cSGreg Kroah-Hartman 	.default_groups	= cpufreq_groups,
10061da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
10071da177e4SLinus Torvalds };
10081da177e4SLinus Torvalds 
10092c1b5a84SXiongfeng Wang static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu,
10102c1b5a84SXiongfeng Wang 				struct device *dev)
101187549141SViresh Kumar {
101267d874c3SViresh Kumar 	if (unlikely(!dev))
10132f0ba790SRafael J. Wysocki 		return;
10142f0ba790SRafael J. Wysocki 
10152f0ba790SRafael J. Wysocki 	if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
10162f0ba790SRafael J. Wysocki 		return;
10172f0ba790SRafael J. Wysocki 
101826619804SViresh Kumar 	dev_dbg(dev, "%s: Adding symlink\n", __func__);
10192f0ba790SRafael J. Wysocki 	if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
10202f0ba790SRafael J. Wysocki 		dev_err(dev, "cpufreq symlink creation failed\n");
102187549141SViresh Kumar }
102287549141SViresh Kumar 
1023*5c84c1b8SViresh Kumar static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu,
102426619804SViresh Kumar 				   struct device *dev)
102587549141SViresh Kumar {
102626619804SViresh Kumar 	dev_dbg(dev, "%s: Removing symlink\n", __func__);
102726619804SViresh Kumar 	sysfs_remove_link(&dev->kobj, "cpufreq");
1028*5c84c1b8SViresh Kumar 	cpumask_clear_cpu(cpu, policy->real_cpus);
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 		/*
13011e81d3e0STang Yizhou 		 * Remove max_freq_req after sending CPUFREQ_REMOVE_POLICY
13021e81d3e0STang Yizhou 		 * notification, since CPUFREQ_CREATE_POLICY notification was
13031e81d3e0STang Yizhou 		 * sent after adding max_freq_req earlier.
13046a149036SViresh Kumar 		 */
13056a149036SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
13066a149036SViresh Kumar 					     CPUFREQ_REMOVE_POLICY, policy);
13073000ce3cSRafael J. Wysocki 		freq_qos_remove_request(policy->max_freq_req);
13086a149036SViresh Kumar 	}
13096a149036SViresh Kumar 
13103000ce3cSRafael J. Wysocki 	freq_qos_remove_request(policy->min_freq_req);
131118c49926SViresh Kumar 	kfree(policy->min_freq_req);
131267d874c3SViresh Kumar 
1313f9f41e3eSViresh Kumar 	cpufreq_policy_put_kobj(policy);
1314559ed407SRafael J. Wysocki 	free_cpumask_var(policy->real_cpus);
1315e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
1316e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1317e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1318e9698cc5SSrivatsa S. Bhat }
1319e9698cc5SSrivatsa S. Bhat 
13200b275352SRafael J. Wysocki static int cpufreq_online(unsigned int cpu)
13211da177e4SLinus Torvalds {
13227f0c020aSViresh Kumar 	struct cpufreq_policy *policy;
1323194d99c7SRafael J. Wysocki 	bool new_policy;
13241da177e4SLinus Torvalds 	unsigned long flags;
13250b275352SRafael J. Wysocki 	unsigned int j;
13260b275352SRafael J. Wysocki 	int ret;
1327c32b6b8eSAshok Raj 
13280b275352SRafael J. Wysocki 	pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
13296eed9404SViresh Kumar 
1330bb29ae15SViresh Kumar 	/* Check if this CPU already has a policy to manage it */
13319104bb26SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
133211ce707eSRafael J. Wysocki 	if (policy) {
13339104bb26SViresh Kumar 		WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
133411ce707eSRafael J. Wysocki 		if (!policy_is_inactive(policy))
1335d9612a49SRafael J. Wysocki 			return cpufreq_add_policy_cpu(policy, cpu);
13361da177e4SLinus Torvalds 
133711ce707eSRafael J. Wysocki 		/* This is the only online CPU for the policy.  Start over. */
1338194d99c7SRafael J. Wysocki 		new_policy = false;
133911ce707eSRafael J. Wysocki 		down_write(&policy->rwsem);
134011ce707eSRafael J. Wysocki 		policy->cpu = cpu;
134111ce707eSRafael J. Wysocki 		policy->governor = NULL;
134211ce707eSRafael J. Wysocki 		up_write(&policy->rwsem);
134311ce707eSRafael J. Wysocki 	} else {
1344194d99c7SRafael J. Wysocki 		new_policy = true;
1345a34e63b1SRafael J. Wysocki 		policy = cpufreq_policy_alloc(cpu);
1346059019a3SDave Jones 		if (!policy)
1347d4d854d6SRafael J. Wysocki 			return -ENOMEM;
134872368d12SRafael J. Wysocki 	}
13490d66b91eSSrivatsa S. Bhat 
135091a12e91SViresh Kumar 	if (!new_policy && cpufreq_driver->online) {
135191a12e91SViresh Kumar 		ret = cpufreq_driver->online(policy);
135291a12e91SViresh Kumar 		if (ret) {
135391a12e91SViresh Kumar 			pr_debug("%s: %d: initialization failed\n", __func__,
135491a12e91SViresh Kumar 				 __LINE__);
135591a12e91SViresh Kumar 			goto out_exit_policy;
135691a12e91SViresh Kumar 		}
135791a12e91SViresh Kumar 
135891a12e91SViresh Kumar 		/* Recover policy->cpus using related_cpus */
135991a12e91SViresh Kumar 		cpumask_copy(policy->cpus, policy->related_cpus);
136091a12e91SViresh Kumar 	} else {
1361835481d9SRusty Russell 		cpumask_copy(policy->cpus, cpumask_of(cpu));
13621da177e4SLinus Torvalds 
136391a12e91SViresh Kumar 		/*
136491a12e91SViresh Kumar 		 * Call driver. From then on the cpufreq must be able
136591a12e91SViresh Kumar 		 * to accept all calls to ->verify and ->setpolicy for this CPU.
13661da177e4SLinus Torvalds 		 */
13671c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->init(policy);
13681da177e4SLinus Torvalds 		if (ret) {
136991a12e91SViresh Kumar 			pr_debug("%s: %d: initialization failed\n", __func__,
137091a12e91SViresh Kumar 				 __LINE__);
13718101f997SViresh Kumar 			goto out_free_policy;
13721da177e4SLinus Torvalds 		}
1373643ae6e8SViresh Kumar 
13743b718057SRafael J. Wysocki 		/*
13753b718057SRafael J. Wysocki 		 * The initialization has succeeded and the policy is online.
13763b718057SRafael J. Wysocki 		 * If there is a problem with its frequency table, take it
13773b718057SRafael J. Wysocki 		 * offline and drop it.
13783b718057SRafael J. Wysocki 		 */
1379d417e069SViresh Kumar 		ret = cpufreq_table_validate_and_sort(policy);
1380d417e069SViresh Kumar 		if (ret)
13813b718057SRafael J. Wysocki 			goto out_offline_policy;
1382d417e069SViresh Kumar 
13834d1f3a5bSRafael J. Wysocki 		/* related_cpus should at least include policy->cpus. */
13840998a03aSViresh Kumar 		cpumask_copy(policy->related_cpus, policy->cpus);
13854d1f3a5bSRafael J. Wysocki 	}
1386559ed407SRafael J. Wysocki 
138791a12e91SViresh Kumar 	down_write(&policy->rwsem);
13885a7e56a5SViresh Kumar 	/*
13895a7e56a5SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
13905a7e56a5SViresh Kumar 	 * managing offline cpus here.
13915a7e56a5SViresh Kumar 	 */
13925a7e56a5SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
13935a7e56a5SViresh Kumar 
1394194d99c7SRafael J. Wysocki 	if (new_policy) {
13952f0ba790SRafael J. Wysocki 		for_each_cpu(j, policy->related_cpus) {
1396652ed95dSViresh Kumar 			per_cpu(cpufreq_cpu_data, j) = policy;
13972c1b5a84SXiongfeng Wang 			add_cpu_dev_symlink(policy, j, get_cpu_device(j));
13982f0ba790SRafael J. Wysocki 		}
139918c49926SViresh Kumar 
140018c49926SViresh Kumar 		policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req),
140118c49926SViresh Kumar 					       GFP_KERNEL);
1402b96f0384SWang ShaoBo 		if (!policy->min_freq_req) {
1403b96f0384SWang ShaoBo 			ret = -ENOMEM;
140418c49926SViresh Kumar 			goto out_destroy_policy;
1405b96f0384SWang ShaoBo 		}
140618c49926SViresh Kumar 
14073000ce3cSRafael J. Wysocki 		ret = freq_qos_add_request(&policy->constraints,
14083000ce3cSRafael J. Wysocki 					   policy->min_freq_req, FREQ_QOS_MIN,
1409521223d8SRafael J. Wysocki 					   FREQ_QOS_MIN_DEFAULT_VALUE);
141018c49926SViresh Kumar 		if (ret < 0) {
141118c49926SViresh Kumar 			/*
14123000ce3cSRafael J. Wysocki 			 * So we don't call freq_qos_remove_request() for an
141318c49926SViresh Kumar 			 * uninitialized request.
141418c49926SViresh Kumar 			 */
141518c49926SViresh Kumar 			kfree(policy->min_freq_req);
141618c49926SViresh Kumar 			policy->min_freq_req = NULL;
141718c49926SViresh Kumar 			goto out_destroy_policy;
141818c49926SViresh Kumar 		}
141918c49926SViresh Kumar 
142018c49926SViresh Kumar 		/*
142118c49926SViresh Kumar 		 * This must be initialized right here to avoid calling
14223000ce3cSRafael J. Wysocki 		 * freq_qos_remove_request() on uninitialized request in case
142318c49926SViresh Kumar 		 * of errors.
142418c49926SViresh Kumar 		 */
142518c49926SViresh Kumar 		policy->max_freq_req = policy->min_freq_req + 1;
142618c49926SViresh Kumar 
14273000ce3cSRafael J. Wysocki 		ret = freq_qos_add_request(&policy->constraints,
14283000ce3cSRafael J. Wysocki 					   policy->max_freq_req, FREQ_QOS_MAX,
1429521223d8SRafael J. Wysocki 					   FREQ_QOS_MAX_DEFAULT_VALUE);
143018c49926SViresh Kumar 		if (ret < 0) {
143118c49926SViresh Kumar 			policy->max_freq_req = NULL;
143218c49926SViresh Kumar 			goto out_destroy_policy;
143318c49926SViresh Kumar 		}
14346a149036SViresh Kumar 
14356a149036SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
14366a149036SViresh Kumar 				CPUFREQ_CREATE_POLICY, policy);
1437988bed09SViresh Kumar 	}
1438652ed95dSViresh Kumar 
14395ddc6d4eSViresh Kumar 	if (cpufreq_driver->get && has_target()) {
1440da60ce9fSViresh Kumar 		policy->cur = cpufreq_driver->get(policy->cpu);
1441da60ce9fSViresh Kumar 		if (!policy->cur) {
1442b96f0384SWang ShaoBo 			ret = -EIO;
1443da60ce9fSViresh Kumar 			pr_err("%s: ->get() failed\n", __func__);
1444d417e069SViresh Kumar 			goto out_destroy_policy;
1445da60ce9fSViresh Kumar 		}
1446da60ce9fSViresh Kumar 	}
1447da60ce9fSViresh Kumar 
1448d3916691SViresh Kumar 	/*
1449d3916691SViresh Kumar 	 * Sometimes boot loaders set CPU frequency to a value outside of
1450d3916691SViresh Kumar 	 * frequency table present with cpufreq core. In such cases CPU might be
1451d3916691SViresh Kumar 	 * unstable if it has to run on that frequency for long duration of time
1452d3916691SViresh Kumar 	 * and so its better to set it to a frequency which is specified in
1453d3916691SViresh Kumar 	 * freq-table. This also makes cpufreq stats inconsistent as
1454d3916691SViresh Kumar 	 * cpufreq-stats would fail to register because current frequency of CPU
1455d3916691SViresh Kumar 	 * isn't found in freq-table.
1456d3916691SViresh Kumar 	 *
1457d3916691SViresh Kumar 	 * Because we don't want this change to effect boot process badly, we go
1458d3916691SViresh Kumar 	 * for the next freq which is >= policy->cur ('cur' must be set by now,
1459d3916691SViresh Kumar 	 * otherwise we will end up setting freq to lowest of the table as 'cur'
1460d3916691SViresh Kumar 	 * is initialized to zero).
1461d3916691SViresh Kumar 	 *
1462d3916691SViresh Kumar 	 * We are passing target-freq as "policy->cur - 1" otherwise
1463d3916691SViresh Kumar 	 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1464d3916691SViresh Kumar 	 * equal to target-freq.
1465d3916691SViresh Kumar 	 */
1466d3916691SViresh Kumar 	if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1467d3916691SViresh Kumar 	    && has_target()) {
146897148d0aSViresh Kumar 		unsigned int old_freq = policy->cur;
146997148d0aSViresh Kumar 
1470d3916691SViresh Kumar 		/* Are we running at unknown frequency ? */
147197148d0aSViresh Kumar 		ret = cpufreq_frequency_table_get_index(policy, old_freq);
1472d3916691SViresh Kumar 		if (ret == -EINVAL) {
147397148d0aSViresh Kumar 			ret = __cpufreq_driver_target(policy, old_freq - 1,
1474d3916691SViresh Kumar 						      CPUFREQ_RELATION_L);
1475d3916691SViresh Kumar 
1476d3916691SViresh Kumar 			/*
1477d3916691SViresh Kumar 			 * Reaching here after boot in a few seconds may not
1478d3916691SViresh Kumar 			 * mean that system will remain stable at "unknown"
1479d3916691SViresh Kumar 			 * frequency for longer duration. Hence, a BUG_ON().
1480d3916691SViresh Kumar 			 */
1481d3916691SViresh Kumar 			BUG_ON(ret);
148297148d0aSViresh Kumar 			pr_info("%s: CPU%d: Running at unlisted initial frequency: %u KHz, changing to: %u KHz\n",
148397148d0aSViresh Kumar 				__func__, policy->cpu, old_freq, policy->cur);
1484d3916691SViresh Kumar 		}
1485d3916691SViresh Kumar 	}
1486d3916691SViresh Kumar 
1487194d99c7SRafael J. Wysocki 	if (new_policy) {
1488d9612a49SRafael J. Wysocki 		ret = cpufreq_add_dev_interface(policy);
148919d6f7ecSDave Jones 		if (ret)
1490d417e069SViresh Kumar 			goto out_destroy_policy;
14911aefc75bSRafael J. Wysocki 
14921aefc75bSRafael J. Wysocki 		cpufreq_stats_create_table(policy);
1493c88a1f8bSLukasz Majewski 
1494c88a1f8bSLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
1495c88a1f8bSLukasz Majewski 		list_add(&policy->policy_list, &cpufreq_policy_list);
1496c88a1f8bSLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1497c17495b0SViresh Kumar 
1498c17495b0SViresh Kumar 		/*
1499c17495b0SViresh Kumar 		 * Register with the energy model before
1500c17495b0SViresh Kumar 		 * sched_cpufreq_governor_change() is called, which will result
1501c17495b0SViresh Kumar 		 * in rebuilding of the sched domains, which should only be done
1502c17495b0SViresh Kumar 		 * once the energy model is properly initialized for the policy
1503c17495b0SViresh Kumar 		 * first.
1504c17495b0SViresh Kumar 		 *
1505c17495b0SViresh Kumar 		 * Also, this should be called before the policy is registered
1506c17495b0SViresh Kumar 		 * with cooling framework.
1507c17495b0SViresh Kumar 		 */
1508c17495b0SViresh Kumar 		if (cpufreq_driver->register_em)
1509c17495b0SViresh Kumar 			cpufreq_driver->register_em(policy);
1510988bed09SViresh Kumar 	}
15118ff69732SDave Jones 
15127f0fa40fSViresh Kumar 	ret = cpufreq_init_policy(policy);
15137f0fa40fSViresh Kumar 	if (ret) {
15147f0fa40fSViresh Kumar 		pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
15157f0fa40fSViresh Kumar 		       __func__, cpu, ret);
1516d417e069SViresh Kumar 		goto out_destroy_policy;
151708fd8c1cSViresh Kumar 	}
1518e18f1682SSrivatsa S. Bhat 
15194e97b631SViresh Kumar 	up_write(&policy->rwsem);
152008fd8c1cSViresh Kumar 
1521038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
15227c45cf31SViresh Kumar 
15234f774c4aSBjorn Andersson 	/* Callback for handling stuff after policy is ready */
15244f774c4aSBjorn Andersson 	if (cpufreq_driver->ready)
15254f774c4aSBjorn Andersson 		cpufreq_driver->ready(policy);
15264f774c4aSBjorn Andersson 
1527bcc61569SDaniel Lezcano 	if (cpufreq_thermal_control_enabled(cpufreq_driver))
15285c238a8bSAmit Kucheria 		policy->cdev = of_cpufreq_cooling_register(policy);
15295c238a8bSAmit Kucheria 
15302d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
15311da177e4SLinus Torvalds 
15321da177e4SLinus Torvalds 	return 0;
15331da177e4SLinus Torvalds 
1534d417e069SViresh Kumar out_destroy_policy:
1535b24b6478SViresh Kumar 	for_each_cpu(j, policy->real_cpus)
1536*5c84c1b8SViresh Kumar 		remove_cpu_dev_symlink(policy, j, get_cpu_device(j));
1537b24b6478SViresh Kumar 
153885f0e42bSViresh Kumar 	up_write(&policy->rwsem);
153985f0e42bSViresh Kumar 
15403b718057SRafael J. Wysocki out_offline_policy:
15413b718057SRafael J. Wysocki 	if (cpufreq_driver->offline)
15423b718057SRafael J. Wysocki 		cpufreq_driver->offline(policy);
15433b718057SRafael J. Wysocki 
1544d417e069SViresh Kumar out_exit_policy:
1545da60ce9fSViresh Kumar 	if (cpufreq_driver->exit)
1546da60ce9fSViresh Kumar 		cpufreq_driver->exit(policy);
15472f0ba790SRafael J. Wysocki 
15488101f997SViresh Kumar out_free_policy:
1549f9f41e3eSViresh Kumar 	cpufreq_policy_free(policy);
15501da177e4SLinus Torvalds 	return ret;
15511da177e4SLinus Torvalds }
15521da177e4SLinus Torvalds 
15530b275352SRafael J. Wysocki /**
15540b275352SRafael J. Wysocki  * cpufreq_add_dev - the cpufreq interface for a CPU device.
15550b275352SRafael J. Wysocki  * @dev: CPU device.
15560b275352SRafael J. Wysocki  * @sif: Subsystem interface structure pointer (not used)
15570b275352SRafael J. Wysocki  */
15580b275352SRafael J. Wysocki static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
15590b275352SRafael J. Wysocki {
1560a794d613SRafael J. Wysocki 	struct cpufreq_policy *policy;
15610b275352SRafael J. Wysocki 	unsigned cpu = dev->id;
156226619804SViresh Kumar 	int ret;
15630b275352SRafael J. Wysocki 
15640b275352SRafael J. Wysocki 	dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
15650b275352SRafael J. Wysocki 
156626619804SViresh Kumar 	if (cpu_online(cpu)) {
156726619804SViresh Kumar 		ret = cpufreq_online(cpu);
156826619804SViresh Kumar 		if (ret)
156926619804SViresh Kumar 			return ret;
157026619804SViresh Kumar 	}
1571a794d613SRafael J. Wysocki 
157226619804SViresh Kumar 	/* Create sysfs link on CPU registration */
1573a794d613SRafael J. Wysocki 	policy = per_cpu(cpufreq_cpu_data, cpu);
15742f0ba790SRafael J. Wysocki 	if (policy)
15752c1b5a84SXiongfeng Wang 		add_cpu_dev_symlink(policy, cpu, dev);
15762f0ba790SRafael J. Wysocki 
1577a794d613SRafael J. Wysocki 	return 0;
15781da177e4SLinus Torvalds }
15791da177e4SLinus Torvalds 
158027622b06SSebastian Andrzej Siewior static int cpufreq_offline(unsigned int cpu)
15811da177e4SLinus Torvalds {
15823a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
158369cee714SViresh Kumar 	int ret;
15841da177e4SLinus Torvalds 
1585b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
15861da177e4SLinus Torvalds 
1587988bed09SViresh Kumar 	policy = cpufreq_cpu_get_raw(cpu);
15883a3e9e06SViresh Kumar 	if (!policy) {
1589b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
159027622b06SSebastian Andrzej Siewior 		return 0;
15911da177e4SLinus Torvalds 	}
15921da177e4SLinus Torvalds 
159349f18560SViresh Kumar 	down_write(&policy->rwsem);
159445482c70SRafael J. Wysocki 	if (has_target())
159545482c70SRafael J. Wysocki 		cpufreq_stop_governor(policy);
15961da177e4SLinus Torvalds 
15979591becbSViresh Kumar 	cpumask_clear_cpu(cpu, policy->cpus);
15984573237bSViresh Kumar 
15999591becbSViresh Kumar 	if (policy_is_inactive(policy)) {
16009591becbSViresh Kumar 		if (has_target())
16014573237bSViresh Kumar 			strncpy(policy->last_governor, policy->governor->name,
16024573237bSViresh Kumar 				CPUFREQ_NAME_LEN);
160369030dd1SSrinivas Pandruvada 		else
160469030dd1SSrinivas Pandruvada 			policy->last_policy = policy->policy;
16059591becbSViresh Kumar 	} else if (cpu == policy->cpu) {
16069591becbSViresh Kumar 		/* Nominate new CPU */
16079591becbSViresh Kumar 		policy->cpu = cpumask_any(policy->cpus);
16089591becbSViresh Kumar 	}
16091da177e4SLinus Torvalds 
16109591becbSViresh Kumar 	/* Start governor again for active policy */
16119591becbSViresh Kumar 	if (!policy_is_inactive(policy)) {
16129591becbSViresh Kumar 		if (has_target()) {
16130a300767SRafael J. Wysocki 			ret = cpufreq_start_governor(policy);
16149591becbSViresh Kumar 			if (ret)
16159591becbSViresh Kumar 				pr_err("%s: Failed to start governor\n", __func__);
16169591becbSViresh Kumar 		}
161769cee714SViresh Kumar 
161849f18560SViresh Kumar 		goto unlock;
161969cee714SViresh Kumar 	}
162069cee714SViresh Kumar 
1621bcc61569SDaniel Lezcano 	if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
16225c238a8bSAmit Kucheria 		cpufreq_cooling_unregister(policy->cdev);
16235c238a8bSAmit Kucheria 		policy->cdev = NULL;
16245c238a8bSAmit Kucheria 	}
16255c238a8bSAmit Kucheria 
162636be3418SRafael J. Wysocki 	if (has_target())
162736be3418SRafael J. Wysocki 		cpufreq_exit_governor(policy);
16282a998599SRafael J. Wysocki 
16298414809cSSrivatsa S. Bhat 	/*
163091a12e91SViresh Kumar 	 * Perform the ->offline() during light-weight tear-down, as
163191a12e91SViresh Kumar 	 * that allows fast recovery when the CPU comes back.
16328414809cSSrivatsa S. Bhat 	 */
163391a12e91SViresh Kumar 	if (cpufreq_driver->offline) {
163491a12e91SViresh Kumar 		cpufreq_driver->offline(policy);
163591a12e91SViresh Kumar 	} else if (cpufreq_driver->exit) {
16363a3e9e06SViresh Kumar 		cpufreq_driver->exit(policy);
163755582bccSSrinivas Pandruvada 		policy->freq_table = NULL;
163855582bccSSrinivas Pandruvada 	}
163949f18560SViresh Kumar 
164049f18560SViresh Kumar unlock:
164149f18560SViresh Kumar 	up_write(&policy->rwsem);
164227622b06SSebastian Andrzej Siewior 	return 0;
16431da177e4SLinus Torvalds }
16441da177e4SLinus Torvalds 
1645a9909c21SLee Jones /*
164627a862e9SViresh Kumar  * cpufreq_remove_dev - remove a CPU device
1647cedb70afSSrivatsa S. Bhat  *
1648cedb70afSSrivatsa S. Bhat  * Removes the cpufreq interface for a CPU device.
1649cedb70afSSrivatsa S. Bhat  */
165071db87baSViresh Kumar static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
16515a01f2e8SVenkatesh Pallipadi {
16528a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
165387549141SViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
165487549141SViresh Kumar 
165587549141SViresh Kumar 	if (!policy)
165671db87baSViresh Kumar 		return;
1657ec28297aSVenki Pallipadi 
165869cee714SViresh Kumar 	if (cpu_online(cpu))
165969cee714SViresh Kumar 		cpufreq_offline(cpu);
166087549141SViresh Kumar 
1661*5c84c1b8SViresh Kumar 	remove_cpu_dev_symlink(policy, cpu, dev);
1662f344dae0SViresh Kumar 
166391a12e91SViresh Kumar 	if (cpumask_empty(policy->real_cpus)) {
166491a12e91SViresh Kumar 		/* We did light-weight exit earlier, do full tear down now */
166591a12e91SViresh Kumar 		if (cpufreq_driver->offline)
166691a12e91SViresh Kumar 			cpufreq_driver->exit(policy);
166791a12e91SViresh Kumar 
1668f9f41e3eSViresh Kumar 		cpufreq_policy_free(policy);
16695a01f2e8SVenkatesh Pallipadi 	}
167091a12e91SViresh Kumar }
16715a01f2e8SVenkatesh Pallipadi 
16721da177e4SLinus Torvalds /**
1673ec06e586SRafael J. Wysocki  * cpufreq_out_of_sync - Fix up actual and saved CPU frequency difference.
1674ec06e586SRafael J. Wysocki  * @policy: Policy managing CPUs.
1675ec06e586SRafael J. Wysocki  * @new_freq: New CPU frequency.
16761da177e4SLinus Torvalds  *
1677ec06e586SRafael J. Wysocki  * Adjust to the current frequency first and clean up later by either calling
1678ec06e586SRafael J. Wysocki  * cpufreq_update_policy(), or scheduling handle_update().
16791da177e4SLinus Torvalds  */
1680a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1681e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
16821da177e4SLinus Torvalds {
16831da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1684b43a7ffbSViresh Kumar 
1685e837f9b5SJoe Perches 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1686a1e1dc41SViresh Kumar 		 policy->cur, new_freq);
16871da177e4SLinus Torvalds 
1688a1e1dc41SViresh Kumar 	freqs.old = policy->cur;
16891da177e4SLinus Torvalds 	freqs.new = new_freq;
1690b43a7ffbSViresh Kumar 
16918fec051eSViresh Kumar 	cpufreq_freq_transition_begin(policy, &freqs);
16928fec051eSViresh Kumar 	cpufreq_freq_transition_end(policy, &freqs, 0);
16931da177e4SLinus Torvalds }
16941da177e4SLinus Torvalds 
16955980752eSViresh Kumar static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update)
16965980752eSViresh Kumar {
16975980752eSViresh Kumar 	unsigned int new_freq;
16985980752eSViresh Kumar 
16995980752eSViresh Kumar 	new_freq = cpufreq_driver->get(policy->cpu);
17005980752eSViresh Kumar 	if (!new_freq)
17015980752eSViresh Kumar 		return 0;
17025980752eSViresh Kumar 
17035980752eSViresh Kumar 	/*
17045980752eSViresh Kumar 	 * If fast frequency switching is used with the given policy, the check
17055980752eSViresh Kumar 	 * against policy->cur is pointless, so skip it in that case.
17065980752eSViresh Kumar 	 */
17075980752eSViresh Kumar 	if (policy->fast_switch_enabled || !has_target())
17085980752eSViresh Kumar 		return new_freq;
17095980752eSViresh Kumar 
17105980752eSViresh Kumar 	if (policy->cur != new_freq) {
1711f55ae08cSViresh Kumar 		/*
1712f55ae08cSViresh Kumar 		 * For some platforms, the frequency returned by hardware may be
1713f55ae08cSViresh Kumar 		 * slightly different from what is provided in the frequency
1714f55ae08cSViresh Kumar 		 * table, for example hardware may return 499 MHz instead of 500
1715f55ae08cSViresh Kumar 		 * MHz. In such cases it is better to avoid getting into
1716f55ae08cSViresh Kumar 		 * unnecessary frequency updates.
1717f55ae08cSViresh Kumar 		 */
1718f55ae08cSViresh Kumar 		if (abs(policy->cur - new_freq) < HZ_PER_MHZ)
1719f55ae08cSViresh Kumar 			return policy->cur;
1720f55ae08cSViresh Kumar 
17215980752eSViresh Kumar 		cpufreq_out_of_sync(policy, new_freq);
17225980752eSViresh Kumar 		if (update)
17235980752eSViresh Kumar 			schedule_work(&policy->update);
17245980752eSViresh Kumar 	}
17255980752eSViresh Kumar 
17265980752eSViresh Kumar 	return new_freq;
17275980752eSViresh Kumar }
17285980752eSViresh Kumar 
17291da177e4SLinus Torvalds /**
17304ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
173195235ca2SVenkatesh Pallipadi  * @cpu: CPU number
173295235ca2SVenkatesh Pallipadi  *
173395235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
173495235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
173595235ca2SVenkatesh Pallipadi  */
173695235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
173795235ca2SVenkatesh Pallipadi {
17389e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1739e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
1740c75361c0SRichard Cochran 	unsigned long flags;
174195235ca2SVenkatesh Pallipadi 
1742c75361c0SRichard Cochran 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1743c75361c0SRichard Cochran 
1744c75361c0SRichard Cochran 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1745c75361c0SRichard Cochran 		ret_freq = cpufreq_driver->get(cpu);
1746c75361c0SRichard Cochran 		read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1747c75361c0SRichard Cochran 		return ret_freq;
1748c75361c0SRichard Cochran 	}
1749c75361c0SRichard Cochran 
1750c75361c0SRichard Cochran 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
17519e21ba8bSDirk Brandewie 
17529e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
175395235ca2SVenkatesh Pallipadi 	if (policy) {
1754e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
175595235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
175695235ca2SVenkatesh Pallipadi 	}
175795235ca2SVenkatesh Pallipadi 
17584d34a67dSDave Jones 	return ret_freq;
175995235ca2SVenkatesh Pallipadi }
176095235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
176195235ca2SVenkatesh Pallipadi 
17623d737108SJesse Barnes /**
17633d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
17643d737108SJesse Barnes  * @cpu: CPU number
17653d737108SJesse Barnes  *
17663d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
17673d737108SJesse Barnes  */
17683d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
17693d737108SJesse Barnes {
17703d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
17713d737108SJesse Barnes 	unsigned int ret_freq = 0;
17723d737108SJesse Barnes 
17733d737108SJesse Barnes 	if (policy) {
17743d737108SJesse Barnes 		ret_freq = policy->max;
17753d737108SJesse Barnes 		cpufreq_cpu_put(policy);
17763d737108SJesse Barnes 	}
17773d737108SJesse Barnes 
17783d737108SJesse Barnes 	return ret_freq;
17793d737108SJesse Barnes }
17803d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
17813d737108SJesse Barnes 
1782bbce8eaaSIonela Voinescu /**
1783bbce8eaaSIonela Voinescu  * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU
1784bbce8eaaSIonela Voinescu  * @cpu: CPU number
1785bbce8eaaSIonela Voinescu  *
1786bbce8eaaSIonela Voinescu  * The default return value is the max_freq field of cpuinfo.
1787bbce8eaaSIonela Voinescu  */
1788bbce8eaaSIonela Voinescu __weak unsigned int cpufreq_get_hw_max_freq(unsigned int cpu)
1789bbce8eaaSIonela Voinescu {
1790bbce8eaaSIonela Voinescu 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1791bbce8eaaSIonela Voinescu 	unsigned int ret_freq = 0;
1792bbce8eaaSIonela Voinescu 
1793bbce8eaaSIonela Voinescu 	if (policy) {
1794bbce8eaaSIonela Voinescu 		ret_freq = policy->cpuinfo.max_freq;
1795bbce8eaaSIonela Voinescu 		cpufreq_cpu_put(policy);
1796bbce8eaaSIonela Voinescu 	}
1797bbce8eaaSIonela Voinescu 
1798bbce8eaaSIonela Voinescu 	return ret_freq;
1799bbce8eaaSIonela Voinescu }
1800bbce8eaaSIonela Voinescu EXPORT_SYMBOL(cpufreq_get_hw_max_freq);
1801bbce8eaaSIonela Voinescu 
1802d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
18031da177e4SLinus Torvalds {
18044db7c34cSYue Hu 	if (unlikely(policy_is_inactive(policy)))
18055980752eSViresh Kumar 		return 0;
18061da177e4SLinus Torvalds 
18075980752eSViresh Kumar 	return cpufreq_verify_current_freq(policy, true);
18085a01f2e8SVenkatesh Pallipadi }
18091da177e4SLinus Torvalds 
18105a01f2e8SVenkatesh Pallipadi /**
18115a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
18125a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
18135a01f2e8SVenkatesh Pallipadi  *
18145a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
18155a01f2e8SVenkatesh Pallipadi  */
18165a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
18175a01f2e8SVenkatesh Pallipadi {
1818999976e0SAaron Plattner 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
18195a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
18205a01f2e8SVenkatesh Pallipadi 
1821999976e0SAaron Plattner 	if (policy) {
1822ad7722daSviresh kumar 		down_read(&policy->rwsem);
18234db7c34cSYue Hu 		if (cpufreq_driver->get)
1824d92d50a4SViresh Kumar 			ret_freq = __cpufreq_get(policy);
1825ad7722daSviresh kumar 		up_read(&policy->rwsem);
1826999976e0SAaron Plattner 
1827999976e0SAaron Plattner 		cpufreq_cpu_put(policy);
1828999976e0SAaron Plattner 	}
18296eed9404SViresh Kumar 
18304d34a67dSDave Jones 	return ret_freq;
18311da177e4SLinus Torvalds }
18321da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
18331da177e4SLinus Torvalds 
18348a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
18358a25a2fdSKay Sievers 	.name		= "cpufreq",
18368a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
18378a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
18388a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1839e00e56dfSRafael J. Wysocki };
1840e00e56dfSRafael J. Wysocki 
1841e28867eaSViresh Kumar /*
1842e28867eaSViresh Kumar  * In case platform wants some specific frequency to be configured
1843e28867eaSViresh Kumar  * during suspend..
184442d4dc3fSBenjamin Herrenschmidt  */
1845e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy)
184642d4dc3fSBenjamin Herrenschmidt {
1847e28867eaSViresh Kumar 	int ret;
18484bc5d341SDave Jones 
1849e28867eaSViresh Kumar 	if (!policy->suspend_freq) {
1850201f3716SBartlomiej Zolnierkiewicz 		pr_debug("%s: suspend_freq not defined\n", __func__);
1851201f3716SBartlomiej Zolnierkiewicz 		return 0;
185242d4dc3fSBenjamin Herrenschmidt 	}
185342d4dc3fSBenjamin Herrenschmidt 
1854e28867eaSViresh Kumar 	pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1855e28867eaSViresh Kumar 			policy->suspend_freq);
1856e28867eaSViresh Kumar 
1857e28867eaSViresh Kumar 	ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1858e28867eaSViresh Kumar 			CPUFREQ_RELATION_H);
1859e28867eaSViresh Kumar 	if (ret)
1860e28867eaSViresh Kumar 		pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1861e28867eaSViresh Kumar 				__func__, policy->suspend_freq, ret);
1862e28867eaSViresh Kumar 
1863c9060494SDave Jones 	return ret;
186442d4dc3fSBenjamin Herrenschmidt }
1865e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend);
186642d4dc3fSBenjamin Herrenschmidt 
186742d4dc3fSBenjamin Herrenschmidt /**
1868ec06e586SRafael J. Wysocki  * cpufreq_suspend() - Suspend CPUFreq governors.
18691da177e4SLinus Torvalds  *
18702f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycles for suspending governors
18712f0aea93SViresh Kumar  * as some platforms can't change frequency after this point in suspend cycle.
18722f0aea93SViresh Kumar  * Because some of the devices (like: i2c, regulators, etc) they use for
18732f0aea93SViresh Kumar  * changing frequency are suspended quickly after this point.
18741da177e4SLinus Torvalds  */
18752f0aea93SViresh Kumar void cpufreq_suspend(void)
18761da177e4SLinus Torvalds {
18773a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
18781da177e4SLinus Torvalds 
18792f0aea93SViresh Kumar 	if (!cpufreq_driver)
1880e00e56dfSRafael J. Wysocki 		return;
18811da177e4SLinus Torvalds 
1882ba41e1bcSRafael J. Wysocki 	if (!has_target() && !cpufreq_driver->suspend)
1883b1b12babSViresh Kumar 		goto suspend;
18841da177e4SLinus Torvalds 
18852f0aea93SViresh Kumar 	pr_debug("%s: Suspending Governors\n", __func__);
18862f0aea93SViresh Kumar 
1887f963735aSViresh Kumar 	for_each_active_policy(policy) {
1888ba41e1bcSRafael J. Wysocki 		if (has_target()) {
188949f18560SViresh Kumar 			down_write(&policy->rwsem);
189045482c70SRafael J. Wysocki 			cpufreq_stop_governor(policy);
189149f18560SViresh Kumar 			up_write(&policy->rwsem);
1892ba41e1bcSRafael J. Wysocki 		}
1893ba41e1bcSRafael J. Wysocki 
1894ba41e1bcSRafael J. Wysocki 		if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
1895e9a7cc1dSFlorian Fainelli 			pr_err("%s: Failed to suspend driver: %s\n", __func__,
1896e9a7cc1dSFlorian Fainelli 				cpufreq_driver->name);
18971da177e4SLinus Torvalds 	}
1898b1b12babSViresh Kumar 
1899b1b12babSViresh Kumar suspend:
1900b1b12babSViresh Kumar 	cpufreq_suspended = true;
19011da177e4SLinus Torvalds }
19021da177e4SLinus Torvalds 
19031da177e4SLinus Torvalds /**
1904ec06e586SRafael J. Wysocki  * cpufreq_resume() - Resume CPUFreq governors.
19051da177e4SLinus Torvalds  *
19062f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycle for resuming governors that
19072f0aea93SViresh Kumar  * are suspended with cpufreq_suspend().
19081da177e4SLinus Torvalds  */
19092f0aea93SViresh Kumar void cpufreq_resume(void)
19101da177e4SLinus Torvalds {
19111da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
191249f18560SViresh Kumar 	int ret;
19131da177e4SLinus Torvalds 
19142f0aea93SViresh Kumar 	if (!cpufreq_driver)
19151da177e4SLinus Torvalds 		return;
19161da177e4SLinus Torvalds 
1917703cbaa6SBo Yan 	if (unlikely(!cpufreq_suspended))
1918703cbaa6SBo Yan 		return;
1919703cbaa6SBo Yan 
19208e30444eSLan Tianyu 	cpufreq_suspended = false;
19218e30444eSLan Tianyu 
1922ba41e1bcSRafael J. Wysocki 	if (!has_target() && !cpufreq_driver->resume)
19232f0aea93SViresh Kumar 		return;
19241da177e4SLinus Torvalds 
19252f0aea93SViresh Kumar 	pr_debug("%s: Resuming Governors\n", __func__);
19262f0aea93SViresh Kumar 
1927f963735aSViresh Kumar 	for_each_active_policy(policy) {
192849f18560SViresh Kumar 		if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
19290c5aa405SViresh Kumar 			pr_err("%s: Failed to resume driver: %p\n", __func__,
19300c5aa405SViresh Kumar 				policy);
1931ba41e1bcSRafael J. Wysocki 		} else if (has_target()) {
193249f18560SViresh Kumar 			down_write(&policy->rwsem);
19330a300767SRafael J. Wysocki 			ret = cpufreq_start_governor(policy);
193449f18560SViresh Kumar 			up_write(&policy->rwsem);
193549f18560SViresh Kumar 
193649f18560SViresh Kumar 			if (ret)
19372f0aea93SViresh Kumar 				pr_err("%s: Failed to start governor for policy: %p\n",
19382f0aea93SViresh Kumar 				       __func__, policy);
1939c75de0acSViresh Kumar 		}
194049f18560SViresh Kumar 	}
19411da177e4SLinus Torvalds }
19421da177e4SLinus Torvalds 
19439d95046eSBorislav Petkov /**
1944a62f68f5SRafael J. Wysocki  * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones.
1945a62f68f5SRafael J. Wysocki  * @flags: Flags to test against the current cpufreq driver's flags.
1946a62f68f5SRafael J. Wysocki  *
1947a62f68f5SRafael J. Wysocki  * Assumes that the driver is there, so callers must ensure that this is the
1948a62f68f5SRafael J. Wysocki  * case.
1949a62f68f5SRafael J. Wysocki  */
1950a62f68f5SRafael J. Wysocki bool cpufreq_driver_test_flags(u16 flags)
1951a62f68f5SRafael J. Wysocki {
1952a62f68f5SRafael J. Wysocki 	return !!(cpufreq_driver->flags & flags);
1953a62f68f5SRafael J. Wysocki }
1954a62f68f5SRafael J. Wysocki 
1955a62f68f5SRafael J. Wysocki /**
1956ec06e586SRafael J. Wysocki  * cpufreq_get_current_driver - Return the current driver's name.
19579d95046eSBorislav Petkov  *
1958ec06e586SRafael J. Wysocki  * Return the name string of the currently registered cpufreq driver or NULL if
1959ec06e586SRafael J. Wysocki  * none.
19609d95046eSBorislav Petkov  */
19619d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
19629d95046eSBorislav Petkov {
19631c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
19641c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
19651c3d85ddSRafael J. Wysocki 
19661c3d85ddSRafael J. Wysocki 	return NULL;
19679d95046eSBorislav Petkov }
19689d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
19691da177e4SLinus Torvalds 
197051315cdfSThomas Petazzoni /**
1971ec06e586SRafael J. Wysocki  * cpufreq_get_driver_data - Return current driver data.
197251315cdfSThomas Petazzoni  *
1973ec06e586SRafael J. Wysocki  * Return the private data of the currently registered cpufreq driver, or NULL
1974ec06e586SRafael J. Wysocki  * if no cpufreq driver has been registered.
197551315cdfSThomas Petazzoni  */
197651315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void)
197751315cdfSThomas Petazzoni {
197851315cdfSThomas Petazzoni 	if (cpufreq_driver)
197951315cdfSThomas Petazzoni 		return cpufreq_driver->driver_data;
198051315cdfSThomas Petazzoni 
198151315cdfSThomas Petazzoni 	return NULL;
198251315cdfSThomas Petazzoni }
198351315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
198451315cdfSThomas Petazzoni 
19851da177e4SLinus Torvalds /*********************************************************************
19861da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
19871da177e4SLinus Torvalds  *********************************************************************/
19881da177e4SLinus Torvalds 
19891da177e4SLinus Torvalds /**
1990ec06e586SRafael J. Wysocki  * cpufreq_register_notifier - Register a notifier with cpufreq.
1991ec06e586SRafael J. Wysocki  * @nb: notifier function to register.
1992ec06e586SRafael J. Wysocki  * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER.
19931da177e4SLinus Torvalds  *
1994ec06e586SRafael J. Wysocki  * Add a notifier to one of two lists: either a list of notifiers that run on
1995ec06e586SRafael J. Wysocki  * clock rate changes (once before and once after every transition), or a list
1996ec06e586SRafael J. Wysocki  * of notifiers that ron on cpufreq policy changes.
19971da177e4SLinus Torvalds  *
1998ec06e586SRafael J. Wysocki  * This function may sleep and it has the same return values as
1999ec06e586SRafael J. Wysocki  * blocking_notifier_chain_register().
20001da177e4SLinus Torvalds  */
20011da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
20021da177e4SLinus Torvalds {
20031da177e4SLinus Torvalds 	int ret;
20041da177e4SLinus Torvalds 
2005d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
2006d5aaffa9SDirk Brandewie 		return -EINVAL;
2007d5aaffa9SDirk Brandewie 
20081da177e4SLinus Torvalds 	switch (list) {
20091da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
2010b7898fdaSRafael J. Wysocki 		mutex_lock(&cpufreq_fast_switch_lock);
2011b7898fdaSRafael J. Wysocki 
2012b7898fdaSRafael J. Wysocki 		if (cpufreq_fast_switch_count > 0) {
2013b7898fdaSRafael J. Wysocki 			mutex_unlock(&cpufreq_fast_switch_lock);
2014b7898fdaSRafael J. Wysocki 			return -EBUSY;
2015b7898fdaSRafael J. Wysocki 		}
2016b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
2017e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
2018b7898fdaSRafael J. Wysocki 		if (!ret)
2019b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count--;
2020b7898fdaSRafael J. Wysocki 
2021b7898fdaSRafael J. Wysocki 		mutex_unlock(&cpufreq_fast_switch_lock);
20221da177e4SLinus Torvalds 		break;
20231da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
2024e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
2025e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
20261da177e4SLinus Torvalds 		break;
20271da177e4SLinus Torvalds 	default:
20281da177e4SLinus Torvalds 		ret = -EINVAL;
20291da177e4SLinus Torvalds 	}
20301da177e4SLinus Torvalds 
20311da177e4SLinus Torvalds 	return ret;
20321da177e4SLinus Torvalds }
20331da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
20341da177e4SLinus Torvalds 
20351da177e4SLinus Torvalds /**
2036ec06e586SRafael J. Wysocki  * cpufreq_unregister_notifier - Unregister a notifier from cpufreq.
2037ec06e586SRafael J. Wysocki  * @nb: notifier block to be unregistered.
2038ec06e586SRafael J. Wysocki  * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER.
20391da177e4SLinus Torvalds  *
2040ec06e586SRafael J. Wysocki  * Remove a notifier from one of the cpufreq notifier lists.
20411da177e4SLinus Torvalds  *
2042ec06e586SRafael J. Wysocki  * This function may sleep and it has the same return values as
2043ec06e586SRafael J. Wysocki  * blocking_notifier_chain_unregister().
20441da177e4SLinus Torvalds  */
20451da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
20461da177e4SLinus Torvalds {
20471da177e4SLinus Torvalds 	int ret;
20481da177e4SLinus Torvalds 
2049d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
2050d5aaffa9SDirk Brandewie 		return -EINVAL;
2051d5aaffa9SDirk Brandewie 
20521da177e4SLinus Torvalds 	switch (list) {
20531da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
2054b7898fdaSRafael J. Wysocki 		mutex_lock(&cpufreq_fast_switch_lock);
2055b7898fdaSRafael J. Wysocki 
2056b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
2057e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
2058b7898fdaSRafael J. Wysocki 		if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
2059b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count++;
2060b7898fdaSRafael J. Wysocki 
2061b7898fdaSRafael J. Wysocki 		mutex_unlock(&cpufreq_fast_switch_lock);
20621da177e4SLinus Torvalds 		break;
20631da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
2064e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
2065e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
20661da177e4SLinus Torvalds 		break;
20671da177e4SLinus Torvalds 	default:
20681da177e4SLinus Torvalds 		ret = -EINVAL;
20691da177e4SLinus Torvalds 	}
20701da177e4SLinus Torvalds 
20711da177e4SLinus Torvalds 	return ret;
20721da177e4SLinus Torvalds }
20731da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
20741da177e4SLinus Torvalds 
20751da177e4SLinus Torvalds 
20761da177e4SLinus Torvalds /*********************************************************************
20771da177e4SLinus Torvalds  *                              GOVERNORS                            *
20781da177e4SLinus Torvalds  *********************************************************************/
20791da177e4SLinus Torvalds 
2080b7898fdaSRafael J. Wysocki /**
2081b7898fdaSRafael J. Wysocki  * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
2082b7898fdaSRafael J. Wysocki  * @policy: cpufreq policy to switch the frequency for.
2083b7898fdaSRafael J. Wysocki  * @target_freq: New frequency to set (may be approximate).
2084b7898fdaSRafael J. Wysocki  *
2085b7898fdaSRafael J. Wysocki  * Carry out a fast frequency switch without sleeping.
2086b7898fdaSRafael J. Wysocki  *
2087b7898fdaSRafael J. Wysocki  * The driver's ->fast_switch() callback invoked by this function must be
2088b7898fdaSRafael J. Wysocki  * suitable for being called from within RCU-sched read-side critical sections
2089b7898fdaSRafael J. Wysocki  * and it is expected to select the minimum available frequency greater than or
2090b7898fdaSRafael J. Wysocki  * equal to @target_freq (CPUFREQ_RELATION_L).
2091b7898fdaSRafael J. Wysocki  *
2092b7898fdaSRafael J. Wysocki  * This function must not be called if policy->fast_switch_enabled is unset.
2093b7898fdaSRafael J. Wysocki  *
2094b7898fdaSRafael J. Wysocki  * Governors calling this function must guarantee that it will never be invoked
2095b7898fdaSRafael J. Wysocki  * twice in parallel for the same policy and that it will never be called in
2096b7898fdaSRafael J. Wysocki  * parallel with either ->target() or ->target_index() for the same policy.
2097b7898fdaSRafael J. Wysocki  *
2098209887e6SViresh Kumar  * Returns the actual frequency set for the CPU.
2099209887e6SViresh Kumar  *
2100209887e6SViresh Kumar  * If 0 is returned by the driver's ->fast_switch() callback to indicate an
2101209887e6SViresh Kumar  * error condition, the hardware configuration must be preserved.
2102b7898fdaSRafael J. Wysocki  */
2103b7898fdaSRafael J. Wysocki unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
2104b7898fdaSRafael J. Wysocki 					unsigned int target_freq)
2105b7898fdaSRafael J. Wysocki {
21061a0419b0SIonela Voinescu 	unsigned int freq;
210708d8c65eSViresh Kumar 	int cpu;
2108b7898fdaSRafael J. Wysocki 
21091a0419b0SIonela Voinescu 	target_freq = clamp_val(target_freq, policy->min, policy->max);
21101a0419b0SIonela Voinescu 	freq = cpufreq_driver->fast_switch(policy, target_freq);
21111a0419b0SIonela Voinescu 
211296f60cddSViresh Kumar 	if (!freq)
211396f60cddSViresh Kumar 		return 0;
211496f60cddSViresh Kumar 
211508d8c65eSViresh Kumar 	policy->cur = freq;
21161a0419b0SIonela Voinescu 	arch_set_freq_scale(policy->related_cpus, freq,
21171a0419b0SIonela Voinescu 			    policy->cpuinfo.max_freq);
211896f60cddSViresh Kumar 	cpufreq_stats_record_transition(policy, freq);
21191a0419b0SIonela Voinescu 
212008d8c65eSViresh Kumar 	if (trace_cpu_frequency_enabled()) {
212108d8c65eSViresh Kumar 		for_each_cpu(cpu, policy->cpus)
212208d8c65eSViresh Kumar 			trace_cpu_frequency(freq, cpu);
212308d8c65eSViresh Kumar 	}
212408d8c65eSViresh Kumar 
21251a0419b0SIonela Voinescu 	return freq;
2126b7898fdaSRafael J. Wysocki }
2127b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
2128b7898fdaSRafael J. Wysocki 
2129ee2cc427SRafael J. Wysocki /**
2130ee2cc427SRafael J. Wysocki  * cpufreq_driver_adjust_perf - Adjust CPU performance level in one go.
2131ee2cc427SRafael J. Wysocki  * @cpu: Target CPU.
2132ee2cc427SRafael J. Wysocki  * @min_perf: Minimum (required) performance level (units of @capacity).
21334e6df217SYue Hu  * @target_perf: Target (desired) performance level (units of @capacity).
2134ee2cc427SRafael J. Wysocki  * @capacity: Capacity of the target CPU.
2135ee2cc427SRafael J. Wysocki  *
2136ee2cc427SRafael J. Wysocki  * Carry out a fast performance level switch of @cpu without sleeping.
2137ee2cc427SRafael J. Wysocki  *
2138ee2cc427SRafael J. Wysocki  * The driver's ->adjust_perf() callback invoked by this function must be
2139ee2cc427SRafael J. Wysocki  * suitable for being called from within RCU-sched read-side critical sections
2140ee2cc427SRafael J. Wysocki  * and it is expected to select a suitable performance level equal to or above
2141ee2cc427SRafael J. Wysocki  * @min_perf and preferably equal to or below @target_perf.
2142ee2cc427SRafael J. Wysocki  *
2143ee2cc427SRafael J. Wysocki  * This function must not be called if policy->fast_switch_enabled is unset.
2144ee2cc427SRafael J. Wysocki  *
2145ee2cc427SRafael J. Wysocki  * Governors calling this function must guarantee that it will never be invoked
2146ee2cc427SRafael J. Wysocki  * twice in parallel for the same CPU and that it will never be called in
2147ee2cc427SRafael J. Wysocki  * parallel with either ->target() or ->target_index() or ->fast_switch() for
2148ee2cc427SRafael J. Wysocki  * the same CPU.
2149ee2cc427SRafael J. Wysocki  */
2150ee2cc427SRafael J. Wysocki void cpufreq_driver_adjust_perf(unsigned int cpu,
2151ee2cc427SRafael J. Wysocki 				 unsigned long min_perf,
2152ee2cc427SRafael J. Wysocki 				 unsigned long target_perf,
2153ee2cc427SRafael J. Wysocki 				 unsigned long capacity)
2154ee2cc427SRafael J. Wysocki {
2155ee2cc427SRafael J. Wysocki 	cpufreq_driver->adjust_perf(cpu, min_perf, target_perf, capacity);
2156ee2cc427SRafael J. Wysocki }
2157ee2cc427SRafael J. Wysocki 
2158ee2cc427SRafael J. Wysocki /**
2159ee2cc427SRafael J. Wysocki  * cpufreq_driver_has_adjust_perf - Check "direct fast switch" callback.
2160ee2cc427SRafael J. Wysocki  *
2161ee2cc427SRafael J. Wysocki  * Return 'true' if the ->adjust_perf callback is present for the
2162ee2cc427SRafael J. Wysocki  * current driver or 'false' otherwise.
2163ee2cc427SRafael J. Wysocki  */
2164ee2cc427SRafael J. Wysocki bool cpufreq_driver_has_adjust_perf(void)
2165ee2cc427SRafael J. Wysocki {
2166ee2cc427SRafael J. Wysocki 	return !!cpufreq_driver->adjust_perf;
2167ee2cc427SRafael J. Wysocki }
2168ee2cc427SRafael J. Wysocki 
21691c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */
21701c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy,
21711c03a2d0SViresh Kumar 				 struct cpufreq_freqs *freqs, int index)
21721c03a2d0SViresh Kumar {
21731c03a2d0SViresh Kumar 	int ret;
21741c03a2d0SViresh Kumar 
21751c03a2d0SViresh Kumar 	freqs->new = cpufreq_driver->get_intermediate(policy, index);
21761c03a2d0SViresh Kumar 
21771c03a2d0SViresh Kumar 	/* We don't need to switch to intermediate freq */
21781c03a2d0SViresh Kumar 	if (!freqs->new)
21791c03a2d0SViresh Kumar 		return 0;
21801c03a2d0SViresh Kumar 
21811c03a2d0SViresh Kumar 	pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
21821c03a2d0SViresh Kumar 		 __func__, policy->cpu, freqs->old, freqs->new);
21831c03a2d0SViresh Kumar 
21841c03a2d0SViresh Kumar 	cpufreq_freq_transition_begin(policy, freqs);
21851c03a2d0SViresh Kumar 	ret = cpufreq_driver->target_intermediate(policy, index);
21861c03a2d0SViresh Kumar 	cpufreq_freq_transition_end(policy, freqs, ret);
21871c03a2d0SViresh Kumar 
21881c03a2d0SViresh Kumar 	if (ret)
21891c03a2d0SViresh Kumar 		pr_err("%s: Failed to change to intermediate frequency: %d\n",
21901c03a2d0SViresh Kumar 		       __func__, ret);
21911c03a2d0SViresh Kumar 
21921c03a2d0SViresh Kumar 	return ret;
21931c03a2d0SViresh Kumar }
21941c03a2d0SViresh Kumar 
219523727845SViresh Kumar static int __target_index(struct cpufreq_policy *policy, int index)
21968d65775dSViresh Kumar {
21971c03a2d0SViresh Kumar 	struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
219856a7ff75SRafael J. Wysocki 	unsigned int restore_freq, intermediate_freq = 0;
219923727845SViresh Kumar 	unsigned int newfreq = policy->freq_table[index].frequency;
22008d65775dSViresh Kumar 	int retval = -EINVAL;
22018d65775dSViresh Kumar 	bool notify;
22028d65775dSViresh Kumar 
220323727845SViresh Kumar 	if (newfreq == policy->cur)
220423727845SViresh Kumar 		return 0;
220523727845SViresh Kumar 
220656a7ff75SRafael J. Wysocki 	/* Save last value to restore later on errors */
220756a7ff75SRafael J. Wysocki 	restore_freq = policy->cur;
220856a7ff75SRafael J. Wysocki 
22098d65775dSViresh Kumar 	notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
22108d65775dSViresh Kumar 	if (notify) {
22111c03a2d0SViresh Kumar 		/* Handle switching to intermediate frequency */
22121c03a2d0SViresh Kumar 		if (cpufreq_driver->get_intermediate) {
22131c03a2d0SViresh Kumar 			retval = __target_intermediate(policy, &freqs, index);
22141c03a2d0SViresh Kumar 			if (retval)
22151c03a2d0SViresh Kumar 				return retval;
22168d65775dSViresh Kumar 
22171c03a2d0SViresh Kumar 			intermediate_freq = freqs.new;
22181c03a2d0SViresh Kumar 			/* Set old freq to intermediate */
22191c03a2d0SViresh Kumar 			if (intermediate_freq)
22201c03a2d0SViresh Kumar 				freqs.old = freqs.new;
22211c03a2d0SViresh Kumar 		}
22221c03a2d0SViresh Kumar 
222323727845SViresh Kumar 		freqs.new = newfreq;
22248d65775dSViresh Kumar 		pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
22258d65775dSViresh Kumar 			 __func__, policy->cpu, freqs.old, freqs.new);
22268d65775dSViresh Kumar 
22278d65775dSViresh Kumar 		cpufreq_freq_transition_begin(policy, &freqs);
22288d65775dSViresh Kumar 	}
22298d65775dSViresh Kumar 
22308d65775dSViresh Kumar 	retval = cpufreq_driver->target_index(policy, index);
22318d65775dSViresh Kumar 	if (retval)
22328d65775dSViresh Kumar 		pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
22338d65775dSViresh Kumar 		       retval);
22348d65775dSViresh Kumar 
22351c03a2d0SViresh Kumar 	if (notify) {
22368d65775dSViresh Kumar 		cpufreq_freq_transition_end(policy, &freqs, retval);
22378d65775dSViresh Kumar 
22381c03a2d0SViresh Kumar 		/*
22391c03a2d0SViresh Kumar 		 * Failed after setting to intermediate freq? Driver should have
22401c03a2d0SViresh Kumar 		 * reverted back to initial frequency and so should we. Check
22411c03a2d0SViresh Kumar 		 * here for intermediate_freq instead of get_intermediate, in
224258405af6SShailendra Verma 		 * case we haven't switched to intermediate freq at all.
22431c03a2d0SViresh Kumar 		 */
22441c03a2d0SViresh Kumar 		if (unlikely(retval && intermediate_freq)) {
22451c03a2d0SViresh Kumar 			freqs.old = intermediate_freq;
224656a7ff75SRafael J. Wysocki 			freqs.new = restore_freq;
22471c03a2d0SViresh Kumar 			cpufreq_freq_transition_begin(policy, &freqs);
22481c03a2d0SViresh Kumar 			cpufreq_freq_transition_end(policy, &freqs, 0);
22491c03a2d0SViresh Kumar 		}
22501c03a2d0SViresh Kumar 	}
22511c03a2d0SViresh Kumar 
22528d65775dSViresh Kumar 	return retval;
22538d65775dSViresh Kumar }
22548d65775dSViresh Kumar 
22551da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
22561da177e4SLinus Torvalds 			    unsigned int target_freq,
22571da177e4SLinus Torvalds 			    unsigned int relation)
22581da177e4SLinus Torvalds {
22597249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
2260c32b6b8eSAshok Raj 
2261a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2262a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2263a7b422cdSKonrad Rzeszutek Wilk 
2264f9ccdec2SViresh Kumar 	target_freq = __resolve_freq(policy, target_freq, relation);
22657249924eSViresh Kumar 
22667249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
22677249924eSViresh Kumar 		 policy->cpu, target_freq, relation, old_target_freq);
22685a1c0228SViresh Kumar 
22699c0ebcf7SViresh Kumar 	/*
22709c0ebcf7SViresh Kumar 	 * This might look like a redundant call as we are checking it again
22719c0ebcf7SViresh Kumar 	 * after finding index. But it is left intentionally for cases where
22729c0ebcf7SViresh Kumar 	 * exactly same freq is called again and so we can save on few function
22739c0ebcf7SViresh Kumar 	 * calls.
22749c0ebcf7SViresh Kumar 	 */
22751c534352SRafael J. Wysocki 	if (target_freq == policy->cur &&
22761c534352SRafael J. Wysocki 	    !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS))
22775a1c0228SViresh Kumar 		return 0;
22785a1c0228SViresh Kumar 
22791f39fa0dSVincent Donnefort 	if (cpufreq_driver->target) {
22801f39fa0dSVincent Donnefort 		/*
22811f39fa0dSVincent Donnefort 		 * If the driver hasn't setup a single inefficient frequency,
22821f39fa0dSVincent Donnefort 		 * it's unlikely it knows how to decode CPUFREQ_RELATION_E.
22831f39fa0dSVincent Donnefort 		 */
22841f39fa0dSVincent Donnefort 		if (!policy->efficiencies_available)
22851f39fa0dSVincent Donnefort 			relation &= ~CPUFREQ_RELATION_E;
22861f39fa0dSVincent Donnefort 
22876019d23aSRafael J. Wysocki 		return cpufreq_driver->target(policy, target_freq, relation);
22881f39fa0dSVincent Donnefort 	}
22896019d23aSRafael J. Wysocki 
22906019d23aSRafael J. Wysocki 	if (!cpufreq_driver->target_index)
22916019d23aSRafael J. Wysocki 		return -EINVAL;
229290d45d17SAshok Raj 
2293f9ccdec2SViresh Kumar 	return __target_index(policy, policy->cached_resolved_idx);
22946019d23aSRafael J. Wysocki }
22951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
22961da177e4SLinus Torvalds 
22971da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
22981da177e4SLinus Torvalds 			  unsigned int target_freq,
22991da177e4SLinus Torvalds 			  unsigned int relation)
23001da177e4SLinus Torvalds {
230162c23a89SColin Ian King 	int ret;
23021da177e4SLinus Torvalds 
2303ad7722daSviresh kumar 	down_write(&policy->rwsem);
23041da177e4SLinus Torvalds 
23051da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
23061da177e4SLinus Torvalds 
2307ad7722daSviresh kumar 	up_write(&policy->rwsem);
23081da177e4SLinus Torvalds 
23091da177e4SLinus Torvalds 	return ret;
23101da177e4SLinus Torvalds }
23111da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
23121da177e4SLinus Torvalds 
2313de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
2314de1df26bSRafael J. Wysocki {
2315de1df26bSRafael J. Wysocki 	return NULL;
2316de1df26bSRafael J. Wysocki }
2317de1df26bSRafael J. Wysocki 
2318a92604b4SRafael J. Wysocki static int cpufreq_init_governor(struct cpufreq_policy *policy)
23191da177e4SLinus Torvalds {
2320cc993cabSDave Jones 	int ret;
23216afde10cSThomas Renninger 
23222f0aea93SViresh Kumar 	/* Don't start any governor operations if we are entering suspend */
23232f0aea93SViresh Kumar 	if (cpufreq_suspended)
23242f0aea93SViresh Kumar 		return 0;
2325cb57720bSEthan Zhao 	/*
2326cb57720bSEthan Zhao 	 * Governor might not be initiated here if ACPI _PPC changed
2327cb57720bSEthan Zhao 	 * notification happened, so check it.
2328cb57720bSEthan Zhao 	 */
2329cb57720bSEthan Zhao 	if (!policy->governor)
2330cb57720bSEthan Zhao 		return -EINVAL;
23312f0aea93SViresh Kumar 
2332ed4676e2SViresh Kumar 	/* Platform doesn't want dynamic frequency switching ? */
23339a2a9ebcSRafael J. Wysocki 	if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING &&
2334fc4c709fSViresh Kumar 	    cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
2335de1df26bSRafael J. Wysocki 		struct cpufreq_governor *gov = cpufreq_fallback_governor();
2336de1df26bSRafael J. Wysocki 
2337de1df26bSRafael J. Wysocki 		if (gov) {
2338fe829ed8SViresh Kumar 			pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
2339e837f9b5SJoe Perches 				policy->governor->name, gov->name);
23401c256245SThomas Renninger 			policy->governor = gov;
2341de1df26bSRafael J. Wysocki 		} else {
2342de1df26bSRafael J. Wysocki 			return -EINVAL;
23431c256245SThomas Renninger 		}
23446afde10cSThomas Renninger 	}
23451da177e4SLinus Torvalds 
23461da177e4SLinus Torvalds 	if (!try_module_get(policy->governor->owner))
23471da177e4SLinus Torvalds 		return -EINVAL;
23481da177e4SLinus Torvalds 
2349a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
235095731ebbSXiaoguang Chen 
2351e788892bSRafael J. Wysocki 	if (policy->governor->init) {
2352e788892bSRafael J. Wysocki 		ret = policy->governor->init(policy);
2353a92604b4SRafael J. Wysocki 		if (ret) {
23541da177e4SLinus Torvalds 			module_put(policy->governor->owner);
2355a92604b4SRafael J. Wysocki 			return ret;
23569e8c0a89SRafael J. Wysocki 		}
235736be3418SRafael J. Wysocki 	}
23581da177e4SLinus Torvalds 
2359ea9364bbSRafael J. Wysocki 	policy->strict_target = !!(policy->governor->flags & CPUFREQ_GOV_STRICT_TARGET);
2360ea9364bbSRafael J. Wysocki 
2361a92604b4SRafael J. Wysocki 	return 0;
2362a92604b4SRafael J. Wysocki }
2363a92604b4SRafael J. Wysocki 
2364a92604b4SRafael J. Wysocki static void cpufreq_exit_governor(struct cpufreq_policy *policy)
2365a92604b4SRafael J. Wysocki {
2366a92604b4SRafael J. Wysocki 	if (cpufreq_suspended || !policy->governor)
2367a92604b4SRafael J. Wysocki 		return;
2368a92604b4SRafael J. Wysocki 
2369a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2370a92604b4SRafael J. Wysocki 
2371e788892bSRafael J. Wysocki 	if (policy->governor->exit)
2372e788892bSRafael J. Wysocki 		policy->governor->exit(policy);
2373a92604b4SRafael J. Wysocki 
23741da177e4SLinus Torvalds 	module_put(policy->governor->owner);
23751da177e4SLinus Torvalds }
23761da177e4SLinus Torvalds 
2377f6ebbcf0SRafael J. Wysocki int cpufreq_start_governor(struct cpufreq_policy *policy)
23780a300767SRafael J. Wysocki {
23790a300767SRafael J. Wysocki 	int ret;
23800a300767SRafael J. Wysocki 
2381a92604b4SRafael J. Wysocki 	if (cpufreq_suspended)
2382a92604b4SRafael J. Wysocki 		return 0;
2383a92604b4SRafael J. Wysocki 
2384a92604b4SRafael J. Wysocki 	if (!policy->governor)
2385a92604b4SRafael J. Wysocki 		return -EINVAL;
2386a92604b4SRafael J. Wysocki 
2387a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2388a92604b4SRafael J. Wysocki 
2389407d0fffSViresh Kumar 	if (cpufreq_driver->get)
23905980752eSViresh Kumar 		cpufreq_verify_current_freq(policy, false);
23913bbf8fe3SRafael J. Wysocki 
2392e788892bSRafael J. Wysocki 	if (policy->governor->start) {
2393e788892bSRafael J. Wysocki 		ret = policy->governor->start(policy);
2394d6ff44d6SRafael J. Wysocki 		if (ret)
2395d6ff44d6SRafael J. Wysocki 			return ret;
2396e788892bSRafael J. Wysocki 	}
2397d6ff44d6SRafael J. Wysocki 
2398e788892bSRafael J. Wysocki 	if (policy->governor->limits)
2399e788892bSRafael J. Wysocki 		policy->governor->limits(policy);
2400e788892bSRafael J. Wysocki 
2401d6ff44d6SRafael J. Wysocki 	return 0;
24020a300767SRafael J. Wysocki }
24030a300767SRafael J. Wysocki 
2404f6ebbcf0SRafael J. Wysocki void cpufreq_stop_governor(struct cpufreq_policy *policy)
2405a92604b4SRafael J. Wysocki {
2406a92604b4SRafael J. Wysocki 	if (cpufreq_suspended || !policy->governor)
2407a92604b4SRafael J. Wysocki 		return;
2408a92604b4SRafael J. Wysocki 
2409a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2410a92604b4SRafael J. Wysocki 
2411e788892bSRafael J. Wysocki 	if (policy->governor->stop)
2412e788892bSRafael J. Wysocki 		policy->governor->stop(policy);
2413a92604b4SRafael J. Wysocki }
2414a92604b4SRafael J. Wysocki 
2415a92604b4SRafael J. Wysocki static void cpufreq_governor_limits(struct cpufreq_policy *policy)
2416a92604b4SRafael J. Wysocki {
2417a92604b4SRafael J. Wysocki 	if (cpufreq_suspended || !policy->governor)
2418a92604b4SRafael J. Wysocki 		return;
2419a92604b4SRafael J. Wysocki 
2420a92604b4SRafael J. Wysocki 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
2421a92604b4SRafael J. Wysocki 
2422e788892bSRafael J. Wysocki 	if (policy->governor->limits)
2423e788892bSRafael J. Wysocki 		policy->governor->limits(policy);
24241da177e4SLinus Torvalds }
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
24271da177e4SLinus Torvalds {
24283bcb09a3SJeremy Fitzhardinge 	int err;
24291da177e4SLinus Torvalds 
24301da177e4SLinus Torvalds 	if (!governor)
24311da177e4SLinus Torvalds 		return -EINVAL;
24321da177e4SLinus Torvalds 
2433a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2434a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2435a7b422cdSKonrad Rzeszutek Wilk 
24363fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
24371da177e4SLinus Torvalds 
24383bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
243942f91fa1SViresh Kumar 	if (!find_governor(governor->name)) {
24403bcb09a3SJeremy Fitzhardinge 		err = 0;
24411da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
24423bcb09a3SJeremy Fitzhardinge 	}
24431da177e4SLinus Torvalds 
24443fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
24453bcb09a3SJeremy Fitzhardinge 	return err;
24461da177e4SLinus Torvalds }
24471da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
24481da177e4SLinus Torvalds 
24491da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
24501da177e4SLinus Torvalds {
24514573237bSViresh Kumar 	struct cpufreq_policy *policy;
24524573237bSViresh Kumar 	unsigned long flags;
245390e41bacSPrarit Bhargava 
24541da177e4SLinus Torvalds 	if (!governor)
24551da177e4SLinus Torvalds 		return;
24561da177e4SLinus Torvalds 
2457a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2458a7b422cdSKonrad Rzeszutek Wilk 		return;
2459a7b422cdSKonrad Rzeszutek Wilk 
24604573237bSViresh Kumar 	/* clear last_governor for all inactive policies */
24614573237bSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
24624573237bSViresh Kumar 	for_each_inactive_policy(policy) {
246318bf3a12SViresh Kumar 		if (!strcmp(policy->last_governor, governor->name)) {
246418bf3a12SViresh Kumar 			policy->governor = NULL;
24654573237bSViresh Kumar 			strcpy(policy->last_governor, "\0");
246690e41bacSPrarit Bhargava 		}
246718bf3a12SViresh Kumar 	}
24684573237bSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
246990e41bacSPrarit Bhargava 
24703fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
24711da177e4SLinus Torvalds 	list_del(&governor->governor_list);
24723fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
24731da177e4SLinus Torvalds }
24741da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
24751da177e4SLinus Torvalds 
24761da177e4SLinus Torvalds 
24771da177e4SLinus Torvalds /*********************************************************************
24781da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
24791da177e4SLinus Torvalds  *********************************************************************/
24801da177e4SLinus Torvalds 
24811da177e4SLinus Torvalds /**
24821da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
248329464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
248429464f28SDave Jones  *	is written
2485a9909c21SLee Jones  * @cpu: CPU to find the policy for
24861da177e4SLinus Torvalds  *
24871da177e4SLinus Torvalds  * Reads the current cpufreq policy.
24881da177e4SLinus Torvalds  */
24891da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
24901da177e4SLinus Torvalds {
24911da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
24921da177e4SLinus Torvalds 	if (!policy)
24931da177e4SLinus Torvalds 		return -EINVAL;
24941da177e4SLinus Torvalds 
24951da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
24961da177e4SLinus Torvalds 	if (!cpu_policy)
24971da177e4SLinus Torvalds 		return -EINVAL;
24981da177e4SLinus Torvalds 
2499d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
25001da177e4SLinus Torvalds 
25011da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
25021da177e4SLinus Torvalds 	return 0;
25031da177e4SLinus Torvalds }
25041da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
25051da177e4SLinus Torvalds 
2506a0dbb819SRafael J. Wysocki /**
2507a0dbb819SRafael J. Wysocki  * cpufreq_set_policy - Modify cpufreq policy parameters.
2508a0dbb819SRafael J. Wysocki  * @policy: Policy object to modify.
25091e4f63aeSRafael J. Wysocki  * @new_gov: Policy governor pointer.
25101e4f63aeSRafael J. Wysocki  * @new_pol: Policy value (for drivers with built-in governors).
2511a0dbb819SRafael J. Wysocki  *
25121e4f63aeSRafael J. Wysocki  * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency
25131e4f63aeSRafael J. Wysocki  * limits to be set for the policy, update @policy with the verified limits
25141e4f63aeSRafael J. Wysocki  * values and either invoke the driver's ->setpolicy() callback (if present) or
25151e4f63aeSRafael J. Wysocki  * carry out a governor update for @policy.  That is, run the current governor's
25161e4f63aeSRafael J. Wysocki  * ->limits() callback (if @new_gov points to the same object as the one in
25171e4f63aeSRafael J. Wysocki  * @policy) or replace the governor for @policy with @new_gov.
2518a0dbb819SRafael J. Wysocki  *
2519a0dbb819SRafael J. Wysocki  * The cpuinfo part of @policy is not updated by this function.
2520153d7f3fSArjan van de Ven  */
25211e4f63aeSRafael J. Wysocki static int cpufreq_set_policy(struct cpufreq_policy *policy,
25221e4f63aeSRafael J. Wysocki 			      struct cpufreq_governor *new_gov,
25231e4f63aeSRafael J. Wysocki 			      unsigned int new_pol)
25241da177e4SLinus Torvalds {
25251e4f63aeSRafael J. Wysocki 	struct cpufreq_policy_data new_data;
2526d9a789c7SRafael J. Wysocki 	struct cpufreq_governor *old_gov;
2527d9a789c7SRafael J. Wysocki 	int ret;
25281da177e4SLinus Torvalds 
25291e4f63aeSRafael J. Wysocki 	memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
25301e4f63aeSRafael J. Wysocki 	new_data.freq_table = policy->freq_table;
25311e4f63aeSRafael J. Wysocki 	new_data.cpu = policy->cpu;
2532fba9573bSPan Xinhui 	/*
253367d874c3SViresh Kumar 	 * PM QoS framework collects all the requests from users and provide us
253467d874c3SViresh Kumar 	 * the final aggregated value here.
2535fba9573bSPan Xinhui 	 */
25361e4f63aeSRafael J. Wysocki 	new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
25371e4f63aeSRafael J. Wysocki 	new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
25381e4f63aeSRafael J. Wysocki 
25391e4f63aeSRafael J. Wysocki 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
25401e4f63aeSRafael J. Wysocki 		 new_data.cpu, new_data.min, new_data.max);
25419c9a43edSMattia Dongili 
2542737ffb27SViresh Kumar 	/*
2543737ffb27SViresh Kumar 	 * Verify that the CPU speed can be set within these limits and make sure
2544737ffb27SViresh Kumar 	 * that min <= max.
2545737ffb27SViresh Kumar 	 */
25461e4f63aeSRafael J. Wysocki 	ret = cpufreq_driver->verify(&new_data);
25471da177e4SLinus Torvalds 	if (ret)
2548d9a789c7SRafael J. Wysocki 		return ret;
25491da177e4SLinus Torvalds 
255015171769SVincent Donnefort 	/*
255115171769SVincent Donnefort 	 * Resolve policy min/max to available frequencies. It ensures
255215171769SVincent Donnefort 	 * no frequency resolution will neither overshoot the requested maximum
255315171769SVincent Donnefort 	 * nor undershoot the requested minimum.
255415171769SVincent Donnefort 	 */
25551e4f63aeSRafael J. Wysocki 	policy->min = new_data.min;
25561e4f63aeSRafael J. Wysocki 	policy->max = new_data.max;
255715171769SVincent Donnefort 	policy->min = __resolve_freq(policy, policy->min, CPUFREQ_RELATION_L);
255815171769SVincent Donnefort 	policy->max = __resolve_freq(policy, policy->max, CPUFREQ_RELATION_H);
2559601b2185SRuchi Kandoi 	trace_cpu_frequency_limits(policy);
25601da177e4SLinus Torvalds 
2561e3c06236SSteve Muckle 	policy->cached_target_freq = UINT_MAX;
2562e3c06236SSteve Muckle 
25632d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
25643a3e9e06SViresh Kumar 		 policy->min, policy->max);
25651da177e4SLinus Torvalds 
25661c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
25671e4f63aeSRafael J. Wysocki 		policy->policy = new_pol;
25682d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
2569167a38dcSRafael J. Wysocki 		return cpufreq_driver->setpolicy(policy);
2570d9a789c7SRafael J. Wysocki 	}
2571d9a789c7SRafael J. Wysocki 
25721e4f63aeSRafael J. Wysocki 	if (new_gov == policy->governor) {
25732bb4059eSRafael J. Wysocki 		pr_debug("governor limits update\n");
2574a92604b4SRafael J. Wysocki 		cpufreq_governor_limits(policy);
2575d6ff44d6SRafael J. Wysocki 		return 0;
25760a300767SRafael J. Wysocki 	}
25771da177e4SLinus Torvalds 
25782d06d8c4SDominik Brodowski 	pr_debug("governor switch\n");
25791da177e4SLinus Torvalds 
2580d9a789c7SRafael J. Wysocki 	/* save old, working values */
2581d9a789c7SRafael J. Wysocki 	old_gov = policy->governor;
25821da177e4SLinus Torvalds 	/* end old governor */
2583d9a789c7SRafael J. Wysocki 	if (old_gov) {
258445482c70SRafael J. Wysocki 		cpufreq_stop_governor(policy);
258536be3418SRafael J. Wysocki 		cpufreq_exit_governor(policy);
25867bd353a9SViresh Kumar 	}
25871da177e4SLinus Torvalds 
25881da177e4SLinus Torvalds 	/* start new governor */
25891e4f63aeSRafael J. Wysocki 	policy->governor = new_gov;
2590a92604b4SRafael J. Wysocki 	ret = cpufreq_init_governor(policy);
25914bc384aeSViresh Kumar 	if (!ret) {
25920a300767SRafael J. Wysocki 		ret = cpufreq_start_governor(policy);
25930a300767SRafael J. Wysocki 		if (!ret) {
25942bb4059eSRafael J. Wysocki 			pr_debug("governor change\n");
2595531b5c9fSQuentin Perret 			sched_cpufreq_governor_change(policy, old_gov);
25960a300767SRafael J. Wysocki 			return 0;
25970a300767SRafael J. Wysocki 		}
2598b7898fdaSRafael J. Wysocki 		cpufreq_exit_governor(policy);
2599955ef483SViresh Kumar 	}
26007bd353a9SViresh Kumar 
26011da177e4SLinus Torvalds 	/* new governor failed, so re-start old one */
2602d9a789c7SRafael J. Wysocki 	pr_debug("starting governor %s failed\n", policy->governor->name);
26031da177e4SLinus Torvalds 	if (old_gov) {
26043a3e9e06SViresh Kumar 		policy->governor = old_gov;
2605a92604b4SRafael J. Wysocki 		if (cpufreq_init_governor(policy))
26064bc384aeSViresh Kumar 			policy->governor = NULL;
26074bc384aeSViresh Kumar 		else
26080a300767SRafael J. Wysocki 			cpufreq_start_governor(policy);
26091da177e4SLinus Torvalds 	}
26101da177e4SLinus Torvalds 
26114bc384aeSViresh Kumar 	return ret;
26121da177e4SLinus Torvalds }
26131da177e4SLinus Torvalds 
26141da177e4SLinus Torvalds /**
2615a0dbb819SRafael J. Wysocki  * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
2616a0dbb819SRafael J. Wysocki  * @cpu: CPU to re-evaluate the policy for.
26171da177e4SLinus Torvalds  *
2618a0dbb819SRafael J. Wysocki  * Update the current frequency for the cpufreq policy of @cpu and use
261918c49926SViresh Kumar  * cpufreq_set_policy() to re-apply the min and max limits, which triggers the
262018c49926SViresh Kumar  * evaluation of policy notifiers and the cpufreq driver's ->verify() callback
262118c49926SViresh Kumar  * for the policy in question, among other things.
26221da177e4SLinus Torvalds  */
262330248fefSRafael J. Wysocki void cpufreq_update_policy(unsigned int cpu)
26241da177e4SLinus Torvalds {
2625540a3758SRafael J. Wysocki 	struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
26261da177e4SLinus Torvalds 
2627fefa8ff8SAaron Plattner 	if (!policy)
262830248fefSRafael J. Wysocki 		return;
26291da177e4SLinus Torvalds 
2630bb176f7dSViresh Kumar 	/*
2631bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
2632bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
2633bb176f7dSViresh Kumar 	 */
26345ddc6d4eSViresh Kumar 	if (cpufreq_driver->get && has_target() &&
26355980752eSViresh Kumar 	    (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
2636742c87bfSRafael J. Wysocki 		goto unlock;
263730248fefSRafael J. Wysocki 
263870a59fdeSViresh Kumar 	refresh_frequency_limits(policy);
26391da177e4SLinus Torvalds 
2640fefa8ff8SAaron Plattner unlock:
2641540a3758SRafael J. Wysocki 	cpufreq_cpu_release(policy);
26421da177e4SLinus Torvalds }
26431da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
26441da177e4SLinus Torvalds 
26455a25e3f7SRafael J. Wysocki /**
26465a25e3f7SRafael J. Wysocki  * cpufreq_update_limits - Update policy limits for a given CPU.
26475a25e3f7SRafael J. Wysocki  * @cpu: CPU to update the policy limits for.
26485a25e3f7SRafael J. Wysocki  *
26495a25e3f7SRafael J. Wysocki  * Invoke the driver's ->update_limits callback if present or call
26505a25e3f7SRafael J. Wysocki  * cpufreq_update_policy() for @cpu.
26515a25e3f7SRafael J. Wysocki  */
26525a25e3f7SRafael J. Wysocki void cpufreq_update_limits(unsigned int cpu)
26535a25e3f7SRafael J. Wysocki {
26545a25e3f7SRafael J. Wysocki 	if (cpufreq_driver->update_limits)
26555a25e3f7SRafael J. Wysocki 		cpufreq_driver->update_limits(cpu);
26565a25e3f7SRafael J. Wysocki 	else
26575a25e3f7SRafael J. Wysocki 		cpufreq_update_policy(cpu);
26585a25e3f7SRafael J. Wysocki }
26595a25e3f7SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_update_limits);
26605a25e3f7SRafael J. Wysocki 
26611da177e4SLinus Torvalds /*********************************************************************
26626f19efc0SLukasz Majewski  *               BOOST						     *
26636f19efc0SLukasz Majewski  *********************************************************************/
2664cf6fada7SXiongfeng Wang static int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state)
26656f19efc0SLukasz Majewski {
2666552abb88SRafael J. Wysocki 	int ret;
2667552abb88SRafael J. Wysocki 
2668f8bfc116SViresh Kumar 	if (!policy->freq_table)
2669552abb88SRafael J. Wysocki 		return -ENXIO;
2670f8bfc116SViresh Kumar 
2671cf6fada7SXiongfeng Wang 	ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table);
26726f19efc0SLukasz Majewski 	if (ret) {
2673cf6fada7SXiongfeng Wang 		pr_err("%s: Policy frequency update failed\n", __func__);
2674552abb88SRafael J. Wysocki 		return ret;
26756f19efc0SLukasz Majewski 	}
267649f18560SViresh Kumar 
26773000ce3cSRafael J. Wysocki 	ret = freq_qos_update_request(policy->max_freq_req, policy->max);
2678e61a4125SViresh Kumar 	if (ret < 0)
2679552abb88SRafael J. Wysocki 		return ret;
26806f19efc0SLukasz Majewski 
2681552abb88SRafael J. Wysocki 	return 0;
26826f19efc0SLukasz Majewski }
26836f19efc0SLukasz Majewski 
26846f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state)
26856f19efc0SLukasz Majewski {
2686cf6fada7SXiongfeng Wang 	struct cpufreq_policy *policy;
26876f19efc0SLukasz Majewski 	unsigned long flags;
26886f19efc0SLukasz Majewski 	int ret = 0;
26896f19efc0SLukasz Majewski 
26906f19efc0SLukasz Majewski 	if (cpufreq_driver->boost_enabled == state)
26916f19efc0SLukasz Majewski 		return 0;
26926f19efc0SLukasz Majewski 
26936f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
26946f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = state;
26956f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
26966f19efc0SLukasz Majewski 
269709681a07SSebastian Andrzej Siewior 	cpus_read_lock();
2698cf6fada7SXiongfeng Wang 	for_each_active_policy(policy) {
2699cf6fada7SXiongfeng Wang 		ret = cpufreq_driver->set_boost(policy, state);
2700cf6fada7SXiongfeng Wang 		if (ret)
2701cf6fada7SXiongfeng Wang 			goto err_reset_state;
2702cf6fada7SXiongfeng Wang 	}
270309681a07SSebastian Andrzej Siewior 	cpus_read_unlock();
2704cf6fada7SXiongfeng Wang 
2705cf6fada7SXiongfeng Wang 	return 0;
2706cf6fada7SXiongfeng Wang 
2707cf6fada7SXiongfeng Wang err_reset_state:
270809681a07SSebastian Andrzej Siewior 	cpus_read_unlock();
2709cf6fada7SXiongfeng Wang 
27106f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
27116f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = !state;
27126f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
27136f19efc0SLukasz Majewski 
2714e837f9b5SJoe Perches 	pr_err("%s: Cannot %s BOOST\n",
2715e837f9b5SJoe Perches 	       __func__, state ? "enable" : "disable");
27166f19efc0SLukasz Majewski 
27176f19efc0SLukasz Majewski 	return ret;
27186f19efc0SLukasz Majewski }
27196f19efc0SLukasz Majewski 
272041669da0SRafael J. Wysocki static bool cpufreq_boost_supported(void)
27216f19efc0SLukasz Majewski {
272289f98d7eSYue Hu 	return cpufreq_driver->set_boost;
27236f19efc0SLukasz Majewski }
27246f19efc0SLukasz Majewski 
272544139ed4SViresh Kumar static int create_boost_sysfs_file(void)
272644139ed4SViresh Kumar {
272744139ed4SViresh Kumar 	int ret;
272844139ed4SViresh Kumar 
2729c82bd444SViresh Kumar 	ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
273044139ed4SViresh Kumar 	if (ret)
273144139ed4SViresh Kumar 		pr_err("%s: cannot register global BOOST sysfs file\n",
273244139ed4SViresh Kumar 		       __func__);
273344139ed4SViresh Kumar 
273444139ed4SViresh Kumar 	return ret;
273544139ed4SViresh Kumar }
273644139ed4SViresh Kumar 
273744139ed4SViresh Kumar static void remove_boost_sysfs_file(void)
273844139ed4SViresh Kumar {
273944139ed4SViresh Kumar 	if (cpufreq_boost_supported())
2740c82bd444SViresh Kumar 		sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
274144139ed4SViresh Kumar }
274244139ed4SViresh Kumar 
274344139ed4SViresh Kumar int cpufreq_enable_boost_support(void)
274444139ed4SViresh Kumar {
274544139ed4SViresh Kumar 	if (!cpufreq_driver)
274644139ed4SViresh Kumar 		return -EINVAL;
274744139ed4SViresh Kumar 
274844139ed4SViresh Kumar 	if (cpufreq_boost_supported())
274944139ed4SViresh Kumar 		return 0;
275044139ed4SViresh Kumar 
27517a6c79f2SRafael J. Wysocki 	cpufreq_driver->set_boost = cpufreq_boost_set_sw;
275244139ed4SViresh Kumar 
275344139ed4SViresh Kumar 	/* This will get removed on driver unregister */
275444139ed4SViresh Kumar 	return create_boost_sysfs_file();
275544139ed4SViresh Kumar }
275644139ed4SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
275744139ed4SViresh Kumar 
27586f19efc0SLukasz Majewski int cpufreq_boost_enabled(void)
27596f19efc0SLukasz Majewski {
27606f19efc0SLukasz Majewski 	return cpufreq_driver->boost_enabled;
27616f19efc0SLukasz Majewski }
27626f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
27636f19efc0SLukasz Majewski 
27646f19efc0SLukasz Majewski /*********************************************************************
27651da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
27661da177e4SLinus Torvalds  *********************************************************************/
276727622b06SSebastian Andrzej Siewior static enum cpuhp_state hp_online;
27681da177e4SLinus Torvalds 
2769c4a3fa26SChen Yu static int cpuhp_cpufreq_online(unsigned int cpu)
2770c4a3fa26SChen Yu {
2771c4a3fa26SChen Yu 	cpufreq_online(cpu);
2772c4a3fa26SChen Yu 
2773c4a3fa26SChen Yu 	return 0;
2774c4a3fa26SChen Yu }
2775c4a3fa26SChen Yu 
2776c4a3fa26SChen Yu static int cpuhp_cpufreq_offline(unsigned int cpu)
2777c4a3fa26SChen Yu {
2778c4a3fa26SChen Yu 	cpufreq_offline(cpu);
2779c4a3fa26SChen Yu 
2780c4a3fa26SChen Yu 	return 0;
2781c4a3fa26SChen Yu }
2782c4a3fa26SChen Yu 
27831da177e4SLinus Torvalds /**
27841da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
27851da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
27861da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
27871da177e4SLinus Torvalds  *
27881da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
278963af4055SEric Biggers  * returns zero on success, -EEXIST when another driver got here first
27901da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
27911da177e4SLinus Torvalds  *
27921da177e4SLinus Torvalds  */
2793221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
27941da177e4SLinus Torvalds {
27951da177e4SLinus Torvalds 	unsigned long flags;
27961da177e4SLinus Torvalds 	int ret;
27971da177e4SLinus Torvalds 
2798a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2799a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2800a7b422cdSKonrad Rzeszutek Wilk 
280146770be0SViresh Kumar 	/*
280246770be0SViresh Kumar 	 * The cpufreq core depends heavily on the availability of device
280346770be0SViresh Kumar 	 * structure, make sure they are available before proceeding further.
280446770be0SViresh Kumar 	 */
280546770be0SViresh Kumar 	if (!get_cpu_device(0))
280646770be0SViresh Kumar 		return -EPROBE_DEFER;
280746770be0SViresh Kumar 
28081da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
28099c0ebcf7SViresh Kumar 	    !(driver_data->setpolicy || driver_data->target_index ||
28109832235fSRafael J. Wysocki 		    driver_data->target) ||
28119832235fSRafael J. Wysocki 	     (driver_data->setpolicy && (driver_data->target_index ||
28121c03a2d0SViresh Kumar 		    driver_data->target)) ||
2813a9a22b57SViresh Kumar 	     (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
281491a12e91SViresh Kumar 	     (!driver_data->online != !driver_data->offline))
28151da177e4SLinus Torvalds 		return -EINVAL;
28161da177e4SLinus Torvalds 
28172d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
28181da177e4SLinus Torvalds 
2819fdd320daSRafael J. Wysocki 	/* Protect against concurrent CPU online/offline. */
2820a92551e4SSebastian Andrzej Siewior 	cpus_read_lock();
2821fdd320daSRafael J. Wysocki 
28220d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
28231c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
28240d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2825fdd320daSRafael J. Wysocki 		ret = -EEXIST;
2826fdd320daSRafael J. Wysocki 		goto out;
28271da177e4SLinus Torvalds 	}
28281c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
28290d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
28301da177e4SLinus Torvalds 
2831874f6353SIonela Voinescu 	/*
2832874f6353SIonela Voinescu 	 * Mark support for the scheduler's frequency invariance engine for
2833874f6353SIonela Voinescu 	 * drivers that implement target(), target_index() or fast_switch().
2834874f6353SIonela Voinescu 	 */
2835874f6353SIonela Voinescu 	if (!cpufreq_driver->setpolicy) {
2836874f6353SIonela Voinescu 		static_branch_enable_cpuslocked(&cpufreq_freq_invariance);
2837874f6353SIonela Voinescu 		pr_debug("supports frequency invariance");
2838874f6353SIonela Voinescu 	}
2839874f6353SIonela Voinescu 
2840bc68b7dfSViresh Kumar 	if (driver_data->setpolicy)
2841bc68b7dfSViresh Kumar 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
2842bc68b7dfSViresh Kumar 
28437a6c79f2SRafael J. Wysocki 	if (cpufreq_boost_supported()) {
284444139ed4SViresh Kumar 		ret = create_boost_sysfs_file();
284544139ed4SViresh Kumar 		if (ret)
28466f19efc0SLukasz Majewski 			goto err_null_driver;
28477a6c79f2SRafael J. Wysocki 	}
28486f19efc0SLukasz Majewski 
28498a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
28508f5bc2abSJiri Slaby 	if (ret)
28516f19efc0SLukasz Majewski 		goto err_boost_unreg;
28521da177e4SLinus Torvalds 
28535ae4a4b4SViresh Kumar 	if (unlikely(list_empty(&cpufreq_policy_list))) {
28541da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
28556c770036SDavid Arcari 		ret = -ENODEV;
2856ce1bcfe9SViresh Kumar 		pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2857e08f5f5bSGautham R Shenoy 			 driver_data->name);
28588a25a2fdSKay Sievers 		goto err_if_unreg;
28591da177e4SLinus Torvalds 	}
28601da177e4SLinus Torvalds 
2861a92551e4SSebastian Andrzej Siewior 	ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
2862a92551e4SSebastian Andrzej Siewior 						   "cpufreq:online",
2863c4a3fa26SChen Yu 						   cpuhp_cpufreq_online,
2864c4a3fa26SChen Yu 						   cpuhp_cpufreq_offline);
286527622b06SSebastian Andrzej Siewior 	if (ret < 0)
286627622b06SSebastian Andrzej Siewior 		goto err_if_unreg;
286727622b06SSebastian Andrzej Siewior 	hp_online = ret;
28685372e054SSebastian Andrzej Siewior 	ret = 0;
286927622b06SSebastian Andrzej Siewior 
28702d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
28713834abb4SPankaj Gupta 	goto out;
2872fdd320daSRafael J. Wysocki 
28738a25a2fdSKay Sievers err_if_unreg:
28748a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
28756f19efc0SLukasz Majewski err_boost_unreg:
287644139ed4SViresh Kumar 	remove_boost_sysfs_file();
28778f5bc2abSJiri Slaby err_null_driver:
28780d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
28791c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
28800d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
28813834abb4SPankaj Gupta out:
2882a92551e4SSebastian Andrzej Siewior 	cpus_read_unlock();
28833834abb4SPankaj Gupta 	return ret;
28841da177e4SLinus Torvalds }
28851da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
28861da177e4SLinus Torvalds 
2887a9909c21SLee Jones /*
28881da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
28891da177e4SLinus Torvalds  *
28901da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
28911da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
28921da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
28931da177e4SLinus Torvalds  * currently not initialised.
28941da177e4SLinus Torvalds  */
2895221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
28961da177e4SLinus Torvalds {
28971da177e4SLinus Torvalds 	unsigned long flags;
28981da177e4SLinus Torvalds 
28991c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
29001da177e4SLinus Torvalds 		return -EINVAL;
29011da177e4SLinus Torvalds 
29022d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
29031da177e4SLinus Torvalds 
2904454d3a25SSebastian Andrzej Siewior 	/* Protect against concurrent cpu hotplug */
2905a92551e4SSebastian Andrzej Siewior 	cpus_read_lock();
29068a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
290744139ed4SViresh Kumar 	remove_boost_sysfs_file();
2908874f6353SIonela Voinescu 	static_branch_disable_cpuslocked(&cpufreq_freq_invariance);
2909a92551e4SSebastian Andrzej Siewior 	cpuhp_remove_state_nocalls_cpuslocked(hp_online);
29101da177e4SLinus Torvalds 
29110d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
29126eed9404SViresh Kumar 
29131c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
29146eed9404SViresh Kumar 
29150d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2916a92551e4SSebastian Andrzej Siewior 	cpus_read_unlock();
29171da177e4SLinus Torvalds 
29181da177e4SLinus Torvalds 	return 0;
29191da177e4SLinus Torvalds }
29201da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
29215a01f2e8SVenkatesh Pallipadi 
29225a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
29235a01f2e8SVenkatesh Pallipadi {
29248412b456SQuentin Perret 	struct cpufreq_governor *gov = cpufreq_default_governor();
29258412b456SQuentin Perret 
2926a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2927a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2928a7b422cdSKonrad Rzeszutek Wilk 
29298eec1020SViresh Kumar 	cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
29308aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
29318aa84ad8SThomas Renninger 
29328412b456SQuentin Perret 	if (!strlen(default_governor))
29338412b456SQuentin Perret 		strncpy(default_governor, gov->name, CPUFREQ_NAME_LEN);
29348412b456SQuentin Perret 
29355a01f2e8SVenkatesh Pallipadi 	return 0;
29365a01f2e8SVenkatesh Pallipadi }
2937d82f2692SLen Brown module_param(off, int, 0444);
29388412b456SQuentin Perret module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444);
29395a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2940