blob: bc1088d9b379a97843c58b050f046b8b0df023f5 [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 \
Arjan van de Ven153d7f32006-07-26 15:40:07 +0200367 lock_cpu_hotplug(); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200368 mutex_lock(&policy->lock); \
369 ret = __cpufreq_set_policy(policy, &new_policy); \
370 policy->user_policy.object = policy->object; \
371 mutex_unlock(&policy->lock); \
Arjan van de Ven153d7f32006-07-26 15:40:07 +0200372 unlock_cpu_hotplug(); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 \
374 return ret ? ret : count; \
375}
376
377store_one(scaling_min_freq,min);
378store_one(scaling_max_freq,max);
379
380/**
381 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
382 */
383static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
384{
385 unsigned int cur_freq = cpufreq_get(policy->cpu);
386 if (!cur_freq)
387 return sprintf(buf, "<unknown>");
388 return sprintf(buf, "%u\n", cur_freq);
389}
390
391
392/**
393 * show_scaling_governor - show the current policy for the specified CPU
394 */
395static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
396{
397 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
398 return sprintf(buf, "powersave\n");
399 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
400 return sprintf(buf, "performance\n");
401 else if (policy->governor)
402 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
403 return -EINVAL;
404}
405
406
407/**
408 * store_scaling_governor - store policy for the specified CPU
409 */
Dave Jones32ee8c32006-02-28 00:43:23 -0500410static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
411 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413 unsigned int ret = -EINVAL;
414 char str_governor[16];
415 struct cpufreq_policy new_policy;
416
417 ret = cpufreq_get_policy(&new_policy, policy->cpu);
418 if (ret)
419 return ret;
420
421 ret = sscanf (buf, "%15s", str_governor);
422 if (ret != 1)
423 return -EINVAL;
424
425 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
426 return -EINVAL;
427
Dave Jonesa496e252006-07-07 12:31:27 -0400428 lock_cpu_hotplug();
429
Thomas Renninger7970e082006-04-13 15:14:04 +0200430 /* Do not use cpufreq_set_policy here or the user_policy.max
431 will be wrongly overridden */
432 mutex_lock(&policy->lock);
433 ret = __cpufreq_set_policy(policy, &new_policy);
434
435 policy->user_policy.policy = policy->policy;
436 policy->user_policy.governor = policy->governor;
437 mutex_unlock(&policy->lock);
438
Dave Jonesa496e252006-07-07 12:31:27 -0400439 unlock_cpu_hotplug();
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return ret ? ret : count;
442}
443
444/**
445 * show_scaling_driver - show the cpufreq driver currently loaded
446 */
447static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
448{
449 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
450}
451
452/**
453 * show_scaling_available_governors - show the available CPUfreq governors
454 */
455static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
456 char *buf)
457{
458 ssize_t i = 0;
459 struct cpufreq_governor *t;
460
461 if (!cpufreq_driver->target) {
462 i += sprintf(buf, "performance powersave");
463 goto out;
464 }
465
466 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
467 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
468 goto out;
469 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
470 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500471out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 i += sprintf(&buf[i], "\n");
473 return i;
474}
475/**
476 * show_affected_cpus - show the CPUs affected by each transition
477 */
478static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
479{
480 ssize_t i = 0;
481 unsigned int cpu;
482
483 for_each_cpu_mask(cpu, policy->cpus) {
484 if (i)
485 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
486 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
487 if (i >= (PAGE_SIZE - 5))
488 break;
489 }
490 i += sprintf(&buf[i], "\n");
491 return i;
492}
493
494
495#define define_one_ro(_name) \
496static struct freq_attr _name = \
497__ATTR(_name, 0444, show_##_name, NULL)
498
499#define define_one_ro0400(_name) \
500static struct freq_attr _name = \
501__ATTR(_name, 0400, show_##_name, NULL)
502
503#define define_one_rw(_name) \
504static struct freq_attr _name = \
505__ATTR(_name, 0644, show_##_name, store_##_name)
506
507define_one_ro0400(cpuinfo_cur_freq);
508define_one_ro(cpuinfo_min_freq);
509define_one_ro(cpuinfo_max_freq);
510define_one_ro(scaling_available_governors);
511define_one_ro(scaling_driver);
512define_one_ro(scaling_cur_freq);
513define_one_ro(affected_cpus);
514define_one_rw(scaling_min_freq);
515define_one_rw(scaling_max_freq);
516define_one_rw(scaling_governor);
517
518static struct attribute * default_attrs[] = {
519 &cpuinfo_min_freq.attr,
520 &cpuinfo_max_freq.attr,
521 &scaling_min_freq.attr,
522 &scaling_max_freq.attr,
523 &affected_cpus.attr,
524 &scaling_governor.attr,
525 &scaling_driver.attr,
526 &scaling_available_governors.attr,
527 NULL
528};
529
530#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
531#define to_attr(a) container_of(a,struct freq_attr,attr)
532
533static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
534{
535 struct cpufreq_policy * policy = to_policy(kobj);
536 struct freq_attr * fattr = to_attr(attr);
537 ssize_t ret;
538 policy = cpufreq_cpu_get(policy->cpu);
539 if (!policy)
540 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500541 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 cpufreq_cpu_put(policy);
543 return ret;
544}
545
Dave Jones32ee8c32006-02-28 00:43:23 -0500546static ssize_t store(struct kobject * kobj, struct attribute * attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 const char * buf, size_t count)
548{
549 struct cpufreq_policy * policy = to_policy(kobj);
550 struct freq_attr * fattr = to_attr(attr);
551 ssize_t ret;
552 policy = cpufreq_cpu_get(policy->cpu);
553 if (!policy)
554 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500555 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 cpufreq_cpu_put(policy);
557 return ret;
558}
559
560static void cpufreq_sysfs_release(struct kobject * kobj)
561{
562 struct cpufreq_policy * policy = to_policy(kobj);
563 dprintk("last reference is dropped\n");
564 complete(&policy->kobj_unregister);
565}
566
567static struct sysfs_ops sysfs_ops = {
568 .show = show,
569 .store = store,
570};
571
572static struct kobj_type ktype_cpufreq = {
573 .sysfs_ops = &sysfs_ops,
574 .default_attrs = default_attrs,
575 .release = cpufreq_sysfs_release,
576};
577
578
579/**
580 * cpufreq_add_dev - add a CPU device
581 *
Dave Jones32ee8c32006-02-28 00:43:23 -0500582 * Adds the cpufreq interface for a CPU device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
584static int cpufreq_add_dev (struct sys_device * sys_dev)
585{
586 unsigned int cpu = sys_dev->id;
587 int ret = 0;
588 struct cpufreq_policy new_policy;
589 struct cpufreq_policy *policy;
590 struct freq_attr **drv_attr;
Dave Jones8ff69732006-03-05 03:37:23 -0500591 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 unsigned long flags;
593 unsigned int j;
Dave Jones8ff69732006-03-05 03:37:23 -0500594#ifdef CONFIG_SMP
595 struct cpufreq_policy *managed_policy;
596#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Ashok Rajc32b6b82005-10-30 14:59:54 -0800598 if (cpu_is_offline(cpu))
599 return 0;
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 cpufreq_debug_disable_ratelimit();
602 dprintk("adding CPU %u\n", cpu);
603
604#ifdef CONFIG_SMP
605 /* check whether a different CPU already registered this
606 * CPU because it is in the same boat. */
607 policy = cpufreq_cpu_get(cpu);
608 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500609 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 cpufreq_debug_enable_ratelimit();
611 return 0;
612 }
613#endif
614
615 if (!try_module_get(cpufreq_driver->owner)) {
616 ret = -EINVAL;
617 goto module_out;
618 }
619
Dave Jonese98df502005-10-20 15:17:43 -0700620 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (!policy) {
622 ret = -ENOMEM;
623 goto nomem_out;
624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 policy->cpu = cpu;
627 policy->cpus = cpumask_of_cpu(cpu);
628
Arjan van de Ven83933af2006-01-14 16:01:49 +0100629 mutex_init(&policy->lock);
630 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 init_completion(&policy->kobj_unregister);
632 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
633
634 /* call driver. From then on the cpufreq must be able
635 * to accept all calls to ->verify and ->setpolicy for this CPU
636 */
637 ret = cpufreq_driver->init(policy);
638 if (ret) {
639 dprintk("initialization failed\n");
Andrew Mortonf3876c12006-01-18 13:40:54 -0800640 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 goto err_out;
642 }
643
Dave Jones8ff69732006-03-05 03:37:23 -0500644#ifdef CONFIG_SMP
645 for_each_cpu_mask(j, policy->cpus) {
646 if (cpu == j)
647 continue;
648
649 /* check for existing affected CPUs. They may not be aware
650 * of it due to CPU Hotplug.
651 */
652 managed_policy = cpufreq_cpu_get(j);
653 if (unlikely(managed_policy)) {
654 spin_lock_irqsave(&cpufreq_driver_lock, flags);
655 managed_policy->cpus = policy->cpus;
656 cpufreq_cpu_data[cpu] = managed_policy;
657 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
658
659 dprintk("CPU already managed, adding link\n");
660 sysfs_create_link(&sys_dev->kobj,
661 &managed_policy->kobj, "cpufreq");
662
663 cpufreq_debug_enable_ratelimit();
664 mutex_unlock(&policy->lock);
665 ret = 0;
666 goto err_out_driver_exit; /* call driver->exit() */
667 }
668 }
669#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
671
672 /* prepare interface data */
673 policy->kobj.parent = &sys_dev->kobj;
674 policy->kobj.ktype = &ktype_cpufreq;
675 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
676
677 ret = kobject_register(&policy->kobj);
Andrew Mortonf3876c12006-01-18 13:40:54 -0800678 if (ret) {
679 mutex_unlock(&policy->lock);
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700680 goto err_out_driver_exit;
Andrew Mortonf3876c12006-01-18 13:40:54 -0800681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* set up files for this cpu device */
683 drv_attr = cpufreq_driver->attr;
684 while ((drv_attr) && (*drv_attr)) {
685 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
686 drv_attr++;
687 }
688 if (cpufreq_driver->get)
689 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
690 if (cpufreq_driver->target)
691 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
692
693 spin_lock_irqsave(&cpufreq_driver_lock, flags);
694 for_each_cpu_mask(j, policy->cpus)
695 cpufreq_cpu_data[j] = policy;
696 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones8ff69732006-03-05 03:37:23 -0500697
698 /* symlink affected CPUs */
699 for_each_cpu_mask(j, policy->cpus) {
700 if (j == cpu)
701 continue;
702 if (!cpu_online(j))
703 continue;
704
Dave Jones1f8b2c92006-03-29 01:40:04 -0500705 dprintk("CPU %u already managed, adding link\n", j);
Dave Jones8ff69732006-03-05 03:37:23 -0500706 cpufreq_cpu_get(cpu);
707 cpu_sys_dev = get_cpu_sysdev(j);
708 sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
709 "cpufreq");
710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 policy->governor = NULL; /* to assure that the starting sequence is
713 * run in cpufreq_set_policy */
Arjan van de Ven83933af2006-01-14 16:01:49 +0100714 mutex_unlock(&policy->lock);
Dave Jones87c32272006-03-29 01:48:37 -0500715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 /* set default policy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ret = cpufreq_set_policy(&new_policy);
718 if (ret) {
719 dprintk("setting policy failed\n");
720 goto err_out_unregister;
721 }
722
723 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 dprintk("initialization complete\n");
725 cpufreq_debug_enable_ratelimit();
Dave Jones87c32272006-03-29 01:48:37 -0500726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 return 0;
728
729
730err_out_unregister:
731 spin_lock_irqsave(&cpufreq_driver_lock, flags);
732 for_each_cpu_mask(j, policy->cpus)
733 cpufreq_cpu_data[j] = NULL;
734 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
735
736 kobject_unregister(&policy->kobj);
737 wait_for_completion(&policy->kobj_unregister);
738
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700739err_out_driver_exit:
740 if (cpufreq_driver->exit)
741 cpufreq_driver->exit(policy);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743err_out:
744 kfree(policy);
745
746nomem_out:
747 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800748module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 cpufreq_debug_enable_ratelimit();
750 return ret;
751}
752
753
754/**
755 * cpufreq_remove_dev - remove a CPU device
756 *
757 * Removes the cpufreq interface for a CPU device.
758 */
759static int cpufreq_remove_dev (struct sys_device * sys_dev)
760{
761 unsigned int cpu = sys_dev->id;
762 unsigned long flags;
763 struct cpufreq_policy *data;
764#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -0800765 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 unsigned int j;
767#endif
768
769 cpufreq_debug_disable_ratelimit();
770 dprintk("unregistering CPU %u\n", cpu);
771
772 spin_lock_irqsave(&cpufreq_driver_lock, flags);
773 data = cpufreq_cpu_data[cpu];
774
775 if (!data) {
776 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 cpufreq_debug_enable_ratelimit();
778 return -EINVAL;
779 }
780 cpufreq_cpu_data[cpu] = NULL;
781
782
783#ifdef CONFIG_SMP
784 /* if this isn't the CPU which is the parent of the kobj, we
Dave Jones32ee8c32006-02-28 00:43:23 -0500785 * only need to unlink, put and exit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
787 if (unlikely(cpu != data->cpu)) {
788 dprintk("removing link\n");
Dave Jones8ff69732006-03-05 03:37:23 -0500789 cpu_clear(cpu, data->cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
791 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 cpufreq_cpu_put(data);
793 cpufreq_debug_enable_ratelimit();
794 return 0;
795 }
796#endif
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 if (!kobject_get(&data->kobj)) {
800 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
801 cpufreq_debug_enable_ratelimit();
Dave Jones32ee8c32006-02-28 00:43:23 -0500802 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
805#ifdef CONFIG_SMP
806 /* if we have other CPUs still registered, we need to unlink them,
807 * or else wait_for_completion below will lock up. Clean the
808 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
809 * links afterwards.
810 */
811 if (unlikely(cpus_weight(data->cpus) > 1)) {
812 for_each_cpu_mask(j, data->cpus) {
813 if (j == cpu)
814 continue;
815 cpufreq_cpu_data[j] = NULL;
816 }
817 }
818
819 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
820
821 if (unlikely(cpus_weight(data->cpus) > 1)) {
822 for_each_cpu_mask(j, data->cpus) {
823 if (j == cpu)
824 continue;
825 dprintk("removing link for cpu %u\n", j);
Ashok Rajd434fca2005-10-30 14:59:52 -0800826 cpu_sys_dev = get_cpu_sysdev(j);
827 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 cpufreq_cpu_put(data);
829 }
830 }
831#else
832 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
833#endif
834
Arjan van de Ven83933af2006-01-14 16:01:49 +0100835 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (cpufreq_driver->target)
837 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Arjan van de Ven83933af2006-01-14 16:01:49 +0100838 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 kobject_unregister(&data->kobj);
841
842 kobject_put(&data->kobj);
843
844 /* we need to make sure that the underlying kobj is actually
Dave Jones32ee8c32006-02-28 00:43:23 -0500845 * not referenced anymore by anybody before we proceed with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * unloading.
847 */
848 dprintk("waiting for dropping of refcount\n");
849 wait_for_completion(&data->kobj_unregister);
850 dprintk("wait complete\n");
851
852 if (cpufreq_driver->exit)
853 cpufreq_driver->exit(data);
854
855 kfree(data);
856
857 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return 0;
859}
860
861
862static void handle_update(void *data)
863{
864 unsigned int cpu = (unsigned int)(long)data;
865 dprintk("handle_update for cpu %u called\n", cpu);
866 cpufreq_update_policy(cpu);
867}
868
869/**
870 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
871 * @cpu: cpu number
872 * @old_freq: CPU frequency the kernel thinks the CPU runs at
873 * @new_freq: CPU frequency the CPU actually runs at
874 *
875 * We adjust to current frequency first, and need to clean up later. So either call
876 * to cpufreq_update_policy() or schedule handle_update()).
877 */
878static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
879{
880 struct cpufreq_freqs freqs;
881
Jan Beulichb10eec22006-04-28 13:47:13 +0200882 dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
884
885 freqs.cpu = cpu;
886 freqs.old = old_freq;
887 freqs.new = new_freq;
888 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
889 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
890}
891
892
Dave Jones32ee8c32006-02-28 00:43:23 -0500893/**
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800894 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
895 * @cpu: CPU number
896 *
897 * This is the last known freq, without actually getting it from the driver.
898 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
899 */
900unsigned int cpufreq_quick_get(unsigned int cpu)
901{
902 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
903 unsigned int ret = 0;
904
905 if (policy) {
Arjan van de Ven83933af2006-01-14 16:01:49 +0100906 mutex_lock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800907 ret = policy->cur;
Arjan van de Ven83933af2006-01-14 16:01:49 +0100908 mutex_unlock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800909 cpufreq_cpu_put(policy);
910 }
911
912 return (ret);
913}
914EXPORT_SYMBOL(cpufreq_quick_get);
915
916
Dave Jones32ee8c32006-02-28 00:43:23 -0500917/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 * cpufreq_get - get the current CPU frequency (in kHz)
919 * @cpu: CPU number
920 *
921 * Get the CPU current (static) CPU frequency
922 */
923unsigned int cpufreq_get(unsigned int cpu)
924{
925 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
926 unsigned int ret = 0;
927
928 if (!policy)
929 return 0;
930
931 if (!cpufreq_driver->get)
932 goto out;
933
Arjan van de Ven83933af2006-01-14 16:01:49 +0100934 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 ret = cpufreq_driver->get(cpu);
937
Dave Jones7d5e3502006-02-02 17:03:42 -0500938 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* verify no discrepancy between actual and saved value exists */
940 if (unlikely(ret != policy->cur)) {
941 cpufreq_out_of_sync(cpu, policy->cur, ret);
942 schedule_work(&policy->update);
943 }
944 }
945
Arjan van de Ven83933af2006-01-14 16:01:49 +0100946 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Dave Jones7d5e3502006-02-02 17:03:42 -0500948out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 cpufreq_cpu_put(policy);
950
951 return (ret);
952}
953EXPORT_SYMBOL(cpufreq_get);
954
955
956/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700957 * cpufreq_suspend - let the low level driver prepare for suspend
958 */
959
Bernard Blackhame00d9962005-07-07 17:56:42 -0700960static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700961{
962 int cpu = sysdev->id;
963 unsigned int ret = 0;
964 unsigned int cur_freq = 0;
965 struct cpufreq_policy *cpu_policy;
966
967 dprintk("resuming cpu %u\n", cpu);
968
969 if (!cpu_online(cpu))
970 return 0;
971
972 /* we may be lax here as interrupts are off. Nonetheless
973 * we need to grab the correct cpu policy, as to check
974 * whether we really run on this CPU.
975 */
976
977 cpu_policy = cpufreq_cpu_get(cpu);
978 if (!cpu_policy)
979 return -EINVAL;
980
981 /* only handle each CPU group once */
982 if (unlikely(cpu_policy->cpu != cpu)) {
983 cpufreq_cpu_put(cpu_policy);
984 return 0;
985 }
986
987 if (cpufreq_driver->suspend) {
Bernard Blackhame00d9962005-07-07 17:56:42 -0700988 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700989 if (ret) {
990 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
991 "step on CPU %u\n", cpu_policy->cpu);
992 cpufreq_cpu_put(cpu_policy);
993 return ret;
994 }
995 }
996
997
998 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
999 goto out;
1000
1001 if (cpufreq_driver->get)
1002 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1003
1004 if (!cur_freq || !cpu_policy->cur) {
1005 printk(KERN_ERR "cpufreq: suspend failed to assert current "
1006 "frequency is what timing core thinks it is.\n");
1007 goto out;
1008 }
1009
1010 if (unlikely(cur_freq != cpu_policy->cur)) {
1011 struct cpufreq_freqs freqs;
1012
1013 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001014 dprintk("Warning: CPU frequency is %u, "
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001015 "cpufreq assumed %u kHz.\n",
1016 cur_freq, cpu_policy->cur);
1017
1018 freqs.cpu = cpu;
1019 freqs.old = cpu_policy->cur;
1020 freqs.new = cur_freq;
1021
Alan Sterne041c682006-03-27 01:16:30 -08001022 blocking_notifier_call_chain(&cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001023 CPUFREQ_SUSPENDCHANGE, &freqs);
1024 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
1025
1026 cpu_policy->cur = cur_freq;
1027 }
1028
Dave Jones7d5e3502006-02-02 17:03:42 -05001029out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001030 cpufreq_cpu_put(cpu_policy);
1031 return 0;
1032}
1033
1034/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 * cpufreq_resume - restore proper CPU frequency handling after resume
1036 *
1037 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1038 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001039 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
1040 * restored.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 */
1042static int cpufreq_resume(struct sys_device * sysdev)
1043{
1044 int cpu = sysdev->id;
1045 unsigned int ret = 0;
1046 struct cpufreq_policy *cpu_policy;
1047
1048 dprintk("resuming cpu %u\n", cpu);
1049
1050 if (!cpu_online(cpu))
1051 return 0;
1052
1053 /* we may be lax here as interrupts are off. Nonetheless
1054 * we need to grab the correct cpu policy, as to check
1055 * whether we really run on this CPU.
1056 */
1057
1058 cpu_policy = cpufreq_cpu_get(cpu);
1059 if (!cpu_policy)
1060 return -EINVAL;
1061
1062 /* only handle each CPU group once */
1063 if (unlikely(cpu_policy->cpu != cpu)) {
1064 cpufreq_cpu_put(cpu_policy);
1065 return 0;
1066 }
1067
1068 if (cpufreq_driver->resume) {
1069 ret = cpufreq_driver->resume(cpu_policy);
1070 if (ret) {
1071 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1072 "step on CPU %u\n", cpu_policy->cpu);
1073 cpufreq_cpu_put(cpu_policy);
1074 return ret;
1075 }
1076 }
1077
1078 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1079 unsigned int cur_freq = 0;
1080
1081 if (cpufreq_driver->get)
1082 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1083
1084 if (!cur_freq || !cpu_policy->cur) {
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001085 printk(KERN_ERR "cpufreq: resume failed to assert "
1086 "current frequency is what timing core "
1087 "thinks it is.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 goto out;
1089 }
1090
1091 if (unlikely(cur_freq != cpu_policy->cur)) {
1092 struct cpufreq_freqs freqs;
1093
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001094 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Jan Beulichb10eec22006-04-28 13:47:13 +02001095 dprintk("Warning: CPU frequency"
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001096 "is %u, cpufreq assumed %u kHz.\n",
1097 cur_freq, cpu_policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 freqs.cpu = cpu;
1100 freqs.old = cpu_policy->cur;
1101 freqs.new = cur_freq;
1102
Alan Sterne041c682006-03-27 01:16:30 -08001103 blocking_notifier_call_chain(
1104 &cpufreq_transition_notifier_list,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001105 CPUFREQ_RESUMECHANGE, &freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1107
1108 cpu_policy->cur = cur_freq;
1109 }
1110 }
1111
1112out:
1113 schedule_work(&cpu_policy->update);
1114 cpufreq_cpu_put(cpu_policy);
1115 return ret;
1116}
1117
1118static struct sysdev_driver cpufreq_sysdev_driver = {
1119 .add = cpufreq_add_dev,
1120 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001121 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 .resume = cpufreq_resume,
1123};
1124
1125
1126/*********************************************************************
1127 * NOTIFIER LISTS INTERFACE *
1128 *********************************************************************/
1129
1130/**
1131 * cpufreq_register_notifier - register a driver with cpufreq
1132 * @nb: notifier function to register
1133 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1134 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001135 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 * are notified about clock rate changes (once before and once after
1137 * the transition), or a list of drivers that are notified about
1138 * changes in cpufreq policy.
1139 *
1140 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001141 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 */
1143int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1144{
1145 int ret;
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 switch (list) {
1148 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001149 ret = blocking_notifier_chain_register(
1150 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
1152 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001153 ret = blocking_notifier_chain_register(
1154 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 break;
1156 default:
1157 ret = -EINVAL;
1158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 return ret;
1161}
1162EXPORT_SYMBOL(cpufreq_register_notifier);
1163
1164
1165/**
1166 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1167 * @nb: notifier block to be unregistered
1168 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1169 *
1170 * Remove a driver from the CPU frequency notifier list.
1171 *
1172 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001173 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
1175int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1176{
1177 int ret;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 switch (list) {
1180 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001181 ret = blocking_notifier_chain_unregister(
1182 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 break;
1184 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001185 ret = blocking_notifier_chain_unregister(
1186 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 break;
1188 default:
1189 ret = -EINVAL;
1190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 return ret;
1193}
1194EXPORT_SYMBOL(cpufreq_unregister_notifier);
1195
1196
1197/*********************************************************************
1198 * GOVERNORS *
1199 *********************************************************************/
1200
1201
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001202/* Must be called with lock_cpu_hotplug held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203int __cpufreq_driver_target(struct cpufreq_policy *policy,
1204 unsigned int target_freq,
1205 unsigned int relation)
1206{
1207 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1210 target_freq, relation);
1211 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1212 retval = cpufreq_driver->target(policy, target_freq, relation);
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 Ven153d7f32006-07-26 15:40:07 +02001228 lock_cpu_hotplug();
Arjan van de Ven83933af2006-01-14 16:01:49 +01001229 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 ret = __cpufreq_driver_target(policy, target_freq, relation);
1232
Arjan van de Ven83933af2006-01-14 16:01:49 +01001233 mutex_unlock(&policy->lock);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001234 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return ret;
1238}
1239EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1240
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001241/*
1242 * Locking: Must be called with the lock_cpu_hotplug() lock held
1243 * when "event" is CPUFREQ_GOV_LIMITS
1244 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1247{
Dave Jonescc993ca2005-07-28 09:43:56 -07001248 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 if (!try_module_get(policy->governor->owner))
1251 return -EINVAL;
1252
1253 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1254 ret = policy->governor->governor(policy, event);
1255
1256 /* we keep one module reference alive for each CPU governed by this CPU */
1257 if ((event != CPUFREQ_GOV_START) || ret)
1258 module_put(policy->governor->owner);
1259 if ((event == CPUFREQ_GOV_STOP) && !ret)
1260 module_put(policy->governor->owner);
1261
1262 return ret;
1263}
1264
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266int cpufreq_register_governor(struct cpufreq_governor *governor)
1267{
1268 struct cpufreq_governor *t;
1269
1270 if (!governor)
1271 return -EINVAL;
1272
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001273 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1276 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001277 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 return -EBUSY;
1279 }
1280 }
1281 list_add(&governor->governor_list, &cpufreq_governor_list);
1282
Dave Jones32ee8c32006-02-28 00:43:23 -05001283 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 0;
1285}
1286EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1287
1288
1289void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1290{
1291 if (!governor)
1292 return;
1293
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001294 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001296 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return;
1298}
1299EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1300
1301
1302
1303/*********************************************************************
1304 * POLICY INTERFACE *
1305 *********************************************************************/
1306
1307/**
1308 * cpufreq_get_policy - get the current cpufreq_policy
1309 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1310 *
1311 * Reads the current cpufreq policy.
1312 */
1313int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1314{
1315 struct cpufreq_policy *cpu_policy;
1316 if (!policy)
1317 return -EINVAL;
1318
1319 cpu_policy = cpufreq_cpu_get(cpu);
1320 if (!cpu_policy)
1321 return -EINVAL;
1322
Arjan van de Ven83933af2006-01-14 16:01:49 +01001323 mutex_lock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Arjan van de Ven83933af2006-01-14 16:01:49 +01001325 mutex_unlock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return 0;
1329}
1330EXPORT_SYMBOL(cpufreq_get_policy);
1331
1332
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001333/*
1334 * Locking: Must be called with the lock_cpu_hotplug() lock held
1335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1337{
1338 int ret = 0;
1339
1340 cpufreq_debug_disable_ratelimit();
1341 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1342 policy->min, policy->max);
1343
Dave Jones7d5e3502006-02-02 17:03:42 -05001344 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346 /* verify the cpu speed can be set within this limit */
1347 ret = cpufreq_driver->verify(policy);
1348 if (ret)
1349 goto error_out;
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001352 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1353 CPUFREQ_ADJUST, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001356 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1357 CPUFREQ_INCOMPATIBLE, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358
1359 /* verify the cpu speed can be set within this limit,
1360 which might be different to the first one */
1361 ret = cpufreq_driver->verify(policy);
Alan Sterne041c682006-03-27 01:16:30 -08001362 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001366 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1367 CPUFREQ_NOTIFY, policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Dave Jones7d5e3502006-02-02 17:03:42 -05001369 data->min = policy->min;
1370 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1373
1374 if (cpufreq_driver->setpolicy) {
1375 data->policy = policy->policy;
1376 dprintk("setting range\n");
1377 ret = cpufreq_driver->setpolicy(policy);
1378 } else {
1379 if (policy->governor != data->governor) {
1380 /* save old, working values */
1381 struct cpufreq_governor *old_gov = data->governor;
1382
1383 dprintk("governor switch\n");
1384
1385 /* end old governor */
1386 if (data->governor)
1387 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1388
1389 /* start new governor */
1390 data->governor = policy->governor;
1391 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1392 /* new governor failed, so re-start old one */
1393 dprintk("starting governor %s failed\n", data->governor->name);
1394 if (old_gov) {
1395 data->governor = old_gov;
1396 __cpufreq_governor(data, CPUFREQ_GOV_START);
1397 }
1398 ret = -EINVAL;
1399 goto error_out;
1400 }
1401 /* might be a policy change, too, so fall through */
1402 }
1403 dprintk("governor: change or update limits\n");
1404 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1405 }
1406
Dave Jones7d5e3502006-02-02 17:03:42 -05001407error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 cpufreq_debug_enable_ratelimit();
1409 return ret;
1410}
1411
1412/**
1413 * cpufreq_set_policy - set a new CPUFreq policy
1414 * @policy: policy to be set.
1415 *
1416 * Sets a new CPU frequency and voltage scaling policy.
1417 */
1418int cpufreq_set_policy(struct cpufreq_policy *policy)
1419{
1420 int ret = 0;
1421 struct cpufreq_policy *data;
1422
1423 if (!policy)
1424 return -EINVAL;
1425
1426 data = cpufreq_cpu_get(policy->cpu);
1427 if (!data)
1428 return -EINVAL;
1429
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001430 lock_cpu_hotplug();
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* lock this CPU */
Arjan van de Ven83933af2006-01-14 16:01:49 +01001433 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435 ret = __cpufreq_set_policy(data, policy);
1436 data->user_policy.min = data->min;
1437 data->user_policy.max = data->max;
1438 data->user_policy.policy = data->policy;
1439 data->user_policy.governor = data->governor;
1440
Arjan van de Ven83933af2006-01-14 16:01:49 +01001441 mutex_unlock(&data->lock);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001442
1443 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 cpufreq_cpu_put(data);
1445
1446 return ret;
1447}
1448EXPORT_SYMBOL(cpufreq_set_policy);
1449
1450
1451/**
1452 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1453 * @cpu: CPU which shall be re-evaluated
1454 *
1455 * Usefull for policy notifiers which have different necessities
1456 * at different times.
1457 */
1458int cpufreq_update_policy(unsigned int cpu)
1459{
1460 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1461 struct cpufreq_policy policy;
1462 int ret = 0;
1463
1464 if (!data)
1465 return -ENODEV;
1466
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001467 lock_cpu_hotplug();
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);
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001493 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 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);