xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision ec28297a562f2b022115b9eb82e4ea724d996240)
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>
61da177e4SLinus Torvalds  *
7c32b6b8eSAshok Raj  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
8c32b6b8eSAshok Raj  *	Added handling for CPU hotplug
98ff69732SDave Jones  *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
108ff69732SDave Jones  *	Fix handling for CPU hotplug -- affected CPUs
11c32b6b8eSAshok Raj  *
121da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
131da177e4SLinus Torvalds  * it under the terms of the GNU General Public License version 2 as
141da177e4SLinus Torvalds  * published by the Free Software Foundation.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <linux/kernel.h>
191da177e4SLinus Torvalds #include <linux/module.h>
201da177e4SLinus Torvalds #include <linux/init.h>
211da177e4SLinus Torvalds #include <linux/notifier.h>
221da177e4SLinus Torvalds #include <linux/cpufreq.h>
231da177e4SLinus Torvalds #include <linux/delay.h>
241da177e4SLinus Torvalds #include <linux/interrupt.h>
251da177e4SLinus Torvalds #include <linux/spinlock.h>
261da177e4SLinus Torvalds #include <linux/device.h>
271da177e4SLinus Torvalds #include <linux/slab.h>
281da177e4SLinus Torvalds #include <linux/cpu.h>
291da177e4SLinus Torvalds #include <linux/completion.h>
303fc54d37Sakpm@osdl.org #include <linux/mutex.h>
311da177e4SLinus Torvalds 
32e08f5f5bSGautham R Shenoy #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \
33e08f5f5bSGautham R Shenoy 						"cpufreq-core", msg)
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds /**
36cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
371da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
381da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
391da177e4SLinus Torvalds  */
401da177e4SLinus Torvalds static struct cpufreq_driver *cpufreq_driver;
411da177e4SLinus Torvalds static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
421da177e4SLinus Torvalds static DEFINE_SPINLOCK(cpufreq_driver_lock);
431da177e4SLinus Torvalds 
445a01f2e8SVenkatesh Pallipadi /*
455a01f2e8SVenkatesh Pallipadi  * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
465a01f2e8SVenkatesh Pallipadi  * all cpufreq/hotplug/workqueue/etc related lock issues.
475a01f2e8SVenkatesh Pallipadi  *
485a01f2e8SVenkatesh Pallipadi  * The rules for this semaphore:
495a01f2e8SVenkatesh Pallipadi  * - Any routine that wants to read from the policy structure will
505a01f2e8SVenkatesh Pallipadi  *   do a down_read on this semaphore.
515a01f2e8SVenkatesh Pallipadi  * - Any routine that will write to the policy structure and/or may take away
525a01f2e8SVenkatesh Pallipadi  *   the policy altogether (eg. CPU hotplug), will hold this lock in write
535a01f2e8SVenkatesh Pallipadi  *   mode before doing so.
545a01f2e8SVenkatesh Pallipadi  *
555a01f2e8SVenkatesh Pallipadi  * Additional rules:
565a01f2e8SVenkatesh Pallipadi  * - All holders of the lock should check to make sure that the CPU they
575a01f2e8SVenkatesh Pallipadi  *   are concerned with are online after they get the lock.
585a01f2e8SVenkatesh Pallipadi  * - Governor routines that can be called in cpufreq hotplug path should not
595a01f2e8SVenkatesh Pallipadi  *   take this sem as top level hotplug notifier handler takes this.
605a01f2e8SVenkatesh Pallipadi  */
615a01f2e8SVenkatesh Pallipadi static DEFINE_PER_CPU(int, policy_cpu);
625a01f2e8SVenkatesh Pallipadi static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
635a01f2e8SVenkatesh Pallipadi 
645a01f2e8SVenkatesh Pallipadi #define lock_policy_rwsem(mode, cpu)					\
655a01f2e8SVenkatesh Pallipadi int lock_policy_rwsem_##mode						\
665a01f2e8SVenkatesh Pallipadi (int cpu)								\
675a01f2e8SVenkatesh Pallipadi {									\
685a01f2e8SVenkatesh Pallipadi 	int policy_cpu = per_cpu(policy_cpu, cpu);			\
695a01f2e8SVenkatesh Pallipadi 	BUG_ON(policy_cpu == -1);					\
705a01f2e8SVenkatesh Pallipadi 	down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));		\
715a01f2e8SVenkatesh Pallipadi 	if (unlikely(!cpu_online(cpu))) {				\
725a01f2e8SVenkatesh Pallipadi 		up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));	\
735a01f2e8SVenkatesh Pallipadi 		return -1;						\
745a01f2e8SVenkatesh Pallipadi 	}								\
755a01f2e8SVenkatesh Pallipadi 									\
765a01f2e8SVenkatesh Pallipadi 	return 0;							\
775a01f2e8SVenkatesh Pallipadi }
785a01f2e8SVenkatesh Pallipadi 
795a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(read, cpu);
805a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(lock_policy_rwsem_read);
815a01f2e8SVenkatesh Pallipadi 
825a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(write, cpu);
835a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(lock_policy_rwsem_write);
845a01f2e8SVenkatesh Pallipadi 
855a01f2e8SVenkatesh Pallipadi void unlock_policy_rwsem_read(int cpu)
865a01f2e8SVenkatesh Pallipadi {
875a01f2e8SVenkatesh Pallipadi 	int policy_cpu = per_cpu(policy_cpu, cpu);
885a01f2e8SVenkatesh Pallipadi 	BUG_ON(policy_cpu == -1);
895a01f2e8SVenkatesh Pallipadi 	up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
905a01f2e8SVenkatesh Pallipadi }
915a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(unlock_policy_rwsem_read);
925a01f2e8SVenkatesh Pallipadi 
935a01f2e8SVenkatesh Pallipadi void unlock_policy_rwsem_write(int cpu)
945a01f2e8SVenkatesh Pallipadi {
955a01f2e8SVenkatesh Pallipadi 	int policy_cpu = per_cpu(policy_cpu, cpu);
965a01f2e8SVenkatesh Pallipadi 	BUG_ON(policy_cpu == -1);
975a01f2e8SVenkatesh Pallipadi 	up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
985a01f2e8SVenkatesh Pallipadi }
995a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(unlock_policy_rwsem_write);
1005a01f2e8SVenkatesh Pallipadi 
1015a01f2e8SVenkatesh Pallipadi 
1021da177e4SLinus Torvalds /* internal prototypes */
1031da177e4SLinus Torvalds static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
1045a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu);
10565f27f38SDavid Howells static void handle_update(struct work_struct *work);
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds /**
1081da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
1091da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
1101da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
1111da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
1121da177e4SLinus Torvalds  * The mutex locks both lists.
1131da177e4SLinus Torvalds  */
114e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
115b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list;
1161da177e4SLinus Torvalds 
117b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void)
118b4dfdbb3SAlan Stern {
119b4dfdbb3SAlan Stern 	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
120b4dfdbb3SAlan Stern 	return 0;
121b4dfdbb3SAlan Stern }
122b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list);
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list);
1253fc54d37Sakpm@osdl.org static DEFINE_MUTEX (cpufreq_governor_mutex);
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	struct cpufreq_policy *data;
1301da177e4SLinus Torvalds 	unsigned long flags;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	if (cpu >= NR_CPUS)
1331da177e4SLinus Torvalds 		goto err_out;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	/* get the cpufreq driver */
1361da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	if (!cpufreq_driver)
1391da177e4SLinus Torvalds 		goto err_out_unlock;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	if (!try_module_get(cpufreq_driver->owner))
1421da177e4SLinus Torvalds 		goto err_out_unlock;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	/* get the CPU */
1461da177e4SLinus Torvalds 	data = cpufreq_cpu_data[cpu];
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds 	if (!data)
1491da177e4SLinus Torvalds 		goto err_out_put_module;
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 	if (!kobject_get(&data->kobj))
1521da177e4SLinus Torvalds 		goto err_out_put_module;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1551da177e4SLinus Torvalds 	return data;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds err_out_put_module:
1581da177e4SLinus Torvalds 	module_put(cpufreq_driver->owner);
1591da177e4SLinus Torvalds err_out_unlock:
1601da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1611da177e4SLinus Torvalds err_out:
1621da177e4SLinus Torvalds 	return NULL;
1631da177e4SLinus Torvalds }
1641da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
1651da177e4SLinus Torvalds 
1667d5e350fSDave Jones 
1671da177e4SLinus Torvalds void cpufreq_cpu_put(struct cpufreq_policy *data)
1681da177e4SLinus Torvalds {
1691da177e4SLinus Torvalds 	kobject_put(&data->kobj);
1701da177e4SLinus Torvalds 	module_put(cpufreq_driver->owner);
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /*********************************************************************
1761da177e4SLinus Torvalds  *                     UNIFIED DEBUG HELPERS                         *
1771da177e4SLinus Torvalds  *********************************************************************/
1781da177e4SLinus Torvalds #ifdef CONFIG_CPU_FREQ_DEBUG
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /* what part(s) of the CPUfreq subsystem are debugged? */
1811da177e4SLinus Torvalds static unsigned int debug;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /* is the debug output ratelimit'ed using printk_ratelimit? User can
1841da177e4SLinus Torvalds  * set or modify this value.
1851da177e4SLinus Torvalds  */
1861da177e4SLinus Torvalds static unsigned int debug_ratelimit = 1;
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds /* is the printk_ratelimit'ing enabled? It's enabled after a successful
1891da177e4SLinus Torvalds  * loading of a cpufreq driver, temporarily disabled when a new policy
1901da177e4SLinus Torvalds  * is set, and disabled upon cpufreq driver removal
1911da177e4SLinus Torvalds  */
1921da177e4SLinus Torvalds static unsigned int disable_ratelimit = 1;
1931da177e4SLinus Torvalds static DEFINE_SPINLOCK(disable_ratelimit_lock);
1941da177e4SLinus Torvalds 
195858119e1SArjan van de Ven static void cpufreq_debug_enable_ratelimit(void)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	unsigned long flags;
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 	spin_lock_irqsave(&disable_ratelimit_lock, flags);
2001da177e4SLinus Torvalds 	if (disable_ratelimit)
2011da177e4SLinus Torvalds 		disable_ratelimit--;
2021da177e4SLinus Torvalds 	spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
2031da177e4SLinus Torvalds }
2041da177e4SLinus Torvalds 
205858119e1SArjan van de Ven static void cpufreq_debug_disable_ratelimit(void)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	unsigned long flags;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	spin_lock_irqsave(&disable_ratelimit_lock, flags);
2101da177e4SLinus Torvalds 	disable_ratelimit++;
2111da177e4SLinus Torvalds 	spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
214e08f5f5bSGautham R Shenoy void cpufreq_debug_printk(unsigned int type, const char *prefix,
215e08f5f5bSGautham R Shenoy 							const char *fmt, ...)
2161da177e4SLinus Torvalds {
2171da177e4SLinus Torvalds 	char s[256];
2181da177e4SLinus Torvalds 	va_list args;
2191da177e4SLinus Torvalds 	unsigned int len;
2201da177e4SLinus Torvalds 	unsigned long flags;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	WARN_ON(!prefix);
2231da177e4SLinus Torvalds 	if (type & debug) {
2241da177e4SLinus Torvalds 		spin_lock_irqsave(&disable_ratelimit_lock, flags);
225e08f5f5bSGautham R Shenoy 		if (!disable_ratelimit && debug_ratelimit
226e08f5f5bSGautham R Shenoy 					&& !printk_ratelimit()) {
2271da177e4SLinus Torvalds 			spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
2281da177e4SLinus Torvalds 			return;
2291da177e4SLinus Torvalds 		}
2301da177e4SLinus Torvalds 		spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 		len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 		va_start(args, fmt);
2351da177e4SLinus Torvalds 		len += vsnprintf(&s[len], (256 - len), fmt, args);
2361da177e4SLinus Torvalds 		va_end(args);
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 		printk(s);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 		WARN_ON(len < 5);
2411da177e4SLinus Torvalds 	}
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_debug_printk);
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds module_param(debug, uint, 0644);
247e08f5f5bSGautham R Shenoy MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
248e08f5f5bSGautham R Shenoy 			" 2 to debug drivers, and 4 to debug governors.");
2491da177e4SLinus Torvalds 
2501da177e4SLinus Torvalds module_param(debug_ratelimit, uint, 0644);
251e08f5f5bSGautham R Shenoy MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
252e08f5f5bSGautham R Shenoy 					" set to 0 to disable ratelimiting.");
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds #else /* !CONFIG_CPU_FREQ_DEBUG */
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds static inline void cpufreq_debug_enable_ratelimit(void) { return; }
2571da177e4SLinus Torvalds static inline void cpufreq_debug_disable_ratelimit(void) { return; }
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds #endif /* CONFIG_CPU_FREQ_DEBUG */
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds /*********************************************************************
2631da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2641da177e4SLinus Torvalds  *********************************************************************/
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds /**
2671da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
2681da177e4SLinus Torvalds  *
2691da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
2701da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
2711da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
2721da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
2731da177e4SLinus Torvalds  */
2741da177e4SLinus Torvalds #ifndef CONFIG_SMP
2751da177e4SLinus Torvalds static unsigned long l_p_j_ref;
2761da177e4SLinus Torvalds static unsigned int  l_p_j_ref_freq;
2771da177e4SLinus Torvalds 
278858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
2811da177e4SLinus Torvalds 		return;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
2841da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
2851da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
286e08f5f5bSGautham R Shenoy 		dprintk("saving %lu as reference value for loops_per_jiffy;"
287e08f5f5bSGautham R Shenoy 			"freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
2881da177e4SLinus Torvalds 	}
2891da177e4SLinus Torvalds 	if ((val == CPUFREQ_PRECHANGE  && ci->old < ci->new) ||
2901da177e4SLinus Torvalds 	    (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
29142d4dc3fSBenjamin Herrenschmidt 	    (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
292e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
293e08f5f5bSGautham R Shenoy 								ci->new);
294e08f5f5bSGautham R Shenoy 		dprintk("scaling loops_per_jiffy to %lu"
295e08f5f5bSGautham R Shenoy 			"for frequency %u kHz\n", loops_per_jiffy, ci->new);
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds #else
299e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
300e08f5f5bSGautham R Shenoy {
301e08f5f5bSGautham R Shenoy 	return;
302e08f5f5bSGautham R Shenoy }
3031da177e4SLinus Torvalds #endif
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 
3061da177e4SLinus Torvalds /**
307e4472cb3SDave Jones  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
308e4472cb3SDave Jones  * on frequency transition.
3091da177e4SLinus Torvalds  *
310e4472cb3SDave Jones  * This function calls the transition notifiers and the "adjust_jiffies"
311e4472cb3SDave Jones  * function. It is called twice on all CPU frequency changes that have
312e4472cb3SDave Jones  * external effects.
3131da177e4SLinus Torvalds  */
3141da177e4SLinus Torvalds void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
3151da177e4SLinus Torvalds {
316e4472cb3SDave Jones 	struct cpufreq_policy *policy;
317e4472cb3SDave Jones 
3181da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	freqs->flags = cpufreq_driver->flags;
321e4472cb3SDave Jones 	dprintk("notification %u of frequency transition to %u kHz\n",
322e4472cb3SDave Jones 		state, freqs->new);
3231da177e4SLinus Torvalds 
324e4472cb3SDave Jones 	policy = cpufreq_cpu_data[freqs->cpu];
3251da177e4SLinus Torvalds 	switch (state) {
326e4472cb3SDave Jones 
3271da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
328e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
329e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
330e4472cb3SDave Jones 		 * "old frequency".
3311da177e4SLinus Torvalds 		 */
3321da177e4SLinus Torvalds 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
333e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
334e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
335b10eec22SJan Beulich 				dprintk("Warning: CPU frequency is"
336e4472cb3SDave Jones 					" %u, cpufreq assumed %u kHz.\n",
337e4472cb3SDave Jones 					freqs->old, policy->cur);
338e4472cb3SDave Jones 				freqs->old = policy->cur;
3391da177e4SLinus Torvalds 			}
3401da177e4SLinus Torvalds 		}
341b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
342e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
3431da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3441da177e4SLinus Torvalds 		break;
345e4472cb3SDave Jones 
3461da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3471da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
348b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
349e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
350e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
351e4472cb3SDave Jones 			policy->cur = freqs->new;
3521da177e4SLinus Torvalds 		break;
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds /*********************************************************************
3601da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
3611da177e4SLinus Torvalds  *********************************************************************/
3621da177e4SLinus Torvalds 
3633bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor)
3643bcb09a3SJeremy Fitzhardinge {
3653bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
3663bcb09a3SJeremy Fitzhardinge 
3673bcb09a3SJeremy Fitzhardinge 	list_for_each_entry(t, &cpufreq_governor_list, governor_list)
3683bcb09a3SJeremy Fitzhardinge 		if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN))
3693bcb09a3SJeremy Fitzhardinge 			return t;
3703bcb09a3SJeremy Fitzhardinge 
3713bcb09a3SJeremy Fitzhardinge 	return NULL;
3723bcb09a3SJeremy Fitzhardinge }
3733bcb09a3SJeremy Fitzhardinge 
3741da177e4SLinus Torvalds /**
3751da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
3761da177e4SLinus Torvalds  */
3771da177e4SLinus Torvalds static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
3781da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
3791da177e4SLinus Torvalds {
3803bcb09a3SJeremy Fitzhardinge 	int err = -EINVAL;
3813bcb09a3SJeremy Fitzhardinge 
3821da177e4SLinus Torvalds 	if (!cpufreq_driver)
3833bcb09a3SJeremy Fitzhardinge 		goto out;
3843bcb09a3SJeremy Fitzhardinge 
3851da177e4SLinus Torvalds 	if (cpufreq_driver->setpolicy) {
3861da177e4SLinus Torvalds 		if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
3871da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
3883bcb09a3SJeremy Fitzhardinge 			err = 0;
389e08f5f5bSGautham R Shenoy 		} else if (!strnicmp(str_governor, "powersave",
390e08f5f5bSGautham R Shenoy 						CPUFREQ_NAME_LEN)) {
3911da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
3923bcb09a3SJeremy Fitzhardinge 			err = 0;
3931da177e4SLinus Torvalds 		}
3943bcb09a3SJeremy Fitzhardinge 	} else if (cpufreq_driver->target) {
3951da177e4SLinus Torvalds 		struct cpufreq_governor *t;
3963bcb09a3SJeremy Fitzhardinge 
3973fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
3983bcb09a3SJeremy Fitzhardinge 
3993bcb09a3SJeremy Fitzhardinge 		t = __find_governor(str_governor);
4003bcb09a3SJeremy Fitzhardinge 
401ea714970SJeremy Fitzhardinge 		if (t == NULL) {
402e08f5f5bSGautham R Shenoy 			char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
403e08f5f5bSGautham R Shenoy 								str_governor);
404ea714970SJeremy Fitzhardinge 
405ea714970SJeremy Fitzhardinge 			if (name) {
406ea714970SJeremy Fitzhardinge 				int ret;
407ea714970SJeremy Fitzhardinge 
408ea714970SJeremy Fitzhardinge 				mutex_unlock(&cpufreq_governor_mutex);
409ea714970SJeremy Fitzhardinge 				ret = request_module(name);
410ea714970SJeremy Fitzhardinge 				mutex_lock(&cpufreq_governor_mutex);
411ea714970SJeremy Fitzhardinge 
412ea714970SJeremy Fitzhardinge 				if (ret == 0)
413ea714970SJeremy Fitzhardinge 					t = __find_governor(str_governor);
414ea714970SJeremy Fitzhardinge 			}
415ea714970SJeremy Fitzhardinge 
416ea714970SJeremy Fitzhardinge 			kfree(name);
417ea714970SJeremy Fitzhardinge 		}
418ea714970SJeremy Fitzhardinge 
4193bcb09a3SJeremy Fitzhardinge 		if (t != NULL) {
4201da177e4SLinus Torvalds 			*governor = t;
4213bcb09a3SJeremy Fitzhardinge 			err = 0;
4221da177e4SLinus Torvalds 		}
4233bcb09a3SJeremy Fitzhardinge 
4243bcb09a3SJeremy Fitzhardinge 		mutex_unlock(&cpufreq_governor_mutex);
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds   out:
4273bcb09a3SJeremy Fitzhardinge 	return err;
4281da177e4SLinus Torvalds }
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds /* drivers/base/cpu.c */
4321da177e4SLinus Torvalds extern struct sysdev_class cpu_sysdev_class;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds /**
436e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
437e08f5f5bSGautham R Shenoy  * print out cpufreq information
4381da177e4SLinus Torvalds  *
4391da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
4401da177e4SLinus Torvalds  * "unsigned int".
4411da177e4SLinus Torvalds  */
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds #define show_one(file_name, object)			\
4441da177e4SLinus Torvalds static ssize_t show_##file_name				\
4451da177e4SLinus Torvalds (struct cpufreq_policy * policy, char *buf)		\
4461da177e4SLinus Torvalds {							\
4471da177e4SLinus Torvalds 	return sprintf (buf, "%u\n", policy->object);	\
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
4511da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
4521da177e4SLinus Torvalds show_one(scaling_min_freq, min);
4531da177e4SLinus Torvalds show_one(scaling_max_freq, max);
4541da177e4SLinus Torvalds show_one(scaling_cur_freq, cur);
4551da177e4SLinus Torvalds 
456e08f5f5bSGautham R Shenoy static int __cpufreq_set_policy(struct cpufreq_policy *data,
457e08f5f5bSGautham R Shenoy 				struct cpufreq_policy *policy);
4587970e08bSThomas Renninger 
4591da177e4SLinus Torvalds /**
4601da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
4611da177e4SLinus Torvalds  */
4621da177e4SLinus Torvalds #define store_one(file_name, object)			\
4631da177e4SLinus Torvalds static ssize_t store_##file_name					\
4641da177e4SLinus Torvalds (struct cpufreq_policy * policy, const char *buf, size_t count)		\
4651da177e4SLinus Torvalds {									\
4661da177e4SLinus Torvalds 	unsigned int ret = -EINVAL;					\
4671da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
4681da177e4SLinus Torvalds 									\
4691da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
4701da177e4SLinus Torvalds 	if (ret)							\
4711da177e4SLinus Torvalds 		return -EINVAL;						\
4721da177e4SLinus Torvalds 									\
4731da177e4SLinus Torvalds 	ret = sscanf (buf, "%u", &new_policy.object);			\
4741da177e4SLinus Torvalds 	if (ret != 1)							\
4751da177e4SLinus Torvalds 		return -EINVAL;						\
4761da177e4SLinus Torvalds 									\
4777970e08bSThomas Renninger 	ret = __cpufreq_set_policy(policy, &new_policy);		\
4787970e08bSThomas Renninger 	policy->user_policy.object = policy->object;			\
4791da177e4SLinus Torvalds 									\
4801da177e4SLinus Torvalds 	return ret ? ret : count;					\
4811da177e4SLinus Torvalds }
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds store_one(scaling_min_freq,min);
4841da177e4SLinus Torvalds store_one(scaling_max_freq,max);
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds /**
4871da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
4881da177e4SLinus Torvalds  */
489e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy,
490e08f5f5bSGautham R Shenoy 							char *buf)
4911da177e4SLinus Torvalds {
4925a01f2e8SVenkatesh Pallipadi 	unsigned int cur_freq = __cpufreq_get(policy->cpu);
4931da177e4SLinus Torvalds 	if (!cur_freq)
4941da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
4951da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
4961da177e4SLinus Torvalds }
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds /**
5001da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
5011da177e4SLinus Torvalds  */
502e08f5f5bSGautham R Shenoy static ssize_t show_scaling_governor (struct cpufreq_policy * policy,
503e08f5f5bSGautham R Shenoy 							char *buf)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds 	if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
5061da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
5071da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
5081da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
5091da177e4SLinus Torvalds 	else if (policy->governor)
5101da177e4SLinus Torvalds 		return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
5111da177e4SLinus Torvalds 	return -EINVAL;
5121da177e4SLinus Torvalds }
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds /**
5161da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
5171da177e4SLinus Torvalds  */
5181da177e4SLinus Torvalds static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
5191da177e4SLinus Torvalds 				       const char *buf, size_t count)
5201da177e4SLinus Torvalds {
5211da177e4SLinus Torvalds 	unsigned int ret = -EINVAL;
5221da177e4SLinus Torvalds 	char	str_governor[16];
5231da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);
5261da177e4SLinus Torvalds 	if (ret)
5271da177e4SLinus Torvalds 		return ret;
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	ret = sscanf (buf, "%15s", str_governor);
5301da177e4SLinus Torvalds 	if (ret != 1)
5311da177e4SLinus Torvalds 		return -EINVAL;
5321da177e4SLinus Torvalds 
533e08f5f5bSGautham R Shenoy 	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
534e08f5f5bSGautham R Shenoy 						&new_policy.governor))
5351da177e4SLinus Torvalds 		return -EINVAL;
5361da177e4SLinus Torvalds 
5377970e08bSThomas Renninger 	/* Do not use cpufreq_set_policy here or the user_policy.max
5387970e08bSThomas Renninger 	   will be wrongly overridden */
5397970e08bSThomas Renninger 	ret = __cpufreq_set_policy(policy, &new_policy);
5407970e08bSThomas Renninger 
5417970e08bSThomas Renninger 	policy->user_policy.policy = policy->policy;
5427970e08bSThomas Renninger 	policy->user_policy.governor = policy->governor;
5437970e08bSThomas Renninger 
544e08f5f5bSGautham R Shenoy 	if (ret)
545e08f5f5bSGautham R Shenoy 		return ret;
546e08f5f5bSGautham R Shenoy 	else
547e08f5f5bSGautham R Shenoy 		return count;
5481da177e4SLinus Torvalds }
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds /**
5511da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
5521da177e4SLinus Torvalds  */
5531da177e4SLinus Torvalds static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
5541da177e4SLinus Torvalds {
5551da177e4SLinus Torvalds 	return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
5561da177e4SLinus Torvalds }
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds /**
5591da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
5601da177e4SLinus Torvalds  */
5611da177e4SLinus Torvalds static ssize_t show_scaling_available_governors (struct cpufreq_policy *policy,
5621da177e4SLinus Torvalds 				char *buf)
5631da177e4SLinus Torvalds {
5641da177e4SLinus Torvalds 	ssize_t i = 0;
5651da177e4SLinus Torvalds 	struct cpufreq_governor *t;
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	if (!cpufreq_driver->target) {
5681da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
5691da177e4SLinus Torvalds 		goto out;
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
5731da177e4SLinus Torvalds 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
5741da177e4SLinus Torvalds 			goto out;
5751da177e4SLinus Torvalds 		i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
5761da177e4SLinus Torvalds 	}
5771da177e4SLinus Torvalds out:
5781da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
5791da177e4SLinus Torvalds 	return i;
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds /**
5821da177e4SLinus Torvalds  * show_affected_cpus - show the CPUs affected by each transition
5831da177e4SLinus Torvalds  */
5841da177e4SLinus Torvalds static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
5851da177e4SLinus Torvalds {
5861da177e4SLinus Torvalds 	ssize_t i = 0;
5871da177e4SLinus Torvalds 	unsigned int cpu;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds 	for_each_cpu_mask(cpu, policy->cpus) {
5901da177e4SLinus Torvalds 		if (i)
5911da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
5921da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
5931da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
5941da177e4SLinus Torvalds 		    break;
5951da177e4SLinus Torvalds 	}
5961da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
5971da177e4SLinus Torvalds 	return i;
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds #define define_one_ro(_name) \
6021da177e4SLinus Torvalds static struct freq_attr _name = \
6031da177e4SLinus Torvalds __ATTR(_name, 0444, show_##_name, NULL)
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds #define define_one_ro0400(_name) \
6061da177e4SLinus Torvalds static struct freq_attr _name = \
6071da177e4SLinus Torvalds __ATTR(_name, 0400, show_##_name, NULL)
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds #define define_one_rw(_name) \
6101da177e4SLinus Torvalds static struct freq_attr _name = \
6111da177e4SLinus Torvalds __ATTR(_name, 0644, show_##_name, store_##_name)
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds define_one_ro0400(cpuinfo_cur_freq);
6141da177e4SLinus Torvalds define_one_ro(cpuinfo_min_freq);
6151da177e4SLinus Torvalds define_one_ro(cpuinfo_max_freq);
6161da177e4SLinus Torvalds define_one_ro(scaling_available_governors);
6171da177e4SLinus Torvalds define_one_ro(scaling_driver);
6181da177e4SLinus Torvalds define_one_ro(scaling_cur_freq);
6191da177e4SLinus Torvalds define_one_ro(affected_cpus);
6201da177e4SLinus Torvalds define_one_rw(scaling_min_freq);
6211da177e4SLinus Torvalds define_one_rw(scaling_max_freq);
6221da177e4SLinus Torvalds define_one_rw(scaling_governor);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds static struct attribute * default_attrs[] = {
6251da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
6261da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
6271da177e4SLinus Torvalds 	&scaling_min_freq.attr,
6281da177e4SLinus Torvalds 	&scaling_max_freq.attr,
6291da177e4SLinus Torvalds 	&affected_cpus.attr,
6301da177e4SLinus Torvalds 	&scaling_governor.attr,
6311da177e4SLinus Torvalds 	&scaling_driver.attr,
6321da177e4SLinus Torvalds 	&scaling_available_governors.attr,
6331da177e4SLinus Torvalds 	NULL
6341da177e4SLinus Torvalds };
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
6371da177e4SLinus Torvalds #define to_attr(a) container_of(a,struct freq_attr,attr)
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
6401da177e4SLinus Torvalds {
6411da177e4SLinus Torvalds 	struct cpufreq_policy * policy = to_policy(kobj);
6421da177e4SLinus Torvalds 	struct freq_attr * fattr = to_attr(attr);
6431da177e4SLinus Torvalds 	ssize_t ret;
6441da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(policy->cpu);
6451da177e4SLinus Torvalds 	if (!policy)
6461da177e4SLinus Torvalds 		return -EINVAL;
6475a01f2e8SVenkatesh Pallipadi 
6485a01f2e8SVenkatesh Pallipadi 	if (lock_policy_rwsem_read(policy->cpu) < 0)
6495a01f2e8SVenkatesh Pallipadi 		return -EINVAL;
6505a01f2e8SVenkatesh Pallipadi 
651e08f5f5bSGautham R Shenoy 	if (fattr->show)
652e08f5f5bSGautham R Shenoy 		ret = fattr->show(policy, buf);
653e08f5f5bSGautham R Shenoy 	else
654e08f5f5bSGautham R Shenoy 		ret = -EIO;
655e08f5f5bSGautham R Shenoy 
6565a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_read(policy->cpu);
6575a01f2e8SVenkatesh Pallipadi 
6581da177e4SLinus Torvalds 	cpufreq_cpu_put(policy);
6591da177e4SLinus Torvalds 	return ret;
6601da177e4SLinus Torvalds }
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds static ssize_t store(struct kobject * kobj, struct attribute * attr,
6631da177e4SLinus Torvalds 		     const char * buf, size_t count)
6641da177e4SLinus Torvalds {
6651da177e4SLinus Torvalds 	struct cpufreq_policy * policy = to_policy(kobj);
6661da177e4SLinus Torvalds 	struct freq_attr * fattr = to_attr(attr);
6671da177e4SLinus Torvalds 	ssize_t ret;
6681da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(policy->cpu);
6691da177e4SLinus Torvalds 	if (!policy)
6701da177e4SLinus Torvalds 		return -EINVAL;
6715a01f2e8SVenkatesh Pallipadi 
6725a01f2e8SVenkatesh Pallipadi 	if (lock_policy_rwsem_write(policy->cpu) < 0)
6735a01f2e8SVenkatesh Pallipadi 		return -EINVAL;
6745a01f2e8SVenkatesh Pallipadi 
675e08f5f5bSGautham R Shenoy 	if (fattr->store)
676e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
677e08f5f5bSGautham R Shenoy 	else
678e08f5f5bSGautham R Shenoy 		ret = -EIO;
679e08f5f5bSGautham R Shenoy 
6805a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(policy->cpu);
6815a01f2e8SVenkatesh Pallipadi 
6821da177e4SLinus Torvalds 	cpufreq_cpu_put(policy);
6831da177e4SLinus Torvalds 	return ret;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject * kobj)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	struct cpufreq_policy * policy = to_policy(kobj);
6891da177e4SLinus Torvalds 	dprintk("last reference is dropped\n");
6901da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds static struct sysfs_ops sysfs_ops = {
6941da177e4SLinus Torvalds 	.show	= show,
6951da177e4SLinus Torvalds 	.store	= store,
6961da177e4SLinus Torvalds };
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
6991da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
7001da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
7011da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
7021da177e4SLinus Torvalds };
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds /**
7061da177e4SLinus Torvalds  * cpufreq_add_dev - add a CPU device
7071da177e4SLinus Torvalds  *
7081da177e4SLinus Torvalds  * Adds the cpufreq interface for a CPU device.
7091da177e4SLinus Torvalds  */
7101da177e4SLinus Torvalds static int cpufreq_add_dev (struct sys_device * sys_dev)
7111da177e4SLinus Torvalds {
7121da177e4SLinus Torvalds 	unsigned int cpu = sys_dev->id;
7131da177e4SLinus Torvalds 	int ret = 0;
7141da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
7151da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
7161da177e4SLinus Torvalds 	struct freq_attr **drv_attr;
7178ff69732SDave Jones 	struct sys_device *cpu_sys_dev;
7181da177e4SLinus Torvalds 	unsigned long flags;
7191da177e4SLinus Torvalds 	unsigned int j;
7208ff69732SDave Jones #ifdef CONFIG_SMP
7218ff69732SDave Jones 	struct cpufreq_policy *managed_policy;
7228ff69732SDave Jones #endif
7231da177e4SLinus Torvalds 
724c32b6b8eSAshok Raj 	if (cpu_is_offline(cpu))
725c32b6b8eSAshok Raj 		return 0;
726c32b6b8eSAshok Raj 
7271da177e4SLinus Torvalds 	cpufreq_debug_disable_ratelimit();
7281da177e4SLinus Torvalds 	dprintk("adding CPU %u\n", cpu);
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds #ifdef CONFIG_SMP
7311da177e4SLinus Torvalds 	/* check whether a different CPU already registered this
7321da177e4SLinus Torvalds 	 * CPU because it is in the same boat. */
7331da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(cpu);
7341da177e4SLinus Torvalds 	if (unlikely(policy)) {
7358ff69732SDave Jones 		cpufreq_cpu_put(policy);
7361da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
7371da177e4SLinus Torvalds 		return 0;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds #endif
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	if (!try_module_get(cpufreq_driver->owner)) {
7421da177e4SLinus Torvalds 		ret = -EINVAL;
7431da177e4SLinus Torvalds 		goto module_out;
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 
746e98df50cSDave Jones 	policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
7471da177e4SLinus Torvalds 	if (!policy) {
7481da177e4SLinus Torvalds 		ret = -ENOMEM;
7491da177e4SLinus Torvalds 		goto nomem_out;
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	policy->cpu = cpu;
7531da177e4SLinus Torvalds 	policy->cpus = cpumask_of_cpu(cpu);
7541da177e4SLinus Torvalds 
7555a01f2e8SVenkatesh Pallipadi 	/* Initially set CPU itself as the policy_cpu */
7565a01f2e8SVenkatesh Pallipadi 	per_cpu(policy_cpu, cpu) = cpu;
7575a01f2e8SVenkatesh Pallipadi 	lock_policy_rwsem_write(cpu);
7585a01f2e8SVenkatesh Pallipadi 
7591da177e4SLinus Torvalds 	init_completion(&policy->kobj_unregister);
76065f27f38SDavid Howells 	INIT_WORK(&policy->update, handle_update);
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
7631da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
7641da177e4SLinus Torvalds 	 */
7651da177e4SLinus Torvalds 	ret = cpufreq_driver->init(policy);
7661da177e4SLinus Torvalds 	if (ret) {
7671da177e4SLinus Torvalds 		dprintk("initialization failed\n");
7685a01f2e8SVenkatesh Pallipadi 		unlock_policy_rwsem_write(cpu);
7691da177e4SLinus Torvalds 		goto err_out;
7701da177e4SLinus Torvalds 	}
7711da177e4SLinus Torvalds 
7728ff69732SDave Jones #ifdef CONFIG_SMP
7738ff69732SDave Jones 	for_each_cpu_mask(j, policy->cpus) {
7748ff69732SDave Jones 		if (cpu == j)
7758ff69732SDave Jones 			continue;
7768ff69732SDave Jones 
7778ff69732SDave Jones 		/* check for existing affected CPUs.  They may not be aware
7788ff69732SDave Jones 		 * of it due to CPU Hotplug.
7798ff69732SDave Jones 		 */
7808ff69732SDave Jones 		managed_policy = cpufreq_cpu_get(j);
7818ff69732SDave Jones 		if (unlikely(managed_policy)) {
7825a01f2e8SVenkatesh Pallipadi 
7835a01f2e8SVenkatesh Pallipadi 			/* Set proper policy_cpu */
7845a01f2e8SVenkatesh Pallipadi 			unlock_policy_rwsem_write(cpu);
7855a01f2e8SVenkatesh Pallipadi 			per_cpu(policy_cpu, cpu) = managed_policy->cpu;
7865a01f2e8SVenkatesh Pallipadi 
7875a01f2e8SVenkatesh Pallipadi 			if (lock_policy_rwsem_write(cpu) < 0)
7885a01f2e8SVenkatesh Pallipadi 				goto err_out_driver_exit;
7895a01f2e8SVenkatesh Pallipadi 
7908ff69732SDave Jones 			spin_lock_irqsave(&cpufreq_driver_lock, flags);
7918ff69732SDave Jones 			managed_policy->cpus = policy->cpus;
7928ff69732SDave Jones 			cpufreq_cpu_data[cpu] = managed_policy;
7938ff69732SDave Jones 			spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
7948ff69732SDave Jones 
7958ff69732SDave Jones 			dprintk("CPU already managed, adding link\n");
7960142f9dcSAhmed S. Darwish 			ret = sysfs_create_link(&sys_dev->kobj,
7970142f9dcSAhmed S. Darwish 						&managed_policy->kobj,
7980142f9dcSAhmed S. Darwish 						"cpufreq");
7990142f9dcSAhmed S. Darwish 			if (ret) {
8005a01f2e8SVenkatesh Pallipadi 				unlock_policy_rwsem_write(cpu);
8010142f9dcSAhmed S. Darwish 				goto err_out_driver_exit;
8020142f9dcSAhmed S. Darwish 			}
8038ff69732SDave Jones 
8048ff69732SDave Jones 			cpufreq_debug_enable_ratelimit();
8058ff69732SDave Jones 			ret = 0;
8065a01f2e8SVenkatesh Pallipadi 			unlock_policy_rwsem_write(cpu);
8078ff69732SDave Jones 			goto err_out_driver_exit; /* call driver->exit() */
8088ff69732SDave Jones 		}
8098ff69732SDave Jones 	}
8108ff69732SDave Jones #endif
8111da177e4SLinus Torvalds 	memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	/* prepare interface data */
8141da177e4SLinus Torvalds 	policy->kobj.parent = &sys_dev->kobj;
8151da177e4SLinus Torvalds 	policy->kobj.ktype = &ktype_cpufreq;
8161da177e4SLinus Torvalds 	strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds 	ret = kobject_register(&policy->kobj);
819f3876c1bSAndrew Morton 	if (ret) {
8205a01f2e8SVenkatesh Pallipadi 		unlock_policy_rwsem_write(cpu);
8218085e1f1SVenkatesh Pallipadi 		goto err_out_driver_exit;
822f3876c1bSAndrew Morton 	}
8231da177e4SLinus Torvalds 	/* set up files for this cpu device */
8241da177e4SLinus Torvalds 	drv_attr = cpufreq_driver->attr;
8251da177e4SLinus Torvalds 	while ((drv_attr) && (*drv_attr)) {
8261da177e4SLinus Torvalds 		sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
8271da177e4SLinus Torvalds 		drv_attr++;
8281da177e4SLinus Torvalds 	}
8291da177e4SLinus Torvalds 	if (cpufreq_driver->get)
8301da177e4SLinus Torvalds 		sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
8311da177e4SLinus Torvalds 	if (cpufreq_driver->target)
8321da177e4SLinus Torvalds 		sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
8355a01f2e8SVenkatesh Pallipadi 	for_each_cpu_mask(j, policy->cpus) {
8361da177e4SLinus Torvalds 		cpufreq_cpu_data[j] = policy;
8375a01f2e8SVenkatesh Pallipadi 		per_cpu(policy_cpu, j) = policy->cpu;
8385a01f2e8SVenkatesh Pallipadi 	}
8391da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
8408ff69732SDave Jones 
8418ff69732SDave Jones 	/* symlink affected CPUs */
8428ff69732SDave Jones 	for_each_cpu_mask(j, policy->cpus) {
8438ff69732SDave Jones 		if (j == cpu)
8448ff69732SDave Jones 			continue;
8458ff69732SDave Jones 		if (!cpu_online(j))
8468ff69732SDave Jones 			continue;
8478ff69732SDave Jones 
8481f8b2c9dSDave Jones 		dprintk("CPU %u already managed, adding link\n", j);
8498ff69732SDave Jones 		cpufreq_cpu_get(cpu);
8508ff69732SDave Jones 		cpu_sys_dev = get_cpu_sysdev(j);
8510142f9dcSAhmed S. Darwish 		ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
8528ff69732SDave Jones 					"cpufreq");
8530142f9dcSAhmed S. Darwish 		if (ret) {
8545a01f2e8SVenkatesh Pallipadi 			unlock_policy_rwsem_write(cpu);
8550142f9dcSAhmed S. Darwish 			goto err_out_unregister;
8560142f9dcSAhmed S. Darwish 		}
8578ff69732SDave Jones 	}
8588ff69732SDave Jones 
8591da177e4SLinus Torvalds 	policy->governor = NULL; /* to assure that the starting sequence is
8601da177e4SLinus Torvalds 				  * run in cpufreq_set_policy */
8615a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(cpu);
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	/* set default policy */
8641da177e4SLinus Torvalds 	ret = cpufreq_set_policy(&new_policy);
8651da177e4SLinus Torvalds 	if (ret) {
8661da177e4SLinus Torvalds 		dprintk("setting policy failed\n");
8671da177e4SLinus Torvalds 		goto err_out_unregister;
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 
8701da177e4SLinus Torvalds 	module_put(cpufreq_driver->owner);
8711da177e4SLinus Torvalds 	dprintk("initialization complete\n");
8721da177e4SLinus Torvalds 	cpufreq_debug_enable_ratelimit();
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	return 0;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds err_out_unregister:
8781da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
8791da177e4SLinus Torvalds 	for_each_cpu_mask(j, policy->cpus)
8801da177e4SLinus Torvalds 		cpufreq_cpu_data[j] = NULL;
8811da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
8821da177e4SLinus Torvalds 
8831da177e4SLinus Torvalds 	kobject_unregister(&policy->kobj);
8841da177e4SLinus Torvalds 	wait_for_completion(&policy->kobj_unregister);
8851da177e4SLinus Torvalds 
8868085e1f1SVenkatesh Pallipadi err_out_driver_exit:
8878085e1f1SVenkatesh Pallipadi 	if (cpufreq_driver->exit)
8888085e1f1SVenkatesh Pallipadi 		cpufreq_driver->exit(policy);
8898085e1f1SVenkatesh Pallipadi 
8901da177e4SLinus Torvalds err_out:
8911da177e4SLinus Torvalds 	kfree(policy);
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds nomem_out:
8941da177e4SLinus Torvalds 	module_put(cpufreq_driver->owner);
8951da177e4SLinus Torvalds module_out:
8961da177e4SLinus Torvalds 	cpufreq_debug_enable_ratelimit();
8971da177e4SLinus Torvalds 	return ret;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds /**
9025a01f2e8SVenkatesh Pallipadi  * __cpufreq_remove_dev - remove a CPU device
9031da177e4SLinus Torvalds  *
9041da177e4SLinus Torvalds  * Removes the cpufreq interface for a CPU device.
9055a01f2e8SVenkatesh Pallipadi  * Caller should already have policy_rwsem in write mode for this CPU.
9065a01f2e8SVenkatesh Pallipadi  * This routine frees the rwsem before returning.
9071da177e4SLinus Torvalds  */
9085a01f2e8SVenkatesh Pallipadi static int __cpufreq_remove_dev (struct sys_device * sys_dev)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	unsigned int cpu = sys_dev->id;
9111da177e4SLinus Torvalds 	unsigned long flags;
9121da177e4SLinus Torvalds 	struct cpufreq_policy *data;
9131da177e4SLinus Torvalds #ifdef CONFIG_SMP
914e738cf6dSGrant Coady 	struct sys_device *cpu_sys_dev;
9151da177e4SLinus Torvalds 	unsigned int j;
9161da177e4SLinus Torvalds #endif
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	cpufreq_debug_disable_ratelimit();
9191da177e4SLinus Torvalds 	dprintk("unregistering CPU %u\n", cpu);
9201da177e4SLinus Torvalds 
9211da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
9221da177e4SLinus Torvalds 	data = cpufreq_cpu_data[cpu];
9231da177e4SLinus Torvalds 
9241da177e4SLinus Torvalds 	if (!data) {
9251da177e4SLinus Torvalds 		spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
9261da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
9275a01f2e8SVenkatesh Pallipadi 		unlock_policy_rwsem_write(cpu);
9281da177e4SLinus Torvalds 		return -EINVAL;
9291da177e4SLinus Torvalds 	}
9301da177e4SLinus Torvalds 	cpufreq_cpu_data[cpu] = NULL;
9311da177e4SLinus Torvalds 
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds #ifdef CONFIG_SMP
9341da177e4SLinus Torvalds 	/* if this isn't the CPU which is the parent of the kobj, we
9351da177e4SLinus Torvalds 	 * only need to unlink, put and exit
9361da177e4SLinus Torvalds 	 */
9371da177e4SLinus Torvalds 	if (unlikely(cpu != data->cpu)) {
9381da177e4SLinus Torvalds 		dprintk("removing link\n");
9398ff69732SDave Jones 		cpu_clear(cpu, data->cpus);
9401da177e4SLinus Torvalds 		spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
9411da177e4SLinus Torvalds 		sysfs_remove_link(&sys_dev->kobj, "cpufreq");
9421da177e4SLinus Torvalds 		cpufreq_cpu_put(data);
9431da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
9445a01f2e8SVenkatesh Pallipadi 		unlock_policy_rwsem_write(cpu);
9451da177e4SLinus Torvalds 		return 0;
9461da177e4SLinus Torvalds 	}
9471da177e4SLinus Torvalds #endif
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	if (!kobject_get(&data->kobj)) {
9511da177e4SLinus Torvalds 		spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
9521da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
9535a01f2e8SVenkatesh Pallipadi 		unlock_policy_rwsem_write(cpu);
9541da177e4SLinus Torvalds 		return -EFAULT;
9551da177e4SLinus Torvalds 	}
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds #ifdef CONFIG_SMP
9581da177e4SLinus Torvalds 	/* if we have other CPUs still registered, we need to unlink them,
9591da177e4SLinus Torvalds 	 * or else wait_for_completion below will lock up. Clean the
9601da177e4SLinus Torvalds 	 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
9611da177e4SLinus Torvalds 	 * links afterwards.
9621da177e4SLinus Torvalds 	 */
9631da177e4SLinus Torvalds 	if (unlikely(cpus_weight(data->cpus) > 1)) {
9641da177e4SLinus Torvalds 		for_each_cpu_mask(j, data->cpus) {
9651da177e4SLinus Torvalds 			if (j == cpu)
9661da177e4SLinus Torvalds 				continue;
9671da177e4SLinus Torvalds 			cpufreq_cpu_data[j] = NULL;
9681da177e4SLinus Torvalds 		}
9691da177e4SLinus Torvalds 	}
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	if (unlikely(cpus_weight(data->cpus) > 1)) {
9741da177e4SLinus Torvalds 		for_each_cpu_mask(j, data->cpus) {
9751da177e4SLinus Torvalds 			if (j == cpu)
9761da177e4SLinus Torvalds 				continue;
9771da177e4SLinus Torvalds 			dprintk("removing link for cpu %u\n", j);
978d434fca7SAshok Raj 			cpu_sys_dev = get_cpu_sysdev(j);
979d434fca7SAshok Raj 			sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
9801da177e4SLinus Torvalds 			cpufreq_cpu_put(data);
9811da177e4SLinus Torvalds 		}
9821da177e4SLinus Torvalds 	}
9831da177e4SLinus Torvalds #else
9841da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
9851da177e4SLinus Torvalds #endif
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds 	if (cpufreq_driver->target)
9881da177e4SLinus Torvalds 		__cpufreq_governor(data, CPUFREQ_GOV_STOP);
9895a01f2e8SVenkatesh Pallipadi 
9905a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(cpu);
9911da177e4SLinus Torvalds 
9921da177e4SLinus Torvalds 	kobject_unregister(&data->kobj);
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	kobject_put(&data->kobj);
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds 	/* we need to make sure that the underlying kobj is actually
9971da177e4SLinus Torvalds 	 * not referenced anymore by anybody before we proceed with
9981da177e4SLinus Torvalds 	 * unloading.
9991da177e4SLinus Torvalds 	 */
10001da177e4SLinus Torvalds 	dprintk("waiting for dropping of refcount\n");
10011da177e4SLinus Torvalds 	wait_for_completion(&data->kobj_unregister);
10021da177e4SLinus Torvalds 	dprintk("wait complete\n");
10031da177e4SLinus Torvalds 
10041da177e4SLinus Torvalds 	if (cpufreq_driver->exit)
10051da177e4SLinus Torvalds 		cpufreq_driver->exit(data);
10061da177e4SLinus Torvalds 
10071da177e4SLinus Torvalds 	kfree(data);
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds 	cpufreq_debug_enable_ratelimit();
10101da177e4SLinus Torvalds 	return 0;
10111da177e4SLinus Torvalds }
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 
10145a01f2e8SVenkatesh Pallipadi static int cpufreq_remove_dev (struct sys_device * sys_dev)
10155a01f2e8SVenkatesh Pallipadi {
10165a01f2e8SVenkatesh Pallipadi 	unsigned int cpu = sys_dev->id;
10175a01f2e8SVenkatesh Pallipadi 	int retval;
1018*ec28297aSVenki Pallipadi 
1019*ec28297aSVenki Pallipadi 	if (cpu_is_offline(cpu))
1020*ec28297aSVenki Pallipadi 		return 0;
1021*ec28297aSVenki Pallipadi 
10225a01f2e8SVenkatesh Pallipadi 	if (unlikely(lock_policy_rwsem_write(cpu)))
10235a01f2e8SVenkatesh Pallipadi 		BUG();
10245a01f2e8SVenkatesh Pallipadi 
10255a01f2e8SVenkatesh Pallipadi 	retval = __cpufreq_remove_dev(sys_dev);
10265a01f2e8SVenkatesh Pallipadi 	return retval;
10275a01f2e8SVenkatesh Pallipadi }
10285a01f2e8SVenkatesh Pallipadi 
10295a01f2e8SVenkatesh Pallipadi 
103065f27f38SDavid Howells static void handle_update(struct work_struct *work)
10311da177e4SLinus Torvalds {
103265f27f38SDavid Howells 	struct cpufreq_policy *policy =
103365f27f38SDavid Howells 		container_of(work, struct cpufreq_policy, update);
103465f27f38SDavid Howells 	unsigned int cpu = policy->cpu;
10351da177e4SLinus Torvalds 	dprintk("handle_update for cpu %u called\n", cpu);
10361da177e4SLinus Torvalds 	cpufreq_update_policy(cpu);
10371da177e4SLinus Torvalds }
10381da177e4SLinus Torvalds 
10391da177e4SLinus Torvalds /**
10401da177e4SLinus Torvalds  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
10411da177e4SLinus Torvalds  *	@cpu: cpu number
10421da177e4SLinus Torvalds  *	@old_freq: CPU frequency the kernel thinks the CPU runs at
10431da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
10441da177e4SLinus Torvalds  *
10451da177e4SLinus Torvalds  *	We adjust to current frequency first, and need to clean up later. So either call
10461da177e4SLinus Torvalds  *	to cpufreq_update_policy() or schedule handle_update()).
10471da177e4SLinus Torvalds  */
1048e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1049e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
10501da177e4SLinus Torvalds {
10511da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
10521da177e4SLinus Torvalds 
1053b10eec22SJan Beulich 	dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
10541da177e4SLinus Torvalds 	       "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
10551da177e4SLinus Torvalds 
10561da177e4SLinus Torvalds 	freqs.cpu = cpu;
10571da177e4SLinus Torvalds 	freqs.old = old_freq;
10581da177e4SLinus Torvalds 	freqs.new = new_freq;
10591da177e4SLinus Torvalds 	cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
10601da177e4SLinus Torvalds 	cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
10611da177e4SLinus Torvalds }
10621da177e4SLinus Torvalds 
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds /**
10654ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
106695235ca2SVenkatesh Pallipadi  * @cpu: CPU number
106795235ca2SVenkatesh Pallipadi  *
106895235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
106995235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
107095235ca2SVenkatesh Pallipadi  */
107195235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
107295235ca2SVenkatesh Pallipadi {
107395235ca2SVenkatesh Pallipadi 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1074e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
107595235ca2SVenkatesh Pallipadi 
107695235ca2SVenkatesh Pallipadi 	if (policy) {
10775a01f2e8SVenkatesh Pallipadi 		if (unlikely(lock_policy_rwsem_read(cpu)))
10785a01f2e8SVenkatesh Pallipadi 			return ret_freq;
10795a01f2e8SVenkatesh Pallipadi 
1080e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
10815a01f2e8SVenkatesh Pallipadi 
10825a01f2e8SVenkatesh Pallipadi 		unlock_policy_rwsem_read(cpu);
108395235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
108495235ca2SVenkatesh Pallipadi 	}
108595235ca2SVenkatesh Pallipadi 
1086e08f5f5bSGautham R Shenoy 	return (ret_freq);
108795235ca2SVenkatesh Pallipadi }
108895235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
108995235ca2SVenkatesh Pallipadi 
109095235ca2SVenkatesh Pallipadi 
10915a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu)
10921da177e4SLinus Torvalds {
10935a01f2e8SVenkatesh Pallipadi 	struct cpufreq_policy *policy = cpufreq_cpu_data[cpu];
1094e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds 	if (!cpufreq_driver->get)
10975a01f2e8SVenkatesh Pallipadi 		return (ret_freq);
10981da177e4SLinus Torvalds 
1099e08f5f5bSGautham R Shenoy 	ret_freq = cpufreq_driver->get(cpu);
11001da177e4SLinus Torvalds 
1101e08f5f5bSGautham R Shenoy 	if (ret_freq && policy->cur &&
1102e08f5f5bSGautham R Shenoy 		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1103e08f5f5bSGautham R Shenoy 		/* verify no discrepancy between actual and
1104e08f5f5bSGautham R Shenoy 					saved value exists */
1105e08f5f5bSGautham R Shenoy 		if (unlikely(ret_freq != policy->cur)) {
1106e08f5f5bSGautham R Shenoy 			cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
11071da177e4SLinus Torvalds 			schedule_work(&policy->update);
11081da177e4SLinus Torvalds 		}
11091da177e4SLinus Torvalds 	}
11101da177e4SLinus Torvalds 
11115a01f2e8SVenkatesh Pallipadi 	return (ret_freq);
11125a01f2e8SVenkatesh Pallipadi }
11131da177e4SLinus Torvalds 
11145a01f2e8SVenkatesh Pallipadi /**
11155a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
11165a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
11175a01f2e8SVenkatesh Pallipadi  *
11185a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
11195a01f2e8SVenkatesh Pallipadi  */
11205a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
11215a01f2e8SVenkatesh Pallipadi {
11225a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
11235a01f2e8SVenkatesh Pallipadi 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
11245a01f2e8SVenkatesh Pallipadi 
11255a01f2e8SVenkatesh Pallipadi 	if (!policy)
11265a01f2e8SVenkatesh Pallipadi 		goto out;
11275a01f2e8SVenkatesh Pallipadi 
11285a01f2e8SVenkatesh Pallipadi 	if (unlikely(lock_policy_rwsem_read(cpu)))
11295a01f2e8SVenkatesh Pallipadi 		goto out_policy;
11305a01f2e8SVenkatesh Pallipadi 
11315a01f2e8SVenkatesh Pallipadi 	ret_freq = __cpufreq_get(cpu);
11325a01f2e8SVenkatesh Pallipadi 
11335a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_read(cpu);
11345a01f2e8SVenkatesh Pallipadi 
11355a01f2e8SVenkatesh Pallipadi out_policy:
11361da177e4SLinus Torvalds 	cpufreq_cpu_put(policy);
11375a01f2e8SVenkatesh Pallipadi out:
1138e08f5f5bSGautham R Shenoy 	return (ret_freq);
11391da177e4SLinus Torvalds }
11401da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 
11431da177e4SLinus Torvalds /**
114442d4dc3fSBenjamin Herrenschmidt  *	cpufreq_suspend - let the low level driver prepare for suspend
114542d4dc3fSBenjamin Herrenschmidt  */
114642d4dc3fSBenjamin Herrenschmidt 
1147e00d9967SBernard Blackham static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
114842d4dc3fSBenjamin Herrenschmidt {
114942d4dc3fSBenjamin Herrenschmidt 	int cpu = sysdev->id;
1150e08f5f5bSGautham R Shenoy 	int ret = 0;
115142d4dc3fSBenjamin Herrenschmidt 	unsigned int cur_freq = 0;
115242d4dc3fSBenjamin Herrenschmidt 	struct cpufreq_policy *cpu_policy;
115342d4dc3fSBenjamin Herrenschmidt 
11540e37b159SDave Jones 	dprintk("suspending cpu %u\n", cpu);
115542d4dc3fSBenjamin Herrenschmidt 
115642d4dc3fSBenjamin Herrenschmidt 	if (!cpu_online(cpu))
115742d4dc3fSBenjamin Herrenschmidt 		return 0;
115842d4dc3fSBenjamin Herrenschmidt 
115942d4dc3fSBenjamin Herrenschmidt 	/* we may be lax here as interrupts are off. Nonetheless
116042d4dc3fSBenjamin Herrenschmidt 	 * we need to grab the correct cpu policy, as to check
116142d4dc3fSBenjamin Herrenschmidt 	 * whether we really run on this CPU.
116242d4dc3fSBenjamin Herrenschmidt 	 */
116342d4dc3fSBenjamin Herrenschmidt 
116442d4dc3fSBenjamin Herrenschmidt 	cpu_policy = cpufreq_cpu_get(cpu);
116542d4dc3fSBenjamin Herrenschmidt 	if (!cpu_policy)
116642d4dc3fSBenjamin Herrenschmidt 		return -EINVAL;
116742d4dc3fSBenjamin Herrenschmidt 
116842d4dc3fSBenjamin Herrenschmidt 	/* only handle each CPU group once */
116942d4dc3fSBenjamin Herrenschmidt 	if (unlikely(cpu_policy->cpu != cpu)) {
117042d4dc3fSBenjamin Herrenschmidt 		cpufreq_cpu_put(cpu_policy);
117142d4dc3fSBenjamin Herrenschmidt 		return 0;
117242d4dc3fSBenjamin Herrenschmidt 	}
117342d4dc3fSBenjamin Herrenschmidt 
117442d4dc3fSBenjamin Herrenschmidt 	if (cpufreq_driver->suspend) {
1175e00d9967SBernard Blackham 		ret = cpufreq_driver->suspend(cpu_policy, pmsg);
117642d4dc3fSBenjamin Herrenschmidt 		if (ret) {
117742d4dc3fSBenjamin Herrenschmidt 			printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
117842d4dc3fSBenjamin Herrenschmidt 					"step on CPU %u\n", cpu_policy->cpu);
117942d4dc3fSBenjamin Herrenschmidt 			cpufreq_cpu_put(cpu_policy);
118042d4dc3fSBenjamin Herrenschmidt 			return ret;
118142d4dc3fSBenjamin Herrenschmidt 		}
118242d4dc3fSBenjamin Herrenschmidt 	}
118342d4dc3fSBenjamin Herrenschmidt 
118442d4dc3fSBenjamin Herrenschmidt 
118542d4dc3fSBenjamin Herrenschmidt 	if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
118642d4dc3fSBenjamin Herrenschmidt 		goto out;
118742d4dc3fSBenjamin Herrenschmidt 
118842d4dc3fSBenjamin Herrenschmidt 	if (cpufreq_driver->get)
118942d4dc3fSBenjamin Herrenschmidt 		cur_freq = cpufreq_driver->get(cpu_policy->cpu);
119042d4dc3fSBenjamin Herrenschmidt 
119142d4dc3fSBenjamin Herrenschmidt 	if (!cur_freq || !cpu_policy->cur) {
119242d4dc3fSBenjamin Herrenschmidt 		printk(KERN_ERR "cpufreq: suspend failed to assert current "
119342d4dc3fSBenjamin Herrenschmidt 		       "frequency is what timing core thinks it is.\n");
119442d4dc3fSBenjamin Herrenschmidt 		goto out;
119542d4dc3fSBenjamin Herrenschmidt 	}
119642d4dc3fSBenjamin Herrenschmidt 
119742d4dc3fSBenjamin Herrenschmidt 	if (unlikely(cur_freq != cpu_policy->cur)) {
119842d4dc3fSBenjamin Herrenschmidt 		struct cpufreq_freqs freqs;
119942d4dc3fSBenjamin Herrenschmidt 
120042d4dc3fSBenjamin Herrenschmidt 		if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1201b10eec22SJan Beulich 			dprintk("Warning: CPU frequency is %u, "
120242d4dc3fSBenjamin Herrenschmidt 			       "cpufreq assumed %u kHz.\n",
120342d4dc3fSBenjamin Herrenschmidt 			       cur_freq, cpu_policy->cur);
120442d4dc3fSBenjamin Herrenschmidt 
120542d4dc3fSBenjamin Herrenschmidt 		freqs.cpu = cpu;
120642d4dc3fSBenjamin Herrenschmidt 		freqs.old = cpu_policy->cur;
120742d4dc3fSBenjamin Herrenschmidt 		freqs.new = cur_freq;
120842d4dc3fSBenjamin Herrenschmidt 
1209b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
121042d4dc3fSBenjamin Herrenschmidt 				    CPUFREQ_SUSPENDCHANGE, &freqs);
121142d4dc3fSBenjamin Herrenschmidt 		adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
121242d4dc3fSBenjamin Herrenschmidt 
121342d4dc3fSBenjamin Herrenschmidt 		cpu_policy->cur = cur_freq;
121442d4dc3fSBenjamin Herrenschmidt 	}
121542d4dc3fSBenjamin Herrenschmidt 
121642d4dc3fSBenjamin Herrenschmidt out:
121742d4dc3fSBenjamin Herrenschmidt 	cpufreq_cpu_put(cpu_policy);
121842d4dc3fSBenjamin Herrenschmidt 	return 0;
121942d4dc3fSBenjamin Herrenschmidt }
122042d4dc3fSBenjamin Herrenschmidt 
122142d4dc3fSBenjamin Herrenschmidt /**
12221da177e4SLinus Torvalds  *	cpufreq_resume -  restore proper CPU frequency handling after resume
12231da177e4SLinus Torvalds  *
12241da177e4SLinus Torvalds  *	1.) resume CPUfreq hardware support (cpufreq_driver->resume())
12251da177e4SLinus Torvalds  *	2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
122642d4dc3fSBenjamin Herrenschmidt  *	3.) schedule call cpufreq_update_policy() ASAP as interrupts are
122742d4dc3fSBenjamin Herrenschmidt  *	    restored.
12281da177e4SLinus Torvalds  */
12291da177e4SLinus Torvalds static int cpufreq_resume(struct sys_device * sysdev)
12301da177e4SLinus Torvalds {
12311da177e4SLinus Torvalds 	int cpu = sysdev->id;
1232e08f5f5bSGautham R Shenoy 	int ret = 0;
12331da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	dprintk("resuming cpu %u\n", cpu);
12361da177e4SLinus Torvalds 
12371da177e4SLinus Torvalds 	if (!cpu_online(cpu))
12381da177e4SLinus Torvalds 		return 0;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	/* we may be lax here as interrupts are off. Nonetheless
12411da177e4SLinus Torvalds 	 * we need to grab the correct cpu policy, as to check
12421da177e4SLinus Torvalds 	 * whether we really run on this CPU.
12431da177e4SLinus Torvalds 	 */
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
12461da177e4SLinus Torvalds 	if (!cpu_policy)
12471da177e4SLinus Torvalds 		return -EINVAL;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds 	/* only handle each CPU group once */
12501da177e4SLinus Torvalds 	if (unlikely(cpu_policy->cpu != cpu)) {
12511da177e4SLinus Torvalds 		cpufreq_cpu_put(cpu_policy);
12521da177e4SLinus Torvalds 		return 0;
12531da177e4SLinus Torvalds 	}
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 	if (cpufreq_driver->resume) {
12561da177e4SLinus Torvalds 		ret = cpufreq_driver->resume(cpu_policy);
12571da177e4SLinus Torvalds 		if (ret) {
12581da177e4SLinus Torvalds 			printk(KERN_ERR "cpufreq: resume failed in ->resume "
12591da177e4SLinus Torvalds 					"step on CPU %u\n", cpu_policy->cpu);
12601da177e4SLinus Torvalds 			cpufreq_cpu_put(cpu_policy);
12611da177e4SLinus Torvalds 			return ret;
12621da177e4SLinus Torvalds 		}
12631da177e4SLinus Torvalds 	}
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
12661da177e4SLinus Torvalds 		unsigned int cur_freq = 0;
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 		if (cpufreq_driver->get)
12691da177e4SLinus Torvalds 			cur_freq = cpufreq_driver->get(cpu_policy->cpu);
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 		if (!cur_freq || !cpu_policy->cur) {
127242d4dc3fSBenjamin Herrenschmidt 			printk(KERN_ERR "cpufreq: resume failed to assert "
127342d4dc3fSBenjamin Herrenschmidt 					"current frequency is what timing core "
127442d4dc3fSBenjamin Herrenschmidt 					"thinks it is.\n");
12751da177e4SLinus Torvalds 			goto out;
12761da177e4SLinus Torvalds 		}
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 		if (unlikely(cur_freq != cpu_policy->cur)) {
12791da177e4SLinus Torvalds 			struct cpufreq_freqs freqs;
12801da177e4SLinus Torvalds 
1281ac09f698SBenjamin Herrenschmidt 			if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1282b10eec22SJan Beulich 				dprintk("Warning: CPU frequency"
1283ac09f698SBenjamin Herrenschmidt 				       "is %u, cpufreq assumed %u kHz.\n",
128442d4dc3fSBenjamin Herrenschmidt 				       cur_freq, cpu_policy->cur);
12851da177e4SLinus Torvalds 
12861da177e4SLinus Torvalds 			freqs.cpu = cpu;
12871da177e4SLinus Torvalds 			freqs.old = cpu_policy->cur;
12881da177e4SLinus Torvalds 			freqs.new = cur_freq;
12891da177e4SLinus Torvalds 
1290b4dfdbb3SAlan Stern 			srcu_notifier_call_chain(
1291e041c683SAlan Stern 					&cpufreq_transition_notifier_list,
129242d4dc3fSBenjamin Herrenschmidt 					CPUFREQ_RESUMECHANGE, &freqs);
12931da177e4SLinus Torvalds 			adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 			cpu_policy->cur = cur_freq;
12961da177e4SLinus Torvalds 		}
12971da177e4SLinus Torvalds 	}
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds out:
13001da177e4SLinus Torvalds 	schedule_work(&cpu_policy->update);
13011da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
13021da177e4SLinus Torvalds 	return ret;
13031da177e4SLinus Torvalds }
13041da177e4SLinus Torvalds 
13051da177e4SLinus Torvalds static struct sysdev_driver cpufreq_sysdev_driver = {
13061da177e4SLinus Torvalds 	.add		= cpufreq_add_dev,
13071da177e4SLinus Torvalds 	.remove		= cpufreq_remove_dev,
130842d4dc3fSBenjamin Herrenschmidt 	.suspend	= cpufreq_suspend,
13091da177e4SLinus Torvalds 	.resume		= cpufreq_resume,
13101da177e4SLinus Torvalds };
13111da177e4SLinus Torvalds 
13121da177e4SLinus Torvalds 
13131da177e4SLinus Torvalds /*********************************************************************
13141da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
13151da177e4SLinus Torvalds  *********************************************************************/
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds /**
13181da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
13191da177e4SLinus Torvalds  *	@nb: notifier function to register
13201da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
13211da177e4SLinus Torvalds  *
13221da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
13231da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
13241da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
13251da177e4SLinus Torvalds  *      changes in cpufreq policy.
13261da177e4SLinus Torvalds  *
13271da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1328e041c683SAlan Stern  *	blocking_notifier_chain_register.
13291da177e4SLinus Torvalds  */
13301da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
13311da177e4SLinus Torvalds {
13321da177e4SLinus Torvalds 	int ret;
13331da177e4SLinus Torvalds 
13341da177e4SLinus Torvalds 	switch (list) {
13351da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1336b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1337e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
13381da177e4SLinus Torvalds 		break;
13391da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1340e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1341e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
13421da177e4SLinus Torvalds 		break;
13431da177e4SLinus Torvalds 	default:
13441da177e4SLinus Torvalds 		ret = -EINVAL;
13451da177e4SLinus Torvalds 	}
13461da177e4SLinus Torvalds 
13471da177e4SLinus Torvalds 	return ret;
13481da177e4SLinus Torvalds }
13491da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds /**
13531da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
13541da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
13551da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
13561da177e4SLinus Torvalds  *
13571da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
13581da177e4SLinus Torvalds  *
13591da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1360e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
13611da177e4SLinus Torvalds  */
13621da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
13631da177e4SLinus Torvalds {
13641da177e4SLinus Torvalds 	int ret;
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	switch (list) {
13671da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1368b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
1369e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
13701da177e4SLinus Torvalds 		break;
13711da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1372e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1373e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
13741da177e4SLinus Torvalds 		break;
13751da177e4SLinus Torvalds 	default:
13761da177e4SLinus Torvalds 		ret = -EINVAL;
13771da177e4SLinus Torvalds 	}
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	return ret;
13801da177e4SLinus Torvalds }
13811da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
13821da177e4SLinus Torvalds 
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds /*********************************************************************
13851da177e4SLinus Torvalds  *                              GOVERNORS                            *
13861da177e4SLinus Torvalds  *********************************************************************/
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
13901da177e4SLinus Torvalds 			    unsigned int target_freq,
13911da177e4SLinus Torvalds 			    unsigned int relation)
13921da177e4SLinus Torvalds {
13931da177e4SLinus Torvalds 	int retval = -EINVAL;
1394c32b6b8eSAshok Raj 
13951da177e4SLinus Torvalds 	dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
13961da177e4SLinus Torvalds 		target_freq, relation);
13971da177e4SLinus Torvalds 	if (cpu_online(policy->cpu) && cpufreq_driver->target)
13981da177e4SLinus Torvalds 		retval = cpufreq_driver->target(policy, target_freq, relation);
139990d45d17SAshok Raj 
14001da177e4SLinus Torvalds 	return retval;
14011da177e4SLinus Torvalds }
14021da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
14051da177e4SLinus Torvalds 			  unsigned int target_freq,
14061da177e4SLinus Torvalds 			  unsigned int relation)
14071da177e4SLinus Torvalds {
1408cc993cabSDave Jones 	int ret;
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(policy->cpu);
14111da177e4SLinus Torvalds 	if (!policy)
14121da177e4SLinus Torvalds 		return -EINVAL;
14131da177e4SLinus Torvalds 
14145a01f2e8SVenkatesh Pallipadi 	if (unlikely(lock_policy_rwsem_write(policy->cpu)))
14155a01f2e8SVenkatesh Pallipadi 		return -EINVAL;
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
14181da177e4SLinus Torvalds 
14195a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(policy->cpu);
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	cpufreq_cpu_put(policy);
14221da177e4SLinus Torvalds 	return ret;
14231da177e4SLinus Torvalds }
14241da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
14251da177e4SLinus Torvalds 
14265a01f2e8SVenkatesh Pallipadi int __cpufreq_driver_getavg(struct cpufreq_policy *policy)
1427dfde5d62SVenkatesh Pallipadi {
1428dfde5d62SVenkatesh Pallipadi 	int ret = 0;
1429dfde5d62SVenkatesh Pallipadi 
1430dfde5d62SVenkatesh Pallipadi 	policy = cpufreq_cpu_get(policy->cpu);
1431dfde5d62SVenkatesh Pallipadi 	if (!policy)
1432dfde5d62SVenkatesh Pallipadi 		return -EINVAL;
1433dfde5d62SVenkatesh Pallipadi 
1434dfde5d62SVenkatesh Pallipadi 	if (cpu_online(policy->cpu) && cpufreq_driver->getavg)
1435dfde5d62SVenkatesh Pallipadi 		ret = cpufreq_driver->getavg(policy->cpu);
1436dfde5d62SVenkatesh Pallipadi 
1437dfde5d62SVenkatesh Pallipadi 	cpufreq_cpu_put(policy);
1438dfde5d62SVenkatesh Pallipadi 	return ret;
1439dfde5d62SVenkatesh Pallipadi }
14405a01f2e8SVenkatesh Pallipadi EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
1441dfde5d62SVenkatesh Pallipadi 
1442153d7f3fSArjan van de Ven /*
1443153d7f3fSArjan van de Ven  * when "event" is CPUFREQ_GOV_LIMITS
1444153d7f3fSArjan van de Ven  */
14451da177e4SLinus Torvalds 
1446e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy,
1447e08f5f5bSGautham R Shenoy 					unsigned int event)
14481da177e4SLinus Torvalds {
1449cc993cabSDave Jones 	int ret;
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	if (!try_module_get(policy->governor->owner))
14521da177e4SLinus Torvalds 		return -EINVAL;
14531da177e4SLinus Torvalds 
1454e08f5f5bSGautham R Shenoy 	dprintk("__cpufreq_governor for CPU %u, event %u\n",
1455e08f5f5bSGautham R Shenoy 						policy->cpu, event);
14561da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
14571da177e4SLinus Torvalds 
1458e08f5f5bSGautham R Shenoy 	/* we keep one module reference alive for
1459e08f5f5bSGautham R Shenoy 			each CPU governed by this CPU */
14601da177e4SLinus Torvalds 	if ((event != CPUFREQ_GOV_START) || ret)
14611da177e4SLinus Torvalds 		module_put(policy->governor->owner);
14621da177e4SLinus Torvalds 	if ((event == CPUFREQ_GOV_STOP) && !ret)
14631da177e4SLinus Torvalds 		module_put(policy->governor->owner);
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 	return ret;
14661da177e4SLinus Torvalds }
14671da177e4SLinus Torvalds 
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
14701da177e4SLinus Torvalds {
14713bcb09a3SJeremy Fitzhardinge 	int err;
14721da177e4SLinus Torvalds 
14731da177e4SLinus Torvalds 	if (!governor)
14741da177e4SLinus Torvalds 		return -EINVAL;
14751da177e4SLinus Torvalds 
14763fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
14771da177e4SLinus Torvalds 
14783bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
14793bcb09a3SJeremy Fitzhardinge 	if (__find_governor(governor->name) == NULL) {
14803bcb09a3SJeremy Fitzhardinge 		err = 0;
14811da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
14823bcb09a3SJeremy Fitzhardinge 	}
14831da177e4SLinus Torvalds 
14843fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
14853bcb09a3SJeremy Fitzhardinge 	return err;
14861da177e4SLinus Torvalds }
14871da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
14881da177e4SLinus Torvalds 
14891da177e4SLinus Torvalds 
14901da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
14911da177e4SLinus Torvalds {
14921da177e4SLinus Torvalds 	if (!governor)
14931da177e4SLinus Torvalds 		return;
14941da177e4SLinus Torvalds 
14953fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
14961da177e4SLinus Torvalds 	list_del(&governor->governor_list);
14973fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
14981da177e4SLinus Torvalds 	return;
14991da177e4SLinus Torvalds }
15001da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds 
15031da177e4SLinus Torvalds 
15041da177e4SLinus Torvalds /*********************************************************************
15051da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
15061da177e4SLinus Torvalds  *********************************************************************/
15071da177e4SLinus Torvalds 
15081da177e4SLinus Torvalds /**
15091da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
15101da177e4SLinus Torvalds  * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
15111da177e4SLinus Torvalds  *
15121da177e4SLinus Torvalds  * Reads the current cpufreq policy.
15131da177e4SLinus Torvalds  */
15141da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
15151da177e4SLinus Torvalds {
15161da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
15171da177e4SLinus Torvalds 	if (!policy)
15181da177e4SLinus Torvalds 		return -EINVAL;
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
15211da177e4SLinus Torvalds 	if (!cpu_policy)
15221da177e4SLinus Torvalds 		return -EINVAL;
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
15271da177e4SLinus Torvalds 	return 0;
15281da177e4SLinus Torvalds }
15291da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 
1532153d7f3fSArjan van de Ven /*
1533e08f5f5bSGautham R Shenoy  * data   : current policy.
1534e08f5f5bSGautham R Shenoy  * policy : policy to be set.
1535153d7f3fSArjan van de Ven  */
1536e08f5f5bSGautham R Shenoy static int __cpufreq_set_policy(struct cpufreq_policy *data,
1537e08f5f5bSGautham R Shenoy 				struct cpufreq_policy *policy)
15381da177e4SLinus Torvalds {
15391da177e4SLinus Torvalds 	int ret = 0;
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 	cpufreq_debug_disable_ratelimit();
15421da177e4SLinus Torvalds 	dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
15431da177e4SLinus Torvalds 		policy->min, policy->max);
15441da177e4SLinus Torvalds 
1545e08f5f5bSGautham R Shenoy 	memcpy(&policy->cpuinfo, &data->cpuinfo,
1546e08f5f5bSGautham R Shenoy 				sizeof(struct cpufreq_cpuinfo));
15471da177e4SLinus Torvalds 
15489c9a43edSMattia Dongili 	if (policy->min > data->min && policy->min > policy->max) {
15499c9a43edSMattia Dongili 		ret = -EINVAL;
15509c9a43edSMattia Dongili 		goto error_out;
15519c9a43edSMattia Dongili 	}
15529c9a43edSMattia Dongili 
15531da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
15541da177e4SLinus Torvalds 	ret = cpufreq_driver->verify(policy);
15551da177e4SLinus Torvalds 	if (ret)
15561da177e4SLinus Torvalds 		goto error_out;
15571da177e4SLinus Torvalds 
15581da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
1559e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1560e041c683SAlan Stern 			CPUFREQ_ADJUST, policy);
15611da177e4SLinus Torvalds 
15621da177e4SLinus Torvalds 	/* adjust if necessary - hardware incompatibility*/
1563e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1564e041c683SAlan Stern 			CPUFREQ_INCOMPATIBLE, policy);
15651da177e4SLinus Torvalds 
15661da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit,
15671da177e4SLinus Torvalds 	   which might be different to the first one */
15681da177e4SLinus Torvalds 	ret = cpufreq_driver->verify(policy);
1569e041c683SAlan Stern 	if (ret)
15701da177e4SLinus Torvalds 		goto error_out;
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 	/* notification of the new policy */
1573e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1574e041c683SAlan Stern 			CPUFREQ_NOTIFY, policy);
15751da177e4SLinus Torvalds 
15761da177e4SLinus Torvalds 	data->min = policy->min;
15771da177e4SLinus Torvalds 	data->max = policy->max;
15781da177e4SLinus Torvalds 
1579e08f5f5bSGautham R Shenoy 	dprintk("new min and max freqs are %u - %u kHz\n",
1580e08f5f5bSGautham R Shenoy 					data->min, data->max);
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	if (cpufreq_driver->setpolicy) {
15831da177e4SLinus Torvalds 		data->policy = policy->policy;
15841da177e4SLinus Torvalds 		dprintk("setting range\n");
15851da177e4SLinus Torvalds 		ret = cpufreq_driver->setpolicy(policy);
15861da177e4SLinus Torvalds 	} else {
15871da177e4SLinus Torvalds 		if (policy->governor != data->governor) {
15881da177e4SLinus Torvalds 			/* save old, working values */
15891da177e4SLinus Torvalds 			struct cpufreq_governor *old_gov = data->governor;
15901da177e4SLinus Torvalds 
15911da177e4SLinus Torvalds 			dprintk("governor switch\n");
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 			/* end old governor */
15941da177e4SLinus Torvalds 			if (data->governor)
15951da177e4SLinus Torvalds 				__cpufreq_governor(data, CPUFREQ_GOV_STOP);
15961da177e4SLinus Torvalds 
15971da177e4SLinus Torvalds 			/* start new governor */
15981da177e4SLinus Torvalds 			data->governor = policy->governor;
15991da177e4SLinus Torvalds 			if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
16001da177e4SLinus Torvalds 				/* new governor failed, so re-start old one */
1601e08f5f5bSGautham R Shenoy 				dprintk("starting governor %s failed\n",
1602e08f5f5bSGautham R Shenoy 							data->governor->name);
16031da177e4SLinus Torvalds 				if (old_gov) {
16041da177e4SLinus Torvalds 					data->governor = old_gov;
1605e08f5f5bSGautham R Shenoy 					__cpufreq_governor(data,
1606e08f5f5bSGautham R Shenoy 							   CPUFREQ_GOV_START);
16071da177e4SLinus Torvalds 				}
16081da177e4SLinus Torvalds 				ret = -EINVAL;
16091da177e4SLinus Torvalds 				goto error_out;
16101da177e4SLinus Torvalds 			}
16111da177e4SLinus Torvalds 			/* might be a policy change, too, so fall through */
16121da177e4SLinus Torvalds 		}
16131da177e4SLinus Torvalds 		dprintk("governor: change or update limits\n");
16141da177e4SLinus Torvalds 		__cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
16151da177e4SLinus Torvalds 	}
16161da177e4SLinus Torvalds 
16171da177e4SLinus Torvalds error_out:
16181da177e4SLinus Torvalds 	cpufreq_debug_enable_ratelimit();
16191da177e4SLinus Torvalds 	return ret;
16201da177e4SLinus Torvalds }
16211da177e4SLinus Torvalds 
16221da177e4SLinus Torvalds /**
16231da177e4SLinus Torvalds  *	cpufreq_set_policy - set a new CPUFreq policy
16241da177e4SLinus Torvalds  *	@policy: policy to be set.
16251da177e4SLinus Torvalds  *
16261da177e4SLinus Torvalds  *	Sets a new CPU frequency and voltage scaling policy.
16271da177e4SLinus Torvalds  */
16281da177e4SLinus Torvalds int cpufreq_set_policy(struct cpufreq_policy *policy)
16291da177e4SLinus Torvalds {
16301da177e4SLinus Torvalds 	int ret = 0;
16311da177e4SLinus Torvalds 	struct cpufreq_policy *data;
16321da177e4SLinus Torvalds 
16331da177e4SLinus Torvalds 	if (!policy)
16341da177e4SLinus Torvalds 		return -EINVAL;
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 	data = cpufreq_cpu_get(policy->cpu);
16371da177e4SLinus Torvalds 	if (!data)
16381da177e4SLinus Torvalds 		return -EINVAL;
16391da177e4SLinus Torvalds 
16405a01f2e8SVenkatesh Pallipadi 	if (unlikely(lock_policy_rwsem_write(policy->cpu)))
16415a01f2e8SVenkatesh Pallipadi 		return -EINVAL;
16425a01f2e8SVenkatesh Pallipadi 
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	ret = __cpufreq_set_policy(data, policy);
16451da177e4SLinus Torvalds 	data->user_policy.min = data->min;
16461da177e4SLinus Torvalds 	data->user_policy.max = data->max;
16471da177e4SLinus Torvalds 	data->user_policy.policy = data->policy;
16481da177e4SLinus Torvalds 	data->user_policy.governor = data->governor;
16491da177e4SLinus Torvalds 
16505a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(policy->cpu);
1651153d7f3fSArjan van de Ven 
16521da177e4SLinus Torvalds 	cpufreq_cpu_put(data);
16531da177e4SLinus Torvalds 
16541da177e4SLinus Torvalds 	return ret;
16551da177e4SLinus Torvalds }
16561da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_set_policy);
16571da177e4SLinus Torvalds 
16581da177e4SLinus Torvalds 
16591da177e4SLinus Torvalds /**
16601da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
16611da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
16621da177e4SLinus Torvalds  *
16631da177e4SLinus Torvalds  *	Usefull for policy notifiers which have different necessities
16641da177e4SLinus Torvalds  *	at different times.
16651da177e4SLinus Torvalds  */
16661da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
16671da177e4SLinus Torvalds {
16681da177e4SLinus Torvalds 	struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
16691da177e4SLinus Torvalds 	struct cpufreq_policy policy;
16701da177e4SLinus Torvalds 	int ret = 0;
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds 	if (!data)
16731da177e4SLinus Torvalds 		return -ENODEV;
16741da177e4SLinus Torvalds 
16755a01f2e8SVenkatesh Pallipadi 	if (unlikely(lock_policy_rwsem_write(cpu)))
16765a01f2e8SVenkatesh Pallipadi 		return -EINVAL;
16771da177e4SLinus Torvalds 
16781da177e4SLinus Torvalds 	dprintk("updating policy for CPU %u\n", cpu);
16797d5e350fSDave Jones 	memcpy(&policy, data, sizeof(struct cpufreq_policy));
16801da177e4SLinus Torvalds 	policy.min = data->user_policy.min;
16811da177e4SLinus Torvalds 	policy.max = data->user_policy.max;
16821da177e4SLinus Torvalds 	policy.policy = data->user_policy.policy;
16831da177e4SLinus Torvalds 	policy.governor = data->user_policy.governor;
16841da177e4SLinus Torvalds 
16850961dd0dSThomas Renninger 	/* BIOS might change freq behind our back
16860961dd0dSThomas Renninger 	  -> ask driver for current freq and notify governors about a change */
16870961dd0dSThomas Renninger 	if (cpufreq_driver->get) {
16880961dd0dSThomas Renninger 		policy.cur = cpufreq_driver->get(cpu);
1689a85f7bd3SThomas Renninger 		if (!data->cur) {
1690a85f7bd3SThomas Renninger 			dprintk("Driver did not initialize current freq");
1691a85f7bd3SThomas Renninger 			data->cur = policy.cur;
1692a85f7bd3SThomas Renninger 		} else {
16930961dd0dSThomas Renninger 			if (data->cur != policy.cur)
1694e08f5f5bSGautham R Shenoy 				cpufreq_out_of_sync(cpu, data->cur,
1695e08f5f5bSGautham R Shenoy 								policy.cur);
16960961dd0dSThomas Renninger 		}
1697a85f7bd3SThomas Renninger 	}
16980961dd0dSThomas Renninger 
16991da177e4SLinus Torvalds 	ret = __cpufreq_set_policy(data, &policy);
17001da177e4SLinus Torvalds 
17015a01f2e8SVenkatesh Pallipadi 	unlock_policy_rwsem_write(cpu);
17025a01f2e8SVenkatesh Pallipadi 
17031da177e4SLinus Torvalds 	cpufreq_cpu_put(data);
17041da177e4SLinus Torvalds 	return ret;
17051da177e4SLinus Torvalds }
17061da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
17071da177e4SLinus Torvalds 
170865edc68cSChandra Seetharaman static int cpufreq_cpu_callback(struct notifier_block *nfb,
1709c32b6b8eSAshok Raj 					unsigned long action, void *hcpu)
1710c32b6b8eSAshok Raj {
1711c32b6b8eSAshok Raj 	unsigned int cpu = (unsigned long)hcpu;
1712c32b6b8eSAshok Raj 	struct sys_device *sys_dev;
17135a01f2e8SVenkatesh Pallipadi 	struct cpufreq_policy *policy;
1714c32b6b8eSAshok Raj 
1715c32b6b8eSAshok Raj 	sys_dev = get_cpu_sysdev(cpu);
1716c32b6b8eSAshok Raj 	if (sys_dev) {
1717c32b6b8eSAshok Raj 		switch (action) {
1718c32b6b8eSAshok Raj 		case CPU_ONLINE:
1719c32b6b8eSAshok Raj 			cpufreq_add_dev(sys_dev);
1720c32b6b8eSAshok Raj 			break;
1721c32b6b8eSAshok Raj 		case CPU_DOWN_PREPARE:
17225a01f2e8SVenkatesh Pallipadi 			if (unlikely(lock_policy_rwsem_write(cpu)))
17235a01f2e8SVenkatesh Pallipadi 				BUG();
17245a01f2e8SVenkatesh Pallipadi 
1725c32b6b8eSAshok Raj 			policy = cpufreq_cpu_data[cpu];
1726c32b6b8eSAshok Raj 			if (policy) {
17275a01f2e8SVenkatesh Pallipadi 				__cpufreq_driver_target(policy, policy->min,
1728c32b6b8eSAshok Raj 						CPUFREQ_RELATION_H);
1729c32b6b8eSAshok Raj 			}
17305a01f2e8SVenkatesh Pallipadi 			__cpufreq_remove_dev(sys_dev);
1731c32b6b8eSAshok Raj 			break;
17325a01f2e8SVenkatesh Pallipadi 		case CPU_DOWN_FAILED:
17335a01f2e8SVenkatesh Pallipadi 			cpufreq_add_dev(sys_dev);
1734c32b6b8eSAshok Raj 			break;
1735c32b6b8eSAshok Raj 		}
1736c32b6b8eSAshok Raj 	}
1737c32b6b8eSAshok Raj 	return NOTIFY_OK;
1738c32b6b8eSAshok Raj }
1739c32b6b8eSAshok Raj 
174074b85f37SChandra Seetharaman static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
1741c32b6b8eSAshok Raj {
1742c32b6b8eSAshok Raj     .notifier_call = cpufreq_cpu_callback,
1743c32b6b8eSAshok Raj };
17441da177e4SLinus Torvalds 
17451da177e4SLinus Torvalds /*********************************************************************
17461da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
17471da177e4SLinus Torvalds  *********************************************************************/
17481da177e4SLinus Torvalds 
17491da177e4SLinus Torvalds /**
17501da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
17511da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
17521da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
17531da177e4SLinus Torvalds  *
17541da177e4SLinus Torvalds  *   Registers a CPU Frequency driver to this core code. This code
17551da177e4SLinus Torvalds  * returns zero on success, -EBUSY when another driver got here first
17561da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
17571da177e4SLinus Torvalds  *
17581da177e4SLinus Torvalds  */
1759221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
17601da177e4SLinus Torvalds {
17611da177e4SLinus Torvalds 	unsigned long flags;
17621da177e4SLinus Torvalds 	int ret;
17631da177e4SLinus Torvalds 
17641da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
17651da177e4SLinus Torvalds 	    ((!driver_data->setpolicy) && (!driver_data->target)))
17661da177e4SLinus Torvalds 		return -EINVAL;
17671da177e4SLinus Torvalds 
17681da177e4SLinus Torvalds 	dprintk("trying to register driver %s\n", driver_data->name);
17691da177e4SLinus Torvalds 
17701da177e4SLinus Torvalds 	if (driver_data->setpolicy)
17711da177e4SLinus Torvalds 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
17741da177e4SLinus Torvalds 	if (cpufreq_driver) {
17751da177e4SLinus Torvalds 		spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
17761da177e4SLinus Torvalds 		return -EBUSY;
17771da177e4SLinus Torvalds 	}
17781da177e4SLinus Torvalds 	cpufreq_driver = driver_data;
17791da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
17821da177e4SLinus Torvalds 
17831da177e4SLinus Torvalds 	if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
17841da177e4SLinus Torvalds 		int i;
17851da177e4SLinus Torvalds 		ret = -ENODEV;
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 		/* check for at least one working CPU */
17881da177e4SLinus Torvalds 		for (i=0; i<NR_CPUS; i++)
17891da177e4SLinus Torvalds 			if (cpufreq_cpu_data[i])
17901da177e4SLinus Torvalds 				ret = 0;
17911da177e4SLinus Torvalds 
17921da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
17931da177e4SLinus Torvalds 		if (ret) {
1794e08f5f5bSGautham R Shenoy 			dprintk("no CPU initialized for driver %s\n",
1795e08f5f5bSGautham R Shenoy 							driver_data->name);
1796e08f5f5bSGautham R Shenoy 			sysdev_driver_unregister(&cpu_sysdev_class,
1797e08f5f5bSGautham R Shenoy 						&cpufreq_sysdev_driver);
17981da177e4SLinus Torvalds 
17991da177e4SLinus Torvalds 			spin_lock_irqsave(&cpufreq_driver_lock, flags);
18001da177e4SLinus Torvalds 			cpufreq_driver = NULL;
18011da177e4SLinus Torvalds 			spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
18021da177e4SLinus Torvalds 		}
18031da177e4SLinus Torvalds 	}
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	if (!ret) {
180665edc68cSChandra Seetharaman 		register_hotcpu_notifier(&cpufreq_cpu_notifier);
18071da177e4SLinus Torvalds 		dprintk("driver %s up and running\n", driver_data->name);
18081da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
18091da177e4SLinus Torvalds 	}
18101da177e4SLinus Torvalds 
18111da177e4SLinus Torvalds 	return (ret);
18121da177e4SLinus Torvalds }
18131da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
18141da177e4SLinus Torvalds 
18151da177e4SLinus Torvalds 
18161da177e4SLinus Torvalds /**
18171da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
18181da177e4SLinus Torvalds  *
18191da177e4SLinus Torvalds  *    Unregister the current CPUFreq driver. Only call this if you have
18201da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
18211da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
18221da177e4SLinus Torvalds  * currently not initialised.
18231da177e4SLinus Torvalds  */
1824221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
18251da177e4SLinus Torvalds {
18261da177e4SLinus Torvalds 	unsigned long flags;
18271da177e4SLinus Torvalds 
18281da177e4SLinus Torvalds 	cpufreq_debug_disable_ratelimit();
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 	if (!cpufreq_driver || (driver != cpufreq_driver)) {
18311da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
18321da177e4SLinus Torvalds 		return -EINVAL;
18331da177e4SLinus Torvalds 	}
18341da177e4SLinus Torvalds 
18351da177e4SLinus Torvalds 	dprintk("unregistering driver %s\n", driver->name);
18361da177e4SLinus Torvalds 
18371da177e4SLinus Torvalds 	sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
183865edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
18391da177e4SLinus Torvalds 
18401da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
18411da177e4SLinus Torvalds 	cpufreq_driver = NULL;
18421da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	return 0;
18451da177e4SLinus Torvalds }
18461da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
18475a01f2e8SVenkatesh Pallipadi 
18485a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
18495a01f2e8SVenkatesh Pallipadi {
18505a01f2e8SVenkatesh Pallipadi 	int cpu;
18515a01f2e8SVenkatesh Pallipadi 
18525a01f2e8SVenkatesh Pallipadi 	for_each_possible_cpu(cpu) {
18535a01f2e8SVenkatesh Pallipadi 		per_cpu(policy_cpu, cpu) = -1;
18545a01f2e8SVenkatesh Pallipadi 		init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
18555a01f2e8SVenkatesh Pallipadi 	}
18565a01f2e8SVenkatesh Pallipadi 	return 0;
18575a01f2e8SVenkatesh Pallipadi }
18585a01f2e8SVenkatesh Pallipadi 
18595a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
1860