OSDN Git Service

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