blob: 9582de1c9cadc543517b1aff92d8a7910960a7eb [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>
8 * Added handling for CPU hotplug
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
16#include <linux/config.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/notifier.h>
21#include <linux/cpufreq.h>
22#include <linux/delay.h>
23#include <linux/interrupt.h>
24#include <linux/spinlock.h>
25#include <linux/device.h>
26#include <linux/slab.h>
27#include <linux/cpu.h>
28#include <linux/completion.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080029#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, "cpufreq-core", msg)
32
33/**
34 * The "cpufreq driver" - the arch- or hardware-dependend low
35 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
37 */
Dave Jones7d5e3502006-02-02 17:03:42 -050038static struct cpufreq_driver *cpufreq_driver;
39static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static DEFINE_SPINLOCK(cpufreq_driver_lock);
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042/* internal prototypes */
43static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
44static void handle_update(void *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/**
47 * Two notifier lists: the "policy" list is involved in the
48 * validation process for a new CPU frequency policy; the
49 * "transition" list for kernel code that needs to handle
50 * changes to devices when the CPU clock speed changes.
51 * The mutex locks both lists.
52 */
Dave Jones7d5e3502006-02-02 17:03:42 -050053static struct notifier_block *cpufreq_policy_notifier_list;
54static struct notifier_block *cpufreq_transition_notifier_list;
55static DECLARE_RWSEM (cpufreq_notifier_rwsem);
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;
154
155 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
201 * systems as each CPU might be scaled differently. So, use the arch
202 * 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
236 * 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
248 down_read(&cpufreq_notifier_rwsem);
Dave Jonese4472cb2006-01-31 15:53:55 -0800249
250 policy = cpufreq_cpu_data[freqs->cpu];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 case CPUFREQ_PRECHANGE:
Dave Jonese4472cb2006-01-31 15:53:55 -0800254 /* detect if the driver reported a value as "old frequency"
255 * which is not equal to what the cpufreq core thinks is
256 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
258 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800259 if ((policy) && (policy->cpu == freqs->cpu) &&
260 (policy->cur) && (policy->cur != freqs->old)) {
261 dprintk(KERN_WARNING "Warning: CPU frequency is"
262 " %u, cpufreq assumed %u kHz.\n",
263 freqs->old, policy->cur);
264 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 }
Dave Jonese4472cb2006-01-31 15:53:55 -0800267 notifier_call_chain(&cpufreq_transition_notifier_list,
268 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
270 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 case CPUFREQ_POSTCHANGE:
273 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800274 notifier_call_chain(&cpufreq_transition_notifier_list,
275 CPUFREQ_POSTCHANGE, freqs);
276 if (likely(policy) && likely(policy->cpu == freqs->cpu))
277 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
279 }
280 up_read(&cpufreq_notifier_rwsem);
281}
282EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
283
284
285
286/*********************************************************************
287 * SYSFS INTERFACE *
288 *********************************************************************/
289
290/**
291 * cpufreq_parse_governor - parse a governor string
292 */
293static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
294 struct cpufreq_governor **governor)
295{
296 if (!cpufreq_driver)
297 return -EINVAL;
298 if (cpufreq_driver->setpolicy) {
299 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
300 *policy = CPUFREQ_POLICY_PERFORMANCE;
301 return 0;
302 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
303 *policy = CPUFREQ_POLICY_POWERSAVE;
304 return 0;
305 }
306 return -EINVAL;
307 } else {
308 struct cpufreq_governor *t;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800309 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!cpufreq_driver || !cpufreq_driver->target)
311 goto out;
312 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
313 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
314 *governor = t;
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800315 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return 0;
317 }
318 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500319out:
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800320 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322 return -EINVAL;
323}
324EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
325
326
327/* drivers/base/cpu.c */
328extern struct sysdev_class cpu_sysdev_class;
329
330
331/**
332 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
333 *
334 * Write out information from cpufreq_driver->policy[cpu]; object must be
335 * "unsigned int".
336 */
337
338#define show_one(file_name, object) \
339static ssize_t show_##file_name \
340(struct cpufreq_policy * policy, char *buf) \
341{ \
342 return sprintf (buf, "%u\n", policy->object); \
343}
344
345show_one(cpuinfo_min_freq, cpuinfo.min_freq);
346show_one(cpuinfo_max_freq, cpuinfo.max_freq);
347show_one(scaling_min_freq, min);
348show_one(scaling_max_freq, max);
349show_one(scaling_cur_freq, cur);
350
351/**
352 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
353 */
354#define store_one(file_name, object) \
355static ssize_t store_##file_name \
356(struct cpufreq_policy * policy, const char *buf, size_t count) \
357{ \
358 unsigned int ret = -EINVAL; \
359 struct cpufreq_policy new_policy; \
360 \
361 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
362 if (ret) \
363 return -EINVAL; \
364 \
365 ret = sscanf (buf, "%u", &new_policy.object); \
366 if (ret != 1) \
367 return -EINVAL; \
368 \
369 ret = cpufreq_set_policy(&new_policy); \
370 \
371 return ret ? ret : count; \
372}
373
374store_one(scaling_min_freq,min);
375store_one(scaling_max_freq,max);
376
377/**
378 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
379 */
380static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
381{
382 unsigned int cur_freq = cpufreq_get(policy->cpu);
383 if (!cur_freq)
384 return sprintf(buf, "<unknown>");
385 return sprintf(buf, "%u\n", cur_freq);
386}
387
388
389/**
390 * show_scaling_governor - show the current policy for the specified CPU
391 */
392static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
393{
394 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
395 return sprintf(buf, "powersave\n");
396 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
397 return sprintf(buf, "performance\n");
398 else if (policy->governor)
399 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
400 return -EINVAL;
401}
402
403
404/**
405 * store_scaling_governor - store policy for the specified CPU
406 */
407static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
408 const char *buf, size_t count)
409{
410 unsigned int ret = -EINVAL;
411 char str_governor[16];
412 struct cpufreq_policy new_policy;
413
414 ret = cpufreq_get_policy(&new_policy, policy->cpu);
415 if (ret)
416 return ret;
417
418 ret = sscanf (buf, "%15s", str_governor);
419 if (ret != 1)
420 return -EINVAL;
421
422 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
423 return -EINVAL;
424
425 ret = cpufreq_set_policy(&new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return ret ? ret : count;
427}
428
429/**
430 * show_scaling_driver - show the cpufreq driver currently loaded
431 */
432static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
433{
434 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
435}
436
437/**
438 * show_scaling_available_governors - show the available CPUfreq governors
439 */
440static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
441 char *buf)
442{
443 ssize_t i = 0;
444 struct cpufreq_governor *t;
445
446 if (!cpufreq_driver->target) {
447 i += sprintf(buf, "performance powersave");
448 goto out;
449 }
450
451 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
452 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
453 goto out;
454 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
455 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500456out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 i += sprintf(&buf[i], "\n");
458 return i;
459}
460/**
461 * show_affected_cpus - show the CPUs affected by each transition
462 */
463static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
464{
465 ssize_t i = 0;
466 unsigned int cpu;
467
468 for_each_cpu_mask(cpu, policy->cpus) {
469 if (i)
470 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
471 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
472 if (i >= (PAGE_SIZE - 5))
473 break;
474 }
475 i += sprintf(&buf[i], "\n");
476 return i;
477}
478
479
480#define define_one_ro(_name) \
481static struct freq_attr _name = \
482__ATTR(_name, 0444, show_##_name, NULL)
483
484#define define_one_ro0400(_name) \
485static struct freq_attr _name = \
486__ATTR(_name, 0400, show_##_name, NULL)
487
488#define define_one_rw(_name) \
489static struct freq_attr _name = \
490__ATTR(_name, 0644, show_##_name, store_##_name)
491
492define_one_ro0400(cpuinfo_cur_freq);
493define_one_ro(cpuinfo_min_freq);
494define_one_ro(cpuinfo_max_freq);
495define_one_ro(scaling_available_governors);
496define_one_ro(scaling_driver);
497define_one_ro(scaling_cur_freq);
498define_one_ro(affected_cpus);
499define_one_rw(scaling_min_freq);
500define_one_rw(scaling_max_freq);
501define_one_rw(scaling_governor);
502
503static struct attribute * default_attrs[] = {
504 &cpuinfo_min_freq.attr,
505 &cpuinfo_max_freq.attr,
506 &scaling_min_freq.attr,
507 &scaling_max_freq.attr,
508 &affected_cpus.attr,
509 &scaling_governor.attr,
510 &scaling_driver.attr,
511 &scaling_available_governors.attr,
512 NULL
513};
514
515#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
516#define to_attr(a) container_of(a,struct freq_attr,attr)
517
518static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
519{
520 struct cpufreq_policy * policy = to_policy(kobj);
521 struct freq_attr * fattr = to_attr(attr);
522 ssize_t ret;
523 policy = cpufreq_cpu_get(policy->cpu);
524 if (!policy)
525 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500526 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 cpufreq_cpu_put(policy);
528 return ret;
529}
530
531static ssize_t store(struct kobject * kobj, struct attribute * attr,
532 const char * buf, size_t count)
533{
534 struct cpufreq_policy * policy = to_policy(kobj);
535 struct freq_attr * fattr = to_attr(attr);
536 ssize_t ret;
537 policy = cpufreq_cpu_get(policy->cpu);
538 if (!policy)
539 return -EINVAL;
Dmitry Torokhov70f28172005-04-29 01:27:34 -0500540 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 cpufreq_cpu_put(policy);
542 return ret;
543}
544
545static void cpufreq_sysfs_release(struct kobject * kobj)
546{
547 struct cpufreq_policy * policy = to_policy(kobj);
548 dprintk("last reference is dropped\n");
549 complete(&policy->kobj_unregister);
550}
551
552static struct sysfs_ops sysfs_ops = {
553 .show = show,
554 .store = store,
555};
556
557static struct kobj_type ktype_cpufreq = {
558 .sysfs_ops = &sysfs_ops,
559 .default_attrs = default_attrs,
560 .release = cpufreq_sysfs_release,
561};
562
563
564/**
565 * cpufreq_add_dev - add a CPU device
566 *
567 * Adds the cpufreq interface for a CPU device.
568 */
569static int cpufreq_add_dev (struct sys_device * sys_dev)
570{
571 unsigned int cpu = sys_dev->id;
572 int ret = 0;
573 struct cpufreq_policy new_policy;
574 struct cpufreq_policy *policy;
575 struct freq_attr **drv_attr;
576 unsigned long flags;
577 unsigned int j;
578
Ashok Rajc32b6b82005-10-30 14:59:54 -0800579 if (cpu_is_offline(cpu))
580 return 0;
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 cpufreq_debug_disable_ratelimit();
583 dprintk("adding CPU %u\n", cpu);
584
585#ifdef CONFIG_SMP
586 /* check whether a different CPU already registered this
587 * CPU because it is in the same boat. */
588 policy = cpufreq_cpu_get(cpu);
589 if (unlikely(policy)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 dprintk("CPU already managed, adding link\n");
591 sysfs_create_link(&sys_dev->kobj, &policy->kobj, "cpufreq");
592 cpufreq_debug_enable_ratelimit();
593 return 0;
594 }
595#endif
596
597 if (!try_module_get(cpufreq_driver->owner)) {
598 ret = -EINVAL;
599 goto module_out;
600 }
601
Dave Jonese98df502005-10-20 15:17:43 -0700602 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (!policy) {
604 ret = -ENOMEM;
605 goto nomem_out;
606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 policy->cpu = cpu;
609 policy->cpus = cpumask_of_cpu(cpu);
610
Arjan van de Ven83933af2006-01-14 16:01:49 +0100611 mutex_init(&policy->lock);
612 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 init_completion(&policy->kobj_unregister);
614 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
615
616 /* call driver. From then on the cpufreq must be able
617 * to accept all calls to ->verify and ->setpolicy for this CPU
618 */
619 ret = cpufreq_driver->init(policy);
620 if (ret) {
621 dprintk("initialization failed\n");
Andrew Mortonf3876c12006-01-18 13:40:54 -0800622 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 goto err_out;
624 }
625
626 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
627
628 /* prepare interface data */
629 policy->kobj.parent = &sys_dev->kobj;
630 policy->kobj.ktype = &ktype_cpufreq;
631 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
632
633 ret = kobject_register(&policy->kobj);
Andrew Mortonf3876c12006-01-18 13:40:54 -0800634 if (ret) {
635 mutex_unlock(&policy->lock);
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700636 goto err_out_driver_exit;
Andrew Mortonf3876c12006-01-18 13:40:54 -0800637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /* set up files for this cpu device */
639 drv_attr = cpufreq_driver->attr;
640 while ((drv_attr) && (*drv_attr)) {
641 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
642 drv_attr++;
643 }
644 if (cpufreq_driver->get)
645 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
646 if (cpufreq_driver->target)
647 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
648
649 spin_lock_irqsave(&cpufreq_driver_lock, flags);
650 for_each_cpu_mask(j, policy->cpus)
651 cpufreq_cpu_data[j] = policy;
652 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
653 policy->governor = NULL; /* to assure that the starting sequence is
654 * run in cpufreq_set_policy */
Arjan van de Ven83933af2006-01-14 16:01:49 +0100655 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 /* set default policy */
658
659 ret = cpufreq_set_policy(&new_policy);
660 if (ret) {
661 dprintk("setting policy failed\n");
662 goto err_out_unregister;
663 }
664
665 module_put(cpufreq_driver->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 dprintk("initialization complete\n");
667 cpufreq_debug_enable_ratelimit();
668
669 return 0;
670
671
672err_out_unregister:
673 spin_lock_irqsave(&cpufreq_driver_lock, flags);
674 for_each_cpu_mask(j, policy->cpus)
675 cpufreq_cpu_data[j] = NULL;
676 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
677
678 kobject_unregister(&policy->kobj);
679 wait_for_completion(&policy->kobj_unregister);
680
Venkatesh Pallipadi8085e1f2005-08-25 13:14:06 -0700681err_out_driver_exit:
682 if (cpufreq_driver->exit)
683 cpufreq_driver->exit(policy);
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685err_out:
686 kfree(policy);
687
688nomem_out:
689 module_put(cpufreq_driver->owner);
Ashok Rajc32b6b82005-10-30 14:59:54 -0800690module_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 cpufreq_debug_enable_ratelimit();
692 return ret;
693}
694
695
696/**
697 * cpufreq_remove_dev - remove a CPU device
698 *
699 * Removes the cpufreq interface for a CPU device.
700 */
701static int cpufreq_remove_dev (struct sys_device * sys_dev)
702{
703 unsigned int cpu = sys_dev->id;
704 unsigned long flags;
705 struct cpufreq_policy *data;
706#ifdef CONFIG_SMP
Grant Coadye738cf62005-11-21 21:32:28 -0800707 struct sys_device *cpu_sys_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 unsigned int j;
709#endif
710
711 cpufreq_debug_disable_ratelimit();
712 dprintk("unregistering CPU %u\n", cpu);
713
714 spin_lock_irqsave(&cpufreq_driver_lock, flags);
715 data = cpufreq_cpu_data[cpu];
716
717 if (!data) {
718 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 cpufreq_debug_enable_ratelimit();
720 return -EINVAL;
721 }
722 cpufreq_cpu_data[cpu] = NULL;
723
724
725#ifdef CONFIG_SMP
726 /* if this isn't the CPU which is the parent of the kobj, we
727 * only need to unlink, put and exit
728 */
729 if (unlikely(cpu != data->cpu)) {
730 dprintk("removing link\n");
731 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
732 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 cpufreq_cpu_put(data);
734 cpufreq_debug_enable_ratelimit();
735 return 0;
736 }
737#endif
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (!kobject_get(&data->kobj)) {
741 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
742 cpufreq_debug_enable_ratelimit();
743 return -EFAULT;
744 }
745
746#ifdef CONFIG_SMP
747 /* if we have other CPUs still registered, we need to unlink them,
748 * or else wait_for_completion below will lock up. Clean the
749 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
750 * links afterwards.
751 */
752 if (unlikely(cpus_weight(data->cpus) > 1)) {
753 for_each_cpu_mask(j, data->cpus) {
754 if (j == cpu)
755 continue;
756 cpufreq_cpu_data[j] = NULL;
757 }
758 }
759
760 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
761
762 if (unlikely(cpus_weight(data->cpus) > 1)) {
763 for_each_cpu_mask(j, data->cpus) {
764 if (j == cpu)
765 continue;
766 dprintk("removing link for cpu %u\n", j);
Ashok Rajd434fca2005-10-30 14:59:52 -0800767 cpu_sys_dev = get_cpu_sysdev(j);
768 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 cpufreq_cpu_put(data);
770 }
771 }
772#else
773 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
774#endif
775
Arjan van de Ven83933af2006-01-14 16:01:49 +0100776 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (cpufreq_driver->target)
778 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
Arjan van de Ven83933af2006-01-14 16:01:49 +0100779 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 kobject_unregister(&data->kobj);
782
783 kobject_put(&data->kobj);
784
785 /* we need to make sure that the underlying kobj is actually
786 * not referenced anymore by anybody before we proceed with
787 * unloading.
788 */
789 dprintk("waiting for dropping of refcount\n");
790 wait_for_completion(&data->kobj_unregister);
791 dprintk("wait complete\n");
792
793 if (cpufreq_driver->exit)
794 cpufreq_driver->exit(data);
795
796 kfree(data);
797
798 cpufreq_debug_enable_ratelimit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return 0;
800}
801
802
803static void handle_update(void *data)
804{
805 unsigned int cpu = (unsigned int)(long)data;
806 dprintk("handle_update for cpu %u called\n", cpu);
807 cpufreq_update_policy(cpu);
808}
809
810/**
811 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
812 * @cpu: cpu number
813 * @old_freq: CPU frequency the kernel thinks the CPU runs at
814 * @new_freq: CPU frequency the CPU actually runs at
815 *
816 * We adjust to current frequency first, and need to clean up later. So either call
817 * to cpufreq_update_policy() or schedule handle_update()).
818 */
819static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
820{
821 struct cpufreq_freqs freqs;
822
Dave Jones78ee9982005-05-31 19:03:43 -0700823 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
825
826 freqs.cpu = cpu;
827 freqs.old = old_freq;
828 freqs.new = new_freq;
829 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
830 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
831}
832
833
834/**
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800835 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
836 * @cpu: CPU number
837 *
838 * This is the last known freq, without actually getting it from the driver.
839 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
840 */
841unsigned int cpufreq_quick_get(unsigned int cpu)
842{
843 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
844 unsigned int ret = 0;
845
846 if (policy) {
Arjan van de Ven83933af2006-01-14 16:01:49 +0100847 mutex_lock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800848 ret = policy->cur;
Arjan van de Ven83933af2006-01-14 16:01:49 +0100849 mutex_unlock(&policy->lock);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -0800850 cpufreq_cpu_put(policy);
851 }
852
853 return (ret);
854}
855EXPORT_SYMBOL(cpufreq_quick_get);
856
857
858/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 * cpufreq_get - get the current CPU frequency (in kHz)
860 * @cpu: CPU number
861 *
862 * Get the CPU current (static) CPU frequency
863 */
864unsigned int cpufreq_get(unsigned int cpu)
865{
866 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
867 unsigned int ret = 0;
868
869 if (!policy)
870 return 0;
871
872 if (!cpufreq_driver->get)
873 goto out;
874
Arjan van de Ven83933af2006-01-14 16:01:49 +0100875 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 ret = cpufreq_driver->get(cpu);
878
Dave Jones7d5e3502006-02-02 17:03:42 -0500879 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* verify no discrepancy between actual and saved value exists */
881 if (unlikely(ret != policy->cur)) {
882 cpufreq_out_of_sync(cpu, policy->cur, ret);
883 schedule_work(&policy->update);
884 }
885 }
886
Arjan van de Ven83933af2006-01-14 16:01:49 +0100887 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Dave Jones7d5e3502006-02-02 17:03:42 -0500889out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 cpufreq_cpu_put(policy);
891
892 return (ret);
893}
894EXPORT_SYMBOL(cpufreq_get);
895
896
897/**
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700898 * cpufreq_suspend - let the low level driver prepare for suspend
899 */
900
Bernard Blackhame00d9962005-07-07 17:56:42 -0700901static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700902{
903 int cpu = sysdev->id;
904 unsigned int ret = 0;
905 unsigned int cur_freq = 0;
906 struct cpufreq_policy *cpu_policy;
907
908 dprintk("resuming cpu %u\n", cpu);
909
910 if (!cpu_online(cpu))
911 return 0;
912
913 /* we may be lax here as interrupts are off. Nonetheless
914 * we need to grab the correct cpu policy, as to check
915 * whether we really run on this CPU.
916 */
917
918 cpu_policy = cpufreq_cpu_get(cpu);
919 if (!cpu_policy)
920 return -EINVAL;
921
922 /* only handle each CPU group once */
923 if (unlikely(cpu_policy->cpu != cpu)) {
924 cpufreq_cpu_put(cpu_policy);
925 return 0;
926 }
927
928 if (cpufreq_driver->suspend) {
Bernard Blackhame00d9962005-07-07 17:56:42 -0700929 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700930 if (ret) {
931 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
932 "step on CPU %u\n", cpu_policy->cpu);
933 cpufreq_cpu_put(cpu_policy);
934 return ret;
935 }
936 }
937
938
939 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
940 goto out;
941
942 if (cpufreq_driver->get)
943 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
944
945 if (!cur_freq || !cpu_policy->cur) {
946 printk(KERN_ERR "cpufreq: suspend failed to assert current "
947 "frequency is what timing core thinks it is.\n");
948 goto out;
949 }
950
951 if (unlikely(cur_freq != cpu_policy->cur)) {
952 struct cpufreq_freqs freqs;
953
954 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Dave Jones78ee9982005-05-31 19:03:43 -0700955 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700956 "cpufreq assumed %u kHz.\n",
957 cur_freq, cpu_policy->cur);
958
959 freqs.cpu = cpu;
960 freqs.old = cpu_policy->cur;
961 freqs.new = cur_freq;
962
963 notifier_call_chain(&cpufreq_transition_notifier_list,
964 CPUFREQ_SUSPENDCHANGE, &freqs);
965 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
966
967 cpu_policy->cur = cur_freq;
968 }
969
Dave Jones7d5e3502006-02-02 17:03:42 -0500970out:
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700971 cpufreq_cpu_put(cpu_policy);
972 return 0;
973}
974
975/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 * cpufreq_resume - restore proper CPU frequency handling after resume
977 *
978 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
979 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700980 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
981 * restored.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 */
983static int cpufreq_resume(struct sys_device * sysdev)
984{
985 int cpu = sysdev->id;
986 unsigned int ret = 0;
987 struct cpufreq_policy *cpu_policy;
988
989 dprintk("resuming cpu %u\n", cpu);
990
991 if (!cpu_online(cpu))
992 return 0;
993
994 /* we may be lax here as interrupts are off. Nonetheless
995 * we need to grab the correct cpu policy, as to check
996 * whether we really run on this CPU.
997 */
998
999 cpu_policy = cpufreq_cpu_get(cpu);
1000 if (!cpu_policy)
1001 return -EINVAL;
1002
1003 /* only handle each CPU group once */
1004 if (unlikely(cpu_policy->cpu != cpu)) {
1005 cpufreq_cpu_put(cpu_policy);
1006 return 0;
1007 }
1008
1009 if (cpufreq_driver->resume) {
1010 ret = cpufreq_driver->resume(cpu_policy);
1011 if (ret) {
1012 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1013 "step on CPU %u\n", cpu_policy->cpu);
1014 cpufreq_cpu_put(cpu_policy);
1015 return ret;
1016 }
1017 }
1018
1019 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1020 unsigned int cur_freq = 0;
1021
1022 if (cpufreq_driver->get)
1023 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1024
1025 if (!cur_freq || !cpu_policy->cur) {
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001026 printk(KERN_ERR "cpufreq: resume failed to assert "
1027 "current frequency is what timing core "
1028 "thinks it is.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 goto out;
1030 }
1031
1032 if (unlikely(cur_freq != cpu_policy->cur)) {
1033 struct cpufreq_freqs freqs;
1034
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001035 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
Dave Jones78ee9982005-05-31 19:03:43 -07001036 dprintk(KERN_WARNING "Warning: CPU frequency"
Benjamin Herrenschmidtac09f692005-05-02 16:25:10 +10001037 "is %u, cpufreq assumed %u kHz.\n",
1038 cur_freq, cpu_policy->cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 freqs.cpu = cpu;
1041 freqs.old = cpu_policy->cur;
1042 freqs.new = cur_freq;
1043
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001044 notifier_call_chain(&cpufreq_transition_notifier_list,
1045 CPUFREQ_RESUMECHANGE, &freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1047
1048 cpu_policy->cur = cur_freq;
1049 }
1050 }
1051
1052out:
1053 schedule_work(&cpu_policy->update);
1054 cpufreq_cpu_put(cpu_policy);
1055 return ret;
1056}
1057
1058static struct sysdev_driver cpufreq_sysdev_driver = {
1059 .add = cpufreq_add_dev,
1060 .remove = cpufreq_remove_dev,
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001061 .suspend = cpufreq_suspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 .resume = cpufreq_resume,
1063};
1064
1065
1066/*********************************************************************
1067 * NOTIFIER LISTS INTERFACE *
1068 *********************************************************************/
1069
1070/**
1071 * cpufreq_register_notifier - register a driver with cpufreq
1072 * @nb: notifier function to register
1073 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1074 *
1075 * Add a driver to one of two lists: either a list of drivers that
1076 * are notified about clock rate changes (once before and once after
1077 * the transition), or a list of drivers that are notified about
1078 * changes in cpufreq policy.
1079 *
1080 * This function may sleep, and has the same return conditions as
1081 * notifier_chain_register.
1082 */
1083int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1084{
1085 int ret;
1086
1087 down_write(&cpufreq_notifier_rwsem);
1088 switch (list) {
1089 case CPUFREQ_TRANSITION_NOTIFIER:
1090 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
1091 break;
1092 case CPUFREQ_POLICY_NOTIFIER:
1093 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
1094 break;
1095 default:
1096 ret = -EINVAL;
1097 }
1098 up_write(&cpufreq_notifier_rwsem);
1099
1100 return ret;
1101}
1102EXPORT_SYMBOL(cpufreq_register_notifier);
1103
1104
1105/**
1106 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1107 * @nb: notifier block to be unregistered
1108 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1109 *
1110 * Remove a driver from the CPU frequency notifier list.
1111 *
1112 * This function may sleep, and has the same return conditions as
1113 * notifier_chain_unregister.
1114 */
1115int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1116{
1117 int ret;
1118
1119 down_write(&cpufreq_notifier_rwsem);
1120 switch (list) {
1121 case CPUFREQ_TRANSITION_NOTIFIER:
1122 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
1123 break;
1124 case CPUFREQ_POLICY_NOTIFIER:
1125 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
1126 break;
1127 default:
1128 ret = -EINVAL;
1129 }
1130 up_write(&cpufreq_notifier_rwsem);
1131
1132 return ret;
1133}
1134EXPORT_SYMBOL(cpufreq_unregister_notifier);
1135
1136
1137/*********************************************************************
1138 * GOVERNORS *
1139 *********************************************************************/
1140
1141
1142int __cpufreq_driver_target(struct cpufreq_policy *policy,
1143 unsigned int target_freq,
1144 unsigned int relation)
1145{
1146 int retval = -EINVAL;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001147
Ashok Raja9d9baa2005-11-28 13:43:46 -08001148 lock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1150 target_freq, relation);
1151 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1152 retval = cpufreq_driver->target(policy, target_freq, relation);
Ashok Raj90d45d12005-11-08 21:34:24 -08001153
Ashok Raja9d9baa2005-11-28 13:43:46 -08001154 unlock_cpu_hotplug();
Ashok Raj90d45d12005-11-08 21:34:24 -08001155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return retval;
1157}
1158EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160int cpufreq_driver_target(struct cpufreq_policy *policy,
1161 unsigned int target_freq,
1162 unsigned int relation)
1163{
Dave Jonescc993ca2005-07-28 09:43:56 -07001164 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 policy = cpufreq_cpu_get(policy->cpu);
1167 if (!policy)
1168 return -EINVAL;
1169
Arjan van de Ven83933af2006-01-14 16:01:49 +01001170 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 ret = __cpufreq_driver_target(policy, target_freq, relation);
1173
Arjan van de Ven83933af2006-01-14 16:01:49 +01001174 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 return ret;
1178}
1179EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1180
1181
1182static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1183{
Dave Jonescc993ca2005-07-28 09:43:56 -07001184 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 if (!try_module_get(policy->governor->owner))
1187 return -EINVAL;
1188
1189 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1190 ret = policy->governor->governor(policy, event);
1191
1192 /* we keep one module reference alive for each CPU governed by this CPU */
1193 if ((event != CPUFREQ_GOV_START) || ret)
1194 module_put(policy->governor->owner);
1195 if ((event == CPUFREQ_GOV_STOP) && !ret)
1196 module_put(policy->governor->owner);
1197
1198 return ret;
1199}
1200
1201
1202int cpufreq_governor(unsigned int cpu, unsigned int event)
1203{
1204 int ret = 0;
1205 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1206
1207 if (!policy)
1208 return -EINVAL;
1209
Arjan van de Ven83933af2006-01-14 16:01:49 +01001210 mutex_lock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 ret = __cpufreq_governor(policy, event);
Arjan van de Ven83933af2006-01-14 16:01:49 +01001212 mutex_unlock(&policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
1214 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return ret;
1216}
1217EXPORT_SYMBOL_GPL(cpufreq_governor);
1218
1219
1220int cpufreq_register_governor(struct cpufreq_governor *governor)
1221{
1222 struct cpufreq_governor *t;
1223
1224 if (!governor)
1225 return -EINVAL;
1226
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001227 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1230 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001231 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return -EBUSY;
1233 }
1234 }
1235 list_add(&governor->governor_list, &cpufreq_governor_list);
1236
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001237 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return 0;
1239}
1240EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1241
1242
1243void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1244{
1245 if (!governor)
1246 return;
1247
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001248 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001250 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return;
1252}
1253EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1254
1255
1256
1257/*********************************************************************
1258 * POLICY INTERFACE *
1259 *********************************************************************/
1260
1261/**
1262 * cpufreq_get_policy - get the current cpufreq_policy
1263 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1264 *
1265 * Reads the current cpufreq policy.
1266 */
1267int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1268{
1269 struct cpufreq_policy *cpu_policy;
1270 if (!policy)
1271 return -EINVAL;
1272
1273 cpu_policy = cpufreq_cpu_get(cpu);
1274 if (!cpu_policy)
1275 return -EINVAL;
1276
Arjan van de Ven83933af2006-01-14 16:01:49 +01001277 mutex_lock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
Arjan van de Ven83933af2006-01-14 16:01:49 +01001279 mutex_unlock(&cpu_policy->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 return 0;
1283}
1284EXPORT_SYMBOL(cpufreq_get_policy);
1285
1286
1287static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1288{
1289 int ret = 0;
1290
1291 cpufreq_debug_disable_ratelimit();
1292 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1293 policy->min, policy->max);
1294
Dave Jones7d5e3502006-02-02 17:03:42 -05001295 memcpy(&policy->cpuinfo, &data->cpuinfo, sizeof(struct cpufreq_cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 /* verify the cpu speed can be set within this limit */
1298 ret = cpufreq_driver->verify(policy);
1299 if (ret)
1300 goto error_out;
1301
1302 down_read(&cpufreq_notifier_rwsem);
1303
1304 /* adjust if necessary - all reasons */
1305 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
1306 policy);
1307
1308 /* adjust if necessary - hardware incompatibility*/
1309 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
1310 policy);
1311
1312 /* verify the cpu speed can be set within this limit,
1313 which might be different to the first one */
1314 ret = cpufreq_driver->verify(policy);
1315 if (ret) {
1316 up_read(&cpufreq_notifier_rwsem);
1317 goto error_out;
1318 }
1319
1320 /* notification of the new policy */
1321 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
1322 policy);
1323
1324 up_read(&cpufreq_notifier_rwsem);
1325
Dave Jones7d5e3502006-02-02 17:03:42 -05001326 data->min = policy->min;
1327 data->max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1330
1331 if (cpufreq_driver->setpolicy) {
1332 data->policy = policy->policy;
1333 dprintk("setting range\n");
1334 ret = cpufreq_driver->setpolicy(policy);
1335 } else {
1336 if (policy->governor != data->governor) {
1337 /* save old, working values */
1338 struct cpufreq_governor *old_gov = data->governor;
1339
1340 dprintk("governor switch\n");
1341
1342 /* end old governor */
1343 if (data->governor)
1344 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1345
1346 /* start new governor */
1347 data->governor = policy->governor;
1348 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1349 /* new governor failed, so re-start old one */
1350 dprintk("starting governor %s failed\n", data->governor->name);
1351 if (old_gov) {
1352 data->governor = old_gov;
1353 __cpufreq_governor(data, CPUFREQ_GOV_START);
1354 }
1355 ret = -EINVAL;
1356 goto error_out;
1357 }
1358 /* might be a policy change, too, so fall through */
1359 }
1360 dprintk("governor: change or update limits\n");
1361 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1362 }
1363
Dave Jones7d5e3502006-02-02 17:03:42 -05001364error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 cpufreq_debug_enable_ratelimit();
1366 return ret;
1367}
1368
1369/**
1370 * cpufreq_set_policy - set a new CPUFreq policy
1371 * @policy: policy to be set.
1372 *
1373 * Sets a new CPU frequency and voltage scaling policy.
1374 */
1375int cpufreq_set_policy(struct cpufreq_policy *policy)
1376{
1377 int ret = 0;
1378 struct cpufreq_policy *data;
1379
1380 if (!policy)
1381 return -EINVAL;
1382
1383 data = cpufreq_cpu_get(policy->cpu);
1384 if (!data)
1385 return -EINVAL;
1386
1387 /* lock this CPU */
Arjan van de Ven83933af2006-01-14 16:01:49 +01001388 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 ret = __cpufreq_set_policy(data, policy);
1391 data->user_policy.min = data->min;
1392 data->user_policy.max = data->max;
1393 data->user_policy.policy = data->policy;
1394 data->user_policy.governor = data->governor;
1395
Arjan van de Ven83933af2006-01-14 16:01:49 +01001396 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 cpufreq_cpu_put(data);
1398
1399 return ret;
1400}
1401EXPORT_SYMBOL(cpufreq_set_policy);
1402
1403
1404/**
1405 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1406 * @cpu: CPU which shall be re-evaluated
1407 *
1408 * Usefull for policy notifiers which have different necessities
1409 * at different times.
1410 */
1411int cpufreq_update_policy(unsigned int cpu)
1412{
1413 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1414 struct cpufreq_policy policy;
1415 int ret = 0;
1416
1417 if (!data)
1418 return -ENODEV;
1419
Arjan van de Ven83933af2006-01-14 16:01:49 +01001420 mutex_lock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 dprintk("updating policy for CPU %u\n", cpu);
Dave Jones7d5e3502006-02-02 17:03:42 -05001423 memcpy(&policy, data, sizeof(struct cpufreq_policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 policy.min = data->user_policy.min;
1425 policy.max = data->user_policy.max;
1426 policy.policy = data->user_policy.policy;
1427 policy.governor = data->user_policy.governor;
1428
Thomas Renninger0961dd02006-01-26 18:46:33 +01001429 /* BIOS might change freq behind our back
1430 -> ask driver for current freq and notify governors about a change */
1431 if (cpufreq_driver->get) {
1432 policy.cur = cpufreq_driver->get(cpu);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01001433 if (!data->cur) {
1434 dprintk("Driver did not initialize current freq");
1435 data->cur = policy.cur;
1436 } else {
1437 if (data->cur != policy.cur)
1438 cpufreq_out_of_sync(cpu, data->cur, policy.cur);
1439 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01001440 }
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 ret = __cpufreq_set_policy(data, &policy);
1443
Arjan van de Ven83933af2006-01-14 16:01:49 +01001444 mutex_unlock(&data->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 cpufreq_cpu_put(data);
1447 return ret;
1448}
1449EXPORT_SYMBOL(cpufreq_update_policy);
1450
Ashok Rajc32b6b82005-10-30 14:59:54 -08001451static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1452 unsigned long action, void *hcpu)
1453{
1454 unsigned int cpu = (unsigned long)hcpu;
1455 struct cpufreq_policy *policy;
1456 struct sys_device *sys_dev;
1457
1458 sys_dev = get_cpu_sysdev(cpu);
1459
1460 if (sys_dev) {
1461 switch (action) {
1462 case CPU_ONLINE:
1463 cpufreq_add_dev(sys_dev);
1464 break;
1465 case CPU_DOWN_PREPARE:
1466 /*
1467 * We attempt to put this cpu in lowest frequency
1468 * possible before going down. This will permit
1469 * hardware-managed P-State to switch other related
1470 * threads to min or higher speeds if possible.
1471 */
1472 policy = cpufreq_cpu_data[cpu];
1473 if (policy) {
1474 cpufreq_driver_target(policy, policy->min,
1475 CPUFREQ_RELATION_H);
1476 }
1477 break;
1478 case CPU_DEAD:
1479 cpufreq_remove_dev(sys_dev);
1480 break;
1481 }
1482 }
1483 return NOTIFY_OK;
1484}
1485
1486static struct notifier_block cpufreq_cpu_notifier =
1487{
1488 .notifier_call = cpufreq_cpu_callback,
1489};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491/*********************************************************************
1492 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1493 *********************************************************************/
1494
1495/**
1496 * cpufreq_register_driver - register a CPU Frequency driver
1497 * @driver_data: A struct cpufreq_driver containing the values#
1498 * submitted by the CPU Frequency driver.
1499 *
1500 * Registers a CPU Frequency driver to this core code. This code
1501 * returns zero on success, -EBUSY when another driver got here first
1502 * (and isn't unregistered in the meantime).
1503 *
1504 */
1505int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1506{
1507 unsigned long flags;
1508 int ret;
1509
1510 if (!driver_data || !driver_data->verify || !driver_data->init ||
1511 ((!driver_data->setpolicy) && (!driver_data->target)))
1512 return -EINVAL;
1513
1514 dprintk("trying to register driver %s\n", driver_data->name);
1515
1516 if (driver_data->setpolicy)
1517 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1518
1519 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1520 if (cpufreq_driver) {
1521 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1522 return -EBUSY;
1523 }
1524 cpufreq_driver = driver_data;
1525 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1526
1527 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1528
1529 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1530 int i;
1531 ret = -ENODEV;
1532
1533 /* check for at least one working CPU */
1534 for (i=0; i<NR_CPUS; i++)
1535 if (cpufreq_cpu_data[i])
1536 ret = 0;
1537
1538 /* if all ->init() calls failed, unregister */
1539 if (ret) {
1540 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1541 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1542
1543 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1544 cpufreq_driver = NULL;
1545 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1546 }
1547 }
1548
1549 if (!ret) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08001550 register_cpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 dprintk("driver %s up and running\n", driver_data->name);
1552 cpufreq_debug_enable_ratelimit();
1553 }
1554
1555 return (ret);
1556}
1557EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1558
1559
1560/**
1561 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1562 *
1563 * Unregister the current CPUFreq driver. Only call this if you have
1564 * the right to do so, i.e. if you have succeeded in initialising before!
1565 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1566 * currently not initialised.
1567 */
1568int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1569{
1570 unsigned long flags;
1571
1572 cpufreq_debug_disable_ratelimit();
1573
1574 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1575 cpufreq_debug_enable_ratelimit();
1576 return -EINVAL;
1577 }
1578
1579 dprintk("unregistering driver %s\n", driver->name);
1580
1581 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
Ashok Rajc32b6b82005-10-30 14:59:54 -08001582 unregister_cpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
1584 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1585 cpufreq_driver = NULL;
1586 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1587
1588 return 0;
1589}
1590EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);