blob: 81e9d4412db8584b6e97086cf9a257edb2c7056f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/cpufreq/cpufreq.c
3 *
4 * Copyright (C) 2001 Russell King
5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
Viresh Kumarbb176f72013-06-19 14:19:33 +05306 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Ashok Rajc32b6b82005-10-30 14:59:54 -08008 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
Dave Jones32ee8c32006-02-28 00:43:23 -05009 * Added handling for CPU hotplug
Dave Jones8ff69732006-03-05 03:37:23 -050010 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
Ashok Rajc32b6b82005-10-30 14:59:54 -080012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 */
17
Viresh Kumardb701152012-10-23 01:29:03 +020018#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
Viresh Kumar5ff0a262013-08-06 22:53:03 +053020#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/cpufreq.h>
22#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/device.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053024#include <linux/init.h>
25#include <linux/kernel_stat.h>
26#include <linux/module.h>
akpm@osdl.org3fc54d32006-01-13 15:54:22 -080027#include <linux/mutex.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053028#include <linux/slab.h>
Viresh Kumar5a871822013-11-27 09:09:42 +053029#include <linux/suspend.h>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010030#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053031#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020032#include <trace/events/power.h>
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/**
Dave Jonescd878472006-08-11 17:59:28 -040035 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 * level driver of CPUFreq support, and its spinlock. This lock
37 * also protects the cpufreq_cpu_data array.
38 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020039static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070040static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +053041static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
Viresh Kumarbb176f72013-06-19 14:19:33 +053042static DEFINE_RWLOCK(cpufreq_driver_lock);
43static DEFINE_MUTEX(cpufreq_governor_lock);
Lukasz Majewskic88a1f82013-08-06 22:53:08 +053044static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarbb176f72013-06-19 14:19:33 +053045
Thomas Renninger084f3492007-07-09 11:35:28 -070046#ifdef CONFIG_HOTPLUG_CPU
47/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040048static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070049#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Viresh Kumar5a871822013-11-27 09:09:42 +053051/* Flag to suspend/resume CPUFreq governors */
52static bool cpufreq_suspended;
53
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053054static inline bool has_target(void)
55{
56 return cpufreq_driver->target_index || cpufreq_driver->target;
57}
58
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080059/*
Viresh Kumar6eed9402013-08-06 22:53:11 +053060 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
61 * sections
62 */
63static DECLARE_RWSEM(cpufreq_rwsem);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050066static int __cpufreq_governor(struct cpufreq_policy *policy,
67 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080068static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +000069static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71/**
Dave Jones32ee8c32006-02-28 00:43:23 -050072 * Two notifier lists: the "policy" list is involved in the
73 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 * "transition" list for kernel code that needs to handle
75 * changes to devices when the CPU clock speed changes.
76 * The mutex locks both lists.
77 */
Alan Sterne041c682006-03-27 01:16:30 -080078static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070079static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020081static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070082static int __init init_cpufreq_transition_notifier_list(void)
83{
84 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020085 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070086 return 0;
87}
Linus Torvaldsb3438f82006-11-20 11:47:18 -080088pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040090static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020091static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040092{
93 return off;
94}
95void disable_cpufreq(void)
96{
97 off = 1;
98}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -0500100static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000102bool have_governor_per_policy(void)
103{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530104 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000105}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000106EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000107
Viresh Kumar944e9a02013-05-16 05:09:57 +0000108struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
109{
110 if (have_governor_per_policy())
111 return &policy->kobj;
112 else
113 return cpufreq_global_kobject;
114}
115EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
116
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000117static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
118{
119 u64 idle_time;
120 u64 cur_wall_time;
121 u64 busy_time;
122
123 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
124
125 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
126 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
127 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
128 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
129 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
130 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
131
132 idle_time = cur_wall_time - busy_time;
133 if (wall)
134 *wall = cputime_to_usecs(cur_wall_time);
135
136 return cputime_to_usecs(idle_time);
137}
138
139u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
140{
141 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
142
143 if (idle_time == -1ULL)
144 return get_cpu_idle_time_jiffy(cpu, wall);
145 else if (!io_busy)
146 idle_time += get_cpu_iowait_time_us(cpu, wall);
147
148 return idle_time;
149}
150EXPORT_SYMBOL_GPL(get_cpu_idle_time);
151
Viresh Kumar70e9e772013-10-03 20:29:07 +0530152/*
153 * This is a generic cpufreq init() routine which can be used by cpufreq
154 * drivers of SMP systems. It will do following:
155 * - validate & show freq table passed
156 * - set policies transition latency
157 * - policy->cpus with all possible CPUs
158 */
159int cpufreq_generic_init(struct cpufreq_policy *policy,
160 struct cpufreq_frequency_table *table,
161 unsigned int transition_latency)
162{
163 int ret;
164
165 ret = cpufreq_table_validate_and_show(policy, table);
166 if (ret) {
167 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
168 return ret;
169 }
170
171 policy->cpuinfo.transition_latency = transition_latency;
172
173 /*
174 * The driver only supports the SMP configuartion where all processors
175 * share the clock and voltage and clock.
176 */
177 cpumask_setall(policy->cpus);
178
179 return 0;
180}
181EXPORT_SYMBOL_GPL(cpufreq_generic_init);
182
Viresh Kumar6eed9402013-08-06 22:53:11 +0530183struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530185 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 unsigned long flags;
187
Viresh Kumar6eed9402013-08-06 22:53:11 +0530188 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
189 return NULL;
190
191 if (!down_read_trylock(&cpufreq_rwsem))
192 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000195 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Viresh Kumar6eed9402013-08-06 22:53:11 +0530197 if (cpufreq_driver) {
198 /* get the CPU */
199 policy = per_cpu(cpufreq_cpu_data, cpu);
200 if (policy)
201 kobject_get(&policy->kobj);
202 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200203
Viresh Kumar6eed9402013-08-06 22:53:11 +0530204 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530206 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530207 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530209 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
212
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530213void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000215 if (cpufreq_disabled())
216 return;
217
Viresh Kumar6eed9402013-08-06 22:53:11 +0530218 kobject_put(&policy->kobj);
219 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
225 *********************************************************************/
226
227/**
228 * adjust_jiffies - adjust the system "loops_per_jiffy"
229 *
230 * This function alters the system "loops_per_jiffy" for the clock
231 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500232 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * per-CPU loops_per_jiffy value wherever possible.
234 */
235#ifndef CONFIG_SMP
236static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530237static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Arjan van de Ven858119e2006-01-14 13:20:43 -0800239static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 if (ci->flags & CPUFREQ_CONST_LOOPS)
242 return;
243
244 if (!l_p_j_ref_freq) {
245 l_p_j_ref = loops_per_jiffy;
246 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200247 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530248 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530250 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700251 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530252 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
253 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200254 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530255 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257}
258#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530259static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
260{
261 return;
262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#endif
264
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530265static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530266 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 BUG_ON(irqs_disabled());
269
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000270 if (cpufreq_disabled())
271 return;
272
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200273 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200274 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800275 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500280 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800281 * which is not equal to what the cpufreq core thinks is
282 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200284 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800285 if ((policy) && (policy->cpu == freqs->cpu) &&
286 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200287 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800288 " %u, cpufreq assumed %u kHz.\n",
289 freqs->old, policy->cur);
290 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700293 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800294 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
296 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 case CPUFREQ_POSTCHANGE:
299 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200300 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200301 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100302 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700303 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800304 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800305 if (likely(policy) && likely(policy->cpu == freqs->cpu))
306 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 break;
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530310
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530311/**
312 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
313 * on frequency transition.
314 *
315 * This function calls the transition notifiers and the "adjust_jiffies"
316 * function. It is called twice on all CPU frequency changes that have
317 * external effects.
318 */
319void cpufreq_notify_transition(struct cpufreq_policy *policy,
320 struct cpufreq_freqs *freqs, unsigned int state)
321{
322 for_each_cpu(freqs->cpu, policy->cpus)
323 __cpufreq_notify_transition(policy, freqs, state);
324}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
326
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328/*********************************************************************
329 * SYSFS INTERFACE *
330 *********************************************************************/
331
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700332static struct cpufreq_governor *__find_governor(const char *str_governor)
333{
334 struct cpufreq_governor *t;
335
336 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500337 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700338 return t;
339
340 return NULL;
341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/**
344 * cpufreq_parse_governor - parse a governor string
345 */
Dave Jones905d77c2008-03-05 14:28:32 -0500346static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 struct cpufreq_governor **governor)
348{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700349 int err = -EINVAL;
350
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200351 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700352 goto out;
353
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200354 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
356 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700357 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530358 } else if (!strnicmp(str_governor, "powersave",
359 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700361 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530363 } else if (has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700365
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800366 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700367
368 t = __find_governor(str_governor);
369
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700370 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700371 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700372
Kees Cook1a8e1462011-05-04 08:38:56 -0700373 mutex_unlock(&cpufreq_governor_mutex);
374 ret = request_module("cpufreq_%s", str_governor);
375 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700376
Kees Cook1a8e1462011-05-04 08:38:56 -0700377 if (ret == 0)
378 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700379 }
380
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700381 if (t != NULL) {
382 *governor = t;
383 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700385
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800386 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Dave Jones29464f22009-01-18 01:37:11 -0500388out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700389 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530393 * cpufreq_per_cpu_attr_read() / show_##file_name() -
394 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 *
396 * Write out information from cpufreq_driver->policy[cpu]; object must be
397 * "unsigned int".
398 */
399
Dave Jones32ee8c32006-02-28 00:43:23 -0500400#define show_one(file_name, object) \
401static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500402(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500403{ \
Dave Jones29464f22009-01-18 01:37:11 -0500404 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407show_one(cpuinfo_min_freq, cpuinfo.min_freq);
408show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100409show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410show_one(scaling_min_freq, min);
411show_one(scaling_max_freq, max);
412show_one(scaling_cur_freq, cur);
413
Viresh Kumar037ce832013-10-02 14:13:16 +0530414static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530415 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417/**
418 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
419 */
420#define store_one(file_name, object) \
421static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500422(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530424 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 struct cpufreq_policy new_policy; \
426 \
427 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
428 if (ret) \
429 return -EINVAL; \
430 \
Dave Jones29464f22009-01-18 01:37:11 -0500431 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (ret != 1) \
433 return -EINVAL; \
434 \
Viresh Kumar037ce832013-10-02 14:13:16 +0530435 ret = cpufreq_set_policy(policy, &new_policy); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200436 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 \
438 return ret ? ret : count; \
439}
440
Dave Jones29464f22009-01-18 01:37:11 -0500441store_one(scaling_min_freq, min);
442store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444/**
445 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
446 */
Dave Jones905d77c2008-03-05 14:28:32 -0500447static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
448 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800450 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (!cur_freq)
452 return sprintf(buf, "<unknown>");
453 return sprintf(buf, "%u\n", cur_freq);
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/**
457 * show_scaling_governor - show the current policy for the specified CPU
458 */
Dave Jones905d77c2008-03-05 14:28:32 -0500459static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Dave Jones29464f22009-01-18 01:37:11 -0500461 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 return sprintf(buf, "powersave\n");
463 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
464 return sprintf(buf, "performance\n");
465 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200466 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500467 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return -EINVAL;
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/**
472 * store_scaling_governor - store policy for the specified CPU
473 */
Dave Jones905d77c2008-03-05 14:28:32 -0500474static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
475 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530477 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 char str_governor[16];
479 struct cpufreq_policy new_policy;
480
481 ret = cpufreq_get_policy(&new_policy, policy->cpu);
482 if (ret)
483 return ret;
484
Dave Jones29464f22009-01-18 01:37:11 -0500485 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (ret != 1)
487 return -EINVAL;
488
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530489 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
490 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -EINVAL;
492
Viresh Kumar037ce832013-10-02 14:13:16 +0530493 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200494
495 policy->user_policy.policy = policy->policy;
496 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200497
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530498 if (ret)
499 return ret;
500 else
501 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504/**
505 * show_scaling_driver - show the cpufreq driver currently loaded
506 */
Dave Jones905d77c2008-03-05 14:28:32 -0500507static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200509 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512/**
513 * show_scaling_available_governors - show the available CPUfreq governors
514 */
Dave Jones905d77c2008-03-05 14:28:32 -0500515static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
516 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 ssize_t i = 0;
519 struct cpufreq_governor *t;
520
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530521 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 i += sprintf(buf, "performance powersave");
523 goto out;
524 }
525
526 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500527 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
528 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200530 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500532out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 i += sprintf(&buf[i], "\n");
534 return i;
535}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700536
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800537ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 ssize_t i = 0;
540 unsigned int cpu;
541
Rusty Russell835481d2009-01-04 05:18:06 -0800542 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (i)
544 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
545 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
546 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500547 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 i += sprintf(&buf[i], "\n");
550 return i;
551}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800552EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700554/**
555 * show_related_cpus - show the CPUs affected by each transition even if
556 * hw coordination is in use
557 */
558static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
559{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800560 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700561}
562
563/**
564 * show_affected_cpus - show the CPUs affected by each transition
565 */
566static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
567{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800568 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700569}
570
Venki Pallipadi9e769882007-10-26 10:18:21 -0700571static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500572 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700573{
574 unsigned int freq = 0;
575 unsigned int ret;
576
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700577 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700578 return -EINVAL;
579
580 ret = sscanf(buf, "%u", &freq);
581 if (ret != 1)
582 return -EINVAL;
583
584 policy->governor->store_setspeed(policy, freq);
585
586 return count;
587}
588
589static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
590{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700591 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700592 return sprintf(buf, "<unsupported>\n");
593
594 return policy->governor->show_setspeed(policy, buf);
595}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Thomas Renningere2f74f32009-11-19 12:31:01 +0100597/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200598 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100599 */
600static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
601{
602 unsigned int limit;
603 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200604 if (cpufreq_driver->bios_limit) {
605 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100606 if (!ret)
607 return sprintf(buf, "%u\n", limit);
608 }
609 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
610}
611
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200612cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
613cpufreq_freq_attr_ro(cpuinfo_min_freq);
614cpufreq_freq_attr_ro(cpuinfo_max_freq);
615cpufreq_freq_attr_ro(cpuinfo_transition_latency);
616cpufreq_freq_attr_ro(scaling_available_governors);
617cpufreq_freq_attr_ro(scaling_driver);
618cpufreq_freq_attr_ro(scaling_cur_freq);
619cpufreq_freq_attr_ro(bios_limit);
620cpufreq_freq_attr_ro(related_cpus);
621cpufreq_freq_attr_ro(affected_cpus);
622cpufreq_freq_attr_rw(scaling_min_freq);
623cpufreq_freq_attr_rw(scaling_max_freq);
624cpufreq_freq_attr_rw(scaling_governor);
625cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Dave Jones905d77c2008-03-05 14:28:32 -0500627static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 &cpuinfo_min_freq.attr,
629 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100630 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 &scaling_min_freq.attr,
632 &scaling_max_freq.attr,
633 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700634 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 &scaling_governor.attr,
636 &scaling_driver.attr,
637 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700638 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 NULL
640};
641
Dave Jones29464f22009-01-18 01:37:11 -0500642#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
643#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Dave Jones29464f22009-01-18 01:37:11 -0500645static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Dave Jones905d77c2008-03-05 14:28:32 -0500647 struct cpufreq_policy *policy = to_policy(kobj);
648 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530649 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530650
651 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530652 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800653
viresh kumarad7722d2013-10-18 19:10:15 +0530654 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800655
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530656 if (fattr->show)
657 ret = fattr->show(policy, buf);
658 else
659 ret = -EIO;
660
viresh kumarad7722d2013-10-18 19:10:15 +0530661 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530662 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return ret;
665}
666
Dave Jones905d77c2008-03-05 14:28:32 -0500667static ssize_t store(struct kobject *kobj, struct attribute *attr,
668 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Dave Jones905d77c2008-03-05 14:28:32 -0500670 struct cpufreq_policy *policy = to_policy(kobj);
671 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500672 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530673
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530674 get_online_cpus();
675
676 if (!cpu_online(policy->cpu))
677 goto unlock;
678
Viresh Kumar6eed9402013-08-06 22:53:11 +0530679 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530680 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800681
viresh kumarad7722d2013-10-18 19:10:15 +0530682 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800683
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530684 if (fattr->store)
685 ret = fattr->store(policy, buf, count);
686 else
687 ret = -EIO;
688
viresh kumarad7722d2013-10-18 19:10:15 +0530689 up_write(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530690
Viresh Kumar6eed9402013-08-06 22:53:11 +0530691 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530692unlock:
693 put_online_cpus();
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return ret;
696}
697
Dave Jones905d77c2008-03-05 14:28:32 -0500698static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Dave Jones905d77c2008-03-05 14:28:32 -0500700 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200701 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 complete(&policy->kobj_unregister);
703}
704
Emese Revfy52cf25d2010-01-19 02:58:23 +0100705static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 .show = show,
707 .store = store,
708};
709
710static struct kobj_type ktype_cpufreq = {
711 .sysfs_ops = &sysfs_ops,
712 .default_attrs = default_attrs,
713 .release = cpufreq_sysfs_release,
714};
715
Viresh Kumar2361be22013-05-17 16:09:09 +0530716struct kobject *cpufreq_global_kobject;
717EXPORT_SYMBOL(cpufreq_global_kobject);
718
719static int cpufreq_global_kobject_usage;
720
721int cpufreq_get_global_kobject(void)
722{
723 if (!cpufreq_global_kobject_usage++)
724 return kobject_add(cpufreq_global_kobject,
725 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
726
727 return 0;
728}
729EXPORT_SYMBOL(cpufreq_get_global_kobject);
730
731void cpufreq_put_global_kobject(void)
732{
733 if (!--cpufreq_global_kobject_usage)
734 kobject_del(cpufreq_global_kobject);
735}
736EXPORT_SYMBOL(cpufreq_put_global_kobject);
737
738int cpufreq_sysfs_create_file(const struct attribute *attr)
739{
740 int ret = cpufreq_get_global_kobject();
741
742 if (!ret) {
743 ret = sysfs_create_file(cpufreq_global_kobject, attr);
744 if (ret)
745 cpufreq_put_global_kobject();
746 }
747
748 return ret;
749}
750EXPORT_SYMBOL(cpufreq_sysfs_create_file);
751
752void cpufreq_sysfs_remove_file(const struct attribute *attr)
753{
754 sysfs_remove_file(cpufreq_global_kobject, attr);
755 cpufreq_put_global_kobject();
756}
757EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
758
Dave Jones19d6f7e2009-07-08 17:35:39 -0400759/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200760static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400761{
762 unsigned int j;
763 int ret = 0;
764
765 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800766 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400767
Viresh Kumar308b60e2013-07-31 14:35:14 +0200768 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400769 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400770
Viresh Kumare8fdde12013-07-31 14:31:33 +0200771 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800772 cpu_dev = get_cpu_device(j);
773 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400774 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200775 if (ret)
776 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400777 }
778 return ret;
779}
780
Viresh Kumar308b60e2013-07-31 14:35:14 +0200781static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800782 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400783{
784 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400785 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400786
787 /* prepare interface data */
788 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800789 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400790 if (ret)
791 return ret;
792
793 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200794 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400795 while ((drv_attr) && (*drv_attr)) {
796 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
797 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200798 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400799 drv_attr++;
800 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200801 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400802 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
803 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200804 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400805 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530806 if (has_target()) {
Dave Jones909a6942009-07-08 18:05:42 -0400807 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
808 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200809 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400810 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200811 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100812 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
813 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200814 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100815 }
Dave Jones909a6942009-07-08 18:05:42 -0400816
Viresh Kumar308b60e2013-07-31 14:35:14 +0200817 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400818 if (ret)
819 goto err_out_kobj_put;
820
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530821 return ret;
822
823err_out_kobj_put:
824 kobject_put(&policy->kobj);
825 wait_for_completion(&policy->kobj_unregister);
826 return ret;
827}
828
829static void cpufreq_init_policy(struct cpufreq_policy *policy)
830{
831 struct cpufreq_policy new_policy;
832 int ret = 0;
833
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530834 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar037ce832013-10-02 14:13:16 +0530835 /* assure that the starting sequence is run in cpufreq_set_policy */
Dave Jonesecf7e462009-07-08 18:48:47 -0400836 policy->governor = NULL;
837
838 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530839 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400840 policy->user_policy.policy = policy->policy;
841 policy->user_policy.governor = policy->governor;
842
843 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200844 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200845 if (cpufreq_driver->exit)
846 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400847 }
Dave Jones909a6942009-07-08 18:05:42 -0400848}
849
Viresh Kumarfcf80582013-01-29 14:39:08 +0000850#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200851static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
852 unsigned int cpu, struct device *dev,
853 bool frozen)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000854{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530855 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000856 unsigned long flags;
857
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530858 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530859 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
860 if (ret) {
861 pr_err("%s: Failed to stop governor\n", __func__);
862 return ret;
863 }
864 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000865
viresh kumarad7722d2013-10-18 19:10:15 +0530866 down_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530867
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000868 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530869
Viresh Kumarfcf80582013-01-29 14:39:08 +0000870 cpumask_set_cpu(cpu, policy->cpus);
871 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000872 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000873
viresh kumarad7722d2013-10-18 19:10:15 +0530874 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530875
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530876 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530877 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
878 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
879 pr_err("%s: Failed to start governor\n", __func__);
880 return ret;
881 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200882 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000883
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530884 /* Don't touch sysfs links during light-weight init */
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200885 if (!frozen)
886 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000887
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530888 return ret;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000889}
890#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530892static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
893{
894 struct cpufreq_policy *policy;
895 unsigned long flags;
896
Lan Tianyu44871c92013-09-11 15:05:05 +0800897 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530898
899 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
900
Lan Tianyu44871c92013-09-11 15:05:05 +0800901 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530902
903 return policy;
904}
905
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530906static struct cpufreq_policy *cpufreq_policy_alloc(void)
907{
908 struct cpufreq_policy *policy;
909
910 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
911 if (!policy)
912 return NULL;
913
914 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
915 goto err_free_policy;
916
917 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
918 goto err_free_cpumask;
919
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530920 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +0530921 init_rwsem(&policy->rwsem);
922
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530923 return policy;
924
925err_free_cpumask:
926 free_cpumask_var(policy->cpus);
927err_free_policy:
928 kfree(policy);
929
930 return NULL;
931}
932
933static void cpufreq_policy_free(struct cpufreq_policy *policy)
934{
935 free_cpumask_var(policy->related_cpus);
936 free_cpumask_var(policy->cpus);
937 kfree(policy);
938}
939
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530940static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
941{
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +0530942 if (WARN_ON(cpu == policy->cpu))
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +0530943 return;
944
viresh kumarad7722d2013-10-18 19:10:15 +0530945 down_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +0530946
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530947 policy->last_cpu = policy->cpu;
948 policy->cpu = cpu;
949
viresh kumarad7722d2013-10-18 19:10:15 +0530950 up_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +0530951
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530952 cpufreq_frequency_table_update_policy_cpu(policy);
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530953 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
954 CPUFREQ_UPDATE_POLICY_CPU, policy);
955}
956
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530957static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
958 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000960 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530961 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500964#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +0530965 struct cpufreq_policy *tpolicy;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000966 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500967#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Ashok Rajc32b6b82005-10-30 14:59:54 -0800969 if (cpu_is_offline(cpu))
970 return 0;
971
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200972 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974#ifdef CONFIG_SMP
975 /* check whether a different CPU already registered this
976 * CPU because it is in the same boat. */
977 policy = cpufreq_cpu_get(cpu);
978 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -0500979 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 return 0;
981 }
Li Zhong5025d622013-08-21 01:31:08 +0200982#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +0000983
Viresh Kumar6eed9402013-08-06 22:53:11 +0530984 if (!down_read_trylock(&cpufreq_rwsem))
985 return 0;
986
Viresh Kumarfcf80582013-01-29 14:39:08 +0000987#ifdef CONFIG_HOTPLUG_CPU
988 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000989 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +0530990 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
991 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000992 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +0530993 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev, frozen);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530994 up_read(&cpufreq_rwsem);
995 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530996 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000997 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000998 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000999#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301001 if (frozen)
1002 /* Restore the saved policy when doing light-weight init */
1003 policy = cpufreq_policy_restore(cpu);
1004 else
1005 policy = cpufreq_policy_alloc();
1006
Dave Jones059019a2009-07-08 16:30:03 -04001007 if (!policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto nomem_out;
Dave Jones059019a2009-07-08 16:30:03 -04001009
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301010
1011 /*
1012 * In the resume path, since we restore a saved policy, the assignment
1013 * to policy->cpu is like an update of the existing policy, rather than
1014 * the creation of a brand new one. So we need to perform this update
1015 * by invoking update_policy_cpu().
1016 */
1017 if (frozen && cpu != policy->cpu)
1018 update_policy_cpu(policy, cpu);
1019 else
1020 policy->cpu = cpu;
1021
Viresh Kumar65922462013-02-07 10:56:03 +05301022 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001023 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001026 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 /* call driver. From then on the cpufreq must be able
1029 * to accept all calls to ->verify and ->setpolicy for this CPU
1030 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001031 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001033 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301034 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001036
Viresh Kumarda60ce92013-10-03 20:28:30 +05301037 if (cpufreq_driver->get) {
1038 policy->cur = cpufreq_driver->get(policy->cpu);
1039 if (!policy->cur) {
1040 pr_err("%s: ->get() failed\n", __func__);
1041 goto err_get_freq;
1042 }
1043 }
1044
Viresh Kumarfcf80582013-01-29 14:39:08 +00001045 /* related cpus should atleast have policy->cpus */
1046 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1047
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001048 /*
1049 * affected cpus must always be the one, which are online. We aren't
1050 * managing offline cpus here.
1051 */
1052 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1053
Mike Chan187d9f42008-12-04 12:19:17 -08001054 policy->user_policy.min = policy->min;
1055 policy->user_policy.max = policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Thomas Renningera1531ac2008-07-29 22:32:58 -07001057 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1058 CPUFREQ_START, policy);
1059
Viresh Kumarfcf80582013-01-29 14:39:08 +00001060#ifdef CONFIG_HOTPLUG_CPU
1061 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1062 if (gov) {
1063 policy->governor = gov;
1064 pr_debug("Restoring governor %s for cpu %d\n",
1065 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001066 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001067#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301069 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301070 for_each_cpu(j, policy->cpus)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301071 per_cpu(cpufreq_cpu_data, j) = policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301072 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1073
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301074 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001075 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301076 if (ret)
1077 goto err_out_unregister;
1078 }
Dave Jones8ff69732006-03-05 03:37:23 -05001079
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301080 write_lock_irqsave(&cpufreq_driver_lock, flags);
1081 list_add(&policy->policy_list, &cpufreq_policy_list);
1082 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1083
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301084 cpufreq_init_policy(policy);
1085
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001086 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301087 up_read(&cpufreq_rwsem);
1088
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001089 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return 0;
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001094 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301095 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001096 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001097 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Viresh Kumarda60ce92013-10-03 20:28:30 +05301099err_get_freq:
1100 if (cpufreq_driver->exit)
1101 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301102err_set_policy_cpu:
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301103 cpufreq_policy_free(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301105 up_read(&cpufreq_rwsem);
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return ret;
1108}
1109
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301110/**
1111 * cpufreq_add_dev - add a CPU device
1112 *
1113 * Adds the cpufreq interface for a CPU device.
1114 *
1115 * The Oracle says: try running cpufreq registration/unregistration concurrently
1116 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1117 * mess up, but more thorough testing is needed. - Mathieu
1118 */
1119static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1120{
1121 return __cpufreq_add_dev(dev, sif, false);
1122}
1123
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301124static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301125 unsigned int old_cpu, bool frozen)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301126{
1127 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301128 int ret;
1129
1130 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301131 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301132
1133 /* Don't touch sysfs files during light-weight tear-down */
1134 if (frozen)
1135 return cpu_dev->id;
1136
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301137 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301138 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301139 if (ret) {
1140 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1141
viresh kumarad7722d2013-10-18 19:10:15 +05301142 down_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301143 cpumask_set_cpu(old_cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301144 up_write(&policy->rwsem);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301145
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301146 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301147 "cpufreq");
1148
1149 return -EINVAL;
1150 }
1151
1152 return cpu_dev->id;
1153}
1154
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301155static int __cpufreq_remove_dev_prepare(struct device *dev,
1156 struct subsys_interface *sif,
1157 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301159 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301160 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301162 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001164 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001166 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301168 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301170 /* Save the policy somewhere when doing a light-weight tear-down */
1171 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301172 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301173
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001174 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301176 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001177 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301181 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301182 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1183 if (ret) {
1184 pr_err("%s: Failed to stop governor\n", __func__);
1185 return ret;
1186 }
1187 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001188
Jacob Shin27ecddc2011-04-27 13:32:11 -05001189#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001190 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001191 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301192 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001193#endif
1194
viresh kumarad7722d2013-10-18 19:10:15 +05301195 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301196 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301197 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301199 if (cpu != policy->cpu) {
1200 if (!frozen)
1201 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001202 } else if (cpus > 1) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301203 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301204 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301205 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301206
1207 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301208 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1209 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301210 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001211 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001212 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001213
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301214 return 0;
1215}
1216
1217static int __cpufreq_remove_dev_finish(struct device *dev,
1218 struct subsys_interface *sif,
1219 bool frozen)
1220{
1221 unsigned int cpu = dev->id, cpus;
1222 int ret;
1223 unsigned long flags;
1224 struct cpufreq_policy *policy;
1225 struct kobject *kobj;
1226 struct completion *cmp;
1227
1228 read_lock_irqsave(&cpufreq_driver_lock, flags);
1229 policy = per_cpu(cpufreq_cpu_data, cpu);
1230 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1231
1232 if (!policy) {
1233 pr_debug("%s: No cpu_data found\n", __func__);
1234 return -EINVAL;
1235 }
1236
viresh kumarad7722d2013-10-18 19:10:15 +05301237 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301238 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301239
1240 if (cpus > 1)
1241 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301242 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301243
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001244 /* If cpu is last user of policy, free policy */
1245 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301246 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301247 ret = __cpufreq_governor(policy,
1248 CPUFREQ_GOV_POLICY_EXIT);
1249 if (ret) {
1250 pr_err("%s: Failed to exit governor\n",
1251 __func__);
1252 return ret;
1253 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301254 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001255
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301256 if (!frozen) {
viresh kumarad7722d2013-10-18 19:10:15 +05301257 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301258 kobj = &policy->kobj;
1259 cmp = &policy->kobj_unregister;
viresh kumarad7722d2013-10-18 19:10:15 +05301260 up_read(&policy->rwsem);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301261 kobject_put(kobj);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001262
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301263 /*
1264 * We need to make sure that the underlying kobj is
1265 * actually not referenced anymore by anybody before we
1266 * proceed with unloading.
1267 */
1268 pr_debug("waiting for dropping of refcount\n");
1269 wait_for_completion(cmp);
1270 pr_debug("wait complete\n");
1271 }
1272
1273 /*
1274 * Perform the ->exit() even during light-weight tear-down,
1275 * since this is a core component, and is essential for the
1276 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001277 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001278 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301279 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001280
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301281 /* Remove policy from list of active policies */
1282 write_lock_irqsave(&cpufreq_driver_lock, flags);
1283 list_del(&policy->policy_list);
1284 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1285
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301286 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301287 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001288 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301289 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301290 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1291 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1292 pr_err("%s: Failed to start governor\n",
1293 __func__);
1294 return ret;
1295 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001296 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Viresh Kumar474deff2013-08-20 12:08:25 +05301299 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return 0;
1301}
1302
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301303/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301304 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301305 *
1306 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301307 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001308static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001309{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001310 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301311 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001312
1313 if (cpu_is_offline(cpu))
1314 return 0;
1315
Viresh Kumar27a862e2013-10-02 14:13:14 +05301316 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1317
1318 if (!ret)
1319 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1320
1321 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001322}
1323
David Howells65f27f32006-11-22 14:55:48 +00001324static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325{
David Howells65f27f32006-11-22 14:55:48 +00001326 struct cpufreq_policy *policy =
1327 container_of(work, struct cpufreq_policy, update);
1328 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001329 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 cpufreq_update_policy(cpu);
1331}
1332
1333/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301334 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1335 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 * @cpu: cpu number
1337 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1338 * @new_freq: CPU frequency the CPU actually runs at
1339 *
Dave Jones29464f22009-01-18 01:37:11 -05001340 * We adjust to current frequency first, and need to clean up later.
1341 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301343static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1344 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301346 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301348 unsigned long flags;
1349
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001350 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 freqs.old = old_freq;
1354 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301355
1356 read_lock_irqsave(&cpufreq_driver_lock, flags);
1357 policy = per_cpu(cpufreq_cpu_data, cpu);
1358 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1359
1360 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1361 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
Dave Jones32ee8c32006-02-28 00:43:23 -05001364/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301365 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001366 * @cpu: CPU number
1367 *
1368 * This is the last known freq, without actually getting it from the driver.
1369 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1370 */
1371unsigned int cpufreq_quick_get(unsigned int cpu)
1372{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001373 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301374 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001375
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001376 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1377 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001378
1379 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001380 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301381 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001382 cpufreq_cpu_put(policy);
1383 }
1384
Dave Jones4d34a672008-02-07 16:33:49 -05001385 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001386}
1387EXPORT_SYMBOL(cpufreq_quick_get);
1388
Jesse Barnes3d737102011-06-28 10:59:12 -07001389/**
1390 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1391 * @cpu: CPU number
1392 *
1393 * Just return the max possible frequency for a given CPU.
1394 */
1395unsigned int cpufreq_quick_get_max(unsigned int cpu)
1396{
1397 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1398 unsigned int ret_freq = 0;
1399
1400 if (policy) {
1401 ret_freq = policy->max;
1402 cpufreq_cpu_put(policy);
1403 }
1404
1405 return ret_freq;
1406}
1407EXPORT_SYMBOL(cpufreq_quick_get_max);
1408
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001409static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001411 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301412 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001414 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001415 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001417 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301419 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001420 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301421 /* verify no discrepancy between actual and
1422 saved value exists */
1423 if (unlikely(ret_freq != policy->cur)) {
1424 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 schedule_work(&policy->update);
1426 }
1427 }
1428
Dave Jones4d34a672008-02-07 16:33:49 -05001429 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001432/**
1433 * cpufreq_get - get the current CPU frequency (in kHz)
1434 * @cpu: CPU number
1435 *
1436 * Get the CPU current (static) CPU frequency
1437 */
1438unsigned int cpufreq_get(unsigned int cpu)
1439{
viresh kumarad7722d2013-10-18 19:10:15 +05301440 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001441 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001442
Viresh Kumar26ca8692013-09-20 22:37:31 +05301443 if (cpufreq_disabled() || !cpufreq_driver)
1444 return -ENOENT;
1445
viresh kumarad7722d2013-10-18 19:10:15 +05301446 BUG_ON(!policy);
1447
Viresh Kumar6eed9402013-08-06 22:53:11 +05301448 if (!down_read_trylock(&cpufreq_rwsem))
1449 return 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001450
viresh kumarad7722d2013-10-18 19:10:15 +05301451 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001452
1453 ret_freq = __cpufreq_get(cpu);
1454
viresh kumarad7722d2013-10-18 19:10:15 +05301455 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301456 up_read(&cpufreq_rwsem);
1457
Dave Jones4d34a672008-02-07 16:33:49 -05001458 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460EXPORT_SYMBOL(cpufreq_get);
1461
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001462static struct subsys_interface cpufreq_interface = {
1463 .name = "cpufreq",
1464 .subsys = &cpu_subsys,
1465 .add_dev = cpufreq_add_dev,
1466 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001467};
1468
Viresh Kumar5a871822013-11-27 09:09:42 +05301469void cpufreq_suspend(void)
1470{
1471 struct cpufreq_policy *policy;
1472
1473 if (!has_target())
1474 return;
1475
1476 pr_debug("%s: Suspending Governors\n", __func__);
1477
1478 list_for_each_entry(policy, &cpufreq_policy_list, policy_list)
1479 if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP))
1480 pr_err("%s: Failed to stop governor for policy: %p\n",
1481 __func__, policy);
1482
1483 cpufreq_suspended = true;
1484}
1485
1486void cpufreq_resume(void)
1487{
1488 struct cpufreq_policy *policy;
1489
1490 if (!has_target())
1491 return;
1492
1493 pr_debug("%s: Resuming Governors\n", __func__);
1494
1495 cpufreq_suspended = false;
1496
1497 list_for_each_entry(policy, &cpufreq_policy_list, policy_list)
1498 if (__cpufreq_governor(policy, CPUFREQ_GOV_START)
1499 || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))
1500 pr_err("%s: Failed to start governor for policy: %p\n",
1501 __func__, policy);
1502}
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001505 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1506 *
1507 * This function is only executed for the boot processor. The other CPUs
1508 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001509 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001510static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001511{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301512 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001513
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001514 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301515 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001516
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001517 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001518
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001519 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301520 policy = cpufreq_cpu_get(cpu);
1521 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001522 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001523
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001524 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301525 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001526 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001527 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301528 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001529 }
1530
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301531 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001532 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001533}
1534
1535/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001536 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 *
1538 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001539 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1540 * restored. It will verify that the current freq is in sync with
1541 * what we believe it to be. This is a bit later than when it
1542 * should be, but nonethteless it's better than calling
1543 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001544 *
1545 * This function is only executed for the boot CPU. The other CPUs have not
1546 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001548static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301550 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001551
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001552 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301553 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001555 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001557 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301558 policy = cpufreq_cpu_get(cpu);
1559 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001560 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001562 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301563 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (ret) {
1565 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301566 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001567 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569 }
1570
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301571 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001572
Dave Jonesc9060492008-02-07 16:32:18 -05001573fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301574 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001577static struct syscore_ops cpufreq_syscore_ops = {
1578 .suspend = cpufreq_bp_suspend,
1579 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580};
1581
Borislav Petkov9d950462013-01-20 10:24:28 +00001582/**
1583 * cpufreq_get_current_driver - return current driver's name
1584 *
1585 * Return the name string of the currently loaded cpufreq driver
1586 * or NULL, if none.
1587 */
1588const char *cpufreq_get_current_driver(void)
1589{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001590 if (cpufreq_driver)
1591 return cpufreq_driver->name;
1592
1593 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001594}
1595EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597/*********************************************************************
1598 * NOTIFIER LISTS INTERFACE *
1599 *********************************************************************/
1600
1601/**
1602 * cpufreq_register_notifier - register a driver with cpufreq
1603 * @nb: notifier function to register
1604 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1605 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001606 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 * are notified about clock rate changes (once before and once after
1608 * the transition), or a list of drivers that are notified about
1609 * changes in cpufreq policy.
1610 *
1611 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001612 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 */
1614int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1615{
1616 int ret;
1617
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001618 if (cpufreq_disabled())
1619 return -EINVAL;
1620
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001621 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 switch (list) {
1624 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001625 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001626 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 break;
1628 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001629 ret = blocking_notifier_chain_register(
1630 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 break;
1632 default:
1633 ret = -EINVAL;
1634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 return ret;
1637}
1638EXPORT_SYMBOL(cpufreq_register_notifier);
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640/**
1641 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1642 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301643 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 *
1645 * Remove a driver from the CPU frequency notifier list.
1646 *
1647 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001648 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 */
1650int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1651{
1652 int ret;
1653
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001654 if (cpufreq_disabled())
1655 return -EINVAL;
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 switch (list) {
1658 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001659 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001660 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 break;
1662 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001663 ret = blocking_notifier_chain_unregister(
1664 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 break;
1666 default:
1667 ret = -EINVAL;
1668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 return ret;
1671}
1672EXPORT_SYMBOL(cpufreq_unregister_notifier);
1673
1674
1675/*********************************************************************
1676 * GOVERNORS *
1677 *********************************************************************/
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679int __cpufreq_driver_target(struct cpufreq_policy *policy,
1680 unsigned int target_freq,
1681 unsigned int relation)
1682{
1683 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001684 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001685
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001686 if (cpufreq_disabled())
1687 return -ENODEV;
1688
Viresh Kumar72499242012-10-31 01:28:21 +01001689 /* Make sure that target_freq is within supported range */
1690 if (target_freq > policy->max)
1691 target_freq = policy->max;
1692 if (target_freq < policy->min)
1693 target_freq = policy->min;
1694
1695 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1696 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001697
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301698 /*
1699 * This might look like a redundant call as we are checking it again
1700 * after finding index. But it is left intentionally for cases where
1701 * exactly same freq is called again and so we can save on few function
1702 * calls.
1703 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001704 if (target_freq == policy->cur)
1705 return 0;
1706
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001707 if (cpufreq_driver->target)
1708 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301709 else if (cpufreq_driver->target_index) {
1710 struct cpufreq_frequency_table *freq_table;
Viresh Kumard4019f02013-08-14 19:38:24 +05301711 struct cpufreq_freqs freqs;
1712 bool notify;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301713 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001714
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301715 freq_table = cpufreq_frequency_get_table(policy->cpu);
1716 if (unlikely(!freq_table)) {
1717 pr_err("%s: Unable to find freq_table\n", __func__);
1718 goto out;
1719 }
1720
1721 retval = cpufreq_frequency_table_target(policy, freq_table,
1722 target_freq, relation, &index);
1723 if (unlikely(retval)) {
1724 pr_err("%s: Unable to find matching freq\n", __func__);
1725 goto out;
1726 }
1727
Viresh Kumard4019f02013-08-14 19:38:24 +05301728 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301729 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301730 goto out;
1731 }
1732
1733 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1734
1735 if (notify) {
1736 freqs.old = policy->cur;
1737 freqs.new = freq_table[index].frequency;
1738 freqs.flags = 0;
1739
1740 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1741 __func__, policy->cpu, freqs.old,
1742 freqs.new);
1743
1744 cpufreq_notify_transition(policy, &freqs,
1745 CPUFREQ_PRECHANGE);
1746 }
1747
1748 retval = cpufreq_driver->target_index(policy, index);
1749 if (retval)
1750 pr_err("%s: Failed to change cpu frequency: %d\n",
1751 __func__, retval);
1752
1753 if (notify) {
1754 /*
1755 * Notify with old freq in case we failed to change
1756 * frequency
1757 */
1758 if (retval)
1759 freqs.new = freqs.old;
1760
1761 cpufreq_notify_transition(policy, &freqs,
1762 CPUFREQ_POSTCHANGE);
1763 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301764 }
1765
1766out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return retval;
1768}
1769EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1770
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771int cpufreq_driver_target(struct cpufreq_policy *policy,
1772 unsigned int target_freq,
1773 unsigned int relation)
1774{
Julia Lawallf1829e42008-07-25 22:44:53 +02001775 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
viresh kumarad7722d2013-10-18 19:10:15 +05301777 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 ret = __cpufreq_driver_target(policy, target_freq, relation);
1780
viresh kumarad7722d2013-10-18 19:10:15 +05301781 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return ret;
1784}
1785EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1786
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001787/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001788 * when "event" is CPUFREQ_GOV_LIMITS
1789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301791static int __cpufreq_governor(struct cpufreq_policy *policy,
1792 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Dave Jonescc993ca2005-07-28 09:43:56 -07001794 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001795
1796 /* Only must be defined when default governor is known to have latency
1797 restrictions, like e.g. conservative or ondemand.
1798 That this is the case is already ensured in Kconfig
1799 */
1800#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1801 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1802#else
1803 struct cpufreq_governor *gov = NULL;
1804#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001805
Viresh Kumar5a871822013-11-27 09:09:42 +05301806 /* Don't start any governor operations if we are entering suspend */
1807 if (cpufreq_suspended)
1808 return 0;
1809
Thomas Renninger1c256242007-10-02 13:28:12 -07001810 if (policy->governor->max_transition_latency &&
1811 policy->cpuinfo.transition_latency >
1812 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001813 if (!gov)
1814 return -EINVAL;
1815 else {
1816 printk(KERN_WARNING "%s governor failed, too long"
1817 " transition latency of HW, fallback"
1818 " to %s governor\n",
1819 policy->governor->name,
1820 gov->name);
1821 policy->governor = gov;
1822 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Viresh Kumarfe492f32013-08-06 22:53:10 +05301825 if (event == CPUFREQ_GOV_POLICY_INIT)
1826 if (!try_module_get(policy->governor->owner))
1827 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001829 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301830 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001831
1832 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301833 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301834 || (!policy->governor_enabled
1835 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001836 mutex_unlock(&cpufreq_governor_lock);
1837 return -EBUSY;
1838 }
1839
1840 if (event == CPUFREQ_GOV_STOP)
1841 policy->governor_enabled = false;
1842 else if (event == CPUFREQ_GOV_START)
1843 policy->governor_enabled = true;
1844
1845 mutex_unlock(&cpufreq_governor_lock);
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 ret = policy->governor->governor(policy, event);
1848
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001849 if (!ret) {
1850 if (event == CPUFREQ_GOV_POLICY_INIT)
1851 policy->governor->initialized++;
1852 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1853 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001854 } else {
1855 /* Restore original values */
1856 mutex_lock(&cpufreq_governor_lock);
1857 if (event == CPUFREQ_GOV_STOP)
1858 policy->governor_enabled = true;
1859 else if (event == CPUFREQ_GOV_START)
1860 policy->governor_enabled = false;
1861 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001862 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001863
Viresh Kumarfe492f32013-08-06 22:53:10 +05301864 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1865 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 module_put(policy->governor->owner);
1867
1868 return ret;
1869}
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871int cpufreq_register_governor(struct cpufreq_governor *governor)
1872{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001873 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 if (!governor)
1876 return -EINVAL;
1877
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001878 if (cpufreq_disabled())
1879 return -ENODEV;
1880
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001881 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001882
Viresh Kumarb3940582013-02-01 05:42:58 +00001883 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001884 err = -EBUSY;
1885 if (__find_governor(governor->name) == NULL) {
1886 err = 0;
1887 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Dave Jones32ee8c32006-02-28 00:43:23 -05001890 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001891 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1896{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001897#ifdef CONFIG_HOTPLUG_CPU
1898 int cpu;
1899#endif
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (!governor)
1902 return;
1903
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001904 if (cpufreq_disabled())
1905 return;
1906
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001907#ifdef CONFIG_HOTPLUG_CPU
1908 for_each_present_cpu(cpu) {
1909 if (cpu_online(cpu))
1910 continue;
1911 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1912 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1913 }
1914#endif
1915
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001916 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001918 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return;
1920}
1921EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1922
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924/*********************************************************************
1925 * POLICY INTERFACE *
1926 *********************************************************************/
1927
1928/**
1929 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001930 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1931 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 *
1933 * Reads the current cpufreq policy.
1934 */
1935int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1936{
1937 struct cpufreq_policy *cpu_policy;
1938 if (!policy)
1939 return -EINVAL;
1940
1941 cpu_policy = cpufreq_cpu_get(cpu);
1942 if (!cpu_policy)
1943 return -EINVAL;
1944
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301945 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return 0;
1949}
1950EXPORT_SYMBOL(cpufreq_get_policy);
1951
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001952/*
Viresh Kumar037ce832013-10-02 14:13:16 +05301953 * policy : current policy.
1954 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001955 */
Viresh Kumar037ce832013-10-02 14:13:16 +05301956static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301957 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001959 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301961 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1962 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301964 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301966 if (new_policy->min > policy->max || new_policy->max < policy->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001967 ret = -EINVAL;
1968 goto error_out;
1969 }
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301972 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 if (ret)
1974 goto error_out;
1975
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001977 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301978 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001981 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301982 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
Viresh Kumarbb176f72013-06-19 14:19:33 +05301984 /*
1985 * verify the cpu speed can be set within this limit, which might be
1986 * different to the first one
1987 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301988 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08001989 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001993 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301994 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301996 policy->min = new_policy->min;
1997 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001999 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302000 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002002 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302003 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002004 pr_debug("setting range\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302005 ret = cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302007 if (new_policy->governor != policy->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 /* save old, working values */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302009 struct cpufreq_governor *old_gov = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002011 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 /* end old governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302014 if (policy->governor) {
2015 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
viresh kumarad7722d2013-10-18 19:10:15 +05302016 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302017 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002018 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302019 down_write(&policy->rwsem);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022 /* start new governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302023 policy->governor = new_policy->governor;
2024 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2025 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002026 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00002027 } else {
viresh kumarad7722d2013-10-18 19:10:15 +05302028 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302029 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002030 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302031 down_write(&policy->rwsem);
Viresh Kumar955ef482013-05-16 05:09:58 +00002032 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002033 }
2034
2035 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002037 pr_debug("starting governor %s failed\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302038 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (old_gov) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302040 policy->governor = old_gov;
2041 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002042 CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302043 __cpufreq_governor(policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302044 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 }
2046 ret = -EINVAL;
2047 goto error_out;
2048 }
2049 /* might be a policy change, too, so fall through */
2050 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002051 pr_debug("governor: change or update limits\n");
Viresh Kumar3de9bde2013-08-06 22:53:13 +05302052 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
2054
Dave Jones7d5e3502006-02-02 17:03:42 -05002055error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 return ret;
2057}
2058
2059/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2061 * @cpu: CPU which shall be re-evaluated
2062 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002063 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 * at different times.
2065 */
2066int cpufreq_update_policy(unsigned int cpu)
2067{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302068 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2069 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002070 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302072 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02002073 ret = -ENODEV;
2074 goto no_policy;
2075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
viresh kumarad7722d2013-10-18 19:10:15 +05302077 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002079 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302080 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302081 new_policy.min = policy->user_policy.min;
2082 new_policy.max = policy->user_policy.max;
2083 new_policy.policy = policy->user_policy.policy;
2084 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Viresh Kumarbb176f72013-06-19 14:19:33 +05302086 /*
2087 * BIOS might change freq behind our back
2088 * -> ask driver for current freq and notify governors about a change
2089 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002090 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302091 new_policy.cur = cpufreq_driver->get(cpu);
2092 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002093 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302094 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002095 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302096 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302097 cpufreq_out_of_sync(cpu, policy->cur,
2098 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002099 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002100 }
2101
Viresh Kumar037ce832013-10-02 14:13:16 +05302102 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
viresh kumarad7722d2013-10-18 19:10:15 +05302104 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002105
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302106 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002107no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 return ret;
2109}
2110EXPORT_SYMBOL(cpufreq_update_policy);
2111
Paul Gortmaker27609842013-06-19 13:54:04 -04002112static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002113 unsigned long action, void *hcpu)
2114{
2115 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002116 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302117 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002118
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002119 dev = get_cpu_device(cpu);
2120 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302121
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302122 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002123 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302124 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302125 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002126 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302127
Ashok Rajc32b6b82005-10-30 14:59:54 -08002128 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302129 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302130 break;
2131
2132 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302133 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002134 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302135
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002136 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302137 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002138 break;
2139 }
2140 }
2141 return NOTIFY_OK;
2142}
2143
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002144static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302145 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002146};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148/*********************************************************************
2149 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2150 *********************************************************************/
2151
2152/**
2153 * cpufreq_register_driver - register a CPU Frequency driver
2154 * @driver_data: A struct cpufreq_driver containing the values#
2155 * submitted by the CPU Frequency driver.
2156 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302157 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002159 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 *
2161 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002162int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
2164 unsigned long flags;
2165 int ret;
2166
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002167 if (cpufreq_disabled())
2168 return -ENODEV;
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302171 !(driver_data->setpolicy || driver_data->target_index ||
2172 driver_data->target))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return -EINVAL;
2174
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002175 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 if (driver_data->setpolicy)
2178 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2179
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002180 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002181 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002182 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002183 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002185 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002186 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002188 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002189 if (ret)
2190 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002192 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 int i;
2194 ret = -ENODEV;
2195
2196 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002197 for (i = 0; i < nr_cpu_ids; i++)
2198 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002200 break;
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 /* if all ->init() calls failed, unregister */
2204 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002205 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302206 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002207 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209 }
2210
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002211 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002212 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002214 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002215err_if_unreg:
2216 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002217err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002218 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002219 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002220 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002221 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225/**
2226 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2227 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302228 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 * the right to do so, i.e. if you have succeeded in initialising before!
2230 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2231 * currently not initialised.
2232 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002233int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234{
2235 unsigned long flags;
2236
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002237 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002240 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002242 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002243 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Viresh Kumar6eed9402013-08-06 22:53:11 +05302245 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002246 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302247
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002248 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302249
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002250 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302251 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
2253 return 0;
2254}
2255EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002256
2257static int __init cpufreq_core_init(void)
2258{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002259 if (cpufreq_disabled())
2260 return -ENODEV;
2261
Viresh Kumar2361be22013-05-17 16:09:09 +05302262 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002263 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002264 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002265
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002266 return 0;
2267}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002268core_initcall(cpufreq_core_init);