xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision e0b3165ba521c6824b3e9f563f38a701dfa2d8e6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/drivers/cpufreq/cpufreq.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2001 Russell King
51da177e4SLinus Torvalds  *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6bb176f7dSViresh Kumar  *            (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
71da177e4SLinus Torvalds  *
8c32b6b8eSAshok Raj  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9c32b6b8eSAshok Raj  *	Added handling for CPU hotplug
108ff69732SDave Jones  *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
118ff69732SDave Jones  *	Fix handling for CPU hotplug -- affected CPUs
12c32b6b8eSAshok Raj  *
131da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
141da177e4SLinus Torvalds  * it under the terms of the GNU General Public License version 2 as
151da177e4SLinus Torvalds  * published by the Free Software Foundation.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
18db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19db701151SViresh Kumar 
205ff0a268SViresh Kumar #include <linux/cpu.h>
211da177e4SLinus Torvalds #include <linux/cpufreq.h>
221da177e4SLinus Torvalds #include <linux/delay.h>
231da177e4SLinus Torvalds #include <linux/device.h>
245ff0a268SViresh Kumar #include <linux/init.h>
255ff0a268SViresh Kumar #include <linux/kernel_stat.h>
265ff0a268SViresh Kumar #include <linux/module.h>
273fc54d37Sakpm@osdl.org #include <linux/mutex.h>
285ff0a268SViresh Kumar #include <linux/slab.h>
292f0aea93SViresh Kumar #include <linux/suspend.h>
305ff0a268SViresh Kumar #include <linux/tick.h>
316f4f2723SThomas Renninger #include <trace/events/power.h>
326f4f2723SThomas Renninger 
331da177e4SLinus Torvalds /**
34cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
351da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
361da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
371da177e4SLinus Torvalds  */
381c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
397a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
408414809cSSrivatsa S. Bhat static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
41bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
426f1e4efdSJane Li DEFINE_MUTEX(cpufreq_governor_lock);
43c88a1f8bSLukasz Majewski static LIST_HEAD(cpufreq_policy_list);
44bb176f7dSViresh Kumar 
45084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */
46e77b89f1SDmitry Monakhov static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
471da177e4SLinus Torvalds 
482f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */
492f0aea93SViresh Kumar static bool cpufreq_suspended;
502f0aea93SViresh Kumar 
519c0ebcf7SViresh Kumar static inline bool has_target(void)
529c0ebcf7SViresh Kumar {
539c0ebcf7SViresh Kumar 	return cpufreq_driver->target_index || cpufreq_driver->target;
549c0ebcf7SViresh Kumar }
559c0ebcf7SViresh Kumar 
565a01f2e8SVenkatesh Pallipadi /*
576eed9404SViresh Kumar  * rwsem to guarantee that cpufreq driver module doesn't unload during critical
586eed9404SViresh Kumar  * sections
596eed9404SViresh Kumar  */
606eed9404SViresh Kumar static DECLARE_RWSEM(cpufreq_rwsem);
616eed9404SViresh Kumar 
621da177e4SLinus Torvalds /* internal prototypes */
6329464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy,
6429464f28SDave Jones 		unsigned int event);
655a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu);
6665f27f38SDavid Howells static void handle_update(struct work_struct *work);
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds /**
691da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
701da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
711da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
721da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
731da177e4SLinus Torvalds  * The mutex locks both lists.
741da177e4SLinus Torvalds  */
75e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
76b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list;
771da177e4SLinus Torvalds 
7874212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called;
79b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void)
80b4dfdbb3SAlan Stern {
81b4dfdbb3SAlan Stern 	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
8274212ca4SCesar Eduardo Barros 	init_cpufreq_transition_notifier_list_called = true;
83b4dfdbb3SAlan Stern 	return 0;
84b4dfdbb3SAlan Stern }
85b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list);
861da177e4SLinus Torvalds 
87a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
88da584455SViresh Kumar static int cpufreq_disabled(void)
89a7b422cdSKonrad Rzeszutek Wilk {
90a7b422cdSKonrad Rzeszutek Wilk 	return off;
91a7b422cdSKonrad Rzeszutek Wilk }
92a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
93a7b422cdSKonrad Rzeszutek Wilk {
94a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
95a7b422cdSKonrad Rzeszutek Wilk }
961da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list);
973fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
981da177e4SLinus Torvalds 
994d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1004d5dcc42SViresh Kumar {
1010b981e70SViresh Kumar 	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
1024d5dcc42SViresh Kumar }
1033f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1044d5dcc42SViresh Kumar 
105944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
106944e9a03SViresh Kumar {
107944e9a03SViresh Kumar 	if (have_governor_per_policy())
108944e9a03SViresh Kumar 		return &policy->kobj;
109944e9a03SViresh Kumar 	else
110944e9a03SViresh Kumar 		return cpufreq_global_kobject;
111944e9a03SViresh Kumar }
112944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
113944e9a03SViresh Kumar 
11472a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
11572a4ce34SViresh Kumar {
11672a4ce34SViresh Kumar 	u64 idle_time;
11772a4ce34SViresh Kumar 	u64 cur_wall_time;
11872a4ce34SViresh Kumar 	u64 busy_time;
11972a4ce34SViresh Kumar 
12072a4ce34SViresh Kumar 	cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
12172a4ce34SViresh Kumar 
12272a4ce34SViresh Kumar 	busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
12372a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
12472a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
12572a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
12672a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
12772a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
12872a4ce34SViresh Kumar 
12972a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
13072a4ce34SViresh Kumar 	if (wall)
13172a4ce34SViresh Kumar 		*wall = cputime_to_usecs(cur_wall_time);
13272a4ce34SViresh Kumar 
13372a4ce34SViresh Kumar 	return cputime_to_usecs(idle_time);
13472a4ce34SViresh Kumar }
13572a4ce34SViresh Kumar 
13672a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
13772a4ce34SViresh Kumar {
13872a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
13972a4ce34SViresh Kumar 
14072a4ce34SViresh Kumar 	if (idle_time == -1ULL)
14172a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
14272a4ce34SViresh Kumar 	else if (!io_busy)
14372a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
14472a4ce34SViresh Kumar 
14572a4ce34SViresh Kumar 	return idle_time;
14672a4ce34SViresh Kumar }
14772a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
14872a4ce34SViresh Kumar 
14970e9e778SViresh Kumar /*
15070e9e778SViresh Kumar  * This is a generic cpufreq init() routine which can be used by cpufreq
15170e9e778SViresh Kumar  * drivers of SMP systems. It will do following:
15270e9e778SViresh Kumar  * - validate & show freq table passed
15370e9e778SViresh Kumar  * - set policies transition latency
15470e9e778SViresh Kumar  * - policy->cpus with all possible CPUs
15570e9e778SViresh Kumar  */
15670e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy,
15770e9e778SViresh Kumar 		struct cpufreq_frequency_table *table,
15870e9e778SViresh Kumar 		unsigned int transition_latency)
15970e9e778SViresh Kumar {
16070e9e778SViresh Kumar 	int ret;
16170e9e778SViresh Kumar 
16270e9e778SViresh Kumar 	ret = cpufreq_table_validate_and_show(policy, table);
16370e9e778SViresh Kumar 	if (ret) {
16470e9e778SViresh Kumar 		pr_err("%s: invalid frequency table: %d\n", __func__, ret);
16570e9e778SViresh Kumar 		return ret;
16670e9e778SViresh Kumar 	}
16770e9e778SViresh Kumar 
16870e9e778SViresh Kumar 	policy->cpuinfo.transition_latency = transition_latency;
16970e9e778SViresh Kumar 
17070e9e778SViresh Kumar 	/*
17170e9e778SViresh Kumar 	 * The driver only supports the SMP configuartion where all processors
17270e9e778SViresh Kumar 	 * share the clock and voltage and clock.
17370e9e778SViresh Kumar 	 */
17470e9e778SViresh Kumar 	cpumask_setall(policy->cpus);
17570e9e778SViresh Kumar 
17670e9e778SViresh Kumar 	return 0;
17770e9e778SViresh Kumar }
17870e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init);
17970e9e778SViresh Kumar 
180652ed95dSViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu)
181652ed95dSViresh Kumar {
182652ed95dSViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
183652ed95dSViresh Kumar 
184652ed95dSViresh Kumar 	if (!policy || IS_ERR(policy->clk)) {
185e837f9b5SJoe Perches 		pr_err("%s: No %s associated to cpu: %d\n",
186e837f9b5SJoe Perches 		       __func__, policy ? "clk" : "policy", cpu);
187652ed95dSViresh Kumar 		return 0;
188652ed95dSViresh Kumar 	}
189652ed95dSViresh Kumar 
190652ed95dSViresh Kumar 	return clk_get_rate(policy->clk) / 1000;
191652ed95dSViresh Kumar }
192652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get);
193652ed95dSViresh Kumar 
194*e0b3165bSViresh Kumar /* Only for cpufreq core internal use */
195*e0b3165bSViresh Kumar struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
196*e0b3165bSViresh Kumar {
197*e0b3165bSViresh Kumar 	return per_cpu(cpufreq_cpu_data, cpu);
198*e0b3165bSViresh Kumar }
199*e0b3165bSViresh Kumar 
2006eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
2011da177e4SLinus Torvalds {
2026eed9404SViresh Kumar 	struct cpufreq_policy *policy = NULL;
2031da177e4SLinus Torvalds 	unsigned long flags;
2041da177e4SLinus Torvalds 
2056eed9404SViresh Kumar 	if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
2066eed9404SViresh Kumar 		return NULL;
2076eed9404SViresh Kumar 
2086eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
2096eed9404SViresh Kumar 		return NULL;
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	/* get the cpufreq driver */
2120d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
2131da177e4SLinus Torvalds 
2146eed9404SViresh Kumar 	if (cpufreq_driver) {
2151da177e4SLinus Torvalds 		/* get the CPU */
2163a3e9e06SViresh Kumar 		policy = per_cpu(cpufreq_cpu_data, cpu);
2176eed9404SViresh Kumar 		if (policy)
2186eed9404SViresh Kumar 			kobject_get(&policy->kobj);
2196eed9404SViresh Kumar 	}
2206eed9404SViresh Kumar 
2216eed9404SViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2221da177e4SLinus Torvalds 
2233a3e9e06SViresh Kumar 	if (!policy)
2246eed9404SViresh Kumar 		up_read(&cpufreq_rwsem);
2251da177e4SLinus Torvalds 
2263a3e9e06SViresh Kumar 	return policy;
227a9144436SStephen Boyd }
2281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2291da177e4SLinus Torvalds 
2303a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
231a9144436SStephen Boyd {
232d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
233d5aaffa9SDirk Brandewie 		return;
234d5aaffa9SDirk Brandewie 
2356eed9404SViresh Kumar 	kobject_put(&policy->kobj);
2366eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
237a9144436SStephen Boyd }
2381da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds /*********************************************************************
2411da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2421da177e4SLinus Torvalds  *********************************************************************/
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds /**
2451da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
2461da177e4SLinus Torvalds  *
2471da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
2481da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
2491da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
2501da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
2511da177e4SLinus Torvalds  */
2521da177e4SLinus Torvalds #ifndef CONFIG_SMP
2531da177e4SLinus Torvalds static unsigned long l_p_j_ref;
2541da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq;
2551da177e4SLinus Torvalds 
256858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
2571da177e4SLinus Torvalds {
2581da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
2591da177e4SLinus Torvalds 		return;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
2621da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
2631da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
264e837f9b5SJoe Perches 		pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
265e837f9b5SJoe Perches 			 l_p_j_ref, l_p_j_ref_freq);
2661da177e4SLinus Torvalds 	}
267d08de0c1SAfzal Mohammed 	if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
26842d4dc3fSBenjamin Herrenschmidt 	    (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
269e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
270e08f5f5bSGautham R Shenoy 								ci->new);
271e837f9b5SJoe Perches 		pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
272e837f9b5SJoe Perches 			 loops_per_jiffy, ci->new);
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds #else
276e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
277e08f5f5bSGautham R Shenoy {
278e08f5f5bSGautham R Shenoy 	return;
279e08f5f5bSGautham R Shenoy }
2801da177e4SLinus Torvalds #endif
2811da177e4SLinus Torvalds 
2820956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
283b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
2861da177e4SLinus Torvalds 
287d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
288d5aaffa9SDirk Brandewie 		return;
289d5aaffa9SDirk Brandewie 
2901c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
2912d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
292e4472cb3SDave Jones 		 state, freqs->new);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	switch (state) {
295e4472cb3SDave Jones 
2961da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
297e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
298e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
299e4472cb3SDave Jones 		 * "old frequency".
3001da177e4SLinus Torvalds 		 */
3011c3d85ddSRafael J. Wysocki 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
302e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
303e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
304e837f9b5SJoe Perches 				pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
305e4472cb3SDave Jones 					 freqs->old, policy->cur);
306e4472cb3SDave Jones 				freqs->old = policy->cur;
3071da177e4SLinus Torvalds 			}
3081da177e4SLinus Torvalds 		}
309b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
310e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
3111da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3121da177e4SLinus Torvalds 		break;
313e4472cb3SDave Jones 
3141da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3151da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
316e837f9b5SJoe Perches 		pr_debug("FREQ: %lu - CPU: %lu\n",
317e837f9b5SJoe Perches 			 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
31825e41933SThomas Renninger 		trace_cpu_frequency(freqs->new, freqs->cpu);
319b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
320e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
321e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
322e4472cb3SDave Jones 			policy->cur = freqs->new;
3231da177e4SLinus Torvalds 		break;
3241da177e4SLinus Torvalds 	}
3251da177e4SLinus Torvalds }
326bb176f7dSViresh Kumar 
327b43a7ffbSViresh Kumar /**
328b43a7ffbSViresh Kumar  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
329b43a7ffbSViresh Kumar  * on frequency transition.
330b43a7ffbSViresh Kumar  *
331b43a7ffbSViresh Kumar  * This function calls the transition notifiers and the "adjust_jiffies"
332b43a7ffbSViresh Kumar  * function. It is called twice on all CPU frequency changes that have
333b43a7ffbSViresh Kumar  * external effects.
334b43a7ffbSViresh Kumar  */
335b43a7ffbSViresh Kumar void cpufreq_notify_transition(struct cpufreq_policy *policy,
336b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
337b43a7ffbSViresh Kumar {
338b43a7ffbSViresh Kumar 	for_each_cpu(freqs->cpu, policy->cpus)
339b43a7ffbSViresh Kumar 		__cpufreq_notify_transition(policy, freqs, state);
340b43a7ffbSViresh Kumar }
3411da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
3421da177e4SLinus Torvalds 
343f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */
344f7ba3b41SViresh Kumar void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
345f7ba3b41SViresh Kumar 		struct cpufreq_freqs *freqs, int transition_failed)
346f7ba3b41SViresh Kumar {
347f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
348f7ba3b41SViresh Kumar 	if (!transition_failed)
349f7ba3b41SViresh Kumar 		return;
350f7ba3b41SViresh Kumar 
351f7ba3b41SViresh Kumar 	swap(freqs->old, freqs->new);
352f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
353f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
354f7ba3b41SViresh Kumar }
355f7ba3b41SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition);
356f7ba3b41SViresh Kumar 
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds /*********************************************************************
3591da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
3601da177e4SLinus Torvalds  *********************************************************************/
3618a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj,
3626f19efc0SLukasz Majewski 				 struct attribute *attr, char *buf)
3636f19efc0SLukasz Majewski {
3646f19efc0SLukasz Majewski 	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
3656f19efc0SLukasz Majewski }
3666f19efc0SLukasz Majewski 
3676f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
3686f19efc0SLukasz Majewski 				  const char *buf, size_t count)
3696f19efc0SLukasz Majewski {
3706f19efc0SLukasz Majewski 	int ret, enable;
3716f19efc0SLukasz Majewski 
3726f19efc0SLukasz Majewski 	ret = sscanf(buf, "%d", &enable);
3736f19efc0SLukasz Majewski 	if (ret != 1 || enable < 0 || enable > 1)
3746f19efc0SLukasz Majewski 		return -EINVAL;
3756f19efc0SLukasz Majewski 
3766f19efc0SLukasz Majewski 	if (cpufreq_boost_trigger_state(enable)) {
377e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST!\n",
378e837f9b5SJoe Perches 		       __func__, enable ? "enable" : "disable");
3796f19efc0SLukasz Majewski 		return -EINVAL;
3806f19efc0SLukasz Majewski 	}
3816f19efc0SLukasz Majewski 
382e837f9b5SJoe Perches 	pr_debug("%s: cpufreq BOOST %s\n",
383e837f9b5SJoe Perches 		 __func__, enable ? "enabled" : "disabled");
3846f19efc0SLukasz Majewski 
3856f19efc0SLukasz Majewski 	return count;
3866f19efc0SLukasz Majewski }
3876f19efc0SLukasz Majewski define_one_global_rw(boost);
3881da177e4SLinus Torvalds 
3893bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor)
3903bcb09a3SJeremy Fitzhardinge {
3913bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
3923bcb09a3SJeremy Fitzhardinge 
3933bcb09a3SJeremy Fitzhardinge 	list_for_each_entry(t, &cpufreq_governor_list, governor_list)
3943bcb09a3SJeremy Fitzhardinge 		if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
3953bcb09a3SJeremy Fitzhardinge 			return t;
3963bcb09a3SJeremy Fitzhardinge 
3973bcb09a3SJeremy Fitzhardinge 	return NULL;
3983bcb09a3SJeremy Fitzhardinge }
3993bcb09a3SJeremy Fitzhardinge 
4001da177e4SLinus Torvalds /**
4011da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
4021da177e4SLinus Torvalds  */
4031da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
4041da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
4051da177e4SLinus Torvalds {
4063bcb09a3SJeremy Fitzhardinge 	int err = -EINVAL;
4073bcb09a3SJeremy Fitzhardinge 
4081c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver)
4093bcb09a3SJeremy Fitzhardinge 		goto out;
4103bcb09a3SJeremy Fitzhardinge 
4111c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
4121da177e4SLinus Torvalds 		if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
4131da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
4143bcb09a3SJeremy Fitzhardinge 			err = 0;
415e08f5f5bSGautham R Shenoy 		} else if (!strnicmp(str_governor, "powersave",
416e08f5f5bSGautham R Shenoy 						CPUFREQ_NAME_LEN)) {
4171da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
4183bcb09a3SJeremy Fitzhardinge 			err = 0;
4191da177e4SLinus Torvalds 		}
4209c0ebcf7SViresh Kumar 	} else if (has_target()) {
4211da177e4SLinus Torvalds 		struct cpufreq_governor *t;
4223bcb09a3SJeremy Fitzhardinge 
4233fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
4243bcb09a3SJeremy Fitzhardinge 
4253bcb09a3SJeremy Fitzhardinge 		t = __find_governor(str_governor);
4263bcb09a3SJeremy Fitzhardinge 
427ea714970SJeremy Fitzhardinge 		if (t == NULL) {
428ea714970SJeremy Fitzhardinge 			int ret;
429ea714970SJeremy Fitzhardinge 
430ea714970SJeremy Fitzhardinge 			mutex_unlock(&cpufreq_governor_mutex);
4311a8e1463SKees Cook 			ret = request_module("cpufreq_%s", str_governor);
432ea714970SJeremy Fitzhardinge 			mutex_lock(&cpufreq_governor_mutex);
433ea714970SJeremy Fitzhardinge 
434ea714970SJeremy Fitzhardinge 			if (ret == 0)
435ea714970SJeremy Fitzhardinge 				t = __find_governor(str_governor);
436ea714970SJeremy Fitzhardinge 		}
437ea714970SJeremy Fitzhardinge 
4383bcb09a3SJeremy Fitzhardinge 		if (t != NULL) {
4391da177e4SLinus Torvalds 			*governor = t;
4403bcb09a3SJeremy Fitzhardinge 			err = 0;
4411da177e4SLinus Torvalds 		}
4423bcb09a3SJeremy Fitzhardinge 
4433bcb09a3SJeremy Fitzhardinge 		mutex_unlock(&cpufreq_governor_mutex);
4441da177e4SLinus Torvalds 	}
4451da177e4SLinus Torvalds out:
4463bcb09a3SJeremy Fitzhardinge 	return err;
4471da177e4SLinus Torvalds }
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds /**
450e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
451e08f5f5bSGautham R Shenoy  * print out cpufreq information
4521da177e4SLinus Torvalds  *
4531da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
4541da177e4SLinus Torvalds  * "unsigned int".
4551da177e4SLinus Torvalds  */
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds #define show_one(file_name, object)			\
4581da177e4SLinus Torvalds static ssize_t show_##file_name				\
4591da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
4601da177e4SLinus Torvalds {							\
4611da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
4621da177e4SLinus Torvalds }
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
4651da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
466ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
4671da177e4SLinus Torvalds show_one(scaling_min_freq, min);
4681da177e4SLinus Torvalds show_one(scaling_max_freq, max);
4691da177e4SLinus Torvalds show_one(scaling_cur_freq, cur);
4701da177e4SLinus Torvalds 
471037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
4723a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy);
4737970e08bSThomas Renninger 
4741da177e4SLinus Torvalds /**
4751da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
4761da177e4SLinus Torvalds  */
4771da177e4SLinus Torvalds #define store_one(file_name, object)			\
4781da177e4SLinus Torvalds static ssize_t store_##file_name					\
4791da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
4801da177e4SLinus Torvalds {									\
4815136fa56SSrivatsa S. Bhat 	int ret;							\
4821da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
4831da177e4SLinus Torvalds 									\
4841da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
4851da177e4SLinus Torvalds 	if (ret)							\
4861da177e4SLinus Torvalds 		return -EINVAL;						\
4871da177e4SLinus Torvalds 									\
4881da177e4SLinus Torvalds 	ret = sscanf(buf, "%u", &new_policy.object);			\
4891da177e4SLinus Torvalds 	if (ret != 1)							\
4901da177e4SLinus Torvalds 		return -EINVAL;						\
4911da177e4SLinus Torvalds 									\
492037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);		\
4937970e08bSThomas Renninger 	policy->user_policy.object = policy->object;			\
4941da177e4SLinus Torvalds 									\
4951da177e4SLinus Torvalds 	return ret ? ret : count;					\
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds store_one(scaling_min_freq, min);
4991da177e4SLinus Torvalds store_one(scaling_max_freq, max);
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds /**
5021da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
5031da177e4SLinus Torvalds  */
504e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
505e08f5f5bSGautham R Shenoy 					char *buf)
5061da177e4SLinus Torvalds {
5075a01f2e8SVenkatesh Pallipadi 	unsigned int cur_freq = __cpufreq_get(policy->cpu);
5081da177e4SLinus Torvalds 	if (!cur_freq)
5091da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
5101da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds /**
5141da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
5151da177e4SLinus Torvalds  */
516905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
5171da177e4SLinus Torvalds {
5181da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
5191da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
5201da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
5211da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
5221da177e4SLinus Torvalds 	else if (policy->governor)
5234b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
52429464f28SDave Jones 				policy->governor->name);
5251da177e4SLinus Torvalds 	return -EINVAL;
5261da177e4SLinus Torvalds }
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds /**
5291da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
5301da177e4SLinus Torvalds  */
5311da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
5321da177e4SLinus Torvalds 					const char *buf, size_t count)
5331da177e4SLinus Torvalds {
5345136fa56SSrivatsa S. Bhat 	int ret;
5351da177e4SLinus Torvalds 	char	str_governor[16];
5361da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);
5391da177e4SLinus Torvalds 	if (ret)
5401da177e4SLinus Torvalds 		return ret;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
5431da177e4SLinus Torvalds 	if (ret != 1)
5441da177e4SLinus Torvalds 		return -EINVAL;
5451da177e4SLinus Torvalds 
546e08f5f5bSGautham R Shenoy 	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
547e08f5f5bSGautham R Shenoy 						&new_policy.governor))
5481da177e4SLinus Torvalds 		return -EINVAL;
5491da177e4SLinus Torvalds 
550037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
5517970e08bSThomas Renninger 
5527970e08bSThomas Renninger 	policy->user_policy.policy = policy->policy;
5537970e08bSThomas Renninger 	policy->user_policy.governor = policy->governor;
5547970e08bSThomas Renninger 
555e08f5f5bSGautham R Shenoy 	if (ret)
556e08f5f5bSGautham R Shenoy 		return ret;
557e08f5f5bSGautham R Shenoy 	else
558e08f5f5bSGautham R Shenoy 		return count;
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds /**
5621da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
5631da177e4SLinus Torvalds  */
5641da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
5651da177e4SLinus Torvalds {
5661c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds /**
5701da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
5711da177e4SLinus Torvalds  */
5721da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
5731da177e4SLinus Torvalds 						char *buf)
5741da177e4SLinus Torvalds {
5751da177e4SLinus Torvalds 	ssize_t i = 0;
5761da177e4SLinus Torvalds 	struct cpufreq_governor *t;
5771da177e4SLinus Torvalds 
5789c0ebcf7SViresh Kumar 	if (!has_target()) {
5791da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
5801da177e4SLinus Torvalds 		goto out;
5811da177e4SLinus Torvalds 	}
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
58429464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
58529464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
5861da177e4SLinus Torvalds 			goto out;
5874b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
5881da177e4SLinus Torvalds 	}
5891da177e4SLinus Torvalds out:
5901da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
5911da177e4SLinus Torvalds 	return i;
5921da177e4SLinus Torvalds }
593e8628dd0SDarrick J. Wong 
594f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
5951da177e4SLinus Torvalds {
5961da177e4SLinus Torvalds 	ssize_t i = 0;
5971da177e4SLinus Torvalds 	unsigned int cpu;
5981da177e4SLinus Torvalds 
599835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
6001da177e4SLinus Torvalds 		if (i)
6011da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
6021da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
6031da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
6041da177e4SLinus Torvalds 			break;
6051da177e4SLinus Torvalds 	}
6061da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
6071da177e4SLinus Torvalds 	return i;
6081da177e4SLinus Torvalds }
609f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
6101da177e4SLinus Torvalds 
611e8628dd0SDarrick J. Wong /**
612e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
613e8628dd0SDarrick J. Wong  * hw coordination is in use
614e8628dd0SDarrick J. Wong  */
615e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
616e8628dd0SDarrick J. Wong {
617f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
618e8628dd0SDarrick J. Wong }
619e8628dd0SDarrick J. Wong 
620e8628dd0SDarrick J. Wong /**
621e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
622e8628dd0SDarrick J. Wong  */
623e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
624e8628dd0SDarrick J. Wong {
625f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
626e8628dd0SDarrick J. Wong }
627e8628dd0SDarrick J. Wong 
6289e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
6299e76988eSVenki Pallipadi 					const char *buf, size_t count)
6309e76988eSVenki Pallipadi {
6319e76988eSVenki Pallipadi 	unsigned int freq = 0;
6329e76988eSVenki Pallipadi 	unsigned int ret;
6339e76988eSVenki Pallipadi 
634879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
6359e76988eSVenki Pallipadi 		return -EINVAL;
6369e76988eSVenki Pallipadi 
6379e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
6389e76988eSVenki Pallipadi 	if (ret != 1)
6399e76988eSVenki Pallipadi 		return -EINVAL;
6409e76988eSVenki Pallipadi 
6419e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
6429e76988eSVenki Pallipadi 
6439e76988eSVenki Pallipadi 	return count;
6449e76988eSVenki Pallipadi }
6459e76988eSVenki Pallipadi 
6469e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
6479e76988eSVenki Pallipadi {
648879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
6499e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
6509e76988eSVenki Pallipadi 
6519e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
6529e76988eSVenki Pallipadi }
6531da177e4SLinus Torvalds 
654e2f74f35SThomas Renninger /**
6558bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
656e2f74f35SThomas Renninger  */
657e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
658e2f74f35SThomas Renninger {
659e2f74f35SThomas Renninger 	unsigned int limit;
660e2f74f35SThomas Renninger 	int ret;
6611c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
6621c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
663e2f74f35SThomas Renninger 		if (!ret)
664e2f74f35SThomas Renninger 			return sprintf(buf, "%u\n", limit);
665e2f74f35SThomas Renninger 	}
666e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
667e2f74f35SThomas Renninger }
668e2f74f35SThomas Renninger 
6696dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
6706dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
6716dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
6726dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
6736dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
6746dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
6756dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
6766dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
6776dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
6786dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
6796dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
6806dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
6816dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
6826dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds static struct attribute *default_attrs[] = {
6851da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
6861da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
687ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
6881da177e4SLinus Torvalds 	&scaling_min_freq.attr,
6891da177e4SLinus Torvalds 	&scaling_max_freq.attr,
6901da177e4SLinus Torvalds 	&affected_cpus.attr,
691e8628dd0SDarrick J. Wong 	&related_cpus.attr,
6921da177e4SLinus Torvalds 	&scaling_governor.attr,
6931da177e4SLinus Torvalds 	&scaling_driver.attr,
6941da177e4SLinus Torvalds 	&scaling_available_governors.attr,
6959e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
6961da177e4SLinus Torvalds 	NULL
6971da177e4SLinus Torvalds };
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
7001da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
7011da177e4SLinus Torvalds 
7021da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
7031da177e4SLinus Torvalds {
7041da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7051da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
7061b750e3bSViresh Kumar 	ssize_t ret;
7076eed9404SViresh Kumar 
7086eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
7091b750e3bSViresh Kumar 		return -EINVAL;
7105a01f2e8SVenkatesh Pallipadi 
711ad7722daSviresh kumar 	down_read(&policy->rwsem);
7125a01f2e8SVenkatesh Pallipadi 
713e08f5f5bSGautham R Shenoy 	if (fattr->show)
714e08f5f5bSGautham R Shenoy 		ret = fattr->show(policy, buf);
715e08f5f5bSGautham R Shenoy 	else
716e08f5f5bSGautham R Shenoy 		ret = -EIO;
717e08f5f5bSGautham R Shenoy 
718ad7722daSviresh kumar 	up_read(&policy->rwsem);
7196eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
7201b750e3bSViresh Kumar 
7211da177e4SLinus Torvalds 	return ret;
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
7251da177e4SLinus Torvalds 		     const char *buf, size_t count)
7261da177e4SLinus Torvalds {
7271da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7281da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
729a07530b4SDave Jones 	ssize_t ret = -EINVAL;
7306eed9404SViresh Kumar 
7314f750c93SSrivatsa S. Bhat 	get_online_cpus();
7324f750c93SSrivatsa S. Bhat 
7334f750c93SSrivatsa S. Bhat 	if (!cpu_online(policy->cpu))
7344f750c93SSrivatsa S. Bhat 		goto unlock;
7354f750c93SSrivatsa S. Bhat 
7366eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
7374f750c93SSrivatsa S. Bhat 		goto unlock;
7385a01f2e8SVenkatesh Pallipadi 
739ad7722daSviresh kumar 	down_write(&policy->rwsem);
7405a01f2e8SVenkatesh Pallipadi 
741e08f5f5bSGautham R Shenoy 	if (fattr->store)
742e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
743e08f5f5bSGautham R Shenoy 	else
744e08f5f5bSGautham R Shenoy 		ret = -EIO;
745e08f5f5bSGautham R Shenoy 
746ad7722daSviresh kumar 	up_write(&policy->rwsem);
7476eed9404SViresh Kumar 
7486eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
7494f750c93SSrivatsa S. Bhat unlock:
7504f750c93SSrivatsa S. Bhat 	put_online_cpus();
7514f750c93SSrivatsa S. Bhat 
7521da177e4SLinus Torvalds 	return ret;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
7561da177e4SLinus Torvalds {
7571da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7582d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
7591da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
76252cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
7631da177e4SLinus Torvalds 	.show	= show,
7641da177e4SLinus Torvalds 	.store	= store,
7651da177e4SLinus Torvalds };
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
7681da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
7691da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
7701da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
7711da177e4SLinus Torvalds };
7721da177e4SLinus Torvalds 
7732361be23SViresh Kumar struct kobject *cpufreq_global_kobject;
7742361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject);
7752361be23SViresh Kumar 
7762361be23SViresh Kumar static int cpufreq_global_kobject_usage;
7772361be23SViresh Kumar 
7782361be23SViresh Kumar int cpufreq_get_global_kobject(void)
7792361be23SViresh Kumar {
7802361be23SViresh Kumar 	if (!cpufreq_global_kobject_usage++)
7812361be23SViresh Kumar 		return kobject_add(cpufreq_global_kobject,
7822361be23SViresh Kumar 				&cpu_subsys.dev_root->kobj, "%s", "cpufreq");
7832361be23SViresh Kumar 
7842361be23SViresh Kumar 	return 0;
7852361be23SViresh Kumar }
7862361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject);
7872361be23SViresh Kumar 
7882361be23SViresh Kumar void cpufreq_put_global_kobject(void)
7892361be23SViresh Kumar {
7902361be23SViresh Kumar 	if (!--cpufreq_global_kobject_usage)
7912361be23SViresh Kumar 		kobject_del(cpufreq_global_kobject);
7922361be23SViresh Kumar }
7932361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject);
7942361be23SViresh Kumar 
7952361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr)
7962361be23SViresh Kumar {
7972361be23SViresh Kumar 	int ret = cpufreq_get_global_kobject();
7982361be23SViresh Kumar 
7992361be23SViresh Kumar 	if (!ret) {
8002361be23SViresh Kumar 		ret = sysfs_create_file(cpufreq_global_kobject, attr);
8012361be23SViresh Kumar 		if (ret)
8022361be23SViresh Kumar 			cpufreq_put_global_kobject();
8032361be23SViresh Kumar 	}
8042361be23SViresh Kumar 
8052361be23SViresh Kumar 	return ret;
8062361be23SViresh Kumar }
8072361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file);
8082361be23SViresh Kumar 
8092361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr)
8102361be23SViresh Kumar {
8112361be23SViresh Kumar 	sysfs_remove_file(cpufreq_global_kobject, attr);
8122361be23SViresh Kumar 	cpufreq_put_global_kobject();
8132361be23SViresh Kumar }
8142361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
8152361be23SViresh Kumar 
81619d6f7ecSDave Jones /* symlink affected CPUs */
817308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
81819d6f7ecSDave Jones {
81919d6f7ecSDave Jones 	unsigned int j;
82019d6f7ecSDave Jones 	int ret = 0;
82119d6f7ecSDave Jones 
82219d6f7ecSDave Jones 	for_each_cpu(j, policy->cpus) {
8238a25a2fdSKay Sievers 		struct device *cpu_dev;
82419d6f7ecSDave Jones 
825308b60e7SViresh Kumar 		if (j == policy->cpu)
82619d6f7ecSDave Jones 			continue;
82719d6f7ecSDave Jones 
828e8fdde10SViresh Kumar 		pr_debug("Adding link for CPU: %u\n", j);
8298a25a2fdSKay Sievers 		cpu_dev = get_cpu_device(j);
8308a25a2fdSKay Sievers 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
83119d6f7ecSDave Jones 					"cpufreq");
83271c3461eSRafael J. Wysocki 		if (ret)
83371c3461eSRafael J. Wysocki 			break;
83419d6f7ecSDave Jones 	}
83519d6f7ecSDave Jones 	return ret;
83619d6f7ecSDave Jones }
83719d6f7ecSDave Jones 
838308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
8398a25a2fdSKay Sievers 				     struct device *dev)
840909a694eSDave Jones {
841909a694eSDave Jones 	struct freq_attr **drv_attr;
842909a694eSDave Jones 	int ret = 0;
843909a694eSDave Jones 
844909a694eSDave Jones 	/* prepare interface data */
845909a694eSDave Jones 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
8468a25a2fdSKay Sievers 				   &dev->kobj, "cpufreq");
847909a694eSDave Jones 	if (ret)
848909a694eSDave Jones 		return ret;
849909a694eSDave Jones 
850909a694eSDave Jones 	/* set up files for this cpu device */
8511c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
852909a694eSDave Jones 	while ((drv_attr) && (*drv_attr)) {
853909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
854909a694eSDave Jones 		if (ret)
8551c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
856909a694eSDave Jones 		drv_attr++;
857909a694eSDave Jones 	}
8581c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
859909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
860909a694eSDave Jones 		if (ret)
8611c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
862909a694eSDave Jones 	}
8639c0ebcf7SViresh Kumar 	if (has_target()) {
864909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
865909a694eSDave Jones 		if (ret)
8661c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
867909a694eSDave Jones 	}
8681c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
869e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
870e2f74f35SThomas Renninger 		if (ret)
8711c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
872e2f74f35SThomas Renninger 	}
873909a694eSDave Jones 
874308b60e7SViresh Kumar 	ret = cpufreq_add_dev_symlink(policy);
875ecf7e461SDave Jones 	if (ret)
876ecf7e461SDave Jones 		goto err_out_kobj_put;
877ecf7e461SDave Jones 
878e18f1682SSrivatsa S. Bhat 	return ret;
879e18f1682SSrivatsa S. Bhat 
880e18f1682SSrivatsa S. Bhat err_out_kobj_put:
881e18f1682SSrivatsa S. Bhat 	kobject_put(&policy->kobj);
882e18f1682SSrivatsa S. Bhat 	wait_for_completion(&policy->kobj_unregister);
883e18f1682SSrivatsa S. Bhat 	return ret;
884e18f1682SSrivatsa S. Bhat }
885e18f1682SSrivatsa S. Bhat 
886e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy)
887e18f1682SSrivatsa S. Bhat {
8886e2c89d1Sviresh kumar 	struct cpufreq_governor *gov = NULL;
889e18f1682SSrivatsa S. Bhat 	struct cpufreq_policy new_policy;
890e18f1682SSrivatsa S. Bhat 	int ret = 0;
891e18f1682SSrivatsa S. Bhat 
892d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
893a27a9ab7SJason Baron 
8946e2c89d1Sviresh kumar 	/* Update governor of new_policy to the governor used before hotplug */
8956e2c89d1Sviresh kumar 	gov = __find_governor(per_cpu(cpufreq_cpu_governor, policy->cpu));
8966e2c89d1Sviresh kumar 	if (gov)
8976e2c89d1Sviresh kumar 		pr_debug("Restoring governor %s for cpu %d\n",
8986e2c89d1Sviresh kumar 				policy->governor->name, policy->cpu);
8996e2c89d1Sviresh kumar 	else
9006e2c89d1Sviresh kumar 		gov = CPUFREQ_DEFAULT_GOVERNOR;
9016e2c89d1Sviresh kumar 
9026e2c89d1Sviresh kumar 	new_policy.governor = gov;
9036e2c89d1Sviresh kumar 
904a27a9ab7SJason Baron 	/* Use the default policy if its valid. */
905a27a9ab7SJason Baron 	if (cpufreq_driver->setpolicy)
9066e2c89d1Sviresh kumar 		cpufreq_parse_governor(gov->name, &new_policy.policy, NULL);
907ecf7e461SDave Jones 
908ecf7e461SDave Jones 	/* set default policy */
909037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
910ecf7e461SDave Jones 	if (ret) {
9112d06d8c4SDominik Brodowski 		pr_debug("setting policy failed\n");
9121c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
9131c3d85ddSRafael J. Wysocki 			cpufreq_driver->exit(policy);
914ecf7e461SDave Jones 	}
915909a694eSDave Jones }
916909a694eSDave Jones 
917fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
918d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
91942f921a6SViresh Kumar 				  unsigned int cpu, struct device *dev)
920fcf80582SViresh Kumar {
9219c0ebcf7SViresh Kumar 	int ret = 0;
922fcf80582SViresh Kumar 	unsigned long flags;
923fcf80582SViresh Kumar 
9249c0ebcf7SViresh Kumar 	if (has_target()) {
9253de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
9263de9bdebSViresh Kumar 		if (ret) {
9273de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
9283de9bdebSViresh Kumar 			return ret;
9293de9bdebSViresh Kumar 		}
9303de9bdebSViresh Kumar 	}
931fcf80582SViresh Kumar 
932ad7722daSviresh kumar 	down_write(&policy->rwsem);
9332eaa3e2dSViresh Kumar 
9340d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
9352eaa3e2dSViresh Kumar 
936fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
937fcf80582SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = policy;
9380d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
939fcf80582SViresh Kumar 
940ad7722daSviresh kumar 	up_write(&policy->rwsem);
9412eaa3e2dSViresh Kumar 
9429c0ebcf7SViresh Kumar 	if (has_target()) {
9433de9bdebSViresh Kumar 		if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
9443de9bdebSViresh Kumar 			(ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
9453de9bdebSViresh Kumar 			pr_err("%s: Failed to start governor\n", __func__);
9463de9bdebSViresh Kumar 			return ret;
9473de9bdebSViresh Kumar 		}
948820c6ca2SViresh Kumar 	}
949fcf80582SViresh Kumar 
95042f921a6SViresh Kumar 	return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
951fcf80582SViresh Kumar }
952fcf80582SViresh Kumar #endif
9531da177e4SLinus Torvalds 
9548414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
9558414809cSSrivatsa S. Bhat {
9568414809cSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
9578414809cSSrivatsa S. Bhat 	unsigned long flags;
9588414809cSSrivatsa S. Bhat 
95944871c9cSLan Tianyu 	read_lock_irqsave(&cpufreq_driver_lock, flags);
9608414809cSSrivatsa S. Bhat 
9618414809cSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
9628414809cSSrivatsa S. Bhat 
96344871c9cSLan Tianyu 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
9648414809cSSrivatsa S. Bhat 
9656e2c89d1Sviresh kumar 	policy->governor = NULL;
9666e2c89d1Sviresh kumar 
9678414809cSSrivatsa S. Bhat 	return policy;
9688414809cSSrivatsa S. Bhat }
9698414809cSSrivatsa S. Bhat 
970e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void)
971e9698cc5SSrivatsa S. Bhat {
972e9698cc5SSrivatsa S. Bhat 	struct cpufreq_policy *policy;
973e9698cc5SSrivatsa S. Bhat 
974e9698cc5SSrivatsa S. Bhat 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
975e9698cc5SSrivatsa S. Bhat 	if (!policy)
976e9698cc5SSrivatsa S. Bhat 		return NULL;
977e9698cc5SSrivatsa S. Bhat 
978e9698cc5SSrivatsa S. Bhat 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
979e9698cc5SSrivatsa S. Bhat 		goto err_free_policy;
980e9698cc5SSrivatsa S. Bhat 
981e9698cc5SSrivatsa S. Bhat 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
982e9698cc5SSrivatsa S. Bhat 		goto err_free_cpumask;
983e9698cc5SSrivatsa S. Bhat 
984c88a1f8bSLukasz Majewski 	INIT_LIST_HEAD(&policy->policy_list);
985ad7722daSviresh kumar 	init_rwsem(&policy->rwsem);
986ad7722daSviresh kumar 
987e9698cc5SSrivatsa S. Bhat 	return policy;
988e9698cc5SSrivatsa S. Bhat 
989e9698cc5SSrivatsa S. Bhat err_free_cpumask:
990e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
991e9698cc5SSrivatsa S. Bhat err_free_policy:
992e9698cc5SSrivatsa S. Bhat 	kfree(policy);
993e9698cc5SSrivatsa S. Bhat 
994e9698cc5SSrivatsa S. Bhat 	return NULL;
995e9698cc5SSrivatsa S. Bhat }
996e9698cc5SSrivatsa S. Bhat 
99742f921a6SViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
99842f921a6SViresh Kumar {
99942f921a6SViresh Kumar 	struct kobject *kobj;
100042f921a6SViresh Kumar 	struct completion *cmp;
100142f921a6SViresh Kumar 
1002fcd7af91SViresh Kumar 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1003fcd7af91SViresh Kumar 			CPUFREQ_REMOVE_POLICY, policy);
1004fcd7af91SViresh Kumar 
100542f921a6SViresh Kumar 	down_read(&policy->rwsem);
100642f921a6SViresh Kumar 	kobj = &policy->kobj;
100742f921a6SViresh Kumar 	cmp = &policy->kobj_unregister;
100842f921a6SViresh Kumar 	up_read(&policy->rwsem);
100942f921a6SViresh Kumar 	kobject_put(kobj);
101042f921a6SViresh Kumar 
101142f921a6SViresh Kumar 	/*
101242f921a6SViresh Kumar 	 * We need to make sure that the underlying kobj is
101342f921a6SViresh Kumar 	 * actually not referenced anymore by anybody before we
101442f921a6SViresh Kumar 	 * proceed with unloading.
101542f921a6SViresh Kumar 	 */
101642f921a6SViresh Kumar 	pr_debug("waiting for dropping of refcount\n");
101742f921a6SViresh Kumar 	wait_for_completion(cmp);
101842f921a6SViresh Kumar 	pr_debug("wait complete\n");
101942f921a6SViresh Kumar }
102042f921a6SViresh Kumar 
1021e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy)
1022e9698cc5SSrivatsa S. Bhat {
1023e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
1024e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1025e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1026e9698cc5SSrivatsa S. Bhat }
1027e9698cc5SSrivatsa S. Bhat 
10280d66b91eSSrivatsa S. Bhat static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
10290d66b91eSSrivatsa S. Bhat {
103099ec899eSSrivatsa S. Bhat 	if (WARN_ON(cpu == policy->cpu))
1031cb38ed5cSSrivatsa S. Bhat 		return;
1032cb38ed5cSSrivatsa S. Bhat 
1033ad7722daSviresh kumar 	down_write(&policy->rwsem);
10348efd5765SViresh Kumar 
10350d66b91eSSrivatsa S. Bhat 	policy->last_cpu = policy->cpu;
10360d66b91eSSrivatsa S. Bhat 	policy->cpu = cpu;
10370d66b91eSSrivatsa S. Bhat 
1038ad7722daSviresh kumar 	up_write(&policy->rwsem);
10398efd5765SViresh Kumar 
10400d66b91eSSrivatsa S. Bhat 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
10410d66b91eSSrivatsa S. Bhat 			CPUFREQ_UPDATE_POLICY_CPU, policy);
10420d66b91eSSrivatsa S. Bhat }
10430d66b91eSSrivatsa S. Bhat 
1044a82fab29SSrivatsa S. Bhat static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1045a82fab29SSrivatsa S. Bhat 			     bool frozen)
10461da177e4SLinus Torvalds {
1047fcf80582SViresh Kumar 	unsigned int j, cpu = dev->id;
104865922465SViresh Kumar 	int ret = -ENOMEM;
10491da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
10501da177e4SLinus Torvalds 	unsigned long flags;
105190e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
10521b274294SViresh Kumar 	struct cpufreq_policy *tpolicy;
105390e41bacSPrarit Bhargava #endif
10541da177e4SLinus Torvalds 
1055c32b6b8eSAshok Raj 	if (cpu_is_offline(cpu))
1056c32b6b8eSAshok Raj 		return 0;
1057c32b6b8eSAshok Raj 
10582d06d8c4SDominik Brodowski 	pr_debug("adding CPU %u\n", cpu);
10591da177e4SLinus Torvalds 
10601da177e4SLinus Torvalds #ifdef CONFIG_SMP
10611da177e4SLinus Torvalds 	/* check whether a different CPU already registered this
10621da177e4SLinus Torvalds 	 * CPU because it is in the same boat. */
10631da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(cpu);
10641da177e4SLinus Torvalds 	if (unlikely(policy)) {
10658ff69732SDave Jones 		cpufreq_cpu_put(policy);
10661da177e4SLinus Torvalds 		return 0;
10671da177e4SLinus Torvalds 	}
10685025d628SLi Zhong #endif
1069fcf80582SViresh Kumar 
10706eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
10716eed9404SViresh Kumar 		return 0;
10726eed9404SViresh Kumar 
1073fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
1074fcf80582SViresh Kumar 	/* Check if this cpu was hot-unplugged earlier and has siblings */
10750d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
10761b274294SViresh Kumar 	list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
10771b274294SViresh Kumar 		if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
10780d1857a1SNathan Zimmer 			read_unlock_irqrestore(&cpufreq_driver_lock, flags);
107942f921a6SViresh Kumar 			ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
10806eed9404SViresh Kumar 			up_read(&cpufreq_rwsem);
10816eed9404SViresh Kumar 			return ret;
1082fcf80582SViresh Kumar 		}
10832eaa3e2dSViresh Kumar 	}
10840d1857a1SNathan Zimmer 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1085fcf80582SViresh Kumar #endif
10861da177e4SLinus Torvalds 
108772368d12SRafael J. Wysocki 	/*
108872368d12SRafael J. Wysocki 	 * Restore the saved policy when doing light-weight init and fall back
108972368d12SRafael J. Wysocki 	 * to the full init if that fails.
109072368d12SRafael J. Wysocki 	 */
109172368d12SRafael J. Wysocki 	policy = frozen ? cpufreq_policy_restore(cpu) : NULL;
109272368d12SRafael J. Wysocki 	if (!policy) {
109372368d12SRafael J. Wysocki 		frozen = false;
1094e9698cc5SSrivatsa S. Bhat 		policy = cpufreq_policy_alloc();
1095059019a3SDave Jones 		if (!policy)
10961da177e4SLinus Torvalds 			goto nomem_out;
109772368d12SRafael J. Wysocki 	}
10980d66b91eSSrivatsa S. Bhat 
10990d66b91eSSrivatsa S. Bhat 	/*
11000d66b91eSSrivatsa S. Bhat 	 * In the resume path, since we restore a saved policy, the assignment
11010d66b91eSSrivatsa S. Bhat 	 * to policy->cpu is like an update of the existing policy, rather than
11020d66b91eSSrivatsa S. Bhat 	 * the creation of a brand new one. So we need to perform this update
11030d66b91eSSrivatsa S. Bhat 	 * by invoking update_policy_cpu().
11040d66b91eSSrivatsa S. Bhat 	 */
11050d66b91eSSrivatsa S. Bhat 	if (frozen && cpu != policy->cpu)
11060d66b91eSSrivatsa S. Bhat 		update_policy_cpu(policy, cpu);
11070d66b91eSSrivatsa S. Bhat 	else
11081da177e4SLinus Torvalds 		policy->cpu = cpu;
11090d66b91eSSrivatsa S. Bhat 
1110835481d9SRusty Russell 	cpumask_copy(policy->cpus, cpumask_of(cpu));
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	init_completion(&policy->kobj_unregister);
111365f27f38SDavid Howells 	INIT_WORK(&policy->update, handle_update);
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
11161da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
11171da177e4SLinus Torvalds 	 */
11181c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->init(policy);
11191da177e4SLinus Torvalds 	if (ret) {
11202d06d8c4SDominik Brodowski 		pr_debug("initialization failed\n");
11212eaa3e2dSViresh Kumar 		goto err_set_policy_cpu;
11221da177e4SLinus Torvalds 	}
1123643ae6e8SViresh Kumar 
11245a7e56a5SViresh Kumar 	/* related cpus should atleast have policy->cpus */
11255a7e56a5SViresh Kumar 	cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
11265a7e56a5SViresh Kumar 
11275a7e56a5SViresh Kumar 	/*
11285a7e56a5SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
11295a7e56a5SViresh Kumar 	 * managing offline cpus here.
11305a7e56a5SViresh Kumar 	 */
11315a7e56a5SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
11325a7e56a5SViresh Kumar 
11335a7e56a5SViresh Kumar 	if (!frozen) {
11345a7e56a5SViresh Kumar 		policy->user_policy.min = policy->min;
11355a7e56a5SViresh Kumar 		policy->user_policy.max = policy->max;
11365a7e56a5SViresh Kumar 	}
11375a7e56a5SViresh Kumar 
11384e97b631SViresh Kumar 	down_write(&policy->rwsem);
1139652ed95dSViresh Kumar 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1140652ed95dSViresh Kumar 	for_each_cpu(j, policy->cpus)
1141652ed95dSViresh Kumar 		per_cpu(cpufreq_cpu_data, j) = policy;
1142652ed95dSViresh Kumar 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1143652ed95dSViresh Kumar 
1144da60ce9fSViresh Kumar 	if (cpufreq_driver->get) {
1145da60ce9fSViresh Kumar 		policy->cur = cpufreq_driver->get(policy->cpu);
1146da60ce9fSViresh Kumar 		if (!policy->cur) {
1147da60ce9fSViresh Kumar 			pr_err("%s: ->get() failed\n", __func__);
1148da60ce9fSViresh Kumar 			goto err_get_freq;
1149da60ce9fSViresh Kumar 		}
1150da60ce9fSViresh Kumar 	}
1151da60ce9fSViresh Kumar 
1152d3916691SViresh Kumar 	/*
1153d3916691SViresh Kumar 	 * Sometimes boot loaders set CPU frequency to a value outside of
1154d3916691SViresh Kumar 	 * frequency table present with cpufreq core. In such cases CPU might be
1155d3916691SViresh Kumar 	 * unstable if it has to run on that frequency for long duration of time
1156d3916691SViresh Kumar 	 * and so its better to set it to a frequency which is specified in
1157d3916691SViresh Kumar 	 * freq-table. This also makes cpufreq stats inconsistent as
1158d3916691SViresh Kumar 	 * cpufreq-stats would fail to register because current frequency of CPU
1159d3916691SViresh Kumar 	 * isn't found in freq-table.
1160d3916691SViresh Kumar 	 *
1161d3916691SViresh Kumar 	 * Because we don't want this change to effect boot process badly, we go
1162d3916691SViresh Kumar 	 * for the next freq which is >= policy->cur ('cur' must be set by now,
1163d3916691SViresh Kumar 	 * otherwise we will end up setting freq to lowest of the table as 'cur'
1164d3916691SViresh Kumar 	 * is initialized to zero).
1165d3916691SViresh Kumar 	 *
1166d3916691SViresh Kumar 	 * We are passing target-freq as "policy->cur - 1" otherwise
1167d3916691SViresh Kumar 	 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1168d3916691SViresh Kumar 	 * equal to target-freq.
1169d3916691SViresh Kumar 	 */
1170d3916691SViresh Kumar 	if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1171d3916691SViresh Kumar 	    && has_target()) {
1172d3916691SViresh Kumar 		/* Are we running at unknown frequency ? */
1173d3916691SViresh Kumar 		ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1174d3916691SViresh Kumar 		if (ret == -EINVAL) {
1175d3916691SViresh Kumar 			/* Warn user and fix it */
1176d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1177d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1178d3916691SViresh Kumar 			ret = __cpufreq_driver_target(policy, policy->cur - 1,
1179d3916691SViresh Kumar 				CPUFREQ_RELATION_L);
1180d3916691SViresh Kumar 
1181d3916691SViresh Kumar 			/*
1182d3916691SViresh Kumar 			 * Reaching here after boot in a few seconds may not
1183d3916691SViresh Kumar 			 * mean that system will remain stable at "unknown"
1184d3916691SViresh Kumar 			 * frequency for longer duration. Hence, a BUG_ON().
1185d3916691SViresh Kumar 			 */
1186d3916691SViresh Kumar 			BUG_ON(ret);
1187d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1188d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1189d3916691SViresh Kumar 		}
1190d3916691SViresh Kumar 	}
1191d3916691SViresh Kumar 
1192a1531acdSThomas Renninger 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1193a1531acdSThomas Renninger 				     CPUFREQ_START, policy);
1194a1531acdSThomas Renninger 
1195a82fab29SSrivatsa S. Bhat 	if (!frozen) {
1196308b60e7SViresh Kumar 		ret = cpufreq_add_dev_interface(policy, dev);
119719d6f7ecSDave Jones 		if (ret)
11980142f9dcSAhmed S. Darwish 			goto err_out_unregister;
1199fcd7af91SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1200fcd7af91SViresh Kumar 				CPUFREQ_CREATE_POLICY, policy);
12019515f4d6SViresh Kumar 	}
1202c88a1f8bSLukasz Majewski 
1203c88a1f8bSLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1204c88a1f8bSLukasz Majewski 	list_add(&policy->policy_list, &cpufreq_policy_list);
1205c88a1f8bSLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
12068ff69732SDave Jones 
1207e18f1682SSrivatsa S. Bhat 	cpufreq_init_policy(policy);
1208e18f1682SSrivatsa S. Bhat 
120908fd8c1cSViresh Kumar 	if (!frozen) {
121008fd8c1cSViresh Kumar 		policy->user_policy.policy = policy->policy;
121108fd8c1cSViresh Kumar 		policy->user_policy.governor = policy->governor;
121208fd8c1cSViresh Kumar 	}
12134e97b631SViresh Kumar 	up_write(&policy->rwsem);
121408fd8c1cSViresh Kumar 
1215038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
12166eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
12176eed9404SViresh Kumar 
12182d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
12191da177e4SLinus Torvalds 
12201da177e4SLinus Torvalds 	return 0;
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds err_out_unregister:
1223652ed95dSViresh Kumar err_get_freq:
12240d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1225474deff7SViresh Kumar 	for_each_cpu(j, policy->cpus)
12267a6aedfaSMike Travis 		per_cpu(cpufreq_cpu_data, j) = NULL;
12270d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
12281da177e4SLinus Torvalds 
1229da60ce9fSViresh Kumar 	if (cpufreq_driver->exit)
1230da60ce9fSViresh Kumar 		cpufreq_driver->exit(policy);
12312eaa3e2dSViresh Kumar err_set_policy_cpu:
123272368d12SRafael J. Wysocki 	if (frozen) {
123372368d12SRafael J. Wysocki 		/* Do not leave stale fallback data behind. */
123472368d12SRafael J. Wysocki 		per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
123542f921a6SViresh Kumar 		cpufreq_policy_put_kobj(policy);
123672368d12SRafael J. Wysocki 	}
1237e9698cc5SSrivatsa S. Bhat 	cpufreq_policy_free(policy);
123842f921a6SViresh Kumar 
12391da177e4SLinus Torvalds nomem_out:
12406eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
12416eed9404SViresh Kumar 
12421da177e4SLinus Torvalds 	return ret;
12431da177e4SLinus Torvalds }
12441da177e4SLinus Torvalds 
1245a82fab29SSrivatsa S. Bhat /**
1246a82fab29SSrivatsa S. Bhat  * cpufreq_add_dev - add a CPU device
1247a82fab29SSrivatsa S. Bhat  *
1248a82fab29SSrivatsa S. Bhat  * Adds the cpufreq interface for a CPU device.
1249a82fab29SSrivatsa S. Bhat  *
1250a82fab29SSrivatsa S. Bhat  * The Oracle says: try running cpufreq registration/unregistration concurrently
1251a82fab29SSrivatsa S. Bhat  * with with cpu hotplugging and all hell will break loose. Tried to clean this
1252a82fab29SSrivatsa S. Bhat  * mess up, but more thorough testing is needed. - Mathieu
1253a82fab29SSrivatsa S. Bhat  */
1254a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1255a82fab29SSrivatsa S. Bhat {
1256a82fab29SSrivatsa S. Bhat 	return __cpufreq_add_dev(dev, sif, false);
1257a82fab29SSrivatsa S. Bhat }
1258a82fab29SSrivatsa S. Bhat 
12593a3e9e06SViresh Kumar static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
126042f921a6SViresh Kumar 					   unsigned int old_cpu)
1261f9ba680dSSrivatsa S. Bhat {
1262f9ba680dSSrivatsa S. Bhat 	struct device *cpu_dev;
1263f9ba680dSSrivatsa S. Bhat 	int ret;
1264f9ba680dSSrivatsa S. Bhat 
1265f9ba680dSSrivatsa S. Bhat 	/* first sibling now owns the new sysfs dir */
12669c8f1ee4SViresh Kumar 	cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
1267a82fab29SSrivatsa S. Bhat 
1268f9ba680dSSrivatsa S. Bhat 	sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
12693a3e9e06SViresh Kumar 	ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1270f9ba680dSSrivatsa S. Bhat 	if (ret) {
1271e837f9b5SJoe Perches 		pr_err("%s: Failed to move kobj: %d\n", __func__, ret);
1272f9ba680dSSrivatsa S. Bhat 
1273ad7722daSviresh kumar 		down_write(&policy->rwsem);
12743a3e9e06SViresh Kumar 		cpumask_set_cpu(old_cpu, policy->cpus);
1275ad7722daSviresh kumar 		up_write(&policy->rwsem);
1276f9ba680dSSrivatsa S. Bhat 
12773a3e9e06SViresh Kumar 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1278f9ba680dSSrivatsa S. Bhat 					"cpufreq");
1279f9ba680dSSrivatsa S. Bhat 
1280f9ba680dSSrivatsa S. Bhat 		return -EINVAL;
1281f9ba680dSSrivatsa S. Bhat 	}
1282f9ba680dSSrivatsa S. Bhat 
1283f9ba680dSSrivatsa S. Bhat 	return cpu_dev->id;
1284f9ba680dSSrivatsa S. Bhat }
1285f9ba680dSSrivatsa S. Bhat 
1286cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_prepare(struct device *dev,
1287cedb70afSSrivatsa S. Bhat 					struct subsys_interface *sif,
1288cedb70afSSrivatsa S. Bhat 					bool frozen)
12891da177e4SLinus Torvalds {
1290f9ba680dSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
12913de9bdebSViresh Kumar 	int new_cpu, ret;
12921da177e4SLinus Torvalds 	unsigned long flags;
12933a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
12941da177e4SLinus Torvalds 
1295b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
12961da177e4SLinus Torvalds 
12970d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
12981da177e4SLinus Torvalds 
12993a3e9e06SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
13001da177e4SLinus Torvalds 
13018414809cSSrivatsa S. Bhat 	/* Save the policy somewhere when doing a light-weight tear-down */
13028414809cSSrivatsa S. Bhat 	if (frozen)
13033a3e9e06SViresh Kumar 		per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
13048414809cSSrivatsa S. Bhat 
13050d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
13061da177e4SLinus Torvalds 
13073a3e9e06SViresh Kumar 	if (!policy) {
1308b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
13091da177e4SLinus Torvalds 		return -EINVAL;
13101da177e4SLinus Torvalds 	}
13111da177e4SLinus Torvalds 
13129c0ebcf7SViresh Kumar 	if (has_target()) {
13133de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
13143de9bdebSViresh Kumar 		if (ret) {
13153de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
13163de9bdebSViresh Kumar 			return ret;
13173de9bdebSViresh Kumar 		}
13183de9bdebSViresh Kumar 	}
13195a01f2e8SVenkatesh Pallipadi 
13201c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->setpolicy)
1321fa69e33fSDirk Brandewie 		strncpy(per_cpu(cpufreq_cpu_governor, cpu),
13223a3e9e06SViresh Kumar 			policy->governor->name, CPUFREQ_NAME_LEN);
13231da177e4SLinus Torvalds 
1324ad7722daSviresh kumar 	down_read(&policy->rwsem);
13253a3e9e06SViresh Kumar 	cpus = cpumask_weight(policy->cpus);
1326ad7722daSviresh kumar 	up_read(&policy->rwsem);
13271da177e4SLinus Torvalds 
132861173f25SSrivatsa S. Bhat 	if (cpu != policy->cpu) {
132973bf0fc2SViresh Kumar 		sysfs_remove_link(&dev->kobj, "cpufreq");
133073bf0fc2SViresh Kumar 	} else if (cpus > 1) {
133142f921a6SViresh Kumar 		new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
1332f9ba680dSSrivatsa S. Bhat 		if (new_cpu >= 0) {
13333a3e9e06SViresh Kumar 			update_policy_cpu(policy, new_cpu);
1334a82fab29SSrivatsa S. Bhat 
1335a82fab29SSrivatsa S. Bhat 			if (!frozen) {
133675949c9aSViresh Kumar 				pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
133775949c9aSViresh Kumar 					 __func__, new_cpu, cpu);
13381da177e4SLinus Torvalds 			}
13391da177e4SLinus Torvalds 		}
1340a82fab29SSrivatsa S. Bhat 	}
1341b8eed8afSViresh Kumar 
1342cedb70afSSrivatsa S. Bhat 	return 0;
1343cedb70afSSrivatsa S. Bhat }
1344cedb70afSSrivatsa S. Bhat 
1345cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_finish(struct device *dev,
1346cedb70afSSrivatsa S. Bhat 				       struct subsys_interface *sif,
1347cedb70afSSrivatsa S. Bhat 				       bool frozen)
1348cedb70afSSrivatsa S. Bhat {
1349cedb70afSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
1350cedb70afSSrivatsa S. Bhat 	int ret;
1351cedb70afSSrivatsa S. Bhat 	unsigned long flags;
1352cedb70afSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
1353cedb70afSSrivatsa S. Bhat 
1354cedb70afSSrivatsa S. Bhat 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1355cedb70afSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data, cpu);
1356cedb70afSSrivatsa S. Bhat 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1357cedb70afSSrivatsa S. Bhat 
1358cedb70afSSrivatsa S. Bhat 	if (!policy) {
1359cedb70afSSrivatsa S. Bhat 		pr_debug("%s: No cpu_data found\n", __func__);
1360cedb70afSSrivatsa S. Bhat 		return -EINVAL;
1361cedb70afSSrivatsa S. Bhat 	}
1362cedb70afSSrivatsa S. Bhat 
1363ad7722daSviresh kumar 	down_write(&policy->rwsem);
1364cedb70afSSrivatsa S. Bhat 	cpus = cpumask_weight(policy->cpus);
13659c8f1ee4SViresh Kumar 
13669c8f1ee4SViresh Kumar 	if (cpus > 1)
13679c8f1ee4SViresh Kumar 		cpumask_clear_cpu(cpu, policy->cpus);
1368ad7722daSviresh kumar 	up_write(&policy->rwsem);
1369cedb70afSSrivatsa S. Bhat 
1370b8eed8afSViresh Kumar 	/* If cpu is last user of policy, free policy */
1371b8eed8afSViresh Kumar 	if (cpus == 1) {
13729c0ebcf7SViresh Kumar 		if (has_target()) {
13733de9bdebSViresh Kumar 			ret = __cpufreq_governor(policy,
13743de9bdebSViresh Kumar 					CPUFREQ_GOV_POLICY_EXIT);
13753de9bdebSViresh Kumar 			if (ret) {
13763de9bdebSViresh Kumar 				pr_err("%s: Failed to exit governor\n",
13773de9bdebSViresh Kumar 				       __func__);
13783de9bdebSViresh Kumar 				return ret;
13793de9bdebSViresh Kumar 			}
13803de9bdebSViresh Kumar 		}
13812a998599SRafael J. Wysocki 
138242f921a6SViresh Kumar 		if (!frozen)
138342f921a6SViresh Kumar 			cpufreq_policy_put_kobj(policy);
13841da177e4SLinus Torvalds 
13858414809cSSrivatsa S. Bhat 		/*
13868414809cSSrivatsa S. Bhat 		 * Perform the ->exit() even during light-weight tear-down,
13878414809cSSrivatsa S. Bhat 		 * since this is a core component, and is essential for the
13888414809cSSrivatsa S. Bhat 		 * subsequent light-weight ->init() to succeed.
13898414809cSSrivatsa S. Bhat 		 */
13901c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
13913a3e9e06SViresh Kumar 			cpufreq_driver->exit(policy);
139227ecddc2SJacob Shin 
13939515f4d6SViresh Kumar 		/* Remove policy from list of active policies */
13949515f4d6SViresh Kumar 		write_lock_irqsave(&cpufreq_driver_lock, flags);
13959515f4d6SViresh Kumar 		list_del(&policy->policy_list);
13969515f4d6SViresh Kumar 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
13979515f4d6SViresh Kumar 
13988414809cSSrivatsa S. Bhat 		if (!frozen)
13993a3e9e06SViresh Kumar 			cpufreq_policy_free(policy);
14002a998599SRafael J. Wysocki 	} else {
14019c0ebcf7SViresh Kumar 		if (has_target()) {
14023de9bdebSViresh Kumar 			if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
14033de9bdebSViresh Kumar 					(ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
14043de9bdebSViresh Kumar 				pr_err("%s: Failed to start governor\n",
14053de9bdebSViresh Kumar 				       __func__);
14063de9bdebSViresh Kumar 				return ret;
14073de9bdebSViresh Kumar 			}
1408b8eed8afSViresh Kumar 		}
14092a998599SRafael J. Wysocki 	}
14101da177e4SLinus Torvalds 
1411474deff7SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = NULL;
14121da177e4SLinus Torvalds 	return 0;
14131da177e4SLinus Torvalds }
14141da177e4SLinus Torvalds 
1415cedb70afSSrivatsa S. Bhat /**
141627a862e9SViresh Kumar  * cpufreq_remove_dev - remove a CPU device
1417cedb70afSSrivatsa S. Bhat  *
1418cedb70afSSrivatsa S. Bhat  * Removes the cpufreq interface for a CPU device.
1419cedb70afSSrivatsa S. Bhat  */
14208a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
14215a01f2e8SVenkatesh Pallipadi {
14228a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
142327a862e9SViresh Kumar 	int ret;
1424ec28297aSVenki Pallipadi 
1425ec28297aSVenki Pallipadi 	if (cpu_is_offline(cpu))
1426ec28297aSVenki Pallipadi 		return 0;
1427ec28297aSVenki Pallipadi 
142827a862e9SViresh Kumar 	ret = __cpufreq_remove_dev_prepare(dev, sif, false);
142927a862e9SViresh Kumar 
143027a862e9SViresh Kumar 	if (!ret)
143127a862e9SViresh Kumar 		ret = __cpufreq_remove_dev_finish(dev, sif, false);
143227a862e9SViresh Kumar 
143327a862e9SViresh Kumar 	return ret;
14345a01f2e8SVenkatesh Pallipadi }
14355a01f2e8SVenkatesh Pallipadi 
143665f27f38SDavid Howells static void handle_update(struct work_struct *work)
14371da177e4SLinus Torvalds {
143865f27f38SDavid Howells 	struct cpufreq_policy *policy =
143965f27f38SDavid Howells 		container_of(work, struct cpufreq_policy, update);
144065f27f38SDavid Howells 	unsigned int cpu = policy->cpu;
14412d06d8c4SDominik Brodowski 	pr_debug("handle_update for cpu %u called\n", cpu);
14421da177e4SLinus Torvalds 	cpufreq_update_policy(cpu);
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds 
14451da177e4SLinus Torvalds /**
1446bb176f7dSViresh Kumar  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1447bb176f7dSViresh Kumar  *	in deep trouble.
14481da177e4SLinus Torvalds  *	@cpu: cpu number
14491da177e4SLinus Torvalds  *	@old_freq: CPU frequency the kernel thinks the CPU runs at
14501da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
14511da177e4SLinus Torvalds  *
145229464f28SDave Jones  *	We adjust to current frequency first, and need to clean up later.
145329464f28SDave Jones  *	So either call to cpufreq_update_policy() or schedule handle_update()).
14541da177e4SLinus Torvalds  */
1455e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1456e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
14571da177e4SLinus Torvalds {
1458b43a7ffbSViresh Kumar 	struct cpufreq_policy *policy;
14591da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1460b43a7ffbSViresh Kumar 	unsigned long flags;
1461b43a7ffbSViresh Kumar 
1462e837f9b5SJoe Perches 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1463e837f9b5SJoe Perches 		 old_freq, new_freq);
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	freqs.old = old_freq;
14661da177e4SLinus Torvalds 	freqs.new = new_freq;
1467b43a7ffbSViresh Kumar 
1468b43a7ffbSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1469b43a7ffbSViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
1470b43a7ffbSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1471b43a7ffbSViresh Kumar 
1472b43a7ffbSViresh Kumar 	cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1473b43a7ffbSViresh Kumar 	cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
14741da177e4SLinus Torvalds }
14751da177e4SLinus Torvalds 
14761da177e4SLinus Torvalds /**
14774ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
147895235ca2SVenkatesh Pallipadi  * @cpu: CPU number
147995235ca2SVenkatesh Pallipadi  *
148095235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
148195235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
148295235ca2SVenkatesh Pallipadi  */
148395235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
148495235ca2SVenkatesh Pallipadi {
14859e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1486e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
148795235ca2SVenkatesh Pallipadi 
14881c3d85ddSRafael J. Wysocki 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
14891c3d85ddSRafael J. Wysocki 		return cpufreq_driver->get(cpu);
14909e21ba8bSDirk Brandewie 
14919e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
149295235ca2SVenkatesh Pallipadi 	if (policy) {
1493e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
149495235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
149595235ca2SVenkatesh Pallipadi 	}
149695235ca2SVenkatesh Pallipadi 
14974d34a67dSDave Jones 	return ret_freq;
149895235ca2SVenkatesh Pallipadi }
149995235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
150095235ca2SVenkatesh Pallipadi 
15013d737108SJesse Barnes /**
15023d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
15033d737108SJesse Barnes  * @cpu: CPU number
15043d737108SJesse Barnes  *
15053d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
15063d737108SJesse Barnes  */
15073d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
15083d737108SJesse Barnes {
15093d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
15103d737108SJesse Barnes 	unsigned int ret_freq = 0;
15113d737108SJesse Barnes 
15123d737108SJesse Barnes 	if (policy) {
15133d737108SJesse Barnes 		ret_freq = policy->max;
15143d737108SJesse Barnes 		cpufreq_cpu_put(policy);
15153d737108SJesse Barnes 	}
15163d737108SJesse Barnes 
15173d737108SJesse Barnes 	return ret_freq;
15183d737108SJesse Barnes }
15193d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
15203d737108SJesse Barnes 
15215a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu)
15221da177e4SLinus Torvalds {
15237a6aedfaSMike Travis 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1524e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
15251da177e4SLinus Torvalds 
15261c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->get)
15274d34a67dSDave Jones 		return ret_freq;
15281da177e4SLinus Torvalds 
15291c3d85ddSRafael J. Wysocki 	ret_freq = cpufreq_driver->get(cpu);
15301da177e4SLinus Torvalds 
1531e08f5f5bSGautham R Shenoy 	if (ret_freq && policy->cur &&
15321c3d85ddSRafael J. Wysocki 		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1533e08f5f5bSGautham R Shenoy 		/* verify no discrepancy between actual and
1534e08f5f5bSGautham R Shenoy 					saved value exists */
1535e08f5f5bSGautham R Shenoy 		if (unlikely(ret_freq != policy->cur)) {
1536e08f5f5bSGautham R Shenoy 			cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
15371da177e4SLinus Torvalds 			schedule_work(&policy->update);
15381da177e4SLinus Torvalds 		}
15391da177e4SLinus Torvalds 	}
15401da177e4SLinus Torvalds 
15414d34a67dSDave Jones 	return ret_freq;
15425a01f2e8SVenkatesh Pallipadi }
15431da177e4SLinus Torvalds 
15445a01f2e8SVenkatesh Pallipadi /**
15455a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
15465a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
15475a01f2e8SVenkatesh Pallipadi  *
15485a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
15495a01f2e8SVenkatesh Pallipadi  */
15505a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
15515a01f2e8SVenkatesh Pallipadi {
1552999976e0SAaron Plattner 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
15535a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
15545a01f2e8SVenkatesh Pallipadi 
1555999976e0SAaron Plattner 	if (policy) {
1556ad7722daSviresh kumar 		down_read(&policy->rwsem);
15575a01f2e8SVenkatesh Pallipadi 		ret_freq = __cpufreq_get(cpu);
1558ad7722daSviresh kumar 		up_read(&policy->rwsem);
1559999976e0SAaron Plattner 
1560999976e0SAaron Plattner 		cpufreq_cpu_put(policy);
1561999976e0SAaron Plattner 	}
15626eed9404SViresh Kumar 
15634d34a67dSDave Jones 	return ret_freq;
15641da177e4SLinus Torvalds }
15651da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
15661da177e4SLinus Torvalds 
15678a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
15688a25a2fdSKay Sievers 	.name		= "cpufreq",
15698a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
15708a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
15718a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1572e00e56dfSRafael J. Wysocki };
1573e00e56dfSRafael J. Wysocki 
1574e28867eaSViresh Kumar /*
1575e28867eaSViresh Kumar  * In case platform wants some specific frequency to be configured
1576e28867eaSViresh Kumar  * during suspend..
1577e28867eaSViresh Kumar  */
1578e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1579e28867eaSViresh Kumar {
1580e28867eaSViresh Kumar 	int ret;
1581e28867eaSViresh Kumar 
1582e28867eaSViresh Kumar 	if (!policy->suspend_freq) {
1583e28867eaSViresh Kumar 		pr_err("%s: suspend_freq can't be zero\n", __func__);
1584e28867eaSViresh Kumar 		return -EINVAL;
1585e28867eaSViresh Kumar 	}
1586e28867eaSViresh Kumar 
1587e28867eaSViresh Kumar 	pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1588e28867eaSViresh Kumar 			policy->suspend_freq);
1589e28867eaSViresh Kumar 
1590e28867eaSViresh Kumar 	ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1591e28867eaSViresh Kumar 			CPUFREQ_RELATION_H);
1592e28867eaSViresh Kumar 	if (ret)
1593e28867eaSViresh Kumar 		pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1594e28867eaSViresh Kumar 				__func__, policy->suspend_freq, ret);
1595e28867eaSViresh Kumar 
1596e28867eaSViresh Kumar 	return ret;
1597e28867eaSViresh Kumar }
1598e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend);
1599e28867eaSViresh Kumar 
16001da177e4SLinus Torvalds /**
16012f0aea93SViresh Kumar  * cpufreq_suspend() - Suspend CPUFreq governors
1602e00e56dfSRafael J. Wysocki  *
16032f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycles for suspending governors
16042f0aea93SViresh Kumar  * as some platforms can't change frequency after this point in suspend cycle.
16052f0aea93SViresh Kumar  * Because some of the devices (like: i2c, regulators, etc) they use for
16062f0aea93SViresh Kumar  * changing frequency are suspended quickly after this point.
160742d4dc3fSBenjamin Herrenschmidt  */
16082f0aea93SViresh Kumar void cpufreq_suspend(void)
160942d4dc3fSBenjamin Herrenschmidt {
16103a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
161142d4dc3fSBenjamin Herrenschmidt 
16122f0aea93SViresh Kumar 	if (!cpufreq_driver)
16132f0aea93SViresh Kumar 		return;
161442d4dc3fSBenjamin Herrenschmidt 
16152f0aea93SViresh Kumar 	if (!has_target())
16162f0aea93SViresh Kumar 		return;
161742d4dc3fSBenjamin Herrenschmidt 
16182f0aea93SViresh Kumar 	pr_debug("%s: Suspending Governors\n", __func__);
16192f0aea93SViresh Kumar 
16202f0aea93SViresh Kumar 	list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
16212f0aea93SViresh Kumar 		if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
16222f0aea93SViresh Kumar 			pr_err("%s: Failed to stop governor for policy: %p\n",
16232f0aea93SViresh Kumar 				__func__, policy);
16242f0aea93SViresh Kumar 		else if (cpufreq_driver->suspend
16252f0aea93SViresh Kumar 		    && cpufreq_driver->suspend(policy))
16262f0aea93SViresh Kumar 			pr_err("%s: Failed to suspend driver: %p\n", __func__,
16272f0aea93SViresh Kumar 				policy);
162842d4dc3fSBenjamin Herrenschmidt 	}
162942d4dc3fSBenjamin Herrenschmidt 
16302f0aea93SViresh Kumar 	cpufreq_suspended = true;
163142d4dc3fSBenjamin Herrenschmidt }
163242d4dc3fSBenjamin Herrenschmidt 
163342d4dc3fSBenjamin Herrenschmidt /**
16342f0aea93SViresh Kumar  * cpufreq_resume() - Resume CPUFreq governors
16351da177e4SLinus Torvalds  *
16362f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycle for resuming governors that
16372f0aea93SViresh Kumar  * are suspended with cpufreq_suspend().
16381da177e4SLinus Torvalds  */
16392f0aea93SViresh Kumar void cpufreq_resume(void)
16401da177e4SLinus Torvalds {
16413a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
16421da177e4SLinus Torvalds 
16432f0aea93SViresh Kumar 	if (!cpufreq_driver)
1644e00e56dfSRafael J. Wysocki 		return;
16451da177e4SLinus Torvalds 
16462f0aea93SViresh Kumar 	if (!has_target())
16472f0aea93SViresh Kumar 		return;
16481da177e4SLinus Torvalds 
16492f0aea93SViresh Kumar 	pr_debug("%s: Resuming Governors\n", __func__);
16502f0aea93SViresh Kumar 
16512f0aea93SViresh Kumar 	cpufreq_suspended = false;
16522f0aea93SViresh Kumar 
16532f0aea93SViresh Kumar 	list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
16542f0aea93SViresh Kumar 		if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
16552f0aea93SViresh Kumar 		    || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
16562f0aea93SViresh Kumar 			pr_err("%s: Failed to start governor for policy: %p\n",
16572f0aea93SViresh Kumar 				__func__, policy);
16582f0aea93SViresh Kumar 		else if (cpufreq_driver->resume
16592f0aea93SViresh Kumar 		    && cpufreq_driver->resume(policy))
16602f0aea93SViresh Kumar 			pr_err("%s: Failed to resume driver: %p\n", __func__,
16612f0aea93SViresh Kumar 				policy);
16622f0aea93SViresh Kumar 
16632f0aea93SViresh Kumar 		/*
16642f0aea93SViresh Kumar 		 * schedule call cpufreq_update_policy() for boot CPU, i.e. last
16652f0aea93SViresh Kumar 		 * policy in list. It will verify that the current freq is in
16662f0aea93SViresh Kumar 		 * sync with what we believe it to be.
16672f0aea93SViresh Kumar 		 */
16682f0aea93SViresh Kumar 		if (list_is_last(&policy->policy_list, &cpufreq_policy_list))
16693a3e9e06SViresh Kumar 			schedule_work(&policy->update);
16701da177e4SLinus Torvalds 	}
16712f0aea93SViresh Kumar }
16721da177e4SLinus Torvalds 
16739d95046eSBorislav Petkov /**
16749d95046eSBorislav Petkov  *	cpufreq_get_current_driver - return current driver's name
16759d95046eSBorislav Petkov  *
16769d95046eSBorislav Petkov  *	Return the name string of the currently loaded cpufreq driver
16779d95046eSBorislav Petkov  *	or NULL, if none.
16789d95046eSBorislav Petkov  */
16799d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
16809d95046eSBorislav Petkov {
16811c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
16821c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
16831c3d85ddSRafael J. Wysocki 
16841c3d85ddSRafael J. Wysocki 	return NULL;
16859d95046eSBorislav Petkov }
16869d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
16871da177e4SLinus Torvalds 
16881da177e4SLinus Torvalds /*********************************************************************
16891da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
16901da177e4SLinus Torvalds  *********************************************************************/
16911da177e4SLinus Torvalds 
16921da177e4SLinus Torvalds /**
16931da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
16941da177e4SLinus Torvalds  *	@nb: notifier function to register
16951da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
16961da177e4SLinus Torvalds  *
16971da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
16981da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
16991da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
17001da177e4SLinus Torvalds  *      changes in cpufreq policy.
17011da177e4SLinus Torvalds  *
17021da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1703e041c683SAlan Stern  *	blocking_notifier_chain_register.
17041da177e4SLinus Torvalds  */
17051da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
17061da177e4SLinus Torvalds {
17071da177e4SLinus Torvalds 	int ret;
17081da177e4SLinus Torvalds 
1709d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1710d5aaffa9SDirk Brandewie 		return -EINVAL;
1711d5aaffa9SDirk Brandewie 
171274212ca4SCesar Eduardo Barros 	WARN_ON(!init_cpufreq_transition_notifier_list_called);
171374212ca4SCesar Eduardo Barros 
17141da177e4SLinus Torvalds 	switch (list) {
17151da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1716b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1717e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
17181da177e4SLinus Torvalds 		break;
17191da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1720e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1721e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
17221da177e4SLinus Torvalds 		break;
17231da177e4SLinus Torvalds 	default:
17241da177e4SLinus Torvalds 		ret = -EINVAL;
17251da177e4SLinus Torvalds 	}
17261da177e4SLinus Torvalds 
17271da177e4SLinus Torvalds 	return ret;
17281da177e4SLinus Torvalds }
17291da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
17301da177e4SLinus Torvalds 
17311da177e4SLinus Torvalds /**
17321da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
17331da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
17341da177e4SLinus Torvalds  *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
17351da177e4SLinus Torvalds  *
17361da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
17371da177e4SLinus Torvalds  *
17381da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1739e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
17401da177e4SLinus Torvalds  */
17411da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
17421da177e4SLinus Torvalds {
17431da177e4SLinus Torvalds 	int ret;
17441da177e4SLinus Torvalds 
1745d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1746d5aaffa9SDirk Brandewie 		return -EINVAL;
1747d5aaffa9SDirk Brandewie 
17481da177e4SLinus Torvalds 	switch (list) {
17491da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1750b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
1751e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
17521da177e4SLinus Torvalds 		break;
17531da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1754e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1755e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
17561da177e4SLinus Torvalds 		break;
17571da177e4SLinus Torvalds 	default:
17581da177e4SLinus Torvalds 		ret = -EINVAL;
17591da177e4SLinus Torvalds 	}
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	return ret;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds 
17661da177e4SLinus Torvalds /*********************************************************************
17671da177e4SLinus Torvalds  *                              GOVERNORS                            *
17681da177e4SLinus Torvalds  *********************************************************************/
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
17711da177e4SLinus Torvalds 			    unsigned int target_freq,
17721da177e4SLinus Torvalds 			    unsigned int relation)
17731da177e4SLinus Torvalds {
17741da177e4SLinus Torvalds 	int retval = -EINVAL;
17757249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
1776c32b6b8eSAshok Raj 
1777a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1778a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1779a7b422cdSKonrad Rzeszutek Wilk 
17807249924eSViresh Kumar 	/* Make sure that target_freq is within supported range */
17817249924eSViresh Kumar 	if (target_freq > policy->max)
17827249924eSViresh Kumar 		target_freq = policy->max;
17837249924eSViresh Kumar 	if (target_freq < policy->min)
17847249924eSViresh Kumar 		target_freq = policy->min;
17857249924eSViresh Kumar 
17867249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
17877249924eSViresh Kumar 		 policy->cpu, target_freq, relation, old_target_freq);
17885a1c0228SViresh Kumar 
17899c0ebcf7SViresh Kumar 	/*
17909c0ebcf7SViresh Kumar 	 * This might look like a redundant call as we are checking it again
17919c0ebcf7SViresh Kumar 	 * after finding index. But it is left intentionally for cases where
17929c0ebcf7SViresh Kumar 	 * exactly same freq is called again and so we can save on few function
17939c0ebcf7SViresh Kumar 	 * calls.
17949c0ebcf7SViresh Kumar 	 */
17955a1c0228SViresh Kumar 	if (target_freq == policy->cur)
17965a1c0228SViresh Kumar 		return 0;
17975a1c0228SViresh Kumar 
17981c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
17991c3d85ddSRafael J. Wysocki 		retval = cpufreq_driver->target(policy, target_freq, relation);
18009c0ebcf7SViresh Kumar 	else if (cpufreq_driver->target_index) {
18019c0ebcf7SViresh Kumar 		struct cpufreq_frequency_table *freq_table;
1802d4019f0aSViresh Kumar 		struct cpufreq_freqs freqs;
1803d4019f0aSViresh Kumar 		bool notify;
18049c0ebcf7SViresh Kumar 		int index;
180590d45d17SAshok Raj 
18069c0ebcf7SViresh Kumar 		freq_table = cpufreq_frequency_get_table(policy->cpu);
18079c0ebcf7SViresh Kumar 		if (unlikely(!freq_table)) {
18089c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find freq_table\n", __func__);
18099c0ebcf7SViresh Kumar 			goto out;
18109c0ebcf7SViresh Kumar 		}
18119c0ebcf7SViresh Kumar 
18129c0ebcf7SViresh Kumar 		retval = cpufreq_frequency_table_target(policy, freq_table,
18139c0ebcf7SViresh Kumar 				target_freq, relation, &index);
18149c0ebcf7SViresh Kumar 		if (unlikely(retval)) {
18159c0ebcf7SViresh Kumar 			pr_err("%s: Unable to find matching freq\n", __func__);
18169c0ebcf7SViresh Kumar 			goto out;
18179c0ebcf7SViresh Kumar 		}
18189c0ebcf7SViresh Kumar 
1819d4019f0aSViresh Kumar 		if (freq_table[index].frequency == policy->cur) {
18209c0ebcf7SViresh Kumar 			retval = 0;
1821d4019f0aSViresh Kumar 			goto out;
1822d4019f0aSViresh Kumar 		}
1823d4019f0aSViresh Kumar 
1824d4019f0aSViresh Kumar 		notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1825d4019f0aSViresh Kumar 
1826d4019f0aSViresh Kumar 		if (notify) {
1827d4019f0aSViresh Kumar 			freqs.old = policy->cur;
1828d4019f0aSViresh Kumar 			freqs.new = freq_table[index].frequency;
1829d4019f0aSViresh Kumar 			freqs.flags = 0;
1830d4019f0aSViresh Kumar 
1831d4019f0aSViresh Kumar 			pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1832e837f9b5SJoe Perches 				 __func__, policy->cpu, freqs.old, freqs.new);
1833d4019f0aSViresh Kumar 
1834d4019f0aSViresh Kumar 			cpufreq_notify_transition(policy, &freqs,
1835d4019f0aSViresh Kumar 					CPUFREQ_PRECHANGE);
1836d4019f0aSViresh Kumar 		}
1837d4019f0aSViresh Kumar 
18389c0ebcf7SViresh Kumar 		retval = cpufreq_driver->target_index(policy, index);
1839d4019f0aSViresh Kumar 		if (retval)
1840d4019f0aSViresh Kumar 			pr_err("%s: Failed to change cpu frequency: %d\n",
1841d4019f0aSViresh Kumar 			       __func__, retval);
1842d4019f0aSViresh Kumar 
1843ab1b1c4eSViresh Kumar 		if (notify)
1844ab1b1c4eSViresh Kumar 			cpufreq_notify_post_transition(policy, &freqs, retval);
18459c0ebcf7SViresh Kumar 	}
18469c0ebcf7SViresh Kumar 
18479c0ebcf7SViresh Kumar out:
18481da177e4SLinus Torvalds 	return retval;
18491da177e4SLinus Torvalds }
18501da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
18511da177e4SLinus Torvalds 
18521da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
18531da177e4SLinus Torvalds 			  unsigned int target_freq,
18541da177e4SLinus Torvalds 			  unsigned int relation)
18551da177e4SLinus Torvalds {
1856f1829e4aSJulia Lawall 	int ret = -EINVAL;
18571da177e4SLinus Torvalds 
1858ad7722daSviresh kumar 	down_write(&policy->rwsem);
18591da177e4SLinus Torvalds 
18601da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
18611da177e4SLinus Torvalds 
1862ad7722daSviresh kumar 	up_write(&policy->rwsem);
18631da177e4SLinus Torvalds 
18641da177e4SLinus Torvalds 	return ret;
18651da177e4SLinus Torvalds }
18661da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
18671da177e4SLinus Torvalds 
1868153d7f3fSArjan van de Ven /*
1869153d7f3fSArjan van de Ven  * when "event" is CPUFREQ_GOV_LIMITS
1870153d7f3fSArjan van de Ven  */
18711da177e4SLinus Torvalds 
1872e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy,
1873e08f5f5bSGautham R Shenoy 					unsigned int event)
18741da177e4SLinus Torvalds {
1875cc993cabSDave Jones 	int ret;
18766afde10cSThomas Renninger 
18776afde10cSThomas Renninger 	/* Only must be defined when default governor is known to have latency
18786afde10cSThomas Renninger 	   restrictions, like e.g. conservative or ondemand.
18796afde10cSThomas Renninger 	   That this is the case is already ensured in Kconfig
18806afde10cSThomas Renninger 	*/
18816afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
18826afde10cSThomas Renninger 	struct cpufreq_governor *gov = &cpufreq_gov_performance;
18836afde10cSThomas Renninger #else
18846afde10cSThomas Renninger 	struct cpufreq_governor *gov = NULL;
18856afde10cSThomas Renninger #endif
18861c256245SThomas Renninger 
18872f0aea93SViresh Kumar 	/* Don't start any governor operations if we are entering suspend */
18882f0aea93SViresh Kumar 	if (cpufreq_suspended)
18892f0aea93SViresh Kumar 		return 0;
18902f0aea93SViresh Kumar 
18911c256245SThomas Renninger 	if (policy->governor->max_transition_latency &&
18921c256245SThomas Renninger 	    policy->cpuinfo.transition_latency >
18931c256245SThomas Renninger 	    policy->governor->max_transition_latency) {
18946afde10cSThomas Renninger 		if (!gov)
18956afde10cSThomas Renninger 			return -EINVAL;
18966afde10cSThomas Renninger 		else {
1897e837f9b5SJoe Perches 			pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
1898e837f9b5SJoe Perches 				policy->governor->name, gov->name);
18991c256245SThomas Renninger 			policy->governor = gov;
19001c256245SThomas Renninger 		}
19016afde10cSThomas Renninger 	}
19021da177e4SLinus Torvalds 
1903fe492f3fSViresh Kumar 	if (event == CPUFREQ_GOV_POLICY_INIT)
19041da177e4SLinus Torvalds 		if (!try_module_get(policy->governor->owner))
19051da177e4SLinus Torvalds 			return -EINVAL;
19061da177e4SLinus Torvalds 
19072d06d8c4SDominik Brodowski 	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1908e08f5f5bSGautham R Shenoy 		 policy->cpu, event);
190995731ebbSXiaoguang Chen 
191095731ebbSXiaoguang Chen 	mutex_lock(&cpufreq_governor_lock);
191156d07db2SSrivatsa S. Bhat 	if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
1912f73d3933SViresh Kumar 	    || (!policy->governor_enabled
1913f73d3933SViresh Kumar 	    && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
191495731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
191595731ebbSXiaoguang Chen 		return -EBUSY;
191695731ebbSXiaoguang Chen 	}
191795731ebbSXiaoguang Chen 
191895731ebbSXiaoguang Chen 	if (event == CPUFREQ_GOV_STOP)
191995731ebbSXiaoguang Chen 		policy->governor_enabled = false;
192095731ebbSXiaoguang Chen 	else if (event == CPUFREQ_GOV_START)
192195731ebbSXiaoguang Chen 		policy->governor_enabled = true;
192295731ebbSXiaoguang Chen 
192395731ebbSXiaoguang Chen 	mutex_unlock(&cpufreq_governor_lock);
192495731ebbSXiaoguang Chen 
19251da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
19261da177e4SLinus Torvalds 
19274d5dcc42SViresh Kumar 	if (!ret) {
19284d5dcc42SViresh Kumar 		if (event == CPUFREQ_GOV_POLICY_INIT)
19298e53695fSViresh Kumar 			policy->governor->initialized++;
19304d5dcc42SViresh Kumar 		else if (event == CPUFREQ_GOV_POLICY_EXIT)
19318e53695fSViresh Kumar 			policy->governor->initialized--;
193295731ebbSXiaoguang Chen 	} else {
193395731ebbSXiaoguang Chen 		/* Restore original values */
193495731ebbSXiaoguang Chen 		mutex_lock(&cpufreq_governor_lock);
193595731ebbSXiaoguang Chen 		if (event == CPUFREQ_GOV_STOP)
193695731ebbSXiaoguang Chen 			policy->governor_enabled = true;
193795731ebbSXiaoguang Chen 		else if (event == CPUFREQ_GOV_START)
193895731ebbSXiaoguang Chen 			policy->governor_enabled = false;
193995731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
19404d5dcc42SViresh Kumar 	}
1941b394058fSViresh Kumar 
1942fe492f3fSViresh Kumar 	if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1943fe492f3fSViresh Kumar 			((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
19441da177e4SLinus Torvalds 		module_put(policy->governor->owner);
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds 	return ret;
19471da177e4SLinus Torvalds }
19481da177e4SLinus Torvalds 
19491da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
19501da177e4SLinus Torvalds {
19513bcb09a3SJeremy Fitzhardinge 	int err;
19521da177e4SLinus Torvalds 
19531da177e4SLinus Torvalds 	if (!governor)
19541da177e4SLinus Torvalds 		return -EINVAL;
19551da177e4SLinus Torvalds 
1956a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1957a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1958a7b422cdSKonrad Rzeszutek Wilk 
19593fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
19601da177e4SLinus Torvalds 
1961b394058fSViresh Kumar 	governor->initialized = 0;
19623bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
19633bcb09a3SJeremy Fitzhardinge 	if (__find_governor(governor->name) == NULL) {
19643bcb09a3SJeremy Fitzhardinge 		err = 0;
19651da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
19663bcb09a3SJeremy Fitzhardinge 	}
19671da177e4SLinus Torvalds 
19683fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
19693bcb09a3SJeremy Fitzhardinge 	return err;
19701da177e4SLinus Torvalds }
19711da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
19721da177e4SLinus Torvalds 
19731da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
19741da177e4SLinus Torvalds {
197590e41bacSPrarit Bhargava 	int cpu;
197690e41bacSPrarit Bhargava 
19771da177e4SLinus Torvalds 	if (!governor)
19781da177e4SLinus Torvalds 		return;
19791da177e4SLinus Torvalds 
1980a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1981a7b422cdSKonrad Rzeszutek Wilk 		return;
1982a7b422cdSKonrad Rzeszutek Wilk 
198390e41bacSPrarit Bhargava 	for_each_present_cpu(cpu) {
198490e41bacSPrarit Bhargava 		if (cpu_online(cpu))
198590e41bacSPrarit Bhargava 			continue;
198690e41bacSPrarit Bhargava 		if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
198790e41bacSPrarit Bhargava 			strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
198890e41bacSPrarit Bhargava 	}
198990e41bacSPrarit Bhargava 
19903fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
19911da177e4SLinus Torvalds 	list_del(&governor->governor_list);
19923fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
19931da177e4SLinus Torvalds 	return;
19941da177e4SLinus Torvalds }
19951da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
19961da177e4SLinus Torvalds 
19971da177e4SLinus Torvalds 
19981da177e4SLinus Torvalds /*********************************************************************
19991da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
20001da177e4SLinus Torvalds  *********************************************************************/
20011da177e4SLinus Torvalds 
20021da177e4SLinus Torvalds /**
20031da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
200429464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
200529464f28SDave Jones  *	is written
20061da177e4SLinus Torvalds  *
20071da177e4SLinus Torvalds  * Reads the current cpufreq policy.
20081da177e4SLinus Torvalds  */
20091da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
20101da177e4SLinus Torvalds {
20111da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
20121da177e4SLinus Torvalds 	if (!policy)
20131da177e4SLinus Torvalds 		return -EINVAL;
20141da177e4SLinus Torvalds 
20151da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
20161da177e4SLinus Torvalds 	if (!cpu_policy)
20171da177e4SLinus Torvalds 		return -EINVAL;
20181da177e4SLinus Torvalds 
2019d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
20201da177e4SLinus Torvalds 
20211da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
20221da177e4SLinus Torvalds 	return 0;
20231da177e4SLinus Torvalds }
20241da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
20251da177e4SLinus Torvalds 
2026153d7f3fSArjan van de Ven /*
2027037ce839SViresh Kumar  * policy : current policy.
2028037ce839SViresh Kumar  * new_policy: policy to be set.
2029153d7f3fSArjan van de Ven  */
2030037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
20313a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy)
20321da177e4SLinus Torvalds {
2033d9a789c7SRafael J. Wysocki 	struct cpufreq_governor *old_gov;
2034d9a789c7SRafael J. Wysocki 	int ret;
20351da177e4SLinus Torvalds 
2036e837f9b5SJoe Perches 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2037e837f9b5SJoe Perches 		 new_policy->cpu, new_policy->min, new_policy->max);
20381da177e4SLinus Torvalds 
2039d5b73cd8SViresh Kumar 	memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
20401da177e4SLinus Torvalds 
2041d9a789c7SRafael J. Wysocki 	if (new_policy->min > policy->max || new_policy->max < policy->min)
2042d9a789c7SRafael J. Wysocki 		return -EINVAL;
20439c9a43edSMattia Dongili 
20441da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
20453a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
20461da177e4SLinus Torvalds 	if (ret)
2047d9a789c7SRafael J. Wysocki 		return ret;
20481da177e4SLinus Torvalds 
20491da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
2050e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
20513a3e9e06SViresh Kumar 			CPUFREQ_ADJUST, new_policy);
20521da177e4SLinus Torvalds 
20531da177e4SLinus Torvalds 	/* adjust if necessary - hardware incompatibility*/
2054e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
20553a3e9e06SViresh Kumar 			CPUFREQ_INCOMPATIBLE, new_policy);
20561da177e4SLinus Torvalds 
2057bb176f7dSViresh Kumar 	/*
2058bb176f7dSViresh Kumar 	 * verify the cpu speed can be set within this limit, which might be
2059bb176f7dSViresh Kumar 	 * different to the first one
2060bb176f7dSViresh Kumar 	 */
20613a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
2062e041c683SAlan Stern 	if (ret)
2063d9a789c7SRafael J. Wysocki 		return ret;
20641da177e4SLinus Torvalds 
20651da177e4SLinus Torvalds 	/* notification of the new policy */
2066e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
20673a3e9e06SViresh Kumar 			CPUFREQ_NOTIFY, new_policy);
20681da177e4SLinus Torvalds 
20693a3e9e06SViresh Kumar 	policy->min = new_policy->min;
20703a3e9e06SViresh Kumar 	policy->max = new_policy->max;
20711da177e4SLinus Torvalds 
20722d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
20733a3e9e06SViresh Kumar 		 policy->min, policy->max);
20741da177e4SLinus Torvalds 
20751c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
20763a3e9e06SViresh Kumar 		policy->policy = new_policy->policy;
20772d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
2078d9a789c7SRafael J. Wysocki 		return cpufreq_driver->setpolicy(new_policy);
2079d9a789c7SRafael J. Wysocki 	}
2080d9a789c7SRafael J. Wysocki 
2081d9a789c7SRafael J. Wysocki 	if (new_policy->governor == policy->governor)
2082d9a789c7SRafael J. Wysocki 		goto out;
20831da177e4SLinus Torvalds 
20842d06d8c4SDominik Brodowski 	pr_debug("governor switch\n");
20851da177e4SLinus Torvalds 
2086d9a789c7SRafael J. Wysocki 	/* save old, working values */
2087d9a789c7SRafael J. Wysocki 	old_gov = policy->governor;
20881da177e4SLinus Torvalds 	/* end old governor */
2089d9a789c7SRafael J. Wysocki 	if (old_gov) {
20903a3e9e06SViresh Kumar 		__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2091ad7722daSviresh kumar 		up_write(&policy->rwsem);
2092d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy,CPUFREQ_GOV_POLICY_EXIT);
2093ad7722daSviresh kumar 		down_write(&policy->rwsem);
20947bd353a9SViresh Kumar 	}
20951da177e4SLinus Torvalds 
20961da177e4SLinus Torvalds 	/* start new governor */
20973a3e9e06SViresh Kumar 	policy->governor = new_policy->governor;
20983a3e9e06SViresh Kumar 	if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2099d9a789c7SRafael J. Wysocki 		if (!__cpufreq_governor(policy, CPUFREQ_GOV_START))
2100d9a789c7SRafael J. Wysocki 			goto out;
2101d9a789c7SRafael J. Wysocki 
2102ad7722daSviresh kumar 		up_write(&policy->rwsem);
2103d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2104ad7722daSviresh kumar 		down_write(&policy->rwsem);
2105955ef483SViresh Kumar 	}
21067bd353a9SViresh Kumar 
21071da177e4SLinus Torvalds 	/* new governor failed, so re-start old one */
2108d9a789c7SRafael J. Wysocki 	pr_debug("starting governor %s failed\n", policy->governor->name);
21091da177e4SLinus Torvalds 	if (old_gov) {
21103a3e9e06SViresh Kumar 		policy->governor = old_gov;
2111d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2112d9a789c7SRafael J. Wysocki 		__cpufreq_governor(policy, CPUFREQ_GOV_START);
21131da177e4SLinus Torvalds 	}
21141da177e4SLinus Torvalds 
2115d9a789c7SRafael J. Wysocki 	return -EINVAL;
2116d9a789c7SRafael J. Wysocki 
2117d9a789c7SRafael J. Wysocki  out:
2118d9a789c7SRafael J. Wysocki 	pr_debug("governor: change or update limits\n");
2119d9a789c7SRafael J. Wysocki 	return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
21201da177e4SLinus Torvalds }
21211da177e4SLinus Torvalds 
21221da177e4SLinus Torvalds /**
21231da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
21241da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
21251da177e4SLinus Torvalds  *
212625985edcSLucas De Marchi  *	Useful for policy notifiers which have different necessities
21271da177e4SLinus Torvalds  *	at different times.
21281da177e4SLinus Torvalds  */
21291da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
21301da177e4SLinus Torvalds {
21313a3e9e06SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
21323a3e9e06SViresh Kumar 	struct cpufreq_policy new_policy;
2133f1829e4aSJulia Lawall 	int ret;
21341da177e4SLinus Torvalds 
21353a3e9e06SViresh Kumar 	if (!policy) {
2136f1829e4aSJulia Lawall 		ret = -ENODEV;
2137f1829e4aSJulia Lawall 		goto no_policy;
2138f1829e4aSJulia Lawall 	}
21391da177e4SLinus Torvalds 
2140ad7722daSviresh kumar 	down_write(&policy->rwsem);
21411da177e4SLinus Torvalds 
21422d06d8c4SDominik Brodowski 	pr_debug("updating policy for CPU %u\n", cpu);
2143d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
21443a3e9e06SViresh Kumar 	new_policy.min = policy->user_policy.min;
21453a3e9e06SViresh Kumar 	new_policy.max = policy->user_policy.max;
21463a3e9e06SViresh Kumar 	new_policy.policy = policy->user_policy.policy;
21473a3e9e06SViresh Kumar 	new_policy.governor = policy->user_policy.governor;
21481da177e4SLinus Torvalds 
2149bb176f7dSViresh Kumar 	/*
2150bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
2151bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
2152bb176f7dSViresh Kumar 	 */
21531c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
21543a3e9e06SViresh Kumar 		new_policy.cur = cpufreq_driver->get(cpu);
2155bd0fa9bbSViresh Kumar 		if (WARN_ON(!new_policy.cur)) {
2156bd0fa9bbSViresh Kumar 			ret = -EIO;
2157bd0fa9bbSViresh Kumar 			goto no_policy;
2158bd0fa9bbSViresh Kumar 		}
2159bd0fa9bbSViresh Kumar 
21603a3e9e06SViresh Kumar 		if (!policy->cur) {
2161e837f9b5SJoe Perches 			pr_debug("Driver did not initialize current freq\n");
21623a3e9e06SViresh Kumar 			policy->cur = new_policy.cur;
2163a85f7bd3SThomas Renninger 		} else {
21649c0ebcf7SViresh Kumar 			if (policy->cur != new_policy.cur && has_target())
21653a3e9e06SViresh Kumar 				cpufreq_out_of_sync(cpu, policy->cur,
21663a3e9e06SViresh Kumar 								new_policy.cur);
21670961dd0dSThomas Renninger 		}
2168a85f7bd3SThomas Renninger 	}
21690961dd0dSThomas Renninger 
2170037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
21711da177e4SLinus Torvalds 
2172ad7722daSviresh kumar 	up_write(&policy->rwsem);
21735a01f2e8SVenkatesh Pallipadi 
21743a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
2175f1829e4aSJulia Lawall no_policy:
21761da177e4SLinus Torvalds 	return ret;
21771da177e4SLinus Torvalds }
21781da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
21791da177e4SLinus Torvalds 
21802760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb,
2181c32b6b8eSAshok Raj 					unsigned long action, void *hcpu)
2182c32b6b8eSAshok Raj {
2183c32b6b8eSAshok Raj 	unsigned int cpu = (unsigned long)hcpu;
21848a25a2fdSKay Sievers 	struct device *dev;
21855302c3fbSSrivatsa S. Bhat 	bool frozen = false;
2186c32b6b8eSAshok Raj 
21878a25a2fdSKay Sievers 	dev = get_cpu_device(cpu);
21888a25a2fdSKay Sievers 	if (dev) {
21895302c3fbSSrivatsa S. Bhat 
2190d4faadd5SRafael J. Wysocki 		if (action & CPU_TASKS_FROZEN)
2191d4faadd5SRafael J. Wysocki 			frozen = true;
2192d4faadd5SRafael J. Wysocki 
21935302c3fbSSrivatsa S. Bhat 		switch (action & ~CPU_TASKS_FROZEN) {
2194c32b6b8eSAshok Raj 		case CPU_ONLINE:
21955302c3fbSSrivatsa S. Bhat 			__cpufreq_add_dev(dev, NULL, frozen);
2196c32b6b8eSAshok Raj 			break;
21975302c3fbSSrivatsa S. Bhat 
2198c32b6b8eSAshok Raj 		case CPU_DOWN_PREPARE:
2199cedb70afSSrivatsa S. Bhat 			__cpufreq_remove_dev_prepare(dev, NULL, frozen);
22001aee40acSSrivatsa S. Bhat 			break;
22011aee40acSSrivatsa S. Bhat 
22021aee40acSSrivatsa S. Bhat 		case CPU_POST_DEAD:
2203cedb70afSSrivatsa S. Bhat 			__cpufreq_remove_dev_finish(dev, NULL, frozen);
2204c32b6b8eSAshok Raj 			break;
22055302c3fbSSrivatsa S. Bhat 
22065a01f2e8SVenkatesh Pallipadi 		case CPU_DOWN_FAILED:
22075302c3fbSSrivatsa S. Bhat 			__cpufreq_add_dev(dev, NULL, frozen);
2208c32b6b8eSAshok Raj 			break;
2209c32b6b8eSAshok Raj 		}
2210c32b6b8eSAshok Raj 	}
2211c32b6b8eSAshok Raj 	return NOTIFY_OK;
2212c32b6b8eSAshok Raj }
2213c32b6b8eSAshok Raj 
22149c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = {
2215c32b6b8eSAshok Raj 	.notifier_call = cpufreq_cpu_callback,
2216c32b6b8eSAshok Raj };
22171da177e4SLinus Torvalds 
22181da177e4SLinus Torvalds /*********************************************************************
22196f19efc0SLukasz Majewski  *               BOOST						     *
22206f19efc0SLukasz Majewski  *********************************************************************/
22216f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state)
22226f19efc0SLukasz Majewski {
22236f19efc0SLukasz Majewski 	struct cpufreq_frequency_table *freq_table;
22246f19efc0SLukasz Majewski 	struct cpufreq_policy *policy;
22256f19efc0SLukasz Majewski 	int ret = -EINVAL;
22266f19efc0SLukasz Majewski 
22276f19efc0SLukasz Majewski 	list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
22286f19efc0SLukasz Majewski 		freq_table = cpufreq_frequency_get_table(policy->cpu);
22296f19efc0SLukasz Majewski 		if (freq_table) {
22306f19efc0SLukasz Majewski 			ret = cpufreq_frequency_table_cpuinfo(policy,
22316f19efc0SLukasz Majewski 							freq_table);
22326f19efc0SLukasz Majewski 			if (ret) {
22336f19efc0SLukasz Majewski 				pr_err("%s: Policy frequency update failed\n",
22346f19efc0SLukasz Majewski 				       __func__);
22356f19efc0SLukasz Majewski 				break;
22366f19efc0SLukasz Majewski 			}
22376f19efc0SLukasz Majewski 			policy->user_policy.max = policy->max;
22386f19efc0SLukasz Majewski 			__cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
22396f19efc0SLukasz Majewski 		}
22406f19efc0SLukasz Majewski 	}
22416f19efc0SLukasz Majewski 
22426f19efc0SLukasz Majewski 	return ret;
22436f19efc0SLukasz Majewski }
22446f19efc0SLukasz Majewski 
22456f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state)
22466f19efc0SLukasz Majewski {
22476f19efc0SLukasz Majewski 	unsigned long flags;
22486f19efc0SLukasz Majewski 	int ret = 0;
22496f19efc0SLukasz Majewski 
22506f19efc0SLukasz Majewski 	if (cpufreq_driver->boost_enabled == state)
22516f19efc0SLukasz Majewski 		return 0;
22526f19efc0SLukasz Majewski 
22536f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
22546f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = state;
22556f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
22566f19efc0SLukasz Majewski 
22576f19efc0SLukasz Majewski 	ret = cpufreq_driver->set_boost(state);
22586f19efc0SLukasz Majewski 	if (ret) {
22596f19efc0SLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
22606f19efc0SLukasz Majewski 		cpufreq_driver->boost_enabled = !state;
22616f19efc0SLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
22626f19efc0SLukasz Majewski 
2263e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST\n",
2264e837f9b5SJoe Perches 		       __func__, state ? "enable" : "disable");
22656f19efc0SLukasz Majewski 	}
22666f19efc0SLukasz Majewski 
22676f19efc0SLukasz Majewski 	return ret;
22686f19efc0SLukasz Majewski }
22696f19efc0SLukasz Majewski 
22706f19efc0SLukasz Majewski int cpufreq_boost_supported(void)
22716f19efc0SLukasz Majewski {
22726f19efc0SLukasz Majewski 	if (likely(cpufreq_driver))
22736f19efc0SLukasz Majewski 		return cpufreq_driver->boost_supported;
22746f19efc0SLukasz Majewski 
22756f19efc0SLukasz Majewski 	return 0;
22766f19efc0SLukasz Majewski }
22776f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
22786f19efc0SLukasz Majewski 
22796f19efc0SLukasz Majewski int cpufreq_boost_enabled(void)
22806f19efc0SLukasz Majewski {
22816f19efc0SLukasz Majewski 	return cpufreq_driver->boost_enabled;
22826f19efc0SLukasz Majewski }
22836f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
22846f19efc0SLukasz Majewski 
22856f19efc0SLukasz Majewski /*********************************************************************
22861da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
22871da177e4SLinus Torvalds  *********************************************************************/
22881da177e4SLinus Torvalds 
22891da177e4SLinus Torvalds /**
22901da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
22911da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
22921da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
22931da177e4SLinus Torvalds  *
22941da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
22951da177e4SLinus Torvalds  * returns zero on success, -EBUSY when another driver got here first
22961da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
22971da177e4SLinus Torvalds  *
22981da177e4SLinus Torvalds  */
2299221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
23001da177e4SLinus Torvalds {
23011da177e4SLinus Torvalds 	unsigned long flags;
23021da177e4SLinus Torvalds 	int ret;
23031da177e4SLinus Torvalds 
2304a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2305a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2306a7b422cdSKonrad Rzeszutek Wilk 
23071da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
23089c0ebcf7SViresh Kumar 	    !(driver_data->setpolicy || driver_data->target_index ||
23099c0ebcf7SViresh Kumar 		    driver_data->target))
23101da177e4SLinus Torvalds 		return -EINVAL;
23111da177e4SLinus Torvalds 
23122d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
23131da177e4SLinus Torvalds 
23141da177e4SLinus Torvalds 	if (driver_data->setpolicy)
23151da177e4SLinus Torvalds 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
23161da177e4SLinus Torvalds 
23170d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
23181c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
23190d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23204dea5806SYinghai Lu 		return -EEXIST;
23211da177e4SLinus Torvalds 	}
23221c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
23230d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23241da177e4SLinus Torvalds 
23256f19efc0SLukasz Majewski 	if (cpufreq_boost_supported()) {
23266f19efc0SLukasz Majewski 		/*
23276f19efc0SLukasz Majewski 		 * Check if driver provides function to enable boost -
23286f19efc0SLukasz Majewski 		 * if not, use cpufreq_boost_set_sw as default
23296f19efc0SLukasz Majewski 		 */
23306f19efc0SLukasz Majewski 		if (!cpufreq_driver->set_boost)
23316f19efc0SLukasz Majewski 			cpufreq_driver->set_boost = cpufreq_boost_set_sw;
23326f19efc0SLukasz Majewski 
23336f19efc0SLukasz Majewski 		ret = cpufreq_sysfs_create_file(&boost.attr);
23346f19efc0SLukasz Majewski 		if (ret) {
23356f19efc0SLukasz Majewski 			pr_err("%s: cannot register global BOOST sysfs file\n",
23366f19efc0SLukasz Majewski 			       __func__);
23376f19efc0SLukasz Majewski 			goto err_null_driver;
23386f19efc0SLukasz Majewski 		}
23396f19efc0SLukasz Majewski 	}
23406f19efc0SLukasz Majewski 
23418a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
23428f5bc2abSJiri Slaby 	if (ret)
23436f19efc0SLukasz Majewski 		goto err_boost_unreg;
23441da177e4SLinus Torvalds 
23451c3d85ddSRafael J. Wysocki 	if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
23461da177e4SLinus Torvalds 		int i;
23471da177e4SLinus Torvalds 		ret = -ENODEV;
23481da177e4SLinus Torvalds 
23491da177e4SLinus Torvalds 		/* check for at least one working CPU */
23507a6aedfaSMike Travis 		for (i = 0; i < nr_cpu_ids; i++)
23517a6aedfaSMike Travis 			if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
23521da177e4SLinus Torvalds 				ret = 0;
23537a6aedfaSMike Travis 				break;
23547a6aedfaSMike Travis 			}
23551da177e4SLinus Torvalds 
23561da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
23571da177e4SLinus Torvalds 		if (ret) {
23582d06d8c4SDominik Brodowski 			pr_debug("no CPU initialized for driver %s\n",
2359e08f5f5bSGautham R Shenoy 				 driver_data->name);
23608a25a2fdSKay Sievers 			goto err_if_unreg;
23611da177e4SLinus Torvalds 		}
23621da177e4SLinus Torvalds 	}
23631da177e4SLinus Torvalds 
236465edc68cSChandra Seetharaman 	register_hotcpu_notifier(&cpufreq_cpu_notifier);
23652d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
23661da177e4SLinus Torvalds 
23678f5bc2abSJiri Slaby 	return 0;
23688a25a2fdSKay Sievers err_if_unreg:
23698a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
23706f19efc0SLukasz Majewski err_boost_unreg:
23716f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
23726f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
23738f5bc2abSJiri Slaby err_null_driver:
23740d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
23751c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
23760d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23774d34a67dSDave Jones 	return ret;
23781da177e4SLinus Torvalds }
23791da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
23801da177e4SLinus Torvalds 
23811da177e4SLinus Torvalds /**
23821da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
23831da177e4SLinus Torvalds  *
23841da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
23851da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
23861da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
23871da177e4SLinus Torvalds  * currently not initialised.
23881da177e4SLinus Torvalds  */
2389221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
23901da177e4SLinus Torvalds {
23911da177e4SLinus Torvalds 	unsigned long flags;
23921da177e4SLinus Torvalds 
23931c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
23941da177e4SLinus Torvalds 		return -EINVAL;
23951da177e4SLinus Torvalds 
23962d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
23971da177e4SLinus Torvalds 
23988a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
23996f19efc0SLukasz Majewski 	if (cpufreq_boost_supported())
24006f19efc0SLukasz Majewski 		cpufreq_sysfs_remove_file(&boost.attr);
24016f19efc0SLukasz Majewski 
240265edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
24031da177e4SLinus Torvalds 
24046eed9404SViresh Kumar 	down_write(&cpufreq_rwsem);
24050d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
24066eed9404SViresh Kumar 
24071c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
24086eed9404SViresh Kumar 
24090d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24106eed9404SViresh Kumar 	up_write(&cpufreq_rwsem);
24111da177e4SLinus Torvalds 
24121da177e4SLinus Torvalds 	return 0;
24131da177e4SLinus Torvalds }
24141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
24155a01f2e8SVenkatesh Pallipadi 
24165a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
24175a01f2e8SVenkatesh Pallipadi {
2418a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2419a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2420a7b422cdSKonrad Rzeszutek Wilk 
24212361be23SViresh Kumar 	cpufreq_global_kobject = kobject_create();
24228aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
24238aa84ad8SThomas Renninger 
24245a01f2e8SVenkatesh Pallipadi 	return 0;
24255a01f2e8SVenkatesh Pallipadi }
24265a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2427