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