xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision 9515f4d69b92feafe37581047a1bb41e41602faa)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/drivers/cpufreq/cpufreq.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2001 Russell King
51da177e4SLinus Torvalds  *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6bb176f7dSViresh Kumar  *            (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
71da177e4SLinus Torvalds  *
8c32b6b8eSAshok Raj  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9c32b6b8eSAshok Raj  *	Added handling for CPU hotplug
108ff69732SDave Jones  *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
118ff69732SDave Jones  *	Fix handling for CPU hotplug -- affected CPUs
12c32b6b8eSAshok Raj  *
131da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
141da177e4SLinus Torvalds  * it under the terms of the GNU General Public License version 2 as
151da177e4SLinus Torvalds  * published by the Free Software Foundation.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
18db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19db701151SViresh Kumar 
205ff0a268SViresh Kumar #include <linux/cpu.h>
211da177e4SLinus Torvalds #include <linux/cpufreq.h>
221da177e4SLinus Torvalds #include <linux/delay.h>
231da177e4SLinus Torvalds #include <linux/device.h>
245ff0a268SViresh Kumar #include <linux/init.h>
255ff0a268SViresh Kumar #include <linux/kernel_stat.h>
265ff0a268SViresh Kumar #include <linux/module.h>
273fc54d37Sakpm@osdl.org #include <linux/mutex.h>
285ff0a268SViresh Kumar #include <linux/slab.h>
29e00e56dfSRafael J. Wysocki #include <linux/syscore_ops.h>
305ff0a268SViresh Kumar #include <linux/tick.h>
316f4f2723SThomas Renninger #include <trace/events/power.h>
326f4f2723SThomas Renninger 
331da177e4SLinus Torvalds /**
34cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
351da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
361da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
371da177e4SLinus Torvalds  */
381c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
397a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
408414809cSSrivatsa S. Bhat static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
41bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
42bb176f7dSViresh Kumar static DEFINE_MUTEX(cpufreq_governor_lock);
43c88a1f8bSLukasz Majewski static LIST_HEAD(cpufreq_policy_list);
44bb176f7dSViresh Kumar 
45084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU
46084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */
47e77b89f1SDmitry Monakhov static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
48084f3493SThomas Renninger #endif
491da177e4SLinus Torvalds 
505a01f2e8SVenkatesh Pallipadi /*
515a01f2e8SVenkatesh Pallipadi  * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
525a01f2e8SVenkatesh Pallipadi  * all cpufreq/hotplug/workqueue/etc related lock issues.
535a01f2e8SVenkatesh Pallipadi  *
545a01f2e8SVenkatesh Pallipadi  * The rules for this semaphore:
555a01f2e8SVenkatesh Pallipadi  * - Any routine that wants to read from the policy structure will
565a01f2e8SVenkatesh Pallipadi  *   do a down_read on this semaphore.
575a01f2e8SVenkatesh Pallipadi  * - Any routine that will write to the policy structure and/or may take away
585a01f2e8SVenkatesh Pallipadi  *   the policy altogether (eg. CPU hotplug), will hold this lock in write
595a01f2e8SVenkatesh Pallipadi  *   mode before doing so.
605a01f2e8SVenkatesh Pallipadi  *
615a01f2e8SVenkatesh Pallipadi  * Additional rules:
625a01f2e8SVenkatesh Pallipadi  * - Governor routines that can be called in cpufreq hotplug path should not
635a01f2e8SVenkatesh Pallipadi  *   take this sem as top level hotplug notifier handler takes this.
64395913d0SMathieu Desnoyers  * - Lock should not be held across
65395913d0SMathieu Desnoyers  *     __cpufreq_governor(data, CPUFREQ_GOV_STOP);
665a01f2e8SVenkatesh Pallipadi  */
67f1625066STejun Heo static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
685a01f2e8SVenkatesh Pallipadi static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
695a01f2e8SVenkatesh Pallipadi 
705a01f2e8SVenkatesh Pallipadi #define lock_policy_rwsem(mode, cpu)					\
71fa1d8af4SViresh Kumar static int lock_policy_rwsem_##mode(int cpu)				\
725a01f2e8SVenkatesh Pallipadi {									\
73f1625066STejun Heo 	int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);		\
745a01f2e8SVenkatesh Pallipadi 	BUG_ON(policy_cpu == -1);					\
755a01f2e8SVenkatesh Pallipadi 	down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));		\
765a01f2e8SVenkatesh Pallipadi 									\
775a01f2e8SVenkatesh Pallipadi 	return 0;							\
785a01f2e8SVenkatesh Pallipadi }
795a01f2e8SVenkatesh Pallipadi 
805a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(read, cpu);
815a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(write, cpu);
825a01f2e8SVenkatesh Pallipadi 
83fa1d8af4SViresh Kumar #define unlock_policy_rwsem(mode, cpu)					\
84fa1d8af4SViresh Kumar static void unlock_policy_rwsem_##mode(int cpu)				\
85fa1d8af4SViresh Kumar {									\
86fa1d8af4SViresh Kumar 	int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);		\
87fa1d8af4SViresh Kumar 	BUG_ON(policy_cpu == -1);					\
88fa1d8af4SViresh Kumar 	up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));		\
895a01f2e8SVenkatesh Pallipadi }
905a01f2e8SVenkatesh Pallipadi 
91fa1d8af4SViresh Kumar unlock_policy_rwsem(read, cpu);
92fa1d8af4SViresh Kumar unlock_policy_rwsem(write, cpu);
935a01f2e8SVenkatesh Pallipadi 
946eed9404SViresh Kumar /*
956eed9404SViresh Kumar  * rwsem to guarantee that cpufreq driver module doesn't unload during critical
966eed9404SViresh Kumar  * sections
976eed9404SViresh Kumar  */
986eed9404SViresh Kumar static DECLARE_RWSEM(cpufreq_rwsem);
996eed9404SViresh Kumar 
1001da177e4SLinus Torvalds /* internal prototypes */
10129464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy,
10229464f28SDave Jones 		unsigned int event);
1035a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu);
10465f27f38SDavid Howells static void handle_update(struct work_struct *work);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /**
1071da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
1081da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
1091da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
1101da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
1111da177e4SLinus Torvalds  * The mutex locks both lists.
1121da177e4SLinus Torvalds  */
113e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
114b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list;
1151da177e4SLinus Torvalds 
11674212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called;
117b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void)
118b4dfdbb3SAlan Stern {
119b4dfdbb3SAlan Stern 	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
12074212ca4SCesar Eduardo Barros 	init_cpufreq_transition_notifier_list_called = true;
121b4dfdbb3SAlan Stern 	return 0;
122b4dfdbb3SAlan Stern }
123b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list);
1241da177e4SLinus Torvalds 
125a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
126da584455SViresh Kumar static int cpufreq_disabled(void)
127a7b422cdSKonrad Rzeszutek Wilk {
128a7b422cdSKonrad Rzeszutek Wilk 	return off;
129a7b422cdSKonrad Rzeszutek Wilk }
130a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
131a7b422cdSKonrad Rzeszutek Wilk {
132a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
133a7b422cdSKonrad Rzeszutek Wilk }
1341da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list);
1353fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
1361da177e4SLinus Torvalds 
1374d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1384d5dcc42SViresh Kumar {
1391c3d85ddSRafael J. Wysocki 	return cpufreq_driver->have_governor_per_policy;
1404d5dcc42SViresh Kumar }
1413f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1424d5dcc42SViresh Kumar 
143944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
144944e9a03SViresh Kumar {
145944e9a03SViresh Kumar 	if (have_governor_per_policy())
146944e9a03SViresh Kumar 		return &policy->kobj;
147944e9a03SViresh Kumar 	else
148944e9a03SViresh Kumar 		return cpufreq_global_kobject;
149944e9a03SViresh Kumar }
150944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
151944e9a03SViresh Kumar 
15272a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
15372a4ce34SViresh Kumar {
15472a4ce34SViresh Kumar 	u64 idle_time;
15572a4ce34SViresh Kumar 	u64 cur_wall_time;
15672a4ce34SViresh Kumar 	u64 busy_time;
15772a4ce34SViresh Kumar 
15872a4ce34SViresh Kumar 	cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
15972a4ce34SViresh Kumar 
16072a4ce34SViresh Kumar 	busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
16172a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
16272a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
16372a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
16472a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
16572a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
16672a4ce34SViresh Kumar 
16772a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
16872a4ce34SViresh Kumar 	if (wall)
16972a4ce34SViresh Kumar 		*wall = cputime_to_usecs(cur_wall_time);
17072a4ce34SViresh Kumar 
17172a4ce34SViresh Kumar 	return cputime_to_usecs(idle_time);
17272a4ce34SViresh Kumar }
17372a4ce34SViresh Kumar 
17472a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
17572a4ce34SViresh Kumar {
17672a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
17772a4ce34SViresh Kumar 
17872a4ce34SViresh Kumar 	if (idle_time == -1ULL)
17972a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
18072a4ce34SViresh Kumar 	else if (!io_busy)
18172a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
18272a4ce34SViresh Kumar 
18372a4ce34SViresh Kumar 	return idle_time;
18472a4ce34SViresh Kumar }
18572a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
18672a4ce34SViresh Kumar 
1876eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
1881da177e4SLinus Torvalds {
1896eed9404SViresh Kumar 	struct cpufreq_policy *policy = NULL;
1901da177e4SLinus Torvalds 	unsigned long flags;
1911da177e4SLinus Torvalds 
1926eed9404SViresh Kumar 	if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
1936eed9404SViresh Kumar 		return NULL;
1946eed9404SViresh Kumar 
1956eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
1966eed9404SViresh Kumar 		return NULL;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	/* get the cpufreq driver */
1990d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
2001da177e4SLinus Torvalds 
2016eed9404SViresh Kumar 	if (cpufreq_driver) {
2021da177e4SLinus Torvalds 		/* get the CPU */
2033a3e9e06SViresh Kumar 		policy = per_cpu(cpufreq_cpu_data, cpu);
2046eed9404SViresh Kumar 		if (policy)
2056eed9404SViresh Kumar 			kobject_get(&policy->kobj);
2066eed9404SViresh Kumar 	}
2076eed9404SViresh Kumar 
2086eed9404SViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2091da177e4SLinus Torvalds 
2103a3e9e06SViresh Kumar 	if (!policy)
2116eed9404SViresh Kumar 		up_read(&cpufreq_rwsem);
2121da177e4SLinus Torvalds 
2133a3e9e06SViresh Kumar 	return policy;
214a9144436SStephen Boyd }
2151da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2161da177e4SLinus Torvalds 
2173a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
218a9144436SStephen Boyd {
219d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
220d5aaffa9SDirk Brandewie 		return;
221d5aaffa9SDirk Brandewie 
2226eed9404SViresh Kumar 	kobject_put(&policy->kobj);
2236eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
224a9144436SStephen Boyd }
2251da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds /*********************************************************************
2281da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2291da177e4SLinus Torvalds  *********************************************************************/
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds /**
2321da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
2331da177e4SLinus Torvalds  *
2341da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
2351da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
2361da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
2371da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
2381da177e4SLinus Torvalds  */
2391da177e4SLinus Torvalds #ifndef CONFIG_SMP
2401da177e4SLinus Torvalds static unsigned long l_p_j_ref;
2411da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq;
2421da177e4SLinus Torvalds 
243858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
2461da177e4SLinus Torvalds 		return;
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
2491da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
2501da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
2512d06d8c4SDominik Brodowski 		pr_debug("saving %lu as reference value for loops_per_jiffy; "
252e08f5f5bSGautham R Shenoy 			"freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
2531da177e4SLinus Torvalds 	}
254d08de0c1SAfzal Mohammed 	if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
25542d4dc3fSBenjamin Herrenschmidt 	    (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
256e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
257e08f5f5bSGautham R Shenoy 								ci->new);
2582d06d8c4SDominik Brodowski 		pr_debug("scaling loops_per_jiffy to %lu "
259e08f5f5bSGautham R Shenoy 			"for frequency %u kHz\n", loops_per_jiffy, ci->new);
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds }
2621da177e4SLinus Torvalds #else
263e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
264e08f5f5bSGautham R Shenoy {
265e08f5f5bSGautham R Shenoy 	return;
266e08f5f5bSGautham R Shenoy }
2671da177e4SLinus Torvalds #endif
2681da177e4SLinus Torvalds 
2690956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
270b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
2711da177e4SLinus Torvalds {
2721da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
2731da177e4SLinus Torvalds 
274d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
275d5aaffa9SDirk Brandewie 		return;
276d5aaffa9SDirk Brandewie 
2771c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
2782d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
279e4472cb3SDave Jones 		state, freqs->new);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 	switch (state) {
282e4472cb3SDave Jones 
2831da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
284266c13d7SViresh Kumar 		if (WARN(policy->transition_ongoing ==
285266c13d7SViresh Kumar 					cpumask_weight(policy->cpus),
2867c30ed53SViresh Kumar 				"In middle of another frequency transition\n"))
2877c30ed53SViresh Kumar 			return;
2887c30ed53SViresh Kumar 
289266c13d7SViresh Kumar 		policy->transition_ongoing++;
2907c30ed53SViresh Kumar 
291e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
292e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
293e4472cb3SDave Jones 		 * "old frequency".
2941da177e4SLinus Torvalds 		 */
2951c3d85ddSRafael J. Wysocki 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
296e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
297e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
2982d06d8c4SDominik Brodowski 				pr_debug("Warning: CPU frequency is"
299e4472cb3SDave Jones 					" %u, cpufreq assumed %u kHz.\n",
300e4472cb3SDave Jones 					freqs->old, policy->cur);
301e4472cb3SDave Jones 				freqs->old = policy->cur;
3021da177e4SLinus Torvalds 			}
3031da177e4SLinus Torvalds 		}
304b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
305e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
3061da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3071da177e4SLinus Torvalds 		break;
308e4472cb3SDave Jones 
3091da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3107c30ed53SViresh Kumar 		if (WARN(!policy->transition_ongoing,
3117c30ed53SViresh Kumar 				"No frequency transition in progress\n"))
3127c30ed53SViresh Kumar 			return;
3137c30ed53SViresh Kumar 
314266c13d7SViresh Kumar 		policy->transition_ongoing--;
3157c30ed53SViresh Kumar 
3161da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
3172d06d8c4SDominik Brodowski 		pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
3186f4f2723SThomas Renninger 			(unsigned long)freqs->cpu);
31925e41933SThomas Renninger 		trace_cpu_frequency(freqs->new, freqs->cpu);
320b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
321e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
322e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
323e4472cb3SDave Jones 			policy->cur = freqs->new;
3241da177e4SLinus Torvalds 		break;
3251da177e4SLinus Torvalds 	}
3261da177e4SLinus Torvalds }
327bb176f7dSViresh Kumar 
328b43a7ffbSViresh Kumar /**
329b43a7ffbSViresh Kumar  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
330b43a7ffbSViresh Kumar  * on frequency transition.
331b43a7ffbSViresh Kumar  *
332b43a7ffbSViresh Kumar  * This function calls the transition notifiers and the "adjust_jiffies"
333b43a7ffbSViresh Kumar  * function. It is called twice on all CPU frequency changes that have
334b43a7ffbSViresh Kumar  * external effects.
335b43a7ffbSViresh Kumar  */
336b43a7ffbSViresh Kumar void cpufreq_notify_transition(struct cpufreq_policy *policy,
337b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
338b43a7ffbSViresh Kumar {
339b43a7ffbSViresh Kumar 	for_each_cpu(freqs->cpu, policy->cpus)
340b43a7ffbSViresh Kumar 		__cpufreq_notify_transition(policy, freqs, state);
341b43a7ffbSViresh Kumar }
3421da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds /*********************************************************************
3461da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
3471da177e4SLinus Torvalds  *********************************************************************/
3481da177e4SLinus Torvalds 
3493bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor)
3503bcb09a3SJeremy Fitzhardinge {
3513bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
3523bcb09a3SJeremy Fitzhardinge 
3533bcb09a3SJeremy Fitzhardinge 	list_for_each_entry(t, &cpufreq_governor_list, governor_list)
3543bcb09a3SJeremy Fitzhardinge 		if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
3553bcb09a3SJeremy Fitzhardinge 			return t;
3563bcb09a3SJeremy Fitzhardinge 
3573bcb09a3SJeremy Fitzhardinge 	return NULL;
3583bcb09a3SJeremy Fitzhardinge }
3593bcb09a3SJeremy Fitzhardinge 
3601da177e4SLinus Torvalds /**
3611da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
3621da177e4SLinus Torvalds  */
3631da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
3641da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
3651da177e4SLinus Torvalds {
3663bcb09a3SJeremy Fitzhardinge 	int err = -EINVAL;
3673bcb09a3SJeremy Fitzhardinge 
3681c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver)
3693bcb09a3SJeremy Fitzhardinge 		goto out;
3703bcb09a3SJeremy Fitzhardinge 
3711c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
3721da177e4SLinus Torvalds 		if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
3731da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
3743bcb09a3SJeremy Fitzhardinge 			err = 0;
375e08f5f5bSGautham R Shenoy 		} else if (!strnicmp(str_governor, "powersave",
376e08f5f5bSGautham R Shenoy 						CPUFREQ_NAME_LEN)) {
3771da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
3783bcb09a3SJeremy Fitzhardinge 			err = 0;
3791da177e4SLinus Torvalds 		}
3801c3d85ddSRafael J. Wysocki 	} else if (cpufreq_driver->target) {
3811da177e4SLinus Torvalds 		struct cpufreq_governor *t;
3823bcb09a3SJeremy Fitzhardinge 
3833fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
3843bcb09a3SJeremy Fitzhardinge 
3853bcb09a3SJeremy Fitzhardinge 		t = __find_governor(str_governor);
3863bcb09a3SJeremy Fitzhardinge 
387ea714970SJeremy Fitzhardinge 		if (t == NULL) {
388ea714970SJeremy Fitzhardinge 			int ret;
389ea714970SJeremy Fitzhardinge 
390ea714970SJeremy Fitzhardinge 			mutex_unlock(&cpufreq_governor_mutex);
3911a8e1463SKees Cook 			ret = request_module("cpufreq_%s", str_governor);
392ea714970SJeremy Fitzhardinge 			mutex_lock(&cpufreq_governor_mutex);
393ea714970SJeremy Fitzhardinge 
394ea714970SJeremy Fitzhardinge 			if (ret == 0)
395ea714970SJeremy Fitzhardinge 				t = __find_governor(str_governor);
396ea714970SJeremy Fitzhardinge 		}
397ea714970SJeremy Fitzhardinge 
3983bcb09a3SJeremy Fitzhardinge 		if (t != NULL) {
3991da177e4SLinus Torvalds 			*governor = t;
4003bcb09a3SJeremy Fitzhardinge 			err = 0;
4011da177e4SLinus Torvalds 		}
4023bcb09a3SJeremy Fitzhardinge 
4033bcb09a3SJeremy Fitzhardinge 		mutex_unlock(&cpufreq_governor_mutex);
4041da177e4SLinus Torvalds 	}
4051da177e4SLinus Torvalds out:
4063bcb09a3SJeremy Fitzhardinge 	return err;
4071da177e4SLinus Torvalds }
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds /**
410e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
411e08f5f5bSGautham R Shenoy  * print out cpufreq information
4121da177e4SLinus Torvalds  *
4131da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
4141da177e4SLinus Torvalds  * "unsigned int".
4151da177e4SLinus Torvalds  */
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds #define show_one(file_name, object)			\
4181da177e4SLinus Torvalds static ssize_t show_##file_name				\
4191da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
4201da177e4SLinus Torvalds {							\
4211da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
4221da177e4SLinus Torvalds }
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
4251da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
426ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
4271da177e4SLinus Torvalds show_one(scaling_min_freq, min);
4281da177e4SLinus Torvalds show_one(scaling_max_freq, max);
4291da177e4SLinus Torvalds show_one(scaling_cur_freq, cur);
4301da177e4SLinus Torvalds 
4313a3e9e06SViresh Kumar static int __cpufreq_set_policy(struct cpufreq_policy *policy,
4323a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy);
4337970e08bSThomas Renninger 
4341da177e4SLinus Torvalds /**
4351da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
4361da177e4SLinus Torvalds  */
4371da177e4SLinus Torvalds #define store_one(file_name, object)			\
4381da177e4SLinus Torvalds static ssize_t store_##file_name					\
4391da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
4401da177e4SLinus Torvalds {									\
441f55c9c26SJingoo Han 	unsigned int ret;						\
4421da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
4431da177e4SLinus Torvalds 									\
4441da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
4451da177e4SLinus Torvalds 	if (ret)							\
4461da177e4SLinus Torvalds 		return -EINVAL;						\
4471da177e4SLinus Torvalds 									\
4481da177e4SLinus Torvalds 	ret = sscanf(buf, "%u", &new_policy.object);			\
4491da177e4SLinus Torvalds 	if (ret != 1)							\
4501da177e4SLinus Torvalds 		return -EINVAL;						\
4511da177e4SLinus Torvalds 									\
4527970e08bSThomas Renninger 	ret = __cpufreq_set_policy(policy, &new_policy);		\
4537970e08bSThomas Renninger 	policy->user_policy.object = policy->object;			\
4541da177e4SLinus Torvalds 									\
4551da177e4SLinus Torvalds 	return ret ? ret : count;					\
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds store_one(scaling_min_freq, min);
4591da177e4SLinus Torvalds store_one(scaling_max_freq, max);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds /**
4621da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
4631da177e4SLinus Torvalds  */
464e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
465e08f5f5bSGautham R Shenoy 					char *buf)
4661da177e4SLinus Torvalds {
4675a01f2e8SVenkatesh Pallipadi 	unsigned int cur_freq = __cpufreq_get(policy->cpu);
4681da177e4SLinus Torvalds 	if (!cur_freq)
4691da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
4701da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
4711da177e4SLinus Torvalds }
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds /**
4741da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
4751da177e4SLinus Torvalds  */
476905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
4771da177e4SLinus Torvalds {
4781da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
4791da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
4801da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
4811da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
4821da177e4SLinus Torvalds 	else if (policy->governor)
4834b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
48429464f28SDave Jones 				policy->governor->name);
4851da177e4SLinus Torvalds 	return -EINVAL;
4861da177e4SLinus Torvalds }
4871da177e4SLinus Torvalds 
4881da177e4SLinus Torvalds /**
4891da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
4901da177e4SLinus Torvalds  */
4911da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
4921da177e4SLinus Torvalds 					const char *buf, size_t count)
4931da177e4SLinus Torvalds {
494f55c9c26SJingoo Han 	unsigned int ret;
4951da177e4SLinus Torvalds 	char	str_governor[16];
4961da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);
4991da177e4SLinus Torvalds 	if (ret)
5001da177e4SLinus Torvalds 		return ret;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
5031da177e4SLinus Torvalds 	if (ret != 1)
5041da177e4SLinus Torvalds 		return -EINVAL;
5051da177e4SLinus Torvalds 
506e08f5f5bSGautham R Shenoy 	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
507e08f5f5bSGautham R Shenoy 						&new_policy.governor))
5081da177e4SLinus Torvalds 		return -EINVAL;
5091da177e4SLinus Torvalds 
510bb176f7dSViresh Kumar 	/*
511bb176f7dSViresh Kumar 	 * Do not use cpufreq_set_policy here or the user_policy.max
512bb176f7dSViresh Kumar 	 * will be wrongly overridden
513bb176f7dSViresh Kumar 	 */
5147970e08bSThomas Renninger 	ret = __cpufreq_set_policy(policy, &new_policy);
5157970e08bSThomas Renninger 
5167970e08bSThomas Renninger 	policy->user_policy.policy = policy->policy;
5177970e08bSThomas Renninger 	policy->user_policy.governor = policy->governor;
5187970e08bSThomas Renninger 
519e08f5f5bSGautham R Shenoy 	if (ret)
520e08f5f5bSGautham R Shenoy 		return ret;
521e08f5f5bSGautham R Shenoy 	else
522e08f5f5bSGautham R Shenoy 		return count;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds /**
5261da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
5271da177e4SLinus Torvalds  */
5281da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
5291da177e4SLinus Torvalds {
5301c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds /**
5341da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
5351da177e4SLinus Torvalds  */
5361da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
5371da177e4SLinus Torvalds 						char *buf)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	ssize_t i = 0;
5401da177e4SLinus Torvalds 	struct cpufreq_governor *t;
5411da177e4SLinus Torvalds 
5421c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->target) {
5431da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
5441da177e4SLinus Torvalds 		goto out;
5451da177e4SLinus Torvalds 	}
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
54829464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
54929464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
5501da177e4SLinus Torvalds 			goto out;
5514b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
5521da177e4SLinus Torvalds 	}
5531da177e4SLinus Torvalds out:
5541da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
5551da177e4SLinus Torvalds 	return i;
5561da177e4SLinus Torvalds }
557e8628dd0SDarrick J. Wong 
558f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
5591da177e4SLinus Torvalds {
5601da177e4SLinus Torvalds 	ssize_t i = 0;
5611da177e4SLinus Torvalds 	unsigned int cpu;
5621da177e4SLinus Torvalds 
563835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
5641da177e4SLinus Torvalds 		if (i)
5651da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
5661da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
5671da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
5681da177e4SLinus Torvalds 			break;
5691da177e4SLinus Torvalds 	}
5701da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
5711da177e4SLinus Torvalds 	return i;
5721da177e4SLinus Torvalds }
573f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
5741da177e4SLinus Torvalds 
575e8628dd0SDarrick J. Wong /**
576e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
577e8628dd0SDarrick J. Wong  * hw coordination is in use
578e8628dd0SDarrick J. Wong  */
579e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
580e8628dd0SDarrick J. Wong {
581f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
582e8628dd0SDarrick J. Wong }
583e8628dd0SDarrick J. Wong 
584e8628dd0SDarrick J. Wong /**
585e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
586e8628dd0SDarrick J. Wong  */
587e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
588e8628dd0SDarrick J. Wong {
589f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
590e8628dd0SDarrick J. Wong }
591e8628dd0SDarrick J. Wong 
5929e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
5939e76988eSVenki Pallipadi 					const char *buf, size_t count)
5949e76988eSVenki Pallipadi {
5959e76988eSVenki Pallipadi 	unsigned int freq = 0;
5969e76988eSVenki Pallipadi 	unsigned int ret;
5979e76988eSVenki Pallipadi 
598879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
5999e76988eSVenki Pallipadi 		return -EINVAL;
6009e76988eSVenki Pallipadi 
6019e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
6029e76988eSVenki Pallipadi 	if (ret != 1)
6039e76988eSVenki Pallipadi 		return -EINVAL;
6049e76988eSVenki Pallipadi 
6059e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
6069e76988eSVenki Pallipadi 
6079e76988eSVenki Pallipadi 	return count;
6089e76988eSVenki Pallipadi }
6099e76988eSVenki Pallipadi 
6109e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
6119e76988eSVenki Pallipadi {
612879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
6139e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
6149e76988eSVenki Pallipadi 
6159e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
6169e76988eSVenki Pallipadi }
6171da177e4SLinus Torvalds 
618e2f74f35SThomas Renninger /**
6198bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
620e2f74f35SThomas Renninger  */
621e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
622e2f74f35SThomas Renninger {
623e2f74f35SThomas Renninger 	unsigned int limit;
624e2f74f35SThomas Renninger 	int ret;
6251c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
6261c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
627e2f74f35SThomas Renninger 		if (!ret)
628e2f74f35SThomas Renninger 			return sprintf(buf, "%u\n", limit);
629e2f74f35SThomas Renninger 	}
630e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
631e2f74f35SThomas Renninger }
632e2f74f35SThomas Renninger 
6336dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
6346dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
6356dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
6366dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
6376dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
6386dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
6396dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
6406dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
6416dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
6426dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
6436dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
6446dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
6456dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
6466dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
6471da177e4SLinus Torvalds 
6481da177e4SLinus Torvalds static struct attribute *default_attrs[] = {
6491da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
6501da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
651ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
6521da177e4SLinus Torvalds 	&scaling_min_freq.attr,
6531da177e4SLinus Torvalds 	&scaling_max_freq.attr,
6541da177e4SLinus Torvalds 	&affected_cpus.attr,
655e8628dd0SDarrick J. Wong 	&related_cpus.attr,
6561da177e4SLinus Torvalds 	&scaling_governor.attr,
6571da177e4SLinus Torvalds 	&scaling_driver.attr,
6581da177e4SLinus Torvalds 	&scaling_available_governors.attr,
6599e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
6601da177e4SLinus Torvalds 	NULL
6611da177e4SLinus Torvalds };
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
6641da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
6671da177e4SLinus Torvalds {
6681da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
6691da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
6700db4a8a9SDave Jones 	ssize_t ret = -EINVAL;
6716eed9404SViresh Kumar 
6726eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
6736eed9404SViresh Kumar 		goto exit;
6745a01f2e8SVenkatesh Pallipadi 
6755a01f2e8SVenkatesh Pallipadi 	if (lock_policy_rwsem_read(policy->cpu) < 0)
6766eed9404SViresh Kumar 		goto up_read;
6775a01f2e8SVenkatesh Pallipadi 
678e08f5f5bSGautham R Shenoy 	if (fattr->show)
679e08f5f5bSGautham R Shenoy 		ret = fattr->show(policy, buf);
680e08f5f5bSGautham R Shenoy 	else
681e08f5f5bSGautham R Shenoy 		ret = -EIO;
682e08f5f5bSGautham R Shenoy 
6835a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_read(policy->cpu);
6846eed9404SViresh Kumar 
6856eed9404SViresh Kumar up_read:
6866eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
6876eed9404SViresh Kumar exit:
6881da177e4SLinus Torvalds 	return ret;
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
6921da177e4SLinus Torvalds 		     const char *buf, size_t count)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
6951da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
696a07530b4SDave Jones 	ssize_t ret = -EINVAL;
6976eed9404SViresh Kumar 
6986eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
6996eed9404SViresh Kumar 		goto exit;
7005a01f2e8SVenkatesh Pallipadi 
7015a01f2e8SVenkatesh Pallipadi 	if (lock_policy_rwsem_write(policy->cpu) < 0)
7026eed9404SViresh Kumar 		goto up_read;
7035a01f2e8SVenkatesh Pallipadi 
704e08f5f5bSGautham R Shenoy 	if (fattr->store)
705e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
706e08f5f5bSGautham R Shenoy 	else
707e08f5f5bSGautham R Shenoy 		ret = -EIO;
708e08f5f5bSGautham R Shenoy 
7095a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(policy->cpu);
7106eed9404SViresh Kumar 
7116eed9404SViresh Kumar up_read:
7126eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
7136eed9404SViresh Kumar exit:
7141da177e4SLinus Torvalds 	return ret;
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
7202d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
7211da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
72452cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
7251da177e4SLinus Torvalds 	.show	= show,
7261da177e4SLinus Torvalds 	.store	= store,
7271da177e4SLinus Torvalds };
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
7301da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
7311da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
7321da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
7331da177e4SLinus Torvalds };
7341da177e4SLinus Torvalds 
7352361be23SViresh Kumar struct kobject *cpufreq_global_kobject;
7362361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject);
7372361be23SViresh Kumar 
7382361be23SViresh Kumar static int cpufreq_global_kobject_usage;
7392361be23SViresh Kumar 
7402361be23SViresh Kumar int cpufreq_get_global_kobject(void)
7412361be23SViresh Kumar {
7422361be23SViresh Kumar 	if (!cpufreq_global_kobject_usage++)
7432361be23SViresh Kumar 		return kobject_add(cpufreq_global_kobject,
7442361be23SViresh Kumar 				&cpu_subsys.dev_root->kobj, "%s", "cpufreq");
7452361be23SViresh Kumar 
7462361be23SViresh Kumar 	return 0;
7472361be23SViresh Kumar }
7482361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject);
7492361be23SViresh Kumar 
7502361be23SViresh Kumar void cpufreq_put_global_kobject(void)
7512361be23SViresh Kumar {
7522361be23SViresh Kumar 	if (!--cpufreq_global_kobject_usage)
7532361be23SViresh Kumar 		kobject_del(cpufreq_global_kobject);
7542361be23SViresh Kumar }
7552361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject);
7562361be23SViresh Kumar 
7572361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr)
7582361be23SViresh Kumar {
7592361be23SViresh Kumar 	int ret = cpufreq_get_global_kobject();
7602361be23SViresh Kumar 
7612361be23SViresh Kumar 	if (!ret) {
7622361be23SViresh Kumar 		ret = sysfs_create_file(cpufreq_global_kobject, attr);
7632361be23SViresh Kumar 		if (ret)
7642361be23SViresh Kumar 			cpufreq_put_global_kobject();
7652361be23SViresh Kumar 	}
7662361be23SViresh Kumar 
7672361be23SViresh Kumar 	return ret;
7682361be23SViresh Kumar }
7692361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file);
7702361be23SViresh Kumar 
7712361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr)
7722361be23SViresh Kumar {
7732361be23SViresh Kumar 	sysfs_remove_file(cpufreq_global_kobject, attr);
7742361be23SViresh Kumar 	cpufreq_put_global_kobject();
7752361be23SViresh Kumar }
7762361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
7772361be23SViresh Kumar 
77819d6f7ecSDave Jones /* symlink affected CPUs */
779308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
78019d6f7ecSDave Jones {
78119d6f7ecSDave Jones 	unsigned int j;
78219d6f7ecSDave Jones 	int ret = 0;
78319d6f7ecSDave Jones 
78419d6f7ecSDave Jones 	for_each_cpu(j, policy->cpus) {
7858a25a2fdSKay Sievers 		struct device *cpu_dev;
78619d6f7ecSDave Jones 
787308b60e7SViresh Kumar 		if (j == policy->cpu)
78819d6f7ecSDave Jones 			continue;
78919d6f7ecSDave Jones 
790e8fdde10SViresh Kumar 		pr_debug("Adding link for CPU: %u\n", j);
7918a25a2fdSKay Sievers 		cpu_dev = get_cpu_device(j);
7928a25a2fdSKay Sievers 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
79319d6f7ecSDave Jones 					"cpufreq");
79471c3461eSRafael J. Wysocki 		if (ret)
79571c3461eSRafael J. Wysocki 			break;
79619d6f7ecSDave Jones 	}
79719d6f7ecSDave Jones 	return ret;
79819d6f7ecSDave Jones }
79919d6f7ecSDave Jones 
800308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
8018a25a2fdSKay Sievers 				     struct device *dev)
802909a694eSDave Jones {
803909a694eSDave Jones 	struct freq_attr **drv_attr;
804909a694eSDave Jones 	int ret = 0;
805909a694eSDave Jones 
806909a694eSDave Jones 	/* prepare interface data */
807909a694eSDave Jones 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
8088a25a2fdSKay Sievers 				   &dev->kobj, "cpufreq");
809909a694eSDave Jones 	if (ret)
810909a694eSDave Jones 		return ret;
811909a694eSDave Jones 
812909a694eSDave Jones 	/* set up files for this cpu device */
8131c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
814909a694eSDave Jones 	while ((drv_attr) && (*drv_attr)) {
815909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
816909a694eSDave Jones 		if (ret)
8171c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
818909a694eSDave Jones 		drv_attr++;
819909a694eSDave Jones 	}
8201c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
821909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
822909a694eSDave Jones 		if (ret)
8231c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
824909a694eSDave Jones 	}
8251c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target) {
826909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
827909a694eSDave Jones 		if (ret)
8281c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
829909a694eSDave Jones 	}
8301c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
831e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
832e2f74f35SThomas Renninger 		if (ret)
8331c3d85ddSRafael J. Wysocki 			goto err_out_kobj_put;
834e2f74f35SThomas Renninger 	}
835909a694eSDave Jones 
836308b60e7SViresh Kumar 	ret = cpufreq_add_dev_symlink(policy);
837ecf7e461SDave Jones 	if (ret)
838ecf7e461SDave Jones 		goto err_out_kobj_put;
839ecf7e461SDave Jones 
840e18f1682SSrivatsa S. Bhat 	return ret;
841e18f1682SSrivatsa S. Bhat 
842e18f1682SSrivatsa S. Bhat err_out_kobj_put:
843e18f1682SSrivatsa S. Bhat 	kobject_put(&policy->kobj);
844e18f1682SSrivatsa S. Bhat 	wait_for_completion(&policy->kobj_unregister);
845e18f1682SSrivatsa S. Bhat 	return ret;
846e18f1682SSrivatsa S. Bhat }
847e18f1682SSrivatsa S. Bhat 
848e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy)
849e18f1682SSrivatsa S. Bhat {
850e18f1682SSrivatsa S. Bhat 	struct cpufreq_policy new_policy;
851e18f1682SSrivatsa S. Bhat 	int ret = 0;
852e18f1682SSrivatsa S. Bhat 
853d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
854ecf7e461SDave Jones 	/* assure that the starting sequence is run in __cpufreq_set_policy */
855ecf7e461SDave Jones 	policy->governor = NULL;
856ecf7e461SDave Jones 
857ecf7e461SDave Jones 	/* set default policy */
858ecf7e461SDave Jones 	ret = __cpufreq_set_policy(policy, &new_policy);
859ecf7e461SDave Jones 	policy->user_policy.policy = policy->policy;
860ecf7e461SDave Jones 	policy->user_policy.governor = policy->governor;
861ecf7e461SDave Jones 
862ecf7e461SDave Jones 	if (ret) {
8632d06d8c4SDominik Brodowski 		pr_debug("setting policy failed\n");
8641c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
8651c3d85ddSRafael J. Wysocki 			cpufreq_driver->exit(policy);
866ecf7e461SDave Jones 	}
867909a694eSDave Jones }
868909a694eSDave Jones 
869fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
870d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
871d8d3b471SViresh Kumar 				  unsigned int cpu, struct device *dev,
872d8d3b471SViresh Kumar 				  bool frozen)
873fcf80582SViresh Kumar {
8741c3d85ddSRafael J. Wysocki 	int ret = 0, has_target = !!cpufreq_driver->target;
875fcf80582SViresh Kumar 	unsigned long flags;
876fcf80582SViresh Kumar 
8773de9bdebSViresh Kumar 	if (has_target) {
8783de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
8793de9bdebSViresh Kumar 		if (ret) {
8803de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
8813de9bdebSViresh Kumar 			return ret;
8823de9bdebSViresh Kumar 		}
8833de9bdebSViresh Kumar 	}
884fcf80582SViresh Kumar 
885d8d3b471SViresh Kumar 	lock_policy_rwsem_write(policy->cpu);
8862eaa3e2dSViresh Kumar 
8870d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
8882eaa3e2dSViresh Kumar 
889fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
8902eaa3e2dSViresh Kumar 	per_cpu(cpufreq_policy_cpu, cpu) = policy->cpu;
891fcf80582SViresh Kumar 	per_cpu(cpufreq_cpu_data, cpu) = policy;
8920d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
893fcf80582SViresh Kumar 
894d8d3b471SViresh Kumar 	unlock_policy_rwsem_write(policy->cpu);
8952eaa3e2dSViresh Kumar 
896820c6ca2SViresh Kumar 	if (has_target) {
8973de9bdebSViresh Kumar 		if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
8983de9bdebSViresh Kumar 			(ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
8993de9bdebSViresh Kumar 			pr_err("%s: Failed to start governor\n", __func__);
9003de9bdebSViresh Kumar 			return ret;
9013de9bdebSViresh Kumar 		}
902820c6ca2SViresh Kumar 	}
903fcf80582SViresh Kumar 
904a82fab29SSrivatsa S. Bhat 	/* Don't touch sysfs links during light-weight init */
90571c3461eSRafael J. Wysocki 	if (!frozen)
906a82fab29SSrivatsa S. Bhat 		ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
907a82fab29SSrivatsa S. Bhat 
908a82fab29SSrivatsa S. Bhat 	return ret;
909fcf80582SViresh Kumar }
910fcf80582SViresh Kumar #endif
9111da177e4SLinus Torvalds 
9128414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
9138414809cSSrivatsa S. Bhat {
9148414809cSSrivatsa S. Bhat 	struct cpufreq_policy *policy;
9158414809cSSrivatsa S. Bhat 	unsigned long flags;
9168414809cSSrivatsa S. Bhat 
9178414809cSSrivatsa S. Bhat 	write_lock_irqsave(&cpufreq_driver_lock, flags);
9188414809cSSrivatsa S. Bhat 
9198414809cSSrivatsa S. Bhat 	policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
9208414809cSSrivatsa S. Bhat 
9218414809cSSrivatsa S. Bhat 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
9228414809cSSrivatsa S. Bhat 
9238414809cSSrivatsa S. Bhat 	return policy;
9248414809cSSrivatsa S. Bhat }
9258414809cSSrivatsa S. Bhat 
926e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void)
927e9698cc5SSrivatsa S. Bhat {
928e9698cc5SSrivatsa S. Bhat 	struct cpufreq_policy *policy;
929e9698cc5SSrivatsa S. Bhat 
930e9698cc5SSrivatsa S. Bhat 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
931e9698cc5SSrivatsa S. Bhat 	if (!policy)
932e9698cc5SSrivatsa S. Bhat 		return NULL;
933e9698cc5SSrivatsa S. Bhat 
934e9698cc5SSrivatsa S. Bhat 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
935e9698cc5SSrivatsa S. Bhat 		goto err_free_policy;
936e9698cc5SSrivatsa S. Bhat 
937e9698cc5SSrivatsa S. Bhat 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
938e9698cc5SSrivatsa S. Bhat 		goto err_free_cpumask;
939e9698cc5SSrivatsa S. Bhat 
940c88a1f8bSLukasz Majewski 	INIT_LIST_HEAD(&policy->policy_list);
941e9698cc5SSrivatsa S. Bhat 	return policy;
942e9698cc5SSrivatsa S. Bhat 
943e9698cc5SSrivatsa S. Bhat err_free_cpumask:
944e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
945e9698cc5SSrivatsa S. Bhat err_free_policy:
946e9698cc5SSrivatsa S. Bhat 	kfree(policy);
947e9698cc5SSrivatsa S. Bhat 
948e9698cc5SSrivatsa S. Bhat 	return NULL;
949e9698cc5SSrivatsa S. Bhat }
950e9698cc5SSrivatsa S. Bhat 
951e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy)
952e9698cc5SSrivatsa S. Bhat {
953e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
954e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
955e9698cc5SSrivatsa S. Bhat 	kfree(policy);
956e9698cc5SSrivatsa S. Bhat }
957e9698cc5SSrivatsa S. Bhat 
958a82fab29SSrivatsa S. Bhat static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
959a82fab29SSrivatsa S. Bhat 			     bool frozen)
9601da177e4SLinus Torvalds {
961fcf80582SViresh Kumar 	unsigned int j, cpu = dev->id;
96265922465SViresh Kumar 	int ret = -ENOMEM;
9631da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
9641da177e4SLinus Torvalds 	unsigned long flags;
96590e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
966fcf80582SViresh Kumar 	struct cpufreq_governor *gov;
967878f6e07SRafael J. Wysocki 	int sibling;
96890e41bacSPrarit Bhargava #endif
9691da177e4SLinus Torvalds 
970c32b6b8eSAshok Raj 	if (cpu_is_offline(cpu))
971c32b6b8eSAshok Raj 		return 0;
972c32b6b8eSAshok Raj 
9732d06d8c4SDominik Brodowski 	pr_debug("adding CPU %u\n", cpu);
9741da177e4SLinus Torvalds 
9751da177e4SLinus Torvalds #ifdef CONFIG_SMP
9761da177e4SLinus Torvalds 	/* check whether a different CPU already registered this
9771da177e4SLinus Torvalds 	 * CPU because it is in the same boat. */
9781da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(cpu);
9791da177e4SLinus Torvalds 	if (unlikely(policy)) {
9808ff69732SDave Jones 		cpufreq_cpu_put(policy);
9811da177e4SLinus Torvalds 		return 0;
9821da177e4SLinus Torvalds 	}
983fcf80582SViresh Kumar 
9846eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
9856eed9404SViresh Kumar 		return 0;
9866eed9404SViresh Kumar 
987fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
988fcf80582SViresh Kumar 	/* Check if this cpu was hot-unplugged earlier and has siblings */
9890d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
990878f6e07SRafael J. Wysocki 	for_each_online_cpu(sibling) {
991878f6e07SRafael J. Wysocki 		struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
992878f6e07SRafael J. Wysocki 		if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) {
9930d1857a1SNathan Zimmer 			read_unlock_irqrestore(&cpufreq_driver_lock, flags);
994878f6e07SRafael J. Wysocki 			ret = cpufreq_add_policy_cpu(cp, cpu, dev, frozen);
9956eed9404SViresh Kumar 			up_read(&cpufreq_rwsem);
9966eed9404SViresh Kumar 			return ret;
997fcf80582SViresh Kumar 		}
9982eaa3e2dSViresh Kumar 	}
9990d1857a1SNathan Zimmer 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1000fcf80582SViresh Kumar #endif
10011da177e4SLinus Torvalds #endif
10021da177e4SLinus Torvalds 
10038414809cSSrivatsa S. Bhat 	if (frozen)
10048414809cSSrivatsa S. Bhat 		/* Restore the saved policy when doing light-weight init */
10058414809cSSrivatsa S. Bhat 		policy = cpufreq_policy_restore(cpu);
10068414809cSSrivatsa S. Bhat 	else
1007e9698cc5SSrivatsa S. Bhat 		policy = cpufreq_policy_alloc();
10088414809cSSrivatsa S. Bhat 
1009059019a3SDave Jones 	if (!policy)
10101da177e4SLinus Torvalds 		goto nomem_out;
1011059019a3SDave Jones 
10121da177e4SLinus Torvalds 	policy->cpu = cpu;
101365922465SViresh Kumar 	policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1014835481d9SRusty Russell 	cpumask_copy(policy->cpus, cpumask_of(cpu));
10151da177e4SLinus Torvalds 
10165a01f2e8SVenkatesh Pallipadi 	/* Initially set CPU itself as the policy_cpu */
1017f1625066STejun Heo 	per_cpu(cpufreq_policy_cpu, cpu) = cpu;
10185a01f2e8SVenkatesh Pallipadi 
10191da177e4SLinus Torvalds 	init_completion(&policy->kobj_unregister);
102065f27f38SDavid Howells 	INIT_WORK(&policy->update, handle_update);
10211da177e4SLinus Torvalds 
10221da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
10231da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
10241da177e4SLinus Torvalds 	 */
10251c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->init(policy);
10261da177e4SLinus Torvalds 	if (ret) {
10272d06d8c4SDominik Brodowski 		pr_debug("initialization failed\n");
10282eaa3e2dSViresh Kumar 		goto err_set_policy_cpu;
10291da177e4SLinus Torvalds 	}
1030643ae6e8SViresh Kumar 
1031fcf80582SViresh Kumar 	/* related cpus should atleast have policy->cpus */
1032fcf80582SViresh Kumar 	cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1033fcf80582SViresh Kumar 
1034643ae6e8SViresh Kumar 	/*
1035643ae6e8SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
1036643ae6e8SViresh Kumar 	 * managing offline cpus here.
1037643ae6e8SViresh Kumar 	 */
1038643ae6e8SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1039643ae6e8SViresh Kumar 
1040187d9f4eSMike Chan 	policy->user_policy.min = policy->min;
1041187d9f4eSMike Chan 	policy->user_policy.max = policy->max;
10421da177e4SLinus Torvalds 
1043a1531acdSThomas Renninger 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1044a1531acdSThomas Renninger 				     CPUFREQ_START, policy);
1045a1531acdSThomas Renninger 
1046fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU
1047fcf80582SViresh Kumar 	gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1048fcf80582SViresh Kumar 	if (gov) {
1049fcf80582SViresh Kumar 		policy->governor = gov;
1050fcf80582SViresh Kumar 		pr_debug("Restoring governor %s for cpu %d\n",
1051fcf80582SViresh Kumar 		       policy->governor->name, cpu);
10524bfa042cSThomas Renninger 	}
1053fcf80582SViresh Kumar #endif
10541da177e4SLinus Torvalds 
1055e18f1682SSrivatsa S. Bhat 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1056e18f1682SSrivatsa S. Bhat 	for_each_cpu(j, policy->cpus) {
1057e18f1682SSrivatsa S. Bhat 		per_cpu(cpufreq_cpu_data, j) = policy;
1058e18f1682SSrivatsa S. Bhat 		per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
1059e18f1682SSrivatsa S. Bhat 	}
1060e18f1682SSrivatsa S. Bhat 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1061e18f1682SSrivatsa S. Bhat 
1062a82fab29SSrivatsa S. Bhat 	if (!frozen) {
1063308b60e7SViresh Kumar 		ret = cpufreq_add_dev_interface(policy, dev);
106419d6f7ecSDave Jones 		if (ret)
10650142f9dcSAhmed S. Darwish 			goto err_out_unregister;
1066*9515f4d6SViresh Kumar 	}
1067c88a1f8bSLukasz Majewski 
1068c88a1f8bSLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1069c88a1f8bSLukasz Majewski 	list_add(&policy->policy_list, &cpufreq_policy_list);
1070c88a1f8bSLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
10718ff69732SDave Jones 
1072e18f1682SSrivatsa S. Bhat 	cpufreq_init_policy(policy);
1073e18f1682SSrivatsa S. Bhat 
1074038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
10756eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
10766eed9404SViresh Kumar 
10772d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	return 0;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds err_out_unregister:
10820d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1083e18f1682SSrivatsa S. Bhat 	for_each_cpu(j, policy->cpus) {
10847a6aedfaSMike Travis 		per_cpu(cpufreq_cpu_data, j) = NULL;
1085e18f1682SSrivatsa S. Bhat 		if (j != cpu)
1086e18f1682SSrivatsa S. Bhat 			per_cpu(cpufreq_policy_cpu, j) = -1;
1087e18f1682SSrivatsa S. Bhat 	}
10880d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
10891da177e4SLinus Torvalds 
10902eaa3e2dSViresh Kumar err_set_policy_cpu:
10912eaa3e2dSViresh Kumar 	per_cpu(cpufreq_policy_cpu, cpu) = -1;
1092e9698cc5SSrivatsa S. Bhat 	cpufreq_policy_free(policy);
10931da177e4SLinus Torvalds nomem_out:
10946eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
10956eed9404SViresh Kumar 
10961da177e4SLinus Torvalds 	return ret;
10971da177e4SLinus Torvalds }
10981da177e4SLinus Torvalds 
1099a82fab29SSrivatsa S. Bhat /**
1100a82fab29SSrivatsa S. Bhat  * cpufreq_add_dev - add a CPU device
1101a82fab29SSrivatsa S. Bhat  *
1102a82fab29SSrivatsa S. Bhat  * Adds the cpufreq interface for a CPU device.
1103a82fab29SSrivatsa S. Bhat  *
1104a82fab29SSrivatsa S. Bhat  * The Oracle says: try running cpufreq registration/unregistration concurrently
1105a82fab29SSrivatsa S. Bhat  * with with cpu hotplugging and all hell will break loose. Tried to clean this
1106a82fab29SSrivatsa S. Bhat  * mess up, but more thorough testing is needed. - Mathieu
1107a82fab29SSrivatsa S. Bhat  */
1108a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1109a82fab29SSrivatsa S. Bhat {
1110a82fab29SSrivatsa S. Bhat 	return __cpufreq_add_dev(dev, sif, false);
1111a82fab29SSrivatsa S. Bhat }
1112a82fab29SSrivatsa S. Bhat 
1113b8eed8afSViresh Kumar static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1114b8eed8afSViresh Kumar {
1115b8eed8afSViresh Kumar 	int j;
1116b8eed8afSViresh Kumar 
1117b8eed8afSViresh Kumar 	policy->last_cpu = policy->cpu;
1118b8eed8afSViresh Kumar 	policy->cpu = cpu;
1119b8eed8afSViresh Kumar 
11203361b7b1SViresh Kumar 	for_each_cpu(j, policy->cpus)
1121b8eed8afSViresh Kumar 		per_cpu(cpufreq_policy_cpu, j) = cpu;
1122b8eed8afSViresh Kumar 
1123b8eed8afSViresh Kumar #ifdef CONFIG_CPU_FREQ_TABLE
1124b8eed8afSViresh Kumar 	cpufreq_frequency_table_update_policy_cpu(policy);
1125b8eed8afSViresh Kumar #endif
1126b8eed8afSViresh Kumar 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1127b8eed8afSViresh Kumar 			CPUFREQ_UPDATE_POLICY_CPU, policy);
1128b8eed8afSViresh Kumar }
11291da177e4SLinus Torvalds 
11303a3e9e06SViresh Kumar static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
1131a82fab29SSrivatsa S. Bhat 					   unsigned int old_cpu, bool frozen)
1132f9ba680dSSrivatsa S. Bhat {
1133f9ba680dSSrivatsa S. Bhat 	struct device *cpu_dev;
1134f9ba680dSSrivatsa S. Bhat 	unsigned long flags;
1135f9ba680dSSrivatsa S. Bhat 	int ret;
1136f9ba680dSSrivatsa S. Bhat 
1137f9ba680dSSrivatsa S. Bhat 	/* first sibling now owns the new sysfs dir */
11383a3e9e06SViresh Kumar 	cpu_dev = get_cpu_device(cpumask_first(policy->cpus));
1139a82fab29SSrivatsa S. Bhat 
1140a82fab29SSrivatsa S. Bhat 	/* Don't touch sysfs files during light-weight tear-down */
1141a82fab29SSrivatsa S. Bhat 	if (frozen)
1142a82fab29SSrivatsa S. Bhat 		return cpu_dev->id;
1143a82fab29SSrivatsa S. Bhat 
1144f9ba680dSSrivatsa S. Bhat 	sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
11453a3e9e06SViresh Kumar 	ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1146f9ba680dSSrivatsa S. Bhat 	if (ret) {
1147f9ba680dSSrivatsa S. Bhat 		pr_err("%s: Failed to move kobj: %d", __func__, ret);
1148f9ba680dSSrivatsa S. Bhat 
1149f9ba680dSSrivatsa S. Bhat 		WARN_ON(lock_policy_rwsem_write(old_cpu));
11503a3e9e06SViresh Kumar 		cpumask_set_cpu(old_cpu, policy->cpus);
1151f9ba680dSSrivatsa S. Bhat 
1152f9ba680dSSrivatsa S. Bhat 		write_lock_irqsave(&cpufreq_driver_lock, flags);
11533a3e9e06SViresh Kumar 		per_cpu(cpufreq_cpu_data, old_cpu) = policy;
1154f9ba680dSSrivatsa S. Bhat 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1155f9ba680dSSrivatsa S. Bhat 
1156f9ba680dSSrivatsa S. Bhat 		unlock_policy_rwsem_write(old_cpu);
1157f9ba680dSSrivatsa S. Bhat 
11583a3e9e06SViresh Kumar 		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1159f9ba680dSSrivatsa S. Bhat 					"cpufreq");
1160f9ba680dSSrivatsa S. Bhat 
1161f9ba680dSSrivatsa S. Bhat 		return -EINVAL;
1162f9ba680dSSrivatsa S. Bhat 	}
1163f9ba680dSSrivatsa S. Bhat 
1164f9ba680dSSrivatsa S. Bhat 	return cpu_dev->id;
1165f9ba680dSSrivatsa S. Bhat }
1166f9ba680dSSrivatsa S. Bhat 
11671da177e4SLinus Torvalds /**
11685a01f2e8SVenkatesh Pallipadi  * __cpufreq_remove_dev - remove a CPU device
11691da177e4SLinus Torvalds  *
11701da177e4SLinus Torvalds  * Removes the cpufreq interface for a CPU device.
11715a01f2e8SVenkatesh Pallipadi  * Caller should already have policy_rwsem in write mode for this CPU.
11725a01f2e8SVenkatesh Pallipadi  * This routine frees the rwsem before returning.
11731da177e4SLinus Torvalds  */
1174bb176f7dSViresh Kumar static int __cpufreq_remove_dev(struct device *dev,
1175a82fab29SSrivatsa S. Bhat 				struct subsys_interface *sif, bool frozen)
11761da177e4SLinus Torvalds {
1177f9ba680dSSrivatsa S. Bhat 	unsigned int cpu = dev->id, cpus;
11783de9bdebSViresh Kumar 	int new_cpu, ret;
11791da177e4SLinus Torvalds 	unsigned long flags;
11803a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
1181499bca9bSAmerigo Wang 	struct kobject *kobj;
1182499bca9bSAmerigo Wang 	struct completion *cmp;
11831da177e4SLinus Torvalds 
1184b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
11851da177e4SLinus Torvalds 
11860d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
11871da177e4SLinus Torvalds 
11883a3e9e06SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
11897a6aedfaSMike Travis 	per_cpu(cpufreq_cpu_data, cpu) = NULL;
11901da177e4SLinus Torvalds 
11918414809cSSrivatsa S. Bhat 	/* Save the policy somewhere when doing a light-weight tear-down */
11928414809cSSrivatsa S. Bhat 	if (frozen)
11933a3e9e06SViresh Kumar 		per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
11948414809cSSrivatsa S. Bhat 
11950d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
11961da177e4SLinus Torvalds 
11973a3e9e06SViresh Kumar 	if (!policy) {
1198b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
11991da177e4SLinus Torvalds 		return -EINVAL;
12001da177e4SLinus Torvalds 	}
12011da177e4SLinus Torvalds 
12023de9bdebSViresh Kumar 	if (cpufreq_driver->target) {
12033de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
12043de9bdebSViresh Kumar 		if (ret) {
12053de9bdebSViresh Kumar 			pr_err("%s: Failed to stop governor\n", __func__);
12063de9bdebSViresh Kumar 			return ret;
12073de9bdebSViresh Kumar 		}
12083de9bdebSViresh Kumar 	}
12095a01f2e8SVenkatesh Pallipadi 
12101da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU
12111c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->setpolicy)
1212fa69e33fSDirk Brandewie 		strncpy(per_cpu(cpufreq_cpu_governor, cpu),
12133a3e9e06SViresh Kumar 			policy->governor->name, CPUFREQ_NAME_LEN);
12141da177e4SLinus Torvalds #endif
12151da177e4SLinus Torvalds 
12162eaa3e2dSViresh Kumar 	WARN_ON(lock_policy_rwsem_write(cpu));
12173a3e9e06SViresh Kumar 	cpus = cpumask_weight(policy->cpus);
1218e4969ebaSViresh Kumar 
1219e4969ebaSViresh Kumar 	if (cpus > 1)
12203a3e9e06SViresh Kumar 		cpumask_clear_cpu(cpu, policy->cpus);
12212eaa3e2dSViresh Kumar 	unlock_policy_rwsem_write(cpu);
12221da177e4SLinus Torvalds 
12233a3e9e06SViresh Kumar 	if (cpu != policy->cpu && !frozen) {
122473bf0fc2SViresh Kumar 		sysfs_remove_link(&dev->kobj, "cpufreq");
122573bf0fc2SViresh Kumar 	} else if (cpus > 1) {
12262eaa3e2dSViresh Kumar 
12273a3e9e06SViresh Kumar 		new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
1228f9ba680dSSrivatsa S. Bhat 		if (new_cpu >= 0) {
12292eaa3e2dSViresh Kumar 			WARN_ON(lock_policy_rwsem_write(cpu));
12303a3e9e06SViresh Kumar 			update_policy_cpu(policy, new_cpu);
12312eaa3e2dSViresh Kumar 			unlock_policy_rwsem_write(cpu);
1232a82fab29SSrivatsa S. Bhat 
1233a82fab29SSrivatsa S. Bhat 			if (!frozen) {
1234f9ba680dSSrivatsa S. Bhat 				pr_debug("%s: policy Kobject moved to cpu: %d "
1235f9ba680dSSrivatsa S. Bhat 					 "from: %d\n",__func__, new_cpu, cpu);
12361da177e4SLinus Torvalds 			}
12371da177e4SLinus Torvalds 		}
1238a82fab29SSrivatsa S. Bhat 	}
1239b8eed8afSViresh Kumar 
1240b8eed8afSViresh Kumar 	/* If cpu is last user of policy, free policy */
1241b8eed8afSViresh Kumar 	if (cpus == 1) {
12423de9bdebSViresh Kumar 		if (cpufreq_driver->target) {
12433de9bdebSViresh Kumar 			ret = __cpufreq_governor(policy,
12443de9bdebSViresh Kumar 					CPUFREQ_GOV_POLICY_EXIT);
12453de9bdebSViresh Kumar 			if (ret) {
12463de9bdebSViresh Kumar 				pr_err("%s: Failed to exit governor\n",
12473de9bdebSViresh Kumar 						__func__);
12483de9bdebSViresh Kumar 				return ret;
12493de9bdebSViresh Kumar 			}
12503de9bdebSViresh Kumar 		}
12512a998599SRafael J. Wysocki 
12528414809cSSrivatsa S. Bhat 		if (!frozen) {
12532eaa3e2dSViresh Kumar 			lock_policy_rwsem_read(cpu);
12543a3e9e06SViresh Kumar 			kobj = &policy->kobj;
12553a3e9e06SViresh Kumar 			cmp = &policy->kobj_unregister;
12562eaa3e2dSViresh Kumar 			unlock_policy_rwsem_read(cpu);
1257499bca9bSAmerigo Wang 			kobject_put(kobj);
12581da177e4SLinus Torvalds 
12598414809cSSrivatsa S. Bhat 			/*
12608414809cSSrivatsa S. Bhat 			 * We need to make sure that the underlying kobj is
12618414809cSSrivatsa S. Bhat 			 * actually not referenced anymore by anybody before we
12628414809cSSrivatsa S. Bhat 			 * proceed with unloading.
12631da177e4SLinus Torvalds 			 */
12642d06d8c4SDominik Brodowski 			pr_debug("waiting for dropping of refcount\n");
1265499bca9bSAmerigo Wang 			wait_for_completion(cmp);
12662d06d8c4SDominik Brodowski 			pr_debug("wait complete\n");
12678414809cSSrivatsa S. Bhat 		}
12681da177e4SLinus Torvalds 
12698414809cSSrivatsa S. Bhat 		/*
12708414809cSSrivatsa S. Bhat 		 * Perform the ->exit() even during light-weight tear-down,
12718414809cSSrivatsa S. Bhat 		 * since this is a core component, and is essential for the
12728414809cSSrivatsa S. Bhat 		 * subsequent light-weight ->init() to succeed.
12738414809cSSrivatsa S. Bhat 		 */
12741c3d85ddSRafael J. Wysocki 		if (cpufreq_driver->exit)
12753a3e9e06SViresh Kumar 			cpufreq_driver->exit(policy);
127627ecddc2SJacob Shin 
1277*9515f4d6SViresh Kumar 		/* Remove policy from list of active policies */
1278*9515f4d6SViresh Kumar 		write_lock_irqsave(&cpufreq_driver_lock, flags);
1279*9515f4d6SViresh Kumar 		list_del(&policy->policy_list);
1280*9515f4d6SViresh Kumar 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1281*9515f4d6SViresh Kumar 
12828414809cSSrivatsa S. Bhat 		if (!frozen)
12833a3e9e06SViresh Kumar 			cpufreq_policy_free(policy);
12842a998599SRafael J. Wysocki 	} else {
12852a998599SRafael J. Wysocki 		if (cpufreq_driver->target) {
12863de9bdebSViresh Kumar 			if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
12873de9bdebSViresh Kumar 					(ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
12883de9bdebSViresh Kumar 				pr_err("%s: Failed to start governor\n",
12893de9bdebSViresh Kumar 						__func__);
12903de9bdebSViresh Kumar 				return ret;
12913de9bdebSViresh Kumar 			}
1292b8eed8afSViresh Kumar 		}
12932a998599SRafael J. Wysocki 	}
12941da177e4SLinus Torvalds 
12952eaa3e2dSViresh Kumar 	per_cpu(cpufreq_policy_cpu, cpu) = -1;
12961da177e4SLinus Torvalds 	return 0;
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds 
12998a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
13005a01f2e8SVenkatesh Pallipadi {
13018a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
13025a01f2e8SVenkatesh Pallipadi 	int retval;
1303ec28297aSVenki Pallipadi 
1304ec28297aSVenki Pallipadi 	if (cpu_is_offline(cpu))
1305ec28297aSVenki Pallipadi 		return 0;
1306ec28297aSVenki Pallipadi 
1307a82fab29SSrivatsa S. Bhat 	retval = __cpufreq_remove_dev(dev, sif, false);
13085a01f2e8SVenkatesh Pallipadi 	return retval;
13095a01f2e8SVenkatesh Pallipadi }
13105a01f2e8SVenkatesh Pallipadi 
131165f27f38SDavid Howells static void handle_update(struct work_struct *work)
13121da177e4SLinus Torvalds {
131365f27f38SDavid Howells 	struct cpufreq_policy *policy =
131465f27f38SDavid Howells 		container_of(work, struct cpufreq_policy, update);
131565f27f38SDavid Howells 	unsigned int cpu = policy->cpu;
13162d06d8c4SDominik Brodowski 	pr_debug("handle_update for cpu %u called\n", cpu);
13171da177e4SLinus Torvalds 	cpufreq_update_policy(cpu);
13181da177e4SLinus Torvalds }
13191da177e4SLinus Torvalds 
13201da177e4SLinus Torvalds /**
1321bb176f7dSViresh Kumar  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1322bb176f7dSViresh Kumar  *	in deep trouble.
13231da177e4SLinus Torvalds  *	@cpu: cpu number
13241da177e4SLinus Torvalds  *	@old_freq: CPU frequency the kernel thinks the CPU runs at
13251da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
13261da177e4SLinus Torvalds  *
132729464f28SDave Jones  *	We adjust to current frequency first, and need to clean up later.
132829464f28SDave Jones  *	So either call to cpufreq_update_policy() or schedule handle_update()).
13291da177e4SLinus Torvalds  */
1330e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1331e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
13321da177e4SLinus Torvalds {
1333b43a7ffbSViresh Kumar 	struct cpufreq_policy *policy;
13341da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1335b43a7ffbSViresh Kumar 	unsigned long flags;
1336b43a7ffbSViresh Kumar 
13372d06d8c4SDominik Brodowski 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
13381da177e4SLinus Torvalds 	       "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	freqs.old = old_freq;
13411da177e4SLinus Torvalds 	freqs.new = new_freq;
1342b43a7ffbSViresh Kumar 
1343b43a7ffbSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1344b43a7ffbSViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
1345b43a7ffbSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1346b43a7ffbSViresh Kumar 
1347b43a7ffbSViresh Kumar 	cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1348b43a7ffbSViresh Kumar 	cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
13491da177e4SLinus Torvalds }
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds /**
13524ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
135395235ca2SVenkatesh Pallipadi  * @cpu: CPU number
135495235ca2SVenkatesh Pallipadi  *
135595235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
135695235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
135795235ca2SVenkatesh Pallipadi  */
135895235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
135995235ca2SVenkatesh Pallipadi {
13609e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1361e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
136295235ca2SVenkatesh Pallipadi 
13631c3d85ddSRafael J. Wysocki 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
13641c3d85ddSRafael J. Wysocki 		return cpufreq_driver->get(cpu);
13659e21ba8bSDirk Brandewie 
13669e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
136795235ca2SVenkatesh Pallipadi 	if (policy) {
1368e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
136995235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
137095235ca2SVenkatesh Pallipadi 	}
137195235ca2SVenkatesh Pallipadi 
13724d34a67dSDave Jones 	return ret_freq;
137395235ca2SVenkatesh Pallipadi }
137495235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
137595235ca2SVenkatesh Pallipadi 
13763d737108SJesse Barnes /**
13773d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
13783d737108SJesse Barnes  * @cpu: CPU number
13793d737108SJesse Barnes  *
13803d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
13813d737108SJesse Barnes  */
13823d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
13833d737108SJesse Barnes {
13843d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
13853d737108SJesse Barnes 	unsigned int ret_freq = 0;
13863d737108SJesse Barnes 
13873d737108SJesse Barnes 	if (policy) {
13883d737108SJesse Barnes 		ret_freq = policy->max;
13893d737108SJesse Barnes 		cpufreq_cpu_put(policy);
13903d737108SJesse Barnes 	}
13913d737108SJesse Barnes 
13923d737108SJesse Barnes 	return ret_freq;
13933d737108SJesse Barnes }
13943d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
13953d737108SJesse Barnes 
13965a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu)
13971da177e4SLinus Torvalds {
13987a6aedfaSMike Travis 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1399e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
14001da177e4SLinus Torvalds 
14011c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->get)
14024d34a67dSDave Jones 		return ret_freq;
14031da177e4SLinus Torvalds 
14041c3d85ddSRafael J. Wysocki 	ret_freq = cpufreq_driver->get(cpu);
14051da177e4SLinus Torvalds 
1406e08f5f5bSGautham R Shenoy 	if (ret_freq && policy->cur &&
14071c3d85ddSRafael J. Wysocki 		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1408e08f5f5bSGautham R Shenoy 		/* verify no discrepancy between actual and
1409e08f5f5bSGautham R Shenoy 					saved value exists */
1410e08f5f5bSGautham R Shenoy 		if (unlikely(ret_freq != policy->cur)) {
1411e08f5f5bSGautham R Shenoy 			cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
14121da177e4SLinus Torvalds 			schedule_work(&policy->update);
14131da177e4SLinus Torvalds 		}
14141da177e4SLinus Torvalds 	}
14151da177e4SLinus Torvalds 
14164d34a67dSDave Jones 	return ret_freq;
14175a01f2e8SVenkatesh Pallipadi }
14181da177e4SLinus Torvalds 
14195a01f2e8SVenkatesh Pallipadi /**
14205a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
14215a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
14225a01f2e8SVenkatesh Pallipadi  *
14235a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
14245a01f2e8SVenkatesh Pallipadi  */
14255a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
14265a01f2e8SVenkatesh Pallipadi {
14275a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
14285a01f2e8SVenkatesh Pallipadi 
14296eed9404SViresh Kumar 	if (!down_read_trylock(&cpufreq_rwsem))
14306eed9404SViresh Kumar 		return 0;
14315a01f2e8SVenkatesh Pallipadi 
14325a01f2e8SVenkatesh Pallipadi 	if (unlikely(lock_policy_rwsem_read(cpu)))
14335a01f2e8SVenkatesh Pallipadi 		goto out_policy;
14345a01f2e8SVenkatesh Pallipadi 
14355a01f2e8SVenkatesh Pallipadi 	ret_freq = __cpufreq_get(cpu);
14365a01f2e8SVenkatesh Pallipadi 
14375a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_read(cpu);
14385a01f2e8SVenkatesh Pallipadi 
14395a01f2e8SVenkatesh Pallipadi out_policy:
14406eed9404SViresh Kumar 	up_read(&cpufreq_rwsem);
14416eed9404SViresh Kumar 
14424d34a67dSDave Jones 	return ret_freq;
14431da177e4SLinus Torvalds }
14441da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
14451da177e4SLinus Torvalds 
14468a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
14478a25a2fdSKay Sievers 	.name		= "cpufreq",
14488a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
14498a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
14508a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1451e00e56dfSRafael J. Wysocki };
1452e00e56dfSRafael J. Wysocki 
14531da177e4SLinus Torvalds /**
1454e00e56dfSRafael J. Wysocki  * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1455e00e56dfSRafael J. Wysocki  *
1456e00e56dfSRafael J. Wysocki  * This function is only executed for the boot processor.  The other CPUs
1457e00e56dfSRafael J. Wysocki  * have been put offline by means of CPU hotplug.
145842d4dc3fSBenjamin Herrenschmidt  */
1459e00e56dfSRafael J. Wysocki static int cpufreq_bp_suspend(void)
146042d4dc3fSBenjamin Herrenschmidt {
1461e08f5f5bSGautham R Shenoy 	int ret = 0;
14624bc5d341SDave Jones 
1463e00e56dfSRafael J. Wysocki 	int cpu = smp_processor_id();
14643a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
146542d4dc3fSBenjamin Herrenschmidt 
14662d06d8c4SDominik Brodowski 	pr_debug("suspending cpu %u\n", cpu);
146742d4dc3fSBenjamin Herrenschmidt 
1468e00e56dfSRafael J. Wysocki 	/* If there's no policy for the boot CPU, we have nothing to do. */
14693a3e9e06SViresh Kumar 	policy = cpufreq_cpu_get(cpu);
14703a3e9e06SViresh Kumar 	if (!policy)
1471e00e56dfSRafael J. Wysocki 		return 0;
147242d4dc3fSBenjamin Herrenschmidt 
14731c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->suspend) {
14743a3e9e06SViresh Kumar 		ret = cpufreq_driver->suspend(policy);
1475ce6c3997SDominik Brodowski 		if (ret)
147642d4dc3fSBenjamin Herrenschmidt 			printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
14773a3e9e06SViresh Kumar 					"step on CPU %u\n", policy->cpu);
147842d4dc3fSBenjamin Herrenschmidt 	}
147942d4dc3fSBenjamin Herrenschmidt 
14803a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
1481c9060494SDave Jones 	return ret;
148242d4dc3fSBenjamin Herrenschmidt }
148342d4dc3fSBenjamin Herrenschmidt 
148442d4dc3fSBenjamin Herrenschmidt /**
1485e00e56dfSRafael J. Wysocki  * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
14861da177e4SLinus Torvalds  *
14871da177e4SLinus Torvalds  *	1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1488ce6c3997SDominik Brodowski  *	2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1489ce6c3997SDominik Brodowski  *	    restored. It will verify that the current freq is in sync with
1490ce6c3997SDominik Brodowski  *	    what we believe it to be. This is a bit later than when it
1491ce6c3997SDominik Brodowski  *	    should be, but nonethteless it's better than calling
1492ce6c3997SDominik Brodowski  *	    cpufreq_driver->get() here which might re-enable interrupts...
1493e00e56dfSRafael J. Wysocki  *
1494e00e56dfSRafael J. Wysocki  * This function is only executed for the boot CPU.  The other CPUs have not
1495e00e56dfSRafael J. Wysocki  * been turned on yet.
14961da177e4SLinus Torvalds  */
1497e00e56dfSRafael J. Wysocki static void cpufreq_bp_resume(void)
14981da177e4SLinus Torvalds {
1499e08f5f5bSGautham R Shenoy 	int ret = 0;
15004bc5d341SDave Jones 
1501e00e56dfSRafael J. Wysocki 	int cpu = smp_processor_id();
15023a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
15031da177e4SLinus Torvalds 
15042d06d8c4SDominik Brodowski 	pr_debug("resuming cpu %u\n", cpu);
15051da177e4SLinus Torvalds 
1506e00e56dfSRafael J. Wysocki 	/* If there's no policy for the boot CPU, we have nothing to do. */
15073a3e9e06SViresh Kumar 	policy = cpufreq_cpu_get(cpu);
15083a3e9e06SViresh Kumar 	if (!policy)
1509e00e56dfSRafael J. Wysocki 		return;
15101da177e4SLinus Torvalds 
15111c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->resume) {
15123a3e9e06SViresh Kumar 		ret = cpufreq_driver->resume(policy);
15131da177e4SLinus Torvalds 		if (ret) {
15141da177e4SLinus Torvalds 			printk(KERN_ERR "cpufreq: resume failed in ->resume "
15153a3e9e06SViresh Kumar 					"step on CPU %u\n", policy->cpu);
1516c9060494SDave Jones 			goto fail;
15171da177e4SLinus Torvalds 		}
15181da177e4SLinus Torvalds 	}
15191da177e4SLinus Torvalds 
15203a3e9e06SViresh Kumar 	schedule_work(&policy->update);
1521ce6c3997SDominik Brodowski 
1522c9060494SDave Jones fail:
15233a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
15241da177e4SLinus Torvalds }
15251da177e4SLinus Torvalds 
1526e00e56dfSRafael J. Wysocki static struct syscore_ops cpufreq_syscore_ops = {
1527e00e56dfSRafael J. Wysocki 	.suspend	= cpufreq_bp_suspend,
1528e00e56dfSRafael J. Wysocki 	.resume		= cpufreq_bp_resume,
15291da177e4SLinus Torvalds };
15301da177e4SLinus Torvalds 
15319d95046eSBorislav Petkov /**
15329d95046eSBorislav Petkov  *	cpufreq_get_current_driver - return current driver's name
15339d95046eSBorislav Petkov  *
15349d95046eSBorislav Petkov  *	Return the name string of the currently loaded cpufreq driver
15359d95046eSBorislav Petkov  *	or NULL, if none.
15369d95046eSBorislav Petkov  */
15379d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
15389d95046eSBorislav Petkov {
15391c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
15401c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
15411c3d85ddSRafael J. Wysocki 
15421c3d85ddSRafael J. Wysocki 	return NULL;
15439d95046eSBorislav Petkov }
15449d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds /*********************************************************************
15471da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
15481da177e4SLinus Torvalds  *********************************************************************/
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds /**
15511da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
15521da177e4SLinus Torvalds  *	@nb: notifier function to register
15531da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
15541da177e4SLinus Torvalds  *
15551da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
15561da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
15571da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
15581da177e4SLinus Torvalds  *      changes in cpufreq policy.
15591da177e4SLinus Torvalds  *
15601da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1561e041c683SAlan Stern  *	blocking_notifier_chain_register.
15621da177e4SLinus Torvalds  */
15631da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
15641da177e4SLinus Torvalds {
15651da177e4SLinus Torvalds 	int ret;
15661da177e4SLinus Torvalds 
1567d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1568d5aaffa9SDirk Brandewie 		return -EINVAL;
1569d5aaffa9SDirk Brandewie 
157074212ca4SCesar Eduardo Barros 	WARN_ON(!init_cpufreq_transition_notifier_list_called);
157174212ca4SCesar Eduardo Barros 
15721da177e4SLinus Torvalds 	switch (list) {
15731da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1574b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1575e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
15761da177e4SLinus Torvalds 		break;
15771da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1578e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1579e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
15801da177e4SLinus Torvalds 		break;
15811da177e4SLinus Torvalds 	default:
15821da177e4SLinus Torvalds 		ret = -EINVAL;
15831da177e4SLinus Torvalds 	}
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 	return ret;
15861da177e4SLinus Torvalds }
15871da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
15881da177e4SLinus Torvalds 
15891da177e4SLinus Torvalds /**
15901da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
15911da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
15921da177e4SLinus Torvalds  *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
15931da177e4SLinus Torvalds  *
15941da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
15951da177e4SLinus Torvalds  *
15961da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1597e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
15981da177e4SLinus Torvalds  */
15991da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
16001da177e4SLinus Torvalds {
16011da177e4SLinus Torvalds 	int ret;
16021da177e4SLinus Torvalds 
1603d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1604d5aaffa9SDirk Brandewie 		return -EINVAL;
1605d5aaffa9SDirk Brandewie 
16061da177e4SLinus Torvalds 	switch (list) {
16071da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1608b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
1609e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
16101da177e4SLinus Torvalds 		break;
16111da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1612e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1613e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
16141da177e4SLinus Torvalds 		break;
16151da177e4SLinus Torvalds 	default:
16161da177e4SLinus Torvalds 		ret = -EINVAL;
16171da177e4SLinus Torvalds 	}
16181da177e4SLinus Torvalds 
16191da177e4SLinus Torvalds 	return ret;
16201da177e4SLinus Torvalds }
16211da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
16221da177e4SLinus Torvalds 
16231da177e4SLinus Torvalds 
16241da177e4SLinus Torvalds /*********************************************************************
16251da177e4SLinus Torvalds  *                              GOVERNORS                            *
16261da177e4SLinus Torvalds  *********************************************************************/
16271da177e4SLinus Torvalds 
16281da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
16291da177e4SLinus Torvalds 			    unsigned int target_freq,
16301da177e4SLinus Torvalds 			    unsigned int relation)
16311da177e4SLinus Torvalds {
16321da177e4SLinus Torvalds 	int retval = -EINVAL;
16337249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
1634c32b6b8eSAshok Raj 
1635a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1636a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
16377c30ed53SViresh Kumar 	if (policy->transition_ongoing)
16387c30ed53SViresh Kumar 		return -EBUSY;
1639a7b422cdSKonrad Rzeszutek Wilk 
16407249924eSViresh Kumar 	/* Make sure that target_freq is within supported range */
16417249924eSViresh Kumar 	if (target_freq > policy->max)
16427249924eSViresh Kumar 		target_freq = policy->max;
16437249924eSViresh Kumar 	if (target_freq < policy->min)
16447249924eSViresh Kumar 		target_freq = policy->min;
16457249924eSViresh Kumar 
16467249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
16477249924eSViresh Kumar 			policy->cpu, target_freq, relation, old_target_freq);
16485a1c0228SViresh Kumar 
16495a1c0228SViresh Kumar 	if (target_freq == policy->cur)
16505a1c0228SViresh Kumar 		return 0;
16515a1c0228SViresh Kumar 
16521c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
16531c3d85ddSRafael J. Wysocki 		retval = cpufreq_driver->target(policy, target_freq, relation);
165490d45d17SAshok Raj 
16551da177e4SLinus Torvalds 	return retval;
16561da177e4SLinus Torvalds }
16571da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
16601da177e4SLinus Torvalds 			  unsigned int target_freq,
16611da177e4SLinus Torvalds 			  unsigned int relation)
16621da177e4SLinus Torvalds {
1663f1829e4aSJulia Lawall 	int ret = -EINVAL;
16641da177e4SLinus Torvalds 
16655a01f2e8SVenkatesh Pallipadi 	if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1666f1829e4aSJulia Lawall 		goto fail;
16671da177e4SLinus Torvalds 
16681da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
16691da177e4SLinus Torvalds 
16705a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(policy->cpu);
16711da177e4SLinus Torvalds 
1672f1829e4aSJulia Lawall fail:
16731da177e4SLinus Torvalds 	return ret;
16741da177e4SLinus Torvalds }
16751da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
16761da177e4SLinus Torvalds 
1677153d7f3fSArjan van de Ven /*
1678153d7f3fSArjan van de Ven  * when "event" is CPUFREQ_GOV_LIMITS
1679153d7f3fSArjan van de Ven  */
16801da177e4SLinus Torvalds 
1681e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy,
1682e08f5f5bSGautham R Shenoy 					unsigned int event)
16831da177e4SLinus Torvalds {
1684cc993cabSDave Jones 	int ret;
16856afde10cSThomas Renninger 
16866afde10cSThomas Renninger 	/* Only must be defined when default governor is known to have latency
16876afde10cSThomas Renninger 	   restrictions, like e.g. conservative or ondemand.
16886afde10cSThomas Renninger 	   That this is the case is already ensured in Kconfig
16896afde10cSThomas Renninger 	*/
16906afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
16916afde10cSThomas Renninger 	struct cpufreq_governor *gov = &cpufreq_gov_performance;
16926afde10cSThomas Renninger #else
16936afde10cSThomas Renninger 	struct cpufreq_governor *gov = NULL;
16946afde10cSThomas Renninger #endif
16951c256245SThomas Renninger 
16961c256245SThomas Renninger 	if (policy->governor->max_transition_latency &&
16971c256245SThomas Renninger 	    policy->cpuinfo.transition_latency >
16981c256245SThomas Renninger 	    policy->governor->max_transition_latency) {
16996afde10cSThomas Renninger 		if (!gov)
17006afde10cSThomas Renninger 			return -EINVAL;
17016afde10cSThomas Renninger 		else {
17021c256245SThomas Renninger 			printk(KERN_WARNING "%s governor failed, too long"
17031c256245SThomas Renninger 			       " transition latency of HW, fallback"
17041c256245SThomas Renninger 			       " to %s governor\n",
17051c256245SThomas Renninger 			       policy->governor->name,
17061c256245SThomas Renninger 			       gov->name);
17071c256245SThomas Renninger 			policy->governor = gov;
17081c256245SThomas Renninger 		}
17096afde10cSThomas Renninger 	}
17101da177e4SLinus Torvalds 
1711fe492f3fSViresh Kumar 	if (event == CPUFREQ_GOV_POLICY_INIT)
17121da177e4SLinus Torvalds 		if (!try_module_get(policy->governor->owner))
17131da177e4SLinus Torvalds 			return -EINVAL;
17141da177e4SLinus Torvalds 
17152d06d8c4SDominik Brodowski 	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1716e08f5f5bSGautham R Shenoy 						policy->cpu, event);
171795731ebbSXiaoguang Chen 
171895731ebbSXiaoguang Chen 	mutex_lock(&cpufreq_governor_lock);
171995731ebbSXiaoguang Chen 	if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) ||
172095731ebbSXiaoguang Chen 	    (policy->governor_enabled && (event == CPUFREQ_GOV_START))) {
172195731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
1722fe492f3fSViresh Kumar 		if (event == CPUFREQ_GOV_POLICY_INIT)
1723fe492f3fSViresh Kumar 			module_put(policy->governor->owner);
172495731ebbSXiaoguang Chen 		return -EBUSY;
172595731ebbSXiaoguang Chen 	}
172695731ebbSXiaoguang Chen 
172795731ebbSXiaoguang Chen 	if (event == CPUFREQ_GOV_STOP)
172895731ebbSXiaoguang Chen 		policy->governor_enabled = false;
172995731ebbSXiaoguang Chen 	else if (event == CPUFREQ_GOV_START)
173095731ebbSXiaoguang Chen 		policy->governor_enabled = true;
173195731ebbSXiaoguang Chen 
173295731ebbSXiaoguang Chen 	mutex_unlock(&cpufreq_governor_lock);
173395731ebbSXiaoguang Chen 
17341da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
17351da177e4SLinus Torvalds 
17364d5dcc42SViresh Kumar 	if (!ret) {
17374d5dcc42SViresh Kumar 		if (event == CPUFREQ_GOV_POLICY_INIT)
17388e53695fSViresh Kumar 			policy->governor->initialized++;
17394d5dcc42SViresh Kumar 		else if (event == CPUFREQ_GOV_POLICY_EXIT)
17408e53695fSViresh Kumar 			policy->governor->initialized--;
174195731ebbSXiaoguang Chen 	} else {
174295731ebbSXiaoguang Chen 		/* Restore original values */
174395731ebbSXiaoguang Chen 		mutex_lock(&cpufreq_governor_lock);
174495731ebbSXiaoguang Chen 		if (event == CPUFREQ_GOV_STOP)
174595731ebbSXiaoguang Chen 			policy->governor_enabled = true;
174695731ebbSXiaoguang Chen 		else if (event == CPUFREQ_GOV_START)
174795731ebbSXiaoguang Chen 			policy->governor_enabled = false;
174895731ebbSXiaoguang Chen 		mutex_unlock(&cpufreq_governor_lock);
17494d5dcc42SViresh Kumar 	}
1750b394058fSViresh Kumar 
1751fe492f3fSViresh Kumar 	if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1752fe492f3fSViresh Kumar 			((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
17531da177e4SLinus Torvalds 		module_put(policy->governor->owner);
17541da177e4SLinus Torvalds 
17551da177e4SLinus Torvalds 	return ret;
17561da177e4SLinus Torvalds }
17571da177e4SLinus Torvalds 
17581da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
17591da177e4SLinus Torvalds {
17603bcb09a3SJeremy Fitzhardinge 	int err;
17611da177e4SLinus Torvalds 
17621da177e4SLinus Torvalds 	if (!governor)
17631da177e4SLinus Torvalds 		return -EINVAL;
17641da177e4SLinus Torvalds 
1765a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1766a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1767a7b422cdSKonrad Rzeszutek Wilk 
17683fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
17691da177e4SLinus Torvalds 
1770b394058fSViresh Kumar 	governor->initialized = 0;
17713bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
17723bcb09a3SJeremy Fitzhardinge 	if (__find_governor(governor->name) == NULL) {
17733bcb09a3SJeremy Fitzhardinge 		err = 0;
17741da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
17753bcb09a3SJeremy Fitzhardinge 	}
17761da177e4SLinus Torvalds 
17773fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
17783bcb09a3SJeremy Fitzhardinge 	return err;
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
17811da177e4SLinus Torvalds 
17821da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
17831da177e4SLinus Torvalds {
178490e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
178590e41bacSPrarit Bhargava 	int cpu;
178690e41bacSPrarit Bhargava #endif
178790e41bacSPrarit Bhargava 
17881da177e4SLinus Torvalds 	if (!governor)
17891da177e4SLinus Torvalds 		return;
17901da177e4SLinus Torvalds 
1791a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1792a7b422cdSKonrad Rzeszutek Wilk 		return;
1793a7b422cdSKonrad Rzeszutek Wilk 
179490e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU
179590e41bacSPrarit Bhargava 	for_each_present_cpu(cpu) {
179690e41bacSPrarit Bhargava 		if (cpu_online(cpu))
179790e41bacSPrarit Bhargava 			continue;
179890e41bacSPrarit Bhargava 		if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
179990e41bacSPrarit Bhargava 			strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
180090e41bacSPrarit Bhargava 	}
180190e41bacSPrarit Bhargava #endif
180290e41bacSPrarit Bhargava 
18033fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
18041da177e4SLinus Torvalds 	list_del(&governor->governor_list);
18053fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
18061da177e4SLinus Torvalds 	return;
18071da177e4SLinus Torvalds }
18081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
18091da177e4SLinus Torvalds 
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds /*********************************************************************
18121da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
18131da177e4SLinus Torvalds  *********************************************************************/
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds /**
18161da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
181729464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
181829464f28SDave Jones  *	is written
18191da177e4SLinus Torvalds  *
18201da177e4SLinus Torvalds  * Reads the current cpufreq policy.
18211da177e4SLinus Torvalds  */
18221da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
18231da177e4SLinus Torvalds {
18241da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
18251da177e4SLinus Torvalds 	if (!policy)
18261da177e4SLinus Torvalds 		return -EINVAL;
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
18291da177e4SLinus Torvalds 	if (!cpu_policy)
18301da177e4SLinus Torvalds 		return -EINVAL;
18311da177e4SLinus Torvalds 
1832d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
18351da177e4SLinus Torvalds 	return 0;
18361da177e4SLinus Torvalds }
18371da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
18381da177e4SLinus Torvalds 
1839153d7f3fSArjan van de Ven /*
1840e08f5f5bSGautham R Shenoy  * data   : current policy.
1841e08f5f5bSGautham R Shenoy  * policy : policy to be set.
1842153d7f3fSArjan van de Ven  */
18433a3e9e06SViresh Kumar static int __cpufreq_set_policy(struct cpufreq_policy *policy,
18443a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy)
18451da177e4SLinus Torvalds {
18467bd353a9SViresh Kumar 	int ret = 0, failed = 1;
18471da177e4SLinus Torvalds 
18483a3e9e06SViresh Kumar 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
18493a3e9e06SViresh Kumar 		new_policy->min, new_policy->max);
18501da177e4SLinus Torvalds 
1851d5b73cd8SViresh Kumar 	memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
18521da177e4SLinus Torvalds 
18533a3e9e06SViresh Kumar 	if (new_policy->min > policy->max || new_policy->max < policy->min) {
18549c9a43edSMattia Dongili 		ret = -EINVAL;
18559c9a43edSMattia Dongili 		goto error_out;
18569c9a43edSMattia Dongili 	}
18579c9a43edSMattia Dongili 
18581da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
18593a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
18601da177e4SLinus Torvalds 	if (ret)
18611da177e4SLinus Torvalds 		goto error_out;
18621da177e4SLinus Torvalds 
18631da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
1864e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
18653a3e9e06SViresh Kumar 			CPUFREQ_ADJUST, new_policy);
18661da177e4SLinus Torvalds 
18671da177e4SLinus Torvalds 	/* adjust if necessary - hardware incompatibility*/
1868e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
18693a3e9e06SViresh Kumar 			CPUFREQ_INCOMPATIBLE, new_policy);
18701da177e4SLinus Torvalds 
1871bb176f7dSViresh Kumar 	/*
1872bb176f7dSViresh Kumar 	 * verify the cpu speed can be set within this limit, which might be
1873bb176f7dSViresh Kumar 	 * different to the first one
1874bb176f7dSViresh Kumar 	 */
18753a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
1876e041c683SAlan Stern 	if (ret)
18771da177e4SLinus Torvalds 		goto error_out;
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds 	/* notification of the new policy */
1880e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
18813a3e9e06SViresh Kumar 			CPUFREQ_NOTIFY, new_policy);
18821da177e4SLinus Torvalds 
18833a3e9e06SViresh Kumar 	policy->min = new_policy->min;
18843a3e9e06SViresh Kumar 	policy->max = new_policy->max;
18851da177e4SLinus Torvalds 
18862d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
18873a3e9e06SViresh Kumar 					policy->min, policy->max);
18881da177e4SLinus Torvalds 
18891c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
18903a3e9e06SViresh Kumar 		policy->policy = new_policy->policy;
18912d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
18923a3e9e06SViresh Kumar 		ret = cpufreq_driver->setpolicy(new_policy);
18931da177e4SLinus Torvalds 	} else {
18943a3e9e06SViresh Kumar 		if (new_policy->governor != policy->governor) {
18951da177e4SLinus Torvalds 			/* save old, working values */
18963a3e9e06SViresh Kumar 			struct cpufreq_governor *old_gov = policy->governor;
18971da177e4SLinus Torvalds 
18982d06d8c4SDominik Brodowski 			pr_debug("governor switch\n");
18991da177e4SLinus Torvalds 
19001da177e4SLinus Torvalds 			/* end old governor */
19013a3e9e06SViresh Kumar 			if (policy->governor) {
19023a3e9e06SViresh Kumar 				__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
19033a3e9e06SViresh Kumar 				unlock_policy_rwsem_write(new_policy->cpu);
19043a3e9e06SViresh Kumar 				__cpufreq_governor(policy,
19057bd353a9SViresh Kumar 						CPUFREQ_GOV_POLICY_EXIT);
19063a3e9e06SViresh Kumar 				lock_policy_rwsem_write(new_policy->cpu);
19077bd353a9SViresh Kumar 			}
19081da177e4SLinus Torvalds 
19091da177e4SLinus Torvalds 			/* start new governor */
19103a3e9e06SViresh Kumar 			policy->governor = new_policy->governor;
19113a3e9e06SViresh Kumar 			if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
19123a3e9e06SViresh Kumar 				if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
19137bd353a9SViresh Kumar 					failed = 0;
1914955ef483SViresh Kumar 				} else {
19153a3e9e06SViresh Kumar 					unlock_policy_rwsem_write(new_policy->cpu);
19163a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
19177bd353a9SViresh Kumar 							CPUFREQ_GOV_POLICY_EXIT);
19183a3e9e06SViresh Kumar 					lock_policy_rwsem_write(new_policy->cpu);
1919955ef483SViresh Kumar 				}
19207bd353a9SViresh Kumar 			}
19217bd353a9SViresh Kumar 
19227bd353a9SViresh Kumar 			if (failed) {
19231da177e4SLinus Torvalds 				/* new governor failed, so re-start old one */
19242d06d8c4SDominik Brodowski 				pr_debug("starting governor %s failed\n",
19253a3e9e06SViresh Kumar 							policy->governor->name);
19261da177e4SLinus Torvalds 				if (old_gov) {
19273a3e9e06SViresh Kumar 					policy->governor = old_gov;
19283a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
19297bd353a9SViresh Kumar 							CPUFREQ_GOV_POLICY_INIT);
19303a3e9e06SViresh Kumar 					__cpufreq_governor(policy,
1931e08f5f5bSGautham R Shenoy 							   CPUFREQ_GOV_START);
19321da177e4SLinus Torvalds 				}
19331da177e4SLinus Torvalds 				ret = -EINVAL;
19341da177e4SLinus Torvalds 				goto error_out;
19351da177e4SLinus Torvalds 			}
19361da177e4SLinus Torvalds 			/* might be a policy change, too, so fall through */
19371da177e4SLinus Torvalds 		}
19382d06d8c4SDominik Brodowski 		pr_debug("governor: change or update limits\n");
19393de9bdebSViresh Kumar 		ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
19401da177e4SLinus Torvalds 	}
19411da177e4SLinus Torvalds 
19421da177e4SLinus Torvalds error_out:
19431da177e4SLinus Torvalds 	return ret;
19441da177e4SLinus Torvalds }
19451da177e4SLinus Torvalds 
19461da177e4SLinus Torvalds /**
19471da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
19481da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
19491da177e4SLinus Torvalds  *
195025985edcSLucas De Marchi  *	Useful for policy notifiers which have different necessities
19511da177e4SLinus Torvalds  *	at different times.
19521da177e4SLinus Torvalds  */
19531da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
19541da177e4SLinus Torvalds {
19553a3e9e06SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
19563a3e9e06SViresh Kumar 	struct cpufreq_policy new_policy;
1957f1829e4aSJulia Lawall 	int ret;
19581da177e4SLinus Torvalds 
19593a3e9e06SViresh Kumar 	if (!policy) {
1960f1829e4aSJulia Lawall 		ret = -ENODEV;
1961f1829e4aSJulia Lawall 		goto no_policy;
1962f1829e4aSJulia Lawall 	}
19631da177e4SLinus Torvalds 
1964f1829e4aSJulia Lawall 	if (unlikely(lock_policy_rwsem_write(cpu))) {
1965f1829e4aSJulia Lawall 		ret = -EINVAL;
1966f1829e4aSJulia Lawall 		goto fail;
1967f1829e4aSJulia Lawall 	}
19681da177e4SLinus Torvalds 
19692d06d8c4SDominik Brodowski 	pr_debug("updating policy for CPU %u\n", cpu);
1970d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
19713a3e9e06SViresh Kumar 	new_policy.min = policy->user_policy.min;
19723a3e9e06SViresh Kumar 	new_policy.max = policy->user_policy.max;
19733a3e9e06SViresh Kumar 	new_policy.policy = policy->user_policy.policy;
19743a3e9e06SViresh Kumar 	new_policy.governor = policy->user_policy.governor;
19751da177e4SLinus Torvalds 
1976bb176f7dSViresh Kumar 	/*
1977bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
1978bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
1979bb176f7dSViresh Kumar 	 */
19801c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
19813a3e9e06SViresh Kumar 		new_policy.cur = cpufreq_driver->get(cpu);
19823a3e9e06SViresh Kumar 		if (!policy->cur) {
19832d06d8c4SDominik Brodowski 			pr_debug("Driver did not initialize current freq");
19843a3e9e06SViresh Kumar 			policy->cur = new_policy.cur;
1985a85f7bd3SThomas Renninger 		} else {
19863a3e9e06SViresh Kumar 			if (policy->cur != new_policy.cur && cpufreq_driver->target)
19873a3e9e06SViresh Kumar 				cpufreq_out_of_sync(cpu, policy->cur,
19883a3e9e06SViresh Kumar 								new_policy.cur);
19890961dd0dSThomas Renninger 		}
1990a85f7bd3SThomas Renninger 	}
19910961dd0dSThomas Renninger 
19923a3e9e06SViresh Kumar 	ret = __cpufreq_set_policy(policy, &new_policy);
19931da177e4SLinus Torvalds 
19945a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(cpu);
19955a01f2e8SVenkatesh Pallipadi 
1996f1829e4aSJulia Lawall fail:
19973a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
1998f1829e4aSJulia Lawall no_policy:
19991da177e4SLinus Torvalds 	return ret;
20001da177e4SLinus Torvalds }
20011da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
20021da177e4SLinus Torvalds 
20032760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb,
2004c32b6b8eSAshok Raj 					unsigned long action, void *hcpu)
2005c32b6b8eSAshok Raj {
2006c32b6b8eSAshok Raj 	unsigned int cpu = (unsigned long)hcpu;
20078a25a2fdSKay Sievers 	struct device *dev;
20085302c3fbSSrivatsa S. Bhat 	bool frozen = false;
2009c32b6b8eSAshok Raj 
20108a25a2fdSKay Sievers 	dev = get_cpu_device(cpu);
20118a25a2fdSKay Sievers 	if (dev) {
20125302c3fbSSrivatsa S. Bhat 
20135302c3fbSSrivatsa S. Bhat 		if (action & CPU_TASKS_FROZEN)
20145302c3fbSSrivatsa S. Bhat 			frozen = true;
20155302c3fbSSrivatsa S. Bhat 
20165302c3fbSSrivatsa S. Bhat 		switch (action & ~CPU_TASKS_FROZEN) {
2017c32b6b8eSAshok Raj 		case CPU_ONLINE:
20185302c3fbSSrivatsa S. Bhat 			__cpufreq_add_dev(dev, NULL, frozen);
201923d32899SSrivatsa S. Bhat 			cpufreq_update_policy(cpu);
2020c32b6b8eSAshok Raj 			break;
20215302c3fbSSrivatsa S. Bhat 
2022c32b6b8eSAshok Raj 		case CPU_DOWN_PREPARE:
20235302c3fbSSrivatsa S. Bhat 			__cpufreq_remove_dev(dev, NULL, frozen);
2024c32b6b8eSAshok Raj 			break;
20255302c3fbSSrivatsa S. Bhat 
20265a01f2e8SVenkatesh Pallipadi 		case CPU_DOWN_FAILED:
20275302c3fbSSrivatsa S. Bhat 			__cpufreq_add_dev(dev, NULL, frozen);
2028c32b6b8eSAshok Raj 			break;
2029c32b6b8eSAshok Raj 		}
2030c32b6b8eSAshok Raj 	}
2031c32b6b8eSAshok Raj 	return NOTIFY_OK;
2032c32b6b8eSAshok Raj }
2033c32b6b8eSAshok Raj 
20349c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = {
2035c32b6b8eSAshok Raj 	.notifier_call = cpufreq_cpu_callback,
2036c32b6b8eSAshok Raj };
20371da177e4SLinus Torvalds 
20381da177e4SLinus Torvalds /*********************************************************************
20391da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
20401da177e4SLinus Torvalds  *********************************************************************/
20411da177e4SLinus Torvalds 
20421da177e4SLinus Torvalds /**
20431da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
20441da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
20451da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
20461da177e4SLinus Torvalds  *
20471da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
20481da177e4SLinus Torvalds  * returns zero on success, -EBUSY when another driver got here first
20491da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
20501da177e4SLinus Torvalds  *
20511da177e4SLinus Torvalds  */
2052221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
20531da177e4SLinus Torvalds {
20541da177e4SLinus Torvalds 	unsigned long flags;
20551da177e4SLinus Torvalds 	int ret;
20561da177e4SLinus Torvalds 
2057a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2058a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2059a7b422cdSKonrad Rzeszutek Wilk 
20601da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
20611da177e4SLinus Torvalds 	    ((!driver_data->setpolicy) && (!driver_data->target)))
20621da177e4SLinus Torvalds 		return -EINVAL;
20631da177e4SLinus Torvalds 
20642d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
20651da177e4SLinus Torvalds 
20661da177e4SLinus Torvalds 	if (driver_data->setpolicy)
20671da177e4SLinus Torvalds 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
20681da177e4SLinus Torvalds 
20690d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
20701c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
20710d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
20721da177e4SLinus Torvalds 		return -EBUSY;
20731da177e4SLinus Torvalds 	}
20741c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
20750d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
20761da177e4SLinus Torvalds 
20778a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
20788f5bc2abSJiri Slaby 	if (ret)
20798f5bc2abSJiri Slaby 		goto err_null_driver;
20801da177e4SLinus Torvalds 
20811c3d85ddSRafael J. Wysocki 	if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
20821da177e4SLinus Torvalds 		int i;
20831da177e4SLinus Torvalds 		ret = -ENODEV;
20841da177e4SLinus Torvalds 
20851da177e4SLinus Torvalds 		/* check for at least one working CPU */
20867a6aedfaSMike Travis 		for (i = 0; i < nr_cpu_ids; i++)
20877a6aedfaSMike Travis 			if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
20881da177e4SLinus Torvalds 				ret = 0;
20897a6aedfaSMike Travis 				break;
20907a6aedfaSMike Travis 			}
20911da177e4SLinus Torvalds 
20921da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
20931da177e4SLinus Torvalds 		if (ret) {
20942d06d8c4SDominik Brodowski 			pr_debug("no CPU initialized for driver %s\n",
2095e08f5f5bSGautham R Shenoy 							driver_data->name);
20968a25a2fdSKay Sievers 			goto err_if_unreg;
20971da177e4SLinus Torvalds 		}
20981da177e4SLinus Torvalds 	}
20991da177e4SLinus Torvalds 
210065edc68cSChandra Seetharaman 	register_hotcpu_notifier(&cpufreq_cpu_notifier);
21012d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
21021da177e4SLinus Torvalds 
21038f5bc2abSJiri Slaby 	return 0;
21048a25a2fdSKay Sievers err_if_unreg:
21058a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
21068f5bc2abSJiri Slaby err_null_driver:
21070d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
21081c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
21090d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
21104d34a67dSDave Jones 	return ret;
21111da177e4SLinus Torvalds }
21121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
21131da177e4SLinus Torvalds 
21141da177e4SLinus Torvalds /**
21151da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
21161da177e4SLinus Torvalds  *
21171da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
21181da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
21191da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
21201da177e4SLinus Torvalds  * currently not initialised.
21211da177e4SLinus Torvalds  */
2122221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
21231da177e4SLinus Torvalds {
21241da177e4SLinus Torvalds 	unsigned long flags;
21251da177e4SLinus Torvalds 
21261c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
21271da177e4SLinus Torvalds 		return -EINVAL;
21281da177e4SLinus Torvalds 
21292d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
21301da177e4SLinus Torvalds 
21318a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
213265edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
21331da177e4SLinus Torvalds 
21346eed9404SViresh Kumar 	down_write(&cpufreq_rwsem);
21350d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
21366eed9404SViresh Kumar 
21371c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
21386eed9404SViresh Kumar 
21390d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
21406eed9404SViresh Kumar 	up_write(&cpufreq_rwsem);
21411da177e4SLinus Torvalds 
21421da177e4SLinus Torvalds 	return 0;
21431da177e4SLinus Torvalds }
21441da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
21455a01f2e8SVenkatesh Pallipadi 
21465a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
21475a01f2e8SVenkatesh Pallipadi {
21485a01f2e8SVenkatesh Pallipadi 	int cpu;
21495a01f2e8SVenkatesh Pallipadi 
2150a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2151a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2152a7b422cdSKonrad Rzeszutek Wilk 
21535a01f2e8SVenkatesh Pallipadi 	for_each_possible_cpu(cpu) {
2154f1625066STejun Heo 		per_cpu(cpufreq_policy_cpu, cpu) = -1;
21555a01f2e8SVenkatesh Pallipadi 		init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
21565a01f2e8SVenkatesh Pallipadi 	}
21578aa84ad8SThomas Renninger 
21582361be23SViresh Kumar 	cpufreq_global_kobject = kobject_create();
21598aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
2160e00e56dfSRafael J. Wysocki 	register_syscore_ops(&cpufreq_syscore_ops);
21618aa84ad8SThomas Renninger 
21625a01f2e8SVenkatesh Pallipadi 	return 0;
21635a01f2e8SVenkatesh Pallipadi }
21645a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2165