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