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