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