blob: 1ba4039777e88757503af3601d791365abc1c59c [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
Thomas Renninger7970e082006-04-13 15:14:04 +0200426 /* Do not use cpufreq_set_policy here or the user_policy.max
427 will be wrongly overridden */
428 mutex_lock(&policy->lock);
429 ret = __cpufreq_set_policy(policy, &new_policy);
430
431 policy->user_policy.policy = policy->policy;
432 policy->user_policy.governor = policy->governor;
433 mutex_unlock(&policy->lock);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return ret ? ret : count;
436}
437
438/**
439 * show_scaling_driver - show the cpufreq driver currently loaded
440 */
441static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
442{
443 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
444}
445
446/**
447 * show_scaling_available_governors - show the available CPUfreq governors
448 */
449static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
450 char *buf)
451{
452 ssize_t i = 0;
453 struct cpufreq_governor *t;
454
455 if (!cpufreq_driver->target) {
456 i += sprintf(buf, "performance powersave");
457 goto out;
458 }
459
460 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
461 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
462 goto out;
463 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
464 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500465out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 i += sprintf(&buf[i], "\n");
467 return i;
468}
469/**
470 * show_affected_cpus - show the CPUs affected by each transition
471 */
472static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
473{
474 ssize_t i = 0;
475 unsigned int cpu;
476
477 for_each_cpu_mask(cpu, policy->cpus) {
478 if (i)
479 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
480 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
481 if (i >= (PAGE_SIZE - 5))
482 break;
483 }
484 i += sprintf(&buf[i], "\n");
485 return i;
486}
487
488
489#define define_one_ro(_name) \
490static struct freq_attr _name = \
491__ATTR(_name, 0444, show_##_name, NULL)
492
493#define define_one_ro0400(_name) \
494static struct freq_attr _name = \
495__ATTR(_name, 0400, show_##_name, NULL)
496
497#define define_one_rw(_name) \
498static struct freq_attr _name = \
499__ATTR(_name, 0644, show_##_name, store_##_name)
500
501define_one_ro0400(cpuinfo_cur_freq);
502define_one_ro(cpuinfo_min_freq);
503define_one_ro(cpuinfo_max_freq);
504define_one_ro(scaling_available_governors);
505define_one_ro(scaling_driver);
506define_one_ro(scaling_cur_freq);
507define_one_ro(affected_cpus);
508define_one_rw(scaling_min_freq);
509define_one_rw(scaling_max_freq);
510define_one_rw(scaling_governor);
511
512static struct attribute * default_attrs[] = {
513 &cpuinfo_min_freq.attr,
514 &cpuinfo_max_freq.attr,
515 &scaling_min_freq.attr,
516 &scaling_max_freq.attr,
517 &affected_cpus.attr,
518 &scaling_governor.attr,
519 &scaling_driver.attr,
520 &scaling_available_governors.attr,
521 NULL
522};
523
524#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
525#define to_attr(a) container_of(a,struct freq_attr,attr)
526
527static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
528{
529 struct cpufreq_policy * policy = to_policy(kobj);
530 struct freq_attr * fattr = to_attr(attr);
531 ssize_t ret;
532 policy = cpufreq_cpu_get(policy->cpu);
533 if (!policy)
534 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500535 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 cpufreq_cpu_put(policy);
537 return ret;
538}
539
Dave Jones32ee8c32006-02-28 00:43:23 -0500540static ssize_t store(struct kobject * kobj, struct attribute * attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 const char * buf, size_t count)
542{
543 struct cpufreq_policy * policy = to_policy(kobj);
544 struct freq_attr * fattr = to_attr(attr);
545 ssize_t ret;
546 policy = cpufreq_cpu_get(policy->cpu);
547 if (!policy)
548 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500549 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 cpufreq_cpu_put(policy);
551 return ret;
552}
553
554static void cpufreq_sysfs_release(struct kobject * kobj)
555{
556 struct cpufreq_policy * policy = to_policy(kobj);
557 dprintk("last reference is dropped\n");
558 complete(&policy->kobj_unregister);
559}
560
561static struct sysfs_ops sysfs_ops = {
562 .show = show,
563 .store = store,
564};
565
566static struct kobj_type ktype_cpufreq = {
567 .sysfs_ops = &sysfs_ops,
568 .default_attrs = default_attrs,
569 .release = cpufreq_sysfs_release,
570};
571
572
573/**
574 * cpufreq_add_dev - add a CPU device
575 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500576 * Adds the cpufreq interface for a CPU device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
578static int cpufreq_add_dev (struct sys_device * sys_dev)
579{
580 unsigned int cpu = sys_dev->id;
581 int ret = 0;
582 struct cpufreq_policy new_policy;
583 struct cpufreq_policy *policy;
584 struct freq_attr **drv_attr;
Dave Jones8ff69732006-03-05 03:37:23 -0500585 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 unsigned long flags;
587 unsigned int j;
Dave Jones8ff69732006-03-05 03:37:23 -0500588#ifdef CONFIG_SMP
589 struct cpufreq_policy *managed_policy;
590#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Ashok Rajc32b6b82005-10-30 14:59:54 -0800592 if (cpu_is_offline(cpu))
593 return 0;
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 cpufreq_debug_disable_ratelimit();
596 dprintk("adding CPU %u\n", cpu);
597
598#ifdef CONFIG_SMP
599 /* check whether a different CPU already registered this
600 * CPU because it is in the same boat. */
601 policy = cpufreq_cpu_get(cpu);
602 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500603 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 cpufreq_debug_enable_ratelimit();
605 return 0;
606 }
607#endif
608
609 if (!try_module_get(cpufreq_driver->owner)) {
610 ret = -EINVAL;
611 goto module_out;
612 }
613
Dave Jonese98df502005-10-20 15:17:43 -0700614 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (!policy) {
616 ret = -ENOMEM;
617 goto nomem_out;
618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 policy->cpu = cpu;
621 policy->cpus = cpumask_of_cpu(cpu);
622
Arjan van de Ven83933af2006-01-14 16:01:49 +0100623 mutex_init(&policy->lock);
624 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 init_completion(&policy->kobj_unregister);
626 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
627
628 /* call driver. From then on the cpufreq must be able
629 * to accept all calls to ->verify and ->setpolicy for this CPU
630 */
631 ret = cpufreq_driver->init(policy);
632 if (ret) {
633 dprintk("initialization failed\n");
Andrew Mortonf3876c12006-01-18 13:40:54 -0800634 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 goto err_out;
636 }
637
Dave Jones8ff69732006-03-05 03:37:23 -0500638#ifdef CONFIG_SMP
639 for_each_cpu_mask(j, policy->cpus) {
640 if (cpu == j)
641 continue;
642
643 /* check for existing affected CPUs. They may not be aware
644 * of it due to CPU Hotplug.
645 */
646 managed_policy = cpufreq_cpu_get(j);
647 if (unlikely(managed_policy)) {
648 spin_lock_irqsave(&cpufreq_driver_lock, flags);
649 managed_policy->cpus = policy->cpus;
650 cpufreq_cpu_data[cpu] = managed_policy;
651 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
652
653 dprintk("CPU already managed, adding link\n");
654 sysfs_create_link(&sys_dev->kobj,
655 &managed_policy->kobj, "cpufreq");
656
657 cpufreq_debug_enable_ratelimit();
658 mutex_unlock(&policy->lock);
659 ret = 0;
660 goto err_out_driver_exit; /* call driver->exit() */
661 }
662 }
663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
665
666 /* prepare interface data */
667 policy->kobj.parent = &sys_dev->kobj;
668 policy->kobj.ktype = &ktype_cpufreq;
669 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
670
671 ret = kobject_register(&policy->kobj);
Andrew Mortonf3876c12006-01-18 13:40:54 -0800672 if (ret) {
673 mutex_unlock(&policy->lock);
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700674 goto err_out_driver_exit;
Andrew Mortonf3876c12006-01-18 13:40:54 -0800675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 /* set up files for this cpu device */
677 drv_attr = cpufreq_driver->attr;
678 while ((drv_attr) && (*drv_attr)) {
679 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
680 drv_attr++;
681 }
682 if (cpufreq_driver->get)
683 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
684 if (cpufreq_driver->target)
685 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
686
687 spin_lock_irqsave(&cpufreq_driver_lock, flags);
688 for_each_cpu_mask(j, policy->cpus)
689 cpufreq_cpu_data[j] = policy;
690 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones8ff69732006-03-05 03:37:23 -0500691
692 /* symlink affected CPUs */
693 for_each_cpu_mask(j, policy->cpus) {
694 if (j == cpu)
695 continue;
696 if (!cpu_online(j))
697 continue;
698
Dave Jones1f8b2c92006-03-29 01:40:04 -0500699 dprintk("CPU %u already managed, adding link\n", j);
Dave Jones8ff69732006-03-05 03:37:23 -0500700 cpufreq_cpu_get(cpu);
701 cpu_sys_dev = get_cpu_sysdev(j);
702 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
703 "cpufreq");
704 }
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 policy->governor = NULL; /* to assure that the starting sequence is
707 * run in cpufreq_set_policy */
Arjan van de Ven83933af2006-01-14 16:01:49 +0100708 mutex_unlock(&policy->lock);
Dave Jones87c32272006-03-29 01:48:37 -0500709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /* set default policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 ret = cpufreq_set_policy(&new_policy);
712 if (ret) {
713 dprintk("setting policy failed\n");
714 goto err_out_unregister;
715 }
716
717 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 dprintk("initialization complete\n");
719 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -0500720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return 0;
722
723
724err_out_unregister:
725 spin_lock_irqsave(&cpufreq_driver_lock, flags);
726 for_each_cpu_mask(j, policy->cpus)
727 cpufreq_cpu_data[j] = NULL;
728 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
729
730 kobject_unregister(&policy->kobj);
731 wait_for_completion(&policy->kobj_unregister);
732
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700733err_out_driver_exit:
734 if (cpufreq_driver->exit)
735 cpufreq_driver->exit(policy);
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737err_out:
738 kfree(policy);
739
740nomem_out:
741 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800742module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 cpufreq_debug_enable_ratelimit();
744 return ret;
745}
746
747
748/**
749 * cpufreq_remove_dev - remove a CPU device
750 *
751 * Removes the cpufreq interface for a CPU device.
752 */
753static int cpufreq_remove_dev (struct sys_device * sys_dev)
754{
755 unsigned int cpu = sys_dev->id;
756 unsigned long flags;
757 struct cpufreq_policy *data;
758#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -0800759 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 unsigned int j;
761#endif
762
763 cpufreq_debug_disable_ratelimit();
764 dprintk("unregistering CPU %u\n", cpu);
765
766 spin_lock_irqsave(&cpufreq_driver_lock, flags);
767 data = cpufreq_cpu_data[cpu];
768
769 if (!data) {
770 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 cpufreq_debug_enable_ratelimit();
772 return -EINVAL;
773 }
774 cpufreq_cpu_data[cpu] = NULL;
775
776
777#ifdef CONFIG_SMP
778 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -0500779 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
781 if (unlikely(cpu != data->cpu)) {
782 dprintk("removing link\n");
Dave Jones8ff69732006-03-05 03:37:23 -0500783 cpu_clear(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
785 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 cpufreq_cpu_put(data);
787 cpufreq_debug_enable_ratelimit();
788 return 0;
789 }
790#endif
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (!kobject_get(&data->kobj)) {
794 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
795 cpufreq_debug_enable_ratelimit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500796 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
799#ifdef CONFIG_SMP
800 /* if we have other CPUs still registered, we need to unlink them,
801 * or else wait_for_completion below will lock up. Clean the
802 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
803 * links afterwards.
804 */
805 if (unlikely(cpus_weight(data->cpus) > 1)) {
806 for_each_cpu_mask(j, data->cpus) {
807 if (j == cpu)
808 continue;
809 cpufreq_cpu_data[j] = NULL;
810 }
811 }
812
813 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
814
815 if (unlikely(cpus_weight(data->cpus) > 1)) {
816 for_each_cpu_mask(j, data->cpus) {
817 if (j == cpu)
818 continue;
819 dprintk("removing link for cpu %u\n", j);
Ashok Rajd434fca2005-10-30 14:59:52 -0800820 cpu_sys_dev = get_cpu_sysdev(j);
821 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 cpufreq_cpu_put(data);
823 }
824 }
825#else
826 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
827#endif
828
Arjan van de Ven83933af2006-01-14 16:01:49 +0100829 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 if (cpufreq_driver->target)
831 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Arjan van de Ven83933af2006-01-14 16:01:49 +0100832 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 kobject_unregister(&data->kobj);
835
836 kobject_put(&data->kobj);
837
838 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -0500839 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * unloading.
841 */
842 dprintk("waiting for dropping of refcount\n");
843 wait_for_completion(&data->kobj_unregister);
844 dprintk("wait complete\n");
845
846 if (cpufreq_driver->exit)
847 cpufreq_driver->exit(data);
848
849 kfree(data);
850
851 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return 0;
853}
854
855
856static void handle_update(void *data)
857{
858 unsigned int cpu = (unsigned int)(long)data;
859 dprintk("handle_update for cpu %u called\n", cpu);
860 cpufreq_update_policy(cpu);
861}
862
863/**
864 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
865 * @cpu: cpu number
866 * @old_freq: CPU frequency the kernel thinks the CPU runs at
867 * @new_freq: CPU frequency the CPU actually runs at
868 *
869 * We adjust to current frequency first, and need to clean up later. So either call
870 * to cpufreq_update_policy() or schedule handle_update()).
871 */
872static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
873{
874 struct cpufreq_freqs freqs;
875
Jan Beulichb10eec22006-04-28 13:47:13 +0200876 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
878
879 freqs.cpu = cpu;
880 freqs.old = old_freq;
881 freqs.new = new_freq;
882 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
883 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
884}
885
886
Dave Jones32ee8c32006-02-28 00:43:23 -0500887/**
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800888 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
889 * @cpu: CPU number
890 *
891 * This is the last known freq, without actually getting it from the driver.
892 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
893 */
894unsigned int cpufreq_quick_get(unsigned int cpu)
895{
896 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
897 unsigned int ret = 0;
898
899 if (policy) {
Arjan van de Ven83933af2006-01-14 16:01:49 +0100900 mutex_lock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800901 ret = policy->cur;
Arjan van de Ven83933af2006-01-14 16:01:49 +0100902 mutex_unlock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800903 cpufreq_cpu_put(policy);
904 }
905
906 return (ret);
907}
908EXPORT_SYMBOL(cpufreq_quick_get);
909
910
Dave Jones32ee8c32006-02-28 00:43:23 -0500911/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 * cpufreq_get - get the current CPU frequency (in kHz)
913 * @cpu: CPU number
914 *
915 * Get the CPU current (static) CPU frequency
916 */
917unsigned int cpufreq_get(unsigned int cpu)
918{
919 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
920 unsigned int ret = 0;
921
922 if (!policy)
923 return 0;
924
925 if (!cpufreq_driver->get)
926 goto out;
927
Arjan van de Ven83933af2006-01-14 16:01:49 +0100928 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 ret = cpufreq_driver->get(cpu);
931
Dave Jones7d5e3502006-02-02 17:03:42 -0500932 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* verify no discrepancy between actual and saved value exists */
934 if (unlikely(ret != policy->cur)) {
935 cpufreq_out_of_sync(cpu, policy->cur, ret);
936 schedule_work(&policy->update);
937 }
938 }
939
Arjan van de Ven83933af2006-01-14 16:01:49 +0100940 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Dave Jones7d5e3502006-02-02 17:03:42 -0500942out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 cpufreq_cpu_put(policy);
944
945 return (ret);
946}
947EXPORT_SYMBOL(cpufreq_get);
948
949
950/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700951 * cpufreq_suspend - let the low level driver prepare for suspend
952 */
953
Bernard Blackhame00d9962005-07-07 17:56:42 -0700954static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700955{
956 int cpu = sysdev->id;
957 unsigned int ret = 0;
958 unsigned int cur_freq = 0;
959 struct cpufreq_policy *cpu_policy;
960
961 dprintk("resuming cpu %u\n", cpu);
962
963 if (!cpu_online(cpu))
964 return 0;
965
966 /* we may be lax here as interrupts are off. Nonetheless
967 * we need to grab the correct cpu policy, as to check
968 * whether we really run on this CPU.
969 */
970
971 cpu_policy = cpufreq_cpu_get(cpu);
972 if (!cpu_policy)
973 return -EINVAL;
974
975 /* only handle each CPU group once */
976 if (unlikely(cpu_policy->cpu != cpu)) {
977 cpufreq_cpu_put(cpu_policy);
978 return 0;
979 }
980
981 if (cpufreq_driver->suspend) {
Bernard Blackhame00d9962005-07-07 17:56:42 -0700982 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700983 if (ret) {
984 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
985 "step on CPU %u\n", cpu_policy->cpu);
986 cpufreq_cpu_put(cpu_policy);
987 return ret;
988 }
989 }
990
991
992 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
993 goto out;
994
995 if (cpufreq_driver->get)
996 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
997
998 if (!cur_freq || !cpu_policy->cur) {
999 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1000 "frequency is what timing core thinks it is.\n");
1001 goto out;
1002 }
1003
1004 if (unlikely(cur_freq != cpu_policy->cur)) {
1005 struct cpufreq_freqs freqs;
1006
1007 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001008 dprintk("Warning: CPU frequency is %u, "
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001009 "cpufreq assumed %u kHz.\n",
1010 cur_freq, cpu_policy->cur);
1011
1012 freqs.cpu = cpu;
1013 freqs.old = cpu_policy->cur;
1014 freqs.new = cur_freq;
1015
Alan Sterne041c682006-03-27 01:16:30 -08001016 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001017 CPUFREQ_SUSPENDCHANGE, &freqs);
1018 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1019
1020 cpu_policy->cur = cur_freq;
1021 }
1022
Dave Jones7d5e3502006-02-02 17:03:42 -05001023out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001024 cpufreq_cpu_put(cpu_policy);
1025 return 0;
1026}
1027
1028/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 * cpufreq_resume - restore proper CPU frequency handling after resume
1030 *
1031 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1032 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001033 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1034 * restored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
1036static int cpufreq_resume(struct sys_device * sysdev)
1037{
1038 int cpu = sysdev->id;
1039 unsigned int ret = 0;
1040 struct cpufreq_policy *cpu_policy;
1041
1042 dprintk("resuming cpu %u\n", cpu);
1043
1044 if (!cpu_online(cpu))
1045 return 0;
1046
1047 /* we may be lax here as interrupts are off. Nonetheless
1048 * we need to grab the correct cpu policy, as to check
1049 * whether we really run on this CPU.
1050 */
1051
1052 cpu_policy = cpufreq_cpu_get(cpu);
1053 if (!cpu_policy)
1054 return -EINVAL;
1055
1056 /* only handle each CPU group once */
1057 if (unlikely(cpu_policy->cpu != cpu)) {
1058 cpufreq_cpu_put(cpu_policy);
1059 return 0;
1060 }
1061
1062 if (cpufreq_driver->resume) {
1063 ret = cpufreq_driver->resume(cpu_policy);
1064 if (ret) {
1065 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1066 "step on CPU %u\n", cpu_policy->cpu);
1067 cpufreq_cpu_put(cpu_policy);
1068 return ret;
1069 }
1070 }
1071
1072 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1073 unsigned int cur_freq = 0;
1074
1075 if (cpufreq_driver->get)
1076 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1077
1078 if (!cur_freq || !cpu_policy->cur) {
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001079 printk(KERN_ERR "cpufreq: resume failed to assert "
1080 "current frequency is what timing core "
1081 "thinks it is.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 goto out;
1083 }
1084
1085 if (unlikely(cur_freq != cpu_policy->cur)) {
1086 struct cpufreq_freqs freqs;
1087
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001088 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001089 dprintk("Warning: CPU frequency"
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001090 "is %u, cpufreq assumed %u kHz.\n",
1091 cur_freq, cpu_policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 freqs.cpu = cpu;
1094 freqs.old = cpu_policy->cur;
1095 freqs.new = cur_freq;
1096
Alan Sterne041c682006-03-27 01:16:30 -08001097 blocking_notifier_call_chain(
1098 &cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001099 CPUFREQ_RESUMECHANGE, &freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1101
1102 cpu_policy->cur = cur_freq;
1103 }
1104 }
1105
1106out:
1107 schedule_work(&cpu_policy->update);
1108 cpufreq_cpu_put(cpu_policy);
1109 return ret;
1110}
1111
1112static struct sysdev_driver cpufreq_sysdev_driver = {
1113 .add = cpufreq_add_dev,
1114 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001115 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 .resume = cpufreq_resume,
1117};
1118
1119
1120/*********************************************************************
1121 * NOTIFIER LISTS INTERFACE *
1122 *********************************************************************/
1123
1124/**
1125 * cpufreq_register_notifier - register a driver with cpufreq
1126 * @nb: notifier function to register
1127 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1128 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001129 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 * are notified about clock rate changes (once before and once after
1131 * the transition), or a list of drivers that are notified about
1132 * changes in cpufreq policy.
1133 *
1134 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001135 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 */
1137int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1138{
1139 int ret;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 switch (list) {
1142 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001143 ret = blocking_notifier_chain_register(
1144 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 break;
1146 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001147 ret = blocking_notifier_chain_register(
1148 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 break;
1150 default:
1151 ret = -EINVAL;
1152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 return ret;
1155}
1156EXPORT_SYMBOL(cpufreq_register_notifier);
1157
1158
1159/**
1160 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1161 * @nb: notifier block to be unregistered
1162 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1163 *
1164 * Remove a driver from the CPU frequency notifier list.
1165 *
1166 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001167 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 */
1169int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1170{
1171 int ret;
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 switch (list) {
1174 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001175 ret = blocking_notifier_chain_unregister(
1176 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 break;
1178 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001179 ret = blocking_notifier_chain_unregister(
1180 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 break;
1182 default:
1183 ret = -EINVAL;
1184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 return ret;
1187}
1188EXPORT_SYMBOL(cpufreq_unregister_notifier);
1189
1190
1191/*********************************************************************
1192 * GOVERNORS *
1193 *********************************************************************/
1194
1195
1196int __cpufreq_driver_target(struct cpufreq_policy *policy,
1197 unsigned int target_freq,
1198 unsigned int relation)
1199{
1200 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001201
Ashok Raja9d9baa2005-11-28 13:43:46 -08001202 lock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1204 target_freq, relation);
1205 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1206 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001207
Ashok Raja9d9baa2005-11-28 13:43:46 -08001208 unlock_cpu_hotplug();
Ashok Raj90d45d12005-11-08 21:34:24 -08001209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return retval;
1211}
1212EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214int cpufreq_driver_target(struct cpufreq_policy *policy,
1215 unsigned int target_freq,
1216 unsigned int relation)
1217{
Dave Jonescc993ca2005-07-28 09:43:56 -07001218 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 policy = cpufreq_cpu_get(policy->cpu);
1221 if (!policy)
1222 return -EINVAL;
1223
Arjan van de Ven83933af2006-01-14 16:01:49 +01001224 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 ret = __cpufreq_driver_target(policy, target_freq, relation);
1227
Arjan van de Ven83933af2006-01-14 16:01:49 +01001228 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 return ret;
1232}
1233EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1234
1235
1236static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1237{
Dave Jonescc993ca2005-07-28 09:43:56 -07001238 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 if (!try_module_get(policy->governor->owner))
1241 return -EINVAL;
1242
1243 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1244 ret = policy->governor->governor(policy, event);
1245
1246 /* we keep one module reference alive for each CPU governed by this CPU */
1247 if ((event != CPUFREQ_GOV_START) || ret)
1248 module_put(policy->governor->owner);
1249 if ((event == CPUFREQ_GOV_STOP) && !ret)
1250 module_put(policy->governor->owner);
1251
1252 return ret;
1253}
1254
1255
1256int cpufreq_governor(unsigned int cpu, unsigned int event)
1257{
1258 int ret = 0;
1259 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1260
1261 if (!policy)
1262 return -EINVAL;
1263
Arjan van de Ven83933af2006-01-14 16:01:49 +01001264 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 ret = __cpufreq_governor(policy, event);
Arjan van de Ven83933af2006-01-14 16:01:49 +01001266 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return ret;
1270}
1271EXPORT_SYMBOL_GPL(cpufreq_governor);
1272
1273
1274int cpufreq_register_governor(struct cpufreq_governor *governor)
1275{
1276 struct cpufreq_governor *t;
1277
1278 if (!governor)
1279 return -EINVAL;
1280
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001281 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1284 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001285 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 return -EBUSY;
1287 }
1288 }
1289 list_add(&governor->governor_list, &cpufreq_governor_list);
1290
Dave Jones32ee8c32006-02-28 00:43:23 -05001291 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return 0;
1293}
1294EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1295
1296
1297void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1298{
1299 if (!governor)
1300 return;
1301
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001302 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001304 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return;
1306}
1307EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1308
1309
1310
1311/*********************************************************************
1312 * POLICY INTERFACE *
1313 *********************************************************************/
1314
1315/**
1316 * cpufreq_get_policy - get the current cpufreq_policy
1317 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1318 *
1319 * Reads the current cpufreq policy.
1320 */
1321int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1322{
1323 struct cpufreq_policy *cpu_policy;
1324 if (!policy)
1325 return -EINVAL;
1326
1327 cpu_policy = cpufreq_cpu_get(cpu);
1328 if (!cpu_policy)
1329 return -EINVAL;
1330
Arjan van de Ven83933af2006-01-14 16:01:49 +01001331 mutex_lock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Arjan van de Ven83933af2006-01-14 16:01:49 +01001333 mutex_unlock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return 0;
1337}
1338EXPORT_SYMBOL(cpufreq_get_policy);
1339
1340
1341static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1342{
1343 int ret = 0;
1344
1345 cpufreq_debug_disable_ratelimit();
1346 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1347 policy->min, policy->max);
1348
Dave Jones7d5e3502006-02-02 17:03:42 -05001349 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /* verify the cpu speed can be set within this limit */
1352 ret = cpufreq_driver->verify(policy);
1353 if (ret)
1354 goto error_out;
1355
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001357 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1358 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001361 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1362 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 /* verify the cpu speed can be set within this limit,
1365 which might be different to the first one */
1366 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001367 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001371 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1372 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Dave Jones7d5e3502006-02-02 17:03:42 -05001374 data->min = policy->min;
1375 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1378
1379 if (cpufreq_driver->setpolicy) {
1380 data->policy = policy->policy;
1381 dprintk("setting range\n");
1382 ret = cpufreq_driver->setpolicy(policy);
1383 } else {
1384 if (policy->governor != data->governor) {
1385 /* save old, working values */
1386 struct cpufreq_governor *old_gov = data->governor;
1387
1388 dprintk("governor switch\n");
1389
1390 /* end old governor */
1391 if (data->governor)
1392 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1393
1394 /* start new governor */
1395 data->governor = policy->governor;
1396 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1397 /* new governor failed, so re-start old one */
1398 dprintk("starting governor %s failed\n", data->governor->name);
1399 if (old_gov) {
1400 data->governor = old_gov;
1401 __cpufreq_governor(data, CPUFREQ_GOV_START);
1402 }
1403 ret = -EINVAL;
1404 goto error_out;
1405 }
1406 /* might be a policy change, too, so fall through */
1407 }
1408 dprintk("governor: change or update limits\n");
1409 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1410 }
1411
Dave Jones7d5e3502006-02-02 17:03:42 -05001412error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 cpufreq_debug_enable_ratelimit();
1414 return ret;
1415}
1416
1417/**
1418 * cpufreq_set_policy - set a new CPUFreq policy
1419 * @policy: policy to be set.
1420 *
1421 * Sets a new CPU frequency and voltage scaling policy.
1422 */
1423int cpufreq_set_policy(struct cpufreq_policy *policy)
1424{
1425 int ret = 0;
1426 struct cpufreq_policy *data;
1427
1428 if (!policy)
1429 return -EINVAL;
1430
1431 data = cpufreq_cpu_get(policy->cpu);
1432 if (!data)
1433 return -EINVAL;
1434
1435 /* lock this CPU */
Arjan van de Ven83933af2006-01-14 16:01:49 +01001436 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 ret = __cpufreq_set_policy(data, policy);
1439 data->user_policy.min = data->min;
1440 data->user_policy.max = data->max;
1441 data->user_policy.policy = data->policy;
1442 data->user_policy.governor = data->governor;
1443
Arjan van de Ven83933af2006-01-14 16:01:49 +01001444 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 cpufreq_cpu_put(data);
1446
1447 return ret;
1448}
1449EXPORT_SYMBOL(cpufreq_set_policy);
1450
1451
1452/**
1453 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1454 * @cpu: CPU which shall be re-evaluated
1455 *
1456 * Usefull for policy notifiers which have different necessities
1457 * at different times.
1458 */
1459int cpufreq_update_policy(unsigned int cpu)
1460{
1461 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1462 struct cpufreq_policy policy;
1463 int ret = 0;
1464
1465 if (!data)
1466 return -ENODEV;
1467
Arjan van de Ven83933af2006-01-14 16:01:49 +01001468 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001471 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 policy.min = data->user_policy.min;
1473 policy.max = data->user_policy.max;
1474 policy.policy = data->user_policy.policy;
1475 policy.governor = data->user_policy.governor;
1476
Thomas Renninger0961dd02006-01-26 18:46:33 +01001477 /* BIOS might change freq behind our back
1478 -> ask driver for current freq and notify governors about a change */
1479 if (cpufreq_driver->get) {
1480 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001481 if (!data->cur) {
1482 dprintk("Driver did not initialize current freq");
1483 data->cur = policy.cur;
1484 } else {
1485 if (data->cur != policy.cur)
1486 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1487 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001488 }
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 ret = __cpufreq_set_policy(data, &policy);
1491
Arjan van de Ven83933af2006-01-14 16:01:49 +01001492 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 cpufreq_cpu_put(data);
1495 return ret;
1496}
1497EXPORT_SYMBOL(cpufreq_update_policy);
1498
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001499#ifdef CONFIG_HOTPLUG_CPU
1500static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08001501 unsigned long action, void *hcpu)
1502{
1503 unsigned int cpu = (unsigned long)hcpu;
1504 struct cpufreq_policy *policy;
1505 struct sys_device *sys_dev;
1506
1507 sys_dev = get_cpu_sysdev(cpu);
1508
1509 if (sys_dev) {
1510 switch (action) {
1511 case CPU_ONLINE:
1512 cpufreq_add_dev(sys_dev);
1513 break;
1514 case CPU_DOWN_PREPARE:
1515 /*
1516 * We attempt to put this cpu in lowest frequency
1517 * possible before going down. This will permit
1518 * hardware-managed P-State to switch other related
1519 * threads to min or higher speeds if possible.
1520 */
1521 policy = cpufreq_cpu_data[cpu];
1522 if (policy) {
1523 cpufreq_driver_target(policy, policy->min,
1524 CPUFREQ_RELATION_H);
1525 }
1526 break;
1527 case CPU_DEAD:
1528 cpufreq_remove_dev(sys_dev);
1529 break;
1530 }
1531 }
1532 return NOTIFY_OK;
1533}
1534
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001535static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
Ashok Rajc32b6b82005-10-30 14:59:54 -08001536{
1537 .notifier_call = cpufreq_cpu_callback,
1538};
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001539#endif /* CONFIG_HOTPLUG_CPU */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541/*********************************************************************
1542 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1543 *********************************************************************/
1544
1545/**
1546 * cpufreq_register_driver - register a CPU Frequency driver
1547 * @driver_data: A struct cpufreq_driver containing the values#
1548 * submitted by the CPU Frequency driver.
1549 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001550 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05001552 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 *
1554 */
1555int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1556{
1557 unsigned long flags;
1558 int ret;
1559
1560 if (!driver_data || !driver_data->verify || !driver_data->init ||
1561 ((!driver_data->setpolicy) && (!driver_data->target)))
1562 return -EINVAL;
1563
1564 dprintk("trying to register driver %s\n", driver_data->name);
1565
1566 if (driver_data->setpolicy)
1567 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1568
1569 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1570 if (cpufreq_driver) {
1571 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1572 return -EBUSY;
1573 }
1574 cpufreq_driver = driver_data;
1575 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1576
1577 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1578
1579 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1580 int i;
1581 ret = -ENODEV;
1582
1583 /* check for at least one working CPU */
1584 for (i=0; i<NR_CPUS; i++)
1585 if (cpufreq_cpu_data[i])
1586 ret = 0;
1587
1588 /* if all ->init() calls failed, unregister */
1589 if (ret) {
1590 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1591 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1592
1593 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1594 cpufreq_driver = NULL;
1595 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1596 }
1597 }
1598
1599 if (!ret) {
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001600 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 dprintk("driver %s up and running\n", driver_data->name);
1602 cpufreq_debug_enable_ratelimit();
1603 }
1604
1605 return (ret);
1606}
1607EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1608
1609
1610/**
1611 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1612 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001613 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 * the right to do so, i.e. if you have succeeded in initialising before!
1615 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1616 * currently not initialised.
1617 */
1618int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1619{
1620 unsigned long flags;
1621
1622 cpufreq_debug_disable_ratelimit();
1623
1624 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1625 cpufreq_debug_enable_ratelimit();
1626 return -EINVAL;
1627 }
1628
1629 dprintk("unregistering driver %s\n", driver->name);
1630
1631 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07001632 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
1634 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1635 cpufreq_driver = NULL;
1636 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1637
1638 return 0;
1639}
1640EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);