blob: 3d0430741b5a2974924ce5ff35815e75d9422e9e [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
349/**
350 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
351 */
352#define store_one(file_name, object) \
353static ssize_t store_##file_name \
354(struct cpufreq_policy * policy, const char *buf, size_t count) \
355{ \
356 unsigned int ret = -EINVAL; \
357 struct cpufreq_policy new_policy; \
358 \
359 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
360 if (ret) \
361 return -EINVAL; \
362 \
363 ret = sscanf (buf, "%u", &new_policy.object); \
364 if (ret != 1) \
365 return -EINVAL; \
366 \
367 ret = cpufreq_set_policy(&new_policy); \
368 \
369 return ret ? ret : count; \
370}
371
372store_one(scaling_min_freq,min);
373store_one(scaling_max_freq,max);
374
375/**
376 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
377 */
378static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
379{
380 unsigned int cur_freq = cpufreq_get(policy->cpu);
381 if (!cur_freq)
382 return sprintf(buf, "<unknown>");
383 return sprintf(buf, "%u\n", cur_freq);
384}
385
386
387/**
388 * show_scaling_governor - show the current policy for the specified CPU
389 */
390static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
391{
392 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
393 return sprintf(buf, "powersave\n");
394 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
395 return sprintf(buf, "performance\n");
396 else if (policy->governor)
397 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
398 return -EINVAL;
399}
400
401
402/**
403 * store_scaling_governor - store policy for the specified CPU
404 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500405static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
406 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
408 unsigned int ret = -EINVAL;
409 char str_governor[16];
410 struct cpufreq_policy new_policy;
411
412 ret = cpufreq_get_policy(&new_policy, policy->cpu);
413 if (ret)
414 return ret;
415
416 ret = sscanf (buf, "%15s", str_governor);
417 if (ret != 1)
418 return -EINVAL;
419
420 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
421 return -EINVAL;
422
423 ret = cpufreq_set_policy(&new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return ret ? ret : count;
425}
426
427/**
428 * show_scaling_driver - show the cpufreq driver currently loaded
429 */
430static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
431{
432 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
433}
434
435/**
436 * show_scaling_available_governors - show the available CPUfreq governors
437 */
438static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
439 char *buf)
440{
441 ssize_t i = 0;
442 struct cpufreq_governor *t;
443
444 if (!cpufreq_driver->target) {
445 i += sprintf(buf, "performance powersave");
446 goto out;
447 }
448
449 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
450 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
451 goto out;
452 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
453 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500454out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 i += sprintf(&buf[i], "\n");
456 return i;
457}
458/**
459 * show_affected_cpus - show the CPUs affected by each transition
460 */
461static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
462{
463 ssize_t i = 0;
464 unsigned int cpu;
465
466 for_each_cpu_mask(cpu, policy->cpus) {
467 if (i)
468 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
469 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
470 if (i >= (PAGE_SIZE - 5))
471 break;
472 }
473 i += sprintf(&buf[i], "\n");
474 return i;
475}
476
477
478#define define_one_ro(_name) \
479static struct freq_attr _name = \
480__ATTR(_name, 0444, show_##_name, NULL)
481
482#define define_one_ro0400(_name) \
483static struct freq_attr _name = \
484__ATTR(_name, 0400, show_##_name, NULL)
485
486#define define_one_rw(_name) \
487static struct freq_attr _name = \
488__ATTR(_name, 0644, show_##_name, store_##_name)
489
490define_one_ro0400(cpuinfo_cur_freq);
491define_one_ro(cpuinfo_min_freq);
492define_one_ro(cpuinfo_max_freq);
493define_one_ro(scaling_available_governors);
494define_one_ro(scaling_driver);
495define_one_ro(scaling_cur_freq);
496define_one_ro(affected_cpus);
497define_one_rw(scaling_min_freq);
498define_one_rw(scaling_max_freq);
499define_one_rw(scaling_governor);
500
501static struct attribute * default_attrs[] = {
502 &cpuinfo_min_freq.attr,
503 &cpuinfo_max_freq.attr,
504 &scaling_min_freq.attr,
505 &scaling_max_freq.attr,
506 &affected_cpus.attr,
507 &scaling_governor.attr,
508 &scaling_driver.attr,
509 &scaling_available_governors.attr,
510 NULL
511};
512
513#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
514#define to_attr(a) container_of(a,struct freq_attr,attr)
515
516static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
517{
518 struct cpufreq_policy * policy = to_policy(kobj);
519 struct freq_attr * fattr = to_attr(attr);
520 ssize_t ret;
521 policy = cpufreq_cpu_get(policy->cpu);
522 if (!policy)
523 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500524 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 cpufreq_cpu_put(policy);
526 return ret;
527}
528
Dave Jones32ee8c32006-02-28 00:43:23 -0500529static ssize_t store(struct kobject * kobj, struct attribute * attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 const char * buf, size_t count)
531{
532 struct cpufreq_policy * policy = to_policy(kobj);
533 struct freq_attr * fattr = to_attr(attr);
534 ssize_t ret;
535 policy = cpufreq_cpu_get(policy->cpu);
536 if (!policy)
537 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500538 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 cpufreq_cpu_put(policy);
540 return ret;
541}
542
543static void cpufreq_sysfs_release(struct kobject * kobj)
544{
545 struct cpufreq_policy * policy = to_policy(kobj);
546 dprintk("last reference is dropped\n");
547 complete(&policy->kobj_unregister);
548}
549
550static struct sysfs_ops sysfs_ops = {
551 .show = show,
552 .store = store,
553};
554
555static struct kobj_type ktype_cpufreq = {
556 .sysfs_ops = &sysfs_ops,
557 .default_attrs = default_attrs,
558 .release = cpufreq_sysfs_release,
559};
560
561
562/**
563 * cpufreq_add_dev - add a CPU device
564 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500565 * Adds the cpufreq interface for a CPU device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
567static int cpufreq_add_dev (struct sys_device * sys_dev)
568{
569 unsigned int cpu = sys_dev->id;
570 int ret = 0;
571 struct cpufreq_policy new_policy;
572 struct cpufreq_policy *policy;
573 struct freq_attr **drv_attr;
Dave Jones8ff69732006-03-05 03:37:23 -0500574 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 unsigned long flags;
576 unsigned int j;
Dave Jones8ff69732006-03-05 03:37:23 -0500577#ifdef CONFIG_SMP
578 struct cpufreq_policy *managed_policy;
579#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Ashok Rajc32b6b82005-10-30 14:59:54 -0800581 if (cpu_is_offline(cpu))
582 return 0;
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 cpufreq_debug_disable_ratelimit();
585 dprintk("adding CPU %u\n", cpu);
586
587#ifdef CONFIG_SMP
588 /* check whether a different CPU already registered this
589 * CPU because it is in the same boat. */
590 policy = cpufreq_cpu_get(cpu);
591 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500592 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 cpufreq_debug_enable_ratelimit();
594 return 0;
595 }
596#endif
597
598 if (!try_module_get(cpufreq_driver->owner)) {
599 ret = -EINVAL;
600 goto module_out;
601 }
602
Dave Jonese98df502005-10-20 15:17:43 -0700603 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!policy) {
605 ret = -ENOMEM;
606 goto nomem_out;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 policy->cpu = cpu;
610 policy->cpus = cpumask_of_cpu(cpu);
611
Arjan van de Ven83933af2006-01-14 16:01:49 +0100612 mutex_init(&policy->lock);
613 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 init_completion(&policy->kobj_unregister);
615 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
616
617 /* call driver. From then on the cpufreq must be able
618 * to accept all calls to ->verify and ->setpolicy for this CPU
619 */
620 ret = cpufreq_driver->init(policy);
621 if (ret) {
622 dprintk("initialization failed\n");
Andrew Mortonf3876c12006-01-18 13:40:54 -0800623 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 goto err_out;
625 }
626
Dave Jones8ff69732006-03-05 03:37:23 -0500627#ifdef CONFIG_SMP
628 for_each_cpu_mask(j, policy->cpus) {
629 if (cpu == j)
630 continue;
631
632 /* check for existing affected CPUs. They may not be aware
633 * of it due to CPU Hotplug.
634 */
635 managed_policy = cpufreq_cpu_get(j);
636 if (unlikely(managed_policy)) {
637 spin_lock_irqsave(&cpufreq_driver_lock, flags);
638 managed_policy->cpus = policy->cpus;
639 cpufreq_cpu_data[cpu] = managed_policy;
640 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
641
642 dprintk("CPU already managed, adding link\n");
643 sysfs_create_link(&sys_dev->kobj,
644 &managed_policy->kobj, "cpufreq");
645
646 cpufreq_debug_enable_ratelimit();
647 mutex_unlock(&policy->lock);
648 ret = 0;
649 goto err_out_driver_exit; /* call driver->exit() */
650 }
651 }
652#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
654
655 /* prepare interface data */
656 policy->kobj.parent = &sys_dev->kobj;
657 policy->kobj.ktype = &ktype_cpufreq;
658 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
659
660 ret = kobject_register(&policy->kobj);
Andrew Mortonf3876c12006-01-18 13:40:54 -0800661 if (ret) {
662 mutex_unlock(&policy->lock);
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700663 goto err_out_driver_exit;
Andrew Mortonf3876c12006-01-18 13:40:54 -0800664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 /* set up files for this cpu device */
666 drv_attr = cpufreq_driver->attr;
667 while ((drv_attr) && (*drv_attr)) {
668 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
669 drv_attr++;
670 }
671 if (cpufreq_driver->get)
672 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
673 if (cpufreq_driver->target)
674 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
675
676 spin_lock_irqsave(&cpufreq_driver_lock, flags);
677 for_each_cpu_mask(j, policy->cpus)
678 cpufreq_cpu_data[j] = policy;
679 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones8ff69732006-03-05 03:37:23 -0500680
681 /* symlink affected CPUs */
682 for_each_cpu_mask(j, policy->cpus) {
683 if (j == cpu)
684 continue;
685 if (!cpu_online(j))
686 continue;
687
Dave Jones1f8b2c92006-03-29 01:40:04 -0500688 dprintk("CPU %u already managed, adding link\n", j);
Dave Jones8ff69732006-03-05 03:37:23 -0500689 cpufreq_cpu_get(cpu);
690 cpu_sys_dev = get_cpu_sysdev(j);
691 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
692 "cpufreq");
693 }
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 policy->governor = NULL; /* to assure that the starting sequence is
696 * run in cpufreq_set_policy */
Arjan van de Ven83933af2006-01-14 16:01:49 +0100697 mutex_unlock(&policy->lock);
Dave Jones87c32272006-03-29 01:48:37 -0500698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 /* set default policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 ret = cpufreq_set_policy(&new_policy);
701 if (ret) {
702 dprintk("setting policy failed\n");
703 goto err_out_unregister;
704 }
705
706 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 dprintk("initialization complete\n");
708 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -0500709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 return 0;
711
712
713err_out_unregister:
714 spin_lock_irqsave(&cpufreq_driver_lock, flags);
715 for_each_cpu_mask(j, policy->cpus)
716 cpufreq_cpu_data[j] = NULL;
717 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
718
719 kobject_unregister(&policy->kobj);
720 wait_for_completion(&policy->kobj_unregister);
721
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700722err_out_driver_exit:
723 if (cpufreq_driver->exit)
724 cpufreq_driver->exit(policy);
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726err_out:
727 kfree(policy);
728
729nomem_out:
730 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800731module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 cpufreq_debug_enable_ratelimit();
733 return ret;
734}
735
736
737/**
738 * cpufreq_remove_dev - remove a CPU device
739 *
740 * Removes the cpufreq interface for a CPU device.
741 */
742static int cpufreq_remove_dev (struct sys_device * sys_dev)
743{
744 unsigned int cpu = sys_dev->id;
745 unsigned long flags;
746 struct cpufreq_policy *data;
747#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -0800748 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 unsigned int j;
750#endif
751
752 cpufreq_debug_disable_ratelimit();
753 dprintk("unregistering CPU %u\n", cpu);
754
755 spin_lock_irqsave(&cpufreq_driver_lock, flags);
756 data = cpufreq_cpu_data[cpu];
757
758 if (!data) {
759 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 cpufreq_debug_enable_ratelimit();
761 return -EINVAL;
762 }
763 cpufreq_cpu_data[cpu] = NULL;
764
765
766#ifdef CONFIG_SMP
767 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -0500768 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 */
770 if (unlikely(cpu != data->cpu)) {
771 dprintk("removing link\n");
Dave Jones8ff69732006-03-05 03:37:23 -0500772 cpu_clear(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
774 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 cpufreq_cpu_put(data);
776 cpufreq_debug_enable_ratelimit();
777 return 0;
778 }
779#endif
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 if (!kobject_get(&data->kobj)) {
783 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
784 cpufreq_debug_enable_ratelimit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500785 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
788#ifdef CONFIG_SMP
789 /* if we have other CPUs still registered, we need to unlink them,
790 * or else wait_for_completion below will lock up. Clean the
791 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
792 * links afterwards.
793 */
794 if (unlikely(cpus_weight(data->cpus) > 1)) {
795 for_each_cpu_mask(j, data->cpus) {
796 if (j == cpu)
797 continue;
798 cpufreq_cpu_data[j] = NULL;
799 }
800 }
801
802 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
803
804 if (unlikely(cpus_weight(data->cpus) > 1)) {
805 for_each_cpu_mask(j, data->cpus) {
806 if (j == cpu)
807 continue;
808 dprintk("removing link for cpu %u\n", j);
Ashok Rajd434fca2005-10-30 14:59:52 -0800809 cpu_sys_dev = get_cpu_sysdev(j);
810 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 cpufreq_cpu_put(data);
812 }
813 }
814#else
815 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
816#endif
817
Arjan van de Ven83933af2006-01-14 16:01:49 +0100818 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (cpufreq_driver->target)
820 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Arjan van de Ven83933af2006-01-14 16:01:49 +0100821 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 kobject_unregister(&data->kobj);
824
825 kobject_put(&data->kobj);
826
827 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -0500828 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 * unloading.
830 */
831 dprintk("waiting for dropping of refcount\n");
832 wait_for_completion(&data->kobj_unregister);
833 dprintk("wait complete\n");
834
835 if (cpufreq_driver->exit)
836 cpufreq_driver->exit(data);
837
838 kfree(data);
839
840 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return 0;
842}
843
844
845static void handle_update(void *data)
846{
847 unsigned int cpu = (unsigned int)(long)data;
848 dprintk("handle_update for cpu %u called\n", cpu);
849 cpufreq_update_policy(cpu);
850}
851
852/**
853 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
854 * @cpu: cpu number
855 * @old_freq: CPU frequency the kernel thinks the CPU runs at
856 * @new_freq: CPU frequency the CPU actually runs at
857 *
858 * We adjust to current frequency first, and need to clean up later. So either call
859 * to cpufreq_update_policy() or schedule handle_update()).
860 */
861static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
862{
863 struct cpufreq_freqs freqs;
864
Dave Jones78ee9982005-05-31 19:03:43 -0700865 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
867
868 freqs.cpu = cpu;
869 freqs.old = old_freq;
870 freqs.new = new_freq;
871 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
872 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
873}
874
875
Dave Jones32ee8c32006-02-28 00:43:23 -0500876/**
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800877 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
878 * @cpu: CPU number
879 *
880 * This is the last known freq, without actually getting it from the driver.
881 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
882 */
883unsigned int cpufreq_quick_get(unsigned int cpu)
884{
885 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
886 unsigned int ret = 0;
887
888 if (policy) {
Arjan van de Ven83933af2006-01-14 16:01:49 +0100889 mutex_lock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800890 ret = policy->cur;
Arjan van de Ven83933af2006-01-14 16:01:49 +0100891 mutex_unlock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800892 cpufreq_cpu_put(policy);
893 }
894
895 return (ret);
896}
897EXPORT_SYMBOL(cpufreq_quick_get);
898
899
Dave Jones32ee8c32006-02-28 00:43:23 -0500900/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 * cpufreq_get - get the current CPU frequency (in kHz)
902 * @cpu: CPU number
903 *
904 * Get the CPU current (static) CPU frequency
905 */
906unsigned int cpufreq_get(unsigned int cpu)
907{
908 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
909 unsigned int ret = 0;
910
911 if (!policy)
912 return 0;
913
914 if (!cpufreq_driver->get)
915 goto out;
916
Arjan van de Ven83933af2006-01-14 16:01:49 +0100917 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 ret = cpufreq_driver->get(cpu);
920
Dave Jones7d5e3502006-02-02 17:03:42 -0500921 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /* verify no discrepancy between actual and saved value exists */
923 if (unlikely(ret != policy->cur)) {
924 cpufreq_out_of_sync(cpu, policy->cur, ret);
925 schedule_work(&policy->update);
926 }
927 }
928
Arjan van de Ven83933af2006-01-14 16:01:49 +0100929 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Dave Jones7d5e3502006-02-02 17:03:42 -0500931out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 cpufreq_cpu_put(policy);
933
934 return (ret);
935}
936EXPORT_SYMBOL(cpufreq_get);
937
938
939/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700940 * cpufreq_suspend - let the low level driver prepare for suspend
941 */
942
Bernard Blackhame00d9962005-07-07 17:56:42 -0700943static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700944{
945 int cpu = sysdev->id;
946 unsigned int ret = 0;
947 unsigned int cur_freq = 0;
948 struct cpufreq_policy *cpu_policy;
949
950 dprintk("resuming cpu %u\n", cpu);
951
952 if (!cpu_online(cpu))
953 return 0;
954
955 /* we may be lax here as interrupts are off. Nonetheless
956 * we need to grab the correct cpu policy, as to check
957 * whether we really run on this CPU.
958 */
959
960 cpu_policy = cpufreq_cpu_get(cpu);
961 if (!cpu_policy)
962 return -EINVAL;
963
964 /* only handle each CPU group once */
965 if (unlikely(cpu_policy->cpu != cpu)) {
966 cpufreq_cpu_put(cpu_policy);
967 return 0;
968 }
969
970 if (cpufreq_driver->suspend) {
Bernard Blackhame00d9962005-07-07 17:56:42 -0700971 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700972 if (ret) {
973 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
974 "step on CPU %u\n", cpu_policy->cpu);
975 cpufreq_cpu_put(cpu_policy);
976 return ret;
977 }
978 }
979
980
981 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
982 goto out;
983
984 if (cpufreq_driver->get)
985 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
986
987 if (!cur_freq || !cpu_policy->cur) {
988 printk(KERN_ERR "cpufreq: suspend failed to assert current "
989 "frequency is what timing core thinks it is.\n");
990 goto out;
991 }
992
993 if (unlikely(cur_freq != cpu_policy->cur)) {
994 struct cpufreq_freqs freqs;
995
996 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Dave Jones78ee9982005-05-31 19:03:43 -0700997 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700998 "cpufreq assumed %u kHz.\n",
999 cur_freq, cpu_policy->cur);
1000
1001 freqs.cpu = cpu;
1002 freqs.old = cpu_policy->cur;
1003 freqs.new = cur_freq;
1004
Alan Sterne041c682006-03-27 01:16:30 -08001005 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001006 CPUFREQ_SUSPENDCHANGE, &freqs);
1007 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1008
1009 cpu_policy->cur = cur_freq;
1010 }
1011
Dave Jones7d5e3502006-02-02 17:03:42 -05001012out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001013 cpufreq_cpu_put(cpu_policy);
1014 return 0;
1015}
1016
1017/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 * cpufreq_resume - restore proper CPU frequency handling after resume
1019 *
1020 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1021 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001022 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1023 * restored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
1025static int cpufreq_resume(struct sys_device * sysdev)
1026{
1027 int cpu = sysdev->id;
1028 unsigned int ret = 0;
1029 struct cpufreq_policy *cpu_policy;
1030
1031 dprintk("resuming cpu %u\n", cpu);
1032
1033 if (!cpu_online(cpu))
1034 return 0;
1035
1036 /* we may be lax here as interrupts are off. Nonetheless
1037 * we need to grab the correct cpu policy, as to check
1038 * whether we really run on this CPU.
1039 */
1040
1041 cpu_policy = cpufreq_cpu_get(cpu);
1042 if (!cpu_policy)
1043 return -EINVAL;
1044
1045 /* only handle each CPU group once */
1046 if (unlikely(cpu_policy->cpu != cpu)) {
1047 cpufreq_cpu_put(cpu_policy);
1048 return 0;
1049 }
1050
1051 if (cpufreq_driver->resume) {
1052 ret = cpufreq_driver->resume(cpu_policy);
1053 if (ret) {
1054 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1055 "step on CPU %u\n", cpu_policy->cpu);
1056 cpufreq_cpu_put(cpu_policy);
1057 return ret;
1058 }
1059 }
1060
1061 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1062 unsigned int cur_freq = 0;
1063
1064 if (cpufreq_driver->get)
1065 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1066
1067 if (!cur_freq || !cpu_policy->cur) {
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001068 printk(KERN_ERR "cpufreq: resume failed to assert "
1069 "current frequency is what timing core "
1070 "thinks it is.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto out;
1072 }
1073
1074 if (unlikely(cur_freq != cpu_policy->cur)) {
1075 struct cpufreq_freqs freqs;
1076
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001077 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Dave Jones78ee9982005-05-31 19:03:43 -07001078 dprintk(KERN_WARNING "Warning: CPU frequency"
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001079 "is %u, cpufreq assumed %u kHz.\n",
1080 cur_freq, cpu_policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 freqs.cpu = cpu;
1083 freqs.old = cpu_policy->cur;
1084 freqs.new = cur_freq;
1085
Alan Sterne041c682006-03-27 01:16:30 -08001086 blocking_notifier_call_chain(
1087 &cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001088 CPUFREQ_RESUMECHANGE, &freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1090
1091 cpu_policy->cur = cur_freq;
1092 }
1093 }
1094
1095out:
1096 schedule_work(&cpu_policy->update);
1097 cpufreq_cpu_put(cpu_policy);
1098 return ret;
1099}
1100
1101static struct sysdev_driver cpufreq_sysdev_driver = {
1102 .add = cpufreq_add_dev,
1103 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001104 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 .resume = cpufreq_resume,
1106};
1107
1108
1109/*********************************************************************
1110 * NOTIFIER LISTS INTERFACE *
1111 *********************************************************************/
1112
1113/**
1114 * cpufreq_register_notifier - register a driver with cpufreq
1115 * @nb: notifier function to register
1116 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1117 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001118 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 * are notified about clock rate changes (once before and once after
1120 * the transition), or a list of drivers that are notified about
1121 * changes in cpufreq policy.
1122 *
1123 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001124 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
1126int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1127{
1128 int ret;
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 switch (list) {
1131 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001132 ret = blocking_notifier_chain_register(
1133 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001136 ret = blocking_notifier_chain_register(
1137 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 break;
1139 default:
1140 ret = -EINVAL;
1141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 return ret;
1144}
1145EXPORT_SYMBOL(cpufreq_register_notifier);
1146
1147
1148/**
1149 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1150 * @nb: notifier block to be unregistered
1151 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1152 *
1153 * Remove a driver from the CPU frequency notifier list.
1154 *
1155 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001156 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 */
1158int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1159{
1160 int ret;
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 switch (list) {
1163 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001164 ret = blocking_notifier_chain_unregister(
1165 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 break;
1167 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001168 ret = blocking_notifier_chain_unregister(
1169 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 break;
1171 default:
1172 ret = -EINVAL;
1173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 return ret;
1176}
1177EXPORT_SYMBOL(cpufreq_unregister_notifier);
1178
1179
1180/*********************************************************************
1181 * GOVERNORS *
1182 *********************************************************************/
1183
1184
1185int __cpufreq_driver_target(struct cpufreq_policy *policy,
1186 unsigned int target_freq,
1187 unsigned int relation)
1188{
1189 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001190
Ashok Raja9d9baa2005-11-28 13:43:46 -08001191 lock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1193 target_freq, relation);
1194 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1195 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001196
Ashok Raja9d9baa2005-11-28 13:43:46 -08001197 unlock_cpu_hotplug();
Ashok Raj90d45d12005-11-08 21:34:24 -08001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return retval;
1200}
1201EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203int cpufreq_driver_target(struct cpufreq_policy *policy,
1204 unsigned int target_freq,
1205 unsigned int relation)
1206{
Dave Jonescc993ca2005-07-28 09:43:56 -07001207 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 policy = cpufreq_cpu_get(policy->cpu);
1210 if (!policy)
1211 return -EINVAL;
1212
Arjan van de Ven83933af2006-01-14 16:01:49 +01001213 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 ret = __cpufreq_driver_target(policy, target_freq, relation);
1216
Arjan van de Ven83933af2006-01-14 16:01:49 +01001217 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return ret;
1221}
1222EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1223
1224
1225static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1226{
Dave Jonescc993ca2005-07-28 09:43:56 -07001227 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 if (!try_module_get(policy->governor->owner))
1230 return -EINVAL;
1231
1232 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1233 ret = policy->governor->governor(policy, event);
1234
1235 /* we keep one module reference alive for each CPU governed by this CPU */
1236 if ((event != CPUFREQ_GOV_START) || ret)
1237 module_put(policy->governor->owner);
1238 if ((event == CPUFREQ_GOV_STOP) && !ret)
1239 module_put(policy->governor->owner);
1240
1241 return ret;
1242}
1243
1244
1245int cpufreq_governor(unsigned int cpu, unsigned int event)
1246{
1247 int ret = 0;
1248 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1249
1250 if (!policy)
1251 return -EINVAL;
1252
Arjan van de Ven83933af2006-01-14 16:01:49 +01001253 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 ret = __cpufreq_governor(policy, event);
Arjan van de Ven83933af2006-01-14 16:01:49 +01001255 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return ret;
1259}
1260EXPORT_SYMBOL_GPL(cpufreq_governor);
1261
1262
1263int cpufreq_register_governor(struct cpufreq_governor *governor)
1264{
1265 struct cpufreq_governor *t;
1266
1267 if (!governor)
1268 return -EINVAL;
1269
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001270 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1273 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001274 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return -EBUSY;
1276 }
1277 }
1278 list_add(&governor->governor_list, &cpufreq_governor_list);
1279
Dave Jones32ee8c32006-02-28 00:43:23 -05001280 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 return 0;
1282}
1283EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1284
1285
1286void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1287{
1288 if (!governor)
1289 return;
1290
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001291 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001293 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return;
1295}
1296EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1297
1298
1299
1300/*********************************************************************
1301 * POLICY INTERFACE *
1302 *********************************************************************/
1303
1304/**
1305 * cpufreq_get_policy - get the current cpufreq_policy
1306 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1307 *
1308 * Reads the current cpufreq policy.
1309 */
1310int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1311{
1312 struct cpufreq_policy *cpu_policy;
1313 if (!policy)
1314 return -EINVAL;
1315
1316 cpu_policy = cpufreq_cpu_get(cpu);
1317 if (!cpu_policy)
1318 return -EINVAL;
1319
Arjan van de Ven83933af2006-01-14 16:01:49 +01001320 mutex_lock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Arjan van de Ven83933af2006-01-14 16:01:49 +01001322 mutex_unlock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
1324 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return 0;
1326}
1327EXPORT_SYMBOL(cpufreq_get_policy);
1328
1329
1330static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1331{
1332 int ret = 0;
1333
1334 cpufreq_debug_disable_ratelimit();
1335 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1336 policy->min, policy->max);
1337
Dave Jones7d5e3502006-02-02 17:03:42 -05001338 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 /* verify the cpu speed can be set within this limit */
1341 ret = cpufreq_driver->verify(policy);
1342 if (ret)
1343 goto error_out;
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001346 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1347 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001350 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1351 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /* verify the cpu speed can be set within this limit,
1354 which might be different to the first one */
1355 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001356 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001360 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1361 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Dave Jones7d5e3502006-02-02 17:03:42 -05001363 data->min = policy->min;
1364 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1367
1368 if (cpufreq_driver->setpolicy) {
1369 data->policy = policy->policy;
1370 dprintk("setting range\n");
1371 ret = cpufreq_driver->setpolicy(policy);
1372 } else {
1373 if (policy->governor != data->governor) {
1374 /* save old, working values */
1375 struct cpufreq_governor *old_gov = data->governor;
1376
1377 dprintk("governor switch\n");
1378
1379 /* end old governor */
1380 if (data->governor)
1381 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1382
1383 /* start new governor */
1384 data->governor = policy->governor;
1385 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1386 /* new governor failed, so re-start old one */
1387 dprintk("starting governor %s failed\n", data->governor->name);
1388 if (old_gov) {
1389 data->governor = old_gov;
1390 __cpufreq_governor(data, CPUFREQ_GOV_START);
1391 }
1392 ret = -EINVAL;
1393 goto error_out;
1394 }
1395 /* might be a policy change, too, so fall through */
1396 }
1397 dprintk("governor: change or update limits\n");
1398 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1399 }
1400
Dave Jones7d5e3502006-02-02 17:03:42 -05001401error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 cpufreq_debug_enable_ratelimit();
1403 return ret;
1404}
1405
1406/**
1407 * cpufreq_set_policy - set a new CPUFreq policy
1408 * @policy: policy to be set.
1409 *
1410 * Sets a new CPU frequency and voltage scaling policy.
1411 */
1412int cpufreq_set_policy(struct cpufreq_policy *policy)
1413{
1414 int ret = 0;
1415 struct cpufreq_policy *data;
1416
1417 if (!policy)
1418 return -EINVAL;
1419
1420 data = cpufreq_cpu_get(policy->cpu);
1421 if (!data)
1422 return -EINVAL;
1423
1424 /* lock this CPU */
Arjan van de Ven83933af2006-01-14 16:01:49 +01001425 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 ret = __cpufreq_set_policy(data, policy);
1428 data->user_policy.min = data->min;
1429 data->user_policy.max = data->max;
1430 data->user_policy.policy = data->policy;
1431 data->user_policy.governor = data->governor;
1432
Arjan van de Ven83933af2006-01-14 16:01:49 +01001433 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 cpufreq_cpu_put(data);
1435
1436 return ret;
1437}
1438EXPORT_SYMBOL(cpufreq_set_policy);
1439
1440
1441/**
1442 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1443 * @cpu: CPU which shall be re-evaluated
1444 *
1445 * Usefull for policy notifiers which have different necessities
1446 * at different times.
1447 */
1448int cpufreq_update_policy(unsigned int cpu)
1449{
1450 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1451 struct cpufreq_policy policy;
1452 int ret = 0;
1453
1454 if (!data)
1455 return -ENODEV;
1456
Arjan van de Ven83933af2006-01-14 16:01:49 +01001457 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001460 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 policy.min = data->user_policy.min;
1462 policy.max = data->user_policy.max;
1463 policy.policy = data->user_policy.policy;
1464 policy.governor = data->user_policy.governor;
1465
Thomas Renninger0961dd02006-01-26 18:46:33 +01001466 /* BIOS might change freq behind our back
1467 -> ask driver for current freq and notify governors about a change */
1468 if (cpufreq_driver->get) {
1469 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001470 if (!data->cur) {
1471 dprintk("Driver did not initialize current freq");
1472 data->cur = policy.cur;
1473 } else {
1474 if (data->cur != policy.cur)
1475 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1476 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001477 }
1478
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 ret = __cpufreq_set_policy(data, &policy);
1480
Arjan van de Ven83933af2006-01-14 16:01:49 +01001481 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 cpufreq_cpu_put(data);
1484 return ret;
1485}
1486EXPORT_SYMBOL(cpufreq_update_policy);
1487
Ashok Rajc32b6b82005-10-30 14:59:54 -08001488static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1489 unsigned long action, void *hcpu)
1490{
1491 unsigned int cpu = (unsigned long)hcpu;
1492 struct cpufreq_policy *policy;
1493 struct sys_device *sys_dev;
1494
1495 sys_dev = get_cpu_sysdev(cpu);
1496
1497 if (sys_dev) {
1498 switch (action) {
1499 case CPU_ONLINE:
1500 cpufreq_add_dev(sys_dev);
1501 break;
1502 case CPU_DOWN_PREPARE:
1503 /*
1504 * We attempt to put this cpu in lowest frequency
1505 * possible before going down. This will permit
1506 * hardware-managed P-State to switch other related
1507 * threads to min or higher speeds if possible.
1508 */
1509 policy = cpufreq_cpu_data[cpu];
1510 if (policy) {
1511 cpufreq_driver_target(policy, policy->min,
1512 CPUFREQ_RELATION_H);
1513 }
1514 break;
1515 case CPU_DEAD:
1516 cpufreq_remove_dev(sys_dev);
1517 break;
1518 }
1519 }
1520 return NOTIFY_OK;
1521}
1522
1523static struct notifier_block cpufreq_cpu_notifier =
1524{
1525 .notifier_call = cpufreq_cpu_callback,
1526};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528/*********************************************************************
1529 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1530 *********************************************************************/
1531
1532/**
1533 * cpufreq_register_driver - register a CPU Frequency driver
1534 * @driver_data: A struct cpufreq_driver containing the values#
1535 * submitted by the CPU Frequency driver.
1536 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001537 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001539 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 *
1541 */
1542int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1543{
1544 unsigned long flags;
1545 int ret;
1546
1547 if (!driver_data || !driver_data->verify || !driver_data->init ||
1548 ((!driver_data->setpolicy) && (!driver_data->target)))
1549 return -EINVAL;
1550
1551 dprintk("trying to register driver %s\n", driver_data->name);
1552
1553 if (driver_data->setpolicy)
1554 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1555
1556 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1557 if (cpufreq_driver) {
1558 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1559 return -EBUSY;
1560 }
1561 cpufreq_driver = driver_data;
1562 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1563
1564 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1565
1566 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1567 int i;
1568 ret = -ENODEV;
1569
1570 /* check for at least one working CPU */
1571 for (i=0; i<NR_CPUS; i++)
1572 if (cpufreq_cpu_data[i])
1573 ret = 0;
1574
1575 /* if all ->init() calls failed, unregister */
1576 if (ret) {
1577 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1578 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1579
1580 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1581 cpufreq_driver = NULL;
1582 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1583 }
1584 }
1585
1586 if (!ret) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001587 register_cpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 dprintk("driver %s up and running\n", driver_data->name);
1589 cpufreq_debug_enable_ratelimit();
1590 }
1591
1592 return (ret);
1593}
1594EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1595
1596
1597/**
1598 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1599 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001600 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 * the right to do so, i.e. if you have succeeded in initialising before!
1602 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1603 * currently not initialised.
1604 */
1605int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1606{
1607 unsigned long flags;
1608
1609 cpufreq_debug_disable_ratelimit();
1610
1611 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1612 cpufreq_debug_enable_ratelimit();
1613 return -EINVAL;
1614 }
1615
1616 dprintk("unregistering driver %s\n", driver->name);
1617
1618 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001619 unregister_cpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
1621 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1622 cpufreq_driver = NULL;
1623 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1624
1625 return 0;
1626}
1627EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);