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