xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision f13f1184a1f4e046306c33e542b4d654866e5d70)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/drivers/cpufreq/cpufreq.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2001 Russell King
51da177e4SLinus Torvalds  *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6bb176f7dSViresh Kumar  *            (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
71da177e4SLinus Torvalds  *
8c32b6b8eSAshok Raj  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9c32b6b8eSAshok Raj  *	Added handling for CPU hotplug
108ff69732SDave Jones  *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
118ff69732SDave Jones  *	Fix handling for CPU hotplug -- affected CPUs
12c32b6b8eSAshok Raj  *
131da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
141da177e4SLinus Torvalds  * it under the terms of the GNU General Public License version 2 as
151da177e4SLinus Torvalds  * published by the Free Software Foundation.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
18db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19db701151SViresh Kumar 
205ff0a268SViresh Kumar #include <linux/cpu.h>
211da177e4SLinus Torvalds #include <linux/cpufreq.h>
221da177e4SLinus Torvalds #include <linux/delay.h>
231da177e4SLinus Torvalds #include <linux/device.h>
245ff0a268SViresh Kumar #include <linux/init.h>
255ff0a268SViresh Kumar #include <linux/kernel_stat.h>
265ff0a268SViresh Kumar #include <linux/module.h>
273fc54d37Sakpm@osdl.org #include <linux/mutex.h>
285ff0a268SViresh Kumar #include <linux/slab.h>
292f0aea93SViresh Kumar #include <linux/suspend.h>
3090de2a4aSDoug Anderson #include <linux/syscore_ops.h>
315ff0a268SViresh Kumar #include <linux/tick.h>
326f4f2723SThomas Renninger #include <trace/events/power.h>
336f4f2723SThomas Renninger 
341da177e4SLinus Torvalds /**
35cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
361da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
371da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
381da177e4SLinus Torvalds  */
391c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
407a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
418414809cSSrivatsa S. Bhat static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
42bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
436f1e4efdSJane Li DEFINE_MUTEX(cpufreq_governor_lock);
44c88a1f8bSLukasz Majewski static LIST_HEAD(cpufreq_policy_list);
45bb176f7dSViresh Kumar 
46084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */
47e77b89f1SDmitry Monakhov static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
481da177e4SLinus Torvalds 
492f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */
502f0aea93SViresh Kumar static bool cpufreq_suspended;
511da177e4SLinus Torvalds 
529c0ebcf7SViresh Kumar static inline bool has_target(void)
539c0ebcf7SViresh Kumar {
549c0ebcf7SViresh Kumar 	return cpufreq_driver->target_index || cpufreq_driver->target;
559c0ebcf7SViresh Kumar }
569c0ebcf7SViresh Kumar 
575a01f2e8SVenkatesh Pallipadi /*
586eed9404SViresh Kumar  * rwsem to guarantee that cpufreq driver module doesn't unload during critical
596eed9404SViresh Kumar  * sections
606eed9404SViresh Kumar  */
616eed9404SViresh Kumar static DECLARE_RWSEM(cpufreq_rwsem);
626eed9404SViresh Kumar 
631da177e4SLinus Torvalds /* internal prototypes */
6429464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy,
6529464f28SDave Jones 		unsigned int event);
665a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu);
6765f27f38SDavid Howells static void handle_update(struct work_struct *work);
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /**
701da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
711da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
721da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
731da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
741da177e4SLinus Torvalds  * The mutex locks both lists.
751da177e4SLinus Torvalds  */
76e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
77b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list;
781da177e4SLinus Torvalds 
7974212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called;
80b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void)
81b4dfdbb3SAlan Stern {
82b4dfdbb3SAlan Stern 	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
8374212ca4SCesar Eduardo Barros 	init_cpufreq_transition_notifier_list_called = true;
84b4dfdbb3SAlan Stern 	return 0;
85b4dfdbb3SAlan Stern }
86b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list);
871da177e4SLinus Torvalds 
88a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
89da584455SViresh Kumar static int cpufreq_disabled(void)
90a7b422cdSKonrad Rzeszutek Wilk {
91a7b422cdSKonrad Rzeszutek Wilk 	return off;
92a7b422cdSKonrad Rzeszutek Wilk }
93a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
94a7b422cdSKonrad Rzeszutek Wilk {
95a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
96a7b422cdSKonrad Rzeszutek Wilk }
971da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list);
983fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
991da177e4SLinus Torvalds 
1004d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1014d5dcc42SViresh Kumar {
1020b981e70SViresh Kumar 	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
1034d5dcc42SViresh Kumar }
1043f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1054d5dcc42SViresh Kumar 
106944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
107944e9a03SViresh Kumar {
108944e9a03SViresh Kumar 	if (have_governor_per_policy())
109944e9a03SViresh Kumar 		return &policy->kobj;
110944e9a03SViresh Kumar 	else
111944e9a03SViresh Kumar 		return cpufreq_global_kobject;
112944e9a03SViresh Kumar }
113944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
114944e9a03SViresh Kumar 
11572a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
11672a4ce34SViresh Kumar {
11772a4ce34SViresh Kumar 	u64 idle_time;
11872a4ce34SViresh Kumar 	u64 cur_wall_time;
11972a4ce34SViresh Kumar 	u64 busy_time;
12072a4ce34SViresh Kumar 
12172a4ce34SViresh Kumar 	cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
12272a4ce34SViresh Kumar 
12372a4ce34SViresh Kumar 	busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
12472a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
12572a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
12672a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
12772a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
12872a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
12972a4ce34SViresh Kumar 
13072a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
13172a4ce34SViresh Kumar 	if (wall)
13272a4ce34SViresh Kumar 		*wall = cputime_to_usecs(cur_wall_time);
13372a4ce34SViresh Kumar 
13472a4ce34SViresh Kumar 	return cputime_to_usecs(idle_time);
13572a4ce34SViresh Kumar }
13672a4ce34SViresh Kumar 
13772a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
13872a4ce34SViresh Kumar {
13972a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
14072a4ce34SViresh Kumar 
14172a4ce34SViresh Kumar 	if (idle_time == -1ULL)
14272a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
14372a4ce34SViresh Kumar 	else if (!io_busy)
14472a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
14572a4ce34SViresh Kumar 
14672a4ce34SViresh Kumar 	return idle_time;
14772a4ce34SViresh Kumar }
14872a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
14972a4ce34SViresh Kumar 
15070e9e778SViresh Kumar /*
15170e9e778SViresh Kumar  * This is a generic cpufreq init() routine which can be used by cpufreq
15270e9e778SViresh Kumar  * drivers of SMP systems. It will do following:
15370e9e778SViresh Kumar  * - validate & show freq table passed
15470e9e778SViresh Kumar  * - set policies transition latency
15570e9e778SViresh Kumar  * - policy->cpus with all possible CPUs
15670e9e778SViresh Kumar  */
15770e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy,
15870e9e778SViresh Kumar 		struct cpufreq_frequency_table *table,
15970e9e778SViresh Kumar 		unsigned int transition_latency)
16070e9e778SViresh Kumar {
16170e9e778SViresh Kumar 	int ret;
16270e9e778SViresh Kumar 
16370e9e778SViresh Kumar 	ret = cpufreq_table_validate_and_show(policy, table);
16470e9e778SViresh Kumar 	if (ret) {
16570e9e778SViresh Kumar 		pr_err("%s: invalid frequency table: %d\n", __func__, ret);
16670e9e778SViresh Kumar 		return ret;
16770e9e778SViresh Kumar 	}
16870e9e778SViresh Kumar 
16970e9e778SViresh Kumar 	policy->cpuinfo.transition_latency = transition_latency;
17070e9e778SViresh Kumar 
17170e9e778SViresh Kumar 	/*
17270e9e778SViresh Kumar 	 * The driver only supports the SMP configuartion where all processors
17370e9e778SViresh Kumar 	 * share the clock and voltage and clock.
17470e9e778SViresh Kumar 	 */
17570e9e778SViresh Kumar 	cpumask_setall(policy->cpus);
17670e9e778SViresh Kumar 
17770e9e778SViresh Kumar 	return 0;
17870e9e778SViresh Kumar }
17970e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init);
18070e9e778SViresh Kumar 
181652ed95dSViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu)
182652ed95dSViresh Kumar {
183652ed95dSViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
184652ed95dSViresh Kumar 
185652ed95dSViresh Kumar 	if (!policy || IS_ERR(policy->clk)) {
186e837f9b5SJoe Perches 		pr_err("%s: No %s associated to cpu: %d\n",
187e837f9b5SJoe Perches 		       __func__, policy ? "clk" : "policy", cpu);
188652ed95dSViresh Kumar 		return 0;
189652ed95dSViresh Kumar 	}
190652ed95dSViresh Kumar 
191652ed95dSViresh Kumar 	return clk_get_rate(policy->clk) / 1000;
192652ed95dSViresh Kumar }
193652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get);
194652ed95dSViresh Kumar 
195e0b3165bSViresh Kumar /* Only for cpufreq core internal use */
196e0b3165bSViresh Kumar struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
197e0b3165bSViresh Kumar {
198e0b3165bSViresh Kumar 	return per_cpu(cpufreq_cpu_data, cpu);
199e0b3165bSViresh Kumar }
200e0b3165bSViresh Kumar 
2016eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
2021da177e4SLinus Torvalds {
2036eed9404SViresh Kumar 	struct cpufreq_policy *policy = NULL;
2041da177e4SLinus Torvalds 	unsigned long flags;
2051da177e4SLinus Torvalds 
2066eed9404SViresh Kumar 	if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
2076eed9404SViresh Kumar 		return NULL;
2086eed9404SViresh Kumar 
2096eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
2106eed9404SViresh Kumar 		return NULL;
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	/* get the cpufreq driver */
2130d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
2141da177e4SLinus Torvalds 
2156eed9404SViresh Kumar 	if (cpufreq_driver) {
2161da177e4SLinus Torvalds 		/* get the CPU */
2173a3e9e06SViresh Kumar 		policy = per_cpu(cpufreq_cpu_data, cpu);
2186eed9404SViresh Kumar 		if (policy)
2196eed9404SViresh Kumar 			kobject_get(&policy->kobj);
2206eed9404SViresh Kumar 	}
2216eed9404SViresh Kumar 
2226eed9404SViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2231da177e4SLinus Torvalds 
2243a3e9e06SViresh Kumar 	if (!policy)
2256eed9404SViresh Kumar 		up_read(&cpufreq_rwsem);
2261da177e4SLinus Torvalds 
2273a3e9e06SViresh Kumar 	return policy;
228a9144436SStephen Boyd }
2291da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2301da177e4SLinus Torvalds 
2313a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
232a9144436SStephen Boyd {
233d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
234d5aaffa9SDirk Brandewie 		return;
235d5aaffa9SDirk Brandewie 
2366eed9404SViresh Kumar 	kobject_put(&policy->kobj);
2376eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
238a9144436SStephen Boyd }
2391da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds /*********************************************************************
2421da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2431da177e4SLinus Torvalds  *********************************************************************/
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds /**
2461da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
2471da177e4SLinus Torvalds  *
2481da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
2491da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
2501da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
2511da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
2521da177e4SLinus Torvalds  */
2531da177e4SLinus Torvalds #ifndef CONFIG_SMP
2541da177e4SLinus Torvalds static unsigned long l_p_j_ref;
2551da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq;
2561da177e4SLinus Torvalds 
257858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
2581da177e4SLinus Torvalds {
2591da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
2601da177e4SLinus Torvalds 		return;
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
2631da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
2641da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
265e837f9b5SJoe Perches 		pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
266e837f9b5SJoe Perches 			 l_p_j_ref, l_p_j_ref_freq);
2671da177e4SLinus Torvalds 	}
2680b443eadSViresh Kumar 	if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
269e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
270e08f5f5bSGautham R Shenoy 								ci->new);
271e837f9b5SJoe Perches 		pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
272e837f9b5SJoe Perches 			 loops_per_jiffy, ci->new);
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds #else
276e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
277e08f5f5bSGautham R Shenoy {
278e08f5f5bSGautham R Shenoy 	return;
279e08f5f5bSGautham R Shenoy }
2801da177e4SLinus Torvalds #endif
2811da177e4SLinus Torvalds 
2820956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
283b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
2861da177e4SLinus Torvalds 
287d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
288d5aaffa9SDirk Brandewie 		return;
289d5aaffa9SDirk Brandewie 
2901c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
2912d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
292e4472cb3SDave Jones 		 state, freqs->new);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	switch (state) {
295e4472cb3SDave Jones 
2961da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
297e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
298e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
299e4472cb3SDave Jones 		 * "old frequency".
3001da177e4SLinus Torvalds 		 */
3011c3d85ddSRafael J. Wysocki 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
302e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
303e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
304e837f9b5SJoe Perches 				pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
305e4472cb3SDave Jones 					 freqs->old, policy->cur);
306e4472cb3SDave Jones 				freqs->old = policy->cur;
3071da177e4SLinus Torvalds 			}
3081da177e4SLinus Torvalds 		}
309b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
310e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
3111da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3121da177e4SLinus Torvalds 		break;
313e4472cb3SDave Jones 
3141da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3151da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
316e837f9b5SJoe Perches 		pr_debug("FREQ: %lu - CPU: %lu\n",
317e837f9b5SJoe Perches 			 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
31825e41933SThomas Renninger 		trace_cpu_frequency(freqs->new, freqs->cpu);
319b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
320e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
321e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
322e4472cb3SDave Jones 			policy->cur = freqs->new;
3231da177e4SLinus Torvalds 		break;
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds }
326bb176f7dSViresh Kumar 
327b43a7ffbSViresh Kumar /**
328b43a7ffbSViresh Kumar  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
329b43a7ffbSViresh Kumar  * on frequency transition.
330b43a7ffbSViresh Kumar  *
331b43a7ffbSViresh Kumar  * This function calls the transition notifiers and the "adjust_jiffies"
332b43a7ffbSViresh Kumar  * function. It is called twice on all CPU frequency changes that have
333b43a7ffbSViresh Kumar  * external effects.
334b43a7ffbSViresh Kumar  */
335236a9800SViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy,
336b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
337b43a7ffbSViresh Kumar {
338b43a7ffbSViresh Kumar 	for_each_cpu(freqs->cpu, policy->cpus)
339b43a7ffbSViresh Kumar 		__cpufreq_notify_transition(policy, freqs, state);
340b43a7ffbSViresh Kumar }
3411da177e4SLinus Torvalds 
342f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */
343236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
344f7ba3b41SViresh Kumar 		struct cpufreq_freqs *freqs, int transition_failed)
345f7ba3b41SViresh Kumar {
346f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
347f7ba3b41SViresh Kumar 	if (!transition_failed)
348f7ba3b41SViresh Kumar 		return;
349f7ba3b41SViresh Kumar 
350f7ba3b41SViresh Kumar 	swap(freqs->old, freqs->new);
351f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
352f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
353f7ba3b41SViresh Kumar }
354f7ba3b41SViresh Kumar 
35512478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
35612478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs)
35712478cf0SSrivatsa S. Bhat {
358ca654dc3SSrivatsa S. Bhat 
359ca654dc3SSrivatsa S. Bhat 	/*
360ca654dc3SSrivatsa S. Bhat 	 * Catch double invocations of _begin() which lead to self-deadlock.
361ca654dc3SSrivatsa S. Bhat 	 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
362ca654dc3SSrivatsa S. Bhat 	 * doesn't invoke _begin() on their behalf, and hence the chances of
363ca654dc3SSrivatsa S. Bhat 	 * double invocations are very low. Moreover, there are scenarios
364ca654dc3SSrivatsa S. Bhat 	 * where these checks can emit false-positive warnings in these
365ca654dc3SSrivatsa S. Bhat 	 * drivers; so we avoid that by skipping them altogether.
366ca654dc3SSrivatsa S. Bhat 	 */
367ca654dc3SSrivatsa S. Bhat 	WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
368ca654dc3SSrivatsa S. Bhat 				&& current == policy->transition_task);
369ca654dc3SSrivatsa S. Bhat 
37012478cf0SSrivatsa S. Bhat wait:
37112478cf0SSrivatsa S. Bhat 	wait_event(policy->transition_wait, !policy->transition_ongoing);
37212478cf0SSrivatsa S. Bhat 
37312478cf0SSrivatsa S. Bhat 	spin_lock(&policy->transition_lock);
37412478cf0SSrivatsa S. Bhat 
37512478cf0SSrivatsa S. Bhat 	if (unlikely(policy->transition_ongoing)) {
37612478cf0SSrivatsa S. Bhat 		spin_unlock(&policy->transition_lock);
37712478cf0SSrivatsa S. Bhat 		goto wait;
37812478cf0SSrivatsa S. Bhat 	}
37912478cf0SSrivatsa S. Bhat 
38012478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = true;
381ca654dc3SSrivatsa S. Bhat 	policy->transition_task = current;
38212478cf0SSrivatsa S. Bhat 
38312478cf0SSrivatsa S. Bhat 	spin_unlock(&policy->transition_lock);
38412478cf0SSrivatsa S. Bhat 
38512478cf0SSrivatsa S. Bhat 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
38612478cf0SSrivatsa S. Bhat }
38712478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
38812478cf0SSrivatsa S. Bhat 
38912478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
39012478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs, int transition_failed)
39112478cf0SSrivatsa S. Bhat {
39212478cf0SSrivatsa S. Bhat 	if (unlikely(WARN_ON(!policy->transition_ongoing)))
39312478cf0SSrivatsa S. Bhat 		return;
39412478cf0SSrivatsa S. Bhat 
39512478cf0SSrivatsa S. Bhat 	cpufreq_notify_post_transition(policy, freqs, transition_failed);
39612478cf0SSrivatsa S. Bhat 
39712478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = false;
398ca654dc3SSrivatsa S. Bhat 	policy->transition_task = NULL;
39912478cf0SSrivatsa S. Bhat 
40012478cf0SSrivatsa S. Bhat 	wake_up(&policy->transition_wait);
40112478cf0SSrivatsa S. Bhat }
40212478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
40312478cf0SSrivatsa S. Bhat 
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds /*********************************************************************
4061da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
4071da177e4SLinus Torvalds  *********************************************************************/
4088a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj,
4096f19efc0SLukasz Majewski 				 struct attribute *attr, char *buf)
4106f19efc0SLukasz Majewski {
4116f19efc0SLukasz Majewski 	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
4126f19efc0SLukasz Majewski }
4136f19efc0SLukasz Majewski 
4146f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
4156f19efc0SLukasz Majewski 				  const char *buf, size_t count)
4166f19efc0SLukasz Majewski {
4176f19efc0SLukasz Majewski 	int ret, enable;
4186f19efc0SLukasz Majewski 
4196f19efc0SLukasz Majewski 	ret = sscanf(buf, "%d", &enable);
4206f19efc0SLukasz Majewski 	if (ret != 1 || enable < 0 || enable > 1)
4216f19efc0SLukasz Majewski 		return -EINVAL;
4226f19efc0SLukasz Majewski 
4236f19efc0SLukasz Majewski 	if (cpufreq_boost_trigger_state(enable)) {
424e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST!\n",
425e837f9b5SJoe Perches 		       __func__, enable ? "enable" : "disable");
4266f19efc0SLukasz Majewski 		return -EINVAL;
4276f19efc0SLukasz Majewski 	}
4286f19efc0SLukasz Majewski 
429e837f9b5SJoe Perches 	pr_debug("%s: cpufreq BOOST %s\n",
430e837f9b5SJoe Perches 		 __func__, enable ? "enabled" : "disabled");
4316f19efc0SLukasz Majewski 
4326f19efc0SLukasz Majewski 	return count;
4336f19efc0SLukasz Majewski }
4346f19efc0SLukasz Majewski define_one_global_rw(boost);
4351da177e4SLinus Torvalds 
4363bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor)
4373bcb09a3SJeremy Fitzhardinge {
4383bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
4393bcb09a3SJeremy Fitzhardinge 
4403bcb09a3SJeremy Fitzhardinge 	list_for_each_entry(t, &cpufreq_governor_list, governor_list)
4417c4f4539SRasmus Villemoes 		if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
4423bcb09a3SJeremy Fitzhardinge 			return t;
4433bcb09a3SJeremy Fitzhardinge 
4443bcb09a3SJeremy Fitzhardinge 	return NULL;
4453bcb09a3SJeremy Fitzhardinge }
4463bcb09a3SJeremy Fitzhardinge 
4471da177e4SLinus Torvalds /**
4481da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
4491da177e4SLinus Torvalds  */
4501da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
4511da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
4521da177e4SLinus Torvalds {
4533bcb09a3SJeremy Fitzhardinge 	int err = -EINVAL;
4543bcb09a3SJeremy Fitzhardinge 
4551c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver)
4563bcb09a3SJeremy Fitzhardinge 		goto out;
4573bcb09a3SJeremy Fitzhardinge 
4581c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
4597c4f4539SRasmus Villemoes 		if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
4601da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
4613bcb09a3SJeremy Fitzhardinge 			err = 0;
4627c4f4539SRasmus Villemoes 		} else if (!strncasecmp(str_governor, "powersave",
463e08f5f5bSGautham R Shenoy 						CPUFREQ_NAME_LEN)) {
4641da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
4653bcb09a3SJeremy Fitzhardinge 			err = 0;
4661da177e4SLinus Torvalds 		}
4679c0ebcf7SViresh Kumar 	} else if (has_target()) {
4681da177e4SLinus Torvalds 		struct cpufreq_governor *t;
4693bcb09a3SJeremy Fitzhardinge 
4703fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
4713bcb09a3SJeremy Fitzhardinge 
4723bcb09a3SJeremy Fitzhardinge 		t = __find_governor(str_governor);
4733bcb09a3SJeremy Fitzhardinge 
474ea714970SJeremy Fitzhardinge 		if (t == NULL) {
475ea714970SJeremy Fitzhardinge 			int ret;
476ea714970SJeremy Fitzhardinge 
477ea714970SJeremy Fitzhardinge 			mutex_unlock(&cpufreq_governor_mutex);
4781a8e1463SKees Cook 			ret = request_module("cpufreq_%s", str_governor);
479ea714970SJeremy Fitzhardinge 			mutex_lock(&cpufreq_governor_mutex);
480ea714970SJeremy Fitzhardinge 
481ea714970SJeremy Fitzhardinge 			if (ret == 0)
482ea714970SJeremy Fitzhardinge 				t = __find_governor(str_governor);
483ea714970SJeremy Fitzhardinge 		}
484ea714970SJeremy Fitzhardinge 
4853bcb09a3SJeremy Fitzhardinge 		if (t != NULL) {
4861da177e4SLinus Torvalds 			*governor = t;
4873bcb09a3SJeremy Fitzhardinge 			err = 0;
4881da177e4SLinus Torvalds 		}
4893bcb09a3SJeremy Fitzhardinge 
4903bcb09a3SJeremy Fitzhardinge 		mutex_unlock(&cpufreq_governor_mutex);
4911da177e4SLinus Torvalds 	}
4921da177e4SLinus Torvalds out:
4933bcb09a3SJeremy Fitzhardinge 	return err;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds /**
497e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
498e08f5f5bSGautham R Shenoy  * print out cpufreq information
4991da177e4SLinus Torvalds  *
5001da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
5011da177e4SLinus Torvalds  * "unsigned int".
5021da177e4SLinus Torvalds  */
5031da177e4SLinus Torvalds 
5041da177e4SLinus Torvalds #define show_one(file_name, object)			\
5051da177e4SLinus Torvalds static ssize_t show_##file_name				\
5061da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
5071da177e4SLinus Torvalds {							\
5081da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
5121da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
513ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
5141da177e4SLinus Torvalds show_one(scaling_min_freq, min);
5151da177e4SLinus Torvalds show_one(scaling_max_freq, max);
516c034b02eSDirk Brandewie 
517c034b02eSDirk Brandewie static ssize_t show_scaling_cur_freq(
518c034b02eSDirk Brandewie 	struct cpufreq_policy *policy, char *buf)
519c034b02eSDirk Brandewie {
520c034b02eSDirk Brandewie 	ssize_t ret;
521c034b02eSDirk Brandewie 
522c034b02eSDirk Brandewie 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
523c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
524c034b02eSDirk Brandewie 	else
525c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", policy->cur);
526c034b02eSDirk Brandewie 	return ret;
527c034b02eSDirk Brandewie }
5281da177e4SLinus Torvalds 
529037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
5303a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy);
5317970e08bSThomas Renninger 
5321da177e4SLinus Torvalds /**
5331da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
5341da177e4SLinus Torvalds  */
5351da177e4SLinus Torvalds #define store_one(file_name, object)			\
5361da177e4SLinus Torvalds static ssize_t store_##file_name					\
5371da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
5381da177e4SLinus Torvalds {									\
539619c144cSVince Hsu 	int ret, temp;							\
5401da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
5411da177e4SLinus Torvalds 									\
5421da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
5431da177e4SLinus Torvalds 	if (ret)							\
5441da177e4SLinus Torvalds 		return -EINVAL;						\
5451da177e4SLinus Torvalds 									\
5461da177e4SLinus Torvalds 	ret = sscanf(buf, "%u", &new_policy.object);			\
5471da177e4SLinus Torvalds 	if (ret != 1)							\
5481da177e4SLinus Torvalds 		return -EINVAL;						\
5491da177e4SLinus Torvalds 									\
550619c144cSVince Hsu 	temp = new_policy.object;					\
551037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);		\
552619c144cSVince Hsu 	if (!ret)							\
553619c144cSVince Hsu 		policy->user_policy.object = temp;			\
5541da177e4SLinus Torvalds 									\
5551da177e4SLinus Torvalds 	return ret ? ret : count;					\
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds store_one(scaling_min_freq, min);
5591da177e4SLinus Torvalds store_one(scaling_max_freq, max);
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds /**
5621da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
5631da177e4SLinus Torvalds  */
564e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
565e08f5f5bSGautham R Shenoy 					char *buf)
5661da177e4SLinus Torvalds {
5675a01f2e8SVenkatesh Pallipadi 	unsigned int cur_freq = __cpufreq_get(policy->cpu);
5681da177e4SLinus Torvalds 	if (!cur_freq)
5691da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
5701da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
5711da177e4SLinus Torvalds }
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds /**
5741da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
5751da177e4SLinus Torvalds  */
576905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
5771da177e4SLinus Torvalds {
5781da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
5791da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
5801da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
5811da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
5821da177e4SLinus Torvalds 	else if (policy->governor)
5834b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
58429464f28SDave Jones 				policy->governor->name);
5851da177e4SLinus Torvalds 	return -EINVAL;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds /**
5891da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
5901da177e4SLinus Torvalds  */
5911da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
5921da177e4SLinus Torvalds 					const char *buf, size_t count)
5931da177e4SLinus Torvalds {
5945136fa56SSrivatsa S. Bhat 	int ret;
5951da177e4SLinus Torvalds 	char	str_governor[16];
5961da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);
5991da177e4SLinus Torvalds 	if (ret)
6001da177e4SLinus Torvalds 		return ret;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
6031da177e4SLinus Torvalds 	if (ret != 1)
6041da177e4SLinus Torvalds 		return -EINVAL;
6051da177e4SLinus Torvalds 
606e08f5f5bSGautham R Shenoy 	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
607e08f5f5bSGautham R Shenoy 						&new_policy.governor))
6081da177e4SLinus Torvalds 		return -EINVAL;
6091da177e4SLinus Torvalds 
610037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
6117970e08bSThomas Renninger 
6127970e08bSThomas Renninger 	policy->user_policy.policy = policy->policy;
6137970e08bSThomas Renninger 	policy->user_policy.governor = policy->governor;
6147970e08bSThomas Renninger 
615e08f5f5bSGautham R Shenoy 	if (ret)
616e08f5f5bSGautham R Shenoy 		return ret;
617e08f5f5bSGautham R Shenoy 	else
618e08f5f5bSGautham R Shenoy 		return count;
6191da177e4SLinus Torvalds }
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds /**
6221da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
6231da177e4SLinus Torvalds  */
6241da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
6251da177e4SLinus Torvalds {
6261c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds /**
6301da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
6311da177e4SLinus Torvalds  */
6321da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
6331da177e4SLinus Torvalds 						char *buf)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	ssize_t i = 0;
6361da177e4SLinus Torvalds 	struct cpufreq_governor *t;
6371da177e4SLinus Torvalds 
6389c0ebcf7SViresh Kumar 	if (!has_target()) {
6391da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
6401da177e4SLinus Torvalds 		goto out;
6411da177e4SLinus Torvalds 	}
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
64429464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
64529464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
6461da177e4SLinus Torvalds 			goto out;
6474b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
6481da177e4SLinus Torvalds 	}
6491da177e4SLinus Torvalds out:
6501da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
6511da177e4SLinus Torvalds 	return i;
6521da177e4SLinus Torvalds }
653e8628dd0SDarrick J. Wong 
654f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
6551da177e4SLinus Torvalds {
6561da177e4SLinus Torvalds 	ssize_t i = 0;
6571da177e4SLinus Torvalds 	unsigned int cpu;
6581da177e4SLinus Torvalds 
659835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
6601da177e4SLinus Torvalds 		if (i)
6611da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
6621da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
6631da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
6641da177e4SLinus Torvalds 			break;
6651da177e4SLinus Torvalds 	}
6661da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
6671da177e4SLinus Torvalds 	return i;
6681da177e4SLinus Torvalds }
669f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
6701da177e4SLinus Torvalds 
671e8628dd0SDarrick J. Wong /**
672e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
673e8628dd0SDarrick J. Wong  * hw coordination is in use
674e8628dd0SDarrick J. Wong  */
675e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
676e8628dd0SDarrick J. Wong {
677f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
678e8628dd0SDarrick J. Wong }
679e8628dd0SDarrick J. Wong 
680e8628dd0SDarrick J. Wong /**
681e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
682e8628dd0SDarrick J. Wong  */
683e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
684e8628dd0SDarrick J. Wong {
685f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
686e8628dd0SDarrick J. Wong }
687e8628dd0SDarrick J. Wong 
6889e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
6899e76988eSVenki Pallipadi 					const char *buf, size_t count)
6909e76988eSVenki Pallipadi {
6919e76988eSVenki Pallipadi 	unsigned int freq = 0;
6929e76988eSVenki Pallipadi 	unsigned int ret;
6939e76988eSVenki Pallipadi 
694879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
6959e76988eSVenki Pallipadi 		return -EINVAL;
6969e76988eSVenki Pallipadi 
6979e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
6989e76988eSVenki Pallipadi 	if (ret != 1)
6999e76988eSVenki Pallipadi 		return -EINVAL;
7009e76988eSVenki Pallipadi 
7019e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
7029e76988eSVenki Pallipadi 
7039e76988eSVenki Pallipadi 	return count;
7049e76988eSVenki Pallipadi }
7059e76988eSVenki Pallipadi 
7069e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
7079e76988eSVenki Pallipadi {
708879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
7099e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
7109e76988eSVenki Pallipadi 
7119e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
7129e76988eSVenki Pallipadi }
7131da177e4SLinus Torvalds 
714e2f74f35SThomas Renninger /**
7158bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
716e2f74f35SThomas Renninger  */
717e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
718e2f74f35SThomas Renninger {
719e2f74f35SThomas Renninger 	unsigned int limit;
720e2f74f35SThomas Renninger 	int ret;
7211c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
7221c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
723e2f74f35SThomas Renninger 		if (!ret)
724e2f74f35SThomas Renninger 			return sprintf(buf, "%u\n", limit);
725e2f74f35SThomas Renninger 	}
726e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
727e2f74f35SThomas Renninger }
728e2f74f35SThomas Renninger 
7296dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
7306dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
7316dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
7326dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
7336dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
7346dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
7356dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
7366dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
7376dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
7386dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
7396dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
7406dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
7416dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
7426dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds static struct attribute *default_attrs[] = {
7451da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
7461da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
747ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
7481da177e4SLinus Torvalds 	&scaling_min_freq.attr,
7491da177e4SLinus Torvalds 	&scaling_max_freq.attr,
7501da177e4SLinus Torvalds 	&affected_cpus.attr,
751e8628dd0SDarrick J. Wong 	&related_cpus.attr,
7521da177e4SLinus Torvalds 	&scaling_governor.attr,
7531da177e4SLinus Torvalds 	&scaling_driver.attr,
7541da177e4SLinus Torvalds 	&scaling_available_governors.attr,
7559e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
7561da177e4SLinus Torvalds 	NULL
7571da177e4SLinus Torvalds };
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
7601da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
7631da177e4SLinus Torvalds {
7641da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7651da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
7661b750e3bSViresh Kumar 	ssize_t ret;
7676eed9404SViresh Kumar 
7686eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
7691b750e3bSViresh Kumar 		return -EINVAL;
7705a01f2e8SVenkatesh Pallipadi 
771ad7722daSviresh kumar 	down_read(&policy->rwsem);
7725a01f2e8SVenkatesh Pallipadi 
773e08f5f5bSGautham R Shenoy 	if (fattr->show)
774e08f5f5bSGautham R Shenoy 		ret = fattr->show(policy, buf);
775e08f5f5bSGautham R Shenoy 	else
776e08f5f5bSGautham R Shenoy 		ret = -EIO;
777e08f5f5bSGautham R Shenoy 
778ad7722daSviresh kumar 	up_read(&policy->rwsem);
7796eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
7801b750e3bSViresh Kumar 
7811da177e4SLinus Torvalds 	return ret;
7821da177e4SLinus Torvalds }
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
7851da177e4SLinus Torvalds 		     const char *buf, size_t count)
7861da177e4SLinus Torvalds {
7871da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7881da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
789a07530b4SDave Jones 	ssize_t ret = -EINVAL;
7906eed9404SViresh Kumar 
7914f750c93SSrivatsa S. Bhat 	get_online_cpus();
7924f750c93SSrivatsa S. Bhat 
7934f750c93SSrivatsa S. Bhat 	if (!cpu_online(policy->cpu))
7944f750c93SSrivatsa S. Bhat 		goto unlock;
7954f750c93SSrivatsa S. Bhat 
7966eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
7974f750c93SSrivatsa S. Bhat 		goto unlock;
7985a01f2e8SVenkatesh Pallipadi 
799ad7722daSviresh kumar 	down_write(&policy->rwsem);
8005a01f2e8SVenkatesh Pallipadi 
801e08f5f5bSGautham R Shenoy 	if (fattr->store)
802e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
803e08f5f5bSGautham R Shenoy 	else
804e08f5f5bSGautham R Shenoy 		ret = -EIO;
805e08f5f5bSGautham R Shenoy 
806ad7722daSviresh kumar 	up_write(&policy->rwsem);
8076eed9404SViresh Kumar 
8086eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
8094f750c93SSrivatsa S. Bhat unlock:
8104f750c93SSrivatsa S. Bhat 	put_online_cpus();
8114f750c93SSrivatsa S. Bhat 
8121da177e4SLinus Torvalds 	return ret;
8131da177e4SLinus Torvalds }
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
8161da177e4SLinus Torvalds {
8171da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
8182d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
8191da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
8201da177e4SLinus Torvalds }
8211da177e4SLinus Torvalds 
82252cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
8231da177e4SLinus Torvalds 	.show	= show,
8241da177e4SLinus Torvalds 	.store	= store,
8251da177e4SLinus Torvalds };
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
8281da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
8291da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
8301da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
8311da177e4SLinus Torvalds };
8321da177e4SLinus Torvalds 
8332361be23SViresh Kumar struct kobject *cpufreq_global_kobject;
8342361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject);
8352361be23SViresh Kumar 
8362361be23SViresh Kumar static int cpufreq_global_kobject_usage;
8372361be23SViresh Kumar 
8382361be23SViresh Kumar int cpufreq_get_global_kobject(void)
8392361be23SViresh Kumar {
8402361be23SViresh Kumar 	if (!cpufreq_global_kobject_usage++)
8412361be23SViresh Kumar 		return kobject_add(cpufreq_global_kobject,
8422361be23SViresh Kumar 				&cpu_subsys.dev_root->kobj, "%s", "cpufreq");
8432361be23SViresh Kumar 
8442361be23SViresh Kumar 	return 0;
8452361be23SViresh Kumar }
8462361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject);
8472361be23SViresh Kumar 
8482361be23SViresh Kumar void cpufreq_put_global_kobject(void)
8492361be23SViresh Kumar {
8502361be23SViresh Kumar 	if (!--cpufreq_global_kobject_usage)
8512361be23SViresh Kumar 		kobject_del(cpufreq_global_kobject);
8522361be23SViresh Kumar }
8532361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject);
8542361be23SViresh Kumar 
8552361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr)
8562361be23SViresh Kumar {
8572361be23SViresh Kumar 	int ret = cpufreq_get_global_kobject();
8582361be23SViresh Kumar 
8592361be23SViresh Kumar 	if (!ret) {
8602361be23SViresh Kumar 		ret = sysfs_create_file(cpufreq_global_kobject, attr);
8612361be23SViresh Kumar 		if (ret)
8622361be23SViresh Kumar 			cpufreq_put_global_kobject();
8632361be23SViresh Kumar 	}
8642361be23SViresh Kumar 
8652361be23SViresh Kumar 	return ret;
8662361be23SViresh Kumar }
8672361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file);
8682361be23SViresh Kumar 
8692361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr)
8702361be23SViresh Kumar {
8712361be23SViresh Kumar 	sysfs_remove_file(cpufreq_global_kobject, attr);
8722361be23SViresh Kumar 	cpufreq_put_global_kobject();
8732361be23SViresh Kumar }
8742361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
8752361be23SViresh Kumar 
87619d6f7ecSDave Jones /* symlink affected CPUs */
877308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
87819d6f7ecSDave Jones {
87919d6f7ecSDave Jones 	unsigned int j;
88019d6f7ecSDave Jones 	int ret = 0;
88119d6f7ecSDave Jones 
88219d6f7ecSDave Jones 	for_each_cpu(j, policy->cpus) {
8838a25a2fdSKay Sievers 		struct device *cpu_dev;
88419d6f7ecSDave Jones 
885308b60e7SViresh Kumar 		if (j == policy->cpu)
88619d6f7ecSDave Jones 			continue;
88719d6f7ecSDave Jones 
888e8fdde10SViresh Kumar 		pr_debug("Adding link for CPU: %u\n", j);
8898a25a2fdSKay Sievers 		cpu_dev = get_cpu_device(j);
8908a25a2fdSKay Sievers 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
89119d6f7ecSDave Jones 					"cpufreq");
89271c3461eSRafael J. Wysocki 		if (ret)
89371c3461eSRafael J. Wysocki 			break;
89419d6f7ecSDave Jones 	}
89519d6f7ecSDave Jones 	return ret;
89619d6f7ecSDave Jones }
89719d6f7ecSDave Jones 
898308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
8998a25a2fdSKay Sievers 				     struct device *dev)
900909a694eSDave Jones {
901909a694eSDave Jones 	struct freq_attr **drv_attr;
902909a694eSDave Jones 	int ret = 0;
903909a694eSDave Jones 
904909a694eSDave Jones 	/* set up files for this cpu device */
9051c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
906*f13f1184SViresh Kumar 	while (drv_attr && *drv_attr) {
907909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
908909a694eSDave Jones 		if (ret)
9096d4e81edSTomeu Vizoso 			return ret;
910909a694eSDave Jones 		drv_attr++;
911909a694eSDave Jones 	}
9121c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
913909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
914909a694eSDave Jones 		if (ret)
9156d4e81edSTomeu Vizoso 			return ret;
916909a694eSDave Jones 	}
917c034b02eSDirk Brandewie 
918909a694eSDave Jones 	ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
919909a694eSDave Jones 	if (ret)
9206d4e81edSTomeu Vizoso 		return ret;
921c034b02eSDirk Brandewie 
9221c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
923e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
924e2f74f35SThomas Renninger 		if (ret)
9256d4e81edSTomeu Vizoso 			return ret;
926e2f74f35SThomas Renninger 	}
927909a694eSDave Jones 
9286d4e81edSTomeu Vizoso 	return cpufreq_add_dev_symlink(policy);
929e18f1682SSrivatsa S. Bhat }
930e18f1682SSrivatsa S. Bhat 
931e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy)
932e18f1682SSrivatsa S. Bhat {
9336e2c89d1Sviresh kumar 	struct cpufreq_governor *gov = NULL;
934e18f1682SSrivatsa S. Bhat 	struct cpufreq_policy new_policy;
935e18f1682SSrivatsa S. Bhat 	int ret = 0;
936e18f1682SSrivatsa S. Bhat 
937d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
938a27a9ab7SJason Baron 
9396e2c89d1Sviresh kumar 	/* Update governor of new_policy to the governor used before hotplug */
9406e2c89d1Sviresh kumar 	gov = __find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu));
9416e2c89d1Sviresh kumar 	if (gov)
9426e2c89d1Sviresh kumar 		pr_debug("Restoring governor %s for cpu %d\n",
9436e2c89d1Sviresh kumar 				policy->governor->name, policy->cpu);
9446e2c89d1Sviresh kumar 	else
9456e2c89d1Sviresh kumar 		gov = CPUFREQ_DEFAULT_GOVERNOR;
9466e2c89d1Sviresh kumar 
9476e2c89d1Sviresh kumar 	new_policy.governor = gov;
9486e2c89d1Sviresh kumar 
949a27a9ab7SJason Baron 	/* Use the default policy if its valid. */
950a27a9ab7SJason Baron 	if (cpufreq_driver->setpolicy)
9516e2c89d1Sviresh kumar 		cpufreq_parse_governor(gov->name, &new_policy.policy, NULL);
952ecf7e461SDave Jones 
953ecf7e461SDave Jones 	/* set default policy */
954037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
955ecf7e461SDave Jones 	if (ret) {
9562d06d8c4SDominik Brodowski 		pr_debug("setting policy failed\n");
9571c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
9581c3d85ddSRafael J. Wysocki 			cpufreq_driver->exit(policy);
959ecf7e461SDave Jones 	}
960909a694eSDave Jones }
961909a694eSDave Jones 
962fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
963d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
96442f921a6SViresh Kumar 				  unsigned int cpu, struct device *dev)
965fcf80582SViresh Kumar {
9669c0ebcf7SViresh Kumar 	int ret = 0;
967fcf80582SViresh Kumar 	unsigned long flags;
968fcf80582SViresh Kumar 
9699c0ebcf7SViresh Kumar 	if (has_target()) {
9703de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
9713de9bdebSViresh Kumar 		if (ret) {
9723de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
9733de9bdebSViresh Kumar 			return ret;
9743de9bdebSViresh Kumar 		}
9753de9bdebSViresh Kumar 	}
976fcf80582SViresh Kumar 
977ad7722daSviresh kumar 	down_write(&policy->rwsem);
9782eaa3e2dSViresh Kumar 
9790d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
9802eaa3e2dSViresh Kumar 
981fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
982fcf80582SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = policy;
9830d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
984fcf80582SViresh Kumar 
985ad7722daSviresh kumar 	up_write(&policy->rwsem);
9862eaa3e2dSViresh Kumar 
9879c0ebcf7SViresh Kumar 	if (has_target()) {
988e5c87b76SStratos Karafotis 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
989e5c87b76SStratos Karafotis 		if (!ret)
990e5c87b76SStratos Karafotis 			ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
991e5c87b76SStratos Karafotis 
992e5c87b76SStratos Karafotis 		if (ret) {
9933de9bdebSViresh Kumar 			pr_err("%s: Failed to start governor\n", __func__);
9943de9bdebSViresh Kumar 			return ret;
9953de9bdebSViresh Kumar 		}
996820c6ca2SViresh Kumar 	}
997fcf80582SViresh Kumar 
99842f921a6SViresh Kumar 	return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
999fcf80582SViresh Kumar }
1000fcf80582SViresh Kumar #endif
10011da177e4SLinus Torvalds 
10028414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
10038414809cSSrivatsa S. Bhat {
10048414809cSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
10058414809cSSrivatsa S. Bhat 	unsigned long flags;
10068414809cSSrivatsa S. Bhat 
100744871c9cSLan Tianyu 	read_lock_irqsave(&cpufreq_driver_lock, flags);
10088414809cSSrivatsa S. Bhat 
10098414809cSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
10108414809cSSrivatsa S. Bhat 
101144871c9cSLan Tianyu 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
10128414809cSSrivatsa S. Bhat 
101309712f55SGeert Uytterhoeven 	if (policy)
10146e2c89d1Sviresh kumar 		policy->governor = NULL;
10156e2c89d1Sviresh kumar 
10168414809cSSrivatsa S. Bhat 	return policy;
10178414809cSSrivatsa S. Bhat }
10188414809cSSrivatsa S. Bhat 
1019e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void)
1020e9698cc5SSrivatsa S. Bhat {
1021e9698cc5SSrivatsa S. Bhat 	struct cpufreq_policy *policy;
1022e9698cc5SSrivatsa S. Bhat 
1023e9698cc5SSrivatsa S. Bhat 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1024e9698cc5SSrivatsa S. Bhat 	if (!policy)
1025e9698cc5SSrivatsa S. Bhat 		return NULL;
1026e9698cc5SSrivatsa S. Bhat 
1027e9698cc5SSrivatsa S. Bhat 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1028e9698cc5SSrivatsa S. Bhat 		goto err_free_policy;
1029e9698cc5SSrivatsa S. Bhat 
1030e9698cc5SSrivatsa S. Bhat 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1031e9698cc5SSrivatsa S. Bhat 		goto err_free_cpumask;
1032e9698cc5SSrivatsa S. Bhat 
1033c88a1f8bSLukasz Majewski 	INIT_LIST_HEAD(&policy->policy_list);
1034ad7722daSviresh kumar 	init_rwsem(&policy->rwsem);
103512478cf0SSrivatsa S. Bhat 	spin_lock_init(&policy->transition_lock);
103612478cf0SSrivatsa S. Bhat 	init_waitqueue_head(&policy->transition_wait);
1037ad7722daSviresh kumar 
1038e9698cc5SSrivatsa S. Bhat 	return policy;
1039e9698cc5SSrivatsa S. Bhat 
1040e9698cc5SSrivatsa S. Bhat err_free_cpumask:
1041e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1042e9698cc5SSrivatsa S. Bhat err_free_policy:
1043e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1044e9698cc5SSrivatsa S. Bhat 
1045e9698cc5SSrivatsa S. Bhat 	return NULL;
1046e9698cc5SSrivatsa S. Bhat }
1047e9698cc5SSrivatsa S. Bhat 
104842f921a6SViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
104942f921a6SViresh Kumar {
105042f921a6SViresh Kumar 	struct kobject *kobj;
105142f921a6SViresh Kumar 	struct completion *cmp;
105242f921a6SViresh Kumar 
1053fcd7af91SViresh Kumar 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1054fcd7af91SViresh Kumar 			CPUFREQ_REMOVE_POLICY, policy);
1055fcd7af91SViresh Kumar 
105642f921a6SViresh Kumar 	down_read(&policy->rwsem);
105742f921a6SViresh Kumar 	kobj = &policy->kobj;
105842f921a6SViresh Kumar 	cmp = &policy->kobj_unregister;
105942f921a6SViresh Kumar 	up_read(&policy->rwsem);
106042f921a6SViresh Kumar 	kobject_put(kobj);
106142f921a6SViresh Kumar 
106242f921a6SViresh Kumar 	/*
106342f921a6SViresh Kumar 	 * We need to make sure that the underlying kobj is
106442f921a6SViresh Kumar 	 * actually not referenced anymore by anybody before we
106542f921a6SViresh Kumar 	 * proceed with unloading.
106642f921a6SViresh Kumar 	 */
106742f921a6SViresh Kumar 	pr_debug("waiting for dropping of refcount\n");
106842f921a6SViresh Kumar 	wait_for_completion(cmp);
106942f921a6SViresh Kumar 	pr_debug("wait complete\n");
107042f921a6SViresh Kumar }
107142f921a6SViresh Kumar 
1072e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy)
1073e9698cc5SSrivatsa S. Bhat {
1074e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
1075e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1076e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1077e9698cc5SSrivatsa S. Bhat }
1078e9698cc5SSrivatsa S. Bhat 
10791bfb425bSViresh Kumar static int update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu,
10801bfb425bSViresh Kumar 			     struct device *cpu_dev)
10810d66b91eSSrivatsa S. Bhat {
10821bfb425bSViresh Kumar 	int ret;
10831bfb425bSViresh Kumar 
108499ec899eSSrivatsa S. Bhat 	if (WARN_ON(cpu == policy->cpu))
10851bfb425bSViresh Kumar 		return 0;
10861bfb425bSViresh Kumar 
10871bfb425bSViresh Kumar 	/* Move kobject to the new policy->cpu */
10881bfb425bSViresh Kumar 	ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
10891bfb425bSViresh Kumar 	if (ret) {
10901bfb425bSViresh Kumar 		pr_err("%s: Failed to move kobj: %d\n", __func__, ret);
10911bfb425bSViresh Kumar 		return ret;
10921bfb425bSViresh Kumar 	}
1093cb38ed5cSSrivatsa S. Bhat 
1094ad7722daSviresh kumar 	down_write(&policy->rwsem);
10958efd5765SViresh Kumar 
10960d66b91eSSrivatsa S. Bhat 	policy->last_cpu = policy->cpu;
10970d66b91eSSrivatsa S. Bhat 	policy->cpu = cpu;
10980d66b91eSSrivatsa S. Bhat 
1099ad7722daSviresh kumar 	up_write(&policy->rwsem);
11008efd5765SViresh Kumar 
11010d66b91eSSrivatsa S. Bhat 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
11020d66b91eSSrivatsa S. Bhat 			CPUFREQ_UPDATE_POLICY_CPU, policy);
11031bfb425bSViresh Kumar 
11041bfb425bSViresh Kumar 	return 0;
11050d66b91eSSrivatsa S. Bhat }
11060d66b91eSSrivatsa S. Bhat 
110796bbbe4aSViresh Kumar static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
11081da177e4SLinus Torvalds {
1109fcf80582SViresh Kumar 	unsigned int j, cpu = dev->id;
111065922465SViresh Kumar 	int ret = -ENOMEM;
11111da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
11121da177e4SLinus Torvalds 	unsigned long flags;
111396bbbe4aSViresh Kumar 	bool recover_policy = cpufreq_suspended;
111490e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
11151b274294SViresh Kumar 	struct cpufreq_policy *tpolicy;
111690e41bacSPrarit Bhargava #endif
11171da177e4SLinus Torvalds 
1118c32b6b8eSAshok Raj 	if (cpu_is_offline(cpu))
1119c32b6b8eSAshok Raj 		return 0;
1120c32b6b8eSAshok Raj 
11212d06d8c4SDominik Brodowski 	pr_debug("adding CPU %u\n", cpu);
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds #ifdef CONFIG_SMP
11241da177e4SLinus Torvalds 	/* check whether a different CPU already registered this
11251da177e4SLinus Torvalds 	 * CPU because it is in the same boat. */
11261da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(cpu);
11271da177e4SLinus Torvalds 	if (unlikely(policy)) {
11288ff69732SDave Jones 		cpufreq_cpu_put(policy);
11291da177e4SLinus Torvalds 		return 0;
11301da177e4SLinus Torvalds 	}
11315025d628SLi Zhong #endif
1132fcf80582SViresh Kumar 
11336eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
11346eed9404SViresh Kumar 		return 0;
11356eed9404SViresh Kumar 
1136fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
1137fcf80582SViresh Kumar 	/* Check if this cpu was hot-unplugged earlier and has siblings */
11380d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
11391b274294SViresh Kumar 	list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
11401b274294SViresh Kumar 		if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
11410d1857a1SNathan Zimmer 			read_unlock_irqrestore(&cpufreq_driver_lock, flags);
114242f921a6SViresh Kumar 			ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
11436eed9404SViresh Kumar 			up_read(&cpufreq_rwsem);
11446eed9404SViresh Kumar 			return ret;
1145fcf80582SViresh Kumar 		}
11462eaa3e2dSViresh Kumar 	}
11470d1857a1SNathan Zimmer 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1148fcf80582SViresh Kumar #endif
11491da177e4SLinus Torvalds 
115072368d12SRafael J. Wysocki 	/*
115172368d12SRafael J. Wysocki 	 * Restore the saved policy when doing light-weight init and fall back
115272368d12SRafael J. Wysocki 	 * to the full init if that fails.
115372368d12SRafael J. Wysocki 	 */
115496bbbe4aSViresh Kumar 	policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL;
115572368d12SRafael J. Wysocki 	if (!policy) {
115696bbbe4aSViresh Kumar 		recover_policy = false;
1157e9698cc5SSrivatsa S. Bhat 		policy = cpufreq_policy_alloc();
1158059019a3SDave Jones 		if (!policy)
11591da177e4SLinus Torvalds 			goto nomem_out;
116072368d12SRafael J. Wysocki 	}
11610d66b91eSSrivatsa S. Bhat 
11620d66b91eSSrivatsa S. Bhat 	/*
11630d66b91eSSrivatsa S. Bhat 	 * In the resume path, since we restore a saved policy, the assignment
11640d66b91eSSrivatsa S. Bhat 	 * to policy->cpu is like an update of the existing policy, rather than
11650d66b91eSSrivatsa S. Bhat 	 * the creation of a brand new one. So we need to perform this update
11660d66b91eSSrivatsa S. Bhat 	 * by invoking update_policy_cpu().
11670d66b91eSSrivatsa S. Bhat 	 */
11681bfb425bSViresh Kumar 	if (recover_policy && cpu != policy->cpu)
11691bfb425bSViresh Kumar 		WARN_ON(update_policy_cpu(policy, cpu, dev));
11701bfb425bSViresh Kumar 	else
11711da177e4SLinus Torvalds 		policy->cpu = cpu;
11720d66b91eSSrivatsa S. Bhat 
1173835481d9SRusty Russell 	cpumask_copy(policy->cpus, cpumask_of(cpu));
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	init_completion(&policy->kobj_unregister);
117665f27f38SDavid Howells 	INIT_WORK(&policy->update, handle_update);
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
11791da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
11801da177e4SLinus Torvalds 	 */
11811c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->init(policy);
11821da177e4SLinus Torvalds 	if (ret) {
11832d06d8c4SDominik Brodowski 		pr_debug("initialization failed\n");
11842eaa3e2dSViresh Kumar 		goto err_set_policy_cpu;
11851da177e4SLinus Torvalds 	}
1186643ae6e8SViresh Kumar 
11876d4e81edSTomeu Vizoso 	down_write(&policy->rwsem);
11886d4e81edSTomeu Vizoso 
11895a7e56a5SViresh Kumar 	/* related cpus should atleast have policy->cpus */
11905a7e56a5SViresh Kumar 	cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
11915a7e56a5SViresh Kumar 
11925a7e56a5SViresh Kumar 	/*
11935a7e56a5SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
11945a7e56a5SViresh Kumar 	 * managing offline cpus here.
11955a7e56a5SViresh Kumar 	 */
11965a7e56a5SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
11975a7e56a5SViresh Kumar 
119896bbbe4aSViresh Kumar 	if (!recover_policy) {
11995a7e56a5SViresh Kumar 		policy->user_policy.min = policy->min;
12005a7e56a5SViresh Kumar 		policy->user_policy.max = policy->max;
12016d4e81edSTomeu Vizoso 
12026d4e81edSTomeu Vizoso 		/* prepare interface data */
12036d4e81edSTomeu Vizoso 		ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
12046d4e81edSTomeu Vizoso 					   &dev->kobj, "cpufreq");
12056d4e81edSTomeu Vizoso 		if (ret) {
12066d4e81edSTomeu Vizoso 			pr_err("%s: failed to init policy->kobj: %d\n",
12076d4e81edSTomeu Vizoso 			       __func__, ret);
12086d4e81edSTomeu Vizoso 			goto err_init_policy_kobj;
12096d4e81edSTomeu Vizoso 		}
12105a7e56a5SViresh Kumar 	}
12115a7e56a5SViresh Kumar 
1212652ed95dSViresh Kumar 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1213652ed95dSViresh Kumar 	for_each_cpu(j, policy->cpus)
1214652ed95dSViresh Kumar 		per_cpu(cpufreq_cpu_data, j) = policy;
1215652ed95dSViresh Kumar 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1216652ed95dSViresh Kumar 
12172ed99e39SRafael J. Wysocki 	if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1218da60ce9fSViresh Kumar 		policy->cur = cpufreq_driver->get(policy->cpu);
1219da60ce9fSViresh Kumar 		if (!policy->cur) {
1220da60ce9fSViresh Kumar 			pr_err("%s: ->get() failed\n", __func__);
1221da60ce9fSViresh Kumar 			goto err_get_freq;
1222da60ce9fSViresh Kumar 		}
1223da60ce9fSViresh Kumar 	}
1224da60ce9fSViresh Kumar 
1225d3916691SViresh Kumar 	/*
1226d3916691SViresh Kumar 	 * Sometimes boot loaders set CPU frequency to a value outside of
1227d3916691SViresh Kumar 	 * frequency table present with cpufreq core. In such cases CPU might be
1228d3916691SViresh Kumar 	 * unstable if it has to run on that frequency for long duration of time
1229d3916691SViresh Kumar 	 * and so its better to set it to a frequency which is specified in
1230d3916691SViresh Kumar 	 * freq-table. This also makes cpufreq stats inconsistent as
1231d3916691SViresh Kumar 	 * cpufreq-stats would fail to register because current frequency of CPU
1232d3916691SViresh Kumar 	 * isn't found in freq-table.
1233d3916691SViresh Kumar 	 *
1234d3916691SViresh Kumar 	 * Because we don't want this change to effect boot process badly, we go
1235d3916691SViresh Kumar 	 * for the next freq which is >= policy->cur ('cur' must be set by now,
1236d3916691SViresh Kumar 	 * otherwise we will end up setting freq to lowest of the table as 'cur'
1237d3916691SViresh Kumar 	 * is initialized to zero).
1238d3916691SViresh Kumar 	 *
1239d3916691SViresh Kumar 	 * We are passing target-freq as "policy->cur - 1" otherwise
1240d3916691SViresh Kumar 	 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1241d3916691SViresh Kumar 	 * equal to target-freq.
1242d3916691SViresh Kumar 	 */
1243d3916691SViresh Kumar 	if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1244d3916691SViresh Kumar 	    && has_target()) {
1245d3916691SViresh Kumar 		/* Are we running at unknown frequency ? */
1246d3916691SViresh Kumar 		ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1247d3916691SViresh Kumar 		if (ret == -EINVAL) {
1248d3916691SViresh Kumar 			/* Warn user and fix it */
1249d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1250d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1251d3916691SViresh Kumar 			ret = __cpufreq_driver_target(policy, policy->cur - 1,
1252d3916691SViresh Kumar 				CPUFREQ_RELATION_L);
1253d3916691SViresh Kumar 
1254d3916691SViresh Kumar 			/*
1255d3916691SViresh Kumar 			 * Reaching here after boot in a few seconds may not
1256d3916691SViresh Kumar 			 * mean that system will remain stable at "unknown"
1257d3916691SViresh Kumar 			 * frequency for longer duration. Hence, a BUG_ON().
1258d3916691SViresh Kumar 			 */
1259d3916691SViresh Kumar 			BUG_ON(ret);
1260d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1261d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1262d3916691SViresh Kumar 		}
1263d3916691SViresh Kumar 	}
1264d3916691SViresh Kumar 
1265a1531acdSThomas Renninger 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1266a1531acdSThomas Renninger 				     CPUFREQ_START, policy);
1267a1531acdSThomas Renninger 
126896bbbe4aSViresh Kumar 	if (!recover_policy) {
1269308b60e7SViresh Kumar 		ret = cpufreq_add_dev_interface(policy, dev);
127019d6f7ecSDave Jones 		if (ret)
12710142f9dcSAhmed S. Darwish 			goto err_out_unregister;
1272fcd7af91SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1273fcd7af91SViresh Kumar 				CPUFREQ_CREATE_POLICY, policy);
12749515f4d6SViresh Kumar 	}
1275c88a1f8bSLukasz Majewski 
1276c88a1f8bSLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1277c88a1f8bSLukasz Majewski 	list_add(&policy->policy_list, &cpufreq_policy_list);
1278c88a1f8bSLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
12798ff69732SDave Jones 
1280e18f1682SSrivatsa S. Bhat 	cpufreq_init_policy(policy);
1281e18f1682SSrivatsa S. Bhat 
128296bbbe4aSViresh Kumar 	if (!recover_policy) {
128308fd8c1cSViresh Kumar 		policy->user_policy.policy = policy->policy;
128408fd8c1cSViresh Kumar 		policy->user_policy.governor = policy->governor;
128508fd8c1cSViresh Kumar 	}
12864e97b631SViresh Kumar 	up_write(&policy->rwsem);
128708fd8c1cSViresh Kumar 
1288038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
12897c45cf31SViresh Kumar 
12906eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
12916eed9404SViresh Kumar 
12927c45cf31SViresh Kumar 	/* Callback for handling stuff after policy is ready */
12937c45cf31SViresh Kumar 	if (cpufreq_driver->ready)
12947c45cf31SViresh Kumar 		cpufreq_driver->ready(policy);
12957c45cf31SViresh Kumar 
12962d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds 	return 0;
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds err_out_unregister:
1301652ed95dSViresh Kumar err_get_freq:
13020d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1303474deff7SViresh Kumar 	for_each_cpu(j, policy->cpus)
13047a6aedfaSMike Travis 		per_cpu(cpufreq_cpu_data, j) = NULL;
13050d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
13061da177e4SLinus Torvalds 
13076d4e81edSTomeu Vizoso 	if (!recover_policy) {
13086d4e81edSTomeu Vizoso 		kobject_put(&policy->kobj);
13096d4e81edSTomeu Vizoso 		wait_for_completion(&policy->kobj_unregister);
13106d4e81edSTomeu Vizoso 	}
13116d4e81edSTomeu Vizoso err_init_policy_kobj:
13127106e02bSPrarit Bhargava 	up_write(&policy->rwsem);
13137106e02bSPrarit Bhargava 
1314da60ce9fSViresh Kumar 	if (cpufreq_driver->exit)
1315da60ce9fSViresh Kumar 		cpufreq_driver->exit(policy);
13162eaa3e2dSViresh Kumar err_set_policy_cpu:
131796bbbe4aSViresh Kumar 	if (recover_policy) {
131872368d12SRafael J. Wysocki 		/* Do not leave stale fallback data behind. */
131972368d12SRafael J. Wysocki 		per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
132042f921a6SViresh Kumar 		cpufreq_policy_put_kobj(policy);
132172368d12SRafael J. Wysocki 	}
1322e9698cc5SSrivatsa S. Bhat 	cpufreq_policy_free(policy);
132342f921a6SViresh Kumar 
13241da177e4SLinus Torvalds nomem_out:
13256eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
13266eed9404SViresh Kumar 
13271da177e4SLinus Torvalds 	return ret;
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds 
1330a82fab29SSrivatsa S. Bhat /**
1331a82fab29SSrivatsa S. Bhat  * cpufreq_add_dev - add a CPU device
1332a82fab29SSrivatsa S. Bhat  *
1333a82fab29SSrivatsa S. Bhat  * Adds the cpufreq interface for a CPU device.
1334a82fab29SSrivatsa S. Bhat  *
1335a82fab29SSrivatsa S. Bhat  * The Oracle says: try running cpufreq registration/unregistration concurrently
1336a82fab29SSrivatsa S. Bhat  * with with cpu hotplugging and all hell will break loose. Tried to clean this
1337a82fab29SSrivatsa S. Bhat  * mess up, but more thorough testing is needed. - Mathieu
1338a82fab29SSrivatsa S. Bhat  */
1339a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1340a82fab29SSrivatsa S. Bhat {
134196bbbe4aSViresh Kumar 	return __cpufreq_add_dev(dev, sif);
1342a82fab29SSrivatsa S. Bhat }
1343a82fab29SSrivatsa S. Bhat 
1344cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_prepare(struct device *dev,
134596bbbe4aSViresh Kumar 					struct subsys_interface *sif)
13461da177e4SLinus Torvalds {
1347f9ba680dSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
13481bfb425bSViresh Kumar 	int ret;
13491da177e4SLinus Torvalds 	unsigned long flags;
13503a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
13511da177e4SLinus Torvalds 
1352b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
13531da177e4SLinus Torvalds 
13540d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
13551da177e4SLinus Torvalds 
13563a3e9e06SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
13571da177e4SLinus Torvalds 
13588414809cSSrivatsa S. Bhat 	/* Save the policy somewhere when doing a light-weight tear-down */
135996bbbe4aSViresh Kumar 	if (cpufreq_suspended)
13603a3e9e06SViresh Kumar 		per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
13618414809cSSrivatsa S. Bhat 
13620d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
13631da177e4SLinus Torvalds 
13643a3e9e06SViresh Kumar 	if (!policy) {
1365b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
13661da177e4SLinus Torvalds 		return -EINVAL;
13671da177e4SLinus Torvalds 	}
13681da177e4SLinus Torvalds 
13699c0ebcf7SViresh Kumar 	if (has_target()) {
13703de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
13713de9bdebSViresh Kumar 		if (ret) {
13723de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
13733de9bdebSViresh Kumar 			return ret;
13743de9bdebSViresh Kumar 		}
13753de9bdebSViresh Kumar 	}
13765a01f2e8SVenkatesh Pallipadi 
13771c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->setpolicy)
1378fa69e33fSDirk Brandewie 		strncpy(per_cpu(cpufreq_cpu_governor, cpu),
13793a3e9e06SViresh Kumar 			policy->governor->name, CPUFREQ_NAME_LEN);
13801da177e4SLinus Torvalds 
1381ad7722daSviresh kumar 	down_read(&policy->rwsem);
13823a3e9e06SViresh Kumar 	cpus = cpumask_weight(policy->cpus);
1383ad7722daSviresh kumar 	up_read(&policy->rwsem);
13841da177e4SLinus Torvalds 
138561173f25SSrivatsa S. Bhat 	if (cpu != policy->cpu) {
138673bf0fc2SViresh Kumar 		sysfs_remove_link(&dev->kobj, "cpufreq");
138773bf0fc2SViresh Kumar 	} else if (cpus > 1) {
13881bfb425bSViresh Kumar 		/* Nominate new CPU */
13891bfb425bSViresh Kumar 		int new_cpu = cpumask_any_but(policy->cpus, cpu);
13901bfb425bSViresh Kumar 		struct device *cpu_dev = get_cpu_device(new_cpu);
13911bfb425bSViresh Kumar 
13921bfb425bSViresh Kumar 		sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
13931bfb425bSViresh Kumar 		ret = update_policy_cpu(policy, new_cpu, cpu_dev);
13941bfb425bSViresh Kumar 		if (ret) {
13951bfb425bSViresh Kumar 			if (sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
13961bfb425bSViresh Kumar 					      "cpufreq"))
13971bfb425bSViresh Kumar 				pr_err("%s: Failed to restore kobj link to cpu:%d\n",
13981bfb425bSViresh Kumar 				       __func__, cpu_dev->id);
13991bfb425bSViresh Kumar 			return ret;
14001bfb425bSViresh Kumar 		}
1401a82fab29SSrivatsa S. Bhat 
1402bda9f552SStratos Karafotis 		if (!cpufreq_suspended)
140375949c9aSViresh Kumar 			pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
140475949c9aSViresh Kumar 				 __func__, new_cpu, cpu);
1405789ca243SPreeti U Murthy 	} else if (cpufreq_driver->stop_cpu) {
1406367dc4aaSDirk Brandewie 		cpufreq_driver->stop_cpu(policy);
14071da177e4SLinus Torvalds 	}
1408b8eed8afSViresh Kumar 
1409cedb70afSSrivatsa S. Bhat 	return 0;
1410cedb70afSSrivatsa S. Bhat }
1411cedb70afSSrivatsa S. Bhat 
1412cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_finish(struct device *dev,
141396bbbe4aSViresh Kumar 				       struct subsys_interface *sif)
1414cedb70afSSrivatsa S. Bhat {
1415cedb70afSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
1416cedb70afSSrivatsa S. Bhat 	int ret;
1417cedb70afSSrivatsa S. Bhat 	unsigned long flags;
1418cedb70afSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
1419cedb70afSSrivatsa S. Bhat 
1420cedb70afSSrivatsa S. Bhat 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1421cedb70afSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data, cpu);
1422cedb70afSSrivatsa S. Bhat 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1423cedb70afSSrivatsa S. Bhat 
1424cedb70afSSrivatsa S. Bhat 	if (!policy) {
1425cedb70afSSrivatsa S. Bhat 		pr_debug("%s: No cpu_data found\n", __func__);
1426cedb70afSSrivatsa S. Bhat 		return -EINVAL;
1427cedb70afSSrivatsa S. Bhat 	}
1428cedb70afSSrivatsa S. Bhat 
1429ad7722daSviresh kumar 	down_write(&policy->rwsem);
1430cedb70afSSrivatsa S. Bhat 	cpus = cpumask_weight(policy->cpus);
14319c8f1ee4SViresh Kumar 
14329c8f1ee4SViresh Kumar 	if (cpus > 1)
14339c8f1ee4SViresh Kumar 		cpumask_clear_cpu(cpu, policy->cpus);
1434ad7722daSviresh kumar 	up_write(&policy->rwsem);
1435cedb70afSSrivatsa S. Bhat 
1436b8eed8afSViresh Kumar 	/* If cpu is last user of policy, free policy */
1437b8eed8afSViresh Kumar 	if (cpus == 1) {
14389c0ebcf7SViresh Kumar 		if (has_target()) {
14393de9bdebSViresh Kumar 			ret = __cpufreq_governor(policy,
14403de9bdebSViresh Kumar 					CPUFREQ_GOV_POLICY_EXIT);
14413de9bdebSViresh Kumar 			if (ret) {
14423de9bdebSViresh Kumar 				pr_err("%s: Failed to exit governor\n",
14433de9bdebSViresh Kumar 				       __func__);
14443de9bdebSViresh Kumar 				return ret;
14453de9bdebSViresh Kumar 			}
14463de9bdebSViresh Kumar 		}
14472a998599SRafael J. Wysocki 
144896bbbe4aSViresh Kumar 		if (!cpufreq_suspended)
144942f921a6SViresh Kumar 			cpufreq_policy_put_kobj(policy);
14501da177e4SLinus Torvalds 
14518414809cSSrivatsa S. Bhat 		/*
14528414809cSSrivatsa S. Bhat 		 * Perform the ->exit() even during light-weight tear-down,
14538414809cSSrivatsa S. Bhat 		 * since this is a core component, and is essential for the
14548414809cSSrivatsa S. Bhat 		 * subsequent light-weight ->init() to succeed.
14558414809cSSrivatsa S. Bhat 		 */
14561c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
14573a3e9e06SViresh Kumar 			cpufreq_driver->exit(policy);
145827ecddc2SJacob Shin 
14599515f4d6SViresh Kumar 		/* Remove policy from list of active policies */
14609515f4d6SViresh Kumar 		write_lock_irqsave(&cpufreq_driver_lock, flags);
14619515f4d6SViresh Kumar 		list_del(&policy->policy_list);
14629515f4d6SViresh Kumar 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
14639515f4d6SViresh Kumar 
146496bbbe4aSViresh Kumar 		if (!cpufreq_suspended)
14653a3e9e06SViresh Kumar 			cpufreq_policy_free(policy);
1466e5c87b76SStratos Karafotis 	} else if (has_target()) {
1467e5c87b76SStratos Karafotis 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1468e5c87b76SStratos Karafotis 		if (!ret)
1469e5c87b76SStratos Karafotis 			ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1470e5c87b76SStratos Karafotis 
1471e5c87b76SStratos Karafotis 		if (ret) {
1472e5c87b76SStratos Karafotis 			pr_err("%s: Failed to start governor\n", __func__);
14733de9bdebSViresh Kumar 			return ret;
14743de9bdebSViresh Kumar 		}
1475b8eed8afSViresh Kumar 	}
14761da177e4SLinus Torvalds 
1477474deff7SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = NULL;
14781da177e4SLinus Torvalds 	return 0;
14791da177e4SLinus Torvalds }
14801da177e4SLinus Torvalds 
1481cedb70afSSrivatsa S. Bhat /**
148227a862e9SViresh Kumar  * cpufreq_remove_dev - remove a CPU device
1483cedb70afSSrivatsa S. Bhat  *
1484cedb70afSSrivatsa S. Bhat  * Removes the cpufreq interface for a CPU device.
1485cedb70afSSrivatsa S. Bhat  */
14868a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
14875a01f2e8SVenkatesh Pallipadi {
14888a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
148927a862e9SViresh Kumar 	int ret;
1490ec28297aSVenki Pallipadi 
1491ec28297aSVenki Pallipadi 	if (cpu_is_offline(cpu))
1492ec28297aSVenki Pallipadi 		return 0;
1493ec28297aSVenki Pallipadi 
149496bbbe4aSViresh Kumar 	ret = __cpufreq_remove_dev_prepare(dev, sif);
149527a862e9SViresh Kumar 
149627a862e9SViresh Kumar 	if (!ret)
149796bbbe4aSViresh Kumar 		ret = __cpufreq_remove_dev_finish(dev, sif);
149827a862e9SViresh Kumar 
149927a862e9SViresh Kumar 	return ret;
15005a01f2e8SVenkatesh Pallipadi }
15015a01f2e8SVenkatesh Pallipadi 
150265f27f38SDavid Howells static void handle_update(struct work_struct *work)
15031da177e4SLinus Torvalds {
150465f27f38SDavid Howells 	struct cpufreq_policy *policy =
150565f27f38SDavid Howells 		container_of(work, struct cpufreq_policy, update);
150665f27f38SDavid Howells 	unsigned int cpu = policy->cpu;
15072d06d8c4SDominik Brodowski 	pr_debug("handle_update for cpu %u called\n", cpu);
15081da177e4SLinus Torvalds 	cpufreq_update_policy(cpu);
15091da177e4SLinus Torvalds }
15101da177e4SLinus Torvalds 
15111da177e4SLinus Torvalds /**
1512bb176f7dSViresh Kumar  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1513bb176f7dSViresh Kumar  *	in deep trouble.
15141da177e4SLinus Torvalds  *	@cpu: cpu number
15151da177e4SLinus Torvalds  *	@old_freq: CPU frequency the kernel thinks the CPU runs at
15161da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
15171da177e4SLinus Torvalds  *
151829464f28SDave Jones  *	We adjust to current frequency first, and need to clean up later.
151929464f28SDave Jones  *	So either call to cpufreq_update_policy() or schedule handle_update()).
15201da177e4SLinus Torvalds  */
1521e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1522e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
15231da177e4SLinus Torvalds {
1524b43a7ffbSViresh Kumar 	struct cpufreq_policy *policy;
15251da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1526b43a7ffbSViresh Kumar 	unsigned long flags;
1527b43a7ffbSViresh Kumar 
1528e837f9b5SJoe Perches 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1529e837f9b5SJoe Perches 		 old_freq, new_freq);
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	freqs.old = old_freq;
15321da177e4SLinus Torvalds 	freqs.new = new_freq;
1533b43a7ffbSViresh Kumar 
1534b43a7ffbSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1535b43a7ffbSViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
1536b43a7ffbSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1537b43a7ffbSViresh Kumar 
15388fec051eSViresh Kumar 	cpufreq_freq_transition_begin(policy, &freqs);
15398fec051eSViresh Kumar 	cpufreq_freq_transition_end(policy, &freqs, 0);
15401da177e4SLinus Torvalds }
15411da177e4SLinus Torvalds 
15421da177e4SLinus Torvalds /**
15434ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
154495235ca2SVenkatesh Pallipadi  * @cpu: CPU number
154595235ca2SVenkatesh Pallipadi  *
154695235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
154795235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
154895235ca2SVenkatesh Pallipadi  */
154995235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
155095235ca2SVenkatesh Pallipadi {
15519e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1552e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
155395235ca2SVenkatesh Pallipadi 
15541c3d85ddSRafael J. Wysocki 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
15551c3d85ddSRafael J. Wysocki 		return cpufreq_driver->get(cpu);
15569e21ba8bSDirk Brandewie 
15579e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
155895235ca2SVenkatesh Pallipadi 	if (policy) {
1559e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
156095235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
156195235ca2SVenkatesh Pallipadi 	}
156295235ca2SVenkatesh Pallipadi 
15634d34a67dSDave Jones 	return ret_freq;
156495235ca2SVenkatesh Pallipadi }
156595235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
156695235ca2SVenkatesh Pallipadi 
15673d737108SJesse Barnes /**
15683d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
15693d737108SJesse Barnes  * @cpu: CPU number
15703d737108SJesse Barnes  *
15713d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
15723d737108SJesse Barnes  */
15733d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
15743d737108SJesse Barnes {
15753d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
15763d737108SJesse Barnes 	unsigned int ret_freq = 0;
15773d737108SJesse Barnes 
15783d737108SJesse Barnes 	if (policy) {
15793d737108SJesse Barnes 		ret_freq = policy->max;
15803d737108SJesse Barnes 		cpufreq_cpu_put(policy);
15813d737108SJesse Barnes 	}
15823d737108SJesse Barnes 
15833d737108SJesse Barnes 	return ret_freq;
15843d737108SJesse Barnes }
15853d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
15863d737108SJesse Barnes 
15875a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu)
15881da177e4SLinus Torvalds {
15897a6aedfaSMike Travis 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1590e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
15911da177e4SLinus Torvalds 
15921c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->get)
15934d34a67dSDave Jones 		return ret_freq;
15941da177e4SLinus Torvalds 
15951c3d85ddSRafael J. Wysocki 	ret_freq = cpufreq_driver->get(cpu);
15961da177e4SLinus Torvalds 
1597e08f5f5bSGautham R Shenoy 	if (ret_freq && policy->cur &&
15981c3d85ddSRafael J. Wysocki 		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1599e08f5f5bSGautham R Shenoy 		/* verify no discrepancy between actual and
1600e08f5f5bSGautham R Shenoy 					saved value exists */
1601e08f5f5bSGautham R Shenoy 		if (unlikely(ret_freq != policy->cur)) {
1602e08f5f5bSGautham R Shenoy 			cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
16031da177e4SLinus Torvalds 			schedule_work(&policy->update);
16041da177e4SLinus Torvalds 		}
16051da177e4SLinus Torvalds 	}
16061da177e4SLinus Torvalds 
16074d34a67dSDave Jones 	return ret_freq;
16085a01f2e8SVenkatesh Pallipadi }
16091da177e4SLinus Torvalds 
16105a01f2e8SVenkatesh Pallipadi /**
16115a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
16125a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
16135a01f2e8SVenkatesh Pallipadi  *
16145a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
16155a01f2e8SVenkatesh Pallipadi  */
16165a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
16175a01f2e8SVenkatesh Pallipadi {
1618999976e0SAaron Plattner 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
16195a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
16205a01f2e8SVenkatesh Pallipadi 
1621999976e0SAaron Plattner 	if (policy) {
1622ad7722daSviresh kumar 		down_read(&policy->rwsem);
16235a01f2e8SVenkatesh Pallipadi 		ret_freq = __cpufreq_get(cpu);
1624ad7722daSviresh kumar 		up_read(&policy->rwsem);
1625999976e0SAaron Plattner 
1626999976e0SAaron Plattner 		cpufreq_cpu_put(policy);
1627999976e0SAaron Plattner 	}
16286eed9404SViresh Kumar 
16294d34a67dSDave Jones 	return ret_freq;
16301da177e4SLinus Torvalds }
16311da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
16321da177e4SLinus Torvalds 
16338a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
16348a25a2fdSKay Sievers 	.name		= "cpufreq",
16358a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
16368a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
16378a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1638e00e56dfSRafael J. Wysocki };
1639e00e56dfSRafael J. Wysocki 
1640e28867eaSViresh Kumar /*
1641e28867eaSViresh Kumar  * In case platform wants some specific frequency to be configured
1642e28867eaSViresh Kumar  * during suspend..
164342d4dc3fSBenjamin Herrenschmidt  */
1644e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy)
164542d4dc3fSBenjamin Herrenschmidt {
1646e28867eaSViresh Kumar 	int ret;
16474bc5d341SDave Jones 
1648e28867eaSViresh Kumar 	if (!policy->suspend_freq) {
1649e28867eaSViresh Kumar 		pr_err("%s: suspend_freq can't be zero\n", __func__);
1650e28867eaSViresh Kumar 		return -EINVAL;
165142d4dc3fSBenjamin Herrenschmidt 	}
165242d4dc3fSBenjamin Herrenschmidt 
1653e28867eaSViresh Kumar 	pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1654e28867eaSViresh Kumar 			policy->suspend_freq);
1655e28867eaSViresh Kumar 
1656e28867eaSViresh Kumar 	ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1657e28867eaSViresh Kumar 			CPUFREQ_RELATION_H);
1658e28867eaSViresh Kumar 	if (ret)
1659e28867eaSViresh Kumar 		pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1660e28867eaSViresh Kumar 				__func__, policy->suspend_freq, ret);
1661e28867eaSViresh Kumar 
1662c9060494SDave Jones 	return ret;
166342d4dc3fSBenjamin Herrenschmidt }
1664e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend);
166542d4dc3fSBenjamin Herrenschmidt 
166642d4dc3fSBenjamin Herrenschmidt /**
16672f0aea93SViresh Kumar  * cpufreq_suspend() - Suspend CPUFreq governors
16681da177e4SLinus Torvalds  *
16692f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycles for suspending governors
16702f0aea93SViresh Kumar  * as some platforms can't change frequency after this point in suspend cycle.
16712f0aea93SViresh Kumar  * Because some of the devices (like: i2c, regulators, etc) they use for
16722f0aea93SViresh Kumar  * changing frequency are suspended quickly after this point.
16731da177e4SLinus Torvalds  */
16742f0aea93SViresh Kumar void cpufreq_suspend(void)
16751da177e4SLinus Torvalds {
16763a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
16771da177e4SLinus Torvalds 
16782f0aea93SViresh Kumar 	if (!cpufreq_driver)
1679e00e56dfSRafael J. Wysocki 		return;
16801da177e4SLinus Torvalds 
16812f0aea93SViresh Kumar 	if (!has_target())
1682b1b12babSViresh Kumar 		goto suspend;
16831da177e4SLinus Torvalds 
16842f0aea93SViresh Kumar 	pr_debug("%s: Suspending Governors\n", __func__);
16852f0aea93SViresh Kumar 
16862f0aea93SViresh Kumar 	list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
16872f0aea93SViresh Kumar 		if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
16882f0aea93SViresh Kumar 			pr_err("%s: Failed to stop governor for policy: %p\n",
16892f0aea93SViresh Kumar 				__func__, policy);
16902f0aea93SViresh Kumar 		else if (cpufreq_driver->suspend
16912f0aea93SViresh Kumar 		    && cpufreq_driver->suspend(policy))
16922f0aea93SViresh Kumar 			pr_err("%s: Failed to suspend driver: %p\n", __func__,
16932f0aea93SViresh Kumar 				policy);
16941da177e4SLinus Torvalds 	}
1695b1b12babSViresh Kumar 
1696b1b12babSViresh Kumar suspend:
1697b1b12babSViresh Kumar 	cpufreq_suspended = true;
16981da177e4SLinus Torvalds }
16991da177e4SLinus Torvalds 
17001da177e4SLinus Torvalds /**
17012f0aea93SViresh Kumar  * cpufreq_resume() - Resume CPUFreq governors
17021da177e4SLinus Torvalds  *
17032f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycle for resuming governors that
17042f0aea93SViresh Kumar  * are suspended with cpufreq_suspend().
17051da177e4SLinus Torvalds  */
17062f0aea93SViresh Kumar void cpufreq_resume(void)
17071da177e4SLinus Torvalds {
17081da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
17091da177e4SLinus Torvalds 
17102f0aea93SViresh Kumar 	if (!cpufreq_driver)
17111da177e4SLinus Torvalds 		return;
17121da177e4SLinus Torvalds 
17138e30444eSLan Tianyu 	cpufreq_suspended = false;
17148e30444eSLan Tianyu 
17152f0aea93SViresh Kumar 	if (!has_target())
17162f0aea93SViresh Kumar 		return;
17171da177e4SLinus Torvalds 
17182f0aea93SViresh Kumar 	pr_debug("%s: Resuming Governors\n", __func__);
17192f0aea93SViresh Kumar 
17202f0aea93SViresh Kumar 	list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
17210c5aa405SViresh Kumar 		if (cpufreq_driver->resume && cpufreq_driver->resume(policy))
17220c5aa405SViresh Kumar 			pr_err("%s: Failed to resume driver: %p\n", __func__,
17230c5aa405SViresh Kumar 				policy);
17240c5aa405SViresh Kumar 		else if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
17252f0aea93SViresh Kumar 		    || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
17262f0aea93SViresh Kumar 			pr_err("%s: Failed to start governor for policy: %p\n",
17272f0aea93SViresh Kumar 				__func__, policy);
17282f0aea93SViresh Kumar 
17292f0aea93SViresh Kumar 		/*
17302f0aea93SViresh Kumar 		 * schedule call cpufreq_update_policy() for boot CPU, i.e. last
17312f0aea93SViresh Kumar 		 * policy in list. It will verify that the current freq is in
17322f0aea93SViresh Kumar 		 * sync with what we believe it to be.
17332f0aea93SViresh Kumar 		 */
17342f0aea93SViresh Kumar 		if (list_is_last(&policy->policy_list, &cpufreq_policy_list))
17353a3e9e06SViresh Kumar 			schedule_work(&policy->update);
17361da177e4SLinus Torvalds 	}
17372f0aea93SViresh Kumar }
17381da177e4SLinus Torvalds 
17399d95046eSBorislav Petkov /**
17409d95046eSBorislav Petkov  *	cpufreq_get_current_driver - return current driver's name
17419d95046eSBorislav Petkov  *
17429d95046eSBorislav Petkov  *	Return the name string of the currently loaded cpufreq driver
17439d95046eSBorislav Petkov  *	or NULL, if none.
17449d95046eSBorislav Petkov  */
17459d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
17469d95046eSBorislav Petkov {
17471c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
17481c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
17491c3d85ddSRafael J. Wysocki 
17501c3d85ddSRafael J. Wysocki 	return NULL;
17519d95046eSBorislav Petkov }
17529d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
17531da177e4SLinus Torvalds 
175451315cdfSThomas Petazzoni /**
175551315cdfSThomas Petazzoni  *	cpufreq_get_driver_data - return current driver data
175651315cdfSThomas Petazzoni  *
175751315cdfSThomas Petazzoni  *	Return the private data of the currently loaded cpufreq
175851315cdfSThomas Petazzoni  *	driver, or NULL if no cpufreq driver is loaded.
175951315cdfSThomas Petazzoni  */
176051315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void)
176151315cdfSThomas Petazzoni {
176251315cdfSThomas Petazzoni 	if (cpufreq_driver)
176351315cdfSThomas Petazzoni 		return cpufreq_driver->driver_data;
176451315cdfSThomas Petazzoni 
176551315cdfSThomas Petazzoni 	return NULL;
176651315cdfSThomas Petazzoni }
176751315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
176851315cdfSThomas Petazzoni 
17691da177e4SLinus Torvalds /*********************************************************************
17701da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
17711da177e4SLinus Torvalds  *********************************************************************/
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds /**
17741da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
17751da177e4SLinus Torvalds  *	@nb: notifier function to register
17761da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
17771da177e4SLinus Torvalds  *
17781da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
17791da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
17801da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
17811da177e4SLinus Torvalds  *      changes in cpufreq policy.
17821da177e4SLinus Torvalds  *
17831da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1784e041c683SAlan Stern  *	blocking_notifier_chain_register.
17851da177e4SLinus Torvalds  */
17861da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
17871da177e4SLinus Torvalds {
17881da177e4SLinus Torvalds 	int ret;
17891da177e4SLinus Torvalds 
1790d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1791d5aaffa9SDirk Brandewie 		return -EINVAL;
1792d5aaffa9SDirk Brandewie 
179374212ca4SCesar Eduardo Barros 	WARN_ON(!init_cpufreq_transition_notifier_list_called);
179474212ca4SCesar Eduardo Barros 
17951da177e4SLinus Torvalds 	switch (list) {
17961da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1797b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1798e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
17991da177e4SLinus Torvalds 		break;
18001da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1801e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1802e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
18031da177e4SLinus Torvalds 		break;
18041da177e4SLinus Torvalds 	default:
18051da177e4SLinus Torvalds 		ret = -EINVAL;
18061da177e4SLinus Torvalds 	}
18071da177e4SLinus Torvalds 
18081da177e4SLinus Torvalds 	return ret;
18091da177e4SLinus Torvalds }
18101da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
18111da177e4SLinus Torvalds 
18121da177e4SLinus Torvalds /**
18131da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
18141da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
18151da177e4SLinus Torvalds  *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
18161da177e4SLinus Torvalds  *
18171da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
18181da177e4SLinus Torvalds  *
18191da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1820e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
18211da177e4SLinus Torvalds  */
18221da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
18231da177e4SLinus Torvalds {
18241da177e4SLinus Torvalds 	int ret;
18251da177e4SLinus Torvalds 
1826d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1827d5aaffa9SDirk Brandewie 		return -EINVAL;
1828d5aaffa9SDirk Brandewie 
18291da177e4SLinus Torvalds 	switch (list) {
18301da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1831b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
1832e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
18331da177e4SLinus Torvalds 		break;
18341da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1835e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1836e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
18371da177e4SLinus Torvalds 		break;
18381da177e4SLinus Torvalds 	default:
18391da177e4SLinus Torvalds 		ret = -EINVAL;
18401da177e4SLinus Torvalds 	}
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 	return ret;
18431da177e4SLinus Torvalds }
18441da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 
18471da177e4SLinus Torvalds /*********************************************************************
18481da177e4SLinus Torvalds  *                              GOVERNORS                            *
18491da177e4SLinus Torvalds  *********************************************************************/
18501da177e4SLinus Torvalds 
18511c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */
18521c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy,
18531c03a2d0SViresh Kumar 				 struct cpufreq_freqs *freqs, int index)
18541c03a2d0SViresh Kumar {
18551c03a2d0SViresh Kumar 	int ret;
18561c03a2d0SViresh Kumar 
18571c03a2d0SViresh Kumar 	freqs->new = cpufreq_driver->get_intermediate(policy, index);
18581c03a2d0SViresh Kumar 
18591c03a2d0SViresh Kumar 	/* We don't need to switch to intermediate freq */
18601c03a2d0SViresh Kumar 	if (!freqs->new)
18611c03a2d0SViresh Kumar 		return 0;
18621c03a2d0SViresh Kumar 
18631c03a2d0SViresh Kumar 	pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
18641c03a2d0SViresh Kumar 		 __func__, policy->cpu, freqs->old, freqs->new);
18651c03a2d0SViresh Kumar 
18661c03a2d0SViresh Kumar 	cpufreq_freq_transition_begin(policy, freqs);
18671c03a2d0SViresh Kumar 	ret = cpufreq_driver->target_intermediate(policy, index);
18681c03a2d0SViresh Kumar 	cpufreq_freq_transition_end(policy, freqs, ret);
18691c03a2d0SViresh Kumar 
18701c03a2d0SViresh Kumar 	if (ret)
18711c03a2d0SViresh Kumar 		pr_err("%s: Failed to change to intermediate frequency: %d\n",
18721c03a2d0SViresh Kumar 		       __func__, ret);
18731c03a2d0SViresh Kumar 
18741c03a2d0SViresh Kumar 	return ret;
18751c03a2d0SViresh Kumar }
18761c03a2d0SViresh Kumar 
18778d65775dSViresh Kumar static int __target_index(struct cpufreq_policy *policy,
18788d65775dSViresh Kumar 			  struct cpufreq_frequency_table *freq_table, int index)
18798d65775dSViresh Kumar {
18801c03a2d0SViresh Kumar 	struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
18811c03a2d0SViresh Kumar 	unsigned int intermediate_freq = 0;
18828d65775dSViresh Kumar 	int retval = -EINVAL;
18838d65775dSViresh Kumar 	bool notify;
18848d65775dSViresh Kumar 
18858d65775dSViresh Kumar 	notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
18868d65775dSViresh Kumar 	if (notify) {
18871c03a2d0SViresh Kumar 		/* Handle switching to intermediate frequency */
18881c03a2d0SViresh Kumar 		if (cpufreq_driver->get_intermediate) {
18891c03a2d0SViresh Kumar 			retval = __target_intermediate(policy, &freqs, index);
18901c03a2d0SViresh Kumar 			if (retval)
18911c03a2d0SViresh Kumar 				return retval;
18928d65775dSViresh Kumar 
18931c03a2d0SViresh Kumar 			intermediate_freq = freqs.new;
18941c03a2d0SViresh Kumar 			/* Set old freq to intermediate */
18951c03a2d0SViresh Kumar 			if (intermediate_freq)
18961c03a2d0SViresh Kumar 				freqs.old = freqs.new;
18971c03a2d0SViresh Kumar 		}
18981c03a2d0SViresh Kumar 
18991c03a2d0SViresh Kumar 		freqs.new = freq_table[index].frequency;
19008d65775dSViresh Kumar 		pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
19018d65775dSViresh Kumar 			 __func__, policy->cpu, freqs.old, freqs.new);
19028d65775dSViresh Kumar 
19038d65775dSViresh Kumar 		cpufreq_freq_transition_begin(policy, &freqs);
19048d65775dSViresh Kumar 	}
19058d65775dSViresh Kumar 
19068d65775dSViresh Kumar 	retval = cpufreq_driver->target_index(policy, index);
19078d65775dSViresh Kumar 	if (retval)
19088d65775dSViresh Kumar 		pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
19098d65775dSViresh Kumar 		       retval);
19108d65775dSViresh Kumar 
19111c03a2d0SViresh Kumar 	if (notify) {
19128d65775dSViresh Kumar 		cpufreq_freq_transition_end(policy, &freqs, retval);
19138d65775dSViresh Kumar 
19141c03a2d0SViresh Kumar 		/*
19151c03a2d0SViresh Kumar 		 * Failed after setting to intermediate freq? Driver should have
19161c03a2d0SViresh Kumar 		 * reverted back to initial frequency and so should we. Check
19171c03a2d0SViresh Kumar 		 * here for intermediate_freq instead of get_intermediate, in
19181c03a2d0SViresh Kumar 		 * case we have't switched to intermediate freq at all.
19191c03a2d0SViresh Kumar 		 */
19201c03a2d0SViresh Kumar 		if (unlikely(retval && intermediate_freq)) {
19211c03a2d0SViresh Kumar 			freqs.old = intermediate_freq;
19221c03a2d0SViresh Kumar 			freqs.new = policy->restore_freq;
19231c03a2d0SViresh Kumar 			cpufreq_freq_transition_begin(policy, &freqs);
19241c03a2d0SViresh Kumar 			cpufreq_freq_transition_end(policy, &freqs, 0);
19251c03a2d0SViresh Kumar 		}
19261c03a2d0SViresh Kumar 	}
19271c03a2d0SViresh Kumar 
19288d65775dSViresh Kumar 	return retval;
19298d65775dSViresh Kumar }
19308d65775dSViresh Kumar 
19311da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
19321da177e4SLinus Torvalds 			    unsigned int target_freq,
19331da177e4SLinus Torvalds 			    unsigned int relation)
19341da177e4SLinus Torvalds {
19357249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
19368d65775dSViresh Kumar 	int retval = -EINVAL;
1937c32b6b8eSAshok Raj 
1938a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1939a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1940a7b422cdSKonrad Rzeszutek Wilk 
19417249924eSViresh Kumar 	/* Make sure that target_freq is within supported range */
19427249924eSViresh Kumar 	if (target_freq > policy->max)
19437249924eSViresh Kumar 		target_freq = policy->max;
19447249924eSViresh Kumar 	if (target_freq < policy->min)
19457249924eSViresh Kumar 		target_freq = policy->min;
19467249924eSViresh Kumar 
19477249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
19487249924eSViresh Kumar 		 policy->cpu, target_freq, relation, old_target_freq);
19495a1c0228SViresh Kumar 
19509c0ebcf7SViresh Kumar 	/*
19519c0ebcf7SViresh Kumar 	 * This might look like a redundant call as we are checking it again
19529c0ebcf7SViresh Kumar 	 * after finding index. But it is left intentionally for cases where
19539c0ebcf7SViresh Kumar 	 * exactly same freq is called again and so we can save on few function
19549c0ebcf7SViresh Kumar 	 * calls.
19559c0ebcf7SViresh Kumar 	 */
19565a1c0228SViresh Kumar 	if (target_freq == policy->cur)
19575a1c0228SViresh Kumar 		return 0;
19585a1c0228SViresh Kumar 
19591c03a2d0SViresh Kumar 	/* Save last value to restore later on errors */
19601c03a2d0SViresh Kumar 	policy->restore_freq = policy->cur;
19611c03a2d0SViresh Kumar 
19621c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
19631c3d85ddSRafael J. Wysocki 		retval = cpufreq_driver->target(policy, target_freq, relation);
19649c0ebcf7SViresh Kumar 	else if (cpufreq_driver->target_index) {
19659c0ebcf7SViresh Kumar 		struct cpufreq_frequency_table *freq_table;
19669c0ebcf7SViresh Kumar 		int index;
196790d45d17SAshok Raj 
19689c0ebcf7SViresh Kumar 		freq_table = cpufreq_frequency_get_table(policy->cpu);
19699c0ebcf7SViresh Kumar 		if (unlikely(!freq_table)) {
19709c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find freq_table\n", __func__);
19719c0ebcf7SViresh Kumar 			goto out;
19729c0ebcf7SViresh Kumar 		}
19739c0ebcf7SViresh Kumar 
19749c0ebcf7SViresh Kumar 		retval = cpufreq_frequency_table_target(policy, freq_table,
19759c0ebcf7SViresh Kumar 				target_freq, relation, &index);
19769c0ebcf7SViresh Kumar 		if (unlikely(retval)) {
19779c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find matching freq\n", __func__);
19789c0ebcf7SViresh Kumar 			goto out;
19799c0ebcf7SViresh Kumar 		}
19809c0ebcf7SViresh Kumar 
1981d4019f0aSViresh Kumar 		if (freq_table[index].frequency == policy->cur) {
19829c0ebcf7SViresh Kumar 			retval = 0;
1983d4019f0aSViresh Kumar 			goto out;
1984d4019f0aSViresh Kumar 		}
1985d4019f0aSViresh Kumar 
19868d65775dSViresh Kumar 		retval = __target_index(policy, freq_table, index);
19879c0ebcf7SViresh Kumar 	}
19889c0ebcf7SViresh Kumar 
19899c0ebcf7SViresh Kumar out:
19901da177e4SLinus Torvalds 	return retval;
19911da177e4SLinus Torvalds }
19921da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
19931da177e4SLinus Torvalds 
19941da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
19951da177e4SLinus Torvalds 			  unsigned int target_freq,
19961da177e4SLinus Torvalds 			  unsigned int relation)
19971da177e4SLinus Torvalds {
1998f1829e4aSJulia Lawall 	int ret = -EINVAL;
19991da177e4SLinus Torvalds 
2000ad7722daSviresh kumar 	down_write(&policy->rwsem);
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
20031da177e4SLinus Torvalds 
2004ad7722daSviresh kumar 	up_write(&policy->rwsem);
20051da177e4SLinus Torvalds 
20061da177e4SLinus Torvalds 	return ret;
20071da177e4SLinus Torvalds }
20081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
20091da177e4SLinus Torvalds 
2010e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy,
2011e08f5f5bSGautham R Shenoy 					unsigned int event)
20121da177e4SLinus Torvalds {
2013cc993cabSDave Jones 	int ret;
20146afde10cSThomas Renninger 
20156afde10cSThomas Renninger 	/* Only must be defined when default governor is known to have latency
20166afde10cSThomas Renninger 	   restrictions, like e.g. conservative or ondemand.
20176afde10cSThomas Renninger 	   That this is the case is already ensured in Kconfig
20186afde10cSThomas Renninger 	*/
20196afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
20206afde10cSThomas Renninger 	struct cpufreq_governor *gov = &cpufreq_gov_performance;
20216afde10cSThomas Renninger #else
20226afde10cSThomas Renninger 	struct cpufreq_governor *gov = NULL;
20236afde10cSThomas Renninger #endif
20241c256245SThomas Renninger 
20252f0aea93SViresh Kumar 	/* Don't start any governor operations if we are entering suspend */
20262f0aea93SViresh Kumar 	if (cpufreq_suspended)
20272f0aea93SViresh Kumar 		return 0;
2028cb57720bSEthan Zhao 	/*
2029cb57720bSEthan Zhao 	 * Governor might not be initiated here if ACPI _PPC changed
2030cb57720bSEthan Zhao 	 * notification happened, so check it.
2031cb57720bSEthan Zhao 	 */
2032cb57720bSEthan Zhao 	if (!policy->governor)
2033cb57720bSEthan Zhao 		return -EINVAL;
20342f0aea93SViresh Kumar 
20351c256245SThomas Renninger 	if (policy->governor->max_transition_latency &&
20361c256245SThomas Renninger 	    policy->cpuinfo.transition_latency >
20371c256245SThomas Renninger 	    policy->governor->max_transition_latency) {
20386afde10cSThomas Renninger 		if (!gov)
20396afde10cSThomas Renninger 			return -EINVAL;
20406afde10cSThomas Renninger 		else {
2041e837f9b5SJoe Perches 			pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2042e837f9b5SJoe Perches 				policy->governor->name, gov->name);
20431c256245SThomas Renninger 			policy->governor = gov;
20441c256245SThomas Renninger 		}
20456afde10cSThomas Renninger 	}
20461da177e4SLinus Torvalds 
2047fe492f3fSViresh Kumar 	if (event == CPUFREQ_GOV_POLICY_INIT)
20481da177e4SLinus Torvalds 		if (!try_module_get(policy->governor->owner))
20491da177e4SLinus Torvalds 			return -EINVAL;
20501da177e4SLinus Torvalds 
20512d06d8c4SDominik Brodowski 	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
2052e08f5f5bSGautham R Shenoy 		 policy->cpu, event);
205395731ebbSXiaoguang Chen 
205495731ebbSXiaoguang Chen 	mutex_lock(&cpufreq_governor_lock);
205556d07db2SSrivatsa S. Bhat 	if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
2056f73d3933SViresh Kumar 	    || (!policy->governor_enabled
2057f73d3933SViresh Kumar 	    && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
205895731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
205995731ebbSXiaoguang Chen 		return -EBUSY;
206095731ebbSXiaoguang Chen 	}
206195731ebbSXiaoguang Chen 
206295731ebbSXiaoguang Chen 	if (event == CPUFREQ_GOV_STOP)
206395731ebbSXiaoguang Chen 		policy->governor_enabled = false;
206495731ebbSXiaoguang Chen 	else if (event == CPUFREQ_GOV_START)
206595731ebbSXiaoguang Chen 		policy->governor_enabled = true;
206695731ebbSXiaoguang Chen 
206795731ebbSXiaoguang Chen 	mutex_unlock(&cpufreq_governor_lock);
206895731ebbSXiaoguang Chen 
20691da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
20701da177e4SLinus Torvalds 
20714d5dcc42SViresh Kumar 	if (!ret) {
20724d5dcc42SViresh Kumar 		if (event == CPUFREQ_GOV_POLICY_INIT)
20738e53695fSViresh Kumar 			policy->governor->initialized++;
20744d5dcc42SViresh Kumar 		else if (event == CPUFREQ_GOV_POLICY_EXIT)
20758e53695fSViresh Kumar 			policy->governor->initialized--;
207695731ebbSXiaoguang Chen 	} else {
207795731ebbSXiaoguang Chen 		/* Restore original values */
207895731ebbSXiaoguang Chen 		mutex_lock(&cpufreq_governor_lock);
207995731ebbSXiaoguang Chen 		if (event == CPUFREQ_GOV_STOP)
208095731ebbSXiaoguang Chen 			policy->governor_enabled = true;
208195731ebbSXiaoguang Chen 		else if (event == CPUFREQ_GOV_START)
208295731ebbSXiaoguang Chen 			policy->governor_enabled = false;
208395731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
20844d5dcc42SViresh Kumar 	}
2085b394058fSViresh Kumar 
2086fe492f3fSViresh Kumar 	if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2087fe492f3fSViresh Kumar 			((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
20881da177e4SLinus Torvalds 		module_put(policy->governor->owner);
20891da177e4SLinus Torvalds 
20901da177e4SLinus Torvalds 	return ret;
20911da177e4SLinus Torvalds }
20921da177e4SLinus Torvalds 
20931da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
20941da177e4SLinus Torvalds {
20953bcb09a3SJeremy Fitzhardinge 	int err;
20961da177e4SLinus Torvalds 
20971da177e4SLinus Torvalds 	if (!governor)
20981da177e4SLinus Torvalds 		return -EINVAL;
20991da177e4SLinus Torvalds 
2100a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2101a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2102a7b422cdSKonrad Rzeszutek Wilk 
21033fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
21041da177e4SLinus Torvalds 
2105b394058fSViresh Kumar 	governor->initialized = 0;
21063bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
21073bcb09a3SJeremy Fitzhardinge 	if (__find_governor(governor->name) == NULL) {
21083bcb09a3SJeremy Fitzhardinge 		err = 0;
21091da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
21103bcb09a3SJeremy Fitzhardinge 	}
21111da177e4SLinus Torvalds 
21123fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
21133bcb09a3SJeremy Fitzhardinge 	return err;
21141da177e4SLinus Torvalds }
21151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
21181da177e4SLinus Torvalds {
211990e41bacSPrarit Bhargava 	int cpu;
212090e41bacSPrarit Bhargava 
21211da177e4SLinus Torvalds 	if (!governor)
21221da177e4SLinus Torvalds 		return;
21231da177e4SLinus Torvalds 
2124a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2125a7b422cdSKonrad Rzeszutek Wilk 		return;
2126a7b422cdSKonrad Rzeszutek Wilk 
212790e41bacSPrarit Bhargava 	for_each_present_cpu(cpu) {
212890e41bacSPrarit Bhargava 		if (cpu_online(cpu))
212990e41bacSPrarit Bhargava 			continue;
213090e41bacSPrarit Bhargava 		if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
213190e41bacSPrarit Bhargava 			strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
213290e41bacSPrarit Bhargava 	}
213390e41bacSPrarit Bhargava 
21343fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
21351da177e4SLinus Torvalds 	list_del(&governor->governor_list);
21363fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
21371da177e4SLinus Torvalds 	return;
21381da177e4SLinus Torvalds }
21391da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
21401da177e4SLinus Torvalds 
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds /*********************************************************************
21431da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
21441da177e4SLinus Torvalds  *********************************************************************/
21451da177e4SLinus Torvalds 
21461da177e4SLinus Torvalds /**
21471da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
214829464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
214929464f28SDave Jones  *	is written
21501da177e4SLinus Torvalds  *
21511da177e4SLinus Torvalds  * Reads the current cpufreq policy.
21521da177e4SLinus Torvalds  */
21531da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
21541da177e4SLinus Torvalds {
21551da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
21561da177e4SLinus Torvalds 	if (!policy)
21571da177e4SLinus Torvalds 		return -EINVAL;
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
21601da177e4SLinus Torvalds 	if (!cpu_policy)
21611da177e4SLinus Torvalds 		return -EINVAL;
21621da177e4SLinus Torvalds 
2163d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
21661da177e4SLinus Torvalds 	return 0;
21671da177e4SLinus Torvalds }
21681da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
21691da177e4SLinus Torvalds 
2170153d7f3fSArjan van de Ven /*
2171037ce839SViresh Kumar  * policy : current policy.
2172037ce839SViresh Kumar  * new_policy: policy to be set.
2173153d7f3fSArjan van de Ven  */
2174037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
21753a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy)
21761da177e4SLinus Torvalds {
2177d9a789c7SRafael J. Wysocki 	struct cpufreq_governor *old_gov;
2178d9a789c7SRafael J. Wysocki 	int ret;
21791da177e4SLinus Torvalds 
2180e837f9b5SJoe Perches 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2181e837f9b5SJoe Perches 		 new_policy->cpu, new_policy->min, new_policy->max);
21821da177e4SLinus Torvalds 
2183d5b73cd8SViresh Kumar 	memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
21841da177e4SLinus Torvalds 
2185d9a789c7SRafael J. Wysocki 	if (new_policy->min > policy->max || new_policy->max < policy->min)
2186d9a789c7SRafael J. Wysocki 		return -EINVAL;
21879c9a43edSMattia Dongili 
21881da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
21893a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
21901da177e4SLinus Torvalds 	if (ret)
2191d9a789c7SRafael J. Wysocki 		return ret;
21921da177e4SLinus Torvalds 
21931da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
2194e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
21953a3e9e06SViresh Kumar 			CPUFREQ_ADJUST, new_policy);
21961da177e4SLinus Torvalds 
21971da177e4SLinus Torvalds 	/* adjust if necessary - hardware incompatibility*/
2198e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
21993a3e9e06SViresh Kumar 			CPUFREQ_INCOMPATIBLE, new_policy);
22001da177e4SLinus Torvalds 
2201bb176f7dSViresh Kumar 	/*
2202bb176f7dSViresh Kumar 	 * verify the cpu speed can be set within this limit, which might be
2203bb176f7dSViresh Kumar 	 * different to the first one
2204bb176f7dSViresh Kumar 	 */
22053a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
2206e041c683SAlan Stern 	if (ret)
2207d9a789c7SRafael J. Wysocki 		return ret;
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	/* notification of the new policy */
2210e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
22113a3e9e06SViresh Kumar 			CPUFREQ_NOTIFY, new_policy);
22121da177e4SLinus Torvalds 
22133a3e9e06SViresh Kumar 	policy->min = new_policy->min;
22143a3e9e06SViresh Kumar 	policy->max = new_policy->max;
22151da177e4SLinus Torvalds 
22162d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
22173a3e9e06SViresh Kumar 		 policy->min, policy->max);
22181da177e4SLinus Torvalds 
22191c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
22203a3e9e06SViresh Kumar 		policy->policy = new_policy->policy;
22212d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
2222d9a789c7SRafael J. Wysocki 		return cpufreq_driver->setpolicy(new_policy);
2223d9a789c7SRafael J. Wysocki 	}
2224d9a789c7SRafael J. Wysocki 
2225d9a789c7SRafael J. Wysocki 	if (new_policy->governor == policy->governor)
2226d9a789c7SRafael J. Wysocki 		goto out;
22271da177e4SLinus Torvalds 
22282d06d8c4SDominik Brodowski 	pr_debug("governor switch\n");
22291da177e4SLinus Torvalds 
2230d9a789c7SRafael J. Wysocki 	/* save old, working values */
2231d9a789c7SRafael J. Wysocki 	old_gov = policy->governor;
22321da177e4SLinus Torvalds 	/* end old governor */
2233d9a789c7SRafael J. Wysocki 	if (old_gov) {
22343a3e9e06SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2235ad7722daSviresh kumar 		up_write(&policy->rwsem);
2236d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2237ad7722daSviresh kumar 		down_write(&policy->rwsem);
22387bd353a9SViresh Kumar 	}
22391da177e4SLinus Torvalds 
22401da177e4SLinus Torvalds 	/* start new governor */
22413a3e9e06SViresh Kumar 	policy->governor = new_policy->governor;
22423a3e9e06SViresh Kumar 	if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2243d9a789c7SRafael J. Wysocki 		if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2244d9a789c7SRafael J. Wysocki 			goto out;
2245d9a789c7SRafael J. Wysocki 
2246ad7722daSviresh kumar 		up_write(&policy->rwsem);
2247d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2248ad7722daSviresh kumar 		down_write(&policy->rwsem);
2249955ef483SViresh Kumar 	}
22507bd353a9SViresh Kumar 
22511da177e4SLinus Torvalds 	/* new governor failed, so re-start old one */
2252d9a789c7SRafael J. Wysocki 	pr_debug("starting governor %s failed\n", policy->governor->name);
22531da177e4SLinus Torvalds 	if (old_gov) {
22543a3e9e06SViresh Kumar 		policy->governor = old_gov;
2255d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2256d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_START);
22571da177e4SLinus Torvalds 	}
22581da177e4SLinus Torvalds 
2259d9a789c7SRafael J. Wysocki 	return -EINVAL;
2260d9a789c7SRafael J. Wysocki 
2261d9a789c7SRafael J. Wysocki  out:
2262d9a789c7SRafael J. Wysocki 	pr_debug("governor: change or update limits\n");
2263d9a789c7SRafael J. Wysocki 	return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
22641da177e4SLinus Torvalds }
22651da177e4SLinus Torvalds 
22661da177e4SLinus Torvalds /**
22671da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
22681da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
22691da177e4SLinus Torvalds  *
227025985edcSLucas De Marchi  *	Useful for policy notifiers which have different necessities
22711da177e4SLinus Torvalds  *	at different times.
22721da177e4SLinus Torvalds  */
22731da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
22741da177e4SLinus Torvalds {
22753a3e9e06SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
22763a3e9e06SViresh Kumar 	struct cpufreq_policy new_policy;
2277f1829e4aSJulia Lawall 	int ret;
22781da177e4SLinus Torvalds 
2279fefa8ff8SAaron Plattner 	if (!policy)
2280fefa8ff8SAaron Plattner 		return -ENODEV;
22811da177e4SLinus Torvalds 
2282ad7722daSviresh kumar 	down_write(&policy->rwsem);
22831da177e4SLinus Torvalds 
22842d06d8c4SDominik Brodowski 	pr_debug("updating policy for CPU %u\n", cpu);
2285d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
22863a3e9e06SViresh Kumar 	new_policy.min = policy->user_policy.min;
22873a3e9e06SViresh Kumar 	new_policy.max = policy->user_policy.max;
22883a3e9e06SViresh Kumar 	new_policy.policy = policy->user_policy.policy;
22893a3e9e06SViresh Kumar 	new_policy.governor = policy->user_policy.governor;
22901da177e4SLinus Torvalds 
2291bb176f7dSViresh Kumar 	/*
2292bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
2293bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
2294bb176f7dSViresh Kumar 	 */
22952ed99e39SRafael J. Wysocki 	if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
22963a3e9e06SViresh Kumar 		new_policy.cur = cpufreq_driver->get(cpu);
2297bd0fa9bbSViresh Kumar 		if (WARN_ON(!new_policy.cur)) {
2298bd0fa9bbSViresh Kumar 			ret = -EIO;
2299fefa8ff8SAaron Plattner 			goto unlock;
2300bd0fa9bbSViresh Kumar 		}
2301bd0fa9bbSViresh Kumar 
23023a3e9e06SViresh Kumar 		if (!policy->cur) {
2303e837f9b5SJoe Perches 			pr_debug("Driver did not initialize current freq\n");
23043a3e9e06SViresh Kumar 			policy->cur = new_policy.cur;
2305a85f7bd3SThomas Renninger 		} else {
23069c0ebcf7SViresh Kumar 			if (policy->cur != new_policy.cur && has_target())
23073a3e9e06SViresh Kumar 				cpufreq_out_of_sync(cpu, policy->cur,
23083a3e9e06SViresh Kumar 								new_policy.cur);
23090961dd0dSThomas Renninger 		}
2310a85f7bd3SThomas Renninger 	}
23110961dd0dSThomas Renninger 
2312037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
23131da177e4SLinus Torvalds 
2314fefa8ff8SAaron Plattner unlock:
2315ad7722daSviresh kumar 	up_write(&policy->rwsem);
23165a01f2e8SVenkatesh Pallipadi 
23173a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
23181da177e4SLinus Torvalds 	return ret;
23191da177e4SLinus Torvalds }
23201da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
23211da177e4SLinus Torvalds 
23222760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb,
2323c32b6b8eSAshok Raj 					unsigned long action, void *hcpu)
2324c32b6b8eSAshok Raj {
2325c32b6b8eSAshok Raj 	unsigned int cpu = (unsigned long)hcpu;
23268a25a2fdSKay Sievers 	struct device *dev;
2327c32b6b8eSAshok Raj 
23288a25a2fdSKay Sievers 	dev = get_cpu_device(cpu);
23298a25a2fdSKay Sievers 	if (dev) {
23305302c3fbSSrivatsa S. Bhat 		switch (action & ~CPU_TASKS_FROZEN) {
2331c32b6b8eSAshok Raj 		case CPU_ONLINE:
233296bbbe4aSViresh Kumar 			__cpufreq_add_dev(dev, NULL);
2333c32b6b8eSAshok Raj 			break;
23345302c3fbSSrivatsa S. Bhat 
2335c32b6b8eSAshok Raj 		case CPU_DOWN_PREPARE:
233696bbbe4aSViresh Kumar 			__cpufreq_remove_dev_prepare(dev, NULL);
23371aee40acSSrivatsa S. Bhat 			break;
23381aee40acSSrivatsa S. Bhat 
23391aee40acSSrivatsa S. Bhat 		case CPU_POST_DEAD:
234096bbbe4aSViresh Kumar 			__cpufreq_remove_dev_finish(dev, NULL);
2341c32b6b8eSAshok Raj 			break;
23425302c3fbSSrivatsa S. Bhat 
23435a01f2e8SVenkatesh Pallipadi 		case CPU_DOWN_FAILED:
234496bbbe4aSViresh Kumar 			__cpufreq_add_dev(dev, NULL);
2345c32b6b8eSAshok Raj 			break;
2346c32b6b8eSAshok Raj 		}
2347c32b6b8eSAshok Raj 	}
2348c32b6b8eSAshok Raj 	return NOTIFY_OK;
2349c32b6b8eSAshok Raj }
2350c32b6b8eSAshok Raj 
23519c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = {
2352c32b6b8eSAshok Raj 	.notifier_call = cpufreq_cpu_callback,
2353c32b6b8eSAshok Raj };
23541da177e4SLinus Torvalds 
23551da177e4SLinus Torvalds /*********************************************************************
23566f19efc0SLukasz Majewski  *               BOOST						     *
23576f19efc0SLukasz Majewski  *********************************************************************/
23586f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state)
23596f19efc0SLukasz Majewski {
23606f19efc0SLukasz Majewski 	struct cpufreq_frequency_table *freq_table;
23616f19efc0SLukasz Majewski 	struct cpufreq_policy *policy;
23626f19efc0SLukasz Majewski 	int ret = -EINVAL;
23636f19efc0SLukasz Majewski 
23646f19efc0SLukasz Majewski 	list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
23656f19efc0SLukasz Majewski 		freq_table = cpufreq_frequency_get_table(policy->cpu);
23666f19efc0SLukasz Majewski 		if (freq_table) {
23676f19efc0SLukasz Majewski 			ret = cpufreq_frequency_table_cpuinfo(policy,
23686f19efc0SLukasz Majewski 							freq_table);
23696f19efc0SLukasz Majewski 			if (ret) {
23706f19efc0SLukasz Majewski 				pr_err("%s: Policy frequency update failed\n",
23716f19efc0SLukasz Majewski 				       __func__);
23726f19efc0SLukasz Majewski 				break;
23736f19efc0SLukasz Majewski 			}
23746f19efc0SLukasz Majewski 			policy->user_policy.max = policy->max;
23756f19efc0SLukasz Majewski 			__cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
23766f19efc0SLukasz Majewski 		}
23776f19efc0SLukasz Majewski 	}
23786f19efc0SLukasz Majewski 
23796f19efc0SLukasz Majewski 	return ret;
23806f19efc0SLukasz Majewski }
23816f19efc0SLukasz Majewski 
23826f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state)
23836f19efc0SLukasz Majewski {
23846f19efc0SLukasz Majewski 	unsigned long flags;
23856f19efc0SLukasz Majewski 	int ret = 0;
23866f19efc0SLukasz Majewski 
23876f19efc0SLukasz Majewski 	if (cpufreq_driver->boost_enabled == state)
23886f19efc0SLukasz Majewski 		return 0;
23896f19efc0SLukasz Majewski 
23906f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
23916f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = state;
23926f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23936f19efc0SLukasz Majewski 
23946f19efc0SLukasz Majewski 	ret = cpufreq_driver->set_boost(state);
23956f19efc0SLukasz Majewski 	if (ret) {
23966f19efc0SLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
23976f19efc0SLukasz Majewski 		cpufreq_driver->boost_enabled = !state;
23986f19efc0SLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23996f19efc0SLukasz Majewski 
2400e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST\n",
2401e837f9b5SJoe Perches 		       __func__, state ? "enable" : "disable");
24026f19efc0SLukasz Majewski 	}
24036f19efc0SLukasz Majewski 
24046f19efc0SLukasz Majewski 	return ret;
24056f19efc0SLukasz Majewski }
24066f19efc0SLukasz Majewski 
24076f19efc0SLukasz Majewski int cpufreq_boost_supported(void)
24086f19efc0SLukasz Majewski {
24096f19efc0SLukasz Majewski 	if (likely(cpufreq_driver))
24106f19efc0SLukasz Majewski 		return cpufreq_driver->boost_supported;
24116f19efc0SLukasz Majewski 
24126f19efc0SLukasz Majewski 	return 0;
24136f19efc0SLukasz Majewski }
24146f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
24156f19efc0SLukasz Majewski 
24166f19efc0SLukasz Majewski int cpufreq_boost_enabled(void)
24176f19efc0SLukasz Majewski {
24186f19efc0SLukasz Majewski 	return cpufreq_driver->boost_enabled;
24196f19efc0SLukasz Majewski }
24206f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
24216f19efc0SLukasz Majewski 
24226f19efc0SLukasz Majewski /*********************************************************************
24231da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
24241da177e4SLinus Torvalds  *********************************************************************/
24251da177e4SLinus Torvalds 
24261da177e4SLinus Torvalds /**
24271da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
24281da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
24291da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
24301da177e4SLinus Torvalds  *
24311da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
24321da177e4SLinus Torvalds  * returns zero on success, -EBUSY when another driver got here first
24331da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
24341da177e4SLinus Torvalds  *
24351da177e4SLinus Torvalds  */
2436221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
24371da177e4SLinus Torvalds {
24381da177e4SLinus Torvalds 	unsigned long flags;
24391da177e4SLinus Torvalds 	int ret;
24401da177e4SLinus Torvalds 
2441a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2442a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2443a7b422cdSKonrad Rzeszutek Wilk 
24441da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
24459c0ebcf7SViresh Kumar 	    !(driver_data->setpolicy || driver_data->target_index ||
24469832235fSRafael J. Wysocki 		    driver_data->target) ||
24479832235fSRafael J. Wysocki 	     (driver_data->setpolicy && (driver_data->target_index ||
24481c03a2d0SViresh Kumar 		    driver_data->target)) ||
24491c03a2d0SViresh Kumar 	     (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
24501da177e4SLinus Torvalds 		return -EINVAL;
24511da177e4SLinus Torvalds 
24522d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
24531da177e4SLinus Torvalds 
24541da177e4SLinus Torvalds 	if (driver_data->setpolicy)
24551da177e4SLinus Torvalds 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
24561da177e4SLinus Torvalds 
24570d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
24581c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
24590d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24604dea5806SYinghai Lu 		return -EEXIST;
24611da177e4SLinus Torvalds 	}
24621c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
24630d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24641da177e4SLinus Torvalds 
24656f19efc0SLukasz Majewski 	if (cpufreq_boost_supported()) {
24666f19efc0SLukasz Majewski 		/*
24676f19efc0SLukasz Majewski 		 * Check if driver provides function to enable boost -
24686f19efc0SLukasz Majewski 		 * if not, use cpufreq_boost_set_sw as default
24696f19efc0SLukasz Majewski 		 */
24706f19efc0SLukasz Majewski 		if (!cpufreq_driver->set_boost)
24716f19efc0SLukasz Majewski 			cpufreq_driver->set_boost = cpufreq_boost_set_sw;
24726f19efc0SLukasz Majewski 
24736f19efc0SLukasz Majewski 		ret = cpufreq_sysfs_create_file(&boost.attr);
24746f19efc0SLukasz Majewski 		if (ret) {
24756f19efc0SLukasz Majewski 			pr_err("%s: cannot register global BOOST sysfs file\n",
24766f19efc0SLukasz Majewski 			       __func__);
24776f19efc0SLukasz Majewski 			goto err_null_driver;
24786f19efc0SLukasz Majewski 		}
24796f19efc0SLukasz Majewski 	}
24806f19efc0SLukasz Majewski 
24818a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
24828f5bc2abSJiri Slaby 	if (ret)
24836f19efc0SLukasz Majewski 		goto err_boost_unreg;
24841da177e4SLinus Torvalds 
24851c3d85ddSRafael J. Wysocki 	if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
24861da177e4SLinus Torvalds 		int i;
24871da177e4SLinus Torvalds 		ret = -ENODEV;
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds 		/* check for at least one working CPU */
24907a6aedfaSMike Travis 		for (i = 0; i < nr_cpu_ids; i++)
24917a6aedfaSMike Travis 			if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
24921da177e4SLinus Torvalds 				ret = 0;
24937a6aedfaSMike Travis 				break;
24947a6aedfaSMike Travis 			}
24951da177e4SLinus Torvalds 
24961da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
24971da177e4SLinus Torvalds 		if (ret) {
24982d06d8c4SDominik Brodowski 			pr_debug("no CPU initialized for driver %s\n",
2499e08f5f5bSGautham R Shenoy 				 driver_data->name);
25008a25a2fdSKay Sievers 			goto err_if_unreg;
25011da177e4SLinus Torvalds 		}
25021da177e4SLinus Torvalds 	}
25031da177e4SLinus Torvalds 
250465edc68cSChandra Seetharaman 	register_hotcpu_notifier(&cpufreq_cpu_notifier);
25052d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
25061da177e4SLinus Torvalds 
25078f5bc2abSJiri Slaby 	return 0;
25088a25a2fdSKay Sievers err_if_unreg:
25098a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
25106f19efc0SLukasz Majewski err_boost_unreg:
25116f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
25126f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
25138f5bc2abSJiri Slaby err_null_driver:
25140d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
25151c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
25160d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
25174d34a67dSDave Jones 	return ret;
25181da177e4SLinus Torvalds }
25191da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
25201da177e4SLinus Torvalds 
25211da177e4SLinus Torvalds /**
25221da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
25231da177e4SLinus Torvalds  *
25241da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
25251da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
25261da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
25271da177e4SLinus Torvalds  * currently not initialised.
25281da177e4SLinus Torvalds  */
2529221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
25301da177e4SLinus Torvalds {
25311da177e4SLinus Torvalds 	unsigned long flags;
25321da177e4SLinus Torvalds 
25331c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
25341da177e4SLinus Torvalds 		return -EINVAL;
25351da177e4SLinus Torvalds 
25362d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
25371da177e4SLinus Torvalds 
25388a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
25396f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
25406f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
25416f19efc0SLukasz Majewski 
254265edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
25431da177e4SLinus Torvalds 
25446eed9404SViresh Kumar 	down_write(&cpufreq_rwsem);
25450d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
25466eed9404SViresh Kumar 
25471c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
25486eed9404SViresh Kumar 
25490d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
25506eed9404SViresh Kumar 	up_write(&cpufreq_rwsem);
25511da177e4SLinus Torvalds 
25521da177e4SLinus Torvalds 	return 0;
25531da177e4SLinus Torvalds }
25541da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
25555a01f2e8SVenkatesh Pallipadi 
255690de2a4aSDoug Anderson /*
255790de2a4aSDoug Anderson  * Stop cpufreq at shutdown to make sure it isn't holding any locks
255890de2a4aSDoug Anderson  * or mutexes when secondary CPUs are halted.
255990de2a4aSDoug Anderson  */
256090de2a4aSDoug Anderson static struct syscore_ops cpufreq_syscore_ops = {
256190de2a4aSDoug Anderson 	.shutdown = cpufreq_suspend,
256290de2a4aSDoug Anderson };
256390de2a4aSDoug Anderson 
25645a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
25655a01f2e8SVenkatesh Pallipadi {
2566a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2567a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2568a7b422cdSKonrad Rzeszutek Wilk 
25692361be23SViresh Kumar 	cpufreq_global_kobject = kobject_create();
25708aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
25718aa84ad8SThomas Renninger 
257290de2a4aSDoug Anderson 	register_syscore_ops(&cpufreq_syscore_ops);
257390de2a4aSDoug Anderson 
25745a01f2e8SVenkatesh Pallipadi 	return 0;
25755a01f2e8SVenkatesh Pallipadi }
25765a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2577