blob: 8d328186f774b81cfdd1eabf5c01d244f50115be [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08007 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05008 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -05009 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
10 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
15 *
16 */
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/notifier.h>
22#include <linux/cpufreq.h>
23#include <linux/delay.h>
24#include <linux/interrupt.h>
25#include <linux/spinlock.h>
26#include <linux/device.h>
27#include <linux/slab.h>
28#include <linux/cpu.h>
29#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080030#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
33
34/**
35 * The "cpufreq driver" - the arch- or hardware-dependend low
36 * level driver of CPUFreq support, and its spinlock. This lock
37 * also protects the cpufreq_cpu_data array.
38 */
Dave Jones7d5e3502006-02-02 17:03:42 -050039static struct cpufreq_driver *cpufreq_driver;
40static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static DEFINE_SPINLOCK(cpufreq_driver_lock);
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* internal prototypes */
44static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
45static void handle_update(void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/**
Dave Jones32ee8c32006-02-28 00:43:23 -050048 * Two notifier lists: the "policy" list is involved in the
49 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * "transition" list for kernel code that needs to handle
51 * changes to devices when the CPU clock speed changes.
52 * The mutex locks both lists.
53 */
Alan Sterne041c682006-03-27 01:16:30 -080054static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
55static BLOCKING_NOTIFIER_HEAD(cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57
58static LIST_HEAD(cpufreq_governor_list);
Dave Jones7d5e3502006-02-02 17:03:42 -050059static DEFINE_MUTEX (cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Dave Jones7d5e3502006-02-02 17:03:42 -050061struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 struct cpufreq_policy *data;
64 unsigned long flags;
65
66 if (cpu >= NR_CPUS)
67 goto err_out;
68
69 /* get the cpufreq driver */
70 spin_lock_irqsave(&cpufreq_driver_lock, flags);
71
72 if (!cpufreq_driver)
73 goto err_out_unlock;
74
75 if (!try_module_get(cpufreq_driver->owner))
76 goto err_out_unlock;
77
78
79 /* get the CPU */
80 data = cpufreq_cpu_data[cpu];
81
82 if (!data)
83 goto err_out_put_module;
84
85 if (!kobject_get(&data->kobj))
86 goto err_out_put_module;
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return data;
90
Dave Jones7d5e3502006-02-02 17:03:42 -050091err_out_put_module:
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 module_put(cpufreq_driver->owner);
Dave Jones7d5e3502006-02-02 17:03:42 -050093err_out_unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones7d5e3502006-02-02 17:03:42 -050095err_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return NULL;
97}
98EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
99
Dave Jones7d5e3502006-02-02 17:03:42 -0500100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101void cpufreq_cpu_put(struct cpufreq_policy *data)
102{
103 kobject_put(&data->kobj);
104 module_put(cpufreq_driver->owner);
105}
106EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
107
108
109/*********************************************************************
110 * UNIFIED DEBUG HELPERS *
111 *********************************************************************/
112#ifdef CONFIG_CPU_FREQ_DEBUG
113
114/* what part(s) of the CPUfreq subsystem are debugged? */
115static unsigned int debug;
116
117/* is the debug output ratelimit'ed using printk_ratelimit? User can
118 * set or modify this value.
119 */
120static unsigned int debug_ratelimit = 1;
121
122/* is the printk_ratelimit'ing enabled? It's enabled after a successful
123 * loading of a cpufreq driver, temporarily disabled when a new policy
124 * is set, and disabled upon cpufreq driver removal
125 */
126static unsigned int disable_ratelimit = 1;
127static DEFINE_SPINLOCK(disable_ratelimit_lock);
128
Arjan van de Ven858119e2006-01-14 13:20:43 -0800129static void cpufreq_debug_enable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 unsigned long flags;
132
133 spin_lock_irqsave(&disable_ratelimit_lock, flags);
134 if (disable_ratelimit)
135 disable_ratelimit--;
136 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
137}
138
Arjan van de Ven858119e2006-01-14 13:20:43 -0800139static void cpufreq_debug_disable_ratelimit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 unsigned long flags;
142
143 spin_lock_irqsave(&disable_ratelimit_lock, flags);
144 disable_ratelimit++;
145 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
146}
147
148void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
149{
150 char s[256];
151 va_list args;
152 unsigned int len;
153 unsigned long flags;
Dave Jones32ee8c32006-02-28 00:43:23 -0500154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 WARN_ON(!prefix);
156 if (type & debug) {
157 spin_lock_irqsave(&disable_ratelimit_lock, flags);
158 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
159 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
160 return;
161 }
162 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
163
164 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
165
166 va_start(args, fmt);
167 len += vsnprintf(&s[len], (256 - len), fmt, args);
168 va_end(args);
169
170 printk(s);
171
172 WARN_ON(len < 5);
173 }
174}
175EXPORT_SYMBOL(cpufreq_debug_printk);
176
177
178module_param(debug, uint, 0644);
179MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
180
181module_param(debug_ratelimit, uint, 0644);
182MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
183
184#else /* !CONFIG_CPU_FREQ_DEBUG */
185
186static inline void cpufreq_debug_enable_ratelimit(void) { return; }
187static inline void cpufreq_debug_disable_ratelimit(void) { return; }
188
189#endif /* CONFIG_CPU_FREQ_DEBUG */
190
191
192/*********************************************************************
193 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
194 *********************************************************************/
195
196/**
197 * adjust_jiffies - adjust the system "loops_per_jiffy"
198 *
199 * This function alters the system "loops_per_jiffy" for the clock
200 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500201 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * per-CPU loops_per_jiffy value wherever possible.
203 */
204#ifndef CONFIG_SMP
205static unsigned long l_p_j_ref;
206static unsigned int l_p_j_ref_freq;
207
Arjan van de Ven858119e2006-01-14 13:20:43 -0800208static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 if (ci->flags & CPUFREQ_CONST_LOOPS)
211 return;
212
213 if (!l_p_j_ref_freq) {
214 l_p_j_ref = loops_per_jiffy;
215 l_p_j_ref_freq = ci->old;
216 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
217 }
218 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
219 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700220 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
222 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
223 }
224}
225#else
226static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
227#endif
228
229
230/**
Dave Jonese4472cb2006-01-31 15:53:55 -0800231 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
232 * on frequency transition.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *
Dave Jonese4472cb2006-01-31 15:53:55 -0800234 * This function calls the transition notifiers and the "adjust_jiffies"
235 * function. It is called twice on all CPU frequency changes that have
Dave Jones32ee8c32006-02-28 00:43:23 -0500236 * external effects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
238void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
239{
Dave Jonese4472cb2006-01-31 15:53:55 -0800240 struct cpufreq_policy *policy;
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 BUG_ON(irqs_disabled());
243
244 freqs->flags = cpufreq_driver->flags;
Dave Jonese4472cb2006-01-31 15:53:55 -0800245 dprintk("notification %u of frequency transition to %u kHz\n",
246 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Dave Jonese4472cb2006-01-31 15:53:55 -0800248 policy = cpufreq_cpu_data[freqs->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500252 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800253 * which is not equal to what the cpufreq core thinks is
254 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
256 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800257 if ((policy) && (policy->cpu == freqs->cpu) &&
258 (policy->cur) && (policy->cur != freqs->old)) {
Jan Beulichb10eec22006-04-28 13:47:13 +0200259 dprintk("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800260 " %u, cpufreq assumed %u kHz.\n",
261 freqs->old, policy->cur);
262 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
264 }
Alan Sterne041c682006-03-27 01:16:30 -0800265 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
266 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
268 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 case CPUFREQ_POSTCHANGE:
271 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Alan Sterne041c682006-03-27 01:16:30 -0800272 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
273 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800274 if (likely(policy) && likely(policy->cpu == freqs->cpu))
275 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
280
281
282
283/*********************************************************************
284 * SYSFS INTERFACE *
285 *********************************************************************/
286
287/**
288 * cpufreq_parse_governor - parse a governor string
289 */
290static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
291 struct cpufreq_governor **governor)
292{
293 if (!cpufreq_driver)
294 return -EINVAL;
295 if (cpufreq_driver->setpolicy) {
296 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
297 *policy = CPUFREQ_POLICY_PERFORMANCE;
298 return 0;
299 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
300 *policy = CPUFREQ_POLICY_POWERSAVE;
301 return 0;
302 }
303 return -EINVAL;
304 } else {
305 struct cpufreq_governor *t;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800306 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!cpufreq_driver || !cpufreq_driver->target)
308 goto out;
309 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
310 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
311 *governor = t;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800312 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return 0;
314 }
315 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500316out:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800317 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
319 return -EINVAL;
320}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322
323/* drivers/base/cpu.c */
324extern struct sysdev_class cpu_sysdev_class;
325
326
327/**
328 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
329 *
330 * Write out information from cpufreq_driver->policy[cpu]; object must be
331 * "unsigned int".
332 */
333
Dave Jones32ee8c32006-02-28 00:43:23 -0500334#define show_one(file_name, object) \
335static ssize_t show_##file_name \
336(struct cpufreq_policy * policy, char *buf) \
337{ \
338 return sprintf (buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341show_one(cpuinfo_min_freq, cpuinfo.min_freq);
342show_one(cpuinfo_max_freq, cpuinfo.max_freq);
343show_one(scaling_min_freq, min);
344show_one(scaling_max_freq, max);
345show_one(scaling_cur_freq, cur);
346
Thomas Renninger7970e082006-04-13 15:14:04 +0200347static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/**
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 \
Thomas Renninger7970e082006-04-13 15:14:04 +0200367 mutex_lock(&policy->lock); \
368 ret = __cpufreq_set_policy(policy, &new_policy); \
369 policy->user_policy.object = policy->object; \
370 mutex_unlock(&policy->lock); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 \
372 return ret ? ret : count; \
373}
374
375store_one(scaling_min_freq,min);
376store_one(scaling_max_freq,max);
377
378/**
379 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
380 */
381static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
382{
383 unsigned int cur_freq = cpufreq_get(policy->cpu);
384 if (!cur_freq)
385 return sprintf(buf, "<unknown>");
386 return sprintf(buf, "%u\n", cur_freq);
387}
388
389
390/**
391 * show_scaling_governor - show the current policy for the specified CPU
392 */
393static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
394{
395 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
396 return sprintf(buf, "powersave\n");
397 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
398 return sprintf(buf, "performance\n");
399 else if (policy->governor)
400 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
401 return -EINVAL;
402}
403
404
405/**
406 * store_scaling_governor - store policy for the specified CPU
407 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500408static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
409 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 unsigned int ret = -EINVAL;
412 char str_governor[16];
413 struct cpufreq_policy new_policy;
414
415 ret = cpufreq_get_policy(&new_policy, policy->cpu);
416 if (ret)
417 return ret;
418
419 ret = sscanf (buf, "%15s", str_governor);
420 if (ret != 1)
421 return -EINVAL;
422
423 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
424 return -EINVAL;
425
Dave Jonesa496e252006-07-07 12:31:27 -0400426 lock_cpu_hotplug();
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
Dave Jonesa496e252006-07-07 12:31:27 -0400437 unlock_cpu_hotplug();
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return ret ? ret : count;
440}
441
442/**
443 * show_scaling_driver - show the cpufreq driver currently loaded
444 */
445static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
446{
447 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
448}
449
450/**
451 * show_scaling_available_governors - show the available CPUfreq governors
452 */
453static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
454 char *buf)
455{
456 ssize_t i = 0;
457 struct cpufreq_governor *t;
458
459 if (!cpufreq_driver->target) {
460 i += sprintf(buf, "performance powersave");
461 goto out;
462 }
463
464 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
465 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
466 goto out;
467 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
468 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500469out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 i += sprintf(&buf[i], "\n");
471 return i;
472}
473/**
474 * show_affected_cpus - show the CPUs affected by each transition
475 */
476static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
477{
478 ssize_t i = 0;
479 unsigned int cpu;
480
481 for_each_cpu_mask(cpu, policy->cpus) {
482 if (i)
483 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
484 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
485 if (i >= (PAGE_SIZE - 5))
486 break;
487 }
488 i += sprintf(&buf[i], "\n");
489 return i;
490}
491
492
493#define define_one_ro(_name) \
494static struct freq_attr _name = \
495__ATTR(_name, 0444, show_##_name, NULL)
496
497#define define_one_ro0400(_name) \
498static struct freq_attr _name = \
499__ATTR(_name, 0400, show_##_name, NULL)
500
501#define define_one_rw(_name) \
502static struct freq_attr _name = \
503__ATTR(_name, 0644, show_##_name, store_##_name)
504
505define_one_ro0400(cpuinfo_cur_freq);
506define_one_ro(cpuinfo_min_freq);
507define_one_ro(cpuinfo_max_freq);
508define_one_ro(scaling_available_governors);
509define_one_ro(scaling_driver);
510define_one_ro(scaling_cur_freq);
511define_one_ro(affected_cpus);
512define_one_rw(scaling_min_freq);
513define_one_rw(scaling_max_freq);
514define_one_rw(scaling_governor);
515
516static struct attribute * default_attrs[] = {
517 &cpuinfo_min_freq.attr,
518 &cpuinfo_max_freq.attr,
519 &scaling_min_freq.attr,
520 &scaling_max_freq.attr,
521 &affected_cpus.attr,
522 &scaling_governor.attr,
523 &scaling_driver.attr,
524 &scaling_available_governors.attr,
525 NULL
526};
527
528#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
529#define to_attr(a) container_of(a,struct freq_attr,attr)
530
531static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
532{
533 struct cpufreq_policy * policy = to_policy(kobj);
534 struct freq_attr * fattr = to_attr(attr);
535 ssize_t ret;
536 policy = cpufreq_cpu_get(policy->cpu);
537 if (!policy)
538 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500539 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 cpufreq_cpu_put(policy);
541 return ret;
542}
543
Dave Jones32ee8c32006-02-28 00:43:23 -0500544static ssize_t store(struct kobject * kobj, struct attribute * attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 const char * buf, size_t count)
546{
547 struct cpufreq_policy * policy = to_policy(kobj);
548 struct freq_attr * fattr = to_attr(attr);
549 ssize_t ret;
550 policy = cpufreq_cpu_get(policy->cpu);
551 if (!policy)
552 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500553 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 cpufreq_cpu_put(policy);
555 return ret;
556}
557
558static void cpufreq_sysfs_release(struct kobject * kobj)
559{
560 struct cpufreq_policy * policy = to_policy(kobj);
561 dprintk("last reference is dropped\n");
562 complete(&policy->kobj_unregister);
563}
564
565static struct sysfs_ops sysfs_ops = {
566 .show = show,
567 .store = store,
568};
569
570static struct kobj_type ktype_cpufreq = {
571 .sysfs_ops = &sysfs_ops,
572 .default_attrs = default_attrs,
573 .release = cpufreq_sysfs_release,
574};
575
576
577/**
578 * cpufreq_add_dev - add a CPU device
579 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500580 * Adds the cpufreq interface for a CPU device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
582static int cpufreq_add_dev (struct sys_device * sys_dev)
583{
584 unsigned int cpu = sys_dev->id;
585 int ret = 0;
586 struct cpufreq_policy new_policy;
587 struct cpufreq_policy *policy;
588 struct freq_attr **drv_attr;
Dave Jones8ff69732006-03-05 03:37:23 -0500589 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 unsigned long flags;
591 unsigned int j;
Dave Jones8ff69732006-03-05 03:37:23 -0500592#ifdef CONFIG_SMP
593 struct cpufreq_policy *managed_policy;
594#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Ashok Rajc32b6b82005-10-30 14:59:54 -0800596 if (cpu_is_offline(cpu))
597 return 0;
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 cpufreq_debug_disable_ratelimit();
600 dprintk("adding CPU %u\n", cpu);
601
602#ifdef CONFIG_SMP
603 /* check whether a different CPU already registered this
604 * CPU because it is in the same boat. */
605 policy = cpufreq_cpu_get(cpu);
606 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500607 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 cpufreq_debug_enable_ratelimit();
609 return 0;
610 }
611#endif
612
613 if (!try_module_get(cpufreq_driver->owner)) {
614 ret = -EINVAL;
615 goto module_out;
616 }
617
Dave Jonese98df502005-10-20 15:17:43 -0700618 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (!policy) {
620 ret = -ENOMEM;
621 goto nomem_out;
622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 policy->cpu = cpu;
625 policy->cpus = cpumask_of_cpu(cpu);
626
Arjan van de Ven83933af2006-01-14 16:01:49 +0100627 mutex_init(&policy->lock);
628 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 init_completion(&policy->kobj_unregister);
630 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
631
632 /* call driver. From then on the cpufreq must be able
633 * to accept all calls to ->verify and ->setpolicy for this CPU
634 */
635 ret = cpufreq_driver->init(policy);
636 if (ret) {
637 dprintk("initialization failed\n");
Andrew Mortonf3876c12006-01-18 13:40:54 -0800638 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 goto err_out;
640 }
641
Dave Jones8ff69732006-03-05 03:37:23 -0500642#ifdef CONFIG_SMP
643 for_each_cpu_mask(j, policy->cpus) {
644 if (cpu == j)
645 continue;
646
647 /* check for existing affected CPUs. They may not be aware
648 * of it due to CPU Hotplug.
649 */
650 managed_policy = cpufreq_cpu_get(j);
651 if (unlikely(managed_policy)) {
652 spin_lock_irqsave(&cpufreq_driver_lock, flags);
653 managed_policy->cpus = policy->cpus;
654 cpufreq_cpu_data[cpu] = managed_policy;
655 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
656
657 dprintk("CPU already managed, adding link\n");
658 sysfs_create_link(&sys_dev->kobj,
659 &managed_policy->kobj, "cpufreq");
660
661 cpufreq_debug_enable_ratelimit();
662 mutex_unlock(&policy->lock);
663 ret = 0;
664 goto err_out_driver_exit; /* call driver->exit() */
665 }
666 }
667#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
669
670 /* prepare interface data */
671 policy->kobj.parent = &sys_dev->kobj;
672 policy->kobj.ktype = &ktype_cpufreq;
673 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
674
675 ret = kobject_register(&policy->kobj);
Andrew Mortonf3876c12006-01-18 13:40:54 -0800676 if (ret) {
677 mutex_unlock(&policy->lock);
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700678 goto err_out_driver_exit;
Andrew Mortonf3876c12006-01-18 13:40:54 -0800679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* set up files for this cpu device */
681 drv_attr = cpufreq_driver->attr;
682 while ((drv_attr) && (*drv_attr)) {
683 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
684 drv_attr++;
685 }
686 if (cpufreq_driver->get)
687 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
688 if (cpufreq_driver->target)
689 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
690
691 spin_lock_irqsave(&cpufreq_driver_lock, flags);
692 for_each_cpu_mask(j, policy->cpus)
693 cpufreq_cpu_data[j] = policy;
694 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones8ff69732006-03-05 03:37:23 -0500695
696 /* symlink affected CPUs */
697 for_each_cpu_mask(j, policy->cpus) {
698 if (j == cpu)
699 continue;
700 if (!cpu_online(j))
701 continue;
702
Dave Jones1f8b2c92006-03-29 01:40:04 -0500703 dprintk("CPU %u already managed, adding link\n", j);
Dave Jones8ff69732006-03-05 03:37:23 -0500704 cpufreq_cpu_get(cpu);
705 cpu_sys_dev = get_cpu_sysdev(j);
706 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
707 "cpufreq");
708 }
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 policy->governor = NULL; /* to assure that the starting sequence is
711 * run in cpufreq_set_policy */
Arjan van de Ven83933af2006-01-14 16:01:49 +0100712 mutex_unlock(&policy->lock);
Dave Jones87c32272006-03-29 01:48:37 -0500713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /* set default policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 ret = cpufreq_set_policy(&new_policy);
716 if (ret) {
717 dprintk("setting policy failed\n");
718 goto err_out_unregister;
719 }
720
721 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 dprintk("initialization complete\n");
723 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -0500724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726
727
728err_out_unregister:
729 spin_lock_irqsave(&cpufreq_driver_lock, flags);
730 for_each_cpu_mask(j, policy->cpus)
731 cpufreq_cpu_data[j] = NULL;
732 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
733
734 kobject_unregister(&policy->kobj);
735 wait_for_completion(&policy->kobj_unregister);
736
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700737err_out_driver_exit:
738 if (cpufreq_driver->exit)
739 cpufreq_driver->exit(policy);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741err_out:
742 kfree(policy);
743
744nomem_out:
745 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800746module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 cpufreq_debug_enable_ratelimit();
748 return ret;
749}
750
751
752/**
753 * cpufreq_remove_dev - remove a CPU device
754 *
755 * Removes the cpufreq interface for a CPU device.
756 */
757static int cpufreq_remove_dev (struct sys_device * sys_dev)
758{
759 unsigned int cpu = sys_dev->id;
760 unsigned long flags;
761 struct cpufreq_policy *data;
762#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -0800763 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 unsigned int j;
765#endif
766
767 cpufreq_debug_disable_ratelimit();
768 dprintk("unregistering CPU %u\n", cpu);
769
770 spin_lock_irqsave(&cpufreq_driver_lock, flags);
771 data = cpufreq_cpu_data[cpu];
772
773 if (!data) {
774 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 cpufreq_debug_enable_ratelimit();
776 return -EINVAL;
777 }
778 cpufreq_cpu_data[cpu] = NULL;
779
780
781#ifdef CONFIG_SMP
782 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -0500783 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
785 if (unlikely(cpu != data->cpu)) {
786 dprintk("removing link\n");
Dave Jones8ff69732006-03-05 03:37:23 -0500787 cpu_clear(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
789 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 cpufreq_cpu_put(data);
791 cpufreq_debug_enable_ratelimit();
792 return 0;
793 }
794#endif
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 if (!kobject_get(&data->kobj)) {
798 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
799 cpufreq_debug_enable_ratelimit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500800 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
803#ifdef CONFIG_SMP
804 /* if we have other CPUs still registered, we need to unlink them,
805 * or else wait_for_completion below will lock up. Clean the
806 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
807 * links afterwards.
808 */
809 if (unlikely(cpus_weight(data->cpus) > 1)) {
810 for_each_cpu_mask(j, data->cpus) {
811 if (j == cpu)
812 continue;
813 cpufreq_cpu_data[j] = NULL;
814 }
815 }
816
817 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
818
819 if (unlikely(cpus_weight(data->cpus) > 1)) {
820 for_each_cpu_mask(j, data->cpus) {
821 if (j == cpu)
822 continue;
823 dprintk("removing link for cpu %u\n", j);
Ashok Rajd434fca2005-10-30 14:59:52 -0800824 cpu_sys_dev = get_cpu_sysdev(j);
825 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 cpufreq_cpu_put(data);
827 }
828 }
829#else
830 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
831#endif
832
Arjan van de Ven83933af2006-01-14 16:01:49 +0100833 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (cpufreq_driver->target)
835 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Arjan van de Ven83933af2006-01-14 16:01:49 +0100836 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 kobject_unregister(&data->kobj);
839
840 kobject_put(&data->kobj);
841
842 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -0500843 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 * unloading.
845 */
846 dprintk("waiting for dropping of refcount\n");
847 wait_for_completion(&data->kobj_unregister);
848 dprintk("wait complete\n");
849
850 if (cpufreq_driver->exit)
851 cpufreq_driver->exit(data);
852
853 kfree(data);
854
855 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 return 0;
857}
858
859
860static void handle_update(void *data)
861{
862 unsigned int cpu = (unsigned int)(long)data;
863 dprintk("handle_update for cpu %u called\n", cpu);
864 cpufreq_update_policy(cpu);
865}
866
867/**
868 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
869 * @cpu: cpu number
870 * @old_freq: CPU frequency the kernel thinks the CPU runs at
871 * @new_freq: CPU frequency the CPU actually runs at
872 *
873 * We adjust to current frequency first, and need to clean up later. So either call
874 * to cpufreq_update_policy() or schedule handle_update()).
875 */
876static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
877{
878 struct cpufreq_freqs freqs;
879
Jan Beulichb10eec22006-04-28 13:47:13 +0200880 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
882
883 freqs.cpu = cpu;
884 freqs.old = old_freq;
885 freqs.new = new_freq;
886 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
887 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
888}
889
890
Dave Jones32ee8c32006-02-28 00:43:23 -0500891/**
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800892 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
893 * @cpu: CPU number
894 *
895 * This is the last known freq, without actually getting it from the driver.
896 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
897 */
898unsigned int cpufreq_quick_get(unsigned int cpu)
899{
900 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
901 unsigned int ret = 0;
902
903 if (policy) {
Arjan van de Ven83933af2006-01-14 16:01:49 +0100904 mutex_lock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800905 ret = policy->cur;
Arjan van de Ven83933af2006-01-14 16:01:49 +0100906 mutex_unlock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800907 cpufreq_cpu_put(policy);
908 }
909
910 return (ret);
911}
912EXPORT_SYMBOL(cpufreq_quick_get);
913
914
Dave Jones32ee8c32006-02-28 00:43:23 -0500915/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 * cpufreq_get - get the current CPU frequency (in kHz)
917 * @cpu: CPU number
918 *
919 * Get the CPU current (static) CPU frequency
920 */
921unsigned int cpufreq_get(unsigned int cpu)
922{
923 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
924 unsigned int ret = 0;
925
926 if (!policy)
927 return 0;
928
929 if (!cpufreq_driver->get)
930 goto out;
931
Arjan van de Ven83933af2006-01-14 16:01:49 +0100932 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 ret = cpufreq_driver->get(cpu);
935
Dave Jones7d5e3502006-02-02 17:03:42 -0500936 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /* verify no discrepancy between actual and saved value exists */
938 if (unlikely(ret != policy->cur)) {
939 cpufreq_out_of_sync(cpu, policy->cur, ret);
940 schedule_work(&policy->update);
941 }
942 }
943
Arjan van de Ven83933af2006-01-14 16:01:49 +0100944 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Dave Jones7d5e3502006-02-02 17:03:42 -0500946out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 cpufreq_cpu_put(policy);
948
949 return (ret);
950}
951EXPORT_SYMBOL(cpufreq_get);
952
953
954/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700955 * cpufreq_suspend - let the low level driver prepare for suspend
956 */
957
Bernard Blackhame00d9962005-07-07 17:56:42 -0700958static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700959{
960 int cpu = sysdev->id;
961 unsigned int ret = 0;
962 unsigned int cur_freq = 0;
963 struct cpufreq_policy *cpu_policy;
964
965 dprintk("resuming cpu %u\n", cpu);
966
967 if (!cpu_online(cpu))
968 return 0;
969
970 /* we may be lax here as interrupts are off. Nonetheless
971 * we need to grab the correct cpu policy, as to check
972 * whether we really run on this CPU.
973 */
974
975 cpu_policy = cpufreq_cpu_get(cpu);
976 if (!cpu_policy)
977 return -EINVAL;
978
979 /* only handle each CPU group once */
980 if (unlikely(cpu_policy->cpu != cpu)) {
981 cpufreq_cpu_put(cpu_policy);
982 return 0;
983 }
984
985 if (cpufreq_driver->suspend) {
Bernard Blackhame00d9962005-07-07 17:56:42 -0700986 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700987 if (ret) {
988 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
989 "step on CPU %u\n", cpu_policy->cpu);
990 cpufreq_cpu_put(cpu_policy);
991 return ret;
992 }
993 }
994
995
996 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
997 goto out;
998
999 if (cpufreq_driver->get)
1000 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1001
1002 if (!cur_freq || !cpu_policy->cur) {
1003 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1004 "frequency is what timing core thinks it is.\n");
1005 goto out;
1006 }
1007
1008 if (unlikely(cur_freq != cpu_policy->cur)) {
1009 struct cpufreq_freqs freqs;
1010
1011 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001012 dprintk("Warning: CPU frequency is %u, "
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001013 "cpufreq assumed %u kHz.\n",
1014 cur_freq, cpu_policy->cur);
1015
1016 freqs.cpu = cpu;
1017 freqs.old = cpu_policy->cur;
1018 freqs.new = cur_freq;
1019
Alan Sterne041c682006-03-27 01:16:30 -08001020 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001021 CPUFREQ_SUSPENDCHANGE, &freqs);
1022 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1023
1024 cpu_policy->cur = cur_freq;
1025 }
1026
Dave Jones7d5e3502006-02-02 17:03:42 -05001027out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001028 cpufreq_cpu_put(cpu_policy);
1029 return 0;
1030}
1031
1032/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 * cpufreq_resume - restore proper CPU frequency handling after resume
1034 *
1035 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1036 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001037 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1038 * restored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 */
1040static int cpufreq_resume(struct sys_device * sysdev)
1041{
1042 int cpu = sysdev->id;
1043 unsigned int ret = 0;
1044 struct cpufreq_policy *cpu_policy;
1045
1046 dprintk("resuming cpu %u\n", cpu);
1047
1048 if (!cpu_online(cpu))
1049 return 0;
1050
1051 /* we may be lax here as interrupts are off. Nonetheless
1052 * we need to grab the correct cpu policy, as to check
1053 * whether we really run on this CPU.
1054 */
1055
1056 cpu_policy = cpufreq_cpu_get(cpu);
1057 if (!cpu_policy)
1058 return -EINVAL;
1059
1060 /* only handle each CPU group once */
1061 if (unlikely(cpu_policy->cpu != cpu)) {
1062 cpufreq_cpu_put(cpu_policy);
1063 return 0;
1064 }
1065
1066 if (cpufreq_driver->resume) {
1067 ret = cpufreq_driver->resume(cpu_policy);
1068 if (ret) {
1069 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1070 "step on CPU %u\n", cpu_policy->cpu);
1071 cpufreq_cpu_put(cpu_policy);
1072 return ret;
1073 }
1074 }
1075
1076 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1077 unsigned int cur_freq = 0;
1078
1079 if (cpufreq_driver->get)
1080 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1081
1082 if (!cur_freq || !cpu_policy->cur) {
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001083 printk(KERN_ERR "cpufreq: resume failed to assert "
1084 "current frequency is what timing core "
1085 "thinks it is.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto out;
1087 }
1088
1089 if (unlikely(cur_freq != cpu_policy->cur)) {
1090 struct cpufreq_freqs freqs;
1091
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001092 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001093 dprintk("Warning: CPU frequency"
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001094 "is %u, cpufreq assumed %u kHz.\n",
1095 cur_freq, cpu_policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 freqs.cpu = cpu;
1098 freqs.old = cpu_policy->cur;
1099 freqs.new = cur_freq;
1100
Alan Sterne041c682006-03-27 01:16:30 -08001101 blocking_notifier_call_chain(
1102 &cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001103 CPUFREQ_RESUMECHANGE, &freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1105
1106 cpu_policy->cur = cur_freq;
1107 }
1108 }
1109
1110out:
1111 schedule_work(&cpu_policy->update);
1112 cpufreq_cpu_put(cpu_policy);
1113 return ret;
1114}
1115
1116static struct sysdev_driver cpufreq_sysdev_driver = {
1117 .add = cpufreq_add_dev,
1118 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001119 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 .resume = cpufreq_resume,
1121};
1122
1123
1124/*********************************************************************
1125 * NOTIFIER LISTS INTERFACE *
1126 *********************************************************************/
1127
1128/**
1129 * cpufreq_register_notifier - register a driver with cpufreq
1130 * @nb: notifier function to register
1131 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1132 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001133 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 * are notified about clock rate changes (once before and once after
1135 * the transition), or a list of drivers that are notified about
1136 * changes in cpufreq policy.
1137 *
1138 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001139 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
1141int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1142{
1143 int ret;
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 switch (list) {
1146 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001147 ret = blocking_notifier_chain_register(
1148 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 break;
1150 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001151 ret = blocking_notifier_chain_register(
1152 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 break;
1154 default:
1155 ret = -EINVAL;
1156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 return ret;
1159}
1160EXPORT_SYMBOL(cpufreq_register_notifier);
1161
1162
1163/**
1164 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1165 * @nb: notifier block to be unregistered
1166 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1167 *
1168 * Remove a driver from the CPU frequency notifier list.
1169 *
1170 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001171 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 */
1173int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1174{
1175 int ret;
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 switch (list) {
1178 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001179 ret = blocking_notifier_chain_unregister(
1180 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 break;
1182 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001183 ret = blocking_notifier_chain_unregister(
1184 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 break;
1186 default:
1187 ret = -EINVAL;
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 return ret;
1191}
1192EXPORT_SYMBOL(cpufreq_unregister_notifier);
1193
1194
1195/*********************************************************************
1196 * GOVERNORS *
1197 *********************************************************************/
1198
1199
1200int __cpufreq_driver_target(struct cpufreq_policy *policy,
1201 unsigned int target_freq,
1202 unsigned int relation)
1203{
1204 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001205
Ashok Raja9d9baa2005-11-28 13:43:46 -08001206 lock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1208 target_freq, relation);
1209 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1210 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001211
Ashok Raja9d9baa2005-11-28 13:43:46 -08001212 unlock_cpu_hotplug();
Ashok Raj90d45d12005-11-08 21:34:24 -08001213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 return retval;
1215}
1216EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218int cpufreq_driver_target(struct cpufreq_policy *policy,
1219 unsigned int target_freq,
1220 unsigned int relation)
1221{
Dave Jonescc993ca2005-07-28 09:43:56 -07001222 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
1224 policy = cpufreq_cpu_get(policy->cpu);
1225 if (!policy)
1226 return -EINVAL;
1227
Arjan van de Ven83933af2006-01-14 16:01:49 +01001228 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 ret = __cpufreq_driver_target(policy, target_freq, relation);
1231
Arjan van de Ven83933af2006-01-14 16:01:49 +01001232 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return ret;
1236}
1237EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1238
1239
1240static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1241{
Dave Jonescc993ca2005-07-28 09:43:56 -07001242 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 if (!try_module_get(policy->governor->owner))
1245 return -EINVAL;
1246
1247 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1248 ret = policy->governor->governor(policy, event);
1249
1250 /* we keep one module reference alive for each CPU governed by this CPU */
1251 if ((event != CPUFREQ_GOV_START) || ret)
1252 module_put(policy->governor->owner);
1253 if ((event == CPUFREQ_GOV_STOP) && !ret)
1254 module_put(policy->governor->owner);
1255
1256 return ret;
1257}
1258
1259
1260int cpufreq_governor(unsigned int cpu, unsigned int event)
1261{
1262 int ret = 0;
1263 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1264
1265 if (!policy)
1266 return -EINVAL;
1267
Arjan van de Ven83933af2006-01-14 16:01:49 +01001268 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 ret = __cpufreq_governor(policy, event);
Arjan van de Ven83933af2006-01-14 16:01:49 +01001270 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return ret;
1274}
1275EXPORT_SYMBOL_GPL(cpufreq_governor);
1276
1277
1278int cpufreq_register_governor(struct cpufreq_governor *governor)
1279{
1280 struct cpufreq_governor *t;
1281
1282 if (!governor)
1283 return -EINVAL;
1284
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001285 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1288 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001289 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return -EBUSY;
1291 }
1292 }
1293 list_add(&governor->governor_list, &cpufreq_governor_list);
1294
Dave Jones32ee8c32006-02-28 00:43:23 -05001295 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 0;
1297}
1298EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1299
1300
1301void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1302{
1303 if (!governor)
1304 return;
1305
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001306 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001308 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return;
1310}
1311EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1312
1313
1314
1315/*********************************************************************
1316 * POLICY INTERFACE *
1317 *********************************************************************/
1318
1319/**
1320 * cpufreq_get_policy - get the current cpufreq_policy
1321 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1322 *
1323 * Reads the current cpufreq policy.
1324 */
1325int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1326{
1327 struct cpufreq_policy *cpu_policy;
1328 if (!policy)
1329 return -EINVAL;
1330
1331 cpu_policy = cpufreq_cpu_get(cpu);
1332 if (!cpu_policy)
1333 return -EINVAL;
1334
Arjan van de Ven83933af2006-01-14 16:01:49 +01001335 mutex_lock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Arjan van de Ven83933af2006-01-14 16:01:49 +01001337 mutex_unlock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return 0;
1341}
1342EXPORT_SYMBOL(cpufreq_get_policy);
1343
1344
1345static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1346{
1347 int ret = 0;
1348
1349 cpufreq_debug_disable_ratelimit();
1350 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1351 policy->min, policy->max);
1352
Dave Jones7d5e3502006-02-02 17:03:42 -05001353 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 /* verify the cpu speed can be set within this limit */
1356 ret = cpufreq_driver->verify(policy);
1357 if (ret)
1358 goto error_out;
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001361 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1362 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001365 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1366 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 /* verify the cpu speed can be set within this limit,
1369 which might be different to the first one */
1370 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001371 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001375 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1376 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Dave Jones7d5e3502006-02-02 17:03:42 -05001378 data->min = policy->min;
1379 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
1381 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1382
1383 if (cpufreq_driver->setpolicy) {
1384 data->policy = policy->policy;
1385 dprintk("setting range\n");
1386 ret = cpufreq_driver->setpolicy(policy);
1387 } else {
1388 if (policy->governor != data->governor) {
1389 /* save old, working values */
1390 struct cpufreq_governor *old_gov = data->governor;
1391
1392 dprintk("governor switch\n");
1393
1394 /* end old governor */
1395 if (data->governor)
1396 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1397
1398 /* start new governor */
1399 data->governor = policy->governor;
1400 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1401 /* new governor failed, so re-start old one */
1402 dprintk("starting governor %s failed\n", data->governor->name);
1403 if (old_gov) {
1404 data->governor = old_gov;
1405 __cpufreq_governor(data, CPUFREQ_GOV_START);
1406 }
1407 ret = -EINVAL;
1408 goto error_out;
1409 }
1410 /* might be a policy change, too, so fall through */
1411 }
1412 dprintk("governor: change or update limits\n");
1413 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1414 }
1415
Dave Jones7d5e3502006-02-02 17:03:42 -05001416error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 cpufreq_debug_enable_ratelimit();
1418 return ret;
1419}
1420
1421/**
1422 * cpufreq_set_policy - set a new CPUFreq policy
1423 * @policy: policy to be set.
1424 *
1425 * Sets a new CPU frequency and voltage scaling policy.
1426 */
1427int cpufreq_set_policy(struct cpufreq_policy *policy)
1428{
1429 int ret = 0;
1430 struct cpufreq_policy *data;
1431
1432 if (!policy)
1433 return -EINVAL;
1434
1435 data = cpufreq_cpu_get(policy->cpu);
1436 if (!data)
1437 return -EINVAL;
1438
1439 /* lock this CPU */
Arjan van de Ven83933af2006-01-14 16:01:49 +01001440 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 ret = __cpufreq_set_policy(data, policy);
1443 data->user_policy.min = data->min;
1444 data->user_policy.max = data->max;
1445 data->user_policy.policy = data->policy;
1446 data->user_policy.governor = data->governor;
1447
Arjan van de Ven83933af2006-01-14 16:01:49 +01001448 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 cpufreq_cpu_put(data);
1450
1451 return ret;
1452}
1453EXPORT_SYMBOL(cpufreq_set_policy);
1454
1455
1456/**
1457 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1458 * @cpu: CPU which shall be re-evaluated
1459 *
1460 * Usefull for policy notifiers which have different necessities
1461 * at different times.
1462 */
1463int cpufreq_update_policy(unsigned int cpu)
1464{
1465 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1466 struct cpufreq_policy policy;
1467 int ret = 0;
1468
1469 if (!data)
1470 return -ENODEV;
1471
Arjan van de Ven83933af2006-01-14 16:01:49 +01001472 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
1474 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001475 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 policy.min = data->user_policy.min;
1477 policy.max = data->user_policy.max;
1478 policy.policy = data->user_policy.policy;
1479 policy.governor = data->user_policy.governor;
1480
Thomas Renninger0961dd02006-01-26 18:46:33 +01001481 /* BIOS might change freq behind our back
1482 -> ask driver for current freq and notify governors about a change */
1483 if (cpufreq_driver->get) {
1484 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001485 if (!data->cur) {
1486 dprintk("Driver did not initialize current freq");
1487 data->cur = policy.cur;
1488 } else {
1489 if (data->cur != policy.cur)
1490 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1491 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001492 }
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 ret = __cpufreq_set_policy(data, &policy);
1495
Arjan van de Ven83933af2006-01-14 16:01:49 +01001496 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 cpufreq_cpu_put(data);
1499 return ret;
1500}
1501EXPORT_SYMBOL(cpufreq_update_policy);
1502
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001503#ifdef CONFIG_HOTPLUG_CPU
1504static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001505 unsigned long action, void *hcpu)
1506{
1507 unsigned int cpu = (unsigned long)hcpu;
1508 struct cpufreq_policy *policy;
1509 struct sys_device *sys_dev;
1510
1511 sys_dev = get_cpu_sysdev(cpu);
1512
1513 if (sys_dev) {
1514 switch (action) {
1515 case CPU_ONLINE:
1516 cpufreq_add_dev(sys_dev);
1517 break;
1518 case CPU_DOWN_PREPARE:
1519 /*
1520 * We attempt to put this cpu in lowest frequency
1521 * possible before going down. This will permit
1522 * hardware-managed P-State to switch other related
1523 * threads to min or higher speeds if possible.
1524 */
1525 policy = cpufreq_cpu_data[cpu];
1526 if (policy) {
1527 cpufreq_driver_target(policy, policy->min,
1528 CPUFREQ_RELATION_H);
1529 }
1530 break;
1531 case CPU_DEAD:
1532 cpufreq_remove_dev(sys_dev);
1533 break;
1534 }
1535 }
1536 return NOTIFY_OK;
1537}
1538
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001539static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
Ashok Rajc32b6b82005-10-30 14:59:54 -08001540{
1541 .notifier_call = cpufreq_cpu_callback,
1542};
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001543#endif /* CONFIG_HOTPLUG_CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545/*********************************************************************
1546 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1547 *********************************************************************/
1548
1549/**
1550 * cpufreq_register_driver - register a CPU Frequency driver
1551 * @driver_data: A struct cpufreq_driver containing the values#
1552 * submitted by the CPU Frequency driver.
1553 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001554 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001556 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 *
1558 */
1559int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1560{
1561 unsigned long flags;
1562 int ret;
1563
1564 if (!driver_data || !driver_data->verify || !driver_data->init ||
1565 ((!driver_data->setpolicy) && (!driver_data->target)))
1566 return -EINVAL;
1567
1568 dprintk("trying to register driver %s\n", driver_data->name);
1569
1570 if (driver_data->setpolicy)
1571 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1572
1573 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1574 if (cpufreq_driver) {
1575 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1576 return -EBUSY;
1577 }
1578 cpufreq_driver = driver_data;
1579 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1580
1581 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1582
1583 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1584 int i;
1585 ret = -ENODEV;
1586
1587 /* check for at least one working CPU */
1588 for (i=0; i<NR_CPUS; i++)
1589 if (cpufreq_cpu_data[i])
1590 ret = 0;
1591
1592 /* if all ->init() calls failed, unregister */
1593 if (ret) {
1594 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1595 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1596
1597 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1598 cpufreq_driver = NULL;
1599 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1600 }
1601 }
1602
1603 if (!ret) {
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001604 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 dprintk("driver %s up and running\n", driver_data->name);
1606 cpufreq_debug_enable_ratelimit();
1607 }
1608
1609 return (ret);
1610}
1611EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1612
1613
1614/**
1615 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1616 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001617 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 * the right to do so, i.e. if you have succeeded in initialising before!
1619 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1620 * currently not initialised.
1621 */
1622int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1623{
1624 unsigned long flags;
1625
1626 cpufreq_debug_disable_ratelimit();
1627
1628 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1629 cpufreq_debug_enable_ratelimit();
1630 return -EINVAL;
1631 }
1632
1633 dprintk("unregistering driver %s\n", driver->name);
1634
1635 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001636 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1639 cpufreq_driver = NULL;
1640 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1641
1642 return 0;
1643}
1644EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);