]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/cpufreq/cpufreq.c
40877d2190819dd6e0dcf189e7ed25ce7465f2b8
[mv-sheeva.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, \
33                                                 "cpufreq-core", msg)
34
35 /**
36  * The "cpufreq driver" - the arch- or hardware-dependent low
37  * level driver of CPUFreq support, and its spinlock. This lock
38  * also protects the cpufreq_cpu_data array.
39  */
40 static struct cpufreq_driver *cpufreq_driver;
41 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
42 #ifdef CONFIG_HOTPLUG_CPU
43 /* This one keeps track of the previously set governor of a removed CPU */
44 static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
45 #endif
46 static DEFINE_SPINLOCK(cpufreq_driver_lock);
47
48 /*
49  * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
50  * all cpufreq/hotplug/workqueue/etc related lock issues.
51  *
52  * The rules for this semaphore:
53  * - Any routine that wants to read from the policy structure will
54  *   do a down_read on this semaphore.
55  * - Any routine that will write to the policy structure and/or may take away
56  *   the policy altogether (eg. CPU hotplug), will hold this lock in write
57  *   mode before doing so.
58  *
59  * Additional rules:
60  * - All holders of the lock should check to make sure that the CPU they
61  *   are concerned with are online after they get the lock.
62  * - Governor routines that can be called in cpufreq hotplug path should not
63  *   take this sem as top level hotplug notifier handler takes this.
64  * - Lock should not be held across
65  *     __cpufreq_governor(data, CPUFREQ_GOV_STOP);
66  */
67 static DEFINE_PER_CPU(int, cpufreq_policy_cpu);
68 static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
69
70 #define lock_policy_rwsem(mode, cpu)                                    \
71 static int lock_policy_rwsem_##mode                                     \
72 (int cpu)                                                               \
73 {                                                                       \
74         int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);              \
75         BUG_ON(policy_cpu == -1);                                       \
76         down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));            \
77         if (unlikely(!cpu_online(cpu))) {                               \
78                 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu));      \
79                 return -1;                                              \
80         }                                                               \
81                                                                         \
82         return 0;                                                       \
83 }
84
85 lock_policy_rwsem(read, cpu);
86
87 lock_policy_rwsem(write, cpu);
88
89 static void unlock_policy_rwsem_read(int cpu)
90 {
91         int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
92         BUG_ON(policy_cpu == -1);
93         up_read(&per_cpu(cpu_policy_rwsem, policy_cpu));
94 }
95
96 static void unlock_policy_rwsem_write(int cpu)
97 {
98         int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu);
99         BUG_ON(policy_cpu == -1);
100         up_write(&per_cpu(cpu_policy_rwsem, policy_cpu));
101 }
102
103
104 /* internal prototypes */
105 static int __cpufreq_governor(struct cpufreq_policy *policy,
106                 unsigned int event);
107 static unsigned int __cpufreq_get(unsigned int cpu);
108 static void handle_update(struct work_struct *work);
109
110 /**
111  * Two notifier lists: the "policy" list is involved in the
112  * validation process for a new CPU frequency policy; the
113  * "transition" list for kernel code that needs to handle
114  * changes to devices when the CPU clock speed changes.
115  * The mutex locks both lists.
116  */
117 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
118 static struct srcu_notifier_head cpufreq_transition_notifier_list;
119
120 static bool init_cpufreq_transition_notifier_list_called;
121 static int __init init_cpufreq_transition_notifier_list(void)
122 {
123         srcu_init_notifier_head(&cpufreq_transition_notifier_list);
124         init_cpufreq_transition_notifier_list_called = true;
125         return 0;
126 }
127 pure_initcall(init_cpufreq_transition_notifier_list);
128
129 static LIST_HEAD(cpufreq_governor_list);
130 static DEFINE_MUTEX(cpufreq_governor_mutex);
131
132 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
133 {
134         struct cpufreq_policy *data;
135         unsigned long flags;
136
137         if (cpu >= nr_cpu_ids)
138                 goto err_out;
139
140         /* get the cpufreq driver */
141         spin_lock_irqsave(&cpufreq_driver_lock, flags);
142
143         if (!cpufreq_driver)
144                 goto err_out_unlock;
145
146         if (!try_module_get(cpufreq_driver->owner))
147                 goto err_out_unlock;
148
149
150         /* get the CPU */
151         data = per_cpu(cpufreq_cpu_data, cpu);
152
153         if (!data)
154                 goto err_out_put_module;
155
156         if (!kobject_get(&data->kobj))
157                 goto err_out_put_module;
158
159         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
160         return data;
161
162 err_out_put_module:
163         module_put(cpufreq_driver->owner);
164 err_out_unlock:
165         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
166 err_out:
167         return NULL;
168 }
169 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
170
171
172 void cpufreq_cpu_put(struct cpufreq_policy *data)
173 {
174         kobject_put(&data->kobj);
175         module_put(cpufreq_driver->owner);
176 }
177 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
178
179
180 /*********************************************************************
181  *                     UNIFIED DEBUG HELPERS                         *
182  *********************************************************************/
183 #ifdef CONFIG_CPU_FREQ_DEBUG
184
185 /* what part(s) of the CPUfreq subsystem are debugged? */
186 static unsigned int debug;
187
188 /* is the debug output ratelimit'ed using printk_ratelimit? User can
189  * set or modify this value.
190  */
191 static unsigned int debug_ratelimit = 1;
192
193 /* is the printk_ratelimit'ing enabled? It's enabled after a successful
194  * loading of a cpufreq driver, temporarily disabled when a new policy
195  * is set, and disabled upon cpufreq driver removal
196  */
197 static unsigned int disable_ratelimit = 1;
198 static DEFINE_SPINLOCK(disable_ratelimit_lock);
199
200 static void cpufreq_debug_enable_ratelimit(void)
201 {
202         unsigned long flags;
203
204         spin_lock_irqsave(&disable_ratelimit_lock, flags);
205         if (disable_ratelimit)
206                 disable_ratelimit--;
207         spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
208 }
209
210 static void cpufreq_debug_disable_ratelimit(void)
211 {
212         unsigned long flags;
213
214         spin_lock_irqsave(&disable_ratelimit_lock, flags);
215         disable_ratelimit++;
216         spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
217 }
218
219 void cpufreq_debug_printk(unsigned int type, const char *prefix,
220                         const char *fmt, ...)
221 {
222         char s[256];
223         va_list args;
224         unsigned int len;
225         unsigned long flags;
226
227         WARN_ON(!prefix);
228         if (type & debug) {
229                 spin_lock_irqsave(&disable_ratelimit_lock, flags);
230                 if (!disable_ratelimit && debug_ratelimit
231                                         && !printk_ratelimit()) {
232                         spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
233                         return;
234                 }
235                 spin_unlock_irqrestore(&disable_ratelimit_lock, flags);
236
237                 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix);
238
239                 va_start(args, fmt);
240                 len += vsnprintf(&s[len], (256 - len), fmt, args);
241                 va_end(args);
242
243                 printk(s);
244
245                 WARN_ON(len < 5);
246         }
247 }
248 EXPORT_SYMBOL(cpufreq_debug_printk);
249
250
251 module_param(debug, uint, 0644);
252 MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core,"
253                         " 2 to debug drivers, and 4 to debug governors.");
254
255 module_param(debug_ratelimit, uint, 0644);
256 MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:"
257                                         " set to 0 to disable ratelimiting.");
258
259 #else /* !CONFIG_CPU_FREQ_DEBUG */
260
261 static inline void cpufreq_debug_enable_ratelimit(void) { return; }
262 static inline void cpufreq_debug_disable_ratelimit(void) { return; }
263
264 #endif /* CONFIG_CPU_FREQ_DEBUG */
265
266
267 /*********************************************************************
268  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
269  *********************************************************************/
270
271 /**
272  * adjust_jiffies - adjust the system "loops_per_jiffy"
273  *
274  * This function alters the system "loops_per_jiffy" for the clock
275  * speed change. Note that loops_per_jiffy cannot be updated on SMP
276  * systems as each CPU might be scaled differently. So, use the arch
277  * per-CPU loops_per_jiffy value wherever possible.
278  */
279 #ifndef CONFIG_SMP
280 static unsigned long l_p_j_ref;
281 static unsigned int  l_p_j_ref_freq;
282
283 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
284 {
285         if (ci->flags & CPUFREQ_CONST_LOOPS)
286                 return;
287
288         if (!l_p_j_ref_freq) {
289                 l_p_j_ref = loops_per_jiffy;
290                 l_p_j_ref_freq = ci->old;
291                 dprintk("saving %lu as reference value for loops_per_jiffy; "
292                         "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
293         }
294         if ((val == CPUFREQ_PRECHANGE  && ci->old < ci->new) ||
295             (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) ||
296             (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
297                 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
298                                                                 ci->new);
299                 dprintk("scaling loops_per_jiffy to %lu "
300                         "for frequency %u kHz\n", loops_per_jiffy, ci->new);
301         }
302 }
303 #else
304 static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
305 {
306         return;
307 }
308 #endif
309
310
311 /**
312  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
313  * on frequency transition.
314  *
315  * This function calls the transition notifiers and the "adjust_jiffies"
316  * function. It is called twice on all CPU frequency changes that have
317  * external effects.
318  */
319 void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state)
320 {
321         struct cpufreq_policy *policy;
322
323         BUG_ON(irqs_disabled());
324
325         freqs->flags = cpufreq_driver->flags;
326         dprintk("notification %u of frequency transition to %u kHz\n",
327                 state, freqs->new);
328
329         policy = per_cpu(cpufreq_cpu_data, freqs->cpu);
330         switch (state) {
331
332         case CPUFREQ_PRECHANGE:
333                 /* detect if the driver reported a value as "old frequency"
334                  * which is not equal to what the cpufreq core thinks is
335                  * "old frequency".
336                  */
337                 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
338                         if ((policy) && (policy->cpu == freqs->cpu) &&
339                             (policy->cur) && (policy->cur != freqs->old)) {
340                                 dprintk("Warning: CPU frequency is"
341                                         " %u, cpufreq assumed %u kHz.\n",
342                                         freqs->old, policy->cur);
343                                 freqs->old = policy->cur;
344                         }
345                 }
346                 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
347                                 CPUFREQ_PRECHANGE, freqs);
348                 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
349                 break;
350
351         case CPUFREQ_POSTCHANGE:
352                 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
353                 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
354                                 CPUFREQ_POSTCHANGE, freqs);
355                 if (likely(policy) && likely(policy->cpu == freqs->cpu))
356                         policy->cur = freqs->new;
357                 break;
358         }
359 }
360 EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
361
362
363
364 /*********************************************************************
365  *                          SYSFS INTERFACE                          *
366  *********************************************************************/
367
368 static struct cpufreq_governor *__find_governor(const char *str_governor)
369 {
370         struct cpufreq_governor *t;
371
372         list_for_each_entry(t, &cpufreq_governor_list, governor_list)
373                 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
374                         return t;
375
376         return NULL;
377 }
378
379 /**
380  * cpufreq_parse_governor - parse a governor string
381  */
382 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
383                                 struct cpufreq_governor **governor)
384 {
385         int err = -EINVAL;
386
387         if (!cpufreq_driver)
388                 goto out;
389
390         if (cpufreq_driver->setpolicy) {
391                 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
392                         *policy = CPUFREQ_POLICY_PERFORMANCE;
393                         err = 0;
394                 } else if (!strnicmp(str_governor, "powersave",
395                                                 CPUFREQ_NAME_LEN)) {
396                         *policy = CPUFREQ_POLICY_POWERSAVE;
397                         err = 0;
398                 }
399         } else if (cpufreq_driver->target) {
400                 struct cpufreq_governor *t;
401
402                 mutex_lock(&cpufreq_governor_mutex);
403
404                 t = __find_governor(str_governor);
405
406                 if (t == NULL) {
407                         char *name = kasprintf(GFP_KERNEL, "cpufreq_%s",
408                                                                 str_governor);
409
410                         if (name) {
411                                 int ret;
412
413                                 mutex_unlock(&cpufreq_governor_mutex);
414                                 ret = request_module("%s", name);
415                                 mutex_lock(&cpufreq_governor_mutex);
416
417                                 if (ret == 0)
418                                         t = __find_governor(str_governor);
419                         }
420
421                         kfree(name);
422                 }
423
424                 if (t != NULL) {
425                         *governor = t;
426                         err = 0;
427                 }
428
429                 mutex_unlock(&cpufreq_governor_mutex);
430         }
431 out:
432         return err;
433 }
434
435
436 /**
437  * cpufreq_per_cpu_attr_read() / show_##file_name() -
438  * print out cpufreq information
439  *
440  * Write out information from cpufreq_driver->policy[cpu]; object must be
441  * "unsigned int".
442  */
443
444 #define show_one(file_name, object)                     \
445 static ssize_t show_##file_name                         \
446 (struct cpufreq_policy *policy, char *buf)              \
447 {                                                       \
448         return sprintf(buf, "%u\n", policy->object);    \
449 }
450
451 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
452 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
453 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
454 show_one(scaling_min_freq, min);
455 show_one(scaling_max_freq, max);
456 show_one(scaling_cur_freq, cur);
457
458 static int __cpufreq_set_policy(struct cpufreq_policy *data,
459                                 struct cpufreq_policy *policy);
460
461 /**
462  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
463  */
464 #define store_one(file_name, object)                    \
465 static ssize_t store_##file_name                                        \
466 (struct cpufreq_policy *policy, const char *buf, size_t count)          \
467 {                                                                       \
468         unsigned int ret = -EINVAL;                                     \
469         struct cpufreq_policy new_policy;                               \
470                                                                         \
471         ret = cpufreq_get_policy(&new_policy, policy->cpu);             \
472         if (ret)                                                        \
473                 return -EINVAL;                                         \
474                                                                         \
475         ret = sscanf(buf, "%u", &new_policy.object);                    \
476         if (ret != 1)                                                   \
477                 return -EINVAL;                                         \
478                                                                         \
479         ret = __cpufreq_set_policy(policy, &new_policy);                \
480         policy->user_policy.object = policy->object;                    \
481                                                                         \
482         return ret ? ret : count;                                       \
483 }
484
485 store_one(scaling_min_freq, min);
486 store_one(scaling_max_freq, max);
487
488 /**
489  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
490  */
491 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
492                                         char *buf)
493 {
494         unsigned int cur_freq = __cpufreq_get(policy->cpu);
495         if (!cur_freq)
496                 return sprintf(buf, "<unknown>");
497         return sprintf(buf, "%u\n", cur_freq);
498 }
499
500
501 /**
502  * show_scaling_governor - show the current policy for the specified CPU
503  */
504 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
505 {
506         if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
507                 return sprintf(buf, "powersave\n");
508         else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
509                 return sprintf(buf, "performance\n");
510         else if (policy->governor)
511                 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n",
512                                 policy->governor->name);
513         return -EINVAL;
514 }
515
516
517 /**
518  * store_scaling_governor - store policy for the specified CPU
519  */
520 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
521                                         const char *buf, size_t count)
522 {
523         unsigned int ret = -EINVAL;
524         char    str_governor[16];
525         struct cpufreq_policy new_policy;
526
527         ret = cpufreq_get_policy(&new_policy, policy->cpu);
528         if (ret)
529                 return ret;
530
531         ret = sscanf(buf, "%15s", str_governor);
532         if (ret != 1)
533                 return -EINVAL;
534
535         if (cpufreq_parse_governor(str_governor, &new_policy.policy,
536                                                 &new_policy.governor))
537                 return -EINVAL;
538
539         /* Do not use cpufreq_set_policy here or the user_policy.max
540            will be wrongly overridden */
541         ret = __cpufreq_set_policy(policy, &new_policy);
542
543         policy->user_policy.policy = policy->policy;
544         policy->user_policy.governor = policy->governor;
545
546         if (ret)
547                 return ret;
548         else
549                 return count;
550 }
551
552 /**
553  * show_scaling_driver - show the cpufreq driver currently loaded
554  */
555 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
556 {
557         return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name);
558 }
559
560 /**
561  * show_scaling_available_governors - show the available CPUfreq governors
562  */
563 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
564                                                 char *buf)
565 {
566         ssize_t i = 0;
567         struct cpufreq_governor *t;
568
569         if (!cpufreq_driver->target) {
570                 i += sprintf(buf, "performance powersave");
571                 goto out;
572         }
573
574         list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
575                 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
576                     - (CPUFREQ_NAME_LEN + 2)))
577                         goto out;
578                 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name);
579         }
580 out:
581         i += sprintf(&buf[i], "\n");
582         return i;
583 }
584
585 static ssize_t show_cpus(const struct cpumask *mask, char *buf)
586 {
587         ssize_t i = 0;
588         unsigned int cpu;
589
590         for_each_cpu(cpu, mask) {
591                 if (i)
592                         i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
593                 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
594                 if (i >= (PAGE_SIZE - 5))
595                         break;
596         }
597         i += sprintf(&buf[i], "\n");
598         return i;
599 }
600
601 /**
602  * show_related_cpus - show the CPUs affected by each transition even if
603  * hw coordination is in use
604  */
605 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
606 {
607         if (cpumask_empty(policy->related_cpus))
608                 return show_cpus(policy->cpus, buf);
609         return show_cpus(policy->related_cpus, buf);
610 }
611
612 /**
613  * show_affected_cpus - show the CPUs affected by each transition
614  */
615 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
616 {
617         return show_cpus(policy->cpus, buf);
618 }
619
620 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
621                                         const char *buf, size_t count)
622 {
623         unsigned int freq = 0;
624         unsigned int ret;
625
626         if (!policy->governor || !policy->governor->store_setspeed)
627                 return -EINVAL;
628
629         ret = sscanf(buf, "%u", &freq);
630         if (ret != 1)
631                 return -EINVAL;
632
633         policy->governor->store_setspeed(policy, freq);
634
635         return count;
636 }
637
638 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
639 {
640         if (!policy->governor || !policy->governor->show_setspeed)
641                 return sprintf(buf, "<unsupported>\n");
642
643         return policy->governor->show_setspeed(policy, buf);
644 }
645
646 /**
647  * show_scaling_driver - show the current cpufreq HW/BIOS limitation
648  */
649 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
650 {
651         unsigned int limit;
652         int ret;
653         if (cpufreq_driver->bios_limit) {
654                 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
655                 if (!ret)
656                         return sprintf(buf, "%u\n", limit);
657         }
658         return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
659 }
660
661 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
662 cpufreq_freq_attr_ro(cpuinfo_min_freq);
663 cpufreq_freq_attr_ro(cpuinfo_max_freq);
664 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
665 cpufreq_freq_attr_ro(scaling_available_governors);
666 cpufreq_freq_attr_ro(scaling_driver);
667 cpufreq_freq_attr_ro(scaling_cur_freq);
668 cpufreq_freq_attr_ro(bios_limit);
669 cpufreq_freq_attr_ro(related_cpus);
670 cpufreq_freq_attr_ro(affected_cpus);
671 cpufreq_freq_attr_rw(scaling_min_freq);
672 cpufreq_freq_attr_rw(scaling_max_freq);
673 cpufreq_freq_attr_rw(scaling_governor);
674 cpufreq_freq_attr_rw(scaling_setspeed);
675
676 static struct attribute *default_attrs[] = {
677         &cpuinfo_min_freq.attr,
678         &cpuinfo_max_freq.attr,
679         &cpuinfo_transition_latency.attr,
680         &scaling_min_freq.attr,
681         &scaling_max_freq.attr,
682         &affected_cpus.attr,
683         &related_cpus.attr,
684         &scaling_governor.attr,
685         &scaling_driver.attr,
686         &scaling_available_governors.attr,
687         &scaling_setspeed.attr,
688         NULL
689 };
690
691 struct kobject *cpufreq_global_kobject;
692 EXPORT_SYMBOL(cpufreq_global_kobject);
693
694 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
695 #define to_attr(a) container_of(a, struct freq_attr, attr)
696
697 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
698 {
699         struct cpufreq_policy *policy = to_policy(kobj);
700         struct freq_attr *fattr = to_attr(attr);
701         ssize_t ret = -EINVAL;
702         policy = cpufreq_cpu_get(policy->cpu);
703         if (!policy)
704                 goto no_policy;
705
706         if (lock_policy_rwsem_read(policy->cpu) < 0)
707                 goto fail;
708
709         if (fattr->show)
710                 ret = fattr->show(policy, buf);
711         else
712                 ret = -EIO;
713
714         unlock_policy_rwsem_read(policy->cpu);
715 fail:
716         cpufreq_cpu_put(policy);
717 no_policy:
718         return ret;
719 }
720
721 static ssize_t store(struct kobject *kobj, struct attribute *attr,
722                      const char *buf, size_t count)
723 {
724         struct cpufreq_policy *policy = to_policy(kobj);
725         struct freq_attr *fattr = to_attr(attr);
726         ssize_t ret = -EINVAL;
727         policy = cpufreq_cpu_get(policy->cpu);
728         if (!policy)
729                 goto no_policy;
730
731         if (lock_policy_rwsem_write(policy->cpu) < 0)
732                 goto fail;
733
734         if (fattr->store)
735                 ret = fattr->store(policy, buf, count);
736         else
737                 ret = -EIO;
738
739         unlock_policy_rwsem_write(policy->cpu);
740 fail:
741         cpufreq_cpu_put(policy);
742 no_policy:
743         return ret;
744 }
745
746 static void cpufreq_sysfs_release(struct kobject *kobj)
747 {
748         struct cpufreq_policy *policy = to_policy(kobj);
749         dprintk("last reference is dropped\n");
750         complete(&policy->kobj_unregister);
751 }
752
753 static const struct sysfs_ops sysfs_ops = {
754         .show   = show,
755         .store  = store,
756 };
757
758 static struct kobj_type ktype_cpufreq = {
759         .sysfs_ops      = &sysfs_ops,
760         .default_attrs  = default_attrs,
761         .release        = cpufreq_sysfs_release,
762 };
763
764 /*
765  * Returns:
766  *   Negative: Failure
767  *   0:        Success
768  *   Positive: When we have a managed CPU and the sysfs got symlinked
769  */
770 static int cpufreq_add_dev_policy(unsigned int cpu,
771                                   struct cpufreq_policy *policy,
772                                   struct sys_device *sys_dev)
773 {
774         int ret = 0;
775 #ifdef CONFIG_SMP
776         unsigned long flags;
777         unsigned int j;
778 #ifdef CONFIG_HOTPLUG_CPU
779         struct cpufreq_governor *gov;
780
781         gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
782         if (gov) {
783                 policy->governor = gov;
784                 dprintk("Restoring governor %s for cpu %d\n",
785                        policy->governor->name, cpu);
786         }
787 #endif
788
789         for_each_cpu(j, policy->cpus) {
790                 struct cpufreq_policy *managed_policy;
791
792                 if (cpu == j)
793                         continue;
794
795                 /* Check for existing affected CPUs.
796                  * They may not be aware of it due to CPU Hotplug.
797                  * cpufreq_cpu_put is called when the device is removed
798                  * in __cpufreq_remove_dev()
799                  */
800                 managed_policy = cpufreq_cpu_get(j);
801                 if (unlikely(managed_policy)) {
802
803                         /* Set proper policy_cpu */
804                         unlock_policy_rwsem_write(cpu);
805                         per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu;
806
807                         if (lock_policy_rwsem_write(cpu) < 0) {
808                                 /* Should not go through policy unlock path */
809                                 if (cpufreq_driver->exit)
810                                         cpufreq_driver->exit(policy);
811                                 cpufreq_cpu_put(managed_policy);
812                                 return -EBUSY;
813                         }
814
815                         spin_lock_irqsave(&cpufreq_driver_lock, flags);
816                         cpumask_copy(managed_policy->cpus, policy->cpus);
817                         per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
818                         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
819
820                         dprintk("CPU already managed, adding link\n");
821                         ret = sysfs_create_link(&sys_dev->kobj,
822                                                 &managed_policy->kobj,
823                                                 "cpufreq");
824                         if (ret)
825                                 cpufreq_cpu_put(managed_policy);
826                         /*
827                          * Success. We only needed to be added to the mask.
828                          * Call driver->exit() because only the cpu parent of
829                          * the kobj needed to call init().
830                          */
831                         if (cpufreq_driver->exit)
832                                 cpufreq_driver->exit(policy);
833
834                         if (!ret)
835                                 return 1;
836                         else
837                                 return ret;
838                 }
839         }
840 #endif
841         return ret;
842 }
843
844
845 /* symlink affected CPUs */
846 static int cpufreq_add_dev_symlink(unsigned int cpu,
847                                    struct cpufreq_policy *policy)
848 {
849         unsigned int j;
850         int ret = 0;
851
852         for_each_cpu(j, policy->cpus) {
853                 struct cpufreq_policy *managed_policy;
854                 struct sys_device *cpu_sys_dev;
855
856                 if (j == cpu)
857                         continue;
858                 if (!cpu_online(j))
859                         continue;
860
861                 dprintk("CPU %u already managed, adding link\n", j);
862                 managed_policy = cpufreq_cpu_get(cpu);
863                 cpu_sys_dev = get_cpu_sysdev(j);
864                 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
865                                         "cpufreq");
866                 if (ret) {
867                         cpufreq_cpu_put(managed_policy);
868                         return ret;
869                 }
870         }
871         return ret;
872 }
873
874 static int cpufreq_add_dev_interface(unsigned int cpu,
875                                      struct cpufreq_policy *policy,
876                                      struct sys_device *sys_dev)
877 {
878         struct cpufreq_policy new_policy;
879         struct freq_attr **drv_attr;
880         unsigned long flags;
881         int ret = 0;
882         unsigned int j;
883
884         /* prepare interface data */
885         ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
886                                    &sys_dev->kobj, "cpufreq");
887         if (ret)
888                 return ret;
889
890         /* set up files for this cpu device */
891         drv_attr = cpufreq_driver->attr;
892         while ((drv_attr) && (*drv_attr)) {
893                 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
894                 if (ret)
895                         goto err_out_kobj_put;
896                 drv_attr++;
897         }
898         if (cpufreq_driver->get) {
899                 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
900                 if (ret)
901                         goto err_out_kobj_put;
902         }
903         if (cpufreq_driver->target) {
904                 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
905                 if (ret)
906                         goto err_out_kobj_put;
907         }
908         if (cpufreq_driver->bios_limit) {
909                 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
910                 if (ret)
911                         goto err_out_kobj_put;
912         }
913
914         spin_lock_irqsave(&cpufreq_driver_lock, flags);
915         for_each_cpu(j, policy->cpus) {
916         if (!cpu_online(j))
917                 continue;
918                 per_cpu(cpufreq_cpu_data, j) = policy;
919                 per_cpu(cpufreq_policy_cpu, j) = policy->cpu;
920         }
921         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
922
923         ret = cpufreq_add_dev_symlink(cpu, policy);
924         if (ret)
925                 goto err_out_kobj_put;
926
927         memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
928         /* assure that the starting sequence is run in __cpufreq_set_policy */
929         policy->governor = NULL;
930
931         /* set default policy */
932         ret = __cpufreq_set_policy(policy, &new_policy);
933         policy->user_policy.policy = policy->policy;
934         policy->user_policy.governor = policy->governor;
935
936         if (ret) {
937                 dprintk("setting policy failed\n");
938                 if (cpufreq_driver->exit)
939                         cpufreq_driver->exit(policy);
940         }
941         return ret;
942
943 err_out_kobj_put:
944         kobject_put(&policy->kobj);
945         wait_for_completion(&policy->kobj_unregister);
946         return ret;
947 }
948
949
950 /**
951  * cpufreq_add_dev - add a CPU device
952  *
953  * Adds the cpufreq interface for a CPU device.
954  *
955  * The Oracle says: try running cpufreq registration/unregistration concurrently
956  * with with cpu hotplugging and all hell will break loose. Tried to clean this
957  * mess up, but more thorough testing is needed. - Mathieu
958  */
959 static int cpufreq_add_dev(struct sys_device *sys_dev)
960 {
961         unsigned int cpu = sys_dev->id;
962         int ret = 0, found = 0;
963         struct cpufreq_policy *policy;
964         unsigned long flags;
965         unsigned int j;
966 #ifdef CONFIG_HOTPLUG_CPU
967         int sibling;
968 #endif
969
970         if (cpu_is_offline(cpu))
971                 return 0;
972
973         cpufreq_debug_disable_ratelimit();
974         dprintk("adding CPU %u\n", cpu);
975
976 #ifdef CONFIG_SMP
977         /* check whether a different CPU already registered this
978          * CPU because it is in the same boat. */
979         policy = cpufreq_cpu_get(cpu);
980         if (unlikely(policy)) {
981                 cpufreq_cpu_put(policy);
982                 cpufreq_debug_enable_ratelimit();
983                 return 0;
984         }
985 #endif
986
987         if (!try_module_get(cpufreq_driver->owner)) {
988                 ret = -EINVAL;
989                 goto module_out;
990         }
991
992         ret = -ENOMEM;
993         policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL);
994         if (!policy)
995                 goto nomem_out;
996
997         if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
998                 goto err_free_policy;
999
1000         if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1001                 goto err_free_cpumask;
1002
1003         policy->cpu = cpu;
1004         cpumask_copy(policy->cpus, cpumask_of(cpu));
1005
1006         /* Initially set CPU itself as the policy_cpu */
1007         per_cpu(cpufreq_policy_cpu, cpu) = cpu;
1008         ret = (lock_policy_rwsem_write(cpu) < 0);
1009         WARN_ON(ret);
1010
1011         init_completion(&policy->kobj_unregister);
1012         INIT_WORK(&policy->update, handle_update);
1013
1014         /* Set governor before ->init, so that driver could check it */
1015 #ifdef CONFIG_HOTPLUG_CPU
1016         for_each_online_cpu(sibling) {
1017                 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling);
1018                 if (cp && cp->governor &&
1019                     (cpumask_test_cpu(cpu, cp->related_cpus))) {
1020                         policy->governor = cp->governor;
1021                         found = 1;
1022                         break;
1023                 }
1024         }
1025 #endif
1026         if (!found)
1027                 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1028         /* call driver. From then on the cpufreq must be able
1029          * to accept all calls to ->verify and ->setpolicy for this CPU
1030          */
1031         ret = cpufreq_driver->init(policy);
1032         if (ret) {
1033                 dprintk("initialization failed\n");
1034                 goto err_unlock_policy;
1035         }
1036         policy->user_policy.min = policy->min;
1037         policy->user_policy.max = policy->max;
1038
1039         blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1040                                      CPUFREQ_START, policy);
1041
1042         ret = cpufreq_add_dev_policy(cpu, policy, sys_dev);
1043         if (ret) {
1044                 if (ret > 0)
1045                         /* This is a managed cpu, symlink created,
1046                            exit with 0 */
1047                         ret = 0;
1048                 goto err_unlock_policy;
1049         }
1050
1051         ret = cpufreq_add_dev_interface(cpu, policy, sys_dev);
1052         if (ret)
1053                 goto err_out_unregister;
1054
1055         unlock_policy_rwsem_write(cpu);
1056
1057         kobject_uevent(&policy->kobj, KOBJ_ADD);
1058         module_put(cpufreq_driver->owner);
1059         dprintk("initialization complete\n");
1060         cpufreq_debug_enable_ratelimit();
1061
1062         return 0;
1063
1064
1065 err_out_unregister:
1066         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1067         for_each_cpu(j, policy->cpus)
1068                 per_cpu(cpufreq_cpu_data, j) = NULL;
1069         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1070
1071         kobject_put(&policy->kobj);
1072         wait_for_completion(&policy->kobj_unregister);
1073
1074 err_unlock_policy:
1075         unlock_policy_rwsem_write(cpu);
1076         free_cpumask_var(policy->related_cpus);
1077 err_free_cpumask:
1078         free_cpumask_var(policy->cpus);
1079 err_free_policy:
1080         kfree(policy);
1081 nomem_out:
1082         module_put(cpufreq_driver->owner);
1083 module_out:
1084         cpufreq_debug_enable_ratelimit();
1085         return ret;
1086 }
1087
1088
1089 /**
1090  * __cpufreq_remove_dev - remove a CPU device
1091  *
1092  * Removes the cpufreq interface for a CPU device.
1093  * Caller should already have policy_rwsem in write mode for this CPU.
1094  * This routine frees the rwsem before returning.
1095  */
1096 static int __cpufreq_remove_dev(struct sys_device *sys_dev)
1097 {
1098         unsigned int cpu = sys_dev->id;
1099         unsigned long flags;
1100         struct cpufreq_policy *data;
1101         struct kobject *kobj;
1102         struct completion *cmp;
1103 #ifdef CONFIG_SMP
1104         struct sys_device *cpu_sys_dev;
1105         unsigned int j;
1106 #endif
1107
1108         cpufreq_debug_disable_ratelimit();
1109         dprintk("unregistering CPU %u\n", cpu);
1110
1111         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1112         data = per_cpu(cpufreq_cpu_data, cpu);
1113
1114         if (!data) {
1115                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1116                 cpufreq_debug_enable_ratelimit();
1117                 unlock_policy_rwsem_write(cpu);
1118                 return -EINVAL;
1119         }
1120         per_cpu(cpufreq_cpu_data, cpu) = NULL;
1121
1122
1123 #ifdef CONFIG_SMP
1124         /* if this isn't the CPU which is the parent of the kobj, we
1125          * only need to unlink, put and exit
1126          */
1127         if (unlikely(cpu != data->cpu)) {
1128                 dprintk("removing link\n");
1129                 cpumask_clear_cpu(cpu, data->cpus);
1130                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1131                 kobj = &sys_dev->kobj;
1132                 cpufreq_cpu_put(data);
1133                 cpufreq_debug_enable_ratelimit();
1134                 unlock_policy_rwsem_write(cpu);
1135                 sysfs_remove_link(kobj, "cpufreq");
1136                 return 0;
1137         }
1138 #endif
1139
1140 #ifdef CONFIG_SMP
1141
1142 #ifdef CONFIG_HOTPLUG_CPU
1143         strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name,
1144                         CPUFREQ_NAME_LEN);
1145 #endif
1146
1147         /* if we have other CPUs still registered, we need to unlink them,
1148          * or else wait_for_completion below will lock up. Clean the
1149          * per_cpu(cpufreq_cpu_data) while holding the lock, and remove
1150          * the sysfs links afterwards.
1151          */
1152         if (unlikely(cpumask_weight(data->cpus) > 1)) {
1153                 for_each_cpu(j, data->cpus) {
1154                         if (j == cpu)
1155                                 continue;
1156                         per_cpu(cpufreq_cpu_data, j) = NULL;
1157                 }
1158         }
1159
1160         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1161
1162         if (unlikely(cpumask_weight(data->cpus) > 1)) {
1163                 for_each_cpu(j, data->cpus) {
1164                         if (j == cpu)
1165                                 continue;
1166                         dprintk("removing link for cpu %u\n", j);
1167 #ifdef CONFIG_HOTPLUG_CPU
1168                         strncpy(per_cpu(cpufreq_cpu_governor, j),
1169                                 data->governor->name, CPUFREQ_NAME_LEN);
1170 #endif
1171                         cpu_sys_dev = get_cpu_sysdev(j);
1172                         kobj = &cpu_sys_dev->kobj;
1173                         unlock_policy_rwsem_write(cpu);
1174                         sysfs_remove_link(kobj, "cpufreq");
1175                         lock_policy_rwsem_write(cpu);
1176                         cpufreq_cpu_put(data);
1177                 }
1178         }
1179 #else
1180         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1181 #endif
1182
1183         if (cpufreq_driver->target)
1184                 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1185
1186         kobj = &data->kobj;
1187         cmp = &data->kobj_unregister;
1188         unlock_policy_rwsem_write(cpu);
1189         kobject_put(kobj);
1190
1191         /* we need to make sure that the underlying kobj is actually
1192          * not referenced anymore by anybody before we proceed with
1193          * unloading.
1194          */
1195         dprintk("waiting for dropping of refcount\n");
1196         wait_for_completion(cmp);
1197         dprintk("wait complete\n");
1198
1199         lock_policy_rwsem_write(cpu);
1200         if (cpufreq_driver->exit)
1201                 cpufreq_driver->exit(data);
1202         unlock_policy_rwsem_write(cpu);
1203
1204         free_cpumask_var(data->related_cpus);
1205         free_cpumask_var(data->cpus);
1206         kfree(data);
1207         per_cpu(cpufreq_cpu_data, cpu) = NULL;
1208
1209         cpufreq_debug_enable_ratelimit();
1210         return 0;
1211 }
1212
1213
1214 static int cpufreq_remove_dev(struct sys_device *sys_dev)
1215 {
1216         unsigned int cpu = sys_dev->id;
1217         int retval;
1218
1219         if (cpu_is_offline(cpu))
1220                 return 0;
1221
1222         if (unlikely(lock_policy_rwsem_write(cpu)))
1223                 BUG();
1224
1225         retval = __cpufreq_remove_dev(sys_dev);
1226         return retval;
1227 }
1228
1229
1230 static void handle_update(struct work_struct *work)
1231 {
1232         struct cpufreq_policy *policy =
1233                 container_of(work, struct cpufreq_policy, update);
1234         unsigned int cpu = policy->cpu;
1235         dprintk("handle_update for cpu %u called\n", cpu);
1236         cpufreq_update_policy(cpu);
1237 }
1238
1239 /**
1240  *      cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
1241  *      @cpu: cpu number
1242  *      @old_freq: CPU frequency the kernel thinks the CPU runs at
1243  *      @new_freq: CPU frequency the CPU actually runs at
1244  *
1245  *      We adjust to current frequency first, and need to clean up later.
1246  *      So either call to cpufreq_update_policy() or schedule handle_update()).
1247  */
1248 static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1249                                 unsigned int new_freq)
1250 {
1251         struct cpufreq_freqs freqs;
1252
1253         dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
1254                "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1255
1256         freqs.cpu = cpu;
1257         freqs.old = old_freq;
1258         freqs.new = new_freq;
1259         cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
1260         cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
1261 }
1262
1263
1264 /**
1265  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1266  * @cpu: CPU number
1267  *
1268  * This is the last known freq, without actually getting it from the driver.
1269  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1270  */
1271 unsigned int cpufreq_quick_get(unsigned int cpu)
1272 {
1273         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1274         unsigned int ret_freq = 0;
1275
1276         if (policy) {
1277                 ret_freq = policy->cur;
1278                 cpufreq_cpu_put(policy);
1279         }
1280
1281         return ret_freq;
1282 }
1283 EXPORT_SYMBOL(cpufreq_quick_get);
1284
1285
1286 static unsigned int __cpufreq_get(unsigned int cpu)
1287 {
1288         struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1289         unsigned int ret_freq = 0;
1290
1291         if (!cpufreq_driver->get)
1292                 return ret_freq;
1293
1294         ret_freq = cpufreq_driver->get(cpu);
1295
1296         if (ret_freq && policy->cur &&
1297                 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1298                 /* verify no discrepancy between actual and
1299                                         saved value exists */
1300                 if (unlikely(ret_freq != policy->cur)) {
1301                         cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1302                         schedule_work(&policy->update);
1303                 }
1304         }
1305
1306         return ret_freq;
1307 }
1308
1309 /**
1310  * cpufreq_get - get the current CPU frequency (in kHz)
1311  * @cpu: CPU number
1312  *
1313  * Get the CPU current (static) CPU frequency
1314  */
1315 unsigned int cpufreq_get(unsigned int cpu)
1316 {
1317         unsigned int ret_freq = 0;
1318         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1319
1320         if (!policy)
1321                 goto out;
1322
1323         if (unlikely(lock_policy_rwsem_read(cpu)))
1324                 goto out_policy;
1325
1326         ret_freq = __cpufreq_get(cpu);
1327
1328         unlock_policy_rwsem_read(cpu);
1329
1330 out_policy:
1331         cpufreq_cpu_put(policy);
1332 out:
1333         return ret_freq;
1334 }
1335 EXPORT_SYMBOL(cpufreq_get);
1336
1337
1338 /**
1339  *      cpufreq_suspend - let the low level driver prepare for suspend
1340  */
1341
1342 static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg)
1343 {
1344         int ret = 0;
1345
1346         int cpu = sysdev->id;
1347         struct cpufreq_policy *cpu_policy;
1348
1349         dprintk("suspending cpu %u\n", cpu);
1350
1351         if (!cpu_online(cpu))
1352                 return 0;
1353
1354         /* we may be lax here as interrupts are off. Nonetheless
1355          * we need to grab the correct cpu policy, as to check
1356          * whether we really run on this CPU.
1357          */
1358
1359         cpu_policy = cpufreq_cpu_get(cpu);
1360         if (!cpu_policy)
1361                 return -EINVAL;
1362
1363         /* only handle each CPU group once */
1364         if (unlikely(cpu_policy->cpu != cpu))
1365                 goto out;
1366
1367         if (cpufreq_driver->suspend) {
1368                 ret = cpufreq_driver->suspend(cpu_policy, pmsg);
1369                 if (ret)
1370                         printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1371                                         "step on CPU %u\n", cpu_policy->cpu);
1372         }
1373
1374 out:
1375         cpufreq_cpu_put(cpu_policy);
1376         return ret;
1377 }
1378
1379 /**
1380  *      cpufreq_resume -  restore proper CPU frequency handling after resume
1381  *
1382  *      1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1383  *      2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1384  *          restored. It will verify that the current freq is in sync with
1385  *          what we believe it to be. This is a bit later than when it
1386  *          should be, but nonethteless it's better than calling
1387  *          cpufreq_driver->get() here which might re-enable interrupts...
1388  */
1389 static int cpufreq_resume(struct sys_device *sysdev)
1390 {
1391         int ret = 0;
1392
1393         int cpu = sysdev->id;
1394         struct cpufreq_policy *cpu_policy;
1395
1396         dprintk("resuming cpu %u\n", cpu);
1397
1398         if (!cpu_online(cpu))
1399                 return 0;
1400
1401         /* we may be lax here as interrupts are off. Nonetheless
1402          * we need to grab the correct cpu policy, as to check
1403          * whether we really run on this CPU.
1404          */
1405
1406         cpu_policy = cpufreq_cpu_get(cpu);
1407         if (!cpu_policy)
1408                 return -EINVAL;
1409
1410         /* only handle each CPU group once */
1411         if (unlikely(cpu_policy->cpu != cpu))
1412                 goto fail;
1413
1414         if (cpufreq_driver->resume) {
1415                 ret = cpufreq_driver->resume(cpu_policy);
1416                 if (ret) {
1417                         printk(KERN_ERR "cpufreq: resume failed in ->resume "
1418                                         "step on CPU %u\n", cpu_policy->cpu);
1419                         goto fail;
1420                 }
1421         }
1422
1423         schedule_work(&cpu_policy->update);
1424
1425 fail:
1426         cpufreq_cpu_put(cpu_policy);
1427         return ret;
1428 }
1429
1430 static struct sysdev_driver cpufreq_sysdev_driver = {
1431         .add            = cpufreq_add_dev,
1432         .remove         = cpufreq_remove_dev,
1433         .suspend        = cpufreq_suspend,
1434         .resume         = cpufreq_resume,
1435 };
1436
1437
1438 /*********************************************************************
1439  *                     NOTIFIER LISTS INTERFACE                      *
1440  *********************************************************************/
1441
1442 /**
1443  *      cpufreq_register_notifier - register a driver with cpufreq
1444  *      @nb: notifier function to register
1445  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1446  *
1447  *      Add a driver to one of two lists: either a list of drivers that
1448  *      are notified about clock rate changes (once before and once after
1449  *      the transition), or a list of drivers that are notified about
1450  *      changes in cpufreq policy.
1451  *
1452  *      This function may sleep, and has the same return conditions as
1453  *      blocking_notifier_chain_register.
1454  */
1455 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1456 {
1457         int ret;
1458
1459         WARN_ON(!init_cpufreq_transition_notifier_list_called);
1460
1461         switch (list) {
1462         case CPUFREQ_TRANSITION_NOTIFIER:
1463                 ret = srcu_notifier_chain_register(
1464                                 &cpufreq_transition_notifier_list, nb);
1465                 break;
1466         case CPUFREQ_POLICY_NOTIFIER:
1467                 ret = blocking_notifier_chain_register(
1468                                 &cpufreq_policy_notifier_list, nb);
1469                 break;
1470         default:
1471                 ret = -EINVAL;
1472         }
1473
1474         return ret;
1475 }
1476 EXPORT_SYMBOL(cpufreq_register_notifier);
1477
1478
1479 /**
1480  *      cpufreq_unregister_notifier - unregister a driver with cpufreq
1481  *      @nb: notifier block to be unregistered
1482  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1483  *
1484  *      Remove a driver from the CPU frequency notifier list.
1485  *
1486  *      This function may sleep, and has the same return conditions as
1487  *      blocking_notifier_chain_unregister.
1488  */
1489 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1490 {
1491         int ret;
1492
1493         switch (list) {
1494         case CPUFREQ_TRANSITION_NOTIFIER:
1495                 ret = srcu_notifier_chain_unregister(
1496                                 &cpufreq_transition_notifier_list, nb);
1497                 break;
1498         case CPUFREQ_POLICY_NOTIFIER:
1499                 ret = blocking_notifier_chain_unregister(
1500                                 &cpufreq_policy_notifier_list, nb);
1501                 break;
1502         default:
1503                 ret = -EINVAL;
1504         }
1505
1506         return ret;
1507 }
1508 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1509
1510
1511 /*********************************************************************
1512  *                              GOVERNORS                            *
1513  *********************************************************************/
1514
1515
1516 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1517                             unsigned int target_freq,
1518                             unsigned int relation)
1519 {
1520         int retval = -EINVAL;
1521
1522         dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
1523                 target_freq, relation);
1524         if (cpu_online(policy->cpu) && cpufreq_driver->target)
1525                 retval = cpufreq_driver->target(policy, target_freq, relation);
1526
1527         return retval;
1528 }
1529 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1530
1531 int cpufreq_driver_target(struct cpufreq_policy *policy,
1532                           unsigned int target_freq,
1533                           unsigned int relation)
1534 {
1535         int ret = -EINVAL;
1536
1537         policy = cpufreq_cpu_get(policy->cpu);
1538         if (!policy)
1539                 goto no_policy;
1540
1541         if (unlikely(lock_policy_rwsem_write(policy->cpu)))
1542                 goto fail;
1543
1544         ret = __cpufreq_driver_target(policy, target_freq, relation);
1545
1546         unlock_policy_rwsem_write(policy->cpu);
1547
1548 fail:
1549         cpufreq_cpu_put(policy);
1550 no_policy:
1551         return ret;
1552 }
1553 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1554
1555 int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu)
1556 {
1557         int ret = 0;
1558
1559         policy = cpufreq_cpu_get(policy->cpu);
1560         if (!policy)
1561                 return -EINVAL;
1562
1563         if (cpu_online(cpu) && cpufreq_driver->getavg)
1564                 ret = cpufreq_driver->getavg(policy, cpu);
1565
1566         cpufreq_cpu_put(policy);
1567         return ret;
1568 }
1569 EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
1570
1571 /*
1572  * when "event" is CPUFREQ_GOV_LIMITS
1573  */
1574
1575 static int __cpufreq_governor(struct cpufreq_policy *policy,
1576                                         unsigned int event)
1577 {
1578         int ret;
1579
1580         /* Only must be defined when default governor is known to have latency
1581            restrictions, like e.g. conservative or ondemand.
1582            That this is the case is already ensured in Kconfig
1583         */
1584 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1585         struct cpufreq_governor *gov = &cpufreq_gov_performance;
1586 #else
1587         struct cpufreq_governor *gov = NULL;
1588 #endif
1589
1590         if (policy->governor->max_transition_latency &&
1591             policy->cpuinfo.transition_latency >
1592             policy->governor->max_transition_latency) {
1593                 if (!gov)
1594                         return -EINVAL;
1595                 else {
1596                         printk(KERN_WARNING "%s governor failed, too long"
1597                                " transition latency of HW, fallback"
1598                                " to %s governor\n",
1599                                policy->governor->name,
1600                                gov->name);
1601                         policy->governor = gov;
1602                 }
1603         }
1604
1605         if (!try_module_get(policy->governor->owner))
1606                 return -EINVAL;
1607
1608         dprintk("__cpufreq_governor for CPU %u, event %u\n",
1609                                                 policy->cpu, event);
1610         ret = policy->governor->governor(policy, event);
1611
1612         /* we keep one module reference alive for
1613                         each CPU governed by this CPU */
1614         if ((event != CPUFREQ_GOV_START) || ret)
1615                 module_put(policy->governor->owner);
1616         if ((event == CPUFREQ_GOV_STOP) && !ret)
1617                 module_put(policy->governor->owner);
1618
1619         return ret;
1620 }
1621
1622
1623 int cpufreq_register_governor(struct cpufreq_governor *governor)
1624 {
1625         int err;
1626
1627         if (!governor)
1628                 return -EINVAL;
1629
1630         mutex_lock(&cpufreq_governor_mutex);
1631
1632         err = -EBUSY;
1633         if (__find_governor(governor->name) == NULL) {
1634                 err = 0;
1635                 list_add(&governor->governor_list, &cpufreq_governor_list);
1636         }
1637
1638         mutex_unlock(&cpufreq_governor_mutex);
1639         return err;
1640 }
1641 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1642
1643
1644 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1645 {
1646 #ifdef CONFIG_HOTPLUG_CPU
1647         int cpu;
1648 #endif
1649
1650         if (!governor)
1651                 return;
1652
1653 #ifdef CONFIG_HOTPLUG_CPU
1654         for_each_present_cpu(cpu) {
1655                 if (cpu_online(cpu))
1656                         continue;
1657                 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1658                         strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1659         }
1660 #endif
1661
1662         mutex_lock(&cpufreq_governor_mutex);
1663         list_del(&governor->governor_list);
1664         mutex_unlock(&cpufreq_governor_mutex);
1665         return;
1666 }
1667 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1668
1669
1670
1671 /*********************************************************************
1672  *                          POLICY INTERFACE                         *
1673  *********************************************************************/
1674
1675 /**
1676  * cpufreq_get_policy - get the current cpufreq_policy
1677  * @policy: struct cpufreq_policy into which the current cpufreq_policy
1678  *      is written
1679  *
1680  * Reads the current cpufreq policy.
1681  */
1682 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1683 {
1684         struct cpufreq_policy *cpu_policy;
1685         if (!policy)
1686                 return -EINVAL;
1687
1688         cpu_policy = cpufreq_cpu_get(cpu);
1689         if (!cpu_policy)
1690                 return -EINVAL;
1691
1692         memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
1693
1694         cpufreq_cpu_put(cpu_policy);
1695         return 0;
1696 }
1697 EXPORT_SYMBOL(cpufreq_get_policy);
1698
1699
1700 /*
1701  * data   : current policy.
1702  * policy : policy to be set.
1703  */
1704 static int __cpufreq_set_policy(struct cpufreq_policy *data,
1705                                 struct cpufreq_policy *policy)
1706 {
1707         int ret = 0;
1708
1709         cpufreq_debug_disable_ratelimit();
1710         dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
1711                 policy->min, policy->max);
1712
1713         memcpy(&policy->cpuinfo, &data->cpuinfo,
1714                                 sizeof(struct cpufreq_cpuinfo));
1715
1716         if (policy->min > data->max || policy->max < data->min) {
1717                 ret = -EINVAL;
1718                 goto error_out;
1719         }
1720
1721         /* verify the cpu speed can be set within this limit */
1722         ret = cpufreq_driver->verify(policy);
1723         if (ret)
1724                 goto error_out;
1725
1726         /* adjust if necessary - all reasons */
1727         blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1728                         CPUFREQ_ADJUST, policy);
1729
1730         /* adjust if necessary - hardware incompatibility*/
1731         blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1732                         CPUFREQ_INCOMPATIBLE, policy);
1733
1734         /* verify the cpu speed can be set within this limit,
1735            which might be different to the first one */
1736         ret = cpufreq_driver->verify(policy);
1737         if (ret)
1738                 goto error_out;
1739
1740         /* notification of the new policy */
1741         blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1742                         CPUFREQ_NOTIFY, policy);
1743
1744         data->min = policy->min;
1745         data->max = policy->max;
1746
1747         dprintk("new min and max freqs are %u - %u kHz\n",
1748                                         data->min, data->max);
1749
1750         if (cpufreq_driver->setpolicy) {
1751                 data->policy = policy->policy;
1752                 dprintk("setting range\n");
1753                 ret = cpufreq_driver->setpolicy(policy);
1754         } else {
1755                 if (policy->governor != data->governor) {
1756                         /* save old, working values */
1757                         struct cpufreq_governor *old_gov = data->governor;
1758
1759                         dprintk("governor switch\n");
1760
1761                         /* end old governor */
1762                         if (data->governor)
1763                                 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1764
1765                         /* start new governor */
1766                         data->governor = policy->governor;
1767                         if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
1768                                 /* new governor failed, so re-start old one */
1769                                 dprintk("starting governor %s failed\n",
1770                                                         data->governor->name);
1771                                 if (old_gov) {
1772                                         data->governor = old_gov;
1773                                         __cpufreq_governor(data,
1774                                                            CPUFREQ_GOV_START);
1775                                 }
1776                                 ret = -EINVAL;
1777                                 goto error_out;
1778                         }
1779                         /* might be a policy change, too, so fall through */
1780                 }
1781                 dprintk("governor: change or update limits\n");
1782                 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
1783         }
1784
1785 error_out:
1786         cpufreq_debug_enable_ratelimit();
1787         return ret;
1788 }
1789
1790 /**
1791  *      cpufreq_update_policy - re-evaluate an existing cpufreq policy
1792  *      @cpu: CPU which shall be re-evaluated
1793  *
1794  *      Usefull for policy notifiers which have different necessities
1795  *      at different times.
1796  */
1797 int cpufreq_update_policy(unsigned int cpu)
1798 {
1799         struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
1800         struct cpufreq_policy policy;
1801         int ret;
1802
1803         if (!data) {
1804                 ret = -ENODEV;
1805                 goto no_policy;
1806         }
1807
1808         if (unlikely(lock_policy_rwsem_write(cpu))) {
1809                 ret = -EINVAL;
1810                 goto fail;
1811         }
1812
1813         dprintk("updating policy for CPU %u\n", cpu);
1814         memcpy(&policy, data, sizeof(struct cpufreq_policy));
1815         policy.min = data->user_policy.min;
1816         policy.max = data->user_policy.max;
1817         policy.policy = data->user_policy.policy;
1818         policy.governor = data->user_policy.governor;
1819
1820         /* BIOS might change freq behind our back
1821           -> ask driver for current freq and notify governors about a change */
1822         if (cpufreq_driver->get) {
1823                 policy.cur = cpufreq_driver->get(cpu);
1824                 if (!data->cur) {
1825                         dprintk("Driver did not initialize current freq");
1826                         data->cur = policy.cur;
1827                 } else {
1828                         if (data->cur != policy.cur)
1829                                 cpufreq_out_of_sync(cpu, data->cur,
1830                                                                 policy.cur);
1831                 }
1832         }
1833
1834         ret = __cpufreq_set_policy(data, &policy);
1835
1836         unlock_policy_rwsem_write(cpu);
1837
1838 fail:
1839         cpufreq_cpu_put(data);
1840 no_policy:
1841         return ret;
1842 }
1843 EXPORT_SYMBOL(cpufreq_update_policy);
1844
1845 static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb,
1846                                         unsigned long action, void *hcpu)
1847 {
1848         unsigned int cpu = (unsigned long)hcpu;
1849         struct sys_device *sys_dev;
1850
1851         sys_dev = get_cpu_sysdev(cpu);
1852         if (sys_dev) {
1853                 switch (action) {
1854                 case CPU_ONLINE:
1855                 case CPU_ONLINE_FROZEN:
1856                         cpufreq_add_dev(sys_dev);
1857                         break;
1858                 case CPU_DOWN_PREPARE:
1859                 case CPU_DOWN_PREPARE_FROZEN:
1860                         if (unlikely(lock_policy_rwsem_write(cpu)))
1861                                 BUG();
1862
1863                         __cpufreq_remove_dev(sys_dev);
1864                         break;
1865                 case CPU_DOWN_FAILED:
1866                 case CPU_DOWN_FAILED_FROZEN:
1867                         cpufreq_add_dev(sys_dev);
1868                         break;
1869                 }
1870         }
1871         return NOTIFY_OK;
1872 }
1873
1874 static struct notifier_block __refdata cpufreq_cpu_notifier =
1875 {
1876     .notifier_call = cpufreq_cpu_callback,
1877 };
1878
1879 /*********************************************************************
1880  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
1881  *********************************************************************/
1882
1883 /**
1884  * cpufreq_register_driver - register a CPU Frequency driver
1885  * @driver_data: A struct cpufreq_driver containing the values#
1886  * submitted by the CPU Frequency driver.
1887  *
1888  *   Registers a CPU Frequency driver to this core code. This code
1889  * returns zero on success, -EBUSY when another driver got here first
1890  * (and isn't unregistered in the meantime).
1891  *
1892  */
1893 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1894 {
1895         unsigned long flags;
1896         int ret;
1897
1898         if (!driver_data || !driver_data->verify || !driver_data->init ||
1899             ((!driver_data->setpolicy) && (!driver_data->target)))
1900                 return -EINVAL;
1901
1902         dprintk("trying to register driver %s\n", driver_data->name);
1903
1904         if (driver_data->setpolicy)
1905                 driver_data->flags |= CPUFREQ_CONST_LOOPS;
1906
1907         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1908         if (cpufreq_driver) {
1909                 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1910                 return -EBUSY;
1911         }
1912         cpufreq_driver = driver_data;
1913         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1914
1915         ret = sysdev_driver_register(&cpu_sysdev_class,
1916                                         &cpufreq_sysdev_driver);
1917
1918         if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1919                 int i;
1920                 ret = -ENODEV;
1921
1922                 /* check for at least one working CPU */
1923                 for (i = 0; i < nr_cpu_ids; i++)
1924                         if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
1925                                 ret = 0;
1926                                 break;
1927                         }
1928
1929                 /* if all ->init() calls failed, unregister */
1930                 if (ret) {
1931                         dprintk("no CPU initialized for driver %s\n",
1932                                                         driver_data->name);
1933                         sysdev_driver_unregister(&cpu_sysdev_class,
1934                                                 &cpufreq_sysdev_driver);
1935
1936                         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1937                         cpufreq_driver = NULL;
1938                         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1939                 }
1940         }
1941
1942         if (!ret) {
1943                 register_hotcpu_notifier(&cpufreq_cpu_notifier);
1944                 dprintk("driver %s up and running\n", driver_data->name);
1945                 cpufreq_debug_enable_ratelimit();
1946         }
1947
1948         return ret;
1949 }
1950 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
1951
1952
1953 /**
1954  * cpufreq_unregister_driver - unregister the current CPUFreq driver
1955  *
1956  *    Unregister the current CPUFreq driver. Only call this if you have
1957  * the right to do so, i.e. if you have succeeded in initialising before!
1958  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
1959  * currently not initialised.
1960  */
1961 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1962 {
1963         unsigned long flags;
1964
1965         cpufreq_debug_disable_ratelimit();
1966
1967         if (!cpufreq_driver || (driver != cpufreq_driver)) {
1968                 cpufreq_debug_enable_ratelimit();
1969                 return -EINVAL;
1970         }
1971
1972         dprintk("unregistering driver %s\n", driver->name);
1973
1974         sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
1975         unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1976
1977         spin_lock_irqsave(&cpufreq_driver_lock, flags);
1978         cpufreq_driver = NULL;
1979         spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1980
1981         return 0;
1982 }
1983 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
1984
1985 static int __init cpufreq_core_init(void)
1986 {
1987         int cpu;
1988
1989         for_each_possible_cpu(cpu) {
1990                 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1991                 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
1992         }
1993
1994         cpufreq_global_kobject = kobject_create_and_add("cpufreq",
1995                                                 &cpu_sysdev_class.kset.kobj);
1996         BUG_ON(!cpufreq_global_kobject);
1997
1998         return 0;
1999 }
2000 core_initcall(cpufreq_core_init);