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