xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision 9d16f207112f77711600fb0770182a06e056e5de)
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 
34b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list);
35f963735aSViresh Kumar 
36f963735aSViresh Kumar static inline bool policy_is_inactive(struct cpufreq_policy *policy)
37f963735aSViresh Kumar {
38f963735aSViresh Kumar 	return cpumask_empty(policy->cpus);
39f963735aSViresh Kumar }
40f963735aSViresh Kumar 
41f963735aSViresh Kumar static bool suitable_policy(struct cpufreq_policy *policy, bool active)
42f963735aSViresh Kumar {
43f963735aSViresh Kumar 	return active == !policy_is_inactive(policy);
44f963735aSViresh Kumar }
45f963735aSViresh Kumar 
46f963735aSViresh Kumar /* Finds Next Acive/Inactive policy */
47f963735aSViresh Kumar static struct cpufreq_policy *next_policy(struct cpufreq_policy *policy,
48f963735aSViresh Kumar 					  bool active)
49f963735aSViresh Kumar {
50f963735aSViresh Kumar 	do {
51f963735aSViresh Kumar 		policy = list_next_entry(policy, policy_list);
52f963735aSViresh Kumar 
53f963735aSViresh Kumar 		/* No more policies in the list */
54f963735aSViresh Kumar 		if (&policy->policy_list == &cpufreq_policy_list)
55f963735aSViresh Kumar 			return NULL;
56f963735aSViresh Kumar 	} while (!suitable_policy(policy, active));
57f963735aSViresh Kumar 
58f963735aSViresh Kumar 	return policy;
59f963735aSViresh Kumar }
60f963735aSViresh Kumar 
61f963735aSViresh Kumar static struct cpufreq_policy *first_policy(bool active)
62f963735aSViresh Kumar {
63f963735aSViresh Kumar 	struct cpufreq_policy *policy;
64f963735aSViresh Kumar 
65f963735aSViresh Kumar 	/* No policies in the list */
66f963735aSViresh Kumar 	if (list_empty(&cpufreq_policy_list))
67f963735aSViresh Kumar 		return NULL;
68f963735aSViresh Kumar 
69f963735aSViresh Kumar 	policy = list_first_entry(&cpufreq_policy_list, typeof(*policy),
70f963735aSViresh Kumar 				  policy_list);
71f963735aSViresh Kumar 
72f963735aSViresh Kumar 	if (!suitable_policy(policy, active))
73f963735aSViresh Kumar 		policy = next_policy(policy, active);
74f963735aSViresh Kumar 
75f963735aSViresh Kumar 	return policy;
76f963735aSViresh Kumar }
77f963735aSViresh Kumar 
78f963735aSViresh Kumar /* Macros to iterate over CPU policies */
79f963735aSViresh Kumar #define for_each_suitable_policy(__policy, __active)	\
80f963735aSViresh Kumar 	for (__policy = first_policy(__active);		\
81f963735aSViresh Kumar 	     __policy;					\
82f963735aSViresh Kumar 	     __policy = next_policy(__policy, __active))
83f963735aSViresh Kumar 
84f963735aSViresh Kumar #define for_each_active_policy(__policy)		\
85f963735aSViresh Kumar 	for_each_suitable_policy(__policy, true)
86f963735aSViresh Kumar #define for_each_inactive_policy(__policy)		\
87f963735aSViresh Kumar 	for_each_suitable_policy(__policy, false)
88f963735aSViresh Kumar 
89b4f0676fSViresh Kumar #define for_each_policy(__policy)			\
90b4f0676fSViresh Kumar 	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
91b4f0676fSViresh Kumar 
92f7b27061SViresh Kumar /* Iterate over governors */
93f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list);
94f7b27061SViresh Kumar #define for_each_governor(__governor)				\
95f7b27061SViresh Kumar 	list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
96f7b27061SViresh Kumar 
971da177e4SLinus Torvalds /**
98cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
991da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
1001da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
1011da177e4SLinus Torvalds  */
1021c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
1037a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
104bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
1056f1e4efdSJane Li DEFINE_MUTEX(cpufreq_governor_lock);
106bb176f7dSViresh Kumar 
1072f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */
1082f0aea93SViresh Kumar static bool cpufreq_suspended;
1091da177e4SLinus Torvalds 
1109c0ebcf7SViresh Kumar static inline bool has_target(void)
1119c0ebcf7SViresh Kumar {
1129c0ebcf7SViresh Kumar 	return cpufreq_driver->target_index || cpufreq_driver->target;
1139c0ebcf7SViresh Kumar }
1149c0ebcf7SViresh Kumar 
1155a01f2e8SVenkatesh Pallipadi /*
1166eed9404SViresh Kumar  * rwsem to guarantee that cpufreq driver module doesn't unload during critical
1176eed9404SViresh Kumar  * sections
1186eed9404SViresh Kumar  */
1196eed9404SViresh Kumar static DECLARE_RWSEM(cpufreq_rwsem);
1206eed9404SViresh Kumar 
1211da177e4SLinus Torvalds /* internal prototypes */
12229464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy,
12329464f28SDave Jones 		unsigned int event);
124d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
12565f27f38SDavid Howells static void handle_update(struct work_struct *work);
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds /**
1281da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
1291da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
1301da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
1311da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
1321da177e4SLinus Torvalds  * The mutex locks both lists.
1331da177e4SLinus Torvalds  */
134e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
135b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list;
1361da177e4SLinus Torvalds 
13774212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called;
138b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void)
139b4dfdbb3SAlan Stern {
140b4dfdbb3SAlan Stern 	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
14174212ca4SCesar Eduardo Barros 	init_cpufreq_transition_notifier_list_called = true;
142b4dfdbb3SAlan Stern 	return 0;
143b4dfdbb3SAlan Stern }
144b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list);
1451da177e4SLinus Torvalds 
146a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
147da584455SViresh Kumar static int cpufreq_disabled(void)
148a7b422cdSKonrad Rzeszutek Wilk {
149a7b422cdSKonrad Rzeszutek Wilk 	return off;
150a7b422cdSKonrad Rzeszutek Wilk }
151a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
152a7b422cdSKonrad Rzeszutek Wilk {
153a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
154a7b422cdSKonrad Rzeszutek Wilk }
1553fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
1561da177e4SLinus Torvalds 
1574d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1584d5dcc42SViresh Kumar {
1590b981e70SViresh Kumar 	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
1604d5dcc42SViresh Kumar }
1613f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1624d5dcc42SViresh Kumar 
163944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
164944e9a03SViresh Kumar {
165944e9a03SViresh Kumar 	if (have_governor_per_policy())
166944e9a03SViresh Kumar 		return &policy->kobj;
167944e9a03SViresh Kumar 	else
168944e9a03SViresh Kumar 		return cpufreq_global_kobject;
169944e9a03SViresh Kumar }
170944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
171944e9a03SViresh Kumar 
17272a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
17372a4ce34SViresh Kumar {
17472a4ce34SViresh Kumar 	u64 idle_time;
17572a4ce34SViresh Kumar 	u64 cur_wall_time;
17672a4ce34SViresh Kumar 	u64 busy_time;
17772a4ce34SViresh Kumar 
17872a4ce34SViresh Kumar 	cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
17972a4ce34SViresh Kumar 
18072a4ce34SViresh Kumar 	busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
18172a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
18272a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
18372a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
18472a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
18572a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
18672a4ce34SViresh Kumar 
18772a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
18872a4ce34SViresh Kumar 	if (wall)
18972a4ce34SViresh Kumar 		*wall = cputime_to_usecs(cur_wall_time);
19072a4ce34SViresh Kumar 
19172a4ce34SViresh Kumar 	return cputime_to_usecs(idle_time);
19272a4ce34SViresh Kumar }
19372a4ce34SViresh Kumar 
19472a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
19572a4ce34SViresh Kumar {
19672a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
19772a4ce34SViresh Kumar 
19872a4ce34SViresh Kumar 	if (idle_time == -1ULL)
19972a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
20072a4ce34SViresh Kumar 	else if (!io_busy)
20172a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
20272a4ce34SViresh Kumar 
20372a4ce34SViresh Kumar 	return idle_time;
20472a4ce34SViresh Kumar }
20572a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
20672a4ce34SViresh Kumar 
20770e9e778SViresh Kumar /*
20870e9e778SViresh Kumar  * This is a generic cpufreq init() routine which can be used by cpufreq
20970e9e778SViresh Kumar  * drivers of SMP systems. It will do following:
21070e9e778SViresh Kumar  * - validate & show freq table passed
21170e9e778SViresh Kumar  * - set policies transition latency
21270e9e778SViresh Kumar  * - policy->cpus with all possible CPUs
21370e9e778SViresh Kumar  */
21470e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy,
21570e9e778SViresh Kumar 		struct cpufreq_frequency_table *table,
21670e9e778SViresh Kumar 		unsigned int transition_latency)
21770e9e778SViresh Kumar {
21870e9e778SViresh Kumar 	int ret;
21970e9e778SViresh Kumar 
22070e9e778SViresh Kumar 	ret = cpufreq_table_validate_and_show(policy, table);
22170e9e778SViresh Kumar 	if (ret) {
22270e9e778SViresh Kumar 		pr_err("%s: invalid frequency table: %d\n", __func__, ret);
22370e9e778SViresh Kumar 		return ret;
22470e9e778SViresh Kumar 	}
22570e9e778SViresh Kumar 
22670e9e778SViresh Kumar 	policy->cpuinfo.transition_latency = transition_latency;
22770e9e778SViresh Kumar 
22870e9e778SViresh Kumar 	/*
22958405af6SShailendra Verma 	 * The driver only supports the SMP configuration where all processors
23070e9e778SViresh Kumar 	 * share the clock and voltage and clock.
23170e9e778SViresh Kumar 	 */
23270e9e778SViresh Kumar 	cpumask_setall(policy->cpus);
23370e9e778SViresh Kumar 
23470e9e778SViresh Kumar 	return 0;
23570e9e778SViresh Kumar }
23670e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init);
23770e9e778SViresh Kumar 
238988bed09SViresh Kumar /* Only for cpufreq core internal use */
239988bed09SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
240652ed95dSViresh Kumar {
241652ed95dSViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
242652ed95dSViresh Kumar 
243988bed09SViresh Kumar 	return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
244988bed09SViresh Kumar }
245988bed09SViresh Kumar 
246988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu)
247988bed09SViresh Kumar {
248988bed09SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
249988bed09SViresh Kumar 
250652ed95dSViresh Kumar 	if (!policy || IS_ERR(policy->clk)) {
251e837f9b5SJoe Perches 		pr_err("%s: No %s associated to cpu: %d\n",
252e837f9b5SJoe Perches 		       __func__, policy ? "clk" : "policy", cpu);
253652ed95dSViresh Kumar 		return 0;
254652ed95dSViresh Kumar 	}
255652ed95dSViresh Kumar 
256652ed95dSViresh Kumar 	return clk_get_rate(policy->clk) / 1000;
257652ed95dSViresh Kumar }
258652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get);
259652ed95dSViresh Kumar 
26050e9c852SViresh Kumar /**
26150e9c852SViresh Kumar  * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
26250e9c852SViresh Kumar  *
26350e9c852SViresh Kumar  * @cpu: cpu to find policy for.
26450e9c852SViresh Kumar  *
26550e9c852SViresh Kumar  * This returns policy for 'cpu', returns NULL if it doesn't exist.
26650e9c852SViresh Kumar  * It also increments the kobject reference count to mark it busy and so would
26750e9c852SViresh Kumar  * require a corresponding call to cpufreq_cpu_put() to decrement it back.
26850e9c852SViresh Kumar  * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
26950e9c852SViresh Kumar  * freed as that depends on the kobj count.
27050e9c852SViresh Kumar  *
27150e9c852SViresh Kumar  * It also takes a read-lock of 'cpufreq_rwsem' and doesn't put it back if a
27250e9c852SViresh Kumar  * valid policy is found. This is done to make sure the driver doesn't get
27350e9c852SViresh Kumar  * unregistered while the policy is being used.
27450e9c852SViresh Kumar  *
27550e9c852SViresh Kumar  * Return: A valid policy on success, otherwise NULL on failure.
27650e9c852SViresh Kumar  */
2776eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
2781da177e4SLinus Torvalds {
2796eed9404SViresh Kumar 	struct cpufreq_policy *policy = NULL;
2801da177e4SLinus Torvalds 	unsigned long flags;
2811da177e4SLinus Torvalds 
2821b947c90SViresh Kumar 	if (WARN_ON(cpu >= nr_cpu_ids))
2836eed9404SViresh Kumar 		return NULL;
2846eed9404SViresh Kumar 
2856eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
2866eed9404SViresh Kumar 		return NULL;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	/* get the cpufreq driver */
2890d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
2901da177e4SLinus Torvalds 
2916eed9404SViresh Kumar 	if (cpufreq_driver) {
2921da177e4SLinus Torvalds 		/* get the CPU */
293988bed09SViresh Kumar 		policy = cpufreq_cpu_get_raw(cpu);
2946eed9404SViresh Kumar 		if (policy)
2956eed9404SViresh Kumar 			kobject_get(&policy->kobj);
2966eed9404SViresh Kumar 	}
2976eed9404SViresh Kumar 
2986eed9404SViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2991da177e4SLinus Torvalds 
3003a3e9e06SViresh Kumar 	if (!policy)
3016eed9404SViresh Kumar 		up_read(&cpufreq_rwsem);
3021da177e4SLinus Torvalds 
3033a3e9e06SViresh Kumar 	return policy;
304a9144436SStephen Boyd }
3051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
3061da177e4SLinus Torvalds 
30750e9c852SViresh Kumar /**
30850e9c852SViresh Kumar  * cpufreq_cpu_put: Decrements the usage count of a policy
30950e9c852SViresh Kumar  *
31050e9c852SViresh Kumar  * @policy: policy earlier returned by cpufreq_cpu_get().
31150e9c852SViresh Kumar  *
31250e9c852SViresh Kumar  * This decrements the kobject reference count incremented earlier by calling
31350e9c852SViresh Kumar  * cpufreq_cpu_get().
31450e9c852SViresh Kumar  *
31550e9c852SViresh Kumar  * It also drops the read-lock of 'cpufreq_rwsem' taken at cpufreq_cpu_get().
31650e9c852SViresh Kumar  */
3173a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
318a9144436SStephen Boyd {
3196eed9404SViresh Kumar 	kobject_put(&policy->kobj);
3206eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
321a9144436SStephen Boyd }
3221da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds /*********************************************************************
3251da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
3261da177e4SLinus Torvalds  *********************************************************************/
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds /**
3291da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
3301da177e4SLinus Torvalds  *
3311da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
3321da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
3331da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
3341da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
3351da177e4SLinus Torvalds  */
33639c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
33739c132eeSViresh Kumar {
3381da177e4SLinus Torvalds #ifndef CONFIG_SMP
3391da177e4SLinus Torvalds 	static unsigned long l_p_j_ref;
3401da177e4SLinus Torvalds 	static unsigned int l_p_j_ref_freq;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
3431da177e4SLinus Torvalds 		return;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
3461da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
3471da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
348e837f9b5SJoe Perches 		pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
349e837f9b5SJoe Perches 			 l_p_j_ref, l_p_j_ref_freq);
3501da177e4SLinus Torvalds 	}
3510b443eadSViresh Kumar 	if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
352e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
353e08f5f5bSGautham R Shenoy 								ci->new);
354e837f9b5SJoe Perches 		pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
355e837f9b5SJoe Perches 			 loops_per_jiffy, ci->new);
3561da177e4SLinus Torvalds 	}
3571da177e4SLinus Torvalds #endif
35839c132eeSViresh Kumar }
3591da177e4SLinus Torvalds 
3600956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
361b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
3621da177e4SLinus Torvalds {
3631da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
3641da177e4SLinus Torvalds 
365d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
366d5aaffa9SDirk Brandewie 		return;
367d5aaffa9SDirk Brandewie 
3681c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
3692d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
370e4472cb3SDave Jones 		 state, freqs->new);
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 	switch (state) {
373e4472cb3SDave Jones 
3741da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
375e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
376e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
377e4472cb3SDave Jones 		 * "old frequency".
3781da177e4SLinus Torvalds 		 */
3791c3d85ddSRafael J. Wysocki 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
380e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
381e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
382e837f9b5SJoe Perches 				pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
383e4472cb3SDave Jones 					 freqs->old, policy->cur);
384e4472cb3SDave Jones 				freqs->old = policy->cur;
3851da177e4SLinus Torvalds 			}
3861da177e4SLinus Torvalds 		}
387b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
388e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
3891da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3901da177e4SLinus Torvalds 		break;
391e4472cb3SDave Jones 
3921da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3931da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
394e837f9b5SJoe Perches 		pr_debug("FREQ: %lu - CPU: %lu\n",
395e837f9b5SJoe Perches 			 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
39625e41933SThomas Renninger 		trace_cpu_frequency(freqs->new, freqs->cpu);
397b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
398e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
399e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
400e4472cb3SDave Jones 			policy->cur = freqs->new;
4011da177e4SLinus Torvalds 		break;
4021da177e4SLinus Torvalds 	}
4031da177e4SLinus Torvalds }
404bb176f7dSViresh Kumar 
405b43a7ffbSViresh Kumar /**
406b43a7ffbSViresh Kumar  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
407b43a7ffbSViresh Kumar  * on frequency transition.
408b43a7ffbSViresh Kumar  *
409b43a7ffbSViresh Kumar  * This function calls the transition notifiers and the "adjust_jiffies"
410b43a7ffbSViresh Kumar  * function. It is called twice on all CPU frequency changes that have
411b43a7ffbSViresh Kumar  * external effects.
412b43a7ffbSViresh Kumar  */
413236a9800SViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy,
414b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
415b43a7ffbSViresh Kumar {
416b43a7ffbSViresh Kumar 	for_each_cpu(freqs->cpu, policy->cpus)
417b43a7ffbSViresh Kumar 		__cpufreq_notify_transition(policy, freqs, state);
418b43a7ffbSViresh Kumar }
4191da177e4SLinus Torvalds 
420f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */
421236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
422f7ba3b41SViresh Kumar 		struct cpufreq_freqs *freqs, int transition_failed)
423f7ba3b41SViresh Kumar {
424f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
425f7ba3b41SViresh Kumar 	if (!transition_failed)
426f7ba3b41SViresh Kumar 		return;
427f7ba3b41SViresh Kumar 
428f7ba3b41SViresh Kumar 	swap(freqs->old, freqs->new);
429f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
430f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
431f7ba3b41SViresh Kumar }
432f7ba3b41SViresh Kumar 
43312478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
43412478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs)
43512478cf0SSrivatsa S. Bhat {
436ca654dc3SSrivatsa S. Bhat 
437ca654dc3SSrivatsa S. Bhat 	/*
438ca654dc3SSrivatsa S. Bhat 	 * Catch double invocations of _begin() which lead to self-deadlock.
439ca654dc3SSrivatsa S. Bhat 	 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
440ca654dc3SSrivatsa S. Bhat 	 * doesn't invoke _begin() on their behalf, and hence the chances of
441ca654dc3SSrivatsa S. Bhat 	 * double invocations are very low. Moreover, there are scenarios
442ca654dc3SSrivatsa S. Bhat 	 * where these checks can emit false-positive warnings in these
443ca654dc3SSrivatsa S. Bhat 	 * drivers; so we avoid that by skipping them altogether.
444ca654dc3SSrivatsa S. Bhat 	 */
445ca654dc3SSrivatsa S. Bhat 	WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
446ca654dc3SSrivatsa S. Bhat 				&& current == policy->transition_task);
447ca654dc3SSrivatsa S. Bhat 
44812478cf0SSrivatsa S. Bhat wait:
44912478cf0SSrivatsa S. Bhat 	wait_event(policy->transition_wait, !policy->transition_ongoing);
45012478cf0SSrivatsa S. Bhat 
45112478cf0SSrivatsa S. Bhat 	spin_lock(&policy->transition_lock);
45212478cf0SSrivatsa S. Bhat 
45312478cf0SSrivatsa S. Bhat 	if (unlikely(policy->transition_ongoing)) {
45412478cf0SSrivatsa S. Bhat 		spin_unlock(&policy->transition_lock);
45512478cf0SSrivatsa S. Bhat 		goto wait;
45612478cf0SSrivatsa S. Bhat 	}
45712478cf0SSrivatsa S. Bhat 
45812478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = true;
459ca654dc3SSrivatsa S. Bhat 	policy->transition_task = current;
46012478cf0SSrivatsa S. Bhat 
46112478cf0SSrivatsa S. Bhat 	spin_unlock(&policy->transition_lock);
46212478cf0SSrivatsa S. Bhat 
46312478cf0SSrivatsa S. Bhat 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
46412478cf0SSrivatsa S. Bhat }
46512478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
46612478cf0SSrivatsa S. Bhat 
46712478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
46812478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs, int transition_failed)
46912478cf0SSrivatsa S. Bhat {
47012478cf0SSrivatsa S. Bhat 	if (unlikely(WARN_ON(!policy->transition_ongoing)))
47112478cf0SSrivatsa S. Bhat 		return;
47212478cf0SSrivatsa S. Bhat 
47312478cf0SSrivatsa S. Bhat 	cpufreq_notify_post_transition(policy, freqs, transition_failed);
47412478cf0SSrivatsa S. Bhat 
47512478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = false;
476ca654dc3SSrivatsa S. Bhat 	policy->transition_task = NULL;
47712478cf0SSrivatsa S. Bhat 
47812478cf0SSrivatsa S. Bhat 	wake_up(&policy->transition_wait);
47912478cf0SSrivatsa S. Bhat }
48012478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
48112478cf0SSrivatsa S. Bhat 
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds /*********************************************************************
4841da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
4851da177e4SLinus Torvalds  *********************************************************************/
4868a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj,
4876f19efc0SLukasz Majewski 				 struct attribute *attr, char *buf)
4886f19efc0SLukasz Majewski {
4896f19efc0SLukasz Majewski 	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
4906f19efc0SLukasz Majewski }
4916f19efc0SLukasz Majewski 
4926f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
4936f19efc0SLukasz Majewski 				  const char *buf, size_t count)
4946f19efc0SLukasz Majewski {
4956f19efc0SLukasz Majewski 	int ret, enable;
4966f19efc0SLukasz Majewski 
4976f19efc0SLukasz Majewski 	ret = sscanf(buf, "%d", &enable);
4986f19efc0SLukasz Majewski 	if (ret != 1 || enable < 0 || enable > 1)
4996f19efc0SLukasz Majewski 		return -EINVAL;
5006f19efc0SLukasz Majewski 
5016f19efc0SLukasz Majewski 	if (cpufreq_boost_trigger_state(enable)) {
502e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST!\n",
503e837f9b5SJoe Perches 		       __func__, enable ? "enable" : "disable");
5046f19efc0SLukasz Majewski 		return -EINVAL;
5056f19efc0SLukasz Majewski 	}
5066f19efc0SLukasz Majewski 
507e837f9b5SJoe Perches 	pr_debug("%s: cpufreq BOOST %s\n",
508e837f9b5SJoe Perches 		 __func__, enable ? "enabled" : "disabled");
5096f19efc0SLukasz Majewski 
5106f19efc0SLukasz Majewski 	return count;
5116f19efc0SLukasz Majewski }
5126f19efc0SLukasz Majewski define_one_global_rw(boost);
5131da177e4SLinus Torvalds 
51442f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor)
5153bcb09a3SJeremy Fitzhardinge {
5163bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
5173bcb09a3SJeremy Fitzhardinge 
518f7b27061SViresh Kumar 	for_each_governor(t)
5197c4f4539SRasmus Villemoes 		if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
5203bcb09a3SJeremy Fitzhardinge 			return t;
5213bcb09a3SJeremy Fitzhardinge 
5223bcb09a3SJeremy Fitzhardinge 	return NULL;
5233bcb09a3SJeremy Fitzhardinge }
5243bcb09a3SJeremy Fitzhardinge 
5251da177e4SLinus Torvalds /**
5261da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
5271da177e4SLinus Torvalds  */
5281da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
5291da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
5301da177e4SLinus Torvalds {
5313bcb09a3SJeremy Fitzhardinge 	int err = -EINVAL;
5323bcb09a3SJeremy Fitzhardinge 
5331c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver)
5343bcb09a3SJeremy Fitzhardinge 		goto out;
5353bcb09a3SJeremy Fitzhardinge 
5361c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
5377c4f4539SRasmus Villemoes 		if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
5381da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
5393bcb09a3SJeremy Fitzhardinge 			err = 0;
5407c4f4539SRasmus Villemoes 		} else if (!strncasecmp(str_governor, "powersave",
541e08f5f5bSGautham R Shenoy 						CPUFREQ_NAME_LEN)) {
5421da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
5433bcb09a3SJeremy Fitzhardinge 			err = 0;
5441da177e4SLinus Torvalds 		}
5452e1cc3a5SViresh Kumar 	} else {
5461da177e4SLinus Torvalds 		struct cpufreq_governor *t;
5473bcb09a3SJeremy Fitzhardinge 
5483fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
5493bcb09a3SJeremy Fitzhardinge 
55042f91fa1SViresh Kumar 		t = find_governor(str_governor);
5513bcb09a3SJeremy Fitzhardinge 
552ea714970SJeremy Fitzhardinge 		if (t == NULL) {
553ea714970SJeremy Fitzhardinge 			int ret;
554ea714970SJeremy Fitzhardinge 
555ea714970SJeremy Fitzhardinge 			mutex_unlock(&cpufreq_governor_mutex);
5561a8e1463SKees Cook 			ret = request_module("cpufreq_%s", str_governor);
557ea714970SJeremy Fitzhardinge 			mutex_lock(&cpufreq_governor_mutex);
558ea714970SJeremy Fitzhardinge 
559ea714970SJeremy Fitzhardinge 			if (ret == 0)
56042f91fa1SViresh Kumar 				t = find_governor(str_governor);
561ea714970SJeremy Fitzhardinge 		}
562ea714970SJeremy Fitzhardinge 
5633bcb09a3SJeremy Fitzhardinge 		if (t != NULL) {
5641da177e4SLinus Torvalds 			*governor = t;
5653bcb09a3SJeremy Fitzhardinge 			err = 0;
5661da177e4SLinus Torvalds 		}
5673bcb09a3SJeremy Fitzhardinge 
5683bcb09a3SJeremy Fitzhardinge 		mutex_unlock(&cpufreq_governor_mutex);
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds out:
5713bcb09a3SJeremy Fitzhardinge 	return err;
5721da177e4SLinus Torvalds }
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds /**
575e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
576e08f5f5bSGautham R Shenoy  * print out cpufreq information
5771da177e4SLinus Torvalds  *
5781da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
5791da177e4SLinus Torvalds  * "unsigned int".
5801da177e4SLinus Torvalds  */
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds #define show_one(file_name, object)			\
5831da177e4SLinus Torvalds static ssize_t show_##file_name				\
5841da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
5851da177e4SLinus Torvalds {							\
5861da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
5871da177e4SLinus Torvalds }
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
5901da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
591ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
5921da177e4SLinus Torvalds show_one(scaling_min_freq, min);
5931da177e4SLinus Torvalds show_one(scaling_max_freq, max);
594c034b02eSDirk Brandewie 
59509347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
596c034b02eSDirk Brandewie {
597c034b02eSDirk Brandewie 	ssize_t ret;
598c034b02eSDirk Brandewie 
599c034b02eSDirk Brandewie 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
600c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
601c034b02eSDirk Brandewie 	else
602c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", policy->cur);
603c034b02eSDirk Brandewie 	return ret;
604c034b02eSDirk Brandewie }
6051da177e4SLinus Torvalds 
606037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
6073a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy);
6087970e08bSThomas Renninger 
6091da177e4SLinus Torvalds /**
6101da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
6111da177e4SLinus Torvalds  */
6121da177e4SLinus Torvalds #define store_one(file_name, object)			\
6131da177e4SLinus Torvalds static ssize_t store_##file_name					\
6141da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
6151da177e4SLinus Torvalds {									\
616619c144cSVince Hsu 	int ret, temp;							\
6171da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
6181da177e4SLinus Torvalds 									\
6191da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
6201da177e4SLinus Torvalds 	if (ret)							\
6211da177e4SLinus Torvalds 		return -EINVAL;						\
6221da177e4SLinus Torvalds 									\
6231da177e4SLinus Torvalds 	ret = sscanf(buf, "%u", &new_policy.object);			\
6241da177e4SLinus Torvalds 	if (ret != 1)							\
6251da177e4SLinus Torvalds 		return -EINVAL;						\
6261da177e4SLinus Torvalds 									\
627619c144cSVince Hsu 	temp = new_policy.object;					\
628037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);		\
629619c144cSVince Hsu 	if (!ret)							\
630619c144cSVince Hsu 		policy->user_policy.object = temp;			\
6311da177e4SLinus Torvalds 									\
6321da177e4SLinus Torvalds 	return ret ? ret : count;					\
6331da177e4SLinus Torvalds }
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds store_one(scaling_min_freq, min);
6361da177e4SLinus Torvalds store_one(scaling_max_freq, max);
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds /**
6391da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
6401da177e4SLinus Torvalds  */
641e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
642e08f5f5bSGautham R Shenoy 					char *buf)
6431da177e4SLinus Torvalds {
644d92d50a4SViresh Kumar 	unsigned int cur_freq = __cpufreq_get(policy);
6451da177e4SLinus Torvalds 	if (!cur_freq)
6461da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
6471da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
6481da177e4SLinus Torvalds }
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds /**
6511da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
6521da177e4SLinus Torvalds  */
653905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
6541da177e4SLinus Torvalds {
6551da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
6561da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
6571da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
6581da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
6591da177e4SLinus Torvalds 	else if (policy->governor)
6604b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
66129464f28SDave Jones 				policy->governor->name);
6621da177e4SLinus Torvalds 	return -EINVAL;
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds /**
6661da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
6671da177e4SLinus Torvalds  */
6681da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
6691da177e4SLinus Torvalds 					const char *buf, size_t count)
6701da177e4SLinus Torvalds {
6715136fa56SSrivatsa S. Bhat 	int ret;
6721da177e4SLinus Torvalds 	char	str_governor[16];
6731da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);
6761da177e4SLinus Torvalds 	if (ret)
6771da177e4SLinus Torvalds 		return ret;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
6801da177e4SLinus Torvalds 	if (ret != 1)
6811da177e4SLinus Torvalds 		return -EINVAL;
6821da177e4SLinus Torvalds 
683e08f5f5bSGautham R Shenoy 	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
684e08f5f5bSGautham R Shenoy 						&new_policy.governor))
6851da177e4SLinus Torvalds 		return -EINVAL;
6861da177e4SLinus Torvalds 
687037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
6887970e08bSThomas Renninger 
6897970e08bSThomas Renninger 	policy->user_policy.policy = policy->policy;
6907970e08bSThomas Renninger 	policy->user_policy.governor = policy->governor;
6917970e08bSThomas Renninger 
692e08f5f5bSGautham R Shenoy 	if (ret)
693e08f5f5bSGautham R Shenoy 		return ret;
694e08f5f5bSGautham R Shenoy 	else
695e08f5f5bSGautham R Shenoy 		return count;
6961da177e4SLinus Torvalds }
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds /**
6991da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
7001da177e4SLinus Torvalds  */
7011da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
7021da177e4SLinus Torvalds {
7031c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds /**
7071da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
7081da177e4SLinus Torvalds  */
7091da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
7101da177e4SLinus Torvalds 						char *buf)
7111da177e4SLinus Torvalds {
7121da177e4SLinus Torvalds 	ssize_t i = 0;
7131da177e4SLinus Torvalds 	struct cpufreq_governor *t;
7141da177e4SLinus Torvalds 
7159c0ebcf7SViresh Kumar 	if (!has_target()) {
7161da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
7171da177e4SLinus Torvalds 		goto out;
7181da177e4SLinus Torvalds 	}
7191da177e4SLinus Torvalds 
720f7b27061SViresh Kumar 	for_each_governor(t) {
72129464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
72229464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
7231da177e4SLinus Torvalds 			goto out;
7244b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
7251da177e4SLinus Torvalds 	}
7261da177e4SLinus Torvalds out:
7271da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
7281da177e4SLinus Torvalds 	return i;
7291da177e4SLinus Torvalds }
730e8628dd0SDarrick J. Wong 
731f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
7321da177e4SLinus Torvalds {
7331da177e4SLinus Torvalds 	ssize_t i = 0;
7341da177e4SLinus Torvalds 	unsigned int cpu;
7351da177e4SLinus Torvalds 
736835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
7371da177e4SLinus Torvalds 		if (i)
7381da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
7391da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
7401da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
7411da177e4SLinus Torvalds 			break;
7421da177e4SLinus Torvalds 	}
7431da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
7441da177e4SLinus Torvalds 	return i;
7451da177e4SLinus Torvalds }
746f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
7471da177e4SLinus Torvalds 
748e8628dd0SDarrick J. Wong /**
749e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
750e8628dd0SDarrick J. Wong  * hw coordination is in use
751e8628dd0SDarrick J. Wong  */
752e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
753e8628dd0SDarrick J. Wong {
754f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
755e8628dd0SDarrick J. Wong }
756e8628dd0SDarrick J. Wong 
757e8628dd0SDarrick J. Wong /**
758e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
759e8628dd0SDarrick J. Wong  */
760e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
761e8628dd0SDarrick J. Wong {
762f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
763e8628dd0SDarrick J. Wong }
764e8628dd0SDarrick J. Wong 
7659e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
7669e76988eSVenki Pallipadi 					const char *buf, size_t count)
7679e76988eSVenki Pallipadi {
7689e76988eSVenki Pallipadi 	unsigned int freq = 0;
7699e76988eSVenki Pallipadi 	unsigned int ret;
7709e76988eSVenki Pallipadi 
771879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
7729e76988eSVenki Pallipadi 		return -EINVAL;
7739e76988eSVenki Pallipadi 
7749e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
7759e76988eSVenki Pallipadi 	if (ret != 1)
7769e76988eSVenki Pallipadi 		return -EINVAL;
7779e76988eSVenki Pallipadi 
7789e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
7799e76988eSVenki Pallipadi 
7809e76988eSVenki Pallipadi 	return count;
7819e76988eSVenki Pallipadi }
7829e76988eSVenki Pallipadi 
7839e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
7849e76988eSVenki Pallipadi {
785879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
7869e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
7879e76988eSVenki Pallipadi 
7889e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
7899e76988eSVenki Pallipadi }
7901da177e4SLinus Torvalds 
791e2f74f35SThomas Renninger /**
7928bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
793e2f74f35SThomas Renninger  */
794e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
795e2f74f35SThomas Renninger {
796e2f74f35SThomas Renninger 	unsigned int limit;
797e2f74f35SThomas Renninger 	int ret;
7981c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
7991c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
800e2f74f35SThomas Renninger 		if (!ret)
801e2f74f35SThomas Renninger 			return sprintf(buf, "%u\n", limit);
802e2f74f35SThomas Renninger 	}
803e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
804e2f74f35SThomas Renninger }
805e2f74f35SThomas Renninger 
8066dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
8076dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
8086dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
8096dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
8106dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
8116dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
8126dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
8136dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
8146dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
8156dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
8166dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
8176dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
8186dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
8196dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds static struct attribute *default_attrs[] = {
8221da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
8231da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
824ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
8251da177e4SLinus Torvalds 	&scaling_min_freq.attr,
8261da177e4SLinus Torvalds 	&scaling_max_freq.attr,
8271da177e4SLinus Torvalds 	&affected_cpus.attr,
828e8628dd0SDarrick J. Wong 	&related_cpus.attr,
8291da177e4SLinus Torvalds 	&scaling_governor.attr,
8301da177e4SLinus Torvalds 	&scaling_driver.attr,
8311da177e4SLinus Torvalds 	&scaling_available_governors.attr,
8329e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
8331da177e4SLinus Torvalds 	NULL
8341da177e4SLinus Torvalds };
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
8371da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
8401da177e4SLinus Torvalds {
8411da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
8421da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
8431b750e3bSViresh Kumar 	ssize_t ret;
8446eed9404SViresh Kumar 
8456eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
8461b750e3bSViresh Kumar 		return -EINVAL;
8475a01f2e8SVenkatesh Pallipadi 
848ad7722daSviresh kumar 	down_read(&policy->rwsem);
8495a01f2e8SVenkatesh Pallipadi 
850e08f5f5bSGautham R Shenoy 	if (fattr->show)
851e08f5f5bSGautham R Shenoy 		ret = fattr->show(policy, buf);
852e08f5f5bSGautham R Shenoy 	else
853e08f5f5bSGautham R Shenoy 		ret = -EIO;
854e08f5f5bSGautham R Shenoy 
855ad7722daSviresh kumar 	up_read(&policy->rwsem);
8566eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
8571b750e3bSViresh Kumar 
8581da177e4SLinus Torvalds 	return ret;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
8621da177e4SLinus Torvalds 		     const char *buf, size_t count)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
8651da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
866a07530b4SDave Jones 	ssize_t ret = -EINVAL;
8676eed9404SViresh Kumar 
8684f750c93SSrivatsa S. Bhat 	get_online_cpus();
8694f750c93SSrivatsa S. Bhat 
8704f750c93SSrivatsa S. Bhat 	if (!cpu_online(policy->cpu))
8714f750c93SSrivatsa S. Bhat 		goto unlock;
8724f750c93SSrivatsa S. Bhat 
8736eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
8744f750c93SSrivatsa S. Bhat 		goto unlock;
8755a01f2e8SVenkatesh Pallipadi 
876ad7722daSviresh kumar 	down_write(&policy->rwsem);
8775a01f2e8SVenkatesh Pallipadi 
878e08f5f5bSGautham R Shenoy 	if (fattr->store)
879e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
880e08f5f5bSGautham R Shenoy 	else
881e08f5f5bSGautham R Shenoy 		ret = -EIO;
882e08f5f5bSGautham R Shenoy 
883ad7722daSviresh kumar 	up_write(&policy->rwsem);
8846eed9404SViresh Kumar 
8856eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
8864f750c93SSrivatsa S. Bhat unlock:
8874f750c93SSrivatsa S. Bhat 	put_online_cpus();
8884f750c93SSrivatsa S. Bhat 
8891da177e4SLinus Torvalds 	return ret;
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
8931da177e4SLinus Torvalds {
8941da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
8952d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
8961da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
8971da177e4SLinus Torvalds }
8981da177e4SLinus Torvalds 
89952cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
9001da177e4SLinus Torvalds 	.show	= show,
9011da177e4SLinus Torvalds 	.store	= store,
9021da177e4SLinus Torvalds };
9031da177e4SLinus Torvalds 
9041da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
9051da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
9061da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
9071da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
9081da177e4SLinus Torvalds };
9091da177e4SLinus Torvalds 
9102361be23SViresh Kumar struct kobject *cpufreq_global_kobject;
9112361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject);
9122361be23SViresh Kumar 
9132361be23SViresh Kumar static int cpufreq_global_kobject_usage;
9142361be23SViresh Kumar 
9152361be23SViresh Kumar int cpufreq_get_global_kobject(void)
9162361be23SViresh Kumar {
9172361be23SViresh Kumar 	if (!cpufreq_global_kobject_usage++)
9182361be23SViresh Kumar 		return kobject_add(cpufreq_global_kobject,
9192361be23SViresh Kumar 				&cpu_subsys.dev_root->kobj, "%s", "cpufreq");
9202361be23SViresh Kumar 
9212361be23SViresh Kumar 	return 0;
9222361be23SViresh Kumar }
9232361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject);
9242361be23SViresh Kumar 
9252361be23SViresh Kumar void cpufreq_put_global_kobject(void)
9262361be23SViresh Kumar {
9272361be23SViresh Kumar 	if (!--cpufreq_global_kobject_usage)
9282361be23SViresh Kumar 		kobject_del(cpufreq_global_kobject);
9292361be23SViresh Kumar }
9302361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject);
9312361be23SViresh Kumar 
9322361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr)
9332361be23SViresh Kumar {
9342361be23SViresh Kumar 	int ret = cpufreq_get_global_kobject();
9352361be23SViresh Kumar 
9362361be23SViresh Kumar 	if (!ret) {
9372361be23SViresh Kumar 		ret = sysfs_create_file(cpufreq_global_kobject, attr);
9382361be23SViresh Kumar 		if (ret)
9392361be23SViresh Kumar 			cpufreq_put_global_kobject();
9402361be23SViresh Kumar 	}
9412361be23SViresh Kumar 
9422361be23SViresh Kumar 	return ret;
9432361be23SViresh Kumar }
9442361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file);
9452361be23SViresh Kumar 
9462361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr)
9472361be23SViresh Kumar {
9482361be23SViresh Kumar 	sysfs_remove_file(cpufreq_global_kobject, attr);
9492361be23SViresh Kumar 	cpufreq_put_global_kobject();
9502361be23SViresh Kumar }
9512361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
9522361be23SViresh Kumar 
95319d6f7ecSDave Jones /* symlink affected CPUs */
954308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
95519d6f7ecSDave Jones {
95619d6f7ecSDave Jones 	unsigned int j;
95719d6f7ecSDave Jones 	int ret = 0;
95819d6f7ecSDave Jones 
95919d6f7ecSDave Jones 	for_each_cpu(j, policy->cpus) {
9608a25a2fdSKay Sievers 		struct device *cpu_dev;
96119d6f7ecSDave Jones 
962*9d16f207SSaravana Kannan 		if (j == policy->kobj_cpu)
96319d6f7ecSDave Jones 			continue;
96419d6f7ecSDave Jones 
965e8fdde10SViresh Kumar 		pr_debug("Adding link for CPU: %u\n", j);
9668a25a2fdSKay Sievers 		cpu_dev = get_cpu_device(j);
9678a25a2fdSKay Sievers 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
96819d6f7ecSDave Jones 					"cpufreq");
96971c3461eSRafael J. Wysocki 		if (ret)
97071c3461eSRafael J. Wysocki 			break;
97119d6f7ecSDave Jones 	}
97219d6f7ecSDave Jones 	return ret;
97319d6f7ecSDave Jones }
97419d6f7ecSDave Jones 
975308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
9768a25a2fdSKay Sievers 				     struct device *dev)
977909a694eSDave Jones {
978909a694eSDave Jones 	struct freq_attr **drv_attr;
979909a694eSDave Jones 	int ret = 0;
980909a694eSDave Jones 
981909a694eSDave Jones 	/* set up files for this cpu device */
9821c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
983f13f1184SViresh Kumar 	while (drv_attr && *drv_attr) {
984909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
985909a694eSDave Jones 		if (ret)
9866d4e81edSTomeu Vizoso 			return ret;
987909a694eSDave Jones 		drv_attr++;
988909a694eSDave Jones 	}
9891c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
990909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
991909a694eSDave Jones 		if (ret)
9926d4e81edSTomeu Vizoso 			return ret;
993909a694eSDave Jones 	}
994c034b02eSDirk Brandewie 
995909a694eSDave Jones 	ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
996909a694eSDave Jones 	if (ret)
9976d4e81edSTomeu Vizoso 		return ret;
998c034b02eSDirk Brandewie 
9991c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
1000e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
1001e2f74f35SThomas Renninger 		if (ret)
10026d4e81edSTomeu Vizoso 			return ret;
1003e2f74f35SThomas Renninger 	}
1004909a694eSDave Jones 
10056d4e81edSTomeu Vizoso 	return cpufreq_add_dev_symlink(policy);
1006e18f1682SSrivatsa S. Bhat }
1007e18f1682SSrivatsa S. Bhat 
1008e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy)
1009e18f1682SSrivatsa S. Bhat {
10106e2c89d1Sviresh kumar 	struct cpufreq_governor *gov = NULL;
1011e18f1682SSrivatsa S. Bhat 	struct cpufreq_policy new_policy;
1012e18f1682SSrivatsa S. Bhat 	int ret = 0;
1013e18f1682SSrivatsa S. Bhat 
1014d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
1015a27a9ab7SJason Baron 
10166e2c89d1Sviresh kumar 	/* Update governor of new_policy to the governor used before hotplug */
10174573237bSViresh Kumar 	gov = find_governor(policy->last_governor);
10186e2c89d1Sviresh kumar 	if (gov)
10196e2c89d1Sviresh kumar 		pr_debug("Restoring governor %s for cpu %d\n",
10206e2c89d1Sviresh kumar 				policy->governor->name, policy->cpu);
10216e2c89d1Sviresh kumar 	else
10226e2c89d1Sviresh kumar 		gov = CPUFREQ_DEFAULT_GOVERNOR;
10236e2c89d1Sviresh kumar 
10246e2c89d1Sviresh kumar 	new_policy.governor = gov;
10256e2c89d1Sviresh kumar 
1026a27a9ab7SJason Baron 	/* Use the default policy if its valid. */
1027a27a9ab7SJason Baron 	if (cpufreq_driver->setpolicy)
10286e2c89d1Sviresh kumar 		cpufreq_parse_governor(gov->name, &new_policy.policy, NULL);
1029ecf7e461SDave Jones 
1030ecf7e461SDave Jones 	/* set default policy */
1031037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
1032ecf7e461SDave Jones 	if (ret) {
10332d06d8c4SDominik Brodowski 		pr_debug("setting policy failed\n");
10341c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
10351c3d85ddSRafael J. Wysocki 			cpufreq_driver->exit(policy);
1036ecf7e461SDave Jones 	}
1037909a694eSDave Jones }
1038909a694eSDave Jones 
1039d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
104042f921a6SViresh Kumar 				  unsigned int cpu, struct device *dev)
1041fcf80582SViresh Kumar {
10429c0ebcf7SViresh Kumar 	int ret = 0;
1043fcf80582SViresh Kumar 
1044bb29ae15SViresh Kumar 	/* Has this CPU been taken care of already? */
1045bb29ae15SViresh Kumar 	if (cpumask_test_cpu(cpu, policy->cpus))
1046bb29ae15SViresh Kumar 		return 0;
1047bb29ae15SViresh Kumar 
10489c0ebcf7SViresh Kumar 	if (has_target()) {
10493de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
10503de9bdebSViresh Kumar 		if (ret) {
10513de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
10523de9bdebSViresh Kumar 			return ret;
10533de9bdebSViresh Kumar 		}
10543de9bdebSViresh Kumar 	}
1055fcf80582SViresh Kumar 
1056ad7722daSviresh kumar 	down_write(&policy->rwsem);
1057fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
1058ad7722daSviresh kumar 	up_write(&policy->rwsem);
10592eaa3e2dSViresh Kumar 
10609c0ebcf7SViresh Kumar 	if (has_target()) {
1061e5c87b76SStratos Karafotis 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1062e5c87b76SStratos Karafotis 		if (!ret)
1063e5c87b76SStratos Karafotis 			ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1064e5c87b76SStratos Karafotis 
1065e5c87b76SStratos Karafotis 		if (ret) {
10663de9bdebSViresh Kumar 			pr_err("%s: Failed to start governor\n", __func__);
10673de9bdebSViresh Kumar 			return ret;
10683de9bdebSViresh Kumar 		}
1069820c6ca2SViresh Kumar 	}
1070fcf80582SViresh Kumar 
107142f921a6SViresh Kumar 	return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
1072fcf80582SViresh Kumar }
10731da177e4SLinus Torvalds 
10748414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
10758414809cSSrivatsa S. Bhat {
10768414809cSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
10778414809cSSrivatsa S. Bhat 	unsigned long flags;
10788414809cSSrivatsa S. Bhat 
107944871c9cSLan Tianyu 	read_lock_irqsave(&cpufreq_driver_lock, flags);
10803914d379SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
108144871c9cSLan Tianyu 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
10828414809cSSrivatsa S. Bhat 
10833914d379SViresh Kumar 	if (likely(policy)) {
10843914d379SViresh Kumar 		/* Policy should be inactive here */
10853914d379SViresh Kumar 		WARN_ON(!policy_is_inactive(policy));
10863914d379SViresh Kumar 	}
10876e2c89d1Sviresh kumar 
10888414809cSSrivatsa S. Bhat 	return policy;
10898414809cSSrivatsa S. Bhat }
10908414809cSSrivatsa S. Bhat 
1091e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void)
1092e9698cc5SSrivatsa S. Bhat {
1093e9698cc5SSrivatsa S. Bhat 	struct cpufreq_policy *policy;
1094e9698cc5SSrivatsa S. Bhat 
1095e9698cc5SSrivatsa S. Bhat 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1096e9698cc5SSrivatsa S. Bhat 	if (!policy)
1097e9698cc5SSrivatsa S. Bhat 		return NULL;
1098e9698cc5SSrivatsa S. Bhat 
1099e9698cc5SSrivatsa S. Bhat 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1100e9698cc5SSrivatsa S. Bhat 		goto err_free_policy;
1101e9698cc5SSrivatsa S. Bhat 
1102e9698cc5SSrivatsa S. Bhat 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1103e9698cc5SSrivatsa S. Bhat 		goto err_free_cpumask;
1104e9698cc5SSrivatsa S. Bhat 
1105c88a1f8bSLukasz Majewski 	INIT_LIST_HEAD(&policy->policy_list);
1106ad7722daSviresh kumar 	init_rwsem(&policy->rwsem);
110712478cf0SSrivatsa S. Bhat 	spin_lock_init(&policy->transition_lock);
110812478cf0SSrivatsa S. Bhat 	init_waitqueue_head(&policy->transition_wait);
1109818c5712SViresh Kumar 	init_completion(&policy->kobj_unregister);
1110818c5712SViresh Kumar 	INIT_WORK(&policy->update, handle_update);
1111ad7722daSviresh kumar 
1112e9698cc5SSrivatsa S. Bhat 	return policy;
1113e9698cc5SSrivatsa S. Bhat 
1114e9698cc5SSrivatsa S. Bhat err_free_cpumask:
1115e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1116e9698cc5SSrivatsa S. Bhat err_free_policy:
1117e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1118e9698cc5SSrivatsa S. Bhat 
1119e9698cc5SSrivatsa S. Bhat 	return NULL;
1120e9698cc5SSrivatsa S. Bhat }
1121e9698cc5SSrivatsa S. Bhat 
112242f921a6SViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
112342f921a6SViresh Kumar {
112442f921a6SViresh Kumar 	struct kobject *kobj;
112542f921a6SViresh Kumar 	struct completion *cmp;
112642f921a6SViresh Kumar 
1127fcd7af91SViresh Kumar 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1128fcd7af91SViresh Kumar 			CPUFREQ_REMOVE_POLICY, policy);
1129fcd7af91SViresh Kumar 
113042f921a6SViresh Kumar 	down_read(&policy->rwsem);
113142f921a6SViresh Kumar 	kobj = &policy->kobj;
113242f921a6SViresh Kumar 	cmp = &policy->kobj_unregister;
113342f921a6SViresh Kumar 	up_read(&policy->rwsem);
113442f921a6SViresh Kumar 	kobject_put(kobj);
113542f921a6SViresh Kumar 
113642f921a6SViresh Kumar 	/*
113742f921a6SViresh Kumar 	 * We need to make sure that the underlying kobj is
113842f921a6SViresh Kumar 	 * actually not referenced anymore by anybody before we
113942f921a6SViresh Kumar 	 * proceed with unloading.
114042f921a6SViresh Kumar 	 */
114142f921a6SViresh Kumar 	pr_debug("waiting for dropping of refcount\n");
114242f921a6SViresh Kumar 	wait_for_completion(cmp);
114342f921a6SViresh Kumar 	pr_debug("wait complete\n");
114442f921a6SViresh Kumar }
114542f921a6SViresh Kumar 
1146e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy)
1147e9698cc5SSrivatsa S. Bhat {
1148988bed09SViresh Kumar 	unsigned long flags;
1149988bed09SViresh Kumar 	int cpu;
1150988bed09SViresh Kumar 
1151988bed09SViresh Kumar 	/* Remove policy from list */
1152988bed09SViresh Kumar 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1153988bed09SViresh Kumar 	list_del(&policy->policy_list);
1154988bed09SViresh Kumar 
1155988bed09SViresh Kumar 	for_each_cpu(cpu, policy->related_cpus)
1156988bed09SViresh Kumar 		per_cpu(cpufreq_cpu_data, cpu) = NULL;
1157988bed09SViresh Kumar 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1158988bed09SViresh Kumar 
1159e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
1160e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1161e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1162e9698cc5SSrivatsa S. Bhat }
1163e9698cc5SSrivatsa S. Bhat 
11641bfb425bSViresh Kumar static int update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu,
11651bfb425bSViresh Kumar 			     struct device *cpu_dev)
11660d66b91eSSrivatsa S. Bhat {
11671bfb425bSViresh Kumar 	int ret;
11681bfb425bSViresh Kumar 
116999ec899eSSrivatsa S. Bhat 	if (WARN_ON(cpu == policy->cpu))
11701bfb425bSViresh Kumar 		return 0;
11711bfb425bSViresh Kumar 
11721bfb425bSViresh Kumar 	/* Move kobject to the new policy->cpu */
11731bfb425bSViresh Kumar 	ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
11741bfb425bSViresh Kumar 	if (ret) {
11751bfb425bSViresh Kumar 		pr_err("%s: Failed to move kobj: %d\n", __func__, ret);
11761bfb425bSViresh Kumar 		return ret;
11771bfb425bSViresh Kumar 	}
1178cb38ed5cSSrivatsa S. Bhat 
1179ad7722daSviresh kumar 	down_write(&policy->rwsem);
11800d66b91eSSrivatsa S. Bhat 	policy->cpu = cpu;
1181*9d16f207SSaravana Kannan 	policy->kobj_cpu = cpu;
1182ad7722daSviresh kumar 	up_write(&policy->rwsem);
11838efd5765SViresh Kumar 
11841bfb425bSViresh Kumar 	return 0;
11850d66b91eSSrivatsa S. Bhat }
11860d66b91eSSrivatsa S. Bhat 
118723faf0b7SViresh Kumar /**
118823faf0b7SViresh Kumar  * cpufreq_add_dev - add a CPU device
118923faf0b7SViresh Kumar  *
119023faf0b7SViresh Kumar  * Adds the cpufreq interface for a CPU device.
119123faf0b7SViresh Kumar  *
119223faf0b7SViresh Kumar  * The Oracle says: try running cpufreq registration/unregistration concurrently
119323faf0b7SViresh Kumar  * with with cpu hotplugging and all hell will break loose. Tried to clean this
119423faf0b7SViresh Kumar  * mess up, but more thorough testing is needed. - Mathieu
119523faf0b7SViresh Kumar  */
119623faf0b7SViresh Kumar static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
11971da177e4SLinus Torvalds {
1198fcf80582SViresh Kumar 	unsigned int j, cpu = dev->id;
119965922465SViresh Kumar 	int ret = -ENOMEM;
12007f0c020aSViresh Kumar 	struct cpufreq_policy *policy;
12011da177e4SLinus Torvalds 	unsigned long flags;
120296bbbe4aSViresh Kumar 	bool recover_policy = cpufreq_suspended;
12031da177e4SLinus Torvalds 
1204c32b6b8eSAshok Raj 	if (cpu_is_offline(cpu))
1205c32b6b8eSAshok Raj 		return 0;
1206c32b6b8eSAshok Raj 
12072d06d8c4SDominik Brodowski 	pr_debug("adding CPU %u\n", cpu);
12081da177e4SLinus Torvalds 
12096eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
12106eed9404SViresh Kumar 		return 0;
12116eed9404SViresh Kumar 
1212bb29ae15SViresh Kumar 	/* Check if this CPU already has a policy to manage it */
12139104bb26SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
12149104bb26SViresh Kumar 	if (policy && !policy_is_inactive(policy)) {
12159104bb26SViresh Kumar 		WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
12167f0c020aSViresh Kumar 		ret = cpufreq_add_policy_cpu(policy, cpu, dev);
12176eed9404SViresh Kumar 		up_read(&cpufreq_rwsem);
12186eed9404SViresh Kumar 		return ret;
1219fcf80582SViresh Kumar 	}
12201da177e4SLinus Torvalds 
122172368d12SRafael J. Wysocki 	/*
122272368d12SRafael J. Wysocki 	 * Restore the saved policy when doing light-weight init and fall back
122372368d12SRafael J. Wysocki 	 * to the full init if that fails.
122472368d12SRafael J. Wysocki 	 */
122596bbbe4aSViresh Kumar 	policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL;
122672368d12SRafael J. Wysocki 	if (!policy) {
122796bbbe4aSViresh Kumar 		recover_policy = false;
1228e9698cc5SSrivatsa S. Bhat 		policy = cpufreq_policy_alloc();
1229059019a3SDave Jones 		if (!policy)
12301da177e4SLinus Torvalds 			goto nomem_out;
123172368d12SRafael J. Wysocki 	}
12320d66b91eSSrivatsa S. Bhat 
12330d66b91eSSrivatsa S. Bhat 	/*
12340d66b91eSSrivatsa S. Bhat 	 * In the resume path, since we restore a saved policy, the assignment
12350d66b91eSSrivatsa S. Bhat 	 * to policy->cpu is like an update of the existing policy, rather than
12360d66b91eSSrivatsa S. Bhat 	 * the creation of a brand new one. So we need to perform this update
12370d66b91eSSrivatsa S. Bhat 	 * by invoking update_policy_cpu().
12380d66b91eSSrivatsa S. Bhat 	 */
1239*9d16f207SSaravana Kannan 	if (recover_policy && cpu != policy->cpu) {
12401bfb425bSViresh Kumar 		WARN_ON(update_policy_cpu(policy, cpu, dev));
1241*9d16f207SSaravana Kannan 	} else {
12421da177e4SLinus Torvalds 		policy->cpu = cpu;
1243*9d16f207SSaravana Kannan 		policy->kobj_cpu = cpu;
1244*9d16f207SSaravana Kannan 	}
12450d66b91eSSrivatsa S. Bhat 
1246835481d9SRusty Russell 	cpumask_copy(policy->cpus, cpumask_of(cpu));
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
12491da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
12501da177e4SLinus Torvalds 	 */
12511c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->init(policy);
12521da177e4SLinus Torvalds 	if (ret) {
12532d06d8c4SDominik Brodowski 		pr_debug("initialization failed\n");
12542eaa3e2dSViresh Kumar 		goto err_set_policy_cpu;
12551da177e4SLinus Torvalds 	}
1256643ae6e8SViresh Kumar 
12576d4e81edSTomeu Vizoso 	down_write(&policy->rwsem);
12586d4e81edSTomeu Vizoso 
12595a7e56a5SViresh Kumar 	/* related cpus should atleast have policy->cpus */
12605a7e56a5SViresh Kumar 	cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
12615a7e56a5SViresh Kumar 
12625a7e56a5SViresh Kumar 	/*
12635a7e56a5SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
12645a7e56a5SViresh Kumar 	 * managing offline cpus here.
12655a7e56a5SViresh Kumar 	 */
12665a7e56a5SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
12675a7e56a5SViresh Kumar 
126896bbbe4aSViresh Kumar 	if (!recover_policy) {
12695a7e56a5SViresh Kumar 		policy->user_policy.min = policy->min;
12705a7e56a5SViresh Kumar 		policy->user_policy.max = policy->max;
12716d4e81edSTomeu Vizoso 
12726d4e81edSTomeu Vizoso 		/* prepare interface data */
12736d4e81edSTomeu Vizoso 		ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
12746d4e81edSTomeu Vizoso 					   &dev->kobj, "cpufreq");
12756d4e81edSTomeu Vizoso 		if (ret) {
12766d4e81edSTomeu Vizoso 			pr_err("%s: failed to init policy->kobj: %d\n",
12776d4e81edSTomeu Vizoso 			       __func__, ret);
12786d4e81edSTomeu Vizoso 			goto err_init_policy_kobj;
12796d4e81edSTomeu Vizoso 		}
12805a7e56a5SViresh Kumar 
1281652ed95dSViresh Kumar 		write_lock_irqsave(&cpufreq_driver_lock, flags);
1282988bed09SViresh Kumar 		for_each_cpu(j, policy->related_cpus)
1283652ed95dSViresh Kumar 			per_cpu(cpufreq_cpu_data, j) = policy;
1284652ed95dSViresh Kumar 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1285988bed09SViresh Kumar 	}
1286652ed95dSViresh Kumar 
12872ed99e39SRafael J. Wysocki 	if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1288da60ce9fSViresh Kumar 		policy->cur = cpufreq_driver->get(policy->cpu);
1289da60ce9fSViresh Kumar 		if (!policy->cur) {
1290da60ce9fSViresh Kumar 			pr_err("%s: ->get() failed\n", __func__);
1291da60ce9fSViresh Kumar 			goto err_get_freq;
1292da60ce9fSViresh Kumar 		}
1293da60ce9fSViresh Kumar 	}
1294da60ce9fSViresh Kumar 
1295d3916691SViresh Kumar 	/*
1296d3916691SViresh Kumar 	 * Sometimes boot loaders set CPU frequency to a value outside of
1297d3916691SViresh Kumar 	 * frequency table present with cpufreq core. In such cases CPU might be
1298d3916691SViresh Kumar 	 * unstable if it has to run on that frequency for long duration of time
1299d3916691SViresh Kumar 	 * and so its better to set it to a frequency which is specified in
1300d3916691SViresh Kumar 	 * freq-table. This also makes cpufreq stats inconsistent as
1301d3916691SViresh Kumar 	 * cpufreq-stats would fail to register because current frequency of CPU
1302d3916691SViresh Kumar 	 * isn't found in freq-table.
1303d3916691SViresh Kumar 	 *
1304d3916691SViresh Kumar 	 * Because we don't want this change to effect boot process badly, we go
1305d3916691SViresh Kumar 	 * for the next freq which is >= policy->cur ('cur' must be set by now,
1306d3916691SViresh Kumar 	 * otherwise we will end up setting freq to lowest of the table as 'cur'
1307d3916691SViresh Kumar 	 * is initialized to zero).
1308d3916691SViresh Kumar 	 *
1309d3916691SViresh Kumar 	 * We are passing target-freq as "policy->cur - 1" otherwise
1310d3916691SViresh Kumar 	 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1311d3916691SViresh Kumar 	 * equal to target-freq.
1312d3916691SViresh Kumar 	 */
1313d3916691SViresh Kumar 	if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1314d3916691SViresh Kumar 	    && has_target()) {
1315d3916691SViresh Kumar 		/* Are we running at unknown frequency ? */
1316d3916691SViresh Kumar 		ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1317d3916691SViresh Kumar 		if (ret == -EINVAL) {
1318d3916691SViresh Kumar 			/* Warn user and fix it */
1319d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1320d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1321d3916691SViresh Kumar 			ret = __cpufreq_driver_target(policy, policy->cur - 1,
1322d3916691SViresh Kumar 				CPUFREQ_RELATION_L);
1323d3916691SViresh Kumar 
1324d3916691SViresh Kumar 			/*
1325d3916691SViresh Kumar 			 * Reaching here after boot in a few seconds may not
1326d3916691SViresh Kumar 			 * mean that system will remain stable at "unknown"
1327d3916691SViresh Kumar 			 * frequency for longer duration. Hence, a BUG_ON().
1328d3916691SViresh Kumar 			 */
1329d3916691SViresh Kumar 			BUG_ON(ret);
1330d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1331d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1332d3916691SViresh Kumar 		}
1333d3916691SViresh Kumar 	}
1334d3916691SViresh Kumar 
1335a1531acdSThomas Renninger 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1336a1531acdSThomas Renninger 				     CPUFREQ_START, policy);
1337a1531acdSThomas Renninger 
133896bbbe4aSViresh Kumar 	if (!recover_policy) {
1339308b60e7SViresh Kumar 		ret = cpufreq_add_dev_interface(policy, dev);
134019d6f7ecSDave Jones 		if (ret)
13410142f9dcSAhmed S. Darwish 			goto err_out_unregister;
1342fcd7af91SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1343fcd7af91SViresh Kumar 				CPUFREQ_CREATE_POLICY, policy);
1344c88a1f8bSLukasz Majewski 
1345c88a1f8bSLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
1346c88a1f8bSLukasz Majewski 		list_add(&policy->policy_list, &cpufreq_policy_list);
1347c88a1f8bSLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1348988bed09SViresh Kumar 	}
13498ff69732SDave Jones 
1350e18f1682SSrivatsa S. Bhat 	cpufreq_init_policy(policy);
1351e18f1682SSrivatsa S. Bhat 
135296bbbe4aSViresh Kumar 	if (!recover_policy) {
135308fd8c1cSViresh Kumar 		policy->user_policy.policy = policy->policy;
135408fd8c1cSViresh Kumar 		policy->user_policy.governor = policy->governor;
135508fd8c1cSViresh Kumar 	}
13564e97b631SViresh Kumar 	up_write(&policy->rwsem);
135708fd8c1cSViresh Kumar 
1358038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
13597c45cf31SViresh Kumar 
13606eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
13616eed9404SViresh Kumar 
13627c45cf31SViresh Kumar 	/* Callback for handling stuff after policy is ready */
13637c45cf31SViresh Kumar 	if (cpufreq_driver->ready)
13647c45cf31SViresh Kumar 		cpufreq_driver->ready(policy);
13657c45cf31SViresh Kumar 
13662d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
13671da177e4SLinus Torvalds 
13681da177e4SLinus Torvalds 	return 0;
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds err_out_unregister:
1371652ed95dSViresh Kumar err_get_freq:
13726d4e81edSTomeu Vizoso 	if (!recover_policy) {
13736d4e81edSTomeu Vizoso 		kobject_put(&policy->kobj);
13746d4e81edSTomeu Vizoso 		wait_for_completion(&policy->kobj_unregister);
13756d4e81edSTomeu Vizoso 	}
13766d4e81edSTomeu Vizoso err_init_policy_kobj:
13777106e02bSPrarit Bhargava 	up_write(&policy->rwsem);
13787106e02bSPrarit Bhargava 
1379da60ce9fSViresh Kumar 	if (cpufreq_driver->exit)
1380da60ce9fSViresh Kumar 		cpufreq_driver->exit(policy);
13812eaa3e2dSViresh Kumar err_set_policy_cpu:
13823914d379SViresh Kumar 	if (recover_policy)
138342f921a6SViresh Kumar 		cpufreq_policy_put_kobj(policy);
1384e9698cc5SSrivatsa S. Bhat 	cpufreq_policy_free(policy);
138542f921a6SViresh Kumar 
13861da177e4SLinus Torvalds nomem_out:
13876eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
13886eed9404SViresh Kumar 
13891da177e4SLinus Torvalds 	return ret;
13901da177e4SLinus Torvalds }
13911da177e4SLinus Torvalds 
1392cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_prepare(struct device *dev,
139396bbbe4aSViresh Kumar 					struct subsys_interface *sif)
13941da177e4SLinus Torvalds {
1395f9ba680dSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
13961bfb425bSViresh Kumar 	int ret;
13973a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
13981da177e4SLinus Torvalds 
1399b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
14001da177e4SLinus Torvalds 
1401988bed09SViresh Kumar 	policy = cpufreq_cpu_get_raw(cpu);
14023a3e9e06SViresh Kumar 	if (!policy) {
1403b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
14041da177e4SLinus Torvalds 		return -EINVAL;
14051da177e4SLinus Torvalds 	}
14061da177e4SLinus Torvalds 
14079c0ebcf7SViresh Kumar 	if (has_target()) {
14083de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
14093de9bdebSViresh Kumar 		if (ret) {
14103de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
14113de9bdebSViresh Kumar 			return ret;
14123de9bdebSViresh Kumar 		}
1413db5f2995SViresh Kumar 	}
14141da177e4SLinus Torvalds 
14154573237bSViresh Kumar 	down_write(&policy->rwsem);
14163a3e9e06SViresh Kumar 	cpus = cpumask_weight(policy->cpus);
14174573237bSViresh Kumar 
14184573237bSViresh Kumar 	if (has_target() && cpus == 1)
14194573237bSViresh Kumar 		strncpy(policy->last_governor, policy->governor->name,
14204573237bSViresh Kumar 			CPUFREQ_NAME_LEN);
14214573237bSViresh Kumar 	up_write(&policy->rwsem);
14221da177e4SLinus Torvalds 
1423*9d16f207SSaravana Kannan 	if (cpu != policy->kobj_cpu) {
142473bf0fc2SViresh Kumar 		sysfs_remove_link(&dev->kobj, "cpufreq");
142573bf0fc2SViresh Kumar 	} else if (cpus > 1) {
14261bfb425bSViresh Kumar 		/* Nominate new CPU */
14271bfb425bSViresh Kumar 		int new_cpu = cpumask_any_but(policy->cpus, cpu);
14281bfb425bSViresh Kumar 		struct device *cpu_dev = get_cpu_device(new_cpu);
14291bfb425bSViresh Kumar 
14301bfb425bSViresh Kumar 		sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
14311bfb425bSViresh Kumar 		ret = update_policy_cpu(policy, new_cpu, cpu_dev);
14321bfb425bSViresh Kumar 		if (ret) {
14331bfb425bSViresh Kumar 			if (sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
14341bfb425bSViresh Kumar 					      "cpufreq"))
14351bfb425bSViresh Kumar 				pr_err("%s: Failed to restore kobj link to cpu:%d\n",
14361bfb425bSViresh Kumar 				       __func__, cpu_dev->id);
14371bfb425bSViresh Kumar 			return ret;
14381bfb425bSViresh Kumar 		}
1439a82fab29SSrivatsa S. Bhat 
1440bda9f552SStratos Karafotis 		if (!cpufreq_suspended)
144175949c9aSViresh Kumar 			pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
144275949c9aSViresh Kumar 				 __func__, new_cpu, cpu);
1443789ca243SPreeti U Murthy 	} else if (cpufreq_driver->stop_cpu) {
1444367dc4aaSDirk Brandewie 		cpufreq_driver->stop_cpu(policy);
14451da177e4SLinus Torvalds 	}
1446b8eed8afSViresh Kumar 
1447cedb70afSSrivatsa S. Bhat 	return 0;
1448cedb70afSSrivatsa S. Bhat }
1449cedb70afSSrivatsa S. Bhat 
1450cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_finish(struct device *dev,
145196bbbe4aSViresh Kumar 				       struct subsys_interface *sif)
1452cedb70afSSrivatsa S. Bhat {
1453988bed09SViresh Kumar 	unsigned int cpu = dev->id;
1454cedb70afSSrivatsa S. Bhat 	int ret;
1455988bed09SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
1456cedb70afSSrivatsa S. Bhat 
1457cedb70afSSrivatsa S. Bhat 	if (!policy) {
1458cedb70afSSrivatsa S. Bhat 		pr_debug("%s: No cpu_data found\n", __func__);
1459cedb70afSSrivatsa S. Bhat 		return -EINVAL;
1460cedb70afSSrivatsa S. Bhat 	}
1461cedb70afSSrivatsa S. Bhat 
1462ad7722daSviresh kumar 	down_write(&policy->rwsem);
14639c8f1ee4SViresh Kumar 	cpumask_clear_cpu(cpu, policy->cpus);
1464ad7722daSviresh kumar 	up_write(&policy->rwsem);
1465cedb70afSSrivatsa S. Bhat 
1466b8eed8afSViresh Kumar 	/* If cpu is last user of policy, free policy */
1467988bed09SViresh Kumar 	if (policy_is_inactive(policy)) {
14689c0ebcf7SViresh Kumar 		if (has_target()) {
14693de9bdebSViresh Kumar 			ret = __cpufreq_governor(policy,
14703de9bdebSViresh Kumar 					CPUFREQ_GOV_POLICY_EXIT);
14713de9bdebSViresh Kumar 			if (ret) {
14723de9bdebSViresh Kumar 				pr_err("%s: Failed to exit governor\n",
14733de9bdebSViresh Kumar 				       __func__);
14743de9bdebSViresh Kumar 				return ret;
14753de9bdebSViresh Kumar 			}
14763de9bdebSViresh Kumar 		}
14772a998599SRafael J. Wysocki 
147896bbbe4aSViresh Kumar 		if (!cpufreq_suspended)
147942f921a6SViresh Kumar 			cpufreq_policy_put_kobj(policy);
14801da177e4SLinus Torvalds 
14818414809cSSrivatsa S. Bhat 		/*
14828414809cSSrivatsa S. Bhat 		 * Perform the ->exit() even during light-weight tear-down,
14838414809cSSrivatsa S. Bhat 		 * since this is a core component, and is essential for the
14848414809cSSrivatsa S. Bhat 		 * subsequent light-weight ->init() to succeed.
14858414809cSSrivatsa S. Bhat 		 */
14861c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
14873a3e9e06SViresh Kumar 			cpufreq_driver->exit(policy);
148827ecddc2SJacob Shin 
148996bbbe4aSViresh Kumar 		if (!cpufreq_suspended)
14903a3e9e06SViresh Kumar 			cpufreq_policy_free(policy);
1491e5c87b76SStratos Karafotis 	} else if (has_target()) {
1492e5c87b76SStratos Karafotis 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_START);
1493e5c87b76SStratos Karafotis 		if (!ret)
1494e5c87b76SStratos Karafotis 			ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1495e5c87b76SStratos Karafotis 
1496e5c87b76SStratos Karafotis 		if (ret) {
1497e5c87b76SStratos Karafotis 			pr_err("%s: Failed to start governor\n", __func__);
14983de9bdebSViresh Kumar 			return ret;
14993de9bdebSViresh Kumar 		}
1500b8eed8afSViresh Kumar 	}
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 	return 0;
15031da177e4SLinus Torvalds }
15041da177e4SLinus Torvalds 
1505cedb70afSSrivatsa S. Bhat /**
150627a862e9SViresh Kumar  * cpufreq_remove_dev - remove a CPU device
1507cedb70afSSrivatsa S. Bhat  *
1508cedb70afSSrivatsa S. Bhat  * Removes the cpufreq interface for a CPU device.
1509cedb70afSSrivatsa S. Bhat  */
15108a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
15115a01f2e8SVenkatesh Pallipadi {
15128a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
151327a862e9SViresh Kumar 	int ret;
1514ec28297aSVenki Pallipadi 
1515ec28297aSVenki Pallipadi 	if (cpu_is_offline(cpu))
1516ec28297aSVenki Pallipadi 		return 0;
1517ec28297aSVenki Pallipadi 
151896bbbe4aSViresh Kumar 	ret = __cpufreq_remove_dev_prepare(dev, sif);
151927a862e9SViresh Kumar 
152027a862e9SViresh Kumar 	if (!ret)
152196bbbe4aSViresh Kumar 		ret = __cpufreq_remove_dev_finish(dev, sif);
152227a862e9SViresh Kumar 
152327a862e9SViresh Kumar 	return ret;
15245a01f2e8SVenkatesh Pallipadi }
15255a01f2e8SVenkatesh Pallipadi 
152665f27f38SDavid Howells static void handle_update(struct work_struct *work)
15271da177e4SLinus Torvalds {
152865f27f38SDavid Howells 	struct cpufreq_policy *policy =
152965f27f38SDavid Howells 		container_of(work, struct cpufreq_policy, update);
153065f27f38SDavid Howells 	unsigned int cpu = policy->cpu;
15312d06d8c4SDominik Brodowski 	pr_debug("handle_update for cpu %u called\n", cpu);
15321da177e4SLinus Torvalds 	cpufreq_update_policy(cpu);
15331da177e4SLinus Torvalds }
15341da177e4SLinus Torvalds 
15351da177e4SLinus Torvalds /**
1536bb176f7dSViresh Kumar  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1537bb176f7dSViresh Kumar  *	in deep trouble.
1538a1e1dc41SViresh Kumar  *	@policy: policy managing CPUs
15391da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
15401da177e4SLinus Torvalds  *
154129464f28SDave Jones  *	We adjust to current frequency first, and need to clean up later.
154229464f28SDave Jones  *	So either call to cpufreq_update_policy() or schedule handle_update()).
15431da177e4SLinus Torvalds  */
1544a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1545e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
15461da177e4SLinus Torvalds {
15471da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1548b43a7ffbSViresh Kumar 
1549e837f9b5SJoe Perches 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1550a1e1dc41SViresh Kumar 		 policy->cur, new_freq);
15511da177e4SLinus Torvalds 
1552a1e1dc41SViresh Kumar 	freqs.old = policy->cur;
15531da177e4SLinus Torvalds 	freqs.new = new_freq;
1554b43a7ffbSViresh Kumar 
15558fec051eSViresh Kumar 	cpufreq_freq_transition_begin(policy, &freqs);
15568fec051eSViresh Kumar 	cpufreq_freq_transition_end(policy, &freqs, 0);
15571da177e4SLinus Torvalds }
15581da177e4SLinus Torvalds 
15591da177e4SLinus Torvalds /**
15604ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
156195235ca2SVenkatesh Pallipadi  * @cpu: CPU number
156295235ca2SVenkatesh Pallipadi  *
156395235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
156495235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
156595235ca2SVenkatesh Pallipadi  */
156695235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
156795235ca2SVenkatesh Pallipadi {
15689e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1569e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
157095235ca2SVenkatesh Pallipadi 
15711c3d85ddSRafael J. Wysocki 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
15721c3d85ddSRafael J. Wysocki 		return cpufreq_driver->get(cpu);
15739e21ba8bSDirk Brandewie 
15749e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
157595235ca2SVenkatesh Pallipadi 	if (policy) {
1576e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
157795235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
157895235ca2SVenkatesh Pallipadi 	}
157995235ca2SVenkatesh Pallipadi 
15804d34a67dSDave Jones 	return ret_freq;
158195235ca2SVenkatesh Pallipadi }
158295235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
158395235ca2SVenkatesh Pallipadi 
15843d737108SJesse Barnes /**
15853d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
15863d737108SJesse Barnes  * @cpu: CPU number
15873d737108SJesse Barnes  *
15883d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
15893d737108SJesse Barnes  */
15903d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
15913d737108SJesse Barnes {
15923d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
15933d737108SJesse Barnes 	unsigned int ret_freq = 0;
15943d737108SJesse Barnes 
15953d737108SJesse Barnes 	if (policy) {
15963d737108SJesse Barnes 		ret_freq = policy->max;
15973d737108SJesse Barnes 		cpufreq_cpu_put(policy);
15983d737108SJesse Barnes 	}
15993d737108SJesse Barnes 
16003d737108SJesse Barnes 	return ret_freq;
16013d737108SJesse Barnes }
16023d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
16033d737108SJesse Barnes 
1604d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
16051da177e4SLinus Torvalds {
1606e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
16071da177e4SLinus Torvalds 
16081c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->get)
16094d34a67dSDave Jones 		return ret_freq;
16101da177e4SLinus Torvalds 
1611d92d50a4SViresh Kumar 	ret_freq = cpufreq_driver->get(policy->cpu);
16121da177e4SLinus Torvalds 
1613e08f5f5bSGautham R Shenoy 	if (ret_freq && policy->cur &&
16141c3d85ddSRafael J. Wysocki 		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1615e08f5f5bSGautham R Shenoy 		/* verify no discrepancy between actual and
1616e08f5f5bSGautham R Shenoy 					saved value exists */
1617e08f5f5bSGautham R Shenoy 		if (unlikely(ret_freq != policy->cur)) {
1618a1e1dc41SViresh Kumar 			cpufreq_out_of_sync(policy, ret_freq);
16191da177e4SLinus Torvalds 			schedule_work(&policy->update);
16201da177e4SLinus Torvalds 		}
16211da177e4SLinus Torvalds 	}
16221da177e4SLinus Torvalds 
16234d34a67dSDave Jones 	return ret_freq;
16245a01f2e8SVenkatesh Pallipadi }
16251da177e4SLinus Torvalds 
16265a01f2e8SVenkatesh Pallipadi /**
16275a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
16285a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
16295a01f2e8SVenkatesh Pallipadi  *
16305a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
16315a01f2e8SVenkatesh Pallipadi  */
16325a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
16335a01f2e8SVenkatesh Pallipadi {
1634999976e0SAaron Plattner 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
16355a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
16365a01f2e8SVenkatesh Pallipadi 
1637999976e0SAaron Plattner 	if (policy) {
1638ad7722daSviresh kumar 		down_read(&policy->rwsem);
1639d92d50a4SViresh Kumar 		ret_freq = __cpufreq_get(policy);
1640ad7722daSviresh kumar 		up_read(&policy->rwsem);
1641999976e0SAaron Plattner 
1642999976e0SAaron Plattner 		cpufreq_cpu_put(policy);
1643999976e0SAaron Plattner 	}
16446eed9404SViresh Kumar 
16454d34a67dSDave Jones 	return ret_freq;
16461da177e4SLinus Torvalds }
16471da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
16481da177e4SLinus Torvalds 
16498a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
16508a25a2fdSKay Sievers 	.name		= "cpufreq",
16518a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
16528a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
16538a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1654e00e56dfSRafael J. Wysocki };
1655e00e56dfSRafael J. Wysocki 
1656e28867eaSViresh Kumar /*
1657e28867eaSViresh Kumar  * In case platform wants some specific frequency to be configured
1658e28867eaSViresh Kumar  * during suspend..
165942d4dc3fSBenjamin Herrenschmidt  */
1660e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy)
166142d4dc3fSBenjamin Herrenschmidt {
1662e28867eaSViresh Kumar 	int ret;
16634bc5d341SDave Jones 
1664e28867eaSViresh Kumar 	if (!policy->suspend_freq) {
1665e28867eaSViresh Kumar 		pr_err("%s: suspend_freq can't be zero\n", __func__);
1666e28867eaSViresh Kumar 		return -EINVAL;
166742d4dc3fSBenjamin Herrenschmidt 	}
166842d4dc3fSBenjamin Herrenschmidt 
1669e28867eaSViresh Kumar 	pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1670e28867eaSViresh Kumar 			policy->suspend_freq);
1671e28867eaSViresh Kumar 
1672e28867eaSViresh Kumar 	ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1673e28867eaSViresh Kumar 			CPUFREQ_RELATION_H);
1674e28867eaSViresh Kumar 	if (ret)
1675e28867eaSViresh Kumar 		pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1676e28867eaSViresh Kumar 				__func__, policy->suspend_freq, ret);
1677e28867eaSViresh Kumar 
1678c9060494SDave Jones 	return ret;
167942d4dc3fSBenjamin Herrenschmidt }
1680e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend);
168142d4dc3fSBenjamin Herrenschmidt 
168242d4dc3fSBenjamin Herrenschmidt /**
16832f0aea93SViresh Kumar  * cpufreq_suspend() - Suspend CPUFreq governors
16841da177e4SLinus Torvalds  *
16852f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycles for suspending governors
16862f0aea93SViresh Kumar  * as some platforms can't change frequency after this point in suspend cycle.
16872f0aea93SViresh Kumar  * Because some of the devices (like: i2c, regulators, etc) they use for
16882f0aea93SViresh Kumar  * changing frequency are suspended quickly after this point.
16891da177e4SLinus Torvalds  */
16902f0aea93SViresh Kumar void cpufreq_suspend(void)
16911da177e4SLinus Torvalds {
16923a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
16931da177e4SLinus Torvalds 
16942f0aea93SViresh Kumar 	if (!cpufreq_driver)
1695e00e56dfSRafael J. Wysocki 		return;
16961da177e4SLinus Torvalds 
16972f0aea93SViresh Kumar 	if (!has_target())
1698b1b12babSViresh Kumar 		goto suspend;
16991da177e4SLinus Torvalds 
17002f0aea93SViresh Kumar 	pr_debug("%s: Suspending Governors\n", __func__);
17012f0aea93SViresh Kumar 
1702f963735aSViresh Kumar 	for_each_active_policy(policy) {
17032f0aea93SViresh Kumar 		if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
17042f0aea93SViresh Kumar 			pr_err("%s: Failed to stop governor for policy: %p\n",
17052f0aea93SViresh Kumar 				__func__, policy);
17062f0aea93SViresh Kumar 		else if (cpufreq_driver->suspend
17072f0aea93SViresh Kumar 		    && cpufreq_driver->suspend(policy))
17082f0aea93SViresh Kumar 			pr_err("%s: Failed to suspend driver: %p\n", __func__,
17092f0aea93SViresh Kumar 				policy);
17101da177e4SLinus Torvalds 	}
1711b1b12babSViresh Kumar 
1712b1b12babSViresh Kumar suspend:
1713b1b12babSViresh Kumar 	cpufreq_suspended = true;
17141da177e4SLinus Torvalds }
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds /**
17172f0aea93SViresh Kumar  * cpufreq_resume() - Resume CPUFreq governors
17181da177e4SLinus Torvalds  *
17192f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycle for resuming governors that
17202f0aea93SViresh Kumar  * are suspended with cpufreq_suspend().
17211da177e4SLinus Torvalds  */
17222f0aea93SViresh Kumar void cpufreq_resume(void)
17231da177e4SLinus Torvalds {
17241da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
17251da177e4SLinus Torvalds 
17262f0aea93SViresh Kumar 	if (!cpufreq_driver)
17271da177e4SLinus Torvalds 		return;
17281da177e4SLinus Torvalds 
17298e30444eSLan Tianyu 	cpufreq_suspended = false;
17308e30444eSLan Tianyu 
17312f0aea93SViresh Kumar 	if (!has_target())
17322f0aea93SViresh Kumar 		return;
17331da177e4SLinus Torvalds 
17342f0aea93SViresh Kumar 	pr_debug("%s: Resuming Governors\n", __func__);
17352f0aea93SViresh Kumar 
1736f963735aSViresh Kumar 	for_each_active_policy(policy) {
17370c5aa405SViresh Kumar 		if (cpufreq_driver->resume && cpufreq_driver->resume(policy))
17380c5aa405SViresh Kumar 			pr_err("%s: Failed to resume driver: %p\n", __func__,
17390c5aa405SViresh Kumar 				policy);
17400c5aa405SViresh Kumar 		else if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
17412f0aea93SViresh Kumar 		    || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
17422f0aea93SViresh Kumar 			pr_err("%s: Failed to start governor for policy: %p\n",
17432f0aea93SViresh Kumar 				__func__, policy);
1744c75de0acSViresh Kumar 	}
17452f0aea93SViresh Kumar 
17462f0aea93SViresh Kumar 	/*
1747c75de0acSViresh Kumar 	 * schedule call cpufreq_update_policy() for first-online CPU, as that
1748c75de0acSViresh Kumar 	 * wouldn't be hotplugged-out on suspend. It will verify that the
1749c75de0acSViresh Kumar 	 * current freq is in sync with what we believe it to be.
17502f0aea93SViresh Kumar 	 */
1751c75de0acSViresh Kumar 	policy = cpufreq_cpu_get_raw(cpumask_first(cpu_online_mask));
1752c75de0acSViresh Kumar 	if (WARN_ON(!policy))
1753c75de0acSViresh Kumar 		return;
1754c75de0acSViresh Kumar 
17553a3e9e06SViresh Kumar 	schedule_work(&policy->update);
17561da177e4SLinus Torvalds }
17571da177e4SLinus Torvalds 
17589d95046eSBorislav Petkov /**
17599d95046eSBorislav Petkov  *	cpufreq_get_current_driver - return current driver's name
17609d95046eSBorislav Petkov  *
17619d95046eSBorislav Petkov  *	Return the name string of the currently loaded cpufreq driver
17629d95046eSBorislav Petkov  *	or NULL, if none.
17639d95046eSBorislav Petkov  */
17649d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
17659d95046eSBorislav Petkov {
17661c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
17671c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
17681c3d85ddSRafael J. Wysocki 
17691c3d85ddSRafael J. Wysocki 	return NULL;
17709d95046eSBorislav Petkov }
17719d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
17721da177e4SLinus Torvalds 
177351315cdfSThomas Petazzoni /**
177451315cdfSThomas Petazzoni  *	cpufreq_get_driver_data - return current driver data
177551315cdfSThomas Petazzoni  *
177651315cdfSThomas Petazzoni  *	Return the private data of the currently loaded cpufreq
177751315cdfSThomas Petazzoni  *	driver, or NULL if no cpufreq driver is loaded.
177851315cdfSThomas Petazzoni  */
177951315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void)
178051315cdfSThomas Petazzoni {
178151315cdfSThomas Petazzoni 	if (cpufreq_driver)
178251315cdfSThomas Petazzoni 		return cpufreq_driver->driver_data;
178351315cdfSThomas Petazzoni 
178451315cdfSThomas Petazzoni 	return NULL;
178551315cdfSThomas Petazzoni }
178651315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
178751315cdfSThomas Petazzoni 
17881da177e4SLinus Torvalds /*********************************************************************
17891da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
17901da177e4SLinus Torvalds  *********************************************************************/
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds /**
17931da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
17941da177e4SLinus Torvalds  *	@nb: notifier function to register
17951da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
17961da177e4SLinus Torvalds  *
17971da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
17981da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
17991da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
18001da177e4SLinus Torvalds  *      changes in cpufreq policy.
18011da177e4SLinus Torvalds  *
18021da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1803e041c683SAlan Stern  *	blocking_notifier_chain_register.
18041da177e4SLinus Torvalds  */
18051da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
18061da177e4SLinus Torvalds {
18071da177e4SLinus Torvalds 	int ret;
18081da177e4SLinus Torvalds 
1809d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1810d5aaffa9SDirk Brandewie 		return -EINVAL;
1811d5aaffa9SDirk Brandewie 
181274212ca4SCesar Eduardo Barros 	WARN_ON(!init_cpufreq_transition_notifier_list_called);
181374212ca4SCesar Eduardo Barros 
18141da177e4SLinus Torvalds 	switch (list) {
18151da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1816b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1817e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
18181da177e4SLinus Torvalds 		break;
18191da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1820e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1821e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
18221da177e4SLinus Torvalds 		break;
18231da177e4SLinus Torvalds 	default:
18241da177e4SLinus Torvalds 		ret = -EINVAL;
18251da177e4SLinus Torvalds 	}
18261da177e4SLinus Torvalds 
18271da177e4SLinus Torvalds 	return ret;
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds /**
18321da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
18331da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
18341da177e4SLinus Torvalds  *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
18351da177e4SLinus Torvalds  *
18361da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
18371da177e4SLinus Torvalds  *
18381da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1839e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
18401da177e4SLinus Torvalds  */
18411da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
18421da177e4SLinus Torvalds {
18431da177e4SLinus Torvalds 	int ret;
18441da177e4SLinus Torvalds 
1845d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1846d5aaffa9SDirk Brandewie 		return -EINVAL;
1847d5aaffa9SDirk Brandewie 
18481da177e4SLinus Torvalds 	switch (list) {
18491da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1850b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
1851e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
18521da177e4SLinus Torvalds 		break;
18531da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1854e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1855e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
18561da177e4SLinus Torvalds 		break;
18571da177e4SLinus Torvalds 	default:
18581da177e4SLinus Torvalds 		ret = -EINVAL;
18591da177e4SLinus Torvalds 	}
18601da177e4SLinus Torvalds 
18611da177e4SLinus Torvalds 	return ret;
18621da177e4SLinus Torvalds }
18631da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
18641da177e4SLinus Torvalds 
18651da177e4SLinus Torvalds 
18661da177e4SLinus Torvalds /*********************************************************************
18671da177e4SLinus Torvalds  *                              GOVERNORS                            *
18681da177e4SLinus Torvalds  *********************************************************************/
18691da177e4SLinus Torvalds 
18701c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */
18711c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy,
18721c03a2d0SViresh Kumar 				 struct cpufreq_freqs *freqs, int index)
18731c03a2d0SViresh Kumar {
18741c03a2d0SViresh Kumar 	int ret;
18751c03a2d0SViresh Kumar 
18761c03a2d0SViresh Kumar 	freqs->new = cpufreq_driver->get_intermediate(policy, index);
18771c03a2d0SViresh Kumar 
18781c03a2d0SViresh Kumar 	/* We don't need to switch to intermediate freq */
18791c03a2d0SViresh Kumar 	if (!freqs->new)
18801c03a2d0SViresh Kumar 		return 0;
18811c03a2d0SViresh Kumar 
18821c03a2d0SViresh Kumar 	pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
18831c03a2d0SViresh Kumar 		 __func__, policy->cpu, freqs->old, freqs->new);
18841c03a2d0SViresh Kumar 
18851c03a2d0SViresh Kumar 	cpufreq_freq_transition_begin(policy, freqs);
18861c03a2d0SViresh Kumar 	ret = cpufreq_driver->target_intermediate(policy, index);
18871c03a2d0SViresh Kumar 	cpufreq_freq_transition_end(policy, freqs, ret);
18881c03a2d0SViresh Kumar 
18891c03a2d0SViresh Kumar 	if (ret)
18901c03a2d0SViresh Kumar 		pr_err("%s: Failed to change to intermediate frequency: %d\n",
18911c03a2d0SViresh Kumar 		       __func__, ret);
18921c03a2d0SViresh Kumar 
18931c03a2d0SViresh Kumar 	return ret;
18941c03a2d0SViresh Kumar }
18951c03a2d0SViresh Kumar 
18968d65775dSViresh Kumar static int __target_index(struct cpufreq_policy *policy,
18978d65775dSViresh Kumar 			  struct cpufreq_frequency_table *freq_table, int index)
18988d65775dSViresh Kumar {
18991c03a2d0SViresh Kumar 	struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
19001c03a2d0SViresh Kumar 	unsigned int intermediate_freq = 0;
19018d65775dSViresh Kumar 	int retval = -EINVAL;
19028d65775dSViresh Kumar 	bool notify;
19038d65775dSViresh Kumar 
19048d65775dSViresh Kumar 	notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
19058d65775dSViresh Kumar 	if (notify) {
19061c03a2d0SViresh Kumar 		/* Handle switching to intermediate frequency */
19071c03a2d0SViresh Kumar 		if (cpufreq_driver->get_intermediate) {
19081c03a2d0SViresh Kumar 			retval = __target_intermediate(policy, &freqs, index);
19091c03a2d0SViresh Kumar 			if (retval)
19101c03a2d0SViresh Kumar 				return retval;
19118d65775dSViresh Kumar 
19121c03a2d0SViresh Kumar 			intermediate_freq = freqs.new;
19131c03a2d0SViresh Kumar 			/* Set old freq to intermediate */
19141c03a2d0SViresh Kumar 			if (intermediate_freq)
19151c03a2d0SViresh Kumar 				freqs.old = freqs.new;
19161c03a2d0SViresh Kumar 		}
19171c03a2d0SViresh Kumar 
19181c03a2d0SViresh Kumar 		freqs.new = freq_table[index].frequency;
19198d65775dSViresh Kumar 		pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
19208d65775dSViresh Kumar 			 __func__, policy->cpu, freqs.old, freqs.new);
19218d65775dSViresh Kumar 
19228d65775dSViresh Kumar 		cpufreq_freq_transition_begin(policy, &freqs);
19238d65775dSViresh Kumar 	}
19248d65775dSViresh Kumar 
19258d65775dSViresh Kumar 	retval = cpufreq_driver->target_index(policy, index);
19268d65775dSViresh Kumar 	if (retval)
19278d65775dSViresh Kumar 		pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
19288d65775dSViresh Kumar 		       retval);
19298d65775dSViresh Kumar 
19301c03a2d0SViresh Kumar 	if (notify) {
19318d65775dSViresh Kumar 		cpufreq_freq_transition_end(policy, &freqs, retval);
19328d65775dSViresh Kumar 
19331c03a2d0SViresh Kumar 		/*
19341c03a2d0SViresh Kumar 		 * Failed after setting to intermediate freq? Driver should have
19351c03a2d0SViresh Kumar 		 * reverted back to initial frequency and so should we. Check
19361c03a2d0SViresh Kumar 		 * here for intermediate_freq instead of get_intermediate, in
193758405af6SShailendra Verma 		 * case we haven't switched to intermediate freq at all.
19381c03a2d0SViresh Kumar 		 */
19391c03a2d0SViresh Kumar 		if (unlikely(retval && intermediate_freq)) {
19401c03a2d0SViresh Kumar 			freqs.old = intermediate_freq;
19411c03a2d0SViresh Kumar 			freqs.new = policy->restore_freq;
19421c03a2d0SViresh Kumar 			cpufreq_freq_transition_begin(policy, &freqs);
19431c03a2d0SViresh Kumar 			cpufreq_freq_transition_end(policy, &freqs, 0);
19441c03a2d0SViresh Kumar 		}
19451c03a2d0SViresh Kumar 	}
19461c03a2d0SViresh Kumar 
19478d65775dSViresh Kumar 	return retval;
19488d65775dSViresh Kumar }
19498d65775dSViresh Kumar 
19501da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
19511da177e4SLinus Torvalds 			    unsigned int target_freq,
19521da177e4SLinus Torvalds 			    unsigned int relation)
19531da177e4SLinus Torvalds {
19547249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
19558d65775dSViresh Kumar 	int retval = -EINVAL;
1956c32b6b8eSAshok Raj 
1957a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1958a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1959a7b422cdSKonrad Rzeszutek Wilk 
19607249924eSViresh Kumar 	/* Make sure that target_freq is within supported range */
19617249924eSViresh Kumar 	if (target_freq > policy->max)
19627249924eSViresh Kumar 		target_freq = policy->max;
19637249924eSViresh Kumar 	if (target_freq < policy->min)
19647249924eSViresh Kumar 		target_freq = policy->min;
19657249924eSViresh Kumar 
19667249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
19677249924eSViresh Kumar 		 policy->cpu, target_freq, relation, old_target_freq);
19685a1c0228SViresh Kumar 
19699c0ebcf7SViresh Kumar 	/*
19709c0ebcf7SViresh Kumar 	 * This might look like a redundant call as we are checking it again
19719c0ebcf7SViresh Kumar 	 * after finding index. But it is left intentionally for cases where
19729c0ebcf7SViresh Kumar 	 * exactly same freq is called again and so we can save on few function
19739c0ebcf7SViresh Kumar 	 * calls.
19749c0ebcf7SViresh Kumar 	 */
19755a1c0228SViresh Kumar 	if (target_freq == policy->cur)
19765a1c0228SViresh Kumar 		return 0;
19775a1c0228SViresh Kumar 
19781c03a2d0SViresh Kumar 	/* Save last value to restore later on errors */
19791c03a2d0SViresh Kumar 	policy->restore_freq = policy->cur;
19801c03a2d0SViresh Kumar 
19811c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
19821c3d85ddSRafael J. Wysocki 		retval = cpufreq_driver->target(policy, target_freq, relation);
19839c0ebcf7SViresh Kumar 	else if (cpufreq_driver->target_index) {
19849c0ebcf7SViresh Kumar 		struct cpufreq_frequency_table *freq_table;
19859c0ebcf7SViresh Kumar 		int index;
198690d45d17SAshok Raj 
19879c0ebcf7SViresh Kumar 		freq_table = cpufreq_frequency_get_table(policy->cpu);
19889c0ebcf7SViresh Kumar 		if (unlikely(!freq_table)) {
19899c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find freq_table\n", __func__);
19909c0ebcf7SViresh Kumar 			goto out;
19919c0ebcf7SViresh Kumar 		}
19929c0ebcf7SViresh Kumar 
19939c0ebcf7SViresh Kumar 		retval = cpufreq_frequency_table_target(policy, freq_table,
19949c0ebcf7SViresh Kumar 				target_freq, relation, &index);
19959c0ebcf7SViresh Kumar 		if (unlikely(retval)) {
19969c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find matching freq\n", __func__);
19979c0ebcf7SViresh Kumar 			goto out;
19989c0ebcf7SViresh Kumar 		}
19999c0ebcf7SViresh Kumar 
2000d4019f0aSViresh Kumar 		if (freq_table[index].frequency == policy->cur) {
20019c0ebcf7SViresh Kumar 			retval = 0;
2002d4019f0aSViresh Kumar 			goto out;
2003d4019f0aSViresh Kumar 		}
2004d4019f0aSViresh Kumar 
20058d65775dSViresh Kumar 		retval = __target_index(policy, freq_table, index);
20069c0ebcf7SViresh Kumar 	}
20079c0ebcf7SViresh Kumar 
20089c0ebcf7SViresh Kumar out:
20091da177e4SLinus Torvalds 	return retval;
20101da177e4SLinus Torvalds }
20111da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
20121da177e4SLinus Torvalds 
20131da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
20141da177e4SLinus Torvalds 			  unsigned int target_freq,
20151da177e4SLinus Torvalds 			  unsigned int relation)
20161da177e4SLinus Torvalds {
2017f1829e4aSJulia Lawall 	int ret = -EINVAL;
20181da177e4SLinus Torvalds 
2019ad7722daSviresh kumar 	down_write(&policy->rwsem);
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
20221da177e4SLinus Torvalds 
2023ad7722daSviresh kumar 	up_write(&policy->rwsem);
20241da177e4SLinus Torvalds 
20251da177e4SLinus Torvalds 	return ret;
20261da177e4SLinus Torvalds }
20271da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
20281da177e4SLinus Torvalds 
2029e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy,
2030e08f5f5bSGautham R Shenoy 					unsigned int event)
20311da177e4SLinus Torvalds {
2032cc993cabSDave Jones 	int ret;
20336afde10cSThomas Renninger 
20346afde10cSThomas Renninger 	/* Only must be defined when default governor is known to have latency
20356afde10cSThomas Renninger 	   restrictions, like e.g. conservative or ondemand.
20366afde10cSThomas Renninger 	   That this is the case is already ensured in Kconfig
20376afde10cSThomas Renninger 	*/
20386afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
20396afde10cSThomas Renninger 	struct cpufreq_governor *gov = &cpufreq_gov_performance;
20406afde10cSThomas Renninger #else
20416afde10cSThomas Renninger 	struct cpufreq_governor *gov = NULL;
20426afde10cSThomas Renninger #endif
20431c256245SThomas Renninger 
20442f0aea93SViresh Kumar 	/* Don't start any governor operations if we are entering suspend */
20452f0aea93SViresh Kumar 	if (cpufreq_suspended)
20462f0aea93SViresh Kumar 		return 0;
2047cb57720bSEthan Zhao 	/*
2048cb57720bSEthan Zhao 	 * Governor might not be initiated here if ACPI _PPC changed
2049cb57720bSEthan Zhao 	 * notification happened, so check it.
2050cb57720bSEthan Zhao 	 */
2051cb57720bSEthan Zhao 	if (!policy->governor)
2052cb57720bSEthan Zhao 		return -EINVAL;
20532f0aea93SViresh Kumar 
20541c256245SThomas Renninger 	if (policy->governor->max_transition_latency &&
20551c256245SThomas Renninger 	    policy->cpuinfo.transition_latency >
20561c256245SThomas Renninger 	    policy->governor->max_transition_latency) {
20576afde10cSThomas Renninger 		if (!gov)
20586afde10cSThomas Renninger 			return -EINVAL;
20596afde10cSThomas Renninger 		else {
2060e837f9b5SJoe Perches 			pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2061e837f9b5SJoe Perches 				policy->governor->name, gov->name);
20621c256245SThomas Renninger 			policy->governor = gov;
20631c256245SThomas Renninger 		}
20646afde10cSThomas Renninger 	}
20651da177e4SLinus Torvalds 
2066fe492f3fSViresh Kumar 	if (event == CPUFREQ_GOV_POLICY_INIT)
20671da177e4SLinus Torvalds 		if (!try_module_get(policy->governor->owner))
20681da177e4SLinus Torvalds 			return -EINVAL;
20691da177e4SLinus Torvalds 
20702d06d8c4SDominik Brodowski 	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
2071e08f5f5bSGautham R Shenoy 		 policy->cpu, event);
207295731ebbSXiaoguang Chen 
207395731ebbSXiaoguang Chen 	mutex_lock(&cpufreq_governor_lock);
207456d07db2SSrivatsa S. Bhat 	if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
2075f73d3933SViresh Kumar 	    || (!policy->governor_enabled
2076f73d3933SViresh Kumar 	    && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
207795731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
207895731ebbSXiaoguang Chen 		return -EBUSY;
207995731ebbSXiaoguang Chen 	}
208095731ebbSXiaoguang Chen 
208195731ebbSXiaoguang Chen 	if (event == CPUFREQ_GOV_STOP)
208295731ebbSXiaoguang Chen 		policy->governor_enabled = false;
208395731ebbSXiaoguang Chen 	else if (event == CPUFREQ_GOV_START)
208495731ebbSXiaoguang Chen 		policy->governor_enabled = true;
208595731ebbSXiaoguang Chen 
208695731ebbSXiaoguang Chen 	mutex_unlock(&cpufreq_governor_lock);
208795731ebbSXiaoguang Chen 
20881da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
20891da177e4SLinus Torvalds 
20904d5dcc42SViresh Kumar 	if (!ret) {
20914d5dcc42SViresh Kumar 		if (event == CPUFREQ_GOV_POLICY_INIT)
20928e53695fSViresh Kumar 			policy->governor->initialized++;
20934d5dcc42SViresh Kumar 		else if (event == CPUFREQ_GOV_POLICY_EXIT)
20948e53695fSViresh Kumar 			policy->governor->initialized--;
209595731ebbSXiaoguang Chen 	} else {
209695731ebbSXiaoguang Chen 		/* Restore original values */
209795731ebbSXiaoguang Chen 		mutex_lock(&cpufreq_governor_lock);
209895731ebbSXiaoguang Chen 		if (event == CPUFREQ_GOV_STOP)
209995731ebbSXiaoguang Chen 			policy->governor_enabled = true;
210095731ebbSXiaoguang Chen 		else if (event == CPUFREQ_GOV_START)
210195731ebbSXiaoguang Chen 			policy->governor_enabled = false;
210295731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
21034d5dcc42SViresh Kumar 	}
2104b394058fSViresh Kumar 
2105fe492f3fSViresh Kumar 	if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2106fe492f3fSViresh Kumar 			((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
21071da177e4SLinus Torvalds 		module_put(policy->governor->owner);
21081da177e4SLinus Torvalds 
21091da177e4SLinus Torvalds 	return ret;
21101da177e4SLinus Torvalds }
21111da177e4SLinus Torvalds 
21121da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
21131da177e4SLinus Torvalds {
21143bcb09a3SJeremy Fitzhardinge 	int err;
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 	if (!governor)
21171da177e4SLinus Torvalds 		return -EINVAL;
21181da177e4SLinus Torvalds 
2119a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2120a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2121a7b422cdSKonrad Rzeszutek Wilk 
21223fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
21231da177e4SLinus Torvalds 
2124b394058fSViresh Kumar 	governor->initialized = 0;
21253bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
212642f91fa1SViresh Kumar 	if (!find_governor(governor->name)) {
21273bcb09a3SJeremy Fitzhardinge 		err = 0;
21281da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
21293bcb09a3SJeremy Fitzhardinge 	}
21301da177e4SLinus Torvalds 
21313fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
21323bcb09a3SJeremy Fitzhardinge 	return err;
21331da177e4SLinus Torvalds }
21341da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
21351da177e4SLinus Torvalds 
21361da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
21371da177e4SLinus Torvalds {
21384573237bSViresh Kumar 	struct cpufreq_policy *policy;
21394573237bSViresh Kumar 	unsigned long flags;
214090e41bacSPrarit Bhargava 
21411da177e4SLinus Torvalds 	if (!governor)
21421da177e4SLinus Torvalds 		return;
21431da177e4SLinus Torvalds 
2144a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2145a7b422cdSKonrad Rzeszutek Wilk 		return;
2146a7b422cdSKonrad Rzeszutek Wilk 
21474573237bSViresh Kumar 	/* clear last_governor for all inactive policies */
21484573237bSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
21494573237bSViresh Kumar 	for_each_inactive_policy(policy) {
215018bf3a12SViresh Kumar 		if (!strcmp(policy->last_governor, governor->name)) {
215118bf3a12SViresh Kumar 			policy->governor = NULL;
21524573237bSViresh Kumar 			strcpy(policy->last_governor, "\0");
215390e41bacSPrarit Bhargava 		}
215418bf3a12SViresh Kumar 	}
21554573237bSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
215690e41bacSPrarit Bhargava 
21573fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
21581da177e4SLinus Torvalds 	list_del(&governor->governor_list);
21593fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
21601da177e4SLinus Torvalds 	return;
21611da177e4SLinus Torvalds }
21621da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
21631da177e4SLinus Torvalds 
21641da177e4SLinus Torvalds 
21651da177e4SLinus Torvalds /*********************************************************************
21661da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
21671da177e4SLinus Torvalds  *********************************************************************/
21681da177e4SLinus Torvalds 
21691da177e4SLinus Torvalds /**
21701da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
217129464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
217229464f28SDave Jones  *	is written
21731da177e4SLinus Torvalds  *
21741da177e4SLinus Torvalds  * Reads the current cpufreq policy.
21751da177e4SLinus Torvalds  */
21761da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
21771da177e4SLinus Torvalds {
21781da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
21791da177e4SLinus Torvalds 	if (!policy)
21801da177e4SLinus Torvalds 		return -EINVAL;
21811da177e4SLinus Torvalds 
21821da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
21831da177e4SLinus Torvalds 	if (!cpu_policy)
21841da177e4SLinus Torvalds 		return -EINVAL;
21851da177e4SLinus Torvalds 
2186d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
21871da177e4SLinus Torvalds 
21881da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
21891da177e4SLinus Torvalds 	return 0;
21901da177e4SLinus Torvalds }
21911da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
21921da177e4SLinus Torvalds 
2193153d7f3fSArjan van de Ven /*
2194037ce839SViresh Kumar  * policy : current policy.
2195037ce839SViresh Kumar  * new_policy: policy to be set.
2196153d7f3fSArjan van de Ven  */
2197037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
21983a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy)
21991da177e4SLinus Torvalds {
2200d9a789c7SRafael J. Wysocki 	struct cpufreq_governor *old_gov;
2201d9a789c7SRafael J. Wysocki 	int ret;
22021da177e4SLinus Torvalds 
2203e837f9b5SJoe Perches 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2204e837f9b5SJoe Perches 		 new_policy->cpu, new_policy->min, new_policy->max);
22051da177e4SLinus Torvalds 
2206d5b73cd8SViresh Kumar 	memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
22071da177e4SLinus Torvalds 
2208d9a789c7SRafael J. Wysocki 	if (new_policy->min > policy->max || new_policy->max < policy->min)
2209d9a789c7SRafael J. Wysocki 		return -EINVAL;
22109c9a43edSMattia Dongili 
22111da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
22123a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
22131da177e4SLinus Torvalds 	if (ret)
2214d9a789c7SRafael J. Wysocki 		return ret;
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
2217e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
22183a3e9e06SViresh Kumar 			CPUFREQ_ADJUST, new_policy);
22191da177e4SLinus Torvalds 
22201da177e4SLinus Torvalds 	/* adjust if necessary - hardware incompatibility*/
2221e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
22223a3e9e06SViresh Kumar 			CPUFREQ_INCOMPATIBLE, new_policy);
22231da177e4SLinus Torvalds 
2224bb176f7dSViresh Kumar 	/*
2225bb176f7dSViresh Kumar 	 * verify the cpu speed can be set within this limit, which might be
2226bb176f7dSViresh Kumar 	 * different to the first one
2227bb176f7dSViresh Kumar 	 */
22283a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
2229e041c683SAlan Stern 	if (ret)
2230d9a789c7SRafael J. Wysocki 		return ret;
22311da177e4SLinus Torvalds 
22321da177e4SLinus Torvalds 	/* notification of the new policy */
2233e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
22343a3e9e06SViresh Kumar 			CPUFREQ_NOTIFY, new_policy);
22351da177e4SLinus Torvalds 
22363a3e9e06SViresh Kumar 	policy->min = new_policy->min;
22373a3e9e06SViresh Kumar 	policy->max = new_policy->max;
22381da177e4SLinus Torvalds 
22392d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
22403a3e9e06SViresh Kumar 		 policy->min, policy->max);
22411da177e4SLinus Torvalds 
22421c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
22433a3e9e06SViresh Kumar 		policy->policy = new_policy->policy;
22442d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
2245d9a789c7SRafael J. Wysocki 		return cpufreq_driver->setpolicy(new_policy);
2246d9a789c7SRafael J. Wysocki 	}
2247d9a789c7SRafael J. Wysocki 
2248d9a789c7SRafael J. Wysocki 	if (new_policy->governor == policy->governor)
2249d9a789c7SRafael J. Wysocki 		goto out;
22501da177e4SLinus Torvalds 
22512d06d8c4SDominik Brodowski 	pr_debug("governor switch\n");
22521da177e4SLinus Torvalds 
2253d9a789c7SRafael J. Wysocki 	/* save old, working values */
2254d9a789c7SRafael J. Wysocki 	old_gov = policy->governor;
22551da177e4SLinus Torvalds 	/* end old governor */
2256d9a789c7SRafael J. Wysocki 	if (old_gov) {
22573a3e9e06SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2258ad7722daSviresh kumar 		up_write(&policy->rwsem);
2259d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2260ad7722daSviresh kumar 		down_write(&policy->rwsem);
22617bd353a9SViresh Kumar 	}
22621da177e4SLinus Torvalds 
22631da177e4SLinus Torvalds 	/* start new governor */
22643a3e9e06SViresh Kumar 	policy->governor = new_policy->governor;
22653a3e9e06SViresh Kumar 	if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2266d9a789c7SRafael J. Wysocki 		if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2267d9a789c7SRafael J. Wysocki 			goto out;
2268d9a789c7SRafael J. Wysocki 
2269ad7722daSviresh kumar 		up_write(&policy->rwsem);
2270d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2271ad7722daSviresh kumar 		down_write(&policy->rwsem);
2272955ef483SViresh Kumar 	}
22737bd353a9SViresh Kumar 
22741da177e4SLinus Torvalds 	/* new governor failed, so re-start old one */
2275d9a789c7SRafael J. Wysocki 	pr_debug("starting governor %s failed\n", policy->governor->name);
22761da177e4SLinus Torvalds 	if (old_gov) {
22773a3e9e06SViresh Kumar 		policy->governor = old_gov;
2278d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2279d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_START);
22801da177e4SLinus Torvalds 	}
22811da177e4SLinus Torvalds 
2282d9a789c7SRafael J. Wysocki 	return -EINVAL;
2283d9a789c7SRafael J. Wysocki 
2284d9a789c7SRafael J. Wysocki  out:
2285d9a789c7SRafael J. Wysocki 	pr_debug("governor: change or update limits\n");
2286d9a789c7SRafael J. Wysocki 	return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
22871da177e4SLinus Torvalds }
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds /**
22901da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
22911da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
22921da177e4SLinus Torvalds  *
229325985edcSLucas De Marchi  *	Useful for policy notifiers which have different necessities
22941da177e4SLinus Torvalds  *	at different times.
22951da177e4SLinus Torvalds  */
22961da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
22971da177e4SLinus Torvalds {
22983a3e9e06SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
22993a3e9e06SViresh Kumar 	struct cpufreq_policy new_policy;
2300f1829e4aSJulia Lawall 	int ret;
23011da177e4SLinus Torvalds 
2302fefa8ff8SAaron Plattner 	if (!policy)
2303fefa8ff8SAaron Plattner 		return -ENODEV;
23041da177e4SLinus Torvalds 
2305ad7722daSviresh kumar 	down_write(&policy->rwsem);
23061da177e4SLinus Torvalds 
23072d06d8c4SDominik Brodowski 	pr_debug("updating policy for CPU %u\n", cpu);
2308d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
23093a3e9e06SViresh Kumar 	new_policy.min = policy->user_policy.min;
23103a3e9e06SViresh Kumar 	new_policy.max = policy->user_policy.max;
23113a3e9e06SViresh Kumar 	new_policy.policy = policy->user_policy.policy;
23123a3e9e06SViresh Kumar 	new_policy.governor = policy->user_policy.governor;
23131da177e4SLinus Torvalds 
2314bb176f7dSViresh Kumar 	/*
2315bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
2316bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
2317bb176f7dSViresh Kumar 	 */
23182ed99e39SRafael J. Wysocki 	if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
23193a3e9e06SViresh Kumar 		new_policy.cur = cpufreq_driver->get(cpu);
2320bd0fa9bbSViresh Kumar 		if (WARN_ON(!new_policy.cur)) {
2321bd0fa9bbSViresh Kumar 			ret = -EIO;
2322fefa8ff8SAaron Plattner 			goto unlock;
2323bd0fa9bbSViresh Kumar 		}
2324bd0fa9bbSViresh Kumar 
23253a3e9e06SViresh Kumar 		if (!policy->cur) {
2326e837f9b5SJoe Perches 			pr_debug("Driver did not initialize current freq\n");
23273a3e9e06SViresh Kumar 			policy->cur = new_policy.cur;
2328a85f7bd3SThomas Renninger 		} else {
23299c0ebcf7SViresh Kumar 			if (policy->cur != new_policy.cur && has_target())
2330a1e1dc41SViresh Kumar 				cpufreq_out_of_sync(policy, new_policy.cur);
23310961dd0dSThomas Renninger 		}
2332a85f7bd3SThomas Renninger 	}
23330961dd0dSThomas Renninger 
2334037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
23351da177e4SLinus Torvalds 
2336fefa8ff8SAaron Plattner unlock:
2337ad7722daSviresh kumar 	up_write(&policy->rwsem);
23385a01f2e8SVenkatesh Pallipadi 
23393a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
23401da177e4SLinus Torvalds 	return ret;
23411da177e4SLinus Torvalds }
23421da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
23431da177e4SLinus Torvalds 
23442760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb,
2345c32b6b8eSAshok Raj 					unsigned long action, void *hcpu)
2346c32b6b8eSAshok Raj {
2347c32b6b8eSAshok Raj 	unsigned int cpu = (unsigned long)hcpu;
23488a25a2fdSKay Sievers 	struct device *dev;
2349c32b6b8eSAshok Raj 
23508a25a2fdSKay Sievers 	dev = get_cpu_device(cpu);
23518a25a2fdSKay Sievers 	if (dev) {
23525302c3fbSSrivatsa S. Bhat 		switch (action & ~CPU_TASKS_FROZEN) {
2353c32b6b8eSAshok Raj 		case CPU_ONLINE:
235423faf0b7SViresh Kumar 			cpufreq_add_dev(dev, NULL);
2355c32b6b8eSAshok Raj 			break;
23565302c3fbSSrivatsa S. Bhat 
2357c32b6b8eSAshok Raj 		case CPU_DOWN_PREPARE:
235896bbbe4aSViresh Kumar 			__cpufreq_remove_dev_prepare(dev, NULL);
23591aee40acSSrivatsa S. Bhat 			break;
23601aee40acSSrivatsa S. Bhat 
23611aee40acSSrivatsa S. Bhat 		case CPU_POST_DEAD:
236296bbbe4aSViresh Kumar 			__cpufreq_remove_dev_finish(dev, NULL);
2363c32b6b8eSAshok Raj 			break;
23645302c3fbSSrivatsa S. Bhat 
23655a01f2e8SVenkatesh Pallipadi 		case CPU_DOWN_FAILED:
236623faf0b7SViresh Kumar 			cpufreq_add_dev(dev, NULL);
2367c32b6b8eSAshok Raj 			break;
2368c32b6b8eSAshok Raj 		}
2369c32b6b8eSAshok Raj 	}
2370c32b6b8eSAshok Raj 	return NOTIFY_OK;
2371c32b6b8eSAshok Raj }
2372c32b6b8eSAshok Raj 
23739c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = {
2374c32b6b8eSAshok Raj 	.notifier_call = cpufreq_cpu_callback,
2375c32b6b8eSAshok Raj };
23761da177e4SLinus Torvalds 
23771da177e4SLinus Torvalds /*********************************************************************
23786f19efc0SLukasz Majewski  *               BOOST						     *
23796f19efc0SLukasz Majewski  *********************************************************************/
23806f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state)
23816f19efc0SLukasz Majewski {
23826f19efc0SLukasz Majewski 	struct cpufreq_frequency_table *freq_table;
23836f19efc0SLukasz Majewski 	struct cpufreq_policy *policy;
23846f19efc0SLukasz Majewski 	int ret = -EINVAL;
23856f19efc0SLukasz Majewski 
2386f963735aSViresh Kumar 	for_each_active_policy(policy) {
23876f19efc0SLukasz Majewski 		freq_table = cpufreq_frequency_get_table(policy->cpu);
23886f19efc0SLukasz Majewski 		if (freq_table) {
23896f19efc0SLukasz Majewski 			ret = cpufreq_frequency_table_cpuinfo(policy,
23906f19efc0SLukasz Majewski 							freq_table);
23916f19efc0SLukasz Majewski 			if (ret) {
23926f19efc0SLukasz Majewski 				pr_err("%s: Policy frequency update failed\n",
23936f19efc0SLukasz Majewski 				       __func__);
23946f19efc0SLukasz Majewski 				break;
23956f19efc0SLukasz Majewski 			}
23966f19efc0SLukasz Majewski 			policy->user_policy.max = policy->max;
23976f19efc0SLukasz Majewski 			__cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
23986f19efc0SLukasz Majewski 		}
23996f19efc0SLukasz Majewski 	}
24006f19efc0SLukasz Majewski 
24016f19efc0SLukasz Majewski 	return ret;
24026f19efc0SLukasz Majewski }
24036f19efc0SLukasz Majewski 
24046f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state)
24056f19efc0SLukasz Majewski {
24066f19efc0SLukasz Majewski 	unsigned long flags;
24076f19efc0SLukasz Majewski 	int ret = 0;
24086f19efc0SLukasz Majewski 
24096f19efc0SLukasz Majewski 	if (cpufreq_driver->boost_enabled == state)
24106f19efc0SLukasz Majewski 		return 0;
24116f19efc0SLukasz Majewski 
24126f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
24136f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = state;
24146f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24156f19efc0SLukasz Majewski 
24166f19efc0SLukasz Majewski 	ret = cpufreq_driver->set_boost(state);
24176f19efc0SLukasz Majewski 	if (ret) {
24186f19efc0SLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
24196f19efc0SLukasz Majewski 		cpufreq_driver->boost_enabled = !state;
24206f19efc0SLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24216f19efc0SLukasz Majewski 
2422e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST\n",
2423e837f9b5SJoe Perches 		       __func__, state ? "enable" : "disable");
24246f19efc0SLukasz Majewski 	}
24256f19efc0SLukasz Majewski 
24266f19efc0SLukasz Majewski 	return ret;
24276f19efc0SLukasz Majewski }
24286f19efc0SLukasz Majewski 
24296f19efc0SLukasz Majewski int cpufreq_boost_supported(void)
24306f19efc0SLukasz Majewski {
24316f19efc0SLukasz Majewski 	if (likely(cpufreq_driver))
24326f19efc0SLukasz Majewski 		return cpufreq_driver->boost_supported;
24336f19efc0SLukasz Majewski 
24346f19efc0SLukasz Majewski 	return 0;
24356f19efc0SLukasz Majewski }
24366f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
24376f19efc0SLukasz Majewski 
24386f19efc0SLukasz Majewski int cpufreq_boost_enabled(void)
24396f19efc0SLukasz Majewski {
24406f19efc0SLukasz Majewski 	return cpufreq_driver->boost_enabled;
24416f19efc0SLukasz Majewski }
24426f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
24436f19efc0SLukasz Majewski 
24446f19efc0SLukasz Majewski /*********************************************************************
24451da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
24461da177e4SLinus Torvalds  *********************************************************************/
24471da177e4SLinus Torvalds 
24481da177e4SLinus Torvalds /**
24491da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
24501da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
24511da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
24521da177e4SLinus Torvalds  *
24531da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
24541da177e4SLinus Torvalds  * returns zero on success, -EBUSY when another driver got here first
24551da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
24561da177e4SLinus Torvalds  *
24571da177e4SLinus Torvalds  */
2458221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
24591da177e4SLinus Torvalds {
24601da177e4SLinus Torvalds 	unsigned long flags;
24611da177e4SLinus Torvalds 	int ret;
24621da177e4SLinus Torvalds 
2463a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2464a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2465a7b422cdSKonrad Rzeszutek Wilk 
24661da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
24679c0ebcf7SViresh Kumar 	    !(driver_data->setpolicy || driver_data->target_index ||
24689832235fSRafael J. Wysocki 		    driver_data->target) ||
24699832235fSRafael J. Wysocki 	     (driver_data->setpolicy && (driver_data->target_index ||
24701c03a2d0SViresh Kumar 		    driver_data->target)) ||
24711c03a2d0SViresh Kumar 	     (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
24721da177e4SLinus Torvalds 		return -EINVAL;
24731da177e4SLinus Torvalds 
24742d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
24751da177e4SLinus Torvalds 
24760d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
24771c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
24780d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24794dea5806SYinghai Lu 		return -EEXIST;
24801da177e4SLinus Torvalds 	}
24811c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
24820d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24831da177e4SLinus Torvalds 
2484bc68b7dfSViresh Kumar 	if (driver_data->setpolicy)
2485bc68b7dfSViresh Kumar 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
2486bc68b7dfSViresh Kumar 
24876f19efc0SLukasz Majewski 	if (cpufreq_boost_supported()) {
24886f19efc0SLukasz Majewski 		/*
24896f19efc0SLukasz Majewski 		 * Check if driver provides function to enable boost -
24906f19efc0SLukasz Majewski 		 * if not, use cpufreq_boost_set_sw as default
24916f19efc0SLukasz Majewski 		 */
24926f19efc0SLukasz Majewski 		if (!cpufreq_driver->set_boost)
24936f19efc0SLukasz Majewski 			cpufreq_driver->set_boost = cpufreq_boost_set_sw;
24946f19efc0SLukasz Majewski 
24956f19efc0SLukasz Majewski 		ret = cpufreq_sysfs_create_file(&boost.attr);
24966f19efc0SLukasz Majewski 		if (ret) {
24976f19efc0SLukasz Majewski 			pr_err("%s: cannot register global BOOST sysfs file\n",
24986f19efc0SLukasz Majewski 			       __func__);
24996f19efc0SLukasz Majewski 			goto err_null_driver;
25006f19efc0SLukasz Majewski 		}
25016f19efc0SLukasz Majewski 	}
25026f19efc0SLukasz Majewski 
25038a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
25048f5bc2abSJiri Slaby 	if (ret)
25056f19efc0SLukasz Majewski 		goto err_boost_unreg;
25061da177e4SLinus Torvalds 
2507ce1bcfe9SViresh Kumar 	if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2508ce1bcfe9SViresh Kumar 	    list_empty(&cpufreq_policy_list)) {
25091da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
2510ce1bcfe9SViresh Kumar 		pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2511e08f5f5bSGautham R Shenoy 			 driver_data->name);
25128a25a2fdSKay Sievers 		goto err_if_unreg;
25131da177e4SLinus Torvalds 	}
25141da177e4SLinus Torvalds 
251565edc68cSChandra Seetharaman 	register_hotcpu_notifier(&cpufreq_cpu_notifier);
25162d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
25171da177e4SLinus Torvalds 
25188f5bc2abSJiri Slaby 	return 0;
25198a25a2fdSKay Sievers err_if_unreg:
25208a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
25216f19efc0SLukasz Majewski err_boost_unreg:
25226f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
25236f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
25248f5bc2abSJiri Slaby err_null_driver:
25250d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
25261c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
25270d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
25284d34a67dSDave Jones 	return ret;
25291da177e4SLinus Torvalds }
25301da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
25311da177e4SLinus Torvalds 
25321da177e4SLinus Torvalds /**
25331da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
25341da177e4SLinus Torvalds  *
25351da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
25361da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
25371da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
25381da177e4SLinus Torvalds  * currently not initialised.
25391da177e4SLinus Torvalds  */
2540221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
25411da177e4SLinus Torvalds {
25421da177e4SLinus Torvalds 	unsigned long flags;
25431da177e4SLinus Torvalds 
25441c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
25451da177e4SLinus Torvalds 		return -EINVAL;
25461da177e4SLinus Torvalds 
25472d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
25481da177e4SLinus Torvalds 
25498a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
25506f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
25516f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
25526f19efc0SLukasz Majewski 
255365edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
25541da177e4SLinus Torvalds 
25556eed9404SViresh Kumar 	down_write(&cpufreq_rwsem);
25560d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
25576eed9404SViresh Kumar 
25581c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
25596eed9404SViresh Kumar 
25600d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
25616eed9404SViresh Kumar 	up_write(&cpufreq_rwsem);
25621da177e4SLinus Torvalds 
25631da177e4SLinus Torvalds 	return 0;
25641da177e4SLinus Torvalds }
25651da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
25665a01f2e8SVenkatesh Pallipadi 
256790de2a4aSDoug Anderson /*
256890de2a4aSDoug Anderson  * Stop cpufreq at shutdown to make sure it isn't holding any locks
256990de2a4aSDoug Anderson  * or mutexes when secondary CPUs are halted.
257090de2a4aSDoug Anderson  */
257190de2a4aSDoug Anderson static struct syscore_ops cpufreq_syscore_ops = {
257290de2a4aSDoug Anderson 	.shutdown = cpufreq_suspend,
257390de2a4aSDoug Anderson };
257490de2a4aSDoug Anderson 
25755a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
25765a01f2e8SVenkatesh Pallipadi {
2577a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2578a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2579a7b422cdSKonrad Rzeszutek Wilk 
25802361be23SViresh Kumar 	cpufreq_global_kobject = kobject_create();
25818aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
25828aa84ad8SThomas Renninger 
258390de2a4aSDoug Anderson 	register_syscore_ops(&cpufreq_syscore_ops);
258490de2a4aSDoug Anderson 
25855a01f2e8SVenkatesh Pallipadi 	return 0;
25865a01f2e8SVenkatesh Pallipadi }
25875a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2588