blob: d533c205eea4a0298186c3f07f3ed6df368b69e8 [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 Kumarfcf80582013-01-29 14:39:08 +00001076 /* related cpus should atleast have policy->cpus */
1077 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1078
Viresh Kumar643ae6e2013-01-12 05:14:38 +00001079 /*
1080 * affected cpus must always be the one, which are online. We aren't
1081 * managing offline cpus here.
1082 */
1083 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1084
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301085 if (!frozen) {
1086 policy->user_policy.min = policy->min;
1087 policy->user_policy.max = policy->max;
1088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Thomas Renningera1531ac2008-07-29 22:32:58 -07001090 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1091 CPUFREQ_START, policy);
1092
Viresh Kumarfcf80582013-01-29 14:39:08 +00001093#ifdef CONFIG_HOTPLUG_CPU
1094 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1095 if (gov) {
1096 policy->governor = gov;
1097 pr_debug("Restoring governor %s for cpu %d\n",
1098 policy->governor->name, cpu);
Thomas Renninger4bfa0422009-07-24 15:25:03 +02001099 }
Viresh Kumarfcf80582013-01-29 14:39:08 +00001100#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301102 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301103 for_each_cpu(j, policy->cpus)
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301104 per_cpu(cpufreq_cpu_data, j) = policy;
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301105 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1106
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301107 if (!frozen) {
Viresh Kumar308b60e2013-07-31 14:35:14 +02001108 ret = cpufreq_add_dev_interface(policy, dev);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301109 if (ret)
1110 goto err_out_unregister;
1111 }
Dave Jones8ff69732006-03-05 03:37:23 -05001112
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301113 write_lock_irqsave(&cpufreq_driver_lock, flags);
1114 list_add(&policy->policy_list, &cpufreq_policy_list);
1115 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1116
Srivatsa S. Bhate18f1682013-07-30 04:24:23 +05301117 cpufreq_init_policy(policy);
1118
Viresh Kumar08fd8c1c2013-12-24 07:11:01 +05301119 if (!frozen) {
1120 policy->user_policy.policy = policy->policy;
1121 policy->user_policy.governor = policy->governor;
1122 }
1123
Greg Kroah-Hartman038c5b32007-12-17 15:54:39 -04001124 kobject_uevent(&policy->kobj, KOBJ_ADD);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301125 up_read(&cpufreq_rwsem);
1126
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001127 pr_debug("initialization complete\n");
Dave Jones87c32272006-03-29 01:48:37 -05001128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return 0;
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131err_out_unregister:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001132 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar474deff2013-08-20 12:08:25 +05301133 for_each_cpu(j, policy->cpus)
Mike Travis7a6aedf2008-03-25 15:06:53 -07001134 per_cpu(cpufreq_cpu_data, j) = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001135 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Viresh Kumarda60ce92013-10-03 20:28:30 +05301137err_get_freq:
1138 if (cpufreq_driver->exit)
1139 cpufreq_driver->exit(policy);
Viresh Kumar2eaa3e22013-02-07 10:55:00 +05301140err_set_policy_cpu:
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001141 if (frozen) {
1142 /* Do not leave stale fallback data behind. */
1143 per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
Viresh Kumar42f921a2013-12-20 21:26:02 +05301144 cpufreq_policy_put_kobj(policy);
Rafael J. Wysocki72368d12013-12-27 01:07:11 +01001145 }
Srivatsa S. Bhate9698cc2013-07-30 04:24:11 +05301146 cpufreq_policy_free(policy);
Viresh Kumar42f921a2013-12-20 21:26:02 +05301147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148nomem_out:
Viresh Kumar6eed9402013-08-06 22:53:11 +05301149 up_read(&cpufreq_rwsem);
1150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return ret;
1152}
1153
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301154/**
1155 * cpufreq_add_dev - add a CPU device
1156 *
1157 * Adds the cpufreq interface for a CPU device.
1158 *
1159 * The Oracle says: try running cpufreq registration/unregistration concurrently
1160 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1161 * mess up, but more thorough testing is needed. - Mathieu
1162 */
1163static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1164{
1165 return __cpufreq_add_dev(dev, sif, false);
1166}
1167
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301168static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
Viresh Kumar42f921a2013-12-20 21:26:02 +05301169 unsigned int old_cpu)
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301170{
1171 struct device *cpu_dev;
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301172 int ret;
1173
1174 /* first sibling now owns the new sysfs dir */
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301175 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301176
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301177 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301178 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301179 if (ret) {
1180 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1181
viresh kumarad7722d2013-10-18 19:10:15 +05301182 down_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301183 cpumask_set_cpu(old_cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301184 up_write(&policy->rwsem);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301185
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301186 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301187 "cpufreq");
1188
1189 return -EINVAL;
1190 }
1191
1192 return cpu_dev->id;
1193}
1194
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301195static int __cpufreq_remove_dev_prepare(struct device *dev,
1196 struct subsys_interface *sif,
1197 bool frozen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301199 unsigned int cpu = dev->id, cpus;
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301200 int new_cpu, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 unsigned long flags;
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301202 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001204 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001206 write_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301208 policy = per_cpu(cpufreq_cpu_data, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301210 /* Save the policy somewhere when doing a light-weight tear-down */
1211 if (frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301212 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301213
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00001214 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301216 if (!policy) {
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001217 pr_debug("%s: No cpu_data found\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301221 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301222 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1223 if (ret) {
1224 pr_err("%s: Failed to stop governor\n", __func__);
1225 return ret;
1226 }
1227 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001228
Jacob Shin27ecddc2011-04-27 13:32:11 -05001229#ifdef CONFIG_HOTPLUG_CPU
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001230 if (!cpufreq_driver->setpolicy)
Dirk Brandewiefa69e332013-02-06 09:02:11 -08001231 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301232 policy->governor->name, CPUFREQ_NAME_LEN);
Jacob Shin27ecddc2011-04-27 13:32:11 -05001233#endif
1234
viresh kumarad7722d2013-10-18 19:10:15 +05301235 down_read(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301236 cpus = cpumask_weight(policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301237 up_read(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Srivatsa S. Bhat61173f22013-09-12 01:43:25 +05301239 if (cpu != policy->cpu) {
1240 if (!frozen)
1241 sysfs_remove_link(&dev->kobj, "cpufreq");
Viresh Kumar73bf0fc2013-02-05 22:21:14 +01001242 } else if (cpus > 1) {
Viresh Kumar42f921a2013-12-20 21:26:02 +05301243 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
Srivatsa S. Bhatf9ba6802013-07-30 04:24:36 +05301244 if (new_cpu >= 0) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301245 update_policy_cpu(policy, new_cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301246
1247 if (!frozen) {
Viresh Kumar75949c92013-10-02 14:13:13 +05301248 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1249 __func__, new_cpu, cpu);
Srivatsa S. Bhata82fab22013-07-30 04:24:49 +05301250 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001251 }
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001252 }
Venki Pallipadiec282972007-03-26 12:03:19 -07001253
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301254 return 0;
1255}
1256
1257static int __cpufreq_remove_dev_finish(struct device *dev,
1258 struct subsys_interface *sif,
1259 bool frozen)
1260{
1261 unsigned int cpu = dev->id, cpus;
1262 int ret;
1263 unsigned long flags;
1264 struct cpufreq_policy *policy;
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301265
1266 read_lock_irqsave(&cpufreq_driver_lock, flags);
1267 policy = per_cpu(cpufreq_cpu_data, cpu);
1268 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1269
1270 if (!policy) {
1271 pr_debug("%s: No cpu_data found\n", __func__);
1272 return -EINVAL;
1273 }
1274
viresh kumarad7722d2013-10-18 19:10:15 +05301275 down_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301276 cpus = cpumask_weight(policy->cpus);
Viresh Kumar9c8f1ee2013-09-12 17:06:33 +05301277
1278 if (cpus > 1)
1279 cpumask_clear_cpu(cpu, policy->cpus);
viresh kumarad7722d2013-10-18 19:10:15 +05301280 up_write(&policy->rwsem);
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301281
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001282 /* If cpu is last user of policy, free policy */
1283 if (cpus == 1) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301284 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301285 ret = __cpufreq_governor(policy,
1286 CPUFREQ_GOV_POLICY_EXIT);
1287 if (ret) {
1288 pr_err("%s: Failed to exit governor\n",
1289 __func__);
1290 return ret;
1291 }
Viresh Kumaredab2fb2013-08-20 12:08:22 +05301292 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001293
Viresh Kumar42f921a2013-12-20 21:26:02 +05301294 if (!frozen)
1295 cpufreq_policy_put_kobj(policy);
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301296
1297 /*
1298 * Perform the ->exit() even during light-weight tear-down,
1299 * since this is a core component, and is essential for the
1300 * subsequent light-weight ->init() to succeed.
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001301 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001302 if (cpufreq_driver->exit)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301303 cpufreq_driver->exit(policy);
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001304
Viresh Kumar9515f4d2013-08-20 12:08:23 +05301305 /* Remove policy from list of active policies */
1306 write_lock_irqsave(&cpufreq_driver_lock, flags);
1307 list_del(&policy->policy_list);
1308 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1309
Srivatsa S. Bhat84148092013-07-30 04:25:10 +05301310 if (!frozen)
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301311 cpufreq_policy_free(policy);
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001312 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301313 if (has_target()) {
Viresh Kumar3de9bde2013-08-06 22:53:13 +05301314 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1315 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1316 pr_err("%s: Failed to start governor\n",
1317 __func__);
1318 return ret;
1319 }
Rafael J. Wysocki2a998592013-07-30 00:32:00 +02001320 }
Viresh Kumarb8eed8a2013-01-14 13:23:03 +00001321 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Viresh Kumar474deff2013-08-20 12:08:25 +05301323 per_cpu(cpufreq_cpu_data, cpu) = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325}
1326
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301327/**
Viresh Kumar27a862e2013-10-02 14:13:14 +05301328 * cpufreq_remove_dev - remove a CPU device
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301329 *
1330 * Removes the cpufreq interface for a CPU device.
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05301331 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001332static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001333{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001334 unsigned int cpu = dev->id;
Viresh Kumar27a862e2013-10-02 14:13:14 +05301335 int ret;
Venki Pallipadiec282972007-03-26 12:03:19 -07001336
1337 if (cpu_is_offline(cpu))
1338 return 0;
1339
Viresh Kumar27a862e2013-10-02 14:13:14 +05301340 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1341
1342 if (!ret)
1343 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1344
1345 return ret;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001346}
1347
David Howells65f27f32006-11-22 14:55:48 +00001348static void handle_update(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
David Howells65f27f32006-11-22 14:55:48 +00001350 struct cpufreq_policy *policy =
1351 container_of(work, struct cpufreq_policy, update);
1352 unsigned int cpu = policy->cpu;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001353 pr_debug("handle_update for cpu %u called\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 cpufreq_update_policy(cpu);
1355}
1356
1357/**
Viresh Kumarbb176f72013-06-19 14:19:33 +05301358 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1359 * in deep trouble.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 * @cpu: cpu number
1361 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1362 * @new_freq: CPU frequency the CPU actually runs at
1363 *
Dave Jones29464f22009-01-18 01:37:11 -05001364 * We adjust to current frequency first, and need to clean up later.
1365 * So either call to cpufreq_update_policy() or schedule handle_update()).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 */
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301367static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1368 unsigned int new_freq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301370 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 struct cpufreq_freqs freqs;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301372 unsigned long flags;
1373
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001374 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1376
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 freqs.old = old_freq;
1378 freqs.new = new_freq;
Viresh Kumarb43a7ff2013-03-24 11:56:43 +05301379
1380 read_lock_irqsave(&cpufreq_driver_lock, flags);
1381 policy = per_cpu(cpufreq_cpu_data, cpu);
1382 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1383
1384 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1385 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Dave Jones32ee8c32006-02-28 00:43:23 -05001388/**
Dhaval Giani4ab70df2006-12-13 14:49:15 +05301389 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001390 * @cpu: CPU number
1391 *
1392 * This is the last known freq, without actually getting it from the driver.
1393 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1394 */
1395unsigned int cpufreq_quick_get(unsigned int cpu)
1396{
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001397 struct cpufreq_policy *policy;
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301398 unsigned int ret_freq = 0;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001399
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001400 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1401 return cpufreq_driver->get(cpu);
Dirk Brandewie9e21ba82013-02-06 09:02:08 -08001402
1403 policy = cpufreq_cpu_get(cpu);
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001404 if (policy) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301405 ret_freq = policy->cur;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001406 cpufreq_cpu_put(policy);
1407 }
1408
Dave Jones4d34a672008-02-07 16:33:49 -05001409 return ret_freq;
Venkatesh Pallipadi95235ca2005-12-02 10:43:20 -08001410}
1411EXPORT_SYMBOL(cpufreq_quick_get);
1412
Jesse Barnes3d737102011-06-28 10:59:12 -07001413/**
1414 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1415 * @cpu: CPU number
1416 *
1417 * Just return the max possible frequency for a given CPU.
1418 */
1419unsigned int cpufreq_quick_get_max(unsigned int cpu)
1420{
1421 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1422 unsigned int ret_freq = 0;
1423
1424 if (policy) {
1425 ret_freq = policy->max;
1426 cpufreq_cpu_put(policy);
1427 }
1428
1429 return ret_freq;
1430}
1431EXPORT_SYMBOL(cpufreq_quick_get_max);
1432
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001433static unsigned int __cpufreq_get(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Mike Travis7a6aedf2008-03-25 15:06:53 -07001435 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301436 unsigned int ret_freq = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001438 if (!cpufreq_driver->get)
Dave Jones4d34a672008-02-07 16:33:49 -05001439 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001441 ret_freq = cpufreq_driver->get(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301443 if (ret_freq && policy->cur &&
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001444 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301445 /* verify no discrepancy between actual and
1446 saved value exists */
1447 if (unlikely(ret_freq != policy->cur)) {
1448 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 schedule_work(&policy->update);
1450 }
1451 }
1452
Dave Jones4d34a672008-02-07 16:33:49 -05001453 return ret_freq;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001456/**
1457 * cpufreq_get - get the current CPU frequency (in kHz)
1458 * @cpu: CPU number
1459 *
1460 * Get the CPU current (static) CPU frequency
1461 */
1462unsigned int cpufreq_get(unsigned int cpu)
1463{
viresh kumarad7722d2013-10-18 19:10:15 +05301464 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001465 unsigned int ret_freq = 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001466
Viresh Kumar26ca8692013-09-20 22:37:31 +05301467 if (cpufreq_disabled() || !cpufreq_driver)
1468 return -ENOENT;
1469
viresh kumarad7722d2013-10-18 19:10:15 +05301470 BUG_ON(!policy);
1471
Viresh Kumar6eed9402013-08-06 22:53:11 +05301472 if (!down_read_trylock(&cpufreq_rwsem))
1473 return 0;
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001474
viresh kumarad7722d2013-10-18 19:10:15 +05301475 down_read(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08001476
1477 ret_freq = __cpufreq_get(cpu);
1478
viresh kumarad7722d2013-10-18 19:10:15 +05301479 up_read(&policy->rwsem);
Viresh Kumar6eed9402013-08-06 22:53:11 +05301480 up_read(&cpufreq_rwsem);
1481
Dave Jones4d34a672008-02-07 16:33:49 -05001482 return ret_freq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483}
1484EXPORT_SYMBOL(cpufreq_get);
1485
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001486static struct subsys_interface cpufreq_interface = {
1487 .name = "cpufreq",
1488 .subsys = &cpu_subsys,
1489 .add_dev = cpufreq_add_dev,
1490 .remove_dev = cpufreq_remove_dev,
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001491};
1492
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001494 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1495 *
1496 * This function is only executed for the boot processor. The other CPUs
1497 * have been put offline by means of CPU hotplug.
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001498 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001499static int cpufreq_bp_suspend(void)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001500{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301501 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001502
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001503 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301504 struct cpufreq_policy *policy;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001505
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001506 pr_debug("suspending cpu %u\n", cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001507
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001508 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301509 policy = cpufreq_cpu_get(cpu);
1510 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001511 return 0;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001512
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001513 if (cpufreq_driver->suspend) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301514 ret = cpufreq_driver->suspend(policy);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001515 if (ret)
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001516 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301517 "step on CPU %u\n", policy->cpu);
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001518 }
1519
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301520 cpufreq_cpu_put(policy);
Dave Jonesc9060492008-02-07 16:32:18 -05001521 return ret;
Benjamin Herrenschmidt42d4dc32005-04-29 07:40:12 -07001522}
1523
1524/**
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001525 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 *
1527 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001528 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1529 * restored. It will verify that the current freq is in sync with
1530 * what we believe it to be. This is a bit later than when it
1531 * should be, but nonethteless it's better than calling
1532 * cpufreq_driver->get() here which might re-enable interrupts...
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001533 *
1534 * This function is only executed for the boot CPU. The other CPUs have not
1535 * been turned on yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 */
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001537static void cpufreq_bp_resume(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301539 int ret = 0;
Dave Jones4bc5d342009-08-04 14:03:25 -04001540
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001541 int cpu = smp_processor_id();
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301542 struct cpufreq_policy *policy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001544 pr_debug("resuming cpu %u\n", cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001546 /* If there's no policy for the boot CPU, we have nothing to do. */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301547 policy = cpufreq_cpu_get(cpu);
1548 if (!policy)
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001549 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001551 if (cpufreq_driver->resume) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301552 ret = cpufreq_driver->resume(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (ret) {
1554 printk(KERN_ERR "cpufreq: resume failed in ->resume "
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301555 "step on CPU %u\n", policy->cpu);
Dave Jonesc9060492008-02-07 16:32:18 -05001556 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558 }
1559
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301560 schedule_work(&policy->update);
Dominik Brodowskice6c3992009-08-07 22:58:51 +02001561
Dave Jonesc9060492008-02-07 16:32:18 -05001562fail:
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301563 cpufreq_cpu_put(policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01001566static struct syscore_ops cpufreq_syscore_ops = {
1567 .suspend = cpufreq_bp_suspend,
1568 .resume = cpufreq_bp_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569};
1570
Borislav Petkov9d950462013-01-20 10:24:28 +00001571/**
1572 * cpufreq_get_current_driver - return current driver's name
1573 *
1574 * Return the name string of the currently loaded cpufreq driver
1575 * or NULL, if none.
1576 */
1577const char *cpufreq_get_current_driver(void)
1578{
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001579 if (cpufreq_driver)
1580 return cpufreq_driver->name;
1581
1582 return NULL;
Borislav Petkov9d950462013-01-20 10:24:28 +00001583}
1584EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
1586/*********************************************************************
1587 * NOTIFIER LISTS INTERFACE *
1588 *********************************************************************/
1589
1590/**
1591 * cpufreq_register_notifier - register a driver with cpufreq
1592 * @nb: notifier function to register
1593 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1594 *
Dave Jones32ee8c32006-02-28 00:43:23 -05001595 * Add a driver to one of two lists: either a list of drivers that
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 * are notified about clock rate changes (once before and once after
1597 * the transition), or a list of drivers that are notified about
1598 * changes in cpufreq policy.
1599 *
1600 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001601 * blocking_notifier_chain_register.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 */
1603int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1604{
1605 int ret;
1606
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001607 if (cpufreq_disabled())
1608 return -EINVAL;
1609
Cesar Eduardo Barros74212ca2008-02-16 08:41:24 -02001610 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 switch (list) {
1613 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001614 ret = srcu_notifier_chain_register(
Alan Sterne041c682006-03-27 01:16:30 -08001615 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 break;
1617 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001618 ret = blocking_notifier_chain_register(
1619 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 break;
1621 default:
1622 ret = -EINVAL;
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 return ret;
1626}
1627EXPORT_SYMBOL(cpufreq_register_notifier);
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/**
1630 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1631 * @nb: notifier block to be unregistered
Viresh Kumarbb176f72013-06-19 14:19:33 +05301632 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 *
1634 * Remove a driver from the CPU frequency notifier list.
1635 *
1636 * This function may sleep, and has the same return conditions as
Alan Sterne041c682006-03-27 01:16:30 -08001637 * blocking_notifier_chain_unregister.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 */
1639int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1640{
1641 int ret;
1642
Dirk Brandewied5aaffa2013-01-17 16:22:21 +00001643 if (cpufreq_disabled())
1644 return -EINVAL;
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 switch (list) {
1647 case CPUFREQ_TRANSITION_NOTIFIER:
Alan Sternb4dfdbb2006-10-04 02:17:06 -07001648 ret = srcu_notifier_chain_unregister(
Alan Sterne041c682006-03-27 01:16:30 -08001649 &cpufreq_transition_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 break;
1651 case CPUFREQ_POLICY_NOTIFIER:
Alan Sterne041c682006-03-27 01:16:30 -08001652 ret = blocking_notifier_chain_unregister(
1653 &cpufreq_policy_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 break;
1655 default:
1656 ret = -EINVAL;
1657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 return ret;
1660}
1661EXPORT_SYMBOL(cpufreq_unregister_notifier);
1662
1663
1664/*********************************************************************
1665 * GOVERNORS *
1666 *********************************************************************/
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668int __cpufreq_driver_target(struct cpufreq_policy *policy,
1669 unsigned int target_freq,
1670 unsigned int relation)
1671{
1672 int retval = -EINVAL;
Viresh Kumar72499242012-10-31 01:28:21 +01001673 unsigned int old_target_freq = target_freq;
Ashok Rajc32b6b82005-10-30 14:59:54 -08001674
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001675 if (cpufreq_disabled())
1676 return -ENODEV;
1677
Viresh Kumar72499242012-10-31 01:28:21 +01001678 /* Make sure that target_freq is within supported range */
1679 if (target_freq > policy->max)
1680 target_freq = policy->max;
1681 if (target_freq < policy->min)
1682 target_freq = policy->min;
1683
1684 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1685 policy->cpu, target_freq, relation, old_target_freq);
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001686
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301687 /*
1688 * This might look like a redundant call as we are checking it again
1689 * after finding index. But it is left intentionally for cases where
1690 * exactly same freq is called again and so we can save on few function
1691 * calls.
1692 */
Viresh Kumar5a1c0222012-10-31 01:28:15 +01001693 if (target_freq == policy->cur)
1694 return 0;
1695
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001696 if (cpufreq_driver->target)
1697 retval = cpufreq_driver->target(policy, target_freq, relation);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301698 else if (cpufreq_driver->target_index) {
1699 struct cpufreq_frequency_table *freq_table;
Viresh Kumard4019f02013-08-14 19:38:24 +05301700 struct cpufreq_freqs freqs;
1701 bool notify;
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301702 int index;
Ashok Raj90d45d12005-11-08 21:34:24 -08001703
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301704 freq_table = cpufreq_frequency_get_table(policy->cpu);
1705 if (unlikely(!freq_table)) {
1706 pr_err("%s: Unable to find freq_table\n", __func__);
1707 goto out;
1708 }
1709
1710 retval = cpufreq_frequency_table_target(policy, freq_table,
1711 target_freq, relation, &index);
1712 if (unlikely(retval)) {
1713 pr_err("%s: Unable to find matching freq\n", __func__);
1714 goto out;
1715 }
1716
Viresh Kumard4019f02013-08-14 19:38:24 +05301717 if (freq_table[index].frequency == policy->cur) {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301718 retval = 0;
Viresh Kumard4019f02013-08-14 19:38:24 +05301719 goto out;
1720 }
1721
1722 notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
1723
1724 if (notify) {
1725 freqs.old = policy->cur;
1726 freqs.new = freq_table[index].frequency;
1727 freqs.flags = 0;
1728
1729 pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
1730 __func__, policy->cpu, freqs.old,
1731 freqs.new);
1732
1733 cpufreq_notify_transition(policy, &freqs,
1734 CPUFREQ_PRECHANGE);
1735 }
1736
1737 retval = cpufreq_driver->target_index(policy, index);
1738 if (retval)
1739 pr_err("%s: Failed to change cpu frequency: %d\n",
1740 __func__, retval);
1741
Viresh Kumarab1b1c42013-12-02 11:04:13 +05301742 if (notify)
1743 cpufreq_notify_post_transition(policy, &freqs, retval);
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05301744 }
1745
1746out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 return retval;
1748}
1749EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751int cpufreq_driver_target(struct cpufreq_policy *policy,
1752 unsigned int target_freq,
1753 unsigned int relation)
1754{
Julia Lawallf1829e42008-07-25 22:44:53 +02001755 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
viresh kumarad7722d2013-10-18 19:10:15 +05301757 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 ret = __cpufreq_driver_target(policy, target_freq, relation);
1760
viresh kumarad7722d2013-10-18 19:10:15 +05301761 up_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return ret;
1764}
1765EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1766
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001767/*
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001768 * when "event" is CPUFREQ_GOV_LIMITS
1769 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301771static int __cpufreq_governor(struct cpufreq_policy *policy,
1772 unsigned int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Dave Jonescc993ca2005-07-28 09:43:56 -07001774 int ret;
Thomas Renninger6afde102007-10-02 13:28:13 -07001775
1776 /* Only must be defined when default governor is known to have latency
1777 restrictions, like e.g. conservative or ondemand.
1778 That this is the case is already ensured in Kconfig
1779 */
1780#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1781 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1782#else
1783 struct cpufreq_governor *gov = NULL;
1784#endif
Thomas Renninger1c256242007-10-02 13:28:12 -07001785
1786 if (policy->governor->max_transition_latency &&
1787 policy->cpuinfo.transition_latency >
1788 policy->governor->max_transition_latency) {
Thomas Renninger6afde102007-10-02 13:28:13 -07001789 if (!gov)
1790 return -EINVAL;
1791 else {
1792 printk(KERN_WARNING "%s governor failed, too long"
1793 " transition latency of HW, fallback"
1794 " to %s governor\n",
1795 policy->governor->name,
1796 gov->name);
1797 policy->governor = gov;
1798 }
Thomas Renninger1c256242007-10-02 13:28:12 -07001799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Viresh Kumarfe492f32013-08-06 22:53:10 +05301801 if (event == CPUFREQ_GOV_POLICY_INIT)
1802 if (!try_module_get(policy->governor->owner))
1803 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001805 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05301806 policy->cpu, event);
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001807
1808 mutex_lock(&cpufreq_governor_lock);
Srivatsa S. Bhat56d07db2013-09-07 01:23:55 +05301809 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
Viresh Kumarf73d3932013-08-31 17:53:40 +05301810 || (!policy->governor_enabled
1811 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001812 mutex_unlock(&cpufreq_governor_lock);
1813 return -EBUSY;
1814 }
1815
1816 if (event == CPUFREQ_GOV_STOP)
1817 policy->governor_enabled = false;
1818 else if (event == CPUFREQ_GOV_START)
1819 policy->governor_enabled = true;
1820
1821 mutex_unlock(&cpufreq_governor_lock);
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 ret = policy->governor->governor(policy, event);
1824
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001825 if (!ret) {
1826 if (event == CPUFREQ_GOV_POLICY_INIT)
1827 policy->governor->initialized++;
1828 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1829 policy->governor->initialized--;
Xiaoguang Chen95731eb2013-06-19 15:00:07 +08001830 } else {
1831 /* Restore original values */
1832 mutex_lock(&cpufreq_governor_lock);
1833 if (event == CPUFREQ_GOV_STOP)
1834 policy->governor_enabled = true;
1835 else if (event == CPUFREQ_GOV_START)
1836 policy->governor_enabled = false;
1837 mutex_unlock(&cpufreq_governor_lock);
Viresh Kumar4d5dcc42013-03-27 15:58:58 +00001838 }
Viresh Kumarb3940582013-02-01 05:42:58 +00001839
Viresh Kumarfe492f32013-08-06 22:53:10 +05301840 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1841 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 module_put(policy->governor->owner);
1843
1844 return ret;
1845}
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847int cpufreq_register_governor(struct cpufreq_governor *governor)
1848{
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001849 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 if (!governor)
1852 return -EINVAL;
1853
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001854 if (cpufreq_disabled())
1855 return -ENODEV;
1856
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001857 mutex_lock(&cpufreq_governor_mutex);
Dave Jones32ee8c32006-02-28 00:43:23 -05001858
Viresh Kumarb3940582013-02-01 05:42:58 +00001859 governor->initialized = 0;
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001860 err = -EBUSY;
1861 if (__find_governor(governor->name) == NULL) {
1862 err = 0;
1863 list_add(&governor->governor_list, &cpufreq_governor_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Dave Jones32ee8c32006-02-28 00:43:23 -05001866 mutex_unlock(&cpufreq_governor_mutex);
Jeremy Fitzhardinge3bcb09a2006-07-06 12:30:26 -07001867 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1872{
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001873#ifdef CONFIG_HOTPLUG_CPU
1874 int cpu;
1875#endif
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (!governor)
1878 return;
1879
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04001880 if (cpufreq_disabled())
1881 return;
1882
Prarit Bhargava90e41ba2009-11-12 09:18:46 -05001883#ifdef CONFIG_HOTPLUG_CPU
1884 for_each_present_cpu(cpu) {
1885 if (cpu_online(cpu))
1886 continue;
1887 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1888 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1889 }
1890#endif
1891
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001892 mutex_lock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 list_del(&governor->governor_list);
akpm@osdl.org3fc54d32006-01-13 15:54:22 -08001894 mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 return;
1896}
1897EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1898
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900/*********************************************************************
1901 * POLICY INTERFACE *
1902 *********************************************************************/
1903
1904/**
1905 * cpufreq_get_policy - get the current cpufreq_policy
Dave Jones29464f22009-01-18 01:37:11 -05001906 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1907 * is written
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 *
1909 * Reads the current cpufreq policy.
1910 */
1911int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1912{
1913 struct cpufreq_policy *cpu_policy;
1914 if (!policy)
1915 return -EINVAL;
1916
1917 cpu_policy = cpufreq_cpu_get(cpu);
1918 if (!cpu_policy)
1919 return -EINVAL;
1920
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301921 memcpy(policy, cpu_policy, sizeof(*policy));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 cpufreq_cpu_put(cpu_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 return 0;
1925}
1926EXPORT_SYMBOL(cpufreq_get_policy);
1927
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001928/*
Viresh Kumar037ce832013-10-02 14:13:16 +05301929 * policy : current policy.
1930 * new_policy: policy to be set.
Arjan van de Ven153d7f32006-07-26 15:40:07 +02001931 */
Viresh Kumar037ce832013-10-02 14:13:16 +05301932static int cpufreq_set_policy(struct cpufreq_policy *policy,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301933 struct cpufreq_policy *new_policy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001935 int ret = 0, failed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301937 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1938 new_policy->min, new_policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Viresh Kumard5b73cd2013-08-06 22:53:06 +05301940 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301942 if (new_policy->min > policy->max || new_policy->max < policy->min) {
Mattia Dongili9c9a43e2006-07-05 23:12:20 +02001943 ret = -EINVAL;
1944 goto error_out;
1945 }
1946
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 /* verify the cpu speed can be set within this limit */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301948 ret = cpufreq_driver->verify(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 if (ret)
1950 goto error_out;
1951
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 /* adjust if necessary - all reasons */
Alan Sterne041c682006-03-27 01:16:30 -08001953 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301954 CPUFREQ_ADJUST, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 /* adjust if necessary - hardware incompatibility*/
Alan Sterne041c682006-03-27 01:16:30 -08001957 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301958 CPUFREQ_INCOMPATIBLE, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Viresh Kumarbb176f72013-06-19 14:19:33 +05301960 /*
1961 * verify the cpu speed can be set within this limit, which might be
1962 * different to the first one
1963 */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301964 ret = cpufreq_driver->verify(new_policy);
Alan Sterne041c682006-03-27 01:16:30 -08001965 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 goto error_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
1968 /* notification of the new policy */
Alan Sterne041c682006-03-27 01:16:30 -08001969 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301970 CPUFREQ_NOTIFY, new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301972 policy->min = new_policy->min;
1973 policy->max = new_policy->max;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001975 pr_debug("new min and max freqs are %u - %u kHz\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301976 policy->min, policy->max);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02001978 if (cpufreq_driver->setpolicy) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301979 policy->policy = new_policy->policy;
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001980 pr_debug("setting range\n");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301981 ret = cpufreq_driver->setpolicy(new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 } else {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301983 if (new_policy->governor != policy->governor) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 /* save old, working values */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301985 struct cpufreq_governor *old_gov = policy->governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02001987 pr_debug("governor switch\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 /* end old governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301990 if (policy->governor) {
1991 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
viresh kumarad7722d2013-10-18 19:10:15 +05301992 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301993 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001994 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05301995 down_write(&policy->rwsem);
Viresh Kumar7bd353a2013-03-27 15:58:57 +00001996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* start new governor */
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05301999 policy->governor = new_policy->governor;
2000 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
2001 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002002 failed = 0;
Viresh Kumar955ef482013-05-16 05:09:58 +00002003 } else {
viresh kumarad7722d2013-10-18 19:10:15 +05302004 up_write(&policy->rwsem);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302005 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002006 CPUFREQ_GOV_POLICY_EXIT);
viresh kumarad7722d2013-10-18 19:10:15 +05302007 down_write(&policy->rwsem);
Viresh Kumar955ef482013-05-16 05:09:58 +00002008 }
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002009 }
2010
2011 if (failed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /* new governor failed, so re-start old one */
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002013 pr_debug("starting governor %s failed\n",
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302014 policy->governor->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (old_gov) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302016 policy->governor = old_gov;
2017 __cpufreq_governor(policy,
Viresh Kumar7bd353a2013-03-27 15:58:57 +00002018 CPUFREQ_GOV_POLICY_INIT);
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302019 __cpufreq_governor(policy,
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302020 CPUFREQ_GOV_START);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 ret = -EINVAL;
2023 goto error_out;
2024 }
2025 /* might be a policy change, too, so fall through */
2026 }
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002027 pr_debug("governor: change or update limits\n");
Viresh Kumar3de9bde2013-08-06 22:53:13 +05302028 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
2030
Dave Jones7d5e3502006-02-02 17:03:42 -05002031error_out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 return ret;
2033}
2034
2035/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2037 * @cpu: CPU which shall be re-evaluated
2038 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002039 * Useful for policy notifiers which have different necessities
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 * at different times.
2041 */
2042int cpufreq_update_policy(unsigned int cpu)
2043{
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302044 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2045 struct cpufreq_policy new_policy;
Julia Lawallf1829e42008-07-25 22:44:53 +02002046 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302048 if (!policy) {
Julia Lawallf1829e42008-07-25 22:44:53 +02002049 ret = -ENODEV;
2050 goto no_policy;
2051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
viresh kumarad7722d2013-10-18 19:10:15 +05302053 down_write(&policy->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002055 pr_debug("updating policy for CPU %u\n", cpu);
Viresh Kumard5b73cd2013-08-06 22:53:06 +05302056 memcpy(&new_policy, policy, sizeof(*policy));
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302057 new_policy.min = policy->user_policy.min;
2058 new_policy.max = policy->user_policy.max;
2059 new_policy.policy = policy->user_policy.policy;
2060 new_policy.governor = policy->user_policy.governor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Viresh Kumarbb176f72013-06-19 14:19:33 +05302062 /*
2063 * BIOS might change freq behind our back
2064 * -> ask driver for current freq and notify governors about a change
2065 */
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002066 if (cpufreq_driver->get) {
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302067 new_policy.cur = cpufreq_driver->get(cpu);
2068 if (!policy->cur) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002069 pr_debug("Driver did not initialize current freq");
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302070 policy->cur = new_policy.cur;
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002071 } else {
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302072 if (policy->cur != new_policy.cur && has_target())
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302073 cpufreq_out_of_sync(cpu, policy->cur,
2074 new_policy.cur);
Thomas Renningera85f7bd2006-02-01 11:36:04 +01002075 }
Thomas Renninger0961dd02006-01-26 18:46:33 +01002076 }
2077
Viresh Kumar037ce832013-10-02 14:13:16 +05302078 ret = cpufreq_set_policy(policy, &new_policy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
viresh kumarad7722d2013-10-18 19:10:15 +05302080 up_write(&policy->rwsem);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002081
Viresh Kumar3a3e9e02013-08-06 22:53:05 +05302082 cpufreq_cpu_put(policy);
Julia Lawallf1829e42008-07-25 22:44:53 +02002083no_policy:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return ret;
2085}
2086EXPORT_SYMBOL(cpufreq_update_policy);
2087
Paul Gortmaker27609842013-06-19 13:54:04 -04002088static int cpufreq_cpu_callback(struct notifier_block *nfb,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002089 unsigned long action, void *hcpu)
2090{
2091 unsigned int cpu = (unsigned long)hcpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002092 struct device *dev;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302093 bool frozen = false;
Ashok Rajc32b6b82005-10-30 14:59:54 -08002094
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002095 dev = get_cpu_device(cpu);
2096 if (dev) {
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302097
Rafael J. Wysockid4faadd2013-12-08 01:23:58 +01002098 if (action & CPU_TASKS_FROZEN)
2099 frozen = true;
2100
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302101 switch (action & ~CPU_TASKS_FROZEN) {
Ashok Rajc32b6b82005-10-30 14:59:54 -08002102 case CPU_ONLINE:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302103 __cpufreq_add_dev(dev, NULL, frozen);
Srivatsa S. Bhat23d328992013-07-30 04:23:56 +05302104 cpufreq_update_policy(cpu);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002105 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302106
Ashok Rajc32b6b82005-10-30 14:59:54 -08002107 case CPU_DOWN_PREPARE:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302108 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
Srivatsa S. Bhat1aee40a2013-09-07 01:23:27 +05302109 break;
2110
2111 case CPU_POST_DEAD:
Srivatsa S. Bhatcedb70a2013-09-07 01:23:09 +05302112 __cpufreq_remove_dev_finish(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002113 break;
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302114
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002115 case CPU_DOWN_FAILED:
Srivatsa S. Bhat5302c3f2013-07-30 04:25:25 +05302116 __cpufreq_add_dev(dev, NULL, frozen);
Ashok Rajc32b6b82005-10-30 14:59:54 -08002117 break;
2118 }
2119 }
2120 return NOTIFY_OK;
2121}
2122
Neal Buckendahl9c36f742010-06-22 22:02:44 -05002123static struct notifier_block __refdata cpufreq_cpu_notifier = {
Viresh Kumarbb176f72013-06-19 14:19:33 +05302124 .notifier_call = cpufreq_cpu_callback,
Ashok Rajc32b6b82005-10-30 14:59:54 -08002125};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127/*********************************************************************
2128 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2129 *********************************************************************/
2130
2131/**
2132 * cpufreq_register_driver - register a CPU Frequency driver
2133 * @driver_data: A struct cpufreq_driver containing the values#
2134 * submitted by the CPU Frequency driver.
2135 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302136 * Registers a CPU Frequency driver to this core code. This code
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 * returns zero on success, -EBUSY when another driver got here first
Dave Jones32ee8c32006-02-28 00:43:23 -05002138 * (and isn't unregistered in the meantime).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 *
2140 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002141int cpufreq_register_driver(struct cpufreq_driver *driver_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
2143 unsigned long flags;
2144 int ret;
2145
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002146 if (cpufreq_disabled())
2147 return -ENODEV;
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (!driver_data || !driver_data->verify || !driver_data->init ||
Viresh Kumar9c0ebcf2013-10-25 19:45:48 +05302150 !(driver_data->setpolicy || driver_data->target_index ||
2151 driver_data->target))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return -EINVAL;
2153
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002154 pr_debug("trying to register driver %s\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 if (driver_data->setpolicy)
2157 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2158
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002159 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002160 if (cpufreq_driver) {
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002161 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Yinghai Lu4dea58062013-09-18 21:05:20 -07002162 return -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002164 cpufreq_driver = driver_data;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002165 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002167 ret = subsys_interface_register(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002168 if (ret)
2169 goto err_null_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002171 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 int i;
2173 ret = -ENODEV;
2174
2175 /* check for at least one working CPU */
Mike Travis7a6aedf2008-03-25 15:06:53 -07002176 for (i = 0; i < nr_cpu_ids; i++)
2177 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 ret = 0;
Mike Travis7a6aedf2008-03-25 15:06:53 -07002179 break;
2180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
2182 /* if all ->init() calls failed, unregister */
2183 if (ret) {
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002184 pr_debug("no CPU initialized for driver %s\n",
Gautham R Shenoye08f5f52006-10-26 16:20:58 +05302185 driver_data->name);
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002186 goto err_if_unreg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
2188 }
2189
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002190 register_hotcpu_notifier(&cpufreq_cpu_notifier);
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002191 pr_debug("driver %s up and running\n", driver_data->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002193 return 0;
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002194err_if_unreg:
2195 subsys_interface_unregister(&cpufreq_interface);
Jiri Slaby8f5bc2a2011-03-01 17:41:10 +01002196err_null_driver:
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002197 write_lock_irqsave(&cpufreq_driver_lock, flags);
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002198 cpufreq_driver = NULL;
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002199 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Dave Jones4d34a672008-02-07 16:33:49 -05002200 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204/**
2205 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2206 *
Viresh Kumarbb176f72013-06-19 14:19:33 +05302207 * Unregister the current CPUFreq driver. Only call this if you have
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 * the right to do so, i.e. if you have succeeded in initialising before!
2209 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2210 * currently not initialised.
2211 */
Linus Torvalds221dee22007-02-26 14:55:48 -08002212int cpufreq_unregister_driver(struct cpufreq_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
2214 unsigned long flags;
2215
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002216 if (!cpufreq_driver || (driver != cpufreq_driver))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Dominik Brodowski2d06d8c2011-03-27 15:04:46 +02002219 pr_debug("unregistering driver %s\n", driver->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002221 subsys_interface_unregister(&cpufreq_interface);
Chandra Seetharaman65edc682006-06-27 02:54:08 -07002222 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Viresh Kumar6eed9402013-08-06 22:53:11 +05302224 down_write(&cpufreq_rwsem);
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002225 write_lock_irqsave(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302226
Rafael J. Wysocki1c3d85d2013-04-29 00:08:16 +02002227 cpufreq_driver = NULL;
Viresh Kumar6eed9402013-08-06 22:53:11 +05302228
Nathan Zimmer0d1857a2013-02-22 16:24:34 +00002229 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
Viresh Kumar6eed9402013-08-06 22:53:11 +05302230 up_write(&cpufreq_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 return 0;
2233}
2234EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002235
2236static int __init cpufreq_core_init(void)
2237{
Konrad Rzeszutek Wilka7b422c2012-03-13 19:18:39 -04002238 if (cpufreq_disabled())
2239 return -ENODEV;
2240
Viresh Kumar2361be22013-05-17 16:09:09 +05302241 cpufreq_global_kobject = kobject_create();
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002242 BUG_ON(!cpufreq_global_kobject);
Rafael J. Wysockie00e56d2011-03-23 22:16:32 +01002243 register_syscore_ops(&cpufreq_syscore_ops);
Thomas Renninger8aa84ad2009-07-24 15:25:05 +02002244
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002245 return 0;
2246}
Venkatesh Pallipadi5a01f2e2007-02-05 16:12:44 -08002247core_initcall(cpufreq_core_init);