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