blob: 12e63642aa0f75055e5a08b9c336f7014d823b9c [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
18#include <linux/config.h>
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/notifier.h>
23#include <linux/cpufreq.h>
24#include <linux/delay.h>
25#include <linux/interrupt.h>
26#include <linux/spinlock.h>
27#include <linux/device.h>
28#include <linux/slab.h>
29#include <linux/cpu.h>
30#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080031#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
34
35/**
36 * The "cpufreq driver" - the arch- or hardware-dependend low
37 * level driver of CPUFreq support, and its spinlock. This lock
38 * also protects the cpufreq_cpu_data array.
39 */
Dave Jones7d5e3502006-02-02 17:03:42 -050040static struct cpufreq_driver *cpufreq_driver;
41static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070042static DEFINE_SPINLOCK(cpufreq_driver_lock);
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/* internal prototypes */
45static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
46static void handle_update(void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/**
Dave Jones32ee8c32006-02-28 00:43:23 -050049 * Two notifier lists: the "policy" list is involved in the
50 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * "transition" list for kernel code that needs to handle
52 * changes to devices when the CPU clock speed changes.
53 * The mutex locks both lists.
54 */
Alan Sterne041c682006-03-27 01:16:30 -080055static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
56static BLOCKING_NOTIFIER_HEAD(cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58
59static LIST_HEAD(cpufreq_governor_list);
Dave Jones7d5e3502006-02-02 17:03:42 -050060static DEFINE_MUTEX (cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Dave Jones7d5e3502006-02-02 17:03:42 -050062struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 struct cpufreq_policy *data;
65 unsigned long flags;
66
67 if (cpu >= NR_CPUS)
68 goto err_out;
69
70 /* get the cpufreq driver */
71 spin_lock_irqsave(&cpufreq_driver_lock, flags);
72
73 if (!cpufreq_driver)
74 goto err_out_unlock;
75
76 if (!try_module_get(cpufreq_driver->owner))
77 goto err_out_unlock;
78
79
80 /* get the CPU */
81 data = cpufreq_cpu_data[cpu];
82
83 if (!data)
84 goto err_out_put_module;
85
86 if (!kobject_get(&data->kobj))
87 goto err_out_put_module;
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 return data;
91
Dave Jones7d5e3502006-02-02 17:03:42 -050092err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -050094err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -050096err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 return NULL;
98}
99EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
100
Dave Jones7d5e3502006-02-02 17:03:42 -0500101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102void cpufreq_cpu_put(struct cpufreq_policy *data)
103{
104 kobject_put(&data->kobj);
105 module_put(cpufreq_driver->owner);
106}
107EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
108
109
110/*********************************************************************
111 * UNIFIED DEBUG HELPERS *
112 *********************************************************************/
113#ifdef CONFIG_CPU_FREQ_DEBUG
114
115/* what part(s) of the CPUfreq subsystem are debugged? */
116static unsigned int debug;
117
118/* is the debug output ratelimit'ed using printk_ratelimit? User can
119 * set or modify this value.
120 */
121static unsigned int debug_ratelimit = 1;
122
123/* is the printk_ratelimit'ing enabled? It's enabled after a successful
124 * loading of a cpufreq driver, temporarily disabled when a new policy
125 * is set, and disabled upon cpufreq driver removal
126 */
127static unsigned int disable_ratelimit = 1;
128static DEFINE_SPINLOCK(disable_ratelimit_lock);
129
Arjan van de Ven858119e2006-01-14 13:20:43 -0800130static void cpufreq_debug_enable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 unsigned long flags;
133
134 spin_lock_irqsave(&disable_ratelimit_lock, flags);
135 if (disable_ratelimit)
136 disable_ratelimit--;
137 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
138}
139
Arjan van de Ven858119e2006-01-14 13:20:43 -0800140static void cpufreq_debug_disable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 unsigned long flags;
143
144 spin_lock_irqsave(&disable_ratelimit_lock, flags);
145 disable_ratelimit++;
146 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
147}
148
149void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
150{
151 char s[256];
152 va_list args;
153 unsigned int len;
154 unsigned long flags;
Dave Jones32ee8c32006-02-28 00:43:23 -0500155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 WARN_ON(!prefix);
157 if (type & debug) {
158 spin_lock_irqsave(&disable_ratelimit_lock, flags);
159 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
160 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
161 return;
162 }
163 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
164
165 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
166
167 va_start(args, fmt);
168 len += vsnprintf(&s[len], (256 - len), fmt, args);
169 va_end(args);
170
171 printk(s);
172
173 WARN_ON(len < 5);
174 }
175}
176EXPORT_SYMBOL(cpufreq_debug_printk);
177
178
179module_param(debug, uint, 0644);
180MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
181
182module_param(debug_ratelimit, uint, 0644);
183MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
184
185#else /* !CONFIG_CPU_FREQ_DEBUG */
186
187static inline void cpufreq_debug_enable_ratelimit(void) { return; }
188static inline void cpufreq_debug_disable_ratelimit(void) { return; }
189
190#endif /* CONFIG_CPU_FREQ_DEBUG */
191
192
193/*********************************************************************
194 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
195 *********************************************************************/
196
197/**
198 * adjust_jiffies - adjust the system "loops_per_jiffy"
199 *
200 * This function alters the system "loops_per_jiffy" for the clock
201 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500202 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 * per-CPU loops_per_jiffy value wherever possible.
204 */
205#ifndef CONFIG_SMP
206static unsigned long l_p_j_ref;
207static unsigned int l_p_j_ref_freq;
208
Arjan van de Ven858119e2006-01-14 13:20:43 -0800209static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 if (ci->flags & CPUFREQ_CONST_LOOPS)
212 return;
213
214 if (!l_p_j_ref_freq) {
215 l_p_j_ref = loops_per_jiffy;
216 l_p_j_ref_freq = ci->old;
217 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
218 }
219 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
220 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700221 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
223 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
224 }
225}
226#else
227static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
228#endif
229
230
231/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800232 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
233 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800235 * This function calls the transition notifiers and the "adjust_jiffies"
236 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500237 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 */
239void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
240{
Dave Jonese4472cb2006-01-31 15:53:55 -0800241 struct cpufreq_policy *policy;
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 BUG_ON(irqs_disabled());
244
245 freqs->flags = cpufreq_driver->flags;
Dave Jonese4472cb2006-01-31 15:53:55 -0800246 dprintk("notification %u of frequency transition to %u kHz\n",
247 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Dave Jonese4472cb2006-01-31 15:53:55 -0800249 policy = cpufreq_cpu_data[freqs->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500253 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800254 * which is not equal to what the cpufreq core thinks is
255 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
257 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800258 if ((policy) && (policy->cpu == freqs->cpu) &&
259 (policy->cur) && (policy->cur != freqs->old)) {
260 dprintk(KERN_WARNING "Warning: CPU frequency is"
261 " %u, cpufreq assumed %u kHz.\n",
262 freqs->old, policy->cur);
263 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265 }
Alan Sterne041c682006-03-27 01:16:30 -0800266 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
267 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
269 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 case CPUFREQ_POSTCHANGE:
272 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Alan Sterne041c682006-03-27 01:16:30 -0800273 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
274 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800275 if (likely(policy) && likely(policy->cpu == freqs->cpu))
276 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
281
282
283
284/*********************************************************************
285 * SYSFS INTERFACE *
286 *********************************************************************/
287
288/**
289 * cpufreq_parse_governor - parse a governor string
290 */
291static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
292 struct cpufreq_governor **governor)
293{
294 if (!cpufreq_driver)
295 return -EINVAL;
296 if (cpufreq_driver->setpolicy) {
297 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
298 *policy = CPUFREQ_POLICY_PERFORMANCE;
299 return 0;
300 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
301 *policy = CPUFREQ_POLICY_POWERSAVE;
302 return 0;
303 }
304 return -EINVAL;
305 } else {
306 struct cpufreq_governor *t;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800307 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 if (!cpufreq_driver || !cpufreq_driver->target)
309 goto out;
310 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
311 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
312 *governor = t;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800313 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return 0;
315 }
316 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500317out:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800318 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320 return -EINVAL;
321}
322EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
323
324
325/* drivers/base/cpu.c */
326extern struct sysdev_class cpu_sysdev_class;
327
328
329/**
330 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
331 *
332 * Write out information from cpufreq_driver->policy[cpu]; object must be
333 * "unsigned int".
334 */
335
Dave Jones32ee8c32006-02-28 00:43:23 -0500336#define show_one(file_name, object) \
337static ssize_t show_##file_name \
338(struct cpufreq_policy * policy, char *buf) \
339{ \
340 return sprintf (buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343show_one(cpuinfo_min_freq, cpuinfo.min_freq);
344show_one(cpuinfo_max_freq, cpuinfo.max_freq);
345show_one(scaling_min_freq, min);
346show_one(scaling_max_freq, max);
347show_one(scaling_cur_freq, cur);
348
Thomas Renninger7970e082006-04-13 15:14:04 +0200349static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351/**
352 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
353 */
354#define store_one(file_name, object) \
355static ssize_t store_##file_name \
356(struct cpufreq_policy * policy, const char *buf, size_t count) \
357{ \
358 unsigned int ret = -EINVAL; \
359 struct cpufreq_policy new_policy; \
360 \
361 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
362 if (ret) \
363 return -EINVAL; \
364 \
365 ret = sscanf (buf, "%u", &new_policy.object); \
366 if (ret != 1) \
367 return -EINVAL; \
368 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200369 mutex_lock(&policy->lock); \
370 ret = __cpufreq_set_policy(policy, &new_policy); \
371 policy->user_policy.object = policy->object; \
372 mutex_unlock(&policy->lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 \
374 return ret ? ret : count; \
375}
376
377store_one(scaling_min_freq,min);
378store_one(scaling_max_freq,max);
379
380/**
381 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
382 */
383static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
384{
385 unsigned int cur_freq = cpufreq_get(policy->cpu);
386 if (!cur_freq)
387 return sprintf(buf, "<unknown>");
388 return sprintf(buf, "%u\n", cur_freq);
389}
390
391
392/**
393 * show_scaling_governor - show the current policy for the specified CPU
394 */
395static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
396{
397 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
398 return sprintf(buf, "powersave\n");
399 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
400 return sprintf(buf, "performance\n");
401 else if (policy->governor)
402 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
403 return -EINVAL;
404}
405
406
407/**
408 * store_scaling_governor - store policy for the specified CPU
409 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500410static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
411 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 unsigned int ret = -EINVAL;
414 char str_governor[16];
415 struct cpufreq_policy new_policy;
416
417 ret = cpufreq_get_policy(&new_policy, policy->cpu);
418 if (ret)
419 return ret;
420
421 ret = sscanf (buf, "%15s", str_governor);
422 if (ret != 1)
423 return -EINVAL;
424
425 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
426 return -EINVAL;
427
Thomas Renninger7970e082006-04-13 15:14:04 +0200428 /* Do not use cpufreq_set_policy here or the user_policy.max
429 will be wrongly overridden */
430 mutex_lock(&policy->lock);
431 ret = __cpufreq_set_policy(policy, &new_policy);
432
433 policy->user_policy.policy = policy->policy;
434 policy->user_policy.governor = policy->governor;
435 mutex_unlock(&policy->lock);
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return ret ? ret : count;
438}
439
440/**
441 * show_scaling_driver - show the cpufreq driver currently loaded
442 */
443static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
444{
445 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
446}
447
448/**
449 * show_scaling_available_governors - show the available CPUfreq governors
450 */
451static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
452 char *buf)
453{
454 ssize_t i = 0;
455 struct cpufreq_governor *t;
456
457 if (!cpufreq_driver->target) {
458 i += sprintf(buf, "performance powersave");
459 goto out;
460 }
461
462 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
463 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
464 goto out;
465 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
466 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500467out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 i += sprintf(&buf[i], "\n");
469 return i;
470}
471/**
472 * show_affected_cpus - show the CPUs affected by each transition
473 */
474static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
475{
476 ssize_t i = 0;
477 unsigned int cpu;
478
479 for_each_cpu_mask(cpu, policy->cpus) {
480 if (i)
481 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
482 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
483 if (i >= (PAGE_SIZE - 5))
484 break;
485 }
486 i += sprintf(&buf[i], "\n");
487 return i;
488}
489
490
491#define define_one_ro(_name) \
492static struct freq_attr _name = \
493__ATTR(_name, 0444, show_##_name, NULL)
494
495#define define_one_ro0400(_name) \
496static struct freq_attr _name = \
497__ATTR(_name, 0400, show_##_name, NULL)
498
499#define define_one_rw(_name) \
500static struct freq_attr _name = \
501__ATTR(_name, 0644, show_##_name, store_##_name)
502
503define_one_ro0400(cpuinfo_cur_freq);
504define_one_ro(cpuinfo_min_freq);
505define_one_ro(cpuinfo_max_freq);
506define_one_ro(scaling_available_governors);
507define_one_ro(scaling_driver);
508define_one_ro(scaling_cur_freq);
509define_one_ro(affected_cpus);
510define_one_rw(scaling_min_freq);
511define_one_rw(scaling_max_freq);
512define_one_rw(scaling_governor);
513
514static struct attribute * default_attrs[] = {
515 &cpuinfo_min_freq.attr,
516 &cpuinfo_max_freq.attr,
517 &scaling_min_freq.attr,
518 &scaling_max_freq.attr,
519 &affected_cpus.attr,
520 &scaling_governor.attr,
521 &scaling_driver.attr,
522 &scaling_available_governors.attr,
523 NULL
524};
525
526#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
527#define to_attr(a) container_of(a,struct freq_attr,attr)
528
529static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
530{
531 struct cpufreq_policy * policy = to_policy(kobj);
532 struct freq_attr * fattr = to_attr(attr);
533 ssize_t ret;
534 policy = cpufreq_cpu_get(policy->cpu);
535 if (!policy)
536 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500537 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 cpufreq_cpu_put(policy);
539 return ret;
540}
541
Dave Jones32ee8c32006-02-28 00:43:23 -0500542static ssize_t store(struct kobject * kobj, struct attribute * attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 const char * buf, size_t count)
544{
545 struct cpufreq_policy * policy = to_policy(kobj);
546 struct freq_attr * fattr = to_attr(attr);
547 ssize_t ret;
548 policy = cpufreq_cpu_get(policy->cpu);
549 if (!policy)
550 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500551 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 cpufreq_cpu_put(policy);
553 return ret;
554}
555
556static void cpufreq_sysfs_release(struct kobject * kobj)
557{
558 struct cpufreq_policy * policy = to_policy(kobj);
559 dprintk("last reference is dropped\n");
560 complete(&policy->kobj_unregister);
561}
562
563static struct sysfs_ops sysfs_ops = {
564 .show = show,
565 .store = store,
566};
567
568static struct kobj_type ktype_cpufreq = {
569 .sysfs_ops = &sysfs_ops,
570 .default_attrs = default_attrs,
571 .release = cpufreq_sysfs_release,
572};
573
574
575/**
576 * cpufreq_add_dev - add a CPU device
577 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500578 * Adds the cpufreq interface for a CPU device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
580static int cpufreq_add_dev (struct sys_device * sys_dev)
581{
582 unsigned int cpu = sys_dev->id;
583 int ret = 0;
584 struct cpufreq_policy new_policy;
585 struct cpufreq_policy *policy;
586 struct freq_attr **drv_attr;
Dave Jones8ff69732006-03-05 03:37:23 -0500587 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 unsigned long flags;
589 unsigned int j;
Dave Jones8ff69732006-03-05 03:37:23 -0500590#ifdef CONFIG_SMP
591 struct cpufreq_policy *managed_policy;
592#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Ashok Rajc32b6b82005-10-30 14:59:54 -0800594 if (cpu_is_offline(cpu))
595 return 0;
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 cpufreq_debug_disable_ratelimit();
598 dprintk("adding CPU %u\n", cpu);
599
600#ifdef CONFIG_SMP
601 /* check whether a different CPU already registered this
602 * CPU because it is in the same boat. */
603 policy = cpufreq_cpu_get(cpu);
604 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500605 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 cpufreq_debug_enable_ratelimit();
607 return 0;
608 }
609#endif
610
611 if (!try_module_get(cpufreq_driver->owner)) {
612 ret = -EINVAL;
613 goto module_out;
614 }
615
Dave Jonese98df502005-10-20 15:17:43 -0700616 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (!policy) {
618 ret = -ENOMEM;
619 goto nomem_out;
620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 policy->cpu = cpu;
623 policy->cpus = cpumask_of_cpu(cpu);
624
Arjan van de Ven83933af2006-01-14 16:01:49 +0100625 mutex_init(&policy->lock);
626 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 init_completion(&policy->kobj_unregister);
628 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
629
630 /* call driver. From then on the cpufreq must be able
631 * to accept all calls to ->verify and ->setpolicy for this CPU
632 */
633 ret = cpufreq_driver->init(policy);
634 if (ret) {
635 dprintk("initialization failed\n");
Andrew Mortonf3876c12006-01-18 13:40:54 -0800636 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 goto err_out;
638 }
639
Dave Jones8ff69732006-03-05 03:37:23 -0500640#ifdef CONFIG_SMP
641 for_each_cpu_mask(j, policy->cpus) {
642 if (cpu == j)
643 continue;
644
645 /* check for existing affected CPUs. They may not be aware
646 * of it due to CPU Hotplug.
647 */
648 managed_policy = cpufreq_cpu_get(j);
649 if (unlikely(managed_policy)) {
650 spin_lock_irqsave(&cpufreq_driver_lock, flags);
651 managed_policy->cpus = policy->cpus;
652 cpufreq_cpu_data[cpu] = managed_policy;
653 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
654
655 dprintk("CPU already managed, adding link\n");
656 sysfs_create_link(&sys_dev->kobj,
657 &managed_policy->kobj, "cpufreq");
658
659 cpufreq_debug_enable_ratelimit();
660 mutex_unlock(&policy->lock);
661 ret = 0;
662 goto err_out_driver_exit; /* call driver->exit() */
663 }
664 }
665#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
667
668 /* prepare interface data */
669 policy->kobj.parent = &sys_dev->kobj;
670 policy->kobj.ktype = &ktype_cpufreq;
671 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
672
673 ret = kobject_register(&policy->kobj);
Andrew Mortonf3876c12006-01-18 13:40:54 -0800674 if (ret) {
675 mutex_unlock(&policy->lock);
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700676 goto err_out_driver_exit;
Andrew Mortonf3876c12006-01-18 13:40:54 -0800677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 /* set up files for this cpu device */
679 drv_attr = cpufreq_driver->attr;
680 while ((drv_attr) && (*drv_attr)) {
681 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
682 drv_attr++;
683 }
684 if (cpufreq_driver->get)
685 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
686 if (cpufreq_driver->target)
687 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
688
689 spin_lock_irqsave(&cpufreq_driver_lock, flags);
690 for_each_cpu_mask(j, policy->cpus)
691 cpufreq_cpu_data[j] = policy;
692 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones8ff69732006-03-05 03:37:23 -0500693
694 /* symlink affected CPUs */
695 for_each_cpu_mask(j, policy->cpus) {
696 if (j == cpu)
697 continue;
698 if (!cpu_online(j))
699 continue;
700
Dave Jones1f8b2c92006-03-29 01:40:04 -0500701 dprintk("CPU %u already managed, adding link\n", j);
Dave Jones8ff69732006-03-05 03:37:23 -0500702 cpufreq_cpu_get(cpu);
703 cpu_sys_dev = get_cpu_sysdev(j);
704 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
705 "cpufreq");
706 }
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 policy->governor = NULL; /* to assure that the starting sequence is
709 * run in cpufreq_set_policy */
Arjan van de Ven83933af2006-01-14 16:01:49 +0100710 mutex_unlock(&policy->lock);
Dave Jones87c32272006-03-29 01:48:37 -0500711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /* set default policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 ret = cpufreq_set_policy(&new_policy);
714 if (ret) {
715 dprintk("setting policy failed\n");
716 goto err_out_unregister;
717 }
718
719 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 dprintk("initialization complete\n");
721 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -0500722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return 0;
724
725
726err_out_unregister:
727 spin_lock_irqsave(&cpufreq_driver_lock, flags);
728 for_each_cpu_mask(j, policy->cpus)
729 cpufreq_cpu_data[j] = NULL;
730 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
731
732 kobject_unregister(&policy->kobj);
733 wait_for_completion(&policy->kobj_unregister);
734
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700735err_out_driver_exit:
736 if (cpufreq_driver->exit)
737 cpufreq_driver->exit(policy);
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739err_out:
740 kfree(policy);
741
742nomem_out:
743 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800744module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 cpufreq_debug_enable_ratelimit();
746 return ret;
747}
748
749
750/**
751 * cpufreq_remove_dev - remove a CPU device
752 *
753 * Removes the cpufreq interface for a CPU device.
754 */
755static int cpufreq_remove_dev (struct sys_device * sys_dev)
756{
757 unsigned int cpu = sys_dev->id;
758 unsigned long flags;
759 struct cpufreq_policy *data;
760#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -0800761 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 unsigned int j;
763#endif
764
765 cpufreq_debug_disable_ratelimit();
766 dprintk("unregistering CPU %u\n", cpu);
767
768 spin_lock_irqsave(&cpufreq_driver_lock, flags);
769 data = cpufreq_cpu_data[cpu];
770
771 if (!data) {
772 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 cpufreq_debug_enable_ratelimit();
774 return -EINVAL;
775 }
776 cpufreq_cpu_data[cpu] = NULL;
777
778
779#ifdef CONFIG_SMP
780 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -0500781 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 */
783 if (unlikely(cpu != data->cpu)) {
784 dprintk("removing link\n");
Dave Jones8ff69732006-03-05 03:37:23 -0500785 cpu_clear(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
787 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 cpufreq_cpu_put(data);
789 cpufreq_debug_enable_ratelimit();
790 return 0;
791 }
792#endif
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 if (!kobject_get(&data->kobj)) {
796 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
797 cpufreq_debug_enable_ratelimit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500798 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
801#ifdef CONFIG_SMP
802 /* if we have other CPUs still registered, we need to unlink them,
803 * or else wait_for_completion below will lock up. Clean the
804 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
805 * links afterwards.
806 */
807 if (unlikely(cpus_weight(data->cpus) > 1)) {
808 for_each_cpu_mask(j, data->cpus) {
809 if (j == cpu)
810 continue;
811 cpufreq_cpu_data[j] = NULL;
812 }
813 }
814
815 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
816
817 if (unlikely(cpus_weight(data->cpus) > 1)) {
818 for_each_cpu_mask(j, data->cpus) {
819 if (j == cpu)
820 continue;
821 dprintk("removing link for cpu %u\n", j);
Ashok Rajd434fca2005-10-30 14:59:52 -0800822 cpu_sys_dev = get_cpu_sysdev(j);
823 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 cpufreq_cpu_put(data);
825 }
826 }
827#else
828 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
829#endif
830
Arjan van de Ven83933af2006-01-14 16:01:49 +0100831 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (cpufreq_driver->target)
833 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Arjan van de Ven83933af2006-01-14 16:01:49 +0100834 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 kobject_unregister(&data->kobj);
837
838 kobject_put(&data->kobj);
839
840 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -0500841 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 * unloading.
843 */
844 dprintk("waiting for dropping of refcount\n");
845 wait_for_completion(&data->kobj_unregister);
846 dprintk("wait complete\n");
847
848 if (cpufreq_driver->exit)
849 cpufreq_driver->exit(data);
850
851 kfree(data);
852
853 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 return 0;
855}
856
857
858static void handle_update(void *data)
859{
860 unsigned int cpu = (unsigned int)(long)data;
861 dprintk("handle_update for cpu %u called\n", cpu);
862 cpufreq_update_policy(cpu);
863}
864
865/**
866 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
867 * @cpu: cpu number
868 * @old_freq: CPU frequency the kernel thinks the CPU runs at
869 * @new_freq: CPU frequency the CPU actually runs at
870 *
871 * We adjust to current frequency first, and need to clean up later. So either call
872 * to cpufreq_update_policy() or schedule handle_update()).
873 */
874static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
875{
876 struct cpufreq_freqs freqs;
877
Dave Jones78ee9982005-05-31 19:03:43 -0700878 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
880
881 freqs.cpu = cpu;
882 freqs.old = old_freq;
883 freqs.new = new_freq;
884 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
885 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
886}
887
888
Dave Jones32ee8c32006-02-28 00:43:23 -0500889/**
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800890 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
891 * @cpu: CPU number
892 *
893 * This is the last known freq, without actually getting it from the driver.
894 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
895 */
896unsigned int cpufreq_quick_get(unsigned int cpu)
897{
898 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
899 unsigned int ret = 0;
900
901 if (policy) {
Arjan van de Ven83933af2006-01-14 16:01:49 +0100902 mutex_lock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800903 ret = policy->cur;
Arjan van de Ven83933af2006-01-14 16:01:49 +0100904 mutex_unlock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800905 cpufreq_cpu_put(policy);
906 }
907
908 return (ret);
909}
910EXPORT_SYMBOL(cpufreq_quick_get);
911
912
Dave Jones32ee8c32006-02-28 00:43:23 -0500913/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * cpufreq_get - get the current CPU frequency (in kHz)
915 * @cpu: CPU number
916 *
917 * Get the CPU current (static) CPU frequency
918 */
919unsigned int cpufreq_get(unsigned int cpu)
920{
921 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
922 unsigned int ret = 0;
923
924 if (!policy)
925 return 0;
926
927 if (!cpufreq_driver->get)
928 goto out;
929
Arjan van de Ven83933af2006-01-14 16:01:49 +0100930 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 ret = cpufreq_driver->get(cpu);
933
Dave Jones7d5e3502006-02-02 17:03:42 -0500934 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /* verify no discrepancy between actual and saved value exists */
936 if (unlikely(ret != policy->cur)) {
937 cpufreq_out_of_sync(cpu, policy->cur, ret);
938 schedule_work(&policy->update);
939 }
940 }
941
Arjan van de Ven83933af2006-01-14 16:01:49 +0100942 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Dave Jones7d5e3502006-02-02 17:03:42 -0500944out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 cpufreq_cpu_put(policy);
946
947 return (ret);
948}
949EXPORT_SYMBOL(cpufreq_get);
950
951
952/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700953 * cpufreq_suspend - let the low level driver prepare for suspend
954 */
955
Bernard Blackhame00d99672005-07-07 17:56:42 -0700956static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700957{
958 int cpu = sysdev->id;
959 unsigned int ret = 0;
960 unsigned int cur_freq = 0;
961 struct cpufreq_policy *cpu_policy;
962
963 dprintk("resuming cpu %u\n", cpu);
964
965 if (!cpu_online(cpu))
966 return 0;
967
968 /* we may be lax here as interrupts are off. Nonetheless
969 * we need to grab the correct cpu policy, as to check
970 * whether we really run on this CPU.
971 */
972
973 cpu_policy = cpufreq_cpu_get(cpu);
974 if (!cpu_policy)
975 return -EINVAL;
976
977 /* only handle each CPU group once */
978 if (unlikely(cpu_policy->cpu != cpu)) {
979 cpufreq_cpu_put(cpu_policy);
980 return 0;
981 }
982
983 if (cpufreq_driver->suspend) {
Bernard Blackhame00d99672005-07-07 17:56:42 -0700984 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700985 if (ret) {
986 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
987 "step on CPU %u\n", cpu_policy->cpu);
988 cpufreq_cpu_put(cpu_policy);
989 return ret;
990 }
991 }
992
993
994 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
995 goto out;
996
997 if (cpufreq_driver->get)
998 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
999
1000 if (!cur_freq || !cpu_policy->cur) {
1001 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1002 "frequency is what timing core thinks it is.\n");
1003 goto out;
1004 }
1005
1006 if (unlikely(cur_freq != cpu_policy->cur)) {
1007 struct cpufreq_freqs freqs;
1008
1009 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Dave Jones78ee9982005-05-31 19:03:43 -07001010 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001011 "cpufreq assumed %u kHz.\n",
1012 cur_freq, cpu_policy->cur);
1013
1014 freqs.cpu = cpu;
1015 freqs.old = cpu_policy->cur;
1016 freqs.new = cur_freq;
1017
Alan Sterne041c682006-03-27 01:16:30 -08001018 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001019 CPUFREQ_SUSPENDCHANGE, &freqs);
1020 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1021
1022 cpu_policy->cur = cur_freq;
1023 }
1024
Dave Jones7d5e3502006-02-02 17:03:42 -05001025out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001026 cpufreq_cpu_put(cpu_policy);
1027 return 0;
1028}
1029
1030/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 * cpufreq_resume - restore proper CPU frequency handling after resume
1032 *
1033 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1034 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001035 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1036 * restored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 */
1038static int cpufreq_resume(struct sys_device * sysdev)
1039{
1040 int cpu = sysdev->id;
1041 unsigned int ret = 0;
1042 struct cpufreq_policy *cpu_policy;
1043
1044 dprintk("resuming cpu %u\n", cpu);
1045
1046 if (!cpu_online(cpu))
1047 return 0;
1048
1049 /* we may be lax here as interrupts are off. Nonetheless
1050 * we need to grab the correct cpu policy, as to check
1051 * whether we really run on this CPU.
1052 */
1053
1054 cpu_policy = cpufreq_cpu_get(cpu);
1055 if (!cpu_policy)
1056 return -EINVAL;
1057
1058 /* only handle each CPU group once */
1059 if (unlikely(cpu_policy->cpu != cpu)) {
1060 cpufreq_cpu_put(cpu_policy);
1061 return 0;
1062 }
1063
1064 if (cpufreq_driver->resume) {
1065 ret = cpufreq_driver->resume(cpu_policy);
1066 if (ret) {
1067 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1068 "step on CPU %u\n", cpu_policy->cpu);
1069 cpufreq_cpu_put(cpu_policy);
1070 return ret;
1071 }
1072 }
1073
1074 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1075 unsigned int cur_freq = 0;
1076
1077 if (cpufreq_driver->get)
1078 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1079
1080 if (!cur_freq || !cpu_policy->cur) {
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001081 printk(KERN_ERR "cpufreq: resume failed to assert "
1082 "current frequency is what timing core "
1083 "thinks it is.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 goto out;
1085 }
1086
1087 if (unlikely(cur_freq != cpu_policy->cur)) {
1088 struct cpufreq_freqs freqs;
1089
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001090 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Dave Jones78ee9982005-05-31 19:03:43 -07001091 dprintk(KERN_WARNING "Warning: CPU frequency"
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001092 "is %u, cpufreq assumed %u kHz.\n",
1093 cur_freq, cpu_policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 freqs.cpu = cpu;
1096 freqs.old = cpu_policy->cur;
1097 freqs.new = cur_freq;
1098
Alan Sterne041c682006-03-27 01:16:30 -08001099 blocking_notifier_call_chain(
1100 &cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001101 CPUFREQ_RESUMECHANGE, &freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1103
1104 cpu_policy->cur = cur_freq;
1105 }
1106 }
1107
1108out:
1109 schedule_work(&cpu_policy->update);
1110 cpufreq_cpu_put(cpu_policy);
1111 return ret;
1112}
1113
1114static struct sysdev_driver cpufreq_sysdev_driver = {
1115 .add = cpufreq_add_dev,
1116 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001117 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 .resume = cpufreq_resume,
1119};
1120
1121
1122/*********************************************************************
1123 * NOTIFIER LISTS INTERFACE *
1124 *********************************************************************/
1125
1126/**
1127 * cpufreq_register_notifier - register a driver with cpufreq
1128 * @nb: notifier function to register
1129 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1130 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001131 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 * are notified about clock rate changes (once before and once after
1133 * the transition), or a list of drivers that are notified about
1134 * changes in cpufreq policy.
1135 *
1136 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001137 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
1139int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1140{
1141 int ret;
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 switch (list) {
1144 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001145 ret = blocking_notifier_chain_register(
1146 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 break;
1148 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001149 ret = blocking_notifier_chain_register(
1150 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
1152 default:
1153 ret = -EINVAL;
1154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 return ret;
1157}
1158EXPORT_SYMBOL(cpufreq_register_notifier);
1159
1160
1161/**
1162 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1163 * @nb: notifier block to be unregistered
1164 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1165 *
1166 * Remove a driver from the CPU frequency notifier list.
1167 *
1168 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001169 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 */
1171int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1172{
1173 int ret;
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 switch (list) {
1176 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001177 ret = blocking_notifier_chain_unregister(
1178 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 break;
1180 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001181 ret = blocking_notifier_chain_unregister(
1182 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 break;
1184 default:
1185 ret = -EINVAL;
1186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 return ret;
1189}
1190EXPORT_SYMBOL(cpufreq_unregister_notifier);
1191
1192
1193/*********************************************************************
1194 * GOVERNORS *
1195 *********************************************************************/
1196
1197
1198int __cpufreq_driver_target(struct cpufreq_policy *policy,
1199 unsigned int target_freq,
1200 unsigned int relation)
1201{
1202 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001203
Ashok Raja9d9baa2005-11-28 13:43:46 -08001204 lock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1206 target_freq, relation);
1207 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1208 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001209
Ashok Raja9d9baa2005-11-28 13:43:46 -08001210 unlock_cpu_hotplug();
Ashok Raj90d45d12005-11-08 21:34:24 -08001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return retval;
1213}
1214EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1215
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{
Dave Jonescc993ca2005-07-28 09:43:56 -07001220 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 policy = cpufreq_cpu_get(policy->cpu);
1223 if (!policy)
1224 return -EINVAL;
1225
Arjan van de Ven83933af2006-01-14 16:01:49 +01001226 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 ret = __cpufreq_driver_target(policy, target_freq, relation);
1229
Arjan van de Ven83933af2006-01-14 16:01:49 +01001230 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return ret;
1234}
1235EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1236
1237
1238static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1239{
Dave Jonescc993ca2005-07-28 09:43:56 -07001240 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (!try_module_get(policy->governor->owner))
1243 return -EINVAL;
1244
1245 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1246 ret = policy->governor->governor(policy, event);
1247
1248 /* we keep one module reference alive for each CPU governed by this CPU */
1249 if ((event != CPUFREQ_GOV_START) || ret)
1250 module_put(policy->governor->owner);
1251 if ((event == CPUFREQ_GOV_STOP) && !ret)
1252 module_put(policy->governor->owner);
1253
1254 return ret;
1255}
1256
1257
1258int cpufreq_governor(unsigned int cpu, unsigned int event)
1259{
1260 int ret = 0;
1261 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1262
1263 if (!policy)
1264 return -EINVAL;
1265
Arjan van de Ven83933af2006-01-14 16:01:49 +01001266 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 ret = __cpufreq_governor(policy, event);
Arjan van de Ven83933af2006-01-14 16:01:49 +01001268 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 return ret;
1272}
1273EXPORT_SYMBOL_GPL(cpufreq_governor);
1274
1275
1276int cpufreq_register_governor(struct cpufreq_governor *governor)
1277{
1278 struct cpufreq_governor *t;
1279
1280 if (!governor)
1281 return -EINVAL;
1282
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001283 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1286 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001287 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return -EBUSY;
1289 }
1290 }
1291 list_add(&governor->governor_list, &cpufreq_governor_list);
1292
Dave Jones32ee8c32006-02-28 00:43:23 -05001293 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return 0;
1295}
1296EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1297
1298
1299void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1300{
1301 if (!governor)
1302 return;
1303
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001304 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001306 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return;
1308}
1309EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1310
1311
1312
1313/*********************************************************************
1314 * POLICY INTERFACE *
1315 *********************************************************************/
1316
1317/**
1318 * cpufreq_get_policy - get the current cpufreq_policy
1319 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1320 *
1321 * Reads the current cpufreq policy.
1322 */
1323int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1324{
1325 struct cpufreq_policy *cpu_policy;
1326 if (!policy)
1327 return -EINVAL;
1328
1329 cpu_policy = cpufreq_cpu_get(cpu);
1330 if (!cpu_policy)
1331 return -EINVAL;
1332
Arjan van de Ven83933af2006-01-14 16:01:49 +01001333 mutex_lock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Arjan van de Ven83933af2006-01-14 16:01:49 +01001335 mutex_unlock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 return 0;
1339}
1340EXPORT_SYMBOL(cpufreq_get_policy);
1341
1342
1343static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1344{
1345 int ret = 0;
1346
1347 cpufreq_debug_disable_ratelimit();
1348 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1349 policy->min, policy->max);
1350
Dave Jones7d5e3502006-02-02 17:03:42 -05001351 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /* verify the cpu speed can be set within this limit */
1354 ret = cpufreq_driver->verify(policy);
1355 if (ret)
1356 goto error_out;
1357
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001359 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1360 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001363 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1364 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 /* verify the cpu speed can be set within this limit,
1367 which might be different to the first one */
1368 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001369 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001373 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1374 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
Dave Jones7d5e3502006-02-02 17:03:42 -05001376 data->min = policy->min;
1377 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1380
1381 if (cpufreq_driver->setpolicy) {
1382 data->policy = policy->policy;
1383 dprintk("setting range\n");
1384 ret = cpufreq_driver->setpolicy(policy);
1385 } else {
1386 if (policy->governor != data->governor) {
1387 /* save old, working values */
1388 struct cpufreq_governor *old_gov = data->governor;
1389
1390 dprintk("governor switch\n");
1391
1392 /* end old governor */
1393 if (data->governor)
1394 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1395
1396 /* start new governor */
1397 data->governor = policy->governor;
1398 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1399 /* new governor failed, so re-start old one */
1400 dprintk("starting governor %s failed\n", data->governor->name);
1401 if (old_gov) {
1402 data->governor = old_gov;
1403 __cpufreq_governor(data, CPUFREQ_GOV_START);
1404 }
1405 ret = -EINVAL;
1406 goto error_out;
1407 }
1408 /* might be a policy change, too, so fall through */
1409 }
1410 dprintk("governor: change or update limits\n");
1411 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1412 }
1413
Dave Jones7d5e3502006-02-02 17:03:42 -05001414error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 cpufreq_debug_enable_ratelimit();
1416 return ret;
1417}
1418
1419/**
1420 * cpufreq_set_policy - set a new CPUFreq policy
1421 * @policy: policy to be set.
1422 *
1423 * Sets a new CPU frequency and voltage scaling policy.
1424 */
1425int cpufreq_set_policy(struct cpufreq_policy *policy)
1426{
1427 int ret = 0;
1428 struct cpufreq_policy *data;
1429
1430 if (!policy)
1431 return -EINVAL;
1432
1433 data = cpufreq_cpu_get(policy->cpu);
1434 if (!data)
1435 return -EINVAL;
1436
1437 /* lock this CPU */
Arjan van de Ven83933af2006-01-14 16:01:49 +01001438 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 ret = __cpufreq_set_policy(data, policy);
1441 data->user_policy.min = data->min;
1442 data->user_policy.max = data->max;
1443 data->user_policy.policy = data->policy;
1444 data->user_policy.governor = data->governor;
1445
Arjan van de Ven83933af2006-01-14 16:01:49 +01001446 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 cpufreq_cpu_put(data);
1448
1449 return ret;
1450}
1451EXPORT_SYMBOL(cpufreq_set_policy);
1452
1453
1454/**
1455 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1456 * @cpu: CPU which shall be re-evaluated
1457 *
1458 * Usefull for policy notifiers which have different necessities
1459 * at different times.
1460 */
1461int cpufreq_update_policy(unsigned int cpu)
1462{
1463 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1464 struct cpufreq_policy policy;
1465 int ret = 0;
1466
1467 if (!data)
1468 return -ENODEV;
1469
Arjan van de Ven83933af2006-01-14 16:01:49 +01001470 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001473 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 policy.min = data->user_policy.min;
1475 policy.max = data->user_policy.max;
1476 policy.policy = data->user_policy.policy;
1477 policy.governor = data->user_policy.governor;
1478
Thomas Renninger0961dd02006-01-26 18:46:33 +01001479 /* BIOS might change freq behind our back
1480 -> ask driver for current freq and notify governors about a change */
1481 if (cpufreq_driver->get) {
1482 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001483 if (!data->cur) {
1484 dprintk("Driver did not initialize current freq");
1485 data->cur = policy.cur;
1486 } else {
1487 if (data->cur != policy.cur)
1488 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1489 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001490 }
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 ret = __cpufreq_set_policy(data, &policy);
1493
Arjan van de Ven83933af2006-01-14 16:01:49 +01001494 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
1496 cpufreq_cpu_put(data);
1497 return ret;
1498}
1499EXPORT_SYMBOL(cpufreq_update_policy);
1500
Ashok Rajc32b6b82005-10-30 14:59:54 -08001501static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1502 unsigned long action, void *hcpu)
1503{
1504 unsigned int cpu = (unsigned long)hcpu;
1505 struct cpufreq_policy *policy;
1506 struct sys_device *sys_dev;
1507
1508 sys_dev = get_cpu_sysdev(cpu);
1509
1510 if (sys_dev) {
1511 switch (action) {
1512 case CPU_ONLINE:
1513 cpufreq_add_dev(sys_dev);
1514 break;
1515 case CPU_DOWN_PREPARE:
1516 /*
1517 * We attempt to put this cpu in lowest frequency
1518 * possible before going down. This will permit
1519 * hardware-managed P-State to switch other related
1520 * threads to min or higher speeds if possible.
1521 */
1522 policy = cpufreq_cpu_data[cpu];
1523 if (policy) {
1524 cpufreq_driver_target(policy, policy->min,
1525 CPUFREQ_RELATION_H);
1526 }
1527 break;
1528 case CPU_DEAD:
1529 cpufreq_remove_dev(sys_dev);
1530 break;
1531 }
1532 }
1533 return NOTIFY_OK;
1534}
1535
1536static struct notifier_block cpufreq_cpu_notifier =
1537{
1538 .notifier_call = cpufreq_cpu_callback,
1539};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541/*********************************************************************
1542 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1543 *********************************************************************/
1544
1545/**
1546 * cpufreq_register_driver - register a CPU Frequency driver
1547 * @driver_data: A struct cpufreq_driver containing the values#
1548 * submitted by the CPU Frequency driver.
1549 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001550 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001552 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 *
1554 */
1555int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1556{
1557 unsigned long flags;
1558 int ret;
1559
1560 if (!driver_data || !driver_data->verify || !driver_data->init ||
1561 ((!driver_data->setpolicy) && (!driver_data->target)))
1562 return -EINVAL;
1563
1564 dprintk("trying to register driver %s\n", driver_data->name);
1565
1566 if (driver_data->setpolicy)
1567 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1568
1569 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1570 if (cpufreq_driver) {
1571 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1572 return -EBUSY;
1573 }
1574 cpufreq_driver = driver_data;
1575 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1576
1577 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1578
1579 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1580 int i;
1581 ret = -ENODEV;
1582
1583 /* check for at least one working CPU */
1584 for (i=0; i<NR_CPUS; i++)
1585 if (cpufreq_cpu_data[i])
1586 ret = 0;
1587
1588 /* if all ->init() calls failed, unregister */
1589 if (ret) {
1590 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1591 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1592
1593 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1594 cpufreq_driver = NULL;
1595 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1596 }
1597 }
1598
1599 if (!ret) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001600 register_cpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 dprintk("driver %s up and running\n", driver_data->name);
1602 cpufreq_debug_enable_ratelimit();
1603 }
1604
1605 return (ret);
1606}
1607EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1608
1609
1610/**
1611 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1612 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001613 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 * the right to do so, i.e. if you have succeeded in initialising before!
1615 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1616 * currently not initialised.
1617 */
1618int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1619{
1620 unsigned long flags;
1621
1622 cpufreq_debug_disable_ratelimit();
1623
1624 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1625 cpufreq_debug_enable_ratelimit();
1626 return -EINVAL;
1627 }
1628
1629 dprintk("unregistering driver %s\n", driver->name);
1630
1631 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001632 unregister_cpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1635 cpufreq_driver = NULL;
1636 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1637
1638 return 0;
1639}
1640EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);