blob: 2caaf71d80c85566bd24d235419f6e09fa097d12 [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);
55static BLOCKING_NOTIFIER_HEAD(cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57
58static LIST_HEAD(cpufreq_governor_list);
Dave Jones7d5e3502006-02-02 17:03:42 -050059static DEFINE_MUTEX (cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Dave Jones7d5e3502006-02-02 17:03:42 -050061struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 struct cpufreq_policy *data;
64 unsigned long flags;
65
66 if (cpu >= NR_CPUS)
67 goto err_out;
68
69 /* get the cpufreq driver */
70 spin_lock_irqsave(&cpufreq_driver_lock, flags);
71
72 if (!cpufreq_driver)
73 goto err_out_unlock;
74
75 if (!try_module_get(cpufreq_driver->owner))
76 goto err_out_unlock;
77
78
79 /* get the CPU */
80 data = cpufreq_cpu_data[cpu];
81
82 if (!data)
83 goto err_out_put_module;
84
85 if (!kobject_get(&data->kobj))
86 goto err_out_put_module;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return data;
90
Dave Jones7d5e3502006-02-02 17:03:42 -050091err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -050093err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -050095err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return NULL;
97}
98EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
99
Dave Jones7d5e3502006-02-02 17:03:42 -0500100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101void cpufreq_cpu_put(struct cpufreq_policy *data)
102{
103 kobject_put(&data->kobj);
104 module_put(cpufreq_driver->owner);
105}
106EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
107
108
109/*********************************************************************
110 * UNIFIED DEBUG HELPERS *
111 *********************************************************************/
112#ifdef CONFIG_CPU_FREQ_DEBUG
113
114/* what part(s) of the CPUfreq subsystem are debugged? */
115static unsigned int debug;
116
117/* is the debug output ratelimit'ed using printk_ratelimit? User can
118 * set or modify this value.
119 */
120static unsigned int debug_ratelimit = 1;
121
122/* is the printk_ratelimit'ing enabled? It's enabled after a successful
123 * loading of a cpufreq driver, temporarily disabled when a new policy
124 * is set, and disabled upon cpufreq driver removal
125 */
126static unsigned int disable_ratelimit = 1;
127static DEFINE_SPINLOCK(disable_ratelimit_lock);
128
Arjan van de Ven858119e2006-01-14 13:20:43 -0800129static void cpufreq_debug_enable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 unsigned long flags;
132
133 spin_lock_irqsave(&disable_ratelimit_lock, flags);
134 if (disable_ratelimit)
135 disable_ratelimit--;
136 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
137}
138
Arjan van de Ven858119e2006-01-14 13:20:43 -0800139static void cpufreq_debug_disable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 unsigned long flags;
142
143 spin_lock_irqsave(&disable_ratelimit_lock, flags);
144 disable_ratelimit++;
145 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
146}
147
148void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
149{
150 char s[256];
151 va_list args;
152 unsigned int len;
153 unsigned long flags;
Dave Jones32ee8c32006-02-28 00:43:23 -0500154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 WARN_ON(!prefix);
156 if (type & debug) {
157 spin_lock_irqsave(&disable_ratelimit_lock, flags);
158 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
159 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
160 return;
161 }
162 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
163
164 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
165
166 va_start(args, fmt);
167 len += vsnprintf(&s[len], (256 - len), fmt, args);
168 va_end(args);
169
170 printk(s);
171
172 WARN_ON(len < 5);
173 }
174}
175EXPORT_SYMBOL(cpufreq_debug_printk);
176
177
178module_param(debug, uint, 0644);
179MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
180
181module_param(debug_ratelimit, uint, 0644);
182MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
183
184#else /* !CONFIG_CPU_FREQ_DEBUG */
185
186static inline void cpufreq_debug_enable_ratelimit(void) { return; }
187static inline void cpufreq_debug_disable_ratelimit(void) { return; }
188
189#endif /* CONFIG_CPU_FREQ_DEBUG */
190
191
192/*********************************************************************
193 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
194 *********************************************************************/
195
196/**
197 * adjust_jiffies - adjust the system "loops_per_jiffy"
198 *
199 * This function alters the system "loops_per_jiffy" for the clock
200 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500201 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * per-CPU loops_per_jiffy value wherever possible.
203 */
204#ifndef CONFIG_SMP
205static unsigned long l_p_j_ref;
206static unsigned int l_p_j_ref_freq;
207
Arjan van de Ven858119e2006-01-14 13:20:43 -0800208static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 if (ci->flags & CPUFREQ_CONST_LOOPS)
211 return;
212
213 if (!l_p_j_ref_freq) {
214 l_p_j_ref = loops_per_jiffy;
215 l_p_j_ref_freq = ci->old;
216 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
217 }
218 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
219 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700220 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
222 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
223 }
224}
225#else
226static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
227#endif
228
229
230/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800231 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
232 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800234 * This function calls the transition notifiers and the "adjust_jiffies"
235 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500236 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
238void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
239{
Dave Jonese4472cb2006-01-31 15:53:55 -0800240 struct cpufreq_policy *policy;
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 BUG_ON(irqs_disabled());
243
244 freqs->flags = cpufreq_driver->flags;
Dave Jonese4472cb2006-01-31 15:53:55 -0800245 dprintk("notification %u of frequency transition to %u kHz\n",
246 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Dave Jonese4472cb2006-01-31 15:53:55 -0800248 policy = cpufreq_cpu_data[freqs->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500252 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800253 * which is not equal to what the cpufreq core thinks is
254 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
256 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800257 if ((policy) && (policy->cpu == freqs->cpu) &&
258 (policy->cur) && (policy->cur != freqs->old)) {
Jan Beulichb10eec22006-04-28 13:47:13 +0200259 dprintk("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800260 " %u, cpufreq assumed %u kHz.\n",
261 freqs->old, policy->cur);
262 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 }
Alan Sterne041c682006-03-27 01:16:30 -0800265 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
266 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
268 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 case CPUFREQ_POSTCHANGE:
271 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Alan Sterne041c682006-03-27 01:16:30 -0800272 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
273 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800274 if (likely(policy) && likely(policy->cpu == freqs->cpu))
275 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
280
281
282
283/*********************************************************************
284 * SYSFS INTERFACE *
285 *********************************************************************/
286
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700287static struct cpufreq_governor *__find_governor(const char *str_governor)
288{
289 struct cpufreq_governor *t;
290
291 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
292 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN))
293 return t;
294
295 return NULL;
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/**
299 * cpufreq_parse_governor - parse a governor string
300 */
301static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
302 struct cpufreq_governor **governor)
303{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700304 int err = -EINVAL;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700307 goto out;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (cpufreq_driver->setpolicy) {
310 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
311 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700312 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
314 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700315 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700317 } else if (cpufreq_driver->target) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700319
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800320 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700321
322 t = __find_governor(str_governor);
323
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700324 if (t == NULL) {
325 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s", str_governor);
326
327 if (name) {
328 int ret;
329
330 mutex_unlock(&cpufreq_governor_mutex);
331 ret = request_module(name);
332 mutex_lock(&cpufreq_governor_mutex);
333
334 if (ret == 0)
335 t = __find_governor(str_governor);
336 }
337
338 kfree(name);
339 }
340
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700341 if (t != NULL) {
342 *governor = t;
343 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700345
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800346 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700348 out:
349 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352
353/* drivers/base/cpu.c */
354extern struct sysdev_class cpu_sysdev_class;
355
356
357/**
358 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
359 *
360 * Write out information from cpufreq_driver->policy[cpu]; object must be
361 * "unsigned int".
362 */
363
Dave Jones32ee8c32006-02-28 00:43:23 -0500364#define show_one(file_name, object) \
365static ssize_t show_##file_name \
366(struct cpufreq_policy * policy, char *buf) \
367{ \
368 return sprintf (buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
371show_one(cpuinfo_min_freq, cpuinfo.min_freq);
372show_one(cpuinfo_max_freq, cpuinfo.max_freq);
373show_one(scaling_min_freq, min);
374show_one(scaling_max_freq, max);
375show_one(scaling_cur_freq, cur);
376
Thomas Renninger7970e082006-04-13 15:14:04 +0200377static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379/**
380 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
381 */
382#define store_one(file_name, object) \
383static ssize_t store_##file_name \
384(struct cpufreq_policy * policy, const char *buf, size_t count) \
385{ \
386 unsigned int ret = -EINVAL; \
387 struct cpufreq_policy new_policy; \
388 \
389 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
390 if (ret) \
391 return -EINVAL; \
392 \
393 ret = sscanf (buf, "%u", &new_policy.object); \
394 if (ret != 1) \
395 return -EINVAL; \
396 \
Arjan van de Ven153d7f32006-07-26 15:40:07 +0200397 lock_cpu_hotplug(); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200398 mutex_lock(&policy->lock); \
399 ret = __cpufreq_set_policy(policy, &new_policy); \
400 policy->user_policy.object = policy->object; \
401 mutex_unlock(&policy->lock); \
Arjan van de Ven153d7f32006-07-26 15:40:07 +0200402 unlock_cpu_hotplug(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 \
404 return ret ? ret : count; \
405}
406
407store_one(scaling_min_freq,min);
408store_one(scaling_max_freq,max);
409
410/**
411 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
412 */
413static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
414{
415 unsigned int cur_freq = cpufreq_get(policy->cpu);
416 if (!cur_freq)
417 return sprintf(buf, "<unknown>");
418 return sprintf(buf, "%u\n", cur_freq);
419}
420
421
422/**
423 * show_scaling_governor - show the current policy for the specified CPU
424 */
425static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
426{
427 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
428 return sprintf(buf, "powersave\n");
429 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
430 return sprintf(buf, "performance\n");
431 else if (policy->governor)
432 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
433 return -EINVAL;
434}
435
436
437/**
438 * store_scaling_governor - store policy for the specified CPU
439 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500440static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
441 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
443 unsigned int ret = -EINVAL;
444 char str_governor[16];
445 struct cpufreq_policy new_policy;
446
447 ret = cpufreq_get_policy(&new_policy, policy->cpu);
448 if (ret)
449 return ret;
450
451 ret = sscanf (buf, "%15s", str_governor);
452 if (ret != 1)
453 return -EINVAL;
454
455 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
456 return -EINVAL;
457
Dave Jonesa496e252006-07-07 12:31:27 -0400458 lock_cpu_hotplug();
459
Thomas Renninger7970e082006-04-13 15:14:04 +0200460 /* Do not use cpufreq_set_policy here or the user_policy.max
461 will be wrongly overridden */
462 mutex_lock(&policy->lock);
463 ret = __cpufreq_set_policy(policy, &new_policy);
464
465 policy->user_policy.policy = policy->policy;
466 policy->user_policy.governor = policy->governor;
467 mutex_unlock(&policy->lock);
468
Dave Jonesa496e252006-07-07 12:31:27 -0400469 unlock_cpu_hotplug();
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 return ret ? ret : count;
472}
473
474/**
475 * show_scaling_driver - show the cpufreq driver currently loaded
476 */
477static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
478{
479 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
480}
481
482/**
483 * show_scaling_available_governors - show the available CPUfreq governors
484 */
485static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
486 char *buf)
487{
488 ssize_t i = 0;
489 struct cpufreq_governor *t;
490
491 if (!cpufreq_driver->target) {
492 i += sprintf(buf, "performance powersave");
493 goto out;
494 }
495
496 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
497 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
498 goto out;
499 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
500 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500501out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 i += sprintf(&buf[i], "\n");
503 return i;
504}
505/**
506 * show_affected_cpus - show the CPUs affected by each transition
507 */
508static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
509{
510 ssize_t i = 0;
511 unsigned int cpu;
512
513 for_each_cpu_mask(cpu, policy->cpus) {
514 if (i)
515 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
516 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
517 if (i >= (PAGE_SIZE - 5))
518 break;
519 }
520 i += sprintf(&buf[i], "\n");
521 return i;
522}
523
524
525#define define_one_ro(_name) \
526static struct freq_attr _name = \
527__ATTR(_name, 0444, show_##_name, NULL)
528
529#define define_one_ro0400(_name) \
530static struct freq_attr _name = \
531__ATTR(_name, 0400, show_##_name, NULL)
532
533#define define_one_rw(_name) \
534static struct freq_attr _name = \
535__ATTR(_name, 0644, show_##_name, store_##_name)
536
537define_one_ro0400(cpuinfo_cur_freq);
538define_one_ro(cpuinfo_min_freq);
539define_one_ro(cpuinfo_max_freq);
540define_one_ro(scaling_available_governors);
541define_one_ro(scaling_driver);
542define_one_ro(scaling_cur_freq);
543define_one_ro(affected_cpus);
544define_one_rw(scaling_min_freq);
545define_one_rw(scaling_max_freq);
546define_one_rw(scaling_governor);
547
548static struct attribute * default_attrs[] = {
549 &cpuinfo_min_freq.attr,
550 &cpuinfo_max_freq.attr,
551 &scaling_min_freq.attr,
552 &scaling_max_freq.attr,
553 &affected_cpus.attr,
554 &scaling_governor.attr,
555 &scaling_driver.attr,
556 &scaling_available_governors.attr,
557 NULL
558};
559
560#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
561#define to_attr(a) container_of(a,struct freq_attr,attr)
562
563static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
564{
565 struct cpufreq_policy * policy = to_policy(kobj);
566 struct freq_attr * fattr = to_attr(attr);
567 ssize_t ret;
568 policy = cpufreq_cpu_get(policy->cpu);
569 if (!policy)
570 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500571 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 cpufreq_cpu_put(policy);
573 return ret;
574}
575
Dave Jones32ee8c32006-02-28 00:43:23 -0500576static ssize_t store(struct kobject * kobj, struct attribute * attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 const char * buf, size_t count)
578{
579 struct cpufreq_policy * policy = to_policy(kobj);
580 struct freq_attr * fattr = to_attr(attr);
581 ssize_t ret;
582 policy = cpufreq_cpu_get(policy->cpu);
583 if (!policy)
584 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500585 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 cpufreq_cpu_put(policy);
587 return ret;
588}
589
590static void cpufreq_sysfs_release(struct kobject * kobj)
591{
592 struct cpufreq_policy * policy = to_policy(kobj);
593 dprintk("last reference is dropped\n");
594 complete(&policy->kobj_unregister);
595}
596
597static struct sysfs_ops sysfs_ops = {
598 .show = show,
599 .store = store,
600};
601
602static struct kobj_type ktype_cpufreq = {
603 .sysfs_ops = &sysfs_ops,
604 .default_attrs = default_attrs,
605 .release = cpufreq_sysfs_release,
606};
607
608
609/**
610 * cpufreq_add_dev - add a CPU device
611 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500612 * Adds the cpufreq interface for a CPU device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
614static int cpufreq_add_dev (struct sys_device * sys_dev)
615{
616 unsigned int cpu = sys_dev->id;
617 int ret = 0;
618 struct cpufreq_policy new_policy;
619 struct cpufreq_policy *policy;
620 struct freq_attr **drv_attr;
Dave Jones8ff69732006-03-05 03:37:23 -0500621 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 unsigned long flags;
623 unsigned int j;
Dave Jones8ff69732006-03-05 03:37:23 -0500624#ifdef CONFIG_SMP
625 struct cpufreq_policy *managed_policy;
626#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Ashok Rajc32b6b82005-10-30 14:59:54 -0800628 if (cpu_is_offline(cpu))
629 return 0;
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 cpufreq_debug_disable_ratelimit();
632 dprintk("adding CPU %u\n", cpu);
633
634#ifdef CONFIG_SMP
635 /* check whether a different CPU already registered this
636 * CPU because it is in the same boat. */
637 policy = cpufreq_cpu_get(cpu);
638 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500639 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 cpufreq_debug_enable_ratelimit();
641 return 0;
642 }
643#endif
644
645 if (!try_module_get(cpufreq_driver->owner)) {
646 ret = -EINVAL;
647 goto module_out;
648 }
649
Dave Jonese98df502005-10-20 15:17:43 -0700650 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (!policy) {
652 ret = -ENOMEM;
653 goto nomem_out;
654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 policy->cpu = cpu;
657 policy->cpus = cpumask_of_cpu(cpu);
658
Arjan van de Ven83933af2006-01-14 16:01:49 +0100659 mutex_init(&policy->lock);
660 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 init_completion(&policy->kobj_unregister);
662 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
663
664 /* call driver. From then on the cpufreq must be able
665 * to accept all calls to ->verify and ->setpolicy for this CPU
666 */
667 ret = cpufreq_driver->init(policy);
668 if (ret) {
669 dprintk("initialization failed\n");
Andrew Mortonf3876c12006-01-18 13:40:54 -0800670 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 goto err_out;
672 }
673
Dave Jones8ff69732006-03-05 03:37:23 -0500674#ifdef CONFIG_SMP
675 for_each_cpu_mask(j, policy->cpus) {
676 if (cpu == j)
677 continue;
678
679 /* check for existing affected CPUs. They may not be aware
680 * of it due to CPU Hotplug.
681 */
682 managed_policy = cpufreq_cpu_get(j);
683 if (unlikely(managed_policy)) {
684 spin_lock_irqsave(&cpufreq_driver_lock, flags);
685 managed_policy->cpus = policy->cpus;
686 cpufreq_cpu_data[cpu] = managed_policy;
687 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
688
689 dprintk("CPU already managed, adding link\n");
690 sysfs_create_link(&sys_dev->kobj,
691 &managed_policy->kobj, "cpufreq");
692
693 cpufreq_debug_enable_ratelimit();
694 mutex_unlock(&policy->lock);
695 ret = 0;
696 goto err_out_driver_exit; /* call driver->exit() */
697 }
698 }
699#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
701
702 /* prepare interface data */
703 policy->kobj.parent = &sys_dev->kobj;
704 policy->kobj.ktype = &ktype_cpufreq;
705 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
706
707 ret = kobject_register(&policy->kobj);
Andrew Mortonf3876c12006-01-18 13:40:54 -0800708 if (ret) {
709 mutex_unlock(&policy->lock);
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700710 goto err_out_driver_exit;
Andrew Mortonf3876c12006-01-18 13:40:54 -0800711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* set up files for this cpu device */
713 drv_attr = cpufreq_driver->attr;
714 while ((drv_attr) && (*drv_attr)) {
715 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
716 drv_attr++;
717 }
718 if (cpufreq_driver->get)
719 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
720 if (cpufreq_driver->target)
721 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
722
723 spin_lock_irqsave(&cpufreq_driver_lock, flags);
724 for_each_cpu_mask(j, policy->cpus)
725 cpufreq_cpu_data[j] = policy;
726 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones8ff69732006-03-05 03:37:23 -0500727
728 /* symlink affected CPUs */
729 for_each_cpu_mask(j, policy->cpus) {
730 if (j == cpu)
731 continue;
732 if (!cpu_online(j))
733 continue;
734
Dave Jones1f8b2c92006-03-29 01:40:04 -0500735 dprintk("CPU %u already managed, adding link\n", j);
Dave Jones8ff69732006-03-05 03:37:23 -0500736 cpufreq_cpu_get(cpu);
737 cpu_sys_dev = get_cpu_sysdev(j);
738 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
739 "cpufreq");
740 }
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 policy->governor = NULL; /* to assure that the starting sequence is
743 * run in cpufreq_set_policy */
Arjan van de Ven83933af2006-01-14 16:01:49 +0100744 mutex_unlock(&policy->lock);
Dave Jones87c32272006-03-29 01:48:37 -0500745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /* set default policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 ret = cpufreq_set_policy(&new_policy);
748 if (ret) {
749 dprintk("setting policy failed\n");
750 goto err_out_unregister;
751 }
752
753 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 dprintk("initialization complete\n");
755 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -0500756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 return 0;
758
759
760err_out_unregister:
761 spin_lock_irqsave(&cpufreq_driver_lock, flags);
762 for_each_cpu_mask(j, policy->cpus)
763 cpufreq_cpu_data[j] = NULL;
764 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
765
766 kobject_unregister(&policy->kobj);
767 wait_for_completion(&policy->kobj_unregister);
768
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700769err_out_driver_exit:
770 if (cpufreq_driver->exit)
771 cpufreq_driver->exit(policy);
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773err_out:
774 kfree(policy);
775
776nomem_out:
777 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800778module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 cpufreq_debug_enable_ratelimit();
780 return ret;
781}
782
783
784/**
785 * cpufreq_remove_dev - remove a CPU device
786 *
787 * Removes the cpufreq interface for a CPU device.
788 */
789static int cpufreq_remove_dev (struct sys_device * sys_dev)
790{
791 unsigned int cpu = sys_dev->id;
792 unsigned long flags;
793 struct cpufreq_policy *data;
794#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -0800795 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 unsigned int j;
797#endif
798
799 cpufreq_debug_disable_ratelimit();
800 dprintk("unregistering CPU %u\n", cpu);
801
802 spin_lock_irqsave(&cpufreq_driver_lock, flags);
803 data = cpufreq_cpu_data[cpu];
804
805 if (!data) {
806 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 cpufreq_debug_enable_ratelimit();
808 return -EINVAL;
809 }
810 cpufreq_cpu_data[cpu] = NULL;
811
812
813#ifdef CONFIG_SMP
814 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -0500815 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
817 if (unlikely(cpu != data->cpu)) {
818 dprintk("removing link\n");
Dave Jones8ff69732006-03-05 03:37:23 -0500819 cpu_clear(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
821 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 cpufreq_cpu_put(data);
823 cpufreq_debug_enable_ratelimit();
824 return 0;
825 }
826#endif
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (!kobject_get(&data->kobj)) {
830 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
831 cpufreq_debug_enable_ratelimit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500832 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
835#ifdef CONFIG_SMP
836 /* if we have other CPUs still registered, we need to unlink them,
837 * or else wait_for_completion below will lock up. Clean the
838 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
839 * links afterwards.
840 */
841 if (unlikely(cpus_weight(data->cpus) > 1)) {
842 for_each_cpu_mask(j, data->cpus) {
843 if (j == cpu)
844 continue;
845 cpufreq_cpu_data[j] = NULL;
846 }
847 }
848
849 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
850
851 if (unlikely(cpus_weight(data->cpus) > 1)) {
852 for_each_cpu_mask(j, data->cpus) {
853 if (j == cpu)
854 continue;
855 dprintk("removing link for cpu %u\n", j);
Ashok Rajd434fca2005-10-30 14:59:52 -0800856 cpu_sys_dev = get_cpu_sysdev(j);
857 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 cpufreq_cpu_put(data);
859 }
860 }
861#else
862 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
863#endif
864
Arjan van de Ven83933af2006-01-14 16:01:49 +0100865 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (cpufreq_driver->target)
867 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Arjan van de Ven83933af2006-01-14 16:01:49 +0100868 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 kobject_unregister(&data->kobj);
871
872 kobject_put(&data->kobj);
873
874 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -0500875 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 * unloading.
877 */
878 dprintk("waiting for dropping of refcount\n");
879 wait_for_completion(&data->kobj_unregister);
880 dprintk("wait complete\n");
881
882 if (cpufreq_driver->exit)
883 cpufreq_driver->exit(data);
884
885 kfree(data);
886
887 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 return 0;
889}
890
891
892static void handle_update(void *data)
893{
894 unsigned int cpu = (unsigned int)(long)data;
895 dprintk("handle_update for cpu %u called\n", cpu);
896 cpufreq_update_policy(cpu);
897}
898
899/**
900 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
901 * @cpu: cpu number
902 * @old_freq: CPU frequency the kernel thinks the CPU runs at
903 * @new_freq: CPU frequency the CPU actually runs at
904 *
905 * We adjust to current frequency first, and need to clean up later. So either call
906 * to cpufreq_update_policy() or schedule handle_update()).
907 */
908static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
909{
910 struct cpufreq_freqs freqs;
911
Jan Beulichb10eec22006-04-28 13:47:13 +0200912 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
914
915 freqs.cpu = cpu;
916 freqs.old = old_freq;
917 freqs.new = new_freq;
918 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
919 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
920}
921
922
Dave Jones32ee8c32006-02-28 00:43:23 -0500923/**
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800924 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
925 * @cpu: CPU number
926 *
927 * This is the last known freq, without actually getting it from the driver.
928 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
929 */
930unsigned int cpufreq_quick_get(unsigned int cpu)
931{
932 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
933 unsigned int ret = 0;
934
935 if (policy) {
Arjan van de Ven83933af2006-01-14 16:01:49 +0100936 mutex_lock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800937 ret = policy->cur;
Arjan van de Ven83933af2006-01-14 16:01:49 +0100938 mutex_unlock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800939 cpufreq_cpu_put(policy);
940 }
941
942 return (ret);
943}
944EXPORT_SYMBOL(cpufreq_quick_get);
945
946
Dave Jones32ee8c32006-02-28 00:43:23 -0500947/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * cpufreq_get - get the current CPU frequency (in kHz)
949 * @cpu: CPU number
950 *
951 * Get the CPU current (static) CPU frequency
952 */
953unsigned int cpufreq_get(unsigned int cpu)
954{
955 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
956 unsigned int ret = 0;
957
958 if (!policy)
959 return 0;
960
961 if (!cpufreq_driver->get)
962 goto out;
963
Arjan van de Ven83933af2006-01-14 16:01:49 +0100964 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 ret = cpufreq_driver->get(cpu);
967
Dave Jones7d5e3502006-02-02 17:03:42 -0500968 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* verify no discrepancy between actual and saved value exists */
970 if (unlikely(ret != policy->cur)) {
971 cpufreq_out_of_sync(cpu, policy->cur, ret);
972 schedule_work(&policy->update);
973 }
974 }
975
Arjan van de Ven83933af2006-01-14 16:01:49 +0100976 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Dave Jones7d5e3502006-02-02 17:03:42 -0500978out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 cpufreq_cpu_put(policy);
980
981 return (ret);
982}
983EXPORT_SYMBOL(cpufreq_get);
984
985
986/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700987 * cpufreq_suspend - let the low level driver prepare for suspend
988 */
989
Bernard Blackhame00d9962005-07-07 17:56:42 -0700990static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700991{
992 int cpu = sysdev->id;
993 unsigned int ret = 0;
994 unsigned int cur_freq = 0;
995 struct cpufreq_policy *cpu_policy;
996
Dave Jones0e37b152006-09-26 23:02:34 -0400997 dprintk("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700998
999 if (!cpu_online(cpu))
1000 return 0;
1001
1002 /* we may be lax here as interrupts are off. Nonetheless
1003 * we need to grab the correct cpu policy, as to check
1004 * whether we really run on this CPU.
1005 */
1006
1007 cpu_policy = cpufreq_cpu_get(cpu);
1008 if (!cpu_policy)
1009 return -EINVAL;
1010
1011 /* only handle each CPU group once */
1012 if (unlikely(cpu_policy->cpu != cpu)) {
1013 cpufreq_cpu_put(cpu_policy);
1014 return 0;
1015 }
1016
1017 if (cpufreq_driver->suspend) {
Bernard Blackhame00d9962005-07-07 17:56:42 -07001018 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001019 if (ret) {
1020 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1021 "step on CPU %u\n", cpu_policy->cpu);
1022 cpufreq_cpu_put(cpu_policy);
1023 return ret;
1024 }
1025 }
1026
1027
1028 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
1029 goto out;
1030
1031 if (cpufreq_driver->get)
1032 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1033
1034 if (!cur_freq || !cpu_policy->cur) {
1035 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1036 "frequency is what timing core thinks it is.\n");
1037 goto out;
1038 }
1039
1040 if (unlikely(cur_freq != cpu_policy->cur)) {
1041 struct cpufreq_freqs freqs;
1042
1043 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001044 dprintk("Warning: CPU frequency is %u, "
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001045 "cpufreq assumed %u kHz.\n",
1046 cur_freq, cpu_policy->cur);
1047
1048 freqs.cpu = cpu;
1049 freqs.old = cpu_policy->cur;
1050 freqs.new = cur_freq;
1051
Alan Sterne041c682006-03-27 01:16:30 -08001052 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001053 CPUFREQ_SUSPENDCHANGE, &freqs);
1054 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1055
1056 cpu_policy->cur = cur_freq;
1057 }
1058
Dave Jones7d5e3502006-02-02 17:03:42 -05001059out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001060 cpufreq_cpu_put(cpu_policy);
1061 return 0;
1062}
1063
1064/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 * cpufreq_resume - restore proper CPU frequency handling after resume
1066 *
1067 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1068 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001069 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1070 * restored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 */
1072static int cpufreq_resume(struct sys_device * sysdev)
1073{
1074 int cpu = sysdev->id;
1075 unsigned int ret = 0;
1076 struct cpufreq_policy *cpu_policy;
1077
1078 dprintk("resuming cpu %u\n", cpu);
1079
1080 if (!cpu_online(cpu))
1081 return 0;
1082
1083 /* we may be lax here as interrupts are off. Nonetheless
1084 * we need to grab the correct cpu policy, as to check
1085 * whether we really run on this CPU.
1086 */
1087
1088 cpu_policy = cpufreq_cpu_get(cpu);
1089 if (!cpu_policy)
1090 return -EINVAL;
1091
1092 /* only handle each CPU group once */
1093 if (unlikely(cpu_policy->cpu != cpu)) {
1094 cpufreq_cpu_put(cpu_policy);
1095 return 0;
1096 }
1097
1098 if (cpufreq_driver->resume) {
1099 ret = cpufreq_driver->resume(cpu_policy);
1100 if (ret) {
1101 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1102 "step on CPU %u\n", cpu_policy->cpu);
1103 cpufreq_cpu_put(cpu_policy);
1104 return ret;
1105 }
1106 }
1107
1108 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1109 unsigned int cur_freq = 0;
1110
1111 if (cpufreq_driver->get)
1112 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1113
1114 if (!cur_freq || !cpu_policy->cur) {
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001115 printk(KERN_ERR "cpufreq: resume failed to assert "
1116 "current frequency is what timing core "
1117 "thinks it is.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 goto out;
1119 }
1120
1121 if (unlikely(cur_freq != cpu_policy->cur)) {
1122 struct cpufreq_freqs freqs;
1123
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001124 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001125 dprintk("Warning: CPU frequency"
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001126 "is %u, cpufreq assumed %u kHz.\n",
1127 cur_freq, cpu_policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 freqs.cpu = cpu;
1130 freqs.old = cpu_policy->cur;
1131 freqs.new = cur_freq;
1132
Alan Sterne041c682006-03-27 01:16:30 -08001133 blocking_notifier_call_chain(
1134 &cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001135 CPUFREQ_RESUMECHANGE, &freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1137
1138 cpu_policy->cur = cur_freq;
1139 }
1140 }
1141
1142out:
1143 schedule_work(&cpu_policy->update);
1144 cpufreq_cpu_put(cpu_policy);
1145 return ret;
1146}
1147
1148static struct sysdev_driver cpufreq_sysdev_driver = {
1149 .add = cpufreq_add_dev,
1150 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001151 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 .resume = cpufreq_resume,
1153};
1154
1155
1156/*********************************************************************
1157 * NOTIFIER LISTS INTERFACE *
1158 *********************************************************************/
1159
1160/**
1161 * cpufreq_register_notifier - register a driver with cpufreq
1162 * @nb: notifier function to register
1163 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1164 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001165 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * are notified about clock rate changes (once before and once after
1167 * the transition), or a list of drivers that are notified about
1168 * changes in cpufreq policy.
1169 *
1170 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001171 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 */
1173int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1174{
1175 int ret;
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 switch (list) {
1178 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001179 ret = blocking_notifier_chain_register(
1180 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 break;
1182 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001183 ret = blocking_notifier_chain_register(
1184 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 break;
1186 default:
1187 ret = -EINVAL;
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 return ret;
1191}
1192EXPORT_SYMBOL(cpufreq_register_notifier);
1193
1194
1195/**
1196 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1197 * @nb: notifier block to be unregistered
1198 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1199 *
1200 * Remove a driver from the CPU frequency notifier list.
1201 *
1202 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001203 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
1205int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1206{
1207 int ret;
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 switch (list) {
1210 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001211 ret = blocking_notifier_chain_unregister(
1212 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 break;
1214 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001215 ret = blocking_notifier_chain_unregister(
1216 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 break;
1218 default:
1219 ret = -EINVAL;
1220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 return ret;
1223}
1224EXPORT_SYMBOL(cpufreq_unregister_notifier);
1225
1226
1227/*********************************************************************
1228 * GOVERNORS *
1229 *********************************************************************/
1230
1231
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001232/* Must be called with lock_cpu_hotplug held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233int __cpufreq_driver_target(struct cpufreq_policy *policy,
1234 unsigned int target_freq,
1235 unsigned int relation)
1236{
1237 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1240 target_freq, relation);
1241 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1242 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return retval;
1245}
1246EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248int cpufreq_driver_target(struct cpufreq_policy *policy,
1249 unsigned int target_freq,
1250 unsigned int relation)
1251{
Dave Jonescc993ca2005-07-28 09:43:56 -07001252 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 policy = cpufreq_cpu_get(policy->cpu);
1255 if (!policy)
1256 return -EINVAL;
1257
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001258 lock_cpu_hotplug();
Arjan van de Ven83933af2006-01-14 16:01:49 +01001259 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 ret = __cpufreq_driver_target(policy, target_freq, relation);
1262
Arjan van de Ven83933af2006-01-14 16:01:49 +01001263 mutex_unlock(&policy->lock);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001264 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return ret;
1268}
1269EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1270
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001271/*
1272 * Locking: Must be called with the lock_cpu_hotplug() lock held
1273 * when "event" is CPUFREQ_GOV_LIMITS
1274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1277{
Dave Jonescc993ca2005-07-28 09:43:56 -07001278 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 if (!try_module_get(policy->governor->owner))
1281 return -EINVAL;
1282
1283 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1284 ret = policy->governor->governor(policy, event);
1285
1286 /* we keep one module reference alive for each CPU governed by this CPU */
1287 if ((event != CPUFREQ_GOV_START) || ret)
1288 module_put(policy->governor->owner);
1289 if ((event == CPUFREQ_GOV_STOP) && !ret)
1290 module_put(policy->governor->owner);
1291
1292 return ret;
1293}
1294
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296int cpufreq_register_governor(struct cpufreq_governor *governor)
1297{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001298 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 if (!governor)
1301 return -EINVAL;
1302
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001303 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001304
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001305 err = -EBUSY;
1306 if (__find_governor(governor->name) == NULL) {
1307 err = 0;
1308 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Dave Jones32ee8c32006-02-28 00:43:23 -05001311 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001312 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1315
1316
1317void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1318{
1319 if (!governor)
1320 return;
1321
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001322 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001324 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return;
1326}
1327EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1328
1329
1330
1331/*********************************************************************
1332 * POLICY INTERFACE *
1333 *********************************************************************/
1334
1335/**
1336 * cpufreq_get_policy - get the current cpufreq_policy
1337 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1338 *
1339 * Reads the current cpufreq policy.
1340 */
1341int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1342{
1343 struct cpufreq_policy *cpu_policy;
1344 if (!policy)
1345 return -EINVAL;
1346
1347 cpu_policy = cpufreq_cpu_get(cpu);
1348 if (!cpu_policy)
1349 return -EINVAL;
1350
Arjan van de Ven83933af2006-01-14 16:01:49 +01001351 mutex_lock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Arjan van de Ven83933af2006-01-14 16:01:49 +01001353 mutex_unlock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 return 0;
1357}
1358EXPORT_SYMBOL(cpufreq_get_policy);
1359
1360
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001361/*
1362 * Locking: Must be called with the lock_cpu_hotplug() lock held
1363 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1365{
1366 int ret = 0;
1367
1368 cpufreq_debug_disable_ratelimit();
1369 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1370 policy->min, policy->max);
1371
Dave Jones7d5e3502006-02-02 17:03:42 -05001372 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001374 if (policy->min > data->min && policy->min > policy->max) {
1375 ret = -EINVAL;
1376 goto error_out;
1377 }
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 /* verify the cpu speed can be set within this limit */
1380 ret = cpufreq_driver->verify(policy);
1381 if (ret)
1382 goto error_out;
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001385 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1386 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001389 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1390 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /* verify the cpu speed can be set within this limit,
1393 which might be different to the first one */
1394 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001395 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001399 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1400 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Dave Jones7d5e3502006-02-02 17:03:42 -05001402 data->min = policy->min;
1403 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1406
1407 if (cpufreq_driver->setpolicy) {
1408 data->policy = policy->policy;
1409 dprintk("setting range\n");
1410 ret = cpufreq_driver->setpolicy(policy);
1411 } else {
1412 if (policy->governor != data->governor) {
1413 /* save old, working values */
1414 struct cpufreq_governor *old_gov = data->governor;
1415
1416 dprintk("governor switch\n");
1417
1418 /* end old governor */
1419 if (data->governor)
1420 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1421
1422 /* start new governor */
1423 data->governor = policy->governor;
1424 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1425 /* new governor failed, so re-start old one */
1426 dprintk("starting governor %s failed\n", data->governor->name);
1427 if (old_gov) {
1428 data->governor = old_gov;
1429 __cpufreq_governor(data, CPUFREQ_GOV_START);
1430 }
1431 ret = -EINVAL;
1432 goto error_out;
1433 }
1434 /* might be a policy change, too, so fall through */
1435 }
1436 dprintk("governor: change or update limits\n");
1437 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1438 }
1439
Dave Jones7d5e3502006-02-02 17:03:42 -05001440error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 cpufreq_debug_enable_ratelimit();
1442 return ret;
1443}
1444
1445/**
1446 * cpufreq_set_policy - set a new CPUFreq policy
1447 * @policy: policy to be set.
1448 *
1449 * Sets a new CPU frequency and voltage scaling policy.
1450 */
1451int cpufreq_set_policy(struct cpufreq_policy *policy)
1452{
1453 int ret = 0;
1454 struct cpufreq_policy *data;
1455
1456 if (!policy)
1457 return -EINVAL;
1458
1459 data = cpufreq_cpu_get(policy->cpu);
1460 if (!data)
1461 return -EINVAL;
1462
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001463 lock_cpu_hotplug();
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /* lock this CPU */
Arjan van de Ven83933af2006-01-14 16:01:49 +01001466 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 ret = __cpufreq_set_policy(data, policy);
1469 data->user_policy.min = data->min;
1470 data->user_policy.max = data->max;
1471 data->user_policy.policy = data->policy;
1472 data->user_policy.governor = data->governor;
1473
Arjan van de Ven83933af2006-01-14 16:01:49 +01001474 mutex_unlock(&data->lock);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001475
1476 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 cpufreq_cpu_put(data);
1478
1479 return ret;
1480}
1481EXPORT_SYMBOL(cpufreq_set_policy);
1482
1483
1484/**
1485 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1486 * @cpu: CPU which shall be re-evaluated
1487 *
1488 * Usefull for policy notifiers which have different necessities
1489 * at different times.
1490 */
1491int cpufreq_update_policy(unsigned int cpu)
1492{
1493 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1494 struct cpufreq_policy policy;
1495 int ret = 0;
1496
1497 if (!data)
1498 return -ENODEV;
1499
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001500 lock_cpu_hotplug();
Arjan van de Ven83933af2006-01-14 16:01:49 +01001501 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001504 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 policy.min = data->user_policy.min;
1506 policy.max = data->user_policy.max;
1507 policy.policy = data->user_policy.policy;
1508 policy.governor = data->user_policy.governor;
1509
Thomas Renninger0961dd02006-01-26 18:46:33 +01001510 /* BIOS might change freq behind our back
1511 -> ask driver for current freq and notify governors about a change */
1512 if (cpufreq_driver->get) {
1513 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001514 if (!data->cur) {
1515 dprintk("Driver did not initialize current freq");
1516 data->cur = policy.cur;
1517 } else {
1518 if (data->cur != policy.cur)
1519 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1520 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001521 }
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 ret = __cpufreq_set_policy(data, &policy);
1524
Arjan van de Ven83933af2006-01-14 16:01:49 +01001525 mutex_unlock(&data->lock);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001526 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 cpufreq_cpu_put(data);
1528 return ret;
1529}
1530EXPORT_SYMBOL(cpufreq_update_policy);
1531
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001532#ifdef CONFIG_HOTPLUG_CPU
1533static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001534 unsigned long action, void *hcpu)
1535{
1536 unsigned int cpu = (unsigned long)hcpu;
1537 struct cpufreq_policy *policy;
1538 struct sys_device *sys_dev;
1539
1540 sys_dev = get_cpu_sysdev(cpu);
1541
1542 if (sys_dev) {
1543 switch (action) {
1544 case CPU_ONLINE:
1545 cpufreq_add_dev(sys_dev);
1546 break;
1547 case CPU_DOWN_PREPARE:
1548 /*
1549 * We attempt to put this cpu in lowest frequency
1550 * possible before going down. This will permit
1551 * hardware-managed P-State to switch other related
1552 * threads to min or higher speeds if possible.
1553 */
1554 policy = cpufreq_cpu_data[cpu];
1555 if (policy) {
1556 cpufreq_driver_target(policy, policy->min,
1557 CPUFREQ_RELATION_H);
1558 }
1559 break;
1560 case CPU_DEAD:
1561 cpufreq_remove_dev(sys_dev);
1562 break;
1563 }
1564 }
1565 return NOTIFY_OK;
1566}
1567
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001568static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
Ashok Rajc32b6b82005-10-30 14:59:54 -08001569{
1570 .notifier_call = cpufreq_cpu_callback,
1571};
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001572#endif /* CONFIG_HOTPLUG_CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574/*********************************************************************
1575 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1576 *********************************************************************/
1577
1578/**
1579 * cpufreq_register_driver - register a CPU Frequency driver
1580 * @driver_data: A struct cpufreq_driver containing the values#
1581 * submitted by the CPU Frequency driver.
1582 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001583 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001585 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 *
1587 */
1588int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1589{
1590 unsigned long flags;
1591 int ret;
1592
1593 if (!driver_data || !driver_data->verify || !driver_data->init ||
1594 ((!driver_data->setpolicy) && (!driver_data->target)))
1595 return -EINVAL;
1596
1597 dprintk("trying to register driver %s\n", driver_data->name);
1598
1599 if (driver_data->setpolicy)
1600 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1601
1602 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1603 if (cpufreq_driver) {
1604 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1605 return -EBUSY;
1606 }
1607 cpufreq_driver = driver_data;
1608 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1609
1610 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1611
1612 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1613 int i;
1614 ret = -ENODEV;
1615
1616 /* check for at least one working CPU */
1617 for (i=0; i<NR_CPUS; i++)
1618 if (cpufreq_cpu_data[i])
1619 ret = 0;
1620
1621 /* if all ->init() calls failed, unregister */
1622 if (ret) {
1623 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1624 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1625
1626 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1627 cpufreq_driver = NULL;
1628 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1629 }
1630 }
1631
1632 if (!ret) {
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001633 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 dprintk("driver %s up and running\n", driver_data->name);
1635 cpufreq_debug_enable_ratelimit();
1636 }
1637
1638 return (ret);
1639}
1640EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1641
1642
1643/**
1644 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1645 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001646 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 * the right to do so, i.e. if you have succeeded in initialising before!
1648 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1649 * currently not initialised.
1650 */
1651int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1652{
1653 unsigned long flags;
1654
1655 cpufreq_debug_disable_ratelimit();
1656
1657 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1658 cpufreq_debug_enable_ratelimit();
1659 return -EINVAL;
1660 }
1661
1662 dprintk("unregistering driver %s\n", driver->name);
1663
1664 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001665 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1668 cpufreq_driver = NULL;
1669 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1670
1671 return 0;
1672}
1673EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);