[CPUFREQ] p4-clockmod: Workaround for CPU's with N60 errata
[linux-2.6-block.git] / drivers / cpufreq / cpufreq.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6 *
c32b6b8e
AR
7 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
8 * Added handling for CPU hotplug
9 *
1da177e4
LT
10 * 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>
3fc54d37 29#include <linux/mutex.h>
1da177e4
LT
30
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 */
38static struct cpufreq_driver *cpufreq_driver;
39static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
40static DEFINE_SPINLOCK(cpufreq_driver_lock);
41
1da177e4
LT
42/* internal prototypes */
43static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
44static void handle_update(void *data);
1da177e4
LT
45
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 */
53static struct notifier_block *cpufreq_policy_notifier_list;
54static struct notifier_block *cpufreq_transition_notifier_list;
55static DECLARE_RWSEM (cpufreq_notifier_rwsem);
56
57
58static LIST_HEAD(cpufreq_governor_list);
3fc54d37 59static DEFINE_MUTEX (cpufreq_governor_mutex);
1da177e4
LT
60
61struct cpufreq_policy * cpufreq_cpu_get(unsigned int cpu)
62{
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
88
89 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
90
91 return data;
92
93 err_out_put_module:
94 module_put(cpufreq_driver->owner);
95 err_out_unlock:
96 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
97 err_out:
98 return NULL;
99}
100EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
101
102void cpufreq_cpu_put(struct cpufreq_policy *data)
103{
104 kobject_put(&data->kobj);
105 module_put(cpufreq_driver->owner);
106}
107EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
108
109
110/*********************************************************************
111 * UNIFIED DEBUG HELPERS *
112 *********************************************************************/
113#ifdef CONFIG_CPU_FREQ_DEBUG
114
115/* what part(s) of the CPUfreq subsystem are debugged? */
116static unsigned int debug;
117
118/* is the debug output ratelimit'ed using printk_ratelimit? User can
119 * set or modify this value.
120 */
121static unsigned int debug_ratelimit = 1;
122
123/* is the printk_ratelimit'ing enabled? It's enabled after a successful
124 * loading of a cpufreq driver, temporarily disabled when a new policy
125 * is set, and disabled upon cpufreq driver removal
126 */
127static unsigned int disable_ratelimit = 1;
128static DEFINE_SPINLOCK(disable_ratelimit_lock);
129
858119e1 130static void cpufreq_debug_enable_ratelimit(void)
1da177e4
LT
131{
132 unsigned long flags;
133
134 spin_lock_irqsave(&disable_ratelimit_lock, flags);
135 if (disable_ratelimit)
136 disable_ratelimit--;
137 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
138}
139
858119e1 140static void cpufreq_debug_disable_ratelimit(void)
1da177e4
LT
141{
142 unsigned long flags;
143
144 spin_lock_irqsave(&disable_ratelimit_lock, flags);
145 disable_ratelimit++;
146 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
147}
148
149void cpufreq_debug_printk(unsigned int type, const char *prefix, const char *fmt, ...)
150{
151 char s[256];
152 va_list args;
153 unsigned int len;
154 unsigned long flags;
155
156 WARN_ON(!prefix);
157 if (type & debug) {
158 spin_lock_irqsave(&disable_ratelimit_lock, flags);
159 if (!disable_ratelimit && debug_ratelimit && !printk_ratelimit()) {
160 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
161 return;
162 }
163 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
164
165 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
166
167 va_start(args, fmt);
168 len += vsnprintf(&s[len], (256 - len), fmt, args);
169 va_end(args);
170
171 printk(s);
172
173 WARN_ON(len < 5);
174 }
175}
176EXPORT_SYMBOL(cpufreq_debug_printk);
177
178
179module_param(debug, uint, 0644);
180MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
181
182module_param(debug_ratelimit, uint, 0644);
183MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
184
185#else /* !CONFIG_CPU_FREQ_DEBUG */
186
187static inline void cpufreq_debug_enable_ratelimit(void) { return; }
188static inline void cpufreq_debug_disable_ratelimit(void) { return; }
189
190#endif /* CONFIG_CPU_FREQ_DEBUG */
191
192
193/*********************************************************************
194 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
195 *********************************************************************/
196
197/**
198 * adjust_jiffies - adjust the system "loops_per_jiffy"
199 *
200 * This function alters the system "loops_per_jiffy" for the clock
201 * speed change. Note that loops_per_jiffy cannot be updated on SMP
202 * systems as each CPU might be scaled differently. So, use the arch
203 * per-CPU loops_per_jiffy value wherever possible.
204 */
205#ifndef CONFIG_SMP
206static unsigned long l_p_j_ref;
207static unsigned int l_p_j_ref_freq;
208
858119e1 209static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
1da177e4
LT
210{
211 if (ci->flags & CPUFREQ_CONST_LOOPS)
212 return;
213
214 if (!l_p_j_ref_freq) {
215 l_p_j_ref = loops_per_jiffy;
216 l_p_j_ref_freq = ci->old;
217 dprintk("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
218 }
219 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) ||
220 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
42d4dc3f 221 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
1da177e4
LT
222 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, ci->new);
223 dprintk("scaling loops_per_jiffy to %lu for frequency %u kHz\n", loops_per_jiffy, ci->new);
224 }
225}
226#else
227static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) { return; }
228#endif
229
230
231/**
232 * cpufreq_notify_transition - call notifier chain and adjust_jiffies on frequency transition
233 *
234 * This function calls the transition notifiers and the "adjust_jiffies" function. It is called
235 * twice on all CPU frequency changes that have external effects.
236 */
237void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
238{
239 BUG_ON(irqs_disabled());
240
241 freqs->flags = cpufreq_driver->flags;
242 dprintk("notification %u of frequency transition to %u kHz\n", state, freqs->new);
243
244 down_read(&cpufreq_notifier_rwsem);
245 switch (state) {
246 case CPUFREQ_PRECHANGE:
247 /* detect if the driver reported a value as "old frequency" which
248 * is not equal to what the cpufreq core thinks is "old frequency".
249 */
250 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
251 if ((likely(cpufreq_cpu_data[freqs->cpu])) &&
252 (likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu)) &&
253 (likely(cpufreq_cpu_data[freqs->cpu]->cur)) &&
254 (unlikely(freqs->old != cpufreq_cpu_data[freqs->cpu]->cur)))
255 {
78ee998f 256 dprintk(KERN_WARNING "Warning: CPU frequency is %u, "
1da177e4
LT
257 "cpufreq assumed %u kHz.\n", freqs->old, cpufreq_cpu_data[freqs->cpu]->cur);
258 freqs->old = cpufreq_cpu_data[freqs->cpu]->cur;
259 }
260 }
261 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_PRECHANGE, freqs);
262 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
263 break;
264 case CPUFREQ_POSTCHANGE:
265 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
266 notifier_call_chain(&cpufreq_transition_notifier_list, CPUFREQ_POSTCHANGE, freqs);
267 if ((likely(cpufreq_cpu_data[freqs->cpu])) &&
268 (likely(cpufreq_cpu_data[freqs->cpu]->cpu == freqs->cpu)))
269 cpufreq_cpu_data[freqs->cpu]->cur = freqs->new;
270 break;
271 }
272 up_read(&cpufreq_notifier_rwsem);
273}
274EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
275
276
277
278/*********************************************************************
279 * SYSFS INTERFACE *
280 *********************************************************************/
281
282/**
283 * cpufreq_parse_governor - parse a governor string
284 */
285static int cpufreq_parse_governor (char *str_governor, unsigned int *policy,
286 struct cpufreq_governor **governor)
287{
288 if (!cpufreq_driver)
289 return -EINVAL;
290 if (cpufreq_driver->setpolicy) {
291 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
292 *policy = CPUFREQ_POLICY_PERFORMANCE;
293 return 0;
294 } else if (!strnicmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
295 *policy = CPUFREQ_POLICY_POWERSAVE;
296 return 0;
297 }
298 return -EINVAL;
299 } else {
300 struct cpufreq_governor *t;
3fc54d37 301 mutex_lock(&cpufreq_governor_mutex);
1da177e4
LT
302 if (!cpufreq_driver || !cpufreq_driver->target)
303 goto out;
304 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
305 if (!strnicmp(str_governor,t->name,CPUFREQ_NAME_LEN)) {
306 *governor = t;
3fc54d37 307 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
308 return 0;
309 }
310 }
311 out:
3fc54d37 312 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
313 }
314 return -EINVAL;
315}
316EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
317
318
319/* drivers/base/cpu.c */
320extern struct sysdev_class cpu_sysdev_class;
321
322
323/**
324 * cpufreq_per_cpu_attr_read() / show_##file_name() - print out cpufreq information
325 *
326 * Write out information from cpufreq_driver->policy[cpu]; object must be
327 * "unsigned int".
328 */
329
330#define show_one(file_name, object) \
331static ssize_t show_##file_name \
332(struct cpufreq_policy * policy, char *buf) \
333{ \
334 return sprintf (buf, "%u\n", policy->object); \
335}
336
337show_one(cpuinfo_min_freq, cpuinfo.min_freq);
338show_one(cpuinfo_max_freq, cpuinfo.max_freq);
339show_one(scaling_min_freq, min);
340show_one(scaling_max_freq, max);
341show_one(scaling_cur_freq, cur);
342
343/**
344 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
345 */
346#define store_one(file_name, object) \
347static ssize_t store_##file_name \
348(struct cpufreq_policy * policy, const char *buf, size_t count) \
349{ \
350 unsigned int ret = -EINVAL; \
351 struct cpufreq_policy new_policy; \
352 \
353 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
354 if (ret) \
355 return -EINVAL; \
356 \
357 ret = sscanf (buf, "%u", &new_policy.object); \
358 if (ret != 1) \
359 return -EINVAL; \
360 \
361 ret = cpufreq_set_policy(&new_policy); \
362 \
363 return ret ? ret : count; \
364}
365
366store_one(scaling_min_freq,min);
367store_one(scaling_max_freq,max);
368
369/**
370 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
371 */
372static ssize_t show_cpuinfo_cur_freq (struct cpufreq_policy * policy, char *buf)
373{
374 unsigned int cur_freq = cpufreq_get(policy->cpu);
375 if (!cur_freq)
376 return sprintf(buf, "<unknown>");
377 return sprintf(buf, "%u\n", cur_freq);
378}
379
380
381/**
382 * show_scaling_governor - show the current policy for the specified CPU
383 */
384static ssize_t show_scaling_governor (struct cpufreq_policy * policy, char *buf)
385{
386 if(policy->policy == CPUFREQ_POLICY_POWERSAVE)
387 return sprintf(buf, "powersave\n");
388 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
389 return sprintf(buf, "performance\n");
390 else if (policy->governor)
391 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", policy->governor->name);
392 return -EINVAL;
393}
394
395
396/**
397 * store_scaling_governor - store policy for the specified CPU
398 */
399static ssize_t store_scaling_governor (struct cpufreq_policy * policy,
400 const char *buf, size_t count)
401{
402 unsigned int ret = -EINVAL;
403 char str_governor[16];
404 struct cpufreq_policy new_policy;
405
406 ret = cpufreq_get_policy(&new_policy, policy->cpu);
407 if (ret)
408 return ret;
409
410 ret = sscanf (buf, "%15s", str_governor);
411 if (ret != 1)
412 return -EINVAL;
413
414 if (cpufreq_parse_governor(str_governor, &new_policy.policy, &new_policy.governor))
415 return -EINVAL;
416
417 ret = cpufreq_set_policy(&new_policy);
418
419 return ret ? ret : count;
420}
421
422/**
423 * show_scaling_driver - show the cpufreq driver currently loaded
424 */
425static ssize_t show_scaling_driver (struct cpufreq_policy * policy, char *buf)
426{
427 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
428}
429
430/**
431 * show_scaling_available_governors - show the available CPUfreq governors
432 */
433static ssize_t show_scaling_available_governors (struct cpufreq_policy * policy,
434 char *buf)
435{
436 ssize_t i = 0;
437 struct cpufreq_governor *t;
438
439 if (!cpufreq_driver->target) {
440 i += sprintf(buf, "performance powersave");
441 goto out;
442 }
443
444 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
445 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) - (CPUFREQ_NAME_LEN + 2)))
446 goto out;
447 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
448 }
449 out:
450 i += sprintf(&buf[i], "\n");
451 return i;
452}
453/**
454 * show_affected_cpus - show the CPUs affected by each transition
455 */
456static ssize_t show_affected_cpus (struct cpufreq_policy * policy, char *buf)
457{
458 ssize_t i = 0;
459 unsigned int cpu;
460
461 for_each_cpu_mask(cpu, policy->cpus) {
462 if (i)
463 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
464 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
465 if (i >= (PAGE_SIZE - 5))
466 break;
467 }
468 i += sprintf(&buf[i], "\n");
469 return i;
470}
471
472
473#define define_one_ro(_name) \
474static struct freq_attr _name = \
475__ATTR(_name, 0444, show_##_name, NULL)
476
477#define define_one_ro0400(_name) \
478static struct freq_attr _name = \
479__ATTR(_name, 0400, show_##_name, NULL)
480
481#define define_one_rw(_name) \
482static struct freq_attr _name = \
483__ATTR(_name, 0644, show_##_name, store_##_name)
484
485define_one_ro0400(cpuinfo_cur_freq);
486define_one_ro(cpuinfo_min_freq);
487define_one_ro(cpuinfo_max_freq);
488define_one_ro(scaling_available_governors);
489define_one_ro(scaling_driver);
490define_one_ro(scaling_cur_freq);
491define_one_ro(affected_cpus);
492define_one_rw(scaling_min_freq);
493define_one_rw(scaling_max_freq);
494define_one_rw(scaling_governor);
495
496static struct attribute * default_attrs[] = {
497 &cpuinfo_min_freq.attr,
498 &cpuinfo_max_freq.attr,
499 &scaling_min_freq.attr,
500 &scaling_max_freq.attr,
501 &affected_cpus.attr,
502 &scaling_governor.attr,
503 &scaling_driver.attr,
504 &scaling_available_governors.attr,
505 NULL
506};
507
508#define to_policy(k) container_of(k,struct cpufreq_policy,kobj)
509#define to_attr(a) container_of(a,struct freq_attr,attr)
510
511static ssize_t show(struct kobject * kobj, struct attribute * attr ,char * buf)
512{
513 struct cpufreq_policy * policy = to_policy(kobj);
514 struct freq_attr * fattr = to_attr(attr);
515 ssize_t ret;
516 policy = cpufreq_cpu_get(policy->cpu);
517 if (!policy)
518 return -EINVAL;
70f2817a 519 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
1da177e4
LT
520 cpufreq_cpu_put(policy);
521 return ret;
522}
523
524static ssize_t store(struct kobject * kobj, struct attribute * attr,
525 const char * buf, size_t count)
526{
527 struct cpufreq_policy * policy = to_policy(kobj);
528 struct freq_attr * fattr = to_attr(attr);
529 ssize_t ret;
530 policy = cpufreq_cpu_get(policy->cpu);
531 if (!policy)
532 return -EINVAL;
70f2817a 533 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
1da177e4
LT
534 cpufreq_cpu_put(policy);
535 return ret;
536}
537
538static void cpufreq_sysfs_release(struct kobject * kobj)
539{
540 struct cpufreq_policy * policy = to_policy(kobj);
541 dprintk("last reference is dropped\n");
542 complete(&policy->kobj_unregister);
543}
544
545static struct sysfs_ops sysfs_ops = {
546 .show = show,
547 .store = store,
548};
549
550static struct kobj_type ktype_cpufreq = {
551 .sysfs_ops = &sysfs_ops,
552 .default_attrs = default_attrs,
553 .release = cpufreq_sysfs_release,
554};
555
556
557/**
558 * cpufreq_add_dev - add a CPU device
559 *
560 * Adds the cpufreq interface for a CPU device.
561 */
562static int cpufreq_add_dev (struct sys_device * sys_dev)
563{
564 unsigned int cpu = sys_dev->id;
565 int ret = 0;
566 struct cpufreq_policy new_policy;
567 struct cpufreq_policy *policy;
568 struct freq_attr **drv_attr;
569 unsigned long flags;
570 unsigned int j;
571
c32b6b8e
AR
572 if (cpu_is_offline(cpu))
573 return 0;
574
1da177e4
LT
575 cpufreq_debug_disable_ratelimit();
576 dprintk("adding CPU %u\n", cpu);
577
578#ifdef CONFIG_SMP
579 /* check whether a different CPU already registered this
580 * CPU because it is in the same boat. */
581 policy = cpufreq_cpu_get(cpu);
582 if (unlikely(policy)) {
1da177e4
LT
583 dprintk("CPU already managed, adding link\n");
584 sysfs_create_link(&sys_dev->kobj, &policy->kobj, "cpufreq");
585 cpufreq_debug_enable_ratelimit();
586 return 0;
587 }
588#endif
589
590 if (!try_module_get(cpufreq_driver->owner)) {
591 ret = -EINVAL;
592 goto module_out;
593 }
594
e98df50c 595 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
1da177e4
LT
596 if (!policy) {
597 ret = -ENOMEM;
598 goto nomem_out;
599 }
1da177e4
LT
600
601 policy->cpu = cpu;
602 policy->cpus = cpumask_of_cpu(cpu);
603
83933af4
AV
604 mutex_init(&policy->lock);
605 mutex_lock(&policy->lock);
1da177e4
LT
606 init_completion(&policy->kobj_unregister);
607 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
608
609 /* call driver. From then on the cpufreq must be able
610 * to accept all calls to ->verify and ->setpolicy for this CPU
611 */
612 ret = cpufreq_driver->init(policy);
613 if (ret) {
614 dprintk("initialization failed\n");
f3876c1b 615 mutex_unlock(&policy->lock);
1da177e4
LT
616 goto err_out;
617 }
618
619 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
620
621 /* prepare interface data */
622 policy->kobj.parent = &sys_dev->kobj;
623 policy->kobj.ktype = &ktype_cpufreq;
624 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
625
626 ret = kobject_register(&policy->kobj);
f3876c1b
AM
627 if (ret) {
628 mutex_unlock(&policy->lock);
8085e1f1 629 goto err_out_driver_exit;
f3876c1b 630 }
1da177e4
LT
631 /* set up files for this cpu device */
632 drv_attr = cpufreq_driver->attr;
633 while ((drv_attr) && (*drv_attr)) {
634 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
635 drv_attr++;
636 }
637 if (cpufreq_driver->get)
638 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
639 if (cpufreq_driver->target)
640 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
641
642 spin_lock_irqsave(&cpufreq_driver_lock, flags);
643 for_each_cpu_mask(j, policy->cpus)
644 cpufreq_cpu_data[j] = policy;
645 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
646 policy->governor = NULL; /* to assure that the starting sequence is
647 * run in cpufreq_set_policy */
83933af4 648 mutex_unlock(&policy->lock);
1da177e4
LT
649
650 /* set default policy */
651
652 ret = cpufreq_set_policy(&new_policy);
653 if (ret) {
654 dprintk("setting policy failed\n");
655 goto err_out_unregister;
656 }
657
658 module_put(cpufreq_driver->owner);
1da177e4
LT
659 dprintk("initialization complete\n");
660 cpufreq_debug_enable_ratelimit();
661
662 return 0;
663
664
665err_out_unregister:
666 spin_lock_irqsave(&cpufreq_driver_lock, flags);
667 for_each_cpu_mask(j, policy->cpus)
668 cpufreq_cpu_data[j] = NULL;
669 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
670
671 kobject_unregister(&policy->kobj);
672 wait_for_completion(&policy->kobj_unregister);
673
8085e1f1
VP
674err_out_driver_exit:
675 if (cpufreq_driver->exit)
676 cpufreq_driver->exit(policy);
677
1da177e4
LT
678err_out:
679 kfree(policy);
680
681nomem_out:
682 module_put(cpufreq_driver->owner);
c32b6b8e 683module_out:
1da177e4
LT
684 cpufreq_debug_enable_ratelimit();
685 return ret;
686}
687
688
689/**
690 * cpufreq_remove_dev - remove a CPU device
691 *
692 * Removes the cpufreq interface for a CPU device.
693 */
694static int cpufreq_remove_dev (struct sys_device * sys_dev)
695{
696 unsigned int cpu = sys_dev->id;
697 unsigned long flags;
698 struct cpufreq_policy *data;
699#ifdef CONFIG_SMP
e738cf6d 700 struct sys_device *cpu_sys_dev;
1da177e4
LT
701 unsigned int j;
702#endif
703
704 cpufreq_debug_disable_ratelimit();
705 dprintk("unregistering CPU %u\n", cpu);
706
707 spin_lock_irqsave(&cpufreq_driver_lock, flags);
708 data = cpufreq_cpu_data[cpu];
709
710 if (!data) {
711 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4
LT
712 cpufreq_debug_enable_ratelimit();
713 return -EINVAL;
714 }
715 cpufreq_cpu_data[cpu] = NULL;
716
717
718#ifdef CONFIG_SMP
719 /* if this isn't the CPU which is the parent of the kobj, we
720 * only need to unlink, put and exit
721 */
722 if (unlikely(cpu != data->cpu)) {
723 dprintk("removing link\n");
724 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
725 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
1da177e4
LT
726 cpufreq_cpu_put(data);
727 cpufreq_debug_enable_ratelimit();
728 return 0;
729 }
730#endif
731
1da177e4
LT
732
733 if (!kobject_get(&data->kobj)) {
734 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
735 cpufreq_debug_enable_ratelimit();
736 return -EFAULT;
737 }
738
739#ifdef CONFIG_SMP
740 /* if we have other CPUs still registered, we need to unlink them,
741 * or else wait_for_completion below will lock up. Clean the
742 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
743 * links afterwards.
744 */
745 if (unlikely(cpus_weight(data->cpus) > 1)) {
746 for_each_cpu_mask(j, data->cpus) {
747 if (j == cpu)
748 continue;
749 cpufreq_cpu_data[j] = NULL;
750 }
751 }
752
753 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
754
755 if (unlikely(cpus_weight(data->cpus) > 1)) {
756 for_each_cpu_mask(j, data->cpus) {
757 if (j == cpu)
758 continue;
759 dprintk("removing link for cpu %u\n", j);
d434fca7
AR
760 cpu_sys_dev = get_cpu_sysdev(j);
761 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
1da177e4
LT
762 cpufreq_cpu_put(data);
763 }
764 }
765#else
766 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
767#endif
768
83933af4 769 mutex_lock(&data->lock);
1da177e4
LT
770 if (cpufreq_driver->target)
771 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
83933af4 772 mutex_unlock(&data->lock);
1da177e4
LT
773
774 kobject_unregister(&data->kobj);
775
776 kobject_put(&data->kobj);
777
778 /* we need to make sure that the underlying kobj is actually
779 * not referenced anymore by anybody before we proceed with
780 * unloading.
781 */
782 dprintk("waiting for dropping of refcount\n");
783 wait_for_completion(&data->kobj_unregister);
784 dprintk("wait complete\n");
785
786 if (cpufreq_driver->exit)
787 cpufreq_driver->exit(data);
788
789 kfree(data);
790
791 cpufreq_debug_enable_ratelimit();
792
793 return 0;
794}
795
796
797static void handle_update(void *data)
798{
799 unsigned int cpu = (unsigned int)(long)data;
800 dprintk("handle_update for cpu %u called\n", cpu);
801 cpufreq_update_policy(cpu);
802}
803
804/**
805 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
806 * @cpu: cpu number
807 * @old_freq: CPU frequency the kernel thinks the CPU runs at
808 * @new_freq: CPU frequency the CPU actually runs at
809 *
810 * We adjust to current frequency first, and need to clean up later. So either call
811 * to cpufreq_update_policy() or schedule handle_update()).
812 */
813static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
814{
815 struct cpufreq_freqs freqs;
816
78ee998f 817 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
1da177e4
LT
818 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
819
820 freqs.cpu = cpu;
821 freqs.old = old_freq;
822 freqs.new = new_freq;
823 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
824 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
825}
826
827
95235ca2
VP
828/**
829 * cpufreq_quick_get - get the CPU frequency (in kHz) frpm policy->cur
830 * @cpu: CPU number
831 *
832 * This is the last known freq, without actually getting it from the driver.
833 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
834 */
835unsigned int cpufreq_quick_get(unsigned int cpu)
836{
837 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
838 unsigned int ret = 0;
839
840 if (policy) {
83933af4 841 mutex_lock(&policy->lock);
95235ca2 842 ret = policy->cur;
83933af4 843 mutex_unlock(&policy->lock);
95235ca2
VP
844 cpufreq_cpu_put(policy);
845 }
846
847 return (ret);
848}
849EXPORT_SYMBOL(cpufreq_quick_get);
850
851
1da177e4
LT
852/**
853 * cpufreq_get - get the current CPU frequency (in kHz)
854 * @cpu: CPU number
855 *
856 * Get the CPU current (static) CPU frequency
857 */
858unsigned int cpufreq_get(unsigned int cpu)
859{
860 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
861 unsigned int ret = 0;
862
863 if (!policy)
864 return 0;
865
866 if (!cpufreq_driver->get)
867 goto out;
868
83933af4 869 mutex_lock(&policy->lock);
1da177e4
LT
870
871 ret = cpufreq_driver->get(cpu);
872
873 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS))
874 {
875 /* verify no discrepancy between actual and saved value exists */
876 if (unlikely(ret != policy->cur)) {
877 cpufreq_out_of_sync(cpu, policy->cur, ret);
878 schedule_work(&policy->update);
879 }
880 }
881
83933af4 882 mutex_unlock(&policy->lock);
1da177e4
LT
883
884 out:
885 cpufreq_cpu_put(policy);
886
887 return (ret);
888}
889EXPORT_SYMBOL(cpufreq_get);
890
891
42d4dc3f
BH
892/**
893 * cpufreq_suspend - let the low level driver prepare for suspend
894 */
895
e00d9967 896static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
42d4dc3f
BH
897{
898 int cpu = sysdev->id;
899 unsigned int ret = 0;
900 unsigned int cur_freq = 0;
901 struct cpufreq_policy *cpu_policy;
902
903 dprintk("resuming cpu %u\n", cpu);
904
905 if (!cpu_online(cpu))
906 return 0;
907
908 /* we may be lax here as interrupts are off. Nonetheless
909 * we need to grab the correct cpu policy, as to check
910 * whether we really run on this CPU.
911 */
912
913 cpu_policy = cpufreq_cpu_get(cpu);
914 if (!cpu_policy)
915 return -EINVAL;
916
917 /* only handle each CPU group once */
918 if (unlikely(cpu_policy->cpu != cpu)) {
919 cpufreq_cpu_put(cpu_policy);
920 return 0;
921 }
922
923 if (cpufreq_driver->suspend) {
e00d9967 924 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
42d4dc3f
BH
925 if (ret) {
926 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
927 "step on CPU %u\n", cpu_policy->cpu);
928 cpufreq_cpu_put(cpu_policy);
929 return ret;
930 }
931 }
932
933
934 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
935 goto out;
936
937 if (cpufreq_driver->get)
938 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
939
940 if (!cur_freq || !cpu_policy->cur) {
941 printk(KERN_ERR "cpufreq: suspend failed to assert current "
942 "frequency is what timing core thinks it is.\n");
943 goto out;
944 }
945
946 if (unlikely(cur_freq != cpu_policy->cur)) {
947 struct cpufreq_freqs freqs;
948
949 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
78ee998f 950 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
42d4dc3f
BH
951 "cpufreq assumed %u kHz.\n",
952 cur_freq, cpu_policy->cur);
953
954 freqs.cpu = cpu;
955 freqs.old = cpu_policy->cur;
956 freqs.new = cur_freq;
957
958 notifier_call_chain(&cpufreq_transition_notifier_list,
959 CPUFREQ_SUSPENDCHANGE, &freqs);
960 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
961
962 cpu_policy->cur = cur_freq;
963 }
964
965 out:
966 cpufreq_cpu_put(cpu_policy);
967 return 0;
968}
969
1da177e4
LT
970/**
971 * cpufreq_resume - restore proper CPU frequency handling after resume
972 *
973 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
974 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
42d4dc3f
BH
975 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
976 * restored.
1da177e4
LT
977 */
978static int cpufreq_resume(struct sys_device * sysdev)
979{
980 int cpu = sysdev->id;
981 unsigned int ret = 0;
982 struct cpufreq_policy *cpu_policy;
983
984 dprintk("resuming cpu %u\n", cpu);
985
986 if (!cpu_online(cpu))
987 return 0;
988
989 /* we may be lax here as interrupts are off. Nonetheless
990 * we need to grab the correct cpu policy, as to check
991 * whether we really run on this CPU.
992 */
993
994 cpu_policy = cpufreq_cpu_get(cpu);
995 if (!cpu_policy)
996 return -EINVAL;
997
998 /* only handle each CPU group once */
999 if (unlikely(cpu_policy->cpu != cpu)) {
1000 cpufreq_cpu_put(cpu_policy);
1001 return 0;
1002 }
1003
1004 if (cpufreq_driver->resume) {
1005 ret = cpufreq_driver->resume(cpu_policy);
1006 if (ret) {
1007 printk(KERN_ERR "cpufreq: resume failed in ->resume "
1008 "step on CPU %u\n", cpu_policy->cpu);
1009 cpufreq_cpu_put(cpu_policy);
1010 return ret;
1011 }
1012 }
1013
1014 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1015 unsigned int cur_freq = 0;
1016
1017 if (cpufreq_driver->get)
1018 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
1019
1020 if (!cur_freq || !cpu_policy->cur) {
42d4dc3f
BH
1021 printk(KERN_ERR "cpufreq: resume failed to assert "
1022 "current frequency is what timing core "
1023 "thinks it is.\n");
1da177e4
LT
1024 goto out;
1025 }
1026
1027 if (unlikely(cur_freq != cpu_policy->cur)) {
1028 struct cpufreq_freqs freqs;
1029
ac09f698 1030 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
78ee998f 1031 dprintk(KERN_WARNING "Warning: CPU frequency"
ac09f698
BH
1032 "is %u, cpufreq assumed %u kHz.\n",
1033 cur_freq, cpu_policy->cur);
1da177e4
LT
1034
1035 freqs.cpu = cpu;
1036 freqs.old = cpu_policy->cur;
1037 freqs.new = cur_freq;
1038
42d4dc3f
BH
1039 notifier_call_chain(&cpufreq_transition_notifier_list,
1040 CPUFREQ_RESUMECHANGE, &freqs);
1da177e4
LT
1041 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1042
1043 cpu_policy->cur = cur_freq;
1044 }
1045 }
1046
1047out:
1048 schedule_work(&cpu_policy->update);
1049 cpufreq_cpu_put(cpu_policy);
1050 return ret;
1051}
1052
1053static struct sysdev_driver cpufreq_sysdev_driver = {
1054 .add = cpufreq_add_dev,
1055 .remove = cpufreq_remove_dev,
42d4dc3f 1056 .suspend = cpufreq_suspend,
1da177e4
LT
1057 .resume = cpufreq_resume,
1058};
1059
1060
1061/*********************************************************************
1062 * NOTIFIER LISTS INTERFACE *
1063 *********************************************************************/
1064
1065/**
1066 * cpufreq_register_notifier - register a driver with cpufreq
1067 * @nb: notifier function to register
1068 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1069 *
1070 * Add a driver to one of two lists: either a list of drivers that
1071 * are notified about clock rate changes (once before and once after
1072 * the transition), or a list of drivers that are notified about
1073 * changes in cpufreq policy.
1074 *
1075 * This function may sleep, and has the same return conditions as
1076 * notifier_chain_register.
1077 */
1078int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1079{
1080 int ret;
1081
1082 down_write(&cpufreq_notifier_rwsem);
1083 switch (list) {
1084 case CPUFREQ_TRANSITION_NOTIFIER:
1085 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
1086 break;
1087 case CPUFREQ_POLICY_NOTIFIER:
1088 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
1089 break;
1090 default:
1091 ret = -EINVAL;
1092 }
1093 up_write(&cpufreq_notifier_rwsem);
1094
1095 return ret;
1096}
1097EXPORT_SYMBOL(cpufreq_register_notifier);
1098
1099
1100/**
1101 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1102 * @nb: notifier block to be unregistered
1103 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1104 *
1105 * Remove a driver from the CPU frequency notifier list.
1106 *
1107 * This function may sleep, and has the same return conditions as
1108 * notifier_chain_unregister.
1109 */
1110int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1111{
1112 int ret;
1113
1114 down_write(&cpufreq_notifier_rwsem);
1115 switch (list) {
1116 case CPUFREQ_TRANSITION_NOTIFIER:
1117 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
1118 break;
1119 case CPUFREQ_POLICY_NOTIFIER:
1120 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
1121 break;
1122 default:
1123 ret = -EINVAL;
1124 }
1125 up_write(&cpufreq_notifier_rwsem);
1126
1127 return ret;
1128}
1129EXPORT_SYMBOL(cpufreq_unregister_notifier);
1130
1131
1132/*********************************************************************
1133 * GOVERNORS *
1134 *********************************************************************/
1135
1136
1137int __cpufreq_driver_target(struct cpufreq_policy *policy,
1138 unsigned int target_freq,
1139 unsigned int relation)
1140{
1141 int retval = -EINVAL;
c32b6b8e 1142
a9d9baa1 1143 lock_cpu_hotplug();
1da177e4
LT
1144 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1145 target_freq, relation);
1146 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1147 retval = cpufreq_driver->target(policy, target_freq, relation);
90d45d17 1148
a9d9baa1 1149 unlock_cpu_hotplug();
90d45d17 1150
1da177e4
LT
1151 return retval;
1152}
1153EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1154
1da177e4
LT
1155int cpufreq_driver_target(struct cpufreq_policy *policy,
1156 unsigned int target_freq,
1157 unsigned int relation)
1158{
cc993cab 1159 int ret;
1da177e4
LT
1160
1161 policy = cpufreq_cpu_get(policy->cpu);
1162 if (!policy)
1163 return -EINVAL;
1164
83933af4 1165 mutex_lock(&policy->lock);
1da177e4
LT
1166
1167 ret = __cpufreq_driver_target(policy, target_freq, relation);
1168
83933af4 1169 mutex_unlock(&policy->lock);
1da177e4
LT
1170
1171 cpufreq_cpu_put(policy);
1172
1173 return ret;
1174}
1175EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1176
1177
1178static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1179{
cc993cab 1180 int ret;
1da177e4
LT
1181
1182 if (!try_module_get(policy->governor->owner))
1183 return -EINVAL;
1184
1185 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1186 ret = policy->governor->governor(policy, event);
1187
1188 /* we keep one module reference alive for each CPU governed by this CPU */
1189 if ((event != CPUFREQ_GOV_START) || ret)
1190 module_put(policy->governor->owner);
1191 if ((event == CPUFREQ_GOV_STOP) && !ret)
1192 module_put(policy->governor->owner);
1193
1194 return ret;
1195}
1196
1197
1198int cpufreq_governor(unsigned int cpu, unsigned int event)
1199{
1200 int ret = 0;
1201 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1202
1203 if (!policy)
1204 return -EINVAL;
1205
83933af4 1206 mutex_lock(&policy->lock);
1da177e4 1207 ret = __cpufreq_governor(policy, event);
83933af4 1208 mutex_unlock(&policy->lock);
1da177e4
LT
1209
1210 cpufreq_cpu_put(policy);
1211
1212 return ret;
1213}
1214EXPORT_SYMBOL_GPL(cpufreq_governor);
1215
1216
1217int cpufreq_register_governor(struct cpufreq_governor *governor)
1218{
1219 struct cpufreq_governor *t;
1220
1221 if (!governor)
1222 return -EINVAL;
1223
3fc54d37 1224 mutex_lock(&cpufreq_governor_mutex);
1da177e4
LT
1225
1226 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1227 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
3fc54d37 1228 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
1229 return -EBUSY;
1230 }
1231 }
1232 list_add(&governor->governor_list, &cpufreq_governor_list);
1233
3fc54d37 1234 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
1235
1236 return 0;
1237}
1238EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1239
1240
1241void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1242{
1243 if (!governor)
1244 return;
1245
3fc54d37 1246 mutex_lock(&cpufreq_governor_mutex);
1da177e4 1247 list_del(&governor->governor_list);
3fc54d37 1248 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
1249 return;
1250}
1251EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1252
1253
1254
1255/*********************************************************************
1256 * POLICY INTERFACE *
1257 *********************************************************************/
1258
1259/**
1260 * cpufreq_get_policy - get the current cpufreq_policy
1261 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1262 *
1263 * Reads the current cpufreq policy.
1264 */
1265int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1266{
1267 struct cpufreq_policy *cpu_policy;
1268 if (!policy)
1269 return -EINVAL;
1270
1271 cpu_policy = cpufreq_cpu_get(cpu);
1272 if (!cpu_policy)
1273 return -EINVAL;
1274
83933af4 1275 mutex_lock(&cpu_policy->lock);
1da177e4 1276 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
83933af4 1277 mutex_unlock(&cpu_policy->lock);
1da177e4
LT
1278
1279 cpufreq_cpu_put(cpu_policy);
1280
1281 return 0;
1282}
1283EXPORT_SYMBOL(cpufreq_get_policy);
1284
1285
1286static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1287{
1288 int ret = 0;
1289
1290 cpufreq_debug_disable_ratelimit();
1291 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1292 policy->min, policy->max);
1293
1294 memcpy(&policy->cpuinfo,
1295 &data->cpuinfo,
1296 sizeof(struct cpufreq_cpuinfo));
1297
1298 /* verify the cpu speed can be set within this limit */
1299 ret = cpufreq_driver->verify(policy);
1300 if (ret)
1301 goto error_out;
1302
1303 down_read(&cpufreq_notifier_rwsem);
1304
1305 /* adjust if necessary - all reasons */
1306 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
1307 policy);
1308
1309 /* adjust if necessary - hardware incompatibility*/
1310 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
1311 policy);
1312
1313 /* verify the cpu speed can be set within this limit,
1314 which might be different to the first one */
1315 ret = cpufreq_driver->verify(policy);
1316 if (ret) {
1317 up_read(&cpufreq_notifier_rwsem);
1318 goto error_out;
1319 }
1320
1321 /* notification of the new policy */
1322 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
1323 policy);
1324
1325 up_read(&cpufreq_notifier_rwsem);
1326
1327 data->min = policy->min;
1328 data->max = policy->max;
1329
1330 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1331
1332 if (cpufreq_driver->setpolicy) {
1333 data->policy = policy->policy;
1334 dprintk("setting range\n");
1335 ret = cpufreq_driver->setpolicy(policy);
1336 } else {
1337 if (policy->governor != data->governor) {
1338 /* save old, working values */
1339 struct cpufreq_governor *old_gov = data->governor;
1340
1341 dprintk("governor switch\n");
1342
1343 /* end old governor */
1344 if (data->governor)
1345 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1346
1347 /* start new governor */
1348 data->governor = policy->governor;
1349 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1350 /* new governor failed, so re-start old one */
1351 dprintk("starting governor %s failed\n", data->governor->name);
1352 if (old_gov) {
1353 data->governor = old_gov;
1354 __cpufreq_governor(data, CPUFREQ_GOV_START);
1355 }
1356 ret = -EINVAL;
1357 goto error_out;
1358 }
1359 /* might be a policy change, too, so fall through */
1360 }
1361 dprintk("governor: change or update limits\n");
1362 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1363 }
1364
1365 error_out:
1366 cpufreq_debug_enable_ratelimit();
1367 return ret;
1368}
1369
1370/**
1371 * cpufreq_set_policy - set a new CPUFreq policy
1372 * @policy: policy to be set.
1373 *
1374 * Sets a new CPU frequency and voltage scaling policy.
1375 */
1376int cpufreq_set_policy(struct cpufreq_policy *policy)
1377{
1378 int ret = 0;
1379 struct cpufreq_policy *data;
1380
1381 if (!policy)
1382 return -EINVAL;
1383
1384 data = cpufreq_cpu_get(policy->cpu);
1385 if (!data)
1386 return -EINVAL;
1387
1388 /* lock this CPU */
83933af4 1389 mutex_lock(&data->lock);
1da177e4
LT
1390
1391 ret = __cpufreq_set_policy(data, policy);
1392 data->user_policy.min = data->min;
1393 data->user_policy.max = data->max;
1394 data->user_policy.policy = data->policy;
1395 data->user_policy.governor = data->governor;
1396
83933af4 1397 mutex_unlock(&data->lock);
1da177e4
LT
1398 cpufreq_cpu_put(data);
1399
1400 return ret;
1401}
1402EXPORT_SYMBOL(cpufreq_set_policy);
1403
1404
1405/**
1406 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1407 * @cpu: CPU which shall be re-evaluated
1408 *
1409 * Usefull for policy notifiers which have different necessities
1410 * at different times.
1411 */
1412int cpufreq_update_policy(unsigned int cpu)
1413{
1414 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1415 struct cpufreq_policy policy;
1416 int ret = 0;
1417
1418 if (!data)
1419 return -ENODEV;
1420
83933af4 1421 mutex_lock(&data->lock);
1da177e4
LT
1422
1423 dprintk("updating policy for CPU %u\n", cpu);
1424 memcpy(&policy,
1425 data,
1426 sizeof(struct cpufreq_policy));
1427 policy.min = data->user_policy.min;
1428 policy.max = data->user_policy.max;
1429 policy.policy = data->user_policy.policy;
1430 policy.governor = data->user_policy.governor;
1431
1432 ret = __cpufreq_set_policy(data, &policy);
1433
83933af4 1434 mutex_unlock(&data->lock);
1da177e4
LT
1435
1436 cpufreq_cpu_put(data);
1437 return ret;
1438}
1439EXPORT_SYMBOL(cpufreq_update_policy);
1440
c32b6b8e
AR
1441static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1442 unsigned long action, void *hcpu)
1443{
1444 unsigned int cpu = (unsigned long)hcpu;
1445 struct cpufreq_policy *policy;
1446 struct sys_device *sys_dev;
1447
1448 sys_dev = get_cpu_sysdev(cpu);
1449
1450 if (sys_dev) {
1451 switch (action) {
1452 case CPU_ONLINE:
1453 cpufreq_add_dev(sys_dev);
1454 break;
1455 case CPU_DOWN_PREPARE:
1456 /*
1457 * We attempt to put this cpu in lowest frequency
1458 * possible before going down. This will permit
1459 * hardware-managed P-State to switch other related
1460 * threads to min or higher speeds if possible.
1461 */
1462 policy = cpufreq_cpu_data[cpu];
1463 if (policy) {
1464 cpufreq_driver_target(policy, policy->min,
1465 CPUFREQ_RELATION_H);
1466 }
1467 break;
1468 case CPU_DEAD:
1469 cpufreq_remove_dev(sys_dev);
1470 break;
1471 }
1472 }
1473 return NOTIFY_OK;
1474}
1475
1476static struct notifier_block cpufreq_cpu_notifier =
1477{
1478 .notifier_call = cpufreq_cpu_callback,
1479};
1da177e4
LT
1480
1481/*********************************************************************
1482 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1483 *********************************************************************/
1484
1485/**
1486 * cpufreq_register_driver - register a CPU Frequency driver
1487 * @driver_data: A struct cpufreq_driver containing the values#
1488 * submitted by the CPU Frequency driver.
1489 *
1490 * Registers a CPU Frequency driver to this core code. This code
1491 * returns zero on success, -EBUSY when another driver got here first
1492 * (and isn't unregistered in the meantime).
1493 *
1494 */
1495int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1496{
1497 unsigned long flags;
1498 int ret;
1499
1500 if (!driver_data || !driver_data->verify || !driver_data->init ||
1501 ((!driver_data->setpolicy) && (!driver_data->target)))
1502 return -EINVAL;
1503
1504 dprintk("trying to register driver %s\n", driver_data->name);
1505
1506 if (driver_data->setpolicy)
1507 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1508
1509 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1510 if (cpufreq_driver) {
1511 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1512 return -EBUSY;
1513 }
1514 cpufreq_driver = driver_data;
1515 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1516
1517 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1518
1519 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1520 int i;
1521 ret = -ENODEV;
1522
1523 /* check for at least one working CPU */
1524 for (i=0; i<NR_CPUS; i++)
1525 if (cpufreq_cpu_data[i])
1526 ret = 0;
1527
1528 /* if all ->init() calls failed, unregister */
1529 if (ret) {
1530 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1531 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1532
1533 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1534 cpufreq_driver = NULL;
1535 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1536 }
1537 }
1538
1539 if (!ret) {
c32b6b8e 1540 register_cpu_notifier(&cpufreq_cpu_notifier);
1da177e4
LT
1541 dprintk("driver %s up and running\n", driver_data->name);
1542 cpufreq_debug_enable_ratelimit();
1543 }
1544
1545 return (ret);
1546}
1547EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1548
1549
1550/**
1551 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1552 *
1553 * Unregister the current CPUFreq driver. Only call this if you have
1554 * the right to do so, i.e. if you have succeeded in initialising before!
1555 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1556 * currently not initialised.
1557 */
1558int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1559{
1560 unsigned long flags;
1561
1562 cpufreq_debug_disable_ratelimit();
1563
1564 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1565 cpufreq_debug_enable_ratelimit();
1566 return -EINVAL;
1567 }
1568
1569 dprintk("unregistering driver %s\n", driver->name);
1570
1571 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
c32b6b8e 1572 unregister_cpu_notifier(&cpufreq_cpu_notifier);
1da177e4
LT
1573
1574 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1575 cpufreq_driver = NULL;
1576 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1577
1578 return 0;
1579}
1580EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);