Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[GitHub/mt8127/android_kernel_alcatel_ttab.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 *
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 */
37 static struct cpufreq_driver *cpufreq_driver;
38 static struct cpufreq_policy *cpufreq_cpu_data[NR_CPUS];
39 static DEFINE_SPINLOCK(cpufreq_driver_lock);
40
41
42 /* internal prototypes */
43 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
44 static void handle_update(void *data);
45 static 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 */
54 static struct notifier_block *cpufreq_policy_notifier_list;
55 static struct notifier_block *cpufreq_transition_notifier_list;
56 static DECLARE_RWSEM (cpufreq_notifier_rwsem);
57
58
59 static LIST_HEAD(cpufreq_governor_list);
60 static DECLARE_MUTEX (cpufreq_governor_sem);
61
62 struct 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 }
101 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
102
103 void cpufreq_cpu_put(struct cpufreq_policy *data)
104 {
105 kobject_put(&data->kobj);
106 module_put(cpufreq_driver->owner);
107 }
108 EXPORT_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? */
117 static unsigned int debug;
118
119 /* is the debug output ratelimit'ed using printk_ratelimit? User can
120 * set or modify this value.
121 */
122 static 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 */
128 static unsigned int disable_ratelimit = 1;
129 static DEFINE_SPINLOCK(disable_ratelimit_lock);
130
131 static 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
141 static 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
150 void 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 }
177 EXPORT_SYMBOL(cpufreq_debug_printk);
178
179
180 module_param(debug, uint, 0644);
181 MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core, 2 to debug drivers, and 4 to debug governors.");
182
183 module_param(debug_ratelimit, uint, 0644);
184 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging: set to 0 to disable ratelimiting.");
185
186 #else /* !CONFIG_CPU_FREQ_DEBUG */
187
188 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
189 static 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
207 static unsigned long l_p_j_ref;
208 static unsigned int l_p_j_ref_freq;
209
210 static 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) ||
222 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
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
228 static 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 */
238 void 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 {
257 dprintk(KERN_WARNING "Warning: CPU frequency is %u, "
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 }
275 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
276
277
278
279 /*********************************************************************
280 * SYSFS INTERFACE *
281 *********************************************************************/
282
283 /**
284 * cpufreq_parse_governor - parse a governor string
285 */
286 static 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 }
317 EXPORT_SYMBOL_GPL(cpufreq_parse_governor);
318
319
320 /* drivers/base/cpu.c */
321 extern 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) \
332 static ssize_t show_##file_name \
333 (struct cpufreq_policy * policy, char *buf) \
334 { \
335 return sprintf (buf, "%u\n", policy->object); \
336 }
337
338 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
339 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
340 show_one(scaling_min_freq, min);
341 show_one(scaling_max_freq, max);
342 show_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) \
348 static 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
367 store_one(scaling_min_freq,min);
368 store_one(scaling_max_freq,max);
369
370 /**
371 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
372 */
373 static 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 */
385 static 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 */
400 static 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 */
426 static 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 */
434 static 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 */
457 static 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) \
475 static struct freq_attr _name = \
476 __ATTR(_name, 0444, show_##_name, NULL)
477
478 #define define_one_ro0400(_name) \
479 static struct freq_attr _name = \
480 __ATTR(_name, 0400, show_##_name, NULL)
481
482 #define define_one_rw(_name) \
483 static struct freq_attr _name = \
484 __ATTR(_name, 0644, show_##_name, store_##_name)
485
486 define_one_ro0400(cpuinfo_cur_freq);
487 define_one_ro(cpuinfo_min_freq);
488 define_one_ro(cpuinfo_max_freq);
489 define_one_ro(scaling_available_governors);
490 define_one_ro(scaling_driver);
491 define_one_ro(scaling_cur_freq);
492 define_one_ro(affected_cpus);
493 define_one_rw(scaling_min_freq);
494 define_one_rw(scaling_max_freq);
495 define_one_rw(scaling_governor);
496
497 static 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
512 static 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;
520 ret = fattr->show ? fattr->show(policy,buf) : -EIO;
521 cpufreq_cpu_put(policy);
522 return ret;
523 }
524
525 static 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;
534 ret = fattr->store ? fattr->store(policy,buf,count) : -EIO;
535 cpufreq_cpu_put(policy);
536 return ret;
537 }
538
539 static 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
546 static struct sysfs_ops sysfs_ops = {
547 .show = show,
548 .store = store,
549 };
550
551 static 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 */
563 static 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
573 if (cpu_is_offline(cpu))
574 return 0;
575
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)) {
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
596 policy = kmalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
597 if (!policy) {
598 ret = -ENOMEM;
599 goto nomem_out;
600 }
601 memset(policy, 0, sizeof(struct cpufreq_policy));
602
603 policy->cpu = cpu;
604 policy->cpus = cpumask_of_cpu(cpu);
605
606 init_MUTEX_LOCKED(&policy->lock);
607 init_completion(&policy->kobj_unregister);
608 INIT_WORK(&policy->update, handle_update, (void *)(long)cpu);
609
610 /* call driver. From then on the cpufreq must be able
611 * to accept all calls to ->verify and ->setpolicy for this CPU
612 */
613 ret = cpufreq_driver->init(policy);
614 if (ret) {
615 dprintk("initialization failed\n");
616 goto err_out;
617 }
618
619 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
620
621 /* prepare interface data */
622 policy->kobj.parent = &sys_dev->kobj;
623 policy->kobj.ktype = &ktype_cpufreq;
624 strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
625
626 ret = kobject_register(&policy->kobj);
627 if (ret)
628 goto err_out_driver_exit;
629
630 /* set up files for this cpu device */
631 drv_attr = cpufreq_driver->attr;
632 while ((drv_attr) && (*drv_attr)) {
633 sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
634 drv_attr++;
635 }
636 if (cpufreq_driver->get)
637 sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
638 if (cpufreq_driver->target)
639 sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
640
641 spin_lock_irqsave(&cpufreq_driver_lock, flags);
642 for_each_cpu_mask(j, policy->cpus)
643 cpufreq_cpu_data[j] = policy;
644 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
645 policy->governor = NULL; /* to assure that the starting sequence is
646 * run in cpufreq_set_policy */
647 up(&policy->lock);
648
649 /* set default policy */
650
651 ret = cpufreq_set_policy(&new_policy);
652 if (ret) {
653 dprintk("setting policy failed\n");
654 goto err_out_unregister;
655 }
656
657 module_put(cpufreq_driver->owner);
658 dprintk("initialization complete\n");
659 cpufreq_debug_enable_ratelimit();
660
661 return 0;
662
663
664 err_out_unregister:
665 spin_lock_irqsave(&cpufreq_driver_lock, flags);
666 for_each_cpu_mask(j, policy->cpus)
667 cpufreq_cpu_data[j] = NULL;
668 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
669
670 kobject_unregister(&policy->kobj);
671 wait_for_completion(&policy->kobj_unregister);
672
673 err_out_driver_exit:
674 if (cpufreq_driver->exit)
675 cpufreq_driver->exit(policy);
676
677 err_out:
678 kfree(policy);
679
680 nomem_out:
681 module_put(cpufreq_driver->owner);
682 module_out:
683 cpufreq_debug_enable_ratelimit();
684 return ret;
685 }
686
687
688 /**
689 * cpufreq_remove_dev - remove a CPU device
690 *
691 * Removes the cpufreq interface for a CPU device.
692 */
693 static int cpufreq_remove_dev (struct sys_device * sys_dev)
694 {
695 unsigned int cpu = sys_dev->id;
696 unsigned long flags;
697 struct cpufreq_policy *data;
698 struct sys_device *cpu_sys_dev;
699 #ifdef CONFIG_SMP
700 unsigned int j;
701 #endif
702
703 cpufreq_debug_disable_ratelimit();
704 dprintk("unregistering CPU %u\n", cpu);
705
706 spin_lock_irqsave(&cpufreq_driver_lock, flags);
707 data = cpufreq_cpu_data[cpu];
708
709 if (!data) {
710 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
711 cpufreq_debug_enable_ratelimit();
712 return -EINVAL;
713 }
714 cpufreq_cpu_data[cpu] = NULL;
715
716
717 #ifdef CONFIG_SMP
718 /* if this isn't the CPU which is the parent of the kobj, we
719 * only need to unlink, put and exit
720 */
721 if (unlikely(cpu != data->cpu)) {
722 dprintk("removing link\n");
723 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
724 sysfs_remove_link(&sys_dev->kobj, "cpufreq");
725 cpufreq_cpu_put(data);
726 cpufreq_debug_enable_ratelimit();
727 return 0;
728 }
729 #endif
730
731
732 if (!kobject_get(&data->kobj)) {
733 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
734 cpufreq_debug_enable_ratelimit();
735 return -EFAULT;
736 }
737
738 #ifdef CONFIG_SMP
739 /* if we have other CPUs still registered, we need to unlink them,
740 * or else wait_for_completion below will lock up. Clean the
741 * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
742 * links afterwards.
743 */
744 if (unlikely(cpus_weight(data->cpus) > 1)) {
745 for_each_cpu_mask(j, data->cpus) {
746 if (j == cpu)
747 continue;
748 cpufreq_cpu_data[j] = NULL;
749 }
750 }
751
752 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
753
754 if (unlikely(cpus_weight(data->cpus) > 1)) {
755 for_each_cpu_mask(j, data->cpus) {
756 if (j == cpu)
757 continue;
758 dprintk("removing link for cpu %u\n", j);
759 cpu_sys_dev = get_cpu_sysdev(j);
760 sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
761 cpufreq_cpu_put(data);
762 }
763 }
764 #else
765 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
766 #endif
767
768 down(&data->lock);
769 if (cpufreq_driver->target)
770 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
771 up(&data->lock);
772
773 kobject_unregister(&data->kobj);
774
775 kobject_put(&data->kobj);
776
777 /* we need to make sure that the underlying kobj is actually
778 * not referenced anymore by anybody before we proceed with
779 * unloading.
780 */
781 dprintk("waiting for dropping of refcount\n");
782 wait_for_completion(&data->kobj_unregister);
783 dprintk("wait complete\n");
784
785 if (cpufreq_driver->exit)
786 cpufreq_driver->exit(data);
787
788 kfree(data);
789
790 cpufreq_debug_enable_ratelimit();
791
792 return 0;
793 }
794
795
796 static void handle_update(void *data)
797 {
798 unsigned int cpu = (unsigned int)(long)data;
799 dprintk("handle_update for cpu %u called\n", cpu);
800 cpufreq_update_policy(cpu);
801 }
802
803 /**
804 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
805 * @cpu: cpu number
806 * @old_freq: CPU frequency the kernel thinks the CPU runs at
807 * @new_freq: CPU frequency the CPU actually runs at
808 *
809 * We adjust to current frequency first, and need to clean up later. So either call
810 * to cpufreq_update_policy() or schedule handle_update()).
811 */
812 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, unsigned int new_freq)
813 {
814 struct cpufreq_freqs freqs;
815
816 dprintk(KERN_WARNING "Warning: CPU frequency out of sync: cpufreq and timing "
817 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
818
819 freqs.cpu = cpu;
820 freqs.old = old_freq;
821 freqs.new = new_freq;
822 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
823 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
824 }
825
826
827 /**
828 * cpufreq_get - get the current CPU frequency (in kHz)
829 * @cpu: CPU number
830 *
831 * Get the CPU current (static) CPU frequency
832 */
833 unsigned int cpufreq_get(unsigned int cpu)
834 {
835 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
836 unsigned int ret = 0;
837
838 if (!policy)
839 return 0;
840
841 if (!cpufreq_driver->get)
842 goto out;
843
844 down(&policy->lock);
845
846 ret = cpufreq_driver->get(cpu);
847
848 if (ret && policy->cur && !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS))
849 {
850 /* verify no discrepancy between actual and saved value exists */
851 if (unlikely(ret != policy->cur)) {
852 cpufreq_out_of_sync(cpu, policy->cur, ret);
853 schedule_work(&policy->update);
854 }
855 }
856
857 up(&policy->lock);
858
859 out:
860 cpufreq_cpu_put(policy);
861
862 return (ret);
863 }
864 EXPORT_SYMBOL(cpufreq_get);
865
866
867 /**
868 * cpufreq_suspend - let the low level driver prepare for suspend
869 */
870
871 static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
872 {
873 int cpu = sysdev->id;
874 unsigned int ret = 0;
875 unsigned int cur_freq = 0;
876 struct cpufreq_policy *cpu_policy;
877
878 dprintk("resuming cpu %u\n", cpu);
879
880 if (!cpu_online(cpu))
881 return 0;
882
883 /* we may be lax here as interrupts are off. Nonetheless
884 * we need to grab the correct cpu policy, as to check
885 * whether we really run on this CPU.
886 */
887
888 cpu_policy = cpufreq_cpu_get(cpu);
889 if (!cpu_policy)
890 return -EINVAL;
891
892 /* only handle each CPU group once */
893 if (unlikely(cpu_policy->cpu != cpu)) {
894 cpufreq_cpu_put(cpu_policy);
895 return 0;
896 }
897
898 if (cpufreq_driver->suspend) {
899 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
900 if (ret) {
901 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
902 "step on CPU %u\n", cpu_policy->cpu);
903 cpufreq_cpu_put(cpu_policy);
904 return ret;
905 }
906 }
907
908
909 if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
910 goto out;
911
912 if (cpufreq_driver->get)
913 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
914
915 if (!cur_freq || !cpu_policy->cur) {
916 printk(KERN_ERR "cpufreq: suspend failed to assert current "
917 "frequency is what timing core thinks it is.\n");
918 goto out;
919 }
920
921 if (unlikely(cur_freq != cpu_policy->cur)) {
922 struct cpufreq_freqs freqs;
923
924 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
925 dprintk(KERN_DEBUG "Warning: CPU frequency is %u, "
926 "cpufreq assumed %u kHz.\n",
927 cur_freq, cpu_policy->cur);
928
929 freqs.cpu = cpu;
930 freqs.old = cpu_policy->cur;
931 freqs.new = cur_freq;
932
933 notifier_call_chain(&cpufreq_transition_notifier_list,
934 CPUFREQ_SUSPENDCHANGE, &freqs);
935 adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
936
937 cpu_policy->cur = cur_freq;
938 }
939
940 out:
941 cpufreq_cpu_put(cpu_policy);
942 return 0;
943 }
944
945 /**
946 * cpufreq_resume - restore proper CPU frequency handling after resume
947 *
948 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
949 * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
950 * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
951 * restored.
952 */
953 static int cpufreq_resume(struct sys_device * sysdev)
954 {
955 int cpu = sysdev->id;
956 unsigned int ret = 0;
957 struct cpufreq_policy *cpu_policy;
958
959 dprintk("resuming cpu %u\n", cpu);
960
961 if (!cpu_online(cpu))
962 return 0;
963
964 /* we may be lax here as interrupts are off. Nonetheless
965 * we need to grab the correct cpu policy, as to check
966 * whether we really run on this CPU.
967 */
968
969 cpu_policy = cpufreq_cpu_get(cpu);
970 if (!cpu_policy)
971 return -EINVAL;
972
973 /* only handle each CPU group once */
974 if (unlikely(cpu_policy->cpu != cpu)) {
975 cpufreq_cpu_put(cpu_policy);
976 return 0;
977 }
978
979 if (cpufreq_driver->resume) {
980 ret = cpufreq_driver->resume(cpu_policy);
981 if (ret) {
982 printk(KERN_ERR "cpufreq: resume failed in ->resume "
983 "step on CPU %u\n", cpu_policy->cpu);
984 cpufreq_cpu_put(cpu_policy);
985 return ret;
986 }
987 }
988
989 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
990 unsigned int cur_freq = 0;
991
992 if (cpufreq_driver->get)
993 cur_freq = cpufreq_driver->get(cpu_policy->cpu);
994
995 if (!cur_freq || !cpu_policy->cur) {
996 printk(KERN_ERR "cpufreq: resume failed to assert "
997 "current frequency is what timing core "
998 "thinks it is.\n");
999 goto out;
1000 }
1001
1002 if (unlikely(cur_freq != cpu_policy->cur)) {
1003 struct cpufreq_freqs freqs;
1004
1005 if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
1006 dprintk(KERN_WARNING "Warning: CPU frequency"
1007 "is %u, cpufreq assumed %u kHz.\n",
1008 cur_freq, cpu_policy->cur);
1009
1010 freqs.cpu = cpu;
1011 freqs.old = cpu_policy->cur;
1012 freqs.new = cur_freq;
1013
1014 notifier_call_chain(&cpufreq_transition_notifier_list,
1015 CPUFREQ_RESUMECHANGE, &freqs);
1016 adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
1017
1018 cpu_policy->cur = cur_freq;
1019 }
1020 }
1021
1022 out:
1023 schedule_work(&cpu_policy->update);
1024 cpufreq_cpu_put(cpu_policy);
1025 return ret;
1026 }
1027
1028 static struct sysdev_driver cpufreq_sysdev_driver = {
1029 .add = cpufreq_add_dev,
1030 .remove = cpufreq_remove_dev,
1031 .suspend = cpufreq_suspend,
1032 .resume = cpufreq_resume,
1033 };
1034
1035
1036 /*********************************************************************
1037 * NOTIFIER LISTS INTERFACE *
1038 *********************************************************************/
1039
1040 /**
1041 * cpufreq_register_notifier - register a driver with cpufreq
1042 * @nb: notifier function to register
1043 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1044 *
1045 * Add a driver to one of two lists: either a list of drivers that
1046 * are notified about clock rate changes (once before and once after
1047 * the transition), or a list of drivers that are notified about
1048 * changes in cpufreq policy.
1049 *
1050 * This function may sleep, and has the same return conditions as
1051 * notifier_chain_register.
1052 */
1053 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1054 {
1055 int ret;
1056
1057 down_write(&cpufreq_notifier_rwsem);
1058 switch (list) {
1059 case CPUFREQ_TRANSITION_NOTIFIER:
1060 ret = notifier_chain_register(&cpufreq_transition_notifier_list, nb);
1061 break;
1062 case CPUFREQ_POLICY_NOTIFIER:
1063 ret = notifier_chain_register(&cpufreq_policy_notifier_list, nb);
1064 break;
1065 default:
1066 ret = -EINVAL;
1067 }
1068 up_write(&cpufreq_notifier_rwsem);
1069
1070 return ret;
1071 }
1072 EXPORT_SYMBOL(cpufreq_register_notifier);
1073
1074
1075 /**
1076 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1077 * @nb: notifier block to be unregistered
1078 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1079 *
1080 * Remove a driver from the CPU frequency notifier list.
1081 *
1082 * This function may sleep, and has the same return conditions as
1083 * notifier_chain_unregister.
1084 */
1085 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1086 {
1087 int ret;
1088
1089 down_write(&cpufreq_notifier_rwsem);
1090 switch (list) {
1091 case CPUFREQ_TRANSITION_NOTIFIER:
1092 ret = notifier_chain_unregister(&cpufreq_transition_notifier_list, nb);
1093 break;
1094 case CPUFREQ_POLICY_NOTIFIER:
1095 ret = notifier_chain_unregister(&cpufreq_policy_notifier_list, nb);
1096 break;
1097 default:
1098 ret = -EINVAL;
1099 }
1100 up_write(&cpufreq_notifier_rwsem);
1101
1102 return ret;
1103 }
1104 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1105
1106
1107 /*********************************************************************
1108 * GOVERNORS *
1109 *********************************************************************/
1110
1111
1112 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1113 unsigned int target_freq,
1114 unsigned int relation)
1115 {
1116 int retval = -EINVAL;
1117
1118 /*
1119 * Converted the lock_cpu_hotplug to preempt_disable()
1120 * and preempt_enable(). This is a bit kludgy and relies on how cpu
1121 * hotplug works. All we need is a guarantee that cpu hotplug won't make
1122 * progress on any cpu. Once we do preempt_disable(), this would ensure
1123 * that hotplug threads don't get onto this cpu, thereby delaying
1124 * the cpu remove process.
1125 *
1126 * We removed the lock_cpu_hotplug since we need to call this function
1127 * via cpu hotplug callbacks, which result in locking the cpu hotplug
1128 * thread itself. Agree this is not very clean, cpufreq community
1129 * could improve this if required. - Ashok Raj <ashok.raj@intel.com>
1130 */
1131 preempt_disable();
1132 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1133 target_freq, relation);
1134 if (cpu_online(policy->cpu) && cpufreq_driver->target)
1135 retval = cpufreq_driver->target(policy, target_freq, relation);
1136 preempt_enable();
1137 return retval;
1138 }
1139 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1140
1141 int cpufreq_driver_target(struct cpufreq_policy *policy,
1142 unsigned int target_freq,
1143 unsigned int relation)
1144 {
1145 int ret;
1146
1147 policy = cpufreq_cpu_get(policy->cpu);
1148 if (!policy)
1149 return -EINVAL;
1150
1151 down(&policy->lock);
1152
1153 ret = __cpufreq_driver_target(policy, target_freq, relation);
1154
1155 up(&policy->lock);
1156
1157 cpufreq_cpu_put(policy);
1158
1159 return ret;
1160 }
1161 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1162
1163
1164 static int __cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
1165 {
1166 int ret;
1167
1168 if (!try_module_get(policy->governor->owner))
1169 return -EINVAL;
1170
1171 dprintk("__cpufreq_governor for CPU %u, event %u\n", policy->cpu, event);
1172 ret = policy->governor->governor(policy, event);
1173
1174 /* we keep one module reference alive for each CPU governed by this CPU */
1175 if ((event != CPUFREQ_GOV_START) || ret)
1176 module_put(policy->governor->owner);
1177 if ((event == CPUFREQ_GOV_STOP) && !ret)
1178 module_put(policy->governor->owner);
1179
1180 return ret;
1181 }
1182
1183
1184 int cpufreq_governor(unsigned int cpu, unsigned int event)
1185 {
1186 int ret = 0;
1187 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1188
1189 if (!policy)
1190 return -EINVAL;
1191
1192 down(&policy->lock);
1193 ret = __cpufreq_governor(policy, event);
1194 up(&policy->lock);
1195
1196 cpufreq_cpu_put(policy);
1197
1198 return ret;
1199 }
1200 EXPORT_SYMBOL_GPL(cpufreq_governor);
1201
1202
1203 int cpufreq_register_governor(struct cpufreq_governor *governor)
1204 {
1205 struct cpufreq_governor *t;
1206
1207 if (!governor)
1208 return -EINVAL;
1209
1210 down(&cpufreq_governor_sem);
1211
1212 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
1213 if (!strnicmp(governor->name,t->name,CPUFREQ_NAME_LEN)) {
1214 up(&cpufreq_governor_sem);
1215 return -EBUSY;
1216 }
1217 }
1218 list_add(&governor->governor_list, &cpufreq_governor_list);
1219
1220 up(&cpufreq_governor_sem);
1221
1222 return 0;
1223 }
1224 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1225
1226
1227 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1228 {
1229 if (!governor)
1230 return;
1231
1232 down(&cpufreq_governor_sem);
1233 list_del(&governor->governor_list);
1234 up(&cpufreq_governor_sem);
1235 return;
1236 }
1237 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1238
1239
1240
1241 /*********************************************************************
1242 * POLICY INTERFACE *
1243 *********************************************************************/
1244
1245 /**
1246 * cpufreq_get_policy - get the current cpufreq_policy
1247 * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
1248 *
1249 * Reads the current cpufreq policy.
1250 */
1251 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1252 {
1253 struct cpufreq_policy *cpu_policy;
1254 if (!policy)
1255 return -EINVAL;
1256
1257 cpu_policy = cpufreq_cpu_get(cpu);
1258 if (!cpu_policy)
1259 return -EINVAL;
1260
1261 down(&cpu_policy->lock);
1262 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1263 up(&cpu_policy->lock);
1264
1265 cpufreq_cpu_put(cpu_policy);
1266
1267 return 0;
1268 }
1269 EXPORT_SYMBOL(cpufreq_get_policy);
1270
1271
1272 static int __cpufreq_set_policy(struct cpufreq_policy *data, struct cpufreq_policy *policy)
1273 {
1274 int ret = 0;
1275
1276 cpufreq_debug_disable_ratelimit();
1277 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1278 policy->min, policy->max);
1279
1280 memcpy(&policy->cpuinfo,
1281 &data->cpuinfo,
1282 sizeof(struct cpufreq_cpuinfo));
1283
1284 /* verify the cpu speed can be set within this limit */
1285 ret = cpufreq_driver->verify(policy);
1286 if (ret)
1287 goto error_out;
1288
1289 down_read(&cpufreq_notifier_rwsem);
1290
1291 /* adjust if necessary - all reasons */
1292 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_ADJUST,
1293 policy);
1294
1295 /* adjust if necessary - hardware incompatibility*/
1296 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_INCOMPATIBLE,
1297 policy);
1298
1299 /* verify the cpu speed can be set within this limit,
1300 which might be different to the first one */
1301 ret = cpufreq_driver->verify(policy);
1302 if (ret) {
1303 up_read(&cpufreq_notifier_rwsem);
1304 goto error_out;
1305 }
1306
1307 /* notification of the new policy */
1308 notifier_call_chain(&cpufreq_policy_notifier_list, CPUFREQ_NOTIFY,
1309 policy);
1310
1311 up_read(&cpufreq_notifier_rwsem);
1312
1313 data->min = policy->min;
1314 data->max = policy->max;
1315
1316 dprintk("new min and max freqs are %u - %u kHz\n", data->min, data->max);
1317
1318 if (cpufreq_driver->setpolicy) {
1319 data->policy = policy->policy;
1320 dprintk("setting range\n");
1321 ret = cpufreq_driver->setpolicy(policy);
1322 } else {
1323 if (policy->governor != data->governor) {
1324 /* save old, working values */
1325 struct cpufreq_governor *old_gov = data->governor;
1326
1327 dprintk("governor switch\n");
1328
1329 /* end old governor */
1330 if (data->governor)
1331 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1332
1333 /* start new governor */
1334 data->governor = policy->governor;
1335 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1336 /* new governor failed, so re-start old one */
1337 dprintk("starting governor %s failed\n", data->governor->name);
1338 if (old_gov) {
1339 data->governor = old_gov;
1340 __cpufreq_governor(data, CPUFREQ_GOV_START);
1341 }
1342 ret = -EINVAL;
1343 goto error_out;
1344 }
1345 /* might be a policy change, too, so fall through */
1346 }
1347 dprintk("governor: change or update limits\n");
1348 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1349 }
1350
1351 error_out:
1352 cpufreq_debug_enable_ratelimit();
1353 return ret;
1354 }
1355
1356 /**
1357 * cpufreq_set_policy - set a new CPUFreq policy
1358 * @policy: policy to be set.
1359 *
1360 * Sets a new CPU frequency and voltage scaling policy.
1361 */
1362 int cpufreq_set_policy(struct cpufreq_policy *policy)
1363 {
1364 int ret = 0;
1365 struct cpufreq_policy *data;
1366
1367 if (!policy)
1368 return -EINVAL;
1369
1370 data = cpufreq_cpu_get(policy->cpu);
1371 if (!data)
1372 return -EINVAL;
1373
1374 /* lock this CPU */
1375 down(&data->lock);
1376
1377 ret = __cpufreq_set_policy(data, policy);
1378 data->user_policy.min = data->min;
1379 data->user_policy.max = data->max;
1380 data->user_policy.policy = data->policy;
1381 data->user_policy.governor = data->governor;
1382
1383 up(&data->lock);
1384 cpufreq_cpu_put(data);
1385
1386 return ret;
1387 }
1388 EXPORT_SYMBOL(cpufreq_set_policy);
1389
1390
1391 /**
1392 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
1393 * @cpu: CPU which shall be re-evaluated
1394 *
1395 * Usefull for policy notifiers which have different necessities
1396 * at different times.
1397 */
1398 int cpufreq_update_policy(unsigned int cpu)
1399 {
1400 struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1401 struct cpufreq_policy policy;
1402 int ret = 0;
1403
1404 if (!data)
1405 return -ENODEV;
1406
1407 down(&data->lock);
1408
1409 dprintk("updating policy for CPU %u\n", cpu);
1410 memcpy(&policy,
1411 data,
1412 sizeof(struct cpufreq_policy));
1413 policy.min = data->user_policy.min;
1414 policy.max = data->user_policy.max;
1415 policy.policy = data->user_policy.policy;
1416 policy.governor = data->user_policy.governor;
1417
1418 ret = __cpufreq_set_policy(data, &policy);
1419
1420 up(&data->lock);
1421
1422 cpufreq_cpu_put(data);
1423 return ret;
1424 }
1425 EXPORT_SYMBOL(cpufreq_update_policy);
1426
1427 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1428 unsigned long action, void *hcpu)
1429 {
1430 unsigned int cpu = (unsigned long)hcpu;
1431 struct cpufreq_policy *policy;
1432 struct sys_device *sys_dev;
1433
1434 sys_dev = get_cpu_sysdev(cpu);
1435
1436 if (sys_dev) {
1437 switch (action) {
1438 case CPU_ONLINE:
1439 cpufreq_add_dev(sys_dev);
1440 break;
1441 case CPU_DOWN_PREPARE:
1442 /*
1443 * We attempt to put this cpu in lowest frequency
1444 * possible before going down. This will permit
1445 * hardware-managed P-State to switch other related
1446 * threads to min or higher speeds if possible.
1447 */
1448 policy = cpufreq_cpu_data[cpu];
1449 if (policy) {
1450 cpufreq_driver_target(policy, policy->min,
1451 CPUFREQ_RELATION_H);
1452 }
1453 break;
1454 case CPU_DEAD:
1455 cpufreq_remove_dev(sys_dev);
1456 break;
1457 }
1458 }
1459 return NOTIFY_OK;
1460 }
1461
1462 static struct notifier_block cpufreq_cpu_notifier =
1463 {
1464 .notifier_call = cpufreq_cpu_callback,
1465 };
1466
1467 /*********************************************************************
1468 * REGISTER / UNREGISTER CPUFREQ DRIVER *
1469 *********************************************************************/
1470
1471 /**
1472 * cpufreq_register_driver - register a CPU Frequency driver
1473 * @driver_data: A struct cpufreq_driver containing the values#
1474 * submitted by the CPU Frequency driver.
1475 *
1476 * Registers a CPU Frequency driver to this core code. This code
1477 * returns zero on success, -EBUSY when another driver got here first
1478 * (and isn't unregistered in the meantime).
1479 *
1480 */
1481 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1482 {
1483 unsigned long flags;
1484 int ret;
1485
1486 if (!driver_data || !driver_data->verify || !driver_data->init ||
1487 ((!driver_data->setpolicy) && (!driver_data->target)))
1488 return -EINVAL;
1489
1490 dprintk("trying to register driver %s\n", driver_data->name);
1491
1492 if (driver_data->setpolicy)
1493 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1494
1495 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1496 if (cpufreq_driver) {
1497 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1498 return -EBUSY;
1499 }
1500 cpufreq_driver = driver_data;
1501 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1502
1503 ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
1504
1505 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1506 int i;
1507 ret = -ENODEV;
1508
1509 /* check for at least one working CPU */
1510 for (i=0; i<NR_CPUS; i++)
1511 if (cpufreq_cpu_data[i])
1512 ret = 0;
1513
1514 /* if all ->init() calls failed, unregister */
1515 if (ret) {
1516 dprintk("no CPU initialized for driver %s\n", driver_data->name);
1517 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1518
1519 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1520 cpufreq_driver = NULL;
1521 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1522 }
1523 }
1524
1525 if (!ret) {
1526 register_cpu_notifier(&cpufreq_cpu_notifier);
1527 dprintk("driver %s up and running\n", driver_data->name);
1528 cpufreq_debug_enable_ratelimit();
1529 }
1530
1531 return (ret);
1532 }
1533 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1534
1535
1536 /**
1537 * cpufreq_unregister_driver - unregister the current CPUFreq driver
1538 *
1539 * Unregister the current CPUFreq driver. Only call this if you have
1540 * the right to do so, i.e. if you have succeeded in initialising before!
1541 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1542 * currently not initialised.
1543 */
1544 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1545 {
1546 unsigned long flags;
1547
1548 cpufreq_debug_disable_ratelimit();
1549
1550 if (!cpufreq_driver || (driver != cpufreq_driver)) {
1551 cpufreq_debug_enable_ratelimit();
1552 return -EINVAL;
1553 }
1554
1555 dprintk("unregistering driver %s\n", driver->name);
1556
1557 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1558 unregister_cpu_notifier(&cpufreq_cpu_notifier);
1559
1560 spin_lock_irqsave(&cpufreq_driver_lock, flags);
1561 cpufreq_driver = NULL;
1562 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1563
1564 return 0;
1565 }
1566 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);