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