xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision b4f0676fe20da6abe4577cf960862bed7a31647e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/drivers/cpufreq/cpufreq.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2001 Russell King
51da177e4SLinus Torvalds  *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6bb176f7dSViresh Kumar  *            (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
71da177e4SLinus Torvalds  *
8c32b6b8eSAshok Raj  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9c32b6b8eSAshok Raj  *	Added handling for CPU hotplug
108ff69732SDave Jones  *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
118ff69732SDave Jones  *	Fix handling for CPU hotplug -- affected CPUs
12c32b6b8eSAshok Raj  *
131da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
141da177e4SLinus Torvalds  * it under the terms of the GNU General Public License version 2 as
151da177e4SLinus Torvalds  * published by the Free Software Foundation.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
18db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19db701151SViresh Kumar 
205ff0a268SViresh Kumar #include <linux/cpu.h>
211da177e4SLinus Torvalds #include <linux/cpufreq.h>
221da177e4SLinus Torvalds #include <linux/delay.h>
231da177e4SLinus Torvalds #include <linux/device.h>
245ff0a268SViresh Kumar #include <linux/init.h>
255ff0a268SViresh Kumar #include <linux/kernel_stat.h>
265ff0a268SViresh Kumar #include <linux/module.h>
273fc54d37Sakpm@osdl.org #include <linux/mutex.h>
285ff0a268SViresh Kumar #include <linux/slab.h>
292f0aea93SViresh Kumar #include <linux/suspend.h>
3090de2a4aSDoug Anderson #include <linux/syscore_ops.h>
315ff0a268SViresh Kumar #include <linux/tick.h>
326f4f2723SThomas Renninger #include <trace/events/power.h>
336f4f2723SThomas Renninger 
34*b4f0676fSViresh Kumar /* Macros to iterate over lists */
35*b4f0676fSViresh Kumar /* Iterate over online CPUs policies */
36*b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list);
37*b4f0676fSViresh Kumar #define for_each_policy(__policy)				\
38*b4f0676fSViresh Kumar 	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
39*b4f0676fSViresh Kumar 
401da177e4SLinus Torvalds /**
41cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
421da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
431da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
441da177e4SLinus Torvalds  */
451c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
467a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
478414809cSSrivatsa S. Bhat static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
48bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
496f1e4efdSJane Li DEFINE_MUTEX(cpufreq_governor_lock);
50bb176f7dSViresh Kumar 
51084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */
52e77b89f1SDmitry Monakhov static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
531da177e4SLinus Torvalds 
542f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */
552f0aea93SViresh Kumar static bool cpufreq_suspended;
561da177e4SLinus Torvalds 
579c0ebcf7SViresh Kumar static inline bool has_target(void)
589c0ebcf7SViresh Kumar {
599c0ebcf7SViresh Kumar 	return cpufreq_driver->target_index || cpufreq_driver->target;
609c0ebcf7SViresh Kumar }
619c0ebcf7SViresh Kumar 
625a01f2e8SVenkatesh Pallipadi /*
636eed9404SViresh Kumar  * rwsem to guarantee that cpufreq driver module doesn't unload during critical
646eed9404SViresh Kumar  * sections
656eed9404SViresh Kumar  */
666eed9404SViresh Kumar static DECLARE_RWSEM(cpufreq_rwsem);
676eed9404SViresh Kumar 
681da177e4SLinus Torvalds /* internal prototypes */
6929464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy,
7029464f28SDave Jones 		unsigned int event);
71d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
7265f27f38SDavid Howells static void handle_update(struct work_struct *work);
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds /**
751da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
761da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
771da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
781da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
791da177e4SLinus Torvalds  * The mutex locks both lists.
801da177e4SLinus Torvalds  */
81e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
82b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list;
831da177e4SLinus Torvalds 
8474212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called;
85b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void)
86b4dfdbb3SAlan Stern {
87b4dfdbb3SAlan Stern 	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
8874212ca4SCesar Eduardo Barros 	init_cpufreq_transition_notifier_list_called = true;
89b4dfdbb3SAlan Stern 	return 0;
90b4dfdbb3SAlan Stern }
91b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list);
921da177e4SLinus Torvalds 
93a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
94da584455SViresh Kumar static int cpufreq_disabled(void)
95a7b422cdSKonrad Rzeszutek Wilk {
96a7b422cdSKonrad Rzeszutek Wilk 	return off;
97a7b422cdSKonrad Rzeszutek Wilk }
98a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
99a7b422cdSKonrad Rzeszutek Wilk {
100a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
101a7b422cdSKonrad Rzeszutek Wilk }
1021da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list);
1033fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
1041da177e4SLinus Torvalds 
1054d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1064d5dcc42SViresh Kumar {
1070b981e70SViresh Kumar 	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
1084d5dcc42SViresh Kumar }
1093f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1104d5dcc42SViresh Kumar 
111944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
112944e9a03SViresh Kumar {
113944e9a03SViresh Kumar 	if (have_governor_per_policy())
114944e9a03SViresh Kumar 		return &policy->kobj;
115944e9a03SViresh Kumar 	else
116944e9a03SViresh Kumar 		return cpufreq_global_kobject;
117944e9a03SViresh Kumar }
118944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
119944e9a03SViresh Kumar 
12072a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
12172a4ce34SViresh Kumar {
12272a4ce34SViresh Kumar 	u64 idle_time;
12372a4ce34SViresh Kumar 	u64 cur_wall_time;
12472a4ce34SViresh Kumar 	u64 busy_time;
12572a4ce34SViresh Kumar 
12672a4ce34SViresh Kumar 	cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
12772a4ce34SViresh Kumar 
12872a4ce34SViresh Kumar 	busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
12972a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
13072a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
13172a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
13272a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
13372a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
13472a4ce34SViresh Kumar 
13572a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
13672a4ce34SViresh Kumar 	if (wall)
13772a4ce34SViresh Kumar 		*wall = cputime_to_usecs(cur_wall_time);
13872a4ce34SViresh Kumar 
13972a4ce34SViresh Kumar 	return cputime_to_usecs(idle_time);
14072a4ce34SViresh Kumar }
14172a4ce34SViresh Kumar 
14272a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
14372a4ce34SViresh Kumar {
14472a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
14572a4ce34SViresh Kumar 
14672a4ce34SViresh Kumar 	if (idle_time == -1ULL)
14772a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
14872a4ce34SViresh Kumar 	else if (!io_busy)
14972a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
15072a4ce34SViresh Kumar 
15172a4ce34SViresh Kumar 	return idle_time;
15272a4ce34SViresh Kumar }
15372a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
15472a4ce34SViresh Kumar 
15570e9e778SViresh Kumar /*
15670e9e778SViresh Kumar  * This is a generic cpufreq init() routine which can be used by cpufreq
15770e9e778SViresh Kumar  * drivers of SMP systems. It will do following:
15870e9e778SViresh Kumar  * - validate & show freq table passed
15970e9e778SViresh Kumar  * - set policies transition latency
16070e9e778SViresh Kumar  * - policy->cpus with all possible CPUs
16170e9e778SViresh Kumar  */
16270e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy,
16370e9e778SViresh Kumar 		struct cpufreq_frequency_table *table,
16470e9e778SViresh Kumar 		unsigned int transition_latency)
16570e9e778SViresh Kumar {
16670e9e778SViresh Kumar 	int ret;
16770e9e778SViresh Kumar 
16870e9e778SViresh Kumar 	ret = cpufreq_table_validate_and_show(policy, table);
16970e9e778SViresh Kumar 	if (ret) {
17070e9e778SViresh Kumar 		pr_err("%s: invalid frequency table: %d\n", __func__, ret);
17170e9e778SViresh Kumar 		return ret;
17270e9e778SViresh Kumar 	}
17370e9e778SViresh Kumar 
17470e9e778SViresh Kumar 	policy->cpuinfo.transition_latency = transition_latency;
17570e9e778SViresh Kumar 
17670e9e778SViresh Kumar 	/*
17770e9e778SViresh Kumar 	 * The driver only supports the SMP configuartion where all processors
17870e9e778SViresh Kumar 	 * share the clock and voltage and clock.
17970e9e778SViresh Kumar 	 */
18070e9e778SViresh Kumar 	cpumask_setall(policy->cpus);
18170e9e778SViresh Kumar 
18270e9e778SViresh Kumar 	return 0;
18370e9e778SViresh Kumar }
18470e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init);
18570e9e778SViresh Kumar 
186652ed95dSViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu)
187652ed95dSViresh Kumar {
188652ed95dSViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
189652ed95dSViresh Kumar 
190652ed95dSViresh Kumar 	if (!policy || IS_ERR(policy->clk)) {
191e837f9b5SJoe Perches 		pr_err("%s: No %s associated to cpu: %d\n",
192e837f9b5SJoe Perches 		       __func__, policy ? "clk" : "policy", cpu);
193652ed95dSViresh Kumar 		return 0;
194652ed95dSViresh Kumar 	}
195652ed95dSViresh Kumar 
196652ed95dSViresh Kumar 	return clk_get_rate(policy->clk) / 1000;
197652ed95dSViresh Kumar }
198652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get);
199652ed95dSViresh Kumar 
200e0b3165bSViresh Kumar /* Only for cpufreq core internal use */
201e0b3165bSViresh Kumar struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
202e0b3165bSViresh Kumar {
203e0b3165bSViresh Kumar 	return per_cpu(cpufreq_cpu_data, cpu);
204e0b3165bSViresh Kumar }
205e0b3165bSViresh Kumar 
2066eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
2071da177e4SLinus Torvalds {
2086eed9404SViresh Kumar 	struct cpufreq_policy *policy = NULL;
2091da177e4SLinus Torvalds 	unsigned long flags;
2101da177e4SLinus Torvalds 
2111e63eaf0SViresh Kumar 	if (cpu >= nr_cpu_ids)
2126eed9404SViresh Kumar 		return NULL;
2136eed9404SViresh Kumar 
2146eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
2156eed9404SViresh Kumar 		return NULL;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/* get the cpufreq driver */
2180d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
2191da177e4SLinus Torvalds 
2206eed9404SViresh Kumar 	if (cpufreq_driver) {
2211da177e4SLinus Torvalds 		/* get the CPU */
2223a3e9e06SViresh Kumar 		policy = per_cpu(cpufreq_cpu_data, cpu);
2236eed9404SViresh Kumar 		if (policy)
2246eed9404SViresh Kumar 			kobject_get(&policy->kobj);
2256eed9404SViresh Kumar 	}
2266eed9404SViresh Kumar 
2276eed9404SViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2281da177e4SLinus Torvalds 
2293a3e9e06SViresh Kumar 	if (!policy)
2306eed9404SViresh Kumar 		up_read(&cpufreq_rwsem);
2311da177e4SLinus Torvalds 
2323a3e9e06SViresh Kumar 	return policy;
233a9144436SStephen Boyd }
2341da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2351da177e4SLinus Torvalds 
2363a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
237a9144436SStephen Boyd {
2386eed9404SViresh Kumar 	kobject_put(&policy->kobj);
2396eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
240a9144436SStephen Boyd }
2411da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds /*********************************************************************
2441da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2451da177e4SLinus Torvalds  *********************************************************************/
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds /**
2481da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
2491da177e4SLinus Torvalds  *
2501da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
2511da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
2521da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
2531da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
2541da177e4SLinus Torvalds  */
25539c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
25639c132eeSViresh Kumar {
2571da177e4SLinus Torvalds #ifndef CONFIG_SMP
2581da177e4SLinus Torvalds 	static unsigned long l_p_j_ref;
2591da177e4SLinus Torvalds 	static unsigned int l_p_j_ref_freq;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
2621da177e4SLinus Torvalds 		return;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
2651da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
2661da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
267e837f9b5SJoe Perches 		pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
268e837f9b5SJoe Perches 			 l_p_j_ref, l_p_j_ref_freq);
2691da177e4SLinus Torvalds 	}
2700b443eadSViresh Kumar 	if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
271e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
272e08f5f5bSGautham R Shenoy 								ci->new);
273e837f9b5SJoe Perches 		pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
274e837f9b5SJoe Perches 			 loops_per_jiffy, ci->new);
2751da177e4SLinus Torvalds 	}
2761da177e4SLinus Torvalds #endif
27739c132eeSViresh Kumar }
2781da177e4SLinus Torvalds 
2790956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
280b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
2831da177e4SLinus Torvalds 
284d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
285d5aaffa9SDirk Brandewie 		return;
286d5aaffa9SDirk Brandewie 
2871c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
2882d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
289e4472cb3SDave Jones 		 state, freqs->new);
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	switch (state) {
292e4472cb3SDave Jones 
2931da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
294e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
295e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
296e4472cb3SDave Jones 		 * "old frequency".
2971da177e4SLinus Torvalds 		 */
2981c3d85ddSRafael J. Wysocki 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
299e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
300e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
301e837f9b5SJoe Perches 				pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
302e4472cb3SDave Jones 					 freqs->old, policy->cur);
303e4472cb3SDave Jones 				freqs->old = policy->cur;
3041da177e4SLinus Torvalds 			}
3051da177e4SLinus Torvalds 		}
306b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
307e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
3081da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3091da177e4SLinus Torvalds 		break;
310e4472cb3SDave Jones 
3111da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3121da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
313e837f9b5SJoe Perches 		pr_debug("FREQ: %lu - CPU: %lu\n",
314e837f9b5SJoe Perches 			 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
31525e41933SThomas Renninger 		trace_cpu_frequency(freqs->new, freqs->cpu);
316b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
317e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
318e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
319e4472cb3SDave Jones 			policy->cur = freqs->new;
3201da177e4SLinus Torvalds 		break;
3211da177e4SLinus Torvalds 	}
3221da177e4SLinus Torvalds }
323bb176f7dSViresh Kumar 
324b43a7ffbSViresh Kumar /**
325b43a7ffbSViresh Kumar  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
326b43a7ffbSViresh Kumar  * on frequency transition.
327b43a7ffbSViresh Kumar  *
328b43a7ffbSViresh Kumar  * This function calls the transition notifiers and the "adjust_jiffies"
329b43a7ffbSViresh Kumar  * function. It is called twice on all CPU frequency changes that have
330b43a7ffbSViresh Kumar  * external effects.
331b43a7ffbSViresh Kumar  */
332236a9800SViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy,
333b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
334b43a7ffbSViresh Kumar {
335b43a7ffbSViresh Kumar 	for_each_cpu(freqs->cpu, policy->cpus)
336b43a7ffbSViresh Kumar 		__cpufreq_notify_transition(policy, freqs, state);
337b43a7ffbSViresh Kumar }
3381da177e4SLinus Torvalds 
339f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */
340236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
341f7ba3b41SViresh Kumar 		struct cpufreq_freqs *freqs, int transition_failed)
342f7ba3b41SViresh Kumar {
343f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
344f7ba3b41SViresh Kumar 	if (!transition_failed)
345f7ba3b41SViresh Kumar 		return;
346f7ba3b41SViresh Kumar 
347f7ba3b41SViresh Kumar 	swap(freqs->old, freqs->new);
348f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
349f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
350f7ba3b41SViresh Kumar }
351f7ba3b41SViresh Kumar 
35212478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
35312478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs)
35412478cf0SSrivatsa S. Bhat {
355ca654dc3SSrivatsa S. Bhat 
356ca654dc3SSrivatsa S. Bhat 	/*
357ca654dc3SSrivatsa S. Bhat 	 * Catch double invocations of _begin() which lead to self-deadlock.
358ca654dc3SSrivatsa S. Bhat 	 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
359ca654dc3SSrivatsa S. Bhat 	 * doesn't invoke _begin() on their behalf, and hence the chances of
360ca654dc3SSrivatsa S. Bhat 	 * double invocations are very low. Moreover, there are scenarios
361ca654dc3SSrivatsa S. Bhat 	 * where these checks can emit false-positive warnings in these
362ca654dc3SSrivatsa S. Bhat 	 * drivers; so we avoid that by skipping them altogether.
363ca654dc3SSrivatsa S. Bhat 	 */
364ca654dc3SSrivatsa S. Bhat 	WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
365ca654dc3SSrivatsa S. Bhat 				&& current == policy->transition_task);
366ca654dc3SSrivatsa S. Bhat 
36712478cf0SSrivatsa S. Bhat wait:
36812478cf0SSrivatsa S. Bhat 	wait_event(policy->transition_wait, !policy->transition_ongoing);
36912478cf0SSrivatsa S. Bhat 
37012478cf0SSrivatsa S. Bhat 	spin_lock(&policy->transition_lock);
37112478cf0SSrivatsa S. Bhat 
37212478cf0SSrivatsa S. Bhat 	if (unlikely(policy->transition_ongoing)) {
37312478cf0SSrivatsa S. Bhat 		spin_unlock(&policy->transition_lock);
37412478cf0SSrivatsa S. Bhat 		goto wait;
37512478cf0SSrivatsa S. Bhat 	}
37612478cf0SSrivatsa S. Bhat 
37712478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = true;
378ca654dc3SSrivatsa S. Bhat 	policy->transition_task = current;
37912478cf0SSrivatsa S. Bhat 
38012478cf0SSrivatsa S. Bhat 	spin_unlock(&policy->transition_lock);
38112478cf0SSrivatsa S. Bhat 
38212478cf0SSrivatsa S. Bhat 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
38312478cf0SSrivatsa S. Bhat }
38412478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
38512478cf0SSrivatsa S. Bhat 
38612478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
38712478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs, int transition_failed)
38812478cf0SSrivatsa S. Bhat {
38912478cf0SSrivatsa S. Bhat 	if (unlikely(WARN_ON(!policy->transition_ongoing)))
39012478cf0SSrivatsa S. Bhat 		return;
39112478cf0SSrivatsa S. Bhat 
39212478cf0SSrivatsa S. Bhat 	cpufreq_notify_post_transition(policy, freqs, transition_failed);
39312478cf0SSrivatsa S. Bhat 
39412478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = false;
395ca654dc3SSrivatsa S. Bhat 	policy->transition_task = NULL;
39612478cf0SSrivatsa S. Bhat 
39712478cf0SSrivatsa S. Bhat 	wake_up(&policy->transition_wait);
39812478cf0SSrivatsa S. Bhat }
39912478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
40012478cf0SSrivatsa S. Bhat 
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /*********************************************************************
4031da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
4041da177e4SLinus Torvalds  *********************************************************************/
4058a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj,
4066f19efc0SLukasz Majewski 				 struct attribute *attr, char *buf)
4076f19efc0SLukasz Majewski {
4086f19efc0SLukasz Majewski 	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
4096f19efc0SLukasz Majewski }
4106f19efc0SLukasz Majewski 
4116f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
4126f19efc0SLukasz Majewski 				  const char *buf, size_t count)
4136f19efc0SLukasz Majewski {
4146f19efc0SLukasz Majewski 	int ret, enable;
4156f19efc0SLukasz Majewski 
4166f19efc0SLukasz Majewski 	ret = sscanf(buf, "%d", &enable);
4176f19efc0SLukasz Majewski 	if (ret != 1 || enable < 0 || enable > 1)
4186f19efc0SLukasz Majewski 		return -EINVAL;
4196f19efc0SLukasz Majewski 
4206f19efc0SLukasz Majewski 	if (cpufreq_boost_trigger_state(enable)) {
421e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST!\n",
422e837f9b5SJoe Perches 		       __func__, enable ? "enable" : "disable");
4236f19efc0SLukasz Majewski 		return -EINVAL;
4246f19efc0SLukasz Majewski 	}
4256f19efc0SLukasz Majewski 
426e837f9b5SJoe Perches 	pr_debug("%s: cpufreq BOOST %s\n",
427e837f9b5SJoe Perches 		 __func__, enable ? "enabled" : "disabled");
4286f19efc0SLukasz Majewski 
4296f19efc0SLukasz Majewski 	return count;
4306f19efc0SLukasz Majewski }
4316f19efc0SLukasz Majewski define_one_global_rw(boost);
4321da177e4SLinus Torvalds 
43342f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor)
4343bcb09a3SJeremy Fitzhardinge {
4353bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
4363bcb09a3SJeremy Fitzhardinge 
4373bcb09a3SJeremy Fitzhardinge 	list_for_each_entry(t, &cpufreq_governor_list, governor_list)
4387c4f4539SRasmus Villemoes 		if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
4393bcb09a3SJeremy Fitzhardinge 			return t;
4403bcb09a3SJeremy Fitzhardinge 
4413bcb09a3SJeremy Fitzhardinge 	return NULL;
4423bcb09a3SJeremy Fitzhardinge }
4433bcb09a3SJeremy Fitzhardinge 
4441da177e4SLinus Torvalds /**
4451da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
4461da177e4SLinus Torvalds  */
4471da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
4481da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
4491da177e4SLinus Torvalds {
4503bcb09a3SJeremy Fitzhardinge 	int err = -EINVAL;
4513bcb09a3SJeremy Fitzhardinge 
4521c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver)
4533bcb09a3SJeremy Fitzhardinge 		goto out;
4543bcb09a3SJeremy Fitzhardinge 
4551c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
4567c4f4539SRasmus Villemoes 		if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
4571da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
4583bcb09a3SJeremy Fitzhardinge 			err = 0;
4597c4f4539SRasmus Villemoes 		} else if (!strncasecmp(str_governor, "powersave",
460e08f5f5bSGautham R Shenoy 						CPUFREQ_NAME_LEN)) {
4611da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
4623bcb09a3SJeremy Fitzhardinge 			err = 0;
4631da177e4SLinus Torvalds 		}
4642e1cc3a5SViresh Kumar 	} else {
4651da177e4SLinus Torvalds 		struct cpufreq_governor *t;
4663bcb09a3SJeremy Fitzhardinge 
4673fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
4683bcb09a3SJeremy Fitzhardinge 
46942f91fa1SViresh Kumar 		t = find_governor(str_governor);
4703bcb09a3SJeremy Fitzhardinge 
471ea714970SJeremy Fitzhardinge 		if (t == NULL) {
472ea714970SJeremy Fitzhardinge 			int ret;
473ea714970SJeremy Fitzhardinge 
474ea714970SJeremy Fitzhardinge 			mutex_unlock(&cpufreq_governor_mutex);
4751a8e1463SKees Cook 			ret = request_module("cpufreq_%s", str_governor);
476ea714970SJeremy Fitzhardinge 			mutex_lock(&cpufreq_governor_mutex);
477ea714970SJeremy Fitzhardinge 
478ea714970SJeremy Fitzhardinge 			if (ret == 0)
47942f91fa1SViresh Kumar 				t = find_governor(str_governor);
480ea714970SJeremy Fitzhardinge 		}
481ea714970SJeremy Fitzhardinge 
4823bcb09a3SJeremy Fitzhardinge 		if (t != NULL) {
4831da177e4SLinus Torvalds 			*governor = t;
4843bcb09a3SJeremy Fitzhardinge 			err = 0;
4851da177e4SLinus Torvalds 		}
4863bcb09a3SJeremy Fitzhardinge 
4873bcb09a3SJeremy Fitzhardinge 		mutex_unlock(&cpufreq_governor_mutex);
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds out:
4903bcb09a3SJeremy Fitzhardinge 	return err;
4911da177e4SLinus Torvalds }
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds /**
494e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
495e08f5f5bSGautham R Shenoy  * print out cpufreq information
4961da177e4SLinus Torvalds  *
4971da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
4981da177e4SLinus Torvalds  * "unsigned int".
4991da177e4SLinus Torvalds  */
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds #define show_one(file_name, object)			\
5021da177e4SLinus Torvalds static ssize_t show_##file_name				\
5031da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
5041da177e4SLinus Torvalds {							\
5051da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
5061da177e4SLinus Torvalds }
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
5091da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
510ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
5111da177e4SLinus Torvalds show_one(scaling_min_freq, min);
5121da177e4SLinus Torvalds show_one(scaling_max_freq, max);
513c034b02eSDirk Brandewie 
51409347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
515c034b02eSDirk Brandewie {
516c034b02eSDirk Brandewie 	ssize_t ret;
517c034b02eSDirk Brandewie 
518c034b02eSDirk Brandewie 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
519c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
520c034b02eSDirk Brandewie 	else
521c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", policy->cur);
522c034b02eSDirk Brandewie 	return ret;
523c034b02eSDirk Brandewie }
5241da177e4SLinus Torvalds 
525037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
5263a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy);
5277970e08bSThomas Renninger 
5281da177e4SLinus Torvalds /**
5291da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
5301da177e4SLinus Torvalds  */
5311da177e4SLinus Torvalds #define store_one(file_name, object)			\
5321da177e4SLinus Torvalds static ssize_t store_##file_name					\
5331da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
5341da177e4SLinus Torvalds {									\
535619c144cSVince Hsu 	int ret, temp;							\
5361da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
5371da177e4SLinus Torvalds 									\
5381da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
5391da177e4SLinus Torvalds 	if (ret)							\
5401da177e4SLinus Torvalds 		return -EINVAL;						\
5411da177e4SLinus Torvalds 									\
5421da177e4SLinus Torvalds 	ret = sscanf(buf, "%u", &new_policy.object);			\
5431da177e4SLinus Torvalds 	if (ret != 1)							\
5441da177e4SLinus Torvalds 		return -EINVAL;						\
5451da177e4SLinus Torvalds 									\
546619c144cSVince Hsu 	temp = new_policy.object;					\
547037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);		\
548619c144cSVince Hsu 	if (!ret)							\
549619c144cSVince Hsu 		policy->user_policy.object = temp;			\
5501da177e4SLinus Torvalds 									\
5511da177e4SLinus Torvalds 	return ret ? ret : count;					\
5521da177e4SLinus Torvalds }
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds store_one(scaling_min_freq, min);
5551da177e4SLinus Torvalds store_one(scaling_max_freq, max);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds /**
5581da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
5591da177e4SLinus Torvalds  */
560e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
561e08f5f5bSGautham R Shenoy 					char *buf)
5621da177e4SLinus Torvalds {
563d92d50a4SViresh Kumar 	unsigned int cur_freq = __cpufreq_get(policy);
5641da177e4SLinus Torvalds 	if (!cur_freq)
5651da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
5661da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds /**
5701da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
5711da177e4SLinus Torvalds  */
572905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
5731da177e4SLinus Torvalds {
5741da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
5751da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
5761da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
5771da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
5781da177e4SLinus Torvalds 	else if (policy->governor)
5794b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
58029464f28SDave Jones 				policy->governor->name);
5811da177e4SLinus Torvalds 	return -EINVAL;
5821da177e4SLinus Torvalds }
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds /**
5851da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
5861da177e4SLinus Torvalds  */
5871da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
5881da177e4SLinus Torvalds 					const char *buf, size_t count)
5891da177e4SLinus Torvalds {
5905136fa56SSrivatsa S. Bhat 	int ret;
5911da177e4SLinus Torvalds 	char	str_governor[16];
5921da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);
5951da177e4SLinus Torvalds 	if (ret)
5961da177e4SLinus Torvalds 		return ret;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
5991da177e4SLinus Torvalds 	if (ret != 1)
6001da177e4SLinus Torvalds 		return -EINVAL;
6011da177e4SLinus Torvalds 
602e08f5f5bSGautham R Shenoy 	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
603e08f5f5bSGautham R Shenoy 						&new_policy.governor))
6041da177e4SLinus Torvalds 		return -EINVAL;
6051da177e4SLinus Torvalds 
606037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
6077970e08bSThomas Renninger 
6087970e08bSThomas Renninger 	policy->user_policy.policy = policy->policy;
6097970e08bSThomas Renninger 	policy->user_policy.governor = policy->governor;
6107970e08bSThomas Renninger 
611e08f5f5bSGautham R Shenoy 	if (ret)
612e08f5f5bSGautham R Shenoy 		return ret;
613e08f5f5bSGautham R Shenoy 	else
614e08f5f5bSGautham R Shenoy 		return count;
6151da177e4SLinus Torvalds }
6161da177e4SLinus Torvalds 
6171da177e4SLinus Torvalds /**
6181da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
6191da177e4SLinus Torvalds  */
6201da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
6211da177e4SLinus Torvalds {
6221c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
6251da177e4SLinus Torvalds /**
6261da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
6271da177e4SLinus Torvalds  */
6281da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
6291da177e4SLinus Torvalds 						char *buf)
6301da177e4SLinus Torvalds {
6311da177e4SLinus Torvalds 	ssize_t i = 0;
6321da177e4SLinus Torvalds 	struct cpufreq_governor *t;
6331da177e4SLinus Torvalds 
6349c0ebcf7SViresh Kumar 	if (!has_target()) {
6351da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
6361da177e4SLinus Torvalds 		goto out;
6371da177e4SLinus Torvalds 	}
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
64029464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
64129464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
6421da177e4SLinus Torvalds 			goto out;
6434b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
6441da177e4SLinus Torvalds 	}
6451da177e4SLinus Torvalds out:
6461da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
6471da177e4SLinus Torvalds 	return i;
6481da177e4SLinus Torvalds }
649e8628dd0SDarrick J. Wong 
650f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
6511da177e4SLinus Torvalds {
6521da177e4SLinus Torvalds 	ssize_t i = 0;
6531da177e4SLinus Torvalds 	unsigned int cpu;
6541da177e4SLinus Torvalds 
655835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
6561da177e4SLinus Torvalds 		if (i)
6571da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
6581da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
6591da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
6601da177e4SLinus Torvalds 			break;
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
6631da177e4SLinus Torvalds 	return i;
6641da177e4SLinus Torvalds }
665f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
6661da177e4SLinus Torvalds 
667e8628dd0SDarrick J. Wong /**
668e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
669e8628dd0SDarrick J. Wong  * hw coordination is in use
670e8628dd0SDarrick J. Wong  */
671e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
672e8628dd0SDarrick J. Wong {
673f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
674e8628dd0SDarrick J. Wong }
675e8628dd0SDarrick J. Wong 
676e8628dd0SDarrick J. Wong /**
677e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
678e8628dd0SDarrick J. Wong  */
679e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
680e8628dd0SDarrick J. Wong {
681f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
682e8628dd0SDarrick J. Wong }
683e8628dd0SDarrick J. Wong 
6849e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
6859e76988eSVenki Pallipadi 					const char *buf, size_t count)
6869e76988eSVenki Pallipadi {
6879e76988eSVenki Pallipadi 	unsigned int freq = 0;
6889e76988eSVenki Pallipadi 	unsigned int ret;
6899e76988eSVenki Pallipadi 
690879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
6919e76988eSVenki Pallipadi 		return -EINVAL;
6929e76988eSVenki Pallipadi 
6939e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
6949e76988eSVenki Pallipadi 	if (ret != 1)
6959e76988eSVenki Pallipadi 		return -EINVAL;
6969e76988eSVenki Pallipadi 
6979e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
6989e76988eSVenki Pallipadi 
6999e76988eSVenki Pallipadi 	return count;
7009e76988eSVenki Pallipadi }
7019e76988eSVenki Pallipadi 
7029e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
7039e76988eSVenki Pallipadi {
704879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
7059e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
7069e76988eSVenki Pallipadi 
7079e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
7089e76988eSVenki Pallipadi }
7091da177e4SLinus Torvalds 
710e2f74f35SThomas Renninger /**
7118bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
712e2f74f35SThomas Renninger  */
713e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
714e2f74f35SThomas Renninger {
715e2f74f35SThomas Renninger 	unsigned int limit;
716e2f74f35SThomas Renninger 	int ret;
7171c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
7181c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
719e2f74f35SThomas Renninger 		if (!ret)
720e2f74f35SThomas Renninger 			return sprintf(buf, "%u\n", limit);
721e2f74f35SThomas Renninger 	}
722e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
723e2f74f35SThomas Renninger }
724e2f74f35SThomas Renninger 
7256dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
7266dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
7276dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
7286dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
7296dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
7306dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
7316dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
7326dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
7336dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
7346dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
7356dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
7366dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
7376dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
7386dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds static struct attribute *default_attrs[] = {
7411da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
7421da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
743ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
7441da177e4SLinus Torvalds 	&scaling_min_freq.attr,
7451da177e4SLinus Torvalds 	&scaling_max_freq.attr,
7461da177e4SLinus Torvalds 	&affected_cpus.attr,
747e8628dd0SDarrick J. Wong 	&related_cpus.attr,
7481da177e4SLinus Torvalds 	&scaling_governor.attr,
7491da177e4SLinus Torvalds 	&scaling_driver.attr,
7501da177e4SLinus Torvalds 	&scaling_available_governors.attr,
7519e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
7521da177e4SLinus Torvalds 	NULL
7531da177e4SLinus Torvalds };
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
7561da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
7591da177e4SLinus Torvalds {
7601da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7611da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
7621b750e3bSViresh Kumar 	ssize_t ret;
7636eed9404SViresh Kumar 
7646eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
7651b750e3bSViresh Kumar 		return -EINVAL;
7665a01f2e8SVenkatesh Pallipadi 
767ad7722daSviresh kumar 	down_read(&policy->rwsem);
7685a01f2e8SVenkatesh Pallipadi 
769e08f5f5bSGautham R Shenoy 	if (fattr->show)
770e08f5f5bSGautham R Shenoy 		ret = fattr->show(policy, buf);
771e08f5f5bSGautham R Shenoy 	else
772e08f5f5bSGautham R Shenoy 		ret = -EIO;
773e08f5f5bSGautham R Shenoy 
774ad7722daSviresh kumar 	up_read(&policy->rwsem);
7756eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
7761b750e3bSViresh Kumar 
7771da177e4SLinus Torvalds 	return ret;
7781da177e4SLinus Torvalds }
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
7811da177e4SLinus Torvalds 		     const char *buf, size_t count)
7821da177e4SLinus Torvalds {
7831da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7841da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
785a07530b4SDave Jones 	ssize_t ret = -EINVAL;
7866eed9404SViresh Kumar 
7874f750c93SSrivatsa S. Bhat 	get_online_cpus();
7884f750c93SSrivatsa S. Bhat 
7894f750c93SSrivatsa S. Bhat 	if (!cpu_online(policy->cpu))
7904f750c93SSrivatsa S. Bhat 		goto unlock;
7914f750c93SSrivatsa S. Bhat 
7926eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
7934f750c93SSrivatsa S. Bhat 		goto unlock;
7945a01f2e8SVenkatesh Pallipadi 
795ad7722daSviresh kumar 	down_write(&policy->rwsem);
7965a01f2e8SVenkatesh Pallipadi 
797e08f5f5bSGautham R Shenoy 	if (fattr->store)
798e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
799e08f5f5bSGautham R Shenoy 	else
800e08f5f5bSGautham R Shenoy 		ret = -EIO;
801e08f5f5bSGautham R Shenoy 
802ad7722daSviresh kumar 	up_write(&policy->rwsem);
8036eed9404SViresh Kumar 
8046eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
8054f750c93SSrivatsa S. Bhat unlock:
8064f750c93SSrivatsa S. Bhat 	put_online_cpus();
8074f750c93SSrivatsa S. Bhat 
8081da177e4SLinus Torvalds 	return ret;
8091da177e4SLinus Torvalds }
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
8121da177e4SLinus Torvalds {
8131da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
8142d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
8151da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
81852cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
8191da177e4SLinus Torvalds 	.show	= show,
8201da177e4SLinus Torvalds 	.store	= store,
8211da177e4SLinus Torvalds };
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
8241da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
8251da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
8261da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
8271da177e4SLinus Torvalds };
8281da177e4SLinus Torvalds 
8292361be23SViresh Kumar struct kobject *cpufreq_global_kobject;
8302361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject);
8312361be23SViresh Kumar 
8322361be23SViresh Kumar static int cpufreq_global_kobject_usage;
8332361be23SViresh Kumar 
8342361be23SViresh Kumar int cpufreq_get_global_kobject(void)
8352361be23SViresh Kumar {
8362361be23SViresh Kumar 	if (!cpufreq_global_kobject_usage++)
8372361be23SViresh Kumar 		return kobject_add(cpufreq_global_kobject,
8382361be23SViresh Kumar 				&cpu_subsys.dev_root->kobj, "%s", "cpufreq");
8392361be23SViresh Kumar 
8402361be23SViresh Kumar 	return 0;
8412361be23SViresh Kumar }
8422361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject);
8432361be23SViresh Kumar 
8442361be23SViresh Kumar void cpufreq_put_global_kobject(void)
8452361be23SViresh Kumar {
8462361be23SViresh Kumar 	if (!--cpufreq_global_kobject_usage)
8472361be23SViresh Kumar 		kobject_del(cpufreq_global_kobject);
8482361be23SViresh Kumar }
8492361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject);
8502361be23SViresh Kumar 
8512361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr)
8522361be23SViresh Kumar {
8532361be23SViresh Kumar 	int ret = cpufreq_get_global_kobject();
8542361be23SViresh Kumar 
8552361be23SViresh Kumar 	if (!ret) {
8562361be23SViresh Kumar 		ret = sysfs_create_file(cpufreq_global_kobject, attr);
8572361be23SViresh Kumar 		if (ret)
8582361be23SViresh Kumar 			cpufreq_put_global_kobject();
8592361be23SViresh Kumar 	}
8602361be23SViresh Kumar 
8612361be23SViresh Kumar 	return ret;
8622361be23SViresh Kumar }
8632361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file);
8642361be23SViresh Kumar 
8652361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr)
8662361be23SViresh Kumar {
8672361be23SViresh Kumar 	sysfs_remove_file(cpufreq_global_kobject, attr);
8682361be23SViresh Kumar 	cpufreq_put_global_kobject();
8692361be23SViresh Kumar }
8702361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
8712361be23SViresh Kumar 
87219d6f7ecSDave Jones /* symlink affected CPUs */
873308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
87419d6f7ecSDave Jones {
87519d6f7ecSDave Jones 	unsigned int j;
87619d6f7ecSDave Jones 	int ret = 0;
87719d6f7ecSDave Jones 
87819d6f7ecSDave Jones 	for_each_cpu(j, policy->cpus) {
8798a25a2fdSKay Sievers 		struct device *cpu_dev;
88019d6f7ecSDave Jones 
881308b60e7SViresh Kumar 		if (j == policy->cpu)
88219d6f7ecSDave Jones 			continue;
88319d6f7ecSDave Jones 
884e8fdde10SViresh Kumar 		pr_debug("Adding link for CPU: %u\n", j);
8858a25a2fdSKay Sievers 		cpu_dev = get_cpu_device(j);
8868a25a2fdSKay Sievers 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
88719d6f7ecSDave Jones 					"cpufreq");
88871c3461eSRafael J. Wysocki 		if (ret)
88971c3461eSRafael J. Wysocki 			break;
89019d6f7ecSDave Jones 	}
89119d6f7ecSDave Jones 	return ret;
89219d6f7ecSDave Jones }
89319d6f7ecSDave Jones 
894308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
8958a25a2fdSKay Sievers 				     struct device *dev)
896909a694eSDave Jones {
897909a694eSDave Jones 	struct freq_attr **drv_attr;
898909a694eSDave Jones 	int ret = 0;
899909a694eSDave Jones 
900909a694eSDave Jones 	/* set up files for this cpu device */
9011c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
902f13f1184SViresh Kumar 	while (drv_attr && *drv_attr) {
903909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
904909a694eSDave Jones 		if (ret)
9056d4e81edSTomeu Vizoso 			return ret;
906909a694eSDave Jones 		drv_attr++;
907909a694eSDave Jones 	}
9081c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
909909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
910909a694eSDave Jones 		if (ret)
9116d4e81edSTomeu Vizoso 			return ret;
912909a694eSDave Jones 	}
913c034b02eSDirk Brandewie 
914909a694eSDave Jones 	ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
915909a694eSDave Jones 	if (ret)
9166d4e81edSTomeu Vizoso 		return ret;
917c034b02eSDirk Brandewie 
9181c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
919e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
920e2f74f35SThomas Renninger 		if (ret)
9216d4e81edSTomeu Vizoso 			return ret;
922e2f74f35SThomas Renninger 	}
923909a694eSDave Jones 
9246d4e81edSTomeu Vizoso 	return cpufreq_add_dev_symlink(policy);
925e18f1682SSrivatsa S. Bhat }
926e18f1682SSrivatsa S. Bhat 
927e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy)
928e18f1682SSrivatsa S. Bhat {
9296e2c89d1Sviresh kumar 	struct cpufreq_governor *gov = NULL;
930e18f1682SSrivatsa S. Bhat 	struct cpufreq_policy new_policy;
931e18f1682SSrivatsa S. Bhat 	int ret = 0;
932e18f1682SSrivatsa S. Bhat 
933d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
934a27a9ab7SJason Baron 
9356e2c89d1Sviresh kumar 	/* Update governor of new_policy to the governor used before hotplug */
93642f91fa1SViresh Kumar 	gov = find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu));
9376e2c89d1Sviresh kumar 	if (gov)
9386e2c89d1Sviresh kumar 		pr_debug("Restoring governor %s for cpu %d\n",
9396e2c89d1Sviresh kumar 				policy->governor->name, policy->cpu);
9406e2c89d1Sviresh kumar 	else
9416e2c89d1Sviresh kumar 		gov = CPUFREQ_DEFAULT_GOVERNOR;
9426e2c89d1Sviresh kumar 
9436e2c89d1Sviresh kumar 	new_policy.governor = gov;
9446e2c89d1Sviresh kumar 
945a27a9ab7SJason Baron 	/* Use the default policy if its valid. */
946a27a9ab7SJason Baron 	if (cpufreq_driver->setpolicy)
9476e2c89d1Sviresh kumar 		cpufreq_parse_governor(gov->name, &new_policy.policy, NULL);
948ecf7e461SDave Jones 
949ecf7e461SDave Jones 	/* set default policy */
950037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
951ecf7e461SDave Jones 	if (ret) {
9522d06d8c4SDominik Brodowski 		pr_debug("setting policy failed\n");
9531c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
9541c3d85ddSRafael J. Wysocki 			cpufreq_driver->exit(policy);
955ecf7e461SDave Jones 	}
956909a694eSDave Jones }
957909a694eSDave Jones 
958d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
95942f921a6SViresh Kumar 				  unsigned int cpu, struct device *dev)
960fcf80582SViresh Kumar {
9619c0ebcf7SViresh Kumar 	int ret = 0;
962fcf80582SViresh Kumar 	unsigned long flags;
963fcf80582SViresh Kumar 
9649c0ebcf7SViresh Kumar 	if (has_target()) {
9653de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
9663de9bdebSViresh Kumar 		if (ret) {
9673de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
9683de9bdebSViresh Kumar 			return ret;
9693de9bdebSViresh Kumar 		}
9703de9bdebSViresh Kumar 	}
971fcf80582SViresh Kumar 
972ad7722daSviresh kumar 	down_write(&policy->rwsem);
9732eaa3e2dSViresh Kumar 
9740d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
9752eaa3e2dSViresh Kumar 
976fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
977fcf80582SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = policy;
9780d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
979fcf80582SViresh Kumar 
980ad7722daSviresh kumar 	up_write(&policy->rwsem);
9812eaa3e2dSViresh Kumar 
9829c0ebcf7SViresh Kumar 	if (has_target()) {
983e5c87b76SStratos Karafotis 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
984e5c87b76SStratos Karafotis 		if (!ret)
985e5c87b76SStratos Karafotis 			ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
986e5c87b76SStratos Karafotis 
987e5c87b76SStratos Karafotis 		if (ret) {
9883de9bdebSViresh Kumar 			pr_err("%s: Failed to start governor\n", __func__);
9893de9bdebSViresh Kumar 			return ret;
9903de9bdebSViresh Kumar 		}
991820c6ca2SViresh Kumar 	}
992fcf80582SViresh Kumar 
99342f921a6SViresh Kumar 	return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
994fcf80582SViresh Kumar }
9951da177e4SLinus Torvalds 
9968414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
9978414809cSSrivatsa S. Bhat {
9988414809cSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
9998414809cSSrivatsa S. Bhat 	unsigned long flags;
10008414809cSSrivatsa S. Bhat 
100144871c9cSLan Tianyu 	read_lock_irqsave(&cpufreq_driver_lock, flags);
10028414809cSSrivatsa S. Bhat 
10038414809cSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
10048414809cSSrivatsa S. Bhat 
100544871c9cSLan Tianyu 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
10068414809cSSrivatsa S. Bhat 
100709712f55SGeert Uytterhoeven 	if (policy)
10086e2c89d1Sviresh kumar 		policy->governor = NULL;
10096e2c89d1Sviresh kumar 
10108414809cSSrivatsa S. Bhat 	return policy;
10118414809cSSrivatsa S. Bhat }
10128414809cSSrivatsa S. Bhat 
1013e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void)
1014e9698cc5SSrivatsa S. Bhat {
1015e9698cc5SSrivatsa S. Bhat 	struct cpufreq_policy *policy;
1016e9698cc5SSrivatsa S. Bhat 
1017e9698cc5SSrivatsa S. Bhat 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1018e9698cc5SSrivatsa S. Bhat 	if (!policy)
1019e9698cc5SSrivatsa S. Bhat 		return NULL;
1020e9698cc5SSrivatsa S. Bhat 
1021e9698cc5SSrivatsa S. Bhat 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1022e9698cc5SSrivatsa S. Bhat 		goto err_free_policy;
1023e9698cc5SSrivatsa S. Bhat 
1024e9698cc5SSrivatsa S. Bhat 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1025e9698cc5SSrivatsa S. Bhat 		goto err_free_cpumask;
1026e9698cc5SSrivatsa S. Bhat 
1027c88a1f8bSLukasz Majewski 	INIT_LIST_HEAD(&policy->policy_list);
1028ad7722daSviresh kumar 	init_rwsem(&policy->rwsem);
102912478cf0SSrivatsa S. Bhat 	spin_lock_init(&policy->transition_lock);
103012478cf0SSrivatsa S. Bhat 	init_waitqueue_head(&policy->transition_wait);
1031818c5712SViresh Kumar 	init_completion(&policy->kobj_unregister);
1032818c5712SViresh Kumar 	INIT_WORK(&policy->update, handle_update);
1033ad7722daSviresh kumar 
1034e9698cc5SSrivatsa S. Bhat 	return policy;
1035e9698cc5SSrivatsa S. Bhat 
1036e9698cc5SSrivatsa S. Bhat err_free_cpumask:
1037e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1038e9698cc5SSrivatsa S. Bhat err_free_policy:
1039e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1040e9698cc5SSrivatsa S. Bhat 
1041e9698cc5SSrivatsa S. Bhat 	return NULL;
1042e9698cc5SSrivatsa S. Bhat }
1043e9698cc5SSrivatsa S. Bhat 
104442f921a6SViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
104542f921a6SViresh Kumar {
104642f921a6SViresh Kumar 	struct kobject *kobj;
104742f921a6SViresh Kumar 	struct completion *cmp;
104842f921a6SViresh Kumar 
1049fcd7af91SViresh Kumar 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1050fcd7af91SViresh Kumar 			CPUFREQ_REMOVE_POLICY, policy);
1051fcd7af91SViresh Kumar 
105242f921a6SViresh Kumar 	down_read(&policy->rwsem);
105342f921a6SViresh Kumar 	kobj = &policy->kobj;
105442f921a6SViresh Kumar 	cmp = &policy->kobj_unregister;
105542f921a6SViresh Kumar 	up_read(&policy->rwsem);
105642f921a6SViresh Kumar 	kobject_put(kobj);
105742f921a6SViresh Kumar 
105842f921a6SViresh Kumar 	/*
105942f921a6SViresh Kumar 	 * We need to make sure that the underlying kobj is
106042f921a6SViresh Kumar 	 * actually not referenced anymore by anybody before we
106142f921a6SViresh Kumar 	 * proceed with unloading.
106242f921a6SViresh Kumar 	 */
106342f921a6SViresh Kumar 	pr_debug("waiting for dropping of refcount\n");
106442f921a6SViresh Kumar 	wait_for_completion(cmp);
106542f921a6SViresh Kumar 	pr_debug("wait complete\n");
106642f921a6SViresh Kumar }
106742f921a6SViresh Kumar 
1068e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy)
1069e9698cc5SSrivatsa S. Bhat {
1070e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
1071e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1072e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1073e9698cc5SSrivatsa S. Bhat }
1074e9698cc5SSrivatsa S. Bhat 
10751bfb425bSViresh Kumar static int update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu,
10761bfb425bSViresh Kumar 			     struct device *cpu_dev)
10770d66b91eSSrivatsa S. Bhat {
10781bfb425bSViresh Kumar 	int ret;
10791bfb425bSViresh Kumar 
108099ec899eSSrivatsa S. Bhat 	if (WARN_ON(cpu == policy->cpu))
10811bfb425bSViresh Kumar 		return 0;
10821bfb425bSViresh Kumar 
10831bfb425bSViresh Kumar 	/* Move kobject to the new policy->cpu */
10841bfb425bSViresh Kumar 	ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
10851bfb425bSViresh Kumar 	if (ret) {
10861bfb425bSViresh Kumar 		pr_err("%s: Failed to move kobj: %d\n", __func__, ret);
10871bfb425bSViresh Kumar 		return ret;
10881bfb425bSViresh Kumar 	}
1089cb38ed5cSSrivatsa S. Bhat 
1090ad7722daSviresh kumar 	down_write(&policy->rwsem);
10910d66b91eSSrivatsa S. Bhat 	policy->cpu = cpu;
1092ad7722daSviresh kumar 	up_write(&policy->rwsem);
10938efd5765SViresh Kumar 
10941bfb425bSViresh Kumar 	return 0;
10950d66b91eSSrivatsa S. Bhat }
10960d66b91eSSrivatsa S. Bhat 
109796bbbe4aSViresh Kumar static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
10981da177e4SLinus Torvalds {
1099fcf80582SViresh Kumar 	unsigned int j, cpu = dev->id;
110065922465SViresh Kumar 	int ret = -ENOMEM;
11017f0c020aSViresh Kumar 	struct cpufreq_policy *policy;
11021da177e4SLinus Torvalds 	unsigned long flags;
110396bbbe4aSViresh Kumar 	bool recover_policy = cpufreq_suspended;
11041da177e4SLinus Torvalds 
1105c32b6b8eSAshok Raj 	if (cpu_is_offline(cpu))
1106c32b6b8eSAshok Raj 		return 0;
1107c32b6b8eSAshok Raj 
11082d06d8c4SDominik Brodowski 	pr_debug("adding CPU %u\n", cpu);
11091da177e4SLinus Torvalds 
11101da177e4SLinus Torvalds 	/* check whether a different CPU already registered this
11111da177e4SLinus Torvalds 	 * CPU because it is in the same boat. */
1112d7a9771cSViresh Kumar 	policy = cpufreq_cpu_get_raw(cpu);
1113d7a9771cSViresh Kumar 	if (unlikely(policy))
11141da177e4SLinus Torvalds 		return 0;
1115fcf80582SViresh Kumar 
11166eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
11176eed9404SViresh Kumar 		return 0;
11186eed9404SViresh Kumar 
1119fcf80582SViresh Kumar 	/* Check if this cpu was hot-unplugged earlier and has siblings */
11200d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1121*b4f0676fSViresh Kumar 	for_each_policy(policy) {
11227f0c020aSViresh Kumar 		if (cpumask_test_cpu(cpu, policy->related_cpus)) {
11230d1857a1SNathan Zimmer 			read_unlock_irqrestore(&cpufreq_driver_lock, flags);
11247f0c020aSViresh Kumar 			ret = cpufreq_add_policy_cpu(policy, cpu, dev);
11256eed9404SViresh Kumar 			up_read(&cpufreq_rwsem);
11266eed9404SViresh Kumar 			return ret;
1127fcf80582SViresh Kumar 		}
11282eaa3e2dSViresh Kumar 	}
11290d1857a1SNathan Zimmer 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
11301da177e4SLinus Torvalds 
113172368d12SRafael J. Wysocki 	/*
113272368d12SRafael J. Wysocki 	 * Restore the saved policy when doing light-weight init and fall back
113372368d12SRafael J. Wysocki 	 * to the full init if that fails.
113472368d12SRafael J. Wysocki 	 */
113596bbbe4aSViresh Kumar 	policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL;
113672368d12SRafael J. Wysocki 	if (!policy) {
113796bbbe4aSViresh Kumar 		recover_policy = false;
1138e9698cc5SSrivatsa S. Bhat 		policy = cpufreq_policy_alloc();
1139059019a3SDave Jones 		if (!policy)
11401da177e4SLinus Torvalds 			goto nomem_out;
114172368d12SRafael J. Wysocki 	}
11420d66b91eSSrivatsa S. Bhat 
11430d66b91eSSrivatsa S. Bhat 	/*
11440d66b91eSSrivatsa S. Bhat 	 * In the resume path, since we restore a saved policy, the assignment
11450d66b91eSSrivatsa S. Bhat 	 * to policy->cpu is like an update of the existing policy, rather than
11460d66b91eSSrivatsa S. Bhat 	 * the creation of a brand new one. So we need to perform this update
11470d66b91eSSrivatsa S. Bhat 	 * by invoking update_policy_cpu().
11480d66b91eSSrivatsa S. Bhat 	 */
11491bfb425bSViresh Kumar 	if (recover_policy && cpu != policy->cpu)
11501bfb425bSViresh Kumar 		WARN_ON(update_policy_cpu(policy, cpu, dev));
11511bfb425bSViresh Kumar 	else
11521da177e4SLinus Torvalds 		policy->cpu = cpu;
11530d66b91eSSrivatsa S. Bhat 
1154835481d9SRusty Russell 	cpumask_copy(policy->cpus, cpumask_of(cpu));
11551da177e4SLinus Torvalds 
11561da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
11571da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
11581da177e4SLinus Torvalds 	 */
11591c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->init(policy);
11601da177e4SLinus Torvalds 	if (ret) {
11612d06d8c4SDominik Brodowski 		pr_debug("initialization failed\n");
11622eaa3e2dSViresh Kumar 		goto err_set_policy_cpu;
11631da177e4SLinus Torvalds 	}
1164643ae6e8SViresh Kumar 
11656d4e81edSTomeu Vizoso 	down_write(&policy->rwsem);
11666d4e81edSTomeu Vizoso 
11675a7e56a5SViresh Kumar 	/* related cpus should atleast have policy->cpus */
11685a7e56a5SViresh Kumar 	cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
11695a7e56a5SViresh Kumar 
11705a7e56a5SViresh Kumar 	/*
11715a7e56a5SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
11725a7e56a5SViresh Kumar 	 * managing offline cpus here.
11735a7e56a5SViresh Kumar 	 */
11745a7e56a5SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
11755a7e56a5SViresh Kumar 
117696bbbe4aSViresh Kumar 	if (!recover_policy) {
11775a7e56a5SViresh Kumar 		policy->user_policy.min = policy->min;
11785a7e56a5SViresh Kumar 		policy->user_policy.max = policy->max;
11796d4e81edSTomeu Vizoso 
11806d4e81edSTomeu Vizoso 		/* prepare interface data */
11816d4e81edSTomeu Vizoso 		ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
11826d4e81edSTomeu Vizoso 					   &dev->kobj, "cpufreq");
11836d4e81edSTomeu Vizoso 		if (ret) {
11846d4e81edSTomeu Vizoso 			pr_err("%s: failed to init policy->kobj: %d\n",
11856d4e81edSTomeu Vizoso 			       __func__, ret);
11866d4e81edSTomeu Vizoso 			goto err_init_policy_kobj;
11876d4e81edSTomeu Vizoso 		}
11885a7e56a5SViresh Kumar 	}
11895a7e56a5SViresh Kumar 
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);
12677c45cf31SViresh Kumar 
12686eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
12696eed9404SViresh Kumar 
12707c45cf31SViresh Kumar 	/* Callback for handling stuff after policy is ready */
12717c45cf31SViresh Kumar 	if (cpufreq_driver->ready)
12727c45cf31SViresh Kumar 		cpufreq_driver->ready(policy);
12737c45cf31SViresh Kumar 
12742d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
12751da177e4SLinus Torvalds 
12761da177e4SLinus Torvalds 	return 0;
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds err_out_unregister:
1279652ed95dSViresh Kumar err_get_freq:
12800d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1281474deff7SViresh Kumar 	for_each_cpu(j, policy->cpus)
12827a6aedfaSMike Travis 		per_cpu(cpufreq_cpu_data, j) = NULL;
12830d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
12841da177e4SLinus Torvalds 
12856d4e81edSTomeu Vizoso 	if (!recover_policy) {
12866d4e81edSTomeu Vizoso 		kobject_put(&policy->kobj);
12876d4e81edSTomeu Vizoso 		wait_for_completion(&policy->kobj_unregister);
12886d4e81edSTomeu Vizoso 	}
12896d4e81edSTomeu Vizoso err_init_policy_kobj:
12907106e02bSPrarit Bhargava 	up_write(&policy->rwsem);
12917106e02bSPrarit Bhargava 
1292da60ce9fSViresh Kumar 	if (cpufreq_driver->exit)
1293da60ce9fSViresh Kumar 		cpufreq_driver->exit(policy);
12942eaa3e2dSViresh Kumar err_set_policy_cpu:
129596bbbe4aSViresh Kumar 	if (recover_policy) {
129672368d12SRafael J. Wysocki 		/* Do not leave stale fallback data behind. */
129772368d12SRafael J. Wysocki 		per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
129842f921a6SViresh Kumar 		cpufreq_policy_put_kobj(policy);
129972368d12SRafael J. Wysocki 	}
1300e9698cc5SSrivatsa S. Bhat 	cpufreq_policy_free(policy);
130142f921a6SViresh Kumar 
13021da177e4SLinus Torvalds nomem_out:
13036eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
13046eed9404SViresh Kumar 
13051da177e4SLinus Torvalds 	return ret;
13061da177e4SLinus Torvalds }
13071da177e4SLinus Torvalds 
1308a82fab29SSrivatsa S. Bhat /**
1309a82fab29SSrivatsa S. Bhat  * cpufreq_add_dev - add a CPU device
1310a82fab29SSrivatsa S. Bhat  *
1311a82fab29SSrivatsa S. Bhat  * Adds the cpufreq interface for a CPU device.
1312a82fab29SSrivatsa S. Bhat  *
1313a82fab29SSrivatsa S. Bhat  * The Oracle says: try running cpufreq registration/unregistration concurrently
1314a82fab29SSrivatsa S. Bhat  * with with cpu hotplugging and all hell will break loose. Tried to clean this
1315a82fab29SSrivatsa S. Bhat  * mess up, but more thorough testing is needed. - Mathieu
1316a82fab29SSrivatsa S. Bhat  */
1317a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1318a82fab29SSrivatsa S. Bhat {
131996bbbe4aSViresh Kumar 	return __cpufreq_add_dev(dev, sif);
1320a82fab29SSrivatsa S. Bhat }
1321a82fab29SSrivatsa S. Bhat 
1322cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_prepare(struct device *dev,
132396bbbe4aSViresh Kumar 					struct subsys_interface *sif)
13241da177e4SLinus Torvalds {
1325f9ba680dSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
13261bfb425bSViresh Kumar 	int ret;
13271da177e4SLinus Torvalds 	unsigned long flags;
13283a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
13291da177e4SLinus Torvalds 
1330b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
13311da177e4SLinus Torvalds 
13320d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
13331da177e4SLinus Torvalds 
13343a3e9e06SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
13351da177e4SLinus Torvalds 
13368414809cSSrivatsa S. Bhat 	/* Save the policy somewhere when doing a light-weight tear-down */
133796bbbe4aSViresh Kumar 	if (cpufreq_suspended)
13383a3e9e06SViresh Kumar 		per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
13398414809cSSrivatsa S. Bhat 
13400d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
13411da177e4SLinus Torvalds 
13423a3e9e06SViresh Kumar 	if (!policy) {
1343b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
13441da177e4SLinus Torvalds 		return -EINVAL;
13451da177e4SLinus Torvalds 	}
13461da177e4SLinus Torvalds 
13479c0ebcf7SViresh Kumar 	if (has_target()) {
13483de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
13493de9bdebSViresh Kumar 		if (ret) {
13503de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
13513de9bdebSViresh Kumar 			return ret;
13523de9bdebSViresh Kumar 		}
13535a01f2e8SVenkatesh Pallipadi 
1354fa69e33fSDirk Brandewie 		strncpy(per_cpu(cpufreq_cpu_governor, cpu),
13553a3e9e06SViresh Kumar 			policy->governor->name, CPUFREQ_NAME_LEN);
1356db5f2995SViresh Kumar 	}
13571da177e4SLinus Torvalds 
1358ad7722daSviresh kumar 	down_read(&policy->rwsem);
13593a3e9e06SViresh Kumar 	cpus = cpumask_weight(policy->cpus);
1360ad7722daSviresh kumar 	up_read(&policy->rwsem);
13611da177e4SLinus Torvalds 
136261173f25SSrivatsa S. Bhat 	if (cpu != policy->cpu) {
136373bf0fc2SViresh Kumar 		sysfs_remove_link(&dev->kobj, "cpufreq");
136473bf0fc2SViresh Kumar 	} else if (cpus > 1) {
13651bfb425bSViresh Kumar 		/* Nominate new CPU */
13661bfb425bSViresh Kumar 		int new_cpu = cpumask_any_but(policy->cpus, cpu);
13671bfb425bSViresh Kumar 		struct device *cpu_dev = get_cpu_device(new_cpu);
13681bfb425bSViresh Kumar 
13691bfb425bSViresh Kumar 		sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
13701bfb425bSViresh Kumar 		ret = update_policy_cpu(policy, new_cpu, cpu_dev);
13711bfb425bSViresh Kumar 		if (ret) {
13721bfb425bSViresh Kumar 			if (sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
13731bfb425bSViresh Kumar 					      "cpufreq"))
13741bfb425bSViresh Kumar 				pr_err("%s: Failed to restore kobj link to cpu:%d\n",
13751bfb425bSViresh Kumar 				       __func__, cpu_dev->id);
13761bfb425bSViresh Kumar 			return ret;
13771bfb425bSViresh Kumar 		}
1378a82fab29SSrivatsa S. Bhat 
1379bda9f552SStratos Karafotis 		if (!cpufreq_suspended)
138075949c9aSViresh Kumar 			pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
138175949c9aSViresh Kumar 				 __func__, new_cpu, cpu);
1382789ca243SPreeti U Murthy 	} else if (cpufreq_driver->stop_cpu) {
1383367dc4aaSDirk Brandewie 		cpufreq_driver->stop_cpu(policy);
13841da177e4SLinus Torvalds 	}
1385b8eed8afSViresh Kumar 
1386cedb70afSSrivatsa S. Bhat 	return 0;
1387cedb70afSSrivatsa S. Bhat }
1388cedb70afSSrivatsa S. Bhat 
1389cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_finish(struct device *dev,
139096bbbe4aSViresh Kumar 				       struct subsys_interface *sif)
1391cedb70afSSrivatsa S. Bhat {
1392cedb70afSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
1393cedb70afSSrivatsa S. Bhat 	int ret;
1394cedb70afSSrivatsa S. Bhat 	unsigned long flags;
1395cedb70afSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
1396cedb70afSSrivatsa S. Bhat 
13976ffae8c0SViresh Kumar 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1398cedb70afSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data, cpu);
13996ffae8c0SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = NULL;
14006ffae8c0SViresh Kumar 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1401cedb70afSSrivatsa S. Bhat 
1402cedb70afSSrivatsa S. Bhat 	if (!policy) {
1403cedb70afSSrivatsa S. Bhat 		pr_debug("%s: No cpu_data found\n", __func__);
1404cedb70afSSrivatsa S. Bhat 		return -EINVAL;
1405cedb70afSSrivatsa S. Bhat 	}
1406cedb70afSSrivatsa S. Bhat 
1407ad7722daSviresh kumar 	down_write(&policy->rwsem);
1408cedb70afSSrivatsa S. Bhat 	cpus = cpumask_weight(policy->cpus);
14099c8f1ee4SViresh Kumar 
14109c8f1ee4SViresh Kumar 	if (cpus > 1)
14119c8f1ee4SViresh Kumar 		cpumask_clear_cpu(cpu, policy->cpus);
1412ad7722daSviresh kumar 	up_write(&policy->rwsem);
1413cedb70afSSrivatsa S. Bhat 
1414b8eed8afSViresh Kumar 	/* If cpu is last user of policy, free policy */
1415b8eed8afSViresh Kumar 	if (cpus == 1) {
14169c0ebcf7SViresh Kumar 		if (has_target()) {
14173de9bdebSViresh Kumar 			ret = __cpufreq_governor(policy,
14183de9bdebSViresh Kumar 					CPUFREQ_GOV_POLICY_EXIT);
14193de9bdebSViresh Kumar 			if (ret) {
14203de9bdebSViresh Kumar 				pr_err("%s: Failed to exit governor\n",
14213de9bdebSViresh Kumar 				       __func__);
14223de9bdebSViresh Kumar 				return ret;
14233de9bdebSViresh Kumar 			}
14243de9bdebSViresh Kumar 		}
14252a998599SRafael J. Wysocki 
142696bbbe4aSViresh Kumar 		if (!cpufreq_suspended)
142742f921a6SViresh Kumar 			cpufreq_policy_put_kobj(policy);
14281da177e4SLinus Torvalds 
14298414809cSSrivatsa S. Bhat 		/*
14308414809cSSrivatsa S. Bhat 		 * Perform the ->exit() even during light-weight tear-down,
14318414809cSSrivatsa S. Bhat 		 * since this is a core component, and is essential for the
14328414809cSSrivatsa S. Bhat 		 * subsequent light-weight ->init() to succeed.
14338414809cSSrivatsa S. Bhat 		 */
14341c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
14353a3e9e06SViresh Kumar 			cpufreq_driver->exit(policy);
143627ecddc2SJacob Shin 
14379515f4d6SViresh Kumar 		/* Remove policy from list of active policies */
14389515f4d6SViresh Kumar 		write_lock_irqsave(&cpufreq_driver_lock, flags);
14399515f4d6SViresh Kumar 		list_del(&policy->policy_list);
14409515f4d6SViresh Kumar 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
14419515f4d6SViresh Kumar 
144296bbbe4aSViresh Kumar 		if (!cpufreq_suspended)
14433a3e9e06SViresh Kumar 			cpufreq_policy_free(policy);
1444e5c87b76SStratos Karafotis 	} else if (has_target()) {
1445e5c87b76SStratos Karafotis 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1446e5c87b76SStratos Karafotis 		if (!ret)
1447e5c87b76SStratos Karafotis 			ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1448e5c87b76SStratos Karafotis 
1449e5c87b76SStratos Karafotis 		if (ret) {
1450e5c87b76SStratos Karafotis 			pr_err("%s: Failed to start governor\n", __func__);
14513de9bdebSViresh Kumar 			return ret;
14523de9bdebSViresh Kumar 		}
1453b8eed8afSViresh Kumar 	}
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 	return 0;
14561da177e4SLinus Torvalds }
14571da177e4SLinus Torvalds 
1458cedb70afSSrivatsa S. Bhat /**
145927a862e9SViresh Kumar  * cpufreq_remove_dev - remove a CPU device
1460cedb70afSSrivatsa S. Bhat  *
1461cedb70afSSrivatsa S. Bhat  * Removes the cpufreq interface for a CPU device.
1462cedb70afSSrivatsa S. Bhat  */
14638a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
14645a01f2e8SVenkatesh Pallipadi {
14658a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
146627a862e9SViresh Kumar 	int ret;
1467ec28297aSVenki Pallipadi 
1468ec28297aSVenki Pallipadi 	if (cpu_is_offline(cpu))
1469ec28297aSVenki Pallipadi 		return 0;
1470ec28297aSVenki Pallipadi 
147196bbbe4aSViresh Kumar 	ret = __cpufreq_remove_dev_prepare(dev, sif);
147227a862e9SViresh Kumar 
147327a862e9SViresh Kumar 	if (!ret)
147496bbbe4aSViresh Kumar 		ret = __cpufreq_remove_dev_finish(dev, sif);
147527a862e9SViresh Kumar 
147627a862e9SViresh Kumar 	return ret;
14775a01f2e8SVenkatesh Pallipadi }
14785a01f2e8SVenkatesh Pallipadi 
147965f27f38SDavid Howells static void handle_update(struct work_struct *work)
14801da177e4SLinus Torvalds {
148165f27f38SDavid Howells 	struct cpufreq_policy *policy =
148265f27f38SDavid Howells 		container_of(work, struct cpufreq_policy, update);
148365f27f38SDavid Howells 	unsigned int cpu = policy->cpu;
14842d06d8c4SDominik Brodowski 	pr_debug("handle_update for cpu %u called\n", cpu);
14851da177e4SLinus Torvalds 	cpufreq_update_policy(cpu);
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds 
14881da177e4SLinus Torvalds /**
1489bb176f7dSViresh Kumar  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1490bb176f7dSViresh Kumar  *	in deep trouble.
1491a1e1dc41SViresh Kumar  *	@policy: policy managing CPUs
14921da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
14931da177e4SLinus Torvalds  *
149429464f28SDave Jones  *	We adjust to current frequency first, and need to clean up later.
149529464f28SDave Jones  *	So either call to cpufreq_update_policy() or schedule handle_update()).
14961da177e4SLinus Torvalds  */
1497a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1498e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
14991da177e4SLinus Torvalds {
15001da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1501b43a7ffbSViresh Kumar 
1502e837f9b5SJoe Perches 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1503a1e1dc41SViresh Kumar 		 policy->cur, new_freq);
15041da177e4SLinus Torvalds 
1505a1e1dc41SViresh Kumar 	freqs.old = policy->cur;
15061da177e4SLinus Torvalds 	freqs.new = new_freq;
1507b43a7ffbSViresh Kumar 
15088fec051eSViresh Kumar 	cpufreq_freq_transition_begin(policy, &freqs);
15098fec051eSViresh Kumar 	cpufreq_freq_transition_end(policy, &freqs, 0);
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds 
15121da177e4SLinus Torvalds /**
15134ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
151495235ca2SVenkatesh Pallipadi  * @cpu: CPU number
151595235ca2SVenkatesh Pallipadi  *
151695235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
151795235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
151895235ca2SVenkatesh Pallipadi  */
151995235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
152095235ca2SVenkatesh Pallipadi {
15219e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1522e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
152395235ca2SVenkatesh Pallipadi 
15241c3d85ddSRafael J. Wysocki 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
15251c3d85ddSRafael J. Wysocki 		return cpufreq_driver->get(cpu);
15269e21ba8bSDirk Brandewie 
15279e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
152895235ca2SVenkatesh Pallipadi 	if (policy) {
1529e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
153095235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
153195235ca2SVenkatesh Pallipadi 	}
153295235ca2SVenkatesh Pallipadi 
15334d34a67dSDave Jones 	return ret_freq;
153495235ca2SVenkatesh Pallipadi }
153595235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
153695235ca2SVenkatesh Pallipadi 
15373d737108SJesse Barnes /**
15383d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
15393d737108SJesse Barnes  * @cpu: CPU number
15403d737108SJesse Barnes  *
15413d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
15423d737108SJesse Barnes  */
15433d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
15443d737108SJesse Barnes {
15453d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
15463d737108SJesse Barnes 	unsigned int ret_freq = 0;
15473d737108SJesse Barnes 
15483d737108SJesse Barnes 	if (policy) {
15493d737108SJesse Barnes 		ret_freq = policy->max;
15503d737108SJesse Barnes 		cpufreq_cpu_put(policy);
15513d737108SJesse Barnes 	}
15523d737108SJesse Barnes 
15533d737108SJesse Barnes 	return ret_freq;
15543d737108SJesse Barnes }
15553d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
15563d737108SJesse Barnes 
1557d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
15581da177e4SLinus Torvalds {
1559e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
15601da177e4SLinus Torvalds 
15611c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->get)
15624d34a67dSDave Jones 		return ret_freq;
15631da177e4SLinus Torvalds 
1564d92d50a4SViresh Kumar 	ret_freq = cpufreq_driver->get(policy->cpu);
15651da177e4SLinus Torvalds 
1566e08f5f5bSGautham R Shenoy 	if (ret_freq && policy->cur &&
15671c3d85ddSRafael J. Wysocki 		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1568e08f5f5bSGautham R Shenoy 		/* verify no discrepancy between actual and
1569e08f5f5bSGautham R Shenoy 					saved value exists */
1570e08f5f5bSGautham R Shenoy 		if (unlikely(ret_freq != policy->cur)) {
1571a1e1dc41SViresh Kumar 			cpufreq_out_of_sync(policy, ret_freq);
15721da177e4SLinus Torvalds 			schedule_work(&policy->update);
15731da177e4SLinus Torvalds 		}
15741da177e4SLinus Torvalds 	}
15751da177e4SLinus Torvalds 
15764d34a67dSDave Jones 	return ret_freq;
15775a01f2e8SVenkatesh Pallipadi }
15781da177e4SLinus Torvalds 
15795a01f2e8SVenkatesh Pallipadi /**
15805a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
15815a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
15825a01f2e8SVenkatesh Pallipadi  *
15835a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
15845a01f2e8SVenkatesh Pallipadi  */
15855a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
15865a01f2e8SVenkatesh Pallipadi {
1587999976e0SAaron Plattner 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
15885a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
15895a01f2e8SVenkatesh Pallipadi 
1590999976e0SAaron Plattner 	if (policy) {
1591ad7722daSviresh kumar 		down_read(&policy->rwsem);
1592d92d50a4SViresh Kumar 		ret_freq = __cpufreq_get(policy);
1593ad7722daSviresh kumar 		up_read(&policy->rwsem);
1594999976e0SAaron Plattner 
1595999976e0SAaron Plattner 		cpufreq_cpu_put(policy);
1596999976e0SAaron Plattner 	}
15976eed9404SViresh Kumar 
15984d34a67dSDave Jones 	return ret_freq;
15991da177e4SLinus Torvalds }
16001da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
16011da177e4SLinus Torvalds 
16028a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
16038a25a2fdSKay Sievers 	.name		= "cpufreq",
16048a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
16058a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
16068a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1607e00e56dfSRafael J. Wysocki };
1608e00e56dfSRafael J. Wysocki 
1609e28867eaSViresh Kumar /*
1610e28867eaSViresh Kumar  * In case platform wants some specific frequency to be configured
1611e28867eaSViresh Kumar  * during suspend..
161242d4dc3fSBenjamin Herrenschmidt  */
1613e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy)
161442d4dc3fSBenjamin Herrenschmidt {
1615e28867eaSViresh Kumar 	int ret;
16164bc5d341SDave Jones 
1617e28867eaSViresh Kumar 	if (!policy->suspend_freq) {
1618e28867eaSViresh Kumar 		pr_err("%s: suspend_freq can't be zero\n", __func__);
1619e28867eaSViresh Kumar 		return -EINVAL;
162042d4dc3fSBenjamin Herrenschmidt 	}
162142d4dc3fSBenjamin Herrenschmidt 
1622e28867eaSViresh Kumar 	pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1623e28867eaSViresh Kumar 			policy->suspend_freq);
1624e28867eaSViresh Kumar 
1625e28867eaSViresh Kumar 	ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1626e28867eaSViresh Kumar 			CPUFREQ_RELATION_H);
1627e28867eaSViresh Kumar 	if (ret)
1628e28867eaSViresh Kumar 		pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1629e28867eaSViresh Kumar 				__func__, policy->suspend_freq, ret);
1630e28867eaSViresh Kumar 
1631c9060494SDave Jones 	return ret;
163242d4dc3fSBenjamin Herrenschmidt }
1633e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend);
163442d4dc3fSBenjamin Herrenschmidt 
163542d4dc3fSBenjamin Herrenschmidt /**
16362f0aea93SViresh Kumar  * cpufreq_suspend() - Suspend CPUFreq governors
16371da177e4SLinus Torvalds  *
16382f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycles for suspending governors
16392f0aea93SViresh Kumar  * as some platforms can't change frequency after this point in suspend cycle.
16402f0aea93SViresh Kumar  * Because some of the devices (like: i2c, regulators, etc) they use for
16412f0aea93SViresh Kumar  * changing frequency are suspended quickly after this point.
16421da177e4SLinus Torvalds  */
16432f0aea93SViresh Kumar void cpufreq_suspend(void)
16441da177e4SLinus Torvalds {
16453a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
16461da177e4SLinus Torvalds 
16472f0aea93SViresh Kumar 	if (!cpufreq_driver)
1648e00e56dfSRafael J. Wysocki 		return;
16491da177e4SLinus Torvalds 
16502f0aea93SViresh Kumar 	if (!has_target())
1651b1b12babSViresh Kumar 		goto suspend;
16521da177e4SLinus Torvalds 
16532f0aea93SViresh Kumar 	pr_debug("%s: Suspending Governors\n", __func__);
16542f0aea93SViresh Kumar 
1655*b4f0676fSViresh Kumar 	for_each_policy(policy) {
16562f0aea93SViresh Kumar 		if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
16572f0aea93SViresh Kumar 			pr_err("%s: Failed to stop governor for policy: %p\n",
16582f0aea93SViresh Kumar 				__func__, policy);
16592f0aea93SViresh Kumar 		else if (cpufreq_driver->suspend
16602f0aea93SViresh Kumar 		    && cpufreq_driver->suspend(policy))
16612f0aea93SViresh Kumar 			pr_err("%s: Failed to suspend driver: %p\n", __func__,
16622f0aea93SViresh Kumar 				policy);
16631da177e4SLinus Torvalds 	}
1664b1b12babSViresh Kumar 
1665b1b12babSViresh Kumar suspend:
1666b1b12babSViresh Kumar 	cpufreq_suspended = true;
16671da177e4SLinus Torvalds }
16681da177e4SLinus Torvalds 
16691da177e4SLinus Torvalds /**
16702f0aea93SViresh Kumar  * cpufreq_resume() - Resume CPUFreq governors
16711da177e4SLinus Torvalds  *
16722f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycle for resuming governors that
16732f0aea93SViresh Kumar  * are suspended with cpufreq_suspend().
16741da177e4SLinus Torvalds  */
16752f0aea93SViresh Kumar void cpufreq_resume(void)
16761da177e4SLinus Torvalds {
16771da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
16781da177e4SLinus Torvalds 
16792f0aea93SViresh Kumar 	if (!cpufreq_driver)
16801da177e4SLinus Torvalds 		return;
16811da177e4SLinus Torvalds 
16828e30444eSLan Tianyu 	cpufreq_suspended = false;
16838e30444eSLan Tianyu 
16842f0aea93SViresh Kumar 	if (!has_target())
16852f0aea93SViresh Kumar 		return;
16861da177e4SLinus Torvalds 
16872f0aea93SViresh Kumar 	pr_debug("%s: Resuming Governors\n", __func__);
16882f0aea93SViresh Kumar 
1689*b4f0676fSViresh Kumar 	for_each_policy(policy) {
16900c5aa405SViresh Kumar 		if (cpufreq_driver->resume && cpufreq_driver->resume(policy))
16910c5aa405SViresh Kumar 			pr_err("%s: Failed to resume driver: %p\n", __func__,
16920c5aa405SViresh Kumar 				policy);
16930c5aa405SViresh Kumar 		else if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
16942f0aea93SViresh Kumar 		    || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
16952f0aea93SViresh Kumar 			pr_err("%s: Failed to start governor for policy: %p\n",
16962f0aea93SViresh Kumar 				__func__, policy);
16972f0aea93SViresh Kumar 
16982f0aea93SViresh Kumar 		/*
16992f0aea93SViresh Kumar 		 * schedule call cpufreq_update_policy() for boot CPU, i.e. last
17002f0aea93SViresh Kumar 		 * policy in list. It will verify that the current freq is in
17012f0aea93SViresh Kumar 		 * sync with what we believe it to be.
17022f0aea93SViresh Kumar 		 */
17032f0aea93SViresh Kumar 		if (list_is_last(&policy->policy_list, &cpufreq_policy_list))
17043a3e9e06SViresh Kumar 			schedule_work(&policy->update);
17051da177e4SLinus Torvalds 	}
17062f0aea93SViresh Kumar }
17071da177e4SLinus Torvalds 
17089d95046eSBorislav Petkov /**
17099d95046eSBorislav Petkov  *	cpufreq_get_current_driver - return current driver's name
17109d95046eSBorislav Petkov  *
17119d95046eSBorislav Petkov  *	Return the name string of the currently loaded cpufreq driver
17129d95046eSBorislav Petkov  *	or NULL, if none.
17139d95046eSBorislav Petkov  */
17149d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
17159d95046eSBorislav Petkov {
17161c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
17171c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
17181c3d85ddSRafael J. Wysocki 
17191c3d85ddSRafael J. Wysocki 	return NULL;
17209d95046eSBorislav Petkov }
17219d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
17221da177e4SLinus Torvalds 
172351315cdfSThomas Petazzoni /**
172451315cdfSThomas Petazzoni  *	cpufreq_get_driver_data - return current driver data
172551315cdfSThomas Petazzoni  *
172651315cdfSThomas Petazzoni  *	Return the private data of the currently loaded cpufreq
172751315cdfSThomas Petazzoni  *	driver, or NULL if no cpufreq driver is loaded.
172851315cdfSThomas Petazzoni  */
172951315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void)
173051315cdfSThomas Petazzoni {
173151315cdfSThomas Petazzoni 	if (cpufreq_driver)
173251315cdfSThomas Petazzoni 		return cpufreq_driver->driver_data;
173351315cdfSThomas Petazzoni 
173451315cdfSThomas Petazzoni 	return NULL;
173551315cdfSThomas Petazzoni }
173651315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
173751315cdfSThomas Petazzoni 
17381da177e4SLinus Torvalds /*********************************************************************
17391da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
17401da177e4SLinus Torvalds  *********************************************************************/
17411da177e4SLinus Torvalds 
17421da177e4SLinus Torvalds /**
17431da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
17441da177e4SLinus Torvalds  *	@nb: notifier function to register
17451da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
17461da177e4SLinus Torvalds  *
17471da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
17481da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
17491da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
17501da177e4SLinus Torvalds  *      changes in cpufreq policy.
17511da177e4SLinus Torvalds  *
17521da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1753e041c683SAlan Stern  *	blocking_notifier_chain_register.
17541da177e4SLinus Torvalds  */
17551da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
17561da177e4SLinus Torvalds {
17571da177e4SLinus Torvalds 	int ret;
17581da177e4SLinus Torvalds 
1759d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1760d5aaffa9SDirk Brandewie 		return -EINVAL;
1761d5aaffa9SDirk Brandewie 
176274212ca4SCesar Eduardo Barros 	WARN_ON(!init_cpufreq_transition_notifier_list_called);
176374212ca4SCesar Eduardo Barros 
17641da177e4SLinus Torvalds 	switch (list) {
17651da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1766b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1767e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
17681da177e4SLinus Torvalds 		break;
17691da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1770e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1771e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
17721da177e4SLinus Torvalds 		break;
17731da177e4SLinus Torvalds 	default:
17741da177e4SLinus Torvalds 		ret = -EINVAL;
17751da177e4SLinus Torvalds 	}
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds 	return ret;
17781da177e4SLinus Torvalds }
17791da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds /**
17821da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
17831da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
17841da177e4SLinus Torvalds  *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
17851da177e4SLinus Torvalds  *
17861da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
17871da177e4SLinus Torvalds  *
17881da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1789e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
17901da177e4SLinus Torvalds  */
17911da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
17921da177e4SLinus Torvalds {
17931da177e4SLinus Torvalds 	int ret;
17941da177e4SLinus Torvalds 
1795d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1796d5aaffa9SDirk Brandewie 		return -EINVAL;
1797d5aaffa9SDirk Brandewie 
17981da177e4SLinus Torvalds 	switch (list) {
17991da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1800b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
1801e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
18021da177e4SLinus Torvalds 		break;
18031da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1804e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1805e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
18061da177e4SLinus Torvalds 		break;
18071da177e4SLinus Torvalds 	default:
18081da177e4SLinus Torvalds 		ret = -EINVAL;
18091da177e4SLinus Torvalds 	}
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds 	return ret;
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds /*********************************************************************
18171da177e4SLinus Torvalds  *                              GOVERNORS                            *
18181da177e4SLinus Torvalds  *********************************************************************/
18191da177e4SLinus Torvalds 
18201c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */
18211c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy,
18221c03a2d0SViresh Kumar 				 struct cpufreq_freqs *freqs, int index)
18231c03a2d0SViresh Kumar {
18241c03a2d0SViresh Kumar 	int ret;
18251c03a2d0SViresh Kumar 
18261c03a2d0SViresh Kumar 	freqs->new = cpufreq_driver->get_intermediate(policy, index);
18271c03a2d0SViresh Kumar 
18281c03a2d0SViresh Kumar 	/* We don't need to switch to intermediate freq */
18291c03a2d0SViresh Kumar 	if (!freqs->new)
18301c03a2d0SViresh Kumar 		return 0;
18311c03a2d0SViresh Kumar 
18321c03a2d0SViresh Kumar 	pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
18331c03a2d0SViresh Kumar 		 __func__, policy->cpu, freqs->old, freqs->new);
18341c03a2d0SViresh Kumar 
18351c03a2d0SViresh Kumar 	cpufreq_freq_transition_begin(policy, freqs);
18361c03a2d0SViresh Kumar 	ret = cpufreq_driver->target_intermediate(policy, index);
18371c03a2d0SViresh Kumar 	cpufreq_freq_transition_end(policy, freqs, ret);
18381c03a2d0SViresh Kumar 
18391c03a2d0SViresh Kumar 	if (ret)
18401c03a2d0SViresh Kumar 		pr_err("%s: Failed to change to intermediate frequency: %d\n",
18411c03a2d0SViresh Kumar 		       __func__, ret);
18421c03a2d0SViresh Kumar 
18431c03a2d0SViresh Kumar 	return ret;
18441c03a2d0SViresh Kumar }
18451c03a2d0SViresh Kumar 
18468d65775dSViresh Kumar static int __target_index(struct cpufreq_policy *policy,
18478d65775dSViresh Kumar 			  struct cpufreq_frequency_table *freq_table, int index)
18488d65775dSViresh Kumar {
18491c03a2d0SViresh Kumar 	struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
18501c03a2d0SViresh Kumar 	unsigned int intermediate_freq = 0;
18518d65775dSViresh Kumar 	int retval = -EINVAL;
18528d65775dSViresh Kumar 	bool notify;
18538d65775dSViresh Kumar 
18548d65775dSViresh Kumar 	notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
18558d65775dSViresh Kumar 	if (notify) {
18561c03a2d0SViresh Kumar 		/* Handle switching to intermediate frequency */
18571c03a2d0SViresh Kumar 		if (cpufreq_driver->get_intermediate) {
18581c03a2d0SViresh Kumar 			retval = __target_intermediate(policy, &freqs, index);
18591c03a2d0SViresh Kumar 			if (retval)
18601c03a2d0SViresh Kumar 				return retval;
18618d65775dSViresh Kumar 
18621c03a2d0SViresh Kumar 			intermediate_freq = freqs.new;
18631c03a2d0SViresh Kumar 			/* Set old freq to intermediate */
18641c03a2d0SViresh Kumar 			if (intermediate_freq)
18651c03a2d0SViresh Kumar 				freqs.old = freqs.new;
18661c03a2d0SViresh Kumar 		}
18671c03a2d0SViresh Kumar 
18681c03a2d0SViresh Kumar 		freqs.new = freq_table[index].frequency;
18698d65775dSViresh Kumar 		pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
18708d65775dSViresh Kumar 			 __func__, policy->cpu, freqs.old, freqs.new);
18718d65775dSViresh Kumar 
18728d65775dSViresh Kumar 		cpufreq_freq_transition_begin(policy, &freqs);
18738d65775dSViresh Kumar 	}
18748d65775dSViresh Kumar 
18758d65775dSViresh Kumar 	retval = cpufreq_driver->target_index(policy, index);
18768d65775dSViresh Kumar 	if (retval)
18778d65775dSViresh Kumar 		pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
18788d65775dSViresh Kumar 		       retval);
18798d65775dSViresh Kumar 
18801c03a2d0SViresh Kumar 	if (notify) {
18818d65775dSViresh Kumar 		cpufreq_freq_transition_end(policy, &freqs, retval);
18828d65775dSViresh Kumar 
18831c03a2d0SViresh Kumar 		/*
18841c03a2d0SViresh Kumar 		 * Failed after setting to intermediate freq? Driver should have
18851c03a2d0SViresh Kumar 		 * reverted back to initial frequency and so should we. Check
18861c03a2d0SViresh Kumar 		 * here for intermediate_freq instead of get_intermediate, in
18871c03a2d0SViresh Kumar 		 * case we have't switched to intermediate freq at all.
18881c03a2d0SViresh Kumar 		 */
18891c03a2d0SViresh Kumar 		if (unlikely(retval && intermediate_freq)) {
18901c03a2d0SViresh Kumar 			freqs.old = intermediate_freq;
18911c03a2d0SViresh Kumar 			freqs.new = policy->restore_freq;
18921c03a2d0SViresh Kumar 			cpufreq_freq_transition_begin(policy, &freqs);
18931c03a2d0SViresh Kumar 			cpufreq_freq_transition_end(policy, &freqs, 0);
18941c03a2d0SViresh Kumar 		}
18951c03a2d0SViresh Kumar 	}
18961c03a2d0SViresh Kumar 
18978d65775dSViresh Kumar 	return retval;
18988d65775dSViresh Kumar }
18998d65775dSViresh Kumar 
19001da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
19011da177e4SLinus Torvalds 			    unsigned int target_freq,
19021da177e4SLinus Torvalds 			    unsigned int relation)
19031da177e4SLinus Torvalds {
19047249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
19058d65775dSViresh Kumar 	int retval = -EINVAL;
1906c32b6b8eSAshok Raj 
1907a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1908a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1909a7b422cdSKonrad Rzeszutek Wilk 
19107249924eSViresh Kumar 	/* Make sure that target_freq is within supported range */
19117249924eSViresh Kumar 	if (target_freq > policy->max)
19127249924eSViresh Kumar 		target_freq = policy->max;
19137249924eSViresh Kumar 	if (target_freq < policy->min)
19147249924eSViresh Kumar 		target_freq = policy->min;
19157249924eSViresh Kumar 
19167249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
19177249924eSViresh Kumar 		 policy->cpu, target_freq, relation, old_target_freq);
19185a1c0228SViresh Kumar 
19199c0ebcf7SViresh Kumar 	/*
19209c0ebcf7SViresh Kumar 	 * This might look like a redundant call as we are checking it again
19219c0ebcf7SViresh Kumar 	 * after finding index. But it is left intentionally for cases where
19229c0ebcf7SViresh Kumar 	 * exactly same freq is called again and so we can save on few function
19239c0ebcf7SViresh Kumar 	 * calls.
19249c0ebcf7SViresh Kumar 	 */
19255a1c0228SViresh Kumar 	if (target_freq == policy->cur)
19265a1c0228SViresh Kumar 		return 0;
19275a1c0228SViresh Kumar 
19281c03a2d0SViresh Kumar 	/* Save last value to restore later on errors */
19291c03a2d0SViresh Kumar 	policy->restore_freq = policy->cur;
19301c03a2d0SViresh Kumar 
19311c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
19321c3d85ddSRafael J. Wysocki 		retval = cpufreq_driver->target(policy, target_freq, relation);
19339c0ebcf7SViresh Kumar 	else if (cpufreq_driver->target_index) {
19349c0ebcf7SViresh Kumar 		struct cpufreq_frequency_table *freq_table;
19359c0ebcf7SViresh Kumar 		int index;
193690d45d17SAshok Raj 
19379c0ebcf7SViresh Kumar 		freq_table = cpufreq_frequency_get_table(policy->cpu);
19389c0ebcf7SViresh Kumar 		if (unlikely(!freq_table)) {
19399c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find freq_table\n", __func__);
19409c0ebcf7SViresh Kumar 			goto out;
19419c0ebcf7SViresh Kumar 		}
19429c0ebcf7SViresh Kumar 
19439c0ebcf7SViresh Kumar 		retval = cpufreq_frequency_table_target(policy, freq_table,
19449c0ebcf7SViresh Kumar 				target_freq, relation, &index);
19459c0ebcf7SViresh Kumar 		if (unlikely(retval)) {
19469c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find matching freq\n", __func__);
19479c0ebcf7SViresh Kumar 			goto out;
19489c0ebcf7SViresh Kumar 		}
19499c0ebcf7SViresh Kumar 
1950d4019f0aSViresh Kumar 		if (freq_table[index].frequency == policy->cur) {
19519c0ebcf7SViresh Kumar 			retval = 0;
1952d4019f0aSViresh Kumar 			goto out;
1953d4019f0aSViresh Kumar 		}
1954d4019f0aSViresh Kumar 
19558d65775dSViresh Kumar 		retval = __target_index(policy, freq_table, index);
19569c0ebcf7SViresh Kumar 	}
19579c0ebcf7SViresh Kumar 
19589c0ebcf7SViresh Kumar out:
19591da177e4SLinus Torvalds 	return retval;
19601da177e4SLinus Torvalds }
19611da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
19621da177e4SLinus Torvalds 
19631da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
19641da177e4SLinus Torvalds 			  unsigned int target_freq,
19651da177e4SLinus Torvalds 			  unsigned int relation)
19661da177e4SLinus Torvalds {
1967f1829e4aSJulia Lawall 	int ret = -EINVAL;
19681da177e4SLinus Torvalds 
1969ad7722daSviresh kumar 	down_write(&policy->rwsem);
19701da177e4SLinus Torvalds 
19711da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
19721da177e4SLinus Torvalds 
1973ad7722daSviresh kumar 	up_write(&policy->rwsem);
19741da177e4SLinus Torvalds 
19751da177e4SLinus Torvalds 	return ret;
19761da177e4SLinus Torvalds }
19771da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
19781da177e4SLinus Torvalds 
1979e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy,
1980e08f5f5bSGautham R Shenoy 					unsigned int event)
19811da177e4SLinus Torvalds {
1982cc993cabSDave Jones 	int ret;
19836afde10cSThomas Renninger 
19846afde10cSThomas Renninger 	/* Only must be defined when default governor is known to have latency
19856afde10cSThomas Renninger 	   restrictions, like e.g. conservative or ondemand.
19866afde10cSThomas Renninger 	   That this is the case is already ensured in Kconfig
19876afde10cSThomas Renninger 	*/
19886afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
19896afde10cSThomas Renninger 	struct cpufreq_governor *gov = &cpufreq_gov_performance;
19906afde10cSThomas Renninger #else
19916afde10cSThomas Renninger 	struct cpufreq_governor *gov = NULL;
19926afde10cSThomas Renninger #endif
19931c256245SThomas Renninger 
19942f0aea93SViresh Kumar 	/* Don't start any governor operations if we are entering suspend */
19952f0aea93SViresh Kumar 	if (cpufreq_suspended)
19962f0aea93SViresh Kumar 		return 0;
1997cb57720bSEthan Zhao 	/*
1998cb57720bSEthan Zhao 	 * Governor might not be initiated here if ACPI _PPC changed
1999cb57720bSEthan Zhao 	 * notification happened, so check it.
2000cb57720bSEthan Zhao 	 */
2001cb57720bSEthan Zhao 	if (!policy->governor)
2002cb57720bSEthan Zhao 		return -EINVAL;
20032f0aea93SViresh Kumar 
20041c256245SThomas Renninger 	if (policy->governor->max_transition_latency &&
20051c256245SThomas Renninger 	    policy->cpuinfo.transition_latency >
20061c256245SThomas Renninger 	    policy->governor->max_transition_latency) {
20076afde10cSThomas Renninger 		if (!gov)
20086afde10cSThomas Renninger 			return -EINVAL;
20096afde10cSThomas Renninger 		else {
2010e837f9b5SJoe Perches 			pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2011e837f9b5SJoe Perches 				policy->governor->name, gov->name);
20121c256245SThomas Renninger 			policy->governor = gov;
20131c256245SThomas Renninger 		}
20146afde10cSThomas Renninger 	}
20151da177e4SLinus Torvalds 
2016fe492f3fSViresh Kumar 	if (event == CPUFREQ_GOV_POLICY_INIT)
20171da177e4SLinus Torvalds 		if (!try_module_get(policy->governor->owner))
20181da177e4SLinus Torvalds 			return -EINVAL;
20191da177e4SLinus Torvalds 
20202d06d8c4SDominik Brodowski 	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
2021e08f5f5bSGautham R Shenoy 		 policy->cpu, event);
202295731ebbSXiaoguang Chen 
202395731ebbSXiaoguang Chen 	mutex_lock(&cpufreq_governor_lock);
202456d07db2SSrivatsa S. Bhat 	if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
2025f73d3933SViresh Kumar 	    || (!policy->governor_enabled
2026f73d3933SViresh Kumar 	    && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
202795731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
202895731ebbSXiaoguang Chen 		return -EBUSY;
202995731ebbSXiaoguang Chen 	}
203095731ebbSXiaoguang Chen 
203195731ebbSXiaoguang Chen 	if (event == CPUFREQ_GOV_STOP)
203295731ebbSXiaoguang Chen 		policy->governor_enabled = false;
203395731ebbSXiaoguang Chen 	else if (event == CPUFREQ_GOV_START)
203495731ebbSXiaoguang Chen 		policy->governor_enabled = true;
203595731ebbSXiaoguang Chen 
203695731ebbSXiaoguang Chen 	mutex_unlock(&cpufreq_governor_lock);
203795731ebbSXiaoguang Chen 
20381da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
20391da177e4SLinus Torvalds 
20404d5dcc42SViresh Kumar 	if (!ret) {
20414d5dcc42SViresh Kumar 		if (event == CPUFREQ_GOV_POLICY_INIT)
20428e53695fSViresh Kumar 			policy->governor->initialized++;
20434d5dcc42SViresh Kumar 		else if (event == CPUFREQ_GOV_POLICY_EXIT)
20448e53695fSViresh Kumar 			policy->governor->initialized--;
204595731ebbSXiaoguang Chen 	} else {
204695731ebbSXiaoguang Chen 		/* Restore original values */
204795731ebbSXiaoguang Chen 		mutex_lock(&cpufreq_governor_lock);
204895731ebbSXiaoguang Chen 		if (event == CPUFREQ_GOV_STOP)
204995731ebbSXiaoguang Chen 			policy->governor_enabled = true;
205095731ebbSXiaoguang Chen 		else if (event == CPUFREQ_GOV_START)
205195731ebbSXiaoguang Chen 			policy->governor_enabled = false;
205295731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
20534d5dcc42SViresh Kumar 	}
2054b394058fSViresh Kumar 
2055fe492f3fSViresh Kumar 	if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2056fe492f3fSViresh Kumar 			((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
20571da177e4SLinus Torvalds 		module_put(policy->governor->owner);
20581da177e4SLinus Torvalds 
20591da177e4SLinus Torvalds 	return ret;
20601da177e4SLinus Torvalds }
20611da177e4SLinus Torvalds 
20621da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
20631da177e4SLinus Torvalds {
20643bcb09a3SJeremy Fitzhardinge 	int err;
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	if (!governor)
20671da177e4SLinus Torvalds 		return -EINVAL;
20681da177e4SLinus Torvalds 
2069a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2070a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2071a7b422cdSKonrad Rzeszutek Wilk 
20723fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
20731da177e4SLinus Torvalds 
2074b394058fSViresh Kumar 	governor->initialized = 0;
20753bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
207642f91fa1SViresh Kumar 	if (!find_governor(governor->name)) {
20773bcb09a3SJeremy Fitzhardinge 		err = 0;
20781da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
20793bcb09a3SJeremy Fitzhardinge 	}
20801da177e4SLinus Torvalds 
20813fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
20823bcb09a3SJeremy Fitzhardinge 	return err;
20831da177e4SLinus Torvalds }
20841da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
20851da177e4SLinus Torvalds 
20861da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
20871da177e4SLinus Torvalds {
208890e41bacSPrarit Bhargava 	int cpu;
208990e41bacSPrarit Bhargava 
20901da177e4SLinus Torvalds 	if (!governor)
20911da177e4SLinus Torvalds 		return;
20921da177e4SLinus Torvalds 
2093a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2094a7b422cdSKonrad Rzeszutek Wilk 		return;
2095a7b422cdSKonrad Rzeszutek Wilk 
209690e41bacSPrarit Bhargava 	for_each_present_cpu(cpu) {
209790e41bacSPrarit Bhargava 		if (cpu_online(cpu))
209890e41bacSPrarit Bhargava 			continue;
209990e41bacSPrarit Bhargava 		if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
210090e41bacSPrarit Bhargava 			strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
210190e41bacSPrarit Bhargava 	}
210290e41bacSPrarit Bhargava 
21033fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
21041da177e4SLinus Torvalds 	list_del(&governor->governor_list);
21053fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
21061da177e4SLinus Torvalds 	return;
21071da177e4SLinus Torvalds }
21081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
21091da177e4SLinus Torvalds 
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds /*********************************************************************
21121da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
21131da177e4SLinus Torvalds  *********************************************************************/
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds /**
21161da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
211729464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
211829464f28SDave Jones  *	is written
21191da177e4SLinus Torvalds  *
21201da177e4SLinus Torvalds  * Reads the current cpufreq policy.
21211da177e4SLinus Torvalds  */
21221da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
21231da177e4SLinus Torvalds {
21241da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
21251da177e4SLinus Torvalds 	if (!policy)
21261da177e4SLinus Torvalds 		return -EINVAL;
21271da177e4SLinus Torvalds 
21281da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
21291da177e4SLinus Torvalds 	if (!cpu_policy)
21301da177e4SLinus Torvalds 		return -EINVAL;
21311da177e4SLinus Torvalds 
2132d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
21351da177e4SLinus Torvalds 	return 0;
21361da177e4SLinus Torvalds }
21371da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
21381da177e4SLinus Torvalds 
2139153d7f3fSArjan van de Ven /*
2140037ce839SViresh Kumar  * policy : current policy.
2141037ce839SViresh Kumar  * new_policy: policy to be set.
2142153d7f3fSArjan van de Ven  */
2143037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
21443a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy)
21451da177e4SLinus Torvalds {
2146d9a789c7SRafael J. Wysocki 	struct cpufreq_governor *old_gov;
2147d9a789c7SRafael J. Wysocki 	int ret;
21481da177e4SLinus Torvalds 
2149e837f9b5SJoe Perches 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2150e837f9b5SJoe Perches 		 new_policy->cpu, new_policy->min, new_policy->max);
21511da177e4SLinus Torvalds 
2152d5b73cd8SViresh Kumar 	memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
21531da177e4SLinus Torvalds 
2154d9a789c7SRafael J. Wysocki 	if (new_policy->min > policy->max || new_policy->max < policy->min)
2155d9a789c7SRafael J. Wysocki 		return -EINVAL;
21569c9a43edSMattia Dongili 
21571da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
21583a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
21591da177e4SLinus Torvalds 	if (ret)
2160d9a789c7SRafael J. Wysocki 		return ret;
21611da177e4SLinus Torvalds 
21621da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
2163e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
21643a3e9e06SViresh Kumar 			CPUFREQ_ADJUST, new_policy);
21651da177e4SLinus Torvalds 
21661da177e4SLinus Torvalds 	/* adjust if necessary - hardware incompatibility*/
2167e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
21683a3e9e06SViresh Kumar 			CPUFREQ_INCOMPATIBLE, new_policy);
21691da177e4SLinus Torvalds 
2170bb176f7dSViresh Kumar 	/*
2171bb176f7dSViresh Kumar 	 * verify the cpu speed can be set within this limit, which might be
2172bb176f7dSViresh Kumar 	 * different to the first one
2173bb176f7dSViresh Kumar 	 */
21743a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
2175e041c683SAlan Stern 	if (ret)
2176d9a789c7SRafael J. Wysocki 		return ret;
21771da177e4SLinus Torvalds 
21781da177e4SLinus Torvalds 	/* notification of the new policy */
2179e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
21803a3e9e06SViresh Kumar 			CPUFREQ_NOTIFY, new_policy);
21811da177e4SLinus Torvalds 
21823a3e9e06SViresh Kumar 	policy->min = new_policy->min;
21833a3e9e06SViresh Kumar 	policy->max = new_policy->max;
21841da177e4SLinus Torvalds 
21852d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
21863a3e9e06SViresh Kumar 		 policy->min, policy->max);
21871da177e4SLinus Torvalds 
21881c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
21893a3e9e06SViresh Kumar 		policy->policy = new_policy->policy;
21902d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
2191d9a789c7SRafael J. Wysocki 		return cpufreq_driver->setpolicy(new_policy);
2192d9a789c7SRafael J. Wysocki 	}
2193d9a789c7SRafael J. Wysocki 
2194d9a789c7SRafael J. Wysocki 	if (new_policy->governor == policy->governor)
2195d9a789c7SRafael J. Wysocki 		goto out;
21961da177e4SLinus Torvalds 
21972d06d8c4SDominik Brodowski 	pr_debug("governor switch\n");
21981da177e4SLinus Torvalds 
2199d9a789c7SRafael J. Wysocki 	/* save old, working values */
2200d9a789c7SRafael J. Wysocki 	old_gov = policy->governor;
22011da177e4SLinus Torvalds 	/* end old governor */
2202d9a789c7SRafael J. Wysocki 	if (old_gov) {
22033a3e9e06SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2204ad7722daSviresh kumar 		up_write(&policy->rwsem);
2205d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2206ad7722daSviresh kumar 		down_write(&policy->rwsem);
22077bd353a9SViresh Kumar 	}
22081da177e4SLinus Torvalds 
22091da177e4SLinus Torvalds 	/* start new governor */
22103a3e9e06SViresh Kumar 	policy->governor = new_policy->governor;
22113a3e9e06SViresh Kumar 	if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2212d9a789c7SRafael J. Wysocki 		if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2213d9a789c7SRafael J. Wysocki 			goto out;
2214d9a789c7SRafael J. Wysocki 
2215ad7722daSviresh kumar 		up_write(&policy->rwsem);
2216d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2217ad7722daSviresh kumar 		down_write(&policy->rwsem);
2218955ef483SViresh Kumar 	}
22197bd353a9SViresh Kumar 
22201da177e4SLinus Torvalds 	/* new governor failed, so re-start old one */
2221d9a789c7SRafael J. Wysocki 	pr_debug("starting governor %s failed\n", policy->governor->name);
22221da177e4SLinus Torvalds 	if (old_gov) {
22233a3e9e06SViresh Kumar 		policy->governor = old_gov;
2224d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2225d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_START);
22261da177e4SLinus Torvalds 	}
22271da177e4SLinus Torvalds 
2228d9a789c7SRafael J. Wysocki 	return -EINVAL;
2229d9a789c7SRafael J. Wysocki 
2230d9a789c7SRafael J. Wysocki  out:
2231d9a789c7SRafael J. Wysocki 	pr_debug("governor: change or update limits\n");
2232d9a789c7SRafael J. Wysocki 	return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
22331da177e4SLinus Torvalds }
22341da177e4SLinus Torvalds 
22351da177e4SLinus Torvalds /**
22361da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
22371da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
22381da177e4SLinus Torvalds  *
223925985edcSLucas De Marchi  *	Useful for policy notifiers which have different necessities
22401da177e4SLinus Torvalds  *	at different times.
22411da177e4SLinus Torvalds  */
22421da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
22431da177e4SLinus Torvalds {
22443a3e9e06SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
22453a3e9e06SViresh Kumar 	struct cpufreq_policy new_policy;
2246f1829e4aSJulia Lawall 	int ret;
22471da177e4SLinus Torvalds 
2248fefa8ff8SAaron Plattner 	if (!policy)
2249fefa8ff8SAaron Plattner 		return -ENODEV;
22501da177e4SLinus Torvalds 
2251ad7722daSviresh kumar 	down_write(&policy->rwsem);
22521da177e4SLinus Torvalds 
22532d06d8c4SDominik Brodowski 	pr_debug("updating policy for CPU %u\n", cpu);
2254d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
22553a3e9e06SViresh Kumar 	new_policy.min = policy->user_policy.min;
22563a3e9e06SViresh Kumar 	new_policy.max = policy->user_policy.max;
22573a3e9e06SViresh Kumar 	new_policy.policy = policy->user_policy.policy;
22583a3e9e06SViresh Kumar 	new_policy.governor = policy->user_policy.governor;
22591da177e4SLinus Torvalds 
2260bb176f7dSViresh Kumar 	/*
2261bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
2262bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
2263bb176f7dSViresh Kumar 	 */
22642ed99e39SRafael J. Wysocki 	if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
22653a3e9e06SViresh Kumar 		new_policy.cur = cpufreq_driver->get(cpu);
2266bd0fa9bbSViresh Kumar 		if (WARN_ON(!new_policy.cur)) {
2267bd0fa9bbSViresh Kumar 			ret = -EIO;
2268fefa8ff8SAaron Plattner 			goto unlock;
2269bd0fa9bbSViresh Kumar 		}
2270bd0fa9bbSViresh Kumar 
22713a3e9e06SViresh Kumar 		if (!policy->cur) {
2272e837f9b5SJoe Perches 			pr_debug("Driver did not initialize current freq\n");
22733a3e9e06SViresh Kumar 			policy->cur = new_policy.cur;
2274a85f7bd3SThomas Renninger 		} else {
22759c0ebcf7SViresh Kumar 			if (policy->cur != new_policy.cur && has_target())
2276a1e1dc41SViresh Kumar 				cpufreq_out_of_sync(policy, new_policy.cur);
22770961dd0dSThomas Renninger 		}
2278a85f7bd3SThomas Renninger 	}
22790961dd0dSThomas Renninger 
2280037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
22811da177e4SLinus Torvalds 
2282fefa8ff8SAaron Plattner unlock:
2283ad7722daSviresh kumar 	up_write(&policy->rwsem);
22845a01f2e8SVenkatesh Pallipadi 
22853a3e9e06SViresh Kumar 	cpufreq_cpu_put(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 
2332*b4f0676fSViresh Kumar 	for_each_policy(policy) {
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 ||
24161c03a2d0SViresh Kumar 		    driver_data->target)) ||
24171c03a2d0SViresh 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 
24220d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
24231c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
24240d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24254dea5806SYinghai Lu 		return -EEXIST;
24261da177e4SLinus Torvalds 	}
24271c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
24280d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24291da177e4SLinus Torvalds 
2430bc68b7dfSViresh Kumar 	if (driver_data->setpolicy)
2431bc68b7dfSViresh Kumar 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
2432bc68b7dfSViresh Kumar 
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 
2453ce1bcfe9SViresh Kumar 	if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2454ce1bcfe9SViresh Kumar 	    list_empty(&cpufreq_policy_list)) {
24551da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
2456ce1bcfe9SViresh Kumar 		pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2457e08f5f5bSGautham R Shenoy 			 driver_data->name);
24588a25a2fdSKay Sievers 		goto err_if_unreg;
24591da177e4SLinus Torvalds 	}
24601da177e4SLinus Torvalds 
246165edc68cSChandra Seetharaman 	register_hotcpu_notifier(&cpufreq_cpu_notifier);
24622d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
24631da177e4SLinus Torvalds 
24648f5bc2abSJiri Slaby 	return 0;
24658a25a2fdSKay Sievers err_if_unreg:
24668a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
24676f19efc0SLukasz Majewski err_boost_unreg:
24686f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
24696f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
24708f5bc2abSJiri Slaby err_null_driver:
24710d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
24721c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
24730d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24744d34a67dSDave Jones 	return ret;
24751da177e4SLinus Torvalds }
24761da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
24771da177e4SLinus Torvalds 
24781da177e4SLinus Torvalds /**
24791da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
24801da177e4SLinus Torvalds  *
24811da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
24821da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
24831da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
24841da177e4SLinus Torvalds  * currently not initialised.
24851da177e4SLinus Torvalds  */
2486221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
24871da177e4SLinus Torvalds {
24881da177e4SLinus Torvalds 	unsigned long flags;
24891da177e4SLinus Torvalds 
24901c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
24911da177e4SLinus Torvalds 		return -EINVAL;
24921da177e4SLinus Torvalds 
24932d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
24941da177e4SLinus Torvalds 
24958a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
24966f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
24976f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
24986f19efc0SLukasz Majewski 
249965edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
25001da177e4SLinus Torvalds 
25016eed9404SViresh Kumar 	down_write(&cpufreq_rwsem);
25020d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
25036eed9404SViresh Kumar 
25041c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
25056eed9404SViresh Kumar 
25060d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
25076eed9404SViresh Kumar 	up_write(&cpufreq_rwsem);
25081da177e4SLinus Torvalds 
25091da177e4SLinus Torvalds 	return 0;
25101da177e4SLinus Torvalds }
25111da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
25125a01f2e8SVenkatesh Pallipadi 
251390de2a4aSDoug Anderson /*
251490de2a4aSDoug Anderson  * Stop cpufreq at shutdown to make sure it isn't holding any locks
251590de2a4aSDoug Anderson  * or mutexes when secondary CPUs are halted.
251690de2a4aSDoug Anderson  */
251790de2a4aSDoug Anderson static struct syscore_ops cpufreq_syscore_ops = {
251890de2a4aSDoug Anderson 	.shutdown = cpufreq_suspend,
251990de2a4aSDoug Anderson };
252090de2a4aSDoug Anderson 
25215a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
25225a01f2e8SVenkatesh Pallipadi {
2523a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2524a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2525a7b422cdSKonrad Rzeszutek Wilk 
25262361be23SViresh Kumar 	cpufreq_global_kobject = kobject_create();
25278aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
25288aa84ad8SThomas Renninger 
252990de2a4aSDoug Anderson 	register_syscore_ops(&cpufreq_syscore_ops);
253090de2a4aSDoug Anderson 
25315a01f2e8SVenkatesh Pallipadi 	return 0;
25325a01f2e8SVenkatesh Pallipadi }
25335a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2534