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