blob: 3509ca04b5bbf547ac3dd86780675a5240d15284 [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>
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +010029#include <linux/syscore_ops.h>
Viresh Kumar5ff0a262013-08-06 22:53:03 +053030#include <linux/tick.h>
Thomas Renninger6f4f2722010-04-20 13:17:36 +020031#include <trace/events/power.h>
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/**
Dave Jonescd878472006-08-11 17:59:28 -040034 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
37 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +020038static struct cpufreq_driver *cpufreq_driver;
Mike Travis7a6aedf2008-03-25 15:06:53 -070039static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +053040static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
Viresh Kumarbb176f72013-06-19 14:19:33 +053041static DEFINE_RWLOCK(cpufreq_driver_lock);
Jane Li6f1e4ef2014-01-03 17:17:41 +080042DEFINE_MUTEX(cpufreq_governor_lock);
Lukasz Majewskic88a1f82013-08-06 22:53:08 +053043static LIST_HEAD(cpufreq_policy_list);
Viresh Kumarbb176f72013-06-19 14:19:33 +053044
Thomas Renninger084f3492007-07-09 11:35:28 -070045#ifdef CONFIG_HOTPLUG_CPU
46/* This one keeps track of the previously set governor of a removed CPU */
Dmitry Monakhove77b89f2009-10-05 00:38:55 +040047static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
Thomas Renninger084f3492007-07-09 11:35:28 -070048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +053050static inline bool has_target(void)
51{
52 return cpufreq_driver->target_index || cpufreq_driver->target;
53}
54
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080055/*
Viresh Kumar6eed9402013-08-06 22:53:11 +053056 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
57 * sections
58 */
59static DECLARE_RWSEM(cpufreq_rwsem);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/* internal prototypes */
Dave Jones29464f22009-01-18 01:37:11 -050062static int __cpufreq_governor(struct cpufreq_policy *policy,
63 unsigned int event);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -080064static unsigned int __cpufreq_get(unsigned int cpu);
David Howells65f27f32006-11-22 14:55:48 +000065static void handle_update(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/**
Dave Jones32ee8c32006-02-28 00:43:23 -050068 * Two notifier lists: the "policy" list is involved in the
69 * validation process for a new CPU frequency policy; the
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * "transition" list for kernel code that needs to handle
71 * changes to devices when the CPU clock speed changes.
72 * The mutex locks both lists.
73 */
Alan Sterne041c682006-03-27 01:16:30 -080074static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
Alan Sternb4dfdbb2006-10-04 02:17:06 -070075static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020077static bool init_cpufreq_transition_notifier_list_called;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070078static int __init init_cpufreq_transition_notifier_list(void)
79{
80 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -020081 init_cpufreq_transition_notifier_list_called = true;
Alan Sternb4dfdbb2006-10-04 02:17:06 -070082 return 0;
83}
Linus Torvaldsb3438f82006-11-20 11:47:18 -080084pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040086static int off __read_mostly;
Viresh Kumarda584452012-10-26 00:51:32 +020087static int cpufreq_disabled(void)
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -040088{
89 return off;
90}
91void disable_cpufreq(void)
92{
93 off = 1;
94}
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static LIST_HEAD(cpufreq_governor_list);
Dave Jones29464f22009-01-18 01:37:11 -050096static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Viresh Kumar4d5dcc42013-03-27 15:58:58 +000098bool have_governor_per_policy(void)
99{
Viresh Kumar0b981e72013-10-02 14:13:18 +0530100 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000101}
Viresh Kumar3f869d62013-05-16 05:09:56 +0000102EXPORT_SYMBOL_GPL(have_governor_per_policy);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +0000103
Viresh Kumar944e9a02013-05-16 05:09:57 +0000104struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
105{
106 if (have_governor_per_policy())
107 return &policy->kobj;
108 else
109 return cpufreq_global_kobject;
110}
111EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
112
Viresh Kumar72a4ce32013-05-17 11:26:32 +0000113static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
114{
115 u64 idle_time;
116 u64 cur_wall_time;
117 u64 busy_time;
118
119 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
120
121 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
122 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
123 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
124 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
125 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
126 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
127
128 idle_time = cur_wall_time - busy_time;
129 if (wall)
130 *wall = cputime_to_usecs(cur_wall_time);
131
132 return cputime_to_usecs(idle_time);
133}
134
135u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
136{
137 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
138
139 if (idle_time == -1ULL)
140 return get_cpu_idle_time_jiffy(cpu, wall);
141 else if (!io_busy)
142 idle_time += get_cpu_iowait_time_us(cpu, wall);
143
144 return idle_time;
145}
146EXPORT_SYMBOL_GPL(get_cpu_idle_time);
147
Viresh Kumar70e9e772013-10-03 20:29:07 +0530148/*
149 * This is a generic cpufreq init() routine which can be used by cpufreq
150 * drivers of SMP systems. It will do following:
151 * - validate & show freq table passed
152 * - set policies transition latency
153 * - policy->cpus with all possible CPUs
154 */
155int cpufreq_generic_init(struct cpufreq_policy *policy,
156 struct cpufreq_frequency_table *table,
157 unsigned int transition_latency)
158{
159 int ret;
160
161 ret = cpufreq_table_validate_and_show(policy, table);
162 if (ret) {
163 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
164 return ret;
165 }
166
167 policy->cpuinfo.transition_latency = transition_latency;
168
169 /*
170 * The driver only supports the SMP configuartion where all processors
171 * share the clock and voltage and clock.
172 */
173 cpumask_setall(policy->cpus);
174
175 return 0;
176}
177EXPORT_SYMBOL_GPL(cpufreq_generic_init);
178
Viresh Kumar6eed9402013-08-06 22:53:11 +0530179struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Viresh Kumar6eed9402013-08-06 22:53:11 +0530181 struct cpufreq_policy *policy = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 unsigned long flags;
183
Viresh Kumar6eed9402013-08-06 22:53:11 +0530184 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
185 return NULL;
186
187 if (!down_read_trylock(&cpufreq_rwsem))
188 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* get the cpufreq driver */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000191 read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Viresh Kumar6eed9402013-08-06 22:53:11 +0530193 if (cpufreq_driver) {
194 /* get the CPU */
195 policy = per_cpu(cpufreq_cpu_data, cpu);
196 if (policy)
197 kobject_get(&policy->kobj);
198 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200199
Viresh Kumar6eed9402013-08-06 22:53:11 +0530200 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530202 if (!policy)
Viresh Kumar6eed9402013-08-06 22:53:11 +0530203 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530205 return policy;
Stephen Boyda9144432012-07-20 18:14:38 +0000206}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
208
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530209void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000211 if (cpufreq_disabled())
212 return;
213
Viresh Kumar6eed9402013-08-06 22:53:11 +0530214 kobject_put(&policy->kobj);
215 up_read(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219/*********************************************************************
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
221 *********************************************************************/
222
223/**
224 * adjust_jiffies - adjust the system "loops_per_jiffy"
225 *
226 * This function alters the system "loops_per_jiffy" for the clock
227 * speed change. Note that loops_per_jiffy cannot be updated on SMP
Dave Jones32ee8c32006-02-28 00:43:23 -0500228 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * per-CPU loops_per_jiffy value wherever possible.
230 */
231#ifndef CONFIG_SMP
232static unsigned long l_p_j_ref;
Viresh Kumarbb176f72013-06-19 14:19:33 +0530233static unsigned int l_p_j_ref_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Arjan van de Ven858119e2006-01-14 13:20:43 -0800235static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 if (ci->flags & CPUFREQ_CONST_LOOPS)
238 return;
239
240 if (!l_p_j_ref_freq) {
241 l_p_j_ref = loops_per_jiffy;
242 l_p_j_ref_freq = ci->old;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200243 pr_debug("saving %lu as reference value for loops_per_jiffy; "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530244 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Viresh Kumarbb176f72013-06-19 14:19:33 +0530246 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -0700247 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530248 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
249 ci->new);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200250 pr_debug("scaling loops_per_jiffy to %lu "
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530251 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253}
254#else
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530255static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
256{
257 return;
258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259#endif
260
Viresh Kumar0956df9c2013-06-19 14:19:34 +0530261static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530262 struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 BUG_ON(irqs_disabled());
265
Dirk Brandewied5aaffa2013-01-17 16:22:21 +0000266 if (cpufreq_disabled())
267 return;
268
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200269 freqs->flags = cpufreq_driver->flags;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200270 pr_debug("notification %u of frequency transition to %u kHz\n",
Dave Jonese4472cb2006-01-31 15:53:55 -0800271 state, freqs->new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 switch (state) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 case CPUFREQ_PRECHANGE:
Dave Jones32ee8c32006-02-28 00:43:23 -0500276 /* detect if the driver reported a value as "old frequency"
Dave Jonese4472cb2006-01-31 15:53:55 -0800277 * which is not equal to what the cpufreq core thinks is
278 * "old frequency".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200280 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Dave Jonese4472cb2006-01-31 15:53:55 -0800281 if ((policy) && (policy->cpu == freqs->cpu) &&
282 (policy->cur) && (policy->cur != freqs->old)) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200283 pr_debug("Warning: CPU frequency is"
Dave Jonese4472cb2006-01-31 15:53:55 -0800284 " %u, cpufreq assumed %u kHz.\n",
285 freqs->old, policy->cur);
286 freqs->old = policy->cur;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288 }
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700289 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800290 CPUFREQ_PRECHANGE, freqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
292 break;
Dave Jonese4472cb2006-01-31 15:53:55 -0800293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 case CPUFREQ_POSTCHANGE:
295 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200296 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200297 (unsigned long)freqs->cpu);
Thomas Renninger25e41932011-01-03 17:50:44 +0100298 trace_cpu_frequency(freqs->new, freqs->cpu);
Alan Sternb4dfdbb2006-10-04 02:17:06 -0700299 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
Alan Sterne041c682006-03-27 01:16:30 -0800300 CPUFREQ_POSTCHANGE, freqs);
Dave Jonese4472cb2006-01-31 15:53:55 -0800301 if (likely(policy) && likely(policy->cpu == freqs->cpu))
302 policy->cur = freqs->new;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 break;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
Viresh Kumarbb176f72013-06-19 14:19:33 +0530306
Viresh Kumarb43a7ff2013-03-24 11:56:43 +0530307/**
308 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
309 * on frequency transition.
310 *
311 * This function calls the transition notifiers and the "adjust_jiffies"
312 * function. It is called twice on all CPU frequency changes that have
313 * external effects.
314 */
315void cpufreq_notify_transition(struct cpufreq_policy *policy,
316 struct cpufreq_freqs *freqs, unsigned int state)
317{
318 for_each_cpu(freqs->cpu, policy->cpus)
319 __cpufreq_notify_transition(policy, freqs, state);
320}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
322
Viresh Kumarf7ba3b42013-12-02 11:04:12 +0530323/* Do post notifications when there are chances that transition has failed */
324void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
325 struct cpufreq_freqs *freqs, int transition_failed)
326{
327 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
328 if (!transition_failed)
329 return;
330
331 swap(freqs->old, freqs->new);
332 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
333 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
334}
335EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition);
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/*********************************************************************
339 * SYSFS INTERFACE *
340 *********************************************************************/
341
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700342static struct cpufreq_governor *__find_governor(const char *str_governor)
343{
344 struct cpufreq_governor *t;
345
346 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
Dave Jones29464f22009-01-18 01:37:11 -0500347 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700348 return t;
349
350 return NULL;
351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353/**
354 * cpufreq_parse_governor - parse a governor string
355 */
Dave Jones905d77c2008-03-05 14:28:32 -0500356static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 struct cpufreq_governor **governor)
358{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700359 int err = -EINVAL;
360
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200361 if (!cpufreq_driver)
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700362 goto out;
363
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200364 if (cpufreq_driver->setpolicy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
366 *policy = CPUFREQ_POLICY_PERFORMANCE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700367 err = 0;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530368 } else if (!strnicmp(str_governor, "powersave",
369 CPUFREQ_NAME_LEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 *policy = CPUFREQ_POLICY_POWERSAVE;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700371 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530373 } else if (has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct cpufreq_governor *t;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700375
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800376 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700377
378 t = __find_governor(str_governor);
379
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700380 if (t == NULL) {
Kees Cook1a8e1462011-05-04 08:38:56 -0700381 int ret;
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700382
Kees Cook1a8e1462011-05-04 08:38:56 -0700383 mutex_unlock(&cpufreq_governor_mutex);
384 ret = request_module("cpufreq_%s", str_governor);
385 mutex_lock(&cpufreq_governor_mutex);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700386
Kees Cook1a8e1462011-05-04 08:38:56 -0700387 if (ret == 0)
388 t = __find_governor(str_governor);
Jeremy Fitzhardingeea714972006-07-06 12:32:01 -0700389 }
390
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700391 if (t != NULL) {
392 *governor = t;
393 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700395
akpm@osdl.org3fc54d32006-01-13 15:54:22 -0800396 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Dave Jones29464f22009-01-18 01:37:11 -0500398out:
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -0700399 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402/**
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530403 * cpufreq_per_cpu_attr_read() / show_##file_name() -
404 * print out cpufreq information
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 *
406 * Write out information from cpufreq_driver->policy[cpu]; object must be
407 * "unsigned int".
408 */
409
Dave Jones32ee8c32006-02-28 00:43:23 -0500410#define show_one(file_name, object) \
411static ssize_t show_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500412(struct cpufreq_policy *policy, char *buf) \
Dave Jones32ee8c32006-02-28 00:43:23 -0500413{ \
Dave Jones29464f22009-01-18 01:37:11 -0500414 return sprintf(buf, "%u\n", policy->object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417show_one(cpuinfo_min_freq, cpuinfo.min_freq);
418show_one(cpuinfo_max_freq, cpuinfo.max_freq);
Thomas Renningered129782009-02-04 01:17:41 +0100419show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420show_one(scaling_min_freq, min);
421show_one(scaling_max_freq, max);
422show_one(scaling_cur_freq, cur);
423
Viresh Kumar037ce832013-10-02 14:13:16 +0530424static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +0530425 struct cpufreq_policy *new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/**
428 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
429 */
430#define store_one(file_name, object) \
431static ssize_t store_##file_name \
Dave Jones905d77c2008-03-05 14:28:32 -0500432(struct cpufreq_policy *policy, const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{ \
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530434 int ret; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 struct cpufreq_policy new_policy; \
436 \
437 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
438 if (ret) \
439 return -EINVAL; \
440 \
Dave Jones29464f22009-01-18 01:37:11 -0500441 ret = sscanf(buf, "%u", &new_policy.object); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 if (ret != 1) \
443 return -EINVAL; \
444 \
Viresh Kumar037ce832013-10-02 14:13:16 +0530445 ret = cpufreq_set_policy(policy, &new_policy); \
Thomas Renninger7970e082006-04-13 15:14:04 +0200446 policy->user_policy.object = policy->object; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 \
448 return ret ? ret : count; \
449}
450
Dave Jones29464f22009-01-18 01:37:11 -0500451store_one(scaling_min_freq, min);
452store_one(scaling_max_freq, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454/**
455 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
456 */
Dave Jones905d77c2008-03-05 14:28:32 -0500457static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
458 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800460 unsigned int cur_freq = __cpufreq_get(policy->cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (!cur_freq)
462 return sprintf(buf, "<unknown>");
463 return sprintf(buf, "%u\n", cur_freq);
464}
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466/**
467 * show_scaling_governor - show the current policy for the specified CPU
468 */
Dave Jones905d77c2008-03-05 14:28:32 -0500469static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Dave Jones29464f22009-01-18 01:37:11 -0500471 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return sprintf(buf, "powersave\n");
473 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
474 return sprintf(buf, "performance\n");
475 else if (policy->governor)
viresh kumar4b972f02012-10-23 01:23:43 +0200476 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
Dave Jones29464f22009-01-18 01:37:11 -0500477 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -EINVAL;
479}
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/**
482 * store_scaling_governor - store policy for the specified CPU
483 */
Dave Jones905d77c2008-03-05 14:28:32 -0500484static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
485 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Srivatsa S. Bhat5136fa52013-09-07 01:24:06 +0530487 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 char str_governor[16];
489 struct cpufreq_policy new_policy;
490
491 ret = cpufreq_get_policy(&new_policy, policy->cpu);
492 if (ret)
493 return ret;
494
Dave Jones29464f22009-01-18 01:37:11 -0500495 ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if (ret != 1)
497 return -EINVAL;
498
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530499 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
500 &new_policy.governor))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return -EINVAL;
502
Viresh Kumar037ce832013-10-02 14:13:16 +0530503 ret = cpufreq_set_policy(policy, &new_policy);
Thomas Renninger7970e082006-04-13 15:14:04 +0200504
505 policy->user_policy.policy = policy->policy;
506 policy->user_policy.governor = policy->governor;
Thomas Renninger7970e082006-04-13 15:14:04 +0200507
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530508 if (ret)
509 return ret;
510 else
511 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
514/**
515 * show_scaling_driver - show the cpufreq driver currently loaded
516 */
Dave Jones905d77c2008-03-05 14:28:32 -0500517static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200519 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522/**
523 * show_scaling_available_governors - show the available CPUfreq governors
524 */
Dave Jones905d77c2008-03-05 14:28:32 -0500525static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
526 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
528 ssize_t i = 0;
529 struct cpufreq_governor *t;
530
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530531 if (!has_target()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 i += sprintf(buf, "performance powersave");
533 goto out;
534 }
535
536 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
Dave Jones29464f22009-01-18 01:37:11 -0500537 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
538 - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 goto out;
viresh kumar4b972f02012-10-23 01:23:43 +0200540 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Dave Jones7d5e3502006-02-02 17:03:42 -0500542out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 i += sprintf(&buf[i], "\n");
544 return i;
545}
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700546
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800547ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 ssize_t i = 0;
550 unsigned int cpu;
551
Rusty Russell835481d2009-01-04 05:18:06 -0800552 for_each_cpu(cpu, mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (i)
554 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
555 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
556 if (i >= (PAGE_SIZE - 5))
Dave Jones29464f22009-01-18 01:37:11 -0500557 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
559 i += sprintf(&buf[i], "\n");
560 return i;
561}
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800562EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700564/**
565 * show_related_cpus - show the CPUs affected by each transition even if
566 * hw coordination is in use
567 */
568static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
569{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800570 return cpufreq_show_cpus(policy->related_cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700571}
572
573/**
574 * show_affected_cpus - show the CPUs affected by each transition
575 */
576static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
577{
Lan Tianyuf4fd3792013-06-27 15:08:54 +0800578 return cpufreq_show_cpus(policy->cpus, buf);
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700579}
580
Venki Pallipadi9e769882007-10-26 10:18:21 -0700581static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones905d77c2008-03-05 14:28:32 -0500582 const char *buf, size_t count)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700583{
584 unsigned int freq = 0;
585 unsigned int ret;
586
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700587 if (!policy->governor || !policy->governor->store_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700588 return -EINVAL;
589
590 ret = sscanf(buf, "%u", &freq);
591 if (ret != 1)
592 return -EINVAL;
593
594 policy->governor->store_setspeed(policy, freq);
595
596 return count;
597}
598
599static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
600{
CHIKAMA masaki879000f2008-06-05 22:46:33 -0700601 if (!policy->governor || !policy->governor->show_setspeed)
Venki Pallipadi9e769882007-10-26 10:18:21 -0700602 return sprintf(buf, "<unsupported>\n");
603
604 return policy->governor->show_setspeed(policy, buf);
605}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Thomas Renningere2f74f32009-11-19 12:31:01 +0100607/**
viresh kumar8bf1ac722012-10-23 01:23:33 +0200608 * show_bios_limit - show the current cpufreq HW/BIOS limitation
Thomas Renningere2f74f32009-11-19 12:31:01 +0100609 */
610static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
611{
612 unsigned int limit;
613 int ret;
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200614 if (cpufreq_driver->bios_limit) {
615 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
Thomas Renningere2f74f32009-11-19 12:31:01 +0100616 if (!ret)
617 return sprintf(buf, "%u\n", limit);
618 }
619 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
620}
621
Borislav Petkov6dad2a22010-03-31 21:56:46 +0200622cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
623cpufreq_freq_attr_ro(cpuinfo_min_freq);
624cpufreq_freq_attr_ro(cpuinfo_max_freq);
625cpufreq_freq_attr_ro(cpuinfo_transition_latency);
626cpufreq_freq_attr_ro(scaling_available_governors);
627cpufreq_freq_attr_ro(scaling_driver);
628cpufreq_freq_attr_ro(scaling_cur_freq);
629cpufreq_freq_attr_ro(bios_limit);
630cpufreq_freq_attr_ro(related_cpus);
631cpufreq_freq_attr_ro(affected_cpus);
632cpufreq_freq_attr_rw(scaling_min_freq);
633cpufreq_freq_attr_rw(scaling_max_freq);
634cpufreq_freq_attr_rw(scaling_governor);
635cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Dave Jones905d77c2008-03-05 14:28:32 -0500637static struct attribute *default_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 &cpuinfo_min_freq.attr,
639 &cpuinfo_max_freq.attr,
Thomas Renningered129782009-02-04 01:17:41 +0100640 &cpuinfo_transition_latency.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 &scaling_min_freq.attr,
642 &scaling_max_freq.attr,
643 &affected_cpus.attr,
Darrick J. Wonge8628dd2008-04-18 13:31:12 -0700644 &related_cpus.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 &scaling_governor.attr,
646 &scaling_driver.attr,
647 &scaling_available_governors.attr,
Venki Pallipadi9e769882007-10-26 10:18:21 -0700648 &scaling_setspeed.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 NULL
650};
651
Dave Jones29464f22009-01-18 01:37:11 -0500652#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
653#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Dave Jones29464f22009-01-18 01:37:11 -0500655static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Dave Jones905d77c2008-03-05 14:28:32 -0500657 struct cpufreq_policy *policy = to_policy(kobj);
658 struct freq_attr *fattr = to_attr(attr);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530659 ssize_t ret;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530660
661 if (!down_read_trylock(&cpufreq_rwsem))
Viresh Kumar1b750e32013-10-02 14:13:09 +0530662 return -EINVAL;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800663
viresh kumarad7722d2013-10-18 19:10:15 +0530664 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800665
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530666 if (fattr->show)
667 ret = fattr->show(policy, buf);
668 else
669 ret = -EIO;
670
viresh kumarad7722d2013-10-18 19:10:15 +0530671 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530672 up_read(&cpufreq_rwsem);
Viresh Kumar1b750e32013-10-02 14:13:09 +0530673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 return ret;
675}
676
Dave Jones905d77c2008-03-05 14:28:32 -0500677static ssize_t store(struct kobject *kobj, struct attribute *attr,
678 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Dave Jones905d77c2008-03-05 14:28:32 -0500680 struct cpufreq_policy *policy = to_policy(kobj);
681 struct freq_attr *fattr = to_attr(attr);
Dave Jonesa07530b2008-03-05 14:22:25 -0500682 ssize_t ret = -EINVAL;
Viresh Kumar6eed9402013-08-06 22:53:11 +0530683
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530684 get_online_cpus();
685
686 if (!cpu_online(policy->cpu))
687 goto unlock;
688
Viresh Kumar6eed9402013-08-06 22:53:11 +0530689 if (!down_read_trylock(&cpufreq_rwsem))
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530690 goto unlock;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800691
viresh kumarad7722d2013-10-18 19:10:15 +0530692 down_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -0800693
Gautham R Shenoye08f5f52006-10-26 16:20:58 +0530694 if (fattr->store)
695 ret = fattr->store(policy, buf, count);
696 else
697 ret = -EIO;
698
viresh kumarad7722d2013-10-18 19:10:15 +0530699 up_write(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +0530700
Viresh Kumar6eed9402013-08-06 22:53:11 +0530701 up_read(&cpufreq_rwsem);
Srivatsa S. Bhat4f750c92013-09-07 01:23:43 +0530702unlock:
703 put_online_cpus();
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return ret;
706}
707
Dave Jones905d77c2008-03-05 14:28:32 -0500708static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709{
Dave Jones905d77c2008-03-05 14:28:32 -0500710 struct cpufreq_policy *policy = to_policy(kobj);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200711 pr_debug("last reference is dropped\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 complete(&policy->kobj_unregister);
713}
714
Emese Revfy52cf25d2010-01-19 02:58:23 +0100715static const struct sysfs_ops sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 .show = show,
717 .store = store,
718};
719
720static struct kobj_type ktype_cpufreq = {
721 .sysfs_ops = &sysfs_ops,
722 .default_attrs = default_attrs,
723 .release = cpufreq_sysfs_release,
724};
725
Viresh Kumar2361be22013-05-17 16:09:09 +0530726struct kobject *cpufreq_global_kobject;
727EXPORT_SYMBOL(cpufreq_global_kobject);
728
729static int cpufreq_global_kobject_usage;
730
731int cpufreq_get_global_kobject(void)
732{
733 if (!cpufreq_global_kobject_usage++)
734 return kobject_add(cpufreq_global_kobject,
735 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
736
737 return 0;
738}
739EXPORT_SYMBOL(cpufreq_get_global_kobject);
740
741void cpufreq_put_global_kobject(void)
742{
743 if (!--cpufreq_global_kobject_usage)
744 kobject_del(cpufreq_global_kobject);
745}
746EXPORT_SYMBOL(cpufreq_put_global_kobject);
747
748int cpufreq_sysfs_create_file(const struct attribute *attr)
749{
750 int ret = cpufreq_get_global_kobject();
751
752 if (!ret) {
753 ret = sysfs_create_file(cpufreq_global_kobject, attr);
754 if (ret)
755 cpufreq_put_global_kobject();
756 }
757
758 return ret;
759}
760EXPORT_SYMBOL(cpufreq_sysfs_create_file);
761
762void cpufreq_sysfs_remove_file(const struct attribute *attr)
763{
764 sysfs_remove_file(cpufreq_global_kobject, attr);
765 cpufreq_put_global_kobject();
766}
767EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
768
Dave Jones19d6f7e2009-07-08 17:35:39 -0400769/* symlink affected CPUs */
Viresh Kumar308b60e2013-07-31 14:35:14 +0200770static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400771{
772 unsigned int j;
773 int ret = 0;
774
775 for_each_cpu(j, policy->cpus) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800776 struct device *cpu_dev;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400777
Viresh Kumar308b60e2013-07-31 14:35:14 +0200778 if (j == policy->cpu)
Dave Jones19d6f7e2009-07-08 17:35:39 -0400779 continue;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400780
Viresh Kumare8fdde12013-07-31 14:31:33 +0200781 pr_debug("Adding link for CPU: %u\n", j);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800782 cpu_dev = get_cpu_device(j);
783 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Dave Jones19d6f7e2009-07-08 17:35:39 -0400784 "cpufreq");
Rafael J. Wysocki71c34612013-08-04 01:19:34 +0200785 if (ret)
786 break;
Dave Jones19d6f7e2009-07-08 17:35:39 -0400787 }
788 return ret;
789}
790
Viresh Kumar308b60e2013-07-31 14:35:14 +0200791static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800792 struct device *dev)
Dave Jones909a6942009-07-08 18:05:42 -0400793{
794 struct freq_attr **drv_attr;
Dave Jones909a6942009-07-08 18:05:42 -0400795 int ret = 0;
Dave Jones909a6942009-07-08 18:05:42 -0400796
797 /* prepare interface data */
798 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800799 &dev->kobj, "cpufreq");
Dave Jones909a6942009-07-08 18:05:42 -0400800 if (ret)
801 return ret;
802
803 /* set up files for this cpu device */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200804 drv_attr = cpufreq_driver->attr;
Dave Jones909a6942009-07-08 18:05:42 -0400805 while ((drv_attr) && (*drv_attr)) {
806 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
807 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200808 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400809 drv_attr++;
810 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200811 if (cpufreq_driver->get) {
Dave Jones909a6942009-07-08 18:05:42 -0400812 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
813 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200814 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400815 }
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530816 if (has_target()) {
Dave Jones909a6942009-07-08 18:05:42 -0400817 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
818 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200819 goto err_out_kobj_put;
Dave Jones909a6942009-07-08 18:05:42 -0400820 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200821 if (cpufreq_driver->bios_limit) {
Thomas Renningere2f74f32009-11-19 12:31:01 +0100822 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
823 if (ret)
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200824 goto err_out_kobj_put;
Thomas Renningere2f74f32009-11-19 12:31:01 +0100825 }
Dave Jones909a6942009-07-08 18:05:42 -0400826
Viresh Kumar308b60e2013-07-31 14:35:14 +0200827 ret = cpufreq_add_dev_symlink(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400828 if (ret)
829 goto err_out_kobj_put;
830
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +0530831 return ret;
832
833err_out_kobj_put:
834 kobject_put(&policy->kobj);
835 wait_for_completion(&policy->kobj_unregister);
836 return ret;
837}
838
839static void cpufreq_init_policy(struct cpufreq_policy *policy)
840{
841 struct cpufreq_policy new_policy;
842 int ret = 0;
843
Viresh Kumard5b73cd2013-08-06 22:53:06 +0530844 memcpy(&new_policy, policy, sizeof(*policy));
Jason Barona27a9ab2013-12-19 22:50:50 +0000845
846 /* Use the default policy if its valid. */
847 if (cpufreq_driver->setpolicy)
848 cpufreq_parse_governor(policy->governor->name,
849 &new_policy.policy, NULL);
850
Viresh Kumar037ce832013-10-02 14:13:16 +0530851 /* assure that the starting sequence is run in cpufreq_set_policy */
Dave Jonesecf7e462009-07-08 18:48:47 -0400852 policy->governor = NULL;
853
854 /* set default policy */
Viresh Kumar037ce832013-10-02 14:13:16 +0530855 ret = cpufreq_set_policy(policy, &new_policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400856 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +0200857 pr_debug("setting policy failed\n");
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +0200858 if (cpufreq_driver->exit)
859 cpufreq_driver->exit(policy);
Dave Jonesecf7e462009-07-08 18:48:47 -0400860 }
Dave Jones909a6942009-07-08 18:05:42 -0400861}
862
Viresh Kumarfcf80582013-01-29 14:39:08 +0000863#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumard8d3b472013-08-04 01:20:07 +0200864static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +0530865 unsigned int cpu, struct device *dev)
Viresh Kumarfcf80582013-01-29 14:39:08 +0000866{
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530867 int ret = 0;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000868 unsigned long flags;
869
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530870 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530871 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
872 if (ret) {
873 pr_err("%s: Failed to stop governor\n", __func__);
874 return ret;
875 }
876 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000877
viresh kumarad7722d2013-10-18 19:10:15 +0530878 down_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530879
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000880 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530881
Viresh Kumarfcf80582013-01-29 14:39:08 +0000882 cpumask_set_cpu(cpu, policy->cpus);
883 per_cpu(cpufreq_cpu_data, cpu) = policy;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +0000884 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +0000885
viresh kumarad7722d2013-10-18 19:10:15 +0530886 up_write(&policy->rwsem);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +0530887
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +0530888 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +0530889 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
890 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
891 pr_err("%s: Failed to start governor\n", __func__);
892 return ret;
893 }
Viresh Kumar820c6ca2013-04-22 00:48:03 +0200894 }
Viresh Kumarfcf80582013-01-29 14:39:08 +0000895
Viresh Kumar42f921a2013-12-20 21:26:02 +0530896 return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
Viresh Kumarfcf80582013-01-29 14:39:08 +0000897}
898#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530900static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
901{
902 struct cpufreq_policy *policy;
903 unsigned long flags;
904
Lan Tianyu44871c92013-09-11 15:05:05 +0800905 read_lock_irqsave(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530906
907 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
908
Lan Tianyu44871c92013-09-11 15:05:05 +0800909 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +0530910
911 return policy;
912}
913
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530914static struct cpufreq_policy *cpufreq_policy_alloc(void)
915{
916 struct cpufreq_policy *policy;
917
918 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
919 if (!policy)
920 return NULL;
921
922 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
923 goto err_free_policy;
924
925 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
926 goto err_free_cpumask;
927
Lukasz Majewskic88a1f82013-08-06 22:53:08 +0530928 INIT_LIST_HEAD(&policy->policy_list);
viresh kumarad7722d2013-10-18 19:10:15 +0530929 init_rwsem(&policy->rwsem);
930
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530931 return policy;
932
933err_free_cpumask:
934 free_cpumask_var(policy->cpus);
935err_free_policy:
936 kfree(policy);
937
938 return NULL;
939}
940
Viresh Kumar42f921a2013-12-20 21:26:02 +0530941static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
942{
943 struct kobject *kobj;
944 struct completion *cmp;
945
946 down_read(&policy->rwsem);
947 kobj = &policy->kobj;
948 cmp = &policy->kobj_unregister;
949 up_read(&policy->rwsem);
950 kobject_put(kobj);
951
952 /*
953 * We need to make sure that the underlying kobj is
954 * actually not referenced anymore by anybody before we
955 * proceed with unloading.
956 */
957 pr_debug("waiting for dropping of refcount\n");
958 wait_for_completion(cmp);
959 pr_debug("wait complete\n");
960}
961
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +0530962static void cpufreq_policy_free(struct cpufreq_policy *policy)
963{
964 free_cpumask_var(policy->related_cpus);
965 free_cpumask_var(policy->cpus);
966 kfree(policy);
967}
968
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530969static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
970{
Srivatsa S. Bhat99ec8992013-09-12 17:29:09 +0530971 if (WARN_ON(cpu == policy->cpu))
Srivatsa S. Bhatcb38ed52013-09-12 01:43:42 +0530972 return;
973
viresh kumarad7722d2013-10-18 19:10:15 +0530974 down_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +0530975
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530976 policy->last_cpu = policy->cpu;
977 policy->cpu = cpu;
978
viresh kumarad7722d2013-10-18 19:10:15 +0530979 up_write(&policy->rwsem);
Viresh Kumar8efd5762013-09-17 10:22:11 +0530980
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530981 cpufreq_frequency_table_update_policy_cpu(policy);
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +0530982 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
983 CPUFREQ_UPDATE_POLICY_CPU, policy);
984}
985
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +0530986static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
987 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Viresh Kumarfcf80582013-01-29 14:39:08 +0000989 unsigned int j, cpu = dev->id;
Viresh Kumar65922462013-02-07 10:56:03 +0530990 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 unsigned long flags;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500993#ifdef CONFIG_HOTPLUG_CPU
Viresh Kumar1b274292013-08-20 12:08:26 +0530994 struct cpufreq_policy *tpolicy;
Viresh Kumarfcf80582013-01-29 14:39:08 +0000995 struct cpufreq_governor *gov;
Prarit Bhargava90e41ba2009-11-12 09:18:46 -0500996#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Ashok Rajc32b6b82005-10-30 14:59:54 -0800998 if (cpu_is_offline(cpu))
999 return 0;
1000
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001001 pr_debug("adding CPU %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003#ifdef CONFIG_SMP
1004 /* check whether a different CPU already registered this
1005 * CPU because it is in the same boat. */
1006 policy = cpufreq_cpu_get(cpu);
1007 if (unlikely(policy)) {
Dave Jones8ff69732006-03-05 03:37:23 -05001008 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return 0;
1010 }
Li Zhong5025d622013-08-21 01:31:08 +02001011#endif
Viresh Kumarfcf80582013-01-29 14:39:08 +00001012
Viresh Kumar6eed9402013-08-06 22:53:11 +05301013 if (!down_read_trylock(&cpufreq_rwsem))
1014 return 0;
1015
Viresh Kumarfcf80582013-01-29 14:39:08 +00001016#ifdef CONFIG_HOTPLUG_CPU
1017 /* Check if this cpu was hot-unplugged earlier and has siblings */
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001018 read_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar1b274292013-08-20 12:08:26 +05301019 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1020 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001021 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301022 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301023 up_read(&cpufreq_rwsem);
1024 return ret;
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301025 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001026 }
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001027 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumarfcf80582013-01-29 14:39:08 +00001028#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001030 /*
1031 * Restore the saved policy when doing light-weight init and fall back
1032 * to the full init if that fails.
1033 */
1034 policy = frozen ? cpufreq_policy_restore(cpu) : NULL;
1035 if (!policy) {
1036 frozen = false;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301037 policy = cpufreq_policy_alloc();
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001038 if (!policy)
1039 goto nomem_out;
1040 }
Srivatsa S. Bhat0d66b912013-09-12 01:42:59 +05301041
1042 /*
1043 * In the resume path, since we restore a saved policy, the assignment
1044 * to policy->cpu is like an update of the existing policy, rather than
1045 * the creation of a brand new one. So we need to perform this update
1046 * by invoking update_policy_cpu().
1047 */
1048 if (frozen && cpu != policy->cpu)
1049 update_policy_cpu(policy, cpu);
1050 else
1051 policy->cpu = cpu;
1052
Viresh Kumar65922462013-02-07 10:56:03 +05301053 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
Rusty Russell835481d2009-01-04 05:18:06 -08001054 cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 init_completion(&policy->kobj_unregister);
David Howells65f27f32006-11-22 14:55:48 +00001057 INIT_WORK(&policy->update, handle_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 /* call driver. From then on the cpufreq must be able
1060 * to accept all calls to ->verify and ->setpolicy for this CPU
1061 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001062 ret = cpufreq_driver->init(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001064 pr_debug("initialization failed\n");
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301065 goto err_set_policy_cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001067
Viresh Kumarda60ce92013-10-03 20:28:30 +05301068 if (cpufreq_driver->get) {
1069 policy->cur = cpufreq_driver->get(policy->cpu);
1070 if (!policy->cur) {
1071 pr_err("%s: ->get() failed\n", __func__);
1072 goto err_get_freq;
1073 }
1074 }
1075
Viresh Kumard3916692013-12-03 11:20:46 +05301076 /*
1077 * Sometimes boot loaders set CPU frequency to a value outside of
1078 * frequency table present with cpufreq core. In such cases CPU might be
1079 * unstable if it has to run on that frequency for long duration of time
1080 * and so its better to set it to a frequency which is specified in
1081 * freq-table. This also makes cpufreq stats inconsistent as
1082 * cpufreq-stats would fail to register because current frequency of CPU
1083 * isn't found in freq-table.
1084 *
1085 * Because we don't want this change to effect boot process badly, we go
1086 * for the next freq which is >= policy->cur ('cur' must be set by now,
1087 * otherwise we will end up setting freq to lowest of the table as 'cur'
1088 * is initialized to zero).
1089 *
1090 * We are passing target-freq as "policy->cur - 1" otherwise
1091 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1092 * equal to target-freq.
1093 */
1094 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1095 && has_target()) {
1096 /* Are we running at unknown frequency ? */
1097 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1098 if (ret == -EINVAL) {
1099 /* Warn user and fix it */
1100 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1101 __func__, policy->cpu, policy->cur);
1102 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1103 CPUFREQ_RELATION_L);
1104
1105 /*
1106 * Reaching here after boot in a few seconds may not
1107 * mean that system will remain stable at "unknown"
1108 * frequency for longer duration. Hence, a BUG_ON().
1109 */
1110 BUG_ON(ret);
1111 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1112 __func__, policy->cpu, policy->cur);
1113 }
1114 }
1115
Viresh Kumarfcf80582013-01-29 14:39:08 +00001116 /* related cpus should atleast have policy->cpus */
1117 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1118
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001119 /*
1120 * affected cpus must always be the one, which are online. We aren't
1121 * managing offline cpus here.
1122 */
1123 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1124
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301125 if (!frozen) {
1126 policy->user_policy.min = policy->min;
1127 policy->user_policy.max = policy->max;
1128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Thomas Renningera1531ac2008-07-29 22:32:58 -07001130 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1131 CPUFREQ_START, policy);
1132
Viresh Kumarfcf80582013-01-29 14:39:08 +00001133#ifdef CONFIG_HOTPLUG_CPU
1134 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1135 if (gov) {
1136 policy->governor = gov;
1137 pr_debug("Restoring governor %s for cpu %d\n",
1138 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001139 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001140#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301142 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301143 for_each_cpu(j, policy->cpus)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301144 per_cpu(cpufreq_cpu_data, j) = policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301145 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1146
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301147 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001148 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301149 if (ret)
1150 goto err_out_unregister;
1151 }
Dave Jones8ff69732006-03-05 03:37:23 -05001152
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301153 write_lock_irqsave(&cpufreq_driver_lock, flags);
1154 list_add(&policy->policy_list, &cpufreq_policy_list);
1155 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1156
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301157 cpufreq_init_policy(policy);
1158
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301159 if (!frozen) {
1160 policy->user_policy.policy = policy->policy;
1161 policy->user_policy.governor = policy->governor;
1162 }
1163
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001164 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301165 up_read(&cpufreq_rwsem);
1166
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001167 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return 0;
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001172 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301173 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001174 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001175 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Viresh Kumarda60ce92013-10-03 20:28:30 +05301177err_get_freq:
1178 if (cpufreq_driver->exit)
1179 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301180err_set_policy_cpu:
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001181 if (frozen) {
1182 /* Do not leave stale fallback data behind. */
1183 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301184 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001185 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301186 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301189 up_read(&cpufreq_rwsem);
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 return ret;
1192}
1193
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301194/**
1195 * cpufreq_add_dev - add a CPU device
1196 *
1197 * Adds the cpufreq interface for a CPU device.
1198 *
1199 * The Oracle says: try running cpufreq registration/unregistration concurrently
1200 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1201 * mess up, but more thorough testing is needed. - Mathieu
1202 */
1203static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1204{
1205 return __cpufreq_add_dev(dev, sif, false);
1206}
1207
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301208static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +05301209 unsigned int old_cpu)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301210{
1211 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301212 int ret;
1213
1214 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301215 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301216
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301217 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301218 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301219 if (ret) {
1220 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1221
viresh kumarad7722d2013-10-18 19:10:15 +05301222 down_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301223 cpumask_set_cpu(old_cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301224 up_write(&policy->rwsem);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301225
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301226 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301227 "cpufreq");
1228
1229 return -EINVAL;
1230 }
1231
1232 return cpu_dev->id;
1233}
1234
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301235static int __cpufreq_remove_dev_prepare(struct device *dev,
1236 struct subsys_interface *sif,
1237 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301239 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301240 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301242 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001244 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001246 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301248 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301250 /* Save the policy somewhere when doing a light-weight tear-down */
1251 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301252 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301253
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001254 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301256 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001257 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301261 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301262 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1263 if (ret) {
1264 pr_err("%s: Failed to stop governor\n", __func__);
1265 return ret;
1266 }
1267 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001268
Jacob Shin27ecddc2011-04-27 13:32:11 -05001269#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001270 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001271 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301272 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001273#endif
1274
viresh kumarad7722d2013-10-18 19:10:15 +05301275 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301276 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301277 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301279 if (cpu != policy->cpu) {
1280 if (!frozen)
1281 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001282 } else if (cpus > 1) {
Viresh Kumar42f921a2013-12-20 21:26:02 +05301283 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301284 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301285 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301286
1287 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301288 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1289 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301290 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001291 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001292 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001293
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301294 return 0;
1295}
1296
1297static int __cpufreq_remove_dev_finish(struct device *dev,
1298 struct subsys_interface *sif,
1299 bool frozen)
1300{
1301 unsigned int cpu = dev->id, cpus;
1302 int ret;
1303 unsigned long flags;
1304 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301305
1306 read_lock_irqsave(&cpufreq_driver_lock, flags);
1307 policy = per_cpu(cpufreq_cpu_data, cpu);
1308 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1309
1310 if (!policy) {
1311 pr_debug("%s: No cpu_data found\n", __func__);
1312 return -EINVAL;
1313 }
1314
viresh kumarad7722d2013-10-18 19:10:15 +05301315 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301316 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301317
1318 if (cpus > 1)
1319 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301320 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301321
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001322 /* If cpu is last user of policy, free policy */
1323 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301324 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301325 ret = __cpufreq_governor(policy,
1326 CPUFREQ_GOV_POLICY_EXIT);
1327 if (ret) {
1328 pr_err("%s: Failed to exit governor\n",
1329 __func__);
1330 return ret;
1331 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301332 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001333
Viresh Kumar42f921a2013-12-20 21:26:02 +05301334 if (!frozen)
1335 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301336
1337 /*
1338 * Perform the ->exit() even during light-weight tear-down,
1339 * since this is a core component, and is essential for the
1340 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001341 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001342 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301343 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001344
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301345 /* Remove policy from list of active policies */
1346 write_lock_irqsave(&cpufreq_driver_lock, flags);
1347 list_del(&policy->policy_list);
1348 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1349
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301350 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301351 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001352 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301353 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301354 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1355 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1356 pr_err("%s: Failed to start governor\n",
1357 __func__);
1358 return ret;
1359 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001360 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Viresh Kumar474deff2013-08-20 12:08:25 +05301363 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 return 0;
1365}
1366
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301367/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301368 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301369 *
1370 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301371 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001372static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001373{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001374 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301375 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001376
1377 if (cpu_is_offline(cpu))
1378 return 0;
1379
Viresh Kumar27a862e2013-10-02 14:13:14 +05301380 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1381
1382 if (!ret)
1383 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1384
1385 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001386}
1387
David Howells65f27f32006-11-22 14:55:48 +00001388static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
David Howells65f27f32006-11-22 14:55:48 +00001390 struct cpufreq_policy *policy =
1391 container_of(work, struct cpufreq_policy, update);
1392 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001393 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 cpufreq_update_policy(cpu);
1395}
1396
1397/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301398 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1399 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 * @cpu: cpu number
1401 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1402 * @new_freq: CPU frequency the CPU actually runs at
1403 *
Dave Jones29464f22009-01-18 01:37:11 -05001404 * We adjust to current frequency first, and need to clean up later.
1405 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301407static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1408 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301410 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301412 unsigned long flags;
1413
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001414 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 freqs.old = old_freq;
1418 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301419
1420 read_lock_irqsave(&cpufreq_driver_lock, flags);
1421 policy = per_cpu(cpufreq_cpu_data, cpu);
1422 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1423
1424 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1425 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426}
1427
Dave Jones32ee8c32006-02-28 00:43:23 -05001428/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301429 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001430 * @cpu: CPU number
1431 *
1432 * This is the last known freq, without actually getting it from the driver.
1433 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1434 */
1435unsigned int cpufreq_quick_get(unsigned int cpu)
1436{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001437 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301438 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001439
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001440 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1441 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001442
1443 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001444 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301445 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001446 cpufreq_cpu_put(policy);
1447 }
1448
Dave Jones4d34a672008-02-07 16:33:49 -05001449 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001450}
1451EXPORT_SYMBOL(cpufreq_quick_get);
1452
Jesse Barnes3d737102011-06-28 10:59:12 -07001453/**
1454 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1455 * @cpu: CPU number
1456 *
1457 * Just return the max possible frequency for a given CPU.
1458 */
1459unsigned int cpufreq_quick_get_max(unsigned int cpu)
1460{
1461 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1462 unsigned int ret_freq = 0;
1463
1464 if (policy) {
1465 ret_freq = policy->max;
1466 cpufreq_cpu_put(policy);
1467 }
1468
1469 return ret_freq;
1470}
1471EXPORT_SYMBOL(cpufreq_quick_get_max);
1472
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001473static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001475 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301476 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001478 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001479 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001481 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301483 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001484 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301485 /* verify no discrepancy between actual and
1486 saved value exists */
1487 if (unlikely(ret_freq != policy->cur)) {
1488 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 schedule_work(&policy->update);
1490 }
1491 }
1492
Dave Jones4d34a672008-02-07 16:33:49 -05001493 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001494}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001496/**
1497 * cpufreq_get - get the current CPU frequency (in kHz)
1498 * @cpu: CPU number
1499 *
1500 * Get the CPU current (static) CPU frequency
1501 */
1502unsigned int cpufreq_get(unsigned int cpu)
1503{
viresh kumarad7722d2013-10-18 19:10:15 +05301504 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001505 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001506
Viresh Kumar26ca8692013-09-20 22:37:31 +05301507 if (cpufreq_disabled() || !cpufreq_driver)
1508 return -ENOENT;
1509
viresh kumarad7722d2013-10-18 19:10:15 +05301510 BUG_ON(!policy);
1511
Viresh Kumar6eed9402013-08-06 22:53:11 +05301512 if (!down_read_trylock(&cpufreq_rwsem))
1513 return 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001514
viresh kumarad7722d2013-10-18 19:10:15 +05301515 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001516
1517 ret_freq = __cpufreq_get(cpu);
1518
viresh kumarad7722d2013-10-18 19:10:15 +05301519 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301520 up_read(&cpufreq_rwsem);
1521
Dave Jones4d34a672008-02-07 16:33:49 -05001522 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523}
1524EXPORT_SYMBOL(cpufreq_get);
1525
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001526static struct subsys_interface cpufreq_interface = {
1527 .name = "cpufreq",
1528 .subsys = &cpu_subsys,
1529 .add_dev = cpufreq_add_dev,
1530 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001531};
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001534 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1535 *
1536 * This function is only executed for the boot processor. The other CPUs
1537 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001538 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001539static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001540{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301541 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001542
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001543 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301544 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001545
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001546 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001547
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001548 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301549 policy = cpufreq_cpu_get(cpu);
1550 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001551 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001552
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001553 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301554 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001555 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001556 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301557 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001558 }
1559
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301560 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001561 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001562}
1563
1564/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001565 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 *
1567 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001568 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1569 * restored. It will verify that the current freq is in sync with
1570 * what we believe it to be. This is a bit later than when it
1571 * should be, but nonethteless it's better than calling
1572 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001573 *
1574 * This function is only executed for the boot CPU. The other CPUs have not
1575 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001577static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301579 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001580
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001581 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301582 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001584 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001586 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301587 policy = cpufreq_cpu_get(cpu);
1588 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001589 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001591 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301592 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (ret) {
1594 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301595 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001596 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
1598 }
1599
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301600 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001601
Dave Jonesc9060492008-02-07 16:32:18 -05001602fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301603 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001606static struct syscore_ops cpufreq_syscore_ops = {
1607 .suspend = cpufreq_bp_suspend,
1608 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609};
1610
Borislav Petkov9d950462013-01-20 10:24:28 +00001611/**
1612 * cpufreq_get_current_driver - return current driver's name
1613 *
1614 * Return the name string of the currently loaded cpufreq driver
1615 * or NULL, if none.
1616 */
1617const char *cpufreq_get_current_driver(void)
1618{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001619 if (cpufreq_driver)
1620 return cpufreq_driver->name;
1621
1622 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001623}
1624EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
1626/*********************************************************************
1627 * NOTIFIER LISTS INTERFACE *
1628 *********************************************************************/
1629
1630/**
1631 * cpufreq_register_notifier - register a driver with cpufreq
1632 * @nb: notifier function to register
1633 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1634 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001635 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 * are notified about clock rate changes (once before and once after
1637 * the transition), or a list of drivers that are notified about
1638 * changes in cpufreq policy.
1639 *
1640 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001641 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 */
1643int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1644{
1645 int ret;
1646
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001647 if (cpufreq_disabled())
1648 return -EINVAL;
1649
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001650 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 switch (list) {
1653 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001654 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001655 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 break;
1657 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001658 ret = blocking_notifier_chain_register(
1659 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 break;
1661 default:
1662 ret = -EINVAL;
1663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 return ret;
1666}
1667EXPORT_SYMBOL(cpufreq_register_notifier);
1668
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669/**
1670 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1671 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301672 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 *
1674 * Remove a driver from the CPU frequency notifier list.
1675 *
1676 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001677 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 */
1679int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1680{
1681 int ret;
1682
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001683 if (cpufreq_disabled())
1684 return -EINVAL;
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 switch (list) {
1687 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001688 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001689 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 break;
1691 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001692 ret = blocking_notifier_chain_unregister(
1693 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 break;
1695 default:
1696 ret = -EINVAL;
1697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 return ret;
1700}
1701EXPORT_SYMBOL(cpufreq_unregister_notifier);
1702
1703
1704/*********************************************************************
1705 * GOVERNORS *
1706 *********************************************************************/
1707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708int __cpufreq_driver_target(struct cpufreq_policy *policy,
1709 unsigned int target_freq,
1710 unsigned int relation)
1711{
1712 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001713 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001714
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001715 if (cpufreq_disabled())
1716 return -ENODEV;
1717
Viresh Kumar72499242012-10-31 01:28:21 +01001718 /* Make sure that target_freq is within supported range */
1719 if (target_freq > policy->max)
1720 target_freq = policy->max;
1721 if (target_freq < policy->min)
1722 target_freq = policy->min;
1723
1724 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1725 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001726
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301727 /*
1728 * This might look like a redundant call as we are checking it again
1729 * after finding index. But it is left intentionally for cases where
1730 * exactly same freq is called again and so we can save on few function
1731 * calls.
1732 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001733 if (target_freq == policy->cur)
1734 return 0;
1735
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001736 if (cpufreq_driver->target)
1737 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301738 else if (cpufreq_driver->target_index) {
1739 struct cpufreq_frequency_table *freq_table;
Viresh Kumard4019f02013-08-14 19:38:24 +05301740 struct cpufreq_freqs freqs;
1741 bool notify;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301742 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001743
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301744 freq_table = cpufreq_frequency_get_table(policy->cpu);
1745 if (unlikely(!freq_table)) {
1746 pr_err("%s: Unable to find freq_table\n", __func__);
1747 goto out;
1748 }
1749
1750 retval = cpufreq_frequency_table_target(policy, freq_table,
1751 target_freq, relation, &index);
1752 if (unlikely(retval)) {
1753 pr_err("%s: Unable to find matching freq\n", __func__);
1754 goto out;
1755 }
1756
Viresh Kumard4019f02013-08-14 19:38:24 +05301757 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301758 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301759 goto out;
1760 }
1761
1762 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1763
1764 if (notify) {
1765 freqs.old = policy->cur;
1766 freqs.new = freq_table[index].frequency;
1767 freqs.flags = 0;
1768
1769 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1770 __func__, policy->cpu, freqs.old,
1771 freqs.new);
1772
1773 cpufreq_notify_transition(policy, &freqs,
1774 CPUFREQ_PRECHANGE);
1775 }
1776
1777 retval = cpufreq_driver->target_index(policy, index);
1778 if (retval)
1779 pr_err("%s: Failed to change cpu frequency: %d\n",
1780 __func__, retval);
1781
Viresh Kumarab1b1c42013-12-02 11:04:13 +05301782 if (notify)
1783 cpufreq_notify_post_transition(policy, &freqs, retval);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301784 }
1785
1786out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 return retval;
1788}
1789EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791int cpufreq_driver_target(struct cpufreq_policy *policy,
1792 unsigned int target_freq,
1793 unsigned int relation)
1794{
Julia Lawallf1829e42008-07-25 22:44:53 +02001795 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
viresh kumarad7722d2013-10-18 19:10:15 +05301797 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 ret = __cpufreq_driver_target(policy, target_freq, relation);
1800
viresh kumarad7722d2013-10-18 19:10:15 +05301801 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return ret;
1804}
1805EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1806
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001807/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001808 * when "event" is CPUFREQ_GOV_LIMITS
1809 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301811static int __cpufreq_governor(struct cpufreq_policy *policy,
1812 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Dave Jonescc993ca2005-07-28 09:43:56 -07001814 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001815
1816 /* Only must be defined when default governor is known to have latency
1817 restrictions, like e.g. conservative or ondemand.
1818 That this is the case is already ensured in Kconfig
1819 */
1820#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1821 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1822#else
1823 struct cpufreq_governor *gov = NULL;
1824#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001825
1826 if (policy->governor->max_transition_latency &&
1827 policy->cpuinfo.transition_latency >
1828 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001829 if (!gov)
1830 return -EINVAL;
1831 else {
1832 printk(KERN_WARNING "%s governor failed, too long"
1833 " transition latency of HW, fallback"
1834 " to %s governor\n",
1835 policy->governor->name,
1836 gov->name);
1837 policy->governor = gov;
1838 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Viresh Kumarfe492f32013-08-06 22:53:10 +05301841 if (event == CPUFREQ_GOV_POLICY_INIT)
1842 if (!try_module_get(policy->governor->owner))
1843 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001845 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301846 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001847
1848 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301849 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301850 || (!policy->governor_enabled
1851 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001852 mutex_unlock(&cpufreq_governor_lock);
1853 return -EBUSY;
1854 }
1855
1856 if (event == CPUFREQ_GOV_STOP)
1857 policy->governor_enabled = false;
1858 else if (event == CPUFREQ_GOV_START)
1859 policy->governor_enabled = true;
1860
1861 mutex_unlock(&cpufreq_governor_lock);
1862
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 ret = policy->governor->governor(policy, event);
1864
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001865 if (!ret) {
1866 if (event == CPUFREQ_GOV_POLICY_INIT)
1867 policy->governor->initialized++;
1868 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1869 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001870 } else {
1871 /* Restore original values */
1872 mutex_lock(&cpufreq_governor_lock);
1873 if (event == CPUFREQ_GOV_STOP)
1874 policy->governor_enabled = true;
1875 else if (event == CPUFREQ_GOV_START)
1876 policy->governor_enabled = false;
1877 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001878 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001879
Viresh Kumarfe492f32013-08-06 22:53:10 +05301880 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1881 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 module_put(policy->governor->owner);
1883
1884 return ret;
1885}
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887int cpufreq_register_governor(struct cpufreq_governor *governor)
1888{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001889 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 if (!governor)
1892 return -EINVAL;
1893
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001894 if (cpufreq_disabled())
1895 return -ENODEV;
1896
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001897 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001898
Viresh Kumarb3940582013-02-01 05:42:58 +00001899 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001900 err = -EBUSY;
1901 if (__find_governor(governor->name) == NULL) {
1902 err = 0;
1903 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
Dave Jones32ee8c32006-02-28 00:43:23 -05001906 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001907 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1912{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001913#ifdef CONFIG_HOTPLUG_CPU
1914 int cpu;
1915#endif
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (!governor)
1918 return;
1919
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001920 if (cpufreq_disabled())
1921 return;
1922
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001923#ifdef CONFIG_HOTPLUG_CPU
1924 for_each_present_cpu(cpu) {
1925 if (cpu_online(cpu))
1926 continue;
1927 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1928 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1929 }
1930#endif
1931
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001932 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001934 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 return;
1936}
1937EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1938
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940/*********************************************************************
1941 * POLICY INTERFACE *
1942 *********************************************************************/
1943
1944/**
1945 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001946 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1947 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 *
1949 * Reads the current cpufreq policy.
1950 */
1951int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1952{
1953 struct cpufreq_policy *cpu_policy;
1954 if (!policy)
1955 return -EINVAL;
1956
1957 cpu_policy = cpufreq_cpu_get(cpu);
1958 if (!cpu_policy)
1959 return -EINVAL;
1960
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301961 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 return 0;
1965}
1966EXPORT_SYMBOL(cpufreq_get_policy);
1967
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001968/*
Viresh Kumar037ce832013-10-02 14:13:16 +05301969 * policy : current policy.
1970 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001971 */
Viresh Kumar037ce832013-10-02 14:13:16 +05301972static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301973 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001975 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301977 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1978 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301980 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301982 if (new_policy->min > policy->max || new_policy->max < policy->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001983 ret = -EINVAL;
1984 goto error_out;
1985 }
1986
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301988 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (ret)
1990 goto error_out;
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 /* adjust if necessary - all reasons */
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_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
1996 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001997 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301998 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Viresh Kumarbb176f72013-06-19 14:19:33 +05302000 /*
2001 * verify the cpu speed can be set within this limit, which might be
2002 * different to the first one
2003 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302004 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08002005 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08002009 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302010 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302012 policy->min = new_policy->min;
2013 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002015 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302016 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002018 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302019 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002020 pr_debug("setting range\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302021 ret = cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302023 if (new_policy->governor != policy->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 /* save old, working values */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302025 struct cpufreq_governor *old_gov = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002027 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029 /* end old governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302030 if (policy->governor) {
2031 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
viresh kumarad7722d2013-10-18 19:10:15 +05302032 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302033 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002034 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302035 down_write(&policy->rwsem);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
2038 /* start new governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302039 policy->governor = new_policy->governor;
2040 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2041 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002042 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00002043 } else {
viresh kumarad7722d2013-10-18 19:10:15 +05302044 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302045 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002046 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302047 down_write(&policy->rwsem);
Viresh Kumar955ef482013-05-16 05:09:58 +00002048 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002049 }
2050
2051 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002053 pr_debug("starting governor %s failed\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302054 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (old_gov) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302056 policy->governor = old_gov;
2057 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002058 CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302059 __cpufreq_governor(policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302060 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
2062 ret = -EINVAL;
2063 goto error_out;
2064 }
2065 /* might be a policy change, too, so fall through */
2066 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002067 pr_debug("governor: change or update limits\n");
Viresh Kumar3de9bde2013-08-06 22:53:13 +05302068 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070
Dave Jones7d5e3502006-02-02 17:03:42 -05002071error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 return ret;
2073}
2074
2075/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2077 * @cpu: CPU which shall be re-evaluated
2078 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002079 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 * at different times.
2081 */
2082int cpufreq_update_policy(unsigned int cpu)
2083{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302084 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2085 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002086 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302088 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02002089 ret = -ENODEV;
2090 goto no_policy;
2091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
viresh kumarad7722d2013-10-18 19:10:15 +05302093 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002095 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302096 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302097 new_policy.min = policy->user_policy.min;
2098 new_policy.max = policy->user_policy.max;
2099 new_policy.policy = policy->user_policy.policy;
2100 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Viresh Kumarbb176f72013-06-19 14:19:33 +05302102 /*
2103 * BIOS might change freq behind our back
2104 * -> ask driver for current freq and notify governors about a change
2105 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002106 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302107 new_policy.cur = cpufreq_driver->get(cpu);
2108 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002109 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302110 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002111 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302112 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302113 cpufreq_out_of_sync(cpu, policy->cur,
2114 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002115 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002116 }
2117
Viresh Kumar037ce832013-10-02 14:13:16 +05302118 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
viresh kumarad7722d2013-10-18 19:10:15 +05302120 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002121
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302122 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002123no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return ret;
2125}
2126EXPORT_SYMBOL(cpufreq_update_policy);
2127
Paul Gortmaker27609842013-06-19 13:54:04 -04002128static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002129 unsigned long action, void *hcpu)
2130{
2131 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002132 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302133 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002134
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002135 dev = get_cpu_device(cpu);
2136 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302137
Rafael J. Wysockid4faadd2013-12-08 01:23:58 +01002138 if (action & CPU_TASKS_FROZEN)
2139 frozen = true;
2140
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302141 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002142 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302143 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302144 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002145 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302146
Ashok Rajc32b6b82005-10-30 14:59:54 -08002147 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302148 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302149 break;
2150
2151 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302152 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002153 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302154
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002155 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302156 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002157 break;
2158 }
2159 }
2160 return NOTIFY_OK;
2161}
2162
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002163static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302164 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002165};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167/*********************************************************************
2168 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2169 *********************************************************************/
2170
2171/**
2172 * cpufreq_register_driver - register a CPU Frequency driver
2173 * @driver_data: A struct cpufreq_driver containing the values#
2174 * submitted by the CPU Frequency driver.
2175 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302176 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002178 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 *
2180 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002181int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
2183 unsigned long flags;
2184 int ret;
2185
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002186 if (cpufreq_disabled())
2187 return -ENODEV;
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302190 !(driver_data->setpolicy || driver_data->target_index ||
2191 driver_data->target))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return -EINVAL;
2193
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002194 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196 if (driver_data->setpolicy)
2197 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2198
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002199 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002200 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002201 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002202 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002204 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002205 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002207 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002208 if (ret)
2209 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002211 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 int i;
2213 ret = -ENODEV;
2214
2215 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002216 for (i = 0; i < nr_cpu_ids; i++)
2217 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002219 break;
2220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 /* if all ->init() calls failed, unregister */
2223 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002224 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302225 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002226 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
2228 }
2229
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002230 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002231 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002233 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002234err_if_unreg:
2235 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002236err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002237 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002238 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002239 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002240 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241}
2242EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244/**
2245 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2246 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302247 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 * the right to do so, i.e. if you have succeeded in initialising before!
2249 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2250 * currently not initialised.
2251 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002252int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
2254 unsigned long flags;
2255
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002256 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002259 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002261 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002262 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Viresh Kumar6eed9402013-08-06 22:53:11 +05302264 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002265 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302266
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002267 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302268
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002269 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302270 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 return 0;
2273}
2274EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002275
2276static int __init cpufreq_core_init(void)
2277{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002278 if (cpufreq_disabled())
2279 return -ENODEV;
2280
Viresh Kumar2361be22013-05-17 16:09:09 +05302281 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002282 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002283 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002284
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002285 return 0;
2286}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002287core_initcall(cpufreq_core_init);