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