blob: 9b416372a8e426d94fe6248a36b46b56669d6a3c [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/**
35 * The "cpufreq driver" - the arch- or hardware-dependend low
36 * 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
324 if (t != NULL) {
325 *governor = t;
326 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700328
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800329 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700331 out:
332 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335
336/* drivers/base/cpu.c */
337extern struct sysdev_class cpu_sysdev_class;
338
339
340/**
341 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
342 *
343 * Write out information from cpufreq_driver->policy[cpu]; object must be
344 * "unsigned int".
345 */
346
Dave Jones32ee8c32006-02-28 00:43:23 -0500347#define show_one(file_name, object) \
348static ssize_t show_##file_name \
349(struct cpufreq_policy * policy, char *buf) \
350{ \
351 return sprintf (buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354show_one(cpuinfo_min_freq, cpuinfo.min_freq);
355show_one(cpuinfo_max_freq, cpuinfo.max_freq);
356show_one(scaling_min_freq, min);
357show_one(scaling_max_freq, max);
358show_one(scaling_cur_freq, cur);
359
Thomas Renninger7970e082006-04-13 15:14:04 +0200360static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362/**
363 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
364 */
365#define store_one(file_name, object) \
366static ssize_t store_##file_name \
367(struct cpufreq_policy * policy, const char *buf, size_t count) \
368{ \
369 unsigned int ret = -EINVAL; \
370 struct cpufreq_policy new_policy; \
371 \
372 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
373 if (ret) \
374 return -EINVAL; \
375 \
376 ret = sscanf (buf, "%u", &new_policy.object); \
377 if (ret != 1) \
378 return -EINVAL; \
379 \
Arjan van de Ven153d7f32006-07-26 15:40:07 +0200380 lock_cpu_hotplug(); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200381 mutex_lock(&policy->lock); \
382 ret = __cpufreq_set_policy(policy, &new_policy); \
383 policy->user_policy.object = policy->object; \
384 mutex_unlock(&policy->lock); \
Arjan van de Ven153d7f32006-07-26 15:40:07 +0200385 unlock_cpu_hotplug(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 \
387 return ret ? ret : count; \
388}
389
390store_one(scaling_min_freq,min);
391store_one(scaling_max_freq,max);
392
393/**
394 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
395 */
396static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
397{
398 unsigned int cur_freq = cpufreq_get(policy->cpu);
399 if (!cur_freq)
400 return sprintf(buf, "<unknown>");
401 return sprintf(buf, "%u\n", cur_freq);
402}
403
404
405/**
406 * show_scaling_governor - show the current policy for the specified CPU
407 */
408static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
409{
410 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
411 return sprintf(buf, "powersave\n");
412 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
413 return sprintf(buf, "performance\n");
414 else if (policy->governor)
415 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
416 return -EINVAL;
417}
418
419
420/**
421 * store_scaling_governor - store policy for the specified CPU
422 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500423static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
424 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
426 unsigned int ret = -EINVAL;
427 char str_governor[16];
428 struct cpufreq_policy new_policy;
429
430 ret = cpufreq_get_policy(&new_policy, policy->cpu);
431 if (ret)
432 return ret;
433
434 ret = sscanf (buf, "%15s", str_governor);
435 if (ret != 1)
436 return -EINVAL;
437
438 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
439 return -EINVAL;
440
Dave Jonesa496e252006-07-07 12:31:27 -0400441 lock_cpu_hotplug();
442
Thomas Renninger7970e082006-04-13 15:14:04 +0200443 /* Do not use cpufreq_set_policy here or the user_policy.max
444 will be wrongly overridden */
445 mutex_lock(&policy->lock);
446 ret = __cpufreq_set_policy(policy, &new_policy);
447
448 policy->user_policy.policy = policy->policy;
449 policy->user_policy.governor = policy->governor;
450 mutex_unlock(&policy->lock);
451
Dave Jonesa496e252006-07-07 12:31:27 -0400452 unlock_cpu_hotplug();
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return ret ? ret : count;
455}
456
457/**
458 * show_scaling_driver - show the cpufreq driver currently loaded
459 */
460static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
461{
462 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
463}
464
465/**
466 * show_scaling_available_governors - show the available CPUfreq governors
467 */
468static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
469 char *buf)
470{
471 ssize_t i = 0;
472 struct cpufreq_governor *t;
473
474 if (!cpufreq_driver->target) {
475 i += sprintf(buf, "performance powersave");
476 goto out;
477 }
478
479 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
480 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
481 goto out;
482 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
483 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500484out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 i += sprintf(&buf[i], "\n");
486 return i;
487}
488/**
489 * show_affected_cpus - show the CPUs affected by each transition
490 */
491static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
492{
493 ssize_t i = 0;
494 unsigned int cpu;
495
496 for_each_cpu_mask(cpu, policy->cpus) {
497 if (i)
498 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
499 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
500 if (i >= (PAGE_SIZE - 5))
501 break;
502 }
503 i += sprintf(&buf[i], "\n");
504 return i;
505}
506
507
508#define define_one_ro(_name) \
509static struct freq_attr _name = \
510__ATTR(_name, 0444, show_##_name, NULL)
511
512#define define_one_ro0400(_name) \
513static struct freq_attr _name = \
514__ATTR(_name, 0400, show_##_name, NULL)
515
516#define define_one_rw(_name) \
517static struct freq_attr _name = \
518__ATTR(_name, 0644, show_##_name, store_##_name)
519
520define_one_ro0400(cpuinfo_cur_freq);
521define_one_ro(cpuinfo_min_freq);
522define_one_ro(cpuinfo_max_freq);
523define_one_ro(scaling_available_governors);
524define_one_ro(scaling_driver);
525define_one_ro(scaling_cur_freq);
526define_one_ro(affected_cpus);
527define_one_rw(scaling_min_freq);
528define_one_rw(scaling_max_freq);
529define_one_rw(scaling_governor);
530
531static struct attribute * default_attrs[] = {
532 &cpuinfo_min_freq.attr,
533 &cpuinfo_max_freq.attr,
534 &scaling_min_freq.attr,
535 &scaling_max_freq.attr,
536 &affected_cpus.attr,
537 &scaling_governor.attr,
538 &scaling_driver.attr,
539 &scaling_available_governors.attr,
540 NULL
541};
542
543#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
544#define to_attr(a) container_of(a,struct freq_attr,attr)
545
546static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
547{
548 struct cpufreq_policy * policy = to_policy(kobj);
549 struct freq_attr * fattr = to_attr(attr);
550 ssize_t ret;
551 policy = cpufreq_cpu_get(policy->cpu);
552 if (!policy)
553 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500554 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 cpufreq_cpu_put(policy);
556 return ret;
557}
558
Dave Jones32ee8c32006-02-28 00:43:23 -0500559static ssize_t store(struct kobject * kobj, struct attribute * attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 const char * buf, size_t count)
561{
562 struct cpufreq_policy * policy = to_policy(kobj);
563 struct freq_attr * fattr = to_attr(attr);
564 ssize_t ret;
565 policy = cpufreq_cpu_get(policy->cpu);
566 if (!policy)
567 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500568 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 cpufreq_cpu_put(policy);
570 return ret;
571}
572
573static void cpufreq_sysfs_release(struct kobject * kobj)
574{
575 struct cpufreq_policy * policy = to_policy(kobj);
576 dprintk("last reference is dropped\n");
577 complete(&policy->kobj_unregister);
578}
579
580static struct sysfs_ops sysfs_ops = {
581 .show = show,
582 .store = store,
583};
584
585static struct kobj_type ktype_cpufreq = {
586 .sysfs_ops = &sysfs_ops,
587 .default_attrs = default_attrs,
588 .release = cpufreq_sysfs_release,
589};
590
591
592/**
593 * cpufreq_add_dev - add a CPU device
594 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500595 * Adds the cpufreq interface for a CPU device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
597static int cpufreq_add_dev (struct sys_device * sys_dev)
598{
599 unsigned int cpu = sys_dev->id;
600 int ret = 0;
601 struct cpufreq_policy new_policy;
602 struct cpufreq_policy *policy;
603 struct freq_attr **drv_attr;
Dave Jones8ff69732006-03-05 03:37:23 -0500604 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 unsigned long flags;
606 unsigned int j;
Dave Jones8ff69732006-03-05 03:37:23 -0500607#ifdef CONFIG_SMP
608 struct cpufreq_policy *managed_policy;
609#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Ashok Rajc32b6b82005-10-30 14:59:54 -0800611 if (cpu_is_offline(cpu))
612 return 0;
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 cpufreq_debug_disable_ratelimit();
615 dprintk("adding CPU %u\n", cpu);
616
617#ifdef CONFIG_SMP
618 /* check whether a different CPU already registered this
619 * CPU because it is in the same boat. */
620 policy = cpufreq_cpu_get(cpu);
621 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500622 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 cpufreq_debug_enable_ratelimit();
624 return 0;
625 }
626#endif
627
628 if (!try_module_get(cpufreq_driver->owner)) {
629 ret = -EINVAL;
630 goto module_out;
631 }
632
Dave Jonese98df502005-10-20 15:17:43 -0700633 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 if (!policy) {
635 ret = -ENOMEM;
636 goto nomem_out;
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 policy->cpu = cpu;
640 policy->cpus = cpumask_of_cpu(cpu);
641
Arjan van de Ven83933af2006-01-14 16:01:49 +0100642 mutex_init(&policy->lock);
643 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 init_completion(&policy->kobj_unregister);
645 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
646
647 /* call driver. From then on the cpufreq must be able
648 * to accept all calls to ->verify and ->setpolicy for this CPU
649 */
650 ret = cpufreq_driver->init(policy);
651 if (ret) {
652 dprintk("initialization failed\n");
Andrew Mortonf3876c12006-01-18 13:40:54 -0800653 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 goto err_out;
655 }
656
Dave Jones8ff69732006-03-05 03:37:23 -0500657#ifdef CONFIG_SMP
658 for_each_cpu_mask(j, policy->cpus) {
659 if (cpu == j)
660 continue;
661
662 /* check for existing affected CPUs. They may not be aware
663 * of it due to CPU Hotplug.
664 */
665 managed_policy = cpufreq_cpu_get(j);
666 if (unlikely(managed_policy)) {
667 spin_lock_irqsave(&cpufreq_driver_lock, flags);
668 managed_policy->cpus = policy->cpus;
669 cpufreq_cpu_data[cpu] = managed_policy;
670 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
671
672 dprintk("CPU already managed, adding link\n");
673 sysfs_create_link(&sys_dev->kobj,
674 &managed_policy->kobj, "cpufreq");
675
676 cpufreq_debug_enable_ratelimit();
677 mutex_unlock(&policy->lock);
678 ret = 0;
679 goto err_out_driver_exit; /* call driver->exit() */
680 }
681 }
682#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
684
685 /* prepare interface data */
686 policy->kobj.parent = &sys_dev->kobj;
687 policy->kobj.ktype = &ktype_cpufreq;
688 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
689
690 ret = kobject_register(&policy->kobj);
Andrew Mortonf3876c12006-01-18 13:40:54 -0800691 if (ret) {
692 mutex_unlock(&policy->lock);
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700693 goto err_out_driver_exit;
Andrew Mortonf3876c12006-01-18 13:40:54 -0800694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /* set up files for this cpu device */
696 drv_attr = cpufreq_driver->attr;
697 while ((drv_attr) && (*drv_attr)) {
698 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
699 drv_attr++;
700 }
701 if (cpufreq_driver->get)
702 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
703 if (cpufreq_driver->target)
704 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
705
706 spin_lock_irqsave(&cpufreq_driver_lock, flags);
707 for_each_cpu_mask(j, policy->cpus)
708 cpufreq_cpu_data[j] = policy;
709 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones8ff69732006-03-05 03:37:23 -0500710
711 /* symlink affected CPUs */
712 for_each_cpu_mask(j, policy->cpus) {
713 if (j == cpu)
714 continue;
715 if (!cpu_online(j))
716 continue;
717
Dave Jones1f8b2c92006-03-29 01:40:04 -0500718 dprintk("CPU %u already managed, adding link\n", j);
Dave Jones8ff69732006-03-05 03:37:23 -0500719 cpufreq_cpu_get(cpu);
720 cpu_sys_dev = get_cpu_sysdev(j);
721 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
722 "cpufreq");
723 }
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 policy->governor = NULL; /* to assure that the starting sequence is
726 * run in cpufreq_set_policy */
Arjan van de Ven83933af2006-01-14 16:01:49 +0100727 mutex_unlock(&policy->lock);
Dave Jones87c32272006-03-29 01:48:37 -0500728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* set default policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ret = cpufreq_set_policy(&new_policy);
731 if (ret) {
732 dprintk("setting policy failed\n");
733 goto err_out_unregister;
734 }
735
736 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 dprintk("initialization complete\n");
738 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -0500739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return 0;
741
742
743err_out_unregister:
744 spin_lock_irqsave(&cpufreq_driver_lock, flags);
745 for_each_cpu_mask(j, policy->cpus)
746 cpufreq_cpu_data[j] = NULL;
747 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
748
749 kobject_unregister(&policy->kobj);
750 wait_for_completion(&policy->kobj_unregister);
751
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700752err_out_driver_exit:
753 if (cpufreq_driver->exit)
754 cpufreq_driver->exit(policy);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756err_out:
757 kfree(policy);
758
759nomem_out:
760 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800761module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 cpufreq_debug_enable_ratelimit();
763 return ret;
764}
765
766
767/**
768 * cpufreq_remove_dev - remove a CPU device
769 *
770 * Removes the cpufreq interface for a CPU device.
771 */
772static int cpufreq_remove_dev (struct sys_device * sys_dev)
773{
774 unsigned int cpu = sys_dev->id;
775 unsigned long flags;
776 struct cpufreq_policy *data;
777#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -0800778 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 unsigned int j;
780#endif
781
782 cpufreq_debug_disable_ratelimit();
783 dprintk("unregistering CPU %u\n", cpu);
784
785 spin_lock_irqsave(&cpufreq_driver_lock, flags);
786 data = cpufreq_cpu_data[cpu];
787
788 if (!data) {
789 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 cpufreq_debug_enable_ratelimit();
791 return -EINVAL;
792 }
793 cpufreq_cpu_data[cpu] = NULL;
794
795
796#ifdef CONFIG_SMP
797 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -0500798 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
800 if (unlikely(cpu != data->cpu)) {
801 dprintk("removing link\n");
Dave Jones8ff69732006-03-05 03:37:23 -0500802 cpu_clear(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
804 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 cpufreq_cpu_put(data);
806 cpufreq_debug_enable_ratelimit();
807 return 0;
808 }
809#endif
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (!kobject_get(&data->kobj)) {
813 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
814 cpufreq_debug_enable_ratelimit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500815 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817
818#ifdef CONFIG_SMP
819 /* if we have other CPUs still registered, we need to unlink them,
820 * or else wait_for_completion below will lock up. Clean the
821 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
822 * links afterwards.
823 */
824 if (unlikely(cpus_weight(data->cpus) > 1)) {
825 for_each_cpu_mask(j, data->cpus) {
826 if (j == cpu)
827 continue;
828 cpufreq_cpu_data[j] = NULL;
829 }
830 }
831
832 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
833
834 if (unlikely(cpus_weight(data->cpus) > 1)) {
835 for_each_cpu_mask(j, data->cpus) {
836 if (j == cpu)
837 continue;
838 dprintk("removing link for cpu %u\n", j);
Ashok Rajd434fca2005-10-30 14:59:52 -0800839 cpu_sys_dev = get_cpu_sysdev(j);
840 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 cpufreq_cpu_put(data);
842 }
843 }
844#else
845 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
846#endif
847
Arjan van de Ven83933af2006-01-14 16:01:49 +0100848 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (cpufreq_driver->target)
850 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Arjan van de Ven83933af2006-01-14 16:01:49 +0100851 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 kobject_unregister(&data->kobj);
854
855 kobject_put(&data->kobj);
856
857 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -0500858 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * unloading.
860 */
861 dprintk("waiting for dropping of refcount\n");
862 wait_for_completion(&data->kobj_unregister);
863 dprintk("wait complete\n");
864
865 if (cpufreq_driver->exit)
866 cpufreq_driver->exit(data);
867
868 kfree(data);
869
870 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 0;
872}
873
874
875static void handle_update(void *data)
876{
877 unsigned int cpu = (unsigned int)(long)data;
878 dprintk("handle_update for cpu %u called\n", cpu);
879 cpufreq_update_policy(cpu);
880}
881
882/**
883 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
884 * @cpu: cpu number
885 * @old_freq: CPU frequency the kernel thinks the CPU runs at
886 * @new_freq: CPU frequency the CPU actually runs at
887 *
888 * We adjust to current frequency first, and need to clean up later. So either call
889 * to cpufreq_update_policy() or schedule handle_update()).
890 */
891static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
892{
893 struct cpufreq_freqs freqs;
894
Jan Beulichb10eec22006-04-28 13:47:13 +0200895 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
897
898 freqs.cpu = cpu;
899 freqs.old = old_freq;
900 freqs.new = new_freq;
901 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
902 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
903}
904
905
Dave Jones32ee8c32006-02-28 00:43:23 -0500906/**
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800907 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
908 * @cpu: CPU number
909 *
910 * This is the last known freq, without actually getting it from the driver.
911 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
912 */
913unsigned int cpufreq_quick_get(unsigned int cpu)
914{
915 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
916 unsigned int ret = 0;
917
918 if (policy) {
Arjan van de Ven83933af2006-01-14 16:01:49 +0100919 mutex_lock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800920 ret = policy->cur;
Arjan van de Ven83933af2006-01-14 16:01:49 +0100921 mutex_unlock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800922 cpufreq_cpu_put(policy);
923 }
924
925 return (ret);
926}
927EXPORT_SYMBOL(cpufreq_quick_get);
928
929
Dave Jones32ee8c32006-02-28 00:43:23 -0500930/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 * cpufreq_get - get the current CPU frequency (in kHz)
932 * @cpu: CPU number
933 *
934 * Get the CPU current (static) CPU frequency
935 */
936unsigned int cpufreq_get(unsigned int cpu)
937{
938 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
939 unsigned int ret = 0;
940
941 if (!policy)
942 return 0;
943
944 if (!cpufreq_driver->get)
945 goto out;
946
Arjan van de Ven83933af2006-01-14 16:01:49 +0100947 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 ret = cpufreq_driver->get(cpu);
950
Dave Jones7d5e3502006-02-02 17:03:42 -0500951 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /* verify no discrepancy between actual and saved value exists */
953 if (unlikely(ret != policy->cur)) {
954 cpufreq_out_of_sync(cpu, policy->cur, ret);
955 schedule_work(&policy->update);
956 }
957 }
958
Arjan van de Ven83933af2006-01-14 16:01:49 +0100959 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Dave Jones7d5e3502006-02-02 17:03:42 -0500961out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 cpufreq_cpu_put(policy);
963
964 return (ret);
965}
966EXPORT_SYMBOL(cpufreq_get);
967
968
969/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700970 * cpufreq_suspend - let the low level driver prepare for suspend
971 */
972
Bernard Blackhame00d9962005-07-07 17:56:42 -0700973static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700974{
975 int cpu = sysdev->id;
976 unsigned int ret = 0;
977 unsigned int cur_freq = 0;
978 struct cpufreq_policy *cpu_policy;
979
980 dprintk("resuming cpu %u\n", cpu);
981
982 if (!cpu_online(cpu))
983 return 0;
984
985 /* we may be lax here as interrupts are off. Nonetheless
986 * we need to grab the correct cpu policy, as to check
987 * whether we really run on this CPU.
988 */
989
990 cpu_policy = cpufreq_cpu_get(cpu);
991 if (!cpu_policy)
992 return -EINVAL;
993
994 /* only handle each CPU group once */
995 if (unlikely(cpu_policy->cpu != cpu)) {
996 cpufreq_cpu_put(cpu_policy);
997 return 0;
998 }
999
1000 if (cpufreq_driver->suspend) {
Bernard Blackhame00d9962005-07-07 17:56:42 -07001001 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001002 if (ret) {
1003 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1004 "step on CPU %u\n", cpu_policy->cpu);
1005 cpufreq_cpu_put(cpu_policy);
1006 return ret;
1007 }
1008 }
1009
1010
1011 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
1012 goto out;
1013
1014 if (cpufreq_driver->get)
1015 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1016
1017 if (!cur_freq || !cpu_policy->cur) {
1018 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1019 "frequency is what timing core thinks it is.\n");
1020 goto out;
1021 }
1022
1023 if (unlikely(cur_freq != cpu_policy->cur)) {
1024 struct cpufreq_freqs freqs;
1025
1026 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001027 dprintk("Warning: CPU frequency is %u, "
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001028 "cpufreq assumed %u kHz.\n",
1029 cur_freq, cpu_policy->cur);
1030
1031 freqs.cpu = cpu;
1032 freqs.old = cpu_policy->cur;
1033 freqs.new = cur_freq;
1034
Alan Sterne041c682006-03-27 01:16:30 -08001035 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001036 CPUFREQ_SUSPENDCHANGE, &freqs);
1037 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1038
1039 cpu_policy->cur = cur_freq;
1040 }
1041
Dave Jones7d5e3502006-02-02 17:03:42 -05001042out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001043 cpufreq_cpu_put(cpu_policy);
1044 return 0;
1045}
1046
1047/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 * cpufreq_resume - restore proper CPU frequency handling after resume
1049 *
1050 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1051 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001052 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1053 * restored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 */
1055static int cpufreq_resume(struct sys_device * sysdev)
1056{
1057 int cpu = sysdev->id;
1058 unsigned int ret = 0;
1059 struct cpufreq_policy *cpu_policy;
1060
1061 dprintk("resuming cpu %u\n", cpu);
1062
1063 if (!cpu_online(cpu))
1064 return 0;
1065
1066 /* we may be lax here as interrupts are off. Nonetheless
1067 * we need to grab the correct cpu policy, as to check
1068 * whether we really run on this CPU.
1069 */
1070
1071 cpu_policy = cpufreq_cpu_get(cpu);
1072 if (!cpu_policy)
1073 return -EINVAL;
1074
1075 /* only handle each CPU group once */
1076 if (unlikely(cpu_policy->cpu != cpu)) {
1077 cpufreq_cpu_put(cpu_policy);
1078 return 0;
1079 }
1080
1081 if (cpufreq_driver->resume) {
1082 ret = cpufreq_driver->resume(cpu_policy);
1083 if (ret) {
1084 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1085 "step on CPU %u\n", cpu_policy->cpu);
1086 cpufreq_cpu_put(cpu_policy);
1087 return ret;
1088 }
1089 }
1090
1091 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1092 unsigned int cur_freq = 0;
1093
1094 if (cpufreq_driver->get)
1095 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1096
1097 if (!cur_freq || !cpu_policy->cur) {
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001098 printk(KERN_ERR "cpufreq: resume failed to assert "
1099 "current frequency is what timing core "
1100 "thinks it is.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 goto out;
1102 }
1103
1104 if (unlikely(cur_freq != cpu_policy->cur)) {
1105 struct cpufreq_freqs freqs;
1106
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001107 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001108 dprintk("Warning: CPU frequency"
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001109 "is %u, cpufreq assumed %u kHz.\n",
1110 cur_freq, cpu_policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 freqs.cpu = cpu;
1113 freqs.old = cpu_policy->cur;
1114 freqs.new = cur_freq;
1115
Alan Sterne041c682006-03-27 01:16:30 -08001116 blocking_notifier_call_chain(
1117 &cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001118 CPUFREQ_RESUMECHANGE, &freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1120
1121 cpu_policy->cur = cur_freq;
1122 }
1123 }
1124
1125out:
1126 schedule_work(&cpu_policy->update);
1127 cpufreq_cpu_put(cpu_policy);
1128 return ret;
1129}
1130
1131static struct sysdev_driver cpufreq_sysdev_driver = {
1132 .add = cpufreq_add_dev,
1133 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001134 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 .resume = cpufreq_resume,
1136};
1137
1138
1139/*********************************************************************
1140 * NOTIFIER LISTS INTERFACE *
1141 *********************************************************************/
1142
1143/**
1144 * cpufreq_register_notifier - register a driver with cpufreq
1145 * @nb: notifier function to register
1146 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1147 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001148 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 * are notified about clock rate changes (once before and once after
1150 * the transition), or a list of drivers that are notified about
1151 * changes in cpufreq policy.
1152 *
1153 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001154 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
1156int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1157{
1158 int ret;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 switch (list) {
1161 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001162 ret = blocking_notifier_chain_register(
1163 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 break;
1165 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001166 ret = blocking_notifier_chain_register(
1167 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 break;
1169 default:
1170 ret = -EINVAL;
1171 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 return ret;
1174}
1175EXPORT_SYMBOL(cpufreq_register_notifier);
1176
1177
1178/**
1179 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1180 * @nb: notifier block to be unregistered
1181 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1182 *
1183 * Remove a driver from the CPU frequency notifier list.
1184 *
1185 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001186 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 */
1188int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1189{
1190 int ret;
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 switch (list) {
1193 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001194 ret = blocking_notifier_chain_unregister(
1195 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 break;
1197 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001198 ret = blocking_notifier_chain_unregister(
1199 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 break;
1201 default:
1202 ret = -EINVAL;
1203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 return ret;
1206}
1207EXPORT_SYMBOL(cpufreq_unregister_notifier);
1208
1209
1210/*********************************************************************
1211 * GOVERNORS *
1212 *********************************************************************/
1213
1214
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001215/* Must be called with lock_cpu_hotplug held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216int __cpufreq_driver_target(struct cpufreq_policy *policy,
1217 unsigned int target_freq,
1218 unsigned int relation)
1219{
1220 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1223 target_freq, relation);
1224 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1225 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return retval;
1228}
1229EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231int cpufreq_driver_target(struct cpufreq_policy *policy,
1232 unsigned int target_freq,
1233 unsigned int relation)
1234{
Dave Jonescc993ca2005-07-28 09:43:56 -07001235 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 policy = cpufreq_cpu_get(policy->cpu);
1238 if (!policy)
1239 return -EINVAL;
1240
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001241 lock_cpu_hotplug();
Arjan van de Ven83933af2006-01-14 16:01:49 +01001242 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 ret = __cpufreq_driver_target(policy, target_freq, relation);
1245
Arjan van de Ven83933af2006-01-14 16:01:49 +01001246 mutex_unlock(&policy->lock);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001247 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return ret;
1251}
1252EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1253
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001254/*
1255 * Locking: Must be called with the lock_cpu_hotplug() lock held
1256 * when "event" is CPUFREQ_GOV_LIMITS
1257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1260{
Dave Jonescc993ca2005-07-28 09:43:56 -07001261 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 if (!try_module_get(policy->governor->owner))
1264 return -EINVAL;
1265
1266 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1267 ret = policy->governor->governor(policy, event);
1268
1269 /* we keep one module reference alive for each CPU governed by this CPU */
1270 if ((event != CPUFREQ_GOV_START) || ret)
1271 module_put(policy->governor->owner);
1272 if ((event == CPUFREQ_GOV_STOP) && !ret)
1273 module_put(policy->governor->owner);
1274
1275 return ret;
1276}
1277
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279int cpufreq_register_governor(struct cpufreq_governor *governor)
1280{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001281 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 if (!governor)
1284 return -EINVAL;
1285
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001286 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001287
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001288 err = -EBUSY;
1289 if (__find_governor(governor->name) == NULL) {
1290 err = 0;
1291 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Dave Jones32ee8c32006-02-28 00:43:23 -05001294 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001295 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1298
1299
1300void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1301{
1302 if (!governor)
1303 return;
1304
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001305 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001307 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 return;
1309}
1310EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1311
1312
1313
1314/*********************************************************************
1315 * POLICY INTERFACE *
1316 *********************************************************************/
1317
1318/**
1319 * cpufreq_get_policy - get the current cpufreq_policy
1320 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1321 *
1322 * Reads the current cpufreq policy.
1323 */
1324int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1325{
1326 struct cpufreq_policy *cpu_policy;
1327 if (!policy)
1328 return -EINVAL;
1329
1330 cpu_policy = cpufreq_cpu_get(cpu);
1331 if (!cpu_policy)
1332 return -EINVAL;
1333
Arjan van de Ven83933af2006-01-14 16:01:49 +01001334 mutex_lock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Arjan van de Ven83933af2006-01-14 16:01:49 +01001336 mutex_unlock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 return 0;
1340}
1341EXPORT_SYMBOL(cpufreq_get_policy);
1342
1343
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001344/*
1345 * Locking: Must be called with the lock_cpu_hotplug() lock held
1346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1348{
1349 int ret = 0;
1350
1351 cpufreq_debug_disable_ratelimit();
1352 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1353 policy->min, policy->max);
1354
Dave Jones7d5e3502006-02-02 17:03:42 -05001355 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001357 if (policy->min > data->min && policy->min > policy->max) {
1358 ret = -EINVAL;
1359 goto error_out;
1360 }
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 /* verify the cpu speed can be set within this limit */
1363 ret = cpufreq_driver->verify(policy);
1364 if (ret)
1365 goto error_out;
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001368 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1369 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
1371 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001372 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1373 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 /* verify the cpu speed can be set within this limit,
1376 which might be different to the first one */
1377 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001378 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001382 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1383 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Dave Jones7d5e3502006-02-02 17:03:42 -05001385 data->min = policy->min;
1386 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
1388 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1389
1390 if (cpufreq_driver->setpolicy) {
1391 data->policy = policy->policy;
1392 dprintk("setting range\n");
1393 ret = cpufreq_driver->setpolicy(policy);
1394 } else {
1395 if (policy->governor != data->governor) {
1396 /* save old, working values */
1397 struct cpufreq_governor *old_gov = data->governor;
1398
1399 dprintk("governor switch\n");
1400
1401 /* end old governor */
1402 if (data->governor)
1403 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1404
1405 /* start new governor */
1406 data->governor = policy->governor;
1407 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1408 /* new governor failed, so re-start old one */
1409 dprintk("starting governor %s failed\n", data->governor->name);
1410 if (old_gov) {
1411 data->governor = old_gov;
1412 __cpufreq_governor(data, CPUFREQ_GOV_START);
1413 }
1414 ret = -EINVAL;
1415 goto error_out;
1416 }
1417 /* might be a policy change, too, so fall through */
1418 }
1419 dprintk("governor: change or update limits\n");
1420 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1421 }
1422
Dave Jones7d5e3502006-02-02 17:03:42 -05001423error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 cpufreq_debug_enable_ratelimit();
1425 return ret;
1426}
1427
1428/**
1429 * cpufreq_set_policy - set a new CPUFreq policy
1430 * @policy: policy to be set.
1431 *
1432 * Sets a new CPU frequency and voltage scaling policy.
1433 */
1434int cpufreq_set_policy(struct cpufreq_policy *policy)
1435{
1436 int ret = 0;
1437 struct cpufreq_policy *data;
1438
1439 if (!policy)
1440 return -EINVAL;
1441
1442 data = cpufreq_cpu_get(policy->cpu);
1443 if (!data)
1444 return -EINVAL;
1445
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001446 lock_cpu_hotplug();
1447
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 /* lock this CPU */
Arjan van de Ven83933af2006-01-14 16:01:49 +01001449 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 ret = __cpufreq_set_policy(data, policy);
1452 data->user_policy.min = data->min;
1453 data->user_policy.max = data->max;
1454 data->user_policy.policy = data->policy;
1455 data->user_policy.governor = data->governor;
1456
Arjan van de Ven83933af2006-01-14 16:01:49 +01001457 mutex_unlock(&data->lock);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001458
1459 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 cpufreq_cpu_put(data);
1461
1462 return ret;
1463}
1464EXPORT_SYMBOL(cpufreq_set_policy);
1465
1466
1467/**
1468 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1469 * @cpu: CPU which shall be re-evaluated
1470 *
1471 * Usefull for policy notifiers which have different necessities
1472 * at different times.
1473 */
1474int cpufreq_update_policy(unsigned int cpu)
1475{
1476 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1477 struct cpufreq_policy policy;
1478 int ret = 0;
1479
1480 if (!data)
1481 return -ENODEV;
1482
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001483 lock_cpu_hotplug();
Arjan van de Ven83933af2006-01-14 16:01:49 +01001484 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001487 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 policy.min = data->user_policy.min;
1489 policy.max = data->user_policy.max;
1490 policy.policy = data->user_policy.policy;
1491 policy.governor = data->user_policy.governor;
1492
Thomas Renninger0961dd02006-01-26 18:46:33 +01001493 /* BIOS might change freq behind our back
1494 -> ask driver for current freq and notify governors about a change */
1495 if (cpufreq_driver->get) {
1496 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001497 if (!data->cur) {
1498 dprintk("Driver did not initialize current freq");
1499 data->cur = policy.cur;
1500 } else {
1501 if (data->cur != policy.cur)
1502 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1503 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001504 }
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 ret = __cpufreq_set_policy(data, &policy);
1507
Arjan van de Ven83933af2006-01-14 16:01:49 +01001508 mutex_unlock(&data->lock);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001509 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 cpufreq_cpu_put(data);
1511 return ret;
1512}
1513EXPORT_SYMBOL(cpufreq_update_policy);
1514
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001515#ifdef CONFIG_HOTPLUG_CPU
1516static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001517 unsigned long action, void *hcpu)
1518{
1519 unsigned int cpu = (unsigned long)hcpu;
1520 struct cpufreq_policy *policy;
1521 struct sys_device *sys_dev;
1522
1523 sys_dev = get_cpu_sysdev(cpu);
1524
1525 if (sys_dev) {
1526 switch (action) {
1527 case CPU_ONLINE:
1528 cpufreq_add_dev(sys_dev);
1529 break;
1530 case CPU_DOWN_PREPARE:
1531 /*
1532 * We attempt to put this cpu in lowest frequency
1533 * possible before going down. This will permit
1534 * hardware-managed P-State to switch other related
1535 * threads to min or higher speeds if possible.
1536 */
1537 policy = cpufreq_cpu_data[cpu];
1538 if (policy) {
1539 cpufreq_driver_target(policy, policy->min,
1540 CPUFREQ_RELATION_H);
1541 }
1542 break;
1543 case CPU_DEAD:
1544 cpufreq_remove_dev(sys_dev);
1545 break;
1546 }
1547 }
1548 return NOTIFY_OK;
1549}
1550
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001551static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
Ashok Rajc32b6b82005-10-30 14:59:54 -08001552{
1553 .notifier_call = cpufreq_cpu_callback,
1554};
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001555#endif /* CONFIG_HOTPLUG_CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557/*********************************************************************
1558 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1559 *********************************************************************/
1560
1561/**
1562 * cpufreq_register_driver - register a CPU Frequency driver
1563 * @driver_data: A struct cpufreq_driver containing the values#
1564 * submitted by the CPU Frequency driver.
1565 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001566 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001568 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 *
1570 */
1571int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1572{
1573 unsigned long flags;
1574 int ret;
1575
1576 if (!driver_data || !driver_data->verify || !driver_data->init ||
1577 ((!driver_data->setpolicy) && (!driver_data->target)))
1578 return -EINVAL;
1579
1580 dprintk("trying to register driver %s\n", driver_data->name);
1581
1582 if (driver_data->setpolicy)
1583 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1584
1585 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1586 if (cpufreq_driver) {
1587 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1588 return -EBUSY;
1589 }
1590 cpufreq_driver = driver_data;
1591 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1592
1593 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1594
1595 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1596 int i;
1597 ret = -ENODEV;
1598
1599 /* check for at least one working CPU */
1600 for (i=0; i<NR_CPUS; i++)
1601 if (cpufreq_cpu_data[i])
1602 ret = 0;
1603
1604 /* if all ->init() calls failed, unregister */
1605 if (ret) {
1606 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1607 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1608
1609 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1610 cpufreq_driver = NULL;
1611 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1612 }
1613 }
1614
1615 if (!ret) {
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001616 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 dprintk("driver %s up and running\n", driver_data->name);
1618 cpufreq_debug_enable_ratelimit();
1619 }
1620
1621 return (ret);
1622}
1623EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1624
1625
1626/**
1627 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1628 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001629 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 * the right to do so, i.e. if you have succeeded in initialising before!
1631 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1632 * currently not initialised.
1633 */
1634int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1635{
1636 unsigned long flags;
1637
1638 cpufreq_debug_disable_ratelimit();
1639
1640 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1641 cpufreq_debug_enable_ratelimit();
1642 return -EINVAL;
1643 }
1644
1645 dprintk("unregistering driver %s\n", driver->name);
1646
1647 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001648 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
1650 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1651 cpufreq_driver = NULL;
1652 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1653
1654 return 0;
1655}
1656EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);