blob: 86e69b7f9122d3639e834307c5a1662e33ee8986 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08007 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05008 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -05009 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/notifier.h>
22#include <linux/cpufreq.h>
23#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/spinlock.h>
26#include <linux/device.h>
27#include <linux/slab.h>
28#include <linux/cpu.h>
29#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
33
34/**
Dave Jonescd878472006-08-11 17:59:28 -040035 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * level driver of CPUFreq support, and its spinlock. This lock
37 * also protects the cpufreq_cpu_data array.
38 */
Dave Jones7d5e3502006-02-02 17:03:42 -050039static struct cpufreq_driver *cpufreq_driver;
40static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static DEFINE_SPINLOCK(cpufreq_driver_lock);
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* internal prototypes */
44static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
45static void handle_update(void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/**
Dave Jones32ee8c32006-02-28 00:43:23 -050048 * Two notifier lists: the "policy" list is involved in the
49 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * "transition" list for kernel code that needs to handle
51 * changes to devices when the CPU clock speed changes.
52 * The mutex locks both lists.
53 */
Alan Sterne041c682006-03-27 01:16:30 -080054static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070055static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Alan Sternb4dfdbb2006-10-04 02:17:06 -070057static int __init init_cpufreq_transition_notifier_list(void)
58{
59 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
60 return 0;
61}
62core_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64static LIST_HEAD(cpufreq_governor_list);
Dave Jones7d5e3502006-02-02 17:03:42 -050065static DEFINE_MUTEX (cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Dave Jones7d5e3502006-02-02 17:03:42 -050067struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct cpufreq_policy *data;
70 unsigned long flags;
71
72 if (cpu >= NR_CPUS)
73 goto err_out;
74
75 /* get the cpufreq driver */
76 spin_lock_irqsave(&cpufreq_driver_lock, flags);
77
78 if (!cpufreq_driver)
79 goto err_out_unlock;
80
81 if (!try_module_get(cpufreq_driver->owner))
82 goto err_out_unlock;
83
84
85 /* get the CPU */
86 data = cpufreq_cpu_data[cpu];
87
88 if (!data)
89 goto err_out_put_module;
90
91 if (!kobject_get(&data->kobj))
92 goto err_out_put_module;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return data;
96
Dave Jones7d5e3502006-02-02 17:03:42 -050097err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -050099err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -0500101err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return NULL;
103}
104EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
105
Dave Jones7d5e3502006-02-02 17:03:42 -0500106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107void cpufreq_cpu_put(struct cpufreq_policy *data)
108{
109 kobject_put(&data->kobj);
110 module_put(cpufreq_driver->owner);
111}
112EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
113
114
115/*********************************************************************
116 * UNIFIED DEBUG HELPERS *
117 *********************************************************************/
118#ifdef CONFIG_CPU_FREQ_DEBUG
119
120/* what part(s) of the CPUfreq subsystem are debugged? */
121static unsigned int debug;
122
123/* is the debug output ratelimit'ed using printk_ratelimit? User can
124 * set or modify this value.
125 */
126static unsigned int debug_ratelimit = 1;
127
128/* is the printk_ratelimit'ing enabled? It's enabled after a successful
129 * loading of a cpufreq driver, temporarily disabled when a new policy
130 * is set, and disabled upon cpufreq driver removal
131 */
132static unsigned int disable_ratelimit = 1;
133static DEFINE_SPINLOCK(disable_ratelimit_lock);
134
Arjan van de Ven858119e2006-01-14 13:20:43 -0800135static void cpufreq_debug_enable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 unsigned long flags;
138
139 spin_lock_irqsave(&disable_ratelimit_lock, flags);
140 if (disable_ratelimit)
141 disable_ratelimit--;
142 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
143}
144
Arjan van de Ven858119e2006-01-14 13:20:43 -0800145static void cpufreq_debug_disable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 unsigned long flags;
148
149 spin_lock_irqsave(&disable_ratelimit_lock, flags);
150 disable_ratelimit++;
151 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
152}
153
154void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
155{
156 char s[256];
157 va_list args;
158 unsigned int len;
159 unsigned long flags;
Dave Jones32ee8c32006-02-28 00:43:23 -0500160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 WARN_ON(!prefix);
162 if (type & debug) {
163 spin_lock_irqsave(&disable_ratelimit_lock, flags);
164 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
165 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
166 return;
167 }
168 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
169
170 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
171
172 va_start(args, fmt);
173 len += vsnprintf(&s[len], (256 - len), fmt, args);
174 va_end(args);
175
176 printk(s);
177
178 WARN_ON(len < 5);
179 }
180}
181EXPORT_SYMBOL(cpufreq_debug_printk);
182
183
184module_param(debug, uint, 0644);
185MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
186
187module_param(debug_ratelimit, uint, 0644);
188MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
189
190#else /* !CONFIG_CPU_FREQ_DEBUG */
191
192static inline void cpufreq_debug_enable_ratelimit(void) { return; }
193static inline void cpufreq_debug_disable_ratelimit(void) { return; }
194
195#endif /* CONFIG_CPU_FREQ_DEBUG */
196
197
198/*********************************************************************
199 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
200 *********************************************************************/
201
202/**
203 * adjust_jiffies - adjust the system "loops_per_jiffy"
204 *
205 * This function alters the system "loops_per_jiffy" for the clock
206 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500207 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * per-CPU loops_per_jiffy value wherever possible.
209 */
210#ifndef CONFIG_SMP
211static unsigned long l_p_j_ref;
212static unsigned int l_p_j_ref_freq;
213
Arjan van de Ven858119e2006-01-14 13:20:43 -0800214static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 if (ci->flags & CPUFREQ_CONST_LOOPS)
217 return;
218
219 if (!l_p_j_ref_freq) {
220 l_p_j_ref = loops_per_jiffy;
221 l_p_j_ref_freq = ci->old;
222 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
223 }
224 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
225 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700226 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
228 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
229 }
230}
231#else
232static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
233#endif
234
235
236/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800237 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
238 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800240 * This function calls the transition notifiers and the "adjust_jiffies"
241 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500242 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 */
244void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
245{
Dave Jonese4472cb2006-01-31 15:53:55 -0800246 struct cpufreq_policy *policy;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 BUG_ON(irqs_disabled());
249
250 freqs->flags = cpufreq_driver->flags;
Dave Jonese4472cb2006-01-31 15:53:55 -0800251 dprintk("notification %u of frequency transition to %u kHz\n",
252 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Dave Jonese4472cb2006-01-31 15:53:55 -0800254 policy = cpufreq_cpu_data[freqs->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500258 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800259 * which is not equal to what the cpufreq core thinks is
260 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 */
262 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800263 if ((policy) && (policy->cpu == freqs->cpu) &&
264 (policy->cur) && (policy->cur != freqs->old)) {
Jan Beulichb10eec22006-04-28 13:47:13 +0200265 dprintk("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800266 " %u, cpufreq assumed %u kHz.\n",
267 freqs->old, policy->cur);
268 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700271 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800272 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
274 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 case CPUFREQ_POSTCHANGE:
277 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700278 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800279 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800280 if (likely(policy) && likely(policy->cpu == freqs->cpu))
281 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 break;
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
286
287
288
289/*********************************************************************
290 * SYSFS INTERFACE *
291 *********************************************************************/
292
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700293static struct cpufreq_governor *__find_governor(const char *str_governor)
294{
295 struct cpufreq_governor *t;
296
297 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
298 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN))
299 return t;
300
301 return NULL;
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/**
305 * cpufreq_parse_governor - parse a governor string
306 */
307static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
308 struct cpufreq_governor **governor)
309{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700310 int err = -EINVAL;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700313 goto out;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (cpufreq_driver->setpolicy) {
316 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
317 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700318 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
320 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700321 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700323 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700325
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800326 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700327
328 t = __find_governor(str_governor);
329
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700330 if (t == NULL) {
331 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s", str_governor);
332
333 if (name) {
334 int ret;
335
336 mutex_unlock(&cpufreq_governor_mutex);
337 ret = request_module(name);
338 mutex_lock(&cpufreq_governor_mutex);
339
340 if (ret == 0)
341 t = __find_governor(str_governor);
342 }
343
344 kfree(name);
345 }
346
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700347 if (t != NULL) {
348 *governor = t;
349 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700351
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800352 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700354 out:
355 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358
359/* drivers/base/cpu.c */
360extern struct sysdev_class cpu_sysdev_class;
361
362
363/**
364 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
365 *
366 * Write out information from cpufreq_driver->policy[cpu]; object must be
367 * "unsigned int".
368 */
369
Dave Jones32ee8c32006-02-28 00:43:23 -0500370#define show_one(file_name, object) \
371static ssize_t show_##file_name \
372(struct cpufreq_policy * policy, char *buf) \
373{ \
374 return sprintf (buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377show_one(cpuinfo_min_freq, cpuinfo.min_freq);
378show_one(cpuinfo_max_freq, cpuinfo.max_freq);
379show_one(scaling_min_freq, min);
380show_one(scaling_max_freq, max);
381show_one(scaling_cur_freq, cur);
382
Thomas Renninger7970e082006-04-13 15:14:04 +0200383static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/**
386 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
387 */
388#define store_one(file_name, object) \
389static ssize_t store_##file_name \
390(struct cpufreq_policy * policy, const char *buf, size_t count) \
391{ \
392 unsigned int ret = -EINVAL; \
393 struct cpufreq_policy new_policy; \
394 \
395 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
396 if (ret) \
397 return -EINVAL; \
398 \
399 ret = sscanf (buf, "%u", &new_policy.object); \
400 if (ret != 1) \
401 return -EINVAL; \
402 \
Arjan van de Ven153d7f32006-07-26 15:40:07 +0200403 lock_cpu_hotplug(); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200404 mutex_lock(&policy->lock); \
405 ret = __cpufreq_set_policy(policy, &new_policy); \
406 policy->user_policy.object = policy->object; \
407 mutex_unlock(&policy->lock); \
Arjan van de Ven153d7f32006-07-26 15:40:07 +0200408 unlock_cpu_hotplug(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 \
410 return ret ? ret : count; \
411}
412
413store_one(scaling_min_freq,min);
414store_one(scaling_max_freq,max);
415
416/**
417 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
418 */
419static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
420{
421 unsigned int cur_freq = cpufreq_get(policy->cpu);
422 if (!cur_freq)
423 return sprintf(buf, "<unknown>");
424 return sprintf(buf, "%u\n", cur_freq);
425}
426
427
428/**
429 * show_scaling_governor - show the current policy for the specified CPU
430 */
431static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
432{
433 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
434 return sprintf(buf, "powersave\n");
435 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
436 return sprintf(buf, "performance\n");
437 else if (policy->governor)
438 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
439 return -EINVAL;
440}
441
442
443/**
444 * store_scaling_governor - store policy for the specified CPU
445 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500446static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
447 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 unsigned int ret = -EINVAL;
450 char str_governor[16];
451 struct cpufreq_policy new_policy;
452
453 ret = cpufreq_get_policy(&new_policy, policy->cpu);
454 if (ret)
455 return ret;
456
457 ret = sscanf (buf, "%15s", str_governor);
458 if (ret != 1)
459 return -EINVAL;
460
461 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
462 return -EINVAL;
463
Dave Jonesa496e252006-07-07 12:31:27 -0400464 lock_cpu_hotplug();
465
Thomas Renninger7970e082006-04-13 15:14:04 +0200466 /* Do not use cpufreq_set_policy here or the user_policy.max
467 will be wrongly overridden */
468 mutex_lock(&policy->lock);
469 ret = __cpufreq_set_policy(policy, &new_policy);
470
471 policy->user_policy.policy = policy->policy;
472 policy->user_policy.governor = policy->governor;
473 mutex_unlock(&policy->lock);
474
Dave Jonesa496e252006-07-07 12:31:27 -0400475 unlock_cpu_hotplug();
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return ret ? ret : count;
478}
479
480/**
481 * show_scaling_driver - show the cpufreq driver currently loaded
482 */
483static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
484{
485 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
486}
487
488/**
489 * show_scaling_available_governors - show the available CPUfreq governors
490 */
491static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
492 char *buf)
493{
494 ssize_t i = 0;
495 struct cpufreq_governor *t;
496
497 if (!cpufreq_driver->target) {
498 i += sprintf(buf, "performance powersave");
499 goto out;
500 }
501
502 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
503 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
504 goto out;
505 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
506 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500507out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 i += sprintf(&buf[i], "\n");
509 return i;
510}
511/**
512 * show_affected_cpus - show the CPUs affected by each transition
513 */
514static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
515{
516 ssize_t i = 0;
517 unsigned int cpu;
518
519 for_each_cpu_mask(cpu, policy->cpus) {
520 if (i)
521 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
522 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
523 if (i >= (PAGE_SIZE - 5))
524 break;
525 }
526 i += sprintf(&buf[i], "\n");
527 return i;
528}
529
530
531#define define_one_ro(_name) \
532static struct freq_attr _name = \
533__ATTR(_name, 0444, show_##_name, NULL)
534
535#define define_one_ro0400(_name) \
536static struct freq_attr _name = \
537__ATTR(_name, 0400, show_##_name, NULL)
538
539#define define_one_rw(_name) \
540static struct freq_attr _name = \
541__ATTR(_name, 0644, show_##_name, store_##_name)
542
543define_one_ro0400(cpuinfo_cur_freq);
544define_one_ro(cpuinfo_min_freq);
545define_one_ro(cpuinfo_max_freq);
546define_one_ro(scaling_available_governors);
547define_one_ro(scaling_driver);
548define_one_ro(scaling_cur_freq);
549define_one_ro(affected_cpus);
550define_one_rw(scaling_min_freq);
551define_one_rw(scaling_max_freq);
552define_one_rw(scaling_governor);
553
554static struct attribute * default_attrs[] = {
555 &cpuinfo_min_freq.attr,
556 &cpuinfo_max_freq.attr,
557 &scaling_min_freq.attr,
558 &scaling_max_freq.attr,
559 &affected_cpus.attr,
560 &scaling_governor.attr,
561 &scaling_driver.attr,
562 &scaling_available_governors.attr,
563 NULL
564};
565
566#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
567#define to_attr(a) container_of(a,struct freq_attr,attr)
568
569static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
570{
571 struct cpufreq_policy * policy = to_policy(kobj);
572 struct freq_attr * fattr = to_attr(attr);
573 ssize_t ret;
574 policy = cpufreq_cpu_get(policy->cpu);
575 if (!policy)
576 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500577 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 cpufreq_cpu_put(policy);
579 return ret;
580}
581
Dave Jones32ee8c32006-02-28 00:43:23 -0500582static ssize_t store(struct kobject * kobj, struct attribute * attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 const char * buf, size_t count)
584{
585 struct cpufreq_policy * policy = to_policy(kobj);
586 struct freq_attr * fattr = to_attr(attr);
587 ssize_t ret;
588 policy = cpufreq_cpu_get(policy->cpu);
589 if (!policy)
590 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500591 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 cpufreq_cpu_put(policy);
593 return ret;
594}
595
596static void cpufreq_sysfs_release(struct kobject * kobj)
597{
598 struct cpufreq_policy * policy = to_policy(kobj);
599 dprintk("last reference is dropped\n");
600 complete(&policy->kobj_unregister);
601}
602
603static struct sysfs_ops sysfs_ops = {
604 .show = show,
605 .store = store,
606};
607
608static struct kobj_type ktype_cpufreq = {
609 .sysfs_ops = &sysfs_ops,
610 .default_attrs = default_attrs,
611 .release = cpufreq_sysfs_release,
612};
613
614
615/**
616 * cpufreq_add_dev - add a CPU device
617 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500618 * Adds the cpufreq interface for a CPU device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
620static int cpufreq_add_dev (struct sys_device * sys_dev)
621{
622 unsigned int cpu = sys_dev->id;
623 int ret = 0;
624 struct cpufreq_policy new_policy;
625 struct cpufreq_policy *policy;
626 struct freq_attr **drv_attr;
Dave Jones8ff69732006-03-05 03:37:23 -0500627 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 unsigned long flags;
629 unsigned int j;
Dave Jones8ff69732006-03-05 03:37:23 -0500630#ifdef CONFIG_SMP
631 struct cpufreq_policy *managed_policy;
632#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Ashok Rajc32b6b82005-10-30 14:59:54 -0800634 if (cpu_is_offline(cpu))
635 return 0;
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 cpufreq_debug_disable_ratelimit();
638 dprintk("adding CPU %u\n", cpu);
639
640#ifdef CONFIG_SMP
641 /* check whether a different CPU already registered this
642 * CPU because it is in the same boat. */
643 policy = cpufreq_cpu_get(cpu);
644 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500645 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 cpufreq_debug_enable_ratelimit();
647 return 0;
648 }
649#endif
650
651 if (!try_module_get(cpufreq_driver->owner)) {
652 ret = -EINVAL;
653 goto module_out;
654 }
655
Dave Jonese98df502005-10-20 15:17:43 -0700656 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 if (!policy) {
658 ret = -ENOMEM;
659 goto nomem_out;
660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 policy->cpu = cpu;
663 policy->cpus = cpumask_of_cpu(cpu);
664
Arjan van de Ven83933af2006-01-14 16:01:49 +0100665 mutex_init(&policy->lock);
666 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 init_completion(&policy->kobj_unregister);
668 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
669
670 /* call driver. From then on the cpufreq must be able
671 * to accept all calls to ->verify and ->setpolicy for this CPU
672 */
673 ret = cpufreq_driver->init(policy);
674 if (ret) {
675 dprintk("initialization failed\n");
Andrew Mortonf3876c12006-01-18 13:40:54 -0800676 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 goto err_out;
678 }
679
Dave Jones8ff69732006-03-05 03:37:23 -0500680#ifdef CONFIG_SMP
681 for_each_cpu_mask(j, policy->cpus) {
682 if (cpu == j)
683 continue;
684
685 /* check for existing affected CPUs. They may not be aware
686 * of it due to CPU Hotplug.
687 */
688 managed_policy = cpufreq_cpu_get(j);
689 if (unlikely(managed_policy)) {
690 spin_lock_irqsave(&cpufreq_driver_lock, flags);
691 managed_policy->cpus = policy->cpus;
692 cpufreq_cpu_data[cpu] = managed_policy;
693 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
694
695 dprintk("CPU already managed, adding link\n");
696 sysfs_create_link(&sys_dev->kobj,
697 &managed_policy->kobj, "cpufreq");
698
699 cpufreq_debug_enable_ratelimit();
700 mutex_unlock(&policy->lock);
701 ret = 0;
702 goto err_out_driver_exit; /* call driver->exit() */
703 }
704 }
705#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
707
708 /* prepare interface data */
709 policy->kobj.parent = &sys_dev->kobj;
710 policy->kobj.ktype = &ktype_cpufreq;
711 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
712
713 ret = kobject_register(&policy->kobj);
Andrew Mortonf3876c12006-01-18 13:40:54 -0800714 if (ret) {
715 mutex_unlock(&policy->lock);
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700716 goto err_out_driver_exit;
Andrew Mortonf3876c12006-01-18 13:40:54 -0800717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 /* set up files for this cpu device */
719 drv_attr = cpufreq_driver->attr;
720 while ((drv_attr) && (*drv_attr)) {
721 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
722 drv_attr++;
723 }
724 if (cpufreq_driver->get)
725 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
726 if (cpufreq_driver->target)
727 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
728
729 spin_lock_irqsave(&cpufreq_driver_lock, flags);
730 for_each_cpu_mask(j, policy->cpus)
731 cpufreq_cpu_data[j] = policy;
732 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones8ff69732006-03-05 03:37:23 -0500733
734 /* symlink affected CPUs */
735 for_each_cpu_mask(j, policy->cpus) {
736 if (j == cpu)
737 continue;
738 if (!cpu_online(j))
739 continue;
740
Dave Jones1f8b2c92006-03-29 01:40:04 -0500741 dprintk("CPU %u already managed, adding link\n", j);
Dave Jones8ff69732006-03-05 03:37:23 -0500742 cpufreq_cpu_get(cpu);
743 cpu_sys_dev = get_cpu_sysdev(j);
744 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
745 "cpufreq");
746 }
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 policy->governor = NULL; /* to assure that the starting sequence is
749 * run in cpufreq_set_policy */
Arjan van de Ven83933af2006-01-14 16:01:49 +0100750 mutex_unlock(&policy->lock);
Dave Jones87c32272006-03-29 01:48:37 -0500751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 /* set default policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 ret = cpufreq_set_policy(&new_policy);
754 if (ret) {
755 dprintk("setting policy failed\n");
756 goto err_out_unregister;
757 }
758
759 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 dprintk("initialization complete\n");
761 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -0500762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return 0;
764
765
766err_out_unregister:
767 spin_lock_irqsave(&cpufreq_driver_lock, flags);
768 for_each_cpu_mask(j, policy->cpus)
769 cpufreq_cpu_data[j] = NULL;
770 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
771
772 kobject_unregister(&policy->kobj);
773 wait_for_completion(&policy->kobj_unregister);
774
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700775err_out_driver_exit:
776 if (cpufreq_driver->exit)
777 cpufreq_driver->exit(policy);
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779err_out:
780 kfree(policy);
781
782nomem_out:
783 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800784module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 cpufreq_debug_enable_ratelimit();
786 return ret;
787}
788
789
790/**
791 * cpufreq_remove_dev - remove a CPU device
792 *
793 * Removes the cpufreq interface for a CPU device.
794 */
795static int cpufreq_remove_dev (struct sys_device * sys_dev)
796{
797 unsigned int cpu = sys_dev->id;
798 unsigned long flags;
799 struct cpufreq_policy *data;
800#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -0800801 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 unsigned int j;
803#endif
804
805 cpufreq_debug_disable_ratelimit();
806 dprintk("unregistering CPU %u\n", cpu);
807
808 spin_lock_irqsave(&cpufreq_driver_lock, flags);
809 data = cpufreq_cpu_data[cpu];
810
811 if (!data) {
812 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 cpufreq_debug_enable_ratelimit();
814 return -EINVAL;
815 }
816 cpufreq_cpu_data[cpu] = NULL;
817
818
819#ifdef CONFIG_SMP
820 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -0500821 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 */
823 if (unlikely(cpu != data->cpu)) {
824 dprintk("removing link\n");
Dave Jones8ff69732006-03-05 03:37:23 -0500825 cpu_clear(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
827 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 cpufreq_cpu_put(data);
829 cpufreq_debug_enable_ratelimit();
830 return 0;
831 }
832#endif
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 if (!kobject_get(&data->kobj)) {
836 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
837 cpufreq_debug_enable_ratelimit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500838 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840
841#ifdef CONFIG_SMP
842 /* if we have other CPUs still registered, we need to unlink them,
843 * or else wait_for_completion below will lock up. Clean the
844 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
845 * links afterwards.
846 */
847 if (unlikely(cpus_weight(data->cpus) > 1)) {
848 for_each_cpu_mask(j, data->cpus) {
849 if (j == cpu)
850 continue;
851 cpufreq_cpu_data[j] = NULL;
852 }
853 }
854
855 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
856
857 if (unlikely(cpus_weight(data->cpus) > 1)) {
858 for_each_cpu_mask(j, data->cpus) {
859 if (j == cpu)
860 continue;
861 dprintk("removing link for cpu %u\n", j);
Ashok Rajd434fca2005-10-30 14:59:52 -0800862 cpu_sys_dev = get_cpu_sysdev(j);
863 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 cpufreq_cpu_put(data);
865 }
866 }
867#else
868 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
869#endif
870
Arjan van de Ven83933af2006-01-14 16:01:49 +0100871 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (cpufreq_driver->target)
873 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Arjan van de Ven83933af2006-01-14 16:01:49 +0100874 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 kobject_unregister(&data->kobj);
877
878 kobject_put(&data->kobj);
879
880 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -0500881 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 * unloading.
883 */
884 dprintk("waiting for dropping of refcount\n");
885 wait_for_completion(&data->kobj_unregister);
886 dprintk("wait complete\n");
887
888 if (cpufreq_driver->exit)
889 cpufreq_driver->exit(data);
890
891 kfree(data);
892
893 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 return 0;
895}
896
897
898static void handle_update(void *data)
899{
900 unsigned int cpu = (unsigned int)(long)data;
901 dprintk("handle_update for cpu %u called\n", cpu);
902 cpufreq_update_policy(cpu);
903}
904
905/**
906 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
907 * @cpu: cpu number
908 * @old_freq: CPU frequency the kernel thinks the CPU runs at
909 * @new_freq: CPU frequency the CPU actually runs at
910 *
911 * We adjust to current frequency first, and need to clean up later. So either call
912 * to cpufreq_update_policy() or schedule handle_update()).
913 */
914static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
915{
916 struct cpufreq_freqs freqs;
917
Jan Beulichb10eec22006-04-28 13:47:13 +0200918 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
920
921 freqs.cpu = cpu;
922 freqs.old = old_freq;
923 freqs.new = new_freq;
924 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
925 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
926}
927
928
Dave Jones32ee8c32006-02-28 00:43:23 -0500929/**
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800930 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
931 * @cpu: CPU number
932 *
933 * This is the last known freq, without actually getting it from the driver.
934 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
935 */
936unsigned int cpufreq_quick_get(unsigned int cpu)
937{
938 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
939 unsigned int ret = 0;
940
941 if (policy) {
Arjan van de Ven83933af2006-01-14 16:01:49 +0100942 mutex_lock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800943 ret = policy->cur;
Arjan van de Ven83933af2006-01-14 16:01:49 +0100944 mutex_unlock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800945 cpufreq_cpu_put(policy);
946 }
947
948 return (ret);
949}
950EXPORT_SYMBOL(cpufreq_quick_get);
951
952
Dave Jones32ee8c32006-02-28 00:43:23 -0500953/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 * cpufreq_get - get the current CPU frequency (in kHz)
955 * @cpu: CPU number
956 *
957 * Get the CPU current (static) CPU frequency
958 */
959unsigned int cpufreq_get(unsigned int cpu)
960{
961 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
962 unsigned int ret = 0;
963
964 if (!policy)
965 return 0;
966
967 if (!cpufreq_driver->get)
968 goto out;
969
Arjan van de Ven83933af2006-01-14 16:01:49 +0100970 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 ret = cpufreq_driver->get(cpu);
973
Dave Jones7d5e3502006-02-02 17:03:42 -0500974 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* verify no discrepancy between actual and saved value exists */
976 if (unlikely(ret != policy->cur)) {
977 cpufreq_out_of_sync(cpu, policy->cur, ret);
978 schedule_work(&policy->update);
979 }
980 }
981
Arjan van de Ven83933af2006-01-14 16:01:49 +0100982 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Dave Jones7d5e3502006-02-02 17:03:42 -0500984out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 cpufreq_cpu_put(policy);
986
987 return (ret);
988}
989EXPORT_SYMBOL(cpufreq_get);
990
991
992/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700993 * cpufreq_suspend - let the low level driver prepare for suspend
994 */
995
Bernard Blackhame00d9962005-07-07 17:56:42 -0700996static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700997{
998 int cpu = sysdev->id;
999 unsigned int ret = 0;
1000 unsigned int cur_freq = 0;
1001 struct cpufreq_policy *cpu_policy;
1002
Dave Jones0e37b152006-09-26 23:02:34 -04001003 dprintk("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001004
1005 if (!cpu_online(cpu))
1006 return 0;
1007
1008 /* we may be lax here as interrupts are off. Nonetheless
1009 * we need to grab the correct cpu policy, as to check
1010 * whether we really run on this CPU.
1011 */
1012
1013 cpu_policy = cpufreq_cpu_get(cpu);
1014 if (!cpu_policy)
1015 return -EINVAL;
1016
1017 /* only handle each CPU group once */
1018 if (unlikely(cpu_policy->cpu != cpu)) {
1019 cpufreq_cpu_put(cpu_policy);
1020 return 0;
1021 }
1022
1023 if (cpufreq_driver->suspend) {
Bernard Blackhame00d9962005-07-07 17:56:42 -07001024 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001025 if (ret) {
1026 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1027 "step on CPU %u\n", cpu_policy->cpu);
1028 cpufreq_cpu_put(cpu_policy);
1029 return ret;
1030 }
1031 }
1032
1033
1034 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
1035 goto out;
1036
1037 if (cpufreq_driver->get)
1038 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1039
1040 if (!cur_freq || !cpu_policy->cur) {
1041 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1042 "frequency is what timing core thinks it is.\n");
1043 goto out;
1044 }
1045
1046 if (unlikely(cur_freq != cpu_policy->cur)) {
1047 struct cpufreq_freqs freqs;
1048
1049 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001050 dprintk("Warning: CPU frequency is %u, "
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001051 "cpufreq assumed %u kHz.\n",
1052 cur_freq, cpu_policy->cur);
1053
1054 freqs.cpu = cpu;
1055 freqs.old = cpu_policy->cur;
1056 freqs.new = cur_freq;
1057
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001058 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001059 CPUFREQ_SUSPENDCHANGE, &freqs);
1060 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1061
1062 cpu_policy->cur = cur_freq;
1063 }
1064
Dave Jones7d5e3502006-02-02 17:03:42 -05001065out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001066 cpufreq_cpu_put(cpu_policy);
1067 return 0;
1068}
1069
1070/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 * cpufreq_resume - restore proper CPU frequency handling after resume
1072 *
1073 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1074 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001075 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1076 * restored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
1078static int cpufreq_resume(struct sys_device * sysdev)
1079{
1080 int cpu = sysdev->id;
1081 unsigned int ret = 0;
1082 struct cpufreq_policy *cpu_policy;
1083
1084 dprintk("resuming cpu %u\n", cpu);
1085
1086 if (!cpu_online(cpu))
1087 return 0;
1088
1089 /* we may be lax here as interrupts are off. Nonetheless
1090 * we need to grab the correct cpu policy, as to check
1091 * whether we really run on this CPU.
1092 */
1093
1094 cpu_policy = cpufreq_cpu_get(cpu);
1095 if (!cpu_policy)
1096 return -EINVAL;
1097
1098 /* only handle each CPU group once */
1099 if (unlikely(cpu_policy->cpu != cpu)) {
1100 cpufreq_cpu_put(cpu_policy);
1101 return 0;
1102 }
1103
1104 if (cpufreq_driver->resume) {
1105 ret = cpufreq_driver->resume(cpu_policy);
1106 if (ret) {
1107 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1108 "step on CPU %u\n", cpu_policy->cpu);
1109 cpufreq_cpu_put(cpu_policy);
1110 return ret;
1111 }
1112 }
1113
1114 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1115 unsigned int cur_freq = 0;
1116
1117 if (cpufreq_driver->get)
1118 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1119
1120 if (!cur_freq || !cpu_policy->cur) {
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001121 printk(KERN_ERR "cpufreq: resume failed to assert "
1122 "current frequency is what timing core "
1123 "thinks it is.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 goto out;
1125 }
1126
1127 if (unlikely(cur_freq != cpu_policy->cur)) {
1128 struct cpufreq_freqs freqs;
1129
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001130 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001131 dprintk("Warning: CPU frequency"
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001132 "is %u, cpufreq assumed %u kHz.\n",
1133 cur_freq, cpu_policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 freqs.cpu = cpu;
1136 freqs.old = cpu_policy->cur;
1137 freqs.new = cur_freq;
1138
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001139 srcu_notifier_call_chain(
Alan Sterne041c682006-03-27 01:16:30 -08001140 &cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001141 CPUFREQ_RESUMECHANGE, &freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1143
1144 cpu_policy->cur = cur_freq;
1145 }
1146 }
1147
1148out:
1149 schedule_work(&cpu_policy->update);
1150 cpufreq_cpu_put(cpu_policy);
1151 return ret;
1152}
1153
1154static struct sysdev_driver cpufreq_sysdev_driver = {
1155 .add = cpufreq_add_dev,
1156 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001157 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 .resume = cpufreq_resume,
1159};
1160
1161
1162/*********************************************************************
1163 * NOTIFIER LISTS INTERFACE *
1164 *********************************************************************/
1165
1166/**
1167 * cpufreq_register_notifier - register a driver with cpufreq
1168 * @nb: notifier function to register
1169 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1170 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001171 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * are notified about clock rate changes (once before and once after
1173 * the transition), or a list of drivers that are notified about
1174 * changes in cpufreq policy.
1175 *
1176 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001177 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 */
1179int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1180{
1181 int ret;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 switch (list) {
1184 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001185 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001186 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 break;
1188 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001189 ret = blocking_notifier_chain_register(
1190 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 break;
1192 default:
1193 ret = -EINVAL;
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 return ret;
1197}
1198EXPORT_SYMBOL(cpufreq_register_notifier);
1199
1200
1201/**
1202 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1203 * @nb: notifier block to be unregistered
1204 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1205 *
1206 * Remove a driver from the CPU frequency notifier list.
1207 *
1208 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001209 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 */
1211int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1212{
1213 int ret;
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 switch (list) {
1216 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001217 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001218 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 break;
1220 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001221 ret = blocking_notifier_chain_unregister(
1222 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 break;
1224 default:
1225 ret = -EINVAL;
1226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 return ret;
1229}
1230EXPORT_SYMBOL(cpufreq_unregister_notifier);
1231
1232
1233/*********************************************************************
1234 * GOVERNORS *
1235 *********************************************************************/
1236
1237
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001238/* Must be called with lock_cpu_hotplug held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239int __cpufreq_driver_target(struct cpufreq_policy *policy,
1240 unsigned int target_freq,
1241 unsigned int relation)
1242{
1243 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1246 target_freq, relation);
1247 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1248 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return retval;
1251}
1252EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254int cpufreq_driver_target(struct cpufreq_policy *policy,
1255 unsigned int target_freq,
1256 unsigned int relation)
1257{
Dave Jonescc993ca2005-07-28 09:43:56 -07001258 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 policy = cpufreq_cpu_get(policy->cpu);
1261 if (!policy)
1262 return -EINVAL;
1263
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001264 lock_cpu_hotplug();
Arjan van de Ven83933af2006-01-14 16:01:49 +01001265 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 ret = __cpufreq_driver_target(policy, target_freq, relation);
1268
Arjan van de Ven83933af2006-01-14 16:01:49 +01001269 mutex_unlock(&policy->lock);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001270 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return ret;
1274}
1275EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1276
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001277/*
1278 * Locking: Must be called with the lock_cpu_hotplug() lock held
1279 * when "event" is CPUFREQ_GOV_LIMITS
1280 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1283{
Dave Jonescc993ca2005-07-28 09:43:56 -07001284 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (!try_module_get(policy->governor->owner))
1287 return -EINVAL;
1288
1289 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1290 ret = policy->governor->governor(policy, event);
1291
1292 /* we keep one module reference alive for each CPU governed by this CPU */
1293 if ((event != CPUFREQ_GOV_START) || ret)
1294 module_put(policy->governor->owner);
1295 if ((event == CPUFREQ_GOV_STOP) && !ret)
1296 module_put(policy->governor->owner);
1297
1298 return ret;
1299}
1300
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302int cpufreq_register_governor(struct cpufreq_governor *governor)
1303{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001304 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 if (!governor)
1307 return -EINVAL;
1308
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001309 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001310
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001311 err = -EBUSY;
1312 if (__find_governor(governor->name) == NULL) {
1313 err = 0;
1314 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Dave Jones32ee8c32006-02-28 00:43:23 -05001317 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001318 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1321
1322
1323void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1324{
1325 if (!governor)
1326 return;
1327
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001328 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001330 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return;
1332}
1333EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1334
1335
1336
1337/*********************************************************************
1338 * POLICY INTERFACE *
1339 *********************************************************************/
1340
1341/**
1342 * cpufreq_get_policy - get the current cpufreq_policy
1343 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1344 *
1345 * Reads the current cpufreq policy.
1346 */
1347int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1348{
1349 struct cpufreq_policy *cpu_policy;
1350 if (!policy)
1351 return -EINVAL;
1352
1353 cpu_policy = cpufreq_cpu_get(cpu);
1354 if (!cpu_policy)
1355 return -EINVAL;
1356
Arjan van de Ven83933af2006-01-14 16:01:49 +01001357 mutex_lock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Arjan van de Ven83933af2006-01-14 16:01:49 +01001359 mutex_unlock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return 0;
1363}
1364EXPORT_SYMBOL(cpufreq_get_policy);
1365
1366
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001367/*
1368 * Locking: Must be called with the lock_cpu_hotplug() lock held
1369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1371{
1372 int ret = 0;
1373
1374 cpufreq_debug_disable_ratelimit();
1375 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1376 policy->min, policy->max);
1377
Dave Jones7d5e3502006-02-02 17:03:42 -05001378 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001380 if (policy->min > data->min && policy->min > policy->max) {
1381 ret = -EINVAL;
1382 goto error_out;
1383 }
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 /* verify the cpu speed can be set within this limit */
1386 ret = cpufreq_driver->verify(policy);
1387 if (ret)
1388 goto error_out;
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001391 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1392 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001395 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1396 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /* verify the cpu speed can be set within this limit,
1399 which might be different to the first one */
1400 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001401 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001405 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1406 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Dave Jones7d5e3502006-02-02 17:03:42 -05001408 data->min = policy->min;
1409 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1412
1413 if (cpufreq_driver->setpolicy) {
1414 data->policy = policy->policy;
1415 dprintk("setting range\n");
1416 ret = cpufreq_driver->setpolicy(policy);
1417 } else {
1418 if (policy->governor != data->governor) {
1419 /* save old, working values */
1420 struct cpufreq_governor *old_gov = data->governor;
1421
1422 dprintk("governor switch\n");
1423
1424 /* end old governor */
1425 if (data->governor)
1426 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1427
1428 /* start new governor */
1429 data->governor = policy->governor;
1430 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1431 /* new governor failed, so re-start old one */
1432 dprintk("starting governor %s failed\n", data->governor->name);
1433 if (old_gov) {
1434 data->governor = old_gov;
1435 __cpufreq_governor(data, CPUFREQ_GOV_START);
1436 }
1437 ret = -EINVAL;
1438 goto error_out;
1439 }
1440 /* might be a policy change, too, so fall through */
1441 }
1442 dprintk("governor: change or update limits\n");
1443 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1444 }
1445
Dave Jones7d5e3502006-02-02 17:03:42 -05001446error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 cpufreq_debug_enable_ratelimit();
1448 return ret;
1449}
1450
1451/**
1452 * cpufreq_set_policy - set a new CPUFreq policy
1453 * @policy: policy to be set.
1454 *
1455 * Sets a new CPU frequency and voltage scaling policy.
1456 */
1457int cpufreq_set_policy(struct cpufreq_policy *policy)
1458{
1459 int ret = 0;
1460 struct cpufreq_policy *data;
1461
1462 if (!policy)
1463 return -EINVAL;
1464
1465 data = cpufreq_cpu_get(policy->cpu);
1466 if (!data)
1467 return -EINVAL;
1468
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001469 lock_cpu_hotplug();
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /* lock this CPU */
Arjan van de Ven83933af2006-01-14 16:01:49 +01001472 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 ret = __cpufreq_set_policy(data, policy);
1475 data->user_policy.min = data->min;
1476 data->user_policy.max = data->max;
1477 data->user_policy.policy = data->policy;
1478 data->user_policy.governor = data->governor;
1479
Arjan van de Ven83933af2006-01-14 16:01:49 +01001480 mutex_unlock(&data->lock);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001481
1482 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 cpufreq_cpu_put(data);
1484
1485 return ret;
1486}
1487EXPORT_SYMBOL(cpufreq_set_policy);
1488
1489
1490/**
1491 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1492 * @cpu: CPU which shall be re-evaluated
1493 *
1494 * Usefull for policy notifiers which have different necessities
1495 * at different times.
1496 */
1497int cpufreq_update_policy(unsigned int cpu)
1498{
1499 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1500 struct cpufreq_policy policy;
1501 int ret = 0;
1502
1503 if (!data)
1504 return -ENODEV;
1505
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001506 lock_cpu_hotplug();
Arjan van de Ven83933af2006-01-14 16:01:49 +01001507 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
1509 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001510 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 policy.min = data->user_policy.min;
1512 policy.max = data->user_policy.max;
1513 policy.policy = data->user_policy.policy;
1514 policy.governor = data->user_policy.governor;
1515
Thomas Renninger0961dd02006-01-26 18:46:33 +01001516 /* BIOS might change freq behind our back
1517 -> ask driver for current freq and notify governors about a change */
1518 if (cpufreq_driver->get) {
1519 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001520 if (!data->cur) {
1521 dprintk("Driver did not initialize current freq");
1522 data->cur = policy.cur;
1523 } else {
1524 if (data->cur != policy.cur)
1525 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1526 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001527 }
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 ret = __cpufreq_set_policy(data, &policy);
1530
Arjan van de Ven83933af2006-01-14 16:01:49 +01001531 mutex_unlock(&data->lock);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001532 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 cpufreq_cpu_put(data);
1534 return ret;
1535}
1536EXPORT_SYMBOL(cpufreq_update_policy);
1537
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001538#ifdef CONFIG_HOTPLUG_CPU
1539static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001540 unsigned long action, void *hcpu)
1541{
1542 unsigned int cpu = (unsigned long)hcpu;
1543 struct cpufreq_policy *policy;
1544 struct sys_device *sys_dev;
1545
1546 sys_dev = get_cpu_sysdev(cpu);
1547
1548 if (sys_dev) {
1549 switch (action) {
1550 case CPU_ONLINE:
1551 cpufreq_add_dev(sys_dev);
1552 break;
1553 case CPU_DOWN_PREPARE:
1554 /*
1555 * We attempt to put this cpu in lowest frequency
1556 * possible before going down. This will permit
1557 * hardware-managed P-State to switch other related
1558 * threads to min or higher speeds if possible.
1559 */
1560 policy = cpufreq_cpu_data[cpu];
1561 if (policy) {
1562 cpufreq_driver_target(policy, policy->min,
1563 CPUFREQ_RELATION_H);
1564 }
1565 break;
1566 case CPU_DEAD:
1567 cpufreq_remove_dev(sys_dev);
1568 break;
1569 }
1570 }
1571 return NOTIFY_OK;
1572}
1573
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001574static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
Ashok Rajc32b6b82005-10-30 14:59:54 -08001575{
1576 .notifier_call = cpufreq_cpu_callback,
1577};
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001578#endif /* CONFIG_HOTPLUG_CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580/*********************************************************************
1581 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1582 *********************************************************************/
1583
1584/**
1585 * cpufreq_register_driver - register a CPU Frequency driver
1586 * @driver_data: A struct cpufreq_driver containing the values#
1587 * submitted by the CPU Frequency driver.
1588 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001589 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001591 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 *
1593 */
1594int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1595{
1596 unsigned long flags;
1597 int ret;
1598
1599 if (!driver_data || !driver_data->verify || !driver_data->init ||
1600 ((!driver_data->setpolicy) && (!driver_data->target)))
1601 return -EINVAL;
1602
1603 dprintk("trying to register driver %s\n", driver_data->name);
1604
1605 if (driver_data->setpolicy)
1606 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1607
1608 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1609 if (cpufreq_driver) {
1610 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1611 return -EBUSY;
1612 }
1613 cpufreq_driver = driver_data;
1614 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1615
1616 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1617
1618 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1619 int i;
1620 ret = -ENODEV;
1621
1622 /* check for at least one working CPU */
1623 for (i=0; i<NR_CPUS; i++)
1624 if (cpufreq_cpu_data[i])
1625 ret = 0;
1626
1627 /* if all ->init() calls failed, unregister */
1628 if (ret) {
1629 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1630 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1631
1632 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1633 cpufreq_driver = NULL;
1634 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1635 }
1636 }
1637
1638 if (!ret) {
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001639 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 dprintk("driver %s up and running\n", driver_data->name);
1641 cpufreq_debug_enable_ratelimit();
1642 }
1643
1644 return (ret);
1645}
1646EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1647
1648
1649/**
1650 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1651 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001652 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 * the right to do so, i.e. if you have succeeded in initialising before!
1654 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1655 * currently not initialised.
1656 */
1657int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1658{
1659 unsigned long flags;
1660
1661 cpufreq_debug_disable_ratelimit();
1662
1663 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1664 cpufreq_debug_enable_ratelimit();
1665 return -EINVAL;
1666 }
1667
1668 dprintk("unregistering driver %s\n", driver->name);
1669
1670 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001671 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1674 cpufreq_driver = NULL;
1675 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1676
1677 return 0;
1678}
1679EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);