xref: /openbmc/linux/drivers/cpufreq/cpufreq.c (revision d6ff44d647a9c63911983da9979961fde306b3aa)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/drivers/cpufreq/cpufreq.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (C) 2001 Russell King
51da177e4SLinus Torvalds  *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6bb176f7dSViresh Kumar  *            (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
71da177e4SLinus Torvalds  *
8c32b6b8eSAshok Raj  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9c32b6b8eSAshok Raj  *	Added handling for CPU hotplug
108ff69732SDave Jones  *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
118ff69732SDave Jones  *	Fix handling for CPU hotplug -- affected CPUs
12c32b6b8eSAshok Raj  *
131da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
141da177e4SLinus Torvalds  * it under the terms of the GNU General Public License version 2 as
151da177e4SLinus Torvalds  * published by the Free Software Foundation.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
18db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19db701151SViresh Kumar 
205ff0a268SViresh Kumar #include <linux/cpu.h>
211da177e4SLinus Torvalds #include <linux/cpufreq.h>
221da177e4SLinus Torvalds #include <linux/delay.h>
231da177e4SLinus Torvalds #include <linux/device.h>
245ff0a268SViresh Kumar #include <linux/init.h>
255ff0a268SViresh Kumar #include <linux/kernel_stat.h>
265ff0a268SViresh Kumar #include <linux/module.h>
273fc54d37Sakpm@osdl.org #include <linux/mutex.h>
285ff0a268SViresh Kumar #include <linux/slab.h>
292f0aea93SViresh Kumar #include <linux/suspend.h>
3090de2a4aSDoug Anderson #include <linux/syscore_ops.h>
315ff0a268SViresh Kumar #include <linux/tick.h>
326f4f2723SThomas Renninger #include <trace/events/power.h>
336f4f2723SThomas Renninger 
34b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list);
35f963735aSViresh Kumar 
36f963735aSViresh Kumar static inline bool policy_is_inactive(struct cpufreq_policy *policy)
37f963735aSViresh Kumar {
38f963735aSViresh Kumar 	return cpumask_empty(policy->cpus);
39f963735aSViresh Kumar }
40f963735aSViresh Kumar 
41f963735aSViresh Kumar /* Macros to iterate over CPU policies */
42f963735aSViresh Kumar #define for_each_suitable_policy(__policy, __active)			 \
43fd7dc7e6SEric Biggers 	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
44fd7dc7e6SEric Biggers 		if ((__active) == !policy_is_inactive(__policy))
45f963735aSViresh Kumar 
46f963735aSViresh Kumar #define for_each_active_policy(__policy)		\
47f963735aSViresh Kumar 	for_each_suitable_policy(__policy, true)
48f963735aSViresh Kumar #define for_each_inactive_policy(__policy)		\
49f963735aSViresh Kumar 	for_each_suitable_policy(__policy, false)
50f963735aSViresh Kumar 
51b4f0676fSViresh Kumar #define for_each_policy(__policy)			\
52b4f0676fSViresh Kumar 	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
53b4f0676fSViresh Kumar 
54f7b27061SViresh Kumar /* Iterate over governors */
55f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list);
56f7b27061SViresh Kumar #define for_each_governor(__governor)				\
57f7b27061SViresh Kumar 	list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
58f7b27061SViresh Kumar 
591da177e4SLinus Torvalds /**
60cd878479SDave Jones  * The "cpufreq driver" - the arch- or hardware-dependent low
611da177e4SLinus Torvalds  * level driver of CPUFreq support, and its spinlock. This lock
621da177e4SLinus Torvalds  * also protects the cpufreq_cpu_data array.
631da177e4SLinus Torvalds  */
641c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver;
657a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
66bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock);
67bb176f7dSViresh Kumar 
682f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */
692f0aea93SViresh Kumar static bool cpufreq_suspended;
701da177e4SLinus Torvalds 
719c0ebcf7SViresh Kumar static inline bool has_target(void)
729c0ebcf7SViresh Kumar {
739c0ebcf7SViresh Kumar 	return cpufreq_driver->target_index || cpufreq_driver->target;
749c0ebcf7SViresh Kumar }
759c0ebcf7SViresh Kumar 
761da177e4SLinus Torvalds /* internal prototypes */
77a1317e09SViresh Kumar static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event);
78d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
790a300767SRafael J. Wysocki static int cpufreq_start_governor(struct cpufreq_policy *policy);
801da177e4SLinus Torvalds 
8136be3418SRafael J. Wysocki static inline void cpufreq_exit_governor(struct cpufreq_policy *policy)
826c9d9c81SRafael J. Wysocki {
8336be3418SRafael J. Wysocki 	(void)cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT);
846c9d9c81SRafael J. Wysocki }
851da177e4SLinus Torvalds 
8645482c70SRafael J. Wysocki static inline void cpufreq_stop_governor(struct cpufreq_policy *policy)
8745482c70SRafael J. Wysocki {
8845482c70SRafael J. Wysocki 	(void)cpufreq_governor(policy, CPUFREQ_GOV_STOP);
8945482c70SRafael J. Wysocki }
9045482c70SRafael J. Wysocki 
911da177e4SLinus Torvalds /**
921da177e4SLinus Torvalds  * Two notifier lists: the "policy" list is involved in the
931da177e4SLinus Torvalds  * validation process for a new CPU frequency policy; the
941da177e4SLinus Torvalds  * "transition" list for kernel code that needs to handle
951da177e4SLinus Torvalds  * changes to devices when the CPU clock speed changes.
961da177e4SLinus Torvalds  * The mutex locks both lists.
971da177e4SLinus Torvalds  */
98e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
99b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list;
1001da177e4SLinus Torvalds 
10174212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called;
102b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void)
103b4dfdbb3SAlan Stern {
104b4dfdbb3SAlan Stern 	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
10574212ca4SCesar Eduardo Barros 	init_cpufreq_transition_notifier_list_called = true;
106b4dfdbb3SAlan Stern 	return 0;
107b4dfdbb3SAlan Stern }
108b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list);
1091da177e4SLinus Torvalds 
110a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly;
111da584455SViresh Kumar static int cpufreq_disabled(void)
112a7b422cdSKonrad Rzeszutek Wilk {
113a7b422cdSKonrad Rzeszutek Wilk 	return off;
114a7b422cdSKonrad Rzeszutek Wilk }
115a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void)
116a7b422cdSKonrad Rzeszutek Wilk {
117a7b422cdSKonrad Rzeszutek Wilk 	off = 1;
118a7b422cdSKonrad Rzeszutek Wilk }
1193fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex);
1201da177e4SLinus Torvalds 
1214d5dcc42SViresh Kumar bool have_governor_per_policy(void)
1224d5dcc42SViresh Kumar {
1230b981e70SViresh Kumar 	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
1244d5dcc42SViresh Kumar }
1253f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy);
1264d5dcc42SViresh Kumar 
127944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
128944e9a03SViresh Kumar {
129944e9a03SViresh Kumar 	if (have_governor_per_policy())
130944e9a03SViresh Kumar 		return &policy->kobj;
131944e9a03SViresh Kumar 	else
132944e9a03SViresh Kumar 		return cpufreq_global_kobject;
133944e9a03SViresh Kumar }
134944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
135944e9a03SViresh Kumar 
1365a31d594SViresh Kumar struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
1375a31d594SViresh Kumar {
1385a31d594SViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1395a31d594SViresh Kumar 
1405a31d594SViresh Kumar 	return policy && !policy_is_inactive(policy) ?
1415a31d594SViresh Kumar 		policy->freq_table : NULL;
1425a31d594SViresh Kumar }
1435a31d594SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);
1445a31d594SViresh Kumar 
14572a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
14672a4ce34SViresh Kumar {
14772a4ce34SViresh Kumar 	u64 idle_time;
14872a4ce34SViresh Kumar 	u64 cur_wall_time;
14972a4ce34SViresh Kumar 	u64 busy_time;
15072a4ce34SViresh Kumar 
15172a4ce34SViresh Kumar 	cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
15272a4ce34SViresh Kumar 
15372a4ce34SViresh Kumar 	busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
15472a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
15572a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
15672a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
15772a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
15872a4ce34SViresh Kumar 	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
15972a4ce34SViresh Kumar 
16072a4ce34SViresh Kumar 	idle_time = cur_wall_time - busy_time;
16172a4ce34SViresh Kumar 	if (wall)
16272a4ce34SViresh Kumar 		*wall = cputime_to_usecs(cur_wall_time);
16372a4ce34SViresh Kumar 
16472a4ce34SViresh Kumar 	return cputime_to_usecs(idle_time);
16572a4ce34SViresh Kumar }
16672a4ce34SViresh Kumar 
16772a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
16872a4ce34SViresh Kumar {
16972a4ce34SViresh Kumar 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
17072a4ce34SViresh Kumar 
17172a4ce34SViresh Kumar 	if (idle_time == -1ULL)
17272a4ce34SViresh Kumar 		return get_cpu_idle_time_jiffy(cpu, wall);
17372a4ce34SViresh Kumar 	else if (!io_busy)
17472a4ce34SViresh Kumar 		idle_time += get_cpu_iowait_time_us(cpu, wall);
17572a4ce34SViresh Kumar 
17672a4ce34SViresh Kumar 	return idle_time;
17772a4ce34SViresh Kumar }
17872a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time);
17972a4ce34SViresh Kumar 
18070e9e778SViresh Kumar /*
18170e9e778SViresh Kumar  * This is a generic cpufreq init() routine which can be used by cpufreq
18270e9e778SViresh Kumar  * drivers of SMP systems. It will do following:
18370e9e778SViresh Kumar  * - validate & show freq table passed
18470e9e778SViresh Kumar  * - set policies transition latency
18570e9e778SViresh Kumar  * - policy->cpus with all possible CPUs
18670e9e778SViresh Kumar  */
18770e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy,
18870e9e778SViresh Kumar 		struct cpufreq_frequency_table *table,
18970e9e778SViresh Kumar 		unsigned int transition_latency)
19070e9e778SViresh Kumar {
19170e9e778SViresh Kumar 	int ret;
19270e9e778SViresh Kumar 
19370e9e778SViresh Kumar 	ret = cpufreq_table_validate_and_show(policy, table);
19470e9e778SViresh Kumar 	if (ret) {
19570e9e778SViresh Kumar 		pr_err("%s: invalid frequency table: %d\n", __func__, ret);
19670e9e778SViresh Kumar 		return ret;
19770e9e778SViresh Kumar 	}
19870e9e778SViresh Kumar 
19970e9e778SViresh Kumar 	policy->cpuinfo.transition_latency = transition_latency;
20070e9e778SViresh Kumar 
20170e9e778SViresh Kumar 	/*
20258405af6SShailendra Verma 	 * The driver only supports the SMP configuration where all processors
20370e9e778SViresh Kumar 	 * share the clock and voltage and clock.
20470e9e778SViresh Kumar 	 */
20570e9e778SViresh Kumar 	cpumask_setall(policy->cpus);
20670e9e778SViresh Kumar 
20770e9e778SViresh Kumar 	return 0;
20870e9e778SViresh Kumar }
20970e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init);
21070e9e778SViresh Kumar 
2111f0bd44eSRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
212652ed95dSViresh Kumar {
213652ed95dSViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
214652ed95dSViresh Kumar 
215988bed09SViresh Kumar 	return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
216988bed09SViresh Kumar }
2171f0bd44eSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
218988bed09SViresh Kumar 
219988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu)
220988bed09SViresh Kumar {
221988bed09SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
222988bed09SViresh Kumar 
223652ed95dSViresh Kumar 	if (!policy || IS_ERR(policy->clk)) {
224e837f9b5SJoe Perches 		pr_err("%s: No %s associated to cpu: %d\n",
225e837f9b5SJoe Perches 		       __func__, policy ? "clk" : "policy", cpu);
226652ed95dSViresh Kumar 		return 0;
227652ed95dSViresh Kumar 	}
228652ed95dSViresh Kumar 
229652ed95dSViresh Kumar 	return clk_get_rate(policy->clk) / 1000;
230652ed95dSViresh Kumar }
231652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get);
232652ed95dSViresh Kumar 
23350e9c852SViresh Kumar /**
23450e9c852SViresh Kumar  * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
23550e9c852SViresh Kumar  *
23650e9c852SViresh Kumar  * @cpu: cpu to find policy for.
23750e9c852SViresh Kumar  *
23850e9c852SViresh Kumar  * This returns policy for 'cpu', returns NULL if it doesn't exist.
23950e9c852SViresh Kumar  * It also increments the kobject reference count to mark it busy and so would
24050e9c852SViresh Kumar  * require a corresponding call to cpufreq_cpu_put() to decrement it back.
24150e9c852SViresh Kumar  * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
24250e9c852SViresh Kumar  * freed as that depends on the kobj count.
24350e9c852SViresh Kumar  *
24450e9c852SViresh Kumar  * Return: A valid policy on success, otherwise NULL on failure.
24550e9c852SViresh Kumar  */
2466eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
2471da177e4SLinus Torvalds {
2486eed9404SViresh Kumar 	struct cpufreq_policy *policy = NULL;
2491da177e4SLinus Torvalds 	unsigned long flags;
2501da177e4SLinus Torvalds 
2511b947c90SViresh Kumar 	if (WARN_ON(cpu >= nr_cpu_ids))
2526eed9404SViresh Kumar 		return NULL;
2536eed9404SViresh Kumar 
2541da177e4SLinus Torvalds 	/* get the cpufreq driver */
2550d1857a1SNathan Zimmer 	read_lock_irqsave(&cpufreq_driver_lock, flags);
2561da177e4SLinus Torvalds 
2576eed9404SViresh Kumar 	if (cpufreq_driver) {
2581da177e4SLinus Torvalds 		/* get the CPU */
259988bed09SViresh Kumar 		policy = cpufreq_cpu_get_raw(cpu);
2606eed9404SViresh Kumar 		if (policy)
2616eed9404SViresh Kumar 			kobject_get(&policy->kobj);
2626eed9404SViresh Kumar 	}
2636eed9404SViresh Kumar 
2646eed9404SViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
2651da177e4SLinus Torvalds 
2663a3e9e06SViresh Kumar 	return policy;
267a9144436SStephen Boyd }
2681da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
2691da177e4SLinus Torvalds 
27050e9c852SViresh Kumar /**
27150e9c852SViresh Kumar  * cpufreq_cpu_put: Decrements the usage count of a policy
27250e9c852SViresh Kumar  *
27350e9c852SViresh Kumar  * @policy: policy earlier returned by cpufreq_cpu_get().
27450e9c852SViresh Kumar  *
27550e9c852SViresh Kumar  * This decrements the kobject reference count incremented earlier by calling
27650e9c852SViresh Kumar  * cpufreq_cpu_get().
27750e9c852SViresh Kumar  */
2783a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy)
279a9144436SStephen Boyd {
2806eed9404SViresh Kumar 	kobject_put(&policy->kobj);
281a9144436SStephen Boyd }
2821da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds /*********************************************************************
2851da177e4SLinus Torvalds  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
2861da177e4SLinus Torvalds  *********************************************************************/
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds /**
2891da177e4SLinus Torvalds  * adjust_jiffies - adjust the system "loops_per_jiffy"
2901da177e4SLinus Torvalds  *
2911da177e4SLinus Torvalds  * This function alters the system "loops_per_jiffy" for the clock
2921da177e4SLinus Torvalds  * speed change. Note that loops_per_jiffy cannot be updated on SMP
2931da177e4SLinus Torvalds  * systems as each CPU might be scaled differently. So, use the arch
2941da177e4SLinus Torvalds  * per-CPU loops_per_jiffy value wherever possible.
2951da177e4SLinus Torvalds  */
29639c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
29739c132eeSViresh Kumar {
2981da177e4SLinus Torvalds #ifndef CONFIG_SMP
2991da177e4SLinus Torvalds 	static unsigned long l_p_j_ref;
3001da177e4SLinus Torvalds 	static unsigned int l_p_j_ref_freq;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	if (ci->flags & CPUFREQ_CONST_LOOPS)
3031da177e4SLinus Torvalds 		return;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	if (!l_p_j_ref_freq) {
3061da177e4SLinus Torvalds 		l_p_j_ref = loops_per_jiffy;
3071da177e4SLinus Torvalds 		l_p_j_ref_freq = ci->old;
308e837f9b5SJoe Perches 		pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
309e837f9b5SJoe Perches 			 l_p_j_ref, l_p_j_ref_freq);
3101da177e4SLinus Torvalds 	}
3110b443eadSViresh Kumar 	if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
312e08f5f5bSGautham R Shenoy 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
313e08f5f5bSGautham R Shenoy 								ci->new);
314e837f9b5SJoe Perches 		pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
315e837f9b5SJoe Perches 			 loops_per_jiffy, ci->new);
3161da177e4SLinus Torvalds 	}
3171da177e4SLinus Torvalds #endif
31839c132eeSViresh Kumar }
3191da177e4SLinus Torvalds 
3200956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
321b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	BUG_ON(irqs_disabled());
3241da177e4SLinus Torvalds 
325d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
326d5aaffa9SDirk Brandewie 		return;
327d5aaffa9SDirk Brandewie 
3281c3d85ddSRafael J. Wysocki 	freqs->flags = cpufreq_driver->flags;
3292d06d8c4SDominik Brodowski 	pr_debug("notification %u of frequency transition to %u kHz\n",
330e4472cb3SDave Jones 		 state, freqs->new);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	switch (state) {
333e4472cb3SDave Jones 
3341da177e4SLinus Torvalds 	case CPUFREQ_PRECHANGE:
335e4472cb3SDave Jones 		/* detect if the driver reported a value as "old frequency"
336e4472cb3SDave Jones 		 * which is not equal to what the cpufreq core thinks is
337e4472cb3SDave Jones 		 * "old frequency".
3381da177e4SLinus Torvalds 		 */
3391c3d85ddSRafael J. Wysocki 		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
340e4472cb3SDave Jones 			if ((policy) && (policy->cpu == freqs->cpu) &&
341e4472cb3SDave Jones 			    (policy->cur) && (policy->cur != freqs->old)) {
342e837f9b5SJoe Perches 				pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
343e4472cb3SDave Jones 					 freqs->old, policy->cur);
344e4472cb3SDave Jones 				freqs->old = policy->cur;
3451da177e4SLinus Torvalds 			}
3461da177e4SLinus Torvalds 		}
347b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
348e4472cb3SDave Jones 				CPUFREQ_PRECHANGE, freqs);
3491da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
3501da177e4SLinus Torvalds 		break;
351e4472cb3SDave Jones 
3521da177e4SLinus Torvalds 	case CPUFREQ_POSTCHANGE:
3531da177e4SLinus Torvalds 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
354e837f9b5SJoe Perches 		pr_debug("FREQ: %lu - CPU: %lu\n",
355e837f9b5SJoe Perches 			 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
35625e41933SThomas Renninger 		trace_cpu_frequency(freqs->new, freqs->cpu);
357b4dfdbb3SAlan Stern 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
358e4472cb3SDave Jones 				CPUFREQ_POSTCHANGE, freqs);
359e4472cb3SDave Jones 		if (likely(policy) && likely(policy->cpu == freqs->cpu))
360e4472cb3SDave Jones 			policy->cur = freqs->new;
3611da177e4SLinus Torvalds 		break;
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds }
364bb176f7dSViresh Kumar 
365b43a7ffbSViresh Kumar /**
366b43a7ffbSViresh Kumar  * cpufreq_notify_transition - call notifier chain and adjust_jiffies
367b43a7ffbSViresh Kumar  * on frequency transition.
368b43a7ffbSViresh Kumar  *
369b43a7ffbSViresh Kumar  * This function calls the transition notifiers and the "adjust_jiffies"
370b43a7ffbSViresh Kumar  * function. It is called twice on all CPU frequency changes that have
371b43a7ffbSViresh Kumar  * external effects.
372b43a7ffbSViresh Kumar  */
373236a9800SViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy,
374b43a7ffbSViresh Kumar 		struct cpufreq_freqs *freqs, unsigned int state)
375b43a7ffbSViresh Kumar {
376b43a7ffbSViresh Kumar 	for_each_cpu(freqs->cpu, policy->cpus)
377b43a7ffbSViresh Kumar 		__cpufreq_notify_transition(policy, freqs, state);
378b43a7ffbSViresh Kumar }
3791da177e4SLinus Torvalds 
380f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */
381236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
382f7ba3b41SViresh Kumar 		struct cpufreq_freqs *freqs, int transition_failed)
383f7ba3b41SViresh Kumar {
384f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
385f7ba3b41SViresh Kumar 	if (!transition_failed)
386f7ba3b41SViresh Kumar 		return;
387f7ba3b41SViresh Kumar 
388f7ba3b41SViresh Kumar 	swap(freqs->old, freqs->new);
389f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
390f7ba3b41SViresh Kumar 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
391f7ba3b41SViresh Kumar }
392f7ba3b41SViresh Kumar 
39312478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
39412478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs)
39512478cf0SSrivatsa S. Bhat {
396ca654dc3SSrivatsa S. Bhat 
397ca654dc3SSrivatsa S. Bhat 	/*
398ca654dc3SSrivatsa S. Bhat 	 * Catch double invocations of _begin() which lead to self-deadlock.
399ca654dc3SSrivatsa S. Bhat 	 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
400ca654dc3SSrivatsa S. Bhat 	 * doesn't invoke _begin() on their behalf, and hence the chances of
401ca654dc3SSrivatsa S. Bhat 	 * double invocations are very low. Moreover, there are scenarios
402ca654dc3SSrivatsa S. Bhat 	 * where these checks can emit false-positive warnings in these
403ca654dc3SSrivatsa S. Bhat 	 * drivers; so we avoid that by skipping them altogether.
404ca654dc3SSrivatsa S. Bhat 	 */
405ca654dc3SSrivatsa S. Bhat 	WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
406ca654dc3SSrivatsa S. Bhat 				&& current == policy->transition_task);
407ca654dc3SSrivatsa S. Bhat 
40812478cf0SSrivatsa S. Bhat wait:
40912478cf0SSrivatsa S. Bhat 	wait_event(policy->transition_wait, !policy->transition_ongoing);
41012478cf0SSrivatsa S. Bhat 
41112478cf0SSrivatsa S. Bhat 	spin_lock(&policy->transition_lock);
41212478cf0SSrivatsa S. Bhat 
41312478cf0SSrivatsa S. Bhat 	if (unlikely(policy->transition_ongoing)) {
41412478cf0SSrivatsa S. Bhat 		spin_unlock(&policy->transition_lock);
41512478cf0SSrivatsa S. Bhat 		goto wait;
41612478cf0SSrivatsa S. Bhat 	}
41712478cf0SSrivatsa S. Bhat 
41812478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = true;
419ca654dc3SSrivatsa S. Bhat 	policy->transition_task = current;
42012478cf0SSrivatsa S. Bhat 
42112478cf0SSrivatsa S. Bhat 	spin_unlock(&policy->transition_lock);
42212478cf0SSrivatsa S. Bhat 
42312478cf0SSrivatsa S. Bhat 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
42412478cf0SSrivatsa S. Bhat }
42512478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
42612478cf0SSrivatsa S. Bhat 
42712478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
42812478cf0SSrivatsa S. Bhat 		struct cpufreq_freqs *freqs, int transition_failed)
42912478cf0SSrivatsa S. Bhat {
43012478cf0SSrivatsa S. Bhat 	if (unlikely(WARN_ON(!policy->transition_ongoing)))
43112478cf0SSrivatsa S. Bhat 		return;
43212478cf0SSrivatsa S. Bhat 
43312478cf0SSrivatsa S. Bhat 	cpufreq_notify_post_transition(policy, freqs, transition_failed);
43412478cf0SSrivatsa S. Bhat 
43512478cf0SSrivatsa S. Bhat 	policy->transition_ongoing = false;
436ca654dc3SSrivatsa S. Bhat 	policy->transition_task = NULL;
43712478cf0SSrivatsa S. Bhat 
43812478cf0SSrivatsa S. Bhat 	wake_up(&policy->transition_wait);
43912478cf0SSrivatsa S. Bhat }
44012478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
44112478cf0SSrivatsa S. Bhat 
442b7898fdaSRafael J. Wysocki /*
443b7898fdaSRafael J. Wysocki  * Fast frequency switching status count.  Positive means "enabled", negative
444b7898fdaSRafael J. Wysocki  * means "disabled" and 0 means "not decided yet".
445b7898fdaSRafael J. Wysocki  */
446b7898fdaSRafael J. Wysocki static int cpufreq_fast_switch_count;
447b7898fdaSRafael J. Wysocki static DEFINE_MUTEX(cpufreq_fast_switch_lock);
448b7898fdaSRafael J. Wysocki 
449b7898fdaSRafael J. Wysocki static void cpufreq_list_transition_notifiers(void)
450b7898fdaSRafael J. Wysocki {
451b7898fdaSRafael J. Wysocki 	struct notifier_block *nb;
452b7898fdaSRafael J. Wysocki 
453b7898fdaSRafael J. Wysocki 	pr_info("Registered transition notifiers:\n");
454b7898fdaSRafael J. Wysocki 
455b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_transition_notifier_list.mutex);
456b7898fdaSRafael J. Wysocki 
457b7898fdaSRafael J. Wysocki 	for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
458b7898fdaSRafael J. Wysocki 		pr_info("%pF\n", nb->notifier_call);
459b7898fdaSRafael J. Wysocki 
460b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_transition_notifier_list.mutex);
461b7898fdaSRafael J. Wysocki }
462b7898fdaSRafael J. Wysocki 
463b7898fdaSRafael J. Wysocki /**
464b7898fdaSRafael J. Wysocki  * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
465b7898fdaSRafael J. Wysocki  * @policy: cpufreq policy to enable fast frequency switching for.
466b7898fdaSRafael J. Wysocki  *
467b7898fdaSRafael J. Wysocki  * Try to enable fast frequency switching for @policy.
468b7898fdaSRafael J. Wysocki  *
469b7898fdaSRafael J. Wysocki  * The attempt will fail if there is at least one transition notifier registered
470b7898fdaSRafael J. Wysocki  * at this point, as fast frequency switching is quite fundamentally at odds
471b7898fdaSRafael J. Wysocki  * with transition notifiers.  Thus if successful, it will make registration of
472b7898fdaSRafael J. Wysocki  * transition notifiers fail going forward.
473b7898fdaSRafael J. Wysocki  */
474b7898fdaSRafael J. Wysocki void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
475b7898fdaSRafael J. Wysocki {
476b7898fdaSRafael J. Wysocki 	lockdep_assert_held(&policy->rwsem);
477b7898fdaSRafael J. Wysocki 
478b7898fdaSRafael J. Wysocki 	if (!policy->fast_switch_possible)
479b7898fdaSRafael J. Wysocki 		return;
480b7898fdaSRafael J. Wysocki 
481b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_fast_switch_lock);
482b7898fdaSRafael J. Wysocki 	if (cpufreq_fast_switch_count >= 0) {
483b7898fdaSRafael J. Wysocki 		cpufreq_fast_switch_count++;
484b7898fdaSRafael J. Wysocki 		policy->fast_switch_enabled = true;
485b7898fdaSRafael J. Wysocki 	} else {
486b7898fdaSRafael J. Wysocki 		pr_warn("CPU%u: Fast frequency switching not enabled\n",
487b7898fdaSRafael J. Wysocki 			policy->cpu);
488b7898fdaSRafael J. Wysocki 		cpufreq_list_transition_notifiers();
489b7898fdaSRafael J. Wysocki 	}
490b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_fast_switch_lock);
491b7898fdaSRafael J. Wysocki }
492b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
493b7898fdaSRafael J. Wysocki 
4946c9d9c81SRafael J. Wysocki /**
4956c9d9c81SRafael J. Wysocki  * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
4966c9d9c81SRafael J. Wysocki  * @policy: cpufreq policy to disable fast frequency switching for.
4976c9d9c81SRafael J. Wysocki  */
4986c9d9c81SRafael J. Wysocki void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
499b7898fdaSRafael J. Wysocki {
500b7898fdaSRafael J. Wysocki 	mutex_lock(&cpufreq_fast_switch_lock);
501b7898fdaSRafael J. Wysocki 	if (policy->fast_switch_enabled) {
502b7898fdaSRafael J. Wysocki 		policy->fast_switch_enabled = false;
503b7898fdaSRafael J. Wysocki 		if (!WARN_ON(cpufreq_fast_switch_count <= 0))
504b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count--;
505b7898fdaSRafael J. Wysocki 	}
506b7898fdaSRafael J. Wysocki 	mutex_unlock(&cpufreq_fast_switch_lock);
507b7898fdaSRafael J. Wysocki }
5086c9d9c81SRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds /*********************************************************************
5111da177e4SLinus Torvalds  *                          SYSFS INTERFACE                          *
5121da177e4SLinus Torvalds  *********************************************************************/
5138a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj,
5146f19efc0SLukasz Majewski 				 struct attribute *attr, char *buf)
5156f19efc0SLukasz Majewski {
5166f19efc0SLukasz Majewski 	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
5176f19efc0SLukasz Majewski }
5186f19efc0SLukasz Majewski 
5196f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
5206f19efc0SLukasz Majewski 				  const char *buf, size_t count)
5216f19efc0SLukasz Majewski {
5226f19efc0SLukasz Majewski 	int ret, enable;
5236f19efc0SLukasz Majewski 
5246f19efc0SLukasz Majewski 	ret = sscanf(buf, "%d", &enable);
5256f19efc0SLukasz Majewski 	if (ret != 1 || enable < 0 || enable > 1)
5266f19efc0SLukasz Majewski 		return -EINVAL;
5276f19efc0SLukasz Majewski 
5286f19efc0SLukasz Majewski 	if (cpufreq_boost_trigger_state(enable)) {
529e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST!\n",
530e837f9b5SJoe Perches 		       __func__, enable ? "enable" : "disable");
5316f19efc0SLukasz Majewski 		return -EINVAL;
5326f19efc0SLukasz Majewski 	}
5336f19efc0SLukasz Majewski 
534e837f9b5SJoe Perches 	pr_debug("%s: cpufreq BOOST %s\n",
535e837f9b5SJoe Perches 		 __func__, enable ? "enabled" : "disabled");
5366f19efc0SLukasz Majewski 
5376f19efc0SLukasz Majewski 	return count;
5386f19efc0SLukasz Majewski }
5396f19efc0SLukasz Majewski define_one_global_rw(boost);
5401da177e4SLinus Torvalds 
54142f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor)
5423bcb09a3SJeremy Fitzhardinge {
5433bcb09a3SJeremy Fitzhardinge 	struct cpufreq_governor *t;
5443bcb09a3SJeremy Fitzhardinge 
545f7b27061SViresh Kumar 	for_each_governor(t)
5467c4f4539SRasmus Villemoes 		if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
5473bcb09a3SJeremy Fitzhardinge 			return t;
5483bcb09a3SJeremy Fitzhardinge 
5493bcb09a3SJeremy Fitzhardinge 	return NULL;
5503bcb09a3SJeremy Fitzhardinge }
5513bcb09a3SJeremy Fitzhardinge 
5521da177e4SLinus Torvalds /**
5531da177e4SLinus Torvalds  * cpufreq_parse_governor - parse a governor string
5541da177e4SLinus Torvalds  */
5551da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
5561da177e4SLinus Torvalds 				struct cpufreq_governor **governor)
5571da177e4SLinus Torvalds {
5583bcb09a3SJeremy Fitzhardinge 	int err = -EINVAL;
5593bcb09a3SJeremy Fitzhardinge 
5601c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
5617c4f4539SRasmus Villemoes 		if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
5621da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_PERFORMANCE;
5633bcb09a3SJeremy Fitzhardinge 			err = 0;
5647c4f4539SRasmus Villemoes 		} else if (!strncasecmp(str_governor, "powersave",
565e08f5f5bSGautham R Shenoy 						CPUFREQ_NAME_LEN)) {
5661da177e4SLinus Torvalds 			*policy = CPUFREQ_POLICY_POWERSAVE;
5673bcb09a3SJeremy Fitzhardinge 			err = 0;
5681da177e4SLinus Torvalds 		}
5692e1cc3a5SViresh Kumar 	} else {
5701da177e4SLinus Torvalds 		struct cpufreq_governor *t;
5713bcb09a3SJeremy Fitzhardinge 
5723fc54d37Sakpm@osdl.org 		mutex_lock(&cpufreq_governor_mutex);
5733bcb09a3SJeremy Fitzhardinge 
57442f91fa1SViresh Kumar 		t = find_governor(str_governor);
5753bcb09a3SJeremy Fitzhardinge 
576ea714970SJeremy Fitzhardinge 		if (t == NULL) {
577ea714970SJeremy Fitzhardinge 			int ret;
578ea714970SJeremy Fitzhardinge 
579ea714970SJeremy Fitzhardinge 			mutex_unlock(&cpufreq_governor_mutex);
5801a8e1463SKees Cook 			ret = request_module("cpufreq_%s", str_governor);
581ea714970SJeremy Fitzhardinge 			mutex_lock(&cpufreq_governor_mutex);
582ea714970SJeremy Fitzhardinge 
583ea714970SJeremy Fitzhardinge 			if (ret == 0)
58442f91fa1SViresh Kumar 				t = find_governor(str_governor);
585ea714970SJeremy Fitzhardinge 		}
586ea714970SJeremy Fitzhardinge 
5873bcb09a3SJeremy Fitzhardinge 		if (t != NULL) {
5881da177e4SLinus Torvalds 			*governor = t;
5893bcb09a3SJeremy Fitzhardinge 			err = 0;
5901da177e4SLinus Torvalds 		}
5913bcb09a3SJeremy Fitzhardinge 
5923bcb09a3SJeremy Fitzhardinge 		mutex_unlock(&cpufreq_governor_mutex);
5931da177e4SLinus Torvalds 	}
5943bcb09a3SJeremy Fitzhardinge 	return err;
5951da177e4SLinus Torvalds }
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds /**
598e08f5f5bSGautham R Shenoy  * cpufreq_per_cpu_attr_read() / show_##file_name() -
599e08f5f5bSGautham R Shenoy  * print out cpufreq information
6001da177e4SLinus Torvalds  *
6011da177e4SLinus Torvalds  * Write out information from cpufreq_driver->policy[cpu]; object must be
6021da177e4SLinus Torvalds  * "unsigned int".
6031da177e4SLinus Torvalds  */
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds #define show_one(file_name, object)			\
6061da177e4SLinus Torvalds static ssize_t show_##file_name				\
6071da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf)		\
6081da177e4SLinus Torvalds {							\
6091da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", policy->object);	\
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq);
6131da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq);
614ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
6151da177e4SLinus Torvalds show_one(scaling_min_freq, min);
6161da177e4SLinus Torvalds show_one(scaling_max_freq, max);
617c034b02eSDirk Brandewie 
61809347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
619c034b02eSDirk Brandewie {
620c034b02eSDirk Brandewie 	ssize_t ret;
621c034b02eSDirk Brandewie 
622c034b02eSDirk Brandewie 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
623c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
624c034b02eSDirk Brandewie 	else
625c034b02eSDirk Brandewie 		ret = sprintf(buf, "%u\n", policy->cur);
626c034b02eSDirk Brandewie 	return ret;
627c034b02eSDirk Brandewie }
6281da177e4SLinus Torvalds 
629037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
6303a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy);
6317970e08bSThomas Renninger 
6321da177e4SLinus Torvalds /**
6331da177e4SLinus Torvalds  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
6341da177e4SLinus Torvalds  */
6351da177e4SLinus Torvalds #define store_one(file_name, object)			\
6361da177e4SLinus Torvalds static ssize_t store_##file_name					\
6371da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count)		\
6381da177e4SLinus Torvalds {									\
639619c144cSVince Hsu 	int ret, temp;							\
6401da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;				\
6411da177e4SLinus Torvalds 									\
6428fa5b631SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));			\
6431da177e4SLinus Torvalds 									\
6441da177e4SLinus Torvalds 	ret = sscanf(buf, "%u", &new_policy.object);			\
6451da177e4SLinus Torvalds 	if (ret != 1)							\
6461da177e4SLinus Torvalds 		return -EINVAL;						\
6471da177e4SLinus Torvalds 									\
648619c144cSVince Hsu 	temp = new_policy.object;					\
649037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);		\
650619c144cSVince Hsu 	if (!ret)							\
651619c144cSVince Hsu 		policy->user_policy.object = temp;			\
6521da177e4SLinus Torvalds 									\
6531da177e4SLinus Torvalds 	return ret ? ret : count;					\
6541da177e4SLinus Torvalds }
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds store_one(scaling_min_freq, min);
6571da177e4SLinus Torvalds store_one(scaling_max_freq, max);
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds /**
6601da177e4SLinus Torvalds  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
6611da177e4SLinus Torvalds  */
662e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
663e08f5f5bSGautham R Shenoy 					char *buf)
6641da177e4SLinus Torvalds {
665d92d50a4SViresh Kumar 	unsigned int cur_freq = __cpufreq_get(policy);
6661da177e4SLinus Torvalds 	if (!cur_freq)
6671da177e4SLinus Torvalds 		return sprintf(buf, "<unknown>");
6681da177e4SLinus Torvalds 	return sprintf(buf, "%u\n", cur_freq);
6691da177e4SLinus Torvalds }
6701da177e4SLinus Torvalds 
6711da177e4SLinus Torvalds /**
6721da177e4SLinus Torvalds  * show_scaling_governor - show the current policy for the specified CPU
6731da177e4SLinus Torvalds  */
674905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
6751da177e4SLinus Torvalds {
6761da177e4SLinus Torvalds 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
6771da177e4SLinus Torvalds 		return sprintf(buf, "powersave\n");
6781da177e4SLinus Torvalds 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
6791da177e4SLinus Torvalds 		return sprintf(buf, "performance\n");
6801da177e4SLinus Torvalds 	else if (policy->governor)
6814b972f0bSviresh kumar 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
68229464f28SDave Jones 				policy->governor->name);
6831da177e4SLinus Torvalds 	return -EINVAL;
6841da177e4SLinus Torvalds }
6851da177e4SLinus Torvalds 
6861da177e4SLinus Torvalds /**
6871da177e4SLinus Torvalds  * store_scaling_governor - store policy for the specified CPU
6881da177e4SLinus Torvalds  */
6891da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
6901da177e4SLinus Torvalds 					const char *buf, size_t count)
6911da177e4SLinus Torvalds {
6925136fa56SSrivatsa S. Bhat 	int ret;
6931da177e4SLinus Torvalds 	char	str_governor[16];
6941da177e4SLinus Torvalds 	struct cpufreq_policy new_policy;
6951da177e4SLinus Torvalds 
6968fa5b631SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	ret = sscanf(buf, "%15s", str_governor);
6991da177e4SLinus Torvalds 	if (ret != 1)
7001da177e4SLinus Torvalds 		return -EINVAL;
7011da177e4SLinus Torvalds 
702e08f5f5bSGautham R Shenoy 	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
703e08f5f5bSGautham R Shenoy 						&new_policy.governor))
7041da177e4SLinus Torvalds 		return -EINVAL;
7051da177e4SLinus Torvalds 
706037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
70788dc4384SViresh Kumar 	return ret ? ret : count;
7081da177e4SLinus Torvalds }
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds /**
7111da177e4SLinus Torvalds  * show_scaling_driver - show the cpufreq driver currently loaded
7121da177e4SLinus Torvalds  */
7131da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
7141da177e4SLinus Torvalds {
7151c3d85ddSRafael J. Wysocki 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181da177e4SLinus Torvalds /**
7191da177e4SLinus Torvalds  * show_scaling_available_governors - show the available CPUfreq governors
7201da177e4SLinus Torvalds  */
7211da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
7221da177e4SLinus Torvalds 						char *buf)
7231da177e4SLinus Torvalds {
7241da177e4SLinus Torvalds 	ssize_t i = 0;
7251da177e4SLinus Torvalds 	struct cpufreq_governor *t;
7261da177e4SLinus Torvalds 
7279c0ebcf7SViresh Kumar 	if (!has_target()) {
7281da177e4SLinus Torvalds 		i += sprintf(buf, "performance powersave");
7291da177e4SLinus Torvalds 		goto out;
7301da177e4SLinus Torvalds 	}
7311da177e4SLinus Torvalds 
732f7b27061SViresh Kumar 	for_each_governor(t) {
73329464f28SDave Jones 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
73429464f28SDave Jones 		    - (CPUFREQ_NAME_LEN + 2)))
7351da177e4SLinus Torvalds 			goto out;
7364b972f0bSviresh kumar 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
7371da177e4SLinus Torvalds 	}
7381da177e4SLinus Torvalds out:
7391da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
7401da177e4SLinus Torvalds 	return i;
7411da177e4SLinus Torvalds }
742e8628dd0SDarrick J. Wong 
743f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
7441da177e4SLinus Torvalds {
7451da177e4SLinus Torvalds 	ssize_t i = 0;
7461da177e4SLinus Torvalds 	unsigned int cpu;
7471da177e4SLinus Torvalds 
748835481d9SRusty Russell 	for_each_cpu(cpu, mask) {
7491da177e4SLinus Torvalds 		if (i)
7501da177e4SLinus Torvalds 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
7511da177e4SLinus Torvalds 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
7521da177e4SLinus Torvalds 		if (i >= (PAGE_SIZE - 5))
7531da177e4SLinus Torvalds 			break;
7541da177e4SLinus Torvalds 	}
7551da177e4SLinus Torvalds 	i += sprintf(&buf[i], "\n");
7561da177e4SLinus Torvalds 	return i;
7571da177e4SLinus Torvalds }
758f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
7591da177e4SLinus Torvalds 
760e8628dd0SDarrick J. Wong /**
761e8628dd0SDarrick J. Wong  * show_related_cpus - show the CPUs affected by each transition even if
762e8628dd0SDarrick J. Wong  * hw coordination is in use
763e8628dd0SDarrick J. Wong  */
764e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
765e8628dd0SDarrick J. Wong {
766f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->related_cpus, buf);
767e8628dd0SDarrick J. Wong }
768e8628dd0SDarrick J. Wong 
769e8628dd0SDarrick J. Wong /**
770e8628dd0SDarrick J. Wong  * show_affected_cpus - show the CPUs affected by each transition
771e8628dd0SDarrick J. Wong  */
772e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
773e8628dd0SDarrick J. Wong {
774f4fd3797SLan Tianyu 	return cpufreq_show_cpus(policy->cpus, buf);
775e8628dd0SDarrick J. Wong }
776e8628dd0SDarrick J. Wong 
7779e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
7789e76988eSVenki Pallipadi 					const char *buf, size_t count)
7799e76988eSVenki Pallipadi {
7809e76988eSVenki Pallipadi 	unsigned int freq = 0;
7819e76988eSVenki Pallipadi 	unsigned int ret;
7829e76988eSVenki Pallipadi 
783879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->store_setspeed)
7849e76988eSVenki Pallipadi 		return -EINVAL;
7859e76988eSVenki Pallipadi 
7869e76988eSVenki Pallipadi 	ret = sscanf(buf, "%u", &freq);
7879e76988eSVenki Pallipadi 	if (ret != 1)
7889e76988eSVenki Pallipadi 		return -EINVAL;
7899e76988eSVenki Pallipadi 
7909e76988eSVenki Pallipadi 	policy->governor->store_setspeed(policy, freq);
7919e76988eSVenki Pallipadi 
7929e76988eSVenki Pallipadi 	return count;
7939e76988eSVenki Pallipadi }
7949e76988eSVenki Pallipadi 
7959e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
7969e76988eSVenki Pallipadi {
797879000f9SCHIKAMA masaki 	if (!policy->governor || !policy->governor->show_setspeed)
7989e76988eSVenki Pallipadi 		return sprintf(buf, "<unsupported>\n");
7999e76988eSVenki Pallipadi 
8009e76988eSVenki Pallipadi 	return policy->governor->show_setspeed(policy, buf);
8019e76988eSVenki Pallipadi }
8021da177e4SLinus Torvalds 
803e2f74f35SThomas Renninger /**
8048bf1ac72Sviresh kumar  * show_bios_limit - show the current cpufreq HW/BIOS limitation
805e2f74f35SThomas Renninger  */
806e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
807e2f74f35SThomas Renninger {
808e2f74f35SThomas Renninger 	unsigned int limit;
809e2f74f35SThomas Renninger 	int ret;
8101c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
8111c3d85ddSRafael J. Wysocki 		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
812e2f74f35SThomas Renninger 		if (!ret)
813e2f74f35SThomas Renninger 			return sprintf(buf, "%u\n", limit);
814e2f74f35SThomas Renninger 	}
815e2f74f35SThomas Renninger 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
816e2f74f35SThomas Renninger }
817e2f74f35SThomas Renninger 
8186dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
8196dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq);
8206dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq);
8216dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency);
8226dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors);
8236dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver);
8246dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq);
8256dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit);
8266dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus);
8276dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus);
8286dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq);
8296dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq);
8306dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor);
8316dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed);
8321da177e4SLinus Torvalds 
8331da177e4SLinus Torvalds static struct attribute *default_attrs[] = {
8341da177e4SLinus Torvalds 	&cpuinfo_min_freq.attr,
8351da177e4SLinus Torvalds 	&cpuinfo_max_freq.attr,
836ed129784SThomas Renninger 	&cpuinfo_transition_latency.attr,
8371da177e4SLinus Torvalds 	&scaling_min_freq.attr,
8381da177e4SLinus Torvalds 	&scaling_max_freq.attr,
8391da177e4SLinus Torvalds 	&affected_cpus.attr,
840e8628dd0SDarrick J. Wong 	&related_cpus.attr,
8411da177e4SLinus Torvalds 	&scaling_governor.attr,
8421da177e4SLinus Torvalds 	&scaling_driver.attr,
8431da177e4SLinus Torvalds 	&scaling_available_governors.attr,
8449e76988eSVenki Pallipadi 	&scaling_setspeed.attr,
8451da177e4SLinus Torvalds 	NULL
8461da177e4SLinus Torvalds };
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
8491da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr)
8501da177e4SLinus Torvalds 
8511da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
8521da177e4SLinus Torvalds {
8531da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
8541da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
8551b750e3bSViresh Kumar 	ssize_t ret;
8566eed9404SViresh Kumar 
857ad7722daSviresh kumar 	down_read(&policy->rwsem);
858e08f5f5bSGautham R Shenoy 	ret = fattr->show(policy, buf);
859ad7722daSviresh kumar 	up_read(&policy->rwsem);
8601b750e3bSViresh Kumar 
8611da177e4SLinus Torvalds 	return ret;
8621da177e4SLinus Torvalds }
8631da177e4SLinus Torvalds 
8641da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr,
8651da177e4SLinus Torvalds 		     const char *buf, size_t count)
8661da177e4SLinus Torvalds {
8671da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
8681da177e4SLinus Torvalds 	struct freq_attr *fattr = to_attr(attr);
869a07530b4SDave Jones 	ssize_t ret = -EINVAL;
8706eed9404SViresh Kumar 
8714f750c93SSrivatsa S. Bhat 	get_online_cpus();
8724f750c93SSrivatsa S. Bhat 
8736541aef0SRafael J. Wysocki 	if (cpu_online(policy->cpu)) {
874ad7722daSviresh kumar 		down_write(&policy->rwsem);
875e08f5f5bSGautham R Shenoy 		ret = fattr->store(policy, buf, count);
876ad7722daSviresh kumar 		up_write(&policy->rwsem);
8776541aef0SRafael J. Wysocki 	}
8786541aef0SRafael J. Wysocki 
8794f750c93SSrivatsa S. Bhat 	put_online_cpus();
8804f750c93SSrivatsa S. Bhat 
8811da177e4SLinus Torvalds 	return ret;
8821da177e4SLinus Torvalds }
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj)
8851da177e4SLinus Torvalds {
8861da177e4SLinus Torvalds 	struct cpufreq_policy *policy = to_policy(kobj);
8872d06d8c4SDominik Brodowski 	pr_debug("last reference is dropped\n");
8881da177e4SLinus Torvalds 	complete(&policy->kobj_unregister);
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
89152cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = {
8921da177e4SLinus Torvalds 	.show	= show,
8931da177e4SLinus Torvalds 	.store	= store,
8941da177e4SLinus Torvalds };
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = {
8971da177e4SLinus Torvalds 	.sysfs_ops	= &sysfs_ops,
8981da177e4SLinus Torvalds 	.default_attrs	= default_attrs,
8991da177e4SLinus Torvalds 	.release	= cpufreq_sysfs_release,
9001da177e4SLinus Torvalds };
9011da177e4SLinus Torvalds 
90287549141SViresh Kumar static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
90387549141SViresh Kumar {
90487549141SViresh Kumar 	struct device *cpu_dev;
90587549141SViresh Kumar 
90687549141SViresh Kumar 	pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu);
90787549141SViresh Kumar 
90887549141SViresh Kumar 	if (!policy)
90987549141SViresh Kumar 		return 0;
91087549141SViresh Kumar 
91187549141SViresh Kumar 	cpu_dev = get_cpu_device(cpu);
91287549141SViresh Kumar 	if (WARN_ON(!cpu_dev))
91387549141SViresh Kumar 		return 0;
91487549141SViresh Kumar 
91587549141SViresh Kumar 	return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq");
91687549141SViresh Kumar }
91787549141SViresh Kumar 
91887549141SViresh Kumar static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
91987549141SViresh Kumar {
92087549141SViresh Kumar 	struct device *cpu_dev;
92187549141SViresh Kumar 
92287549141SViresh Kumar 	pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu);
92387549141SViresh Kumar 
92487549141SViresh Kumar 	cpu_dev = get_cpu_device(cpu);
92587549141SViresh Kumar 	if (WARN_ON(!cpu_dev))
92687549141SViresh Kumar 		return;
92787549141SViresh Kumar 
92887549141SViresh Kumar 	sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
92987549141SViresh Kumar }
93087549141SViresh Kumar 
93187549141SViresh Kumar /* Add/remove symlinks for all related CPUs */
932308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
93319d6f7ecSDave Jones {
93419d6f7ecSDave Jones 	unsigned int j;
93519d6f7ecSDave Jones 	int ret = 0;
93619d6f7ecSDave Jones 
93787549141SViresh Kumar 	/* Some related CPUs might not be present (physically hotplugged) */
938559ed407SRafael J. Wysocki 	for_each_cpu(j, policy->real_cpus) {
93987549141SViresh Kumar 		ret = add_cpu_dev_symlink(policy, j);
94071c3461eSRafael J. Wysocki 		if (ret)
94171c3461eSRafael J. Wysocki 			break;
94219d6f7ecSDave Jones 	}
94387549141SViresh Kumar 
94419d6f7ecSDave Jones 	return ret;
94519d6f7ecSDave Jones }
94619d6f7ecSDave Jones 
94787549141SViresh Kumar static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy)
94887549141SViresh Kumar {
94987549141SViresh Kumar 	unsigned int j;
95087549141SViresh Kumar 
95187549141SViresh Kumar 	/* Some related CPUs might not be present (physically hotplugged) */
95296bdda61SViresh Kumar 	for_each_cpu(j, policy->real_cpus)
95387549141SViresh Kumar 		remove_cpu_dev_symlink(policy, j);
95487549141SViresh Kumar }
95587549141SViresh Kumar 
956d9612a49SRafael J. Wysocki static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
957909a694eSDave Jones {
958909a694eSDave Jones 	struct freq_attr **drv_attr;
959909a694eSDave Jones 	int ret = 0;
960909a694eSDave Jones 
961909a694eSDave Jones 	/* set up files for this cpu device */
9621c3d85ddSRafael J. Wysocki 	drv_attr = cpufreq_driver->attr;
963f13f1184SViresh Kumar 	while (drv_attr && *drv_attr) {
964909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
965909a694eSDave Jones 		if (ret)
9666d4e81edSTomeu Vizoso 			return ret;
967909a694eSDave Jones 		drv_attr++;
968909a694eSDave Jones 	}
9691c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->get) {
970909a694eSDave Jones 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
971909a694eSDave Jones 		if (ret)
9726d4e81edSTomeu Vizoso 			return ret;
973909a694eSDave Jones 	}
974c034b02eSDirk Brandewie 
975909a694eSDave Jones 	ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
976909a694eSDave Jones 	if (ret)
9776d4e81edSTomeu Vizoso 		return ret;
978c034b02eSDirk Brandewie 
9791c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->bios_limit) {
980e2f74f35SThomas Renninger 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
981e2f74f35SThomas Renninger 		if (ret)
9826d4e81edSTomeu Vizoso 			return ret;
983e2f74f35SThomas Renninger 	}
984909a694eSDave Jones 
9856d4e81edSTomeu Vizoso 	return cpufreq_add_dev_symlink(policy);
986e18f1682SSrivatsa S. Bhat }
987e18f1682SSrivatsa S. Bhat 
988de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_default_governor(void)
989de1df26bSRafael J. Wysocki {
990de1df26bSRafael J. Wysocki 	return NULL;
991de1df26bSRafael J. Wysocki }
992de1df26bSRafael J. Wysocki 
9937f0fa40fSViresh Kumar static int cpufreq_init_policy(struct cpufreq_policy *policy)
994e18f1682SSrivatsa S. Bhat {
9956e2c89d1Sviresh kumar 	struct cpufreq_governor *gov = NULL;
996e18f1682SSrivatsa S. Bhat 	struct cpufreq_policy new_policy;
997e18f1682SSrivatsa S. Bhat 
998d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
999a27a9ab7SJason Baron 
10006e2c89d1Sviresh kumar 	/* Update governor of new_policy to the governor used before hotplug */
10014573237bSViresh Kumar 	gov = find_governor(policy->last_governor);
1002de1df26bSRafael J. Wysocki 	if (gov) {
10036e2c89d1Sviresh kumar 		pr_debug("Restoring governor %s for cpu %d\n",
10046e2c89d1Sviresh kumar 				policy->governor->name, policy->cpu);
1005de1df26bSRafael J. Wysocki 	} else {
1006de1df26bSRafael J. Wysocki 		gov = cpufreq_default_governor();
1007de1df26bSRafael J. Wysocki 		if (!gov)
1008de1df26bSRafael J. Wysocki 			return -ENODATA;
1009de1df26bSRafael J. Wysocki 	}
10106e2c89d1Sviresh kumar 
10116e2c89d1Sviresh kumar 	new_policy.governor = gov;
10126e2c89d1Sviresh kumar 
101369030dd1SSrinivas Pandruvada 	/* Use the default policy if there is no last_policy. */
101469030dd1SSrinivas Pandruvada 	if (cpufreq_driver->setpolicy) {
101569030dd1SSrinivas Pandruvada 		if (policy->last_policy)
101669030dd1SSrinivas Pandruvada 			new_policy.policy = policy->last_policy;
101769030dd1SSrinivas Pandruvada 		else
101869030dd1SSrinivas Pandruvada 			cpufreq_parse_governor(gov->name, &new_policy.policy,
101969030dd1SSrinivas Pandruvada 					       NULL);
102069030dd1SSrinivas Pandruvada 	}
1021ecf7e461SDave Jones 	/* set default policy */
10227f0fa40fSViresh Kumar 	return cpufreq_set_policy(policy, &new_policy);
1023909a694eSDave Jones }
1024909a694eSDave Jones 
1025d9612a49SRafael J. Wysocki static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1026fcf80582SViresh Kumar {
10279c0ebcf7SViresh Kumar 	int ret = 0;
1028fcf80582SViresh Kumar 
1029bb29ae15SViresh Kumar 	/* Has this CPU been taken care of already? */
1030bb29ae15SViresh Kumar 	if (cpumask_test_cpu(cpu, policy->cpus))
1031bb29ae15SViresh Kumar 		return 0;
1032bb29ae15SViresh Kumar 
103349f18560SViresh Kumar 	down_write(&policy->rwsem);
103445482c70SRafael J. Wysocki 	if (has_target())
103545482c70SRafael J. Wysocki 		cpufreq_stop_governor(policy);
1036fcf80582SViresh Kumar 
1037fcf80582SViresh Kumar 	cpumask_set_cpu(cpu, policy->cpus);
10382eaa3e2dSViresh Kumar 
10399c0ebcf7SViresh Kumar 	if (has_target()) {
10400a300767SRafael J. Wysocki 		ret = cpufreq_start_governor(policy);
104149f18560SViresh Kumar 		if (ret)
10423de9bdebSViresh Kumar 			pr_err("%s: Failed to start governor\n", __func__);
1043820c6ca2SViresh Kumar 	}
104449f18560SViresh Kumar 	up_write(&policy->rwsem);
1045fcf80582SViresh Kumar 	return ret;
1046fcf80582SViresh Kumar }
1047fcf80582SViresh Kumar 
104811eb69b9SViresh Kumar static void handle_update(struct work_struct *work)
104911eb69b9SViresh Kumar {
105011eb69b9SViresh Kumar 	struct cpufreq_policy *policy =
105111eb69b9SViresh Kumar 		container_of(work, struct cpufreq_policy, update);
105211eb69b9SViresh Kumar 	unsigned int cpu = policy->cpu;
105311eb69b9SViresh Kumar 	pr_debug("handle_update for cpu %u called\n", cpu);
105411eb69b9SViresh Kumar 	cpufreq_update_policy(cpu);
1055fcf80582SViresh Kumar }
10561da177e4SLinus Torvalds 
1057a34e63b1SRafael J. Wysocki static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
10588414809cSSrivatsa S. Bhat {
1059a34e63b1SRafael J. Wysocki 	struct device *dev = get_cpu_device(cpu);
1060e9698cc5SSrivatsa S. Bhat 	struct cpufreq_policy *policy;
1061edd4a893SViresh Kumar 	int ret;
1062e9698cc5SSrivatsa S. Bhat 
1063a34e63b1SRafael J. Wysocki 	if (WARN_ON(!dev))
1064a34e63b1SRafael J. Wysocki 		return NULL;
1065a34e63b1SRafael J. Wysocki 
1066e9698cc5SSrivatsa S. Bhat 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
1067e9698cc5SSrivatsa S. Bhat 	if (!policy)
1068e9698cc5SSrivatsa S. Bhat 		return NULL;
1069e9698cc5SSrivatsa S. Bhat 
1070e9698cc5SSrivatsa S. Bhat 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
1071e9698cc5SSrivatsa S. Bhat 		goto err_free_policy;
1072e9698cc5SSrivatsa S. Bhat 
1073e9698cc5SSrivatsa S. Bhat 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
1074e9698cc5SSrivatsa S. Bhat 		goto err_free_cpumask;
1075e9698cc5SSrivatsa S. Bhat 
1076559ed407SRafael J. Wysocki 	if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
1077559ed407SRafael J. Wysocki 		goto err_free_rcpumask;
1078559ed407SRafael J. Wysocki 
1079edd4a893SViresh Kumar 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
1080edd4a893SViresh Kumar 				   cpufreq_global_kobject, "policy%u", cpu);
1081edd4a893SViresh Kumar 	if (ret) {
1082edd4a893SViresh Kumar 		pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
1083edd4a893SViresh Kumar 		goto err_free_real_cpus;
1084edd4a893SViresh Kumar 	}
1085edd4a893SViresh Kumar 
1086c88a1f8bSLukasz Majewski 	INIT_LIST_HEAD(&policy->policy_list);
1087ad7722daSviresh kumar 	init_rwsem(&policy->rwsem);
108812478cf0SSrivatsa S. Bhat 	spin_lock_init(&policy->transition_lock);
108912478cf0SSrivatsa S. Bhat 	init_waitqueue_head(&policy->transition_wait);
1090818c5712SViresh Kumar 	init_completion(&policy->kobj_unregister);
1091818c5712SViresh Kumar 	INIT_WORK(&policy->update, handle_update);
1092ad7722daSviresh kumar 
1093a34e63b1SRafael J. Wysocki 	policy->cpu = cpu;
1094e9698cc5SSrivatsa S. Bhat 	return policy;
1095e9698cc5SSrivatsa S. Bhat 
1096edd4a893SViresh Kumar err_free_real_cpus:
1097edd4a893SViresh Kumar 	free_cpumask_var(policy->real_cpus);
10982fc3384dSViresh Kumar err_free_rcpumask:
10992fc3384dSViresh Kumar 	free_cpumask_var(policy->related_cpus);
1100e9698cc5SSrivatsa S. Bhat err_free_cpumask:
1101e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1102e9698cc5SSrivatsa S. Bhat err_free_policy:
1103e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1104e9698cc5SSrivatsa S. Bhat 
1105e9698cc5SSrivatsa S. Bhat 	return NULL;
1106e9698cc5SSrivatsa S. Bhat }
1107e9698cc5SSrivatsa S. Bhat 
11082fc3384dSViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify)
110942f921a6SViresh Kumar {
111042f921a6SViresh Kumar 	struct kobject *kobj;
111142f921a6SViresh Kumar 	struct completion *cmp;
111242f921a6SViresh Kumar 
11132fc3384dSViresh Kumar 	if (notify)
1114fcd7af91SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1115fcd7af91SViresh Kumar 					     CPUFREQ_REMOVE_POLICY, policy);
1116fcd7af91SViresh Kumar 
111787549141SViresh Kumar 	down_write(&policy->rwsem);
111887549141SViresh Kumar 	cpufreq_remove_dev_symlink(policy);
111942f921a6SViresh Kumar 	kobj = &policy->kobj;
112042f921a6SViresh Kumar 	cmp = &policy->kobj_unregister;
112187549141SViresh Kumar 	up_write(&policy->rwsem);
112242f921a6SViresh Kumar 	kobject_put(kobj);
112342f921a6SViresh Kumar 
112442f921a6SViresh Kumar 	/*
112542f921a6SViresh Kumar 	 * We need to make sure that the underlying kobj is
112642f921a6SViresh Kumar 	 * actually not referenced anymore by anybody before we
112742f921a6SViresh Kumar 	 * proceed with unloading.
112842f921a6SViresh Kumar 	 */
112942f921a6SViresh Kumar 	pr_debug("waiting for dropping of refcount\n");
113042f921a6SViresh Kumar 	wait_for_completion(cmp);
113142f921a6SViresh Kumar 	pr_debug("wait complete\n");
113242f921a6SViresh Kumar }
113342f921a6SViresh Kumar 
11343654c5ccSViresh Kumar static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify)
1135e9698cc5SSrivatsa S. Bhat {
1136988bed09SViresh Kumar 	unsigned long flags;
1137988bed09SViresh Kumar 	int cpu;
1138988bed09SViresh Kumar 
1139988bed09SViresh Kumar 	/* Remove policy from list */
1140988bed09SViresh Kumar 	write_lock_irqsave(&cpufreq_driver_lock, flags);
1141988bed09SViresh Kumar 	list_del(&policy->policy_list);
1142988bed09SViresh Kumar 
1143988bed09SViresh Kumar 	for_each_cpu(cpu, policy->related_cpus)
1144988bed09SViresh Kumar 		per_cpu(cpufreq_cpu_data, cpu) = NULL;
1145988bed09SViresh Kumar 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1146988bed09SViresh Kumar 
11473654c5ccSViresh Kumar 	cpufreq_policy_put_kobj(policy, notify);
1148559ed407SRafael J. Wysocki 	free_cpumask_var(policy->real_cpus);
1149e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->related_cpus);
1150e9698cc5SSrivatsa S. Bhat 	free_cpumask_var(policy->cpus);
1151e9698cc5SSrivatsa S. Bhat 	kfree(policy);
1152e9698cc5SSrivatsa S. Bhat }
1153e9698cc5SSrivatsa S. Bhat 
11540b275352SRafael J. Wysocki static int cpufreq_online(unsigned int cpu)
11551da177e4SLinus Torvalds {
11567f0c020aSViresh Kumar 	struct cpufreq_policy *policy;
1157194d99c7SRafael J. Wysocki 	bool new_policy;
11581da177e4SLinus Torvalds 	unsigned long flags;
11590b275352SRafael J. Wysocki 	unsigned int j;
11600b275352SRafael J. Wysocki 	int ret;
1161c32b6b8eSAshok Raj 
11620b275352SRafael J. Wysocki 	pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
11636eed9404SViresh Kumar 
1164bb29ae15SViresh Kumar 	/* Check if this CPU already has a policy to manage it */
11659104bb26SViresh Kumar 	policy = per_cpu(cpufreq_cpu_data, cpu);
116611ce707eSRafael J. Wysocki 	if (policy) {
11679104bb26SViresh Kumar 		WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
116811ce707eSRafael J. Wysocki 		if (!policy_is_inactive(policy))
1169d9612a49SRafael J. Wysocki 			return cpufreq_add_policy_cpu(policy, cpu);
11701da177e4SLinus Torvalds 
117111ce707eSRafael J. Wysocki 		/* This is the only online CPU for the policy.  Start over. */
1172194d99c7SRafael J. Wysocki 		new_policy = false;
117311ce707eSRafael J. Wysocki 		down_write(&policy->rwsem);
117411ce707eSRafael J. Wysocki 		policy->cpu = cpu;
117511ce707eSRafael J. Wysocki 		policy->governor = NULL;
117611ce707eSRafael J. Wysocki 		up_write(&policy->rwsem);
117711ce707eSRafael J. Wysocki 	} else {
1178194d99c7SRafael J. Wysocki 		new_policy = true;
1179a34e63b1SRafael J. Wysocki 		policy = cpufreq_policy_alloc(cpu);
1180059019a3SDave Jones 		if (!policy)
1181d4d854d6SRafael J. Wysocki 			return -ENOMEM;
118272368d12SRafael J. Wysocki 	}
11830d66b91eSSrivatsa S. Bhat 
1184835481d9SRusty Russell 	cpumask_copy(policy->cpus, cpumask_of(cpu));
11851da177e4SLinus Torvalds 
11861da177e4SLinus Torvalds 	/* call driver. From then on the cpufreq must be able
11871da177e4SLinus Torvalds 	 * to accept all calls to ->verify and ->setpolicy for this CPU
11881da177e4SLinus Torvalds 	 */
11891c3d85ddSRafael J. Wysocki 	ret = cpufreq_driver->init(policy);
11901da177e4SLinus Torvalds 	if (ret) {
11912d06d8c4SDominik Brodowski 		pr_debug("initialization failed\n");
11928101f997SViresh Kumar 		goto out_free_policy;
11931da177e4SLinus Torvalds 	}
1194643ae6e8SViresh Kumar 
11956d4e81edSTomeu Vizoso 	down_write(&policy->rwsem);
11966d4e81edSTomeu Vizoso 
1197194d99c7SRafael J. Wysocki 	if (new_policy) {
11984d1f3a5bSRafael J. Wysocki 		/* related_cpus should at least include policy->cpus. */
11990998a03aSViresh Kumar 		cpumask_copy(policy->related_cpus, policy->cpus);
12004d1f3a5bSRafael J. Wysocki 		/* Remember CPUs present at the policy creation time. */
1201559ed407SRafael J. Wysocki 		cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask);
12024d1f3a5bSRafael J. Wysocki 	}
1203559ed407SRafael J. Wysocki 
12045a7e56a5SViresh Kumar 	/*
12055a7e56a5SViresh Kumar 	 * affected cpus must always be the one, which are online. We aren't
12065a7e56a5SViresh Kumar 	 * managing offline cpus here.
12075a7e56a5SViresh Kumar 	 */
12085a7e56a5SViresh Kumar 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
12095a7e56a5SViresh Kumar 
1210194d99c7SRafael J. Wysocki 	if (new_policy) {
12115a7e56a5SViresh Kumar 		policy->user_policy.min = policy->min;
12125a7e56a5SViresh Kumar 		policy->user_policy.max = policy->max;
12136d4e81edSTomeu Vizoso 
1214652ed95dSViresh Kumar 		write_lock_irqsave(&cpufreq_driver_lock, flags);
1215988bed09SViresh Kumar 		for_each_cpu(j, policy->related_cpus)
1216652ed95dSViresh Kumar 			per_cpu(cpufreq_cpu_data, j) = policy;
1217652ed95dSViresh Kumar 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1218988bed09SViresh Kumar 	}
1219652ed95dSViresh Kumar 
12202ed99e39SRafael J. Wysocki 	if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1221da60ce9fSViresh Kumar 		policy->cur = cpufreq_driver->get(policy->cpu);
1222da60ce9fSViresh Kumar 		if (!policy->cur) {
1223da60ce9fSViresh Kumar 			pr_err("%s: ->get() failed\n", __func__);
12248101f997SViresh Kumar 			goto out_exit_policy;
1225da60ce9fSViresh Kumar 		}
1226da60ce9fSViresh Kumar 	}
1227da60ce9fSViresh Kumar 
1228d3916691SViresh Kumar 	/*
1229d3916691SViresh Kumar 	 * Sometimes boot loaders set CPU frequency to a value outside of
1230d3916691SViresh Kumar 	 * frequency table present with cpufreq core. In such cases CPU might be
1231d3916691SViresh Kumar 	 * unstable if it has to run on that frequency for long duration of time
1232d3916691SViresh Kumar 	 * and so its better to set it to a frequency which is specified in
1233d3916691SViresh Kumar 	 * freq-table. This also makes cpufreq stats inconsistent as
1234d3916691SViresh Kumar 	 * cpufreq-stats would fail to register because current frequency of CPU
1235d3916691SViresh Kumar 	 * isn't found in freq-table.
1236d3916691SViresh Kumar 	 *
1237d3916691SViresh Kumar 	 * Because we don't want this change to effect boot process badly, we go
1238d3916691SViresh Kumar 	 * for the next freq which is >= policy->cur ('cur' must be set by now,
1239d3916691SViresh Kumar 	 * otherwise we will end up setting freq to lowest of the table as 'cur'
1240d3916691SViresh Kumar 	 * is initialized to zero).
1241d3916691SViresh Kumar 	 *
1242d3916691SViresh Kumar 	 * We are passing target-freq as "policy->cur - 1" otherwise
1243d3916691SViresh Kumar 	 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1244d3916691SViresh Kumar 	 * equal to target-freq.
1245d3916691SViresh Kumar 	 */
1246d3916691SViresh Kumar 	if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1247d3916691SViresh Kumar 	    && has_target()) {
1248d3916691SViresh Kumar 		/* Are we running at unknown frequency ? */
1249d3916691SViresh Kumar 		ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1250d3916691SViresh Kumar 		if (ret == -EINVAL) {
1251d3916691SViresh Kumar 			/* Warn user and fix it */
1252d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1253d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1254d3916691SViresh Kumar 			ret = __cpufreq_driver_target(policy, policy->cur - 1,
1255d3916691SViresh Kumar 				CPUFREQ_RELATION_L);
1256d3916691SViresh Kumar 
1257d3916691SViresh Kumar 			/*
1258d3916691SViresh Kumar 			 * Reaching here after boot in a few seconds may not
1259d3916691SViresh Kumar 			 * mean that system will remain stable at "unknown"
1260d3916691SViresh Kumar 			 * frequency for longer duration. Hence, a BUG_ON().
1261d3916691SViresh Kumar 			 */
1262d3916691SViresh Kumar 			BUG_ON(ret);
1263d3916691SViresh Kumar 			pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1264d3916691SViresh Kumar 				__func__, policy->cpu, policy->cur);
1265d3916691SViresh Kumar 		}
1266d3916691SViresh Kumar 	}
1267d3916691SViresh Kumar 
1268a1531acdSThomas Renninger 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1269a1531acdSThomas Renninger 				     CPUFREQ_START, policy);
1270a1531acdSThomas Renninger 
1271194d99c7SRafael J. Wysocki 	if (new_policy) {
1272d9612a49SRafael J. Wysocki 		ret = cpufreq_add_dev_interface(policy);
127319d6f7ecSDave Jones 		if (ret)
12748101f997SViresh Kumar 			goto out_exit_policy;
1275fcd7af91SViresh Kumar 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1276fcd7af91SViresh Kumar 				CPUFREQ_CREATE_POLICY, policy);
1277c88a1f8bSLukasz Majewski 
1278c88a1f8bSLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
1279c88a1f8bSLukasz Majewski 		list_add(&policy->policy_list, &cpufreq_policy_list);
1280c88a1f8bSLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1281988bed09SViresh Kumar 	}
12828ff69732SDave Jones 
12837f0fa40fSViresh Kumar 	ret = cpufreq_init_policy(policy);
12847f0fa40fSViresh Kumar 	if (ret) {
12857f0fa40fSViresh Kumar 		pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
12867f0fa40fSViresh Kumar 		       __func__, cpu, ret);
1287194d99c7SRafael J. Wysocki 		/* cpufreq_policy_free() will notify based on this */
1288194d99c7SRafael J. Wysocki 		new_policy = false;
1289194d99c7SRafael J. Wysocki 		goto out_exit_policy;
129008fd8c1cSViresh Kumar 	}
1291e18f1682SSrivatsa S. Bhat 
12924e97b631SViresh Kumar 	up_write(&policy->rwsem);
129308fd8c1cSViresh Kumar 
1294038c5b3eSGreg Kroah-Hartman 	kobject_uevent(&policy->kobj, KOBJ_ADD);
12957c45cf31SViresh Kumar 
12967c45cf31SViresh Kumar 	/* Callback for handling stuff after policy is ready */
12977c45cf31SViresh Kumar 	if (cpufreq_driver->ready)
12987c45cf31SViresh Kumar 		cpufreq_driver->ready(policy);
12997c45cf31SViresh Kumar 
13002d06d8c4SDominik Brodowski 	pr_debug("initialization complete\n");
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 	return 0;
13031da177e4SLinus Torvalds 
13048101f997SViresh Kumar out_exit_policy:
13057106e02bSPrarit Bhargava 	up_write(&policy->rwsem);
13067106e02bSPrarit Bhargava 
1307da60ce9fSViresh Kumar 	if (cpufreq_driver->exit)
1308da60ce9fSViresh Kumar 		cpufreq_driver->exit(policy);
13098101f997SViresh Kumar out_free_policy:
1310194d99c7SRafael J. Wysocki 	cpufreq_policy_free(policy, !new_policy);
13111da177e4SLinus Torvalds 	return ret;
13121da177e4SLinus Torvalds }
13131da177e4SLinus Torvalds 
13140b275352SRafael J. Wysocki /**
13150b275352SRafael J. Wysocki  * cpufreq_add_dev - the cpufreq interface for a CPU device.
13160b275352SRafael J. Wysocki  * @dev: CPU device.
13170b275352SRafael J. Wysocki  * @sif: Subsystem interface structure pointer (not used)
13180b275352SRafael J. Wysocki  */
13190b275352SRafael J. Wysocki static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
13200b275352SRafael J. Wysocki {
1321a794d613SRafael J. Wysocki 	struct cpufreq_policy *policy;
13220b275352SRafael J. Wysocki 	unsigned cpu = dev->id;
13230b275352SRafael J. Wysocki 
13240b275352SRafael J. Wysocki 	dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
13250b275352SRafael J. Wysocki 
1326a794d613SRafael J. Wysocki 	if (cpu_online(cpu))
1327a794d613SRafael J. Wysocki 		return cpufreq_online(cpu);
1328a794d613SRafael J. Wysocki 
13290b275352SRafael J. Wysocki 	/*
1330a794d613SRafael J. Wysocki 	 * A hotplug notifier will follow and we will handle it as CPU online
1331a794d613SRafael J. Wysocki 	 * then.  For now, just create the sysfs link, unless there is no policy
1332a794d613SRafael J. Wysocki 	 * or the link is already present.
13330b275352SRafael J. Wysocki 	 */
1334a794d613SRafael J. Wysocki 	policy = per_cpu(cpufreq_cpu_data, cpu);
1335a794d613SRafael J. Wysocki 	if (!policy || cpumask_test_and_set_cpu(cpu, policy->real_cpus))
1336a794d613SRafael J. Wysocki 		return 0;
13370b275352SRafael J. Wysocki 
1338a794d613SRafael J. Wysocki 	return add_cpu_dev_symlink(policy, cpu);
13391da177e4SLinus Torvalds }
13401da177e4SLinus Torvalds 
134169cee714SViresh Kumar static void cpufreq_offline(unsigned int cpu)
13421da177e4SLinus Torvalds {
13433a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
134469cee714SViresh Kumar 	int ret;
13451da177e4SLinus Torvalds 
1346b8eed8afSViresh Kumar 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
13471da177e4SLinus Torvalds 
1348988bed09SViresh Kumar 	policy = cpufreq_cpu_get_raw(cpu);
13493a3e9e06SViresh Kumar 	if (!policy) {
1350b8eed8afSViresh Kumar 		pr_debug("%s: No cpu_data found\n", __func__);
135115c0b4d2SRafael J. Wysocki 		return;
13521da177e4SLinus Torvalds 	}
13531da177e4SLinus Torvalds 
135449f18560SViresh Kumar 	down_write(&policy->rwsem);
135545482c70SRafael J. Wysocki 	if (has_target())
135645482c70SRafael J. Wysocki 		cpufreq_stop_governor(policy);
13571da177e4SLinus Torvalds 
13589591becbSViresh Kumar 	cpumask_clear_cpu(cpu, policy->cpus);
13594573237bSViresh Kumar 
13609591becbSViresh Kumar 	if (policy_is_inactive(policy)) {
13619591becbSViresh Kumar 		if (has_target())
13624573237bSViresh Kumar 			strncpy(policy->last_governor, policy->governor->name,
13634573237bSViresh Kumar 				CPUFREQ_NAME_LEN);
136469030dd1SSrinivas Pandruvada 		else
136569030dd1SSrinivas Pandruvada 			policy->last_policy = policy->policy;
13669591becbSViresh Kumar 	} else if (cpu == policy->cpu) {
13679591becbSViresh Kumar 		/* Nominate new CPU */
13689591becbSViresh Kumar 		policy->cpu = cpumask_any(policy->cpus);
13699591becbSViresh Kumar 	}
13701da177e4SLinus Torvalds 
13719591becbSViresh Kumar 	/* Start governor again for active policy */
13729591becbSViresh Kumar 	if (!policy_is_inactive(policy)) {
13739591becbSViresh Kumar 		if (has_target()) {
13740a300767SRafael J. Wysocki 			ret = cpufreq_start_governor(policy);
13759591becbSViresh Kumar 			if (ret)
13769591becbSViresh Kumar 				pr_err("%s: Failed to start governor\n", __func__);
13779591becbSViresh Kumar 		}
137869cee714SViresh Kumar 
137949f18560SViresh Kumar 		goto unlock;
138069cee714SViresh Kumar 	}
138169cee714SViresh Kumar 
138269cee714SViresh Kumar 	if (cpufreq_driver->stop_cpu)
1383367dc4aaSDirk Brandewie 		cpufreq_driver->stop_cpu(policy);
138487549141SViresh Kumar 
138536be3418SRafael J. Wysocki 	if (has_target())
138636be3418SRafael J. Wysocki 		cpufreq_exit_governor(policy);
13872a998599SRafael J. Wysocki 
13888414809cSSrivatsa S. Bhat 	/*
13898414809cSSrivatsa S. Bhat 	 * Perform the ->exit() even during light-weight tear-down,
13908414809cSSrivatsa S. Bhat 	 * since this is a core component, and is essential for the
13918414809cSSrivatsa S. Bhat 	 * subsequent light-weight ->init() to succeed.
13928414809cSSrivatsa S. Bhat 	 */
139355582bccSSrinivas Pandruvada 	if (cpufreq_driver->exit) {
13943a3e9e06SViresh Kumar 		cpufreq_driver->exit(policy);
139555582bccSSrinivas Pandruvada 		policy->freq_table = NULL;
139655582bccSSrinivas Pandruvada 	}
139749f18560SViresh Kumar 
139849f18560SViresh Kumar unlock:
139949f18560SViresh Kumar 	up_write(&policy->rwsem);
14001da177e4SLinus Torvalds }
14011da177e4SLinus Torvalds 
1402cedb70afSSrivatsa S. Bhat /**
140327a862e9SViresh Kumar  * cpufreq_remove_dev - remove a CPU device
1404cedb70afSSrivatsa S. Bhat  *
1405cedb70afSSrivatsa S. Bhat  * Removes the cpufreq interface for a CPU device.
1406cedb70afSSrivatsa S. Bhat  */
140771db87baSViresh Kumar static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
14085a01f2e8SVenkatesh Pallipadi {
14098a25a2fdSKay Sievers 	unsigned int cpu = dev->id;
141087549141SViresh Kumar 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
141187549141SViresh Kumar 
141287549141SViresh Kumar 	if (!policy)
141371db87baSViresh Kumar 		return;
1414ec28297aSVenki Pallipadi 
141569cee714SViresh Kumar 	if (cpu_online(cpu))
141669cee714SViresh Kumar 		cpufreq_offline(cpu);
141787549141SViresh Kumar 
1418559ed407SRafael J. Wysocki 	cpumask_clear_cpu(cpu, policy->real_cpus);
1419559ed407SRafael J. Wysocki 	remove_cpu_dev_symlink(policy, cpu);
1420f344dae0SViresh Kumar 
1421f344dae0SViresh Kumar 	if (cpumask_empty(policy->real_cpus))
1422f344dae0SViresh Kumar 		cpufreq_policy_free(policy, true);
14235a01f2e8SVenkatesh Pallipadi }
14245a01f2e8SVenkatesh Pallipadi 
14251da177e4SLinus Torvalds /**
1426bb176f7dSViresh Kumar  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1427bb176f7dSViresh Kumar  *	in deep trouble.
1428a1e1dc41SViresh Kumar  *	@policy: policy managing CPUs
14291da177e4SLinus Torvalds  *	@new_freq: CPU frequency the CPU actually runs at
14301da177e4SLinus Torvalds  *
143129464f28SDave Jones  *	We adjust to current frequency first, and need to clean up later.
143229464f28SDave Jones  *	So either call to cpufreq_update_policy() or schedule handle_update()).
14331da177e4SLinus Torvalds  */
1434a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
1435e08f5f5bSGautham R Shenoy 				unsigned int new_freq)
14361da177e4SLinus Torvalds {
14371da177e4SLinus Torvalds 	struct cpufreq_freqs freqs;
1438b43a7ffbSViresh Kumar 
1439e837f9b5SJoe Perches 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
1440a1e1dc41SViresh Kumar 		 policy->cur, new_freq);
14411da177e4SLinus Torvalds 
1442a1e1dc41SViresh Kumar 	freqs.old = policy->cur;
14431da177e4SLinus Torvalds 	freqs.new = new_freq;
1444b43a7ffbSViresh Kumar 
14458fec051eSViresh Kumar 	cpufreq_freq_transition_begin(policy, &freqs);
14468fec051eSViresh Kumar 	cpufreq_freq_transition_end(policy, &freqs, 0);
14471da177e4SLinus Torvalds }
14481da177e4SLinus Torvalds 
14491da177e4SLinus Torvalds /**
14504ab70df4SDhaval Giani  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
145195235ca2SVenkatesh Pallipadi  * @cpu: CPU number
145295235ca2SVenkatesh Pallipadi  *
145395235ca2SVenkatesh Pallipadi  * This is the last known freq, without actually getting it from the driver.
145495235ca2SVenkatesh Pallipadi  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
145595235ca2SVenkatesh Pallipadi  */
145695235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu)
145795235ca2SVenkatesh Pallipadi {
14589e21ba8bSDirk Brandewie 	struct cpufreq_policy *policy;
1459e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
1460c75361c0SRichard Cochran 	unsigned long flags;
146195235ca2SVenkatesh Pallipadi 
1462c75361c0SRichard Cochran 	read_lock_irqsave(&cpufreq_driver_lock, flags);
1463c75361c0SRichard Cochran 
1464c75361c0SRichard Cochran 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
1465c75361c0SRichard Cochran 		ret_freq = cpufreq_driver->get(cpu);
1466c75361c0SRichard Cochran 		read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1467c75361c0SRichard Cochran 		return ret_freq;
1468c75361c0SRichard Cochran 	}
1469c75361c0SRichard Cochran 
1470c75361c0SRichard Cochran 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
14719e21ba8bSDirk Brandewie 
14729e21ba8bSDirk Brandewie 	policy = cpufreq_cpu_get(cpu);
147395235ca2SVenkatesh Pallipadi 	if (policy) {
1474e08f5f5bSGautham R Shenoy 		ret_freq = policy->cur;
147595235ca2SVenkatesh Pallipadi 		cpufreq_cpu_put(policy);
147695235ca2SVenkatesh Pallipadi 	}
147795235ca2SVenkatesh Pallipadi 
14784d34a67dSDave Jones 	return ret_freq;
147995235ca2SVenkatesh Pallipadi }
148095235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get);
148195235ca2SVenkatesh Pallipadi 
14823d737108SJesse Barnes /**
14833d737108SJesse Barnes  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
14843d737108SJesse Barnes  * @cpu: CPU number
14853d737108SJesse Barnes  *
14863d737108SJesse Barnes  * Just return the max possible frequency for a given CPU.
14873d737108SJesse Barnes  */
14883d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu)
14893d737108SJesse Barnes {
14903d737108SJesse Barnes 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
14913d737108SJesse Barnes 	unsigned int ret_freq = 0;
14923d737108SJesse Barnes 
14933d737108SJesse Barnes 	if (policy) {
14943d737108SJesse Barnes 		ret_freq = policy->max;
14953d737108SJesse Barnes 		cpufreq_cpu_put(policy);
14963d737108SJesse Barnes 	}
14973d737108SJesse Barnes 
14983d737108SJesse Barnes 	return ret_freq;
14993d737108SJesse Barnes }
15003d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max);
15013d737108SJesse Barnes 
1502d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
15031da177e4SLinus Torvalds {
1504e08f5f5bSGautham R Shenoy 	unsigned int ret_freq = 0;
15051da177e4SLinus Torvalds 
15061c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver->get)
15074d34a67dSDave Jones 		return ret_freq;
15081da177e4SLinus Torvalds 
1509d92d50a4SViresh Kumar 	ret_freq = cpufreq_driver->get(policy->cpu);
15101da177e4SLinus Torvalds 
1511b7898fdaSRafael J. Wysocki 	/*
1512b7898fdaSRafael J. Wysocki 	 * Updating inactive policies is invalid, so avoid doing that.  Also
1513b7898fdaSRafael J. Wysocki 	 * if fast frequency switching is used with the given policy, the check
1514b7898fdaSRafael J. Wysocki 	 * against policy->cur is pointless, so skip it in that case too.
1515b7898fdaSRafael J. Wysocki 	 */
1516b7898fdaSRafael J. Wysocki 	if (unlikely(policy_is_inactive(policy)) || policy->fast_switch_enabled)
151711e584cfSViresh Kumar 		return ret_freq;
151811e584cfSViresh Kumar 
1519e08f5f5bSGautham R Shenoy 	if (ret_freq && policy->cur &&
15201c3d85ddSRafael J. Wysocki 		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1521e08f5f5bSGautham R Shenoy 		/* verify no discrepancy between actual and
1522e08f5f5bSGautham R Shenoy 					saved value exists */
1523e08f5f5bSGautham R Shenoy 		if (unlikely(ret_freq != policy->cur)) {
1524a1e1dc41SViresh Kumar 			cpufreq_out_of_sync(policy, ret_freq);
15251da177e4SLinus Torvalds 			schedule_work(&policy->update);
15261da177e4SLinus Torvalds 		}
15271da177e4SLinus Torvalds 	}
15281da177e4SLinus Torvalds 
15294d34a67dSDave Jones 	return ret_freq;
15305a01f2e8SVenkatesh Pallipadi }
15311da177e4SLinus Torvalds 
15325a01f2e8SVenkatesh Pallipadi /**
15335a01f2e8SVenkatesh Pallipadi  * cpufreq_get - get the current CPU frequency (in kHz)
15345a01f2e8SVenkatesh Pallipadi  * @cpu: CPU number
15355a01f2e8SVenkatesh Pallipadi  *
15365a01f2e8SVenkatesh Pallipadi  * Get the CPU current (static) CPU frequency
15375a01f2e8SVenkatesh Pallipadi  */
15385a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu)
15395a01f2e8SVenkatesh Pallipadi {
1540999976e0SAaron Plattner 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
15415a01f2e8SVenkatesh Pallipadi 	unsigned int ret_freq = 0;
15425a01f2e8SVenkatesh Pallipadi 
1543999976e0SAaron Plattner 	if (policy) {
1544ad7722daSviresh kumar 		down_read(&policy->rwsem);
1545d92d50a4SViresh Kumar 		ret_freq = __cpufreq_get(policy);
1546ad7722daSviresh kumar 		up_read(&policy->rwsem);
1547999976e0SAaron Plattner 
1548999976e0SAaron Plattner 		cpufreq_cpu_put(policy);
1549999976e0SAaron Plattner 	}
15506eed9404SViresh Kumar 
15514d34a67dSDave Jones 	return ret_freq;
15521da177e4SLinus Torvalds }
15531da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get);
15541da177e4SLinus Torvalds 
1555999f5729SRafael J. Wysocki static unsigned int cpufreq_update_current_freq(struct cpufreq_policy *policy)
1556999f5729SRafael J. Wysocki {
1557999f5729SRafael J. Wysocki 	unsigned int new_freq;
1558999f5729SRafael J. Wysocki 
1559c9d9c929SRafael J. Wysocki 	if (cpufreq_suspended)
1560c9d9c929SRafael J. Wysocki 		return 0;
1561c9d9c929SRafael J. Wysocki 
1562999f5729SRafael J. Wysocki 	new_freq = cpufreq_driver->get(policy->cpu);
1563999f5729SRafael J. Wysocki 	if (!new_freq)
1564999f5729SRafael J. Wysocki 		return 0;
1565999f5729SRafael J. Wysocki 
1566999f5729SRafael J. Wysocki 	if (!policy->cur) {
1567999f5729SRafael J. Wysocki 		pr_debug("cpufreq: Driver did not initialize current freq\n");
1568999f5729SRafael J. Wysocki 		policy->cur = new_freq;
1569999f5729SRafael J. Wysocki 	} else if (policy->cur != new_freq && has_target()) {
1570999f5729SRafael J. Wysocki 		cpufreq_out_of_sync(policy, new_freq);
1571999f5729SRafael J. Wysocki 	}
1572999f5729SRafael J. Wysocki 
1573999f5729SRafael J. Wysocki 	return new_freq;
1574999f5729SRafael J. Wysocki }
1575999f5729SRafael J. Wysocki 
15768a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = {
15778a25a2fdSKay Sievers 	.name		= "cpufreq",
15788a25a2fdSKay Sievers 	.subsys		= &cpu_subsys,
15798a25a2fdSKay Sievers 	.add_dev	= cpufreq_add_dev,
15808a25a2fdSKay Sievers 	.remove_dev	= cpufreq_remove_dev,
1581e00e56dfSRafael J. Wysocki };
1582e00e56dfSRafael J. Wysocki 
1583e28867eaSViresh Kumar /*
1584e28867eaSViresh Kumar  * In case platform wants some specific frequency to be configured
1585e28867eaSViresh Kumar  * during suspend..
158642d4dc3fSBenjamin Herrenschmidt  */
1587e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy)
158842d4dc3fSBenjamin Herrenschmidt {
1589e28867eaSViresh Kumar 	int ret;
15904bc5d341SDave Jones 
1591e28867eaSViresh Kumar 	if (!policy->suspend_freq) {
1592201f3716SBartlomiej Zolnierkiewicz 		pr_debug("%s: suspend_freq not defined\n", __func__);
1593201f3716SBartlomiej Zolnierkiewicz 		return 0;
159442d4dc3fSBenjamin Herrenschmidt 	}
159542d4dc3fSBenjamin Herrenschmidt 
1596e28867eaSViresh Kumar 	pr_debug("%s: Setting suspend-freq: %u\n", __func__,
1597e28867eaSViresh Kumar 			policy->suspend_freq);
1598e28867eaSViresh Kumar 
1599e28867eaSViresh Kumar 	ret = __cpufreq_driver_target(policy, policy->suspend_freq,
1600e28867eaSViresh Kumar 			CPUFREQ_RELATION_H);
1601e28867eaSViresh Kumar 	if (ret)
1602e28867eaSViresh Kumar 		pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
1603e28867eaSViresh Kumar 				__func__, policy->suspend_freq, ret);
1604e28867eaSViresh Kumar 
1605c9060494SDave Jones 	return ret;
160642d4dc3fSBenjamin Herrenschmidt }
1607e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend);
160842d4dc3fSBenjamin Herrenschmidt 
160942d4dc3fSBenjamin Herrenschmidt /**
16102f0aea93SViresh Kumar  * cpufreq_suspend() - Suspend CPUFreq governors
16111da177e4SLinus Torvalds  *
16122f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycles for suspending governors
16132f0aea93SViresh Kumar  * as some platforms can't change frequency after this point in suspend cycle.
16142f0aea93SViresh Kumar  * Because some of the devices (like: i2c, regulators, etc) they use for
16152f0aea93SViresh Kumar  * changing frequency are suspended quickly after this point.
16161da177e4SLinus Torvalds  */
16172f0aea93SViresh Kumar void cpufreq_suspend(void)
16181da177e4SLinus Torvalds {
16193a3e9e06SViresh Kumar 	struct cpufreq_policy *policy;
16201da177e4SLinus Torvalds 
16212f0aea93SViresh Kumar 	if (!cpufreq_driver)
1622e00e56dfSRafael J. Wysocki 		return;
16231da177e4SLinus Torvalds 
1624ba41e1bcSRafael J. Wysocki 	if (!has_target() && !cpufreq_driver->suspend)
1625b1b12babSViresh Kumar 		goto suspend;
16261da177e4SLinus Torvalds 
16272f0aea93SViresh Kumar 	pr_debug("%s: Suspending Governors\n", __func__);
16282f0aea93SViresh Kumar 
1629f963735aSViresh Kumar 	for_each_active_policy(policy) {
1630ba41e1bcSRafael J. Wysocki 		if (has_target()) {
163149f18560SViresh Kumar 			down_write(&policy->rwsem);
163245482c70SRafael J. Wysocki 			cpufreq_stop_governor(policy);
163349f18560SViresh Kumar 			up_write(&policy->rwsem);
1634ba41e1bcSRafael J. Wysocki 		}
1635ba41e1bcSRafael J. Wysocki 
1636ba41e1bcSRafael J. Wysocki 		if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
16372f0aea93SViresh Kumar 			pr_err("%s: Failed to suspend driver: %p\n", __func__,
16382f0aea93SViresh Kumar 				policy);
16391da177e4SLinus Torvalds 	}
1640b1b12babSViresh Kumar 
1641b1b12babSViresh Kumar suspend:
1642b1b12babSViresh Kumar 	cpufreq_suspended = true;
16431da177e4SLinus Torvalds }
16441da177e4SLinus Torvalds 
16451da177e4SLinus Torvalds /**
16462f0aea93SViresh Kumar  * cpufreq_resume() - Resume CPUFreq governors
16471da177e4SLinus Torvalds  *
16482f0aea93SViresh Kumar  * Called during system wide Suspend/Hibernate cycle for resuming governors that
16492f0aea93SViresh Kumar  * are suspended with cpufreq_suspend().
16501da177e4SLinus Torvalds  */
16512f0aea93SViresh Kumar void cpufreq_resume(void)
16521da177e4SLinus Torvalds {
16531da177e4SLinus Torvalds 	struct cpufreq_policy *policy;
165449f18560SViresh Kumar 	int ret;
16551da177e4SLinus Torvalds 
16562f0aea93SViresh Kumar 	if (!cpufreq_driver)
16571da177e4SLinus Torvalds 		return;
16581da177e4SLinus Torvalds 
16598e30444eSLan Tianyu 	cpufreq_suspended = false;
16608e30444eSLan Tianyu 
1661ba41e1bcSRafael J. Wysocki 	if (!has_target() && !cpufreq_driver->resume)
16622f0aea93SViresh Kumar 		return;
16631da177e4SLinus Torvalds 
16642f0aea93SViresh Kumar 	pr_debug("%s: Resuming Governors\n", __func__);
16652f0aea93SViresh Kumar 
1666f963735aSViresh Kumar 	for_each_active_policy(policy) {
166749f18560SViresh Kumar 		if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
16680c5aa405SViresh Kumar 			pr_err("%s: Failed to resume driver: %p\n", __func__,
16690c5aa405SViresh Kumar 				policy);
1670ba41e1bcSRafael J. Wysocki 		} else if (has_target()) {
167149f18560SViresh Kumar 			down_write(&policy->rwsem);
16720a300767SRafael J. Wysocki 			ret = cpufreq_start_governor(policy);
167349f18560SViresh Kumar 			up_write(&policy->rwsem);
167449f18560SViresh Kumar 
167549f18560SViresh Kumar 			if (ret)
16762f0aea93SViresh Kumar 				pr_err("%s: Failed to start governor for policy: %p\n",
16772f0aea93SViresh Kumar 				       __func__, policy);
1678c75de0acSViresh Kumar 		}
167949f18560SViresh Kumar 	}
16801da177e4SLinus Torvalds }
16811da177e4SLinus Torvalds 
16829d95046eSBorislav Petkov /**
16839d95046eSBorislav Petkov  *	cpufreq_get_current_driver - return current driver's name
16849d95046eSBorislav Petkov  *
16859d95046eSBorislav Petkov  *	Return the name string of the currently loaded cpufreq driver
16869d95046eSBorislav Petkov  *	or NULL, if none.
16879d95046eSBorislav Petkov  */
16889d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void)
16899d95046eSBorislav Petkov {
16901c3d85ddSRafael J. Wysocki 	if (cpufreq_driver)
16911c3d85ddSRafael J. Wysocki 		return cpufreq_driver->name;
16921c3d85ddSRafael J. Wysocki 
16931c3d85ddSRafael J. Wysocki 	return NULL;
16949d95046eSBorislav Petkov }
16959d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
16961da177e4SLinus Torvalds 
169751315cdfSThomas Petazzoni /**
169851315cdfSThomas Petazzoni  *	cpufreq_get_driver_data - return current driver data
169951315cdfSThomas Petazzoni  *
170051315cdfSThomas Petazzoni  *	Return the private data of the currently loaded cpufreq
170151315cdfSThomas Petazzoni  *	driver, or NULL if no cpufreq driver is loaded.
170251315cdfSThomas Petazzoni  */
170351315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void)
170451315cdfSThomas Petazzoni {
170551315cdfSThomas Petazzoni 	if (cpufreq_driver)
170651315cdfSThomas Petazzoni 		return cpufreq_driver->driver_data;
170751315cdfSThomas Petazzoni 
170851315cdfSThomas Petazzoni 	return NULL;
170951315cdfSThomas Petazzoni }
171051315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
171151315cdfSThomas Petazzoni 
17121da177e4SLinus Torvalds /*********************************************************************
17131da177e4SLinus Torvalds  *                     NOTIFIER LISTS INTERFACE                      *
17141da177e4SLinus Torvalds  *********************************************************************/
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds /**
17171da177e4SLinus Torvalds  *	cpufreq_register_notifier - register a driver with cpufreq
17181da177e4SLinus Torvalds  *	@nb: notifier function to register
17191da177e4SLinus Torvalds  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
17201da177e4SLinus Torvalds  *
17211da177e4SLinus Torvalds  *	Add a driver to one of two lists: either a list of drivers that
17221da177e4SLinus Torvalds  *      are notified about clock rate changes (once before and once after
17231da177e4SLinus Torvalds  *      the transition), or a list of drivers that are notified about
17241da177e4SLinus Torvalds  *      changes in cpufreq policy.
17251da177e4SLinus Torvalds  *
17261da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1727e041c683SAlan Stern  *	blocking_notifier_chain_register.
17281da177e4SLinus Torvalds  */
17291da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
17301da177e4SLinus Torvalds {
17311da177e4SLinus Torvalds 	int ret;
17321da177e4SLinus Torvalds 
1733d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1734d5aaffa9SDirk Brandewie 		return -EINVAL;
1735d5aaffa9SDirk Brandewie 
173674212ca4SCesar Eduardo Barros 	WARN_ON(!init_cpufreq_transition_notifier_list_called);
173774212ca4SCesar Eduardo Barros 
17381da177e4SLinus Torvalds 	switch (list) {
17391da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1740b7898fdaSRafael J. Wysocki 		mutex_lock(&cpufreq_fast_switch_lock);
1741b7898fdaSRafael J. Wysocki 
1742b7898fdaSRafael J. Wysocki 		if (cpufreq_fast_switch_count > 0) {
1743b7898fdaSRafael J. Wysocki 			mutex_unlock(&cpufreq_fast_switch_lock);
1744b7898fdaSRafael J. Wysocki 			return -EBUSY;
1745b7898fdaSRafael J. Wysocki 		}
1746b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_register(
1747e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
1748b7898fdaSRafael J. Wysocki 		if (!ret)
1749b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count--;
1750b7898fdaSRafael J. Wysocki 
1751b7898fdaSRafael J. Wysocki 		mutex_unlock(&cpufreq_fast_switch_lock);
17521da177e4SLinus Torvalds 		break;
17531da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1754e041c683SAlan Stern 		ret = blocking_notifier_chain_register(
1755e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
17561da177e4SLinus Torvalds 		break;
17571da177e4SLinus Torvalds 	default:
17581da177e4SLinus Torvalds 		ret = -EINVAL;
17591da177e4SLinus Torvalds 	}
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	return ret;
17621da177e4SLinus Torvalds }
17631da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier);
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds /**
17661da177e4SLinus Torvalds  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
17671da177e4SLinus Torvalds  *	@nb: notifier block to be unregistered
17681da177e4SLinus Torvalds  *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
17691da177e4SLinus Torvalds  *
17701da177e4SLinus Torvalds  *	Remove a driver from the CPU frequency notifier list.
17711da177e4SLinus Torvalds  *
17721da177e4SLinus Torvalds  *	This function may sleep, and has the same return conditions as
1773e041c683SAlan Stern  *	blocking_notifier_chain_unregister.
17741da177e4SLinus Torvalds  */
17751da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
17761da177e4SLinus Torvalds {
17771da177e4SLinus Torvalds 	int ret;
17781da177e4SLinus Torvalds 
1779d5aaffa9SDirk Brandewie 	if (cpufreq_disabled())
1780d5aaffa9SDirk Brandewie 		return -EINVAL;
1781d5aaffa9SDirk Brandewie 
17821da177e4SLinus Torvalds 	switch (list) {
17831da177e4SLinus Torvalds 	case CPUFREQ_TRANSITION_NOTIFIER:
1784b7898fdaSRafael J. Wysocki 		mutex_lock(&cpufreq_fast_switch_lock);
1785b7898fdaSRafael J. Wysocki 
1786b4dfdbb3SAlan Stern 		ret = srcu_notifier_chain_unregister(
1787e041c683SAlan Stern 				&cpufreq_transition_notifier_list, nb);
1788b7898fdaSRafael J. Wysocki 		if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
1789b7898fdaSRafael J. Wysocki 			cpufreq_fast_switch_count++;
1790b7898fdaSRafael J. Wysocki 
1791b7898fdaSRafael J. Wysocki 		mutex_unlock(&cpufreq_fast_switch_lock);
17921da177e4SLinus Torvalds 		break;
17931da177e4SLinus Torvalds 	case CPUFREQ_POLICY_NOTIFIER:
1794e041c683SAlan Stern 		ret = blocking_notifier_chain_unregister(
1795e041c683SAlan Stern 				&cpufreq_policy_notifier_list, nb);
17961da177e4SLinus Torvalds 		break;
17971da177e4SLinus Torvalds 	default:
17981da177e4SLinus Torvalds 		ret = -EINVAL;
17991da177e4SLinus Torvalds 	}
18001da177e4SLinus Torvalds 
18011da177e4SLinus Torvalds 	return ret;
18021da177e4SLinus Torvalds }
18031da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier);
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 
18061da177e4SLinus Torvalds /*********************************************************************
18071da177e4SLinus Torvalds  *                              GOVERNORS                            *
18081da177e4SLinus Torvalds  *********************************************************************/
18091da177e4SLinus Torvalds 
1810b7898fdaSRafael J. Wysocki /**
1811b7898fdaSRafael J. Wysocki  * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
1812b7898fdaSRafael J. Wysocki  * @policy: cpufreq policy to switch the frequency for.
1813b7898fdaSRafael J. Wysocki  * @target_freq: New frequency to set (may be approximate).
1814b7898fdaSRafael J. Wysocki  *
1815b7898fdaSRafael J. Wysocki  * Carry out a fast frequency switch without sleeping.
1816b7898fdaSRafael J. Wysocki  *
1817b7898fdaSRafael J. Wysocki  * The driver's ->fast_switch() callback invoked by this function must be
1818b7898fdaSRafael J. Wysocki  * suitable for being called from within RCU-sched read-side critical sections
1819b7898fdaSRafael J. Wysocki  * and it is expected to select the minimum available frequency greater than or
1820b7898fdaSRafael J. Wysocki  * equal to @target_freq (CPUFREQ_RELATION_L).
1821b7898fdaSRafael J. Wysocki  *
1822b7898fdaSRafael J. Wysocki  * This function must not be called if policy->fast_switch_enabled is unset.
1823b7898fdaSRafael J. Wysocki  *
1824b7898fdaSRafael J. Wysocki  * Governors calling this function must guarantee that it will never be invoked
1825b7898fdaSRafael J. Wysocki  * twice in parallel for the same policy and that it will never be called in
1826b7898fdaSRafael J. Wysocki  * parallel with either ->target() or ->target_index() for the same policy.
1827b7898fdaSRafael J. Wysocki  *
1828b7898fdaSRafael J. Wysocki  * If CPUFREQ_ENTRY_INVALID is returned by the driver's ->fast_switch()
1829b7898fdaSRafael J. Wysocki  * callback to indicate an error condition, the hardware configuration must be
1830b7898fdaSRafael J. Wysocki  * preserved.
1831b7898fdaSRafael J. Wysocki  */
1832b7898fdaSRafael J. Wysocki unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
1833b7898fdaSRafael J. Wysocki 					unsigned int target_freq)
1834b7898fdaSRafael J. Wysocki {
1835b7898fdaSRafael J. Wysocki 	clamp_val(target_freq, policy->min, policy->max);
1836b7898fdaSRafael J. Wysocki 
1837b7898fdaSRafael J. Wysocki 	return cpufreq_driver->fast_switch(policy, target_freq);
1838b7898fdaSRafael J. Wysocki }
1839b7898fdaSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
1840b7898fdaSRafael J. Wysocki 
18411c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */
18421c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy,
18431c03a2d0SViresh Kumar 				 struct cpufreq_freqs *freqs, int index)
18441c03a2d0SViresh Kumar {
18451c03a2d0SViresh Kumar 	int ret;
18461c03a2d0SViresh Kumar 
18471c03a2d0SViresh Kumar 	freqs->new = cpufreq_driver->get_intermediate(policy, index);
18481c03a2d0SViresh Kumar 
18491c03a2d0SViresh Kumar 	/* We don't need to switch to intermediate freq */
18501c03a2d0SViresh Kumar 	if (!freqs->new)
18511c03a2d0SViresh Kumar 		return 0;
18521c03a2d0SViresh Kumar 
18531c03a2d0SViresh Kumar 	pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
18541c03a2d0SViresh Kumar 		 __func__, policy->cpu, freqs->old, freqs->new);
18551c03a2d0SViresh Kumar 
18561c03a2d0SViresh Kumar 	cpufreq_freq_transition_begin(policy, freqs);
18571c03a2d0SViresh Kumar 	ret = cpufreq_driver->target_intermediate(policy, index);
18581c03a2d0SViresh Kumar 	cpufreq_freq_transition_end(policy, freqs, ret);
18591c03a2d0SViresh Kumar 
18601c03a2d0SViresh Kumar 	if (ret)
18611c03a2d0SViresh Kumar 		pr_err("%s: Failed to change to intermediate frequency: %d\n",
18621c03a2d0SViresh Kumar 		       __func__, ret);
18631c03a2d0SViresh Kumar 
18641c03a2d0SViresh Kumar 	return ret;
18651c03a2d0SViresh Kumar }
18661c03a2d0SViresh Kumar 
18678d65775dSViresh Kumar static int __target_index(struct cpufreq_policy *policy,
18688d65775dSViresh Kumar 			  struct cpufreq_frequency_table *freq_table, int index)
18698d65775dSViresh Kumar {
18701c03a2d0SViresh Kumar 	struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
18711c03a2d0SViresh Kumar 	unsigned int intermediate_freq = 0;
18728d65775dSViresh Kumar 	int retval = -EINVAL;
18738d65775dSViresh Kumar 	bool notify;
18748d65775dSViresh Kumar 
18758d65775dSViresh Kumar 	notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
18768d65775dSViresh Kumar 	if (notify) {
18771c03a2d0SViresh Kumar 		/* Handle switching to intermediate frequency */
18781c03a2d0SViresh Kumar 		if (cpufreq_driver->get_intermediate) {
18791c03a2d0SViresh Kumar 			retval = __target_intermediate(policy, &freqs, index);
18801c03a2d0SViresh Kumar 			if (retval)
18811c03a2d0SViresh Kumar 				return retval;
18828d65775dSViresh Kumar 
18831c03a2d0SViresh Kumar 			intermediate_freq = freqs.new;
18841c03a2d0SViresh Kumar 			/* Set old freq to intermediate */
18851c03a2d0SViresh Kumar 			if (intermediate_freq)
18861c03a2d0SViresh Kumar 				freqs.old = freqs.new;
18871c03a2d0SViresh Kumar 		}
18881c03a2d0SViresh Kumar 
18891c03a2d0SViresh Kumar 		freqs.new = freq_table[index].frequency;
18908d65775dSViresh Kumar 		pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
18918d65775dSViresh Kumar 			 __func__, policy->cpu, freqs.old, freqs.new);
18928d65775dSViresh Kumar 
18938d65775dSViresh Kumar 		cpufreq_freq_transition_begin(policy, &freqs);
18948d65775dSViresh Kumar 	}
18958d65775dSViresh Kumar 
18968d65775dSViresh Kumar 	retval = cpufreq_driver->target_index(policy, index);
18978d65775dSViresh Kumar 	if (retval)
18988d65775dSViresh Kumar 		pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
18998d65775dSViresh Kumar 		       retval);
19008d65775dSViresh Kumar 
19011c03a2d0SViresh Kumar 	if (notify) {
19028d65775dSViresh Kumar 		cpufreq_freq_transition_end(policy, &freqs, retval);
19038d65775dSViresh Kumar 
19041c03a2d0SViresh Kumar 		/*
19051c03a2d0SViresh Kumar 		 * Failed after setting to intermediate freq? Driver should have
19061c03a2d0SViresh Kumar 		 * reverted back to initial frequency and so should we. Check
19071c03a2d0SViresh Kumar 		 * here for intermediate_freq instead of get_intermediate, in
190858405af6SShailendra Verma 		 * case we haven't switched to intermediate freq at all.
19091c03a2d0SViresh Kumar 		 */
19101c03a2d0SViresh Kumar 		if (unlikely(retval && intermediate_freq)) {
19111c03a2d0SViresh Kumar 			freqs.old = intermediate_freq;
19121c03a2d0SViresh Kumar 			freqs.new = policy->restore_freq;
19131c03a2d0SViresh Kumar 			cpufreq_freq_transition_begin(policy, &freqs);
19141c03a2d0SViresh Kumar 			cpufreq_freq_transition_end(policy, &freqs, 0);
19151c03a2d0SViresh Kumar 		}
19161c03a2d0SViresh Kumar 	}
19171c03a2d0SViresh Kumar 
19188d65775dSViresh Kumar 	return retval;
19198d65775dSViresh Kumar }
19208d65775dSViresh Kumar 
19211da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy,
19221da177e4SLinus Torvalds 			    unsigned int target_freq,
19231da177e4SLinus Torvalds 			    unsigned int relation)
19241da177e4SLinus Torvalds {
19257249924eSViresh Kumar 	unsigned int old_target_freq = target_freq;
19266019d23aSRafael J. Wysocki 	struct cpufreq_frequency_table *freq_table;
19276019d23aSRafael J. Wysocki 	int index, retval;
1928c32b6b8eSAshok Raj 
1929a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
1930a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
1931a7b422cdSKonrad Rzeszutek Wilk 
19327249924eSViresh Kumar 	/* Make sure that target_freq is within supported range */
19337249924eSViresh Kumar 	if (target_freq > policy->max)
19347249924eSViresh Kumar 		target_freq = policy->max;
19357249924eSViresh Kumar 	if (target_freq < policy->min)
19367249924eSViresh Kumar 		target_freq = policy->min;
19377249924eSViresh Kumar 
19387249924eSViresh Kumar 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
19397249924eSViresh Kumar 		 policy->cpu, target_freq, relation, old_target_freq);
19405a1c0228SViresh Kumar 
19419c0ebcf7SViresh Kumar 	/*
19429c0ebcf7SViresh Kumar 	 * This might look like a redundant call as we are checking it again
19439c0ebcf7SViresh Kumar 	 * after finding index. But it is left intentionally for cases where
19449c0ebcf7SViresh Kumar 	 * exactly same freq is called again and so we can save on few function
19459c0ebcf7SViresh Kumar 	 * calls.
19469c0ebcf7SViresh Kumar 	 */
19475a1c0228SViresh Kumar 	if (target_freq == policy->cur)
19485a1c0228SViresh Kumar 		return 0;
19495a1c0228SViresh Kumar 
19501c03a2d0SViresh Kumar 	/* Save last value to restore later on errors */
19511c03a2d0SViresh Kumar 	policy->restore_freq = policy->cur;
19521c03a2d0SViresh Kumar 
19531c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->target)
19546019d23aSRafael J. Wysocki 		return cpufreq_driver->target(policy, target_freq, relation);
19556019d23aSRafael J. Wysocki 
19566019d23aSRafael J. Wysocki 	if (!cpufreq_driver->target_index)
19576019d23aSRafael J. Wysocki 		return -EINVAL;
195890d45d17SAshok Raj 
19599c0ebcf7SViresh Kumar 	freq_table = cpufreq_frequency_get_table(policy->cpu);
19609c0ebcf7SViresh Kumar 	if (unlikely(!freq_table)) {
19619c0ebcf7SViresh Kumar 		pr_err("%s: Unable to find freq_table\n", __func__);
19626019d23aSRafael J. Wysocki 		return -EINVAL;
19639c0ebcf7SViresh Kumar 	}
19649c0ebcf7SViresh Kumar 
19656019d23aSRafael J. Wysocki 	retval = cpufreq_frequency_table_target(policy, freq_table, target_freq,
19666019d23aSRafael J. Wysocki 						relation, &index);
19679c0ebcf7SViresh Kumar 	if (unlikely(retval)) {
19689c0ebcf7SViresh Kumar 		pr_err("%s: Unable to find matching freq\n", __func__);
19691da177e4SLinus Torvalds 		return retval;
19701da177e4SLinus Torvalds 	}
19716019d23aSRafael J. Wysocki 
19726019d23aSRafael J. Wysocki 	if (freq_table[index].frequency == policy->cur)
19736019d23aSRafael J. Wysocki 		return 0;
19746019d23aSRafael J. Wysocki 
19756019d23aSRafael J. Wysocki 	return __target_index(policy, freq_table, index);
19766019d23aSRafael J. Wysocki }
19771da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
19781da177e4SLinus Torvalds 
19791da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy,
19801da177e4SLinus Torvalds 			  unsigned int target_freq,
19811da177e4SLinus Torvalds 			  unsigned int relation)
19821da177e4SLinus Torvalds {
1983f1829e4aSJulia Lawall 	int ret = -EINVAL;
19841da177e4SLinus Torvalds 
1985ad7722daSviresh kumar 	down_write(&policy->rwsem);
19861da177e4SLinus Torvalds 
19871da177e4SLinus Torvalds 	ret = __cpufreq_driver_target(policy, target_freq, relation);
19881da177e4SLinus Torvalds 
1989ad7722daSviresh kumar 	up_write(&policy->rwsem);
19901da177e4SLinus Torvalds 
19911da177e4SLinus Torvalds 	return ret;
19921da177e4SLinus Torvalds }
19931da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target);
19941da177e4SLinus Torvalds 
1995de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
1996de1df26bSRafael J. Wysocki {
1997de1df26bSRafael J. Wysocki 	return NULL;
1998de1df26bSRafael J. Wysocki }
1999de1df26bSRafael J. Wysocki 
2000a1317e09SViresh Kumar static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event)
20011da177e4SLinus Torvalds {
2002cc993cabSDave Jones 	int ret;
20036afde10cSThomas Renninger 
20042f0aea93SViresh Kumar 	/* Don't start any governor operations if we are entering suspend */
20052f0aea93SViresh Kumar 	if (cpufreq_suspended)
20062f0aea93SViresh Kumar 		return 0;
2007cb57720bSEthan Zhao 	/*
2008cb57720bSEthan Zhao 	 * Governor might not be initiated here if ACPI _PPC changed
2009cb57720bSEthan Zhao 	 * notification happened, so check it.
2010cb57720bSEthan Zhao 	 */
2011cb57720bSEthan Zhao 	if (!policy->governor)
2012cb57720bSEthan Zhao 		return -EINVAL;
20132f0aea93SViresh Kumar 
20141c256245SThomas Renninger 	if (policy->governor->max_transition_latency &&
20151c256245SThomas Renninger 	    policy->cpuinfo.transition_latency >
20161c256245SThomas Renninger 	    policy->governor->max_transition_latency) {
2017de1df26bSRafael J. Wysocki 		struct cpufreq_governor *gov = cpufreq_fallback_governor();
2018de1df26bSRafael J. Wysocki 
2019de1df26bSRafael J. Wysocki 		if (gov) {
2020e837f9b5SJoe Perches 			pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n",
2021e837f9b5SJoe Perches 				policy->governor->name, gov->name);
20221c256245SThomas Renninger 			policy->governor = gov;
2023de1df26bSRafael J. Wysocki 		} else {
2024de1df26bSRafael J. Wysocki 			return -EINVAL;
20251c256245SThomas Renninger 		}
20266afde10cSThomas Renninger 	}
20271da177e4SLinus Torvalds 
2028fe492f3fSViresh Kumar 	if (event == CPUFREQ_GOV_POLICY_INIT)
20291da177e4SLinus Torvalds 		if (!try_module_get(policy->governor->owner))
20301da177e4SLinus Torvalds 			return -EINVAL;
20311da177e4SLinus Torvalds 
203263431f78SViresh Kumar 	pr_debug("%s: for CPU %u, event %u\n", __func__, policy->cpu, event);
203395731ebbSXiaoguang Chen 
20341da177e4SLinus Torvalds 	ret = policy->governor->governor(policy, event);
20351da177e4SLinus Torvalds 
203636be3418SRafael J. Wysocki 	if (event == CPUFREQ_GOV_POLICY_INIT) {
203736be3418SRafael J. Wysocki 		if (ret)
20381da177e4SLinus Torvalds 			module_put(policy->governor->owner);
203936be3418SRafael J. Wysocki 		else
204036be3418SRafael J. Wysocki 			policy->governor->initialized++;
204136be3418SRafael J. Wysocki 	} else if (event == CPUFREQ_GOV_POLICY_EXIT) {
204236be3418SRafael J. Wysocki 		policy->governor->initialized--;
204336be3418SRafael J. Wysocki 		module_put(policy->governor->owner);
204436be3418SRafael J. Wysocki 	}
20451da177e4SLinus Torvalds 
20461da177e4SLinus Torvalds 	return ret;
20471da177e4SLinus Torvalds }
20481da177e4SLinus Torvalds 
20490a300767SRafael J. Wysocki static int cpufreq_start_governor(struct cpufreq_policy *policy)
20500a300767SRafael J. Wysocki {
20510a300767SRafael J. Wysocki 	int ret;
20520a300767SRafael J. Wysocki 
20533bbf8fe3SRafael J. Wysocki 	if (cpufreq_driver->get && !cpufreq_driver->setpolicy)
20543bbf8fe3SRafael J. Wysocki 		cpufreq_update_current_freq(policy);
20553bbf8fe3SRafael J. Wysocki 
20560a300767SRafael J. Wysocki 	ret = cpufreq_governor(policy, CPUFREQ_GOV_START);
2057*d6ff44d6SRafael J. Wysocki 	if (ret)
2058*d6ff44d6SRafael J. Wysocki 		return ret;
2059*d6ff44d6SRafael J. Wysocki 
2060*d6ff44d6SRafael J. Wysocki 	cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2061*d6ff44d6SRafael J. Wysocki 	return 0;
20620a300767SRafael J. Wysocki }
20630a300767SRafael J. Wysocki 
20641da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor)
20651da177e4SLinus Torvalds {
20663bcb09a3SJeremy Fitzhardinge 	int err;
20671da177e4SLinus Torvalds 
20681da177e4SLinus Torvalds 	if (!governor)
20691da177e4SLinus Torvalds 		return -EINVAL;
20701da177e4SLinus Torvalds 
2071a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2072a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2073a7b422cdSKonrad Rzeszutek Wilk 
20743fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
20751da177e4SLinus Torvalds 
2076b394058fSViresh Kumar 	governor->initialized = 0;
20773bcb09a3SJeremy Fitzhardinge 	err = -EBUSY;
207842f91fa1SViresh Kumar 	if (!find_governor(governor->name)) {
20793bcb09a3SJeremy Fitzhardinge 		err = 0;
20801da177e4SLinus Torvalds 		list_add(&governor->governor_list, &cpufreq_governor_list);
20813bcb09a3SJeremy Fitzhardinge 	}
20821da177e4SLinus Torvalds 
20833fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
20843bcb09a3SJeremy Fitzhardinge 	return err;
20851da177e4SLinus Torvalds }
20861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor);
20871da177e4SLinus Torvalds 
20881da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor)
20891da177e4SLinus Torvalds {
20904573237bSViresh Kumar 	struct cpufreq_policy *policy;
20914573237bSViresh Kumar 	unsigned long flags;
209290e41bacSPrarit Bhargava 
20931da177e4SLinus Torvalds 	if (!governor)
20941da177e4SLinus Torvalds 		return;
20951da177e4SLinus Torvalds 
2096a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2097a7b422cdSKonrad Rzeszutek Wilk 		return;
2098a7b422cdSKonrad Rzeszutek Wilk 
20994573237bSViresh Kumar 	/* clear last_governor for all inactive policies */
21004573237bSViresh Kumar 	read_lock_irqsave(&cpufreq_driver_lock, flags);
21014573237bSViresh Kumar 	for_each_inactive_policy(policy) {
210218bf3a12SViresh Kumar 		if (!strcmp(policy->last_governor, governor->name)) {
210318bf3a12SViresh Kumar 			policy->governor = NULL;
21044573237bSViresh Kumar 			strcpy(policy->last_governor, "\0");
210590e41bacSPrarit Bhargava 		}
210618bf3a12SViresh Kumar 	}
21074573237bSViresh Kumar 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
210890e41bacSPrarit Bhargava 
21093fc54d37Sakpm@osdl.org 	mutex_lock(&cpufreq_governor_mutex);
21101da177e4SLinus Torvalds 	list_del(&governor->governor_list);
21113fc54d37Sakpm@osdl.org 	mutex_unlock(&cpufreq_governor_mutex);
21121da177e4SLinus Torvalds 	return;
21131da177e4SLinus Torvalds }
21141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
21151da177e4SLinus Torvalds 
21161da177e4SLinus Torvalds 
21171da177e4SLinus Torvalds /*********************************************************************
21181da177e4SLinus Torvalds  *                          POLICY INTERFACE                         *
21191da177e4SLinus Torvalds  *********************************************************************/
21201da177e4SLinus Torvalds 
21211da177e4SLinus Torvalds /**
21221da177e4SLinus Torvalds  * cpufreq_get_policy - get the current cpufreq_policy
212329464f28SDave Jones  * @policy: struct cpufreq_policy into which the current cpufreq_policy
212429464f28SDave Jones  *	is written
21251da177e4SLinus Torvalds  *
21261da177e4SLinus Torvalds  * Reads the current cpufreq policy.
21271da177e4SLinus Torvalds  */
21281da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
21291da177e4SLinus Torvalds {
21301da177e4SLinus Torvalds 	struct cpufreq_policy *cpu_policy;
21311da177e4SLinus Torvalds 	if (!policy)
21321da177e4SLinus Torvalds 		return -EINVAL;
21331da177e4SLinus Torvalds 
21341da177e4SLinus Torvalds 	cpu_policy = cpufreq_cpu_get(cpu);
21351da177e4SLinus Torvalds 	if (!cpu_policy)
21361da177e4SLinus Torvalds 		return -EINVAL;
21371da177e4SLinus Torvalds 
2138d5b73cd8SViresh Kumar 	memcpy(policy, cpu_policy, sizeof(*policy));
21391da177e4SLinus Torvalds 
21401da177e4SLinus Torvalds 	cpufreq_cpu_put(cpu_policy);
21411da177e4SLinus Torvalds 	return 0;
21421da177e4SLinus Torvalds }
21431da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy);
21441da177e4SLinus Torvalds 
2145153d7f3fSArjan van de Ven /*
2146037ce839SViresh Kumar  * policy : current policy.
2147037ce839SViresh Kumar  * new_policy: policy to be set.
2148153d7f3fSArjan van de Ven  */
2149037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy,
21503a3e9e06SViresh Kumar 				struct cpufreq_policy *new_policy)
21511da177e4SLinus Torvalds {
2152d9a789c7SRafael J. Wysocki 	struct cpufreq_governor *old_gov;
2153d9a789c7SRafael J. Wysocki 	int ret;
21541da177e4SLinus Torvalds 
2155e837f9b5SJoe Perches 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2156e837f9b5SJoe Perches 		 new_policy->cpu, new_policy->min, new_policy->max);
21571da177e4SLinus Torvalds 
2158d5b73cd8SViresh Kumar 	memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
21591da177e4SLinus Torvalds 
2160fba9573bSPan Xinhui 	/*
2161fba9573bSPan Xinhui 	* This check works well when we store new min/max freq attributes,
2162fba9573bSPan Xinhui 	* because new_policy is a copy of policy with one field updated.
2163fba9573bSPan Xinhui 	*/
2164fba9573bSPan Xinhui 	if (new_policy->min > new_policy->max)
2165d9a789c7SRafael J. Wysocki 		return -EINVAL;
21669c9a43edSMattia Dongili 
21671da177e4SLinus Torvalds 	/* verify the cpu speed can be set within this limit */
21683a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
21691da177e4SLinus Torvalds 	if (ret)
2170d9a789c7SRafael J. Wysocki 		return ret;
21711da177e4SLinus Torvalds 
21721da177e4SLinus Torvalds 	/* adjust if necessary - all reasons */
2173e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
21743a3e9e06SViresh Kumar 			CPUFREQ_ADJUST, new_policy);
21751da177e4SLinus Torvalds 
2176bb176f7dSViresh Kumar 	/*
2177bb176f7dSViresh Kumar 	 * verify the cpu speed can be set within this limit, which might be
2178bb176f7dSViresh Kumar 	 * different to the first one
2179bb176f7dSViresh Kumar 	 */
21803a3e9e06SViresh Kumar 	ret = cpufreq_driver->verify(new_policy);
2181e041c683SAlan Stern 	if (ret)
2182d9a789c7SRafael J. Wysocki 		return ret;
21831da177e4SLinus Torvalds 
21841da177e4SLinus Torvalds 	/* notification of the new policy */
2185e041c683SAlan Stern 	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
21863a3e9e06SViresh Kumar 			CPUFREQ_NOTIFY, new_policy);
21871da177e4SLinus Torvalds 
21883a3e9e06SViresh Kumar 	policy->min = new_policy->min;
21893a3e9e06SViresh Kumar 	policy->max = new_policy->max;
21901da177e4SLinus Torvalds 
21912d06d8c4SDominik Brodowski 	pr_debug("new min and max freqs are %u - %u kHz\n",
21923a3e9e06SViresh Kumar 		 policy->min, policy->max);
21931da177e4SLinus Torvalds 
21941c3d85ddSRafael J. Wysocki 	if (cpufreq_driver->setpolicy) {
21953a3e9e06SViresh Kumar 		policy->policy = new_policy->policy;
21962d06d8c4SDominik Brodowski 		pr_debug("setting range\n");
2197d9a789c7SRafael J. Wysocki 		return cpufreq_driver->setpolicy(new_policy);
2198d9a789c7SRafael J. Wysocki 	}
2199d9a789c7SRafael J. Wysocki 
22000a300767SRafael J. Wysocki 	if (new_policy->governor == policy->governor) {
22010a300767SRafael J. Wysocki 		pr_debug("cpufreq: governor limits update\n");
2202*d6ff44d6SRafael J. Wysocki 		cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2203*d6ff44d6SRafael J. Wysocki 		return 0;
22040a300767SRafael J. Wysocki 	}
22051da177e4SLinus Torvalds 
22062d06d8c4SDominik Brodowski 	pr_debug("governor switch\n");
22071da177e4SLinus Torvalds 
2208d9a789c7SRafael J. Wysocki 	/* save old, working values */
2209d9a789c7SRafael J. Wysocki 	old_gov = policy->governor;
22101da177e4SLinus Torvalds 	/* end old governor */
2211d9a789c7SRafael J. Wysocki 	if (old_gov) {
221245482c70SRafael J. Wysocki 		cpufreq_stop_governor(policy);
221336be3418SRafael J. Wysocki 		cpufreq_exit_governor(policy);
22147bd353a9SViresh Kumar 	}
22151da177e4SLinus Torvalds 
22161da177e4SLinus Torvalds 	/* start new governor */
22173a3e9e06SViresh Kumar 	policy->governor = new_policy->governor;
2218a1317e09SViresh Kumar 	ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT);
22194bc384aeSViresh Kumar 	if (!ret) {
22200a300767SRafael J. Wysocki 		ret = cpufreq_start_governor(policy);
22210a300767SRafael J. Wysocki 		if (!ret) {
22220a300767SRafael J. Wysocki 			pr_debug("cpufreq: governor change\n");
22230a300767SRafael J. Wysocki 			return 0;
22240a300767SRafael J. Wysocki 		}
2225b7898fdaSRafael J. Wysocki 		cpufreq_exit_governor(policy);
2226955ef483SViresh Kumar 	}
22277bd353a9SViresh Kumar 
22281da177e4SLinus Torvalds 	/* new governor failed, so re-start old one */
2229d9a789c7SRafael J. Wysocki 	pr_debug("starting governor %s failed\n", policy->governor->name);
22301da177e4SLinus Torvalds 	if (old_gov) {
22313a3e9e06SViresh Kumar 		policy->governor = old_gov;
2232a1317e09SViresh Kumar 		if (cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT))
22334bc384aeSViresh Kumar 			policy->governor = NULL;
22344bc384aeSViresh Kumar 		else
22350a300767SRafael J. Wysocki 			cpufreq_start_governor(policy);
22361da177e4SLinus Torvalds 	}
22371da177e4SLinus Torvalds 
22384bc384aeSViresh Kumar 	return ret;
22391da177e4SLinus Torvalds }
22401da177e4SLinus Torvalds 
22411da177e4SLinus Torvalds /**
22421da177e4SLinus Torvalds  *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
22431da177e4SLinus Torvalds  *	@cpu: CPU which shall be re-evaluated
22441da177e4SLinus Torvalds  *
224525985edcSLucas De Marchi  *	Useful for policy notifiers which have different necessities
22461da177e4SLinus Torvalds  *	at different times.
22471da177e4SLinus Torvalds  */
22481da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu)
22491da177e4SLinus Torvalds {
22503a3e9e06SViresh Kumar 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
22513a3e9e06SViresh Kumar 	struct cpufreq_policy new_policy;
2252f1829e4aSJulia Lawall 	int ret;
22531da177e4SLinus Torvalds 
2254fefa8ff8SAaron Plattner 	if (!policy)
2255fefa8ff8SAaron Plattner 		return -ENODEV;
22561da177e4SLinus Torvalds 
2257ad7722daSviresh kumar 	down_write(&policy->rwsem);
22581da177e4SLinus Torvalds 
22592d06d8c4SDominik Brodowski 	pr_debug("updating policy for CPU %u\n", cpu);
2260d5b73cd8SViresh Kumar 	memcpy(&new_policy, policy, sizeof(*policy));
22613a3e9e06SViresh Kumar 	new_policy.min = policy->user_policy.min;
22623a3e9e06SViresh Kumar 	new_policy.max = policy->user_policy.max;
22631da177e4SLinus Torvalds 
2264bb176f7dSViresh Kumar 	/*
2265bb176f7dSViresh Kumar 	 * BIOS might change freq behind our back
2266bb176f7dSViresh Kumar 	 * -> ask driver for current freq and notify governors about a change
2267bb176f7dSViresh Kumar 	 */
22682ed99e39SRafael J. Wysocki 	if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
2269999f5729SRafael J. Wysocki 		new_policy.cur = cpufreq_update_current_freq(policy);
2270bd0fa9bbSViresh Kumar 		if (WARN_ON(!new_policy.cur)) {
2271bd0fa9bbSViresh Kumar 			ret = -EIO;
2272fefa8ff8SAaron Plattner 			goto unlock;
2273bd0fa9bbSViresh Kumar 		}
2274a85f7bd3SThomas Renninger 	}
22750961dd0dSThomas Renninger 
2276037ce839SViresh Kumar 	ret = cpufreq_set_policy(policy, &new_policy);
22771da177e4SLinus Torvalds 
2278fefa8ff8SAaron Plattner unlock:
2279ad7722daSviresh kumar 	up_write(&policy->rwsem);
22805a01f2e8SVenkatesh Pallipadi 
22813a3e9e06SViresh Kumar 	cpufreq_cpu_put(policy);
22821da177e4SLinus Torvalds 	return ret;
22831da177e4SLinus Torvalds }
22841da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy);
22851da177e4SLinus Torvalds 
22862760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb,
2287c32b6b8eSAshok Raj 					unsigned long action, void *hcpu)
2288c32b6b8eSAshok Raj {
2289c32b6b8eSAshok Raj 	unsigned int cpu = (unsigned long)hcpu;
2290c32b6b8eSAshok Raj 
22915302c3fbSSrivatsa S. Bhat 	switch (action & ~CPU_TASKS_FROZEN) {
2292c32b6b8eSAshok Raj 	case CPU_ONLINE:
2293cd73e9b0SRafael J. Wysocki 	case CPU_DOWN_FAILED:
22940b275352SRafael J. Wysocki 		cpufreq_online(cpu);
2295c32b6b8eSAshok Raj 		break;
22965302c3fbSSrivatsa S. Bhat 
2297c32b6b8eSAshok Raj 	case CPU_DOWN_PREPARE:
229869cee714SViresh Kumar 		cpufreq_offline(cpu);
2299c32b6b8eSAshok Raj 		break;
2300c32b6b8eSAshok Raj 	}
2301c32b6b8eSAshok Raj 	return NOTIFY_OK;
2302c32b6b8eSAshok Raj }
2303c32b6b8eSAshok Raj 
23049c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = {
2305c32b6b8eSAshok Raj 	.notifier_call = cpufreq_cpu_callback,
2306c32b6b8eSAshok Raj };
23071da177e4SLinus Torvalds 
23081da177e4SLinus Torvalds /*********************************************************************
23096f19efc0SLukasz Majewski  *               BOOST						     *
23106f19efc0SLukasz Majewski  *********************************************************************/
23116f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state)
23126f19efc0SLukasz Majewski {
23136f19efc0SLukasz Majewski 	struct cpufreq_frequency_table *freq_table;
23146f19efc0SLukasz Majewski 	struct cpufreq_policy *policy;
23156f19efc0SLukasz Majewski 	int ret = -EINVAL;
23166f19efc0SLukasz Majewski 
2317f963735aSViresh Kumar 	for_each_active_policy(policy) {
23186f19efc0SLukasz Majewski 		freq_table = cpufreq_frequency_get_table(policy->cpu);
23196f19efc0SLukasz Majewski 		if (freq_table) {
23206f19efc0SLukasz Majewski 			ret = cpufreq_frequency_table_cpuinfo(policy,
23216f19efc0SLukasz Majewski 							freq_table);
23226f19efc0SLukasz Majewski 			if (ret) {
23236f19efc0SLukasz Majewski 				pr_err("%s: Policy frequency update failed\n",
23246f19efc0SLukasz Majewski 				       __func__);
23256f19efc0SLukasz Majewski 				break;
23266f19efc0SLukasz Majewski 			}
232749f18560SViresh Kumar 
232849f18560SViresh Kumar 			down_write(&policy->rwsem);
23296f19efc0SLukasz Majewski 			policy->user_policy.max = policy->max;
2330a1317e09SViresh Kumar 			cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
233149f18560SViresh Kumar 			up_write(&policy->rwsem);
23326f19efc0SLukasz Majewski 		}
23336f19efc0SLukasz Majewski 	}
23346f19efc0SLukasz Majewski 
23356f19efc0SLukasz Majewski 	return ret;
23366f19efc0SLukasz Majewski }
23376f19efc0SLukasz Majewski 
23386f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state)
23396f19efc0SLukasz Majewski {
23406f19efc0SLukasz Majewski 	unsigned long flags;
23416f19efc0SLukasz Majewski 	int ret = 0;
23426f19efc0SLukasz Majewski 
23436f19efc0SLukasz Majewski 	if (cpufreq_driver->boost_enabled == state)
23446f19efc0SLukasz Majewski 		return 0;
23456f19efc0SLukasz Majewski 
23466f19efc0SLukasz Majewski 	write_lock_irqsave(&cpufreq_driver_lock, flags);
23476f19efc0SLukasz Majewski 	cpufreq_driver->boost_enabled = state;
23486f19efc0SLukasz Majewski 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23496f19efc0SLukasz Majewski 
23506f19efc0SLukasz Majewski 	ret = cpufreq_driver->set_boost(state);
23516f19efc0SLukasz Majewski 	if (ret) {
23526f19efc0SLukasz Majewski 		write_lock_irqsave(&cpufreq_driver_lock, flags);
23536f19efc0SLukasz Majewski 		cpufreq_driver->boost_enabled = !state;
23546f19efc0SLukasz Majewski 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
23556f19efc0SLukasz Majewski 
2356e837f9b5SJoe Perches 		pr_err("%s: Cannot %s BOOST\n",
2357e837f9b5SJoe Perches 		       __func__, state ? "enable" : "disable");
23586f19efc0SLukasz Majewski 	}
23596f19efc0SLukasz Majewski 
23606f19efc0SLukasz Majewski 	return ret;
23616f19efc0SLukasz Majewski }
23626f19efc0SLukasz Majewski 
236341669da0SRafael J. Wysocki static bool cpufreq_boost_supported(void)
23646f19efc0SLukasz Majewski {
23657a6c79f2SRafael J. Wysocki 	return likely(cpufreq_driver) && cpufreq_driver->set_boost;
23666f19efc0SLukasz Majewski }
23676f19efc0SLukasz Majewski 
236844139ed4SViresh Kumar static int create_boost_sysfs_file(void)
236944139ed4SViresh Kumar {
237044139ed4SViresh Kumar 	int ret;
237144139ed4SViresh Kumar 
2372c82bd444SViresh Kumar 	ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
237344139ed4SViresh Kumar 	if (ret)
237444139ed4SViresh Kumar 		pr_err("%s: cannot register global BOOST sysfs file\n",
237544139ed4SViresh Kumar 		       __func__);
237644139ed4SViresh Kumar 
237744139ed4SViresh Kumar 	return ret;
237844139ed4SViresh Kumar }
237944139ed4SViresh Kumar 
238044139ed4SViresh Kumar static void remove_boost_sysfs_file(void)
238144139ed4SViresh Kumar {
238244139ed4SViresh Kumar 	if (cpufreq_boost_supported())
2383c82bd444SViresh Kumar 		sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
238444139ed4SViresh Kumar }
238544139ed4SViresh Kumar 
238644139ed4SViresh Kumar int cpufreq_enable_boost_support(void)
238744139ed4SViresh Kumar {
238844139ed4SViresh Kumar 	if (!cpufreq_driver)
238944139ed4SViresh Kumar 		return -EINVAL;
239044139ed4SViresh Kumar 
239144139ed4SViresh Kumar 	if (cpufreq_boost_supported())
239244139ed4SViresh Kumar 		return 0;
239344139ed4SViresh Kumar 
23947a6c79f2SRafael J. Wysocki 	cpufreq_driver->set_boost = cpufreq_boost_set_sw;
239544139ed4SViresh Kumar 
239644139ed4SViresh Kumar 	/* This will get removed on driver unregister */
239744139ed4SViresh Kumar 	return create_boost_sysfs_file();
239844139ed4SViresh Kumar }
239944139ed4SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
240044139ed4SViresh Kumar 
24016f19efc0SLukasz Majewski int cpufreq_boost_enabled(void)
24026f19efc0SLukasz Majewski {
24036f19efc0SLukasz Majewski 	return cpufreq_driver->boost_enabled;
24046f19efc0SLukasz Majewski }
24056f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
24066f19efc0SLukasz Majewski 
24076f19efc0SLukasz Majewski /*********************************************************************
24081da177e4SLinus Torvalds  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
24091da177e4SLinus Torvalds  *********************************************************************/
24101da177e4SLinus Torvalds 
24111da177e4SLinus Torvalds /**
24121da177e4SLinus Torvalds  * cpufreq_register_driver - register a CPU Frequency driver
24131da177e4SLinus Torvalds  * @driver_data: A struct cpufreq_driver containing the values#
24141da177e4SLinus Torvalds  * submitted by the CPU Frequency driver.
24151da177e4SLinus Torvalds  *
24161da177e4SLinus Torvalds  * Registers a CPU Frequency driver to this core code. This code
241763af4055SEric Biggers  * returns zero on success, -EEXIST when another driver got here first
24181da177e4SLinus Torvalds  * (and isn't unregistered in the meantime).
24191da177e4SLinus Torvalds  *
24201da177e4SLinus Torvalds  */
2421221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data)
24221da177e4SLinus Torvalds {
24231da177e4SLinus Torvalds 	unsigned long flags;
24241da177e4SLinus Torvalds 	int ret;
24251da177e4SLinus Torvalds 
2426a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2427a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2428a7b422cdSKonrad Rzeszutek Wilk 
24291da177e4SLinus Torvalds 	if (!driver_data || !driver_data->verify || !driver_data->init ||
24309c0ebcf7SViresh Kumar 	    !(driver_data->setpolicy || driver_data->target_index ||
24319832235fSRafael J. Wysocki 		    driver_data->target) ||
24329832235fSRafael J. Wysocki 	     (driver_data->setpolicy && (driver_data->target_index ||
24331c03a2d0SViresh Kumar 		    driver_data->target)) ||
24341c03a2d0SViresh Kumar 	     (!!driver_data->get_intermediate != !!driver_data->target_intermediate))
24351da177e4SLinus Torvalds 		return -EINVAL;
24361da177e4SLinus Torvalds 
24372d06d8c4SDominik Brodowski 	pr_debug("trying to register driver %s\n", driver_data->name);
24381da177e4SLinus Torvalds 
2439fdd320daSRafael J. Wysocki 	/* Protect against concurrent CPU online/offline. */
2440fdd320daSRafael J. Wysocki 	get_online_cpus();
2441fdd320daSRafael J. Wysocki 
24420d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
24431c3d85ddSRafael J. Wysocki 	if (cpufreq_driver) {
24440d1857a1SNathan Zimmer 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2445fdd320daSRafael J. Wysocki 		ret = -EEXIST;
2446fdd320daSRafael J. Wysocki 		goto out;
24471da177e4SLinus Torvalds 	}
24481c3d85ddSRafael J. Wysocki 	cpufreq_driver = driver_data;
24490d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24501da177e4SLinus Torvalds 
2451bc68b7dfSViresh Kumar 	if (driver_data->setpolicy)
2452bc68b7dfSViresh Kumar 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
2453bc68b7dfSViresh Kumar 
24547a6c79f2SRafael J. Wysocki 	if (cpufreq_boost_supported()) {
245544139ed4SViresh Kumar 		ret = create_boost_sysfs_file();
245644139ed4SViresh Kumar 		if (ret)
24576f19efc0SLukasz Majewski 			goto err_null_driver;
24587a6c79f2SRafael J. Wysocki 	}
24596f19efc0SLukasz Majewski 
24608a25a2fdSKay Sievers 	ret = subsys_interface_register(&cpufreq_interface);
24618f5bc2abSJiri Slaby 	if (ret)
24626f19efc0SLukasz Majewski 		goto err_boost_unreg;
24631da177e4SLinus Torvalds 
2464ce1bcfe9SViresh Kumar 	if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
2465ce1bcfe9SViresh Kumar 	    list_empty(&cpufreq_policy_list)) {
24661da177e4SLinus Torvalds 		/* if all ->init() calls failed, unregister */
2467ce1bcfe9SViresh Kumar 		pr_debug("%s: No CPU initialized for driver %s\n", __func__,
2468e08f5f5bSGautham R Shenoy 			 driver_data->name);
24698a25a2fdSKay Sievers 		goto err_if_unreg;
24701da177e4SLinus Torvalds 	}
24711da177e4SLinus Torvalds 
247265edc68cSChandra Seetharaman 	register_hotcpu_notifier(&cpufreq_cpu_notifier);
24732d06d8c4SDominik Brodowski 	pr_debug("driver %s up and running\n", driver_data->name);
24743834abb4SPankaj Gupta 	goto out;
2475fdd320daSRafael J. Wysocki 
24768a25a2fdSKay Sievers err_if_unreg:
24778a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
24786f19efc0SLukasz Majewski err_boost_unreg:
247944139ed4SViresh Kumar 	remove_boost_sysfs_file();
24808f5bc2abSJiri Slaby err_null_driver:
24810d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
24821c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
24830d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
24843834abb4SPankaj Gupta out:
24853834abb4SPankaj Gupta 	put_online_cpus();
24863834abb4SPankaj Gupta 	return ret;
24871da177e4SLinus Torvalds }
24881da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver);
24891da177e4SLinus Torvalds 
24901da177e4SLinus Torvalds /**
24911da177e4SLinus Torvalds  * cpufreq_unregister_driver - unregister the current CPUFreq driver
24921da177e4SLinus Torvalds  *
24931da177e4SLinus Torvalds  * Unregister the current CPUFreq driver. Only call this if you have
24941da177e4SLinus Torvalds  * the right to do so, i.e. if you have succeeded in initialising before!
24951da177e4SLinus Torvalds  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
24961da177e4SLinus Torvalds  * currently not initialised.
24971da177e4SLinus Torvalds  */
2498221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver)
24991da177e4SLinus Torvalds {
25001da177e4SLinus Torvalds 	unsigned long flags;
25011da177e4SLinus Torvalds 
25021c3d85ddSRafael J. Wysocki 	if (!cpufreq_driver || (driver != cpufreq_driver))
25031da177e4SLinus Torvalds 		return -EINVAL;
25041da177e4SLinus Torvalds 
25052d06d8c4SDominik Brodowski 	pr_debug("unregistering driver %s\n", driver->name);
25061da177e4SLinus Torvalds 
2507454d3a25SSebastian Andrzej Siewior 	/* Protect against concurrent cpu hotplug */
2508454d3a25SSebastian Andrzej Siewior 	get_online_cpus();
25098a25a2fdSKay Sievers 	subsys_interface_unregister(&cpufreq_interface);
251044139ed4SViresh Kumar 	remove_boost_sysfs_file();
251165edc68cSChandra Seetharaman 	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
25121da177e4SLinus Torvalds 
25130d1857a1SNathan Zimmer 	write_lock_irqsave(&cpufreq_driver_lock, flags);
25146eed9404SViresh Kumar 
25151c3d85ddSRafael J. Wysocki 	cpufreq_driver = NULL;
25166eed9404SViresh Kumar 
25170d1857a1SNathan Zimmer 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2518454d3a25SSebastian Andrzej Siewior 	put_online_cpus();
25191da177e4SLinus Torvalds 
25201da177e4SLinus Torvalds 	return 0;
25211da177e4SLinus Torvalds }
25221da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
25235a01f2e8SVenkatesh Pallipadi 
252490de2a4aSDoug Anderson /*
252590de2a4aSDoug Anderson  * Stop cpufreq at shutdown to make sure it isn't holding any locks
252690de2a4aSDoug Anderson  * or mutexes when secondary CPUs are halted.
252790de2a4aSDoug Anderson  */
252890de2a4aSDoug Anderson static struct syscore_ops cpufreq_syscore_ops = {
252990de2a4aSDoug Anderson 	.shutdown = cpufreq_suspend,
253090de2a4aSDoug Anderson };
253190de2a4aSDoug Anderson 
2532c82bd444SViresh Kumar struct kobject *cpufreq_global_kobject;
2533c82bd444SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject);
2534c82bd444SViresh Kumar 
25355a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void)
25365a01f2e8SVenkatesh Pallipadi {
2537a7b422cdSKonrad Rzeszutek Wilk 	if (cpufreq_disabled())
2538a7b422cdSKonrad Rzeszutek Wilk 		return -ENODEV;
2539a7b422cdSKonrad Rzeszutek Wilk 
25408eec1020SViresh Kumar 	cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
25418aa84ad8SThomas Renninger 	BUG_ON(!cpufreq_global_kobject);
25428aa84ad8SThomas Renninger 
254390de2a4aSDoug Anderson 	register_syscore_ops(&cpufreq_syscore_ops);
254490de2a4aSDoug Anderson 
25455a01f2e8SVenkatesh Pallipadi 	return 0;
25465a01f2e8SVenkatesh Pallipadi }
25475a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init);
2548