xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision 9c9a43ed2734081124407c779b36a4761c41139b)
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 
321da177e4SLinus Torvalds #define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds /**
351da177e4SLinus Torvalds  * The "cpufreq driver" - the arch- or hardware-dependend low
361da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
371da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
381da177e4SLinus Torvalds  */
391da177e4SLinus Torvalds static struct cpufreq_driver *cpufreq_driver;
401da177e4SLinus Torvalds static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
411da177e4SLinus Torvalds static DEFINE_SPINLOCK(cpufreq_driver_lock);
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /* internal prototypes */
441da177e4SLinus Torvalds static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
451da177e4SLinus Torvalds static void handle_update(void *data);
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /**
481da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
491da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
501da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
511da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
521da177e4SLinus Torvalds  * The mutex locks both lists.
531da177e4SLinus Torvalds  */
54e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
55e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_transition_notifier_list);
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list);
593fc54d37Sakpm@osdl.org static DEFINE_MUTEX (cpufreq_governor_mutex);
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
621da177e4SLinus Torvalds {
631da177e4SLinus Torvalds 	struct cpufreq_policy *data;
641da177e4SLinus Torvalds 	unsigned long flags;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	if (cpu >= NR_CPUS)
671da177e4SLinus Torvalds 		goto err_out;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	/* get the cpufreq driver */
701da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	if (!cpufreq_driver)
731da177e4SLinus Torvalds 		goto err_out_unlock;
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	if (!try_module_get(cpufreq_driver->owner))
761da177e4SLinus Torvalds 		goto err_out_unlock;
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds 	/* get the CPU */
801da177e4SLinus Torvalds 	data = cpufreq_cpu_data[cpu];
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	if (!data)
831da177e4SLinus Torvalds 		goto err_out_put_module;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	if (!kobject_get(&data->kobj))
861da177e4SLinus Torvalds 		goto err_out_put_module;
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
891da177e4SLinus Torvalds 	return data;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds err_out_put_module:
921da177e4SLinus Torvalds 	module_put(cpufreq_driver->owner);
931da177e4SLinus Torvalds err_out_unlock:
941da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
951da177e4SLinus Torvalds err_out:
961da177e4SLinus Torvalds 	return NULL;
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
991da177e4SLinus Torvalds 
1007d5e350fSDave Jones 
1011da177e4SLinus Torvalds void cpufreq_cpu_put(struct cpufreq_policy *data)
1021da177e4SLinus Torvalds {
1031da177e4SLinus Torvalds 	kobject_put(&data->kobj);
1041da177e4SLinus Torvalds 	module_put(cpufreq_driver->owner);
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /*********************************************************************
1101da177e4SLinus Torvalds  *                     UNIFIED DEBUG HELPERS                         *
1111da177e4SLinus Torvalds  *********************************************************************/
1121da177e4SLinus Torvalds #ifdef CONFIG_CPU_FREQ_DEBUG
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /* what part(s) of the CPUfreq subsystem are debugged? */
1151da177e4SLinus Torvalds static unsigned int debug;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds /* is the debug output ratelimit'ed using printk_ratelimit? User can
1181da177e4SLinus Torvalds  * set or modify this value.
1191da177e4SLinus Torvalds  */
1201da177e4SLinus Torvalds static unsigned int debug_ratelimit = 1;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /* is the printk_ratelimit'ing enabled? It's enabled after a successful
1231da177e4SLinus Torvalds  * loading of a cpufreq driver, temporarily disabled when a new policy
1241da177e4SLinus Torvalds  * is set, and disabled upon cpufreq driver removal
1251da177e4SLinus Torvalds  */
1261da177e4SLinus Torvalds static unsigned int disable_ratelimit = 1;
1271da177e4SLinus Torvalds static DEFINE_SPINLOCK(disable_ratelimit_lock);
1281da177e4SLinus Torvalds 
129858119e1SArjan van de Ven static void cpufreq_debug_enable_ratelimit(void)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	unsigned long flags;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	spin_lock_irqsave(&disable_ratelimit_lock, flags);
1341da177e4SLinus Torvalds 	if (disable_ratelimit)
1351da177e4SLinus Torvalds 		disable_ratelimit--;
1361da177e4SLinus Torvalds 	spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
139858119e1SArjan van de Ven static void cpufreq_debug_disable_ratelimit(void)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	unsigned long flags;
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	spin_lock_irqsave(&disable_ratelimit_lock, flags);
1441da177e4SLinus Torvalds 	disable_ratelimit++;
1451da177e4SLinus Torvalds 	spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	char s[256];
1511da177e4SLinus Torvalds 	va_list args;
1521da177e4SLinus Torvalds 	unsigned int len;
1531da177e4SLinus Torvalds 	unsigned long flags;
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	WARN_ON(!prefix);
1561da177e4SLinus Torvalds 	if (type & debug) {
1571da177e4SLinus Torvalds 		spin_lock_irqsave(&disable_ratelimit_lock, flags);
1581da177e4SLinus Torvalds 		if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
1591da177e4SLinus Torvalds 			spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
1601da177e4SLinus Torvalds 			return;
1611da177e4SLinus Torvalds 		}
1621da177e4SLinus Torvalds 		spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 		len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 		va_start(args, fmt);
1671da177e4SLinus Torvalds 		len += vsnprintf(&s[len], (256 - len), fmt, args);
1681da177e4SLinus Torvalds 		va_end(args);
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 		printk(s);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 		WARN_ON(len < 5);
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_debug_printk);
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds module_param(debug, uint, 0644);
1791da177e4SLinus Torvalds MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds module_param(debug_ratelimit, uint, 0644);
1821da177e4SLinus Torvalds MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds #else /* !CONFIG_CPU_FREQ_DEBUG */
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds static inline void cpufreq_debug_enable_ratelimit(void) { return; }
1871da177e4SLinus Torvalds static inline void cpufreq_debug_disable_ratelimit(void) { return; }
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds #endif /* CONFIG_CPU_FREQ_DEBUG */
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /*********************************************************************
1931da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
1941da177e4SLinus Torvalds  *********************************************************************/
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /**
1971da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
1981da177e4SLinus Torvalds  *
1991da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
2001da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
2011da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
2021da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
2031da177e4SLinus Torvalds  */
2041da177e4SLinus Torvalds #ifndef CONFIG_SMP
2051da177e4SLinus Torvalds static unsigned long l_p_j_ref;
2061da177e4SLinus Torvalds static unsigned int  l_p_j_ref_freq;
2071da177e4SLinus Torvalds 
208858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
2111da177e4SLinus Torvalds 		return;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
2141da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
2151da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
2161da177e4SLinus Torvalds 		dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
2171da177e4SLinus Torvalds 	}
2181da177e4SLinus Torvalds 	if ((val == CPUFREQ_PRECHANGE  && ci->old < ci->new) ||
2191da177e4SLinus Torvalds 	    (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
22042d4dc3fSBenjamin Herrenschmidt 	    (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
2211da177e4SLinus Torvalds 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
2221da177e4SLinus Torvalds 		dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds #else
2261da177e4SLinus Torvalds static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
2271da177e4SLinus Torvalds #endif
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds /**
231e4472cb3SDave Jones  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
232e4472cb3SDave Jones  * on frequency transition.
2331da177e4SLinus Torvalds  *
234e4472cb3SDave Jones  * This function calls the transition notifiers and the "adjust_jiffies"
235e4472cb3SDave Jones  * function. It is called twice on all CPU frequency changes that have
236e4472cb3SDave Jones  * external effects.
2371da177e4SLinus Torvalds  */
2381da177e4SLinus Torvalds void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
2391da177e4SLinus Torvalds {
240e4472cb3SDave Jones 	struct cpufreq_policy *policy;
241e4472cb3SDave Jones 
2421da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	freqs->flags = cpufreq_driver->flags;
245e4472cb3SDave Jones 	dprintk("notification %u of frequency transition to %u kHz\n",
246e4472cb3SDave Jones 		state, freqs->new);
2471da177e4SLinus Torvalds 
248e4472cb3SDave Jones 	policy = cpufreq_cpu_data[freqs->cpu];
2491da177e4SLinus Torvalds 	switch (state) {
250e4472cb3SDave Jones 
2511da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
252e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
253e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
254e4472cb3SDave Jones 		 * "old frequency".
2551da177e4SLinus Torvalds 		 */
2561da177e4SLinus Torvalds 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
257e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
258e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
259b10eec22SJan Beulich 				dprintk("Warning: CPU frequency is"
260e4472cb3SDave Jones 					" %u, cpufreq assumed %u kHz.\n",
261e4472cb3SDave Jones 					freqs->old, policy->cur);
262e4472cb3SDave Jones 				freqs->old = policy->cur;
2631da177e4SLinus Torvalds 			}
2641da177e4SLinus Torvalds 		}
265e041c683SAlan Stern 		blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
266e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
2671da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
2681da177e4SLinus Torvalds 		break;
269e4472cb3SDave Jones 
2701da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
2711da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
272e041c683SAlan Stern 		blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
273e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
274e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
275e4472cb3SDave Jones 			policy->cur = freqs->new;
2761da177e4SLinus Torvalds 		break;
2771da177e4SLinus Torvalds 	}
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
2801da177e4SLinus Torvalds 
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds /*********************************************************************
2841da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
2851da177e4SLinus Torvalds  *********************************************************************/
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds /**
2881da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
2891da177e4SLinus Torvalds  */
2901da177e4SLinus Torvalds static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
2911da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	if (!cpufreq_driver)
2941da177e4SLinus Torvalds 		return -EINVAL;
2951da177e4SLinus Torvalds 	if (cpufreq_driver->setpolicy) {
2961da177e4SLinus Torvalds 		if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
2971da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
2981da177e4SLinus Torvalds 			return 0;
2991da177e4SLinus Torvalds 		} else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
3001da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
3011da177e4SLinus Torvalds 			return 0;
3021da177e4SLinus Torvalds 		}
3031da177e4SLinus Torvalds 		return -EINVAL;
3041da177e4SLinus Torvalds 	} else {
3051da177e4SLinus Torvalds 		struct cpufreq_governor *t;
3063fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
3071da177e4SLinus Torvalds 		if (!cpufreq_driver || !cpufreq_driver->target)
3081da177e4SLinus Torvalds 			goto out;
3091da177e4SLinus Torvalds 		list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
3101da177e4SLinus Torvalds 			if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
3111da177e4SLinus Torvalds 				*governor = t;
3123fc54d37Sakpm@osdl.org 				mutex_unlock(&cpufreq_governor_mutex);
3131da177e4SLinus Torvalds 				return 0;
3141da177e4SLinus Torvalds 			}
3151da177e4SLinus Torvalds 		}
3161da177e4SLinus Torvalds out:
3173fc54d37Sakpm@osdl.org 		mutex_unlock(&cpufreq_governor_mutex);
3181da177e4SLinus Torvalds 	}
3191da177e4SLinus Torvalds 	return -EINVAL;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 
3231da177e4SLinus Torvalds /* drivers/base/cpu.c */
3241da177e4SLinus Torvalds extern struct sysdev_class cpu_sysdev_class;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds /**
3281da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
3291da177e4SLinus Torvalds  *
3301da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
3311da177e4SLinus Torvalds  * "unsigned int".
3321da177e4SLinus Torvalds  */
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds #define show_one(file_name, object)			\
3351da177e4SLinus Torvalds static ssize_t show_##file_name				\
3361da177e4SLinus Torvalds (struct cpufreq_policy * policy, char *buf)		\
3371da177e4SLinus Torvalds {							\
3381da177e4SLinus Torvalds 	return sprintf (buf, "%u\n", policy->object);	\
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
3421da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
3431da177e4SLinus Torvalds show_one(scaling_min_freq, min);
3441da177e4SLinus Torvalds show_one(scaling_max_freq, max);
3451da177e4SLinus Torvalds show_one(scaling_cur_freq, cur);
3461da177e4SLinus Torvalds 
3477970e08bSThomas Renninger static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
3487970e08bSThomas Renninger 
3491da177e4SLinus Torvalds /**
3501da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
3511da177e4SLinus Torvalds  */
3521da177e4SLinus Torvalds #define store_one(file_name, object)			\
3531da177e4SLinus Torvalds static ssize_t store_##file_name					\
3541da177e4SLinus Torvalds (struct cpufreq_policy * policy, const char *buf, size_t count)		\
3551da177e4SLinus Torvalds {									\
3561da177e4SLinus Torvalds 	unsigned int ret = -EINVAL;					\
3571da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
3581da177e4SLinus Torvalds 									\
3591da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
3601da177e4SLinus Torvalds 	if (ret)							\
3611da177e4SLinus Torvalds 		return -EINVAL;						\
3621da177e4SLinus Torvalds 									\
3631da177e4SLinus Torvalds 	ret = sscanf (buf, "%u", &new_policy.object);			\
3641da177e4SLinus Torvalds 	if (ret != 1)							\
3651da177e4SLinus Torvalds 		return -EINVAL;						\
3661da177e4SLinus Torvalds 									\
367153d7f3fSArjan van de Ven 	lock_cpu_hotplug();						\
3687970e08bSThomas Renninger 	mutex_lock(&policy->lock);					\
3697970e08bSThomas Renninger 	ret = __cpufreq_set_policy(policy, &new_policy);		\
3707970e08bSThomas Renninger 	policy->user_policy.object = policy->object;			\
3717970e08bSThomas Renninger 	mutex_unlock(&policy->lock);					\
372153d7f3fSArjan van de Ven 	unlock_cpu_hotplug();						\
3731da177e4SLinus Torvalds 									\
3741da177e4SLinus Torvalds 	return ret ? ret : count;					\
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds store_one(scaling_min_freq,min);
3781da177e4SLinus Torvalds store_one(scaling_max_freq,max);
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds /**
3811da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
3821da177e4SLinus Torvalds  */
3831da177e4SLinus Torvalds static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
3841da177e4SLinus Torvalds {
3851da177e4SLinus Torvalds 	unsigned int cur_freq = cpufreq_get(policy->cpu);
3861da177e4SLinus Torvalds 	if (!cur_freq)
3871da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
3881da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 
3921da177e4SLinus Torvalds /**
3931da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
3941da177e4SLinus Torvalds  */
3951da177e4SLinus Torvalds static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
3961da177e4SLinus Torvalds {
3971da177e4SLinus Torvalds 	if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
3981da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
3991da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
4001da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
4011da177e4SLinus Torvalds 	else if (policy->governor)
4021da177e4SLinus Torvalds 		return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
4031da177e4SLinus Torvalds 	return -EINVAL;
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds /**
4081da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
4091da177e4SLinus Torvalds  */
4101da177e4SLinus Torvalds static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
4111da177e4SLinus Torvalds 				       const char *buf, size_t count)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	unsigned int ret = -EINVAL;
4141da177e4SLinus Torvalds 	char	str_governor[16];
4151da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 	ret = cpufreq_get_policy(&new_policy, policy->cpu);
4181da177e4SLinus Torvalds 	if (ret)
4191da177e4SLinus Torvalds 		return ret;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	ret = sscanf (buf, "%15s", str_governor);
4221da177e4SLinus Torvalds 	if (ret != 1)
4231da177e4SLinus Torvalds 		return -EINVAL;
4241da177e4SLinus Torvalds 
4251da177e4SLinus Torvalds 	if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
4261da177e4SLinus Torvalds 		return -EINVAL;
4271da177e4SLinus Torvalds 
428a496e25dSDave Jones 	lock_cpu_hotplug();
429a496e25dSDave Jones 
4307970e08bSThomas Renninger 	/* Do not use cpufreq_set_policy here or the user_policy.max
4317970e08bSThomas Renninger 	   will be wrongly overridden */
4327970e08bSThomas Renninger 	mutex_lock(&policy->lock);
4337970e08bSThomas Renninger 	ret = __cpufreq_set_policy(policy, &new_policy);
4347970e08bSThomas Renninger 
4357970e08bSThomas Renninger 	policy->user_policy.policy = policy->policy;
4367970e08bSThomas Renninger 	policy->user_policy.governor = policy->governor;
4377970e08bSThomas Renninger 	mutex_unlock(&policy->lock);
4387970e08bSThomas Renninger 
439a496e25dSDave Jones 	unlock_cpu_hotplug();
440a496e25dSDave Jones 
4411da177e4SLinus Torvalds 	return ret ? ret : count;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4441da177e4SLinus Torvalds /**
4451da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
4461da177e4SLinus Torvalds  */
4471da177e4SLinus Torvalds static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
4481da177e4SLinus Torvalds {
4491da177e4SLinus Torvalds 	return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds /**
4531da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
4541da177e4SLinus Torvalds  */
4551da177e4SLinus Torvalds static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
4561da177e4SLinus Torvalds 				char *buf)
4571da177e4SLinus Torvalds {
4581da177e4SLinus Torvalds 	ssize_t i = 0;
4591da177e4SLinus Torvalds 	struct cpufreq_governor *t;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	if (!cpufreq_driver->target) {
4621da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
4631da177e4SLinus Torvalds 		goto out;
4641da177e4SLinus Torvalds 	}
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
4671da177e4SLinus Torvalds 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
4681da177e4SLinus Torvalds 			goto out;
4691da177e4SLinus Torvalds 		i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds out:
4721da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
4731da177e4SLinus Torvalds 	return i;
4741da177e4SLinus Torvalds }
4751da177e4SLinus Torvalds /**
4761da177e4SLinus Torvalds  * show_affected_cpus - show the CPUs affected by each transition
4771da177e4SLinus Torvalds  */
4781da177e4SLinus Torvalds static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
4791da177e4SLinus Torvalds {
4801da177e4SLinus Torvalds 	ssize_t i = 0;
4811da177e4SLinus Torvalds 	unsigned int cpu;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	for_each_cpu_mask(cpu, policy->cpus) {
4841da177e4SLinus Torvalds 		if (i)
4851da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
4861da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
4871da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
4881da177e4SLinus Torvalds 		    break;
4891da177e4SLinus Torvalds 	}
4901da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
4911da177e4SLinus Torvalds 	return i;
4921da177e4SLinus Torvalds }
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds #define define_one_ro(_name) \
4961da177e4SLinus Torvalds static struct freq_attr _name = \
4971da177e4SLinus Torvalds __ATTR(_name, 0444, show_##_name, NULL)
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds #define define_one_ro0400(_name) \
5001da177e4SLinus Torvalds static struct freq_attr _name = \
5011da177e4SLinus Torvalds __ATTR(_name, 0400, show_##_name, NULL)
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds #define define_one_rw(_name) \
5041da177e4SLinus Torvalds static struct freq_attr _name = \
5051da177e4SLinus Torvalds __ATTR(_name, 0644, show_##_name, store_##_name)
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds define_one_ro0400(cpuinfo_cur_freq);
5081da177e4SLinus Torvalds define_one_ro(cpuinfo_min_freq);
5091da177e4SLinus Torvalds define_one_ro(cpuinfo_max_freq);
5101da177e4SLinus Torvalds define_one_ro(scaling_available_governors);
5111da177e4SLinus Torvalds define_one_ro(scaling_driver);
5121da177e4SLinus Torvalds define_one_ro(scaling_cur_freq);
5131da177e4SLinus Torvalds define_one_ro(affected_cpus);
5141da177e4SLinus Torvalds define_one_rw(scaling_min_freq);
5151da177e4SLinus Torvalds define_one_rw(scaling_max_freq);
5161da177e4SLinus Torvalds define_one_rw(scaling_governor);
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds static struct attribute * default_attrs[] = {
5191da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
5201da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
5211da177e4SLinus Torvalds 	&scaling_min_freq.attr,
5221da177e4SLinus Torvalds 	&scaling_max_freq.attr,
5231da177e4SLinus Torvalds 	&affected_cpus.attr,
5241da177e4SLinus Torvalds 	&scaling_governor.attr,
5251da177e4SLinus Torvalds 	&scaling_driver.attr,
5261da177e4SLinus Torvalds 	&scaling_available_governors.attr,
5271da177e4SLinus Torvalds 	NULL
5281da177e4SLinus Torvalds };
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds #define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
5311da177e4SLinus Torvalds #define to_attr(a) container_of(a,struct freq_attr,attr)
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
5341da177e4SLinus Torvalds {
5351da177e4SLinus Torvalds 	struct cpufreq_policy * policy = to_policy(kobj);
5361da177e4SLinus Torvalds 	struct freq_attr * fattr = to_attr(attr);
5371da177e4SLinus Torvalds 	ssize_t ret;
5381da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(policy->cpu);
5391da177e4SLinus Torvalds 	if (!policy)
5401da177e4SLinus Torvalds 		return -EINVAL;
54170f2817aSDmitry Torokhov 	ret = fattr->show ? fattr->show(policy,buf) : -EIO;
5421da177e4SLinus Torvalds 	cpufreq_cpu_put(policy);
5431da177e4SLinus Torvalds 	return ret;
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds static ssize_t store(struct kobject * kobj, struct attribute * attr,
5471da177e4SLinus Torvalds 		     const char * buf, size_t count)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	struct cpufreq_policy * policy = to_policy(kobj);
5501da177e4SLinus Torvalds 	struct freq_attr * fattr = to_attr(attr);
5511da177e4SLinus Torvalds 	ssize_t ret;
5521da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(policy->cpu);
5531da177e4SLinus Torvalds 	if (!policy)
5541da177e4SLinus Torvalds 		return -EINVAL;
55570f2817aSDmitry Torokhov 	ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
5561da177e4SLinus Torvalds 	cpufreq_cpu_put(policy);
5571da177e4SLinus Torvalds 	return ret;
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject * kobj)
5611da177e4SLinus Torvalds {
5621da177e4SLinus Torvalds 	struct cpufreq_policy * policy = to_policy(kobj);
5631da177e4SLinus Torvalds 	dprintk("last reference is dropped\n");
5641da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds static struct sysfs_ops sysfs_ops = {
5681da177e4SLinus Torvalds 	.show	= show,
5691da177e4SLinus Torvalds 	.store	= store,
5701da177e4SLinus Torvalds };
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
5731da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
5741da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
5751da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
5761da177e4SLinus Torvalds };
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds /**
5801da177e4SLinus Torvalds  * cpufreq_add_dev - add a CPU device
5811da177e4SLinus Torvalds  *
5821da177e4SLinus Torvalds  * Adds the cpufreq interface for a CPU device.
5831da177e4SLinus Torvalds  */
5841da177e4SLinus Torvalds static int cpufreq_add_dev (struct sys_device * sys_dev)
5851da177e4SLinus Torvalds {
5861da177e4SLinus Torvalds 	unsigned int cpu = sys_dev->id;
5871da177e4SLinus Torvalds 	int ret = 0;
5881da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
5891da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
5901da177e4SLinus Torvalds 	struct freq_attr **drv_attr;
5918ff69732SDave Jones 	struct sys_device *cpu_sys_dev;
5921da177e4SLinus Torvalds 	unsigned long flags;
5931da177e4SLinus Torvalds 	unsigned int j;
5948ff69732SDave Jones #ifdef CONFIG_SMP
5958ff69732SDave Jones 	struct cpufreq_policy *managed_policy;
5968ff69732SDave Jones #endif
5971da177e4SLinus Torvalds 
598c32b6b8eSAshok Raj 	if (cpu_is_offline(cpu))
599c32b6b8eSAshok Raj 		return 0;
600c32b6b8eSAshok Raj 
6011da177e4SLinus Torvalds 	cpufreq_debug_disable_ratelimit();
6021da177e4SLinus Torvalds 	dprintk("adding CPU %u\n", cpu);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds #ifdef CONFIG_SMP
6051da177e4SLinus Torvalds 	/* check whether a different CPU already registered this
6061da177e4SLinus Torvalds 	 * CPU because it is in the same boat. */
6071da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(cpu);
6081da177e4SLinus Torvalds 	if (unlikely(policy)) {
6098ff69732SDave Jones 		cpufreq_cpu_put(policy);
6101da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
6111da177e4SLinus Torvalds 		return 0;
6121da177e4SLinus Torvalds 	}
6131da177e4SLinus Torvalds #endif
6141da177e4SLinus Torvalds 
6151da177e4SLinus Torvalds 	if (!try_module_get(cpufreq_driver->owner)) {
6161da177e4SLinus Torvalds 		ret = -EINVAL;
6171da177e4SLinus Torvalds 		goto module_out;
6181da177e4SLinus Torvalds 	}
6191da177e4SLinus Torvalds 
620e98df50cSDave Jones 	policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
6211da177e4SLinus Torvalds 	if (!policy) {
6221da177e4SLinus Torvalds 		ret = -ENOMEM;
6231da177e4SLinus Torvalds 		goto nomem_out;
6241da177e4SLinus Torvalds 	}
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	policy->cpu = cpu;
6271da177e4SLinus Torvalds 	policy->cpus = cpumask_of_cpu(cpu);
6281da177e4SLinus Torvalds 
62983933af4SArjan van de Ven 	mutex_init(&policy->lock);
63083933af4SArjan van de Ven 	mutex_lock(&policy->lock);
6311da177e4SLinus Torvalds 	init_completion(&policy->kobj_unregister);
6321da177e4SLinus Torvalds 	INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
6351da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
6361da177e4SLinus Torvalds 	 */
6371da177e4SLinus Torvalds 	ret = cpufreq_driver->init(policy);
6381da177e4SLinus Torvalds 	if (ret) {
6391da177e4SLinus Torvalds 		dprintk("initialization failed\n");
640f3876c1bSAndrew Morton 		mutex_unlock(&policy->lock);
6411da177e4SLinus Torvalds 		goto err_out;
6421da177e4SLinus Torvalds 	}
6431da177e4SLinus Torvalds 
6448ff69732SDave Jones #ifdef CONFIG_SMP
6458ff69732SDave Jones 	for_each_cpu_mask(j, policy->cpus) {
6468ff69732SDave Jones 		if (cpu == j)
6478ff69732SDave Jones 			continue;
6488ff69732SDave Jones 
6498ff69732SDave Jones 		/* check for existing affected CPUs.  They may not be aware
6508ff69732SDave Jones 		 * of it due to CPU Hotplug.
6518ff69732SDave Jones 		 */
6528ff69732SDave Jones 		managed_policy = cpufreq_cpu_get(j);
6538ff69732SDave Jones 		if (unlikely(managed_policy)) {
6548ff69732SDave Jones 			spin_lock_irqsave(&cpufreq_driver_lock, flags);
6558ff69732SDave Jones 			managed_policy->cpus = policy->cpus;
6568ff69732SDave Jones 			cpufreq_cpu_data[cpu] = managed_policy;
6578ff69732SDave Jones 			spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
6588ff69732SDave Jones 
6598ff69732SDave Jones 			dprintk("CPU already managed, adding link\n");
6608ff69732SDave Jones 			sysfs_create_link(&sys_dev->kobj,
6618ff69732SDave Jones 					  &managed_policy->kobj, "cpufreq");
6628ff69732SDave Jones 
6638ff69732SDave Jones 			cpufreq_debug_enable_ratelimit();
6648ff69732SDave Jones 			mutex_unlock(&policy->lock);
6658ff69732SDave Jones 			ret = 0;
6668ff69732SDave Jones 			goto err_out_driver_exit; /* call driver->exit() */
6678ff69732SDave Jones 		}
6688ff69732SDave Jones 	}
6698ff69732SDave Jones #endif
6701da177e4SLinus Torvalds 	memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	/* prepare interface data */
6731da177e4SLinus Torvalds 	policy->kobj.parent = &sys_dev->kobj;
6741da177e4SLinus Torvalds 	policy->kobj.ktype = &ktype_cpufreq;
6751da177e4SLinus Torvalds 	strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds 	ret = kobject_register(&policy->kobj);
678f3876c1bSAndrew Morton 	if (ret) {
679f3876c1bSAndrew Morton 		mutex_unlock(&policy->lock);
6808085e1f1SVenkatesh Pallipadi 		goto err_out_driver_exit;
681f3876c1bSAndrew Morton 	}
6821da177e4SLinus Torvalds 	/* set up files for this cpu device */
6831da177e4SLinus Torvalds 	drv_attr = cpufreq_driver->attr;
6841da177e4SLinus Torvalds 	while ((drv_attr) && (*drv_attr)) {
6851da177e4SLinus Torvalds 		sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
6861da177e4SLinus Torvalds 		drv_attr++;
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds 	if (cpufreq_driver->get)
6891da177e4SLinus Torvalds 		sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
6901da177e4SLinus Torvalds 	if (cpufreq_driver->target)
6911da177e4SLinus Torvalds 		sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
6941da177e4SLinus Torvalds 	for_each_cpu_mask(j, policy->cpus)
6951da177e4SLinus Torvalds 		cpufreq_cpu_data[j] = policy;
6961da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
6978ff69732SDave Jones 
6988ff69732SDave Jones 	/* symlink affected CPUs */
6998ff69732SDave Jones 	for_each_cpu_mask(j, policy->cpus) {
7008ff69732SDave Jones 		if (j == cpu)
7018ff69732SDave Jones 			continue;
7028ff69732SDave Jones 		if (!cpu_online(j))
7038ff69732SDave Jones 			continue;
7048ff69732SDave Jones 
7051f8b2c9dSDave Jones 		dprintk("CPU %u already managed, adding link\n", j);
7068ff69732SDave Jones 		cpufreq_cpu_get(cpu);
7078ff69732SDave Jones 		cpu_sys_dev = get_cpu_sysdev(j);
7088ff69732SDave Jones 		sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
7098ff69732SDave Jones 				  "cpufreq");
7108ff69732SDave Jones 	}
7118ff69732SDave Jones 
7121da177e4SLinus Torvalds 	policy->governor = NULL; /* to assure that the starting sequence is
7131da177e4SLinus Torvalds 				  * run in cpufreq_set_policy */
71483933af4SArjan van de Ven 	mutex_unlock(&policy->lock);
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	/* set default policy */
7171da177e4SLinus Torvalds 	ret = cpufreq_set_policy(&new_policy);
7181da177e4SLinus Torvalds 	if (ret) {
7191da177e4SLinus Torvalds 		dprintk("setting policy failed\n");
7201da177e4SLinus Torvalds 		goto err_out_unregister;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	module_put(cpufreq_driver->owner);
7241da177e4SLinus Torvalds 	dprintk("initialization complete\n");
7251da177e4SLinus Torvalds 	cpufreq_debug_enable_ratelimit();
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	return 0;
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds err_out_unregister:
7311da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
7321da177e4SLinus Torvalds 	for_each_cpu_mask(j, policy->cpus)
7331da177e4SLinus Torvalds 		cpufreq_cpu_data[j] = NULL;
7341da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 	kobject_unregister(&policy->kobj);
7371da177e4SLinus Torvalds 	wait_for_completion(&policy->kobj_unregister);
7381da177e4SLinus Torvalds 
7398085e1f1SVenkatesh Pallipadi err_out_driver_exit:
7408085e1f1SVenkatesh Pallipadi 	if (cpufreq_driver->exit)
7418085e1f1SVenkatesh Pallipadi 		cpufreq_driver->exit(policy);
7428085e1f1SVenkatesh Pallipadi 
7431da177e4SLinus Torvalds err_out:
7441da177e4SLinus Torvalds 	kfree(policy);
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds nomem_out:
7471da177e4SLinus Torvalds 	module_put(cpufreq_driver->owner);
7481da177e4SLinus Torvalds module_out:
7491da177e4SLinus Torvalds 	cpufreq_debug_enable_ratelimit();
7501da177e4SLinus Torvalds 	return ret;
7511da177e4SLinus Torvalds }
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds /**
7551da177e4SLinus Torvalds  * cpufreq_remove_dev - remove a CPU device
7561da177e4SLinus Torvalds  *
7571da177e4SLinus Torvalds  * Removes the cpufreq interface for a CPU device.
7581da177e4SLinus Torvalds  */
7591da177e4SLinus Torvalds static int cpufreq_remove_dev (struct sys_device * sys_dev)
7601da177e4SLinus Torvalds {
7611da177e4SLinus Torvalds 	unsigned int cpu = sys_dev->id;
7621da177e4SLinus Torvalds 	unsigned long flags;
7631da177e4SLinus Torvalds 	struct cpufreq_policy *data;
7641da177e4SLinus Torvalds #ifdef CONFIG_SMP
765e738cf6dSGrant Coady 	struct sys_device *cpu_sys_dev;
7661da177e4SLinus Torvalds 	unsigned int j;
7671da177e4SLinus Torvalds #endif
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	cpufreq_debug_disable_ratelimit();
7701da177e4SLinus Torvalds 	dprintk("unregistering CPU %u\n", cpu);
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
7731da177e4SLinus Torvalds 	data = cpufreq_cpu_data[cpu];
7741da177e4SLinus Torvalds 
7751da177e4SLinus Torvalds 	if (!data) {
7761da177e4SLinus Torvalds 		spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
7771da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
7781da177e4SLinus Torvalds 		return -EINVAL;
7791da177e4SLinus Torvalds 	}
7801da177e4SLinus Torvalds 	cpufreq_cpu_data[cpu] = NULL;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 
7831da177e4SLinus Torvalds #ifdef CONFIG_SMP
7841da177e4SLinus Torvalds 	/* if this isn't the CPU which is the parent of the kobj, we
7851da177e4SLinus Torvalds 	 * only need to unlink, put and exit
7861da177e4SLinus Torvalds 	 */
7871da177e4SLinus Torvalds 	if (unlikely(cpu != data->cpu)) {
7881da177e4SLinus Torvalds 		dprintk("removing link\n");
7898ff69732SDave Jones 		cpu_clear(cpu, data->cpus);
7901da177e4SLinus Torvalds 		spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
7911da177e4SLinus Torvalds 		sysfs_remove_link(&sys_dev->kobj, "cpufreq");
7921da177e4SLinus Torvalds 		cpufreq_cpu_put(data);
7931da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
7941da177e4SLinus Torvalds 		return 0;
7951da177e4SLinus Torvalds 	}
7961da177e4SLinus Torvalds #endif
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	if (!kobject_get(&data->kobj)) {
8001da177e4SLinus Torvalds 		spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
8011da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
8021da177e4SLinus Torvalds 		return -EFAULT;
8031da177e4SLinus Torvalds 	}
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds #ifdef CONFIG_SMP
8061da177e4SLinus Torvalds 	/* if we have other CPUs still registered, we need to unlink them,
8071da177e4SLinus Torvalds 	 * or else wait_for_completion below will lock up. Clean the
8081da177e4SLinus Torvalds 	 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
8091da177e4SLinus Torvalds 	 * links afterwards.
8101da177e4SLinus Torvalds 	 */
8111da177e4SLinus Torvalds 	if (unlikely(cpus_weight(data->cpus) > 1)) {
8121da177e4SLinus Torvalds 		for_each_cpu_mask(j, data->cpus) {
8131da177e4SLinus Torvalds 			if (j == cpu)
8141da177e4SLinus Torvalds 				continue;
8151da177e4SLinus Torvalds 			cpufreq_cpu_data[j] = NULL;
8161da177e4SLinus Torvalds 		}
8171da177e4SLinus Torvalds 	}
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	if (unlikely(cpus_weight(data->cpus) > 1)) {
8221da177e4SLinus Torvalds 		for_each_cpu_mask(j, data->cpus) {
8231da177e4SLinus Torvalds 			if (j == cpu)
8241da177e4SLinus Torvalds 				continue;
8251da177e4SLinus Torvalds 			dprintk("removing link for cpu %u\n", j);
826d434fca7SAshok Raj 			cpu_sys_dev = get_cpu_sysdev(j);
827d434fca7SAshok Raj 			sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
8281da177e4SLinus Torvalds 			cpufreq_cpu_put(data);
8291da177e4SLinus Torvalds 		}
8301da177e4SLinus Torvalds 	}
8311da177e4SLinus Torvalds #else
8321da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
8331da177e4SLinus Torvalds #endif
8341da177e4SLinus Torvalds 
83583933af4SArjan van de Ven 	mutex_lock(&data->lock);
8361da177e4SLinus Torvalds 	if (cpufreq_driver->target)
8371da177e4SLinus Torvalds 		__cpufreq_governor(data, CPUFREQ_GOV_STOP);
83883933af4SArjan van de Ven 	mutex_unlock(&data->lock);
8391da177e4SLinus Torvalds 
8401da177e4SLinus Torvalds 	kobject_unregister(&data->kobj);
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 	kobject_put(&data->kobj);
8431da177e4SLinus Torvalds 
8441da177e4SLinus Torvalds 	/* we need to make sure that the underlying kobj is actually
8451da177e4SLinus Torvalds 	 * not referenced anymore by anybody before we proceed with
8461da177e4SLinus Torvalds 	 * unloading.
8471da177e4SLinus Torvalds 	 */
8481da177e4SLinus Torvalds 	dprintk("waiting for dropping of refcount\n");
8491da177e4SLinus Torvalds 	wait_for_completion(&data->kobj_unregister);
8501da177e4SLinus Torvalds 	dprintk("wait complete\n");
8511da177e4SLinus Torvalds 
8521da177e4SLinus Torvalds 	if (cpufreq_driver->exit)
8531da177e4SLinus Torvalds 		cpufreq_driver->exit(data);
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	kfree(data);
8561da177e4SLinus Torvalds 
8571da177e4SLinus Torvalds 	cpufreq_debug_enable_ratelimit();
8581da177e4SLinus Torvalds 	return 0;
8591da177e4SLinus Torvalds }
8601da177e4SLinus Torvalds 
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds static void handle_update(void *data)
8631da177e4SLinus Torvalds {
8641da177e4SLinus Torvalds 	unsigned int cpu = (unsigned int)(long)data;
8651da177e4SLinus Torvalds 	dprintk("handle_update for cpu %u called\n", cpu);
8661da177e4SLinus Torvalds 	cpufreq_update_policy(cpu);
8671da177e4SLinus Torvalds }
8681da177e4SLinus Torvalds 
8691da177e4SLinus Torvalds /**
8701da177e4SLinus Torvalds  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
8711da177e4SLinus Torvalds  *	@cpu: cpu number
8721da177e4SLinus Torvalds  *	@old_freq: CPU frequency the kernel thinks the CPU runs at
8731da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
8741da177e4SLinus Torvalds  *
8751da177e4SLinus Torvalds  *	We adjust to current frequency first, and need to clean up later. So either call
8761da177e4SLinus Torvalds  *	to cpufreq_update_policy() or schedule handle_update()).
8771da177e4SLinus Torvalds  */
8781da177e4SLinus Torvalds static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
8791da177e4SLinus Torvalds {
8801da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
8811da177e4SLinus Torvalds 
882b10eec22SJan Beulich 	dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
8831da177e4SLinus Torvalds 	       "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	freqs.cpu = cpu;
8861da177e4SLinus Torvalds 	freqs.old = old_freq;
8871da177e4SLinus Torvalds 	freqs.new = new_freq;
8881da177e4SLinus Torvalds 	cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
8891da177e4SLinus Torvalds 	cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
8901da177e4SLinus Torvalds }
8911da177e4SLinus Torvalds 
8921da177e4SLinus Torvalds 
8931da177e4SLinus Torvalds /**
89495235ca2SVenkatesh Pallipadi  * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
89595235ca2SVenkatesh Pallipadi  * @cpu: CPU number
89695235ca2SVenkatesh Pallipadi  *
89795235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
89895235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
89995235ca2SVenkatesh Pallipadi  */
90095235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
90195235ca2SVenkatesh Pallipadi {
90295235ca2SVenkatesh Pallipadi 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
90395235ca2SVenkatesh Pallipadi 	unsigned int ret = 0;
90495235ca2SVenkatesh Pallipadi 
90595235ca2SVenkatesh Pallipadi 	if (policy) {
90683933af4SArjan van de Ven 		mutex_lock(&policy->lock);
90795235ca2SVenkatesh Pallipadi 		ret = policy->cur;
90883933af4SArjan van de Ven 		mutex_unlock(&policy->lock);
90995235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
91095235ca2SVenkatesh Pallipadi 	}
91195235ca2SVenkatesh Pallipadi 
91295235ca2SVenkatesh Pallipadi 	return (ret);
91395235ca2SVenkatesh Pallipadi }
91495235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
91595235ca2SVenkatesh Pallipadi 
91695235ca2SVenkatesh Pallipadi 
91795235ca2SVenkatesh Pallipadi /**
9181da177e4SLinus Torvalds  * cpufreq_get - get the current CPU frequency (in kHz)
9191da177e4SLinus Torvalds  * @cpu: CPU number
9201da177e4SLinus Torvalds  *
9211da177e4SLinus Torvalds  * Get the CPU current (static) CPU frequency
9221da177e4SLinus Torvalds  */
9231da177e4SLinus Torvalds unsigned int cpufreq_get(unsigned int cpu)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
9261da177e4SLinus Torvalds 	unsigned int ret = 0;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	if (!policy)
9291da177e4SLinus Torvalds 		return 0;
9301da177e4SLinus Torvalds 
9311da177e4SLinus Torvalds 	if (!cpufreq_driver->get)
9321da177e4SLinus Torvalds 		goto out;
9331da177e4SLinus Torvalds 
93483933af4SArjan van de Ven 	mutex_lock(&policy->lock);
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds 	ret = cpufreq_driver->get(cpu);
9371da177e4SLinus Torvalds 
9387d5e350fSDave Jones 	if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
9391da177e4SLinus Torvalds 		/* verify no discrepancy between actual and saved value exists */
9401da177e4SLinus Torvalds 		if (unlikely(ret != policy->cur)) {
9411da177e4SLinus Torvalds 			cpufreq_out_of_sync(cpu, policy->cur, ret);
9421da177e4SLinus Torvalds 			schedule_work(&policy->update);
9431da177e4SLinus Torvalds 		}
9441da177e4SLinus Torvalds 	}
9451da177e4SLinus Torvalds 
94683933af4SArjan van de Ven 	mutex_unlock(&policy->lock);
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds out:
9491da177e4SLinus Torvalds 	cpufreq_cpu_put(policy);
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds 	return (ret);
9521da177e4SLinus Torvalds }
9531da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds /**
95742d4dc3fSBenjamin Herrenschmidt  *	cpufreq_suspend - let the low level driver prepare for suspend
95842d4dc3fSBenjamin Herrenschmidt  */
95942d4dc3fSBenjamin Herrenschmidt 
960e00d9967SBernard Blackham static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
96142d4dc3fSBenjamin Herrenschmidt {
96242d4dc3fSBenjamin Herrenschmidt 	int cpu = sysdev->id;
96342d4dc3fSBenjamin Herrenschmidt 	unsigned int ret = 0;
96442d4dc3fSBenjamin Herrenschmidt 	unsigned int cur_freq = 0;
96542d4dc3fSBenjamin Herrenschmidt 	struct cpufreq_policy *cpu_policy;
96642d4dc3fSBenjamin Herrenschmidt 
96742d4dc3fSBenjamin Herrenschmidt 	dprintk("resuming cpu %u\n", cpu);
96842d4dc3fSBenjamin Herrenschmidt 
96942d4dc3fSBenjamin Herrenschmidt 	if (!cpu_online(cpu))
97042d4dc3fSBenjamin Herrenschmidt 		return 0;
97142d4dc3fSBenjamin Herrenschmidt 
97242d4dc3fSBenjamin Herrenschmidt 	/* we may be lax here as interrupts are off. Nonetheless
97342d4dc3fSBenjamin Herrenschmidt 	 * we need to grab the correct cpu policy, as to check
97442d4dc3fSBenjamin Herrenschmidt 	 * whether we really run on this CPU.
97542d4dc3fSBenjamin Herrenschmidt 	 */
97642d4dc3fSBenjamin Herrenschmidt 
97742d4dc3fSBenjamin Herrenschmidt 	cpu_policy = cpufreq_cpu_get(cpu);
97842d4dc3fSBenjamin Herrenschmidt 	if (!cpu_policy)
97942d4dc3fSBenjamin Herrenschmidt 		return -EINVAL;
98042d4dc3fSBenjamin Herrenschmidt 
98142d4dc3fSBenjamin Herrenschmidt 	/* only handle each CPU group once */
98242d4dc3fSBenjamin Herrenschmidt 	if (unlikely(cpu_policy->cpu != cpu)) {
98342d4dc3fSBenjamin Herrenschmidt 		cpufreq_cpu_put(cpu_policy);
98442d4dc3fSBenjamin Herrenschmidt 		return 0;
98542d4dc3fSBenjamin Herrenschmidt 	}
98642d4dc3fSBenjamin Herrenschmidt 
98742d4dc3fSBenjamin Herrenschmidt 	if (cpufreq_driver->suspend) {
988e00d9967SBernard Blackham 		ret = cpufreq_driver->suspend(cpu_policy, pmsg);
98942d4dc3fSBenjamin Herrenschmidt 		if (ret) {
99042d4dc3fSBenjamin Herrenschmidt 			printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
99142d4dc3fSBenjamin Herrenschmidt 					"step on CPU %u\n", cpu_policy->cpu);
99242d4dc3fSBenjamin Herrenschmidt 			cpufreq_cpu_put(cpu_policy);
99342d4dc3fSBenjamin Herrenschmidt 			return ret;
99442d4dc3fSBenjamin Herrenschmidt 		}
99542d4dc3fSBenjamin Herrenschmidt 	}
99642d4dc3fSBenjamin Herrenschmidt 
99742d4dc3fSBenjamin Herrenschmidt 
99842d4dc3fSBenjamin Herrenschmidt 	if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
99942d4dc3fSBenjamin Herrenschmidt 		goto out;
100042d4dc3fSBenjamin Herrenschmidt 
100142d4dc3fSBenjamin Herrenschmidt 	if (cpufreq_driver->get)
100242d4dc3fSBenjamin Herrenschmidt 		cur_freq = cpufreq_driver->get(cpu_policy->cpu);
100342d4dc3fSBenjamin Herrenschmidt 
100442d4dc3fSBenjamin Herrenschmidt 	if (!cur_freq || !cpu_policy->cur) {
100542d4dc3fSBenjamin Herrenschmidt 		printk(KERN_ERR "cpufreq: suspend failed to assert current "
100642d4dc3fSBenjamin Herrenschmidt 		       "frequency is what timing core thinks it is.\n");
100742d4dc3fSBenjamin Herrenschmidt 		goto out;
100842d4dc3fSBenjamin Herrenschmidt 	}
100942d4dc3fSBenjamin Herrenschmidt 
101042d4dc3fSBenjamin Herrenschmidt 	if (unlikely(cur_freq != cpu_policy->cur)) {
101142d4dc3fSBenjamin Herrenschmidt 		struct cpufreq_freqs freqs;
101242d4dc3fSBenjamin Herrenschmidt 
101342d4dc3fSBenjamin Herrenschmidt 		if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1014b10eec22SJan Beulich 			dprintk("Warning: CPU frequency is %u, "
101542d4dc3fSBenjamin Herrenschmidt 			       "cpufreq assumed %u kHz.\n",
101642d4dc3fSBenjamin Herrenschmidt 			       cur_freq, cpu_policy->cur);
101742d4dc3fSBenjamin Herrenschmidt 
101842d4dc3fSBenjamin Herrenschmidt 		freqs.cpu = cpu;
101942d4dc3fSBenjamin Herrenschmidt 		freqs.old = cpu_policy->cur;
102042d4dc3fSBenjamin Herrenschmidt 		freqs.new = cur_freq;
102142d4dc3fSBenjamin Herrenschmidt 
1022e041c683SAlan Stern 		blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
102342d4dc3fSBenjamin Herrenschmidt 				    CPUFREQ_SUSPENDCHANGE, &freqs);
102442d4dc3fSBenjamin Herrenschmidt 		adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
102542d4dc3fSBenjamin Herrenschmidt 
102642d4dc3fSBenjamin Herrenschmidt 		cpu_policy->cur = cur_freq;
102742d4dc3fSBenjamin Herrenschmidt 	}
102842d4dc3fSBenjamin Herrenschmidt 
102942d4dc3fSBenjamin Herrenschmidt out:
103042d4dc3fSBenjamin Herrenschmidt 	cpufreq_cpu_put(cpu_policy);
103142d4dc3fSBenjamin Herrenschmidt 	return 0;
103242d4dc3fSBenjamin Herrenschmidt }
103342d4dc3fSBenjamin Herrenschmidt 
103442d4dc3fSBenjamin Herrenschmidt /**
10351da177e4SLinus Torvalds  *	cpufreq_resume -  restore proper CPU frequency handling after resume
10361da177e4SLinus Torvalds  *
10371da177e4SLinus Torvalds  *	1.) resume CPUfreq hardware support (cpufreq_driver->resume())
10381da177e4SLinus Torvalds  *	2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
103942d4dc3fSBenjamin Herrenschmidt  *	3.) schedule call cpufreq_update_policy() ASAP as interrupts are
104042d4dc3fSBenjamin Herrenschmidt  *	    restored.
10411da177e4SLinus Torvalds  */
10421da177e4SLinus Torvalds static int cpufreq_resume(struct sys_device * sysdev)
10431da177e4SLinus Torvalds {
10441da177e4SLinus Torvalds 	int cpu = sysdev->id;
10451da177e4SLinus Torvalds 	unsigned int ret = 0;
10461da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
10471da177e4SLinus Torvalds 
10481da177e4SLinus Torvalds 	dprintk("resuming cpu %u\n", cpu);
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	if (!cpu_online(cpu))
10511da177e4SLinus Torvalds 		return 0;
10521da177e4SLinus Torvalds 
10531da177e4SLinus Torvalds 	/* we may be lax here as interrupts are off. Nonetheless
10541da177e4SLinus Torvalds 	 * we need to grab the correct cpu policy, as to check
10551da177e4SLinus Torvalds 	 * whether we really run on this CPU.
10561da177e4SLinus Torvalds 	 */
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
10591da177e4SLinus Torvalds 	if (!cpu_policy)
10601da177e4SLinus Torvalds 		return -EINVAL;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	/* only handle each CPU group once */
10631da177e4SLinus Torvalds 	if (unlikely(cpu_policy->cpu != cpu)) {
10641da177e4SLinus Torvalds 		cpufreq_cpu_put(cpu_policy);
10651da177e4SLinus Torvalds 		return 0;
10661da177e4SLinus Torvalds 	}
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	if (cpufreq_driver->resume) {
10691da177e4SLinus Torvalds 		ret = cpufreq_driver->resume(cpu_policy);
10701da177e4SLinus Torvalds 		if (ret) {
10711da177e4SLinus Torvalds 			printk(KERN_ERR "cpufreq: resume failed in ->resume "
10721da177e4SLinus Torvalds 					"step on CPU %u\n", cpu_policy->cpu);
10731da177e4SLinus Torvalds 			cpufreq_cpu_put(cpu_policy);
10741da177e4SLinus Torvalds 			return ret;
10751da177e4SLinus Torvalds 		}
10761da177e4SLinus Torvalds 	}
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds 	if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
10791da177e4SLinus Torvalds 		unsigned int cur_freq = 0;
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 		if (cpufreq_driver->get)
10821da177e4SLinus Torvalds 			cur_freq = cpufreq_driver->get(cpu_policy->cpu);
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 		if (!cur_freq || !cpu_policy->cur) {
108542d4dc3fSBenjamin Herrenschmidt 			printk(KERN_ERR "cpufreq: resume failed to assert "
108642d4dc3fSBenjamin Herrenschmidt 					"current frequency is what timing core "
108742d4dc3fSBenjamin Herrenschmidt 					"thinks it is.\n");
10881da177e4SLinus Torvalds 			goto out;
10891da177e4SLinus Torvalds 		}
10901da177e4SLinus Torvalds 
10911da177e4SLinus Torvalds 		if (unlikely(cur_freq != cpu_policy->cur)) {
10921da177e4SLinus Torvalds 			struct cpufreq_freqs freqs;
10931da177e4SLinus Torvalds 
1094ac09f698SBenjamin Herrenschmidt 			if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1095b10eec22SJan Beulich 				dprintk("Warning: CPU frequency"
1096ac09f698SBenjamin Herrenschmidt 				       "is %u, cpufreq assumed %u kHz.\n",
109742d4dc3fSBenjamin Herrenschmidt 				       cur_freq, cpu_policy->cur);
10981da177e4SLinus Torvalds 
10991da177e4SLinus Torvalds 			freqs.cpu = cpu;
11001da177e4SLinus Torvalds 			freqs.old = cpu_policy->cur;
11011da177e4SLinus Torvalds 			freqs.new = cur_freq;
11021da177e4SLinus Torvalds 
1103e041c683SAlan Stern 			blocking_notifier_call_chain(
1104e041c683SAlan Stern 					&cpufreq_transition_notifier_list,
110542d4dc3fSBenjamin Herrenschmidt 					CPUFREQ_RESUMECHANGE, &freqs);
11061da177e4SLinus Torvalds 			adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds 			cpu_policy->cur = cur_freq;
11091da177e4SLinus Torvalds 		}
11101da177e4SLinus Torvalds 	}
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds out:
11131da177e4SLinus Torvalds 	schedule_work(&cpu_policy->update);
11141da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
11151da177e4SLinus Torvalds 	return ret;
11161da177e4SLinus Torvalds }
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds static struct sysdev_driver cpufreq_sysdev_driver = {
11191da177e4SLinus Torvalds 	.add		= cpufreq_add_dev,
11201da177e4SLinus Torvalds 	.remove		= cpufreq_remove_dev,
112142d4dc3fSBenjamin Herrenschmidt 	.suspend	= cpufreq_suspend,
11221da177e4SLinus Torvalds 	.resume		= cpufreq_resume,
11231da177e4SLinus Torvalds };
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds /*********************************************************************
11271da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
11281da177e4SLinus Torvalds  *********************************************************************/
11291da177e4SLinus Torvalds 
11301da177e4SLinus Torvalds /**
11311da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
11321da177e4SLinus Torvalds  *	@nb: notifier function to register
11331da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
11341da177e4SLinus Torvalds  *
11351da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
11361da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
11371da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
11381da177e4SLinus Torvalds  *      changes in cpufreq policy.
11391da177e4SLinus Torvalds  *
11401da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1141e041c683SAlan Stern  *	blocking_notifier_chain_register.
11421da177e4SLinus Torvalds  */
11431da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
11441da177e4SLinus Torvalds {
11451da177e4SLinus Torvalds 	int ret;
11461da177e4SLinus Torvalds 
11471da177e4SLinus Torvalds 	switch (list) {
11481da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1149e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1150e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
11511da177e4SLinus Torvalds 		break;
11521da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1153e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1154e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
11551da177e4SLinus Torvalds 		break;
11561da177e4SLinus Torvalds 	default:
11571da177e4SLinus Torvalds 		ret = -EINVAL;
11581da177e4SLinus Torvalds 	}
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	return ret;
11611da177e4SLinus Torvalds }
11621da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds /**
11661da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
11671da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
11681da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
11691da177e4SLinus Torvalds  *
11701da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
11711da177e4SLinus Torvalds  *
11721da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1173e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
11741da177e4SLinus Torvalds  */
11751da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
11761da177e4SLinus Torvalds {
11771da177e4SLinus Torvalds 	int ret;
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	switch (list) {
11801da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1181e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1182e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
11831da177e4SLinus Torvalds 		break;
11841da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1185e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1186e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
11871da177e4SLinus Torvalds 		break;
11881da177e4SLinus Torvalds 	default:
11891da177e4SLinus Torvalds 		ret = -EINVAL;
11901da177e4SLinus Torvalds 	}
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	return ret;
11931da177e4SLinus Torvalds }
11941da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds /*********************************************************************
11981da177e4SLinus Torvalds  *                              GOVERNORS                            *
11991da177e4SLinus Torvalds  *********************************************************************/
12001da177e4SLinus Torvalds 
12011da177e4SLinus Torvalds 
1202153d7f3fSArjan van de Ven /* Must be called with lock_cpu_hotplug held */
12031da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
12041da177e4SLinus Torvalds 			    unsigned int target_freq,
12051da177e4SLinus Torvalds 			    unsigned int relation)
12061da177e4SLinus Torvalds {
12071da177e4SLinus Torvalds 	int retval = -EINVAL;
1208c32b6b8eSAshok Raj 
12091da177e4SLinus Torvalds 	dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
12101da177e4SLinus Torvalds 		target_freq, relation);
12111da177e4SLinus Torvalds 	if (cpu_online(policy->cpu) && cpufreq_driver->target)
12121da177e4SLinus Torvalds 		retval = cpufreq_driver->target(policy, target_freq, relation);
121390d45d17SAshok Raj 
12141da177e4SLinus Torvalds 	return retval;
12151da177e4SLinus Torvalds }
12161da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
12191da177e4SLinus Torvalds 			  unsigned int target_freq,
12201da177e4SLinus Torvalds 			  unsigned int relation)
12211da177e4SLinus Torvalds {
1222cc993cabSDave Jones 	int ret;
12231da177e4SLinus Torvalds 
12241da177e4SLinus Torvalds 	policy = cpufreq_cpu_get(policy->cpu);
12251da177e4SLinus Torvalds 	if (!policy)
12261da177e4SLinus Torvalds 		return -EINVAL;
12271da177e4SLinus Torvalds 
1228153d7f3fSArjan van de Ven 	lock_cpu_hotplug();
122983933af4SArjan van de Ven 	mutex_lock(&policy->lock);
12301da177e4SLinus Torvalds 
12311da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
12321da177e4SLinus Torvalds 
123383933af4SArjan van de Ven 	mutex_unlock(&policy->lock);
1234153d7f3fSArjan van de Ven 	unlock_cpu_hotplug();
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	cpufreq_cpu_put(policy);
12371da177e4SLinus Torvalds 	return ret;
12381da177e4SLinus Torvalds }
12391da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
12401da177e4SLinus Torvalds 
1241153d7f3fSArjan van de Ven /*
1242153d7f3fSArjan van de Ven  * Locking: Must be called with the lock_cpu_hotplug() lock held
1243153d7f3fSArjan van de Ven  * when "event" is CPUFREQ_GOV_LIMITS
1244153d7f3fSArjan van de Ven  */
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
12471da177e4SLinus Torvalds {
1248cc993cabSDave Jones 	int ret;
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 	if (!try_module_get(policy->governor->owner))
12511da177e4SLinus Torvalds 		return -EINVAL;
12521da177e4SLinus Torvalds 
12531da177e4SLinus Torvalds 	dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
12541da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	/* we keep one module reference alive for each CPU governed by this CPU */
12571da177e4SLinus Torvalds 	if ((event != CPUFREQ_GOV_START) || ret)
12581da177e4SLinus Torvalds 		module_put(policy->governor->owner);
12591da177e4SLinus Torvalds 	if ((event == CPUFREQ_GOV_STOP) && !ret)
12601da177e4SLinus Torvalds 		module_put(policy->governor->owner);
12611da177e4SLinus Torvalds 
12621da177e4SLinus Torvalds 	return ret;
12631da177e4SLinus Torvalds }
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
12671da177e4SLinus Torvalds {
12681da177e4SLinus Torvalds 	struct cpufreq_governor *t;
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	if (!governor)
12711da177e4SLinus Torvalds 		return -EINVAL;
12721da177e4SLinus Torvalds 
12733fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
12741da177e4SLinus Torvalds 
12751da177e4SLinus Torvalds 	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
12761da177e4SLinus Torvalds 		if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
12773fc54d37Sakpm@osdl.org 			mutex_unlock(&cpufreq_governor_mutex);
12781da177e4SLinus Torvalds 			return -EBUSY;
12791da177e4SLinus Torvalds 		}
12801da177e4SLinus Torvalds 	}
12811da177e4SLinus Torvalds 	list_add(&governor->governor_list, &cpufreq_governor_list);
12821da177e4SLinus Torvalds 
12833fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
12841da177e4SLinus Torvalds 	return 0;
12851da177e4SLinus Torvalds }
12861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
12901da177e4SLinus Torvalds {
12911da177e4SLinus Torvalds 	if (!governor)
12921da177e4SLinus Torvalds 		return;
12931da177e4SLinus Torvalds 
12943fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
12951da177e4SLinus Torvalds 	list_del(&governor->governor_list);
12963fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
12971da177e4SLinus Torvalds 	return;
12981da177e4SLinus Torvalds }
12991da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
13001da177e4SLinus Torvalds 
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 
13031da177e4SLinus Torvalds /*********************************************************************
13041da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
13051da177e4SLinus Torvalds  *********************************************************************/
13061da177e4SLinus Torvalds 
13071da177e4SLinus Torvalds /**
13081da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
13091da177e4SLinus Torvalds  * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
13101da177e4SLinus Torvalds  *
13111da177e4SLinus Torvalds  * Reads the current cpufreq policy.
13121da177e4SLinus Torvalds  */
13131da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
13141da177e4SLinus Torvalds {
13151da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
13161da177e4SLinus Torvalds 	if (!policy)
13171da177e4SLinus Torvalds 		return -EINVAL;
13181da177e4SLinus Torvalds 
13191da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
13201da177e4SLinus Torvalds 	if (!cpu_policy)
13211da177e4SLinus Torvalds 		return -EINVAL;
13221da177e4SLinus Torvalds 
132383933af4SArjan van de Ven 	mutex_lock(&cpu_policy->lock);
13241da177e4SLinus Torvalds 	memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
132583933af4SArjan van de Ven 	mutex_unlock(&cpu_policy->lock);
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
13281da177e4SLinus Torvalds 	return 0;
13291da177e4SLinus Torvalds }
13301da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
13311da177e4SLinus Torvalds 
13321da177e4SLinus Torvalds 
1333153d7f3fSArjan van de Ven /*
1334153d7f3fSArjan van de Ven  * Locking: Must be called with the lock_cpu_hotplug() lock held
1335153d7f3fSArjan van de Ven  */
13361da177e4SLinus Torvalds static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
13371da177e4SLinus Torvalds {
13381da177e4SLinus Torvalds 	int ret = 0;
13391da177e4SLinus Torvalds 
13401da177e4SLinus Torvalds 	cpufreq_debug_disable_ratelimit();
13411da177e4SLinus Torvalds 	dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
13421da177e4SLinus Torvalds 		policy->min, policy->max);
13431da177e4SLinus Torvalds 
13447d5e350fSDave Jones 	memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
13451da177e4SLinus Torvalds 
1346*9c9a43edSMattia Dongili 	if (policy->min > data->min && policy->min > policy->max) {
1347*9c9a43edSMattia Dongili 		ret = -EINVAL;
1348*9c9a43edSMattia Dongili 		goto error_out;
1349*9c9a43edSMattia Dongili 	}
1350*9c9a43edSMattia Dongili 
13511da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
13521da177e4SLinus Torvalds 	ret = cpufreq_driver->verify(policy);
13531da177e4SLinus Torvalds 	if (ret)
13541da177e4SLinus Torvalds 		goto error_out;
13551da177e4SLinus Torvalds 
13561da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
1357e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1358e041c683SAlan Stern 			CPUFREQ_ADJUST, policy);
13591da177e4SLinus Torvalds 
13601da177e4SLinus Torvalds 	/* adjust if necessary - hardware incompatibility*/
1361e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1362e041c683SAlan Stern 			CPUFREQ_INCOMPATIBLE, policy);
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit,
13651da177e4SLinus Torvalds 	   which might be different to the first one */
13661da177e4SLinus Torvalds 	ret = cpufreq_driver->verify(policy);
1367e041c683SAlan Stern 	if (ret)
13681da177e4SLinus Torvalds 		goto error_out;
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 	/* notification of the new policy */
1371e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1372e041c683SAlan Stern 			CPUFREQ_NOTIFY, policy);
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	data->min = policy->min;
13751da177e4SLinus Torvalds 	data->max = policy->max;
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	if (cpufreq_driver->setpolicy) {
13801da177e4SLinus Torvalds 		data->policy = policy->policy;
13811da177e4SLinus Torvalds 		dprintk("setting range\n");
13821da177e4SLinus Torvalds 		ret = cpufreq_driver->setpolicy(policy);
13831da177e4SLinus Torvalds 	} else {
13841da177e4SLinus Torvalds 		if (policy->governor != data->governor) {
13851da177e4SLinus Torvalds 			/* save old, working values */
13861da177e4SLinus Torvalds 			struct cpufreq_governor *old_gov = data->governor;
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds 			dprintk("governor switch\n");
13891da177e4SLinus Torvalds 
13901da177e4SLinus Torvalds 			/* end old governor */
13911da177e4SLinus Torvalds 			if (data->governor)
13921da177e4SLinus Torvalds 				__cpufreq_governor(data, CPUFREQ_GOV_STOP);
13931da177e4SLinus Torvalds 
13941da177e4SLinus Torvalds 			/* start new governor */
13951da177e4SLinus Torvalds 			data->governor = policy->governor;
13961da177e4SLinus Torvalds 			if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
13971da177e4SLinus Torvalds 				/* new governor failed, so re-start old one */
13981da177e4SLinus Torvalds 				dprintk("starting governor %s failed\n", data->governor->name);
13991da177e4SLinus Torvalds 				if (old_gov) {
14001da177e4SLinus Torvalds 					data->governor = old_gov;
14011da177e4SLinus Torvalds 					__cpufreq_governor(data, CPUFREQ_GOV_START);
14021da177e4SLinus Torvalds 				}
14031da177e4SLinus Torvalds 				ret = -EINVAL;
14041da177e4SLinus Torvalds 				goto error_out;
14051da177e4SLinus Torvalds 			}
14061da177e4SLinus Torvalds 			/* might be a policy change, too, so fall through */
14071da177e4SLinus Torvalds 		}
14081da177e4SLinus Torvalds 		dprintk("governor: change or update limits\n");
14091da177e4SLinus Torvalds 		__cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
14101da177e4SLinus Torvalds 	}
14111da177e4SLinus Torvalds 
14121da177e4SLinus Torvalds error_out:
14131da177e4SLinus Torvalds 	cpufreq_debug_enable_ratelimit();
14141da177e4SLinus Torvalds 	return ret;
14151da177e4SLinus Torvalds }
14161da177e4SLinus Torvalds 
14171da177e4SLinus Torvalds /**
14181da177e4SLinus Torvalds  *	cpufreq_set_policy - set a new CPUFreq policy
14191da177e4SLinus Torvalds  *	@policy: policy to be set.
14201da177e4SLinus Torvalds  *
14211da177e4SLinus Torvalds  *	Sets a new CPU frequency and voltage scaling policy.
14221da177e4SLinus Torvalds  */
14231da177e4SLinus Torvalds int cpufreq_set_policy(struct cpufreq_policy *policy)
14241da177e4SLinus Torvalds {
14251da177e4SLinus Torvalds 	int ret = 0;
14261da177e4SLinus Torvalds 	struct cpufreq_policy *data;
14271da177e4SLinus Torvalds 
14281da177e4SLinus Torvalds 	if (!policy)
14291da177e4SLinus Torvalds 		return -EINVAL;
14301da177e4SLinus Torvalds 
14311da177e4SLinus Torvalds 	data = cpufreq_cpu_get(policy->cpu);
14321da177e4SLinus Torvalds 	if (!data)
14331da177e4SLinus Torvalds 		return -EINVAL;
14341da177e4SLinus Torvalds 
1435153d7f3fSArjan van de Ven 	lock_cpu_hotplug();
1436153d7f3fSArjan van de Ven 
14371da177e4SLinus Torvalds 	/* lock this CPU */
143883933af4SArjan van de Ven 	mutex_lock(&data->lock);
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	ret = __cpufreq_set_policy(data, policy);
14411da177e4SLinus Torvalds 	data->user_policy.min = data->min;
14421da177e4SLinus Torvalds 	data->user_policy.max = data->max;
14431da177e4SLinus Torvalds 	data->user_policy.policy = data->policy;
14441da177e4SLinus Torvalds 	data->user_policy.governor = data->governor;
14451da177e4SLinus Torvalds 
144683933af4SArjan van de Ven 	mutex_unlock(&data->lock);
1447153d7f3fSArjan van de Ven 
1448153d7f3fSArjan van de Ven 	unlock_cpu_hotplug();
14491da177e4SLinus Torvalds 	cpufreq_cpu_put(data);
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 	return ret;
14521da177e4SLinus Torvalds }
14531da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_set_policy);
14541da177e4SLinus Torvalds 
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds /**
14571da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
14581da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
14591da177e4SLinus Torvalds  *
14601da177e4SLinus Torvalds  *	Usefull for policy notifiers which have different necessities
14611da177e4SLinus Torvalds  *	at different times.
14621da177e4SLinus Torvalds  */
14631da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
14641da177e4SLinus Torvalds {
14651da177e4SLinus Torvalds 	struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
14661da177e4SLinus Torvalds 	struct cpufreq_policy policy;
14671da177e4SLinus Torvalds 	int ret = 0;
14681da177e4SLinus Torvalds 
14691da177e4SLinus Torvalds 	if (!data)
14701da177e4SLinus Torvalds 		return -ENODEV;
14711da177e4SLinus Torvalds 
1472153d7f3fSArjan van de Ven 	lock_cpu_hotplug();
147383933af4SArjan van de Ven 	mutex_lock(&data->lock);
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 	dprintk("updating policy for CPU %u\n", cpu);
14767d5e350fSDave Jones 	memcpy(&policy, data, sizeof(struct cpufreq_policy));
14771da177e4SLinus Torvalds 	policy.min = data->user_policy.min;
14781da177e4SLinus Torvalds 	policy.max = data->user_policy.max;
14791da177e4SLinus Torvalds 	policy.policy = data->user_policy.policy;
14801da177e4SLinus Torvalds 	policy.governor = data->user_policy.governor;
14811da177e4SLinus Torvalds 
14820961dd0dSThomas Renninger 	/* BIOS might change freq behind our back
14830961dd0dSThomas Renninger 	  -> ask driver for current freq and notify governors about a change */
14840961dd0dSThomas Renninger 	if (cpufreq_driver->get) {
14850961dd0dSThomas Renninger 		policy.cur = cpufreq_driver->get(cpu);
1486a85f7bd3SThomas Renninger 		if (!data->cur) {
1487a85f7bd3SThomas Renninger 			dprintk("Driver did not initialize current freq");
1488a85f7bd3SThomas Renninger 			data->cur = policy.cur;
1489a85f7bd3SThomas Renninger 		} else {
14900961dd0dSThomas Renninger 			if (data->cur != policy.cur)
14910961dd0dSThomas Renninger 				cpufreq_out_of_sync(cpu, data->cur, policy.cur);
14920961dd0dSThomas Renninger 		}
1493a85f7bd3SThomas Renninger 	}
14940961dd0dSThomas Renninger 
14951da177e4SLinus Torvalds 	ret = __cpufreq_set_policy(data, &policy);
14961da177e4SLinus Torvalds 
149783933af4SArjan van de Ven 	mutex_unlock(&data->lock);
1498153d7f3fSArjan van de Ven 	unlock_cpu_hotplug();
14991da177e4SLinus Torvalds 	cpufreq_cpu_put(data);
15001da177e4SLinus Torvalds 	return ret;
15011da177e4SLinus Torvalds }
15021da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
15031da177e4SLinus Torvalds 
150465edc68cSChandra Seetharaman #ifdef CONFIG_HOTPLUG_CPU
150565edc68cSChandra Seetharaman static int cpufreq_cpu_callback(struct notifier_block *nfb,
1506c32b6b8eSAshok Raj 					unsigned long action, void *hcpu)
1507c32b6b8eSAshok Raj {
1508c32b6b8eSAshok Raj 	unsigned int cpu = (unsigned long)hcpu;
1509c32b6b8eSAshok Raj 	struct cpufreq_policy *policy;
1510c32b6b8eSAshok Raj 	struct sys_device *sys_dev;
1511c32b6b8eSAshok Raj 
1512c32b6b8eSAshok Raj 	sys_dev = get_cpu_sysdev(cpu);
1513c32b6b8eSAshok Raj 
1514c32b6b8eSAshok Raj 	if (sys_dev) {
1515c32b6b8eSAshok Raj 		switch (action) {
1516c32b6b8eSAshok Raj 		case CPU_ONLINE:
1517c32b6b8eSAshok Raj 			cpufreq_add_dev(sys_dev);
1518c32b6b8eSAshok Raj 			break;
1519c32b6b8eSAshok Raj 		case CPU_DOWN_PREPARE:
1520c32b6b8eSAshok Raj 			/*
1521c32b6b8eSAshok Raj 			 * We attempt to put this cpu in lowest frequency
1522c32b6b8eSAshok Raj 			 * possible before going down. This will permit
1523c32b6b8eSAshok Raj 			 * hardware-managed P-State to switch other related
1524c32b6b8eSAshok Raj 			 * threads to min or higher speeds if possible.
1525c32b6b8eSAshok Raj 			 */
1526c32b6b8eSAshok Raj 			policy = cpufreq_cpu_data[cpu];
1527c32b6b8eSAshok Raj 			if (policy) {
1528c32b6b8eSAshok Raj 				cpufreq_driver_target(policy, policy->min,
1529c32b6b8eSAshok Raj 						CPUFREQ_RELATION_H);
1530c32b6b8eSAshok Raj 			}
1531c32b6b8eSAshok Raj 			break;
1532c32b6b8eSAshok Raj 		case CPU_DEAD:
1533c32b6b8eSAshok Raj 			cpufreq_remove_dev(sys_dev);
1534c32b6b8eSAshok Raj 			break;
1535c32b6b8eSAshok Raj 		}
1536c32b6b8eSAshok Raj 	}
1537c32b6b8eSAshok Raj 	return NOTIFY_OK;
1538c32b6b8eSAshok Raj }
1539c32b6b8eSAshok Raj 
154074b85f37SChandra Seetharaman static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
1541c32b6b8eSAshok Raj {
1542c32b6b8eSAshok Raj     .notifier_call = cpufreq_cpu_callback,
1543c32b6b8eSAshok Raj };
154465edc68cSChandra Seetharaman #endif /* CONFIG_HOTPLUG_CPU */
15451da177e4SLinus Torvalds 
15461da177e4SLinus Torvalds /*********************************************************************
15471da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
15481da177e4SLinus Torvalds  *********************************************************************/
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds /**
15511da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
15521da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
15531da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
15541da177e4SLinus Torvalds  *
15551da177e4SLinus Torvalds  *   Registers a CPU Frequency driver to this core code. This code
15561da177e4SLinus Torvalds  * returns zero on success, -EBUSY when another driver got here first
15571da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
15581da177e4SLinus Torvalds  *
15591da177e4SLinus Torvalds  */
15601da177e4SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
15611da177e4SLinus Torvalds {
15621da177e4SLinus Torvalds 	unsigned long flags;
15631da177e4SLinus Torvalds 	int ret;
15641da177e4SLinus Torvalds 
15651da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
15661da177e4SLinus Torvalds 	    ((!driver_data->setpolicy) && (!driver_data->target)))
15671da177e4SLinus Torvalds 		return -EINVAL;
15681da177e4SLinus Torvalds 
15691da177e4SLinus Torvalds 	dprintk("trying to register driver %s\n", driver_data->name);
15701da177e4SLinus Torvalds 
15711da177e4SLinus Torvalds 	if (driver_data->setpolicy)
15721da177e4SLinus Torvalds 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
15751da177e4SLinus Torvalds 	if (cpufreq_driver) {
15761da177e4SLinus Torvalds 		spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
15771da177e4SLinus Torvalds 		return -EBUSY;
15781da177e4SLinus Torvalds 	}
15791da177e4SLinus Torvalds 	cpufreq_driver = driver_data;
15801da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
15831da177e4SLinus Torvalds 
15841da177e4SLinus Torvalds 	if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
15851da177e4SLinus Torvalds 		int i;
15861da177e4SLinus Torvalds 		ret = -ENODEV;
15871da177e4SLinus Torvalds 
15881da177e4SLinus Torvalds 		/* check for at least one working CPU */
15891da177e4SLinus Torvalds 		for (i=0; i<NR_CPUS; i++)
15901da177e4SLinus Torvalds 			if (cpufreq_cpu_data[i])
15911da177e4SLinus Torvalds 				ret = 0;
15921da177e4SLinus Torvalds 
15931da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
15941da177e4SLinus Torvalds 		if (ret) {
15951da177e4SLinus Torvalds 			dprintk("no CPU initialized for driver %s\n", driver_data->name);
15961da177e4SLinus Torvalds 			sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
15971da177e4SLinus Torvalds 
15981da177e4SLinus Torvalds 			spin_lock_irqsave(&cpufreq_driver_lock, flags);
15991da177e4SLinus Torvalds 			cpufreq_driver = NULL;
16001da177e4SLinus Torvalds 			spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
16011da177e4SLinus Torvalds 		}
16021da177e4SLinus Torvalds 	}
16031da177e4SLinus Torvalds 
16041da177e4SLinus Torvalds 	if (!ret) {
160565edc68cSChandra Seetharaman 		register_hotcpu_notifier(&cpufreq_cpu_notifier);
16061da177e4SLinus Torvalds 		dprintk("driver %s up and running\n", driver_data->name);
16071da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
16081da177e4SLinus Torvalds 	}
16091da177e4SLinus Torvalds 
16101da177e4SLinus Torvalds 	return (ret);
16111da177e4SLinus Torvalds }
16121da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
16131da177e4SLinus Torvalds 
16141da177e4SLinus Torvalds 
16151da177e4SLinus Torvalds /**
16161da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
16171da177e4SLinus Torvalds  *
16181da177e4SLinus Torvalds  *    Unregister the current CPUFreq driver. Only call this if you have
16191da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
16201da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
16211da177e4SLinus Torvalds  * currently not initialised.
16221da177e4SLinus Torvalds  */
16231da177e4SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
16241da177e4SLinus Torvalds {
16251da177e4SLinus Torvalds 	unsigned long flags;
16261da177e4SLinus Torvalds 
16271da177e4SLinus Torvalds 	cpufreq_debug_disable_ratelimit();
16281da177e4SLinus Torvalds 
16291da177e4SLinus Torvalds 	if (!cpufreq_driver || (driver != cpufreq_driver)) {
16301da177e4SLinus Torvalds 		cpufreq_debug_enable_ratelimit();
16311da177e4SLinus Torvalds 		return -EINVAL;
16321da177e4SLinus Torvalds 	}
16331da177e4SLinus Torvalds 
16341da177e4SLinus Torvalds 	dprintk("unregistering driver %s\n", driver->name);
16351da177e4SLinus Torvalds 
16361da177e4SLinus Torvalds 	sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
163765edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds 	spin_lock_irqsave(&cpufreq_driver_lock, flags);
16401da177e4SLinus Torvalds 	cpufreq_driver = NULL;
16411da177e4SLinus Torvalds 	spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
16421da177e4SLinus Torvalds 
16431da177e4SLinus Torvalds 	return 0;
16441da177e4SLinus Torvalds }
16451da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
1646