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