xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision 1c03a2d04d7ab6d27c1fef8614f08187d974bd21)
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>
305ff0a268SViresh Kumar #include <linux/tick.h>
316f4f2723SThomas Renninger #include <trace/events/power.h>
326f4f2723SThomas Renninger 
331da177e4SLinus Torvalds /**
34cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
351da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
361da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
371da177e4SLinus Torvalds  */
381c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
397a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
408414809cSSrivatsa S. Bhat static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
41bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
426f1e4efdSJane Li DEFINE_MUTEX(cpufreq_governor_lock);
43c88a1f8bSLukasz Majewski static LIST_HEAD(cpufreq_policy_list);
44bb176f7dSViresh Kumar 
45084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */
46e77b89f1SDmitry Monakhov static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
471da177e4SLinus Torvalds 
482f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */
492f0aea93SViresh Kumar static bool cpufreq_suspended;
501da177e4SLinus Torvalds 
519c0ebcf7SViresh Kumar static inline bool has_target(void)
529c0ebcf7SViresh Kumar {
539c0ebcf7SViresh Kumar 	return cpufreq_driver->target_index || cpufreq_driver->target;
549c0ebcf7SViresh Kumar }
559c0ebcf7SViresh Kumar 
565a01f2e8SVenkatesh Pallipadi /*
576eed9404SViresh Kumar  * rwsem to guarantee that cpufreq driver module doesn't unload during critical
586eed9404SViresh Kumar  * sections
596eed9404SViresh Kumar  */
606eed9404SViresh Kumar static DECLARE_RWSEM(cpufreq_rwsem);
616eed9404SViresh Kumar 
621da177e4SLinus Torvalds /* internal prototypes */
6329464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy,
6429464f28SDave Jones 		unsigned int event);
655a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu);
6665f27f38SDavid Howells static void handle_update(struct work_struct *work);
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /**
691da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
701da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
711da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
721da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
731da177e4SLinus Torvalds  * The mutex locks both lists.
741da177e4SLinus Torvalds  */
75e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
76b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list;
771da177e4SLinus Torvalds 
7874212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called;
79b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void)
80b4dfdbb3SAlan Stern {
81b4dfdbb3SAlan Stern 	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
8274212ca4SCesar Eduardo Barros 	init_cpufreq_transition_notifier_list_called = true;
83b4dfdbb3SAlan Stern 	return 0;
84b4dfdbb3SAlan Stern }
85b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list);
861da177e4SLinus Torvalds 
87a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
88da584455SViresh Kumar static int cpufreq_disabled(void)
89a7b422cdSKonrad Rzeszutek Wilk {
90a7b422cdSKonrad Rzeszutek Wilk 	return off;
91a7b422cdSKonrad Rzeszutek Wilk }
92a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
93a7b422cdSKonrad Rzeszutek Wilk {
94a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
95a7b422cdSKonrad Rzeszutek Wilk }
961da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list);
973fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
981da177e4SLinus Torvalds 
994d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1004d5dcc42SViresh Kumar {
1010b981e70SViresh Kumar 	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
1024d5dcc42SViresh Kumar }
1033f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1044d5dcc42SViresh Kumar 
105944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
106944e9a03SViresh Kumar {
107944e9a03SViresh Kumar 	if (have_governor_per_policy())
108944e9a03SViresh Kumar 		return &policy->kobj;
109944e9a03SViresh Kumar 	else
110944e9a03SViresh Kumar 		return cpufreq_global_kobject;
111944e9a03SViresh Kumar }
112944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
113944e9a03SViresh Kumar 
11472a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
11572a4ce34SViresh Kumar {
11672a4ce34SViresh Kumar 	u64 idle_time;
11772a4ce34SViresh Kumar 	u64 cur_wall_time;
11872a4ce34SViresh Kumar 	u64 busy_time;
11972a4ce34SViresh Kumar 
12072a4ce34SViresh Kumar 	cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
12172a4ce34SViresh Kumar 
12272a4ce34SViresh Kumar 	busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
12372a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
12472a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
12572a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
12672a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
12772a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
12872a4ce34SViresh Kumar 
12972a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
13072a4ce34SViresh Kumar 	if (wall)
13172a4ce34SViresh Kumar 		*wall = cputime_to_usecs(cur_wall_time);
13272a4ce34SViresh Kumar 
13372a4ce34SViresh Kumar 	return cputime_to_usecs(idle_time);
13472a4ce34SViresh Kumar }
13572a4ce34SViresh Kumar 
13672a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
13772a4ce34SViresh Kumar {
13872a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
13972a4ce34SViresh Kumar 
14072a4ce34SViresh Kumar 	if (idle_time == -1ULL)
14172a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
14272a4ce34SViresh Kumar 	else if (!io_busy)
14372a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
14472a4ce34SViresh Kumar 
14572a4ce34SViresh Kumar 	return idle_time;
14672a4ce34SViresh Kumar }
14772a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
14872a4ce34SViresh Kumar 
14970e9e778SViresh Kumar /*
15070e9e778SViresh Kumar  * This is a generic cpufreq init() routine which can be used by cpufreq
15170e9e778SViresh Kumar  * drivers of SMP systems. It will do following:
15270e9e778SViresh Kumar  * - validate & show freq table passed
15370e9e778SViresh Kumar  * - set policies transition latency
15470e9e778SViresh Kumar  * - policy->cpus with all possible CPUs
15570e9e778SViresh Kumar  */
15670e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy,
15770e9e778SViresh Kumar 		struct cpufreq_frequency_table *table,
15870e9e778SViresh Kumar 		unsigned int transition_latency)
15970e9e778SViresh Kumar {
16070e9e778SViresh Kumar 	int ret;
16170e9e778SViresh Kumar 
16270e9e778SViresh Kumar 	ret = cpufreq_table_validate_and_show(policy, table);
16370e9e778SViresh Kumar 	if (ret) {
16470e9e778SViresh Kumar 		pr_err("%s: invalid frequency table: %d\n", __func__, ret);
16570e9e778SViresh Kumar 		return ret;
16670e9e778SViresh Kumar 	}
16770e9e778SViresh Kumar 
16870e9e778SViresh Kumar 	policy->cpuinfo.transition_latency = transition_latency;
16970e9e778SViresh Kumar 
17070e9e778SViresh Kumar 	/*
17170e9e778SViresh Kumar 	 * The driver only supports the SMP configuartion where all processors
17270e9e778SViresh Kumar 	 * share the clock and voltage and clock.
17370e9e778SViresh Kumar 	 */
17470e9e778SViresh Kumar 	cpumask_setall(policy->cpus);
17570e9e778SViresh Kumar 
17670e9e778SViresh Kumar 	return 0;
17770e9e778SViresh Kumar }
17870e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init);
17970e9e778SViresh Kumar 
180652ed95dSViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu)
181652ed95dSViresh Kumar {
182652ed95dSViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
183652ed95dSViresh Kumar 
184652ed95dSViresh Kumar 	if (!policy || IS_ERR(policy->clk)) {
185e837f9b5SJoe Perches 		pr_err("%s: No %s associated to cpu: %d\n",
186e837f9b5SJoe Perches 		       __func__, policy ? "clk" : "policy", cpu);
187652ed95dSViresh Kumar 		return 0;
188652ed95dSViresh Kumar 	}
189652ed95dSViresh Kumar 
190652ed95dSViresh Kumar 	return clk_get_rate(policy->clk) / 1000;
191652ed95dSViresh Kumar }
192652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get);
193652ed95dSViresh Kumar 
194e0b3165bSViresh Kumar /* Only for cpufreq core internal use */
195e0b3165bSViresh Kumar struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
196e0b3165bSViresh Kumar {
197e0b3165bSViresh Kumar 	return per_cpu(cpufreq_cpu_data, cpu);
198e0b3165bSViresh Kumar }
199e0b3165bSViresh Kumar 
2006eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
2011da177e4SLinus Torvalds {
2026eed9404SViresh Kumar 	struct cpufreq_policy *policy = NULL;
2031da177e4SLinus Torvalds 	unsigned long flags;
2041da177e4SLinus Torvalds 
2056eed9404SViresh Kumar 	if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
2066eed9404SViresh Kumar 		return NULL;
2076eed9404SViresh Kumar 
2086eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
2096eed9404SViresh Kumar 		return NULL;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	/* get the cpufreq driver */
2120d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
2131da177e4SLinus Torvalds 
2146eed9404SViresh Kumar 	if (cpufreq_driver) {
2151da177e4SLinus Torvalds 		/* get the CPU */
2163a3e9e06SViresh Kumar 		policy = per_cpu(cpufreq_cpu_data, cpu);
2176eed9404SViresh Kumar 		if (policy)
2186eed9404SViresh Kumar 			kobject_get(&policy->kobj);
2196eed9404SViresh Kumar 	}
2206eed9404SViresh Kumar 
2216eed9404SViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2221da177e4SLinus Torvalds 
2233a3e9e06SViresh Kumar 	if (!policy)
2246eed9404SViresh Kumar 		up_read(&cpufreq_rwsem);
2251da177e4SLinus Torvalds 
2263a3e9e06SViresh Kumar 	return policy;
227a9144436SStephen Boyd }
2281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2291da177e4SLinus Torvalds 
2303a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
231a9144436SStephen Boyd {
232d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
233d5aaffa9SDirk Brandewie 		return;
234d5aaffa9SDirk Brandewie 
2356eed9404SViresh Kumar 	kobject_put(&policy->kobj);
2366eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
237a9144436SStephen Boyd }
2381da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds /*********************************************************************
2411da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2421da177e4SLinus Torvalds  *********************************************************************/
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds /**
2451da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
2461da177e4SLinus Torvalds  *
2471da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
2481da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
2491da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
2501da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
2511da177e4SLinus Torvalds  */
2521da177e4SLinus Torvalds #ifndef CONFIG_SMP
2531da177e4SLinus Torvalds static unsigned long l_p_j_ref;
2541da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq;
2551da177e4SLinus Torvalds 
256858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
2591da177e4SLinus Torvalds 		return;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
2621da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
2631da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
264e837f9b5SJoe Perches 		pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
265e837f9b5SJoe Perches 			 l_p_j_ref, l_p_j_ref_freq);
2661da177e4SLinus Torvalds 	}
2670b443eadSViresh Kumar 	if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
268e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
269e08f5f5bSGautham R Shenoy 								ci->new);
270e837f9b5SJoe Perches 		pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
271e837f9b5SJoe Perches 			 loops_per_jiffy, ci->new);
2721da177e4SLinus Torvalds 	}
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds #else
275e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
276e08f5f5bSGautham R Shenoy {
277e08f5f5bSGautham R Shenoy 	return;
278e08f5f5bSGautham R Shenoy }
2791da177e4SLinus Torvalds #endif
2801da177e4SLinus Torvalds 
2810956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
282b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
2831da177e4SLinus Torvalds {
2841da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
2851da177e4SLinus Torvalds 
286d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
287d5aaffa9SDirk Brandewie 		return;
288d5aaffa9SDirk Brandewie 
2891c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
2902d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
291e4472cb3SDave Jones 		 state, freqs->new);
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	switch (state) {
294e4472cb3SDave Jones 
2951da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
296e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
297e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
298e4472cb3SDave Jones 		 * "old frequency".
2991da177e4SLinus Torvalds 		 */
3001c3d85ddSRafael J. Wysocki 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
301e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
302e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
303e837f9b5SJoe Perches 				pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
304e4472cb3SDave Jones 					 freqs->old, policy->cur);
305e4472cb3SDave Jones 				freqs->old = policy->cur;
3061da177e4SLinus Torvalds 			}
3071da177e4SLinus Torvalds 		}
308b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
309e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
3101da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3111da177e4SLinus Torvalds 		break;
312e4472cb3SDave Jones 
3131da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3141da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
315e837f9b5SJoe Perches 		pr_debug("FREQ: %lu - CPU: %lu\n",
316e837f9b5SJoe Perches 			 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
31725e41933SThomas Renninger 		trace_cpu_frequency(freqs->new, freqs->cpu);
318b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
319e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
320e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
321e4472cb3SDave Jones 			policy->cur = freqs->new;
3221da177e4SLinus Torvalds 		break;
3231da177e4SLinus Torvalds 	}
3241da177e4SLinus Torvalds }
325bb176f7dSViresh Kumar 
326b43a7ffbSViresh Kumar /**
327b43a7ffbSViresh Kumar  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
328b43a7ffbSViresh Kumar  * on frequency transition.
329b43a7ffbSViresh Kumar  *
330b43a7ffbSViresh Kumar  * This function calls the transition notifiers and the "adjust_jiffies"
331b43a7ffbSViresh Kumar  * function. It is called twice on all CPU frequency changes that have
332b43a7ffbSViresh Kumar  * external effects.
333b43a7ffbSViresh Kumar  */
334236a9800SViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy,
335b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
336b43a7ffbSViresh Kumar {
337b43a7ffbSViresh Kumar 	for_each_cpu(freqs->cpu, policy->cpus)
338b43a7ffbSViresh Kumar 		__cpufreq_notify_transition(policy, freqs, state);
339b43a7ffbSViresh Kumar }
3401da177e4SLinus Torvalds 
341f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */
342236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
343f7ba3b41SViresh Kumar 		struct cpufreq_freqs *freqs, int transition_failed)
344f7ba3b41SViresh Kumar {
345f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
346f7ba3b41SViresh Kumar 	if (!transition_failed)
347f7ba3b41SViresh Kumar 		return;
348f7ba3b41SViresh Kumar 
349f7ba3b41SViresh Kumar 	swap(freqs->old, freqs->new);
350f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
351f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
352f7ba3b41SViresh Kumar }
353f7ba3b41SViresh Kumar 
35412478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
35512478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs)
35612478cf0SSrivatsa S. Bhat {
357ca654dc3SSrivatsa S. Bhat 
358ca654dc3SSrivatsa S. Bhat 	/*
359ca654dc3SSrivatsa S. Bhat 	 * Catch double invocations of _begin() which lead to self-deadlock.
360ca654dc3SSrivatsa S. Bhat 	 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
361ca654dc3SSrivatsa S. Bhat 	 * doesn't invoke _begin() on their behalf, and hence the chances of
362ca654dc3SSrivatsa S. Bhat 	 * double invocations are very low. Moreover, there are scenarios
363ca654dc3SSrivatsa S. Bhat 	 * where these checks can emit false-positive warnings in these
364ca654dc3SSrivatsa S. Bhat 	 * drivers; so we avoid that by skipping them altogether.
365ca654dc3SSrivatsa S. Bhat 	 */
366ca654dc3SSrivatsa S. Bhat 	WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
367ca654dc3SSrivatsa S. Bhat 				&& current == policy->transition_task);
368ca654dc3SSrivatsa S. Bhat 
36912478cf0SSrivatsa S. Bhat wait:
37012478cf0SSrivatsa S. Bhat 	wait_event(policy->transition_wait, !policy->transition_ongoing);
37112478cf0SSrivatsa S. Bhat 
37212478cf0SSrivatsa S. Bhat 	spin_lock(&policy->transition_lock);
37312478cf0SSrivatsa S. Bhat 
37412478cf0SSrivatsa S. Bhat 	if (unlikely(policy->transition_ongoing)) {
37512478cf0SSrivatsa S. Bhat 		spin_unlock(&policy->transition_lock);
37612478cf0SSrivatsa S. Bhat 		goto wait;
37712478cf0SSrivatsa S. Bhat 	}
37812478cf0SSrivatsa S. Bhat 
37912478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = true;
380ca654dc3SSrivatsa S. Bhat 	policy->transition_task = current;
38112478cf0SSrivatsa S. Bhat 
38212478cf0SSrivatsa S. Bhat 	spin_unlock(&policy->transition_lock);
38312478cf0SSrivatsa S. Bhat 
38412478cf0SSrivatsa S. Bhat 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
38512478cf0SSrivatsa S. Bhat }
38612478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
38712478cf0SSrivatsa S. Bhat 
38812478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
38912478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs, int transition_failed)
39012478cf0SSrivatsa S. Bhat {
39112478cf0SSrivatsa S. Bhat 	if (unlikely(WARN_ON(!policy->transition_ongoing)))
39212478cf0SSrivatsa S. Bhat 		return;
39312478cf0SSrivatsa S. Bhat 
39412478cf0SSrivatsa S. Bhat 	cpufreq_notify_post_transition(policy, freqs, transition_failed);
39512478cf0SSrivatsa S. Bhat 
39612478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = false;
397ca654dc3SSrivatsa S. Bhat 	policy->transition_task = NULL;
39812478cf0SSrivatsa S. Bhat 
39912478cf0SSrivatsa S. Bhat 	wake_up(&policy->transition_wait);
40012478cf0SSrivatsa S. Bhat }
40112478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
40212478cf0SSrivatsa S. Bhat 
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds /*********************************************************************
4051da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
4061da177e4SLinus Torvalds  *********************************************************************/
4078a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj,
4086f19efc0SLukasz Majewski 				 struct attribute *attr, char *buf)
4096f19efc0SLukasz Majewski {
4106f19efc0SLukasz Majewski 	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
4116f19efc0SLukasz Majewski }
4126f19efc0SLukasz Majewski 
4136f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
4146f19efc0SLukasz Majewski 				  const char *buf, size_t count)
4156f19efc0SLukasz Majewski {
4166f19efc0SLukasz Majewski 	int ret, enable;
4176f19efc0SLukasz Majewski 
4186f19efc0SLukasz Majewski 	ret = sscanf(buf, "%d", &enable);
4196f19efc0SLukasz Majewski 	if (ret != 1 || enable < 0 || enable > 1)
4206f19efc0SLukasz Majewski 		return -EINVAL;
4216f19efc0SLukasz Majewski 
4226f19efc0SLukasz Majewski 	if (cpufreq_boost_trigger_state(enable)) {
423e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST!\n",
424e837f9b5SJoe Perches 		       __func__, enable ? "enable" : "disable");
4256f19efc0SLukasz Majewski 		return -EINVAL;
4266f19efc0SLukasz Majewski 	}
4276f19efc0SLukasz Majewski 
428e837f9b5SJoe Perches 	pr_debug("%s: cpufreq BOOST %s\n",
429e837f9b5SJoe Perches 		 __func__, enable ? "enabled" : "disabled");
4306f19efc0SLukasz Majewski 
4316f19efc0SLukasz Majewski 	return count;
4326f19efc0SLukasz Majewski }
4336f19efc0SLukasz Majewski define_one_global_rw(boost);
4341da177e4SLinus Torvalds 
4353bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor)
4363bcb09a3SJeremy Fitzhardinge {
4373bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
4383bcb09a3SJeremy Fitzhardinge 
4393bcb09a3SJeremy Fitzhardinge 	list_for_each_entry(t, &cpufreq_governor_list, governor_list)
4403bcb09a3SJeremy Fitzhardinge 		if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
4413bcb09a3SJeremy Fitzhardinge 			return t;
4423bcb09a3SJeremy Fitzhardinge 
4433bcb09a3SJeremy Fitzhardinge 	return NULL;
4443bcb09a3SJeremy Fitzhardinge }
4453bcb09a3SJeremy Fitzhardinge 
4461da177e4SLinus Torvalds /**
4471da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
4481da177e4SLinus Torvalds  */
4491da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
4501da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
4511da177e4SLinus Torvalds {
4523bcb09a3SJeremy Fitzhardinge 	int err = -EINVAL;
4533bcb09a3SJeremy Fitzhardinge 
4541c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver)
4553bcb09a3SJeremy Fitzhardinge 		goto out;
4563bcb09a3SJeremy Fitzhardinge 
4571c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
4581da177e4SLinus Torvalds 		if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
4591da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
4603bcb09a3SJeremy Fitzhardinge 			err = 0;
461e08f5f5bSGautham R Shenoy 		} else if (!strnicmp(str_governor, "powersave",
462e08f5f5bSGautham R Shenoy 						CPUFREQ_NAME_LEN)) {
4631da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
4643bcb09a3SJeremy Fitzhardinge 			err = 0;
4651da177e4SLinus Torvalds 		}
4669c0ebcf7SViresh Kumar 	} else if (has_target()) {
4671da177e4SLinus Torvalds 		struct cpufreq_governor *t;
4683bcb09a3SJeremy Fitzhardinge 
4693fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
4703bcb09a3SJeremy Fitzhardinge 
4713bcb09a3SJeremy Fitzhardinge 		t = __find_governor(str_governor);
4723bcb09a3SJeremy Fitzhardinge 
473ea714970SJeremy Fitzhardinge 		if (t == NULL) {
474ea714970SJeremy Fitzhardinge 			int ret;
475ea714970SJeremy Fitzhardinge 
476ea714970SJeremy Fitzhardinge 			mutex_unlock(&cpufreq_governor_mutex);
4771a8e1463SKees Cook 			ret = request_module("cpufreq_%s", str_governor);
478ea714970SJeremy Fitzhardinge 			mutex_lock(&cpufreq_governor_mutex);
479ea714970SJeremy Fitzhardinge 
480ea714970SJeremy Fitzhardinge 			if (ret == 0)
481ea714970SJeremy Fitzhardinge 				t = __find_governor(str_governor);
482ea714970SJeremy Fitzhardinge 		}
483ea714970SJeremy Fitzhardinge 
4843bcb09a3SJeremy Fitzhardinge 		if (t != NULL) {
4851da177e4SLinus Torvalds 			*governor = t;
4863bcb09a3SJeremy Fitzhardinge 			err = 0;
4871da177e4SLinus Torvalds 		}
4883bcb09a3SJeremy Fitzhardinge 
4893bcb09a3SJeremy Fitzhardinge 		mutex_unlock(&cpufreq_governor_mutex);
4901da177e4SLinus Torvalds 	}
4911da177e4SLinus Torvalds out:
4923bcb09a3SJeremy Fitzhardinge 	return err;
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds /**
496e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
497e08f5f5bSGautham R Shenoy  * print out cpufreq information
4981da177e4SLinus Torvalds  *
4991da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
5001da177e4SLinus Torvalds  * "unsigned int".
5011da177e4SLinus Torvalds  */
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds #define show_one(file_name, object)			\
5041da177e4SLinus Torvalds static ssize_t show_##file_name				\
5051da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
5061da177e4SLinus Torvalds {							\
5071da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
5111da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
512ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
5131da177e4SLinus Torvalds show_one(scaling_min_freq, min);
5141da177e4SLinus Torvalds show_one(scaling_max_freq, max);
5151da177e4SLinus Torvalds show_one(scaling_cur_freq, cur);
5161da177e4SLinus Torvalds 
517037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
5183a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy);
5197970e08bSThomas Renninger 
5201da177e4SLinus Torvalds /**
5211da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
5221da177e4SLinus Torvalds  */
5231da177e4SLinus Torvalds #define store_one(file_name, object)			\
5241da177e4SLinus Torvalds static ssize_t store_##file_name					\
5251da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
5261da177e4SLinus Torvalds {									\
5275136fa56SSrivatsa S. Bhat 	int ret;							\
5281da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
5291da177e4SLinus Torvalds 									\
5301da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
5311da177e4SLinus Torvalds 	if (ret)							\
5321da177e4SLinus Torvalds 		return -EINVAL;						\
5331da177e4SLinus Torvalds 									\
5341da177e4SLinus Torvalds 	ret = sscanf(buf, "%u", &new_policy.object);			\
5351da177e4SLinus Torvalds 	if (ret != 1)							\
5361da177e4SLinus Torvalds 		return -EINVAL;						\
5371da177e4SLinus Torvalds 									\
538037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);		\
5397970e08bSThomas Renninger 	policy->user_policy.object = policy->object;			\
5401da177e4SLinus Torvalds 									\
5411da177e4SLinus Torvalds 	return ret ? ret : count;					\
5421da177e4SLinus Torvalds }
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds store_one(scaling_min_freq, min);
5451da177e4SLinus Torvalds store_one(scaling_max_freq, max);
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds /**
5481da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
5491da177e4SLinus Torvalds  */
550e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
551e08f5f5bSGautham R Shenoy 					char *buf)
5521da177e4SLinus Torvalds {
5535a01f2e8SVenkatesh Pallipadi 	unsigned int cur_freq = __cpufreq_get(policy->cpu);
5541da177e4SLinus Torvalds 	if (!cur_freq)
5551da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
5561da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
5571da177e4SLinus Torvalds }
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds /**
5601da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
5611da177e4SLinus Torvalds  */
562905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
5631da177e4SLinus Torvalds {
5641da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
5651da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
5661da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
5671da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
5681da177e4SLinus Torvalds 	else if (policy->governor)
5694b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
57029464f28SDave Jones 				policy->governor->name);
5711da177e4SLinus Torvalds 	return -EINVAL;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds /**
5751da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
5761da177e4SLinus Torvalds  */
5771da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
5781da177e4SLinus Torvalds 					const char *buf, size_t count)
5791da177e4SLinus Torvalds {
5805136fa56SSrivatsa S. Bhat 	int ret;
5811da177e4SLinus Torvalds 	char	str_governor[16];
5821da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);
5851da177e4SLinus Torvalds 	if (ret)
5861da177e4SLinus Torvalds 		return ret;
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
5891da177e4SLinus Torvalds 	if (ret != 1)
5901da177e4SLinus Torvalds 		return -EINVAL;
5911da177e4SLinus Torvalds 
592e08f5f5bSGautham R Shenoy 	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
593e08f5f5bSGautham R Shenoy 						&new_policy.governor))
5941da177e4SLinus Torvalds 		return -EINVAL;
5951da177e4SLinus Torvalds 
596037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
5977970e08bSThomas Renninger 
5987970e08bSThomas Renninger 	policy->user_policy.policy = policy->policy;
5997970e08bSThomas Renninger 	policy->user_policy.governor = policy->governor;
6007970e08bSThomas Renninger 
601e08f5f5bSGautham R Shenoy 	if (ret)
602e08f5f5bSGautham R Shenoy 		return ret;
603e08f5f5bSGautham R Shenoy 	else
604e08f5f5bSGautham R Shenoy 		return count;
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds /**
6081da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
6091da177e4SLinus Torvalds  */
6101da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
6111da177e4SLinus Torvalds {
6121c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
6131da177e4SLinus Torvalds }
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds /**
6161da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
6171da177e4SLinus Torvalds  */
6181da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
6191da177e4SLinus Torvalds 						char *buf)
6201da177e4SLinus Torvalds {
6211da177e4SLinus Torvalds 	ssize_t i = 0;
6221da177e4SLinus Torvalds 	struct cpufreq_governor *t;
6231da177e4SLinus Torvalds 
6249c0ebcf7SViresh Kumar 	if (!has_target()) {
6251da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
6261da177e4SLinus Torvalds 		goto out;
6271da177e4SLinus Torvalds 	}
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
63029464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
63129464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
6321da177e4SLinus Torvalds 			goto out;
6334b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
6341da177e4SLinus Torvalds 	}
6351da177e4SLinus Torvalds out:
6361da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
6371da177e4SLinus Torvalds 	return i;
6381da177e4SLinus Torvalds }
639e8628dd0SDarrick J. Wong 
640f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
6411da177e4SLinus Torvalds {
6421da177e4SLinus Torvalds 	ssize_t i = 0;
6431da177e4SLinus Torvalds 	unsigned int cpu;
6441da177e4SLinus Torvalds 
645835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
6461da177e4SLinus Torvalds 		if (i)
6471da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
6481da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
6491da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
6501da177e4SLinus Torvalds 			break;
6511da177e4SLinus Torvalds 	}
6521da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
6531da177e4SLinus Torvalds 	return i;
6541da177e4SLinus Torvalds }
655f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
6561da177e4SLinus Torvalds 
657e8628dd0SDarrick J. Wong /**
658e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
659e8628dd0SDarrick J. Wong  * hw coordination is in use
660e8628dd0SDarrick J. Wong  */
661e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
662e8628dd0SDarrick J. Wong {
663f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
664e8628dd0SDarrick J. Wong }
665e8628dd0SDarrick J. Wong 
666e8628dd0SDarrick J. Wong /**
667e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
668e8628dd0SDarrick J. Wong  */
669e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
670e8628dd0SDarrick J. Wong {
671f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
672e8628dd0SDarrick J. Wong }
673e8628dd0SDarrick J. Wong 
6749e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
6759e76988eSVenki Pallipadi 					const char *buf, size_t count)
6769e76988eSVenki Pallipadi {
6779e76988eSVenki Pallipadi 	unsigned int freq = 0;
6789e76988eSVenki Pallipadi 	unsigned int ret;
6799e76988eSVenki Pallipadi 
680879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
6819e76988eSVenki Pallipadi 		return -EINVAL;
6829e76988eSVenki Pallipadi 
6839e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
6849e76988eSVenki Pallipadi 	if (ret != 1)
6859e76988eSVenki Pallipadi 		return -EINVAL;
6869e76988eSVenki Pallipadi 
6879e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
6889e76988eSVenki Pallipadi 
6899e76988eSVenki Pallipadi 	return count;
6909e76988eSVenki Pallipadi }
6919e76988eSVenki Pallipadi 
6929e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
6939e76988eSVenki Pallipadi {
694879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
6959e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
6969e76988eSVenki Pallipadi 
6979e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
6989e76988eSVenki Pallipadi }
6991da177e4SLinus Torvalds 
700e2f74f35SThomas Renninger /**
7018bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
702e2f74f35SThomas Renninger  */
703e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
704e2f74f35SThomas Renninger {
705e2f74f35SThomas Renninger 	unsigned int limit;
706e2f74f35SThomas Renninger 	int ret;
7071c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
7081c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
709e2f74f35SThomas Renninger 		if (!ret)
710e2f74f35SThomas Renninger 			return sprintf(buf, "%u\n", limit);
711e2f74f35SThomas Renninger 	}
712e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
713e2f74f35SThomas Renninger }
714e2f74f35SThomas Renninger 
7156dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
7166dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
7176dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
7186dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
7196dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
7206dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
7216dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
7226dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
7236dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
7246dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
7256dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
7266dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
7276dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
7286dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds static struct attribute *default_attrs[] = {
7311da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
7321da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
733ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
7341da177e4SLinus Torvalds 	&scaling_min_freq.attr,
7351da177e4SLinus Torvalds 	&scaling_max_freq.attr,
7361da177e4SLinus Torvalds 	&affected_cpus.attr,
737e8628dd0SDarrick J. Wong 	&related_cpus.attr,
7381da177e4SLinus Torvalds 	&scaling_governor.attr,
7391da177e4SLinus Torvalds 	&scaling_driver.attr,
7401da177e4SLinus Torvalds 	&scaling_available_governors.attr,
7419e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
7421da177e4SLinus Torvalds 	NULL
7431da177e4SLinus Torvalds };
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
7461da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
7491da177e4SLinus Torvalds {
7501da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7511da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
7521b750e3bSViresh Kumar 	ssize_t ret;
7536eed9404SViresh Kumar 
7546eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
7551b750e3bSViresh Kumar 		return -EINVAL;
7565a01f2e8SVenkatesh Pallipadi 
757ad7722daSviresh kumar 	down_read(&policy->rwsem);
7585a01f2e8SVenkatesh Pallipadi 
759e08f5f5bSGautham R Shenoy 	if (fattr->show)
760e08f5f5bSGautham R Shenoy 		ret = fattr->show(policy, buf);
761e08f5f5bSGautham R Shenoy 	else
762e08f5f5bSGautham R Shenoy 		ret = -EIO;
763e08f5f5bSGautham R Shenoy 
764ad7722daSviresh kumar 	up_read(&policy->rwsem);
7656eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
7661b750e3bSViresh Kumar 
7671da177e4SLinus Torvalds 	return ret;
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
7711da177e4SLinus Torvalds 		     const char *buf, size_t count)
7721da177e4SLinus Torvalds {
7731da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7741da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
775a07530b4SDave Jones 	ssize_t ret = -EINVAL;
7766eed9404SViresh Kumar 
7774f750c93SSrivatsa S. Bhat 	get_online_cpus();
7784f750c93SSrivatsa S. Bhat 
7794f750c93SSrivatsa S. Bhat 	if (!cpu_online(policy->cpu))
7804f750c93SSrivatsa S. Bhat 		goto unlock;
7814f750c93SSrivatsa S. Bhat 
7826eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
7834f750c93SSrivatsa S. Bhat 		goto unlock;
7845a01f2e8SVenkatesh Pallipadi 
785ad7722daSviresh kumar 	down_write(&policy->rwsem);
7865a01f2e8SVenkatesh Pallipadi 
787e08f5f5bSGautham R Shenoy 	if (fattr->store)
788e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
789e08f5f5bSGautham R Shenoy 	else
790e08f5f5bSGautham R Shenoy 		ret = -EIO;
791e08f5f5bSGautham R Shenoy 
792ad7722daSviresh kumar 	up_write(&policy->rwsem);
7936eed9404SViresh Kumar 
7946eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
7954f750c93SSrivatsa S. Bhat unlock:
7964f750c93SSrivatsa S. Bhat 	put_online_cpus();
7974f750c93SSrivatsa S. Bhat 
7981da177e4SLinus Torvalds 	return ret;
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
8021da177e4SLinus Torvalds {
8031da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
8042d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
8051da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds 
80852cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
8091da177e4SLinus Torvalds 	.show	= show,
8101da177e4SLinus Torvalds 	.store	= store,
8111da177e4SLinus Torvalds };
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
8141da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
8151da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
8161da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
8171da177e4SLinus Torvalds };
8181da177e4SLinus Torvalds 
8192361be23SViresh Kumar struct kobject *cpufreq_global_kobject;
8202361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject);
8212361be23SViresh Kumar 
8222361be23SViresh Kumar static int cpufreq_global_kobject_usage;
8232361be23SViresh Kumar 
8242361be23SViresh Kumar int cpufreq_get_global_kobject(void)
8252361be23SViresh Kumar {
8262361be23SViresh Kumar 	if (!cpufreq_global_kobject_usage++)
8272361be23SViresh Kumar 		return kobject_add(cpufreq_global_kobject,
8282361be23SViresh Kumar 				&cpu_subsys.dev_root->kobj, "%s", "cpufreq");
8292361be23SViresh Kumar 
8302361be23SViresh Kumar 	return 0;
8312361be23SViresh Kumar }
8322361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject);
8332361be23SViresh Kumar 
8342361be23SViresh Kumar void cpufreq_put_global_kobject(void)
8352361be23SViresh Kumar {
8362361be23SViresh Kumar 	if (!--cpufreq_global_kobject_usage)
8372361be23SViresh Kumar 		kobject_del(cpufreq_global_kobject);
8382361be23SViresh Kumar }
8392361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject);
8402361be23SViresh Kumar 
8412361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr)
8422361be23SViresh Kumar {
8432361be23SViresh Kumar 	int ret = cpufreq_get_global_kobject();
8442361be23SViresh Kumar 
8452361be23SViresh Kumar 	if (!ret) {
8462361be23SViresh Kumar 		ret = sysfs_create_file(cpufreq_global_kobject, attr);
8472361be23SViresh Kumar 		if (ret)
8482361be23SViresh Kumar 			cpufreq_put_global_kobject();
8492361be23SViresh Kumar 	}
8502361be23SViresh Kumar 
8512361be23SViresh Kumar 	return ret;
8522361be23SViresh Kumar }
8532361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file);
8542361be23SViresh Kumar 
8552361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr)
8562361be23SViresh Kumar {
8572361be23SViresh Kumar 	sysfs_remove_file(cpufreq_global_kobject, attr);
8582361be23SViresh Kumar 	cpufreq_put_global_kobject();
8592361be23SViresh Kumar }
8602361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
8612361be23SViresh Kumar 
86219d6f7ecSDave Jones /* symlink affected CPUs */
863308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
86419d6f7ecSDave Jones {
86519d6f7ecSDave Jones 	unsigned int j;
86619d6f7ecSDave Jones 	int ret = 0;
86719d6f7ecSDave Jones 
86819d6f7ecSDave Jones 	for_each_cpu(j, policy->cpus) {
8698a25a2fdSKay Sievers 		struct device *cpu_dev;
87019d6f7ecSDave Jones 
871308b60e7SViresh Kumar 		if (j == policy->cpu)
87219d6f7ecSDave Jones 			continue;
87319d6f7ecSDave Jones 
874e8fdde10SViresh Kumar 		pr_debug("Adding link for CPU: %u\n", j);
8758a25a2fdSKay Sievers 		cpu_dev = get_cpu_device(j);
8768a25a2fdSKay Sievers 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
87719d6f7ecSDave Jones 					"cpufreq");
87871c3461eSRafael J. Wysocki 		if (ret)
87971c3461eSRafael J. Wysocki 			break;
88019d6f7ecSDave Jones 	}
88119d6f7ecSDave Jones 	return ret;
88219d6f7ecSDave Jones }
88319d6f7ecSDave Jones 
884308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
8858a25a2fdSKay Sievers 				     struct device *dev)
886909a694eSDave Jones {
887909a694eSDave Jones 	struct freq_attr **drv_attr;
888909a694eSDave Jones 	int ret = 0;
889909a694eSDave Jones 
890909a694eSDave Jones 	/* prepare interface data */
891909a694eSDave Jones 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
8928a25a2fdSKay Sievers 				   &dev->kobj, "cpufreq");
893909a694eSDave Jones 	if (ret)
894909a694eSDave Jones 		return ret;
895909a694eSDave Jones 
896909a694eSDave Jones 	/* set up files for this cpu device */
8971c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
898909a694eSDave Jones 	while ((drv_attr) && (*drv_attr)) {
899909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
900909a694eSDave Jones 		if (ret)
9011c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
902909a694eSDave Jones 		drv_attr++;
903909a694eSDave Jones 	}
9041c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
905909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
906909a694eSDave Jones 		if (ret)
9071c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
908909a694eSDave Jones 	}
9099c0ebcf7SViresh Kumar 	if (has_target()) {
910909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
911909a694eSDave Jones 		if (ret)
9121c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
913909a694eSDave Jones 	}
9141c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
915e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
916e2f74f35SThomas Renninger 		if (ret)
9171c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
918e2f74f35SThomas Renninger 	}
919909a694eSDave Jones 
920308b60e7SViresh Kumar 	ret = cpufreq_add_dev_symlink(policy);
921ecf7e461SDave Jones 	if (ret)
922ecf7e461SDave Jones 		goto err_out_kobj_put;
923ecf7e461SDave Jones 
924e18f1682SSrivatsa S. Bhat 	return ret;
925e18f1682SSrivatsa S. Bhat 
926e18f1682SSrivatsa S. Bhat err_out_kobj_put:
927e18f1682SSrivatsa S. Bhat 	kobject_put(&policy->kobj);
928e18f1682SSrivatsa S. Bhat 	wait_for_completion(&policy->kobj_unregister);
929e18f1682SSrivatsa S. Bhat 	return ret;
930e18f1682SSrivatsa S. Bhat }
931e18f1682SSrivatsa S. Bhat 
932e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy)
933e18f1682SSrivatsa S. Bhat {
9346e2c89d1Sviresh kumar 	struct cpufreq_governor *gov = NULL;
935e18f1682SSrivatsa S. Bhat 	struct cpufreq_policy new_policy;
936e18f1682SSrivatsa S. Bhat 	int ret = 0;
937e18f1682SSrivatsa S. Bhat 
938d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
939a27a9ab7SJason Baron 
9406e2c89d1Sviresh kumar 	/* Update governor of new_policy to the governor used before hotplug */
9416e2c89d1Sviresh kumar 	gov = __find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu));
9426e2c89d1Sviresh kumar 	if (gov)
9436e2c89d1Sviresh kumar 		pr_debug("Restoring governor %s for cpu %d\n",
9446e2c89d1Sviresh kumar 				policy->governor->name, policy->cpu);
9456e2c89d1Sviresh kumar 	else
9466e2c89d1Sviresh kumar 		gov = CPUFREQ_DEFAULT_GOVERNOR;
9476e2c89d1Sviresh kumar 
9486e2c89d1Sviresh kumar 	new_policy.governor = gov;
9496e2c89d1Sviresh kumar 
950a27a9ab7SJason Baron 	/* Use the default policy if its valid. */
951a27a9ab7SJason Baron 	if (cpufreq_driver->setpolicy)
9526e2c89d1Sviresh kumar 		cpufreq_parse_governor(gov->name, &new_policy.policy, NULL);
953ecf7e461SDave Jones 
954ecf7e461SDave Jones 	/* set default policy */
955037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
956ecf7e461SDave Jones 	if (ret) {
9572d06d8c4SDominik Brodowski 		pr_debug("setting policy failed\n");
9581c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
9591c3d85ddSRafael J. Wysocki 			cpufreq_driver->exit(policy);
960ecf7e461SDave Jones 	}
961909a694eSDave Jones }
962909a694eSDave Jones 
963fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
964d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
96542f921a6SViresh Kumar 				  unsigned int cpu, struct device *dev)
966fcf80582SViresh Kumar {
9679c0ebcf7SViresh Kumar 	int ret = 0;
968fcf80582SViresh Kumar 	unsigned long flags;
969fcf80582SViresh Kumar 
9709c0ebcf7SViresh Kumar 	if (has_target()) {
9713de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
9723de9bdebSViresh Kumar 		if (ret) {
9733de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
9743de9bdebSViresh Kumar 			return ret;
9753de9bdebSViresh Kumar 		}
9763de9bdebSViresh Kumar 	}
977fcf80582SViresh Kumar 
978ad7722daSviresh kumar 	down_write(&policy->rwsem);
9792eaa3e2dSViresh Kumar 
9800d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
9812eaa3e2dSViresh Kumar 
982fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
983fcf80582SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = policy;
9840d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
985fcf80582SViresh Kumar 
986ad7722daSviresh kumar 	up_write(&policy->rwsem);
9872eaa3e2dSViresh Kumar 
9889c0ebcf7SViresh Kumar 	if (has_target()) {
989e5c87b76SStratos Karafotis 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
990e5c87b76SStratos Karafotis 		if (!ret)
991e5c87b76SStratos Karafotis 			ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
992e5c87b76SStratos Karafotis 
993e5c87b76SStratos Karafotis 		if (ret) {
9943de9bdebSViresh Kumar 			pr_err("%s: Failed to start governor\n", __func__);
9953de9bdebSViresh Kumar 			return ret;
9963de9bdebSViresh Kumar 		}
997820c6ca2SViresh Kumar 	}
998fcf80582SViresh Kumar 
99942f921a6SViresh Kumar 	return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
1000fcf80582SViresh Kumar }
1001fcf80582SViresh Kumar #endif
10021da177e4SLinus Torvalds 
10038414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
10048414809cSSrivatsa S. Bhat {
10058414809cSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
10068414809cSSrivatsa S. Bhat 	unsigned long flags;
10078414809cSSrivatsa S. Bhat 
100844871c9cSLan Tianyu 	read_lock_irqsave(&cpufreq_driver_lock, flags);
10098414809cSSrivatsa S. Bhat 
10108414809cSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
10118414809cSSrivatsa S. Bhat 
101244871c9cSLan Tianyu 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
10138414809cSSrivatsa S. Bhat 
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 
10790d66b91eSSrivatsa S. Bhat static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
10800d66b91eSSrivatsa S. Bhat {
108199ec899eSSrivatsa S. Bhat 	if (WARN_ON(cpu == policy->cpu))
1082cb38ed5cSSrivatsa S. Bhat 		return;
1083cb38ed5cSSrivatsa S. Bhat 
1084ad7722daSviresh kumar 	down_write(&policy->rwsem);
10858efd5765SViresh Kumar 
10860d66b91eSSrivatsa S. Bhat 	policy->last_cpu = policy->cpu;
10870d66b91eSSrivatsa S. Bhat 	policy->cpu = cpu;
10880d66b91eSSrivatsa S. Bhat 
1089ad7722daSviresh kumar 	up_write(&policy->rwsem);
10908efd5765SViresh Kumar 
10910d66b91eSSrivatsa S. Bhat 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
10920d66b91eSSrivatsa S. Bhat 			CPUFREQ_UPDATE_POLICY_CPU, policy);
10930d66b91eSSrivatsa S. Bhat }
10940d66b91eSSrivatsa S. Bhat 
109596bbbe4aSViresh Kumar static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
10961da177e4SLinus Torvalds {
1097fcf80582SViresh Kumar 	unsigned int j, cpu = dev->id;
109865922465SViresh Kumar 	int ret = -ENOMEM;
10991da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
11001da177e4SLinus Torvalds 	unsigned long flags;
110196bbbe4aSViresh Kumar 	bool recover_policy = cpufreq_suspended;
110290e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
11031b274294SViresh Kumar 	struct cpufreq_policy *tpolicy;
110490e41bacSPrarit Bhargava #endif
11051da177e4SLinus Torvalds 
1106c32b6b8eSAshok Raj 	if (cpu_is_offline(cpu))
1107c32b6b8eSAshok Raj 		return 0;
1108c32b6b8eSAshok Raj 
11092d06d8c4SDominik Brodowski 	pr_debug("adding CPU %u\n", cpu);
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds #ifdef CONFIG_SMP
11121da177e4SLinus Torvalds 	/* check whether a different CPU already registered this
11131da177e4SLinus Torvalds 	 * CPU because it is in the same boat. */
11141da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(cpu);
11151da177e4SLinus Torvalds 	if (unlikely(policy)) {
11168ff69732SDave Jones 		cpufreq_cpu_put(policy);
11171da177e4SLinus Torvalds 		return 0;
11181da177e4SLinus Torvalds 	}
11195025d628SLi Zhong #endif
1120fcf80582SViresh Kumar 
11216eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
11226eed9404SViresh Kumar 		return 0;
11236eed9404SViresh Kumar 
1124fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
1125fcf80582SViresh Kumar 	/* Check if this cpu was hot-unplugged earlier and has siblings */
11260d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
11271b274294SViresh Kumar 	list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
11281b274294SViresh Kumar 		if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
11290d1857a1SNathan Zimmer 			read_unlock_irqrestore(&cpufreq_driver_lock, flags);
113042f921a6SViresh Kumar 			ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
11316eed9404SViresh Kumar 			up_read(&cpufreq_rwsem);
11326eed9404SViresh Kumar 			return ret;
1133fcf80582SViresh Kumar 		}
11342eaa3e2dSViresh Kumar 	}
11350d1857a1SNathan Zimmer 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1136fcf80582SViresh Kumar #endif
11371da177e4SLinus Torvalds 
113872368d12SRafael J. Wysocki 	/*
113972368d12SRafael J. Wysocki 	 * Restore the saved policy when doing light-weight init and fall back
114072368d12SRafael J. Wysocki 	 * to the full init if that fails.
114172368d12SRafael J. Wysocki 	 */
114296bbbe4aSViresh Kumar 	policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL;
114372368d12SRafael J. Wysocki 	if (!policy) {
114496bbbe4aSViresh Kumar 		recover_policy = false;
1145e9698cc5SSrivatsa S. Bhat 		policy = cpufreq_policy_alloc();
1146059019a3SDave Jones 		if (!policy)
11471da177e4SLinus Torvalds 			goto nomem_out;
114872368d12SRafael J. Wysocki 	}
11490d66b91eSSrivatsa S. Bhat 
11500d66b91eSSrivatsa S. Bhat 	/*
11510d66b91eSSrivatsa S. Bhat 	 * In the resume path, since we restore a saved policy, the assignment
11520d66b91eSSrivatsa S. Bhat 	 * to policy->cpu is like an update of the existing policy, rather than
11530d66b91eSSrivatsa S. Bhat 	 * the creation of a brand new one. So we need to perform this update
11540d66b91eSSrivatsa S. Bhat 	 * by invoking update_policy_cpu().
11550d66b91eSSrivatsa S. Bhat 	 */
115696bbbe4aSViresh Kumar 	if (recover_policy && cpu != policy->cpu)
11570d66b91eSSrivatsa S. Bhat 		update_policy_cpu(policy, cpu);
11580d66b91eSSrivatsa S. Bhat 	else
11591da177e4SLinus Torvalds 		policy->cpu = cpu;
11600d66b91eSSrivatsa S. Bhat 
1161835481d9SRusty Russell 	cpumask_copy(policy->cpus, cpumask_of(cpu));
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	init_completion(&policy->kobj_unregister);
116465f27f38SDavid Howells 	INIT_WORK(&policy->update, handle_update);
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
11671da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
11681da177e4SLinus Torvalds 	 */
11691c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->init(policy);
11701da177e4SLinus Torvalds 	if (ret) {
11712d06d8c4SDominik Brodowski 		pr_debug("initialization failed\n");
11722eaa3e2dSViresh Kumar 		goto err_set_policy_cpu;
11731da177e4SLinus Torvalds 	}
1174643ae6e8SViresh Kumar 
11755a7e56a5SViresh Kumar 	/* related cpus should atleast have policy->cpus */
11765a7e56a5SViresh Kumar 	cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
11775a7e56a5SViresh Kumar 
11785a7e56a5SViresh Kumar 	/*
11795a7e56a5SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
11805a7e56a5SViresh Kumar 	 * managing offline cpus here.
11815a7e56a5SViresh Kumar 	 */
11825a7e56a5SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
11835a7e56a5SViresh Kumar 
118496bbbe4aSViresh Kumar 	if (!recover_policy) {
11855a7e56a5SViresh Kumar 		policy->user_policy.min = policy->min;
11865a7e56a5SViresh Kumar 		policy->user_policy.max = policy->max;
11875a7e56a5SViresh Kumar 	}
11885a7e56a5SViresh Kumar 
11894e97b631SViresh Kumar 	down_write(&policy->rwsem);
1190652ed95dSViresh Kumar 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1191652ed95dSViresh Kumar 	for_each_cpu(j, policy->cpus)
1192652ed95dSViresh Kumar 		per_cpu(cpufreq_cpu_data, j) = policy;
1193652ed95dSViresh Kumar 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1194652ed95dSViresh Kumar 
11952ed99e39SRafael J. Wysocki 	if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1196da60ce9fSViresh Kumar 		policy->cur = cpufreq_driver->get(policy->cpu);
1197da60ce9fSViresh Kumar 		if (!policy->cur) {
1198da60ce9fSViresh Kumar 			pr_err("%s: ->get() failed\n", __func__);
1199da60ce9fSViresh Kumar 			goto err_get_freq;
1200da60ce9fSViresh Kumar 		}
1201da60ce9fSViresh Kumar 	}
1202da60ce9fSViresh Kumar 
1203d3916691SViresh Kumar 	/*
1204d3916691SViresh Kumar 	 * Sometimes boot loaders set CPU frequency to a value outside of
1205d3916691SViresh Kumar 	 * frequency table present with cpufreq core. In such cases CPU might be
1206d3916691SViresh Kumar 	 * unstable if it has to run on that frequency for long duration of time
1207d3916691SViresh Kumar 	 * and so its better to set it to a frequency which is specified in
1208d3916691SViresh Kumar 	 * freq-table. This also makes cpufreq stats inconsistent as
1209d3916691SViresh Kumar 	 * cpufreq-stats would fail to register because current frequency of CPU
1210d3916691SViresh Kumar 	 * isn't found in freq-table.
1211d3916691SViresh Kumar 	 *
1212d3916691SViresh Kumar 	 * Because we don't want this change to effect boot process badly, we go
1213d3916691SViresh Kumar 	 * for the next freq which is >= policy->cur ('cur' must be set by now,
1214d3916691SViresh Kumar 	 * otherwise we will end up setting freq to lowest of the table as 'cur'
1215d3916691SViresh Kumar 	 * is initialized to zero).
1216d3916691SViresh Kumar 	 *
1217d3916691SViresh Kumar 	 * We are passing target-freq as "policy->cur - 1" otherwise
1218d3916691SViresh Kumar 	 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1219d3916691SViresh Kumar 	 * equal to target-freq.
1220d3916691SViresh Kumar 	 */
1221d3916691SViresh Kumar 	if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1222d3916691SViresh Kumar 	    && has_target()) {
1223d3916691SViresh Kumar 		/* Are we running at unknown frequency ? */
1224d3916691SViresh Kumar 		ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1225d3916691SViresh Kumar 		if (ret == -EINVAL) {
1226d3916691SViresh Kumar 			/* Warn user and fix it */
1227d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1228d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1229d3916691SViresh Kumar 			ret = __cpufreq_driver_target(policy, policy->cur - 1,
1230d3916691SViresh Kumar 				CPUFREQ_RELATION_L);
1231d3916691SViresh Kumar 
1232d3916691SViresh Kumar 			/*
1233d3916691SViresh Kumar 			 * Reaching here after boot in a few seconds may not
1234d3916691SViresh Kumar 			 * mean that system will remain stable at "unknown"
1235d3916691SViresh Kumar 			 * frequency for longer duration. Hence, a BUG_ON().
1236d3916691SViresh Kumar 			 */
1237d3916691SViresh Kumar 			BUG_ON(ret);
1238d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1239d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1240d3916691SViresh Kumar 		}
1241d3916691SViresh Kumar 	}
1242d3916691SViresh Kumar 
1243a1531acdSThomas Renninger 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1244a1531acdSThomas Renninger 				     CPUFREQ_START, policy);
1245a1531acdSThomas Renninger 
124696bbbe4aSViresh Kumar 	if (!recover_policy) {
1247308b60e7SViresh Kumar 		ret = cpufreq_add_dev_interface(policy, dev);
124819d6f7ecSDave Jones 		if (ret)
12490142f9dcSAhmed S. Darwish 			goto err_out_unregister;
1250fcd7af91SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1251fcd7af91SViresh Kumar 				CPUFREQ_CREATE_POLICY, policy);
12529515f4d6SViresh Kumar 	}
1253c88a1f8bSLukasz Majewski 
1254c88a1f8bSLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1255c88a1f8bSLukasz Majewski 	list_add(&policy->policy_list, &cpufreq_policy_list);
1256c88a1f8bSLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
12578ff69732SDave Jones 
1258e18f1682SSrivatsa S. Bhat 	cpufreq_init_policy(policy);
1259e18f1682SSrivatsa S. Bhat 
126096bbbe4aSViresh Kumar 	if (!recover_policy) {
126108fd8c1cSViresh Kumar 		policy->user_policy.policy = policy->policy;
126208fd8c1cSViresh Kumar 		policy->user_policy.governor = policy->governor;
126308fd8c1cSViresh Kumar 	}
12644e97b631SViresh Kumar 	up_write(&policy->rwsem);
126508fd8c1cSViresh Kumar 
1266038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
12676eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
12686eed9404SViresh Kumar 
12692d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 	return 0;
12721da177e4SLinus Torvalds 
12731da177e4SLinus Torvalds err_out_unregister:
1274652ed95dSViresh Kumar err_get_freq:
12750d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1276474deff7SViresh Kumar 	for_each_cpu(j, policy->cpus)
12777a6aedfaSMike Travis 		per_cpu(cpufreq_cpu_data, j) = NULL;
12780d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
12791da177e4SLinus Torvalds 
1280da60ce9fSViresh Kumar 	if (cpufreq_driver->exit)
1281da60ce9fSViresh Kumar 		cpufreq_driver->exit(policy);
12822eaa3e2dSViresh Kumar err_set_policy_cpu:
128396bbbe4aSViresh Kumar 	if (recover_policy) {
128472368d12SRafael J. Wysocki 		/* Do not leave stale fallback data behind. */
128572368d12SRafael J. Wysocki 		per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
128642f921a6SViresh Kumar 		cpufreq_policy_put_kobj(policy);
128772368d12SRafael J. Wysocki 	}
1288e9698cc5SSrivatsa S. Bhat 	cpufreq_policy_free(policy);
128942f921a6SViresh Kumar 
12901da177e4SLinus Torvalds nomem_out:
12916eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
12926eed9404SViresh Kumar 
12931da177e4SLinus Torvalds 	return ret;
12941da177e4SLinus Torvalds }
12951da177e4SLinus Torvalds 
1296a82fab29SSrivatsa S. Bhat /**
1297a82fab29SSrivatsa S. Bhat  * cpufreq_add_dev - add a CPU device
1298a82fab29SSrivatsa S. Bhat  *
1299a82fab29SSrivatsa S. Bhat  * Adds the cpufreq interface for a CPU device.
1300a82fab29SSrivatsa S. Bhat  *
1301a82fab29SSrivatsa S. Bhat  * The Oracle says: try running cpufreq registration/unregistration concurrently
1302a82fab29SSrivatsa S. Bhat  * with with cpu hotplugging and all hell will break loose. Tried to clean this
1303a82fab29SSrivatsa S. Bhat  * mess up, but more thorough testing is needed. - Mathieu
1304a82fab29SSrivatsa S. Bhat  */
1305a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1306a82fab29SSrivatsa S. Bhat {
130796bbbe4aSViresh Kumar 	return __cpufreq_add_dev(dev, sif);
1308a82fab29SSrivatsa S. Bhat }
1309a82fab29SSrivatsa S. Bhat 
13103a3e9e06SViresh Kumar static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
131142f921a6SViresh Kumar 					   unsigned int old_cpu)
1312f9ba680dSSrivatsa S. Bhat {
1313f9ba680dSSrivatsa S. Bhat 	struct device *cpu_dev;
1314f9ba680dSSrivatsa S. Bhat 	int ret;
1315f9ba680dSSrivatsa S. Bhat 
1316f9ba680dSSrivatsa S. Bhat 	/* first sibling now owns the new sysfs dir */
13179c8f1ee4SViresh Kumar 	cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
1318a82fab29SSrivatsa S. Bhat 
1319f9ba680dSSrivatsa S. Bhat 	sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
13203a3e9e06SViresh Kumar 	ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1321f9ba680dSSrivatsa S. Bhat 	if (ret) {
1322e837f9b5SJoe Perches 		pr_err("%s: Failed to move kobj: %d\n", __func__, ret);
1323f9ba680dSSrivatsa S. Bhat 
1324ad7722daSviresh kumar 		down_write(&policy->rwsem);
13253a3e9e06SViresh Kumar 		cpumask_set_cpu(old_cpu, policy->cpus);
1326ad7722daSviresh kumar 		up_write(&policy->rwsem);
1327f9ba680dSSrivatsa S. Bhat 
13283a3e9e06SViresh Kumar 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1329f9ba680dSSrivatsa S. Bhat 					"cpufreq");
1330f9ba680dSSrivatsa S. Bhat 
1331f9ba680dSSrivatsa S. Bhat 		return -EINVAL;
1332f9ba680dSSrivatsa S. Bhat 	}
1333f9ba680dSSrivatsa S. Bhat 
1334f9ba680dSSrivatsa S. Bhat 	return cpu_dev->id;
1335f9ba680dSSrivatsa S. Bhat }
1336f9ba680dSSrivatsa S. Bhat 
1337cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_prepare(struct device *dev,
133896bbbe4aSViresh Kumar 					struct subsys_interface *sif)
13391da177e4SLinus Torvalds {
1340f9ba680dSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
13413de9bdebSViresh Kumar 	int new_cpu, ret;
13421da177e4SLinus Torvalds 	unsigned long flags;
13433a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
13441da177e4SLinus Torvalds 
1345b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
13461da177e4SLinus Torvalds 
13470d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
13481da177e4SLinus Torvalds 
13493a3e9e06SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
13501da177e4SLinus Torvalds 
13518414809cSSrivatsa S. Bhat 	/* Save the policy somewhere when doing a light-weight tear-down */
135296bbbe4aSViresh Kumar 	if (cpufreq_suspended)
13533a3e9e06SViresh Kumar 		per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
13548414809cSSrivatsa S. Bhat 
13550d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
13561da177e4SLinus Torvalds 
13573a3e9e06SViresh Kumar 	if (!policy) {
1358b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
13591da177e4SLinus Torvalds 		return -EINVAL;
13601da177e4SLinus Torvalds 	}
13611da177e4SLinus Torvalds 
13629c0ebcf7SViresh Kumar 	if (has_target()) {
13633de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
13643de9bdebSViresh Kumar 		if (ret) {
13653de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
13663de9bdebSViresh Kumar 			return ret;
13673de9bdebSViresh Kumar 		}
13683de9bdebSViresh Kumar 	}
13695a01f2e8SVenkatesh Pallipadi 
13701c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->setpolicy)
1371fa69e33fSDirk Brandewie 		strncpy(per_cpu(cpufreq_cpu_governor, cpu),
13723a3e9e06SViresh Kumar 			policy->governor->name, CPUFREQ_NAME_LEN);
13731da177e4SLinus Torvalds 
1374ad7722daSviresh kumar 	down_read(&policy->rwsem);
13753a3e9e06SViresh Kumar 	cpus = cpumask_weight(policy->cpus);
1376ad7722daSviresh kumar 	up_read(&policy->rwsem);
13771da177e4SLinus Torvalds 
137861173f25SSrivatsa S. Bhat 	if (cpu != policy->cpu) {
137973bf0fc2SViresh Kumar 		sysfs_remove_link(&dev->kobj, "cpufreq");
138073bf0fc2SViresh Kumar 	} else if (cpus > 1) {
138142f921a6SViresh Kumar 		new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
1382f9ba680dSSrivatsa S. Bhat 		if (new_cpu >= 0) {
13833a3e9e06SViresh Kumar 			update_policy_cpu(policy, new_cpu);
1384a82fab29SSrivatsa S. Bhat 
1385bda9f552SStratos Karafotis 			if (!cpufreq_suspended)
138675949c9aSViresh Kumar 				pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
138775949c9aSViresh Kumar 					 __func__, new_cpu, cpu);
13881da177e4SLinus Torvalds 		}
1389367dc4aaSDirk Brandewie 	} else if (cpufreq_driver->stop_cpu && cpufreq_driver->setpolicy) {
1390367dc4aaSDirk Brandewie 		cpufreq_driver->stop_cpu(policy);
13911da177e4SLinus Torvalds 	}
1392b8eed8afSViresh Kumar 
1393cedb70afSSrivatsa S. Bhat 	return 0;
1394cedb70afSSrivatsa S. Bhat }
1395cedb70afSSrivatsa S. Bhat 
1396cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_finish(struct device *dev,
139796bbbe4aSViresh Kumar 				       struct subsys_interface *sif)
1398cedb70afSSrivatsa S. Bhat {
1399cedb70afSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
1400cedb70afSSrivatsa S. Bhat 	int ret;
1401cedb70afSSrivatsa S. Bhat 	unsigned long flags;
1402cedb70afSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
1403cedb70afSSrivatsa S. Bhat 
1404cedb70afSSrivatsa S. Bhat 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1405cedb70afSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data, cpu);
1406cedb70afSSrivatsa S. Bhat 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1407cedb70afSSrivatsa S. Bhat 
1408cedb70afSSrivatsa S. Bhat 	if (!policy) {
1409cedb70afSSrivatsa S. Bhat 		pr_debug("%s: No cpu_data found\n", __func__);
1410cedb70afSSrivatsa S. Bhat 		return -EINVAL;
1411cedb70afSSrivatsa S. Bhat 	}
1412cedb70afSSrivatsa S. Bhat 
1413ad7722daSviresh kumar 	down_write(&policy->rwsem);
1414cedb70afSSrivatsa S. Bhat 	cpus = cpumask_weight(policy->cpus);
14159c8f1ee4SViresh Kumar 
14169c8f1ee4SViresh Kumar 	if (cpus > 1)
14179c8f1ee4SViresh Kumar 		cpumask_clear_cpu(cpu, policy->cpus);
1418ad7722daSviresh kumar 	up_write(&policy->rwsem);
1419cedb70afSSrivatsa S. Bhat 
1420b8eed8afSViresh Kumar 	/* If cpu is last user of policy, free policy */
1421b8eed8afSViresh Kumar 	if (cpus == 1) {
14229c0ebcf7SViresh Kumar 		if (has_target()) {
14233de9bdebSViresh Kumar 			ret = __cpufreq_governor(policy,
14243de9bdebSViresh Kumar 					CPUFREQ_GOV_POLICY_EXIT);
14253de9bdebSViresh Kumar 			if (ret) {
14263de9bdebSViresh Kumar 				pr_err("%s: Failed to exit governor\n",
14273de9bdebSViresh Kumar 				       __func__);
14283de9bdebSViresh Kumar 				return ret;
14293de9bdebSViresh Kumar 			}
14303de9bdebSViresh Kumar 		}
14312a998599SRafael J. Wysocki 
143296bbbe4aSViresh Kumar 		if (!cpufreq_suspended)
143342f921a6SViresh Kumar 			cpufreq_policy_put_kobj(policy);
14341da177e4SLinus Torvalds 
14358414809cSSrivatsa S. Bhat 		/*
14368414809cSSrivatsa S. Bhat 		 * Perform the ->exit() even during light-weight tear-down,
14378414809cSSrivatsa S. Bhat 		 * since this is a core component, and is essential for the
14388414809cSSrivatsa S. Bhat 		 * subsequent light-weight ->init() to succeed.
14398414809cSSrivatsa S. Bhat 		 */
14401c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
14413a3e9e06SViresh Kumar 			cpufreq_driver->exit(policy);
144227ecddc2SJacob Shin 
14439515f4d6SViresh Kumar 		/* Remove policy from list of active policies */
14449515f4d6SViresh Kumar 		write_lock_irqsave(&cpufreq_driver_lock, flags);
14459515f4d6SViresh Kumar 		list_del(&policy->policy_list);
14469515f4d6SViresh Kumar 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
14479515f4d6SViresh Kumar 
144896bbbe4aSViresh Kumar 		if (!cpufreq_suspended)
14493a3e9e06SViresh Kumar 			cpufreq_policy_free(policy);
1450e5c87b76SStratos Karafotis 	} else if (has_target()) {
1451e5c87b76SStratos Karafotis 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1452e5c87b76SStratos Karafotis 		if (!ret)
1453e5c87b76SStratos Karafotis 			ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1454e5c87b76SStratos Karafotis 
1455e5c87b76SStratos Karafotis 		if (ret) {
1456e5c87b76SStratos Karafotis 			pr_err("%s: Failed to start governor\n", __func__);
14573de9bdebSViresh Kumar 			return ret;
14583de9bdebSViresh Kumar 		}
1459b8eed8afSViresh Kumar 	}
14601da177e4SLinus Torvalds 
1461474deff7SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = NULL;
14621da177e4SLinus Torvalds 	return 0;
14631da177e4SLinus Torvalds }
14641da177e4SLinus Torvalds 
1465cedb70afSSrivatsa S. Bhat /**
146627a862e9SViresh Kumar  * cpufreq_remove_dev - remove a CPU device
1467cedb70afSSrivatsa S. Bhat  *
1468cedb70afSSrivatsa S. Bhat  * Removes the cpufreq interface for a CPU device.
1469cedb70afSSrivatsa S. Bhat  */
14708a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
14715a01f2e8SVenkatesh Pallipadi {
14728a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
147327a862e9SViresh Kumar 	int ret;
1474ec28297aSVenki Pallipadi 
1475ec28297aSVenki Pallipadi 	if (cpu_is_offline(cpu))
1476ec28297aSVenki Pallipadi 		return 0;
1477ec28297aSVenki Pallipadi 
147896bbbe4aSViresh Kumar 	ret = __cpufreq_remove_dev_prepare(dev, sif);
147927a862e9SViresh Kumar 
148027a862e9SViresh Kumar 	if (!ret)
148196bbbe4aSViresh Kumar 		ret = __cpufreq_remove_dev_finish(dev, sif);
148227a862e9SViresh Kumar 
148327a862e9SViresh Kumar 	return ret;
14845a01f2e8SVenkatesh Pallipadi }
14855a01f2e8SVenkatesh Pallipadi 
148665f27f38SDavid Howells static void handle_update(struct work_struct *work)
14871da177e4SLinus Torvalds {
148865f27f38SDavid Howells 	struct cpufreq_policy *policy =
148965f27f38SDavid Howells 		container_of(work, struct cpufreq_policy, update);
149065f27f38SDavid Howells 	unsigned int cpu = policy->cpu;
14912d06d8c4SDominik Brodowski 	pr_debug("handle_update for cpu %u called\n", cpu);
14921da177e4SLinus Torvalds 	cpufreq_update_policy(cpu);
14931da177e4SLinus Torvalds }
14941da177e4SLinus Torvalds 
14951da177e4SLinus Torvalds /**
1496bb176f7dSViresh Kumar  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1497bb176f7dSViresh Kumar  *	in deep trouble.
14981da177e4SLinus Torvalds  *	@cpu: cpu number
14991da177e4SLinus Torvalds  *	@old_freq: CPU frequency the kernel thinks the CPU runs at
15001da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
15011da177e4SLinus Torvalds  *
150229464f28SDave Jones  *	We adjust to current frequency first, and need to clean up later.
150329464f28SDave Jones  *	So either call to cpufreq_update_policy() or schedule handle_update()).
15041da177e4SLinus Torvalds  */
1505e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1506e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
15071da177e4SLinus Torvalds {
1508b43a7ffbSViresh Kumar 	struct cpufreq_policy *policy;
15091da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1510b43a7ffbSViresh Kumar 	unsigned long flags;
1511b43a7ffbSViresh Kumar 
1512e837f9b5SJoe Perches 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1513e837f9b5SJoe Perches 		 old_freq, new_freq);
15141da177e4SLinus Torvalds 
15151da177e4SLinus Torvalds 	freqs.old = old_freq;
15161da177e4SLinus Torvalds 	freqs.new = new_freq;
1517b43a7ffbSViresh Kumar 
1518b43a7ffbSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1519b43a7ffbSViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
1520b43a7ffbSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1521b43a7ffbSViresh Kumar 
15228fec051eSViresh Kumar 	cpufreq_freq_transition_begin(policy, &freqs);
15238fec051eSViresh Kumar 	cpufreq_freq_transition_end(policy, &freqs, 0);
15241da177e4SLinus Torvalds }
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds /**
15274ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
152895235ca2SVenkatesh Pallipadi  * @cpu: CPU number
152995235ca2SVenkatesh Pallipadi  *
153095235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
153195235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
153295235ca2SVenkatesh Pallipadi  */
153395235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
153495235ca2SVenkatesh Pallipadi {
15359e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1536e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
153795235ca2SVenkatesh Pallipadi 
15381c3d85ddSRafael J. Wysocki 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
15391c3d85ddSRafael J. Wysocki 		return cpufreq_driver->get(cpu);
15409e21ba8bSDirk Brandewie 
15419e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
154295235ca2SVenkatesh Pallipadi 	if (policy) {
1543e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
154495235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
154595235ca2SVenkatesh Pallipadi 	}
154695235ca2SVenkatesh Pallipadi 
15474d34a67dSDave Jones 	return ret_freq;
154895235ca2SVenkatesh Pallipadi }
154995235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
155095235ca2SVenkatesh Pallipadi 
15513d737108SJesse Barnes /**
15523d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
15533d737108SJesse Barnes  * @cpu: CPU number
15543d737108SJesse Barnes  *
15553d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
15563d737108SJesse Barnes  */
15573d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
15583d737108SJesse Barnes {
15593d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
15603d737108SJesse Barnes 	unsigned int ret_freq = 0;
15613d737108SJesse Barnes 
15623d737108SJesse Barnes 	if (policy) {
15633d737108SJesse Barnes 		ret_freq = policy->max;
15643d737108SJesse Barnes 		cpufreq_cpu_put(policy);
15653d737108SJesse Barnes 	}
15663d737108SJesse Barnes 
15673d737108SJesse Barnes 	return ret_freq;
15683d737108SJesse Barnes }
15693d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
15703d737108SJesse Barnes 
15715a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu)
15721da177e4SLinus Torvalds {
15737a6aedfaSMike Travis 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1574e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
15751da177e4SLinus Torvalds 
15761c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->get)
15774d34a67dSDave Jones 		return ret_freq;
15781da177e4SLinus Torvalds 
15791c3d85ddSRafael J. Wysocki 	ret_freq = cpufreq_driver->get(cpu);
15801da177e4SLinus Torvalds 
1581e08f5f5bSGautham R Shenoy 	if (ret_freq && policy->cur &&
15821c3d85ddSRafael J. Wysocki 		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1583e08f5f5bSGautham R Shenoy 		/* verify no discrepancy between actual and
1584e08f5f5bSGautham R Shenoy 					saved value exists */
1585e08f5f5bSGautham R Shenoy 		if (unlikely(ret_freq != policy->cur)) {
1586e08f5f5bSGautham R Shenoy 			cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
15871da177e4SLinus Torvalds 			schedule_work(&policy->update);
15881da177e4SLinus Torvalds 		}
15891da177e4SLinus Torvalds 	}
15901da177e4SLinus Torvalds 
15914d34a67dSDave Jones 	return ret_freq;
15925a01f2e8SVenkatesh Pallipadi }
15931da177e4SLinus Torvalds 
15945a01f2e8SVenkatesh Pallipadi /**
15955a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
15965a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
15975a01f2e8SVenkatesh Pallipadi  *
15985a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
15995a01f2e8SVenkatesh Pallipadi  */
16005a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
16015a01f2e8SVenkatesh Pallipadi {
1602999976e0SAaron Plattner 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
16035a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
16045a01f2e8SVenkatesh Pallipadi 
1605999976e0SAaron Plattner 	if (policy) {
1606ad7722daSviresh kumar 		down_read(&policy->rwsem);
16075a01f2e8SVenkatesh Pallipadi 		ret_freq = __cpufreq_get(cpu);
1608ad7722daSviresh kumar 		up_read(&policy->rwsem);
1609999976e0SAaron Plattner 
1610999976e0SAaron Plattner 		cpufreq_cpu_put(policy);
1611999976e0SAaron Plattner 	}
16126eed9404SViresh Kumar 
16134d34a67dSDave Jones 	return ret_freq;
16141da177e4SLinus Torvalds }
16151da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
16161da177e4SLinus Torvalds 
16178a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
16188a25a2fdSKay Sievers 	.name		= "cpufreq",
16198a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
16208a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
16218a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1622e00e56dfSRafael J. Wysocki };
1623e00e56dfSRafael J. Wysocki 
1624e28867eaSViresh Kumar /*
1625e28867eaSViresh Kumar  * In case platform wants some specific frequency to be configured
1626e28867eaSViresh Kumar  * during suspend..
162742d4dc3fSBenjamin Herrenschmidt  */
1628e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy)
162942d4dc3fSBenjamin Herrenschmidt {
1630e28867eaSViresh Kumar 	int ret;
16314bc5d341SDave Jones 
1632e28867eaSViresh Kumar 	if (!policy->suspend_freq) {
1633e28867eaSViresh Kumar 		pr_err("%s: suspend_freq can't be zero\n", __func__);
1634e28867eaSViresh Kumar 		return -EINVAL;
163542d4dc3fSBenjamin Herrenschmidt 	}
163642d4dc3fSBenjamin Herrenschmidt 
1637e28867eaSViresh Kumar 	pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1638e28867eaSViresh Kumar 			policy->suspend_freq);
1639e28867eaSViresh Kumar 
1640e28867eaSViresh Kumar 	ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1641e28867eaSViresh Kumar 			CPUFREQ_RELATION_H);
1642e28867eaSViresh Kumar 	if (ret)
1643e28867eaSViresh Kumar 		pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1644e28867eaSViresh Kumar 				__func__, policy->suspend_freq, ret);
1645e28867eaSViresh Kumar 
1646c9060494SDave Jones 	return ret;
164742d4dc3fSBenjamin Herrenschmidt }
1648e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend);
164942d4dc3fSBenjamin Herrenschmidt 
165042d4dc3fSBenjamin Herrenschmidt /**
16512f0aea93SViresh Kumar  * cpufreq_suspend() - Suspend CPUFreq governors
16521da177e4SLinus Torvalds  *
16532f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycles for suspending governors
16542f0aea93SViresh Kumar  * as some platforms can't change frequency after this point in suspend cycle.
16552f0aea93SViresh Kumar  * Because some of the devices (like: i2c, regulators, etc) they use for
16562f0aea93SViresh Kumar  * changing frequency are suspended quickly after this point.
16571da177e4SLinus Torvalds  */
16582f0aea93SViresh Kumar void cpufreq_suspend(void)
16591da177e4SLinus Torvalds {
16603a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
16611da177e4SLinus Torvalds 
16622f0aea93SViresh Kumar 	if (!cpufreq_driver)
1663e00e56dfSRafael J. Wysocki 		return;
16641da177e4SLinus Torvalds 
16652f0aea93SViresh Kumar 	if (!has_target())
16662f0aea93SViresh Kumar 		return;
16671da177e4SLinus Torvalds 
16682f0aea93SViresh Kumar 	pr_debug("%s: Suspending Governors\n", __func__);
16692f0aea93SViresh Kumar 
16702f0aea93SViresh Kumar 	list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
16712f0aea93SViresh Kumar 		if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
16722f0aea93SViresh Kumar 			pr_err("%s: Failed to stop governor for policy: %p\n",
16732f0aea93SViresh Kumar 				__func__, policy);
16742f0aea93SViresh Kumar 		else if (cpufreq_driver->suspend
16752f0aea93SViresh Kumar 		    && cpufreq_driver->suspend(policy))
16762f0aea93SViresh Kumar 			pr_err("%s: Failed to suspend driver: %p\n", __func__,
16772f0aea93SViresh Kumar 				policy);
16781da177e4SLinus Torvalds 	}
16791da177e4SLinus Torvalds 
16802f0aea93SViresh Kumar 	cpufreq_suspended = true;
16811da177e4SLinus Torvalds }
16821da177e4SLinus Torvalds 
16831da177e4SLinus Torvalds /**
16842f0aea93SViresh Kumar  * cpufreq_resume() - Resume CPUFreq governors
16851da177e4SLinus Torvalds  *
16862f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycle for resuming governors that
16872f0aea93SViresh Kumar  * are suspended with cpufreq_suspend().
16881da177e4SLinus Torvalds  */
16892f0aea93SViresh Kumar void cpufreq_resume(void)
16901da177e4SLinus Torvalds {
16911da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
16921da177e4SLinus Torvalds 
16932f0aea93SViresh Kumar 	if (!cpufreq_driver)
16941da177e4SLinus Torvalds 		return;
16951da177e4SLinus Torvalds 
16962f0aea93SViresh Kumar 	if (!has_target())
16972f0aea93SViresh Kumar 		return;
16981da177e4SLinus Torvalds 
16992f0aea93SViresh Kumar 	pr_debug("%s: Resuming Governors\n", __func__);
17002f0aea93SViresh Kumar 
17012f0aea93SViresh Kumar 	cpufreq_suspended = false;
17022f0aea93SViresh Kumar 
17032f0aea93SViresh Kumar 	list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
17040c5aa405SViresh Kumar 		if (cpufreq_driver->resume && cpufreq_driver->resume(policy))
17050c5aa405SViresh Kumar 			pr_err("%s: Failed to resume driver: %p\n", __func__,
17060c5aa405SViresh Kumar 				policy);
17070c5aa405SViresh Kumar 		else if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
17082f0aea93SViresh Kumar 		    || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
17092f0aea93SViresh Kumar 			pr_err("%s: Failed to start governor for policy: %p\n",
17102f0aea93SViresh Kumar 				__func__, policy);
17112f0aea93SViresh Kumar 
17122f0aea93SViresh Kumar 		/*
17132f0aea93SViresh Kumar 		 * schedule call cpufreq_update_policy() for boot CPU, i.e. last
17142f0aea93SViresh Kumar 		 * policy in list. It will verify that the current freq is in
17152f0aea93SViresh Kumar 		 * sync with what we believe it to be.
17162f0aea93SViresh Kumar 		 */
17172f0aea93SViresh Kumar 		if (list_is_last(&policy->policy_list, &cpufreq_policy_list))
17183a3e9e06SViresh Kumar 			schedule_work(&policy->update);
17191da177e4SLinus Torvalds 	}
17202f0aea93SViresh Kumar }
17211da177e4SLinus Torvalds 
17229d95046eSBorislav Petkov /**
17239d95046eSBorislav Petkov  *	cpufreq_get_current_driver - return current driver's name
17249d95046eSBorislav Petkov  *
17259d95046eSBorislav Petkov  *	Return the name string of the currently loaded cpufreq driver
17269d95046eSBorislav Petkov  *	or NULL, if none.
17279d95046eSBorislav Petkov  */
17289d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
17299d95046eSBorislav Petkov {
17301c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
17311c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
17321c3d85ddSRafael J. Wysocki 
17331c3d85ddSRafael J. Wysocki 	return NULL;
17349d95046eSBorislav Petkov }
17359d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
17361da177e4SLinus Torvalds 
17371da177e4SLinus Torvalds /*********************************************************************
17381da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
17391da177e4SLinus Torvalds  *********************************************************************/
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds /**
17421da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
17431da177e4SLinus Torvalds  *	@nb: notifier function to register
17441da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
17451da177e4SLinus Torvalds  *
17461da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
17471da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
17481da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
17491da177e4SLinus Torvalds  *      changes in cpufreq policy.
17501da177e4SLinus Torvalds  *
17511da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1752e041c683SAlan Stern  *	blocking_notifier_chain_register.
17531da177e4SLinus Torvalds  */
17541da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
17551da177e4SLinus Torvalds {
17561da177e4SLinus Torvalds 	int ret;
17571da177e4SLinus Torvalds 
1758d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1759d5aaffa9SDirk Brandewie 		return -EINVAL;
1760d5aaffa9SDirk Brandewie 
176174212ca4SCesar Eduardo Barros 	WARN_ON(!init_cpufreq_transition_notifier_list_called);
176274212ca4SCesar Eduardo Barros 
17631da177e4SLinus Torvalds 	switch (list) {
17641da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1765b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1766e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
17671da177e4SLinus Torvalds 		break;
17681da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1769e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1770e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
17711da177e4SLinus Torvalds 		break;
17721da177e4SLinus Torvalds 	default:
17731da177e4SLinus Torvalds 		ret = -EINVAL;
17741da177e4SLinus Torvalds 	}
17751da177e4SLinus Torvalds 
17761da177e4SLinus Torvalds 	return ret;
17771da177e4SLinus Torvalds }
17781da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
17791da177e4SLinus Torvalds 
17801da177e4SLinus Torvalds /**
17811da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
17821da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
17831da177e4SLinus Torvalds  *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
17841da177e4SLinus Torvalds  *
17851da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
17861da177e4SLinus Torvalds  *
17871da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1788e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
17891da177e4SLinus Torvalds  */
17901da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
17911da177e4SLinus Torvalds {
17921da177e4SLinus Torvalds 	int ret;
17931da177e4SLinus Torvalds 
1794d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1795d5aaffa9SDirk Brandewie 		return -EINVAL;
1796d5aaffa9SDirk Brandewie 
17971da177e4SLinus Torvalds 	switch (list) {
17981da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1799b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
1800e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
18011da177e4SLinus Torvalds 		break;
18021da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1803e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1804e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
18051da177e4SLinus Torvalds 		break;
18061da177e4SLinus Torvalds 	default:
18071da177e4SLinus Torvalds 		ret = -EINVAL;
18081da177e4SLinus Torvalds 	}
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 	return ret;
18111da177e4SLinus Torvalds }
18121da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
18131da177e4SLinus Torvalds 
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds /*********************************************************************
18161da177e4SLinus Torvalds  *                              GOVERNORS                            *
18171da177e4SLinus Torvalds  *********************************************************************/
18181da177e4SLinus Torvalds 
1819*1c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */
1820*1c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy,
1821*1c03a2d0SViresh Kumar 				 struct cpufreq_freqs *freqs, int index)
1822*1c03a2d0SViresh Kumar {
1823*1c03a2d0SViresh Kumar 	int ret;
1824*1c03a2d0SViresh Kumar 
1825*1c03a2d0SViresh Kumar 	freqs->new = cpufreq_driver->get_intermediate(policy, index);
1826*1c03a2d0SViresh Kumar 
1827*1c03a2d0SViresh Kumar 	/* We don't need to switch to intermediate freq */
1828*1c03a2d0SViresh Kumar 	if (!freqs->new)
1829*1c03a2d0SViresh Kumar 		return 0;
1830*1c03a2d0SViresh Kumar 
1831*1c03a2d0SViresh Kumar 	pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1832*1c03a2d0SViresh Kumar 		 __func__, policy->cpu, freqs->old, freqs->new);
1833*1c03a2d0SViresh Kumar 
1834*1c03a2d0SViresh Kumar 	cpufreq_freq_transition_begin(policy, freqs);
1835*1c03a2d0SViresh Kumar 	ret = cpufreq_driver->target_intermediate(policy, index);
1836*1c03a2d0SViresh Kumar 	cpufreq_freq_transition_end(policy, freqs, ret);
1837*1c03a2d0SViresh Kumar 
1838*1c03a2d0SViresh Kumar 	if (ret)
1839*1c03a2d0SViresh Kumar 		pr_err("%s: Failed to change to intermediate frequency: %d\n",
1840*1c03a2d0SViresh Kumar 		       __func__, ret);
1841*1c03a2d0SViresh Kumar 
1842*1c03a2d0SViresh Kumar 	return ret;
1843*1c03a2d0SViresh Kumar }
1844*1c03a2d0SViresh Kumar 
18458d65775dSViresh Kumar static int __target_index(struct cpufreq_policy *policy,
18468d65775dSViresh Kumar 			  struct cpufreq_frequency_table *freq_table, int index)
18478d65775dSViresh Kumar {
1848*1c03a2d0SViresh Kumar 	struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1849*1c03a2d0SViresh Kumar 	unsigned int intermediate_freq = 0;
18508d65775dSViresh Kumar 	int retval = -EINVAL;
18518d65775dSViresh Kumar 	bool notify;
18528d65775dSViresh Kumar 
18538d65775dSViresh Kumar 	notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
18548d65775dSViresh Kumar 	if (notify) {
1855*1c03a2d0SViresh Kumar 		/* Handle switching to intermediate frequency */
1856*1c03a2d0SViresh Kumar 		if (cpufreq_driver->get_intermediate) {
1857*1c03a2d0SViresh Kumar 			retval = __target_intermediate(policy, &freqs, index);
1858*1c03a2d0SViresh Kumar 			if (retval)
1859*1c03a2d0SViresh Kumar 				return retval;
18608d65775dSViresh Kumar 
1861*1c03a2d0SViresh Kumar 			intermediate_freq = freqs.new;
1862*1c03a2d0SViresh Kumar 			/* Set old freq to intermediate */
1863*1c03a2d0SViresh Kumar 			if (intermediate_freq)
1864*1c03a2d0SViresh Kumar 				freqs.old = freqs.new;
1865*1c03a2d0SViresh Kumar 		}
1866*1c03a2d0SViresh Kumar 
1867*1c03a2d0SViresh Kumar 		freqs.new = freq_table[index].frequency;
18688d65775dSViresh Kumar 		pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
18698d65775dSViresh Kumar 			 __func__, policy->cpu, freqs.old, freqs.new);
18708d65775dSViresh Kumar 
18718d65775dSViresh Kumar 		cpufreq_freq_transition_begin(policy, &freqs);
18728d65775dSViresh Kumar 	}
18738d65775dSViresh Kumar 
18748d65775dSViresh Kumar 	retval = cpufreq_driver->target_index(policy, index);
18758d65775dSViresh Kumar 	if (retval)
18768d65775dSViresh Kumar 		pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
18778d65775dSViresh Kumar 		       retval);
18788d65775dSViresh Kumar 
1879*1c03a2d0SViresh Kumar 	if (notify) {
18808d65775dSViresh Kumar 		cpufreq_freq_transition_end(policy, &freqs, retval);
18818d65775dSViresh Kumar 
1882*1c03a2d0SViresh Kumar 		/*
1883*1c03a2d0SViresh Kumar 		 * Failed after setting to intermediate freq? Driver should have
1884*1c03a2d0SViresh Kumar 		 * reverted back to initial frequency and so should we. Check
1885*1c03a2d0SViresh Kumar 		 * here for intermediate_freq instead of get_intermediate, in
1886*1c03a2d0SViresh Kumar 		 * case we have't switched to intermediate freq at all.
1887*1c03a2d0SViresh Kumar 		 */
1888*1c03a2d0SViresh Kumar 		if (unlikely(retval && intermediate_freq)) {
1889*1c03a2d0SViresh Kumar 			freqs.old = intermediate_freq;
1890*1c03a2d0SViresh Kumar 			freqs.new = policy->restore_freq;
1891*1c03a2d0SViresh Kumar 			cpufreq_freq_transition_begin(policy, &freqs);
1892*1c03a2d0SViresh Kumar 			cpufreq_freq_transition_end(policy, &freqs, 0);
1893*1c03a2d0SViresh Kumar 		}
1894*1c03a2d0SViresh Kumar 	}
1895*1c03a2d0SViresh Kumar 
18968d65775dSViresh Kumar 	return retval;
18978d65775dSViresh Kumar }
18988d65775dSViresh Kumar 
18991da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
19001da177e4SLinus Torvalds 			    unsigned int target_freq,
19011da177e4SLinus Torvalds 			    unsigned int relation)
19021da177e4SLinus Torvalds {
19037249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
19048d65775dSViresh Kumar 	int retval = -EINVAL;
1905c32b6b8eSAshok Raj 
1906a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1907a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1908a7b422cdSKonrad Rzeszutek Wilk 
19097249924eSViresh Kumar 	/* Make sure that target_freq is within supported range */
19107249924eSViresh Kumar 	if (target_freq > policy->max)
19117249924eSViresh Kumar 		target_freq = policy->max;
19127249924eSViresh Kumar 	if (target_freq < policy->min)
19137249924eSViresh Kumar 		target_freq = policy->min;
19147249924eSViresh Kumar 
19157249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
19167249924eSViresh Kumar 		 policy->cpu, target_freq, relation, old_target_freq);
19175a1c0228SViresh Kumar 
19189c0ebcf7SViresh Kumar 	/*
19199c0ebcf7SViresh Kumar 	 * This might look like a redundant call as we are checking it again
19209c0ebcf7SViresh Kumar 	 * after finding index. But it is left intentionally for cases where
19219c0ebcf7SViresh Kumar 	 * exactly same freq is called again and so we can save on few function
19229c0ebcf7SViresh Kumar 	 * calls.
19239c0ebcf7SViresh Kumar 	 */
19245a1c0228SViresh Kumar 	if (target_freq == policy->cur)
19255a1c0228SViresh Kumar 		return 0;
19265a1c0228SViresh Kumar 
1927*1c03a2d0SViresh Kumar 	/* Save last value to restore later on errors */
1928*1c03a2d0SViresh Kumar 	policy->restore_freq = policy->cur;
1929*1c03a2d0SViresh Kumar 
19301c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
19311c3d85ddSRafael J. Wysocki 		retval = cpufreq_driver->target(policy, target_freq, relation);
19329c0ebcf7SViresh Kumar 	else if (cpufreq_driver->target_index) {
19339c0ebcf7SViresh Kumar 		struct cpufreq_frequency_table *freq_table;
19349c0ebcf7SViresh Kumar 		int index;
193590d45d17SAshok Raj 
19369c0ebcf7SViresh Kumar 		freq_table = cpufreq_frequency_get_table(policy->cpu);
19379c0ebcf7SViresh Kumar 		if (unlikely(!freq_table)) {
19389c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find freq_table\n", __func__);
19399c0ebcf7SViresh Kumar 			goto out;
19409c0ebcf7SViresh Kumar 		}
19419c0ebcf7SViresh Kumar 
19429c0ebcf7SViresh Kumar 		retval = cpufreq_frequency_table_target(policy, freq_table,
19439c0ebcf7SViresh Kumar 				target_freq, relation, &index);
19449c0ebcf7SViresh Kumar 		if (unlikely(retval)) {
19459c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find matching freq\n", __func__);
19469c0ebcf7SViresh Kumar 			goto out;
19479c0ebcf7SViresh Kumar 		}
19489c0ebcf7SViresh Kumar 
1949d4019f0aSViresh Kumar 		if (freq_table[index].frequency == policy->cur) {
19509c0ebcf7SViresh Kumar 			retval = 0;
1951d4019f0aSViresh Kumar 			goto out;
1952d4019f0aSViresh Kumar 		}
1953d4019f0aSViresh Kumar 
19548d65775dSViresh Kumar 		retval = __target_index(policy, freq_table, index);
19559c0ebcf7SViresh Kumar 	}
19569c0ebcf7SViresh Kumar 
19579c0ebcf7SViresh Kumar out:
19581da177e4SLinus Torvalds 	return retval;
19591da177e4SLinus Torvalds }
19601da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
19611da177e4SLinus Torvalds 
19621da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
19631da177e4SLinus Torvalds 			  unsigned int target_freq,
19641da177e4SLinus Torvalds 			  unsigned int relation)
19651da177e4SLinus Torvalds {
1966f1829e4aSJulia Lawall 	int ret = -EINVAL;
19671da177e4SLinus Torvalds 
1968ad7722daSviresh kumar 	down_write(&policy->rwsem);
19691da177e4SLinus Torvalds 
19701da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
19711da177e4SLinus Torvalds 
1972ad7722daSviresh kumar 	up_write(&policy->rwsem);
19731da177e4SLinus Torvalds 
19741da177e4SLinus Torvalds 	return ret;
19751da177e4SLinus Torvalds }
19761da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
19771da177e4SLinus Torvalds 
1978153d7f3fSArjan van de Ven /*
1979153d7f3fSArjan van de Ven  * when "event" is CPUFREQ_GOV_LIMITS
1980153d7f3fSArjan van de Ven  */
19811da177e4SLinus Torvalds 
1982e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy,
1983e08f5f5bSGautham R Shenoy 					unsigned int event)
19841da177e4SLinus Torvalds {
1985cc993cabSDave Jones 	int ret;
19866afde10cSThomas Renninger 
19876afde10cSThomas Renninger 	/* Only must be defined when default governor is known to have latency
19886afde10cSThomas Renninger 	   restrictions, like e.g. conservative or ondemand.
19896afde10cSThomas Renninger 	   That this is the case is already ensured in Kconfig
19906afde10cSThomas Renninger 	*/
19916afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
19926afde10cSThomas Renninger 	struct cpufreq_governor *gov = &cpufreq_gov_performance;
19936afde10cSThomas Renninger #else
19946afde10cSThomas Renninger 	struct cpufreq_governor *gov = NULL;
19956afde10cSThomas Renninger #endif
19961c256245SThomas Renninger 
19972f0aea93SViresh Kumar 	/* Don't start any governor operations if we are entering suspend */
19982f0aea93SViresh Kumar 	if (cpufreq_suspended)
19992f0aea93SViresh Kumar 		return 0;
20002f0aea93SViresh Kumar 
20011c256245SThomas Renninger 	if (policy->governor->max_transition_latency &&
20021c256245SThomas Renninger 	    policy->cpuinfo.transition_latency >
20031c256245SThomas Renninger 	    policy->governor->max_transition_latency) {
20046afde10cSThomas Renninger 		if (!gov)
20056afde10cSThomas Renninger 			return -EINVAL;
20066afde10cSThomas Renninger 		else {
2007e837f9b5SJoe Perches 			pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2008e837f9b5SJoe Perches 				policy->governor->name, gov->name);
20091c256245SThomas Renninger 			policy->governor = gov;
20101c256245SThomas Renninger 		}
20116afde10cSThomas Renninger 	}
20121da177e4SLinus Torvalds 
2013fe492f3fSViresh Kumar 	if (event == CPUFREQ_GOV_POLICY_INIT)
20141da177e4SLinus Torvalds 		if (!try_module_get(policy->governor->owner))
20151da177e4SLinus Torvalds 			return -EINVAL;
20161da177e4SLinus Torvalds 
20172d06d8c4SDominik Brodowski 	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
2018e08f5f5bSGautham R Shenoy 		 policy->cpu, event);
201995731ebbSXiaoguang Chen 
202095731ebbSXiaoguang Chen 	mutex_lock(&cpufreq_governor_lock);
202156d07db2SSrivatsa S. Bhat 	if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
2022f73d3933SViresh Kumar 	    || (!policy->governor_enabled
2023f73d3933SViresh Kumar 	    && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
202495731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
202595731ebbSXiaoguang Chen 		return -EBUSY;
202695731ebbSXiaoguang Chen 	}
202795731ebbSXiaoguang Chen 
202895731ebbSXiaoguang Chen 	if (event == CPUFREQ_GOV_STOP)
202995731ebbSXiaoguang Chen 		policy->governor_enabled = false;
203095731ebbSXiaoguang Chen 	else if (event == CPUFREQ_GOV_START)
203195731ebbSXiaoguang Chen 		policy->governor_enabled = true;
203295731ebbSXiaoguang Chen 
203395731ebbSXiaoguang Chen 	mutex_unlock(&cpufreq_governor_lock);
203495731ebbSXiaoguang Chen 
20351da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
20361da177e4SLinus Torvalds 
20374d5dcc42SViresh Kumar 	if (!ret) {
20384d5dcc42SViresh Kumar 		if (event == CPUFREQ_GOV_POLICY_INIT)
20398e53695fSViresh Kumar 			policy->governor->initialized++;
20404d5dcc42SViresh Kumar 		else if (event == CPUFREQ_GOV_POLICY_EXIT)
20418e53695fSViresh Kumar 			policy->governor->initialized--;
204295731ebbSXiaoguang Chen 	} else {
204395731ebbSXiaoguang Chen 		/* Restore original values */
204495731ebbSXiaoguang Chen 		mutex_lock(&cpufreq_governor_lock);
204595731ebbSXiaoguang Chen 		if (event == CPUFREQ_GOV_STOP)
204695731ebbSXiaoguang Chen 			policy->governor_enabled = true;
204795731ebbSXiaoguang Chen 		else if (event == CPUFREQ_GOV_START)
204895731ebbSXiaoguang Chen 			policy->governor_enabled = false;
204995731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
20504d5dcc42SViresh Kumar 	}
2051b394058fSViresh Kumar 
2052fe492f3fSViresh Kumar 	if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2053fe492f3fSViresh Kumar 			((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
20541da177e4SLinus Torvalds 		module_put(policy->governor->owner);
20551da177e4SLinus Torvalds 
20561da177e4SLinus Torvalds 	return ret;
20571da177e4SLinus Torvalds }
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
20601da177e4SLinus Torvalds {
20613bcb09a3SJeremy Fitzhardinge 	int err;
20621da177e4SLinus Torvalds 
20631da177e4SLinus Torvalds 	if (!governor)
20641da177e4SLinus Torvalds 		return -EINVAL;
20651da177e4SLinus Torvalds 
2066a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2067a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2068a7b422cdSKonrad Rzeszutek Wilk 
20693fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
20701da177e4SLinus Torvalds 
2071b394058fSViresh Kumar 	governor->initialized = 0;
20723bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
20733bcb09a3SJeremy Fitzhardinge 	if (__find_governor(governor->name) == NULL) {
20743bcb09a3SJeremy Fitzhardinge 		err = 0;
20751da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
20763bcb09a3SJeremy Fitzhardinge 	}
20771da177e4SLinus Torvalds 
20783fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
20793bcb09a3SJeremy Fitzhardinge 	return err;
20801da177e4SLinus Torvalds }
20811da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
20821da177e4SLinus Torvalds 
20831da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
20841da177e4SLinus Torvalds {
208590e41bacSPrarit Bhargava 	int cpu;
208690e41bacSPrarit Bhargava 
20871da177e4SLinus Torvalds 	if (!governor)
20881da177e4SLinus Torvalds 		return;
20891da177e4SLinus Torvalds 
2090a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2091a7b422cdSKonrad Rzeszutek Wilk 		return;
2092a7b422cdSKonrad Rzeszutek Wilk 
209390e41bacSPrarit Bhargava 	for_each_present_cpu(cpu) {
209490e41bacSPrarit Bhargava 		if (cpu_online(cpu))
209590e41bacSPrarit Bhargava 			continue;
209690e41bacSPrarit Bhargava 		if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
209790e41bacSPrarit Bhargava 			strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
209890e41bacSPrarit Bhargava 	}
209990e41bacSPrarit Bhargava 
21003fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
21011da177e4SLinus Torvalds 	list_del(&governor->governor_list);
21023fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
21031da177e4SLinus Torvalds 	return;
21041da177e4SLinus Torvalds }
21051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
21061da177e4SLinus Torvalds 
21071da177e4SLinus Torvalds 
21081da177e4SLinus Torvalds /*********************************************************************
21091da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
21101da177e4SLinus Torvalds  *********************************************************************/
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds /**
21131da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
211429464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
211529464f28SDave Jones  *	is written
21161da177e4SLinus Torvalds  *
21171da177e4SLinus Torvalds  * Reads the current cpufreq policy.
21181da177e4SLinus Torvalds  */
21191da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
21201da177e4SLinus Torvalds {
21211da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
21221da177e4SLinus Torvalds 	if (!policy)
21231da177e4SLinus Torvalds 		return -EINVAL;
21241da177e4SLinus Torvalds 
21251da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
21261da177e4SLinus Torvalds 	if (!cpu_policy)
21271da177e4SLinus Torvalds 		return -EINVAL;
21281da177e4SLinus Torvalds 
2129d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
21301da177e4SLinus Torvalds 
21311da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
21321da177e4SLinus Torvalds 	return 0;
21331da177e4SLinus Torvalds }
21341da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
21351da177e4SLinus Torvalds 
2136153d7f3fSArjan van de Ven /*
2137037ce839SViresh Kumar  * policy : current policy.
2138037ce839SViresh Kumar  * new_policy: policy to be set.
2139153d7f3fSArjan van de Ven  */
2140037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
21413a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy)
21421da177e4SLinus Torvalds {
2143d9a789c7SRafael J. Wysocki 	struct cpufreq_governor *old_gov;
2144d9a789c7SRafael J. Wysocki 	int ret;
21451da177e4SLinus Torvalds 
2146e837f9b5SJoe Perches 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2147e837f9b5SJoe Perches 		 new_policy->cpu, new_policy->min, new_policy->max);
21481da177e4SLinus Torvalds 
2149d5b73cd8SViresh Kumar 	memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
21501da177e4SLinus Torvalds 
2151d9a789c7SRafael J. Wysocki 	if (new_policy->min > policy->max || new_policy->max < policy->min)
2152d9a789c7SRafael J. Wysocki 		return -EINVAL;
21539c9a43edSMattia Dongili 
21541da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
21553a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
21561da177e4SLinus Torvalds 	if (ret)
2157d9a789c7SRafael J. Wysocki 		return ret;
21581da177e4SLinus Torvalds 
21591da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
2160e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
21613a3e9e06SViresh Kumar 			CPUFREQ_ADJUST, new_policy);
21621da177e4SLinus Torvalds 
21631da177e4SLinus Torvalds 	/* adjust if necessary - hardware incompatibility*/
2164e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
21653a3e9e06SViresh Kumar 			CPUFREQ_INCOMPATIBLE, new_policy);
21661da177e4SLinus Torvalds 
2167bb176f7dSViresh Kumar 	/*
2168bb176f7dSViresh Kumar 	 * verify the cpu speed can be set within this limit, which might be
2169bb176f7dSViresh Kumar 	 * different to the first one
2170bb176f7dSViresh Kumar 	 */
21713a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
2172e041c683SAlan Stern 	if (ret)
2173d9a789c7SRafael J. Wysocki 		return ret;
21741da177e4SLinus Torvalds 
21751da177e4SLinus Torvalds 	/* notification of the new policy */
2176e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
21773a3e9e06SViresh Kumar 			CPUFREQ_NOTIFY, new_policy);
21781da177e4SLinus Torvalds 
21793a3e9e06SViresh Kumar 	policy->min = new_policy->min;
21803a3e9e06SViresh Kumar 	policy->max = new_policy->max;
21811da177e4SLinus Torvalds 
21822d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
21833a3e9e06SViresh Kumar 		 policy->min, policy->max);
21841da177e4SLinus Torvalds 
21851c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
21863a3e9e06SViresh Kumar 		policy->policy = new_policy->policy;
21872d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
2188d9a789c7SRafael J. Wysocki 		return cpufreq_driver->setpolicy(new_policy);
2189d9a789c7SRafael J. Wysocki 	}
2190d9a789c7SRafael J. Wysocki 
2191d9a789c7SRafael J. Wysocki 	if (new_policy->governor == policy->governor)
2192d9a789c7SRafael J. Wysocki 		goto out;
21931da177e4SLinus Torvalds 
21942d06d8c4SDominik Brodowski 	pr_debug("governor switch\n");
21951da177e4SLinus Torvalds 
2196d9a789c7SRafael J. Wysocki 	/* save old, working values */
2197d9a789c7SRafael J. Wysocki 	old_gov = policy->governor;
21981da177e4SLinus Torvalds 	/* end old governor */
2199d9a789c7SRafael J. Wysocki 	if (old_gov) {
22003a3e9e06SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2201ad7722daSviresh kumar 		up_write(&policy->rwsem);
2202d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2203ad7722daSviresh kumar 		down_write(&policy->rwsem);
22047bd353a9SViresh Kumar 	}
22051da177e4SLinus Torvalds 
22061da177e4SLinus Torvalds 	/* start new governor */
22073a3e9e06SViresh Kumar 	policy->governor = new_policy->governor;
22083a3e9e06SViresh Kumar 	if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2209d9a789c7SRafael J. Wysocki 		if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2210d9a789c7SRafael J. Wysocki 			goto out;
2211d9a789c7SRafael J. Wysocki 
2212ad7722daSviresh kumar 		up_write(&policy->rwsem);
2213d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2214ad7722daSviresh kumar 		down_write(&policy->rwsem);
2215955ef483SViresh Kumar 	}
22167bd353a9SViresh Kumar 
22171da177e4SLinus Torvalds 	/* new governor failed, so re-start old one */
2218d9a789c7SRafael J. Wysocki 	pr_debug("starting governor %s failed\n", policy->governor->name);
22191da177e4SLinus Torvalds 	if (old_gov) {
22203a3e9e06SViresh Kumar 		policy->governor = old_gov;
2221d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2222d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_START);
22231da177e4SLinus Torvalds 	}
22241da177e4SLinus Torvalds 
2225d9a789c7SRafael J. Wysocki 	return -EINVAL;
2226d9a789c7SRafael J. Wysocki 
2227d9a789c7SRafael J. Wysocki  out:
2228d9a789c7SRafael J. Wysocki 	pr_debug("governor: change or update limits\n");
2229d9a789c7SRafael J. Wysocki 	return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
22301da177e4SLinus Torvalds }
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds /**
22331da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
22341da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
22351da177e4SLinus Torvalds  *
223625985edcSLucas De Marchi  *	Useful for policy notifiers which have different necessities
22371da177e4SLinus Torvalds  *	at different times.
22381da177e4SLinus Torvalds  */
22391da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
22401da177e4SLinus Torvalds {
22413a3e9e06SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
22423a3e9e06SViresh Kumar 	struct cpufreq_policy new_policy;
2243f1829e4aSJulia Lawall 	int ret;
22441da177e4SLinus Torvalds 
22453a3e9e06SViresh Kumar 	if (!policy) {
2246f1829e4aSJulia Lawall 		ret = -ENODEV;
2247f1829e4aSJulia Lawall 		goto no_policy;
2248f1829e4aSJulia Lawall 	}
22491da177e4SLinus Torvalds 
2250ad7722daSviresh kumar 	down_write(&policy->rwsem);
22511da177e4SLinus Torvalds 
22522d06d8c4SDominik Brodowski 	pr_debug("updating policy for CPU %u\n", cpu);
2253d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
22543a3e9e06SViresh Kumar 	new_policy.min = policy->user_policy.min;
22553a3e9e06SViresh Kumar 	new_policy.max = policy->user_policy.max;
22563a3e9e06SViresh Kumar 	new_policy.policy = policy->user_policy.policy;
22573a3e9e06SViresh Kumar 	new_policy.governor = policy->user_policy.governor;
22581da177e4SLinus Torvalds 
2259bb176f7dSViresh Kumar 	/*
2260bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
2261bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
2262bb176f7dSViresh Kumar 	 */
22632ed99e39SRafael J. Wysocki 	if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
22643a3e9e06SViresh Kumar 		new_policy.cur = cpufreq_driver->get(cpu);
2265bd0fa9bbSViresh Kumar 		if (WARN_ON(!new_policy.cur)) {
2266bd0fa9bbSViresh Kumar 			ret = -EIO;
2267bd0fa9bbSViresh Kumar 			goto no_policy;
2268bd0fa9bbSViresh Kumar 		}
2269bd0fa9bbSViresh Kumar 
22703a3e9e06SViresh Kumar 		if (!policy->cur) {
2271e837f9b5SJoe Perches 			pr_debug("Driver did not initialize current freq\n");
22723a3e9e06SViresh Kumar 			policy->cur = new_policy.cur;
2273a85f7bd3SThomas Renninger 		} else {
22749c0ebcf7SViresh Kumar 			if (policy->cur != new_policy.cur && has_target())
22753a3e9e06SViresh Kumar 				cpufreq_out_of_sync(cpu, policy->cur,
22763a3e9e06SViresh Kumar 								new_policy.cur);
22770961dd0dSThomas Renninger 		}
2278a85f7bd3SThomas Renninger 	}
22790961dd0dSThomas Renninger 
2280037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
22811da177e4SLinus Torvalds 
2282ad7722daSviresh kumar 	up_write(&policy->rwsem);
22835a01f2e8SVenkatesh Pallipadi 
22843a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
2285f1829e4aSJulia Lawall no_policy:
22861da177e4SLinus Torvalds 	return ret;
22871da177e4SLinus Torvalds }
22881da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
22891da177e4SLinus Torvalds 
22902760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb,
2291c32b6b8eSAshok Raj 					unsigned long action, void *hcpu)
2292c32b6b8eSAshok Raj {
2293c32b6b8eSAshok Raj 	unsigned int cpu = (unsigned long)hcpu;
22948a25a2fdSKay Sievers 	struct device *dev;
2295c32b6b8eSAshok Raj 
22968a25a2fdSKay Sievers 	dev = get_cpu_device(cpu);
22978a25a2fdSKay Sievers 	if (dev) {
22985302c3fbSSrivatsa S. Bhat 		switch (action & ~CPU_TASKS_FROZEN) {
2299c32b6b8eSAshok Raj 		case CPU_ONLINE:
230096bbbe4aSViresh Kumar 			__cpufreq_add_dev(dev, NULL);
2301c32b6b8eSAshok Raj 			break;
23025302c3fbSSrivatsa S. Bhat 
2303c32b6b8eSAshok Raj 		case CPU_DOWN_PREPARE:
230496bbbe4aSViresh Kumar 			__cpufreq_remove_dev_prepare(dev, NULL);
23051aee40acSSrivatsa S. Bhat 			break;
23061aee40acSSrivatsa S. Bhat 
23071aee40acSSrivatsa S. Bhat 		case CPU_POST_DEAD:
230896bbbe4aSViresh Kumar 			__cpufreq_remove_dev_finish(dev, NULL);
2309c32b6b8eSAshok Raj 			break;
23105302c3fbSSrivatsa S. Bhat 
23115a01f2e8SVenkatesh Pallipadi 		case CPU_DOWN_FAILED:
231296bbbe4aSViresh Kumar 			__cpufreq_add_dev(dev, NULL);
2313c32b6b8eSAshok Raj 			break;
2314c32b6b8eSAshok Raj 		}
2315c32b6b8eSAshok Raj 	}
2316c32b6b8eSAshok Raj 	return NOTIFY_OK;
2317c32b6b8eSAshok Raj }
2318c32b6b8eSAshok Raj 
23199c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = {
2320c32b6b8eSAshok Raj 	.notifier_call = cpufreq_cpu_callback,
2321c32b6b8eSAshok Raj };
23221da177e4SLinus Torvalds 
23231da177e4SLinus Torvalds /*********************************************************************
23246f19efc0SLukasz Majewski  *               BOOST						     *
23256f19efc0SLukasz Majewski  *********************************************************************/
23266f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state)
23276f19efc0SLukasz Majewski {
23286f19efc0SLukasz Majewski 	struct cpufreq_frequency_table *freq_table;
23296f19efc0SLukasz Majewski 	struct cpufreq_policy *policy;
23306f19efc0SLukasz Majewski 	int ret = -EINVAL;
23316f19efc0SLukasz Majewski 
23326f19efc0SLukasz Majewski 	list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
23336f19efc0SLukasz Majewski 		freq_table = cpufreq_frequency_get_table(policy->cpu);
23346f19efc0SLukasz Majewski 		if (freq_table) {
23356f19efc0SLukasz Majewski 			ret = cpufreq_frequency_table_cpuinfo(policy,
23366f19efc0SLukasz Majewski 							freq_table);
23376f19efc0SLukasz Majewski 			if (ret) {
23386f19efc0SLukasz Majewski 				pr_err("%s: Policy frequency update failed\n",
23396f19efc0SLukasz Majewski 				       __func__);
23406f19efc0SLukasz Majewski 				break;
23416f19efc0SLukasz Majewski 			}
23426f19efc0SLukasz Majewski 			policy->user_policy.max = policy->max;
23436f19efc0SLukasz Majewski 			__cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
23446f19efc0SLukasz Majewski 		}
23456f19efc0SLukasz Majewski 	}
23466f19efc0SLukasz Majewski 
23476f19efc0SLukasz Majewski 	return ret;
23486f19efc0SLukasz Majewski }
23496f19efc0SLukasz Majewski 
23506f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state)
23516f19efc0SLukasz Majewski {
23526f19efc0SLukasz Majewski 	unsigned long flags;
23536f19efc0SLukasz Majewski 	int ret = 0;
23546f19efc0SLukasz Majewski 
23556f19efc0SLukasz Majewski 	if (cpufreq_driver->boost_enabled == state)
23566f19efc0SLukasz Majewski 		return 0;
23576f19efc0SLukasz Majewski 
23586f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
23596f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = state;
23606f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23616f19efc0SLukasz Majewski 
23626f19efc0SLukasz Majewski 	ret = cpufreq_driver->set_boost(state);
23636f19efc0SLukasz Majewski 	if (ret) {
23646f19efc0SLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
23656f19efc0SLukasz Majewski 		cpufreq_driver->boost_enabled = !state;
23666f19efc0SLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23676f19efc0SLukasz Majewski 
2368e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST\n",
2369e837f9b5SJoe Perches 		       __func__, state ? "enable" : "disable");
23706f19efc0SLukasz Majewski 	}
23716f19efc0SLukasz Majewski 
23726f19efc0SLukasz Majewski 	return ret;
23736f19efc0SLukasz Majewski }
23746f19efc0SLukasz Majewski 
23756f19efc0SLukasz Majewski int cpufreq_boost_supported(void)
23766f19efc0SLukasz Majewski {
23776f19efc0SLukasz Majewski 	if (likely(cpufreq_driver))
23786f19efc0SLukasz Majewski 		return cpufreq_driver->boost_supported;
23796f19efc0SLukasz Majewski 
23806f19efc0SLukasz Majewski 	return 0;
23816f19efc0SLukasz Majewski }
23826f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
23836f19efc0SLukasz Majewski 
23846f19efc0SLukasz Majewski int cpufreq_boost_enabled(void)
23856f19efc0SLukasz Majewski {
23866f19efc0SLukasz Majewski 	return cpufreq_driver->boost_enabled;
23876f19efc0SLukasz Majewski }
23886f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
23896f19efc0SLukasz Majewski 
23906f19efc0SLukasz Majewski /*********************************************************************
23911da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
23921da177e4SLinus Torvalds  *********************************************************************/
23931da177e4SLinus Torvalds 
23941da177e4SLinus Torvalds /**
23951da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
23961da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
23971da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
23981da177e4SLinus Torvalds  *
23991da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
24001da177e4SLinus Torvalds  * returns zero on success, -EBUSY when another driver got here first
24011da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
24021da177e4SLinus Torvalds  *
24031da177e4SLinus Torvalds  */
2404221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
24051da177e4SLinus Torvalds {
24061da177e4SLinus Torvalds 	unsigned long flags;
24071da177e4SLinus Torvalds 	int ret;
24081da177e4SLinus Torvalds 
2409a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2410a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2411a7b422cdSKonrad Rzeszutek Wilk 
24121da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
24139c0ebcf7SViresh Kumar 	    !(driver_data->setpolicy || driver_data->target_index ||
24149832235fSRafael J. Wysocki 		    driver_data->target) ||
24159832235fSRafael J. Wysocki 	     (driver_data->setpolicy && (driver_data->target_index ||
2416*1c03a2d0SViresh Kumar 		    driver_data->target)) ||
2417*1c03a2d0SViresh Kumar 	     (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
24181da177e4SLinus Torvalds 		return -EINVAL;
24191da177e4SLinus Torvalds 
24202d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
24211da177e4SLinus Torvalds 
24221da177e4SLinus Torvalds 	if (driver_data->setpolicy)
24231da177e4SLinus Torvalds 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
24241da177e4SLinus Torvalds 
24250d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
24261c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
24270d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24284dea5806SYinghai Lu 		return -EEXIST;
24291da177e4SLinus Torvalds 	}
24301c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
24310d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24321da177e4SLinus Torvalds 
24336f19efc0SLukasz Majewski 	if (cpufreq_boost_supported()) {
24346f19efc0SLukasz Majewski 		/*
24356f19efc0SLukasz Majewski 		 * Check if driver provides function to enable boost -
24366f19efc0SLukasz Majewski 		 * if not, use cpufreq_boost_set_sw as default
24376f19efc0SLukasz Majewski 		 */
24386f19efc0SLukasz Majewski 		if (!cpufreq_driver->set_boost)
24396f19efc0SLukasz Majewski 			cpufreq_driver->set_boost = cpufreq_boost_set_sw;
24406f19efc0SLukasz Majewski 
24416f19efc0SLukasz Majewski 		ret = cpufreq_sysfs_create_file(&boost.attr);
24426f19efc0SLukasz Majewski 		if (ret) {
24436f19efc0SLukasz Majewski 			pr_err("%s: cannot register global BOOST sysfs file\n",
24446f19efc0SLukasz Majewski 			       __func__);
24456f19efc0SLukasz Majewski 			goto err_null_driver;
24466f19efc0SLukasz Majewski 		}
24476f19efc0SLukasz Majewski 	}
24486f19efc0SLukasz Majewski 
24498a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
24508f5bc2abSJiri Slaby 	if (ret)
24516f19efc0SLukasz Majewski 		goto err_boost_unreg;
24521da177e4SLinus Torvalds 
24531c3d85ddSRafael J. Wysocki 	if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
24541da177e4SLinus Torvalds 		int i;
24551da177e4SLinus Torvalds 		ret = -ENODEV;
24561da177e4SLinus Torvalds 
24571da177e4SLinus Torvalds 		/* check for at least one working CPU */
24587a6aedfaSMike Travis 		for (i = 0; i < nr_cpu_ids; i++)
24597a6aedfaSMike Travis 			if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
24601da177e4SLinus Torvalds 				ret = 0;
24617a6aedfaSMike Travis 				break;
24627a6aedfaSMike Travis 			}
24631da177e4SLinus Torvalds 
24641da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
24651da177e4SLinus Torvalds 		if (ret) {
24662d06d8c4SDominik Brodowski 			pr_debug("no CPU initialized for driver %s\n",
2467e08f5f5bSGautham R Shenoy 				 driver_data->name);
24688a25a2fdSKay Sievers 			goto err_if_unreg;
24691da177e4SLinus Torvalds 		}
24701da177e4SLinus Torvalds 	}
24711da177e4SLinus Torvalds 
247265edc68cSChandra Seetharaman 	register_hotcpu_notifier(&cpufreq_cpu_notifier);
24732d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
24741da177e4SLinus Torvalds 
24758f5bc2abSJiri Slaby 	return 0;
24768a25a2fdSKay Sievers err_if_unreg:
24778a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
24786f19efc0SLukasz Majewski err_boost_unreg:
24796f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
24806f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
24818f5bc2abSJiri Slaby err_null_driver:
24820d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
24831c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
24840d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24854d34a67dSDave Jones 	return ret;
24861da177e4SLinus Torvalds }
24871da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
24881da177e4SLinus Torvalds 
24891da177e4SLinus Torvalds /**
24901da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
24911da177e4SLinus Torvalds  *
24921da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
24931da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
24941da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
24951da177e4SLinus Torvalds  * currently not initialised.
24961da177e4SLinus Torvalds  */
2497221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
24981da177e4SLinus Torvalds {
24991da177e4SLinus Torvalds 	unsigned long flags;
25001da177e4SLinus Torvalds 
25011c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
25021da177e4SLinus Torvalds 		return -EINVAL;
25031da177e4SLinus Torvalds 
25042d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
25051da177e4SLinus Torvalds 
25068a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
25076f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
25086f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
25096f19efc0SLukasz Majewski 
251065edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
25111da177e4SLinus Torvalds 
25126eed9404SViresh Kumar 	down_write(&cpufreq_rwsem);
25130d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
25146eed9404SViresh Kumar 
25151c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
25166eed9404SViresh Kumar 
25170d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
25186eed9404SViresh Kumar 	up_write(&cpufreq_rwsem);
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 	return 0;
25211da177e4SLinus Torvalds }
25221da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
25235a01f2e8SVenkatesh Pallipadi 
25245a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
25255a01f2e8SVenkatesh Pallipadi {
2526a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2527a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2528a7b422cdSKonrad Rzeszutek Wilk 
25292361be23SViresh Kumar 	cpufreq_global_kobject = kobject_create();
25308aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
25318aa84ad8SThomas Renninger 
25325a01f2e8SVenkatesh Pallipadi 	return 0;
25335a01f2e8SVenkatesh Pallipadi }
25345a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2535