xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision 4e97b631f24c927b2302368f4f83efbba82076ee)
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>
29e00e56dfSRafael J. Wysocki #include <linux/syscore_ops.h>
305ff0a268SViresh Kumar #include <linux/tick.h>
316f4f2723SThomas Renninger #include <trace/events/power.h>
326f4f2723SThomas Renninger 
331da177e4SLinus Torvalds /**
34cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
351da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
361da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
371da177e4SLinus Torvalds  */
381c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
397a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
408414809cSSrivatsa S. Bhat static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
41bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
426f1e4efdSJane Li DEFINE_MUTEX(cpufreq_governor_lock);
43c88a1f8bSLukasz Majewski static LIST_HEAD(cpufreq_policy_list);
44bb176f7dSViresh Kumar 
45084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU
46084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */
47e77b89f1SDmitry Monakhov static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
48084f3493SThomas Renninger #endif
491da177e4SLinus Torvalds 
509c0ebcf7SViresh Kumar static inline bool has_target(void)
519c0ebcf7SViresh Kumar {
529c0ebcf7SViresh Kumar 	return cpufreq_driver->target_index || cpufreq_driver->target;
539c0ebcf7SViresh Kumar }
549c0ebcf7SViresh Kumar 
555a01f2e8SVenkatesh Pallipadi /*
566eed9404SViresh Kumar  * rwsem to guarantee that cpufreq driver module doesn't unload during critical
576eed9404SViresh Kumar  * sections
586eed9404SViresh Kumar  */
596eed9404SViresh Kumar static DECLARE_RWSEM(cpufreq_rwsem);
606eed9404SViresh Kumar 
611da177e4SLinus Torvalds /* internal prototypes */
6229464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy,
6329464f28SDave Jones 		unsigned int event);
645a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu);
6565f27f38SDavid Howells static void handle_update(struct work_struct *work);
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds /**
681da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
691da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
701da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
711da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
721da177e4SLinus Torvalds  * The mutex locks both lists.
731da177e4SLinus Torvalds  */
74e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
75b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list;
761da177e4SLinus Torvalds 
7774212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called;
78b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void)
79b4dfdbb3SAlan Stern {
80b4dfdbb3SAlan Stern 	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
8174212ca4SCesar Eduardo Barros 	init_cpufreq_transition_notifier_list_called = true;
82b4dfdbb3SAlan Stern 	return 0;
83b4dfdbb3SAlan Stern }
84b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list);
851da177e4SLinus Torvalds 
86a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
87da584455SViresh Kumar static int cpufreq_disabled(void)
88a7b422cdSKonrad Rzeszutek Wilk {
89a7b422cdSKonrad Rzeszutek Wilk 	return off;
90a7b422cdSKonrad Rzeszutek Wilk }
91a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
92a7b422cdSKonrad Rzeszutek Wilk {
93a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
94a7b422cdSKonrad Rzeszutek Wilk }
951da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list);
963fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
971da177e4SLinus Torvalds 
984d5dcc42SViresh Kumar bool have_governor_per_policy(void)
994d5dcc42SViresh Kumar {
1000b981e70SViresh Kumar 	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
1014d5dcc42SViresh Kumar }
1023f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1034d5dcc42SViresh Kumar 
104944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
105944e9a03SViresh Kumar {
106944e9a03SViresh Kumar 	if (have_governor_per_policy())
107944e9a03SViresh Kumar 		return &policy->kobj;
108944e9a03SViresh Kumar 	else
109944e9a03SViresh Kumar 		return cpufreq_global_kobject;
110944e9a03SViresh Kumar }
111944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
112944e9a03SViresh Kumar 
11372a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
11472a4ce34SViresh Kumar {
11572a4ce34SViresh Kumar 	u64 idle_time;
11672a4ce34SViresh Kumar 	u64 cur_wall_time;
11772a4ce34SViresh Kumar 	u64 busy_time;
11872a4ce34SViresh Kumar 
11972a4ce34SViresh Kumar 	cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
12072a4ce34SViresh Kumar 
12172a4ce34SViresh Kumar 	busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
12272a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
12372a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
12472a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
12572a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
12672a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
12772a4ce34SViresh Kumar 
12872a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
12972a4ce34SViresh Kumar 	if (wall)
13072a4ce34SViresh Kumar 		*wall = cputime_to_usecs(cur_wall_time);
13172a4ce34SViresh Kumar 
13272a4ce34SViresh Kumar 	return cputime_to_usecs(idle_time);
13372a4ce34SViresh Kumar }
13472a4ce34SViresh Kumar 
13572a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
13672a4ce34SViresh Kumar {
13772a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
13872a4ce34SViresh Kumar 
13972a4ce34SViresh Kumar 	if (idle_time == -1ULL)
14072a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
14172a4ce34SViresh Kumar 	else if (!io_busy)
14272a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
14372a4ce34SViresh Kumar 
14472a4ce34SViresh Kumar 	return idle_time;
14572a4ce34SViresh Kumar }
14672a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
14772a4ce34SViresh Kumar 
14870e9e778SViresh Kumar /*
14970e9e778SViresh Kumar  * This is a generic cpufreq init() routine which can be used by cpufreq
15070e9e778SViresh Kumar  * drivers of SMP systems. It will do following:
15170e9e778SViresh Kumar  * - validate & show freq table passed
15270e9e778SViresh Kumar  * - set policies transition latency
15370e9e778SViresh Kumar  * - policy->cpus with all possible CPUs
15470e9e778SViresh Kumar  */
15570e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy,
15670e9e778SViresh Kumar 		struct cpufreq_frequency_table *table,
15770e9e778SViresh Kumar 		unsigned int transition_latency)
15870e9e778SViresh Kumar {
15970e9e778SViresh Kumar 	int ret;
16070e9e778SViresh Kumar 
16170e9e778SViresh Kumar 	ret = cpufreq_table_validate_and_show(policy, table);
16270e9e778SViresh Kumar 	if (ret) {
16370e9e778SViresh Kumar 		pr_err("%s: invalid frequency table: %d\n", __func__, ret);
16470e9e778SViresh Kumar 		return ret;
16570e9e778SViresh Kumar 	}
16670e9e778SViresh Kumar 
16770e9e778SViresh Kumar 	policy->cpuinfo.transition_latency = transition_latency;
16870e9e778SViresh Kumar 
16970e9e778SViresh Kumar 	/*
17070e9e778SViresh Kumar 	 * The driver only supports the SMP configuartion where all processors
17170e9e778SViresh Kumar 	 * share the clock and voltage and clock.
17270e9e778SViresh Kumar 	 */
17370e9e778SViresh Kumar 	cpumask_setall(policy->cpus);
17470e9e778SViresh Kumar 
17570e9e778SViresh Kumar 	return 0;
17670e9e778SViresh Kumar }
17770e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init);
17870e9e778SViresh Kumar 
179652ed95dSViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu)
180652ed95dSViresh Kumar {
181652ed95dSViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
182652ed95dSViresh Kumar 
183652ed95dSViresh Kumar 	if (!policy || IS_ERR(policy->clk)) {
184652ed95dSViresh Kumar 		pr_err("%s: No %s associated to cpu: %d\n", __func__,
185652ed95dSViresh Kumar 				policy ? "clk" : "policy", cpu);
186652ed95dSViresh Kumar 		return 0;
187652ed95dSViresh Kumar 	}
188652ed95dSViresh Kumar 
189652ed95dSViresh Kumar 	return clk_get_rate(policy->clk) / 1000;
190652ed95dSViresh Kumar }
191652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get);
192652ed95dSViresh Kumar 
1936eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
1941da177e4SLinus Torvalds {
1956eed9404SViresh Kumar 	struct cpufreq_policy *policy = NULL;
1961da177e4SLinus Torvalds 	unsigned long flags;
1971da177e4SLinus Torvalds 
1986eed9404SViresh Kumar 	if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
1996eed9404SViresh Kumar 		return NULL;
2006eed9404SViresh Kumar 
2016eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
2026eed9404SViresh Kumar 		return NULL;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	/* get the cpufreq driver */
2050d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
2061da177e4SLinus Torvalds 
2076eed9404SViresh Kumar 	if (cpufreq_driver) {
2081da177e4SLinus Torvalds 		/* get the CPU */
2093a3e9e06SViresh Kumar 		policy = per_cpu(cpufreq_cpu_data, cpu);
2106eed9404SViresh Kumar 		if (policy)
2116eed9404SViresh Kumar 			kobject_get(&policy->kobj);
2126eed9404SViresh Kumar 	}
2136eed9404SViresh Kumar 
2146eed9404SViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2151da177e4SLinus Torvalds 
2163a3e9e06SViresh Kumar 	if (!policy)
2176eed9404SViresh Kumar 		up_read(&cpufreq_rwsem);
2181da177e4SLinus Torvalds 
2193a3e9e06SViresh Kumar 	return policy;
220a9144436SStephen Boyd }
2211da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2221da177e4SLinus Torvalds 
2233a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
224a9144436SStephen Boyd {
225d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
226d5aaffa9SDirk Brandewie 		return;
227d5aaffa9SDirk Brandewie 
2286eed9404SViresh Kumar 	kobject_put(&policy->kobj);
2296eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
230a9144436SStephen Boyd }
2311da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds /*********************************************************************
2341da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2351da177e4SLinus Torvalds  *********************************************************************/
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds /**
2381da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
2391da177e4SLinus Torvalds  *
2401da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
2411da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
2421da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
2431da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
2441da177e4SLinus Torvalds  */
2451da177e4SLinus Torvalds #ifndef CONFIG_SMP
2461da177e4SLinus Torvalds static unsigned long l_p_j_ref;
2471da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq;
2481da177e4SLinus Torvalds 
249858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
2501da177e4SLinus Torvalds {
2511da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
2521da177e4SLinus Torvalds 		return;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
2551da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
2561da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
2572d06d8c4SDominik Brodowski 		pr_debug("saving %lu as reference value for loops_per_jiffy; "
258e08f5f5bSGautham R Shenoy 			"freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
2591da177e4SLinus Torvalds 	}
260d08de0c1SAfzal Mohammed 	if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
26142d4dc3fSBenjamin Herrenschmidt 	    (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
262e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
263e08f5f5bSGautham R Shenoy 								ci->new);
2642d06d8c4SDominik Brodowski 		pr_debug("scaling loops_per_jiffy to %lu "
265e08f5f5bSGautham R Shenoy 			"for frequency %u kHz\n", loops_per_jiffy, ci->new);
2661da177e4SLinus Torvalds 	}
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds #else
269e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
270e08f5f5bSGautham R Shenoy {
271e08f5f5bSGautham R Shenoy 	return;
272e08f5f5bSGautham R Shenoy }
2731da177e4SLinus Torvalds #endif
2741da177e4SLinus Torvalds 
2750956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
276b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
2771da177e4SLinus Torvalds {
2781da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
2791da177e4SLinus Torvalds 
280d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
281d5aaffa9SDirk Brandewie 		return;
282d5aaffa9SDirk Brandewie 
2831c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
2842d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
285e4472cb3SDave Jones 		state, freqs->new);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	switch (state) {
288e4472cb3SDave Jones 
2891da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
290e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
291e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
292e4472cb3SDave Jones 		 * "old frequency".
2931da177e4SLinus Torvalds 		 */
2941c3d85ddSRafael J. Wysocki 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
295e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
296e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
2972d06d8c4SDominik Brodowski 				pr_debug("Warning: CPU frequency is"
298e4472cb3SDave Jones 					" %u, cpufreq assumed %u kHz.\n",
299e4472cb3SDave Jones 					freqs->old, policy->cur);
300e4472cb3SDave Jones 				freqs->old = policy->cur;
3011da177e4SLinus Torvalds 			}
3021da177e4SLinus Torvalds 		}
303b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
304e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
3051da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3061da177e4SLinus Torvalds 		break;
307e4472cb3SDave Jones 
3081da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3091da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
3102d06d8c4SDominik Brodowski 		pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
3116f4f2723SThomas Renninger 			(unsigned long)freqs->cpu);
31225e41933SThomas Renninger 		trace_cpu_frequency(freqs->new, freqs->cpu);
313b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
314e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
315e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
316e4472cb3SDave Jones 			policy->cur = freqs->new;
3171da177e4SLinus Torvalds 		break;
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds }
320bb176f7dSViresh Kumar 
321b43a7ffbSViresh Kumar /**
322b43a7ffbSViresh Kumar  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
323b43a7ffbSViresh Kumar  * on frequency transition.
324b43a7ffbSViresh Kumar  *
325b43a7ffbSViresh Kumar  * This function calls the transition notifiers and the "adjust_jiffies"
326b43a7ffbSViresh Kumar  * function. It is called twice on all CPU frequency changes that have
327b43a7ffbSViresh Kumar  * external effects.
328b43a7ffbSViresh Kumar  */
329b43a7ffbSViresh Kumar void cpufreq_notify_transition(struct cpufreq_policy *policy,
330b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
331b43a7ffbSViresh Kumar {
332b43a7ffbSViresh Kumar 	for_each_cpu(freqs->cpu, policy->cpus)
333b43a7ffbSViresh Kumar 		__cpufreq_notify_transition(policy, freqs, state);
334b43a7ffbSViresh Kumar }
3351da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
3361da177e4SLinus Torvalds 
337f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */
338f7ba3b41SViresh Kumar void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
339f7ba3b41SViresh Kumar 		struct cpufreq_freqs *freqs, int transition_failed)
340f7ba3b41SViresh Kumar {
341f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
342f7ba3b41SViresh Kumar 	if (!transition_failed)
343f7ba3b41SViresh Kumar 		return;
344f7ba3b41SViresh Kumar 
345f7ba3b41SViresh Kumar 	swap(freqs->old, freqs->new);
346f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
347f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
348f7ba3b41SViresh Kumar }
349f7ba3b41SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition);
350f7ba3b41SViresh Kumar 
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds /*********************************************************************
3531da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
3541da177e4SLinus Torvalds  *********************************************************************/
3556f19efc0SLukasz Majewski ssize_t show_boost(struct kobject *kobj,
3566f19efc0SLukasz Majewski 				 struct attribute *attr, char *buf)
3576f19efc0SLukasz Majewski {
3586f19efc0SLukasz Majewski 	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
3596f19efc0SLukasz Majewski }
3606f19efc0SLukasz Majewski 
3616f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
3626f19efc0SLukasz Majewski 				  const char *buf, size_t count)
3636f19efc0SLukasz Majewski {
3646f19efc0SLukasz Majewski 	int ret, enable;
3656f19efc0SLukasz Majewski 
3666f19efc0SLukasz Majewski 	ret = sscanf(buf, "%d", &enable);
3676f19efc0SLukasz Majewski 	if (ret != 1 || enable < 0 || enable > 1)
3686f19efc0SLukasz Majewski 		return -EINVAL;
3696f19efc0SLukasz Majewski 
3706f19efc0SLukasz Majewski 	if (cpufreq_boost_trigger_state(enable)) {
3716f19efc0SLukasz Majewski 		pr_err("%s: Cannot %s BOOST!\n", __func__,
3726f19efc0SLukasz Majewski 		       enable ? "enable" : "disable");
3736f19efc0SLukasz Majewski 		return -EINVAL;
3746f19efc0SLukasz Majewski 	}
3756f19efc0SLukasz Majewski 
3766f19efc0SLukasz Majewski 	pr_debug("%s: cpufreq BOOST %s\n", __func__,
3776f19efc0SLukasz Majewski 		 enable ? "enabled" : "disabled");
3786f19efc0SLukasz Majewski 
3796f19efc0SLukasz Majewski 	return count;
3806f19efc0SLukasz Majewski }
3816f19efc0SLukasz Majewski define_one_global_rw(boost);
3821da177e4SLinus Torvalds 
3833bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor)
3843bcb09a3SJeremy Fitzhardinge {
3853bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
3863bcb09a3SJeremy Fitzhardinge 
3873bcb09a3SJeremy Fitzhardinge 	list_for_each_entry(t, &cpufreq_governor_list, governor_list)
3883bcb09a3SJeremy Fitzhardinge 		if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
3893bcb09a3SJeremy Fitzhardinge 			return t;
3903bcb09a3SJeremy Fitzhardinge 
3913bcb09a3SJeremy Fitzhardinge 	return NULL;
3923bcb09a3SJeremy Fitzhardinge }
3933bcb09a3SJeremy Fitzhardinge 
3941da177e4SLinus Torvalds /**
3951da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
3961da177e4SLinus Torvalds  */
3971da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
3981da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
3991da177e4SLinus Torvalds {
4003bcb09a3SJeremy Fitzhardinge 	int err = -EINVAL;
4013bcb09a3SJeremy Fitzhardinge 
4021c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver)
4033bcb09a3SJeremy Fitzhardinge 		goto out;
4043bcb09a3SJeremy Fitzhardinge 
4051c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
4061da177e4SLinus Torvalds 		if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
4071da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
4083bcb09a3SJeremy Fitzhardinge 			err = 0;
409e08f5f5bSGautham R Shenoy 		} else if (!strnicmp(str_governor, "powersave",
410e08f5f5bSGautham R Shenoy 						CPUFREQ_NAME_LEN)) {
4111da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
4123bcb09a3SJeremy Fitzhardinge 			err = 0;
4131da177e4SLinus Torvalds 		}
4149c0ebcf7SViresh Kumar 	} else if (has_target()) {
4151da177e4SLinus Torvalds 		struct cpufreq_governor *t;
4163bcb09a3SJeremy Fitzhardinge 
4173fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
4183bcb09a3SJeremy Fitzhardinge 
4193bcb09a3SJeremy Fitzhardinge 		t = __find_governor(str_governor);
4203bcb09a3SJeremy Fitzhardinge 
421ea714970SJeremy Fitzhardinge 		if (t == NULL) {
422ea714970SJeremy Fitzhardinge 			int ret;
423ea714970SJeremy Fitzhardinge 
424ea714970SJeremy Fitzhardinge 			mutex_unlock(&cpufreq_governor_mutex);
4251a8e1463SKees Cook 			ret = request_module("cpufreq_%s", str_governor);
426ea714970SJeremy Fitzhardinge 			mutex_lock(&cpufreq_governor_mutex);
427ea714970SJeremy Fitzhardinge 
428ea714970SJeremy Fitzhardinge 			if (ret == 0)
429ea714970SJeremy Fitzhardinge 				t = __find_governor(str_governor);
430ea714970SJeremy Fitzhardinge 		}
431ea714970SJeremy Fitzhardinge 
4323bcb09a3SJeremy Fitzhardinge 		if (t != NULL) {
4331da177e4SLinus Torvalds 			*governor = t;
4343bcb09a3SJeremy Fitzhardinge 			err = 0;
4351da177e4SLinus Torvalds 		}
4363bcb09a3SJeremy Fitzhardinge 
4373bcb09a3SJeremy Fitzhardinge 		mutex_unlock(&cpufreq_governor_mutex);
4381da177e4SLinus Torvalds 	}
4391da177e4SLinus Torvalds out:
4403bcb09a3SJeremy Fitzhardinge 	return err;
4411da177e4SLinus Torvalds }
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds /**
444e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
445e08f5f5bSGautham R Shenoy  * print out cpufreq information
4461da177e4SLinus Torvalds  *
4471da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
4481da177e4SLinus Torvalds  * "unsigned int".
4491da177e4SLinus Torvalds  */
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds #define show_one(file_name, object)			\
4521da177e4SLinus Torvalds static ssize_t show_##file_name				\
4531da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
4541da177e4SLinus Torvalds {							\
4551da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
4591da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
460ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
4611da177e4SLinus Torvalds show_one(scaling_min_freq, min);
4621da177e4SLinus Torvalds show_one(scaling_max_freq, max);
4631da177e4SLinus Torvalds show_one(scaling_cur_freq, cur);
4641da177e4SLinus Torvalds 
465037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
4663a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy);
4677970e08bSThomas Renninger 
4681da177e4SLinus Torvalds /**
4691da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
4701da177e4SLinus Torvalds  */
4711da177e4SLinus Torvalds #define store_one(file_name, object)			\
4721da177e4SLinus Torvalds static ssize_t store_##file_name					\
4731da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
4741da177e4SLinus Torvalds {									\
4755136fa56SSrivatsa S. Bhat 	int ret;							\
4761da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
4771da177e4SLinus Torvalds 									\
4781da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
4791da177e4SLinus Torvalds 	if (ret)							\
4801da177e4SLinus Torvalds 		return -EINVAL;						\
4811da177e4SLinus Torvalds 									\
4821da177e4SLinus Torvalds 	ret = sscanf(buf, "%u", &new_policy.object);			\
4831da177e4SLinus Torvalds 	if (ret != 1)							\
4841da177e4SLinus Torvalds 		return -EINVAL;						\
4851da177e4SLinus Torvalds 									\
486037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);		\
4877970e08bSThomas Renninger 	policy->user_policy.object = policy->object;			\
4881da177e4SLinus Torvalds 									\
4891da177e4SLinus Torvalds 	return ret ? ret : count;					\
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds store_one(scaling_min_freq, min);
4931da177e4SLinus Torvalds store_one(scaling_max_freq, max);
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds /**
4961da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
4971da177e4SLinus Torvalds  */
498e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
499e08f5f5bSGautham R Shenoy 					char *buf)
5001da177e4SLinus Torvalds {
5015a01f2e8SVenkatesh Pallipadi 	unsigned int cur_freq = __cpufreq_get(policy->cpu);
5021da177e4SLinus Torvalds 	if (!cur_freq)
5031da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
5041da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds /**
5081da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
5091da177e4SLinus Torvalds  */
510905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
5111da177e4SLinus Torvalds {
5121da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
5131da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
5141da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
5151da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
5161da177e4SLinus Torvalds 	else if (policy->governor)
5174b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
51829464f28SDave Jones 				policy->governor->name);
5191da177e4SLinus Torvalds 	return -EINVAL;
5201da177e4SLinus Torvalds }
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds /**
5231da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
5241da177e4SLinus Torvalds  */
5251da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
5261da177e4SLinus Torvalds 					const char *buf, size_t count)
5271da177e4SLinus Torvalds {
5285136fa56SSrivatsa S. Bhat 	int ret;
5291da177e4SLinus Torvalds 	char	str_governor[16];
5301da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);
5331da177e4SLinus Torvalds 	if (ret)
5341da177e4SLinus Torvalds 		return ret;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
5371da177e4SLinus Torvalds 	if (ret != 1)
5381da177e4SLinus Torvalds 		return -EINVAL;
5391da177e4SLinus Torvalds 
540e08f5f5bSGautham R Shenoy 	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
541e08f5f5bSGautham R Shenoy 						&new_policy.governor))
5421da177e4SLinus Torvalds 		return -EINVAL;
5431da177e4SLinus Torvalds 
544037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
5457970e08bSThomas Renninger 
5467970e08bSThomas Renninger 	policy->user_policy.policy = policy->policy;
5477970e08bSThomas Renninger 	policy->user_policy.governor = policy->governor;
5487970e08bSThomas Renninger 
549e08f5f5bSGautham R Shenoy 	if (ret)
550e08f5f5bSGautham R Shenoy 		return ret;
551e08f5f5bSGautham R Shenoy 	else
552e08f5f5bSGautham R Shenoy 		return count;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds /**
5561da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
5571da177e4SLinus Torvalds  */
5581da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
5591da177e4SLinus Torvalds {
5601c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
5611da177e4SLinus Torvalds }
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds /**
5641da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
5651da177e4SLinus Torvalds  */
5661da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
5671da177e4SLinus Torvalds 						char *buf)
5681da177e4SLinus Torvalds {
5691da177e4SLinus Torvalds 	ssize_t i = 0;
5701da177e4SLinus Torvalds 	struct cpufreq_governor *t;
5711da177e4SLinus Torvalds 
5729c0ebcf7SViresh Kumar 	if (!has_target()) {
5731da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
5741da177e4SLinus Torvalds 		goto out;
5751da177e4SLinus Torvalds 	}
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
57829464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
57929464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
5801da177e4SLinus Torvalds 			goto out;
5814b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
5821da177e4SLinus Torvalds 	}
5831da177e4SLinus Torvalds out:
5841da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
5851da177e4SLinus Torvalds 	return i;
5861da177e4SLinus Torvalds }
587e8628dd0SDarrick J. Wong 
588f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
5891da177e4SLinus Torvalds {
5901da177e4SLinus Torvalds 	ssize_t i = 0;
5911da177e4SLinus Torvalds 	unsigned int cpu;
5921da177e4SLinus Torvalds 
593835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
5941da177e4SLinus Torvalds 		if (i)
5951da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
5961da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
5971da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
5981da177e4SLinus Torvalds 			break;
5991da177e4SLinus Torvalds 	}
6001da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
6011da177e4SLinus Torvalds 	return i;
6021da177e4SLinus Torvalds }
603f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
6041da177e4SLinus Torvalds 
605e8628dd0SDarrick J. Wong /**
606e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
607e8628dd0SDarrick J. Wong  * hw coordination is in use
608e8628dd0SDarrick J. Wong  */
609e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
610e8628dd0SDarrick J. Wong {
611f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
612e8628dd0SDarrick J. Wong }
613e8628dd0SDarrick J. Wong 
614e8628dd0SDarrick J. Wong /**
615e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
616e8628dd0SDarrick J. Wong  */
617e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
618e8628dd0SDarrick J. Wong {
619f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
620e8628dd0SDarrick J. Wong }
621e8628dd0SDarrick J. Wong 
6229e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
6239e76988eSVenki Pallipadi 					const char *buf, size_t count)
6249e76988eSVenki Pallipadi {
6259e76988eSVenki Pallipadi 	unsigned int freq = 0;
6269e76988eSVenki Pallipadi 	unsigned int ret;
6279e76988eSVenki Pallipadi 
628879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
6299e76988eSVenki Pallipadi 		return -EINVAL;
6309e76988eSVenki Pallipadi 
6319e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
6329e76988eSVenki Pallipadi 	if (ret != 1)
6339e76988eSVenki Pallipadi 		return -EINVAL;
6349e76988eSVenki Pallipadi 
6359e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
6369e76988eSVenki Pallipadi 
6379e76988eSVenki Pallipadi 	return count;
6389e76988eSVenki Pallipadi }
6399e76988eSVenki Pallipadi 
6409e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
6419e76988eSVenki Pallipadi {
642879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
6439e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
6449e76988eSVenki Pallipadi 
6459e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
6469e76988eSVenki Pallipadi }
6471da177e4SLinus Torvalds 
648e2f74f35SThomas Renninger /**
6498bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
650e2f74f35SThomas Renninger  */
651e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
652e2f74f35SThomas Renninger {
653e2f74f35SThomas Renninger 	unsigned int limit;
654e2f74f35SThomas Renninger 	int ret;
6551c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
6561c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
657e2f74f35SThomas Renninger 		if (!ret)
658e2f74f35SThomas Renninger 			return sprintf(buf, "%u\n", limit);
659e2f74f35SThomas Renninger 	}
660e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
661e2f74f35SThomas Renninger }
662e2f74f35SThomas Renninger 
6636dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
6646dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
6656dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
6666dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
6676dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
6686dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
6696dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
6706dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
6716dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
6726dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
6736dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
6746dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
6756dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
6766dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds static struct attribute *default_attrs[] = {
6791da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
6801da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
681ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
6821da177e4SLinus Torvalds 	&scaling_min_freq.attr,
6831da177e4SLinus Torvalds 	&scaling_max_freq.attr,
6841da177e4SLinus Torvalds 	&affected_cpus.attr,
685e8628dd0SDarrick J. Wong 	&related_cpus.attr,
6861da177e4SLinus Torvalds 	&scaling_governor.attr,
6871da177e4SLinus Torvalds 	&scaling_driver.attr,
6881da177e4SLinus Torvalds 	&scaling_available_governors.attr,
6899e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
6901da177e4SLinus Torvalds 	NULL
6911da177e4SLinus Torvalds };
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
6941da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
6971da177e4SLinus Torvalds {
6981da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
6991da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
7001b750e3bSViresh Kumar 	ssize_t ret;
7016eed9404SViresh Kumar 
7026eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
7031b750e3bSViresh Kumar 		return -EINVAL;
7045a01f2e8SVenkatesh Pallipadi 
705ad7722daSviresh kumar 	down_read(&policy->rwsem);
7065a01f2e8SVenkatesh Pallipadi 
707e08f5f5bSGautham R Shenoy 	if (fattr->show)
708e08f5f5bSGautham R Shenoy 		ret = fattr->show(policy, buf);
709e08f5f5bSGautham R Shenoy 	else
710e08f5f5bSGautham R Shenoy 		ret = -EIO;
711e08f5f5bSGautham R Shenoy 
712ad7722daSviresh kumar 	up_read(&policy->rwsem);
7136eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
7141b750e3bSViresh Kumar 
7151da177e4SLinus Torvalds 	return ret;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
7191da177e4SLinus Torvalds 		     const char *buf, size_t count)
7201da177e4SLinus Torvalds {
7211da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7221da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
723a07530b4SDave Jones 	ssize_t ret = -EINVAL;
7246eed9404SViresh Kumar 
7254f750c93SSrivatsa S. Bhat 	get_online_cpus();
7264f750c93SSrivatsa S. Bhat 
7274f750c93SSrivatsa S. Bhat 	if (!cpu_online(policy->cpu))
7284f750c93SSrivatsa S. Bhat 		goto unlock;
7294f750c93SSrivatsa S. Bhat 
7306eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
7314f750c93SSrivatsa S. Bhat 		goto unlock;
7325a01f2e8SVenkatesh Pallipadi 
733ad7722daSviresh kumar 	down_write(&policy->rwsem);
7345a01f2e8SVenkatesh Pallipadi 
735e08f5f5bSGautham R Shenoy 	if (fattr->store)
736e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
737e08f5f5bSGautham R Shenoy 	else
738e08f5f5bSGautham R Shenoy 		ret = -EIO;
739e08f5f5bSGautham R Shenoy 
740ad7722daSviresh kumar 	up_write(&policy->rwsem);
7416eed9404SViresh Kumar 
7426eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
7434f750c93SSrivatsa S. Bhat unlock:
7444f750c93SSrivatsa S. Bhat 	put_online_cpus();
7454f750c93SSrivatsa S. Bhat 
7461da177e4SLinus Torvalds 	return ret;
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
7501da177e4SLinus Torvalds {
7511da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7522d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
7531da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
7541da177e4SLinus Torvalds }
7551da177e4SLinus Torvalds 
75652cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
7571da177e4SLinus Torvalds 	.show	= show,
7581da177e4SLinus Torvalds 	.store	= store,
7591da177e4SLinus Torvalds };
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
7621da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
7631da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
7641da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
7651da177e4SLinus Torvalds };
7661da177e4SLinus Torvalds 
7672361be23SViresh Kumar struct kobject *cpufreq_global_kobject;
7682361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject);
7692361be23SViresh Kumar 
7702361be23SViresh Kumar static int cpufreq_global_kobject_usage;
7712361be23SViresh Kumar 
7722361be23SViresh Kumar int cpufreq_get_global_kobject(void)
7732361be23SViresh Kumar {
7742361be23SViresh Kumar 	if (!cpufreq_global_kobject_usage++)
7752361be23SViresh Kumar 		return kobject_add(cpufreq_global_kobject,
7762361be23SViresh Kumar 				&cpu_subsys.dev_root->kobj, "%s", "cpufreq");
7772361be23SViresh Kumar 
7782361be23SViresh Kumar 	return 0;
7792361be23SViresh Kumar }
7802361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject);
7812361be23SViresh Kumar 
7822361be23SViresh Kumar void cpufreq_put_global_kobject(void)
7832361be23SViresh Kumar {
7842361be23SViresh Kumar 	if (!--cpufreq_global_kobject_usage)
7852361be23SViresh Kumar 		kobject_del(cpufreq_global_kobject);
7862361be23SViresh Kumar }
7872361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject);
7882361be23SViresh Kumar 
7892361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr)
7902361be23SViresh Kumar {
7912361be23SViresh Kumar 	int ret = cpufreq_get_global_kobject();
7922361be23SViresh Kumar 
7932361be23SViresh Kumar 	if (!ret) {
7942361be23SViresh Kumar 		ret = sysfs_create_file(cpufreq_global_kobject, attr);
7952361be23SViresh Kumar 		if (ret)
7962361be23SViresh Kumar 			cpufreq_put_global_kobject();
7972361be23SViresh Kumar 	}
7982361be23SViresh Kumar 
7992361be23SViresh Kumar 	return ret;
8002361be23SViresh Kumar }
8012361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file);
8022361be23SViresh Kumar 
8032361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr)
8042361be23SViresh Kumar {
8052361be23SViresh Kumar 	sysfs_remove_file(cpufreq_global_kobject, attr);
8062361be23SViresh Kumar 	cpufreq_put_global_kobject();
8072361be23SViresh Kumar }
8082361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
8092361be23SViresh Kumar 
81019d6f7ecSDave Jones /* symlink affected CPUs */
811308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
81219d6f7ecSDave Jones {
81319d6f7ecSDave Jones 	unsigned int j;
81419d6f7ecSDave Jones 	int ret = 0;
81519d6f7ecSDave Jones 
81619d6f7ecSDave Jones 	for_each_cpu(j, policy->cpus) {
8178a25a2fdSKay Sievers 		struct device *cpu_dev;
81819d6f7ecSDave Jones 
819308b60e7SViresh Kumar 		if (j == policy->cpu)
82019d6f7ecSDave Jones 			continue;
82119d6f7ecSDave Jones 
822e8fdde10SViresh Kumar 		pr_debug("Adding link for CPU: %u\n", j);
8238a25a2fdSKay Sievers 		cpu_dev = get_cpu_device(j);
8248a25a2fdSKay Sievers 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
82519d6f7ecSDave Jones 					"cpufreq");
82671c3461eSRafael J. Wysocki 		if (ret)
82771c3461eSRafael J. Wysocki 			break;
82819d6f7ecSDave Jones 	}
82919d6f7ecSDave Jones 	return ret;
83019d6f7ecSDave Jones }
83119d6f7ecSDave Jones 
832308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
8338a25a2fdSKay Sievers 				     struct device *dev)
834909a694eSDave Jones {
835909a694eSDave Jones 	struct freq_attr **drv_attr;
836909a694eSDave Jones 	int ret = 0;
837909a694eSDave Jones 
838909a694eSDave Jones 	/* prepare interface data */
839909a694eSDave Jones 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
8408a25a2fdSKay Sievers 				   &dev->kobj, "cpufreq");
841909a694eSDave Jones 	if (ret)
842909a694eSDave Jones 		return ret;
843909a694eSDave Jones 
844909a694eSDave Jones 	/* set up files for this cpu device */
8451c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
846909a694eSDave Jones 	while ((drv_attr) && (*drv_attr)) {
847909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
848909a694eSDave Jones 		if (ret)
8491c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
850909a694eSDave Jones 		drv_attr++;
851909a694eSDave Jones 	}
8521c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
853909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
854909a694eSDave Jones 		if (ret)
8551c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
856909a694eSDave Jones 	}
8579c0ebcf7SViresh Kumar 	if (has_target()) {
858909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
859909a694eSDave Jones 		if (ret)
8601c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
861909a694eSDave Jones 	}
8621c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
863e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
864e2f74f35SThomas Renninger 		if (ret)
8651c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
866e2f74f35SThomas Renninger 	}
867909a694eSDave Jones 
868308b60e7SViresh Kumar 	ret = cpufreq_add_dev_symlink(policy);
869ecf7e461SDave Jones 	if (ret)
870ecf7e461SDave Jones 		goto err_out_kobj_put;
871ecf7e461SDave Jones 
872e18f1682SSrivatsa S. Bhat 	return ret;
873e18f1682SSrivatsa S. Bhat 
874e18f1682SSrivatsa S. Bhat err_out_kobj_put:
875e18f1682SSrivatsa S. Bhat 	kobject_put(&policy->kobj);
876e18f1682SSrivatsa S. Bhat 	wait_for_completion(&policy->kobj_unregister);
877e18f1682SSrivatsa S. Bhat 	return ret;
878e18f1682SSrivatsa S. Bhat }
879e18f1682SSrivatsa S. Bhat 
880e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy)
881e18f1682SSrivatsa S. Bhat {
882e18f1682SSrivatsa S. Bhat 	struct cpufreq_policy new_policy;
883e18f1682SSrivatsa S. Bhat 	int ret = 0;
884e18f1682SSrivatsa S. Bhat 
885d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
886a27a9ab7SJason Baron 
887a27a9ab7SJason Baron 	/* Use the default policy if its valid. */
888a27a9ab7SJason Baron 	if (cpufreq_driver->setpolicy)
889a27a9ab7SJason Baron 		cpufreq_parse_governor(policy->governor->name,
890a27a9ab7SJason Baron 					&new_policy.policy, NULL);
891a27a9ab7SJason Baron 
892037ce839SViresh Kumar 	/* assure that the starting sequence is run in cpufreq_set_policy */
893ecf7e461SDave Jones 	policy->governor = NULL;
894ecf7e461SDave Jones 
895ecf7e461SDave Jones 	/* set default policy */
896037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
897ecf7e461SDave Jones 	if (ret) {
8982d06d8c4SDominik Brodowski 		pr_debug("setting policy failed\n");
8991c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
9001c3d85ddSRafael J. Wysocki 			cpufreq_driver->exit(policy);
901ecf7e461SDave Jones 	}
902909a694eSDave Jones }
903909a694eSDave Jones 
904fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
905d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
90642f921a6SViresh Kumar 				  unsigned int cpu, struct device *dev)
907fcf80582SViresh Kumar {
9089c0ebcf7SViresh Kumar 	int ret = 0;
909fcf80582SViresh Kumar 	unsigned long flags;
910fcf80582SViresh Kumar 
9119c0ebcf7SViresh Kumar 	if (has_target()) {
9123de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
9133de9bdebSViresh Kumar 		if (ret) {
9143de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
9153de9bdebSViresh Kumar 			return ret;
9163de9bdebSViresh Kumar 		}
9173de9bdebSViresh Kumar 	}
918fcf80582SViresh Kumar 
919ad7722daSviresh kumar 	down_write(&policy->rwsem);
9202eaa3e2dSViresh Kumar 
9210d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
9222eaa3e2dSViresh Kumar 
923fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
924fcf80582SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = policy;
9250d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
926fcf80582SViresh Kumar 
927ad7722daSviresh kumar 	up_write(&policy->rwsem);
9282eaa3e2dSViresh Kumar 
9299c0ebcf7SViresh Kumar 	if (has_target()) {
9303de9bdebSViresh Kumar 		if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
9313de9bdebSViresh Kumar 			(ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
9323de9bdebSViresh Kumar 			pr_err("%s: Failed to start governor\n", __func__);
9333de9bdebSViresh Kumar 			return ret;
9343de9bdebSViresh Kumar 		}
935820c6ca2SViresh Kumar 	}
936fcf80582SViresh Kumar 
93742f921a6SViresh Kumar 	return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
938fcf80582SViresh Kumar }
939fcf80582SViresh Kumar #endif
9401da177e4SLinus Torvalds 
9418414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
9428414809cSSrivatsa S. Bhat {
9438414809cSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
9448414809cSSrivatsa S. Bhat 	unsigned long flags;
9458414809cSSrivatsa S. Bhat 
94644871c9cSLan Tianyu 	read_lock_irqsave(&cpufreq_driver_lock, flags);
9478414809cSSrivatsa S. Bhat 
9488414809cSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
9498414809cSSrivatsa S. Bhat 
95044871c9cSLan Tianyu 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
9518414809cSSrivatsa S. Bhat 
9528414809cSSrivatsa S. Bhat 	return policy;
9538414809cSSrivatsa S. Bhat }
9548414809cSSrivatsa S. Bhat 
955e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void)
956e9698cc5SSrivatsa S. Bhat {
957e9698cc5SSrivatsa S. Bhat 	struct cpufreq_policy *policy;
958e9698cc5SSrivatsa S. Bhat 
959e9698cc5SSrivatsa S. Bhat 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
960e9698cc5SSrivatsa S. Bhat 	if (!policy)
961e9698cc5SSrivatsa S. Bhat 		return NULL;
962e9698cc5SSrivatsa S. Bhat 
963e9698cc5SSrivatsa S. Bhat 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
964e9698cc5SSrivatsa S. Bhat 		goto err_free_policy;
965e9698cc5SSrivatsa S. Bhat 
966e9698cc5SSrivatsa S. Bhat 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
967e9698cc5SSrivatsa S. Bhat 		goto err_free_cpumask;
968e9698cc5SSrivatsa S. Bhat 
969c88a1f8bSLukasz Majewski 	INIT_LIST_HEAD(&policy->policy_list);
970ad7722daSviresh kumar 	init_rwsem(&policy->rwsem);
971ad7722daSviresh kumar 
972e9698cc5SSrivatsa S. Bhat 	return policy;
973e9698cc5SSrivatsa S. Bhat 
974e9698cc5SSrivatsa S. Bhat err_free_cpumask:
975e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
976e9698cc5SSrivatsa S. Bhat err_free_policy:
977e9698cc5SSrivatsa S. Bhat 	kfree(policy);
978e9698cc5SSrivatsa S. Bhat 
979e9698cc5SSrivatsa S. Bhat 	return NULL;
980e9698cc5SSrivatsa S. Bhat }
981e9698cc5SSrivatsa S. Bhat 
98242f921a6SViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
98342f921a6SViresh Kumar {
98442f921a6SViresh Kumar 	struct kobject *kobj;
98542f921a6SViresh Kumar 	struct completion *cmp;
98642f921a6SViresh Kumar 
987fcd7af91SViresh Kumar 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
988fcd7af91SViresh Kumar 			CPUFREQ_REMOVE_POLICY, policy);
989fcd7af91SViresh Kumar 
99042f921a6SViresh Kumar 	down_read(&policy->rwsem);
99142f921a6SViresh Kumar 	kobj = &policy->kobj;
99242f921a6SViresh Kumar 	cmp = &policy->kobj_unregister;
99342f921a6SViresh Kumar 	up_read(&policy->rwsem);
99442f921a6SViresh Kumar 	kobject_put(kobj);
99542f921a6SViresh Kumar 
99642f921a6SViresh Kumar 	/*
99742f921a6SViresh Kumar 	 * We need to make sure that the underlying kobj is
99842f921a6SViresh Kumar 	 * actually not referenced anymore by anybody before we
99942f921a6SViresh Kumar 	 * proceed with unloading.
100042f921a6SViresh Kumar 	 */
100142f921a6SViresh Kumar 	pr_debug("waiting for dropping of refcount\n");
100242f921a6SViresh Kumar 	wait_for_completion(cmp);
100342f921a6SViresh Kumar 	pr_debug("wait complete\n");
100442f921a6SViresh Kumar }
100542f921a6SViresh Kumar 
1006e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy)
1007e9698cc5SSrivatsa S. Bhat {
1008e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
1009e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1010e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1011e9698cc5SSrivatsa S. Bhat }
1012e9698cc5SSrivatsa S. Bhat 
10130d66b91eSSrivatsa S. Bhat static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
10140d66b91eSSrivatsa S. Bhat {
101599ec899eSSrivatsa S. Bhat 	if (WARN_ON(cpu == policy->cpu))
1016cb38ed5cSSrivatsa S. Bhat 		return;
1017cb38ed5cSSrivatsa S. Bhat 
1018ad7722daSviresh kumar 	down_write(&policy->rwsem);
10198efd5765SViresh Kumar 
10200d66b91eSSrivatsa S. Bhat 	policy->last_cpu = policy->cpu;
10210d66b91eSSrivatsa S. Bhat 	policy->cpu = cpu;
10220d66b91eSSrivatsa S. Bhat 
1023ad7722daSviresh kumar 	up_write(&policy->rwsem);
10248efd5765SViresh Kumar 
10250d66b91eSSrivatsa S. Bhat 	cpufreq_frequency_table_update_policy_cpu(policy);
10260d66b91eSSrivatsa S. Bhat 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
10270d66b91eSSrivatsa S. Bhat 			CPUFREQ_UPDATE_POLICY_CPU, policy);
10280d66b91eSSrivatsa S. Bhat }
10290d66b91eSSrivatsa S. Bhat 
1030a82fab29SSrivatsa S. Bhat static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1031a82fab29SSrivatsa S. Bhat 			     bool frozen)
10321da177e4SLinus Torvalds {
1033fcf80582SViresh Kumar 	unsigned int j, cpu = dev->id;
103465922465SViresh Kumar 	int ret = -ENOMEM;
10351da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
10361da177e4SLinus Torvalds 	unsigned long flags;
103790e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
10381b274294SViresh Kumar 	struct cpufreq_policy *tpolicy;
1039fcf80582SViresh Kumar 	struct cpufreq_governor *gov;
104090e41bacSPrarit Bhargava #endif
10411da177e4SLinus Torvalds 
1042c32b6b8eSAshok Raj 	if (cpu_is_offline(cpu))
1043c32b6b8eSAshok Raj 		return 0;
1044c32b6b8eSAshok Raj 
10452d06d8c4SDominik Brodowski 	pr_debug("adding CPU %u\n", cpu);
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds #ifdef CONFIG_SMP
10481da177e4SLinus Torvalds 	/* check whether a different CPU already registered this
10491da177e4SLinus Torvalds 	 * CPU because it is in the same boat. */
10501da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(cpu);
10511da177e4SLinus Torvalds 	if (unlikely(policy)) {
10528ff69732SDave Jones 		cpufreq_cpu_put(policy);
10531da177e4SLinus Torvalds 		return 0;
10541da177e4SLinus Torvalds 	}
10555025d628SLi Zhong #endif
1056fcf80582SViresh Kumar 
10576eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
10586eed9404SViresh Kumar 		return 0;
10596eed9404SViresh Kumar 
1060fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
1061fcf80582SViresh Kumar 	/* Check if this cpu was hot-unplugged earlier and has siblings */
10620d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
10631b274294SViresh Kumar 	list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
10641b274294SViresh Kumar 		if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
10650d1857a1SNathan Zimmer 			read_unlock_irqrestore(&cpufreq_driver_lock, flags);
106642f921a6SViresh Kumar 			ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
10676eed9404SViresh Kumar 			up_read(&cpufreq_rwsem);
10686eed9404SViresh Kumar 			return ret;
1069fcf80582SViresh Kumar 		}
10702eaa3e2dSViresh Kumar 	}
10710d1857a1SNathan Zimmer 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1072fcf80582SViresh Kumar #endif
10731da177e4SLinus Torvalds 
107472368d12SRafael J. Wysocki 	/*
107572368d12SRafael J. Wysocki 	 * Restore the saved policy when doing light-weight init and fall back
107672368d12SRafael J. Wysocki 	 * to the full init if that fails.
107772368d12SRafael J. Wysocki 	 */
107872368d12SRafael J. Wysocki 	policy = frozen ? cpufreq_policy_restore(cpu) : NULL;
107972368d12SRafael J. Wysocki 	if (!policy) {
108072368d12SRafael J. Wysocki 		frozen = false;
1081e9698cc5SSrivatsa S. Bhat 		policy = cpufreq_policy_alloc();
1082059019a3SDave Jones 		if (!policy)
10831da177e4SLinus Torvalds 			goto nomem_out;
108472368d12SRafael J. Wysocki 	}
10850d66b91eSSrivatsa S. Bhat 
10860d66b91eSSrivatsa S. Bhat 	/*
10870d66b91eSSrivatsa S. Bhat 	 * In the resume path, since we restore a saved policy, the assignment
10880d66b91eSSrivatsa S. Bhat 	 * to policy->cpu is like an update of the existing policy, rather than
10890d66b91eSSrivatsa S. Bhat 	 * the creation of a brand new one. So we need to perform this update
10900d66b91eSSrivatsa S. Bhat 	 * by invoking update_policy_cpu().
10910d66b91eSSrivatsa S. Bhat 	 */
10920d66b91eSSrivatsa S. Bhat 	if (frozen && cpu != policy->cpu)
10930d66b91eSSrivatsa S. Bhat 		update_policy_cpu(policy, cpu);
10940d66b91eSSrivatsa S. Bhat 	else
10951da177e4SLinus Torvalds 		policy->cpu = cpu;
10960d66b91eSSrivatsa S. Bhat 
109765922465SViresh Kumar 	policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1098835481d9SRusty Russell 	cpumask_copy(policy->cpus, cpumask_of(cpu));
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 	init_completion(&policy->kobj_unregister);
110165f27f38SDavid Howells 	INIT_WORK(&policy->update, handle_update);
11021da177e4SLinus Torvalds 
11031da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
11041da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
11051da177e4SLinus Torvalds 	 */
11061c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->init(policy);
11071da177e4SLinus Torvalds 	if (ret) {
11082d06d8c4SDominik Brodowski 		pr_debug("initialization failed\n");
11092eaa3e2dSViresh Kumar 		goto err_set_policy_cpu;
11101da177e4SLinus Torvalds 	}
1111643ae6e8SViresh Kumar 
11125a7e56a5SViresh Kumar 	/* related cpus should atleast have policy->cpus */
11135a7e56a5SViresh Kumar 	cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
11145a7e56a5SViresh Kumar 
11155a7e56a5SViresh Kumar 	/*
11165a7e56a5SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
11175a7e56a5SViresh Kumar 	 * managing offline cpus here.
11185a7e56a5SViresh Kumar 	 */
11195a7e56a5SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
11205a7e56a5SViresh Kumar 
11215a7e56a5SViresh Kumar 	if (!frozen) {
11225a7e56a5SViresh Kumar 		policy->user_policy.min = policy->min;
11235a7e56a5SViresh Kumar 		policy->user_policy.max = policy->max;
11245a7e56a5SViresh Kumar 	}
11255a7e56a5SViresh Kumar 
1126*4e97b631SViresh Kumar 	down_write(&policy->rwsem);
1127652ed95dSViresh Kumar 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1128652ed95dSViresh Kumar 	for_each_cpu(j, policy->cpus)
1129652ed95dSViresh Kumar 		per_cpu(cpufreq_cpu_data, j) = policy;
1130652ed95dSViresh Kumar 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1131652ed95dSViresh Kumar 
1132da60ce9fSViresh Kumar 	if (cpufreq_driver->get) {
1133da60ce9fSViresh Kumar 		policy->cur = cpufreq_driver->get(policy->cpu);
1134da60ce9fSViresh Kumar 		if (!policy->cur) {
1135da60ce9fSViresh Kumar 			pr_err("%s: ->get() failed\n", __func__);
1136da60ce9fSViresh Kumar 			goto err_get_freq;
1137da60ce9fSViresh Kumar 		}
1138da60ce9fSViresh Kumar 	}
1139da60ce9fSViresh Kumar 
1140d3916691SViresh Kumar 	/*
1141d3916691SViresh Kumar 	 * Sometimes boot loaders set CPU frequency to a value outside of
1142d3916691SViresh Kumar 	 * frequency table present with cpufreq core. In such cases CPU might be
1143d3916691SViresh Kumar 	 * unstable if it has to run on that frequency for long duration of time
1144d3916691SViresh Kumar 	 * and so its better to set it to a frequency which is specified in
1145d3916691SViresh Kumar 	 * freq-table. This also makes cpufreq stats inconsistent as
1146d3916691SViresh Kumar 	 * cpufreq-stats would fail to register because current frequency of CPU
1147d3916691SViresh Kumar 	 * isn't found in freq-table.
1148d3916691SViresh Kumar 	 *
1149d3916691SViresh Kumar 	 * Because we don't want this change to effect boot process badly, we go
1150d3916691SViresh Kumar 	 * for the next freq which is >= policy->cur ('cur' must be set by now,
1151d3916691SViresh Kumar 	 * otherwise we will end up setting freq to lowest of the table as 'cur'
1152d3916691SViresh Kumar 	 * is initialized to zero).
1153d3916691SViresh Kumar 	 *
1154d3916691SViresh Kumar 	 * We are passing target-freq as "policy->cur - 1" otherwise
1155d3916691SViresh Kumar 	 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1156d3916691SViresh Kumar 	 * equal to target-freq.
1157d3916691SViresh Kumar 	 */
1158d3916691SViresh Kumar 	if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1159d3916691SViresh Kumar 	    && has_target()) {
1160d3916691SViresh Kumar 		/* Are we running at unknown frequency ? */
1161d3916691SViresh Kumar 		ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1162d3916691SViresh Kumar 		if (ret == -EINVAL) {
1163d3916691SViresh Kumar 			/* Warn user and fix it */
1164d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1165d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1166d3916691SViresh Kumar 			ret = __cpufreq_driver_target(policy, policy->cur - 1,
1167d3916691SViresh Kumar 				CPUFREQ_RELATION_L);
1168d3916691SViresh Kumar 
1169d3916691SViresh Kumar 			/*
1170d3916691SViresh Kumar 			 * Reaching here after boot in a few seconds may not
1171d3916691SViresh Kumar 			 * mean that system will remain stable at "unknown"
1172d3916691SViresh Kumar 			 * frequency for longer duration. Hence, a BUG_ON().
1173d3916691SViresh Kumar 			 */
1174d3916691SViresh Kumar 			BUG_ON(ret);
1175d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1176d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1177d3916691SViresh Kumar 		}
1178d3916691SViresh Kumar 	}
1179d3916691SViresh Kumar 
1180a1531acdSThomas Renninger 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1181a1531acdSThomas Renninger 				     CPUFREQ_START, policy);
1182a1531acdSThomas Renninger 
1183fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
1184fcf80582SViresh Kumar 	gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1185fcf80582SViresh Kumar 	if (gov) {
1186fcf80582SViresh Kumar 		policy->governor = gov;
1187fcf80582SViresh Kumar 		pr_debug("Restoring governor %s for cpu %d\n",
1188fcf80582SViresh Kumar 		       policy->governor->name, cpu);
11894bfa042cSThomas Renninger 	}
1190fcf80582SViresh Kumar #endif
11911da177e4SLinus Torvalds 
1192a82fab29SSrivatsa S. Bhat 	if (!frozen) {
1193308b60e7SViresh Kumar 		ret = cpufreq_add_dev_interface(policy, dev);
119419d6f7ecSDave Jones 		if (ret)
11950142f9dcSAhmed S. Darwish 			goto err_out_unregister;
1196fcd7af91SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1197fcd7af91SViresh Kumar 				CPUFREQ_CREATE_POLICY, policy);
11989515f4d6SViresh Kumar 	}
1199c88a1f8bSLukasz Majewski 
1200c88a1f8bSLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1201c88a1f8bSLukasz Majewski 	list_add(&policy->policy_list, &cpufreq_policy_list);
1202c88a1f8bSLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
12038ff69732SDave Jones 
1204e18f1682SSrivatsa S. Bhat 	cpufreq_init_policy(policy);
1205e18f1682SSrivatsa S. Bhat 
120608fd8c1cSViresh Kumar 	if (!frozen) {
120708fd8c1cSViresh Kumar 		policy->user_policy.policy = policy->policy;
120808fd8c1cSViresh Kumar 		policy->user_policy.governor = policy->governor;
120908fd8c1cSViresh Kumar 	}
1210*4e97b631SViresh Kumar 	up_write(&policy->rwsem);
121108fd8c1cSViresh Kumar 
1212038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
12136eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
12146eed9404SViresh Kumar 
12152d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
12161da177e4SLinus Torvalds 
12171da177e4SLinus Torvalds 	return 0;
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds err_out_unregister:
1220652ed95dSViresh Kumar err_get_freq:
12210d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1222474deff7SViresh Kumar 	for_each_cpu(j, policy->cpus)
12237a6aedfaSMike Travis 		per_cpu(cpufreq_cpu_data, j) = NULL;
12240d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
12251da177e4SLinus Torvalds 
1226da60ce9fSViresh Kumar 	if (cpufreq_driver->exit)
1227da60ce9fSViresh Kumar 		cpufreq_driver->exit(policy);
12282eaa3e2dSViresh Kumar err_set_policy_cpu:
122972368d12SRafael J. Wysocki 	if (frozen) {
123072368d12SRafael J. Wysocki 		/* Do not leave stale fallback data behind. */
123172368d12SRafael J. Wysocki 		per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
123242f921a6SViresh Kumar 		cpufreq_policy_put_kobj(policy);
123372368d12SRafael J. Wysocki 	}
1234e9698cc5SSrivatsa S. Bhat 	cpufreq_policy_free(policy);
123542f921a6SViresh Kumar 
12361da177e4SLinus Torvalds nomem_out:
12376eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
12386eed9404SViresh Kumar 
12391da177e4SLinus Torvalds 	return ret;
12401da177e4SLinus Torvalds }
12411da177e4SLinus Torvalds 
1242a82fab29SSrivatsa S. Bhat /**
1243a82fab29SSrivatsa S. Bhat  * cpufreq_add_dev - add a CPU device
1244a82fab29SSrivatsa S. Bhat  *
1245a82fab29SSrivatsa S. Bhat  * Adds the cpufreq interface for a CPU device.
1246a82fab29SSrivatsa S. Bhat  *
1247a82fab29SSrivatsa S. Bhat  * The Oracle says: try running cpufreq registration/unregistration concurrently
1248a82fab29SSrivatsa S. Bhat  * with with cpu hotplugging and all hell will break loose. Tried to clean this
1249a82fab29SSrivatsa S. Bhat  * mess up, but more thorough testing is needed. - Mathieu
1250a82fab29SSrivatsa S. Bhat  */
1251a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1252a82fab29SSrivatsa S. Bhat {
1253a82fab29SSrivatsa S. Bhat 	return __cpufreq_add_dev(dev, sif, false);
1254a82fab29SSrivatsa S. Bhat }
1255a82fab29SSrivatsa S. Bhat 
12563a3e9e06SViresh Kumar static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
125742f921a6SViresh Kumar 					   unsigned int old_cpu)
1258f9ba680dSSrivatsa S. Bhat {
1259f9ba680dSSrivatsa S. Bhat 	struct device *cpu_dev;
1260f9ba680dSSrivatsa S. Bhat 	int ret;
1261f9ba680dSSrivatsa S. Bhat 
1262f9ba680dSSrivatsa S. Bhat 	/* first sibling now owns the new sysfs dir */
12639c8f1ee4SViresh Kumar 	cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
1264a82fab29SSrivatsa S. Bhat 
1265f9ba680dSSrivatsa S. Bhat 	sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
12663a3e9e06SViresh Kumar 	ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1267f9ba680dSSrivatsa S. Bhat 	if (ret) {
1268f9ba680dSSrivatsa S. Bhat 		pr_err("%s: Failed to move kobj: %d", __func__, ret);
1269f9ba680dSSrivatsa S. Bhat 
1270ad7722daSviresh kumar 		down_write(&policy->rwsem);
12713a3e9e06SViresh Kumar 		cpumask_set_cpu(old_cpu, policy->cpus);
1272ad7722daSviresh kumar 		up_write(&policy->rwsem);
1273f9ba680dSSrivatsa S. Bhat 
12743a3e9e06SViresh Kumar 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1275f9ba680dSSrivatsa S. Bhat 					"cpufreq");
1276f9ba680dSSrivatsa S. Bhat 
1277f9ba680dSSrivatsa S. Bhat 		return -EINVAL;
1278f9ba680dSSrivatsa S. Bhat 	}
1279f9ba680dSSrivatsa S. Bhat 
1280f9ba680dSSrivatsa S. Bhat 	return cpu_dev->id;
1281f9ba680dSSrivatsa S. Bhat }
1282f9ba680dSSrivatsa S. Bhat 
1283cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_prepare(struct device *dev,
1284cedb70afSSrivatsa S. Bhat 					struct subsys_interface *sif,
1285cedb70afSSrivatsa S. Bhat 					bool frozen)
12861da177e4SLinus Torvalds {
1287f9ba680dSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
12883de9bdebSViresh Kumar 	int new_cpu, ret;
12891da177e4SLinus Torvalds 	unsigned long flags;
12903a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
12911da177e4SLinus Torvalds 
1292b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
12931da177e4SLinus Torvalds 
12940d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
12951da177e4SLinus Torvalds 
12963a3e9e06SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
12971da177e4SLinus Torvalds 
12988414809cSSrivatsa S. Bhat 	/* Save the policy somewhere when doing a light-weight tear-down */
12998414809cSSrivatsa S. Bhat 	if (frozen)
13003a3e9e06SViresh Kumar 		per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
13018414809cSSrivatsa S. Bhat 
13020d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
13031da177e4SLinus Torvalds 
13043a3e9e06SViresh Kumar 	if (!policy) {
1305b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
13061da177e4SLinus Torvalds 		return -EINVAL;
13071da177e4SLinus Torvalds 	}
13081da177e4SLinus Torvalds 
13099c0ebcf7SViresh Kumar 	if (has_target()) {
13103de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
13113de9bdebSViresh Kumar 		if (ret) {
13123de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
13133de9bdebSViresh Kumar 			return ret;
13143de9bdebSViresh Kumar 		}
13153de9bdebSViresh Kumar 	}
13165a01f2e8SVenkatesh Pallipadi 
13171da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
13181c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->setpolicy)
1319fa69e33fSDirk Brandewie 		strncpy(per_cpu(cpufreq_cpu_governor, cpu),
13203a3e9e06SViresh Kumar 			policy->governor->name, CPUFREQ_NAME_LEN);
13211da177e4SLinus Torvalds #endif
13221da177e4SLinus Torvalds 
1323ad7722daSviresh kumar 	down_read(&policy->rwsem);
13243a3e9e06SViresh Kumar 	cpus = cpumask_weight(policy->cpus);
1325ad7722daSviresh kumar 	up_read(&policy->rwsem);
13261da177e4SLinus Torvalds 
132761173f25SSrivatsa S. Bhat 	if (cpu != policy->cpu) {
132873bf0fc2SViresh Kumar 		sysfs_remove_link(&dev->kobj, "cpufreq");
132973bf0fc2SViresh Kumar 	} else if (cpus > 1) {
133042f921a6SViresh Kumar 		new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
1331f9ba680dSSrivatsa S. Bhat 		if (new_cpu >= 0) {
13323a3e9e06SViresh Kumar 			update_policy_cpu(policy, new_cpu);
1333a82fab29SSrivatsa S. Bhat 
1334a82fab29SSrivatsa S. Bhat 			if (!frozen) {
133575949c9aSViresh Kumar 				pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
133675949c9aSViresh Kumar 						__func__, new_cpu, cpu);
13371da177e4SLinus Torvalds 			}
13381da177e4SLinus Torvalds 		}
1339a82fab29SSrivatsa S. Bhat 	}
1340b8eed8afSViresh Kumar 
1341cedb70afSSrivatsa S. Bhat 	return 0;
1342cedb70afSSrivatsa S. Bhat }
1343cedb70afSSrivatsa S. Bhat 
1344cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_finish(struct device *dev,
1345cedb70afSSrivatsa S. Bhat 				       struct subsys_interface *sif,
1346cedb70afSSrivatsa S. Bhat 				       bool frozen)
1347cedb70afSSrivatsa S. Bhat {
1348cedb70afSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
1349cedb70afSSrivatsa S. Bhat 	int ret;
1350cedb70afSSrivatsa S. Bhat 	unsigned long flags;
1351cedb70afSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
1352cedb70afSSrivatsa S. Bhat 
1353cedb70afSSrivatsa S. Bhat 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1354cedb70afSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data, cpu);
1355cedb70afSSrivatsa S. Bhat 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1356cedb70afSSrivatsa S. Bhat 
1357cedb70afSSrivatsa S. Bhat 	if (!policy) {
1358cedb70afSSrivatsa S. Bhat 		pr_debug("%s: No cpu_data found\n", __func__);
1359cedb70afSSrivatsa S. Bhat 		return -EINVAL;
1360cedb70afSSrivatsa S. Bhat 	}
1361cedb70afSSrivatsa S. Bhat 
1362ad7722daSviresh kumar 	down_write(&policy->rwsem);
1363cedb70afSSrivatsa S. Bhat 	cpus = cpumask_weight(policy->cpus);
13649c8f1ee4SViresh Kumar 
13659c8f1ee4SViresh Kumar 	if (cpus > 1)
13669c8f1ee4SViresh Kumar 		cpumask_clear_cpu(cpu, policy->cpus);
1367ad7722daSviresh kumar 	up_write(&policy->rwsem);
1368cedb70afSSrivatsa S. Bhat 
1369b8eed8afSViresh Kumar 	/* If cpu is last user of policy, free policy */
1370b8eed8afSViresh Kumar 	if (cpus == 1) {
13719c0ebcf7SViresh Kumar 		if (has_target()) {
13723de9bdebSViresh Kumar 			ret = __cpufreq_governor(policy,
13733de9bdebSViresh Kumar 					CPUFREQ_GOV_POLICY_EXIT);
13743de9bdebSViresh Kumar 			if (ret) {
13753de9bdebSViresh Kumar 				pr_err("%s: Failed to exit governor\n",
13763de9bdebSViresh Kumar 						__func__);
13773de9bdebSViresh Kumar 				return ret;
13783de9bdebSViresh Kumar 			}
13793de9bdebSViresh Kumar 		}
13802a998599SRafael J. Wysocki 
138142f921a6SViresh Kumar 		if (!frozen)
138242f921a6SViresh Kumar 			cpufreq_policy_put_kobj(policy);
13831da177e4SLinus Torvalds 
13848414809cSSrivatsa S. Bhat 		/*
13858414809cSSrivatsa S. Bhat 		 * Perform the ->exit() even during light-weight tear-down,
13868414809cSSrivatsa S. Bhat 		 * since this is a core component, and is essential for the
13878414809cSSrivatsa S. Bhat 		 * subsequent light-weight ->init() to succeed.
13888414809cSSrivatsa S. Bhat 		 */
13891c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
13903a3e9e06SViresh Kumar 			cpufreq_driver->exit(policy);
139127ecddc2SJacob Shin 
13929515f4d6SViresh Kumar 		/* Remove policy from list of active policies */
13939515f4d6SViresh Kumar 		write_lock_irqsave(&cpufreq_driver_lock, flags);
13949515f4d6SViresh Kumar 		list_del(&policy->policy_list);
13959515f4d6SViresh Kumar 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
13969515f4d6SViresh Kumar 
13978414809cSSrivatsa S. Bhat 		if (!frozen)
13983a3e9e06SViresh Kumar 			cpufreq_policy_free(policy);
13992a998599SRafael J. Wysocki 	} else {
14009c0ebcf7SViresh Kumar 		if (has_target()) {
14013de9bdebSViresh Kumar 			if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
14023de9bdebSViresh Kumar 					(ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
14033de9bdebSViresh Kumar 				pr_err("%s: Failed to start governor\n",
14043de9bdebSViresh Kumar 						__func__);
14053de9bdebSViresh Kumar 				return ret;
14063de9bdebSViresh Kumar 			}
1407b8eed8afSViresh Kumar 		}
14082a998599SRafael J. Wysocki 	}
14091da177e4SLinus Torvalds 
1410474deff7SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = NULL;
14111da177e4SLinus Torvalds 	return 0;
14121da177e4SLinus Torvalds }
14131da177e4SLinus Torvalds 
1414cedb70afSSrivatsa S. Bhat /**
141527a862e9SViresh Kumar  * cpufreq_remove_dev - remove a CPU device
1416cedb70afSSrivatsa S. Bhat  *
1417cedb70afSSrivatsa S. Bhat  * Removes the cpufreq interface for a CPU device.
1418cedb70afSSrivatsa S. Bhat  */
14198a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
14205a01f2e8SVenkatesh Pallipadi {
14218a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
142227a862e9SViresh Kumar 	int ret;
1423ec28297aSVenki Pallipadi 
1424ec28297aSVenki Pallipadi 	if (cpu_is_offline(cpu))
1425ec28297aSVenki Pallipadi 		return 0;
1426ec28297aSVenki Pallipadi 
142727a862e9SViresh Kumar 	ret = __cpufreq_remove_dev_prepare(dev, sif, false);
142827a862e9SViresh Kumar 
142927a862e9SViresh Kumar 	if (!ret)
143027a862e9SViresh Kumar 		ret = __cpufreq_remove_dev_finish(dev, sif, false);
143127a862e9SViresh Kumar 
143227a862e9SViresh Kumar 	return ret;
14335a01f2e8SVenkatesh Pallipadi }
14345a01f2e8SVenkatesh Pallipadi 
143565f27f38SDavid Howells static void handle_update(struct work_struct *work)
14361da177e4SLinus Torvalds {
143765f27f38SDavid Howells 	struct cpufreq_policy *policy =
143865f27f38SDavid Howells 		container_of(work, struct cpufreq_policy, update);
143965f27f38SDavid Howells 	unsigned int cpu = policy->cpu;
14402d06d8c4SDominik Brodowski 	pr_debug("handle_update for cpu %u called\n", cpu);
14411da177e4SLinus Torvalds 	cpufreq_update_policy(cpu);
14421da177e4SLinus Torvalds }
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds /**
1445bb176f7dSViresh Kumar  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1446bb176f7dSViresh Kumar  *	in deep trouble.
14471da177e4SLinus Torvalds  *	@cpu: cpu number
14481da177e4SLinus Torvalds  *	@old_freq: CPU frequency the kernel thinks the CPU runs at
14491da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
14501da177e4SLinus Torvalds  *
145129464f28SDave Jones  *	We adjust to current frequency first, and need to clean up later.
145229464f28SDave Jones  *	So either call to cpufreq_update_policy() or schedule handle_update()).
14531da177e4SLinus Torvalds  */
1454e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1455e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
14561da177e4SLinus Torvalds {
1457b43a7ffbSViresh Kumar 	struct cpufreq_policy *policy;
14581da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1459b43a7ffbSViresh Kumar 	unsigned long flags;
1460b43a7ffbSViresh Kumar 
14612d06d8c4SDominik Brodowski 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
14621da177e4SLinus Torvalds 	       "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
14631da177e4SLinus Torvalds 
14641da177e4SLinus Torvalds 	freqs.old = old_freq;
14651da177e4SLinus Torvalds 	freqs.new = new_freq;
1466b43a7ffbSViresh Kumar 
1467b43a7ffbSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1468b43a7ffbSViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
1469b43a7ffbSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1470b43a7ffbSViresh Kumar 
1471b43a7ffbSViresh Kumar 	cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1472b43a7ffbSViresh Kumar 	cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds /**
14764ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
147795235ca2SVenkatesh Pallipadi  * @cpu: CPU number
147895235ca2SVenkatesh Pallipadi  *
147995235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
148095235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
148195235ca2SVenkatesh Pallipadi  */
148295235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
148395235ca2SVenkatesh Pallipadi {
14849e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1485e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
148695235ca2SVenkatesh Pallipadi 
14871c3d85ddSRafael J. Wysocki 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
14881c3d85ddSRafael J. Wysocki 		return cpufreq_driver->get(cpu);
14899e21ba8bSDirk Brandewie 
14909e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
149195235ca2SVenkatesh Pallipadi 	if (policy) {
1492e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
149395235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
149495235ca2SVenkatesh Pallipadi 	}
149595235ca2SVenkatesh Pallipadi 
14964d34a67dSDave Jones 	return ret_freq;
149795235ca2SVenkatesh Pallipadi }
149895235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
149995235ca2SVenkatesh Pallipadi 
15003d737108SJesse Barnes /**
15013d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
15023d737108SJesse Barnes  * @cpu: CPU number
15033d737108SJesse Barnes  *
15043d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
15053d737108SJesse Barnes  */
15063d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
15073d737108SJesse Barnes {
15083d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
15093d737108SJesse Barnes 	unsigned int ret_freq = 0;
15103d737108SJesse Barnes 
15113d737108SJesse Barnes 	if (policy) {
15123d737108SJesse Barnes 		ret_freq = policy->max;
15133d737108SJesse Barnes 		cpufreq_cpu_put(policy);
15143d737108SJesse Barnes 	}
15153d737108SJesse Barnes 
15163d737108SJesse Barnes 	return ret_freq;
15173d737108SJesse Barnes }
15183d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
15193d737108SJesse Barnes 
15205a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu)
15211da177e4SLinus Torvalds {
15227a6aedfaSMike Travis 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1523e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
15241da177e4SLinus Torvalds 
15251c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->get)
15264d34a67dSDave Jones 		return ret_freq;
15271da177e4SLinus Torvalds 
15281c3d85ddSRafael J. Wysocki 	ret_freq = cpufreq_driver->get(cpu);
15291da177e4SLinus Torvalds 
1530e08f5f5bSGautham R Shenoy 	if (ret_freq && policy->cur &&
15311c3d85ddSRafael J. Wysocki 		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1532e08f5f5bSGautham R Shenoy 		/* verify no discrepancy between actual and
1533e08f5f5bSGautham R Shenoy 					saved value exists */
1534e08f5f5bSGautham R Shenoy 		if (unlikely(ret_freq != policy->cur)) {
1535e08f5f5bSGautham R Shenoy 			cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
15361da177e4SLinus Torvalds 			schedule_work(&policy->update);
15371da177e4SLinus Torvalds 		}
15381da177e4SLinus Torvalds 	}
15391da177e4SLinus Torvalds 
15404d34a67dSDave Jones 	return ret_freq;
15415a01f2e8SVenkatesh Pallipadi }
15421da177e4SLinus Torvalds 
15435a01f2e8SVenkatesh Pallipadi /**
15445a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
15455a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
15465a01f2e8SVenkatesh Pallipadi  *
15475a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
15485a01f2e8SVenkatesh Pallipadi  */
15495a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
15505a01f2e8SVenkatesh Pallipadi {
1551999976e0SAaron Plattner 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
15525a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
15535a01f2e8SVenkatesh Pallipadi 
1554999976e0SAaron Plattner 	if (policy) {
1555ad7722daSviresh kumar 		down_read(&policy->rwsem);
15565a01f2e8SVenkatesh Pallipadi 		ret_freq = __cpufreq_get(cpu);
1557ad7722daSviresh kumar 		up_read(&policy->rwsem);
1558999976e0SAaron Plattner 
1559999976e0SAaron Plattner 		cpufreq_cpu_put(policy);
1560999976e0SAaron Plattner 	}
15616eed9404SViresh Kumar 
15624d34a67dSDave Jones 	return ret_freq;
15631da177e4SLinus Torvalds }
15641da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
15651da177e4SLinus Torvalds 
15668a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
15678a25a2fdSKay Sievers 	.name		= "cpufreq",
15688a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
15698a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
15708a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1571e00e56dfSRafael J. Wysocki };
1572e00e56dfSRafael J. Wysocki 
15731da177e4SLinus Torvalds /**
1574e00e56dfSRafael J. Wysocki  * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1575e00e56dfSRafael J. Wysocki  *
1576e00e56dfSRafael J. Wysocki  * This function is only executed for the boot processor.  The other CPUs
1577e00e56dfSRafael J. Wysocki  * have been put offline by means of CPU hotplug.
157842d4dc3fSBenjamin Herrenschmidt  */
1579e00e56dfSRafael J. Wysocki static int cpufreq_bp_suspend(void)
158042d4dc3fSBenjamin Herrenschmidt {
1581e08f5f5bSGautham R Shenoy 	int ret = 0;
15824bc5d341SDave Jones 
1583e00e56dfSRafael J. Wysocki 	int cpu = smp_processor_id();
15843a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
158542d4dc3fSBenjamin Herrenschmidt 
15862d06d8c4SDominik Brodowski 	pr_debug("suspending cpu %u\n", cpu);
158742d4dc3fSBenjamin Herrenschmidt 
1588e00e56dfSRafael J. Wysocki 	/* If there's no policy for the boot CPU, we have nothing to do. */
15893a3e9e06SViresh Kumar 	policy = cpufreq_cpu_get(cpu);
15903a3e9e06SViresh Kumar 	if (!policy)
1591e00e56dfSRafael J. Wysocki 		return 0;
159242d4dc3fSBenjamin Herrenschmidt 
15931c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->suspend) {
15943a3e9e06SViresh Kumar 		ret = cpufreq_driver->suspend(policy);
1595ce6c3997SDominik Brodowski 		if (ret)
159642d4dc3fSBenjamin Herrenschmidt 			printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
15973a3e9e06SViresh Kumar 					"step on CPU %u\n", policy->cpu);
159842d4dc3fSBenjamin Herrenschmidt 	}
159942d4dc3fSBenjamin Herrenschmidt 
16003a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
1601c9060494SDave Jones 	return ret;
160242d4dc3fSBenjamin Herrenschmidt }
160342d4dc3fSBenjamin Herrenschmidt 
160442d4dc3fSBenjamin Herrenschmidt /**
1605e00e56dfSRafael J. Wysocki  * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
16061da177e4SLinus Torvalds  *
16071da177e4SLinus Torvalds  *	1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1608ce6c3997SDominik Brodowski  *	2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1609ce6c3997SDominik Brodowski  *	    restored. It will verify that the current freq is in sync with
1610ce6c3997SDominik Brodowski  *	    what we believe it to be. This is a bit later than when it
1611ce6c3997SDominik Brodowski  *	    should be, but nonethteless it's better than calling
1612ce6c3997SDominik Brodowski  *	    cpufreq_driver->get() here which might re-enable interrupts...
1613e00e56dfSRafael J. Wysocki  *
1614e00e56dfSRafael J. Wysocki  * This function is only executed for the boot CPU.  The other CPUs have not
1615e00e56dfSRafael J. Wysocki  * been turned on yet.
16161da177e4SLinus Torvalds  */
1617e00e56dfSRafael J. Wysocki static void cpufreq_bp_resume(void)
16181da177e4SLinus Torvalds {
1619e08f5f5bSGautham R Shenoy 	int ret = 0;
16204bc5d341SDave Jones 
1621e00e56dfSRafael J. Wysocki 	int cpu = smp_processor_id();
16223a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
16231da177e4SLinus Torvalds 
16242d06d8c4SDominik Brodowski 	pr_debug("resuming cpu %u\n", cpu);
16251da177e4SLinus Torvalds 
1626e00e56dfSRafael J. Wysocki 	/* If there's no policy for the boot CPU, we have nothing to do. */
16273a3e9e06SViresh Kumar 	policy = cpufreq_cpu_get(cpu);
16283a3e9e06SViresh Kumar 	if (!policy)
1629e00e56dfSRafael J. Wysocki 		return;
16301da177e4SLinus Torvalds 
16311c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->resume) {
16323a3e9e06SViresh Kumar 		ret = cpufreq_driver->resume(policy);
16331da177e4SLinus Torvalds 		if (ret) {
16341da177e4SLinus Torvalds 			printk(KERN_ERR "cpufreq: resume failed in ->resume "
16353a3e9e06SViresh Kumar 					"step on CPU %u\n", policy->cpu);
1636c9060494SDave Jones 			goto fail;
16371da177e4SLinus Torvalds 		}
16381da177e4SLinus Torvalds 	}
16391da177e4SLinus Torvalds 
16403a3e9e06SViresh Kumar 	schedule_work(&policy->update);
1641ce6c3997SDominik Brodowski 
1642c9060494SDave Jones fail:
16433a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
16441da177e4SLinus Torvalds }
16451da177e4SLinus Torvalds 
1646e00e56dfSRafael J. Wysocki static struct syscore_ops cpufreq_syscore_ops = {
1647e00e56dfSRafael J. Wysocki 	.suspend	= cpufreq_bp_suspend,
1648e00e56dfSRafael J. Wysocki 	.resume		= cpufreq_bp_resume,
16491da177e4SLinus Torvalds };
16501da177e4SLinus Torvalds 
16519d95046eSBorislav Petkov /**
16529d95046eSBorislav Petkov  *	cpufreq_get_current_driver - return current driver's name
16539d95046eSBorislav Petkov  *
16549d95046eSBorislav Petkov  *	Return the name string of the currently loaded cpufreq driver
16559d95046eSBorislav Petkov  *	or NULL, if none.
16569d95046eSBorislav Petkov  */
16579d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
16589d95046eSBorislav Petkov {
16591c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
16601c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
16611c3d85ddSRafael J. Wysocki 
16621c3d85ddSRafael J. Wysocki 	return NULL;
16639d95046eSBorislav Petkov }
16649d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
16651da177e4SLinus Torvalds 
16661da177e4SLinus Torvalds /*********************************************************************
16671da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
16681da177e4SLinus Torvalds  *********************************************************************/
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds /**
16711da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
16721da177e4SLinus Torvalds  *	@nb: notifier function to register
16731da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
16741da177e4SLinus Torvalds  *
16751da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
16761da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
16771da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
16781da177e4SLinus Torvalds  *      changes in cpufreq policy.
16791da177e4SLinus Torvalds  *
16801da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1681e041c683SAlan Stern  *	blocking_notifier_chain_register.
16821da177e4SLinus Torvalds  */
16831da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
16841da177e4SLinus Torvalds {
16851da177e4SLinus Torvalds 	int ret;
16861da177e4SLinus Torvalds 
1687d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1688d5aaffa9SDirk Brandewie 		return -EINVAL;
1689d5aaffa9SDirk Brandewie 
169074212ca4SCesar Eduardo Barros 	WARN_ON(!init_cpufreq_transition_notifier_list_called);
169174212ca4SCesar Eduardo Barros 
16921da177e4SLinus Torvalds 	switch (list) {
16931da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1694b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1695e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
16961da177e4SLinus Torvalds 		break;
16971da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1698e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1699e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
17001da177e4SLinus Torvalds 		break;
17011da177e4SLinus Torvalds 	default:
17021da177e4SLinus Torvalds 		ret = -EINVAL;
17031da177e4SLinus Torvalds 	}
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds 	return ret;
17061da177e4SLinus Torvalds }
17071da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds /**
17101da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
17111da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
17121da177e4SLinus Torvalds  *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
17131da177e4SLinus Torvalds  *
17141da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
17151da177e4SLinus Torvalds  *
17161da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1717e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
17181da177e4SLinus Torvalds  */
17191da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
17201da177e4SLinus Torvalds {
17211da177e4SLinus Torvalds 	int ret;
17221da177e4SLinus Torvalds 
1723d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1724d5aaffa9SDirk Brandewie 		return -EINVAL;
1725d5aaffa9SDirk Brandewie 
17261da177e4SLinus Torvalds 	switch (list) {
17271da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1728b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
1729e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
17301da177e4SLinus Torvalds 		break;
17311da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1732e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1733e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
17341da177e4SLinus Torvalds 		break;
17351da177e4SLinus Torvalds 	default:
17361da177e4SLinus Torvalds 		ret = -EINVAL;
17371da177e4SLinus Torvalds 	}
17381da177e4SLinus Torvalds 
17391da177e4SLinus Torvalds 	return ret;
17401da177e4SLinus Torvalds }
17411da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
17421da177e4SLinus Torvalds 
17431da177e4SLinus Torvalds 
17441da177e4SLinus Torvalds /*********************************************************************
17451da177e4SLinus Torvalds  *                              GOVERNORS                            *
17461da177e4SLinus Torvalds  *********************************************************************/
17471da177e4SLinus Torvalds 
17481da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
17491da177e4SLinus Torvalds 			    unsigned int target_freq,
17501da177e4SLinus Torvalds 			    unsigned int relation)
17511da177e4SLinus Torvalds {
17521da177e4SLinus Torvalds 	int retval = -EINVAL;
17537249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
1754c32b6b8eSAshok Raj 
1755a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1756a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1757a7b422cdSKonrad Rzeszutek Wilk 
17587249924eSViresh Kumar 	/* Make sure that target_freq is within supported range */
17597249924eSViresh Kumar 	if (target_freq > policy->max)
17607249924eSViresh Kumar 		target_freq = policy->max;
17617249924eSViresh Kumar 	if (target_freq < policy->min)
17627249924eSViresh Kumar 		target_freq = policy->min;
17637249924eSViresh Kumar 
17647249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
17657249924eSViresh Kumar 			policy->cpu, target_freq, relation, old_target_freq);
17665a1c0228SViresh Kumar 
17679c0ebcf7SViresh Kumar 	/*
17689c0ebcf7SViresh Kumar 	 * This might look like a redundant call as we are checking it again
17699c0ebcf7SViresh Kumar 	 * after finding index. But it is left intentionally for cases where
17709c0ebcf7SViresh Kumar 	 * exactly same freq is called again and so we can save on few function
17719c0ebcf7SViresh Kumar 	 * calls.
17729c0ebcf7SViresh Kumar 	 */
17735a1c0228SViresh Kumar 	if (target_freq == policy->cur)
17745a1c0228SViresh Kumar 		return 0;
17755a1c0228SViresh Kumar 
17761c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
17771c3d85ddSRafael J. Wysocki 		retval = cpufreq_driver->target(policy, target_freq, relation);
17789c0ebcf7SViresh Kumar 	else if (cpufreq_driver->target_index) {
17799c0ebcf7SViresh Kumar 		struct cpufreq_frequency_table *freq_table;
1780d4019f0aSViresh Kumar 		struct cpufreq_freqs freqs;
1781d4019f0aSViresh Kumar 		bool notify;
17829c0ebcf7SViresh Kumar 		int index;
178390d45d17SAshok Raj 
17849c0ebcf7SViresh Kumar 		freq_table = cpufreq_frequency_get_table(policy->cpu);
17859c0ebcf7SViresh Kumar 		if (unlikely(!freq_table)) {
17869c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find freq_table\n", __func__);
17879c0ebcf7SViresh Kumar 			goto out;
17889c0ebcf7SViresh Kumar 		}
17899c0ebcf7SViresh Kumar 
17909c0ebcf7SViresh Kumar 		retval = cpufreq_frequency_table_target(policy, freq_table,
17919c0ebcf7SViresh Kumar 				target_freq, relation, &index);
17929c0ebcf7SViresh Kumar 		if (unlikely(retval)) {
17939c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find matching freq\n", __func__);
17949c0ebcf7SViresh Kumar 			goto out;
17959c0ebcf7SViresh Kumar 		}
17969c0ebcf7SViresh Kumar 
1797d4019f0aSViresh Kumar 		if (freq_table[index].frequency == policy->cur) {
17989c0ebcf7SViresh Kumar 			retval = 0;
1799d4019f0aSViresh Kumar 			goto out;
1800d4019f0aSViresh Kumar 		}
1801d4019f0aSViresh Kumar 
1802d4019f0aSViresh Kumar 		notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1803d4019f0aSViresh Kumar 
1804d4019f0aSViresh Kumar 		if (notify) {
1805d4019f0aSViresh Kumar 			freqs.old = policy->cur;
1806d4019f0aSViresh Kumar 			freqs.new = freq_table[index].frequency;
1807d4019f0aSViresh Kumar 			freqs.flags = 0;
1808d4019f0aSViresh Kumar 
1809d4019f0aSViresh Kumar 			pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1810d4019f0aSViresh Kumar 					__func__, policy->cpu, freqs.old,
1811d4019f0aSViresh Kumar 					freqs.new);
1812d4019f0aSViresh Kumar 
1813d4019f0aSViresh Kumar 			cpufreq_notify_transition(policy, &freqs,
1814d4019f0aSViresh Kumar 					CPUFREQ_PRECHANGE);
1815d4019f0aSViresh Kumar 		}
1816d4019f0aSViresh Kumar 
18179c0ebcf7SViresh Kumar 		retval = cpufreq_driver->target_index(policy, index);
1818d4019f0aSViresh Kumar 		if (retval)
1819d4019f0aSViresh Kumar 			pr_err("%s: Failed to change cpu frequency: %d\n",
1820d4019f0aSViresh Kumar 					__func__, retval);
1821d4019f0aSViresh Kumar 
1822ab1b1c4eSViresh Kumar 		if (notify)
1823ab1b1c4eSViresh Kumar 			cpufreq_notify_post_transition(policy, &freqs, retval);
18249c0ebcf7SViresh Kumar 	}
18259c0ebcf7SViresh Kumar 
18269c0ebcf7SViresh Kumar out:
18271da177e4SLinus Torvalds 	return retval;
18281da177e4SLinus Torvalds }
18291da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
18301da177e4SLinus Torvalds 
18311da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
18321da177e4SLinus Torvalds 			  unsigned int target_freq,
18331da177e4SLinus Torvalds 			  unsigned int relation)
18341da177e4SLinus Torvalds {
1835f1829e4aSJulia Lawall 	int ret = -EINVAL;
18361da177e4SLinus Torvalds 
1837ad7722daSviresh kumar 	down_write(&policy->rwsem);
18381da177e4SLinus Torvalds 
18391da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
18401da177e4SLinus Torvalds 
1841ad7722daSviresh kumar 	up_write(&policy->rwsem);
18421da177e4SLinus Torvalds 
18431da177e4SLinus Torvalds 	return ret;
18441da177e4SLinus Torvalds }
18451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
18461da177e4SLinus Torvalds 
1847153d7f3fSArjan van de Ven /*
1848153d7f3fSArjan van de Ven  * when "event" is CPUFREQ_GOV_LIMITS
1849153d7f3fSArjan van de Ven  */
18501da177e4SLinus Torvalds 
1851e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy,
1852e08f5f5bSGautham R Shenoy 					unsigned int event)
18531da177e4SLinus Torvalds {
1854cc993cabSDave Jones 	int ret;
18556afde10cSThomas Renninger 
18566afde10cSThomas Renninger 	/* Only must be defined when default governor is known to have latency
18576afde10cSThomas Renninger 	   restrictions, like e.g. conservative or ondemand.
18586afde10cSThomas Renninger 	   That this is the case is already ensured in Kconfig
18596afde10cSThomas Renninger 	*/
18606afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
18616afde10cSThomas Renninger 	struct cpufreq_governor *gov = &cpufreq_gov_performance;
18626afde10cSThomas Renninger #else
18636afde10cSThomas Renninger 	struct cpufreq_governor *gov = NULL;
18646afde10cSThomas Renninger #endif
18651c256245SThomas Renninger 
18661c256245SThomas Renninger 	if (policy->governor->max_transition_latency &&
18671c256245SThomas Renninger 	    policy->cpuinfo.transition_latency >
18681c256245SThomas Renninger 	    policy->governor->max_transition_latency) {
18696afde10cSThomas Renninger 		if (!gov)
18706afde10cSThomas Renninger 			return -EINVAL;
18716afde10cSThomas Renninger 		else {
18721c256245SThomas Renninger 			printk(KERN_WARNING "%s governor failed, too long"
18731c256245SThomas Renninger 			       " transition latency of HW, fallback"
18741c256245SThomas Renninger 			       " to %s governor\n",
18751c256245SThomas Renninger 			       policy->governor->name,
18761c256245SThomas Renninger 			       gov->name);
18771c256245SThomas Renninger 			policy->governor = gov;
18781c256245SThomas Renninger 		}
18796afde10cSThomas Renninger 	}
18801da177e4SLinus Torvalds 
1881fe492f3fSViresh Kumar 	if (event == CPUFREQ_GOV_POLICY_INIT)
18821da177e4SLinus Torvalds 		if (!try_module_get(policy->governor->owner))
18831da177e4SLinus Torvalds 			return -EINVAL;
18841da177e4SLinus Torvalds 
18852d06d8c4SDominik Brodowski 	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1886e08f5f5bSGautham R Shenoy 						policy->cpu, event);
188795731ebbSXiaoguang Chen 
188895731ebbSXiaoguang Chen 	mutex_lock(&cpufreq_governor_lock);
188956d07db2SSrivatsa S. Bhat 	if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
1890f73d3933SViresh Kumar 	    || (!policy->governor_enabled
1891f73d3933SViresh Kumar 	    && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
189295731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
189395731ebbSXiaoguang Chen 		return -EBUSY;
189495731ebbSXiaoguang Chen 	}
189595731ebbSXiaoguang Chen 
189695731ebbSXiaoguang Chen 	if (event == CPUFREQ_GOV_STOP)
189795731ebbSXiaoguang Chen 		policy->governor_enabled = false;
189895731ebbSXiaoguang Chen 	else if (event == CPUFREQ_GOV_START)
189995731ebbSXiaoguang Chen 		policy->governor_enabled = true;
190095731ebbSXiaoguang Chen 
190195731ebbSXiaoguang Chen 	mutex_unlock(&cpufreq_governor_lock);
190295731ebbSXiaoguang Chen 
19031da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
19041da177e4SLinus Torvalds 
19054d5dcc42SViresh Kumar 	if (!ret) {
19064d5dcc42SViresh Kumar 		if (event == CPUFREQ_GOV_POLICY_INIT)
19078e53695fSViresh Kumar 			policy->governor->initialized++;
19084d5dcc42SViresh Kumar 		else if (event == CPUFREQ_GOV_POLICY_EXIT)
19098e53695fSViresh Kumar 			policy->governor->initialized--;
191095731ebbSXiaoguang Chen 	} else {
191195731ebbSXiaoguang Chen 		/* Restore original values */
191295731ebbSXiaoguang Chen 		mutex_lock(&cpufreq_governor_lock);
191395731ebbSXiaoguang Chen 		if (event == CPUFREQ_GOV_STOP)
191495731ebbSXiaoguang Chen 			policy->governor_enabled = true;
191595731ebbSXiaoguang Chen 		else if (event == CPUFREQ_GOV_START)
191695731ebbSXiaoguang Chen 			policy->governor_enabled = false;
191795731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
19184d5dcc42SViresh Kumar 	}
1919b394058fSViresh Kumar 
1920fe492f3fSViresh Kumar 	if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1921fe492f3fSViresh Kumar 			((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
19221da177e4SLinus Torvalds 		module_put(policy->governor->owner);
19231da177e4SLinus Torvalds 
19241da177e4SLinus Torvalds 	return ret;
19251da177e4SLinus Torvalds }
19261da177e4SLinus Torvalds 
19271da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
19281da177e4SLinus Torvalds {
19293bcb09a3SJeremy Fitzhardinge 	int err;
19301da177e4SLinus Torvalds 
19311da177e4SLinus Torvalds 	if (!governor)
19321da177e4SLinus Torvalds 		return -EINVAL;
19331da177e4SLinus Torvalds 
1934a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1935a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1936a7b422cdSKonrad Rzeszutek Wilk 
19373fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
19381da177e4SLinus Torvalds 
1939b394058fSViresh Kumar 	governor->initialized = 0;
19403bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
19413bcb09a3SJeremy Fitzhardinge 	if (__find_governor(governor->name) == NULL) {
19423bcb09a3SJeremy Fitzhardinge 		err = 0;
19431da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
19443bcb09a3SJeremy Fitzhardinge 	}
19451da177e4SLinus Torvalds 
19463fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
19473bcb09a3SJeremy Fitzhardinge 	return err;
19481da177e4SLinus Torvalds }
19491da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
19501da177e4SLinus Torvalds 
19511da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
19521da177e4SLinus Torvalds {
195390e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
195490e41bacSPrarit Bhargava 	int cpu;
195590e41bacSPrarit Bhargava #endif
195690e41bacSPrarit Bhargava 
19571da177e4SLinus Torvalds 	if (!governor)
19581da177e4SLinus Torvalds 		return;
19591da177e4SLinus Torvalds 
1960a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1961a7b422cdSKonrad Rzeszutek Wilk 		return;
1962a7b422cdSKonrad Rzeszutek Wilk 
196390e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
196490e41bacSPrarit Bhargava 	for_each_present_cpu(cpu) {
196590e41bacSPrarit Bhargava 		if (cpu_online(cpu))
196690e41bacSPrarit Bhargava 			continue;
196790e41bacSPrarit Bhargava 		if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
196890e41bacSPrarit Bhargava 			strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
196990e41bacSPrarit Bhargava 	}
197090e41bacSPrarit Bhargava #endif
197190e41bacSPrarit Bhargava 
19723fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
19731da177e4SLinus Torvalds 	list_del(&governor->governor_list);
19743fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
19751da177e4SLinus Torvalds 	return;
19761da177e4SLinus Torvalds }
19771da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds 
19801da177e4SLinus Torvalds /*********************************************************************
19811da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
19821da177e4SLinus Torvalds  *********************************************************************/
19831da177e4SLinus Torvalds 
19841da177e4SLinus Torvalds /**
19851da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
198629464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
198729464f28SDave Jones  *	is written
19881da177e4SLinus Torvalds  *
19891da177e4SLinus Torvalds  * Reads the current cpufreq policy.
19901da177e4SLinus Torvalds  */
19911da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
19921da177e4SLinus Torvalds {
19931da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
19941da177e4SLinus Torvalds 	if (!policy)
19951da177e4SLinus Torvalds 		return -EINVAL;
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
19981da177e4SLinus Torvalds 	if (!cpu_policy)
19991da177e4SLinus Torvalds 		return -EINVAL;
20001da177e4SLinus Torvalds 
2001d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
20021da177e4SLinus Torvalds 
20031da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
20041da177e4SLinus Torvalds 	return 0;
20051da177e4SLinus Torvalds }
20061da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
20071da177e4SLinus Torvalds 
2008153d7f3fSArjan van de Ven /*
2009037ce839SViresh Kumar  * policy : current policy.
2010037ce839SViresh Kumar  * new_policy: policy to be set.
2011153d7f3fSArjan van de Ven  */
2012037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
20133a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy)
20141da177e4SLinus Torvalds {
20157bd353a9SViresh Kumar 	int ret = 0, failed = 1;
20161da177e4SLinus Torvalds 
20173a3e9e06SViresh Kumar 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
20183a3e9e06SViresh Kumar 		new_policy->min, new_policy->max);
20191da177e4SLinus Torvalds 
2020d5b73cd8SViresh Kumar 	memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
20211da177e4SLinus Torvalds 
20223a3e9e06SViresh Kumar 	if (new_policy->min > policy->max || new_policy->max < policy->min) {
20239c9a43edSMattia Dongili 		ret = -EINVAL;
20249c9a43edSMattia Dongili 		goto error_out;
20259c9a43edSMattia Dongili 	}
20269c9a43edSMattia Dongili 
20271da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
20283a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
20291da177e4SLinus Torvalds 	if (ret)
20301da177e4SLinus Torvalds 		goto error_out;
20311da177e4SLinus Torvalds 
20321da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
2033e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
20343a3e9e06SViresh Kumar 			CPUFREQ_ADJUST, new_policy);
20351da177e4SLinus Torvalds 
20361da177e4SLinus Torvalds 	/* adjust if necessary - hardware incompatibility*/
2037e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
20383a3e9e06SViresh Kumar 			CPUFREQ_INCOMPATIBLE, new_policy);
20391da177e4SLinus Torvalds 
2040bb176f7dSViresh Kumar 	/*
2041bb176f7dSViresh Kumar 	 * verify the cpu speed can be set within this limit, which might be
2042bb176f7dSViresh Kumar 	 * different to the first one
2043bb176f7dSViresh Kumar 	 */
20443a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
2045e041c683SAlan Stern 	if (ret)
20461da177e4SLinus Torvalds 		goto error_out;
20471da177e4SLinus Torvalds 
20481da177e4SLinus Torvalds 	/* notification of the new policy */
2049e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
20503a3e9e06SViresh Kumar 			CPUFREQ_NOTIFY, new_policy);
20511da177e4SLinus Torvalds 
20523a3e9e06SViresh Kumar 	policy->min = new_policy->min;
20533a3e9e06SViresh Kumar 	policy->max = new_policy->max;
20541da177e4SLinus Torvalds 
20552d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
20563a3e9e06SViresh Kumar 					policy->min, policy->max);
20571da177e4SLinus Torvalds 
20581c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
20593a3e9e06SViresh Kumar 		policy->policy = new_policy->policy;
20602d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
20613a3e9e06SViresh Kumar 		ret = cpufreq_driver->setpolicy(new_policy);
20621da177e4SLinus Torvalds 	} else {
20633a3e9e06SViresh Kumar 		if (new_policy->governor != policy->governor) {
20641da177e4SLinus Torvalds 			/* save old, working values */
20653a3e9e06SViresh Kumar 			struct cpufreq_governor *old_gov = policy->governor;
20661da177e4SLinus Torvalds 
20672d06d8c4SDominik Brodowski 			pr_debug("governor switch\n");
20681da177e4SLinus Torvalds 
20691da177e4SLinus Torvalds 			/* end old governor */
20703a3e9e06SViresh Kumar 			if (policy->governor) {
20713a3e9e06SViresh Kumar 				__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2072ad7722daSviresh kumar 				up_write(&policy->rwsem);
20733a3e9e06SViresh Kumar 				__cpufreq_governor(policy,
20747bd353a9SViresh Kumar 						CPUFREQ_GOV_POLICY_EXIT);
2075ad7722daSviresh kumar 				down_write(&policy->rwsem);
20767bd353a9SViresh Kumar 			}
20771da177e4SLinus Torvalds 
20781da177e4SLinus Torvalds 			/* start new governor */
20793a3e9e06SViresh Kumar 			policy->governor = new_policy->governor;
20803a3e9e06SViresh Kumar 			if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
20813a3e9e06SViresh Kumar 				if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
20827bd353a9SViresh Kumar 					failed = 0;
2083955ef483SViresh Kumar 				} else {
2084ad7722daSviresh kumar 					up_write(&policy->rwsem);
20853a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
20867bd353a9SViresh Kumar 							CPUFREQ_GOV_POLICY_EXIT);
2087ad7722daSviresh kumar 					down_write(&policy->rwsem);
2088955ef483SViresh Kumar 				}
20897bd353a9SViresh Kumar 			}
20907bd353a9SViresh Kumar 
20917bd353a9SViresh Kumar 			if (failed) {
20921da177e4SLinus Torvalds 				/* new governor failed, so re-start old one */
20932d06d8c4SDominik Brodowski 				pr_debug("starting governor %s failed\n",
20943a3e9e06SViresh Kumar 							policy->governor->name);
20951da177e4SLinus Torvalds 				if (old_gov) {
20963a3e9e06SViresh Kumar 					policy->governor = old_gov;
20973a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
20987bd353a9SViresh Kumar 							CPUFREQ_GOV_POLICY_INIT);
20993a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
2100e08f5f5bSGautham R Shenoy 							   CPUFREQ_GOV_START);
21011da177e4SLinus Torvalds 				}
21021da177e4SLinus Torvalds 				ret = -EINVAL;
21031da177e4SLinus Torvalds 				goto error_out;
21041da177e4SLinus Torvalds 			}
21051da177e4SLinus Torvalds 			/* might be a policy change, too, so fall through */
21061da177e4SLinus Torvalds 		}
21072d06d8c4SDominik Brodowski 		pr_debug("governor: change or update limits\n");
21083de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
21091da177e4SLinus Torvalds 	}
21101da177e4SLinus Torvalds 
21111da177e4SLinus Torvalds error_out:
21121da177e4SLinus Torvalds 	return ret;
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds 
21151da177e4SLinus Torvalds /**
21161da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
21171da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
21181da177e4SLinus Torvalds  *
211925985edcSLucas De Marchi  *	Useful for policy notifiers which have different necessities
21201da177e4SLinus Torvalds  *	at different times.
21211da177e4SLinus Torvalds  */
21221da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
21231da177e4SLinus Torvalds {
21243a3e9e06SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
21253a3e9e06SViresh Kumar 	struct cpufreq_policy new_policy;
2126f1829e4aSJulia Lawall 	int ret;
21271da177e4SLinus Torvalds 
21283a3e9e06SViresh Kumar 	if (!policy) {
2129f1829e4aSJulia Lawall 		ret = -ENODEV;
2130f1829e4aSJulia Lawall 		goto no_policy;
2131f1829e4aSJulia Lawall 	}
21321da177e4SLinus Torvalds 
2133ad7722daSviresh kumar 	down_write(&policy->rwsem);
21341da177e4SLinus Torvalds 
21352d06d8c4SDominik Brodowski 	pr_debug("updating policy for CPU %u\n", cpu);
2136d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
21373a3e9e06SViresh Kumar 	new_policy.min = policy->user_policy.min;
21383a3e9e06SViresh Kumar 	new_policy.max = policy->user_policy.max;
21393a3e9e06SViresh Kumar 	new_policy.policy = policy->user_policy.policy;
21403a3e9e06SViresh Kumar 	new_policy.governor = policy->user_policy.governor;
21411da177e4SLinus Torvalds 
2142bb176f7dSViresh Kumar 	/*
2143bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
2144bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
2145bb176f7dSViresh Kumar 	 */
21461c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
21473a3e9e06SViresh Kumar 		new_policy.cur = cpufreq_driver->get(cpu);
21483a3e9e06SViresh Kumar 		if (!policy->cur) {
21492d06d8c4SDominik Brodowski 			pr_debug("Driver did not initialize current freq");
21503a3e9e06SViresh Kumar 			policy->cur = new_policy.cur;
2151a85f7bd3SThomas Renninger 		} else {
21529c0ebcf7SViresh Kumar 			if (policy->cur != new_policy.cur && has_target())
21533a3e9e06SViresh Kumar 				cpufreq_out_of_sync(cpu, policy->cur,
21543a3e9e06SViresh Kumar 								new_policy.cur);
21550961dd0dSThomas Renninger 		}
2156a85f7bd3SThomas Renninger 	}
21570961dd0dSThomas Renninger 
2158037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
21591da177e4SLinus Torvalds 
2160ad7722daSviresh kumar 	up_write(&policy->rwsem);
21615a01f2e8SVenkatesh Pallipadi 
21623a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
2163f1829e4aSJulia Lawall no_policy:
21641da177e4SLinus Torvalds 	return ret;
21651da177e4SLinus Torvalds }
21661da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
21671da177e4SLinus Torvalds 
21682760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb,
2169c32b6b8eSAshok Raj 					unsigned long action, void *hcpu)
2170c32b6b8eSAshok Raj {
2171c32b6b8eSAshok Raj 	unsigned int cpu = (unsigned long)hcpu;
21728a25a2fdSKay Sievers 	struct device *dev;
21735302c3fbSSrivatsa S. Bhat 	bool frozen = false;
2174c32b6b8eSAshok Raj 
21758a25a2fdSKay Sievers 	dev = get_cpu_device(cpu);
21768a25a2fdSKay Sievers 	if (dev) {
21775302c3fbSSrivatsa S. Bhat 
2178d4faadd5SRafael J. Wysocki 		if (action & CPU_TASKS_FROZEN)
2179d4faadd5SRafael J. Wysocki 			frozen = true;
2180d4faadd5SRafael J. Wysocki 
21815302c3fbSSrivatsa S. Bhat 		switch (action & ~CPU_TASKS_FROZEN) {
2182c32b6b8eSAshok Raj 		case CPU_ONLINE:
21835302c3fbSSrivatsa S. Bhat 			__cpufreq_add_dev(dev, NULL, frozen);
218423d32899SSrivatsa S. Bhat 			cpufreq_update_policy(cpu);
2185c32b6b8eSAshok Raj 			break;
21865302c3fbSSrivatsa S. Bhat 
2187c32b6b8eSAshok Raj 		case CPU_DOWN_PREPARE:
2188cedb70afSSrivatsa S. Bhat 			__cpufreq_remove_dev_prepare(dev, NULL, frozen);
21891aee40acSSrivatsa S. Bhat 			break;
21901aee40acSSrivatsa S. Bhat 
21911aee40acSSrivatsa S. Bhat 		case CPU_POST_DEAD:
2192cedb70afSSrivatsa S. Bhat 			__cpufreq_remove_dev_finish(dev, NULL, frozen);
2193c32b6b8eSAshok Raj 			break;
21945302c3fbSSrivatsa S. Bhat 
21955a01f2e8SVenkatesh Pallipadi 		case CPU_DOWN_FAILED:
21965302c3fbSSrivatsa S. Bhat 			__cpufreq_add_dev(dev, NULL, frozen);
2197c32b6b8eSAshok Raj 			break;
2198c32b6b8eSAshok Raj 		}
2199c32b6b8eSAshok Raj 	}
2200c32b6b8eSAshok Raj 	return NOTIFY_OK;
2201c32b6b8eSAshok Raj }
2202c32b6b8eSAshok Raj 
22039c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = {
2204c32b6b8eSAshok Raj 	.notifier_call = cpufreq_cpu_callback,
2205c32b6b8eSAshok Raj };
22061da177e4SLinus Torvalds 
22071da177e4SLinus Torvalds /*********************************************************************
22086f19efc0SLukasz Majewski  *               BOOST						     *
22096f19efc0SLukasz Majewski  *********************************************************************/
22106f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state)
22116f19efc0SLukasz Majewski {
22126f19efc0SLukasz Majewski 	struct cpufreq_frequency_table *freq_table;
22136f19efc0SLukasz Majewski 	struct cpufreq_policy *policy;
22146f19efc0SLukasz Majewski 	int ret = -EINVAL;
22156f19efc0SLukasz Majewski 
22166f19efc0SLukasz Majewski 	list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
22176f19efc0SLukasz Majewski 		freq_table = cpufreq_frequency_get_table(policy->cpu);
22186f19efc0SLukasz Majewski 		if (freq_table) {
22196f19efc0SLukasz Majewski 			ret = cpufreq_frequency_table_cpuinfo(policy,
22206f19efc0SLukasz Majewski 							freq_table);
22216f19efc0SLukasz Majewski 			if (ret) {
22226f19efc0SLukasz Majewski 				pr_err("%s: Policy frequency update failed\n",
22236f19efc0SLukasz Majewski 				       __func__);
22246f19efc0SLukasz Majewski 				break;
22256f19efc0SLukasz Majewski 			}
22266f19efc0SLukasz Majewski 			policy->user_policy.max = policy->max;
22276f19efc0SLukasz Majewski 			__cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
22286f19efc0SLukasz Majewski 		}
22296f19efc0SLukasz Majewski 	}
22306f19efc0SLukasz Majewski 
22316f19efc0SLukasz Majewski 	return ret;
22326f19efc0SLukasz Majewski }
22336f19efc0SLukasz Majewski 
22346f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state)
22356f19efc0SLukasz Majewski {
22366f19efc0SLukasz Majewski 	unsigned long flags;
22376f19efc0SLukasz Majewski 	int ret = 0;
22386f19efc0SLukasz Majewski 
22396f19efc0SLukasz Majewski 	if (cpufreq_driver->boost_enabled == state)
22406f19efc0SLukasz Majewski 		return 0;
22416f19efc0SLukasz Majewski 
22426f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
22436f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = state;
22446f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
22456f19efc0SLukasz Majewski 
22466f19efc0SLukasz Majewski 	ret = cpufreq_driver->set_boost(state);
22476f19efc0SLukasz Majewski 	if (ret) {
22486f19efc0SLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
22496f19efc0SLukasz Majewski 		cpufreq_driver->boost_enabled = !state;
22506f19efc0SLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
22516f19efc0SLukasz Majewski 
22526f19efc0SLukasz Majewski 		pr_err("%s: Cannot %s BOOST\n", __func__,
22536f19efc0SLukasz Majewski 		       state ? "enable" : "disable");
22546f19efc0SLukasz Majewski 	}
22556f19efc0SLukasz Majewski 
22566f19efc0SLukasz Majewski 	return ret;
22576f19efc0SLukasz Majewski }
22586f19efc0SLukasz Majewski 
22596f19efc0SLukasz Majewski int cpufreq_boost_supported(void)
22606f19efc0SLukasz Majewski {
22616f19efc0SLukasz Majewski 	if (likely(cpufreq_driver))
22626f19efc0SLukasz Majewski 		return cpufreq_driver->boost_supported;
22636f19efc0SLukasz Majewski 
22646f19efc0SLukasz Majewski 	return 0;
22656f19efc0SLukasz Majewski }
22666f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
22676f19efc0SLukasz Majewski 
22686f19efc0SLukasz Majewski int cpufreq_boost_enabled(void)
22696f19efc0SLukasz Majewski {
22706f19efc0SLukasz Majewski 	return cpufreq_driver->boost_enabled;
22716f19efc0SLukasz Majewski }
22726f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
22736f19efc0SLukasz Majewski 
22746f19efc0SLukasz Majewski /*********************************************************************
22751da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
22761da177e4SLinus Torvalds  *********************************************************************/
22771da177e4SLinus Torvalds 
22781da177e4SLinus Torvalds /**
22791da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
22801da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
22811da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
22821da177e4SLinus Torvalds  *
22831da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
22841da177e4SLinus Torvalds  * returns zero on success, -EBUSY when another driver got here first
22851da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
22861da177e4SLinus Torvalds  *
22871da177e4SLinus Torvalds  */
2288221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
22891da177e4SLinus Torvalds {
22901da177e4SLinus Torvalds 	unsigned long flags;
22911da177e4SLinus Torvalds 	int ret;
22921da177e4SLinus Torvalds 
2293a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2294a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2295a7b422cdSKonrad Rzeszutek Wilk 
22961da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
22979c0ebcf7SViresh Kumar 	    !(driver_data->setpolicy || driver_data->target_index ||
22989c0ebcf7SViresh Kumar 		    driver_data->target))
22991da177e4SLinus Torvalds 		return -EINVAL;
23001da177e4SLinus Torvalds 
23012d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
23021da177e4SLinus Torvalds 
23031da177e4SLinus Torvalds 	if (driver_data->setpolicy)
23041da177e4SLinus Torvalds 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
23051da177e4SLinus Torvalds 
23060d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
23071c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
23080d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23094dea5806SYinghai Lu 		return -EEXIST;
23101da177e4SLinus Torvalds 	}
23111c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
23120d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23131da177e4SLinus Torvalds 
23146f19efc0SLukasz Majewski 	if (cpufreq_boost_supported()) {
23156f19efc0SLukasz Majewski 		/*
23166f19efc0SLukasz Majewski 		 * Check if driver provides function to enable boost -
23176f19efc0SLukasz Majewski 		 * if not, use cpufreq_boost_set_sw as default
23186f19efc0SLukasz Majewski 		 */
23196f19efc0SLukasz Majewski 		if (!cpufreq_driver->set_boost)
23206f19efc0SLukasz Majewski 			cpufreq_driver->set_boost = cpufreq_boost_set_sw;
23216f19efc0SLukasz Majewski 
23226f19efc0SLukasz Majewski 		ret = cpufreq_sysfs_create_file(&boost.attr);
23236f19efc0SLukasz Majewski 		if (ret) {
23246f19efc0SLukasz Majewski 			pr_err("%s: cannot register global BOOST sysfs file\n",
23256f19efc0SLukasz Majewski 				__func__);
23266f19efc0SLukasz Majewski 			goto err_null_driver;
23276f19efc0SLukasz Majewski 		}
23286f19efc0SLukasz Majewski 	}
23296f19efc0SLukasz Majewski 
23308a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
23318f5bc2abSJiri Slaby 	if (ret)
23326f19efc0SLukasz Majewski 		goto err_boost_unreg;
23331da177e4SLinus Torvalds 
23341c3d85ddSRafael J. Wysocki 	if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
23351da177e4SLinus Torvalds 		int i;
23361da177e4SLinus Torvalds 		ret = -ENODEV;
23371da177e4SLinus Torvalds 
23381da177e4SLinus Torvalds 		/* check for at least one working CPU */
23397a6aedfaSMike Travis 		for (i = 0; i < nr_cpu_ids; i++)
23407a6aedfaSMike Travis 			if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
23411da177e4SLinus Torvalds 				ret = 0;
23427a6aedfaSMike Travis 				break;
23437a6aedfaSMike Travis 			}
23441da177e4SLinus Torvalds 
23451da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
23461da177e4SLinus Torvalds 		if (ret) {
23472d06d8c4SDominik Brodowski 			pr_debug("no CPU initialized for driver %s\n",
2348e08f5f5bSGautham R Shenoy 							driver_data->name);
23498a25a2fdSKay Sievers 			goto err_if_unreg;
23501da177e4SLinus Torvalds 		}
23511da177e4SLinus Torvalds 	}
23521da177e4SLinus Torvalds 
235365edc68cSChandra Seetharaman 	register_hotcpu_notifier(&cpufreq_cpu_notifier);
23542d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
23551da177e4SLinus Torvalds 
23568f5bc2abSJiri Slaby 	return 0;
23578a25a2fdSKay Sievers err_if_unreg:
23588a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
23596f19efc0SLukasz Majewski err_boost_unreg:
23606f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
23616f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
23628f5bc2abSJiri Slaby err_null_driver:
23630d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
23641c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
23650d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23664d34a67dSDave Jones 	return ret;
23671da177e4SLinus Torvalds }
23681da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
23691da177e4SLinus Torvalds 
23701da177e4SLinus Torvalds /**
23711da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
23721da177e4SLinus Torvalds  *
23731da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
23741da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
23751da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
23761da177e4SLinus Torvalds  * currently not initialised.
23771da177e4SLinus Torvalds  */
2378221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
23791da177e4SLinus Torvalds {
23801da177e4SLinus Torvalds 	unsigned long flags;
23811da177e4SLinus Torvalds 
23821c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
23831da177e4SLinus Torvalds 		return -EINVAL;
23841da177e4SLinus Torvalds 
23852d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
23861da177e4SLinus Torvalds 
23878a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
23886f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
23896f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
23906f19efc0SLukasz Majewski 
239165edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
23921da177e4SLinus Torvalds 
23936eed9404SViresh Kumar 	down_write(&cpufreq_rwsem);
23940d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
23956eed9404SViresh Kumar 
23961c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
23976eed9404SViresh Kumar 
23980d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23996eed9404SViresh Kumar 	up_write(&cpufreq_rwsem);
24001da177e4SLinus Torvalds 
24011da177e4SLinus Torvalds 	return 0;
24021da177e4SLinus Torvalds }
24031da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
24045a01f2e8SVenkatesh Pallipadi 
24055a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
24065a01f2e8SVenkatesh Pallipadi {
2407a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2408a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2409a7b422cdSKonrad Rzeszutek Wilk 
24102361be23SViresh Kumar 	cpufreq_global_kobject = kobject_create();
24118aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
2412e00e56dfSRafael J. Wysocki 	register_syscore_ops(&cpufreq_syscore_ops);
24138aa84ad8SThomas Renninger 
24145a01f2e8SVenkatesh Pallipadi 	return 0;
24155a01f2e8SVenkatesh Pallipadi }
24165a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2417