cpufreq: Support for fast frequency switching
[cascardo/linux.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  *            (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
7  *
8  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9  *      Added handling for CPU hotplug
10  *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11  *      Fix handling for CPU hotplug -- affected CPUs
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  */
17
18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20 #include <linux/cpu.h>
21 #include <linux/cpufreq.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
24 #include <linux/init.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/module.h>
27 #include <linux/mutex.h>
28 #include <linux/slab.h>
29 #include <linux/suspend.h>
30 #include <linux/syscore_ops.h>
31 #include <linux/tick.h>
32 #include <trace/events/power.h>
33
34 static LIST_HEAD(cpufreq_policy_list);
35
36 static inline bool policy_is_inactive(struct cpufreq_policy *policy)
37 {
38         return cpumask_empty(policy->cpus);
39 }
40
41 /* Macros to iterate over CPU policies */
42 #define for_each_suitable_policy(__policy, __active)                     \
43         list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
44                 if ((__active) == !policy_is_inactive(__policy))
45
46 #define for_each_active_policy(__policy)                \
47         for_each_suitable_policy(__policy, true)
48 #define for_each_inactive_policy(__policy)              \
49         for_each_suitable_policy(__policy, false)
50
51 #define for_each_policy(__policy)                       \
52         list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
53
54 /* Iterate over governors */
55 static LIST_HEAD(cpufreq_governor_list);
56 #define for_each_governor(__governor)                           \
57         list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
58
59 /**
60  * The "cpufreq driver" - the arch- or hardware-dependent low
61  * level driver of CPUFreq support, and its spinlock. This lock
62  * also protects the cpufreq_cpu_data array.
63  */
64 static struct cpufreq_driver *cpufreq_driver;
65 static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
66 static DEFINE_RWLOCK(cpufreq_driver_lock);
67
68 /* Flag to suspend/resume CPUFreq governors */
69 static bool cpufreq_suspended;
70
71 static inline bool has_target(void)
72 {
73         return cpufreq_driver->target_index || cpufreq_driver->target;
74 }
75
76 /* internal prototypes */
77 static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
78 static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
79 static int cpufreq_start_governor(struct cpufreq_policy *policy);
80 static int cpufreq_exit_governor(struct cpufreq_policy *policy);
81
82 /**
83  * Two notifier lists: the "policy" list is involved in the
84  * validation process for a new CPU frequency policy; the
85  * "transition" list for kernel code that needs to handle
86  * changes to devices when the CPU clock speed changes.
87  * The mutex locks both lists.
88  */
89 static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
90 static struct srcu_notifier_head cpufreq_transition_notifier_list;
91
92 static bool init_cpufreq_transition_notifier_list_called;
93 static int __init init_cpufreq_transition_notifier_list(void)
94 {
95         srcu_init_notifier_head(&cpufreq_transition_notifier_list);
96         init_cpufreq_transition_notifier_list_called = true;
97         return 0;
98 }
99 pure_initcall(init_cpufreq_transition_notifier_list);
100
101 static int off __read_mostly;
102 static int cpufreq_disabled(void)
103 {
104         return off;
105 }
106 void disable_cpufreq(void)
107 {
108         off = 1;
109 }
110 static DEFINE_MUTEX(cpufreq_governor_mutex);
111
112 bool have_governor_per_policy(void)
113 {
114         return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
115 }
116 EXPORT_SYMBOL_GPL(have_governor_per_policy);
117
118 struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
119 {
120         if (have_governor_per_policy())
121                 return &policy->kobj;
122         else
123                 return cpufreq_global_kobject;
124 }
125 EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
126
127 struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
128 {
129         struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
130
131         return policy && !policy_is_inactive(policy) ?
132                 policy->freq_table : NULL;
133 }
134 EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
135
136 static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
137 {
138         u64 idle_time;
139         u64 cur_wall_time;
140         u64 busy_time;
141
142         cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
143
144         busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
145         busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
146         busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
147         busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
148         busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
149         busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
150
151         idle_time = cur_wall_time - busy_time;
152         if (wall)
153                 *wall = cputime_to_usecs(cur_wall_time);
154
155         return cputime_to_usecs(idle_time);
156 }
157
158 u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
159 {
160         u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
161
162         if (idle_time == -1ULL)
163                 return get_cpu_idle_time_jiffy(cpu, wall);
164         else if (!io_busy)
165                 idle_time += get_cpu_iowait_time_us(cpu, wall);
166
167         return idle_time;
168 }
169 EXPORT_SYMBOL_GPL(get_cpu_idle_time);
170
171 /*
172  * This is a generic cpufreq init() routine which can be used by cpufreq
173  * drivers of SMP systems. It will do following:
174  * - validate & show freq table passed
175  * - set policies transition latency
176  * - policy->cpus with all possible CPUs
177  */
178 int cpufreq_generic_init(struct cpufreq_policy *policy,
179                 struct cpufreq_frequency_table *table,
180                 unsigned int transition_latency)
181 {
182         int ret;
183
184         ret = cpufreq_table_validate_and_show(policy, table);
185         if (ret) {
186                 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
187                 return ret;
188         }
189
190         policy->cpuinfo.transition_latency = transition_latency;
191
192         /*
193          * The driver only supports the SMP configuration where all processors
194          * share the clock and voltage and clock.
195          */
196         cpumask_setall(policy->cpus);
197
198         return 0;
199 }
200 EXPORT_SYMBOL_GPL(cpufreq_generic_init);
201
202 struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
203 {
204         struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
205
206         return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
207 }
208 EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
209
210 unsigned int cpufreq_generic_get(unsigned int cpu)
211 {
212         struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
213
214         if (!policy || IS_ERR(policy->clk)) {
215                 pr_err("%s: No %s associated to cpu: %d\n",
216                        __func__, policy ? "clk" : "policy", cpu);
217                 return 0;
218         }
219
220         return clk_get_rate(policy->clk) / 1000;
221 }
222 EXPORT_SYMBOL_GPL(cpufreq_generic_get);
223
224 /**
225  * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
226  *
227  * @cpu: cpu to find policy for.
228  *
229  * This returns policy for 'cpu', returns NULL if it doesn't exist.
230  * It also increments the kobject reference count to mark it busy and so would
231  * require a corresponding call to cpufreq_cpu_put() to decrement it back.
232  * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
233  * freed as that depends on the kobj count.
234  *
235  * Return: A valid policy on success, otherwise NULL on failure.
236  */
237 struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
238 {
239         struct cpufreq_policy *policy = NULL;
240         unsigned long flags;
241
242         if (WARN_ON(cpu >= nr_cpu_ids))
243                 return NULL;
244
245         /* get the cpufreq driver */
246         read_lock_irqsave(&cpufreq_driver_lock, flags);
247
248         if (cpufreq_driver) {
249                 /* get the CPU */
250                 policy = cpufreq_cpu_get_raw(cpu);
251                 if (policy)
252                         kobject_get(&policy->kobj);
253         }
254
255         read_unlock_irqrestore(&cpufreq_driver_lock, flags);
256
257         return policy;
258 }
259 EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
260
261 /**
262  * cpufreq_cpu_put: Decrements the usage count of a policy
263  *
264  * @policy: policy earlier returned by cpufreq_cpu_get().
265  *
266  * This decrements the kobject reference count incremented earlier by calling
267  * cpufreq_cpu_get().
268  */
269 void cpufreq_cpu_put(struct cpufreq_policy *policy)
270 {
271         kobject_put(&policy->kobj);
272 }
273 EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
274
275 /*********************************************************************
276  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
277  *********************************************************************/
278
279 /**
280  * adjust_jiffies - adjust the system "loops_per_jiffy"
281  *
282  * This function alters the system "loops_per_jiffy" for the clock
283  * speed change. Note that loops_per_jiffy cannot be updated on SMP
284  * systems as each CPU might be scaled differently. So, use the arch
285  * per-CPU loops_per_jiffy value wherever possible.
286  */
287 static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
288 {
289 #ifndef CONFIG_SMP
290         static unsigned long l_p_j_ref;
291         static unsigned int l_p_j_ref_freq;
292
293         if (ci->flags & CPUFREQ_CONST_LOOPS)
294                 return;
295
296         if (!l_p_j_ref_freq) {
297                 l_p_j_ref = loops_per_jiffy;
298                 l_p_j_ref_freq = ci->old;
299                 pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
300                          l_p_j_ref, l_p_j_ref_freq);
301         }
302         if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
303                 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
304                                                                 ci->new);
305                 pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
306                          loops_per_jiffy, ci->new);
307         }
308 #endif
309 }
310
311 static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
312                 struct cpufreq_freqs *freqs, unsigned int state)
313 {
314         BUG_ON(irqs_disabled());
315
316         if (cpufreq_disabled())
317                 return;
318
319         freqs->flags = cpufreq_driver->flags;
320         pr_debug("notification %u of frequency transition to %u kHz\n",
321                  state, freqs->new);
322
323         switch (state) {
324
325         case CPUFREQ_PRECHANGE:
326                 /* detect if the driver reported a value as "old frequency"
327                  * which is not equal to what the cpufreq core thinks is
328                  * "old frequency".
329                  */
330                 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
331                         if ((policy) && (policy->cpu == freqs->cpu) &&
332                             (policy->cur) && (policy->cur != freqs->old)) {
333                                 pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
334                                          freqs->old, policy->cur);
335                                 freqs->old = policy->cur;
336                         }
337                 }
338                 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
339                                 CPUFREQ_PRECHANGE, freqs);
340                 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
341                 break;
342
343         case CPUFREQ_POSTCHANGE:
344                 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
345                 pr_debug("FREQ: %lu - CPU: %lu\n",
346                          (unsigned long)freqs->new, (unsigned long)freqs->cpu);
347                 trace_cpu_frequency(freqs->new, freqs->cpu);
348                 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
349                                 CPUFREQ_POSTCHANGE, freqs);
350                 if (likely(policy) && likely(policy->cpu == freqs->cpu))
351                         policy->cur = freqs->new;
352                 break;
353         }
354 }
355
356 /**
357  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
358  * on frequency transition.
359  *
360  * This function calls the transition notifiers and the "adjust_jiffies"
361  * function. It is called twice on all CPU frequency changes that have
362  * external effects.
363  */
364 static void cpufreq_notify_transition(struct cpufreq_policy *policy,
365                 struct cpufreq_freqs *freqs, unsigned int state)
366 {
367         for_each_cpu(freqs->cpu, policy->cpus)
368                 __cpufreq_notify_transition(policy, freqs, state);
369 }
370
371 /* Do post notifications when there are chances that transition has failed */
372 static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
373                 struct cpufreq_freqs *freqs, int transition_failed)
374 {
375         cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
376         if (!transition_failed)
377                 return;
378
379         swap(freqs->old, freqs->new);
380         cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
381         cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
382 }
383
384 void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
385                 struct cpufreq_freqs *freqs)
386 {
387
388         /*
389          * Catch double invocations of _begin() which lead to self-deadlock.
390          * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
391          * doesn't invoke _begin() on their behalf, and hence the chances of
392          * double invocations are very low. Moreover, there are scenarios
393          * where these checks can emit false-positive warnings in these
394          * drivers; so we avoid that by skipping them altogether.
395          */
396         WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
397                                 && current == policy->transition_task);
398
399 wait:
400         wait_event(policy->transition_wait, !policy->transition_ongoing);
401
402         spin_lock(&policy->transition_lock);
403
404         if (unlikely(policy->transition_ongoing)) {
405                 spin_unlock(&policy->transition_lock);
406                 goto wait;
407         }
408
409         policy->transition_ongoing = true;
410         policy->transition_task = current;
411
412         spin_unlock(&policy->transition_lock);
413
414         cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
415 }
416 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
417
418 void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
419                 struct cpufreq_freqs *freqs, int transition_failed)
420 {
421         if (unlikely(WARN_ON(!policy->transition_ongoing)))
422                 return;
423
424         cpufreq_notify_post_transition(policy, freqs, transition_failed);
425
426         policy->transition_ongoing = false;
427         policy->transition_task = NULL;
428
429         wake_up(&policy->transition_wait);
430 }
431 EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
432
433 /*
434  * Fast frequency switching status count.  Positive means "enabled", negative
435  * means "disabled" and 0 means "not decided yet".
436  */
437 static int cpufreq_fast_switch_count;
438 static DEFINE_MUTEX(cpufreq_fast_switch_lock);
439
440 static void cpufreq_list_transition_notifiers(void)
441 {
442         struct notifier_block *nb;
443
444         pr_info("Registered transition notifiers:\n");
445
446         mutex_lock(&cpufreq_transition_notifier_list.mutex);
447
448         for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
449                 pr_info("%pF\n", nb->notifier_call);
450
451         mutex_unlock(&cpufreq_transition_notifier_list.mutex);
452 }
453
454 /**
455  * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
456  * @policy: cpufreq policy to enable fast frequency switching for.
457  *
458  * Try to enable fast frequency switching for @policy.
459  *
460  * The attempt will fail if there is at least one transition notifier registered
461  * at this point, as fast frequency switching is quite fundamentally at odds
462  * with transition notifiers.  Thus if successful, it will make registration of
463  * transition notifiers fail going forward.
464  */
465 void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
466 {
467         lockdep_assert_held(&policy->rwsem);
468
469         if (!policy->fast_switch_possible)
470                 return;
471
472         mutex_lock(&cpufreq_fast_switch_lock);
473         if (cpufreq_fast_switch_count >= 0) {
474                 cpufreq_fast_switch_count++;
475                 policy->fast_switch_enabled = true;
476         } else {
477                 pr_warn("CPU%u: Fast frequency switching not enabled\n",
478                         policy->cpu);
479                 cpufreq_list_transition_notifiers();
480         }
481         mutex_unlock(&cpufreq_fast_switch_lock);
482 }
483 EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
484
485 static void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
486 {
487         mutex_lock(&cpufreq_fast_switch_lock);
488         if (policy->fast_switch_enabled) {
489                 policy->fast_switch_enabled = false;
490                 if (!WARN_ON(cpufreq_fast_switch_count <= 0))
491                         cpufreq_fast_switch_count--;
492         }
493         mutex_unlock(&cpufreq_fast_switch_lock);
494 }
495
496 /*********************************************************************
497  *                          SYSFS INTERFACE                          *
498  *********************************************************************/
499 static ssize_t show_boost(struct kobject *kobj,
500                                  struct attribute *attr, char *buf)
501 {
502         return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
503 }
504
505 static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
506                                   const char *buf, size_t count)
507 {
508         int ret, enable;
509
510         ret = sscanf(buf, "%d", &enable);
511         if (ret != 1 || enable < 0 || enable > 1)
512                 return -EINVAL;
513
514         if (cpufreq_boost_trigger_state(enable)) {
515                 pr_err("%s: Cannot %s BOOST!\n",
516                        __func__, enable ? "enable" : "disable");
517                 return -EINVAL;
518         }
519
520         pr_debug("%s: cpufreq BOOST %s\n",
521                  __func__, enable ? "enabled" : "disabled");
522
523         return count;
524 }
525 define_one_global_rw(boost);
526
527 static struct cpufreq_governor *find_governor(const char *str_governor)
528 {
529         struct cpufreq_governor *t;
530
531         for_each_governor(t)
532                 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
533                         return t;
534
535         return NULL;
536 }
537
538 /**
539  * cpufreq_parse_governor - parse a governor string
540  */
541 static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
542                                 struct cpufreq_governor **governor)
543 {
544         int err = -EINVAL;
545
546         if (cpufreq_driver->setpolicy) {
547                 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
548                         *policy = CPUFREQ_POLICY_PERFORMANCE;
549                         err = 0;
550                 } else if (!strncasecmp(str_governor, "powersave",
551                                                 CPUFREQ_NAME_LEN)) {
552                         *policy = CPUFREQ_POLICY_POWERSAVE;
553                         err = 0;
554                 }
555         } else {
556                 struct cpufreq_governor *t;
557
558                 mutex_lock(&cpufreq_governor_mutex);
559
560                 t = find_governor(str_governor);
561
562                 if (t == NULL) {
563                         int ret;
564
565                         mutex_unlock(&cpufreq_governor_mutex);
566                         ret = request_module("cpufreq_%s", str_governor);
567                         mutex_lock(&cpufreq_governor_mutex);
568
569                         if (ret == 0)
570                                 t = find_governor(str_governor);
571                 }
572
573                 if (t != NULL) {
574                         *governor = t;
575                         err = 0;
576                 }
577
578                 mutex_unlock(&cpufreq_governor_mutex);
579         }
580         return err;
581 }
582
583 /**
584  * cpufreq_per_cpu_attr_read() / show_##file_name() -
585  * print out cpufreq information
586  *
587  * Write out information from cpufreq_driver->policy[cpu]; object must be
588  * "unsigned int".
589  */
590
591 #define show_one(file_name, object)                     \
592 static ssize_t show_##file_name                         \
593 (struct cpufreq_policy *policy, char *buf)              \
594 {                                                       \
595         return sprintf(buf, "%u\n", policy->object);    \
596 }
597
598 show_one(cpuinfo_min_freq, cpuinfo.min_freq);
599 show_one(cpuinfo_max_freq, cpuinfo.max_freq);
600 show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
601 show_one(scaling_min_freq, min);
602 show_one(scaling_max_freq, max);
603
604 static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
605 {
606         ssize_t ret;
607
608         if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
609                 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
610         else
611                 ret = sprintf(buf, "%u\n", policy->cur);
612         return ret;
613 }
614
615 static int cpufreq_set_policy(struct cpufreq_policy *policy,
616                                 struct cpufreq_policy *new_policy);
617
618 /**
619  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
620  */
621 #define store_one(file_name, object)                    \
622 static ssize_t store_##file_name                                        \
623 (struct cpufreq_policy *policy, const char *buf, size_t count)          \
624 {                                                                       \
625         int ret, temp;                                                  \
626         struct cpufreq_policy new_policy;                               \
627                                                                         \
628         memcpy(&new_policy, policy, sizeof(*policy));                   \
629                                                                         \
630         ret = sscanf(buf, "%u", &new_policy.object);                    \
631         if (ret != 1)                                                   \
632                 return -EINVAL;                                         \
633                                                                         \
634         temp = new_policy.object;                                       \
635         ret = cpufreq_set_policy(policy, &new_policy);          \
636         if (!ret)                                                       \
637                 policy->user_policy.object = temp;                      \
638                                                                         \
639         return ret ? ret : count;                                       \
640 }
641
642 store_one(scaling_min_freq, min);
643 store_one(scaling_max_freq, max);
644
645 /**
646  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
647  */
648 static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
649                                         char *buf)
650 {
651         unsigned int cur_freq = __cpufreq_get(policy);
652         if (!cur_freq)
653                 return sprintf(buf, "<unknown>");
654         return sprintf(buf, "%u\n", cur_freq);
655 }
656
657 /**
658  * show_scaling_governor - show the current policy for the specified CPU
659  */
660 static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
661 {
662         if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
663                 return sprintf(buf, "powersave\n");
664         else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
665                 return sprintf(buf, "performance\n");
666         else if (policy->governor)
667                 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
668                                 policy->governor->name);
669         return -EINVAL;
670 }
671
672 /**
673  * store_scaling_governor - store policy for the specified CPU
674  */
675 static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
676                                         const char *buf, size_t count)
677 {
678         int ret;
679         char    str_governor[16];
680         struct cpufreq_policy new_policy;
681
682         memcpy(&new_policy, policy, sizeof(*policy));
683
684         ret = sscanf(buf, "%15s", str_governor);
685         if (ret != 1)
686                 return -EINVAL;
687
688         if (cpufreq_parse_governor(str_governor, &new_policy.policy,
689                                                 &new_policy.governor))
690                 return -EINVAL;
691
692         ret = cpufreq_set_policy(policy, &new_policy);
693         return ret ? ret : count;
694 }
695
696 /**
697  * show_scaling_driver - show the cpufreq driver currently loaded
698  */
699 static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
700 {
701         return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
702 }
703
704 /**
705  * show_scaling_available_governors - show the available CPUfreq governors
706  */
707 static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
708                                                 char *buf)
709 {
710         ssize_t i = 0;
711         struct cpufreq_governor *t;
712
713         if (!has_target()) {
714                 i += sprintf(buf, "performance powersave");
715                 goto out;
716         }
717
718         for_each_governor(t) {
719                 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
720                     - (CPUFREQ_NAME_LEN + 2)))
721                         goto out;
722                 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
723         }
724 out:
725         i += sprintf(&buf[i], "\n");
726         return i;
727 }
728
729 ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
730 {
731         ssize_t i = 0;
732         unsigned int cpu;
733
734         for_each_cpu(cpu, mask) {
735                 if (i)
736                         i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
737                 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
738                 if (i >= (PAGE_SIZE - 5))
739                         break;
740         }
741         i += sprintf(&buf[i], "\n");
742         return i;
743 }
744 EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
745
746 /**
747  * show_related_cpus - show the CPUs affected by each transition even if
748  * hw coordination is in use
749  */
750 static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
751 {
752         return cpufreq_show_cpus(policy->related_cpus, buf);
753 }
754
755 /**
756  * show_affected_cpus - show the CPUs affected by each transition
757  */
758 static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
759 {
760         return cpufreq_show_cpus(policy->cpus, buf);
761 }
762
763 static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
764                                         const char *buf, size_t count)
765 {
766         unsigned int freq = 0;
767         unsigned int ret;
768
769         if (!policy->governor || !policy->governor->store_setspeed)
770                 return -EINVAL;
771
772         ret = sscanf(buf, "%u", &freq);
773         if (ret != 1)
774                 return -EINVAL;
775
776         policy->governor->store_setspeed(policy, freq);
777
778         return count;
779 }
780
781 static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
782 {
783         if (!policy->governor || !policy->governor->show_setspeed)
784                 return sprintf(buf, "<unsupported>\n");
785
786         return policy->governor->show_setspeed(policy, buf);
787 }
788
789 /**
790  * show_bios_limit - show the current cpufreq HW/BIOS limitation
791  */
792 static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
793 {
794         unsigned int limit;
795         int ret;
796         if (cpufreq_driver->bios_limit) {
797                 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
798                 if (!ret)
799                         return sprintf(buf, "%u\n", limit);
800         }
801         return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
802 }
803
804 cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
805 cpufreq_freq_attr_ro(cpuinfo_min_freq);
806 cpufreq_freq_attr_ro(cpuinfo_max_freq);
807 cpufreq_freq_attr_ro(cpuinfo_transition_latency);
808 cpufreq_freq_attr_ro(scaling_available_governors);
809 cpufreq_freq_attr_ro(scaling_driver);
810 cpufreq_freq_attr_ro(scaling_cur_freq);
811 cpufreq_freq_attr_ro(bios_limit);
812 cpufreq_freq_attr_ro(related_cpus);
813 cpufreq_freq_attr_ro(affected_cpus);
814 cpufreq_freq_attr_rw(scaling_min_freq);
815 cpufreq_freq_attr_rw(scaling_max_freq);
816 cpufreq_freq_attr_rw(scaling_governor);
817 cpufreq_freq_attr_rw(scaling_setspeed);
818
819 static struct attribute *default_attrs[] = {
820         &cpuinfo_min_freq.attr,
821         &cpuinfo_max_freq.attr,
822         &cpuinfo_transition_latency.attr,
823         &scaling_min_freq.attr,
824         &scaling_max_freq.attr,
825         &affected_cpus.attr,
826         &related_cpus.attr,
827         &scaling_governor.attr,
828         &scaling_driver.attr,
829         &scaling_available_governors.attr,
830         &scaling_setspeed.attr,
831         NULL
832 };
833
834 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
835 #define to_attr(a) container_of(a, struct freq_attr, attr)
836
837 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
838 {
839         struct cpufreq_policy *policy = to_policy(kobj);
840         struct freq_attr *fattr = to_attr(attr);
841         ssize_t ret;
842
843         down_read(&policy->rwsem);
844         ret = fattr->show(policy, buf);
845         up_read(&policy->rwsem);
846
847         return ret;
848 }
849
850 static ssize_t store(struct kobject *kobj, struct attribute *attr,
851                      const char *buf, size_t count)
852 {
853         struct cpufreq_policy *policy = to_policy(kobj);
854         struct freq_attr *fattr = to_attr(attr);
855         ssize_t ret = -EINVAL;
856
857         get_online_cpus();
858
859         if (cpu_online(policy->cpu)) {
860                 down_write(&policy->rwsem);
861                 ret = fattr->store(policy, buf, count);
862                 up_write(&policy->rwsem);
863         }
864
865         put_online_cpus();
866
867         return ret;
868 }
869
870 static void cpufreq_sysfs_release(struct kobject *kobj)
871 {
872         struct cpufreq_policy *policy = to_policy(kobj);
873         pr_debug("last reference is dropped\n");
874         complete(&policy->kobj_unregister);
875 }
876
877 static const struct sysfs_ops sysfs_ops = {
878         .show   = show,
879         .store  = store,
880 };
881
882 static struct kobj_type ktype_cpufreq = {
883         .sysfs_ops      = &sysfs_ops,
884         .default_attrs  = default_attrs,
885         .release        = cpufreq_sysfs_release,
886 };
887
888 static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
889 {
890         struct device *cpu_dev;
891
892         pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu);
893
894         if (!policy)
895                 return 0;
896
897         cpu_dev = get_cpu_device(cpu);
898         if (WARN_ON(!cpu_dev))
899                 return 0;
900
901         return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq");
902 }
903
904 static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
905 {
906         struct device *cpu_dev;
907
908         pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu);
909
910         cpu_dev = get_cpu_device(cpu);
911         if (WARN_ON(!cpu_dev))
912                 return;
913
914         sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
915 }
916
917 /* Add/remove symlinks for all related CPUs */
918 static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
919 {
920         unsigned int j;
921         int ret = 0;
922
923         /* Some related CPUs might not be present (physically hotplugged) */
924         for_each_cpu(j, policy->real_cpus) {
925                 ret = add_cpu_dev_symlink(policy, j);
926                 if (ret)
927                         break;
928         }
929
930         return ret;
931 }
932
933 static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy)
934 {
935         unsigned int j;
936
937         /* Some related CPUs might not be present (physically hotplugged) */
938         for_each_cpu(j, policy->real_cpus)
939                 remove_cpu_dev_symlink(policy, j);
940 }
941
942 static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
943 {
944         struct freq_attr **drv_attr;
945         int ret = 0;
946
947         /* set up files for this cpu device */
948         drv_attr = cpufreq_driver->attr;
949         while (drv_attr && *drv_attr) {
950                 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
951                 if (ret)
952                         return ret;
953                 drv_attr++;
954         }
955         if (cpufreq_driver->get) {
956                 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
957                 if (ret)
958                         return ret;
959         }
960
961         ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
962         if (ret)
963                 return ret;
964
965         if (cpufreq_driver->bios_limit) {
966                 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
967                 if (ret)
968                         return ret;
969         }
970
971         return cpufreq_add_dev_symlink(policy);
972 }
973
974 __weak struct cpufreq_governor *cpufreq_default_governor(void)
975 {
976         return NULL;
977 }
978
979 static int cpufreq_init_policy(struct cpufreq_policy *policy)
980 {
981         struct cpufreq_governor *gov = NULL;
982         struct cpufreq_policy new_policy;
983
984         memcpy(&new_policy, policy, sizeof(*policy));
985
986         /* Update governor of new_policy to the governor used before hotplug */
987         gov = find_governor(policy->last_governor);
988         if (gov) {
989                 pr_debug("Restoring governor %s for cpu %d\n",
990                                 policy->governor->name, policy->cpu);
991         } else {
992                 gov = cpufreq_default_governor();
993                 if (!gov)
994                         return -ENODATA;
995         }
996
997         new_policy.governor = gov;
998
999         /* Use the default policy if there is no last_policy. */
1000         if (cpufreq_driver->setpolicy) {
1001                 if (policy->last_policy)
1002                         new_policy.policy = policy->last_policy;
1003                 else
1004                         cpufreq_parse_governor(gov->name, &new_policy.policy,
1005                                                NULL);
1006         }
1007         /* set default policy */
1008         return cpufreq_set_policy(policy, &new_policy);
1009 }
1010
1011 static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1012 {
1013         int ret = 0;
1014
1015         /* Has this CPU been taken care of already? */
1016         if (cpumask_test_cpu(cpu, policy->cpus))
1017                 return 0;
1018
1019         down_write(&policy->rwsem);
1020         if (has_target()) {
1021                 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1022                 if (ret) {
1023                         pr_err("%s: Failed to stop governor\n", __func__);
1024                         goto unlock;
1025                 }
1026         }
1027
1028         cpumask_set_cpu(cpu, policy->cpus);
1029
1030         if (has_target()) {
1031                 ret = cpufreq_start_governor(policy);
1032                 if (ret)
1033                         pr_err("%s: Failed to start governor\n", __func__);
1034         }
1035
1036 unlock:
1037         up_write(&policy->rwsem);
1038         return ret;
1039 }
1040
1041 static void handle_update(struct work_struct *work)
1042 {
1043         struct cpufreq_policy *policy =
1044                 container_of(work, struct cpufreq_policy, update);
1045         unsigned int cpu = policy->cpu;
1046         pr_debug("handle_update for cpu %u called\n", cpu);
1047         cpufreq_update_policy(cpu);
1048 }
1049
1050 static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1051 {
1052         struct device *dev = get_cpu_device(cpu);
1053         struct cpufreq_policy *policy;
1054         int ret;
1055
1056         if (WARN_ON(!dev))
1057                 return NULL;
1058
1059         policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1060         if (!policy)
1061                 return NULL;
1062
1063         if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1064                 goto err_free_policy;
1065
1066         if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1067                 goto err_free_cpumask;
1068
1069         if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1070                 goto err_free_rcpumask;
1071
1072         ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1073                                    cpufreq_global_kobject, "policy%u", cpu);
1074         if (ret) {
1075                 pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1076                 goto err_free_real_cpus;
1077         }
1078
1079         INIT_LIST_HEAD(&policy->policy_list);
1080         init_rwsem(&policy->rwsem);
1081         spin_lock_init(&policy->transition_lock);
1082         init_waitqueue_head(&policy->transition_wait);
1083         init_completion(&policy->kobj_unregister);
1084         INIT_WORK(&policy->update, handle_update);
1085
1086         policy->cpu = cpu;
1087         return policy;
1088
1089 err_free_real_cpus:
1090         free_cpumask_var(policy->real_cpus);
1091 err_free_rcpumask:
1092         free_cpumask_var(policy->related_cpus);
1093 err_free_cpumask:
1094         free_cpumask_var(policy->cpus);
1095 err_free_policy:
1096         kfree(policy);
1097
1098         return NULL;
1099 }
1100
1101 static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify)
1102 {
1103         struct kobject *kobj;
1104         struct completion *cmp;
1105
1106         if (notify)
1107                 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1108                                              CPUFREQ_REMOVE_POLICY, policy);
1109
1110         down_write(&policy->rwsem);
1111         cpufreq_remove_dev_symlink(policy);
1112         kobj = &policy->kobj;
1113         cmp = &policy->kobj_unregister;
1114         up_write(&policy->rwsem);
1115         kobject_put(kobj);
1116
1117         /*
1118          * We need to make sure that the underlying kobj is
1119          * actually not referenced anymore by anybody before we
1120          * proceed with unloading.
1121          */
1122         pr_debug("waiting for dropping of refcount\n");
1123         wait_for_completion(cmp);
1124         pr_debug("wait complete\n");
1125 }
1126
1127 static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
1128 {
1129         unsigned long flags;
1130         int cpu;
1131
1132         /* Remove policy from list */
1133         write_lock_irqsave(&cpufreq_driver_lock, flags);
1134         list_del(&policy->policy_list);
1135
1136         for_each_cpu(cpu, policy->related_cpus)
1137                 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1138         write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1139
1140         cpufreq_policy_put_kobj(policy, notify);
1141         free_cpumask_var(policy->real_cpus);
1142         free_cpumask_var(policy->related_cpus);
1143         free_cpumask_var(policy->cpus);
1144         kfree(policy);
1145 }
1146
1147 static int cpufreq_online(unsigned int cpu)
1148 {
1149         struct cpufreq_policy *policy;
1150         bool new_policy;
1151         unsigned long flags;
1152         unsigned int j;
1153         int ret;
1154
1155         pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
1156
1157         /* Check if this CPU already has a policy to manage it */
1158         policy = per_cpu(cpufreq_cpu_data, cpu);
1159         if (policy) {
1160                 WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
1161                 if (!policy_is_inactive(policy))
1162                         return cpufreq_add_policy_cpu(policy, cpu);
1163
1164                 /* This is the only online CPU for the policy.  Start over. */
1165                 new_policy = false;
1166                 down_write(&policy->rwsem);
1167                 policy->cpu = cpu;
1168                 policy->governor = NULL;
1169                 up_write(&policy->rwsem);
1170         } else {
1171                 new_policy = true;
1172                 policy = cpufreq_policy_alloc(cpu);
1173                 if (!policy)
1174                         return -ENOMEM;
1175         }
1176
1177         cpumask_copy(policy->cpus, cpumask_of(cpu));
1178
1179         /* call driver. From then on the cpufreq must be able
1180          * to accept all calls to ->verify and ->setpolicy for this CPU
1181          */
1182         ret = cpufreq_driver->init(policy);
1183         if (ret) {
1184                 pr_debug("initialization failed\n");
1185                 goto out_free_policy;
1186         }
1187
1188         down_write(&policy->rwsem);
1189
1190         if (new_policy) {
1191                 /* related_cpus should at least include policy->cpus. */
1192                 cpumask_copy(policy->related_cpus, policy->cpus);
1193                 /* Remember CPUs present at the policy creation time. */
1194                 cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask);
1195         }
1196
1197         /*
1198          * affected cpus must always be the one, which are online. We aren't
1199          * managing offline cpus here.
1200          */
1201         cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1202
1203         if (new_policy) {
1204                 policy->user_policy.min = policy->min;
1205                 policy->user_policy.max = policy->max;
1206
1207                 write_lock_irqsave(&cpufreq_driver_lock, flags);
1208                 for_each_cpu(j, policy->related_cpus)
1209                         per_cpu(cpufreq_cpu_data, j) = policy;
1210                 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1211         }
1212
1213         if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1214                 policy->cur = cpufreq_driver->get(policy->cpu);
1215                 if (!policy->cur) {
1216                         pr_err("%s: ->get() failed\n", __func__);
1217                         goto out_exit_policy;
1218                 }
1219         }
1220
1221         /*
1222          * Sometimes boot loaders set CPU frequency to a value outside of
1223          * frequency table present with cpufreq core. In such cases CPU might be
1224          * unstable if it has to run on that frequency for long duration of time
1225          * and so its better to set it to a frequency which is specified in
1226          * freq-table. This also makes cpufreq stats inconsistent as
1227          * cpufreq-stats would fail to register because current frequency of CPU
1228          * isn't found in freq-table.
1229          *
1230          * Because we don't want this change to effect boot process badly, we go
1231          * for the next freq which is >= policy->cur ('cur' must be set by now,
1232          * otherwise we will end up setting freq to lowest of the table as 'cur'
1233          * is initialized to zero).
1234          *
1235          * We are passing target-freq as "policy->cur - 1" otherwise
1236          * __cpufreq_driver_target() would simply fail, as policy->cur will be
1237          * equal to target-freq.
1238          */
1239         if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1240             && has_target()) {
1241                 /* Are we running at unknown frequency ? */
1242                 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1243                 if (ret == -EINVAL) {
1244                         /* Warn user and fix it */
1245                         pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1246                                 __func__, policy->cpu, policy->cur);
1247                         ret = __cpufreq_driver_target(policy, policy->cur - 1,
1248                                 CPUFREQ_RELATION_L);
1249
1250                         /*
1251                          * Reaching here after boot in a few seconds may not
1252                          * mean that system will remain stable at "unknown"
1253                          * frequency for longer duration. Hence, a BUG_ON().
1254                          */
1255                         BUG_ON(ret);
1256                         pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1257                                 __func__, policy->cpu, policy->cur);
1258                 }
1259         }
1260
1261         blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1262                                      CPUFREQ_START, policy);
1263
1264         if (new_policy) {
1265                 ret = cpufreq_add_dev_interface(policy);
1266                 if (ret)
1267                         goto out_exit_policy;
1268                 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1269                                 CPUFREQ_CREATE_POLICY, policy);
1270
1271                 write_lock_irqsave(&cpufreq_driver_lock, flags);
1272                 list_add(&policy->policy_list, &cpufreq_policy_list);
1273                 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1274         }
1275
1276         ret = cpufreq_init_policy(policy);
1277         if (ret) {
1278                 pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
1279                        __func__, cpu, ret);
1280                 /* cpufreq_policy_free() will notify based on this */
1281                 new_policy = false;
1282                 goto out_exit_policy;
1283         }
1284
1285         up_write(&policy->rwsem);
1286
1287         kobject_uevent(&policy->kobj, KOBJ_ADD);
1288
1289         /* Callback for handling stuff after policy is ready */
1290         if (cpufreq_driver->ready)
1291                 cpufreq_driver->ready(policy);
1292
1293         pr_debug("initialization complete\n");
1294
1295         return 0;
1296
1297 out_exit_policy:
1298         up_write(&policy->rwsem);
1299
1300         if (cpufreq_driver->exit)
1301                 cpufreq_driver->exit(policy);
1302 out_free_policy:
1303         cpufreq_policy_free(policy, !new_policy);
1304         return ret;
1305 }
1306
1307 /**
1308  * cpufreq_add_dev - the cpufreq interface for a CPU device.
1309  * @dev: CPU device.
1310  * @sif: Subsystem interface structure pointer (not used)
1311  */
1312 static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1313 {
1314         unsigned cpu = dev->id;
1315         int ret;
1316
1317         dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
1318
1319         if (cpu_online(cpu)) {
1320                 ret = cpufreq_online(cpu);
1321         } else {
1322                 /*
1323                  * A hotplug notifier will follow and we will handle it as CPU
1324                  * online then.  For now, just create the sysfs link, unless
1325                  * there is no policy or the link is already present.
1326                  */
1327                 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1328
1329                 ret = policy && !cpumask_test_and_set_cpu(cpu, policy->real_cpus)
1330                         ? add_cpu_dev_symlink(policy, cpu) : 0;
1331         }
1332
1333         return ret;
1334 }
1335
1336 static void cpufreq_offline(unsigned int cpu)
1337 {
1338         struct cpufreq_policy *policy;
1339         int ret;
1340
1341         pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1342
1343         policy = cpufreq_cpu_get_raw(cpu);
1344         if (!policy) {
1345                 pr_debug("%s: No cpu_data found\n", __func__);
1346                 return;
1347         }
1348
1349         down_write(&policy->rwsem);
1350         if (has_target()) {
1351                 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1352                 if (ret)
1353                         pr_err("%s: Failed to stop governor\n", __func__);
1354         }
1355
1356         cpumask_clear_cpu(cpu, policy->cpus);
1357
1358         if (policy_is_inactive(policy)) {
1359                 if (has_target())
1360                         strncpy(policy->last_governor, policy->governor->name,
1361                                 CPUFREQ_NAME_LEN);
1362                 else
1363                         policy->last_policy = policy->policy;
1364         } else if (cpu == policy->cpu) {
1365                 /* Nominate new CPU */
1366                 policy->cpu = cpumask_any(policy->cpus);
1367         }
1368
1369         /* Start governor again for active policy */
1370         if (!policy_is_inactive(policy)) {
1371                 if (has_target()) {
1372                         ret = cpufreq_start_governor(policy);
1373                         if (ret)
1374                                 pr_err("%s: Failed to start governor\n", __func__);
1375                 }
1376
1377                 goto unlock;
1378         }
1379
1380         if (cpufreq_driver->stop_cpu)
1381                 cpufreq_driver->stop_cpu(policy);
1382
1383         /* If cpu is last user of policy, free policy */
1384         if (has_target()) {
1385                 ret = cpufreq_exit_governor(policy);
1386                 if (ret)
1387                         pr_err("%s: Failed to exit governor\n", __func__);
1388         }
1389
1390         /*
1391          * Perform the ->exit() even during light-weight tear-down,
1392          * since this is a core component, and is essential for the
1393          * subsequent light-weight ->init() to succeed.
1394          */
1395         if (cpufreq_driver->exit) {
1396                 cpufreq_driver->exit(policy);
1397                 policy->freq_table = NULL;
1398         }
1399
1400 unlock:
1401         up_write(&policy->rwsem);
1402 }
1403
1404 /**
1405  * cpufreq_remove_dev - remove a CPU device
1406  *
1407  * Removes the cpufreq interface for a CPU device.
1408  */
1409 static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1410 {
1411         unsigned int cpu = dev->id;
1412         struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1413
1414         if (!policy)
1415                 return;
1416
1417         if (cpu_online(cpu))
1418                 cpufreq_offline(cpu);
1419
1420         cpumask_clear_cpu(cpu, policy->real_cpus);
1421         remove_cpu_dev_symlink(policy, cpu);
1422
1423         if (cpumask_empty(policy->real_cpus))
1424                 cpufreq_policy_free(policy, true);
1425 }
1426
1427 /**
1428  *      cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1429  *      in deep trouble.
1430  *      @policy: policy managing CPUs
1431  *      @new_freq: CPU frequency the CPU actually runs at
1432  *
1433  *      We adjust to current frequency first, and need to clean up later.
1434  *      So either call to cpufreq_update_policy() or schedule handle_update()).
1435  */
1436 static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1437                                 unsigned int new_freq)
1438 {
1439         struct cpufreq_freqs freqs;
1440
1441         pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1442                  policy->cur, new_freq);
1443
1444         freqs.old = policy->cur;
1445         freqs.new = new_freq;
1446
1447         cpufreq_freq_transition_begin(policy, &freqs);
1448         cpufreq_freq_transition_end(policy, &freqs, 0);
1449 }
1450
1451 /**
1452  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1453  * @cpu: CPU number
1454  *
1455  * This is the last known freq, without actually getting it from the driver.
1456  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1457  */
1458 unsigned int cpufreq_quick_get(unsigned int cpu)
1459 {
1460         struct cpufreq_policy *policy;
1461         unsigned int ret_freq = 0;
1462         unsigned long flags;
1463
1464         read_lock_irqsave(&cpufreq_driver_lock, flags);
1465
1466         if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1467                 ret_freq = cpufreq_driver->get(cpu);
1468                 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1469                 return ret_freq;
1470         }
1471
1472         read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1473
1474         policy = cpufreq_cpu_get(cpu);
1475         if (policy) {
1476                 ret_freq = policy->cur;
1477                 cpufreq_cpu_put(policy);
1478         }
1479
1480         return ret_freq;
1481 }
1482 EXPORT_SYMBOL(cpufreq_quick_get);
1483
1484 /**
1485  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1486  * @cpu: CPU number
1487  *
1488  * Just return the max possible frequency for a given CPU.
1489  */
1490 unsigned int cpufreq_quick_get_max(unsigned int cpu)
1491 {
1492         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1493         unsigned int ret_freq = 0;
1494
1495         if (policy) {
1496                 ret_freq = policy->max;
1497                 cpufreq_cpu_put(policy);
1498         }
1499
1500         return ret_freq;
1501 }
1502 EXPORT_SYMBOL(cpufreq_quick_get_max);
1503
1504 static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
1505 {
1506         unsigned int ret_freq = 0;
1507
1508         if (!cpufreq_driver->get)
1509                 return ret_freq;
1510
1511         ret_freq = cpufreq_driver->get(policy->cpu);
1512
1513         /*
1514          * Updating inactive policies is invalid, so avoid doing that.  Also
1515          * if fast frequency switching is used with the given policy, the check
1516          * against policy->cur is pointless, so skip it in that case too.
1517          */
1518         if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
1519                 return ret_freq;
1520
1521         if (ret_freq && policy->cur &&
1522                 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1523                 /* verify no discrepancy between actual and
1524                                         saved value exists */
1525                 if (unlikely(ret_freq != policy->cur)) {
1526                         cpufreq_out_of_sync(policy, ret_freq);
1527                         schedule_work(&policy->update);
1528                 }
1529         }
1530
1531         return ret_freq;
1532 }
1533
1534 /**
1535  * cpufreq_get - get the current CPU frequency (in kHz)
1536  * @cpu: CPU number
1537  *
1538  * Get the CPU current (static) CPU frequency
1539  */
1540 unsigned int cpufreq_get(unsigned int cpu)
1541 {
1542         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1543         unsigned int ret_freq = 0;
1544
1545         if (policy) {
1546                 down_read(&policy->rwsem);
1547                 ret_freq = __cpufreq_get(policy);
1548                 up_read(&policy->rwsem);
1549
1550                 cpufreq_cpu_put(policy);
1551         }
1552
1553         return ret_freq;
1554 }
1555 EXPORT_SYMBOL(cpufreq_get);
1556
1557 static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1558 {
1559         unsigned int new_freq;
1560
1561         new_freq = cpufreq_driver->get(policy->cpu);
1562         if (!new_freq)
1563                 return 0;
1564
1565         if (!policy->cur) {
1566                 pr_debug("cpufreq: Driver did not initialize current freq\n");
1567                 policy->cur = new_freq;
1568         } else if (policy->cur != new_freq && has_target()) {
1569                 cpufreq_out_of_sync(policy, new_freq);
1570         }
1571
1572         return new_freq;
1573 }
1574
1575 static struct subsys_interface cpufreq_interface = {
1576         .name           = "cpufreq",
1577         .subsys         = &cpu_subsys,
1578         .add_dev        = cpufreq_add_dev,
1579         .remove_dev     = cpufreq_remove_dev,
1580 };
1581
1582 /*
1583  * In case platform wants some specific frequency to be configured
1584  * during suspend..
1585  */
1586 int cpufreq_generic_suspend(struct cpufreq_policy *policy)
1587 {
1588         int ret;
1589
1590         if (!policy->suspend_freq) {
1591                 pr_debug("%s: suspend_freq not defined\n", __func__);
1592                 return 0;
1593         }
1594
1595         pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1596                         policy->suspend_freq);
1597
1598         ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1599                         CPUFREQ_RELATION_H);
1600         if (ret)
1601                 pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1602                                 __func__, policy->suspend_freq, ret);
1603
1604         return ret;
1605 }
1606 EXPORT_SYMBOL(cpufreq_generic_suspend);
1607
1608 /**
1609  * cpufreq_suspend() - Suspend CPUFreq governors
1610  *
1611  * Called during system wide Suspend/Hibernate cycles for suspending governors
1612  * as some platforms can't change frequency after this point in suspend cycle.
1613  * Because some of the devices (like: i2c, regulators, etc) they use for
1614  * changing frequency are suspended quickly after this point.
1615  */
1616 void cpufreq_suspend(void)
1617 {
1618         struct cpufreq_policy *policy;
1619         int ret;
1620
1621         if (!cpufreq_driver)
1622                 return;
1623
1624         if (!has_target())
1625                 goto suspend;
1626
1627         pr_debug("%s: Suspending Governors\n", __func__);
1628
1629         for_each_active_policy(policy) {
1630                 down_write(&policy->rwsem);
1631                 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1632                 up_write(&policy->rwsem);
1633
1634                 if (ret)
1635                         pr_err("%s: Failed to stop governor for policy: %p\n",
1636                                 __func__, policy);
1637                 else if (cpufreq_driver->suspend
1638                     && cpufreq_driver->suspend(policy))
1639                         pr_err("%s: Failed to suspend driver: %p\n", __func__,
1640                                 policy);
1641         }
1642
1643 suspend:
1644         cpufreq_suspended = true;
1645 }
1646
1647 /**
1648  * cpufreq_resume() - Resume CPUFreq governors
1649  *
1650  * Called during system wide Suspend/Hibernate cycle for resuming governors that
1651  * are suspended with cpufreq_suspend().
1652  */
1653 void cpufreq_resume(void)
1654 {
1655         struct cpufreq_policy *policy;
1656         int ret;
1657
1658         if (!cpufreq_driver)
1659                 return;
1660
1661         cpufreq_suspended = false;
1662
1663         if (!has_target())
1664                 return;
1665
1666         pr_debug("%s: Resuming Governors\n", __func__);
1667
1668         for_each_active_policy(policy) {
1669                 if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
1670                         pr_err("%s: Failed to resume driver: %p\n", __func__,
1671                                 policy);
1672                 } else {
1673                         down_write(&policy->rwsem);
1674                         ret = cpufreq_start_governor(policy);
1675                         up_write(&policy->rwsem);
1676
1677                         if (ret)
1678                                 pr_err("%s: Failed to start governor for policy: %p\n",
1679                                        __func__, policy);
1680                 }
1681         }
1682 }
1683
1684 /**
1685  *      cpufreq_get_current_driver - return current driver's name
1686  *
1687  *      Return the name string of the currently loaded cpufreq driver
1688  *      or NULL, if none.
1689  */
1690 const char *cpufreq_get_current_driver(void)
1691 {
1692         if (cpufreq_driver)
1693                 return cpufreq_driver->name;
1694
1695         return NULL;
1696 }
1697 EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1698
1699 /**
1700  *      cpufreq_get_driver_data - return current driver data
1701  *
1702  *      Return the private data of the currently loaded cpufreq
1703  *      driver, or NULL if no cpufreq driver is loaded.
1704  */
1705 void *cpufreq_get_driver_data(void)
1706 {
1707         if (cpufreq_driver)
1708                 return cpufreq_driver->driver_data;
1709
1710         return NULL;
1711 }
1712 EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
1713
1714 /*********************************************************************
1715  *                     NOTIFIER LISTS INTERFACE                      *
1716  *********************************************************************/
1717
1718 /**
1719  *      cpufreq_register_notifier - register a driver with cpufreq
1720  *      @nb: notifier function to register
1721  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1722  *
1723  *      Add a driver to one of two lists: either a list of drivers that
1724  *      are notified about clock rate changes (once before and once after
1725  *      the transition), or a list of drivers that are notified about
1726  *      changes in cpufreq policy.
1727  *
1728  *      This function may sleep, and has the same return conditions as
1729  *      blocking_notifier_chain_register.
1730  */
1731 int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1732 {
1733         int ret;
1734
1735         if (cpufreq_disabled())
1736                 return -EINVAL;
1737
1738         WARN_ON(!init_cpufreq_transition_notifier_list_called);
1739
1740         switch (list) {
1741         case CPUFREQ_TRANSITION_NOTIFIER:
1742                 mutex_lock(&cpufreq_fast_switch_lock);
1743
1744                 if (cpufreq_fast_switch_count > 0) {
1745                         mutex_unlock(&cpufreq_fast_switch_lock);
1746                         return -EBUSY;
1747                 }
1748                 ret = srcu_notifier_chain_register(
1749                                 &cpufreq_transition_notifier_list, nb);
1750                 if (!ret)
1751                         cpufreq_fast_switch_count--;
1752
1753                 mutex_unlock(&cpufreq_fast_switch_lock);
1754                 break;
1755         case CPUFREQ_POLICY_NOTIFIER:
1756                 ret = blocking_notifier_chain_register(
1757                                 &cpufreq_policy_notifier_list, nb);
1758                 break;
1759         default:
1760                 ret = -EINVAL;
1761         }
1762
1763         return ret;
1764 }
1765 EXPORT_SYMBOL(cpufreq_register_notifier);
1766
1767 /**
1768  *      cpufreq_unregister_notifier - unregister a driver with cpufreq
1769  *      @nb: notifier block to be unregistered
1770  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1771  *
1772  *      Remove a driver from the CPU frequency notifier list.
1773  *
1774  *      This function may sleep, and has the same return conditions as
1775  *      blocking_notifier_chain_unregister.
1776  */
1777 int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1778 {
1779         int ret;
1780
1781         if (cpufreq_disabled())
1782                 return -EINVAL;
1783
1784         switch (list) {
1785         case CPUFREQ_TRANSITION_NOTIFIER:
1786                 mutex_lock(&cpufreq_fast_switch_lock);
1787
1788                 ret = srcu_notifier_chain_unregister(
1789                                 &cpufreq_transition_notifier_list, nb);
1790                 if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1791                         cpufreq_fast_switch_count++;
1792
1793                 mutex_unlock(&cpufreq_fast_switch_lock);
1794                 break;
1795         case CPUFREQ_POLICY_NOTIFIER:
1796                 ret = blocking_notifier_chain_unregister(
1797                                 &cpufreq_policy_notifier_list, nb);
1798                 break;
1799         default:
1800                 ret = -EINVAL;
1801         }
1802
1803         return ret;
1804 }
1805 EXPORT_SYMBOL(cpufreq_unregister_notifier);
1806
1807
1808 /*********************************************************************
1809  *                              GOVERNORS                            *
1810  *********************************************************************/
1811
1812 /**
1813  * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1814  * @policy: cpufreq policy to switch the frequency for.
1815  * @target_freq: New frequency to set (may be approximate).
1816  *
1817  * Carry out a fast frequency switch without sleeping.
1818  *
1819  * The driver's ->fast_switch() callback invoked by this function must be
1820  * suitable for being called from within RCU-sched read-side critical sections
1821  * and it is expected to select the minimum available frequency greater than or
1822  * equal to @target_freq (CPUFREQ_RELATION_L).
1823  *
1824  * This function must not be called if policy->fast_switch_enabled is unset.
1825  *
1826  * Governors calling this function must guarantee that it will never be invoked
1827  * twice in parallel for the same policy and that it will never be called in
1828  * parallel with either ->target() or ->target_index() for the same policy.
1829  *
1830  * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
1831  * callback to indicate an error condition, the hardware configuration must be
1832  * preserved.
1833  */
1834 unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1835                                         unsigned int target_freq)
1836 {
1837         clamp_val(target_freq, policy->min, policy->max);
1838
1839         return cpufreq_driver->fast_switch(policy, target_freq);
1840 }
1841 EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1842
1843 /* Must set freqs->new to intermediate frequency */
1844 static int __target_intermediate(struct cpufreq_policy *policy,
1845                                  struct cpufreq_freqs *freqs, int index)
1846 {
1847         int ret;
1848
1849         freqs->new = cpufreq_driver->get_intermediate(policy, index);
1850
1851         /* We don't need to switch to intermediate freq */
1852         if (!freqs->new)
1853                 return 0;
1854
1855         pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
1856                  __func__, policy->cpu, freqs->old, freqs->new);
1857
1858         cpufreq_freq_transition_begin(policy, freqs);
1859         ret = cpufreq_driver->target_intermediate(policy, index);
1860         cpufreq_freq_transition_end(policy, freqs, ret);
1861
1862         if (ret)
1863                 pr_err("%s: Failed to change to intermediate frequency: %d\n",
1864                        __func__, ret);
1865
1866         return ret;
1867 }
1868
1869 static int __target_index(struct cpufreq_policy *policy,
1870                           struct cpufreq_frequency_table *freq_table, int index)
1871 {
1872         struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
1873         unsigned int intermediate_freq = 0;
1874         int retval = -EINVAL;
1875         bool notify;
1876
1877         notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1878         if (notify) {
1879                 /* Handle switching to intermediate frequency */
1880                 if (cpufreq_driver->get_intermediate) {
1881                         retval = __target_intermediate(policy, &freqs, index);
1882                         if (retval)
1883                                 return retval;
1884
1885                         intermediate_freq = freqs.new;
1886                         /* Set old freq to intermediate */
1887                         if (intermediate_freq)
1888                                 freqs.old = freqs.new;
1889                 }
1890
1891                 freqs.new = freq_table[index].frequency;
1892                 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1893                          __func__, policy->cpu, freqs.old, freqs.new);
1894
1895                 cpufreq_freq_transition_begin(policy, &freqs);
1896         }
1897
1898         retval = cpufreq_driver->target_index(policy, index);
1899         if (retval)
1900                 pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
1901                        retval);
1902
1903         if (notify) {
1904                 cpufreq_freq_transition_end(policy, &freqs, retval);
1905
1906                 /*
1907                  * Failed after setting to intermediate freq? Driver should have
1908                  * reverted back to initial frequency and so should we. Check
1909                  * here for intermediate_freq instead of get_intermediate, in
1910                  * case we haven't switched to intermediate freq at all.
1911                  */
1912                 if (unlikely(retval && intermediate_freq)) {
1913                         freqs.old = intermediate_freq;
1914                         freqs.new = policy->restore_freq;
1915                         cpufreq_freq_transition_begin(policy, &freqs);
1916                         cpufreq_freq_transition_end(policy, &freqs, 0);
1917                 }
1918         }
1919
1920         return retval;
1921 }
1922
1923 int __cpufreq_driver_target(struct cpufreq_policy *policy,
1924                             unsigned int target_freq,
1925                             unsigned int relation)
1926 {
1927         unsigned int old_target_freq = target_freq;
1928         struct cpufreq_frequency_table *freq_table;
1929         int index, retval;
1930
1931         if (cpufreq_disabled())
1932                 return -ENODEV;
1933
1934         /* Make sure that target_freq is within supported range */
1935         if (target_freq > policy->max)
1936                 target_freq = policy->max;
1937         if (target_freq < policy->min)
1938                 target_freq = policy->min;
1939
1940         pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1941                  policy->cpu, target_freq, relation, old_target_freq);
1942
1943         /*
1944          * This might look like a redundant call as we are checking it again
1945          * after finding index. But it is left intentionally for cases where
1946          * exactly same freq is called again and so we can save on few function
1947          * calls.
1948          */
1949         if (target_freq == policy->cur)
1950                 return 0;
1951
1952         /* Save last value to restore later on errors */
1953         policy->restore_freq = policy->cur;
1954
1955         if (cpufreq_driver->target)
1956                 return cpufreq_driver->target(policy, target_freq, relation);
1957
1958         if (!cpufreq_driver->target_index)
1959                 return -EINVAL;
1960
1961         freq_table = cpufreq_frequency_get_table(policy->cpu);
1962         if (unlikely(!freq_table)) {
1963                 pr_err("%s: Unable to find freq_table\n", __func__);
1964                 return -EINVAL;
1965         }
1966
1967         retval = cpufreq_frequency_table_target(policy, freq_table, target_freq,
1968                                                 relation, &index);
1969         if (unlikely(retval)) {
1970                 pr_err("%s: Unable to find matching freq\n", __func__);
1971                 return retval;
1972         }
1973
1974         if (freq_table[index].frequency == policy->cur)
1975                 return 0;
1976
1977         return __target_index(policy, freq_table, index);
1978 }
1979 EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1980
1981 int cpufreq_driver_target(struct cpufreq_policy *policy,
1982                           unsigned int target_freq,
1983                           unsigned int relation)
1984 {
1985         int ret = -EINVAL;
1986
1987         down_write(&policy->rwsem);
1988
1989         ret = __cpufreq_driver_target(policy, target_freq, relation);
1990
1991         up_write(&policy->rwsem);
1992
1993         return ret;
1994 }
1995 EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1996
1997 __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
1998 {
1999         return NULL;
2000 }
2001
2002 static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
2003 {
2004         int ret;
2005
2006         /* Don't start any governor operations if we are entering suspend */
2007         if (cpufreq_suspended)
2008                 return 0;
2009         /*
2010          * Governor might not be initiated here if ACPI _PPC changed
2011          * notification happened, so check it.
2012          */
2013         if (!policy->governor)
2014                 return -EINVAL;
2015
2016         if (policy->governor->max_transition_latency &&
2017             policy->cpuinfo.transition_latency >
2018             policy->governor->max_transition_latency) {
2019                 struct cpufreq_governor *gov = cpufreq_fallback_governor();
2020
2021                 if (gov) {
2022                         pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2023                                 policy->governor->name, gov->name);
2024                         policy->governor = gov;
2025                 } else {
2026                         return -EINVAL;
2027                 }
2028         }
2029
2030         if (event == CPUFREQ_GOV_POLICY_INIT)
2031                 if (!try_module_get(policy->governor->owner))
2032                         return -EINVAL;
2033
2034         pr_debug("%s: for CPU %u, event %u\n", __func__, policy->cpu, event);
2035
2036         ret = policy->governor->governor(policy, event);
2037
2038         if (!ret) {
2039                 if (event == CPUFREQ_GOV_POLICY_INIT)
2040                         policy->governor->initialized++;
2041                 else if (event == CPUFREQ_GOV_POLICY_EXIT)
2042                         policy->governor->initialized--;
2043         }
2044
2045         if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
2046                         ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
2047                 module_put(policy->governor->owner);
2048
2049         return ret;
2050 }
2051
2052 static int cpufreq_start_governor(struct cpufreq_policy *policy)
2053 {
2054         int ret;
2055
2056         if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
2057                 cpufreq_update_current_freq(policy);
2058
2059         ret = cpufreq_governor(policy, CPUFREQ_GOV_START);
2060         return ret ? ret : cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2061 }
2062
2063 static int cpufreq_exit_governor(struct cpufreq_policy *policy)
2064 {
2065         cpufreq_disable_fast_switch(policy);
2066         return cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
2067 }
2068
2069 int cpufreq_register_governor(struct cpufreq_governor *governor)
2070 {
2071         int err;
2072
2073         if (!governor)
2074                 return -EINVAL;
2075
2076         if (cpufreq_disabled())
2077                 return -ENODEV;
2078
2079         mutex_lock(&cpufreq_governor_mutex);
2080
2081         governor->initialized = 0;
2082         err = -EBUSY;
2083         if (!find_governor(governor->name)) {
2084                 err = 0;
2085                 list_add(&governor->governor_list, &cpufreq_governor_list);
2086         }
2087
2088         mutex_unlock(&cpufreq_governor_mutex);
2089         return err;
2090 }
2091 EXPORT_SYMBOL_GPL(cpufreq_register_governor);
2092
2093 void cpufreq_unregister_governor(struct cpufreq_governor *governor)
2094 {
2095         struct cpufreq_policy *policy;
2096         unsigned long flags;
2097
2098         if (!governor)
2099                 return;
2100
2101         if (cpufreq_disabled())
2102                 return;
2103
2104         /* clear last_governor for all inactive policies */
2105         read_lock_irqsave(&cpufreq_driver_lock, flags);
2106         for_each_inactive_policy(policy) {
2107                 if (!strcmp(policy->last_governor, governor->name)) {
2108                         policy->governor = NULL;
2109                         strcpy(policy->last_governor, "\0");
2110                 }
2111         }
2112         read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2113
2114         mutex_lock(&cpufreq_governor_mutex);
2115         list_del(&governor->governor_list);
2116         mutex_unlock(&cpufreq_governor_mutex);
2117         return;
2118 }
2119 EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
2120
2121
2122 /*********************************************************************
2123  *                          POLICY INTERFACE                         *
2124  *********************************************************************/
2125
2126 /**
2127  * cpufreq_get_policy - get the current cpufreq_policy
2128  * @policy: struct cpufreq_policy into which the current cpufreq_policy
2129  *      is written
2130  *
2131  * Reads the current cpufreq policy.
2132  */
2133 int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2134 {
2135         struct cpufreq_policy *cpu_policy;
2136         if (!policy)
2137                 return -EINVAL;
2138
2139         cpu_policy = cpufreq_cpu_get(cpu);
2140         if (!cpu_policy)
2141                 return -EINVAL;
2142
2143         memcpy(policy, cpu_policy, sizeof(*policy));
2144
2145         cpufreq_cpu_put(cpu_policy);
2146         return 0;
2147 }
2148 EXPORT_SYMBOL(cpufreq_get_policy);
2149
2150 /*
2151  * policy : current policy.
2152  * new_policy: policy to be set.
2153  */
2154 static int cpufreq_set_policy(struct cpufreq_policy *policy,
2155                                 struct cpufreq_policy *new_policy)
2156 {
2157         struct cpufreq_governor *old_gov;
2158         int ret;
2159
2160         pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2161                  new_policy->cpu, new_policy->min, new_policy->max);
2162
2163         memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2164
2165         /*
2166         * This check works well when we store new min/max freq attributes,
2167         * because new_policy is a copy of policy with one field updated.
2168         */
2169         if (new_policy->min > new_policy->max)
2170                 return -EINVAL;
2171
2172         /* verify the cpu speed can be set within this limit */
2173         ret = cpufreq_driver->verify(new_policy);
2174         if (ret)
2175                 return ret;
2176
2177         /* adjust if necessary - all reasons */
2178         blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2179                         CPUFREQ_ADJUST, new_policy);
2180
2181         /*
2182          * verify the cpu speed can be set within this limit, which might be
2183          * different to the first one
2184          */
2185         ret = cpufreq_driver->verify(new_policy);
2186         if (ret)
2187                 return ret;
2188
2189         /* notification of the new policy */
2190         blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2191                         CPUFREQ_NOTIFY, new_policy);
2192
2193         policy->min = new_policy->min;
2194         policy->max = new_policy->max;
2195
2196         pr_debug("new min and max freqs are %u - %u kHz\n",
2197                  policy->min, policy->max);
2198
2199         if (cpufreq_driver->setpolicy) {
2200                 policy->policy = new_policy->policy;
2201                 pr_debug("setting range\n");
2202                 return cpufreq_driver->setpolicy(new_policy);
2203         }
2204
2205         if (new_policy->governor == policy->governor) {
2206                 pr_debug("cpufreq: governor limits update\n");
2207                 return cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2208         }
2209
2210         pr_debug("governor switch\n");
2211
2212         /* save old, working values */
2213         old_gov = policy->governor;
2214         /* end old governor */
2215         if (old_gov) {
2216                 ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2217                 if (ret) {
2218                         /* This can happen due to race with other operations */
2219                         pr_debug("%s: Failed to Stop Governor: %s (%d)\n",
2220                                  __func__, old_gov->name, ret);
2221                         return ret;
2222                 }
2223
2224                 ret = cpufreq_exit_governor(policy);
2225                 if (ret) {
2226                         pr_err("%s: Failed to Exit Governor: %s (%d)\n",
2227                                __func__, old_gov->name, ret);
2228                         return ret;
2229                 }
2230         }
2231
2232         /* start new governor */
2233         policy->governor = new_policy->governor;
2234         ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
2235         if (!ret) {
2236                 ret = cpufreq_start_governor(policy);
2237                 if (!ret) {
2238                         pr_debug("cpufreq: governor change\n");
2239                         return 0;
2240                 }
2241                 cpufreq_exit_governor(policy);
2242         }
2243
2244         /* new governor failed, so re-start old one */
2245         pr_debug("starting governor %s failed\n", policy->governor->name);
2246         if (old_gov) {
2247                 policy->governor = old_gov;
2248                 if (cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT))
2249                         policy->governor = NULL;
2250                 else
2251                         cpufreq_start_governor(policy);
2252         }
2253
2254         return ret;
2255 }
2256
2257 /**
2258  *      cpufreq_update_policy - re-evaluate an existing cpufreq policy
2259  *      @cpu: CPU which shall be re-evaluated
2260  *
2261  *      Useful for policy notifiers which have different necessities
2262  *      at different times.
2263  */
2264 int cpufreq_update_policy(unsigned int cpu)
2265 {
2266         struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2267         struct cpufreq_policy new_policy;
2268         int ret;
2269
2270         if (!policy)
2271                 return -ENODEV;
2272
2273         down_write(&policy->rwsem);
2274
2275         pr_debug("updating policy for CPU %u\n", cpu);
2276         memcpy(&new_policy, policy, sizeof(*policy));
2277         new_policy.min = policy->user_policy.min;
2278         new_policy.max = policy->user_policy.max;
2279
2280         /*
2281          * BIOS might change freq behind our back
2282          * -> ask driver for current freq and notify governors about a change
2283          */
2284         if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
2285                 new_policy.cur = cpufreq_update_current_freq(policy);
2286                 if (WARN_ON(!new_policy.cur)) {
2287                         ret = -EIO;
2288                         goto unlock;
2289                 }
2290         }
2291
2292         ret = cpufreq_set_policy(policy, &new_policy);
2293
2294 unlock:
2295         up_write(&policy->rwsem);
2296
2297         cpufreq_cpu_put(policy);
2298         return ret;
2299 }
2300 EXPORT_SYMBOL(cpufreq_update_policy);
2301
2302 static int cpufreq_cpu_callback(struct notifier_block *nfb,
2303                                         unsigned long action, void *hcpu)
2304 {
2305         unsigned int cpu = (unsigned long)hcpu;
2306
2307         switch (action & ~CPU_TASKS_FROZEN) {
2308         case CPU_ONLINE:
2309                 cpufreq_online(cpu);
2310                 break;
2311
2312         case CPU_DOWN_PREPARE:
2313                 cpufreq_offline(cpu);
2314                 break;
2315
2316         case CPU_DOWN_FAILED:
2317                 cpufreq_online(cpu);
2318                 break;
2319         }
2320         return NOTIFY_OK;
2321 }
2322
2323 static struct notifier_block __refdata cpufreq_cpu_notifier = {
2324         .notifier_call = cpufreq_cpu_callback,
2325 };
2326
2327 /*********************************************************************
2328  *               BOOST                                               *
2329  *********************************************************************/
2330 static int cpufreq_boost_set_sw(int state)
2331 {
2332         struct cpufreq_frequency_table *freq_table;
2333         struct cpufreq_policy *policy;
2334         int ret = -EINVAL;
2335
2336         for_each_active_policy(policy) {
2337                 freq_table = cpufreq_frequency_get_table(policy->cpu);
2338                 if (freq_table) {
2339                         ret = cpufreq_frequency_table_cpuinfo(policy,
2340                                                         freq_table);
2341                         if (ret) {
2342                                 pr_err("%s: Policy frequency update failed\n",
2343                                        __func__);
2344                                 break;
2345                         }
2346
2347                         down_write(&policy->rwsem);
2348                         policy->user_policy.max = policy->max;
2349                         cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2350                         up_write(&policy->rwsem);
2351                 }
2352         }
2353
2354         return ret;
2355 }
2356
2357 int cpufreq_boost_trigger_state(int state)
2358 {
2359         unsigned long flags;
2360         int ret = 0;
2361
2362         if (cpufreq_driver->boost_enabled == state)
2363                 return 0;
2364
2365         write_lock_irqsave(&cpufreq_driver_lock, flags);
2366         cpufreq_driver->boost_enabled = state;
2367         write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2368
2369         ret = cpufreq_driver->set_boost(state);
2370         if (ret) {
2371                 write_lock_irqsave(&cpufreq_driver_lock, flags);
2372                 cpufreq_driver->boost_enabled = !state;
2373                 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2374
2375                 pr_err("%s: Cannot %s BOOST\n",
2376                        __func__, state ? "enable" : "disable");
2377         }
2378
2379         return ret;
2380 }
2381
2382 static bool cpufreq_boost_supported(void)
2383 {
2384         return likely(cpufreq_driver) && cpufreq_driver->set_boost;
2385 }
2386
2387 static int create_boost_sysfs_file(void)
2388 {
2389         int ret;
2390
2391         ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
2392         if (ret)
2393                 pr_err("%s: cannot register global BOOST sysfs file\n",
2394                        __func__);
2395
2396         return ret;
2397 }
2398
2399 static void remove_boost_sysfs_file(void)
2400 {
2401         if (cpufreq_boost_supported())
2402                 sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
2403 }
2404
2405 int cpufreq_enable_boost_support(void)
2406 {
2407         if (!cpufreq_driver)
2408                 return -EINVAL;
2409
2410         if (cpufreq_boost_supported())
2411                 return 0;
2412
2413         cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2414
2415         /* This will get removed on driver unregister */
2416         return create_boost_sysfs_file();
2417 }
2418 EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
2419
2420 int cpufreq_boost_enabled(void)
2421 {
2422         return cpufreq_driver->boost_enabled;
2423 }
2424 EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2425
2426 /*********************************************************************
2427  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
2428  *********************************************************************/
2429
2430 /**
2431  * cpufreq_register_driver - register a CPU Frequency driver
2432  * @driver_data: A struct cpufreq_driver containing the values#
2433  * submitted by the CPU Frequency driver.
2434  *
2435  * Registers a CPU Frequency driver to this core code. This code
2436  * returns zero on success, -EEXIST when another driver got here first
2437  * (and isn't unregistered in the meantime).
2438  *
2439  */
2440 int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2441 {
2442         unsigned long flags;
2443         int ret;
2444
2445         if (cpufreq_disabled())
2446                 return -ENODEV;
2447
2448         if (!driver_data || !driver_data->verify || !driver_data->init ||
2449             !(driver_data->setpolicy || driver_data->target_index ||
2450                     driver_data->target) ||
2451              (driver_data->setpolicy && (driver_data->target_index ||
2452                     driver_data->target)) ||
2453              (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
2454                 return -EINVAL;
2455
2456         pr_debug("trying to register driver %s\n", driver_data->name);
2457
2458         /* Protect against concurrent CPU online/offline. */
2459         get_online_cpus();
2460
2461         write_lock_irqsave(&cpufreq_driver_lock, flags);
2462         if (cpufreq_driver) {
2463                 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2464                 ret = -EEXIST;
2465                 goto out;
2466         }
2467         cpufreq_driver = driver_data;
2468         write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2469
2470         if (driver_data->setpolicy)
2471                 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2472
2473         if (cpufreq_boost_supported()) {
2474                 ret = create_boost_sysfs_file();
2475                 if (ret)
2476                         goto err_null_driver;
2477         }
2478
2479         ret = subsys_interface_register(&cpufreq_interface);
2480         if (ret)
2481                 goto err_boost_unreg;
2482
2483         if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2484             list_empty(&cpufreq_policy_list)) {
2485                 /* if all ->init() calls failed, unregister */
2486                 pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2487                          driver_data->name);
2488                 goto err_if_unreg;
2489         }
2490
2491         register_hotcpu_notifier(&cpufreq_cpu_notifier);
2492         pr_debug("driver %s up and running\n", driver_data->name);
2493
2494 out:
2495         put_online_cpus();
2496         return ret;
2497
2498 err_if_unreg:
2499         subsys_interface_unregister(&cpufreq_interface);
2500 err_boost_unreg:
2501         remove_boost_sysfs_file();
2502 err_null_driver:
2503         write_lock_irqsave(&cpufreq_driver_lock, flags);
2504         cpufreq_driver = NULL;
2505         write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2506         goto out;
2507 }
2508 EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2509
2510 /**
2511  * cpufreq_unregister_driver - unregister the current CPUFreq driver
2512  *
2513  * Unregister the current CPUFreq driver. Only call this if you have
2514  * the right to do so, i.e. if you have succeeded in initialising before!
2515  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2516  * currently not initialised.
2517  */
2518 int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2519 {
2520         unsigned long flags;
2521
2522         if (!cpufreq_driver || (driver != cpufreq_driver))
2523                 return -EINVAL;
2524
2525         pr_debug("unregistering driver %s\n", driver->name);
2526
2527         /* Protect against concurrent cpu hotplug */
2528         get_online_cpus();
2529         subsys_interface_unregister(&cpufreq_interface);
2530         remove_boost_sysfs_file();
2531         unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2532
2533         write_lock_irqsave(&cpufreq_driver_lock, flags);
2534
2535         cpufreq_driver = NULL;
2536
2537         write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2538         put_online_cpus();
2539
2540         return 0;
2541 }
2542 EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2543
2544 /*
2545  * Stop cpufreq at shutdown to make sure it isn't holding any locks
2546  * or mutexes when secondary CPUs are halted.
2547  */
2548 static struct syscore_ops cpufreq_syscore_ops = {
2549         .shutdown = cpufreq_suspend,
2550 };
2551
2552 struct kobject *cpufreq_global_kobject;
2553 EXPORT_SYMBOL(cpufreq_global_kobject);
2554
2555 static int __init cpufreq_core_init(void)
2556 {
2557         if (cpufreq_disabled())
2558                 return -ENODEV;
2559
2560         cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
2561         BUG_ON(!cpufreq_global_kobject);
2562
2563         register_syscore_ops(&cpufreq_syscore_ops);
2564
2565         return 0;
2566 }
2567 core_initcall(cpufreq_core_init);